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

1066 modelbuilder fix inexact latlon bbox #1067

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions dfm_tools/meshkernel_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"meshkernel_delete_withgdf",
"meshkernel_get_illegalcells",
"meshkernel_to_UgridDataset",
"meshkernel_get_bbox",
"make_basegrid",
"refine_basegrid",
"generate_bndpli_cutland",
Expand Down Expand Up @@ -351,6 +352,26 @@ def refine_basegrid(mk, data_bathy_sel, min_edge_size):
return mk


def meshkernel_get_outer_xycoords(mk:meshkernel.MeshKernel):
mesh_bnds = mk.mesh2d_get_mesh_boundaries_as_polygons()
xcoords = mesh_bnds.x_coordinates
ycoords = mesh_bnds.y_coordinates
if mesh_bnds.geometry_separator in xcoords:
# TODO: would be nice to get only the outer xycoords instead
# or to have easier selection of outer xycoords
raise Exception('use meshkernel_get_outer_xycoords() on an uncut grid')
return xcoords, ycoords


def meshkernel_get_bbox(mk:meshkernel.MeshKernel):
"""
get the bbox of a meshkernel instance as (xmin, ymin, xmax, ymax)
"""
xcoords, ycoords = meshkernel_get_outer_xycoords(mk)
bbox = (xcoords.min(), ycoords.min(), xcoords.max(), ycoords.max())
return bbox


def generate_bndpli_cutland(mk:meshkernel.MeshKernel, res:str='f', min_area:float = 0, crs:(int,str) = None, buffer:float = 0):
"""
Generate a boundary polyline from the meshkernel object and cut away the landward part.
Expand All @@ -375,15 +396,11 @@ def generate_bndpli_cutland(mk:meshkernel.MeshKernel, res:str='f', min_area:floa
DESCRIPTION.

"""

mesh_bnds = mk.mesh2d_get_mesh_boundaries_as_polygons()
if mesh_bnds.geometry_separator in mesh_bnds.x_coordinates:
raise Exception('use dfmt.generate_bndpli_cutland() on an uncut grid')
mesh_bnds_xy = np.c_[mesh_bnds.x_coordinates,mesh_bnds.y_coordinates]

bbox = (mesh_bnds.x_coordinates.min(), mesh_bnds.y_coordinates.min(), mesh_bnds.x_coordinates.max(), mesh_bnds.y_coordinates.max())
bbox = meshkernel_get_bbox(mk)
coastlines_gdf = get_coastlines_gdb(bbox=bbox, res=res, min_area=min_area, crs=crs)

xcoords,ycoords = meshkernel_get_outer_xycoords(mk)
mesh_bnds_xy = np.c_[xcoords,ycoords]
meshbnd_ls = LineString(mesh_bnds_xy)
coastlines_mp = MultiPolygon(coastlines_gdf.geometry.tolist())
coastlines_mp = coastlines_mp.buffer(buffer)
Expand Down
287 changes: 182 additions & 105 deletions docs/notebooks/modelbuilder_example.ipynb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- replaced buffer and floor/ceil with copernicusmarine `coordinates_selection_method`, this deprecated the `buffer` argument for `dfmt.download_CMEMS()` [#1061](https://github.com/Deltares/dfm_tools/pull/1061)
- inclusive selection of outside timesteps in `open_prepare_dataset()` and thus in `cmems_nc_to_bc()` in [#1062](https://github.com/Deltares/dfm_tools/pull/1062)

### Fix
- fixed inexact latlon bbox in modelbuilder with `dfmt.meshkernel_get_bbox()` in [#1067](https://github.com/Deltares/dfm_tools/pull/1067)


## 0.32.0 (2025-01-14)

Expand Down
2 changes: 1 addition & 1 deletion tests/examples/preprocess_modelbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

## input
model_name = 'Bonaire'
dir_output = r'p:\11209231-003-bes-modellering\hydrodynamica\hackathon\preprocessing\ModelBuilderOutput_JV2'
dir_output = f"{model_name}_model"
path_style = 'windows' # windows / unix, making relative paths only works when path_style is equal to os
overwrite = False # used for downloading of forcing data. Always set to True when changing the domain
crs = 'EPSG:4326'
Expand Down
21 changes: 21 additions & 0 deletions tests/test_meshkernel_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,27 @@ def test_meshkernel_to_UgridDataset(tmp_path):
assert "wgs84" in ds_out.variables


@pytest.mark.unittest
def test_meshkernel_get_bbox():
projection = ProjectionType.SPHERICAL

# create basegrid
lon_min, lon_max, lat_min, lat_max = -6, 2, 48.5, 51.2
dxy = 0.5
make_grid_parameters = MakeGridParameters(origin_x=lon_min,
origin_y=lat_min,
upper_right_x=lon_max,
upper_right_y=lat_max,
block_size_x=dxy,
block_size_y=dxy)
mk = MeshKernel(projection=projection)
mk.curvilinear_compute_rectangular_grid_on_extension(make_grid_parameters)
mk.curvilinear_convert_to_mesh2d() #convert to ugrid/mesh2d

bbox = dfmt.meshkernel_get_bbox(mk)
assert np.allclose(bbox, (-6.0, 48.5, 2.0, 51.40631301478466))


@pytest.mark.unittest
def test_generate_bndpli_cutland():
# domain, resolution and expected values
Expand Down
Loading