Skip to content

Commit 0944797

Browse files
Merge branch 'feature/uxgrid-morton-hashing' of github.com:OceanParcels/Parcels into feature/uxgrid-morton-hashing
2 parents ace5c4f + 34c28c2 commit 0944797

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

parcels/_datasets/utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
from typing import Any
23

34
import 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

0 commit comments

Comments
 (0)