Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions parcels/_datasets/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from typing import Any

import numpy as np
Expand Down Expand Up @@ -183,3 +184,34 @@ def verbose_print(*args, **kwargs):
print(f" {ds1_name}: {var1.dims}")
print(f" {ds2_name}: {var2.dims}")
verbose_print("=" * 30 + " End of Comparison " + "=" * 30)


def from_xarray_dataset_dict(d) -> xr.Dataset:
Copy link
Contributor Author

@VeckoTheGecko VeckoTheGecko Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably makes the above functions redundant - but I thought let's keep the above until v4 is released and we get people using it just in case they end up being useful when debugging people's datasets

"""Reconstruct a dataset with zero data from the output of ``xarray.Dataset.to_dict(data=False)``.

Useful in issues helping users debug fieldsets - sharing dataset schemas with associated metadata
without sharing the data itself.

Example
-------
>>> import xarray as xr
>>> from parcels._datasets.structured.generic import datasets
>>> ds = datasets['ds_2d_left']
>>> d = ds.to_dict(data=False)
>>> ds2 = from_xarray_dataset_dict(d)
"""
return xr.Dataset.from_dict(_fill_with_dummy_data(copy.deepcopy(d)))


def _fill_with_dummy_data(d: dict[str, dict]):
assert isinstance(d, dict)
if "dtype" in d:
d["data"] = np.zeros(d["shape"], dtype=d["dtype"])
del d["dtype"]
del d["shape"]

for k in d:
if isinstance(d[k], dict):
d[k] = _fill_with_dummy_data(d[k])

return d
Loading