@@ -1921,6 +1921,60 @@ def _validate_cubes_coords(
19211921 )
19221922
19231923
1924+ def _calculate_CFAD (
1925+ cube : iris .cube .Cube , vertical_coordinate : str , bin_edges : list [float ]
1926+ ) -> iris .cube .Cube :
1927+ """Calculate a Contour Frequency by Altitude Diagram (CFAD).
1928+
1929+ Parameters
1930+ ----------
1931+ cube: iris.cube.Cube
1932+ A cube of the data to be turned into a CFAD. It should be a minimum
1933+ of two dimensions with one being a user specified vertical coordinate.
1934+ vertical_coordinate: str
1935+ The vertical coordinate of the cube for the CFAD to be calculated over.
1936+ bin_edges: list[float]
1937+ The bin edges for the histogram. The bins need to be specified to
1938+ ensure consistency across the CFAD, otherwise it cannot be interpreted.
1939+ """
1940+ # Setup empty array for containing the CFAD data.
1941+ CFAD_values = np .zeros (
1942+ (len (cube .coord (vertical_coordinate ).points ), len (bin_edges ) - 1 )
1943+ )
1944+
1945+ # Set iterator for CFAD values.
1946+ i = 0
1947+
1948+ # Calculate the CFAD as a histogram summing to one for each level.
1949+ for level_cube in cube .slices_over (vertical_coordinate ):
1950+ # Note setting density to True does not produce the correct
1951+ # normalization for a CFAD, where each row must sum to one.
1952+ CFAD_values [i , :] = (
1953+ np .histogram (level_cube .data .reshape (level_cube .data .size ), bins = bin_edges )[
1954+ 0
1955+ ]
1956+ / level_cube .data .size
1957+ )
1958+ i += 1
1959+ # calculate central points for bins
1960+ bins = (np .array (bin_edges [:- 1 ]) + np .array (bin_edges [1 :])) / 2.0
1961+ bin_bounds = np .array ((bin_edges [:- 1 ], bin_edges [1 :])).T
1962+ # Now construct the coordinates for the cube.
1963+ vert_coord = cube .coord (vertical_coordinate )
1964+ bin_coord = iris .coords .DimCoord (
1965+ bins , bounds = bin_bounds , standard_name = cube .standard_name , units = cube .units
1966+ )
1967+ # Now construct the cube that is to be output.
1968+ CFAD = iris .cube .Cube (
1969+ CFAD_values ,
1970+ dim_coords_and_dims = [(vert_coord , 0 ), (bin_coord , 1 )],
1971+ standard_name = cube .standard_name ,
1972+ units = "1" ,
1973+ )
1974+ CFAD .attributes ["type" ] = "Contour Frequency by Altitude Diagram (CFAD)"
1975+ return CFAD
1976+
1977+
19241978####################
19251979# Public functions #
19261980####################
0 commit comments