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

attributes added to mesh2d variable point to non-existent variables #132

Closed
veenstrajelmer opened this issue Aug 3, 2023 · 1 comment
Closed

Comments

@veenstrajelmer
Copy link
Collaborator

veenstrajelmer commented Aug 3, 2023

xu.open_dataset() adds several attrs to mesh2d that point to non-existing variables, like for instance 'boundary_node_connectivity': 'mesh2d_boundary_nodes'. This is an issue when using the file obtained from .ugrid.to_netcdf() in other processes.

MWE:

import xugrid as xu
import xarray as xr

file_nc = 'Grevelingen-FM_0000_map.nc'
file_nc = xu.data.adh_san_diego(xarray=True).encoding['source']

ds = xr.open_dataset(file_nc)
ds_mesh2d = ds.mesh2d.attrs

uds = xu.open_dataset(file_nc)
uds_mesh2d = uds.grid.to_dataset().mesh2d.attrs

uds_mesh2d_new = uds.grid.to_dataset().mesh2d.attrs
for key in ds_mesh2d.keys():
    uds_mesh2d_new.pop(key)
print(uds_mesh2d_new)

for varn in uds_mesh2d_new.values():
    try:
        uds[varn]
    except KeyError as e:
        print(f'KeyError: {e}')

Gives:

KeyError: 'mesh2d_nEdges'
KeyError: 'face'
KeyError: 'mesh2d_nMax_face_nodes'
KeyError: 'mesh2d_nBoundary_edges'
KeyError: 'mesh2d_boundary_nodes'
KeyError: 'mesh2d_face_faces'
KeyError: 'mesh2d'
@Huite
Copy link
Collaborator

Huite commented Aug 7, 2023

I've come across this issue myself as well: after writing the netCDF, QGIS via MDAL will not accept these attributes.

I think you can make two cases for how these attributes should be interpreted;

  • "This variable exists, under this name"
  • "If it exists, or if you create this variable, it will be under this name"

I went with the second interpretation -- though probably mostly due to implementation convenience since you can always assume the variables are all available. It allows for a relatively easy configuring of a naming scheme.

Regardless, if the unused variables are removed during the to_dataset() call, that behavior is maintained and other software shouldn't trip up.

@Huite Huite closed this as completed in 75d5ff1 Aug 8, 2023
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