Skip to content

Commit

Permalink
reorganise future worldclim
Browse files Browse the repository at this point in the history
  • Loading branch information
tiemvanderdeure committed Sep 10, 2024
1 parent af84294 commit bc73812
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
53 changes: 32 additions & 21 deletions src/worldclim/future.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,82 @@ const WORLDCLIM_URI_CMIP6 = URI(scheme="https", host="geodata.ucdavis.edu", path
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, "bioclim"; res, date)
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}}, layer; res, date)
joinpath(WORLDCLIM_URI_CMIP6, res, _format(T, _model(T)), _format(T, _scenario(T)), rastername(T, layer; res, date))
end

function rastername(T::Type{<:WorldClim{<:Future}}, layer; res, date)
join(["wc2.1", res, string(layer), _format(T, _model(T)), _format(T, _scenario(T)), date], "_") * ".tif"
end
## Bioclim
getraster_keywords(::Type{<:WorldClim{<:Future{BioClim}}}) = (:date, :res)

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

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

function rasterpath(T::Type{<:WorldClim{<:Future{Climate}}}, layer; res, date)
joinpath(_rasterpath(T), rastername(T, layer; res, date))
end
function rasterpath(T::Type{<:WorldClim{<:Future{BioClim}}}; res, date)
joinpath(_rasterpath(T), rastername(T; res, date))
end
function _rasterpath(T::Type{<:WorldClim{<:Future}})
joinpath(rasterpath(WorldClim), "Future", string(_dataset(T)), string(_scenario(T)), string(_model(T)))
end

function rasterurl(T::Type{<:WorldClim{<:Future}}, args...; res, date)
joinpath(WORLDCLIM_URI_CMIP6, res, _format(T, _model(T)), _format(T, _scenario(T)), rastername(T, args...; res, date))
end

function rastername(T::Type{<:WorldClim{<:Future}}, layer; res, date)
join(["wc2.1", res, string(layer), _format(T, _model(T)), _format(T, _scenario(T)), _date_string(T, date)], "_") * ".tif"
end
rastername(T::Type{<:WorldClim{<:Future{BioClim}}}; kw...) = rastername(T, :bioc, ; kw...)

# copy-pasted in from CHELSA - must be some way to implement this abstractly?
_dataset(::Type{<:WorldClim{F}}) where F<:Future = _dataset(F)
_phase(::Type{<:WorldClim{F}}) where F<:Future = _phase(F)
_model(::Type{<:WorldClim{F}}) where F<:Future = _model(F)
_scenario(::Type{<:WorldClim{F}}) where F<:Future = _scenario(F)



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


## Handle all the models
const WORDCLIM_CMIP6_MODEL_STRINGS = [
"ACCESS-CM2"
"BCC-CSM2-MR"
Expand Down
3 changes: 2 additions & 1 deletion src/worldclim/shared.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const WORLDCLIM_URI = URI(scheme="https", host="geodata.ucdavis.edu", path="/cli
resolutions(::Type{<:WorldClim}) = ("30s", "2.5m", "5m", "10m")
defres(::Type{<:WorldClim}) = "10m"

rasterpath(::Type{WorldClim{T}}) where T = joinpath(rasterpath(), "WorldClim", string(nameof(T)))
rasterpath(::Type{WorldClim}) = joinpath(rasterpath(), "WorldClim")
rasterpath(::Type{WorldClim{T}}) where T = joinpath(rasterpath(WorldClim), string(nameof(T)))
rasterpath(T::Type{<:WorldClim}, layer; kw...) =
joinpath(rasterpath(T), string(layer), rastername(T, layer; kw...))

Expand Down

0 comments on commit bc73812

Please sign in to comment.