JuliaGeo / NetCDF.jl

NetCDF support for the julia programming language
http://juliageo.org/NetCDF.jl/
MIT License
116 stars 28 forks source link

Trying to save a Float16 array to netcdf gives a bad error message #174

Open felixcremer opened 1 year ago

felixcremer commented 1 year ago

When I try to save a Float16 array into a NetCDF I got the following confusing error message. Is it not possible to save Float16 data in NetCDF?

Could we implement the look up of the NC types using dispatch? So that the default dispatch would be a meaningful error message like: NetCDF does not support the data type $type

julia> savecube(rqa, "data/E048N018T3_rqatrend_VH_D066__thresh_4_8_8_2023-03-22T15:16:14.120.nc", backend=:netcdf)
ERROR: ArgumentError: array must be non-empty
Stacktrace:
  [1] popfirst!
    @ ./array.jl:1429 [inlined]
  [2] jl2nc(t::DataType)
    @ NetCDF ~/.julia/packages/NetCDF/7hOe9/src/NetCDF.jl:92
  [3] getNCType(t::DataType)
    @ NetCDF ~/.julia/packages/NetCDF/7hOe9/src/NetCDF.jl:102
  [4] NcVar(name::String, dimin::Vector{NcDim}; atts::Dict{String, Any}, t::DataType, compress::Int64, chunksize::Tuple{Int64, Int64})
    @ NetCDF ~/.julia/packages/NetCDF/7hOe9/src/NetCDF.jl:257
  [5] nccreate(::String, ::String, ::String, ::Vararg{Any}; atts::Dict{String, Any}, gatts::Dict{Any, Any}, compress::Int64, t::DataType, mode::UInt16, chunksize::Tuple{Int64, Int64})
    @ NetCDF ~/.julia/packages/NetCDF/7hOe9/src/NetCDF.jl:1211
  [6] add_var(p::YAXArrayBase.NetCDFDataset, T::Type, varname::String, s::Tuple{Int64, Int64}, dimnames::Vector{String}, attr::Dict{String, Any}; chunksize::Tuple{Int64, Int64}, compress::Int64)
    @ YAXArrayBase ~/.julia/packages/YAXArrayBase/R6Frw/src/datasets/netcdf.jl:63
  [7] create_dataset(T::Type, path::String, gatts::Dict{String, Any}, dimnames::Vector{String}, dimvals::Vector{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}}, dimattrs::Vector{Dict{String, Any}}, vartypes::Vector{DataType}, varnames::Vector{String}, vardims::Vector{Vector{String}}, varattrs::Vector{Dict{String, Any}}, varchunks::Vector{Tuple{Int64, Int64}}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ YAXArrayBase ~/.julia/packages/YAXArrayBase/R6Frw/src/datasets/datasetinterface.jl:62
  [8] create_dataset(T::Type, path::String, gatts::Dict{String, Any}, dimnames::Vector{String}, dimvals::Vector{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}}, dimattrs::Vector{Dict{String, Any}}, vartypes::Vector{DataType}, varnames::Vector{String}, vardims::Vector{Vector{String}}, varattrs::Vector{Dict{String, Any}}, varchunks::Vector{Tuple{Int64, Int64}})
    @ YAXArrayBase ~/.julia/packages/YAXArrayBase/R6Frw/src/datasets/datasetinterface.jl:53
  [9] savedataset(ds::Dataset; path::String, persist::Nothing, overwrite::Bool, append::Bool, skeleton::Bool, backend::Symbol, driver::Symbol, max_cache::Float64, writefac::Float64)
    @ YAXArrays.Datasets ~/.julia/packages/YAXArrays/Fe7F8/src/DatasetAPI/Datasets.jl:503
 [10] savecube(c::YAXArray{Union{Missing, Float16}, 2, DiskArrayTools.CFDiskArray{Float16, 2, Float16, ZArray{Float16, 2, Zarr.BloscCompressor, DirectoryStore}}, Vector{RangeAxis{Float64, S, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}} where S}}, path::String; name::String, datasetaxis::String, max_cache::Float64, backend::Symbol, driver::Symbol, chunks::Nothing, overwrite::Bool, append::Bool, skeleton::Bool, writefac::Float64)
    @ YAXArrays.Datasets ~/.julia/packages/YAXArrays/Fe7F8/src/DatasetAPI/Datasets.jl:566
 [11] top-level scope
    @ REPL[39]:1
visr commented 1 year ago

We can probably keep using the nctype2jltyp dict we have now internally, but whenever we use it we must account for the possibility that the key is not present, e.g. get(nctype2jltyp, T, nothing) and handle nothing.