From 5a156ed1a16b1efe4bf1b4aba3e5d1a151cd82b6 Mon Sep 17 00:00:00 2001 From: nefrathenrici Date: Wed, 31 Jan 2024 16:17:02 -0800 Subject: [PATCH] address some comments --- post_processing/ci_plots.jl | 67 ++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/post_processing/ci_plots.jl b/post_processing/ci_plots.jl index 09df0cfb2f2..04254030b75 100644 --- a/post_processing/ci_plots.jl +++ b/post_processing/ci_plots.jl @@ -63,21 +63,40 @@ YLOGSCALE = Dict( long_name(var) = var.attributes["long_name"] short_name(var) = var.attributes["short_name"] -function var_info(var) +""" + parse_var_attributes(var) + +Takes in an OutputVar and parses some of its attributes into a short, informative string. +Used to generate unique titles when the same var is being plotted for several times/locations. +This could be extended to parse more attributes. + +For example, the sample attributes: +attributes = Dict( + "units" => "%", + "short_name" => "cl", + "slice_y" => "0.0", + "long_name" => "Cloud fraction, Instantaneous x = 0.0 m y = 0.0 m", + "slice_y_units" => "m", + "slice_x_units" => "m", + "comments" => "", + "slice_x" => "0.0", +) +will be parsed into "cl, x = 0.0, y = 0.0" +""" +function parse_var_attributes(var) + MISSING_STR = "MISSING_ATTRIBUTE" attr = var.attributes - name = replace(short_name(var), "up" => "") - coords = if haskey(attr, "slice_lat") - "lat = $(attr["slice_lat"]), lon = $(attr["slice_lon"])" - else - "x = $(attr["slice_x"]), y = $(attr["slice_y"])" - end - var_info = [name, coords] - time = get(attr, "slice_time", nothing) - isnothing(time) || push!(var_info, "t = $(time)s") + attributes = ["slice_lat", "slice_lon", "slice_x", "slice_y", "slice_time"] + info = [ + replace(key, "slice_" => "") * " = " * get(attr, key, MISSING_STR) + for key in attributes + ] + # Filter out missing entries + info = filter(x -> !occursin(MISSING_STR, x), [name, info...]) - return join(var_info, ", ") + return join(info, ", ") end function make_plots_generic( @@ -636,7 +655,7 @@ function plot_edmf_vert_profile!(grid_loc, var_group) grid_loc[1, 1], ylabel = "z [$(var_group[1].dim_attributes["z"]["units"])]", xlabel = "$(short_name(var_group[1])) [$units]", - title = var_info(var_group[1]), + title = parse_var_attributes(var_group[1]), ) for var in var_group @@ -647,14 +666,15 @@ end """ - plot_custom_title!(grid_loc, var) + plot_parsed_attribute_title!(grid_loc, var) -Helper function for `make_plots_generic`. Plots a variable with a custom title. +Helper function for `make_plots_generic`. Plots an OutputVar `var`, +setting the axis title to `parse_var_attributes(var)` """ -plot_custom_title!(grid_loc, var) = viz.plot!( +plot_parsed_attribute_title!(grid_loc, var) = viz.plot!( grid_loc, var; - more_kwargs = Dict(:axis => ca_kwargs(title = var_info(var))), + more_kwargs = Dict(:axis => ca_kwargs(title = parse_var_attributes(var))), ) """ @@ -665,12 +685,12 @@ Groupings are matched via variable short name, location, and time (if applicable """ function group_edmf_vars(vars) grouped_vars = OrderedDict{String, Vector{ClimaAnalysis.OutputVar}}() - for var in sort(vars, by = var_info) - suffix = var_info(var) - if haskey(grouped_vars, suffix) - push!(grouped_vars[suffix], var) + for var in sort(vars, by = parse_var_attributes) + info = parse_var_attributes(var) + if haskey(grouped_vars, info) + push!(grouped_vars[info], var) else - grouped_vars[suffix] = [var] + grouped_vars[info] = [var] end end return grouped_vars @@ -723,12 +743,11 @@ function make_plots(::EDMFBoxPlots, simulation_path) make_plots_generic( simulation_path, vars_zt, - plot_fn = plot_custom_title!, + plot_fn = plot_parsed_attribute_title!, summary_files = [tmp_file], MAX_NUM_COLS = 2, MAX_NUM_ROWS = 4, ) - end EDMFSpherePlots = @@ -763,7 +782,7 @@ function make_plots(::EDMFSpherePlots, simulation_path) make_plots_generic( simulation_path, vars_zt, - plot_fn = plot_custom_title!, + plot_fn = plot_parsed_attribute_title!, summary_files = [tmp_file], MAX_NUM_COLS = 2, MAX_NUM_ROWS = 4,