Alexander-Barth/NCDatasets.jl

@select not work for 4d array

Closed this issue · 4 comments

kongdd commented
using NCDatasets


f = "http://esgf3.dkrz.de/thredds/dodsC/cmip6/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r1i1p1f1/day/zg/gr/v20200310/zg_day_EC-Earth3_historical_r1i1p1f1_gr_20130101-20131231.nc"
plevs = [250, 500, 850] .* 100

in_plev(plev, plevs) = indexin(round.(Int, plev), plevs) .!== nothing


nc = NCDataset(f)
var = nc["zg"]
v = @select(var, in_plev(plev, $plevs))

v.var[:, :, 1:2, 1] # error
v.var[:, :, :, 1] # error
v.var[:] # error
julia> v.var[:, :, 1:2, 1] # error   
ERROR: type Variable has no field var
Stacktrace:
 [1] getproperty
   @ .\Base.jl:37 [inlined]
 [2] getindex(::NCDatasets.Variable{Float32, 4, NCDataset{Nothing}}, ::Colon, ::Colon, ::Vector{Int64}, ::Vararg{Union{Colon, Int64, AbstractRange{<:Integer}, Vector{Int64}}})
   @ NCDatasets C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\cfvariable.jl:432
 [3] materialize
   @ C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\subvariable.jl:44 [inlined]
 [4] getindex(::NCDatasets.SubVariable{Float32, 4, NCDatasets.Variable{Float32, 4, NCDataset{Nothing}}, Tuple{Colon, Colon, Vector{Int64}, Colon}, NCDatasets.Attributes{NCDataset{Nothing}}, Nothing}, ::Colon, ::Colon, ::UnitRange{Int64}, ::Vararg{Union{Colon, Int64, AbstractRange{<:Integer}}})
   @ NCDatasets C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\subvariable.jl:98
 [5] top-level scope
   @ z:\Researches\CMIP6\nctools.jl\debug.qmd:1

julia> v.var[:, :, :, 1] # error     
ERROR: type Variable has no field var
Stacktrace:
 [1] getproperty
   @ .\Base.jl:37 [inlined]
 [2] getindex(::NCDatasets.Variable{Float32, 4, NCDataset{Nothing}}, ::Colon, ::Colon, ::Vector{Int64}, ::Vararg{Union{Colon, Int64, AbstractRange{<:Integer}, Vector{Int64}}})
   @ NCDatasets C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\cfvariable.jl:432
 [3] materialize
   @ C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\subvariable.jl:44 [inlined]
 [4] getindex(::NCDatasets.SubVariable{Float32, 4, NCDatasets.Variable{Float32, 4, NCDataset{Nothing}}, Tuple{Colon, Colon, Vector{Int64}, Colon}, NCDatasets.Attributes{NCDataset{Nothing}}, Nothing}, ::Colon, ::Colon, ::Colon, ::Vararg{Union{Colon, Int64, AbstractRange{<:Integer}}})
   @ NCDatasets C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\subvariable.jl:98
 [5] top-level scope
   @ z:\Researches\CMIP6\nctools.jl\debug.qmd:1

julia> v.var[:] # error
ERROR: BoundsError: attempt to access Tuple{Colon} at index [2]
Stacktrace:
 [1] getindex
   @ .\tuple.jl:29 [inlined]
 [2] _subsub(::Tuple{Colon}, ::Tuple{Colon}, ::Int64, ::Colon, ::Vector{Int64}, ::Vararg{Any})
   @ NCDatasets C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\subvariable.jl:33
 [3] _subsub(::Tuple{}, ::Tuple{Colon}, ::Int64, ::Colon, ::Function, ::Vararg{Any})
   @ NCDatasets C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\subvariable.jl:33
 [4] subsub(parentindices::Tuple{Colon, Colon, Vector{Int64}, Colon}, indices::Tuple{Colon})
   @ NCDatasets C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\subvariable.jl:42
 [5] view(v::NCDatasets.SubVariable{Float32, 4, NCDatasets.Variable{Float32, 4, NCDataset{Nothing}}, Tuple{Colon, Colon, Vector{Int64}, Colon}, NCDatasets.Attributes{NCDataset{Nothing}}, Nothing}, indices::Colon)    
   @ NCDatasets C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\subvariable.jl:64
 [6] getindex(v::NCDatasets.SubVariable{Float32, 4, NCDatasets.Variable{Float32, 4, NCDataset{Nothing}}, Tuple{Colon, Colon, Vector{Int64}, Colon}, NCDatasets.Attributes{NCDataset{Nothing}}, Nothing}, indices::Colon)   @ NCDatasets C:\Users\hydro\.julia\packages\NCDatasets\st9Jz\src\subvariable.jl:98
 [7] top-level scope
   @ z:\Researches\CMIP6\nctools.jl\debug.qmd:1
kongdd commented

It works.

if you need un-scaled data you can now use (7a3c9c5):

using NCDatasets
f = "http://esgf3.dkrz.de/thredds/dodsC/cmip6/CMIP/EC-Earth-Consortium/EC-Earth3/historical/r1i1p1f1/day/zg/gr/v20200310/zg_day_EC-Earth3_historical_r1i1p1f1_gr_20130101-20131231.nc"
plevs = [250, 500, 850] .* 100

# plevs*100 -> plevs : an error in your example?
in_plev(plev, plevs) = indexin(round.(Int, plev), plevs) .!== nothing


nc = NCDataset(f)
v = NCDatasets.@select(nc["zg"].var, in_plev(plev, $plevs))
v[:,:,1:2,1] # works

If the error persists, please file a new issue and provide all information in the issue template.
Thanks!

kongdd commented

Got it. Thank you.