Skip to content

Commit 94cf03e

Browse files
committed
Technical review corrections
1 parent b56c421 commit 94cf03e

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

src/CSET/operators/plot.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,17 +1936,31 @@ def _calculate_CFAD(
19361936
bin_edges: list[float]
19371937
The bin edges for the histogram. The bins need to be specified to
19381938
ensure consistency across the CFAD, otherwise it cannot be interpreted.
1939+
1940+
Notes
1941+
-----
1942+
Contour Frequency by Altitude Diagrams (CFADs) were first designed by
1943+
Yuter and Houze (1995)[YuterandHouze95]. They are calculated by binning the
1944+
data by altitude and then by variable bins (e.g. temperature). The variable
1945+
bins are then normalised by each altitude. This essenitally creates a
1946+
normalised frequency distribution for each altitude. These are then stacked
1947+
and combined in a single plot.
1948+
1949+
References
1950+
----------
1951+
.. [YuterandHouze95] Yuter S.E., and Houze, R.A. (1995) "Three-Dimensional
1952+
Kinematic and Microphysical Evolution of Florida Cumulonimbus. Part II:
1953+
Frequency Distributions of Vertical Velocity, Reflectivity, and
1954+
Differential Reflectivity" Monthly Weather Review, vol. 123, 1941-1963,
1955+
doi: 10.1175/1520-0493(1995)123<1941:TDKAME>2.0.CO;2
19391956
"""
19401957
# Setup empty array for containing the CFAD data.
19411958
CFAD_values = np.zeros(
19421959
(len(cube.coord(vertical_coordinate).points), len(bin_edges) - 1)
19431960
)
19441961

1945-
# Set iterator for CFAD values.
1946-
i = 0
1947-
19481962
# Calculate the CFAD as a histogram summing to one for each level.
1949-
for level_cube in cube.slices_over(vertical_coordinate):
1963+
for i, level_cube in enumerate(cube.slices_over(vertical_coordinate)):
19501964
# Note setting density to True does not produce the correct
19511965
# normalization for a CFAD, where each row must sum to one.
19521966
CFAD_values[i, :] = (
@@ -1955,8 +1969,7 @@ def _calculate_CFAD(
19551969
]
19561970
/ level_cube.data.size
19571971
)
1958-
i += 1
1959-
# calculate central points for bins
1972+
# Calculate central points for bins.
19601973
bins = (np.array(bin_edges[:-1]) + np.array(bin_edges[1:])) / 2.0
19611974
bin_bounds = np.array((bin_edges[:-1], bin_edges[1:])).T
19621975
# Now construct the coordinates for the cube.
@@ -1968,10 +1981,11 @@ def _calculate_CFAD(
19681981
CFAD = iris.cube.Cube(
19691982
CFAD_values,
19701983
dim_coords_and_dims=[(vert_coord, 0), (bin_coord, 1)],
1984+
long_name=f"{cube.name()}_cfad",
19711985
standard_name=cube.standard_name,
19721986
units="1",
19731987
)
1974-
CFAD.attributes["type"] = "Contour Frequency by Altitude Diagram (CFAD)"
1988+
CFAD.rename(f"{cube.name()}_cfad")
19751989
return CFAD
19761990

19771991

tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,15 @@ def orography_4D_cube_read_only():
323323
def orography_4D_cube(orography_4D_cube_read_only):
324324
"""Get 4D orography cube to run tests on. It is safe to modify."""
325325
return orography_4D_cube_read_only.copy()
326+
327+
328+
@pytest.fixture()
329+
def xwind_read_only():
330+
"""Get regridded xwind to run tests on. It is NOT safe to modify."""
331+
return read.read_cube("tests/test_data/ageofair/aoa_in_rgd.nc", "x_wind")
332+
333+
334+
@pytest.fixture()
335+
def xwind(xwind_read_only):
336+
"""Get regridded xwind to run tests on. It is safe to modify."""
337+
return xwind_read_only.copy()

tests/operators/test_ageofair.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
from CSET.operators import ageofair, read
2323

2424

25-
@pytest.fixture()
26-
def xwind() -> iris.cube.Cube:
27-
"""Get regridded xwind to run tests on."""
28-
return read.read_cube("tests/test_data/ageofair/aoa_in_rgd.nc", "x_wind")
29-
30-
3125
@pytest.fixture()
3226
def ywind() -> iris.cube.Cube:
3327
"""Get regridded ywind to run tests on."""

tests/operators/test_plot.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import logging
1919
from pathlib import Path
2020

21+
import cf_units
2122
import iris.coords
2223
import iris.cube
2324
import matplotlib as mpl
@@ -28,12 +29,6 @@
2829
from CSET.operators import collapse, misc, plot, read
2930

3031

31-
@pytest.fixture()
32-
def xwind() -> iris.cube.Cube:
33-
"""Get regridded xwind to run tests on."""
34-
return iris.load_cube("tests/test_data/ageofair/aoa_in_rgd.nc", "x_wind")
35-
36-
3732
def test_check_single_cube():
3833
"""Conversion to a single cube, and rejection where not possible."""
3934
cube = iris.cube.Cube([0.0])
@@ -1170,13 +1165,11 @@ def test_calculate_CFAD(xwind):
11701165
"""Test calculating a CFAD."""
11711166
bins = np.array([-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50])
11721167
calculated_CFAD = np.zeros((len(xwind.coord("pressure").points), len(bins) - 1))
1173-
j = 0
1174-
for level_cube in xwind.slices_over("pressure"):
1168+
for j, level_cube in enumerate(xwind.slices_over("pressure")):
11751169
calculated_CFAD[j, :] = (
11761170
np.histogram(level_cube.data.reshape(level_cube.data.size), bins=bins)[0]
11771171
/ level_cube.data.size
11781172
)
1179-
j += 1
11801173
assert np.allclose(
11811174
plot._calculate_CFAD(
11821175
xwind, "pressure", [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50]
@@ -1185,3 +1178,25 @@ def test_calculate_CFAD(xwind):
11851178
rtol=1e-06,
11861179
atol=1e-02,
11871180
)
1181+
1182+
1183+
def test_name_CFAD(xwind):
1184+
"""Test naming of CFAD cube."""
1185+
expected_name = f"{xwind.name()}_cfad"
1186+
assert (
1187+
plot._calculate_CFAD(
1188+
xwind, "pressure", [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50]
1189+
).name()
1190+
== expected_name
1191+
)
1192+
1193+
1194+
def test_units_CFAD(xwind):
1195+
"""Test units of CFAD cube."""
1196+
expected_units = cf_units.Unit("1")
1197+
assert (
1198+
plot._calculate_CFAD(
1199+
xwind, "pressure", [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50]
1200+
).units
1201+
== expected_units
1202+
)

0 commit comments

Comments
 (0)