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

MethodError: reducing over an empty collection is not allowed; consider supplying init to the reducer #325

Closed
ryofurue opened this issue Oct 23, 2022 · 3 comments

Comments

@ryofurue
Copy link

Trying to create a Raster from a certain netCDF file results in the error shown in the title of this post. The file can be opened by the NCDatasets and plotted. I don't know how to provide the file. It's very small; only 5.2 MB. I use Julia 1.8.2 through juliaup.

using Rasters
using NCDatasets
using Plots

const inf = "coads_climatology.cdf"
const varname = "SST"

isfile(inf) && println("exists.")

Dataset(inf,"r") do ds
  sst = ds[varname]
  display(plot(sst[90,:,1]))
end

temp = Raster(inf; name=varname) # -> Error.
@rafaqz
Copy link
Owner

rafaqz commented Oct 23, 2022

It's best to post the complete error, that's usually the most informative part of a github issue! I can probably work out some of the problem from that.

The file may be needed too if it's not an obvious problem. You can put it in google drive, share publicly and link it here, or any similar services.

@ryofurue
Copy link
Author

Thank you for your help! Here is the netCDF file:
https://drive.google.com/file/d/1L8WHzDeDNzv_Gjwfkcrhqb1PsKIdXAkM/

Sorry for not providing the complete error. Here it is:

julia> include("try.jl")
exists.
ERROR: LoadError: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer
Stacktrace:
  [1] mapreduce_empty(#unused#::typeof(identity), op::Function, T::Type)
    @ Base ./reduce.jl:367
  [2] reduce_empty(op::Base.MappingRF{typeof(identity), typeof(promote_type)}, #unused#::Type{DataType})
    @ Base ./reduce.jl:356
  [3] reduce_empty_iter
    @ ./reduce.jl:379 [inlined]
  [4] mapreduce_empty_iter(f::Function, op::Function, itr::Vector{DataType}, ItrEltype::Base.HasEltype)
    @ Base ./reduce.jl:375
  [5] _mapreduce(f::typeof(identity), op::typeof(promote_type), #unused#::IndexLinear, A::Vector{DataType})
    @ Base ./reduce.jl:427
  [6] _mapreduce_dim
    @ ./reducedim.jl:365 [inlined]
  [7] #mapreduce#765
    @ ./reducedim.jl:357 [inlined]
  [8] mapreduce
    @ ./reducedim.jl:357 [inlined]
  [9] #reduce#767
    @ ./reducedim.jl:406 [inlined]
 [10] reduce
    @ ./reducedim.jl:406 [inlined]
 [11] pixeltype(ds::ArchGDAL.Dataset)
    @ ArchGDAL ~/.julia/packages/ArchGDAL/1I2FZ/src/dataset.jl:852
 [12] ArchGDAL.RasterDataset(ds::ArchGDAL.Dataset)
    @ ArchGDAL ~/.julia/packages/ArchGDAL/1I2FZ/src/raster/array.jl:32
 [13] #unsafe_readraster#108
    @ ~/.julia/packages/ArchGDAL/1I2FZ/src/raster/array.jl:126 [inlined]
 [14] unsafe_readraster
    @ ~/.julia/packages/ArchGDAL/1I2FZ/src/raster/array.jl:126 [inlined]
 [15] readraster(f::ComposedFunction{typeof(Rasters.cleanreturn), Rasters.var"#51#52"{Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, String}}, args::String; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ ArchGDAL ~/.julia/packages/ArchGDAL/1I2FZ/src/context.jl:265
 [16] readraster(f::Function, args::String)
    @ ArchGDAL ~/.julia/packages/ArchGDAL/1I2FZ/src/context.jl:264
 [17] _open(f::Function, ::Type{Rasters.GDALfile}, filename::String; write::Bool, kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Rasters ~/.julia/packages/Rasters/fANS1/src/sources/gdal.jl:306
 [18] _open(f::Function, ::Type{Rasters.GDALfile}, filename::String)
    @ Rasters ~/.julia/packages/Rasters/fANS1/src/sources/gdal.jl:292
 [19] _open(f::Function, filename::String; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Rasters ~/.julia/packages/Rasters/fANS1/src/convenience.jl:20
 [20] _open
    @ ~/.julia/packages/Rasters/fANS1/src/convenience.jl:19 [inlined]
 [21] #Raster#50
    @ ~/.julia/packages/Rasters/fANS1/src/array.jl:259 [inlined]
 [22] top-level scope
    @ ~/tmp/try.jl:15
 [23] include(fname::String)
    @ Base.MainInclude ./client.jl:476
 [24] top-level scope
    @ REPL[1]:1
in expression starting at /Users/furue/tmp/try.jl:15

@rafaqz
Copy link
Owner

rafaqz commented Nov 3, 2022

(Sorry I thought I commented on this the other day but mustn't have actually sent it)

But this is an easy one: you are using a netcdf file with an unusual extention .cdf Normally the extension is .nc.

If you change it to that it should work - there is no fancy inspection of the file here, the extension is all we check.

It's also hard to catch these errors because we use GDAL as a backup for any files we don't know, because it supports so many and will still load them if it has the wrong extension.

@rafaqz rafaqz closed this as completed Apr 16, 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