Skip to content

Commit

Permalink
Add support for monthly calendar diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbozzolo committed Aug 15, 2024
1 parent 98e60b9 commit 568b6cd
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ insolation: "timevarying"
z_max: 70000.0
dt_save_to_sol: "30hours"
rad: "allskywithclear"
diagnostics:
- short_name: rhoa
reduction_time: average
period: 1months
4 changes: 3 additions & 1 deletion examples/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@ version = "0.7.6"

[[deps.ClimaDiagnostics]]
deps = ["Accessors", "ClimaComms", "ClimaCore", "Dates", "NCDatasets", "SciMLBase"]
git-tree-sha1 = "228ff3bc4dbd7329ef054f9bfbbe34075234ca25"
git-tree-sha1 = "040f52ac9409d0511edb8ae1859762b6664a633e"
repo-rev = "gb/calendar_peridos"
repo-url = "https://github.com/CliMA/ClimaDiagnostics.jl.git"
uuid = "1ecacbb8-0713-4841-9a07-eb5aa8a2d53f"
version = "0.2.3"

Expand Down
38 changes: 30 additions & 8 deletions src/callbacks/get_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,50 @@ function get_diagnostics(parsed_args, atmos_model, Y, p, t_start, dt)
haskey(yaml_diag, "period") ||
error("period keyword required for diagnostics")

period_seconds = FT(time_to_seconds(yaml_diag["period"]))
period_str = yaml_diag["period"]

if occursin("months", period_str)
months = match(r"^(\d+)months$", period_str)
isnothing(months) && error(
"$(period_str) has to be of the form <NUM>months, e.g. 2months for 2 months",
)
period_dates = Dates.Month(parse(Int, first(months)))
output_schedule = CAD.EveryCalendarDtSchedule(
period_dates;
reference_date = p.start_date,
t_start,
)
compute_schedule = CAD.EveryCalendarDtSchedule(
period_dates;
reference_date = p.start_date,
t_start,
)
else
period_seconds = FT(time_to_seconds(period_str))
output_schedule =
CAD.EveryDtSchedule(period_seconds; t_start)
compute_schedule =
CAD.EveryDtSchedule(period_seconds; t_start)
end

if isnothing(output_name)
output_short_name = CAD.descriptive_short_name(
CAD.get_diagnostic_variable(short_name),
CAD.EveryDtSchedule(period_seconds; t_start),
output_schedule,
reduction_time_func,
pre_output_hook!,
)
end

if isnothing(reduction_time_func)
compute_every = CAD.EveryDtSchedule(period_seconds; t_start)
compute_every = compute_schedule
else
compute_every = CAD.EveryStepSchedule()
end

CAD.ScheduledDiagnostic(
variable = CAD.get_diagnostic_variable(short_name),
output_schedule_func = CAD.EveryDtSchedule(
period_seconds;
t_start,
),
output_schedule_func = output_schedule,
compute_schedule_func = compute_every,
reduction_time_func = reduction_time_func,
pre_output_hook! = pre_output_hook!,
Expand All @@ -134,7 +155,8 @@ function get_diagnostics(parsed_args, atmos_model, Y, p, t_start, dt)
CAD.default_diagnostics(
atmos_model,
t_start,
time_to_seconds(parsed_args["t_end"]);
time_to_seconds(parsed_args["t_end"]),
p.start_date;
output_writer = netcdf_writer,
)...,
diagnostics...,
Expand Down
5 changes: 4 additions & 1 deletion src/diagnostics/Diagnostics.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Diagnostics

import Dates: Month, DateTime

import ClimaComms

import LinearAlgebra: dot
Expand Down Expand Up @@ -50,7 +52,8 @@ import ClimaDiagnostics:

import ClimaDiagnostics.DiagnosticVariables: descriptive_short_name

import ClimaDiagnostics.Schedules: EveryStepSchedule, EveryDtSchedule
import ClimaDiagnostics.Schedules:
EveryStepSchedule, EveryDtSchedule, EveryCalendarDtSchedule

import ClimaDiagnostics.Writers:
HDF5Writer,
Expand Down
115 changes: 91 additions & 24 deletions src/diagnostics/default_diagnostics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
# level interfaces, add them here. Feel free to include extra files.

"""
default_diagnostics(model, t_start, t_end; output_writer)
default_diagnostics(model, t_start, t_end, reference_date; output_writer)
Return a list of `ScheduledDiagnostic`s associated with the given `model` that use
`output_write` to write to disk. `t_end` is the expected simulation end time and it is used
`output_write` to write to disk. `t_end, reference_date` is the expected simulation end time and it is used
to choose the most reasonable output frequency.
The logic is as follows:
Expand All @@ -24,6 +24,7 @@ function default_diagnostics(
model::AtmosModel,
t_start::Real,
t_end::Real;
reference_date::DateTime,
output_writer,
)
# Unfortunately, [] is not treated nicely in a map (we would like it to be "excluded"),
Expand All @@ -34,7 +35,8 @@ function default_diagnostics(
default_diagnostics(
getfield(model, x),
t_start,
t_end;
t_end,
reference_date;
output_writer,
) != [],
fieldnames(AtmosModel),
Expand All @@ -43,12 +45,13 @@ function default_diagnostics(
# We use a map because we want to ensure that diagnostics is a well defined type, not
# Any. This reduces latency.
return vcat(
core_default_diagnostics(output_writer, t_start, t_end),
core_default_diagnostics(output_writer, t_start, t_end, reference_date),
map(non_empty_fields) do field
default_diagnostics(
getfield(model, field),
t_start,
t_end;
t_end,
reference_date;
output_writer,
)
end...,
Expand All @@ -60,7 +63,8 @@ end
# that all the default_diagnostics return the same type). This is used by
# default_diagnostics(model::AtmosModel; output_writer), so that we can ignore defaults for
# submodels that have no given defaults.
default_diagnostics(submodel, t_start, t_end; output_writer) = []
default_diagnostics(submodel, t_start, t_end, reference_date; output_writer) =
[]

"""
produce_common_diagnostic_function(period, reduction)
Expand All @@ -74,12 +78,16 @@ function common_diagnostics(
t_start,
short_names...;
pre_output_hook! = nothing,
compute_schedule_func = EveryStepSchedule(),
output_schedule_func = isnothing(period) ?
error("No output or period schedule provided") :
EveryDtSchedule(period; t_start),
)
return [
ScheduledDiagnostic(
variable = get_diagnostic_variable(short_name),
compute_schedule_func = EveryStepSchedule(),
output_schedule_func = EveryDtSchedule(period; t_start),
compute_schedule_func = compute_schedule_func,
output_schedule_func = output_schedule_func,
reduction_time_func = reduction,
output_writer = output_writer,
pre_output_hook! = pre_output_hook!,
Expand Down Expand Up @@ -118,7 +126,7 @@ end
########
# Core #
########
function core_default_diagnostics(output_writer, t_start, t_end)
function core_default_diagnostics(output_writer, t_start, t_end, reference_date)
core_diagnostics =
["ts", "ta", "thetaa", "ha", "pfull", "rhoa", "ua", "va", "wa", "hfes"]

Expand Down Expand Up @@ -150,8 +158,8 @@ function core_default_diagnostics(output_writer, t_start, t_end)
output_short_name = "orog_inst",
),
average_func(core_diagnostics...; output_writer, t_start)...,
min_func("ts"; output_writer, t_start),
max_func("ts"; output_writer, t_start),
min_func("ts"; output_writer, t_start, reference_date),
max_func("ts"; output_writer, t_start, reference_date),
]
end

Expand All @@ -161,13 +169,21 @@ end
function default_diagnostics(
::T,
t_start,
t_end;
t_end,
reference_date;
output_writer,
) where {T <: Union{EquilMoistModel, NonEquilMoistModel}}
moist_diagnostics =
["hur", "hus", "cl", "clw", "cli", "hussfc", "evspsbl", "pr"]
average_func = frequency_averages(t_start, t_end)
return [average_func(moist_diagnostics...; output_writer, t_start)...]
return [
average_func(
moist_diagnostics...;
output_writer,
t_start,
reference_date,
)...,
]
end

#######################
Expand All @@ -176,14 +192,22 @@ end
function default_diagnostics(
::Microphysics1Moment,
t_start,
t_end;
t_end,
reference_date;
output_writer,
)
precip_diagnostics = ["husra", "hussn"]

average_func = frequency_averages(t_start, t_end)

return [average_func(precip_diagnostics...; output_writer, t_start)...]
return [
average_func(
precip_diagnostics...;
output_writer,
t_start,
reference_date,
)...,
]
end

##################
Expand All @@ -192,7 +216,8 @@ end
function default_diagnostics(
::RRTMGPI.AbstractRRTMGPMode,
t_start,
t_end;
t_end,
reference_date;
output_writer,
)
rad_diagnostics = [
Expand All @@ -211,14 +236,22 @@ function default_diagnostics(

average_func = frequency_averages(t_start, t_end)

return [average_func(rad_diagnostics...; output_writer, t_start)...]
return [
average_func(
rad_diagnostics...;
output_writer,
t_start,
reference_date,
)...,
]
end


function default_diagnostics(
::RRTMGPI.AllSkyRadiationWithClearSkyDiagnostics,
t_start,
t_end;
t_end,
reference_date;
output_writer,
)
rad_diagnostics = [
Expand Down Expand Up @@ -249,15 +282,31 @@ function default_diagnostics(
average_func = frequency_averages(t_start, t_end)

return [
average_func(rad_diagnostics...; output_writer, t_start)...,
average_func(rad_clearsky_diagnostics...; output_writer, t_start)...,
average_func(
rad_diagnostics...;
output_writer,
t_start,
reference_date,
)...,
average_func(
rad_clearsky_diagnostics...;
output_writer,
t_start,
reference_date,
)...,
]
end

##################
# Turbconv model #
##################
function default_diagnostics(::PrognosticEDMFX, t_start, t_end; output_writer)
function default_diagnostics(
::PrognosticEDMFX,
t_start,
t_end,
reference_date;
output_writer,
)
edmfx_draft_diagnostics = [
"arup",
"rhoaup",
Expand Down Expand Up @@ -288,13 +337,29 @@ function default_diagnostics(::PrognosticEDMFX, t_start, t_end; output_writer)
average_func = frequency_averages(t_start, t_end)

return [
average_func(edmfx_draft_diagnostics...; output_writer, t_start)...,
average_func(edmfx_env_diagnostics...; output_writer, t_start)...,
average_func(
edmfx_draft_diagnostics...;
output_writer,
t_start,
reference_date,
)...,
average_func(
edmfx_env_diagnostics...;
output_writer,
t_start,
reference_date,
)...,
]
end


function default_diagnostics(::DiagnosticEDMFX, t_start, t_end; output_writer)
function default_diagnostics(
::DiagnosticEDMFX,
t_start,
t_end,
reference_date;
output_writer,
)
diagnostic_edmfx_draft_diagnostics = [
"arup",
"rhoaup",
Expand All @@ -316,11 +381,13 @@ function default_diagnostics(::DiagnosticEDMFX, t_start, t_end; output_writer)
diagnostic_edmfx_draft_diagnostics...;
output_writer,
t_start,
reference_date,
)...,
average_func(
diagnostic_edmfx_env_diagnostics...;
output_writer,
t_start,
reference_date,
)...,
]
end
Loading

0 comments on commit 568b6cd

Please sign in to comment.