Skip to content

Commit

Permalink
fixed fillvalue issues and tests (#992)
Browse files Browse the repository at this point in the history
* updated fillvalue in dfmt.uds_to_faces() to accomodate new xugrid version

* fixed failing tests

* updated whatsnew
  • Loading branch information
veenstrajelmer authored Sep 9, 2024
1 parent 46c0bdf commit 8aff8ba
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
3 changes: 1 addition & 2 deletions dfm_tools/xugrid_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ def uda_to_faces(uda : xu.UgridDataArray) -> xu.UgridDataArray:
reduce_dim = 'nMax_face_nodes' #arbitrary dimname that is reduced anyway
dimn_nodes = grid.node_dimension
dimn_edges = grid.edge_dimension
fill_value = grid.fill_value

# construct indexing array
if dimn_nodes in uda.dims:
Expand All @@ -536,7 +535,7 @@ def uda_to_faces(uda : xu.UgridDataArray) -> xu.UgridDataArray:
uda = uda.chunk(chunks)

indexer = xr.DataArray(indexer_np,dims=(dimn_faces,reduce_dim))
indexer_validbool = indexer!=fill_value
indexer_validbool = indexer!=-1
indexer = indexer.where(indexer_validbool,-1)

print(f'{dimn_notfaces_name}-to-face interpolation: ',end='')
Expand Down
8 changes: 7 additions & 1 deletion docs/whats-new.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# What's new

## UNRELEASED

### Fix
- simplified `dfmt.meshkernel_to_UgridDataset()` by using new xugrid version in [#991](https://github.com/Deltares/dfm_tools/pull/991)
- updated `dfmt.uda_to_faces()` to accomodate for new fill_value of xugrid (this version includes fix for contour/contourf) [#992](https://github.com/Deltares/dfm_tools/pull/992)


## 0.26.0 (2024-09-03)

### Fix
Expand All @@ -9,7 +16,6 @@
- deprecated `dfmt.open_dataset_extra()` (partly replaced by `dfmt.open_prepare_dataset()`) in [#974](https://github.com/Deltares/dfm_tools/pull/974)
- improved nan-conversion in `dfmt.forcinglike_to_Dataset()` in [#982](https://github.com/Deltares/dfm_tools/pull/982)
- improved performance of `dfmt.open_partitioned_dataset()` for datasets with many variables in [#984](https://github.com/Deltares/dfm_tools/pull/984)
- simplified `dfmt.meshkernel_to_UgridDataset()` by using new xugrid version in [#991](https://github.com/Deltares/dfm_tools/pull/991)


## 0.25.0 (2024-08-16)
Expand Down
12 changes: 4 additions & 8 deletions tests/test_external_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,26 @@ def test_xarray_decode_default_fillvals(tmp_path):
#convert fillvalue in fnc to default fillvalue
varn_fnc = 'mesh2d_face_nodes'
fnc_dtype = ds[varn_fnc].dtype.str[1:]
fill_value = ds[varn_fnc].attrs['_FillValue']
fill_value_default = default_fillvals[fnc_dtype]
ds[varn_fnc].attrs.pop('_FillValue')
fill_value = ds[varn_fnc].attrs.pop('_FillValue')
ds[varn_fnc] = ds[varn_fnc].where(ds[varn_fnc]!=fill_value,fill_value_default)

#write file
file_out = os.path.join(tmp_path, 'temp_fnc_default_fillvals_map.nc')
ds.to_netcdf(file_out)

# chunks=auto to avoid "UserWarning: The specified chunks separate the stored chunks along dimension "time" starting at index 1. This could degrade performance. Instead, consider rechunking after loading."
chunks = 'auto'

#open dataset with decode_fillvals
with pytest.raises(ValueError) as e:
# until xarray handles default fillvalues: https://github.com/Deltares/dfm_tools/issues/490
# if xarray handles default fillvalues, `dfmt.decode_default_fillvals()` can be removed if minimum xarray version is set as requirement
uds = dfmt.open_partitioned_dataset(file_out, decode_fillvals=False, chunks=chunks)
dfmt.open_partitioned_dataset(file_out, decode_fillvals=False)
assert "connectivity contains negative values" in str(e.value)

#this should be successful
uds2 = dfmt.open_partitioned_dataset(file_out, decode_fillvals=True, chunks=chunks)
uds2 = dfmt.open_partitioned_dataset(file_out, decode_fillvals=True)
fnc_new = uds2.grid.face_node_connectivity

assert fill_value_default in fnc_new
assert -1 in fnc_new


@pytest.mark.unittest
Expand Down

0 comments on commit 8aff8ba

Please sign in to comment.