Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: Add future data for worldclim and improve climate model handling #69

Merged
merged 23 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9fc47b6
define climate models programatically
tiemvanderdeure Aug 22, 2024
12f6ede
first draft for future wordclim climate
tiemvanderdeure Aug 22, 2024
2e1cc4a
define ClimateModel before using it
tiemvanderdeure Aug 22, 2024
bc62db1
first draft for future worldclim bioclim
tiemvanderdeure Aug 22, 2024
af84294
move exports to main file
tiemvanderdeure Sep 3, 2024
bc73812
reorganise future worldclim
tiemvanderdeure Sep 10, 2024
692c579
Merge branch 'EcoJulia:master' into future_worldclim
tiemvanderdeure Sep 23, 2024
9c2e729
reorganise some code
tiemvanderdeure Oct 1, 2024
4bc9ac8
always use _format not string
tiemvanderdeure Oct 1, 2024
235184a
Merge branch 'EcoJulia:master' into future_worldclim
tiemvanderdeure Oct 2, 2024
8bd1975
add a dispatch on worldclim future with layers argument
tiemvanderdeure Oct 2, 2024
06e1b0b
fix some dispatches
tiemvanderdeure Oct 2, 2024
3f29737
add worldclim future tests
tiemvanderdeure Oct 2, 2024
68790b4
always _format(T) and no dashes in file path
tiemvanderdeure Oct 2, 2024
eb3f8c0
update chelsa future test
tiemvanderdeure Oct 2, 2024
29680b6
fix a test
tiemvanderdeure Oct 2, 2024
81f42fd
clean up a line outside testset
tiemvanderdeure Oct 2, 2024
63d815c
soft document why dashes in modelname path are dropped
tiemvanderdeure Oct 2, 2024
83fddea
add a generic date formatter
tiemvanderdeure Oct 3, 2024
0611501
make future worldclim work for multiple dates
tiemvanderdeure Oct 3, 2024
809b3ba
fix worldclim futur tests
tiemvanderdeure Oct 3, 2024
2b382d1
fix date_step and range for chelsa bioclim
tiemvanderdeure Oct 3, 2024
4d0a476
define layerkeys for WorldClim{Future{BioClim}}
tiemvanderdeure Oct 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/RasterDataSources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ export ECO4ESIPTJPL,ECO4WUE,GEDI03,GEDI04_B,MCD12Q1,MCD12Q2,MCD15A2H,
MYD17A2H, MYD17A3HGF, MYD21A2, SIF005, SIF_ANN, VNP09A1, VNP09H1,
VNP13A1, VNP15A2H, VNP21A2, VNP22Q2

# Climate models from CMIP5 (used in CHELSA)
export ACCESS1, BNUESM, CCSM4, CESM1BGC, CESM1CAM5, CMCCCMS, CMCCCM, CNRMCM5,
CSIROMk3, CanESM2, FGOALS, FIOESM, GFDLCM3, GFDLESM2G, GFDLESM2M, GISSE2HCC,
GISSE2H, GISSE2RCC, GISSE2R, HadGEM2AO, HadGEM2CC, IPSLCM5ALR, IPSLCM5AMR,
MIROCESMCHEM, MIROCESM, MIROC5, MPIESMLR, MPIESMMR, MRICGCM3, MRIESM1, NorESM1M,
BCCCSM1, Inmcm4

# Climate models from CMIP6 (used in WorldClim)
export BCCCSM2MR, CNRMCM61, CNRMESM21, CanESM5, GFDLESM4, IPSLCM6ALR, MIROCES2L, MIROC6, MRIESM2

# Climate models from CMIP6 (CHELSA)
export UKESM, MPIESMHR

export Values, Deciles

export getraster
Expand All @@ -63,6 +50,7 @@ include("worldclim/bioclim.jl")
include("worldclim/climate.jl")
include("worldclim/weather.jl")
include("worldclim/elevation.jl")
include("worldclim/future.jl")

include("chelsa/shared.jl")
include("chelsa/climate.jl")
Expand All @@ -84,4 +72,9 @@ include("modis/products.jl")
include("modis/utilities.jl")
include("modis/examples.jl")

for model in [CMIP5_MODELS; CMIP6_MODELS]
symb = nameof(model)
@eval export $symb
end

end # module
144 changes: 81 additions & 63 deletions src/chelsa/future.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,46 +155,46 @@ function _rastername(
::Type{CMIP5}, T::Type{<:CHELSA{<:Future{BioClim}}}, layer::Integer; date
)
date_string = _date_string(_phase(T), date)
mod = _format(CHELSA, _model(T))
scen = _format(CHELSA, _scenario(T))
mod = _format(T, _model(T))
scen = _format(T, _scenario(T))
return "CHELSA_bio_mon_$(mod)_$(scen)_r1i1p1_g025.nc_$(layer)_$(date_string)_V1.2.tif"
end
function _rastername(
::Type{CMIP5}, T::Type{<:CHELSA{<:Future{Climate}}}, layer::Symbol; date, month
)
date_string = _date_string(_phase(T), date)
mod = _format(CHELSA, _model(T))
scen = _format(CHELSA, _scenario(T))
mod = _format(T, _model(T))
scen = _format(T, _scenario(T))
key = CHELSAKEY[layer]
suffix = layer === :prec ? "" : "_V1.2" # prec filenames dont end in _V1.2
return "CHELSA_$(key)_mon_$(mod)_$(scen)_r1i1p1_g025.nc_$(month)_$(date_string)$(suffix).tif"
end
function _rastername(::Type{CMIP6}, T::Type{<:CHELSA{<:Future{BioClim}}}, layer::Integer; date)
date_string = _date_string(_phase(T), date)
mod = _format(CHELSA, _model(T))
scen = _format(CHELSA, _scenario(T))
mod = _format(T, _model(T))
scen = _format(T, _scenario(T))
return "CHELSA_bio$(layer)_$(date_string)_$(mod)_$(scen)_V.2.1.tif"
end
function _rastername(::Type{CMIP6}, T::Type{<:CHELSA{<:Future{BioClimPlus}}}, layer::Symbol; date)
date_string = _date_string(_phase(T), date)
mod = _format(CHELSA, _model(T))
scen = _format(CHELSA, _scenario(T))
mod = _format(T, _model(T))
scen = _format(T, _scenario(T))
return "CHELSA_$(layer)_$(date_string)_$(mod)_$(scen)_V.2.1.tif"
end
function _rastername(
::Type{CMIP6}, T::Type{<:CHELSA{<:Future{Climate}}}, layer::Symbol; date, month
)
# CMIP6 Climate uses an underscore in the date string, of course
date_string = replace(_date_string(_phase(T), date), "-" => "_")
mod = _format(CHELSA, _model(T))
scen = _format(CHELSA, _scenario(T))
mod = _format(T, _model(T))
scen = _format(T, _scenario(T))
key = CHELSAKEY[layer]
mon = lpad(month, 2, '0')
return "CHELSA_$(mod)_r1i1p1f1_w5e5_$(scen)_$(key)_$(mon)_$(date_string)_norm.tif"
end

function rasterpath(T::Type{<:CHELSA{<:Future}})
joinpath(rasterpath(CHELSA), "Future", string(_dataset(T)), string(_scenario(T)), string(_model(T)))
joinpath(rasterpath(CHELSA), "Future", _format(T, _dataset(T)), _format(_scenario(T)), replace(_format(_model(T)), "-" => ""))
tiemvanderdeure marked this conversation as resolved.
Show resolved Hide resolved
end
function rasterpath(T::Type{<:CHELSA{<:Future}}, layer; kw...)
joinpath(rasterpath(T), rastername(T, layer; kw...))
Expand All @@ -216,8 +216,8 @@ function _urlpath(::Type{CMIP5}, T::Type{<:CHELSA{<:Future}}, name, date_str)
end
function _urlpath(::Type{CMIP6}, T::Type{<:CHELSA{<:Future}}, name, date_str)
# The model is in uppercase in the URL for CMIP6
mod = uppercase(_format(CHELSA, _model(T)))
scen = _format(CHELSA, _scenario(T))
mod = uppercase(_format(T, _model(T)))
scen = _format(T, _scenario(T))
key = CHELSAKEY[name]
return "chelsav2/GLOBAL/climatologies/$date_str/$mod/$scen/$key/"
end
Expand Down Expand Up @@ -254,61 +254,79 @@ _cmip5_date_error(date) = error("CMIP5 covers the period from 2041-2080, not inc
_cmip6_date_error(date) = error("CMIP6 covers the period from 1981-2100, not including $date")

_dataset(::Type{<:CHELSA{F}}) where F<:Future = _dataset(F)
_dataset(::Type{<:Future{BioClimPlus}}) = BioClim # to make sure bioclimplus and bioclim end up in the same folder
_phase(::Type{<:CHELSA{F}}) where F<:Future = _phase(F)
_model(::Type{<:CHELSA{F}}) where F<:Future = _model(F)
_scenario(::Type{<:CHELSA{F}}) where F<:Future = _scenario(F)

# Climate model string formatters for CHELSA Future
## overload _format to use lowercase
_format(::Type{<:CHELSA}, T::Type{<:SharedSocioeconomicPathway}) = lowercase(_format(T))
_format(::Type{<:CHELSA}, T::Type{<:RepresentativeConcentrationPathway}) = lowercase(_format(T))
_format(::Type{<:CHELSA{<:Future{<:Any, CMIP6}}}, T::Type{<:ClimateModel}) = lowercase(_format(T))

## Climate model string formatters for CHELSA Future
# CMIP5
_format(::Type{CHELSA}, ::Type{ACCESS1}) = "ACCESS1-0"
_format(::Type{CHELSA}, ::Type{BNUESM}) = "BNU-ESM"
_format(::Type{CHELSA}, ::Type{CCSM4}) = "CCSM4"
_format(::Type{CHELSA}, ::Type{CESM1BGC}) = "CESM1-BGC"
_format(::Type{CHELSA}, ::Type{CESM1CAM5}) = "CESM1-CAM5"
_format(::Type{CHELSA}, ::Type{CMCCCMS}) = "CMCC-CMS"
_format(::Type{CHELSA}, ::Type{CMCCCM}) = "CMCC-CM"
_format(::Type{CHELSA}, ::Type{CNRMCM5}) = "CNRM-CM5"
_format(::Type{CHELSA}, ::Type{CSIROMk3}) = "CSIRO-Mk3"
_format(::Type{CHELSA}, ::Type{CanESM2}) = "CanESM2"
_format(::Type{CHELSA}, ::Type{FGOALS}) = "FGOALS-g2"
_format(::Type{CHELSA}, ::Type{FIOESM}) = "FIO-ESM"
_format(::Type{CHELSA}, ::Type{GFDLCM3}) = "GFDL-CM3"
_format(::Type{CHELSA}, ::Type{GFDLESM2G}) = "GFDL-ESM2G"
_format(::Type{CHELSA}, ::Type{GFDLESM2M}) = "GFDL-ESM2M"
_format(::Type{CHELSA}, ::Type{GISSE2HCC}) = "GISS-E2-H-CC"
_format(::Type{CHELSA}, ::Type{GISSE2H}) = "GISS-E2-H"
_format(::Type{CHELSA}, ::Type{GISSE2RCC}) = "GISS-E2-R-CC"
_format(::Type{CHELSA}, ::Type{GISSE2R}) = "GISS-E2-R"
_format(::Type{CHELSA}, ::Type{HadGEM2AO}) = "HadGEM2-AO"
_format(::Type{CHELSA}, ::Type{HadGEM2CC}) = "HadGEM2-CC"
_format(::Type{CHELSA}, ::Type{IPSLCM5ALR}) = "IPSL-CM5A-LR"
_format(::Type{CHELSA}, ::Type{IPSLCM5AMR}) = "IPSL-CM5A-MR"
_format(::Type{CHELSA}, ::Type{MIROCESMCHEM}) = "MIROC-ESM-CHEM"
_format(::Type{CHELSA}, ::Type{MIROCESM}) = "MIROC-ESM"
_format(::Type{CHELSA}, ::Type{MIROC5}) = "MIROC5"
_format(::Type{CHELSA}, ::Type{MPIESMLR}) = "MPI-ESM-LR"
_format(::Type{CHELSA}, ::Type{MPIESMMR}) = "MPI-ESM-MR"
_format(::Type{CHELSA}, ::Type{MRICGCM3}) = "MRI-CGCM3"
_format(::Type{CHELSA}, ::Type{MRIESM1}) = "MRI-ESM1"
_format(::Type{CHELSA}, ::Type{NorESM1M}) = "NorESM1-M"
_format(::Type{CHELSA}, ::Type{BCCCSM1}) = "bcc-csm-1"
_format(::Type{CHELSA}, ::Type{Inmcm4}) = "inmcm4"
const CHELSA_CMIP5_MODELS = Type{<:ClimateModel{CMIP5}}[]
const CHELSA_CMIP5_MODEL_STRINGS =
[
"ACCESS1-0"
"BNU-ESM"
"CCSM4"
"CESM1-BGC"
"CESM1-CAM5"
"CMCC-CMS"
"CMCC-CM"
"CNRM-CM5"
"CSIRO-Mk3"
"CanESM2"
"FGOALS-g2"
"FIO-ESM"
"GFDL-CM3"
"GFDL-ESM2G"
"GFDL-ESM2M"
"GISS-E2-H-CC"
"GISS-E2-H"
"GISS-E2-R-CC"
"GISS-E2-R"
"HadGEM2-AO"
"HadGEM2-CC"
"IPSL-CM5A-LR"
"IPSL-CM5A-MR"
"MIROC-ESM-CHEM"
"MIROC-ESM"
"MIROC5"
"MPI-ESM-LR"
"MPI-ESM-MR"
"MRI-CGCM3"
"MRI-ESM1"
"NorESM1-M"
"bcc-csm-1"
"inmcm4"
]

# CMIP6
_format(::Type{CHELSA}, ::Type{GFDLESM4}) = "gfdl-esm4"
_format(::Type{CHELSA}, ::Type{IPSLCM6ALR}) = "ipsl-cm6a-lr"
_format(::Type{CHELSA}, ::Type{MPIESMHR}) = "mpi-esm1-2-hr"
_format(::Type{CHELSA}, ::Type{MRIESM2}) = "mri-esm2-0"
_format(::Type{CHELSA}, ::Type{UKESM}) = "ukesm1-0-ll"

# Format scenarios
_format(::Type{CHELSA}, ::Type{RCP26}) = "rcp26"
_format(::Type{CHELSA}, ::Type{RCP45}) = "rcp45"
_format(::Type{CHELSA}, ::Type{RCP60}) = "rcp60"
_format(::Type{CHELSA}, ::Type{RCP85}) = "rcp85"

_format(::Type{CHELSA}, ::Type{SSP126}) = "ssp126"
_format(::Type{CHELSA}, ::Type{SSP245}) = "ssp245"
_format(::Type{CHELSA}, ::Type{SSP370}) = "ssp370"
_format(::Type{CHELSA}, ::Type{SSP585}) = "ssp585"
const CHELSA_CMIP6_MODELS = Type{<:ClimateModel{CMIP6}}[]
const CHELSA_CMIP6_MODEL_STRINGS = [
"GFDL-ESM4"
"IPSL-CM6A-LR"
"MPI-ESM1-2-HR"
"MRI-ESM2-0"
"UKESM1-0-LL"
]

for CMIP in [:CMIP5, :CMIP6]
strings = eval(Symbol("CHELSA_$(CMIP)_MODEL_STRINGS"))
models = eval(Symbol("CHELSA_$(CMIP)_MODELS"))
for model_str in strings
type = Symbol(replace(model_str, "-" => "_"))
@eval begin
if !(@isdefined $type)
struct $type <: ClimateModel{$CMIP} end
export $type
end
push!($models, $type)
end
end
append!(eval(Symbol("$(CMIP)_MODELS")), models)
unique!(eval(Symbol("$(CMIP)_MODELS")))
end
5 changes: 5 additions & 0 deletions src/shared.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ function _map_layers(T, layers, args...; kw...)
keys = layerkeys(T, layers)
return NamedTuple{keys}(filenames)
end

# fallback for _format
_format(T::Type) = string(nameof(T))
_format(M::Type{<:ClimateModel}) = replace(string(nameof(M)), "_" => "-")
_format(::Type, T::Type) = _format(T)
Loading
Loading