NCDatasets.jl icon indicating copy to clipboard operation
NCDatasets.jl copied to clipboard

KeyError: key Date not found

Open Datseris opened this issue 5 years ago • 7 comments

Describe the bug Trying to write a dimension with values Date("2000-01-01"):Month(1):Date("2019-12-01") to an .nc file I get

 KeyError: key Date not found

which happens in accessing ncType[vtype] in the definition of defVar.

The fix is to add an entry to this "dictionary ncType that allows Date and it handles it the same as DateTime.

If I transform my data into DateTime it works.

I can't even find where in the source ncType is defined though... Where is it defined? this seems like a simple fix.

Datseris avatar Nov 13 '20 13:11 Datseris

Are you looking for this? https://github.com/Alexander-Barth/NCDatasets.jl/blob/b8751a9320902200edec9b0630b36db0ad600469/src/dataset.jl#L103-L120

visr avatar Nov 13 '20 13:11 visr

I guess so, but now it is not so clear to me how to modify it so that it can handle Date, because I don't see DateTime in it...

Datseris avatar Nov 13 '20 14:11 Datseris

Ah perhaps this needs to include Date? https://github.com/Alexander-Barth/NCDatasets.jl/blob/947db892d59bd6e32e089e2a6360f701d09bb81a/src/cfvariable.jl#L174-L183

Probably with a new test case in test_timeunits.jl.

visr avatar Nov 13 '20 14:11 visr

YeaP, this seems like it!

Datseris avatar Nov 13 '20 14:11 Datseris

I guess for full Date support CFTime.jl would need to support Date as well as DateTime. Though there may be a reason this is not supported, as time in NetCDF is more of a continuous variable, and fit better with DateTime. Unless we decide that Date represents 00:00.

visr avatar Nov 13 '20 14:11 visr

For me it seems reasonable and straight-forward that assuming 00:00 should happen, and Base Julia does the same:

julia> using Dates

julia> x = Date("2000-01-01"):Month(1):Date("2019-12-01");

julia> DateTime.(x)
240-element Array{DateTime,1}:
 2000-01-01T00:00:00
 2000-02-01T00:00:00
 2000-03-01T00:00:00
 2000-04-01T00:00:00
 2000-05-01T00:00:00
 2000-06-01T00:00:00
 2000-07-01T00:00:00
 2000-08-01T00:00:00
 2000-09-01T00:00:00

Datseris avatar Nov 13 '20 14:11 Datseris

I agree that this would be a nice feature. I do not need this myself, but I would be happy to review a PR.

Alexander-Barth avatar Nov 17 '20 10:11 Alexander-Barth