From 0390fdf9f7764ed1aa3f666ee02ccbacbbe232ae Mon Sep 17 00:00:00 2001 From: nefrathenrici Date: Wed, 17 Jan 2024 16:07:56 -0800 Subject: [PATCH 1/2] Add CLIMAParameters package extension --- Project.toml | 8 ++++++ docs/src/InsolationExamples.md | 16 +++++------ docs/src/find_equinox_perihelion_dates.jl | 6 ++-- docs/src/plot_diurnal_cycle.jl | 6 ++-- ext/CreateParametersExt.jl | 35 +++++++++++++++++++++++ parameters/create_parameters.jl | 11 ------- src/Insolation.jl | 5 ++++ test/runtests.jl | 12 ++++---- test/test_insolation.jl | 4 +-- 9 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 ext/CreateParametersExt.jl delete mode 100644 parameters/create_parameters.jl diff --git a/Project.toml b/Project.toml index eba02c5..721a05c 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,13 @@ Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" +CLIMAParameters = "6eacf6c3-8458-43b9-ae03-caf5306d3d53" + +[weakdeps] +CLIMAParameters = "6eacf6c3-8458-43b9-ae03-caf5306d3d53" + +[extensions] +CreateParametersExt = "CLIMAParameters" [compat] Artifacts = "<0.0.1, 1" @@ -15,3 +22,4 @@ Dates = "<0.0.1, 1" DelimitedFiles = "<0.0.1, 1" Interpolations = "0.13, 0.14, 0.15" julia = "1.7" +CLIMAParameters = "0.8" diff --git a/docs/src/InsolationExamples.md b/docs/src/InsolationExamples.md index 5014d5e..e28586b 100644 --- a/docs/src/InsolationExamples.md +++ b/docs/src/InsolationExamples.md @@ -27,10 +27,10 @@ diurnal_cycle(lat, lon, date, od, timezone, "Finland_June.png") ```@example import Insolation import Insolation.Parameters as IP +import CLIMAParameters as CP FT = Float64 -include(joinpath(pkgdir(Insolation), "parameters", "create_parameters.jl")) -param_set = create_insolation_parameters(FT) +param_set = IP.InsolationParameters(FT) include("plot_insolation.jl") @@ -49,10 +49,10 @@ plot_day_lat_insolation(days, lats, F0, "YlOrRd", title, "insol_example1.png") ```@example import Insolation import Insolation.Parameters as IP +import CLIMAParameters as CP FT = Float64 -include(joinpath(pkgdir(Insolation), "parameters", "create_parameters.jl")) -param_set = create_insolation_parameters(FT) +param_set = IP.InsolationParameters(FT) include("plot_insolation.jl") # hide γ0 = IP.obliq_epoch(param_set) # hide @@ -62,7 +62,7 @@ od = Insolation.OrbitalData() days, lats, F0 = calc_day_lat_insolation(od, 365, 180, param_set) # hide # decrease γ to 20.0° -param_set = create_insolation_parameters(FT, (;obliq_epoch = deg2rad(20.0))) +param_set = IP.InsolationParameters(FT, (; obliq_epoch = deg2rad(20.0))) γ1 = IP.obliq_epoch(param_set) days, lats, F2 = calc_day_lat_insolation(od, 365, 180, param_set) @@ -78,10 +78,10 @@ plot_day_lat_insolation(days, lats, F2-F0, "PRGn", title, "insol_example2b.png") ```@example import Insolation import Insolation.Parameters as IP +import CLIMAParameters as CP FT = Float64 -include(joinpath(pkgdir(Insolation), "parameters", "create_parameters.jl")) -param_set = create_insolation_parameters(FT) +param_set = IP.InsolationParameters(FT) include("plot_insolation.jl") # hide γ0 = IP.obliq_epoch(param_set) # hide @@ -91,7 +91,7 @@ od = Insolation.OrbitalData() days, lats, F0 = calc_day_lat_insolation(od, 365, 180, param_set) # hide # now change obliquity to 97.86° -param_set = create_insolation_parameters(FT, (;obliq_epoch = deg2rad(97.86))) +param_set = IP.InsolationParameters(FT, (;obliq_epoch = deg2rad(97.86))) γ4 = IP.obliq_epoch(param_set) days, lats, F5 = calc_day_lat_insolation(od, 365, 180, param_set) diff --git a/docs/src/find_equinox_perihelion_dates.jl b/docs/src/find_equinox_perihelion_dates.jl index 168d3a3..90eff14 100644 --- a/docs/src/find_equinox_perihelion_dates.jl +++ b/docs/src/find_equinox_perihelion_dates.jl @@ -1,12 +1,12 @@ -using Insolation using Plots using Dates +using Insolation +import Insolation.Parameters as IP import CLIMAParameters as CP FT = Float64 -include(joinpath(pkgdir(Insolation), "parameters", "create_parameters.jl")) -param_set = create_insolation_parameters(FT) +param_set = IP.InsolationParameters(FT) date0 = DateTime("2000-01-01T11:58:56.816") diff --git a/docs/src/plot_diurnal_cycle.jl b/docs/src/plot_diurnal_cycle.jl index 90f1185..b5206fd 100644 --- a/docs/src/plot_diurnal_cycle.jl +++ b/docs/src/plot_diurnal_cycle.jl @@ -1,14 +1,14 @@ -using Insolation using Plots using Dates using Statistics using Formatting +using Insolation +import Insolation.Parameters as IP import CLIMAParameters as CP FT = Float64 -include(joinpath(pkgdir(Insolation), "parameters", "create_parameters.jl")) -param_set = create_insolation_parameters(FT) +param_set = IP.InsolationParameters(FT) function diurnal_cycle(lat, lon, date, od, timezone, filename) nhours = 1000 diff --git a/ext/CreateParametersExt.jl b/ext/CreateParametersExt.jl new file mode 100644 index 0000000..7a07770 --- /dev/null +++ b/ext/CreateParametersExt.jl @@ -0,0 +1,35 @@ +module CreateParametersExt + +import Insolation.Parameters.InsolationParameters + +import CLIMAParameters as CP + +InsolationParameters( + ::Type{FT}, + overrides = NamedTuple(), +) where {FT <: AbstractFloat} = + InsolationParameters(CP.create_toml_dict(FT), overrides) + +function InsolationParameters( + toml_dict::CP.AbstractTOMLDict, + overrides = NamedTuple(), +) + name_map = (; + :epoch_time => :epoch, + :day => :day, + :anomalistic_year_length => :year_anom, + :length_orbit_semi_major => :orbit_semimaj, + :orbit_eccentricity_at_epoch => :eccentricity_epoch, + :total_solar_irradiance => :tot_solar_irrad, + :orbit_obliquity_at_epoch => :obliq_epoch, + :mean_anomalistic_at_epoch => :mean_anom_epoch, + :longitude_perihelion_at_epoch => :lon_perihelion_epoch, + ) + + parameters = CP.get_parameter_values(toml_dict, name_map, "Insolation") + parameters = merge(parameters, overrides) + FT = CP.float_type(toml_dict) + return InsolationParameters{FT}(; parameters...) +end + +end diff --git a/parameters/create_parameters.jl b/parameters/create_parameters.jl deleted file mode 100644 index 809507c..0000000 --- a/parameters/create_parameters.jl +++ /dev/null @@ -1,11 +0,0 @@ -import CLIMAParameters as CP -import Insolation.Parameters as IP - -function create_insolation_parameters(FT, overrides::NamedTuple = NamedTuple()) - toml_dict = CP.create_toml_dict(FT; dict_type = "alias") - aliases = string.(fieldnames(IP.InsolationParameters)) - pairs = CP.get_parameter_values!(toml_dict, aliases, "Insolation") - params = merge((; pairs...), overrides) # overrides - param_set = IP.InsolationParameters{FT}(; params...) - return param_set -end diff --git a/src/Insolation.jl b/src/Insolation.jl index e20147f..1a14c3c 100644 --- a/src/Insolation.jl +++ b/src/Insolation.jl @@ -64,4 +64,9 @@ end include("ZenithAngleCalc.jl") include("InsolationCalc.jl") +# For backwards compatibility with package extensions +if !isdefined(Base, :get_extension) + include(joinpath("..", "ext", "CreateParametersExt.jl")) +end + end # module diff --git a/test/runtests.jl b/test/runtests.jl index b147838..b4e72d3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,21 +1,19 @@ using Test push!(LOAD_PATH, joinpath(@__DIR__, "..")) -using Insolation using Dates using Statistics using Roots using Optim -import CLIMAParameters as CP +using Insolation import Insolation.Parameters as IP -const AIP = IP.AbstractInsolationParams import Insolation.OrbitalData -FT = Float32 +import CLIMAParameters as CP -include(joinpath(pkgdir(Insolation), "parameters", "create_parameters.jl")) -param_set = create_insolation_parameters(FT) +FT = Float32 +param_set = IP.InsolationParameters(FT) date0 = DateTime("2000-01-01T11:58:56.816") @@ -37,7 +35,7 @@ end end FT = Float64 -param_set = create_insolation_parameters(FT) +param_set = IP.InsolationParameters(FT) @testset "Orbital Params" begin include("test_orbit_param.jl") diff --git a/test/test_insolation.jl b/test/test_insolation.jl index 9ee81e4..6f71375 100644 --- a/test/test_insolation.jl +++ b/test/test_insolation.jl @@ -85,7 +85,7 @@ global_mean_insol = sum(zonal_mean_insol * area_fac) / sum(area_fac) ## Test invariance of zonal-mean insolation under rotation of ϖ ϖ0 = IP.lon_perihelion_epoch(param_set) -param_set = create_insolation_parameters(FT, (; lon_perihelion_epoch = ϖ0 + π)) +param_set = IP.InsolationParameters(FT, (; lon_perihelion_epoch = ϖ0 + π)) for (i, d) in enumerate(d_arr) for (j, lat) in enumerate(l_arr) @@ -98,4 +98,4 @@ end zonal_mean_insol_rotate = mean(F_arr, dims = 1) @test zonal_mean_insol_rotate ≈ zonal_mean_insol rtol = rtol -param_set = create_insolation_parameters(FT, (; lon_perihelion_epoch = ϖ0)) +param_set = IP.InsolationParameters(FT, (; lon_perihelion_epoch = ϖ0)) From 3f337ddeeaba3b1705b9934aa69f691c48dd7edb Mon Sep 17 00:00:00 2001 From: nefrathenrici Date: Wed, 17 Jan 2024 16:08:05 -0800 Subject: [PATCH 2/2] Format artifacts --- artifacts/laskar2004/create_artifact.jl | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/artifacts/laskar2004/create_artifact.jl b/artifacts/laskar2004/create_artifact.jl index c242e17..d1a9787 100644 --- a/artifacts/laskar2004/create_artifact.jl +++ b/artifacts/laskar2004/create_artifact.jl @@ -1,15 +1,19 @@ using Downloads, Pkg.Artifacts using Printf -insoln_file = Downloads.download("http://vo.imcce.fr/insola/earth/online/earth/La2004/INSOLN.LA2004.BTL.ASC") -insolp_file = Downloads.download("http://vo.imcce.fr/insola/earth/online/earth/La2004/INSOLP.LA2004.BTL.ASC") +insoln_file = Downloads.download( + "http://vo.imcce.fr/insola/earth/online/earth/La2004/INSOLN.LA2004.BTL.ASC", +) +insolp_file = Downloads.download( + "http://vo.imcce.fr/insola/earth/online/earth/La2004/INSOLP.LA2004.BTL.ASC", +) artifact_tree_sha1 = create_artifact() do dir insoln_lines = reverse(readlines(insoln_file)) # stored in reverse order insolp_lines = readlines(insolp_file)[2:end] # 0 is duplicated in both files - open(joinpath(dir, "INSOL.LA2004.BTL.csv"); write=true) do f + open(joinpath(dir, "INSOL.LA2004.BTL.csv"); write = true) do f println(f, "# t (kyr from J2000), ecc, obliq (rad), varpi (rad)") varpi_offset = Float64(pi) varpi_rad_prev = 1.0 + varpi_offset @@ -28,8 +32,15 @@ artifact_tree_sha1 = create_artifact() do dir end varpi_rad += varpi_offset varpi_rad_prev = varpi_rad - @printf(f, "%.3f,%.16e,%.16e,%.16e\n", kyr, ecc, obliq_rad, varpi_rad) - end + @printf( + f, + "%.3f,%.16e,%.16e,%.16e\n", + kyr, + ecc, + obliq_rad, + varpi_rad + ) + end end # add README