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

Automatically save Dates #125

Closed
willtebbutt opened this issue Mar 22, 2021 · 3 comments
Closed

Automatically save Dates #125

willtebbutt opened this issue Mar 22, 2021 · 3 comments

Comments

@willtebbutt
Copy link

willtebbutt commented Mar 22, 2021

It's doesn't presently appear possible to automatically save Date variables, as opposed to DateTime variables, but it's of course entirely possible that I'm mis-using the package. Specifically:

ds = Dataset("foo.nc", "c");

defDim(ds, "date", 250);

t = defVar(
    ds, "date", Int64, ("date", );
    attrib = Dict(
        "units" => "days since 1900-01-01 00:00:00.0",
        "long_name" => "date",
        "calendar" => "gregorian",
    ),
);

dates = range(Date(1905); step=Day(1), length=250);
t[:] = dates

yields

ERROR: MethodError: no method matching -(::Date, ::DateTimeStandard)
Closest candidates are:
  -(::Date, ::Day) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Dates/src/arithmetic.jl:75
  -(::Date, ::Week) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Dates/src/arithmetic.jl:73
  -(::Date, ::Month) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Dates/src/arithmetic.jl:66
  ...
Stacktrace:
 [1] fromdate at /homes/wct23/.julia/packages/NCDatasets/eDnxY/src/cfvariable.jl:497 [inlined]
 [2] CFinvtransform at /homes/wct23/.julia/packages/NCDatasets/eDnxY/src/cfvariable.jl:517 [inlined]
 [3] macro expansion at /homes/wct23/.julia/packages/NCDatasets/eDnxY/src/cfvariable.jl:571 [inlined]
 [4] macro expansion at ./simdloop.jl:77 [inlined]
 [5] CFinvtransformdata at /homes/wct23/.julia/packages/NCDatasets/eDnxY/src/cfvariable.jl:570 [inlined]
 [6] setindex!(::NCDatasets.CFVariable{DateTime,1,NCDatasets.Variable{Int64,1,NCDataset},NCDatasets.Attributes{NCDataset{Nothing}},NamedTuple{(:fillvalue, :scale_factor, :add_offset, :calendar, :time_origin, :time_factor),Tuple{Nothing,Nothing,Nothing,String,DateTimeStandard,Int64}}}, ::StepRange{Date,Day}, ::Colon) at /homes/wct23/.julia/packages/NCDatasets/eDnxY/src/cfvariable.jl:624
 [7] top-level scope at REPL[120]:1

This workaround is fine for now:

t[:] = DateTime.(dates)

but perhaps it would be desirable to make it possible to do this conversion automatically? Or, if this functionality is intentional, so as to ensure that you get the same data types when loading a file back up, perhaps this workaround could be documented via some kind of error?

@willtebbutt
Copy link
Author

I should add, I love the fact that this package automatically handles saving DateTimes -- it's really fantastic that that bit just works.

@visr
Copy link
Contributor

visr commented Mar 22, 2021

See also #110, this can probably be closed in favor of that issue.

@willtebbutt
Copy link
Author

Yup, can be closed in favour of that one.

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