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

(De)serialization of UgridDataset results in UgridDatasets with 0 grids #223

Closed
Tjalling-dejong opened this issue Apr 10, 2024 · 4 comments

Comments

@Tjalling-dejong
Copy link

Tjalling-dejong commented Apr 10, 2024

For the mesh component of HydroMT I was writing some tests involving writing and reading UgridDatasets. HydroMT converts UgridDatasets to xarray.Datasets before writing in order to preserve crs. I wanted to check if XUgrid preserves crs when writing to file. This resulted in an unexpected error. I expected the crs to be None but got an UgridDataset with 0 grids.

Reproducible example:

import xugrid as xu
data = xu.data.elevation_nl().to_dataset()
data.grid.set_crs(28992)
data.to_netcdf("test.nc")
data2 = xu.open_dataset("test.nc")
data2.grid.crs == data.grid.crs 

This will give a TypeError because grids is 0.

@Huite
Copy link
Collaborator

Huite commented Apr 10, 2024

I think the problem is that you omitted the .ugrid accessor on the to_netcdf call.

If you change it to this:

data.ugrid.to_netcdf("test.nc")

It'll work as expected.

Without the ugrid accessor it'll just call the regular xarray method which is topology unaware.

(With a bit of luck, the ordinary method might also work once xarray has implemented explicit indexes.)

@Tjalling-dejong
Copy link
Author

Thanks Huite, that worked.
Is there a way that the crs can be preserved when writing to netcdf?

@Huite
Copy link
Collaborator

Huite commented Apr 11, 2024

Not at the moment :(

These issues are related:

#42
#187
#189

The first one is most relevant. I haven't found the time to implement this. PRs are -- obviously! -- welcome.

@Tjalling-dejong
Copy link
Author

Thanks again, I'm closing this issue since the original issue is not an issue anymore.

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