Skip to content

Commit

Permalink
Feat: Add round trip conversion from xarray to json
Browse files Browse the repository at this point in the history
  • Loading branch information
simmsa committed May 1, 2024
1 parent 8f29f90 commit acc5872
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Binary file added examples/data/river/d3d/turbineTest_map.nc
Binary file not shown.
88 changes: 88 additions & 0 deletions mhkit_python_utils/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import json

import xarray as xr

from datetime import date, datetime, timedelta


# https://stackoverflow.com/a/22238613
def json_serial(obj):
"""
JSON serializer for objects not serializable by default json code.
Parameters
----------
obj : object
Object to be serialized.
Returns
-------
str
Serialized object in ISO format or as string.
Raises
------
TypeError
If the object type is not serializable.
"""
if isinstance(obj, (datetime, date)):
return obj.isoformat()
if isinstance(obj, (datetime, timedelta)):
return str(obj)
raise TypeError("Type %s not serializable" % type(obj))


def xarray_to_serialized_json(ds: xr.Dataset) -> str:
"""
Convert xarray dataset to serialized JSON.
Parameters
----------
ds : xarray.Dataset
Dataset to convert.
Returns
-------
str
Serialized JSON representation of the dataset.
"""
return json.dumps(ds.to_dict(), default=json_serial)


def serialized_json_to_xarray(serialized_json: str) -> xr.Dataset:
"""
Convert serialized JSON to xarray dataset.
Parameters
----------
serialized_json : str
Serialized JSON string representing the dataset.
Returns
-------
xr.Dataset
Xarray dataset reconstructed from the serialized JSON.
"""
return xr.Dataset.from_dict(json.loads(serialized_json))


if __name__ == "__main__":
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_theme()

netcdf_file = Path("../examples/data/river/d3d/turbineTest_map.nc").resolve()
ds = xr.open_dataset(netcdf_file)
print(ds.attrs)

serialized_json = xarray_to_serialized_json(ds)
ds = serialized_json_to_xarray(serialized_json)

turkin = ds.data_vars["turkin1"]
turkin_1d = turkin.isel(nFlowLink=list(range(1, 4)), wdim=1).plot()
plt.show()

0 comments on commit acc5872

Please sign in to comment.