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.
Files changed (79) hide show
  1. eccopy/__init__.py +46 -0
  2. eccopy/core/__init__.py +55 -0
  3. eccopy/core/classification.py +1023 -0
  4. eccopy/core/colormaps.py +267 -0
  5. eccopy/core/convectivity.py +43 -0
  6. eccopy/core/coords.py +245 -0
  7. eccopy/core/data/disk_decomp/disk_decomp_r15_info.mat +0 -0
  8. eccopy/core/data/disk_decomp/disk_decomp_r15_step1.mat +0 -0
  9. eccopy/core/data/disk_decomp/disk_decomp_r15_step2.mat +0 -0
  10. eccopy/core/data/disk_decomp/disk_decomp_r15_step3.mat +0 -0
  11. eccopy/core/data/disk_decomp/disk_decomp_r15_step4.mat +0 -0
  12. eccopy/core/data/disk_decomp/disk_decomp_r15_step5.mat +0 -0
  13. eccopy/core/data/disk_decomp/disk_decomp_r15_step6.mat +0 -0
  14. eccopy/core/data/disk_decomp/disk_decomp_r25_info.mat +0 -0
  15. eccopy/core/data/disk_decomp/disk_decomp_r25_step1.mat +0 -0
  16. eccopy/core/data/disk_decomp/disk_decomp_r25_step2.mat +0 -0
  17. eccopy/core/data/disk_decomp/disk_decomp_r25_step3.mat +0 -0
  18. eccopy/core/data/disk_decomp/disk_decomp_r25_step4.mat +0 -0
  19. eccopy/core/data/disk_decomp/disk_decomp_r25_step5.mat +0 -0
  20. eccopy/core/data/disk_decomp/disk_decomp_r25_step6.mat +0 -0
  21. eccopy/core/data/disk_strels/disk_strel_r15.mat +0 -0
  22. eccopy/core/data/disk_strels/disk_strel_r25.mat +0 -0
  23. eccopy/core/data/disk_strels/disk_strel_r3.mat +0 -0
  24. eccopy/core/data/disk_strels/disk_strel_r5.mat +0 -0
  25. eccopy/core/debug.py +493 -0
  26. eccopy/core/disk.py +178 -0
  27. eccopy/core/temperature.py +250 -0
  28. eccopy/core/texture.py +1398 -0
  29. eccopy/eccopy1d/__init__.py +2 -0
  30. eccopy/eccopy1d/classify.py +273 -0
  31. eccopy/eccopy2d_h/__init__.py +2 -0
  32. eccopy/eccopy2d_h/classify.py +281 -0
  33. eccopy/eccopy2d_h/clumping.py +265 -0
  34. eccopy/eccopy2d_v/__init__.py +2 -0
  35. eccopy/eccopy2d_v/classify.py +288 -0
  36. eccopy/eccopy3d/__init__.py +2 -0
  37. eccopy/eccopy3d/classify.py +421 -0
  38. eccopy/eccopy3d/clumping.py +477 -0
  39. eccopy/params/__init__.py +6 -0
  40. eccopy/params/classification_params.py +151 -0
  41. eccopy/params/texture_params.py +66 -0
  42. eccopy/params/vertical_params.py +54 -0
  43. eccopy/params/window.py +141 -0
  44. eccopy/stats/__init__.py +56 -0
  45. eccopy/stats/basic_stats.py +370 -0
  46. eccopy/tests/__init__.py +0 -0
  47. eccopy/tests/_reference_impls/__init__.py +0 -0
  48. eccopy/tests/_reference_impls/isotherm_height_reference.py +54 -0
  49. eccopy/tests/_reference_impls/sliding_texture_reference.py +77 -0
  50. eccopy/tests/_reference_impls/texture_2d_reference.py +309 -0
  51. eccopy/tests/test_classification_1d_filter.py +151 -0
  52. eccopy/tests/test_classification_rain_below_melt.py +130 -0
  53. eccopy/tests/test_colormaps.py +213 -0
  54. eccopy/tests/test_coords.py +127 -0
  55. eccopy/tests/test_debug.py +226 -0
  56. eccopy/tests/test_disk_generator.py +82 -0
  57. eccopy/tests/test_eccopy1d.py +209 -0
  58. eccopy/tests/test_eccopy2d_h.py +185 -0
  59. eccopy/tests/test_eccopy2d_h_clumping.py +105 -0
  60. eccopy/tests/test_eccopy2d_v.py +359 -0
  61. eccopy/tests/test_eccopy3d.py +282 -0
  62. eccopy/tests/test_examples.py +84 -0
  63. eccopy/tests/test_min_valid_dbz.py +144 -0
  64. eccopy/tests/test_notebooks.py +111 -0
  65. eccopy/tests/test_numba_cross_validation.py +331 -0
  66. eccopy/tests/test_packaging.py +144 -0
  67. eccopy/tests/test_refl_texture_1d.py +167 -0
  68. eccopy/tests/test_stats.py +117 -0
  69. eccopy/tests/test_stats_sentinel.py +74 -0
  70. eccopy/tests/test_temperature.py +207 -0
  71. eccopy/tests/test_texture_kernel_modes.py +82 -0
  72. eccopy/tests/test_texture_params_implemented.py +148 -0
  73. eccopy/tests/test_vert_levels_type.py +180 -0
  74. eccopy/tests/test_window.py +80 -0
  75. eccopy-1.0.0.dist-info/METADATA +420 -0
  76. eccopy-1.0.0.dist-info/RECORD +79 -0
  77. eccopy-1.0.0.dist-info/WHEEL +5 -0
  78. eccopy-1.0.0.dist-info/licenses/LICENSE +39 -0
  79. eccopy-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,250 @@
1
+ """
2
+ Temperature field utilities.
3
+
4
+ isotherm_height() - port of Ecco::_computeHts from Ecco.cc
5
+ """
6
+
7
+ from __future__ import annotations
8
+ import numpy as np
9
+ from numba import njit
10
+
11
+
12
+ @njit(cache=True)
13
+ def _isotherm_height_core(temp_3d: np.ndarray,
14
+ z_levels: np.ndarray,
15
+ target_temp_c: float) -> np.ndarray:
16
+ """
17
+ JIT-compiled core loop for isotherm_height(). Pure numeric logic
18
+ only - see isotherm_height()'s docstring for the full explanation
19
+ of what this computes and why it differs from the Ecco.cc reference.
20
+ This function is intentionally a near-literal translation of the
21
+ pure-Python version once was: same variable names, same control
22
+ flow, just with type-stable operations Numba can compile.
23
+ """
24
+ nz, ny, nx = temp_3d.shape
25
+ ht_grid = np.full((ny, nx), z_levels[0])
26
+
27
+ for iy in range(ny):
28
+ for ix in range(nx):
29
+ # Find valid (non-NaN) level indices for this column.
30
+ # (A manual scan, rather than np.where(...)[0], since this
31
+ # is both what Numba compiles most efficiently and keeps
32
+ # the logic explicit rather than relying on fancy indexing.)
33
+ n_valid = 0
34
+ for iz in range(nz):
35
+ if not np.isnan(temp_3d[iz, iy, ix]):
36
+ n_valid += 1
37
+
38
+ if n_valid == 0:
39
+ ht_grid[iy, ix] = np.nan
40
+ continue
41
+
42
+ valid_iz = np.empty(n_valid, dtype=np.int64)
43
+ k = 0
44
+ for iz in range(nz):
45
+ if not np.isnan(temp_3d[iz, iy, ix]):
46
+ valid_iz[k] = iz
47
+ k += 1
48
+
49
+ bottom_temp = temp_3d[valid_iz[0], iy, ix]
50
+ bottom_ht = z_levels[valid_iz[0]]
51
+ top_temp = temp_3d[valid_iz[-1], iy, ix]
52
+ top_ht = z_levels[valid_iz[-1]]
53
+
54
+ ht_found = False
55
+
56
+ # Walk consecutive VALID levels (skipping over any NaN gaps
57
+ # between them), rather than only immediately adjacent indices.
58
+ for j in range(1, n_valid):
59
+ iz_below = valid_iz[j - 1]
60
+ iz_above = valid_iz[j]
61
+ t_below = temp_3d[iz_below, iy, ix]
62
+ t_above = temp_3d[iz_above, iy, ix]
63
+
64
+ crosses = ((t_below >= target_temp_c >= t_above) or
65
+ (t_below <= target_temp_c <= t_above))
66
+ if crosses:
67
+ delta_t = t_above - t_below
68
+ delta_h = z_levels[iz_above] - z_levels[iz_below]
69
+ # Anchored at z_levels[iz_below] (the lower level) -
70
+ # see isotherm_height()'s docstring for why this
71
+ # differs from the Ecco.cc reference formula.
72
+ if delta_t == 0:
73
+ interp_ht = z_levels[iz_below]
74
+ else:
75
+ interp_ht = z_levels[iz_below] + (
76
+ (target_temp_c - t_below) / delta_t
77
+ ) * delta_h
78
+ ht_grid[iy, ix] = interp_ht
79
+ ht_found = True
80
+ break
81
+
82
+ if not ht_found:
83
+ if target_temp_c >= bottom_temp:
84
+ ht_grid[iy, ix] = bottom_ht
85
+ elif target_temp_c <= top_temp:
86
+ ht_grid[iy, ix] = top_ht
87
+
88
+ return ht_grid
89
+
90
+
91
+ def isotherm_height(temp_3d: np.ndarray,
92
+ z_levels: np.ndarray,
93
+ target_temp_c: float) -> np.ndarray:
94
+ """
95
+ For every (y, x) column, find the altitude [km] where temperature
96
+ equals target_temp_c by linear interpolation between the nearest
97
+ valid levels straddling the crossing.
98
+
99
+ Direct port of Ecco::_computeHts() from Ecco.cc (lrose-core), with
100
+ two corrections beyond the original:
101
+
102
+ 1. NaN gaps are skipped over when searching for a crossing, rather
103
+ than only ever comparing immediately adjacent levels. For example,
104
+ given column values [..., 1, NaN, -1, ...] searching for the 0°C
105
+ isotherm, a human can immediately tell the crossing must be very
106
+ close to the NaN level (we go from +1°C to -1°C across it) - but
107
+ comparing only adjacent pairs never sees this, because the pair
108
+ (1, NaN) and the pair (NaN, -1) are each skipped individually, and
109
+ the search falls through to the bottom/top fallback instead of
110
+ interpolating across the gap. This function instead tracks the
111
+ last VALID level seen and compares it against the next VALID
112
+ level found, using their true height difference (which may span
113
+ more than one level if there are multiple consecutive NaNs) for
114
+ the interpolation.
115
+
116
+ 2. The reference Ecco.cc interpolation line appears to anchor at the
117
+ wrong end of the bracketing pair:
118
+
119
+ interpHt = zProfile[iz] + ((tempC - tempBelow) / deltaTemp) * deltaHt;
120
+
121
+ This anchors at zProfile[iz] - the UPPER level of the pair - but
122
+ the standard linear-interpolation formula anchors at the LOWER
123
+ level (zProfile[iz-1]). Verified independently three ways
124
+ (symbolic algebra, first-principles re-derivation, and a numeric
125
+ check against a known analytic profile): the C++ formula as
126
+ written is off by exactly one level-spacing from the
127
+ mathematically correct answer. This function uses the correct
128
+ (lower-anchored) formula instead. This has NOT been confirmed
129
+ against the live lrose-core GitHub master (only the snapshot
130
+ provided), so it's possible this was already fixed upstream -
131
+ worth rechecking against current lrose-core source before
132
+ assuming either version is authoritative. If you ever need to
133
+ reproduce Ecco.cc's behaviour bit-for-bit instead of the
134
+ mathematically correct one, change `z_levels[iz_below]` to
135
+ `z_levels[iz_above]` in the interpolation line below.
136
+
137
+ Both corrections only matter when there's a real discrepancy to
138
+ correct - on a clean, gapless input where the crossing happens to
139
+ land close to a level (small deltaHt), the difference from #2 is
140
+ small; it grows with level spacing.
141
+
142
+ Handles:
143
+ - Normal lapse-rate profiles and temperature inversions (both crossing
144
+ directions).
145
+ - Target temp below lowest model level → returns bottom height.
146
+ - Target temp above highest model level → returns top height.
147
+ - Missing (NaN) temperature values - skipped over when searching for
148
+ a crossing (see above), not just skipped individually.
149
+
150
+ Parameters
151
+ ----------
152
+ temp_3d : np.ndarray, shape (Z, Y, X)
153
+ Temperature in °C. NaN for missing values. Matches the shape
154
+ convention used throughout EccoPy (see eccopy3d.run()).
155
+ z_levels : array-like, length Z
156
+ Altitude of each level [km MSL], increasing upward.
157
+ target_temp_c : float
158
+ Target isotherm temperature [°C].
159
+
160
+ Returns
161
+ -------
162
+ ht_grid : np.ndarray, shape (Y, X)
163
+ Height of the isotherm [km MSL] at each (y, x) point.
164
+ Falls back to lowest or highest valid level height when out of
165
+ range, or when a column has no valid data at all (returns NaN
166
+ in that case).
167
+ """
168
+ temp_3d = np.asarray(temp_3d, dtype=float)
169
+ z_levels = np.asarray(z_levels, dtype=float)
170
+
171
+ return _isotherm_height_core(temp_3d, z_levels, float(target_temp_c))
172
+
173
+
174
+ def melt_layer_from_temp(temp_2d: np.ndarray) -> np.ndarray:
175
+ """
176
+ Derive a simple melting-layer indicator from a 2-D temperature field.
177
+
178
+ Mirrors the MATLAB run script logic:
179
+ MELTING_LAYER = 20 where T <= 0°C (above/at freezing)
180
+ MELTING_LAYER = 10 where T > 0°C (below freezing)
181
+
182
+ Parameters
183
+ ----------
184
+ temp_2d : np.ndarray, shape (nHeight, nTime) or (ny, nx)
185
+ Temperature [°C].
186
+
187
+ Returns
188
+ -------
189
+ melt : np.ndarray, same shape
190
+ Melting-layer indicator.
191
+ """
192
+ melt = np.full_like(temp_2d, np.nan, dtype=float)
193
+ melt[temp_2d <= 0] = 20.0
194
+ melt[temp_2d > 0] = 10.0
195
+ melt[np.isnan(temp_2d)] = np.nan
196
+ return melt
197
+
198
+
199
+ def broadcast_temp_field(temp: np.ndarray, target_shape: tuple) -> np.ndarray:
200
+ """
201
+ Accept a temperature field given either as a full field matching the
202
+ target shape, or as a single vertical profile (a sounding) to be
203
+ mirrored identically across every horizontal point.
204
+
205
+ A sounding gives temperature as a function of height only - one
206
+ value per Z level, with no horizontal variation. This lets you pass
207
+ a single representative profile (e.g. from a model sounding or
208
+ reanalysis column) and have it apply uniformly across the whole
209
+ horizontal domain, instead of having to manually broadcast it
210
+ yourself.
211
+
212
+ Parameters
213
+ ----------
214
+ temp : np.ndarray
215
+ Either:
216
+ - shape == target_shape → returned unchanged (already a full field)
217
+ - shape (Z,) → a 1-D sounding, broadcast across all
218
+ horizontal points to match target_shape
219
+ target_shape : tuple
220
+ The full field shape this temperature array should match -
221
+ (Z, X) for EccoPy-2D-V, (Z, Y, X) for EccoPy-3D.
222
+
223
+ Returns
224
+ -------
225
+ temp_full : np.ndarray, shape == target_shape
226
+
227
+ Raises
228
+ ------
229
+ ValueError
230
+ If `temp`'s shape is neither the target shape nor a 1-D profile
231
+ of length target_shape[0] (the Z axis).
232
+ """
233
+ temp = np.asarray(temp, dtype=float)
234
+
235
+ if temp.shape == target_shape:
236
+ return temp
237
+
238
+ nz = target_shape[0]
239
+ if temp.ndim == 1 and temp.shape[0] == nz:
240
+ # Sounding: one value per Z level, no horizontal variation.
241
+ # Reshape to (Z, 1, 1, ...) so it broadcasts across every
242
+ # remaining (horizontal) axis of target_shape.
243
+ new_shape = (nz,) + (1,) * (len(target_shape) - 1)
244
+ return np.broadcast_to(temp.reshape(new_shape), target_shape).copy()
245
+
246
+ raise ValueError(
247
+ f"temp has shape {temp.shape}, which matches neither the full "
248
+ f"field shape {target_shape} nor a 1-D sounding of length "
249
+ f"{nz} (target_shape[0], the Z axis)."
250
+ )