forked from modflowpy/pyphoenix-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test some ways to unstructure xarray data
- Loading branch information
1 parent
45702d6
commit 4ef3d93
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |