-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmetadata.jl
More file actions
323 lines (252 loc) · 10.5 KB
/
Copy pathmetadata.jl
File metadata and controls
323 lines (252 loc) · 10.5 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using CFTime
using Dates
using Base: @propagate_inbounds
import Oceananigans.Grids: prettysummary
struct BoundingBox{X, Y, Z}
longitude :: X
latitude :: Y
z :: Z
end
latitude_summary(::Nothing) = "latitude=nothing"
longitude_summary(::Nothing) = "longitude=nothing"
latitude_summary(lat) = string("latitude=(", prettysummary(lat[1]), ", ", prettysummary(lat[2]), ")")
longitude_summary(lon) = string("longitude=(", prettysummary(lon[1]), ", ", prettysummary(lon[2]), ")")
Base.summary(bbox::BoundingBox) = string("BoundingBox(",
longitude_summary(bbox.longitude), ", ",
latitude_summary(bbox.latitude), ")")
"""
BoundingBox(; longitude=nothing, latitude=nothing, z=nothing)
Create a bounding box with `latitude`, `longitude`, and `z` bounds on the sphere.
"""
BoundingBox(; longitude=nothing, latitude=nothing, z=nothing) =
BoundingBox(longitude, latitude, z)
struct Metadata{V, D, B}
name :: Symbol
dataset :: V
dates :: D
bounding_box :: B
dir :: String
end
is_three_dimensional(::Metadata) = true
z_interfaces(md::Metadata) = z_interfaces(md.dataset)
longitude_interfaces(md::Metadata) = longitude_interfaces(md.dataset)
latitude_interfaces(md::Metadata) = latitude_interfaces(md.dataset)
"""
Metadata(variable_name;
dataset,
dates = all_dates(dataset, variable_name),
dir = default_download_directory(dataset),
bounding_box = nothing,
start_date = nothing,
end_date = nothing)
Metadata holding a specific dataset information.
Argument
========
- `variable_name`: a symbol representing the name of the variable (for example, `:temperature`,
`:salinity`, `:u_velocity`, etc)
Keyword Arguments
=================
- `dataset`: Supported datasets are `ETOPO2022()`, `ECCO2Monthly()`, `ECCO2Daily()`, `ECCO4Monthly()`, `EN4Monthly()`,
`GLORYSDaily()`, `GLORYSMonthly()`, `RepeatYearJRA55()`, and `MultiYearJRA55()`.
- `dates`: The dates of the dataset (`Dates.AbstractDateTime` or `CFTime.AbstractCFDateTime`).
Note that `dates` can either be a range or a vector of dates, representing a time-series.
For a single date, use [`Metadatum`](@ref).
- `start_date`: If `dates = nothing`, we can prescribe the first date of metadata as a date
(`Dates.AbstractDateTime` or `CFTime.AbstractCFDateTime`). `start_date` should lie
within the date range of the dataset. Default: nothing.
- `end_date`: If `dates = nothing`, we can prescribe the last date of metadata as a date
(`Dates.AbstractDateTime` or `CFTime.AbstractCFDateTime`). `end_date` should lie
within the date range of the dataset. Default: nothing.
- `bounding_box`: Specifies the bounds of the dataset. See [`BoundingBox`](@ref).
- `dir`: The directory where the dataset is stored.
"""
function Metadata(variable_name;
dataset,
dates = all_dates(dataset, variable_name),
dir = default_download_directory(dataset),
bounding_box = nothing,
start_date = nothing,
end_date = nothing)
if !isnothing(start_date) && !isnothing(end_date)
dates = compute_native_date_range(dates, start_date, end_date)
end
return Metadata(variable_name, dataset, dates, bounding_box, dir)
end
const AnyDateTime = Union{AbstractCFDateTime, Dates.AbstractDateTime}
const Metadatum{V} = Metadata{V, <:Union{AnyDateTime, Nothing}} where V
function Base.size(metadata::Metadata)
Nx, Ny, Nz = size(metadata.dataset, metadata.name)
if metadata.dates isa AbstractArray
Nt = length(metadata.dates)
else
Nt = 1
end
return (Nx, Ny, Nz, Nt)
end
"""
Metadatum(variable_name;
dataset,
bounding_box = nothing,
date = first_date(dataset, variable_name),
dir = default_download_directory(dataset))
A specialized constructor for a [`Metadata`](@ref) object with a single date, representative of a snapshot in time.
"""
function Metadatum(variable_name;
dataset,
bounding_box = nothing,
date = first_date(dataset, variable_name),
dir = default_download_directory(dataset))
if date isa Date
date = DateTime(date)
end
if !isnothing(date) && !(date isa AnyDateTime)
msg = "`date` must be `nothing`, a `Dates.AbstractDateTime`, or `CFTime.AbstractCFDateTime`, received $(typeof(date))"
throw(ArgumentError(msg))
end
return Metadata(variable_name, dataset, date, bounding_box, dir)
end
datestr(md::Metadata) = string(first(md.dates), "--", last(md.dates))
datestr(md::Metadatum) = string(md.dates)
datasetstr(md::Metadata) = string(md.dataset)
metaprefix(md::Metadata) = string("Metadata{", md.dataset, "}")
prettysummary(dt::DateTime) = Dates.format(dt, "yyyy-mm-dd HH:MM:SS")
function Base.show(io::IO, metadata::Metadata)
V = typeof(metadata.dataset)
D = typeof(metadata.dates)
name = if metadata isa Metadatum
"Metadatum"
else
"Metadata"
end
print(io, "$name{$V, $D}:", '\n',
"├── name: $(metadata.name)", '\n',
"├── dataset: ", prettysummary(metadata.dataset), '\n',
"├── dates: ", prettysummary(metadata.dates), '\n')
bbox = metadata.bounding_box
if !isnothing(bbox)
print(io, "├── bounding_box: ", summary(bbox), '\n')
end
print(io, "└── dir: $(metadata.dir)")
end
# Treat Metadata as an array to allow iteration over the dates.
Base.length(metadata::Metadata) = isnothing(metadata.dates) ? 1 : length(metadata.dates)
Base.eltype(metadata::Metadata) = Float32
Base.summary(md::Metadata) = string(metaprefix(md),
"{", datasetstr(md), "} of ",
md.name, " for ", datestr(md))
# If only one date, it's a single element array
Base.length(metadata::Metadatum) = 1
@propagate_inbounds Base.getindex(m::Metadata, i::Int) = Metadata(m.name, m.dataset, m.dates[i], m.bounding_box, m.dir)
@propagate_inbounds Base.first(m::Metadata) = Metadata(m.name, m.dataset, m.dates[1], m.bounding_box, m.dir)
@propagate_inbounds Base.last(m::Metadata) = Metadata(m.name, m.dataset, m.dates[end], m.bounding_box, m.dir)
@inline function Base.iterate(m::Metadata, i=1)
if (i % UInt) - 1 < length(m)
return Metadata(m.name, m.dataset, m.dates[i], m.bounding_box, m.dir), i + 1
else
return nothing
end
end
# Implementation for 1 date
Base.axes(metadata::Metadatum) = 1
Base.first(metadata::Metadatum) = metadata
Base.last(metadata::Metadatum) = metadata
Base.iterate(metadata::Metadatum) = (metadata, nothing)
Base.iterate(::Metadatum, ::Any) = nothing
metadata_path(metadata::Metadatum) = joinpath(metadata.dir, metadata_filename(metadata))
metadata_path(metadata::Metadata) = [metadata_path(metadatum) for metadatum in metadata]
"""
native_times(metadata; start_time=first(metadata).dates)
Extract the time values from the given `metadata`, calculate the time difference
from the `start_time`, and return an array of time differences in seconds.
Argument
========
- `metadata`: The metadata containing the date information.
Keyword Argument
================
- `start_time`: The start time for calculating the time difference. Defaults to the first
date in the metadata.
"""
function native_times(metadata; start_time=first(metadata).dates)
times = zeros(length(metadata))
for (t, data) in enumerate(metadata)
date = data.dates
delta = date - start_time
delta = Second(delta).value
times[t] = delta
end
return times
end
####
#### Metadata interface
####
"""
default_download_directory(dataset)
Return the default directory to which `dataset` is downloaded.
"""
function default_download_directory end
"""
dataset_variable_name(metadata)
Return the name used for the variable `metadata.name` in its raw dataset file.
"""
function dataset_variable_name end
# Note: all_dates needs to be extended for any new dataset.
"""
all_dates(metadata)
Extract all the dates of the given `metadata` formatted using the `DateTime` type.
"""
all_dates(metadata) = all_dates(metadata.dataset, metadata.name)
"""
first_date(dataset, variable_name)
Extracts the first date of the given `dataset` and variable name formatted using the `DateTime` type.
"""
first_date(dataset, variable_name) = first(all_dates(dataset, variable_name))
"""
last_date(dataset, variable_name)
Extracts the last date of the given dataset and variable name formatted using the `DateTime` type.
"""
last_date(dataset, variable_name) = last(all_dates(dataset, variable_name))
"""
metadata_filename(metadata)
File names of metadata containing multiple dates. The specific version for a `Metadatum` object is
extended in the data specific modules.
"""
metadata_filename(metadata) = [metadata_filename(metadatum) for metadatum in metadata]
"""
available_variables(metadata)
Return the available variables in the dataset.
"""
available_variables(metadata) = available_variables(metadata.dataset)
struct Celsius end
struct Kelvin end
temperature_units(metadata) = Celsius()
struct MolePerKilogram end
struct MolePerLiter end
struct MillimolePerKilogram end
struct MillimolePerLiter end
struct MicromolePerKilogram end
struct MicromolePerLiter end
struct NanomolePerKilogram end
struct NanomolePerLiter end
struct GramPerKilogramMinus35 end # Salinity anomaly
struct MilliliterPerLiter end # Sometimes for disssolved_oxygen
concentration_units(metadata) = nothing
#####
##### Utilities
#####
"""
compute_native_date_range(native_dates, start_date, end_date)
Compute the range of `native_dates` that fall within the specified `start_date` and `end_date`.
"""
function compute_native_date_range(native_dates, start_date, end_date)
if last(native_dates) < end_date
@warn "`end_date` ($end_date) is after the last date in the dataset $(last(native_dates))"
end
if last(native_dates) < start_date
throw(ArgumentError("`start_date` ($start_date) is after the last date in the dataset $(last(native_dates))"))
end
start_idx = findfirst(x -> x ≥ start_date, native_dates)
end_idx = findfirst(x -> x ≥ end_date, native_dates)
start_idx = (start_idx > 1 && native_dates[start_idx] > start_date) ? start_idx - 1 : start_idx
end_idx = isnothing(end_idx) ? length(native_dates) : end_idx
return native_dates[start_idx:end_idx]
end