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

Near for Time Axis with DateTime elements fails sometimes with Date value #495

Closed
felixcremer opened this issue Jul 6, 2023 · 4 comments

Comments

@felixcremer
Copy link
Contributor

When I try to subset a cube with a Ti axis with DateTime values this fails for some reason when I try to use a Date as input for Near.

using YAXArrays
using DimensionalData: DimensionalData as DD
using Zarr
g = open_dataset(zopen("https://s3.bgc-jena.mpg.de:9000/esdl-esdc-v3.0.2/esdc-8d-0.25deg-256x128x128-3.0.2.zarr"))
gpp = g.gross_primary_productivity

gcmip = open_dataset(zopen("gs://cmip6/CMIP6/ScenarioMIP/DKRZ/MPI-ESM1-2-HR/ssp585/r1i1p1f1/3hr/tas/gn/v20190710/", consolidated=true));

tas = gcmip.tas

tas[Ti=DD.Near(Date(2015))] # works
gpp[Ti=DD.Near(Date(2015))] # fails
gpp[Ti=DD.Near(DateTime(2015))] # works 

I can use a Date object in Between even though the axes elementtype is DateTime.

I don't understand where the difference between tas and gpp come from the types of the Time axes seem to be the same.
It would be good if I could match and mix the Date types for the subsetting, because I can't know in advance, what possible Time Type a data cube would have.

This is the error I got:

ERROR: MethodError: no method matching -(::Date, ::DateTime)

Closest candidates are:
  -(::StridedArray{<:Union{Dates.CompoundPeriod, Period}}, ::TimeType)
   @ Dates ~/.julia/juliaup/julia-1.9.0+0.x64.linux.gnu/share/julia/stdlib/v1.9/Dates/src/deprecated.jl:26
  -(::Union{CFTime.DateTimeJulian, CFTime.DateTimeProlepticGregorian, CFTime.DateTimeStandard}, ::DateTime)
   @ CFTime ~/.julia/packages/CFTime/n09Um/src/CFTime.jl:263
  -(::TimeType, ::Dates.CompoundPeriod)
   @ Dates ~/.julia/juliaup/julia-1.9.0+0.x64.linux.gnu/share/julia/stdlib/v1.9/Dates/src/periods.jl:364
  ...

Stacktrace:
  [1] near
    @ ~/.julia/packages/DimensionalData/pS9IE/src/LookupArrays/selector.jl:224 [inlined]
  [2] near
    @ ~/.julia/packages/DimensionalData/pS9IE/src/LookupArrays/selector.jl:200 [inlined]
  [3] selectindices
    @ ~/.julia/packages/DimensionalData/pS9IE/src/LookupArrays/selector.jl:193 [inlined]
  [4] _dims2indices
    @ ~/.julia/packages/DimensionalData/pS9IE/src/Dimensions/indexing.jl:114 [inlined]
  [5] macro expansion
    @ ~/.julia/packages/DimensionalData/pS9IE/src/Dimensions/indexing.jl:56 [inlined]
  [6] _dims2indices
    @ ~/.julia/packages/DimensionalData/pS9IE/src/Dimensions/indexing.jl:56 [inlined]
  [7] dims2indices
    @ ~/.julia/packages/DimensionalData/pS9IE/src/Dimensions/indexing.jl:51 [inlined]
  [8] dims2indices
    @ ~/.julia/packages/DimensionalData/pS9IE/src/Dimensions/indexing.jl:34 [inlined]
  [9] view(::YAXArray{Float32, 3, ZArray{Float32, 3, Zarr.BloscCompressor, Zarr.ConsolidatedStore{Zarr.HTTPStore}}, Tuple{Dim{:lon, DimensionalData.Dimensions.LookupArrays.Sampled{Float64, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Regular{Float64}, DimensionalData.Dimensions.LookupArrays.Points, DimensionalData.Dimensions.LookupArrays.NoMetadata}}, Dim{:lat, DimensionalData.Dimensions.LookupArrays.Sampled{Float64, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Regular{Float64}, DimensionalData.Dimensions.LookupArrays.Points, DimensionalData.Dimensions.LookupArrays.NoMetadata}}, Ti{DimensionalData.Dimensions.LookupArrays.Sampled{DateTime, Vector{DateTime}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Irregular{Tuple{Nothing, Nothing}}, DimensionalData.Dimensions.LookupArrays.Points, DimensionalData.Dimensions.LookupArrays.NoMetadata}}}}; kw::Base.Pairs{Symbol, Near{Date}, Tuple{Symbol}, NamedTuple{(:Ti,), Tuple{Near{Date}}}})
    @ DimensionalData ~/.julia/packages/DimensionalData/pS9IE/src/array/indexing.jl:49
 [10] view
    @ ~/.julia/packages/DimensionalData/pS9IE/src/array/indexing.jl:49 [inlined]
 [11] getindex(::YAXArray{Float32, 3, ZArray{Float32, 3, Zarr.BloscCompressor, Zarr.ConsolidatedStore{Zarr.HTTPStore}}, Tuple{Dim{:lon, DimensionalData.Dimensions.LookupArrays.Sampled{Float64, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Regular{Float64}, DimensionalData.Dimensions.LookupArrays.Points, DimensionalData.Dimensions.LookupArrays.NoMetadata}}, Dim{:lat, DimensionalData.Dimensions.LookupArrays.Sampled{Float64, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Regular{Float64}, DimensionalData.Dimensions.LookupArrays.Points, DimensionalData.Dimensions.LookupArrays.NoMetadata}}, Ti{DimensionalData.Dimensions.LookupArrays.Sampled{DateTime, Vector{DateTime}, DimensionalData.Dimensions.LookupArrays.ForwardOrdered, DimensionalData.Dimensions.LookupArrays.Irregular{Tuple{Nothing, Nothing}}, DimensionalData.Dimensions.LookupArrays.Points, DimensionalData.Dimensions.LookupArrays.NoMetadata}}}}; kwargs::Base.Pairs{Symbol, Near{Date}, Tuple{Symbol}, NamedTuple{(:Ti,), Tuple{Near{Date}}}})
    @ YAXArrays.Cubes ~/.julia/dev/YAXArrays/src/Cubes/Cubes.jl:479
 [12] top-level scope
    @ REPL[88]:1
@rafaqz
Copy link
Owner

rafaqz commented Jul 6, 2023

Its annoying that -(::Date, ::DateTime) is not defined in a Base to just convert to DateTime.

We could add a custom method to near that dies the conversion manually.

@felixcremer
Copy link
Contributor Author

felixcremer commented Jul 6, 2023

Do you have an idea why there is a difference between tas and gpp?
From a first look at their time axes, they seem to be the same.

Also the -(::Date, ::DateTime) method in Base would not be enough for us, because there are also the CFTime types and we would like to have the same subsetting experience independent of the eltype of the time axis.

@rafaqz
Copy link
Owner

rafaqz commented Jul 27, 2023

You should implement refdims so we can see what time slice the result comes from ;)

But I think the reason these are different is that tas lookup does not quite go back to the start of 2015, by 3 hours. So Near doesn't need to do a comparison between two dates, it just takes the next one available after 2015. gpp has dates back to 1979 so it has to compare the two on each side of 2015 to see which one is closest. That needs to use subtraction so we hit the error.

If you try 2016 they both fail.

Seems this is actually fixed in Julia base, I guess it will work if you use julia 1.10:
JuliaLang/julia#49700

@rafaqz
Copy link
Owner

rafaqz commented Jul 27, 2023

I think we can close this seeing its resolved in Base already

@rafaqz rafaqz closed this as completed Jul 27, 2023
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