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

support writing of netfile with start_index=1 and _FillValue!=-1 #119

Closed
veenstrajelmer opened this issue Jul 12, 2023 · 0 comments · Fixed by #300
Closed

support writing of netfile with start_index=1 and _FillValue!=-1 #119

veenstrajelmer opened this issue Jul 12, 2023 · 0 comments · Fixed by #300

Comments

@veenstrajelmer
Copy link
Collaborator

veenstrajelmer commented Jul 12, 2023

Unfortunately, dflowfm does not read the start_index attribute but assumes 1. When writing a newly generated meshkernelpy grid (start_index=0, _FillValue=-1) to a netcdf file, the start_index has to be converted to 1. Currently, dfm_tools uses this workaround in meshkernel_to_UgridDataset():

mesh2d_grid = mk.mesh2d_get()
xu_grid = xu.Ugrid2d.from_meshkernel(mesh2d_grid)

#convert 0-based to 1-based indices for connectivity variables like face_node_connectivity
xu_grid_ds = xu_grid.to_dataset()
xu_grid_ds = xr.decode_cf(xu_grid_ds) #decode_cf is essential since it replaces fillvalues with nans
ds_idx = xu_grid_ds.filter_by_attrs(start_index=0)
for varn_conn in ds_idx.data_vars:
    xu_grid_ds[varn_conn] += 1 #from startindex 0 to 1 (fillvalues are now nans, so this is safe to do)
    xu_grid_ds[varn_conn].attrs["start_index"] += 1
    xu_grid_ds[varn_conn].encoding["_FillValue"] = -1 #can be any value <=0, but non-zero is currently the most convenient for proper xugrid plots of node variables.

# convert to uds and add attrs and crs
xu_grid_uds = xu.UgridDataset(xu_grid_ds)

I can imagine that 1-based face_node_connectivity causes issues in 0-based Python. Could we maybe simplify the above process by for instance not changing the internal data, but instead upon writing a netcdf file with uds.ugrid.to_netcdf(), providing an argument start_index=1? Any other alternative is also fine.

@veenstrajelmer veenstrajelmer changed the title support writing of netfile with start_index=1 and _FillValue=0 support writing of netfile with start_index=1 and _FillValue!=-1 Jul 12, 2023
@veenstrajelmer veenstrajelmer linked a pull request Sep 6, 2024 that will close this issue
@Huite Huite closed this as completed in #300 Sep 9, 2024
@Huite Huite closed this as completed in 9becbac Sep 9, 2024
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

Successfully merging a pull request may close this issue.

1 participant