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 3 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
14 changes: 1 addition & 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 Down
112 changes: 63 additions & 49 deletions src/chelsa/future.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,54 +260,68 @@ _scenario(::Type{<:CHELSA{F}}) where F<:Future = _scenario(F)
# 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
_format(::Type{CHELSA}, ::Type{$type}) = lowercase($model_str)
push!($models, $type)
end
end
append!(eval(Symbol("$(CMIP)_MODELS")), models)
unique!(eval(Symbol("$(CMIP)_MODELS")))
end
78 changes: 19 additions & 59 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,58 +165,6 @@ See the [`getraster`](@ref) docs for implementation details.
"""
struct HabitatHeterogeneity <: RasterDataSet end

"""
ClimateModel

Abstract supertype for climate models use in [`Future`](@ref) datasets.
"""
abstract type ClimateModel end

struct ACCESS1 <: ClimateModel end
struct BNUESM <: ClimateModel end
struct CCSM4 <: ClimateModel end
struct CESM1BGC <: ClimateModel end
struct CESM1CAM5 <: ClimateModel end
struct CMCCCMS <: ClimateModel end
struct CMCCCM <: ClimateModel end
struct CNRMCM5 <: ClimateModel end
struct CSIROMk3 <: ClimateModel end
struct CanESM2 <: ClimateModel end
struct FGOALS <: ClimateModel end
struct FIOESM <: ClimateModel end
struct GFDLCM3 <: ClimateModel end
struct GFDLESM2G <: ClimateModel end
struct GFDLESM2M <: ClimateModel end
struct GISSE2HCC <: ClimateModel end
struct GISSE2H <: ClimateModel end
struct GISSE2RCC <: ClimateModel end
struct GISSE2R <: ClimateModel end
struct HadGEM2AO <: ClimateModel end
struct HadGEM2CC <: ClimateModel end
struct IPSLCM5ALR <: ClimateModel end
struct IPSLCM5AMR <: ClimateModel end
struct MIROCESMCHEM <: ClimateModel end
struct MIROCESM <: ClimateModel end
struct MIROC5 <: ClimateModel end
struct MPIESMLR <: ClimateModel end
struct MPIESMMR <: ClimateModel end
struct MRICGCM3 <: ClimateModel end
struct MRIESM1 <: ClimateModel end
struct NorESM1M <: ClimateModel end
struct BCCCSM1 <: ClimateModel end
struct Inmcm4 <: ClimateModel end
struct BCCCSM2MR <: ClimateModel end
struct CNRMCM61 <: ClimateModel end
struct CNRMESM21 <: ClimateModel end
struct CanESM5 <: ClimateModel end
struct GFDLESM4 <: ClimateModel end
struct IPSLCM6ALR <: ClimateModel end
struct MIROCES2L <: ClimateModel end
struct MIROC6 <: ClimateModel end
struct MRIESM2 <: ClimateModel end
struct UKESM <: ClimateModel end
struct MPIESMHR <: ClimateModel end

"""
CMIPphase

Expand All @@ -241,6 +189,15 @@ The Coupled Model Intercomparison Project, Phase 6.
"""
struct CMIP6 <: CMIPphase end

"""
ClimateModel

Abstract supertype for climate models use in [`Future`](@ref) datasets.
"""
abstract type ClimateModel{CMIP<:CMIPphase} end
const CMIP6_MODELS = Type{<:ClimateModel{CMIP6}}[]
const CMIP5_MODELS = Type{<:ClimateModel{CMIP5}}[]

"""
ClimateScenario

Expand Down Expand Up @@ -296,14 +253,9 @@ Can be either [`CMIP5`](@ref) or [`CMIP6`](@ref).

Climate models can be chosen from:

`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`,
`BCCCSM2MR`, `CNRMCM61`, `CNRMESM21`, `CanESM5`, `MIROCES2L`, `MIROC6` for CMIP5;
`$(join(CMIP5_MODELS, "`, `"))` for `CMIP5`;

`UKESM`, `MPIESMHR` `IPSLCM6ALR`, `MRIESM2`, `GFDLESM4` for `CMIP6`.
`$(join(CMIP6_MODELS, "`, `"))` for `CMIP6`;"

#### `ClimateScenario`

Expand Down Expand Up @@ -335,9 +287,17 @@ struct Future{D<:RasterDataSet,C<:CMIPphase,M<:ClimateModel,S<:ClimateScenario}
_dataset(::Type{<:Future{D}}) where D = D
_dataset(::Type{<:Future{BioClimPlus}}) = BioClim
_phase(::Type{<:Future{<:Any,P}}) where P = P
_phase(::Type{<:ClimateModel{P}}) where P = P

_model(::Type{<:Future{<:Any,<:Any,M}}) where M = M
_scenario(::Type{<:Future{<:Any,<:Any,<:Any,S}}) where S = S
layers(::Type{<:Future{BioClimPlus}}) = BIOCLIMPLUS_LAYERS_FUTURE

# fallback for _format
_format(::Type, RCP::Type{<:RepresentativeConcentrationPathway}) = lowercase(string(nameof(RCP)))
_format(::Type, S::Type{<:SharedSocioeconomicPathway}) = lowercase(string(nameof(S)))
_format(::Type, S::Type{<:ClimateModel}) = replace(string(nameof(S)), "_" => "-")

"""
ModisProduct <: RasterDataSet

Expand Down
83 changes: 83 additions & 0 deletions src/worldclim/future.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const WORLDCLIM_URI_CMIP6 = URI(scheme="https", host="geodata.ucdavis.edu", path="/cmip6")

layers(::Type{<:WorldClim{<:Future{Climate}}}) = (:tmin, :tmax, :prec)
getraster_keywords(::Type{<:WorldClim{<:Future{Climate}}}) = (:date, :res)


function getraster(T::Type{<:WorldClim{<:Future{Climate, CMIP6}}}, layers::Union{Tuple,Symbol};
res::String=defres(T), date
)
_getraster(T, layers, res, date)
end


function _getraster(T::Type{<:WorldClim{<:Future{Climate}}}, layer::Symbol, res::String, date)
#date_str = _date_string(T, date)
_check_layer(T, layer)
_check_res(T, res)
raster_path = rasterpath(T, layer; res, date)
if !isfile(raster_path)
_maybe_download(rasterurl(T, layer; res, date), raster_path)
end
return raster_path
end

function rasterurl(T::Type{<:WorldClim{<:Future{Climate, CMIP6, M, S}}}, layer; res, date) where {M, S}
joinpath(WORLDCLIM_URI_CMIP6, res, _format(WorldClim, M), _format(CHELSA, S), rastername(T, layer; res, date))
end

function rastername(T::Type{<:WorldClim{<:Future{Climate, CMIP6, M, S}}}, layer; res, date) where {M, S}
join(["wc2.1", res, string(layer), _format(WorldClim, M), _format(CHELSA, S), date], "_") * ".tif"
end


function _date_string(::Type{<:WorldClim{<:Future{Climate, CMIP6}}}, date)
if date < DateTime(2021)
_cmip6_date_error(date)
elseif date < DateTime(2041)
"2011-2040"
elseif date < DateTime(2061)
"2041-2060"
elseif date < DateTime(2081)
"2041-2080"
elseif date < DateTime(2101)
"2081-2100"
else
_cmip6_date_error(date)
end
end

const WORDCLIM_CMIP6_MODEL_STRINGS = [
"ACCESS-CM2"
"BCC-CSM2-MR"
"CMCC-ESM2"
"EC-Earth3-Veg"
"FIO-ESM-2-0"
"GFDL-ESM4"
"GISS-E2-1-G"
"HadGEM3-GC31-LL"
"INM-CM5-0"
"IPSL-CM6A-LR"
"MIROC6"
"MPI-ESM1-2-HR"
"MRI-ESM2-0"
"UKESM1-0-LL"
]

WORDCLIM_CMIP6_MODELS = Type{<:ClimateModel{CMIP6}}[]

for model_str in WORDCLIM_CMIP6_MODEL_STRINGS
type = Symbol(replace(model_str, "-" => "_"))
@eval begin
export $type
tiemvanderdeure marked this conversation as resolved.
Show resolved Hide resolved
if !(@isdefined $type)
rafaqz marked this conversation as resolved.
Show resolved Hide resolved
struct $type <: ClimateModel{CMIP6} end
export $type
end
push!(WORDCLIM_CMIP6_MODELS, $type)
_format(::Type{WorldClim}, ::Type{$type}) = $model_str
end
end

append!(CMIP6_MODELS, WORDCLIM_CMIP6_MODELS)
unique!(CMIP6_MODELS)
Loading