From 101a32b597302fb2e8e20c1a5df3f02ccf2ab239 Mon Sep 17 00:00:00 2001 From: Gabriele Bozzola Date: Fri, 8 Sep 2023 17:25:15 -0700 Subject: [PATCH] Refactor produce_common_diagnostic_function Refactor closure in favor of a function with more arguments --- src/diagnostics/default_diagnostics.jl | 60 +++++++++++++--------- src/diagnostics/defaults/moisture_model.jl | 4 +- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/diagnostics/default_diagnostics.jl b/src/diagnostics/default_diagnostics.jl index 59fedef52da..46d49e01a27 100644 --- a/src/diagnostics/default_diagnostics.jl +++ b/src/diagnostics/default_diagnostics.jl @@ -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) @@ -67,14 +67,17 @@ 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()) @@ -82,11 +85,14 @@ get_daily_min = produce_common_diagnostic_function(24 * 60 * 60, min) 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()) @@ -94,7 +100,8 @@ get_daily_average = produce_common_diagnostic_function( 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()) @@ -102,7 +109,8 @@ get_hourly_max = produce_common_diagnostic_function(60 * 60, max) 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()) @@ -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!, ) diff --git a/src/diagnostics/defaults/moisture_model.jl b/src/diagnostics/defaults/moisture_model.jl index 14b26d0eb75..3dc063c7d8c 100644 --- a/src/diagnostics/defaults/moisture_model.jl +++ b/src/diagnostics/defaults/moisture_model.jl @@ -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"],