From 1e73538c81aecbd8d60f322b52d2d89826684b72 Mon Sep 17 00:00:00 2001 From: imreddyTeja Date: Mon, 16 Dec 2024 15:01:07 -0800 Subject: [PATCH] Clean up postprocessing functions Remove data from `postprocessing_vars` tuple which is accessable from the coupled sim. Change functions to only extract used variables from `postprocessing_vars`. Move the closing of AMIP diagnostics writers to the end of AMIP postprocessing. --- experiments/ClimaEarth/run_amip.jl | 3 - .../ClimaEarth/user_io/postprocessing.jl | 75 +++++++------------ 2 files changed, 26 insertions(+), 52 deletions(-) diff --git a/experiments/ClimaEarth/run_amip.jl b/experiments/ClimaEarth/run_amip.jl index 30d8a9a5b8..0703dd9cc1 100644 --- a/experiments/ClimaEarth/run_amip.jl +++ b/experiments/ClimaEarth/run_amip.jl @@ -826,15 +826,12 @@ The postprocessing includes: if ClimaComms.iamroot(comms_ctx) postprocessing_vars = (; - dir_paths, plot_diagnostics, use_coupler_diagnostics, output_default_diagnostics, t_end, conservation_softfail, atmos_output_dir, - amip_diags_handler, - comms_ctx, ) postprocess_sim(cs.mode.type, cs, postprocessing_vars) end diff --git a/experiments/ClimaEarth/user_io/postprocessing.jl b/experiments/ClimaEarth/user_io/postprocessing.jl index 7a8d6bfd8c..5b4cc71b22 100644 --- a/experiments/ClimaEarth/user_io/postprocessing.jl +++ b/experiments/ClimaEarth/user_io/postprocessing.jl @@ -3,23 +3,15 @@ include("debug_plots.jl") include("diagnostics_plots.jl") """ - postprocess_sim(mode_type::AbstractSlabPlanetModeType, cs, postprocessing_vars) + postprocess_sim(mode_type::AbstractSlabplanetSimulationMode, cs, postprocessing_vars) If they exist, perform conservation checks, for any slabplanet simulation, and then call `common_postprocessing` to perform common postprocessing tasks that are common to all simulation types. """ -function postprocess_sim(mode_type::AbstractSlabPlanetModeType, cs, postprocessing_vars) - (; - dir_paths, - plot_diagnostics, - use_coupler_diagnostics, - output_default_diagnostics, - t_end, - conservation_softfail, - atmos_output_dir, - amip_diags_handler, - comms_ctx, - ) = postprocessing_vars +function postprocess_sim(mode_type::AbstractSlabplanetSimulationMode, cs, postprocessing_vars) + (; conservation_softfail,) = postprocessing_vars + + common_postprocessing(cs, postprocessing_vars) if !isnothing(cs.conservation_checks) @info "Conservation Check Plots" @@ -27,38 +19,29 @@ function postprocess_sim(mode_type::AbstractSlabPlanetModeType, cs, postprocessi cs.conservation_checks.energy, cs, conservation_softfail, - figname1 = joinpath(dir_paths.artifacts, "total_energy_bucket.png"), - figname2 = joinpath(dir_paths.artifacts, "total_energy_log_bucket.png"), + figname1 = joinpath(cs.dirs.artifacts, "total_energy_bucket.png"), + figname2 = joinpath(cs.dirs.artifacts, "total_energy_log_bucket.png"), ) plot_global_conservation( cs.conservation_checks.water, cs, conservation_softfail, - figname1 = joinpath(dir_paths.artifacts, "total_water_bucket.png"), - figname2 = joinpath(dir_paths.artifacts, "total_water_log_bucket.png"), + figname1 = joinpath(cs.dirs.artifacts, "total_water_bucket.png"), + figname2 = joinpath(cs.dirs.artifacts, "total_water_log_bucket.png"), ) end - common_postprocessing(cs, postprocessing_vars) end """ - postprocess_sim(mode_type::AMIP_mode, cs, postprocessing_vars) + postprocess_sim(mode_type::AMIPMode, cs, postprocessing_vars) Conditionally plot AMIP diagnostics and call `common_postprocessing` to perform postprocessing tasks that are common to all simulation types. """ -function postprocess_sim(mode_type::AMIP_mode, cs, postprocessing_vars) - (; - dir_paths, - plot_diagnostics, - use_coupler_diagnostics, - output_default_diagnostics, - t_end, - conservation_softfail, - atmos_output_dir, - amip_diags_handler, - comms_ctx, - ) = postprocessing_vars +function postprocess_sim(mode_type::AMIPMode, cs, postprocessing_vars) + (; use_coupler_diagnostics, output_default_diagnostics, t_end) = postprocessing_vars + + common_postprocessing(cs, postprocessing_vars) if use_coupler_diagnostics ## plot data that correspond to the model's last save_hdf5 call (i.e., last month) @@ -70,18 +53,18 @@ function postprocess_sim(mode_type::AMIP_mode, cs, postprocessing_vars) # define variable names and output directories for each diagnostic amip_short_names_atmos = ["ta", "ua", "hus", "clw", "pr", "ts", "toa_fluxes_net"] amip_short_names_coupler = ["F_turb_energy"] - output_dir_coupler = dir_paths.output + output_dir_coupler = cs.dirs.output # Check if all output variables are available in the specified directories make_diagnostics_plots( atmos_output_dir, - dir_paths.artifacts, + cs.dirs.artifacts, short_names = amip_short_names_atmos, output_prefix = "atmos_", ) make_diagnostics_plots( output_dir_coupler, - dir_paths.artifacts, + cs.dirs.artifacts, short_names = amip_short_names_coupler, output_prefix = "coupler_", ) @@ -90,34 +73,28 @@ function postprocess_sim(mode_type::AMIP_mode, cs, postprocessing_vars) # Check this because we only want monthly data for making plots if t_end > 84600 * 31 * 3 && output_default_diagnostics include("leaderboard/leaderboard.jl") - leaderboard_base_path = dir_paths.artifacts + leaderboard_base_path = cs.dirs.artifacts compute_leaderboard(leaderboard_base_path, atmos_output_dir) compute_pfull_leaderboard(leaderboard_base_path, atmos_output_dir) end - common_postprocessing(cs, postprocessing_vars) + + !isnothing(cs.amip_diags_handler) && + map(diag -> close(diag.output_writer), cs.amip_diags_handler.scheduled_diagnostics) end """ common_postprocessing(cs, postprocessing_vars) -Perfrom postprocessing common to all simulation types. +Perform postprocessing common to all simulation types. """ function common_postprocessing(cs, postprocessing_vars) - (; plot_diagnostics, comms_ctx, dir_paths, amip_diags_handler, atmos_output_dir) = postprocessing_vars + (; plot_diagnostics, atmos_output_dir) = postprocessing_vars if plot_diagnostics @info "Plotting diagnostics" include("user_io/diagnostics_plots.jl") - make_diagnostics_plots(atmos_output_dir, dir_paths.artifacts) + make_diagnostics_plots(atmos_output_dir, cs.dirs.artifacts) end - ## plot all model states and coupler fields (useful for debugging) - !CA.is_distributed(comms_ctx) && debug(cs, dir_paths.artifacts) - - # if isinteractive() #hide - # ## clean up for interactive runs, retain all output otherwise #hide - # rm(dir_paths.output; recursive = true, force = true) #hide - # end #hide - - ## close all AMIP diagnostics file writers - !isnothing(amip_diags_handler) && map(diag -> close(diag.output_writer), amip_diags_handler.scheduled_diagnostics) + # plot all model states and coupler fields (useful for debugging) + !CA.is_distributed(cs.comms_ctx) && debug(cs, cs.dirs.artifacts) end