Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialization as netcdf throws an exception #42

Open
pmav99 opened this issue Jun 30, 2024 · 1 comment
Open

serialization as netcdf throws an exception #42

pmav99 opened this issue Jun 30, 2024 · 1 comment

Comments

@pmav99
Copy link
Contributor

pmav99 commented Jun 30, 2024

I am not sure if xarray-selafin, wants to support exporting selafin files as netcdfs, but the way the xarray attributes are currently being utilized is not compatible with netcdf. For example:

ds = xr.open_dataset("tests/data/r2d_tidal_flats.slf")
ds.to_netcdf("/tmp/out.nc")

results in:

TypeError: Invalid value for attr 'variables': {'U': ('VELOCITY U', 'M/S'), 'V': ('VELOCITY V', 'M/S'), 'H': ('WATER DEPTH', 'M'), 'S': ('FREE SURFACE', 'M'), 'B': ('BOTTOM', 'M')}. For serialization to netCDF files, its value must be of one of the following types: str, Number, ndarray, number, list, tuple
@tomsail
Copy link
Contributor

tomsail commented Oct 16, 2024

Here is the implementation in io_test.py

def write_netcdf(ds, nc_out):
    # Remove dict and multi-dimensional arrays not supported in netCDF
    del ds.attrs["variables"]
    del ds.attrs["ikle2"]
    try:
        del ds.attrs["ikle3"]
    except KeyError:
        pass
    # Write netCDF file
    ds.to_netcdf(nc_out)

both variables and ikle2 (and ikle3 for 3d outputs) need to be deleted to to write to netcdf.

  • For ikle2/ikle3, this should be solvable with feat: add ikle2/ikle3 as variable #40
  • For variables, I think it could be wise to adapt UGRID conventions for all variable names. this would make the read/write operations easier: Selafins .slf format <-> xarray UGRID datasets. To achieve this, we'd need to revisit the current implementation of the variables names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants