Skip to content

Commit

Permalink
Only reindex patch if all x and y also in the destimation DEM and als…
Browse files Browse the repository at this point in the history
…o it is of the same resolution. other minor code tidy up and corrections
  • Loading branch information
rosepearson committed Jan 14, 2025
1 parent cd1adbe commit 0bf3739
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/geofabrics/dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def _extents_from_mask(self, mask: numpy.ndarray, transform: dict):

return dense_extents

def _chunks_from_dem(self, chunk_size, dem: xarray.Dataset) -> (list, list):
def _chunks_from_dem(self, chunk_size, dem: xarray.Dataset) -> tuple[list, list]:
"""Define the chunks to break the catchment into when reading in and
downsampling LiDAR.
Expand Down Expand Up @@ -1764,7 +1764,7 @@ def __init__(
self.buffer_cells = buffer_cells
self._dem = None

def _set_up_chunks(self) -> (list, list):
def _set_up_chunks(self) -> tuple[list, list]:
"""Define the chunks to break the catchment into when reading in and
downsampling LiDAR.
"""
Expand Down Expand Up @@ -2528,14 +2528,14 @@ def add_patch(self, patch_path: pathlib.Path, label: str, layer: str):
self.logger.info(f"\t\tAdd data from coarse DEM: {patch_path.name}")

# Check if same resolution
if all(patch.x.isin(self._dem.x)) and all(patch.y.isin(self._dem.y)):
# Do a stright replacement if grid aligned
if all(patch.x.isin(self._dem.x)) and all(patch.y.isin(self._dem.y)) and patch_resolution == self.catchment_geometry.resolution:
self.logger.info(f"\t\t\tGrid aligned so do a straight reindex")
patch = patch.reindex_like(self._dem, fill_value=numpy.nan)
elif (
self.chunk_size is not None
and max(len(self._dem.x), len(self._dem.y)) > self.chunk_size
): # Ensure parallelisation if chunks specified
# Expect xarray dims (y, x), not (x, y) as default for rioxarray
): # Expect xarray dims (y, x), not (x, y) as default for rioxarray
self.logger.info(f"\t\t\tInterpolate with dask parallelisation at chunk size")
interpolator = scipy.interpolate.RegularGridInterpolator(
(patch.y.values, patch.x.values),
patch.values,
Expand All @@ -2561,6 +2561,7 @@ def dask_interpolation(y, x):
)
patch.rio.write_transform(inplace=True)
else: # No chunking use built in method
self.logger.info(f"\t\t\tInterpolate using built-in method")
patch = patch.interp(x=self._dem.x, y=self._dem.y, method="linear")
patch.rio.write_crs(self.catchment_geometry.crs["horizontal"], inplace=True)
patch.rio.write_nodata(numpy.nan, encoded=True, inplace=True)
Expand Down Expand Up @@ -2946,8 +2947,9 @@ def _add_tiled_lidar_chunked(
f"\t\tReturning empty tile as no LiDAR or out of ROI"
)
delayed_chunked_x.append(
dask.array.empty(
dask.array.full(
shape=(len(dim_y), len(dim_x)),
fill_value=numpy.nan,
dtype=raster_options["raster_type"],
)
)
Expand Down

0 comments on commit 0bf3739

Please sign in to comment.