-
Notifications
You must be signed in to change notification settings - Fork 10
/
alwb.jl
159 lines (122 loc) · 5.79 KB
/
alwb.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const ALWB_URI = URI(scheme="http", host="www.bom.gov.au", path="/jsp/awra/thredds/fileServer/AWRACMS")
abstract type DataMode end
"""
Values <: DataMode
Get the dataset as regular measured values.
"""
struct Values <: DataMode end
"""
Deciles <: DataMode
Get the dataset in relative deciles.
"""
struct Deciles <: DataMode end
# Docs below
struct ALWB{M<:DataMode,D<:Union{Day,Month,Year}} <: RasterDataSource end
layers(::Type{<:ALWB}) = (
:rain_day, :s0_pct, :ss_pct, :sd_pct, :sm_pct, :qtot, :etot,
:e0, :ma_wet, :pen_pet, :fao_pet, :asce_pet, :msl_wet, :dd
)
# Days are in 1 year nc files
date_step(::Type{<:ALWB{<:Any,Day}}) = Year(1)
# Months and years are in single files
date_step(::Type{<:ALWB{<:Any,Month}}) = Year(100)
date_step(::Type{<:ALWB{<:Any,Year}}) = Year(100)
has_constant_dims(::Type{<:ALWB}) = false
@doc """
ALWB{Union{Deciles,Values},Union{Day,Month,Year}} <: RasterDataSource
Data from the Australian Landscape Water Balance (ALWB) data source.
See: [www.bom.gov.au/water/landscape](http://www.bom.gov.au/water/landscape)
The dataset contains NetCDF files. They have a time dimension so that multiple
dates are stored in each file.
The available layers are: `$(layers(ALWB))`, available in daily, monthly and
annual resolutions, and as `Values` or relative `Deciles`.
`getraster` for `ALWB` must use a `date` keyword to specify the date to download.
See the [`getraster`](@ref) docs for implementation details.
""" ALWB
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/rain_day_2017.nc
# Precipiation = "rain_day"
# SoilMoisture_Upper = "s0_pct"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/ss_pct_2017.nc
# SoilMoisture_Lower = "ss_pct"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/sd_pct_2017.nc
# SoilMoisture_Deep = "sd_pct"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/sm_pct_2017.nc
# SoilMoisture_RootZone = "sm_pct" # http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/qtot_2017.nc # Runoff = "qtot"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/etot_2017.nc
# Evapotrans_Actual = "etot"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/e0_2017.nc
# Evapotrans_Potential_Landscape = "e0"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/ma_wet_2017.nc
# Evapotrans_Potential_Areal = "ma_wet"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/pen_pet_2017.nc
# Evapotrans_Potential_SyntheticPan = "pen_pet"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/fao_pet_2017.nc
# Evapotrans_RefCrop_Short = "fao_pet"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/asce_pet_2017.nc
# Evapotrans_RefCrop_Tall = "asce_pet"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/etot_2017.nc
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/msl_wet_2017.nc
# Evaporation_OpenWater = "msl_wet"
# http://www.bom.gov.au/jsp/awra/thredds/fileServer/AWRACMS/values/day/dd_2017.nc
# DeepDrainage = "dd"
"""
getraster(source::Type{<:ALWB{Union{Deciles,Values},Union{Day,Month,Year}}}, [layer]; date)
Download [`ALWB`](@ref) weather data from
[www.bom.gov.au/water/landscape](http://www.bom.gov.au/water/landscape) as values or
deciles with timesteps of `Day`, `Month` or `Year`.
# Arguments
- `layer`: `Symbol` or `Tuple` of `Symbol` from `$(layers(ALWB))`. Without a
`layer` argument, all layers will be downloaded, and a `NamedTuple` of paths returned.
# Keywords
- `date`: a `DateTime`, `AbstractVector` of `DateTime` or a `Tuple` of start and end dates.
For multiple dates, a `Vector` of multiple filenames will be returned.
ALWB is available with a daily, monthly, and yearly, timestep.
# Example
This will return the file containing annual averages, including your date:
```julia
julia> getraster(ALWB{Values,Year}, :ss_pct; date=Date(2001, 2))
"/your/RASTERDATASOURCES_PATH/ALWB/values/month/ss_pct.nc"
```
Returns the filepath/s of the downloaded or pre-existing files.
"""
function getraster(T::Type{<:ALWB}, layers::Union{Tuple,Symbol}; date)
_getraster(T, layers, date)
end
getraster_keywords(::Type{<:ALWB}) = (:date,)
function _getraster(T::Type{<:ALWB{M,P}}, layers, dates::Tuple) where {M,P}
_getraster(T, layers, date_sequence(T, dates))
end
function _getraster(T::Type{<:ALWB}, layers, dates::AbstractArray)
_getraster.(T, Ref(layers), dates)
end
function _getraster(T::Type{<:ALWB}, layers::Tuple, date::Dates.TimeType)
_map_layers(T, layers, date)
end
function _getraster(T::Type{<:ALWB}, layer::Symbol, date::Dates.TimeType)
_check_layer(T, layer)
mkpath(rasterpath(T))
url = rasterurl(T, layer; date=date)
path = rasterpath(T, layer; date=date)
_maybe_download(url, path)
path
end
rastername(T::Type{<:ALWB{M,P}}, layer; date) where {M,P} =
string(layer, _pathsegment(P, date), ".nc")
rasterpath(::Type{ALWB}) = joinpath(rasterpath(), "ALWB")
rasterpath(::Type{ALWB{M,P}}) where {M,P} =
joinpath(joinpath(rasterpath(), "ALWB"), map(_pathsegment, (M, P))...)
rasterpath(T::Type{<:ALWB}, layer; date=nothing) =
joinpath(rasterpath(T), rastername(T, layer; date))
rasterurl(T::Type{<:ALWB{M,P}}, layer; date) where {M,P} =
joinpath(ALWB_URI, _pathsegments(T)..., rastername(T, layer; date))
# Utility methods
_pathsegments(::Type{ALWB{M,P}}) where {M,P} = _pathsegment(M), _pathsegment(P)
_pathsegment(::Type{Values}) = "values"
_pathsegment(::Type{Deciles}) = "deciles"
_pathsegment(::Type{Day}) = "day"
_pathsegment(::Type{Month}) = "month"
_pathsegment(::Type{Year}) = "year"
# Days are in whole-year files
_pathsegment(::Type{Day}, date) = "_" * string(year(date))
# Months and years are all in one file
_pathsegment(::Type{<:Union{Year,Month}}, date) = ""