NCDatasets.jl
NCDatasets.jl copied to clipboard
Question about time coordinate reading in .nc file with time unit nanoseconds from start Date Time
Hi! Thanks for making the package!
I have several NetCDF files that I am trying to read in and I am running into an issue with the time coordinate. This may be related to CFTime issue 18 because the file I am reading in has "nanoseconds since" as the time unit. However the error I get is not the same as the one I get from CFTime.
Having nanosecond precision is not super important to me at the moment, but I am not sure how to circumvent this to be able to run ds["time"][:]
without error. Apologies if this is somewhere in the docs and I simply have not found it. I appreciate any insight/advice you might have.
fn = "file.nc"
ds = NCDataset(fn, "r")
julia> ds["time"]
time (2588)
Datatype: Int64
Dimensions: time
Attributes:
units = nanoseconds since 2022-08-16 20:02:30.000998258
calendar = proleptic_gregorian
julia> ds["time"].var[:]
2588-element Vector{Int64}:
0
5400000001669
⋮
13926600000002623
13932000000000715
julia> ds["U_mag"][:]
2588×39 Matrix{Union{Missing, Float32}}:
⋮ ⋱
julia> ds["time"][:]
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type DateTime
Closest candidates are:
convert(::Type{DateTime}, ::Millisecond)
@ Dates /usr/share/julia/stdlib/v1.10/Dates/src/conversions.jl:34
convert(::Type{DateTime}, ::Date)
@ Dates /usr/share/julia/stdlib/v1.10/Dates/src/conversions.jl:30
convert(::Type{DateTime}, ::T2) where T2<:Union{DateTimeJulian, DateTimeProlepticGregorian, DateTimeStandard}
@ CFTime ~/.julia/packages/CFTime/n09Um/src/CFTime.jl:308
...
Stacktrace:
[1] setindex!(A::Vector{DateTime}, x::Int64, i1::Int64)
@ Base ./array.jl:1021
[2] macro expansion
@ ~/.julia/packages/NCDatasets/c8XyT/src/cfvariable.jl:538 [inlined]
[3] macro expansion
@ ./simdloop.jl:77 [inlined]
[4] CFtransformdata
@ ~/.julia/packages/NCDatasets/c8XyT/src/cfvariable.jl:537 [inlined]
[5] getindex(v::NCDatasets.CFVariable{…}, indexes::Colon)
@ NCDatasets ~/.julia/packages/NCDatasets/c8XyT/src/cfvariable.jl:604
[6] top-level scope
@ REPL[164]:1
Some type information was truncated. Use `show(err)` to see complete types.
julia> CFTime.timedecode(ds["time"].var[:],"nanoseconds since 2022-08-16 20:02:30.000998258")
ERROR: unknown units nanoseconds
OS: Arch linux julia version 1.10.1
Here is an example. Does it work for you?
t0 = DateTime(2022,08,16,20,02,30,001) # or parse from ds["time"].attrib["units"]
t = t0 .+ Nanosecond.(ds["time"].var[:])
In order to parse the time origin, you might want to have a look here:
https://github.com/JuliaGeo/CFTime.jl/blob/382cfd5918c8c47b2a9c8306cf73b41a405f1735/src/CFTime.jl#L361
Apologies for not replying earlier. I have been using the DateTime package with the Nanoseconds conversion successfully. Thanks!