File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ import copy
12from typing import Any
23
34import numpy as np
@@ -183,3 +184,34 @@ def verbose_print(*args, **kwargs):
183184 print (f" { ds1_name } : { var1 .dims } " )
184185 print (f" { ds2_name } : { var2 .dims } " )
185186 verbose_print ("=" * 30 + " End of Comparison " + "=" * 30 )
187+
188+
189+ def from_xarray_dataset_dict (d ) -> xr .Dataset :
190+ """Reconstruct a dataset with zero data from the output of ``xarray.Dataset.to_dict(data=False)``.
191+
192+ Useful in issues helping users debug fieldsets - sharing dataset schemas with associated metadata
193+ without sharing the data itself.
194+
195+ Example
196+ -------
197+ >>> import xarray as xr
198+ >>> from parcels._datasets.structured.generic import datasets
199+ >>> ds = datasets['ds_2d_left']
200+ >>> d = ds.to_dict(data=False)
201+ >>> ds2 = from_xarray_dataset_dict(d)
202+ """
203+ return xr .Dataset .from_dict (_fill_with_dummy_data (copy .deepcopy (d )))
204+
205+
206+ def _fill_with_dummy_data (d : dict [str , dict ]):
207+ assert isinstance (d , dict )
208+ if "dtype" in d :
209+ d ["data" ] = np .zeros (d ["shape" ], dtype = d ["dtype" ])
210+ del d ["dtype" ]
211+ del d ["shape" ]
212+
213+ for k in d :
214+ if isinstance (d [k ], dict ):
215+ d [k ] = _fill_with_dummy_data (d [k ])
216+
217+ return d
You can’t perform that action at this time.
0 commit comments