Skip to content

Commit 522dffc

Browse files
committed
Add add_date_maybe!
1 parent 9c11971 commit 522dffc

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

src/netcdf_writer.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,13 @@ function write_field!(writer::NetCDFWriter, field, diagnostic, u, p, t)
373373
else
374374
start_date = writer.start_date
375375
end
376+
if !isnothing(start_date)
377+
add_date_maybe!(
378+
nc;
379+
units = "seconds since $start_date",
380+
bounds = "date_bnds",
381+
)
382+
end
376383

377384
if haskey(nc, "$(var.short_name)")
378385
# We already have something in the file
@@ -409,8 +416,9 @@ function write_field!(writer::NetCDFWriter, field, diagnostic, u, p, t)
409416
# FIXME: We are rounding t
410417
if !isnothing(start_date)
411418
# TODO: Use ITime here
412-
nc["date"][time_index] =
413-
string(start_date + Dates.Millisecond(round(1000 * float(t))))
419+
curr_date = start_date + Dates.Millisecond(round(1000 * float(t)))
420+
date_type = typeof(curr_date) # not necessarily a Dates.DateTime
421+
nc["date"][time_index] = curr_date
414422
end
415423

416424
# TODO: It would be nice to find a cleaner way to do this

src/netcdf_writer_coordinates.jl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ end
9898
9999
Add the `time` dimension (with infinite size) to the given NetCDF file if not already there.
100100
Optionally, add all the keyword arguments as attributes.
101-
102-
Also add a `date` dataset (as a string).
103101
"""
104102
function add_time_maybe!(
105103
nc::NCDatasets.NCDataset,
@@ -112,7 +110,28 @@ function add_time_maybe!(
112110

113111
NCDatasets.defDim(nc, "time", Inf)
114112
dim = NCDatasets.defVar(nc, "time", FT, ("time",))
115-
NCDatasets.defVar(nc, "date", String, ("time",))
113+
for (k, v) in kwargs
114+
dim.attrib[String(k)] = v
115+
end
116+
return nothing
117+
end
118+
119+
"""
120+
add_date_maybe!(nc::NCDatasets.NCDataset,
121+
kwargs...)
122+
123+
Add the `date` dimension (with infinite size) to the given NetCDF file if not already there.
124+
Optionally, add all the keyword arguments as attributes.
125+
"""
126+
function add_date_maybe!(nc::NCDatasets.NCDataset; kwargs...)
127+
haskey(nc, "date") && return nothing
128+
!haskey(nc, "time") &&
129+
error("`add_date_maybe!` is meant to be called after `add_time_maybe!`")
130+
dim = NCDatasets.defVar(nc, "date", Float64, ("time",))
131+
for (k, v) in kwargs
132+
dim.attrib[String(k)] = v
133+
end
134+
end
116135
for (k, v) in kwargs
117136
dim.attrib[String(k)] = v
118137
end

test/writers.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ end
9797
string(Dates.DateTime(1453, 5, 29))
9898
@test size(nc["ABC"]) == (1, NUM, 2NUM, 3NUM)
9999
@test nc["time"][1] == 10.0
100-
@test nc["date"][1] ==
101-
string(Dates.DateTime(1453, 5, 29) + Dates.Second(10.0))
100+
@test nc["date"][1] == Dates.DateTime(1453, 5, 29) + Dates.Second(10.0)
102101
@test nc["time"].attrib["standard_name"] == "time"
103102
@test nc["time"].attrib["long_name"] == "Time"
104103
@test nc["time"].attrib["axis"] == "T"

0 commit comments

Comments
 (0)