Skip to content

Commit 4fb1bdd

Browse files
committed
including calc_rmax in utils to be used with sco class
1 parent db12bd9 commit 4fb1bdd

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

pydomcfg/utils.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import xarray as xr
99
from xarray import DataArray, Dataset
1010

11-
11+
# -----------------------------------------------------------------------------
1212
def is_nemo_none(var: Optional[float] = None) -> bool:
1313
"""
1414
Assess if a namelist parameter is None
@@ -25,7 +25,50 @@ def is_nemo_none(var: Optional[float] = None) -> bool:
2525
"""
2626
return var in [None, 999999.0]
2727

28+
# -----------------------------------------------------------------------------
29+
def calc_rmax(depth: DataArray) -> DataArray:
30+
"""
31+
Calculate rmax: measure of steepness
32+
This function returns the maximum slope paramater
33+
34+
rmax = abs(Hb - Ha) / (Ha + Hb)
35+
36+
where Ha and Hb are the depths of adjacent grid cells (Mellor et al 1998).
37+
38+
Reference:
39+
*) Mellor, Oey & Ezer, J Atm. Oce. Tech. 15(5):1122-1131, 1998.
40+
41+
Parameters
42+
----------
43+
depth: DataArray
44+
Bottom depth (units: m).
45+
Returns
46+
-------
47+
rmax: DataArray
48+
Maximum slope parameter (units: None)
49+
"""
50+
51+
depth = depth.reset_index(list(depth.dims))
52+
53+
both_rmax = []
54+
for dim in depth.dims:
55+
56+
# (Hb - Ha) / (Ha + Hb)
57+
depth_diff = depth.diff(dim)
58+
depth_rolling_sum = depth.rolling({dim: 2}).sum().dropna(dim)
59+
rmax = depth_diff / depth_rolling_sum
60+
61+
# (rmax_a + rmax_b) / 2
62+
rmax = rmax.rolling({dim: 2}).mean().dropna(dim)
63+
64+
# Fill first row and column
65+
rmax = rmax.pad({dim: (1, 1)}, constant_values=0)
66+
67+
both_rmax.append(np.abs(rmax))
68+
69+
return np.maximum(*both_rmax)
2870

71+
# -----------------------------------------------------------------------------
2972
def generate_cartesian_grid(
3073
ppe1_m,
3174
ppe2_m,

0 commit comments

Comments
 (0)