From 2e0ac6ac453e794625d348dfac90b9d9ad1e8221 Mon Sep 17 00:00:00 2001 From: Gabriele Bozzola Date: Fri, 21 Jun 2024 15:27:25 -0700 Subject: [PATCH] Small cleanup --- src/diagnostics/bucket_compute_methods.jl | 51 ----------------------- src/diagnostics/default_diagnostics.jl | 10 ++--- src/diagnostics/define_diagnostics.jl | 36 ++++++++++++++-- src/diagnostics/diagnostic.jl | 16 +++---- 4 files changed, 45 insertions(+), 68 deletions(-) diff --git a/src/diagnostics/bucket_compute_methods.jl b/src/diagnostics/bucket_compute_methods.jl index c987d2747b..9594ca729d 100644 --- a/src/diagnostics/bucket_compute_methods.jl +++ b/src/diagnostics/bucket_compute_methods.jl @@ -1,54 +1,3 @@ -# General helper functions for undefined diagnostics for a particular model -error_diagnostic_variable(variable, land_model::T) where {T} = - error("Cannot compute $variable with model = $T") - -macro generate_error_functions(variable_names...) - functions = Expr[] - for variable in variable_names - function_name_sym = Symbol("compute_", variable, "!") - body = esc(quote - function $function_name_sym(_, _, _, _, land_model) - error_diagnostic_variable($variable, land_model) - end - end) - push!(functions, body) - end - return quote - $(functions...) - end -end - -@generate_error_functions "albedo" "net_radiation" "surface_temperature" "surface_specific_humidity" "latent_heat_flux" "aerodynamic_resistance" "sensible_heat_flux" "vapor_flux" "surface_air_density" "soil_temperature" "subsurface_water_storage" "surface_water_content" "snow_water_equivalent" - -#= -compute_albedo!(_, _, _, _, land_model) = - error_diagnostic_variable("albedo", land_model) -compute_net_radiation!(_, _, _, _, land_model) = - error_diagnostic_variable("net_radiation", land_model) -compute_surface_temperature!(_, _, _, _, land_model) = - error_diagnostic_variable("surface_temperature", land_model) -compute_surface_specific_humidity!(_, _, _, _, land_model) = - error_diagnostic_variable("surface_specific_humidity", land_model) -compute_latent_heat_flux!(_, _, _, _, land_model) = - error_diagnostic_variable("latent_heat_flux", land_model) -compute_aerodynamic_resistance!(_, _, _, _, land_model) = - error_diagnostic_variable("aerodynamic_resistance", land_model) -compute_sensible_heat_flux!(_, _, _, _, land_model) = - error_diagnostic_variable("sensible_heat_flux", land_model) -compute_vapor_flux!(_, _, _, _, land_model) = - error_diagnostic_variable("vapor_flux", land_model) -compute_surface_air_density!(_, _, _, _, land_model) = - error_diagnostic_variable("surface_air_density", land_model) -compute_soil_temperature!(_, _, _, _, land_model) = - error_diagnostic_variable("soil_temperature", land_model) -compute_subsurface_water_storage!(_, _, _, _, land_model) = - error_diagnostic_variable("subsurface_water_storage", land_model) -compute_surface_water_content!(_, _, _, _, land_model) = - error_diagnostic_variable("surface_water_content", land_model) -compute_snow_water_equivalent!(_, _, _, _, land_model) = - error_diagnostic_variable("snow_water_equivalent", land_model) -=# - # stored in p function compute_albedo!(out, Y, p, t, land_model::BucketModel) diff --git a/src/diagnostics/default_diagnostics.jl b/src/diagnostics/default_diagnostics.jl index 460203cb39..ec61b7e122 100644 --- a/src/diagnostics/default_diagnostics.jl +++ b/src/diagnostics/default_diagnostics.jl @@ -1,11 +1,11 @@ export default_diagnostics -# This file is included by Diagnostics.jl and defines all the defaults for various models (e.g., Bucket, SoilCanopyModel). -# A model here is either a standalone (e.g., Bucket) or integrated (e.g., SoilCanopy) model. +# This file is included by Diagnostics.jl and defines all the defaults for +# various models (e.g., Bucket, SoilCanopyModel). A model here is either a +# standalone (e.g., Bucket) or integrated (e.g., SoilCanopy) model. # -# If you are developing new models, add your defaults here. If you want to add more high -# level interfaces, add them here. Feel free to include extra files. - +# If you are developing new models, add your defaults here. If you want to add +# more high level interfaces, add them here. Feel free to include extra files. # Bucket model diff --git a/src/diagnostics/define_diagnostics.jl b/src/diagnostics/define_diagnostics.jl index a2e30eea2c..04b4cc4730 100644 --- a/src/diagnostics/define_diagnostics.jl +++ b/src/diagnostics/define_diagnostics.jl @@ -1,7 +1,38 @@ +# General helper functions for undefined diagnostics for a particular model +error_diagnostic_variable(variable, land_model::T) where {T} = + error("Cannot compute $variable with model = $T") + +# generate_error_functions is helper macro that generates the error message +# when the user tries calling something that is incompatible with the model +macro generate_error_functions(variable_names...) + functions = Expr[] + for variable in variable_names + function_name_sym = Symbol("compute_", variable, "!") + body = esc(quote + function $function_name_sym(_, _, _, _, land_model) + error_diagnostic_variable($variable, land_model) + end + end) + push!(functions, body) + end + return quote + $(functions...) + end +end + +@generate_error_functions "albedo" "net_radiation" "surface_temperature" +"surface_specific_humidity" "latent_heat_flux" "aerodynamic_resistance" +"sensible_heat_flux" "vapor_flux" "surface_air_density" "soil_temperature" +"subsurface_water_storage" "surface_water_content" "snow_water_equivalent" +# TODO: Add the soil canopy ones + +# TODO: Automatically generate this list from the names of the diagnostics + """ define_diagnostics!(land_model) -Calls add_diagnostic_viariable! for all variables +Calls `add_diagnostic_variable!` for all available variables specializing the +compute function for `land_model`. """ function define_diagnostics!(land_model) @@ -617,6 +648,3 @@ function define_diagnostics!(land_model) compute_soil_ice!(out, Y, p, t, land_model), ) end - -# define_diagnostics!(1.0) # populates ALL_DIAGNOSTICS from diagnostic.jl -# needs to be moved to docs make table of diagnostic variables diff --git a/src/diagnostics/diagnostic.jl b/src/diagnostics/diagnostic.jl index a8d56389b3..4894dd3f9c 100644 --- a/src/diagnostics/diagnostic.jl +++ b/src/diagnostics/diagnostic.jl @@ -1,10 +1,9 @@ -# ClimaLand diagnostics - -# - A dictionary `ALL_DIAGNOSTICS` with all the diagnostics we know how to compute, keyed -# over their short name. If you want to add more diagnostics, look at the included files. -# You can add your own file if you want to define several new diagnostics that are -# conceptually related. The dictionary `ALL_DIAGNOSTICS` should be considered an -# implementation detail. +# ClimaLand diagnostics contains # a dictionary `ALL_DIAGNOSTICS` with all the +# diagnostics we know how to compute, keyed over their short name. If you want +# to add more diagnostics, look at the included files. You can add your own file +# if you want to define several new diagnostics that are conceptually related. +# The dictionary `ALL_DIAGNOSTICS` should be considered an implementation +# detail, use the getters/setters. const ALL_DIAGNOSTICS = Dict{String, DiagnosticVariable}() @@ -27,7 +26,6 @@ https://airtable.com/appYNLuWqAgzLbhSq/shrKcLEdssxb8Yvcp/tblL7dJkC3vl5zQLb Keyword arguments ================= - - `short_name`: Name used to identify the variable in the output files and in the file names. Short but descriptive. `ClimaLand` diagnostics are identified by the short name. We follow the Coupled Model Intercomparison Project conventions. @@ -93,6 +91,8 @@ end # Do you want to define more diagnostics? Add them here include("bucket_compute_methods.jl") include("soilcanopy_compute_methods.jl") + +# define_diagnostics.jl contains the list of all the diagnostics include("define_diagnostics.jl") # Default diagnostics and higher level interfaces