Skip to content

Commit

Permalink
update(Raster): added min and max resampling for hydrologic resamplin…
Browse files Browse the repository at this point in the history
…g and streamflow representation (#1294)

* added trap for nodata within polygons

* added test_raster_sampling_methods to t065_test_gridintersect.py
  • Loading branch information
jlarsen-usgs committed Nov 12, 2021
1 parent d248734 commit 76bd34e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
58 changes: 58 additions & 0 deletions autotest/t065_test_gridintersect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,5 +1361,63 @@ def test_rasters():
del rio


# %% test raster sampling methods


def test_raster_sampling_methods():
from flopy.utils import Raster
import os
import flopy as fp

ws = os.path.join("..", "examples", "data", "options")
raster_name = "dem.img"

try:
rio = Raster.load(os.path.join(ws, "dem", raster_name))
except:
return

ml = fp.modflow.Modflow.load(
"sagehen.nam", version="mfnwt", model_ws=os.path.join(ws, "sagehen")
)
xoff = 214110
yoff = 4366620
ml.modelgrid.set_coord_info(xoff, yoff)

x0, x1, y0, y1 = rio.bounds

x0 += 3000
y0 += 3000
x1 -= 3000
y1 -= 3000
shape = np.array([(x0, y0), (x0, y1), (x1, y1), (x1, y0), (x0, y0)])

rio.crop(shape)

methods = {
"min": 2088.52343,
"max": 2103.54882,
"mean": 2097.05053,
"median": 2097.36254,
"nearest": 2097.81079,
"linear": 2097.81079,
"cubic": 2097.81079
}

for method, value in methods.items():
data = rio.resample_to_grid(
ml.modelgrid,
band=rio.bands[0],
method=method
)

print(data[30, 37])
if np.abs(data[30, 37] - value) > 1e-05:
raise AssertionError(
f"{method} resampling returning incorrect values"
)


if __name__ == "__main__":
test_rasters()
test_raster_sampling_methods()
37 changes: 29 additions & 8 deletions flopy/utils/rasters.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ def resample_to_grid(
``mean`` for mean sampling
``median`` for median sampling
``min`` for minimum sampling
``max`` for maximum sampling
multithread : bool
boolean flag indicating if multithreading should be used with
the ``mean`` and ``median`` sampling methods
Expand Down Expand Up @@ -399,8 +404,8 @@ def resample_to_grid(
method=method,
)

elif method in ("median", "mean"):
# these methods are slow and could use a speed u
elif method in ("median", "mean", "min", "max"):
# these methods are slow and could use a speed up
ncpl = modelgrid.ncpl
data_shape = modelgrid.xcellcenters.shape
if isinstance(ncpl, (list, np.ndarray)):
Expand Down Expand Up @@ -453,10 +458,17 @@ def resample_to_grid(
msk = np.in1d(rstr_data, self.nodatavals)
rstr_data[msk] = np.nan

if method == "median":
val = np.nanmedian(rstr_data)
if rstr_data.size == 0:
val = self.nodatavals[0]
else:
val = np.nanmean(rstr_data)
if method == "median":
val = np.nanmedian(rstr_data)
elif method == "mean":
val = np.nanmean(rstr_data)
elif method == "max":
val = np.nanmax(rstr_data)
else:
val = np.nanmin(rstr_data)

data[node] = val
else:
Expand Down Expand Up @@ -531,10 +543,19 @@ def __threaded_resampling(
msk = np.in1d(rstr_data, self.nodatavals)
rstr_data[msk] = np.nan

if method == "median":
val = np.nanmedian(rstr_data)
if rstr_data.size == 0:
val = self.nodatavals[0]

else:
val = np.nanmean(rstr_data)
if method == "median":
val = np.nanmedian(rstr_data)
elif method == "mean":
val = np.nanmean(rstr_data)
elif method == "max":
val = np.nanmax(rstr_data)
else:
val = np.nanmin(rstr_data)

q.put((node, val))
container.release()

Expand Down

0 comments on commit 76bd34e

Please sign in to comment.