eccopy 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- eccopy/__init__.py +46 -0
- eccopy/core/__init__.py +55 -0
- eccopy/core/classification.py +1023 -0
- eccopy/core/colormaps.py +267 -0
- eccopy/core/convectivity.py +43 -0
- eccopy/core/coords.py +245 -0
- eccopy/core/data/disk_decomp/disk_decomp_r15_info.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r15_step1.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r15_step2.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r15_step3.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r15_step4.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r15_step5.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r15_step6.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r25_info.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r25_step1.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r25_step2.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r25_step3.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r25_step4.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r25_step5.mat +0 -0
- eccopy/core/data/disk_decomp/disk_decomp_r25_step6.mat +0 -0
- eccopy/core/data/disk_strels/disk_strel_r15.mat +0 -0
- eccopy/core/data/disk_strels/disk_strel_r25.mat +0 -0
- eccopy/core/data/disk_strels/disk_strel_r3.mat +0 -0
- eccopy/core/data/disk_strels/disk_strel_r5.mat +0 -0
- eccopy/core/debug.py +493 -0
- eccopy/core/disk.py +178 -0
- eccopy/core/temperature.py +250 -0
- eccopy/core/texture.py +1398 -0
- eccopy/eccopy1d/__init__.py +2 -0
- eccopy/eccopy1d/classify.py +273 -0
- eccopy/eccopy2d_h/__init__.py +2 -0
- eccopy/eccopy2d_h/classify.py +281 -0
- eccopy/eccopy2d_h/clumping.py +265 -0
- eccopy/eccopy2d_v/__init__.py +2 -0
- eccopy/eccopy2d_v/classify.py +288 -0
- eccopy/eccopy3d/__init__.py +2 -0
- eccopy/eccopy3d/classify.py +421 -0
- eccopy/eccopy3d/clumping.py +477 -0
- eccopy/params/__init__.py +6 -0
- eccopy/params/classification_params.py +151 -0
- eccopy/params/texture_params.py +66 -0
- eccopy/params/vertical_params.py +54 -0
- eccopy/params/window.py +141 -0
- eccopy/stats/__init__.py +56 -0
- eccopy/stats/basic_stats.py +370 -0
- eccopy/tests/__init__.py +0 -0
- eccopy/tests/_reference_impls/__init__.py +0 -0
- eccopy/tests/_reference_impls/isotherm_height_reference.py +54 -0
- eccopy/tests/_reference_impls/sliding_texture_reference.py +77 -0
- eccopy/tests/_reference_impls/texture_2d_reference.py +309 -0
- eccopy/tests/test_classification_1d_filter.py +151 -0
- eccopy/tests/test_classification_rain_below_melt.py +130 -0
- eccopy/tests/test_colormaps.py +213 -0
- eccopy/tests/test_coords.py +127 -0
- eccopy/tests/test_debug.py +226 -0
- eccopy/tests/test_disk_generator.py +82 -0
- eccopy/tests/test_eccopy1d.py +209 -0
- eccopy/tests/test_eccopy2d_h.py +185 -0
- eccopy/tests/test_eccopy2d_h_clumping.py +105 -0
- eccopy/tests/test_eccopy2d_v.py +359 -0
- eccopy/tests/test_eccopy3d.py +282 -0
- eccopy/tests/test_examples.py +84 -0
- eccopy/tests/test_min_valid_dbz.py +144 -0
- eccopy/tests/test_notebooks.py +111 -0
- eccopy/tests/test_numba_cross_validation.py +331 -0
- eccopy/tests/test_packaging.py +144 -0
- eccopy/tests/test_refl_texture_1d.py +167 -0
- eccopy/tests/test_stats.py +117 -0
- eccopy/tests/test_stats_sentinel.py +74 -0
- eccopy/tests/test_temperature.py +207 -0
- eccopy/tests/test_texture_kernel_modes.py +82 -0
- eccopy/tests/test_texture_params_implemented.py +148 -0
- eccopy/tests/test_vert_levels_type.py +180 -0
- eccopy/tests/test_window.py +80 -0
- eccopy-1.0.0.dist-info/METADATA +420 -0
- eccopy-1.0.0.dist-info/RECORD +79 -0
- eccopy-1.0.0.dist-info/WHEEL +5 -0
- eccopy-1.0.0.dist-info/licenses/LICENSE +39 -0
- eccopy-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
"""
|
|
2
|
+
EccoPy-3D public entry point.
|
|
3
|
+
|
|
4
|
+
Input shapes:
|
|
5
|
+
dbz : (Z, Y, X)
|
|
6
|
+
coords_z: (Z,) or (Z, Y, X) - vertical positions/spacing (km)
|
|
7
|
+
coords_y: (Y,) or (Z, Y, X) - N-S positions/spacing (km)
|
|
8
|
+
coords_x: (X,) or (Z, Y, X) - E-W positions/spacing (km)
|
|
9
|
+
height : (Z, Y, X), optional - height field, km
|
|
10
|
+
temp : (Z, Y, X), optional - temperature field, °C
|
|
11
|
+
|
|
12
|
+
Output echo type codes:
|
|
13
|
+
Without height/temp - basic clumping only:
|
|
14
|
+
1 = Stratiform, 2 = Mixed, 3 = Convective
|
|
15
|
+
With height or temp - sub-classified:
|
|
16
|
+
14 = Stratiform Low, 16 = Stratiform Mid, 18 = Stratiform High
|
|
17
|
+
25 = Mixed
|
|
18
|
+
32 = Convective Elevated, 34 = Convective Shallow,
|
|
19
|
+
36 = Convective Mid, 38 = Convective Deep
|
|
20
|
+
|
|
21
|
+
Typical usage
|
|
22
|
+
-------------
|
|
23
|
+
from eccopy import eccopy3d
|
|
24
|
+
from eccopy.params import WindowSpec
|
|
25
|
+
|
|
26
|
+
result = eccopy3d.run(
|
|
27
|
+
dbz,
|
|
28
|
+
coords_z=z_km, coords_y=y_km, coords_x=x_km,
|
|
29
|
+
height=height_km,
|
|
30
|
+
window=WindowSpec((7, 'km')),
|
|
31
|
+
)
|
|
32
|
+
echo = result.echo_type # shape (Z, Y, X)
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
from __future__ import annotations
|
|
36
|
+
import warnings
|
|
37
|
+
from dataclasses import dataclass
|
|
38
|
+
from typing import Optional, Union
|
|
39
|
+
|
|
40
|
+
import numpy as np
|
|
41
|
+
|
|
42
|
+
from ..core.coords import resolve_spacing
|
|
43
|
+
from ..core.texture import refl_texture_2d
|
|
44
|
+
from ..core.convectivity import texture_to_convectivity_linear
|
|
45
|
+
from ..core.classification import set_echo_type_3d
|
|
46
|
+
from ..core.temperature import broadcast_temp_field
|
|
47
|
+
from ..eccopy3d.clumping import find_clumps_3d
|
|
48
|
+
from ..params.window import WindowSpec
|
|
49
|
+
from ..params import TextureParams, ClassificationParams, VerticalParams
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass
|
|
53
|
+
class Result3D:
|
|
54
|
+
"""Output of eccopy3d.run()."""
|
|
55
|
+
echo_type: np.ndarray # shape (Z, Y, X)
|
|
56
|
+
convectivity: np.ndarray # shape (Z, Y, X)
|
|
57
|
+
texture: np.ndarray # shape (Z, Y, X)
|
|
58
|
+
fraction_active: np.ndarray # shape (Y, X)
|
|
59
|
+
n_clumps: int
|
|
60
|
+
# Physical radius (km) of the texture window actually used, resolved
|
|
61
|
+
# from the `window` argument against the grid spacing. Available for
|
|
62
|
+
# plotting the window footprint (see core.colormaps.draw_window_ring()).
|
|
63
|
+
# None only for a bare-pixel window on a unit-agnostic grid.
|
|
64
|
+
texture_radius: Optional[float] = None
|
|
65
|
+
# Highest height at which reflectivity reaches tp.dbz_for_echo_tops,
|
|
66
|
+
# per column - ConvStratFinder's echo-top product. Computed from
|
|
67
|
+
# reflectivity, unlike stats.echo_top_height() which works from the
|
|
68
|
+
# classification. Requires `height`; None otherwise.
|
|
69
|
+
echo_top_km: Optional[np.ndarray] = None # shape (Y, X)
|
|
70
|
+
# Populated ONLY when run(..., return_intermediates=True, levels=[...])
|
|
71
|
+
# is passed; None otherwise (default, zero extra cost). Computed via
|
|
72
|
+
# a slow, plain-Python per-point loop (core.debug.refl_texture_2d_
|
|
73
|
+
# field_debug) -- see that function and run()'s `levels` parameter.
|
|
74
|
+
fitted_dbz: Optional[np.ndarray] = None # shape (len(levels), Y, X)
|
|
75
|
+
detrended_dbz: Optional[np.ndarray] = None # shape (len(levels), Y, X)
|
|
76
|
+
intermediate_levels: Optional[np.ndarray] = None # the Z-indices fitted_dbz/detrended_dbz correspond to
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def run(dbz: Union[np.ndarray, list],
|
|
80
|
+
coords_z: Union[np.ndarray, list],
|
|
81
|
+
coords_y: Union[np.ndarray, list],
|
|
82
|
+
coords_x: Union[np.ndarray, list],
|
|
83
|
+
height: Optional[Union[np.ndarray, list]] = None,
|
|
84
|
+
temp: Optional[Union[np.ndarray, list]] = None,
|
|
85
|
+
terrain_ht: Optional[Union[np.ndarray, list]] = None,
|
|
86
|
+
window: Union[WindowSpec, int, float] = WindowSpec((7, 'km')),
|
|
87
|
+
coord_mode: str = "auto",
|
|
88
|
+
kernel_mode: str = "uniform",
|
|
89
|
+
texture_params: Optional[TextureParams] = None,
|
|
90
|
+
class_params: Optional[ClassificationParams] = None,
|
|
91
|
+
vert_params: Optional[VerticalParams] = None,
|
|
92
|
+
n_threads: int = 1,
|
|
93
|
+
return_intermediates: bool = False,
|
|
94
|
+
levels: Optional[Union[list, np.ndarray]] = None) -> Result3D:
|
|
95
|
+
"""
|
|
96
|
+
Run EccoPy-3D: 2D radial texture → convectivity → 3D clumping →
|
|
97
|
+
echo type classification.
|
|
98
|
+
|
|
99
|
+
Parameters
|
|
100
|
+
----------
|
|
101
|
+
dbz : array-like, shape (Z, Y, X)
|
|
102
|
+
Reflectivity, dBZ. NaN where missing.
|
|
103
|
+
coords_z : array-like, shape (Z,) or (Z, Y, X)
|
|
104
|
+
Vertical coordinate or spacing, km.
|
|
105
|
+
coords_y : array-like, shape (Y,) or (Z, Y, X)
|
|
106
|
+
N-S coordinate or spacing, km.
|
|
107
|
+
coords_x : array-like, shape (X,) or (Z, Y, X)
|
|
108
|
+
E-W coordinate or spacing, km.
|
|
109
|
+
height : array-like, shape (Z, Y, X), optional
|
|
110
|
+
Height field, km. AGL preferred; MSL acceptable if terrain flat.
|
|
111
|
+
If provided, enables sub-classification into Low/Mid/High and
|
|
112
|
+
Shallow/Mid/Deep/Elevated convective codes.
|
|
113
|
+
temp : array-like, shape (Z, Y, X) or (Z,), optional
|
|
114
|
+
Temperature field, °C. Used if `height` not provided. Accepts
|
|
115
|
+
either a full (Z, Y, X) field, or a single vertical profile of
|
|
116
|
+
shape (Z,) - e.g. a sounding - which is broadcast identically
|
|
117
|
+
across every horizontal point.
|
|
118
|
+
window : WindowSpec or float (km)
|
|
119
|
+
Texture window radius. Must be a length unit.
|
|
120
|
+
coord_mode : {'auto', 'position', 'spacing'}
|
|
121
|
+
How to interpret coord arrays.
|
|
122
|
+
kernel_mode : {'uniform', 'varying'}
|
|
123
|
+
How to handle spatially-varying horizontal grid spacing (e.g.
|
|
124
|
+
lat/lon grids, where dx shrinks with cos(latitude)):
|
|
125
|
+
'uniform' (default) - one representative kernel for the whole
|
|
126
|
+
grid, built from the median dy/dx at each level. Matches
|
|
127
|
+
what the validated LROSE ConvStratFinder algorithm itself
|
|
128
|
+
does for lat/lon grids (a single dx/dy computed at the
|
|
129
|
+
domain's mean latitude). Fast; becomes approximate for
|
|
130
|
+
domains spanning a wide latitude range.
|
|
131
|
+
'varying' - rebuilds the kernel from the local spacing at
|
|
132
|
+
every grid point, so the physical kernel size stays
|
|
133
|
+
correct across large/non-uniform domains. This is NOT
|
|
134
|
+
what LROSE does and is unvalidated against LROSE output;
|
|
135
|
+
substantially slower. Recommended only for domains where
|
|
136
|
+
'uniform' is known to break down (e.g. continental-scale
|
|
137
|
+
lat/lon grids).
|
|
138
|
+
texture_params, class_params, vert_params : optional param objects
|
|
139
|
+
n_threads : int
|
|
140
|
+
Parallel threads for per-level texture computation.
|
|
141
|
+
return_intermediates : bool
|
|
142
|
+
If True, also compute and attach `fitted_dbz` and `detrended_dbz`
|
|
143
|
+
(the plane-fit value and detrended value at each point's own
|
|
144
|
+
location, prior to texture) for the Z-levels listed in `levels`
|
|
145
|
+
-- see core.debug.refl_texture_2d_field_debug(). This is SLOW (a
|
|
146
|
+
plain Python double loop per level); default False skips it
|
|
147
|
+
entirely at no cost. Ignored if `levels` is None/empty.
|
|
148
|
+
levels : list or np.ndarray of int, optional
|
|
149
|
+
Which Z-indices to compute fitted_dbz/detrended_dbz for when
|
|
150
|
+
return_intermediates=True. Required (and must be non-empty) to
|
|
151
|
+
actually get intermediates back -- there is no "all levels"
|
|
152
|
+
default, since that would silently run the slow per-point loop
|
|
153
|
+
over the entire volume. Ignored if return_intermediates=False.
|
|
154
|
+
|
|
155
|
+
Returns
|
|
156
|
+
-------
|
|
157
|
+
Result3D
|
|
158
|
+
"""
|
|
159
|
+
dbz = np.asarray(dbz, dtype=float)
|
|
160
|
+
if dbz.ndim != 3:
|
|
161
|
+
raise ValueError(
|
|
162
|
+
f"eccopy3d expects a 3-D (Z, Y, X) dbz array; got shape {dbz.shape}"
|
|
163
|
+
)
|
|
164
|
+
nz, ny, nx = dbz.shape
|
|
165
|
+
|
|
166
|
+
tp = texture_params or TextureParams()
|
|
167
|
+
cp = class_params or ClassificationParams()
|
|
168
|
+
vp = vert_params or VerticalParams()
|
|
169
|
+
|
|
170
|
+
# --- resolve coords → per-point spacing (km) ---
|
|
171
|
+
def _resolve_1d_or_nd(arr, axis, size, name):
|
|
172
|
+
arr = np.asarray(arr, dtype=float)
|
|
173
|
+
if arr.ndim == 1:
|
|
174
|
+
if arr.shape[0] != size:
|
|
175
|
+
raise ValueError(f"{name} length {arr.shape[0]} != {size}")
|
|
176
|
+
sp_1d = resolve_spacing(arr, axis=0, mode=coord_mode)
|
|
177
|
+
return sp_1d
|
|
178
|
+
return resolve_spacing(arr, axis=axis, mode=coord_mode)
|
|
179
|
+
|
|
180
|
+
sp_z_1d = _resolve_1d_or_nd(coords_z, 0, nz, "coords_z")
|
|
181
|
+
sp_y_1d = _resolve_1d_or_nd(coords_y, 0, ny, "coords_y")
|
|
182
|
+
sp_x_1d = _resolve_1d_or_nd(coords_x, 0, nx, "coords_x")
|
|
183
|
+
|
|
184
|
+
# Build 2D (Y, X) spacing arrays for texture calculation
|
|
185
|
+
sp_y_2d = np.broadcast_to(sp_y_1d[:, np.newaxis], (ny, nx)).copy()
|
|
186
|
+
sp_x_2d = np.broadcast_to(sp_x_1d[np.newaxis, :], (ny, nx)).copy()
|
|
187
|
+
|
|
188
|
+
# --- dz for 3-D cell-volume / clump geometry: use the EXACT
|
|
189
|
+
# ConvStratFinder/PjgGridGeom::dzKm() centered-difference convention
|
|
190
|
+
# (iz=0: z[1]-z[0]; iz=last: z[last]-z[last-1]; else average of the
|
|
191
|
+
# two neighbouring gaps) rather than resolve_spacing()'s simple
|
|
192
|
+
# forward-difference. These are IDENTICAL for uniform Z grids (SPOL,
|
|
193
|
+
# SEA) but diverge for non-uniform ones (e.g. WRF: 0.25 km near-
|
|
194
|
+
# surface, coarsening aloft) -- a real bug found by comparing against
|
|
195
|
+
# StormClump::computeGeom() in the C++ source. Scoped to this 3-D
|
|
196
|
+
# volume-geometry path only; does NOT touch resolve_spacing() itself,
|
|
197
|
+
# so the already-validated 2D-V texture-kernel spacing is unaffected.
|
|
198
|
+
coords_z_arr = np.asarray(coords_z, dtype=float)
|
|
199
|
+
dz_z_1d = sp_z_1d
|
|
200
|
+
if coords_z_arr.ndim == 1 and coords_z_arr.shape[0] == nz and nz > 1:
|
|
201
|
+
diffs = np.diff(coords_z_arr)
|
|
202
|
+
finite = diffs[np.isfinite(diffs)]
|
|
203
|
+
if finite.size and (np.all(finite > 0) or np.all(finite < 0)):
|
|
204
|
+
dz_z_1d = np.empty(nz, dtype=float)
|
|
205
|
+
dz_z_1d[0] = coords_z_arr[1] - coords_z_arr[0]
|
|
206
|
+
dz_z_1d[-1] = coords_z_arr[-1] - coords_z_arr[-2]
|
|
207
|
+
if nz > 2:
|
|
208
|
+
dz_z_1d[1:-1] = (coords_z_arr[2:] - coords_z_arr[:-2]) / 2.0
|
|
209
|
+
dz_z_1d = np.abs(dz_z_1d)
|
|
210
|
+
|
|
211
|
+
# Build 3D (Z, Y, X) spacing array for clumping volume calculation.
|
|
212
|
+
# Use geometric mean of the three axis spacings as a representative
|
|
213
|
+
# per-cell size (assumes roughly cuboid cells; adequate for volume
|
|
214
|
+
# filtering at the precision needed by the 30 km³ minimum).
|
|
215
|
+
sp_z_3d = np.broadcast_to(
|
|
216
|
+
dz_z_1d[:, np.newaxis, np.newaxis], (nz, ny, nx)
|
|
217
|
+
).copy()
|
|
218
|
+
sp_y_3d = np.broadcast_to(
|
|
219
|
+
sp_y_1d[np.newaxis, :, np.newaxis], (nz, ny, nx)
|
|
220
|
+
).copy()
|
|
221
|
+
sp_x_3d = np.broadcast_to(
|
|
222
|
+
sp_x_1d[np.newaxis, np.newaxis, :], (nz, ny, nx)
|
|
223
|
+
).copy()
|
|
224
|
+
# Per-cell volume = dz * dy * dx; store geometric mean as "spacing"
|
|
225
|
+
# so _compute_geom()'s spacing**3 gives the correct cell volume.
|
|
226
|
+
sp_3d = np.cbrt(sp_z_3d * sp_y_3d * sp_x_3d)
|
|
227
|
+
|
|
228
|
+
# Resolve texture radius in km
|
|
229
|
+
if isinstance(window, (int, float)):
|
|
230
|
+
radius_km = float(window)
|
|
231
|
+
elif isinstance(window, WindowSpec):
|
|
232
|
+
if window.is_pixel:
|
|
233
|
+
med_sp = float(np.nanmedian(np.stack([sp_y_2d, sp_x_2d])))
|
|
234
|
+
radius_km = window.size * med_sp
|
|
235
|
+
else:
|
|
236
|
+
if window._base_kind != "length_m":
|
|
237
|
+
raise ValueError(
|
|
238
|
+
"eccopy3d texture window must be a length unit (km); "
|
|
239
|
+
f"got unit kind '{window._base_kind}'."
|
|
240
|
+
)
|
|
241
|
+
radius_km = window._base_value / 1000.0
|
|
242
|
+
else:
|
|
243
|
+
radius_km = float(window)
|
|
244
|
+
|
|
245
|
+
# 0. Restrict the analysis to the valid height band, matching
|
|
246
|
+
# ConvStratFinder's min/max valid height. Applied BEFORE texture so
|
|
247
|
+
# excluded levels cannot contribute to any downstream statistic. Uses
|
|
248
|
+
# the `height` field when one is supplied, otherwise the vertical
|
|
249
|
+
# coordinate. The defaults (0 to 25 km) span any realistic radar or
|
|
250
|
+
# model grid, so this is inert unless deliberately narrowed.
|
|
251
|
+
if vp.min_valid_height > -np.inf or vp.max_valid_height < np.inf:
|
|
252
|
+
if height is not None:
|
|
253
|
+
height_for_band = np.broadcast_to(
|
|
254
|
+
np.asarray(height, dtype=float), dbz.shape)
|
|
255
|
+
else:
|
|
256
|
+
height_for_band = np.broadcast_to(
|
|
257
|
+
coords_z_arr.reshape(-1, 1, 1) if coords_z_arr.ndim == 1
|
|
258
|
+
else np.asarray(coords_z_arr, dtype=float), dbz.shape)
|
|
259
|
+
outside = ((height_for_band < vp.min_valid_height)
|
|
260
|
+
| (height_for_band > vp.max_valid_height))
|
|
261
|
+
if outside.any():
|
|
262
|
+
dbz = np.where(outside, np.nan, dbz)
|
|
263
|
+
|
|
264
|
+
# Echo top: highest level at which reflectivity reaches
|
|
265
|
+
# dbz_for_echo_tops, matching the topKm loop in
|
|
266
|
+
# ConvStratFinder::_computeDbzColMax(). Needs a height field.
|
|
267
|
+
echo_top_km = None
|
|
268
|
+
if height is not None:
|
|
269
|
+
height_3d = np.broadcast_to(np.asarray(height, dtype=float), dbz.shape)
|
|
270
|
+
reaches = np.isfinite(dbz) & (dbz >= tp.dbz_for_echo_tops)
|
|
271
|
+
# -inf rather than NaN as the fill, so columns with no qualifying
|
|
272
|
+
# echo do not trigger numpy's all-NaN-slice warning.
|
|
273
|
+
top = np.where(reaches, height_3d, -np.inf).max(axis=0)
|
|
274
|
+
echo_top_km = np.where(reaches.any(axis=0), top, np.nan)
|
|
275
|
+
|
|
276
|
+
# 1. 2D radial texture per level
|
|
277
|
+
if tp.use_dbz_col_max:
|
|
278
|
+
# Compute texture once from the column-maximum reflectivity and copy
|
|
279
|
+
# it to every level that has echo - ConvStratFinder's _useDbzColMax
|
|
280
|
+
# branch. Cheaper than a per-level texture, and appropriate when the
|
|
281
|
+
# classification is meant to be columnar.
|
|
282
|
+
col_max = np.where(np.isfinite(dbz).any(axis=0),
|
|
283
|
+
np.nanmax(dbz, axis=0), np.nan)
|
|
284
|
+
texture_2d, fraction_active = refl_texture_2d(
|
|
285
|
+
col_max[np.newaxis, :, :],
|
|
286
|
+
texture_radius=radius_km,
|
|
287
|
+
dy=sp_y_2d,
|
|
288
|
+
dx=sp_x_2d,
|
|
289
|
+
base_dbz=tp.dbz_base,
|
|
290
|
+
min_valid_dbz=tp.resolve_min_valid_dbz(0.0),
|
|
291
|
+
min_frac_texture=tp.min_frac_texture,
|
|
292
|
+
min_frac_fit=tp.min_frac_fit,
|
|
293
|
+
n_threads=n_threads,
|
|
294
|
+
kernel_mode=kernel_mode,
|
|
295
|
+
)
|
|
296
|
+
texture = np.where(np.isfinite(dbz), texture_2d[0][np.newaxis, :, :], np.nan)
|
|
297
|
+
else:
|
|
298
|
+
texture, fraction_active = refl_texture_2d(
|
|
299
|
+
dbz,
|
|
300
|
+
texture_radius=radius_km,
|
|
301
|
+
dy=sp_y_2d,
|
|
302
|
+
dx=sp_x_2d,
|
|
303
|
+
base_dbz=tp.dbz_base,
|
|
304
|
+
min_valid_dbz=tp.resolve_min_valid_dbz(0.0),
|
|
305
|
+
min_frac_texture=tp.min_frac_texture,
|
|
306
|
+
min_frac_fit=tp.min_frac_fit,
|
|
307
|
+
n_threads=n_threads,
|
|
308
|
+
kernel_mode=kernel_mode,
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
# 2. Convectivity
|
|
312
|
+
conv = texture_to_convectivity_linear(
|
|
313
|
+
texture,
|
|
314
|
+
upper_lim=tp.texture_limit_high,
|
|
315
|
+
lower_lim=tp.texture_limit_low,
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
# 3. 3D clumping
|
|
319
|
+
height_arr = np.asarray(height, dtype=float) if height is not None else None
|
|
320
|
+
temp_arr = broadcast_temp_field(temp, dbz.shape) if temp is not None else None
|
|
321
|
+
terrain_ht_arr = np.asarray(terrain_ht, dtype=float) if terrain_ht is not None else None
|
|
322
|
+
|
|
323
|
+
# Resolve which field assigns vertical levels. find_clumps_3d() and
|
|
324
|
+
# set_echo_type_3d() both prefer height whenever it is supplied, so
|
|
325
|
+
# honouring vert_levels_type="by_temp" means withholding height from
|
|
326
|
+
# them. Do it here rather than inside those functions so the choice is
|
|
327
|
+
# made once, visibly, at the entry point.
|
|
328
|
+
if vp.vert_levels_type == "by_temp":
|
|
329
|
+
if temp_arr is None:
|
|
330
|
+
raise ValueError(
|
|
331
|
+
"vert_params.vert_levels_type='by_temp' requires a temp field, "
|
|
332
|
+
"but temp=None was passed."
|
|
333
|
+
)
|
|
334
|
+
if height_arr is not None:
|
|
335
|
+
warnings.warn(
|
|
336
|
+
"vert_params.vert_levels_type='by_temp', so vertical levels are "
|
|
337
|
+
"assigned from `temp` and the `height` field you supplied is NOT "
|
|
338
|
+
"used for that purpose. Pass vert_levels_type='by_height' to use "
|
|
339
|
+
"height instead.",
|
|
340
|
+
UserWarning, stacklevel=2,
|
|
341
|
+
)
|
|
342
|
+
height_arr = None
|
|
343
|
+
# "by_height" with no height supplied falls through to temp, silently:
|
|
344
|
+
# passing temp alone is an unambiguous request, not a mistake, and the
|
|
345
|
+
# default value of vert_levels_type cannot be distinguished from an
|
|
346
|
+
# explicit one.
|
|
347
|
+
|
|
348
|
+
clumps = find_clumps_3d(
|
|
349
|
+
conv,
|
|
350
|
+
spacing=sp_3d,
|
|
351
|
+
min_conv=cp.min_convectivity_for_convective,
|
|
352
|
+
min_vol_km3=cp.min_valid_volume_for_convective,
|
|
353
|
+
height_km=height_arr,
|
|
354
|
+
temp=temp_arr,
|
|
355
|
+
shallow_threshold_ht=vp.shallow_threshold_ht,
|
|
356
|
+
deep_threshold_ht=vp.deep_threshold_ht,
|
|
357
|
+
shallow_threshold_temp=vp.shallow_threshold_temp,
|
|
358
|
+
deep_threshold_temp=vp.deep_threshold_temp,
|
|
359
|
+
use_dual_thresholds=cp.use_dual_thresholds,
|
|
360
|
+
secondary_threshold=cp.secondary_convectivity,
|
|
361
|
+
all_subclumps_min_area_frac=cp.all_subclumps_min_area_frac,
|
|
362
|
+
each_subclump_min_area_frac=cp.each_subclump_min_area_frac,
|
|
363
|
+
each_subclump_min_area_km2=cp.each_subclump_min_area_km2,
|
|
364
|
+
dx_km=float(np.median(sp_x_1d)),
|
|
365
|
+
dy_km=float(np.median(sp_y_1d)),
|
|
366
|
+
dz0_km=float(dz_z_1d[0]),
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
# 4. Assign echo type codes
|
|
370
|
+
echo = set_echo_type_3d(
|
|
371
|
+
conv,
|
|
372
|
+
clumps=clumps,
|
|
373
|
+
height_km=height_arr,
|
|
374
|
+
temp=temp_arr,
|
|
375
|
+
shallow_threshold_ht=vp.shallow_threshold_ht,
|
|
376
|
+
deep_threshold_ht=vp.deep_threshold_ht,
|
|
377
|
+
shallow_threshold_temp=vp.shallow_threshold_temp,
|
|
378
|
+
deep_threshold_temp=vp.deep_threshold_temp,
|
|
379
|
+
terrain_ht_km=terrain_ht_arr,
|
|
380
|
+
min_ht_agl_for_mid=cp.min_ht_km_agl_for_mid,
|
|
381
|
+
min_ht_agl_for_deep=cp.min_ht_km_agl_for_deep,
|
|
382
|
+
max_conv_for_strat=cp.max_convectivity_for_stratiform,
|
|
383
|
+
min_conv_for_conv=cp.min_convectivity_for_convective,
|
|
384
|
+
min_vol_km3=cp.min_valid_volume_for_convective,
|
|
385
|
+
min_vert_extent_km=cp.min_vert_extent_for_convective,
|
|
386
|
+
min_conv_frac_deep=cp.min_conv_fraction_for_deep,
|
|
387
|
+
min_conv_frac_shallow=cp.min_conv_fraction_for_shallow,
|
|
388
|
+
max_shallow_frac_elevated=cp.max_shallow_conv_fraction_for_elevated,
|
|
389
|
+
max_deep_frac_elevated=cp.max_deep_conv_fraction_for_elevated,
|
|
390
|
+
min_strat_frac_strat_below=cp.min_strat_fraction_for_strat_below,
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
fitted_dbz = detrended_dbz = intermediate_levels = None
|
|
394
|
+
if return_intermediates and levels is not None and len(levels) > 0:
|
|
395
|
+
from ..core.debug import refl_texture_2d_field_debug
|
|
396
|
+
intermediate_levels = np.asarray(levels, dtype=int)
|
|
397
|
+
fitted_list, detrended_list = [], []
|
|
398
|
+
for iz in intermediate_levels:
|
|
399
|
+
f, d, _ = refl_texture_2d_field_debug(
|
|
400
|
+
dbz[iz], radius_km, dy=sp_y_2d, dx=sp_x_2d,
|
|
401
|
+
base_dbz=tp.dbz_base, min_valid_dbz=tp.resolve_min_valid_dbz(0.0),
|
|
402
|
+
min_frac_texture=tp.min_frac_texture, min_frac_fit=tp.min_frac_fit,
|
|
403
|
+
kernel_mode=kernel_mode,
|
|
404
|
+
)
|
|
405
|
+
fitted_list.append(f)
|
|
406
|
+
detrended_list.append(d)
|
|
407
|
+
fitted_dbz = np.stack(fitted_list, axis=0)
|
|
408
|
+
detrended_dbz = np.stack(detrended_list, axis=0)
|
|
409
|
+
|
|
410
|
+
return Result3D(
|
|
411
|
+
echo_type=echo,
|
|
412
|
+
convectivity=conv,
|
|
413
|
+
texture=texture,
|
|
414
|
+
fraction_active=fraction_active,
|
|
415
|
+
n_clumps=len(clumps),
|
|
416
|
+
texture_radius=radius_km,
|
|
417
|
+
echo_top_km=echo_top_km,
|
|
418
|
+
fitted_dbz=fitted_dbz,
|
|
419
|
+
detrended_dbz=detrended_dbz,
|
|
420
|
+
intermediate_levels=intermediate_levels,
|
|
421
|
+
)
|