Skip to content

Commit 0bc4f0b

Browse files
committed
test 999999 == None and final cleanup
1 parent a619fb3 commit 0bc4f0b

3 files changed

Lines changed: 26 additions & 32 deletions

File tree

pydomcfg/domzgr/zco.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ def __call__(
124124
self._sigmas = self._compute_sigma(self._z)
125125

126126
# Compute z3 depths of zco vertical levels
127-
zco_z3 = self._zco_z3
127+
z3t, z3w = self._zco_z3
128128

129129
# Compute e3 scale factors (cell thicknesses)
130-
zco_e3 = self._compute_e3(*zco_z3) if self._ln_e3_dep else self._analyt_e3
130+
e3t, e3w = self._compute_e3(z3t, z3w) if self._ln_e3_dep else self._analyt_e3
131131

132-
return self._merge_z3_and_e3(*zco_z3, *zco_e3)
132+
return self._merge_z3_and_e3(z3t, z3w, e3t, e3w)
133133

134134
# --------------------------------------------------------------------------
135135
def _compute_pp(
@@ -211,7 +211,7 @@ def _zco_z3(self) -> Tuple[DataArray, ...]:
211211
sigmas = self._sigmas
212212
sigmas_p1 = self._compute_sigma(self._z + 1)
213213

214-
zco_z3 = []
214+
both_z3 = []
215215
for grid, sigma, sigma_p1 in zip(grids, sigmas, sigmas_p1):
216216

217217
if self._is_uniform:
@@ -227,25 +227,21 @@ def _zco_z3(self) -> Tuple[DataArray, ...]:
227227
a1 = self._ppsur
228228
a2 = self._ppa0 * (self._jpk - 1.0)
229229
a3 = self._ppa1 * self._ppacr
230-
231-
# Compute z3
232230
z3 = self._compute_z3(su, s1, a1, a2, a3)
233231

234-
# Double tanh
235232
if self._ldbletanh:
236233
# Add double tanh term
237234
ss2 = self._stretch_zco(-sigma, self._ldbletanh)
238235
a4 = self._ppa2 * self._ppacr2
239236
z3 += ss2 * a4
240237

241-
# Force first w-level to be exactly at zero
242238
if grid == "W":
239+
# Force first w-level to be exactly at zero
243240
z3[{"z": 0}] = 0.0
244241

245-
# Append
246-
zco_z3 += [z3]
242+
both_z3 += [z3]
247243

248-
return tuple(zco_z3)
244+
return tuple(both_z3)
249245

250246
# --------------------------------------------------------------------------
251247
@property
@@ -255,33 +251,29 @@ def _analyt_e3(self) -> Tuple[DataArray, ...]:
255251
Return e3{t,w} as analytical derivative of depth function z3{t,w}.
256252
"""
257253

258-
# Return here if is uniform
259254
if self._is_uniform:
260-
# 0d DataArray
255+
# Uniform: Return 0d DataArrays
261256
e3 = DataArray((self._pphmax / (self._jpk - 1.0)))
262257
return tuple([e3, e3])
263258

264-
# Stretched zco grid
265-
zco_e3 = []
259+
both_e3 = []
266260
for sigma in self._sigmas:
261+
# Stretched zco grid
267262
a0 = self._ppa0
268263
a1 = self._ppa1
269264
kk = -sigma * (self._jpk - 1.0) + 1.0
270265
tanh1 = np.tanh((kk - self._ppkth) / self._ppacr)
271-
272-
# Compute e3
273266
e3 = a0 + a1 * tanh1
274267

275-
# Add double tanh term
276268
if self._ldbletanh:
269+
# Add double tanh term
277270
a2 = self._ppa2
278271
tanh2 = np.tanh((kk - self._ppkth2) / self._ppacr2)
279272
e3 += a2 * tanh2
280273

281-
# Append
282-
zco_e3 += [e3]
274+
both_e3 += [e3]
283275

284-
return tuple(zco_e3)
276+
return tuple(both_e3)
285277

286278
def _get_ldbletanh_and_pp2(
287279
self, ldbletanh: Optional[bool], pp2: Tuple[Optional[float], ...]
@@ -306,16 +298,18 @@ def _get_ldbletanh_and_pp2(
306298
# ldbletanh False and double tanh coefficients specified
307299
warning_msg = f"{prefix_msg} are ignored when ldbletanh is False"
308300
else:
301+
# All good
309302
warning_msg = ""
310303

311304
if warning_msg:
305+
# Warn and return dummy values
312306
warnings.warn(warning_msg)
313307
return (False, (0, 0, 0))
314308

315-
# Errors: Wrong types
309+
# Errors: pp have inconsistent types
316310
if ldbletanh is True and any(pp_are_none):
317311
raise ValueError(f"{prefix_msg} MUST be all float when ldbletanh is True")
318312
if ldbletanh is None and (any(pp_are_none) and not all(pp_are_none)):
319-
raise ValueError(f"{prefix_msg} MUST be all None or float")
313+
raise ValueError(f"{prefix_msg} MUST be all None or all float")
320314

321315
return (ldbletanh_out, tuple(pp or 0 for pp in pp2))

pydomcfg/domzgr/zgr.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ def _compute_e3(z3t: DataArray, z3w: DataArray) -> Tuple[DataArray, ...]:
107107
(central-difference) of levels' depth (z3{t,w}).
108108
"""
109109

110-
e3s = []
111-
for z3, k in zip((z3t, z3w), (-1, 0)):
110+
both_e3 = []
111+
for z3, k_to_fill in zip((z3t, z3w), (-1, 0)):
112112
diff = z3.diff("z")
113-
fill = 2.0 * (z3t[{"z": k}] - z3w[{"z": k}])
114-
e3s += [xr.concat([diff, fill], "z")]
113+
fill = 2.0 * (z3t[{"z": k_to_fill}] - z3w[{"z": k_to_fill}])
114+
both_e3 += [xr.concat([diff, fill], "z")]
115115

116-
return tuple(e3s)
116+
return tuple(both_e3)
117117

118118
# --------------------------------------------------------------------------
119119
def _merge_z3_and_e3(

pydomcfg/tests/test_zco.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_zco_errors():
105105

106106
# Without ldbletanh flag, only allow all pps set or none of them
107107
with pytest.raises(
108-
ValueError, match="ppa2, ppkth2 and ppacr2 MUST be all None or float"
108+
ValueError, match="ppa2, ppkth2 and ppacr2 MUST be all None or all float"
109109
):
110110
zco(**kwargs, ppa2=1, ppkth2=1, ppacr2=None)
111111

@@ -121,16 +121,16 @@ def test_zco_warnings():
121121
ds_bathy = Bathymetry(1.0e3, 1.2e3, 1, 1).flat(5.0e3)
122122
zco = Zco(ds_bathy, jpk=10)
123123

124-
# Uniform: Ignore coefficients controlling stretching
124+
# Uniform: Ignore stretching
125125
kwargs = dict(ppdzmin=10, pphmax=5.0e3, ppkth=0, ppacr=0)
126-
expected = zco(**kwargs, ppsur=None, ppa0=None, ppa1=None)
126+
expected = zco(**kwargs, ppsur=None, ppa0=999_999, ppa1=None)
127127
with pytest.warns(
128128
UserWarning, match="ppsur, ppa0 and ppa1 are ignored when ppacr == ppkth == 0"
129129
):
130130
actual = zco(**kwargs, ppsur=2, ppa0=2, ppa1=2)
131131
xr.testing.assert_identical(expected, actual)
132132

133-
# ldbletanh = False : Ignore parameters controlling double tanh
133+
# ldbletanh OFF: Ignore double tanh
134134
kwargs = dict(ppdzmin=10, pphmax=5.0e3, ldbletanh=False)
135135
expected = zco(**kwargs, ppa2=None, ppkth2=None, ppacr2=None)
136136
with pytest.warns(

0 commit comments

Comments
 (0)