Skip to content

Commit

Permalink
Refactor produce_common_diagnostic_function
Browse files Browse the repository at this point in the history
Refactor closure in favor of a function with more arguments
  • Loading branch information
Sbozzolo committed Sep 9, 2023
1 parent 4dec938 commit 101a32b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
60 changes: 35 additions & 25 deletions src/diagnostics/default_diagnostics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ get_default_diagnostics(_) = []
Helper function to define functions like `get_daily_max`.
"""
function produce_common_diagnostic_function(
function common_diagnostics(
period,
reduction;
reduction,
output_writer,
long_names...;
pre_output_hook! = (accum, count) -> nothing,
)
return (long_names...; output_writer = HDF5Writer()) -> begin
[
ScheduledDiagnosticTime(
variable = ALL_DIAGNOSTICS[long_name],
compute_every = :timestep,
output_every = period, # seconds
reduction_time_func = reduction,
output_writer = output_writer,
pre_output_hook! = pre_output_hook!,
) for long_name in long_names
]
end
return [
ScheduledDiagnosticTime(
variable = ALL_DIAGNOSTICS[long_name],
compute_every = :timestep,
output_every = period, # seconds
reduction_time_func = reduction,
output_writer = output_writer,
pre_output_hook! = pre_output_hook!,
) for long_name in long_names
]
end

function average_pre_output_hook!(accum, counter)
Expand All @@ -67,42 +67,50 @@ end
Return a list of `ScheduledDiagnostics` that compute the daily max for the given variables.
"""
get_daily_max = produce_common_diagnostic_function(24 * 60 * 60, max)
daily_max(long_names...; output_writer = HDF5Writer()) =
common_diagnostics(24 * 60 * 60, max, output_writer, long_names...)

"""
get_daily_min(long_names...; output_writer = HDF5Writer())
Return a list of `ScheduledDiagnostics` that compute the daily min for the given variables.
"""
get_daily_min = produce_common_diagnostic_function(24 * 60 * 60, min)
daily_min(long_names...; output_writer = HDF5Writer()) =
common_diagnostics(24 * 60 * 60, min, output_writer, long_names...)
"""
get_daily_average(long_names...; output_writer = HDF5Writer())
Return a list of `ScheduledDiagnostics` that compute the daily average for the given variables.
"""
# An average is just a sum with a normalization before output
get_daily_average = produce_common_diagnostic_function(
24 * 60 * 60,
(+);
pre_output_hook! = average_pre_output_hook!,
)
hourly_average(long_names...; output_writer = HDF5Writer()) =
common_diagnostics(
24 * 60 * 60,
(+),
long_names...,
output_writer;
pre_output_hook! = average_pre_output_hook!,
)

"""
get_hourly_max(long_names...; output_writer = HDF5Writer())
Return a list of `ScheduledDiagnostics` that compute the hourly max for the given variables.
"""
get_hourly_max = produce_common_diagnostic_function(60 * 60, max)
hourly_max(long_names...; output_writer = HDF5Writer()) =
common_diagnostics(60 * 60, max, output_writer, long_names...)

"""
get_hourly_min(long_names...; output_writer = HDF5Writer())
Return a list of `ScheduledDiagnostics` that compute the hourly min for the given variables.
"""
get_hourly_min = produce_common_diagnostic_function(60 * 60, min)
hourly_min(long_names...; output_writer = HDF5Writer()) =
common_diagnostics(60 * 60, min, output_writer, long_names...)

"""
get_daily_average(long_names...; output_writer = HDF5Writer())
Expand All @@ -112,9 +120,11 @@ Return a list of `ScheduledDiagnostics` that compute the hourly average for the
"""

# An average is just a sum with a normalization before output
get_hourly_average = produce_common_diagnostic_function(
daily_average(long_names...; output_writer = HDF5Writer()) = common_diagnostics(
60 * 60,
(+);
(+),
output_writer,
long_names...;
pre_output_hook! = average_pre_output_hook!,
)

Expand Down
4 changes: 2 additions & 2 deletions src/diagnostics/defaults/moisture_model.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# FIXME: Gabriele added this as an example. Put something meaningful here!
function get_default_diagnostics(::T) where {T <: DryModel}
return vcat(
get_daily_average("air_density"),
get_hourly_max("air_density"),
daily_average("air_density"),
hourly_max("air_density"),
[
ScheduledDiagnosticTime(
variable = ALL_DIAGNOSTICS["air_density"],
Expand Down

0 comments on commit 101a32b

Please sign in to comment.