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

consider prevent UgridDataset type if no grid present #180

Open
veenstrajelmer opened this issue Nov 24, 2023 · 3 comments
Open

consider prevent UgridDataset type if no grid present #180

veenstrajelmer opened this issue Nov 24, 2023 · 3 comments

Comments

@veenstrajelmer
Copy link
Collaborator

When loading a non-ugrid dataset with xugrid, it is converted to a UgridDataset anyway. This is a bit inconvenient, since checking whether it is really a dataset with ugrid, can only be done by checking if it has a "grid".

import xugrid as xu
import xarray as xr

file_nc = "test.nc"
ds = xr.tutorial.open_dataset("air_temperature")
ds.to_netcdf(file_nc)

uds = xu.open_dataset(file_nc)

print(type(uds)) # xugrid.core.wrap.UgridDataset
uds.ugrid # works
uds.grid # fails, zero grids
@Huite
Copy link
Collaborator

Huite commented Nov 24, 2023

You could check the xarray object too, there's an xarray accessor (via .ugrid_roles): https://deltares.github.io/xugrid/api.html#ugrid-roles-accessor

uds = xu.data.disk()
ds = uds.ugrid.to_dataset()
print(ds.ugrid_roles.topology)  # ["mesh2d"]

# If there's no UGRID stuff in there, the list is empty, so you could do:
if ds.ugrid_roles.topology:
    uds = xu.UgridDataset(ds)

@veenstrajelmer
Copy link
Collaborator Author

Ok, but the ugrid roles from a UgridDataset are None, since the grid part is under the grid accessor (if it is present)

This issue also relates to grid preservation with operations: #181

@Huite
Copy link
Collaborator

Huite commented Nov 26, 2023

When loading a non-ugrid dataset with xugrid, it is converted to a UgridDataset anyway.

I guess I should've asked to start with: why are you reading non-UGRID data with xugrid anyway?

Just like you should use pandas to read CSV's rather than xarray, you should use xarray to read non-UGRID datasets.
So is the actual question: what's the proper way to check whether a UgridDataset is really a proper UGRID thing?

Maybe related, what I have been thinking about: if the work on xarray's explicit indexes is done, we would get rid of UgridDataset and UgridDataArray, and .ugrid would be an ordinary xarray accessor. That means that typechecking for UgridDataset/UgridDataArray will no longer work. Instead we would add a .ugrid.is_ugrid property (or something) instead -- not quite relevant now, but maybe it does the thing you're after anyway?

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