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

default fillvalue not parsed by xarray, results in negative values in connectivity #125

Closed
veenstrajelmer opened this issue Jul 26, 2023 · 1 comment

Comments

@veenstrajelmer
Copy link
Collaborator

veenstrajelmer commented Jul 26, 2023

xarray does not use the default netcdf4 fillvalue in decode_cf, only the explicitly set _FillValue attribute: pydata/xarray#7723 and pydata/xarray#2374 and pydata/xarray#2742

This raises "ValueError: connectivity contains negative values":

import xugrid as xu
file_nc = r'p:\archivedprojects\1210301-dflow-fm\MeinardTiessen\model\NEVLA\a19\DFM_OUTPUT_WS13_new_04\WS13_new_04_0000_map.nc'
ds = xu.open_dataset(file_nc)

For now, a workaround is to add the default fillvalue to the attributes before feeding to xugrid:

import xugrid as xu
import xarray as xr

file_nc = r'p:\archivedprojects\1210301-dflow-fm\MeinardTiessen\model\NEVLA\a19\DFM_OUTPUT_WS13_new_04\WS13_new_04_0000_map.nc'

ds = xr.open_dataset(file_nc)

#add default fillvalue if missing from connectivity array attrs
varn_conn = 'NetElemNode'
if ds[varn_conn].min()<0 and '_FillValue' not in ds[varn_conn].attrs:
    from netCDF4 import default_fillvals
    dtype_str = ds[varn_conn].dtype.str[1:]
    ds[varn_conn] = ds[varn_conn].assign_attrs({'_FillValue':default_fillvals[dtype_str]})
    #decode_cf again, fillvalues are replaced with nan and _FillValue attr is moved to encoding
    ds = xr.decode_cf(ds)

uds = xu.core.wrap.UgridDataset(ds)

We will probably wait on xarray to fix their issue. If it takes too long, we might want to implement a workaround.

This is also an issue for non-connectivity variables: Deltares/dfm_tools#490

@veenstrajelmer
Copy link
Collaborator Author

This will not be added in xugrid. It is temporarily added in dfm_tools in Deltares/dfm_tools#496 and it will be removed again once xarray handles default fillvalues.

@veenstrajelmer veenstrajelmer closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 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

1 participant