Skip to content

Commit

Permalink
implement alternative to missing values
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Jan 10, 2024
1 parent 4b7ff28 commit 514559e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ makedocs(
"Dimensions" => "dimensions.md",
"Variables" => "variables.md",
"Attributes" => "attributes.md",
"Fill values" => "fillvalue.md",
"Performance tips" => "performance.md",
"Known issues" => "issues.md",
"Experimental features" => "experimental.md",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ To get started quickly see the [Quickstart](@ref) section. Otherwise see the fol
* [Dimensions](@ref) : accessing/creating NetCDF dimensions
* [Variables](@ref) : accessing/examining the variables (or dimensions) stored within a NetCDF dataset.
* [Attributes](@ref) : accessing/creating NetCDF attributes
* See [Performance tips](@ref performance_tips), [Known issues](@ref), [Experimental features](@ref) for more information.
* See [Fill values and missing values](@ref), [Performance tips](@ref performance_tips), [Known issues](@ref), [Experimental features](@ref) for more information.

## Quick start

Expand Down
3 changes: 2 additions & 1 deletion src/NCDatasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import CommonDataModel: AbstractDataset, AbstractVariable,
iswritable, sync, CatArrays,
SubDataset,
@select, select, Near, coordinate_value, coordinate_names, split_by_and,
chunking, deflate, checksum
chunking, deflate, checksum,
_experimental_missing_value


import DiskArrays
Expand Down
18 changes: 13 additions & 5 deletions src/dataset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const jlType = Dict(
const ncType = Dict(value => key for (key, value) in jlType)

iswritable(ds::NCDataset) = ds.iswritable
_experimental_missing_value(ds::NCDataset) = ds._experimental_missing_value

function isopen(ds::NCDataset)
try
Expand Down Expand Up @@ -64,21 +65,25 @@ end
function NCDataset(ncid::Integer,
iswritable::Bool,
isdefmode::Ref{Bool};
parentdataset = nothing)
parentdataset = nothing,
_experimental_missing_value = missing,
)

function _finalize(ds)
# only close open root group
if (ds.ncid != -1) && (ds.parentdataset == nothing)
close(ds)
end
end

ds = NCDataset{typeof(parentdataset)}(
@debug "_experimental_missing_value" _experimental_missing_value
ds = NCDataset{typeof(parentdataset),typeof(_experimental_missing_value)}(
parentdataset,
ncid,
iswritable,
isdefmode,
Dict{String,String}())
Dict{String,String}(),
_experimental_missing_value,
)

if !iswritable
initboundsmap!(ds)
Expand Down Expand Up @@ -169,6 +174,7 @@ function NCDataset(filename::AbstractString,
diskless::Bool = false,
persist::Bool = false,
memory::Union{Vector{UInt8},Nothing} = nothing,
_experimental_missing_value = missing,
attrib = [])

ncid = -1
Expand Down Expand Up @@ -226,7 +232,9 @@ function NCDataset(filename::AbstractString,
end

iswritable = mode != "r"
ds = NCDataset(ncid,iswritable,isdefmode)
ds = NCDataset(
ncid,iswritable,isdefmode,
_experimental_missing_value = _experimental_missing_value)

# set global attributes
for (attname,attval) in attrib
Expand Down
3 changes: 2 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct Variable{NetCDFType,N,TDS} <: AbstractNCVariable{NetCDFType, N}
end

# must be mutable to register a finalizer
mutable struct NCDataset{TDS} <: AbstractNCDataset where TDS <: Union{AbstractNCDataset,Nothing}
mutable struct NCDataset{TDS,T_experimental_missing_value} <: AbstractNCDataset where TDS <: Union{AbstractNCDataset,Nothing}
# parent_dataset is nothing for the root dataset
parentdataset::TDS
ncid::Cint
Expand All @@ -35,6 +35,7 @@ mutable struct NCDataset{TDS} <: AbstractNCDataset where TDS <: Union{AbstractNC
# mapping between variables related via the bounds attribute
# It is only used for read-only datasets to improve performance
_boundsmap::Dict{String,String}
_experimental_missing_value::T_experimental_missing_value
end

"Alias to `NCDataset`"
Expand Down
28 changes: 28 additions & 0 deletions test/test_fillvalue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,31 @@ v = defVar(ds,"instrument_mode",String,("mode_items",),fillvalue = "UNDEFINED MO
@test fillvalue(v) == "UNDEFINED MODE"
close(ds)
rm(filename)



# Alternative to Missing for NetCDF fillvalue

fname = tempname()
data = randn(3,4)
fv = 9999
data[2,2] = fv

ds = NCDataset(fname,"c")
defDim(ds,"lon",size(data,1))
defDim(ds,"lat",size(data,2))
ncv = defVar(ds,"data",Float64,("lon","lat"),fillvalue = fv)
ncv.var[:,:] = data

ncv = cfvariable(ds,"data",_experimental_missing_value = NaN)
@test eltype(ncv) == Float64
@test ncv[1,1] == data[1,1]
@test isnan(ncv[2,2])
close(ds)


ds = NCDataset(fname,"r",_experimental_missing_value = NaN)
ncv = ds["data"]

@test ncv[1,1] == data[1,1]
@test isnan(ncv[2,2])

0 comments on commit 514559e

Please sign in to comment.