Skip to content

Commit d3c5f25

Browse files
committed
Adds basic CFAD calculation and tests
Fixes #1010
1 parent 0915cc3 commit d3c5f25

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/CSET/operators/plot.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,60 @@ def _spatial_plot(
932932
_make_plot_html_page(complete_plot_index)
933933

934934

935+
def _calculate_CFAD(
936+
cube: iris.cube.Cube, vertical_coordinate: str, bin_edges: list[float]
937+
) -> iris.cube.Cube:
938+
"""Calculate a Contour Frequency by Altitude Diagram (CFAD).
939+
940+
Parameters
941+
----------
942+
cube: iris.cube.Cube
943+
A cube of the data to be turned into a CFAD. It should be a minimum
944+
of two dimensions with one being a user specified vertical coordinate.
945+
vertical_coordinate: str
946+
The vertical coordinate of the cube for the CFAD to be calculated over.
947+
bin_edges: list[float]
948+
The bin edges for the histogram. The bins need to be specified to
949+
ensure consistency across the CFAD, otherwise it cannot be interpreted.
950+
"""
951+
# Setup empty array for containing the CFAD data.
952+
CFAD_values = np.zeros(
953+
(len(cube.coord(vertical_coordinate).points), len(bin_edges) - 1)
954+
)
955+
956+
# Set iterator for CFAD values.
957+
i = 0
958+
959+
# Calculate the CFAD as a histogram summing to one for each level.
960+
for level_cube in cube.slices_over(vertical_coordinate):
961+
# Note setting density to True does not produce the correct
962+
# normalization for a CFAD, where each row must sum to one.
963+
CFAD_values[i, :] = (
964+
np.histogram(level_cube.data.reshape(level_cube.data.size), bins=bin_edges)[
965+
0
966+
]
967+
/ level_cube.data.size
968+
)
969+
i += 1
970+
# calculate central points for bins
971+
bins = (np.array(bin_edges[:-1]) + np.array(bin_edges[1:])) / 2.0
972+
bin_bounds = np.array((bin_edges[:-1], bin_edges[1:])).T
973+
# Now construct the coordinates for the cube.
974+
vert_coord = cube.coord(vertical_coordinate)
975+
bin_coord = iris.coords.DimCoord(
976+
bins, bounds=bin_bounds, standard_name=cube.standard_name, units=cube.units
977+
)
978+
# Now construct the cube that is to be output.
979+
CFAD = iris.cube.Cube(
980+
CFAD_values,
981+
dim_coords_and_dims=[(vert_coord, 0), (bin_coord, 1)],
982+
standard_name=cube.standard_name,
983+
units="1",
984+
)
985+
CFAD.attributes["type"] = "Contour Frequency by Altitude Diagram (CFAD)"
986+
return CFAD
987+
988+
935989
####################
936990
# Public functions #
937991
####################

tests/operators/test_plots.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
from CSET.operators import collapse, plot, read
2626

2727

28+
@pytest.fixture()
29+
def xwind() -> iris.cube.Cube:
30+
"""Get regridded xwind to run tests on."""
31+
return iris.load_cube("tests/test_data/ageofair/aoa_in_rgd.nc", "x_wind")
32+
33+
2834
def test_check_single_cube():
2935
"""Conversion to a single cube, and rejection where not possible."""
3036
cube = iris.cube.Cube([0.0])
@@ -414,3 +420,24 @@ def test_invalid_plotting_method_postage_stamp_spatial_plot(cube, tmp_working_di
414420
plot._plot_and_save_postage_stamp_spatial_plot(
415421
cube, "filename", "realization", "title", "invalid"
416422
)
423+
424+
425+
def test_calculate_CFAD(xwind):
426+
"""Test calculating a CFAD."""
427+
bins = np.array([-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50])
428+
calculated_CFAD = np.zeros((len(xwind.coord("pressure").points), len(bins) - 1))
429+
j = 0
430+
for level_cube in xwind.slices_over("pressure"):
431+
calculated_CFAD[j, :] = (
432+
np.histogram(level_cube.data.reshape(level_cube.data.size), bins=bins)[0]
433+
/ level_cube.data.size
434+
)
435+
j += 1
436+
assert np.allclose(
437+
plot._calculate_CFAD(
438+
xwind, "pressure", [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50]
439+
).data,
440+
calculated_CFAD,
441+
rtol=1e-06,
442+
atol=1e-02,
443+
)

0 commit comments

Comments
 (0)