Skip to content

Commit 0083d51

Browse files
committed
Adds basic CFAD calculation and tests
Fixes #1010
1 parent b64697a commit 0083d51

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/CSET/operators/plot.py

+54
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,60 @@ def _spatial_plot(
853853
_make_plot_html_page(complete_plot_index)
854854

855855

856+
def _calculate_CFAD(
857+
cube: iris.cube.Cube, vertical_coordinate: str, bin_edges: list[float]
858+
) -> iris.cube.Cube:
859+
"""Calculate a Contour Frequency by Altitude Diagram (CFAD).
860+
861+
Parameters
862+
----------
863+
cube: iris.cube.Cube
864+
A cube of the data to be turned into a CFAD. It should be a minimum
865+
of two dimensions with one being a user specified vertical coordinate.
866+
vertical_coordinate: str
867+
The vertical coordinate of the cube for the CFAD to be calculated over.
868+
bin_edges: list[float]
869+
The bin edges for the histogram. The bins need to be specified to
870+
ensure consistency across the CFAD, otherwise it cannot be interpreted.
871+
"""
872+
# Setup empty array for containing the CFAD data.
873+
CFAD_values = np.zeros(
874+
(len(cube.coord(vertical_coordinate).points), len(bin_edges) - 1)
875+
)
876+
877+
# Set iterator for CFAD values.
878+
i = 0
879+
880+
# Calculate the CFAD as a histogram summing to one for each level.
881+
for level_cube in cube.slices_over(vertical_coordinate):
882+
# Note setting density to True does not produce the correct
883+
# normalization for a CFAD, where each row must sum to one.
884+
CFAD_values[i, :] = (
885+
np.histogram(level_cube.data.reshape(level_cube.data.size), bins=bin_edges)[
886+
0
887+
]
888+
/ level_cube.data.size
889+
)
890+
i += 1
891+
# calculate central points for bins
892+
bins = (np.array(bin_edges[:-1]) + np.array(bin_edges[1:])) / 2.0
893+
bin_bounds = np.array((bin_edges[:-1], bin_edges[1:])).T
894+
# Now construct the coordinates for the cube.
895+
vert_coord = cube.coord(vertical_coordinate)
896+
bin_coord = iris.coords.DimCoord(
897+
bins, bounds=bin_bounds, standard_name=cube.standard_name, units=cube.units
898+
)
899+
# Now construct the cube that is to be output.
900+
CFAD = iris.cube.Cube(
901+
CFAD_values,
902+
dim_coords_and_dims=[(vert_coord, 0), (bin_coord, 1)],
903+
standard_name=cube.standard_name,
904+
units="1",
905+
)
906+
CFAD.attributes["type"] = "Contour Frequency by Altitude Diagram (CFAD)"
907+
return CFAD
908+
909+
856910
####################
857911
# Public functions #
858912
####################

tests/operators/test_plots.py

+28
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@
1717
from pathlib import Path
1818

1919
import iris.cube
20+
import numpy as np
2021
import pytest
2122

2223
from CSET.operators import collapse, plot, read
2324

2425

26+
@pytest.fixture()
27+
def xwind() -> iris.cube.Cube:
28+
"""Get regridded xwind to run tests on."""
29+
return iris.load_cube("tests/test_data/ageofair/aoa_in_rgd.nc", "x_wind")
30+
31+
2532
def test_check_single_cube():
2633
"""Conversion to a single cube, and rejection where not possible."""
2734
cube = iris.cube.Cube([0.0])
@@ -318,3 +325,24 @@ def test_invalid_plotting_method_postage_stamp_spatial_plot(cube, tmp_working_di
318325
plot._plot_and_save_postage_stamp_spatial_plot(
319326
cube, "filename", "realization", "title", "invalid"
320327
)
328+
329+
330+
def test_calculate_CFAD(xwind):
331+
"""Test calculating a CFAD."""
332+
bins = np.array([-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50])
333+
calculated_CFAD = np.zeros((len(xwind.coord("pressure").points), len(bins) - 1))
334+
j = 0
335+
for level_cube in xwind.slices_over("pressure"):
336+
calculated_CFAD[j, :] = (
337+
np.histogram(level_cube.data.reshape(level_cube.data.size), bins=bins)[0]
338+
/ level_cube.data.size
339+
)
340+
j += 1
341+
assert np.allclose(
342+
plot._calculate_CFAD(
343+
xwind, "pressure", [-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50]
344+
).data,
345+
calculated_CFAD,
346+
rtol=1e-06,
347+
atol=1e-02,
348+
)

0 commit comments

Comments
 (0)