Skip to content

Commit

Permalink
Merge pull request #68 from CliMA/ne/pkg_ext
Browse files Browse the repository at this point in the history
Add CLIMAParameters package extension
  • Loading branch information
charleskawczynski authored Jan 18, 2024
2 parents e4168f3 + 3f337dd commit e2ad0ba
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 39 deletions.
8 changes: 8 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ 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"
Dates = "<0.0.1, 1"
DelimitedFiles = "<0.0.1, 1"
Interpolations = "0.13, 0.14, 0.15"
julia = "1.7"
CLIMAParameters = "0.8"
21 changes: 16 additions & 5 deletions artifacts/laskar2004/create_artifact.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions docs/src/InsolationExamples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions docs/src/find_equinox_perihelion_dates.jl
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
6 changes: 3 additions & 3 deletions docs/src/plot_diurnal_cycle.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
35 changes: 35 additions & 0 deletions ext/CreateParametersExt.jl
Original file line number Diff line number Diff line change
@@ -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
11 changes: 0 additions & 11 deletions parameters/create_parameters.jl

This file was deleted.

5 changes: 5 additions & 0 deletions src/Insolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 5 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions test/test_insolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))

0 comments on commit e2ad0ba

Please sign in to comment.