Skip to content

Commit

Permalink
Updated code to ensure expected DEM resolution is matched and updated…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
rosepearson committed Jan 14, 2025
1 parent 7e47713 commit f12b34b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/geofabrics/dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ def __init__(self, catchment_geometry: geometry.CatchmentGeometry, chunk_size: i
def dem(self) -> xarray.Dataset:
"""Return the DEM over the catchment region"""
raise NotImplementedError("dem must be instantiated in the child class")

def _check_resolution(self, dem: xarray.Dataset):
""" Check the DEM resolution matches the specified resolution. """
dem_resolution = dem.rio.resolution()
dem_resolution = max(abs(dem_resolution[0]), abs(dem_resolution[1]))

return self.catchment_geometry.resolution == dem_resolution


def _load_dem(self, filename: pathlib.Path) -> xarray.Dataset:
"""Load in and replace the DEM with a previously cached version."""
Expand All @@ -337,6 +345,9 @@ def _load_dem(self, filename: pathlib.Path) -> xarray.Dataset:
if "zo" in dem.keys():
dem["zo"] = dem.zo.astype(geometry.RASTER_TYPE)

if not self._check_resolution(dem):
raise ValueError("The specified resolution does not match the "
f"{filename} resolution.")
return dem

def save_dem(
Expand Down Expand Up @@ -642,6 +653,10 @@ def __init__(
# Rerun as otherwise the no data as NaN seems to be lost for the data_source layer
self._write_netcdf_conventions_in_place(raw_dem, catchment_geometry.crs)

if not self._check_resolution(raw_dem):
raise ValueError("The specified resolution does not match the "
f"{raw_dem_path} resolution.")

# Set attributes
self._raw_dem = raw_dem
self.interpolation_method = interpolation_method
Expand Down Expand Up @@ -2457,6 +2472,9 @@ def __init__(
"band", drop=True
) # remove band coordinate added by rasterio.open()
self._write_netcdf_conventions_in_place(initial_dem, catchment_geometry.crs)
if not self._check_resolution(initial_dem):
raise ValueError("The specified resolution does not match the "
f"{initial_dem_path} resolution.")
self._dem = initial_dem

def add_patch(self, patch_path: pathlib.Path, label: str, layer: str):
Expand Down Expand Up @@ -2704,7 +2722,9 @@ def __init__(
self._write_netcdf_conventions_in_place(
hydrological_dem, catchment_geometry.crs
)

if not self._check_resolution(hydrological_dem):
raise ValueError("The specified resolution does not match the "
f"{hydrological_dem_path} resolution.")
# Ensure the resolution of the hydrological DEM matches the input DEM
assert (
abs(float(hydrological_dem.x[1] - hydrological_dem.x[0]))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_add_patches_ngaruroro/instruction.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"vertical": 7839
},
"grid_params": {
"resolution": 10
"resolution": 8
}
},
"processing": {
Expand Down

0 comments on commit f12b34b

Please sign in to comment.