Skip to content

Commit

Permalink
Small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbozzolo committed Jun 21, 2024
1 parent 8a21f06 commit 2e0ac6a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 68 deletions.
51 changes: 0 additions & 51 deletions src/diagnostics/bucket_compute_methods.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/diagnostics/default_diagnostics.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down
36 changes: 32 additions & 4 deletions src/diagnostics/define_diagnostics.jl
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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
16 changes: 8 additions & 8 deletions src/diagnostics/diagnostic.jl
Original file line number Diff line number Diff line change
@@ -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}()

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e0ac6a

Please sign in to comment.