Skip to content

Commit

Permalink
fix detecting first valid variable in netcdf (#346)
Browse files Browse the repository at this point in the history
* fix detecting first valid variable in netcdf
  • Loading branch information
rafaqz authored Dec 8, 2022
1 parent faab23d commit e382d69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/sources/ncdatasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ defaultmappedcrs(::Type{NCDfile}) = EPSG(4326)
# Raster ########################################################################

function Raster(ds::NCD.NCDataset, filename::AbstractString, key=nothing; kw...)
key = _firstkey(ds, key)
Raster(ds[key], filename, key; kw...)
if isnothing(key)
# Find the first valid variable
for key in layerkeys(ds)
if ndims(NCD.variable(ds, key)) > 0
@info "No `key` keyword provided, using first valid key `:$key`"
return Raster(ds[key], filename, key; kw...)
end
end
throw(ArgumentError("dataset at $filename has no array variables"))
else
return Raster(ds[key], filename, key; kw...)
end
end

_firstkey(ds::NCD.NCDataset, key::Nothing=nothing) = Symbol(first(layerkeys(ds)))
Expand Down Expand Up @@ -101,24 +111,18 @@ Keywords are passed to `NCDatasets.defVar`.
- `append`: If true, the variable of the current Raster will be appended to
`filename`. Note that the variable of the current Raster should be not exist
before. If not, you need to set `append = false`. `Rasters` can not
before. If not, you need to set `append = false`. Rasters.jl can not
overwrite a previous existing variable.
- `fillvalue`: A value filled in the NetCDF file to indicate missing data. It
will be stored in the `_FillValue` attribute.
- `chunksizes`: Vector integers setting the chunk size. The total size of a
chunk must be less than 4 GiB.
- `deflatelevel`: Compression level: 0 (default) means no compression and 9
means maximum compression. Each chunk will be compressed individually.
- `shuffle`: If true, the shuffle filter is activated which can improve the
compression ratio.
- `checksum`: The checksum method can be `:fletcher32` or `:nochecksum`
(checksumming is disabled, which is the default)
- `typename` (string): The name of the NetCDF type required for vlen arrays
(https://web.archive.org/save/https://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-c/nc_005fdef_005fvlen.html)
"""
Expand Down Expand Up @@ -240,6 +244,8 @@ _open(f, ::Type{NCDfile}, var::NCD.CFVariable; kw...) = cleanreturn(f(var))

cleanreturn(A::NCD.CFVariable) = Array(A)

# Utils ########################################################################

function _ncddim(ds, dimname::Key, crs=nothing, mappedcrs=nothing)
if haskey(ds, dimname)
var = NCD.variable(ds, dimname)
Expand Down
8 changes: 8 additions & 0 deletions test/sources/ncdatasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ stackkeys = (
@test all(A .=== A3)
end

@testset "ignore empty variables" begin
st = RasterStack((empty=view(ncarray, 1, 1, 1), full=ncarray))
write("emptyval_test.nc", st)
rast = Raster("emptyval_test.nc")
@test name(rast) == :full
rm("emptyval_test.nc")
end

@testset "array properties" begin
@test size(ncarray) == (180, 170, 24)
@test ncarray isa Raster
Expand Down

0 comments on commit e382d69

Please sign in to comment.