From fa21d422be6698321cc68c4c930ac58068562cad Mon Sep 17 00:00:00 2001 From: Marnix Date: Fri, 13 Dec 2024 08:11:21 +0100 Subject: [PATCH] Add test that shows that we need an unstructure_hook for DataTree This is the simplest way to keep DataTree object the original. Otherwise it would return a copy of some sort. --- test/test_cattrs.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test_cattrs.py b/test/test_cattrs.py index 5d96f90..68f6747 100644 --- a/test/test_cattrs.py +++ b/test/test_cattrs.py @@ -58,3 +58,19 @@ def test_unstructure_xarray_tree_to_ascii(): # The whole string will be in memory when doing this, # duplicating the data. assert f_dict == "1 2 3" + + +def test_unstructure_xarray_tree(): + 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(xr.DataTree, lambda v: v) + f_dict = converter.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_tree is f_dict["x"]