Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about time coordinate reading in .nc file with time unit nanoseconds from start Date Time #248

Open
willcoxe opened this issue Feb 23, 2024 · 2 comments

Comments

@willcoxe
Copy link

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

@Alexander-Barth
Copy link
Owner

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

@willcoxe
Copy link
Author

willcoxe commented Mar 8, 2024

Apologies for not replying earlier. I have been using the DateTime package with the Nanoseconds conversion successfully. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants