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