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

Do not drop indexes when computing rmax #40

Merged
merged 8 commits into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion pydomcfg/domzgr/zgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def __init__(self, ds_bathy: Dataset, jpk: int):
Parameters
----------
ds_bathy: Dataset
xarray dataset including grid coordinates and bathymetry
xarray dataset including grid coordinates and bathymetry.
All bathymetry values MUST be >= 0, where 0 is land.
jpk: int
number of computational levels
"""
Expand Down
36 changes: 22 additions & 14 deletions pydomcfg/tests/bathymetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,34 +142,42 @@ def _calc_rmax(depth):
Calculate rmax: measure of steepness

This function returns the slope steepness criteria rmax, which is simply
(H[0] - H[1]) / (H[0] + H[1])
|H[0] - H[1]| / (H[0] + H[1])

Parameters
----------
depth: float
Bottom depth (units: m).
Bottom depth (units: m).

Returns
-------
rmax: float
Slope steepness value (units: None)
Slope steepness value (units: None)

Notes
-----
This function uses a "conservative approach" and rmax is overestimated.
rmax at T points is the maximum rmax estimated at any adjacent U/V point.
"""
depth = depth.reset_index(list(depth.dims))

# Loop over x and y
both_rmax = []
for dim in depth.dims:

# (H[0] - H[1]) / (H[0] + H[1])
depth_diff = depth.diff(dim)
depth_rolling_sum = depth.rolling({dim: 2}).sum().dropna(dim)
rmax = depth_diff / depth_rolling_sum
# Compute rmax
rolled = depth.rolling({dim: 2}).construct("window_dim")
diff = rolled.diff("window_dim").squeeze("window_dim")
rmax = np.abs(diff) / rolled.sum("window_dim")

# (R[0] + R[1]) / 2
rmax = rmax.rolling({dim: 2}).mean().dropna(dim)
# Construct dimension with velocity points adjacent to any T point
# We need to shift as we rolled twice and to force boundaries = NaN
rmax = rmax.rolling({dim: 2}).construct("vel_points")
rmax = rmax.shift({dim: -1})
rmax[{dim: [0, -1]}] = None

# Fill first row and column
rmax = rmax.pad({dim: (1, 1)}, constant_values=0)
both_rmax.append(rmax)

both_rmax.append(np.abs(rmax))
rmax = xr.concat(both_rmax, "vel_points")
malmans2 marked this conversation as resolved.
Show resolved Hide resolved
rmax = rmax.max("vel_points", skipna=True)

return np.maximum(*both_rmax)
return rmax.fillna(0)
malmans2 marked this conversation as resolved.
Show resolved Hide resolved