@@ -249,7 +249,7 @@ function Base.getindex(x::Dataset; var = nothing, kwargs...)
249249 Dataset (; properties= x. properties, map (ds -> ds => subsetifdimexists (cc[ds]; kwargs... ), collect (keys (cc)))... )
250250 end
251251end
252- function collectdims (g)
252+ function collectdims (g; force_datetime = true )
253253 dlist = Set {Tuple{String,Int,Int}} ()
254254 varnames = get_varnames (g)
255255 foreach (varnames) do k
@@ -267,7 +267,7 @@ function collectdims(g)
267267 end
268268 end
269269 end
270- outd = Dict (d[1 ] => (ax = toaxis (d[1 ], g, d[2 ], d[3 ]), offs = d[2 ]) for d in dlist)
270+ outd = Dict (d[1 ] => (ax= toaxis (d[1 ], g, d[2 ], d[3 ]; force_datetime ), offs= d[2 ]) for d in dlist)
271271 length (outd) == length (dlist) ||
272272 throw (ArgumentError (" All Arrays must have the same offset" ))
273273 outd
@@ -279,7 +279,7 @@ function round_datetime(dt)
279279 return DateTime (CFTime. UTInstant {Dates.Millisecond} (ms))
280280end
281281
282- function toaxis (dimname, g, offs, len;prefer_datetime = true )
282+ function toaxis (dimname, g, offs, len; force_datetime = true )
283283 axname = Symbol (dimname)
284284 if ! haskey (g, dimname)
285285 return DD. rebuild (DD. name2dim (axname), 1 : len)
@@ -288,14 +288,18 @@ function toaxis(dimname, g, offs, len;prefer_datetime=true)
288288 aratts = get_var_attrs (g, dimname)
289289 if match (r" ^(days)|(hours)|(seconds)|(months) since" ,lowercase (get (aratts," units" ," " ))) != = nothing
290290 tsteps = try
291- dec = timedecode (ar[:], aratts[" units" ], lowercase (get (aratts, " calendar" , " standard" )), prefer_datetime= false )
292- if prefer_datetime
293- round_datetime .(dec)
291+ timedecode (ar[:], aratts[" units" ], lowercase (get (aratts, " calendar" , " standard" )))
292+ catch e
293+ if e isa InexactError
294+ dec = timedecode (ar[:], aratts[" units" ], lowercase (get (aratts, " calendar" , " standard" )), prefer_datetime= false )
295+ if force_datetime
296+ round_datetime .(dec)
297+ else
298+ dec
299+ end
294300 else
295- dec
301+ ar[:]
296302 end
297- catch
298- ar[:]
299303 end
300304 DD. rebuild (DD. name2dim (axname), tsteps[offs+ 1 : end ])
301305 elseif haskey (aratts, " _ARRAYVALUES" )
@@ -439,19 +443,20 @@ The default driver will search for available drivers and tries to detect the use
439443
440444- `skip_keys` are passed as symbols, i.e., `skip_keys = (:a, :b)`
441445- `driver=:all`, common options are `:netcdf` or `:zarr`.
446+ - `force_datetime=false` force conversion when CFTime fails with an InexactError even if milliseconds must be rounded
442447
443448Example:
444449
445450````julia
446451ds = open_dataset(f, driver=:zarr, skip_keys = (:c,))
447452````
448453"""
449- function open_dataset (g; skip_keys= (), driver = :all )
454+ function open_dataset (g; skip_keys= (), driver= :all , force_datetime = false )
450455 str_skipkeys = string .(skip_keys)
451456 dsopen = YAXArrayBase. to_dataset (g, driver = driver)
452457 YAXArrayBase. open_dataset_handle (dsopen) do g
453458 isempty (get_varnames (g)) && throw (ArgumentError (" Group does not contain datasets." ))
454- dimlist = collectdims (g)
459+ dimlist = collectdims (g; force_datetime )
455460 dnames = string .(keys (dimlist))
456461 varlist = filter (get_varnames (g)) do vn
457462 upname = uppercase (vn)
0 commit comments