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

KeyError: key Date not found #110

Open
Datseris opened this issue Nov 13, 2020 · 7 comments
Open

KeyError: key Date not found #110

Datseris opened this issue Nov 13, 2020 · 7 comments

Comments

@Datseris
Copy link
Contributor

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.

@visr
Copy link
Contributor

visr commented Nov 13, 2020

Are you looking for this?

# Mapping between NetCDF types and Julia types
const jlType = Dict(
NC_BYTE => Int8,
NC_UBYTE => UInt8,
NC_SHORT => Int16,
NC_USHORT => UInt16,
NC_INT => Int32,
NC_UINT => UInt32,
NC_INT64 => Int64,
NC_UINT64 => UInt64,
NC_FLOAT => Float32,
NC_DOUBLE => Float64,
NC_CHAR => Char,
NC_STRING => String
)
# Inverse mapping
const ncType = Dict(value => key for (key, value) in jlType)

@Datseris
Copy link
Contributor Author

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...

@visr
Copy link
Contributor

visr commented Nov 13, 2020

Ah perhaps this needs to include Date?

# data has the type e.g. Vector{DateTime}, Array{Union{Missing,DateTime},3} or
# Vector{DateTime360Day}
# Data is always stored as Float64 in the NetCDF file
function defVar(ds::NCDataset,
name,
data::AbstractArray{<:Union{Missing,nctype},N},
dimnames;
kwargs...) where nctype <: Union{DateTime,AbstractCFDateTime} where N
_defVar(ds::NCDataset,name,data,Float64,dimnames; kwargs...)
end

Probably with a new test case in test_timeunits.jl.

@Datseris
Copy link
Contributor Author

YeaP, this seems like it!

@visr
Copy link
Contributor

visr commented Nov 13, 2020

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.

@Datseris
Copy link
Contributor Author

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

@Alexander-Barth
Copy link
Owner

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

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

No branches or pull requests

3 participants