Skip to content

Commit

Permalink
Test some ways to unstructure xarray data
Browse files Browse the repository at this point in the history
  • Loading branch information
deltamarnix committed Dec 12, 2024
1 parent 45702d6 commit 4ef3d93
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions test/test_cattrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np
import xarray as xr
from attrs import define, field
from cattrs import Converter, unstructure
from numpy.typing import NDArray


@define
class Foo:
x: NDArray[np.float32] = field()


@define
class Bar:
x: xr.DataArray = field()


@define
class Baz:
x: xr.DataTree = field()


def test_unstructure_numpy_array():
np_arr = np.array([1.0, 2.0, 3.0])
f = Foo(x=np_arr)
f_dict = unstructure(f)

# We expect that the default unstructure functionality keeps the numpy array as is.
# This helps when finally converting the dictionary to MF6 input files.
assert np_arr is f_dict["x"]


def test_unstructure_xarray():
x_arr = xr.DataArray([1, 2, 3])
f = Bar(x=x_arr)
f_dict = unstructure(f)

# We expect that the default unstructure functionality keeps the xarray as is.
# This helps when finally converting the dictionary to MF6 input files.
assert x_arr is f_dict["x"]


def test_unstructure_xarray_tree_to_ascii():
x_arr = xr.DataArray([1, 2, 3])
x_set = xr.Dataset({"x": x_arr})
x_tree = xr.DataTree(x_set)
f = Baz(x=x_tree)

converter = Converter()
converter.register_unstructure_hook(
Baz, lambda b: " ".join(b.x["x"].data.astype(str))
)
f_dict = converter.unstructure(f)

# The data is formatted in ascii format.
# The whole string will be in memory when doing this,
# duplicating the data.
assert f_dict == "1 2 3"

0 comments on commit 4ef3d93

Please sign in to comment.