Skip to content

Commit

Permalink
Clean up postprocessing functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
imreddyTeja committed Dec 16, 2024
1 parent 29ab4c8 commit 1e73538
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 52 deletions.
3 changes: 0 additions & 3 deletions experiments/ClimaEarth/run_amip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
75 changes: 26 additions & 49 deletions experiments/ClimaEarth/user_io/postprocessing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,45 @@ 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"
plot_global_conservation(
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)
Expand All @@ -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_",
)
Expand All @@ -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

0 comments on commit 1e73538

Please sign in to comment.