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

remove dfmt.remove_periodic_cells() #509

Merged
22 changes: 2 additions & 20 deletions dfm_tools/xugrid_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,6 @@
return uds


def remove_periodic_cells(uds): #TODO: implement proper fix: https://github.com/Deltares/xugrid/issues/63
"""
For global models with grids that go "around the back". Temporary fix to drop all faces that are larger than grid_extent/2 (eg 360/2=180 degrees in case of GTSM)

"""
face_node_x = uds.grid.face_node_coordinates[:,:,0]
grid_extent = uds.grid.bounds[2] - uds.grid.bounds[0]
face_node_maxdx = np.nanmax(face_node_x,axis=1) - np.nanmin(face_node_x,axis=1)
bool_face = face_node_maxdx < grid_extent/2
if bool_face.all(): #early return for when no cells have to be removed (might increase performance)
return uds
print(f'>> removing {(~bool_face).sum()} periodic cells from dataset: ',end='')
dtstart = dt.datetime.now()
uds = uds.sel({uds.grid.face_dimension:bool_face})
print(f'{(dt.datetime.now()-dtstart).total_seconds():.2f} sec')
return uds


def remove_unassociated_edges(ds: xr.Dataset) -> xr.Dataset:
"""
Removes edges that are not associated to any of the faces, usecase in https://github.com/Deltares/xugrid/issues/68
Expand Down Expand Up @@ -249,7 +231,7 @@

ds = xr.open_mfdataset(file_nc, **kwargs)

print('>> getting vertices from ds: ',end='') #long (but does not reflect in
print('>> getting vertices from ds: ',end='')

Check warning on line 234 in dfm_tools/xugrid_helpers.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/xugrid_helpers.py#L234

Added line #L234 was not covered by tests
dtstart = dt.datetime.now()
vertices_longitude = ds[varn_vert_lon].to_numpy()
vertices_longitude = vertices_longitude.reshape(-1,vertices_longitude.shape[-1])
Expand All @@ -259,7 +241,7 @@

#convert from 0to360 to -180 to 180
if convert_360to180:
vertices_longitude = (vertices_longitude+180)%360 - 180 #TODO: check if periodic cell filter still works properly after doing this
vertices_longitude = (vertices_longitude+180)%360 - 180 #TODO: check if uds.ugrid.to_nonperiodic() still works properly after doing this

Check warning on line 244 in dfm_tools/xugrid_helpers.py

View check run for this annotation

Codecov / codecov/patch

dfm_tools/xugrid_helpers.py#L244

Added line #L244 was not covered by tests

# face_xy = np.stack([longitude,latitude],axis=-1)
# face_coords_x, face_coords_y = face_xy.T
Expand Down
7 changes: 1 addition & 6 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## UNRELEASED

### Feat
- xugrid feature `uds.ugrid.to_nonperiodic()` deprecates `dfmt.remove_periodic_cells()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#503](https://github.com/Deltares/dfm_tools/issues/503)
- support for initial fields for variables other than salinity/temperature with `dfmt.cmems_nc_to_ini()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#619](https://github.com/Deltares/dfm_tools/pull/619)

### Fix
Expand Down Expand Up @@ -51,29 +52,25 @@
## 0.13.0 (2023-07-12)

### Feat

- exposed opendap testdata with `dfmt.data.fm_grevelingen_map()` (and others) by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#439](https://github.com/Deltares/dfm_tools/pull/439)
- made coastlines portable with ` dfmt.data.gshhs_coastlines_shp()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#443](https://github.com/Deltares/dfm_tools/pull/443)
- improved CMEMS and ERA5 authentication via [getpass](https://docs.python.org/3/library/getpass.html) by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#449](https://github.com/Deltares/dfm_tools/pull/449)
- aligned mesh deletion with `dfmt.meshkernel_delete_withgdf()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#455](https://github.com/Deltares/dfm_tools/pull/455)

### Fix

- added `dfmt.uds_to_1based_ds()` to ensure FM-compatible network file (1-based face_node_connectivity) by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#456](https://github.com/Deltares/dfm_tools/pull/456)


## 0.12.0 (2023-07-07)

### Feat

- added support for curvilinear datasets like CMCC and WAQUA with `open_dataset_curvilinear()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#397](https://github.com/Deltares/dfm_tools/pull/397)
- added support for curvilinear Delft3D4 datasets with `open_dataset_delft3d4()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#398](https://github.com/Deltares/dfm_tools/pull/398)
- retrieving [GSHHS landboundary](https://www.ngdc.noaa.gov/mgg/shorelines/) with `get_coastlines_gdb()` and cartopy-alternative `plot_coastlines()` with this dataset by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#342](https://github.com/Deltares/dfm_tools/pull/342)
- added interpolation from UgridDataset (D-FlowFM mapfile) to plipoints with `interp_uds_to_plipoints()` by [@veenstrajelmer](https://github.com/veenstrajelmer) [#387](https://github.com/Deltares/dfm_tools/pull/387)
- added geodataframe support from polyfile with `PolyFile_to_geodataframe_points()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#387](https://github.com/Deltares/dfm_tools/pull/387)

### Fix

- proper selection for either CMEMS reanalysis/forecast product in `download_CMEMS()` by [@JulienGroenenboom](https://github.com/JulienGroenenboom) in [#308](https://github.com/Deltares/dfm_tools/pull/388)
- slightly extend spatial and time domain when downloading ERA5 and CMEMS data by [@JulienGroenenboom](https://github.com/JulienGroenenboom) in [#390](https://github.com/Deltares/dfm_tools/pull/390)
- renamed uxuy to uxuyadvectionvelocitybnd in `open_dataset_extra()` and related example script by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#366](https://github.com/Deltares/dfm_tools/pull/366)
Expand All @@ -86,7 +83,6 @@
Replaced large parts of the code with [HYDROLIB-core](https://github.com/Deltares/hydrolib-core), [xugrid](https://github.com/Deltares/xugrid) and [xarray](https://github.com/pydata/xarray). This means a non-backwards compatible change in the API but improved experience. xugrid gives loads of new features like coordinate conversion, shapefile export, cropping a unstructured dataset, regridding, rasterizing.

### Feat

- improved support for ugrid datasets (D-FlowFM, D-HYDRO) with `open_partitioned_dataset()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in several PRs, this deprecates `get_ncmodeldata()`, `get_netdata()`, `plot_netmapdata()` and `get_ugrid_verts()`
- improved rasterization of ugrid datasets with `rasterize_ugrid()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#314](https://github.com/Deltares/dfm_tools/pull/314), this deprecates `scatter_to_regulargrid()`
- depth slicing of ugrid datasets with `get_Dataset_atdepths()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#261](https://github.com/Deltares/dfm_tools/pull/261)
Expand All @@ -105,5 +101,4 @@ Replaced large parts of the code with [HYDROLIB-core](https://github.com/Deltare
- added meshkernelpy helper functions like `make_basegrid()`, `refine_basegrid()` and `meshkernel_to_UgridDataset()` by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#329](https://github.com/Deltares/dfm_tools/pull/329)

### Fix

- made bedlevel variable optional in `get_Dataset_atdepths()`, if not explicitly needing it by [@veenstrajelmer](https://github.com/veenstrajelmer) in [#241](https://github.com/Deltares/dfm_tools/pull/241)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
file_nc = f'p:\\archivedprojects\\11206304-futuremares-rawdata-preps\\data\\CMIP6_BC\\CMCC-ESM2\\{varn}_Omon_CMCC-ESM2_ssp126_r1i1p1f1_gn_201501-203412.nc'

uds = dfmt.open_dataset_curvilinear(file_nc, convert_360to180=True) #TODO: check TODO in this function for improvements #TODO: plot_coastlines gives wrong result when axis from 0-360
uds = dfmt.remove_periodic_cells(uds)
uds = uds.ugrid.to_nonperiodic(xmax=180)
uds = uds.ugrid.sel(x=slice(-15,30), y=slice(30,70)) #slice to europe (arbitrary, but to visualy compare grids)

fig, ax = plt.subplots()
Expand Down
Loading