Skip to content

Commit

Permalink
support for maskingvalue
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Jan 30, 2024
1 parent c17918b commit 4e13804
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
10 changes: 5 additions & 5 deletions docs/src/fillvalue.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ var_nan = nomissing(ds["var"][:,:],NaN)
close(ds)
```

Such substitution can also be made more automatically using the experimental parameter` _experimental_missing_value` (suggestion for a good name for this parameter are welcome, please make a [github issue](https://github.com/Alexander-Barth/NCDatasets.jl/issues/188)) that can be user per variable:
Such substitution can also be made more automatically using the experimental parameter` maskingvalue` that can be user per variable:


```julia
ds = NCDataset("example.nc","r")
ncvar_nan = cfvariable(ds,"var",_experimental_missing_value = NaN)
ncvar_nan = cfvariable(ds,"var",maskingvalue = NaN)
ncvar_nan[:,:]
# 2×3 Matrix{Float64}:
# 1.0 2.0 3.0
Expand All @@ -57,15 +57,15 @@ close(ds)
Or per data-set:

```julia
ds = NCDataset("example.nc","r", _experimental_missing_value = NaN)
ds = NCDataset("example.nc","r", maskingvalue = NaN)
ds["var"][:,:]
# 2×3 Matrix{Float64}:
# 1.0 2.0 3.0
# NaN 20.0 30.0
close(ds)
```

Note choosing the `_experimental_missing_value` affects the element type of the NetCDF variable using julia type promotion rules, in particular note that following vector:
Note choosing the `maskingvalue` affects the element type of the NetCDF variable using julia type promotion rules, in particular note that following vector:


```julia
Expand Down Expand Up @@ -98,7 +98,7 @@ defVar(ds,"var32",data32,("lon","lat"),fillvalue = 9999f0)
defVar(ds,"var64",data64,("lon","lat"),fillvalue = 9999.)
close(ds)

ds = NCDataset("example_float32_64.nc","r", _experimental_missing_value = NaN32)
ds = NCDataset("example_float32_64.nc","r", maskingvalue = NaN32)
ds["var32"][:,:]
# 2×3 Matrix{Float32}:
# 1.0 2.0 3.0
Expand Down
2 changes: 1 addition & 1 deletion src/NCDatasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import CommonDataModel: AbstractDataset, AbstractVariable,
SubDataset,
@select, select, Near, coordinate_value, coordinate_names, split_by_and,
chunking, deflate, checksum,
_experimental_missing_value
maskingvalue


import DiskArrays
Expand Down
14 changes: 7 additions & 7 deletions src/dataset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +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
maskingvalue(ds::NCDataset) = ds.maskingvalue

function isopen(ds::NCDataset)
try
Expand Down Expand Up @@ -66,7 +66,7 @@ function NCDataset(ncid::Integer,
iswritable::Bool,
isdefmode::Ref{Bool};
parentdataset = nothing,
_experimental_missing_value = missing,
maskingvalue = missing,
)

function _finalize(ds)
Expand All @@ -75,14 +75,14 @@ function NCDataset(ncid::Integer,
close(ds)
end
end
@debug "_experimental_missing_value" _experimental_missing_value
ds = NCDataset{typeof(parentdataset),typeof(_experimental_missing_value)}(
@debug "maskingvalue" maskingvalue
ds = NCDataset{typeof(parentdataset),typeof(maskingvalue)}(
parentdataset,
ncid,
iswritable,
isdefmode,
Dict{String,String}(),
_experimental_missing_value,
maskingvalue,
)

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

ncid = -1
Expand Down Expand Up @@ -234,7 +234,7 @@ function NCDataset(filename::AbstractString,
iswritable = mode != "r"
ds = NCDataset(
ncid,iswritable,isdefmode,
_experimental_missing_value = _experimental_missing_value)
maskingvalue = maskingvalue)

# set global attributes
for (attname,attval) in attrib
Expand Down
4 changes: 2 additions & 2 deletions 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,T_experimental_missing_value} <: AbstractNCDataset where TDS <: Union{AbstractNCDataset,Nothing}
mutable struct NCDataset{TDS,Tmaskingvalue} <: AbstractNCDataset where TDS <: Union{AbstractNCDataset,Nothing}
# parent_dataset is nothing for the root dataset
parentdataset::TDS
ncid::Cint
Expand All @@ -35,7 +35,7 @@ mutable struct NCDataset{TDS,T_experimental_missing_value} <: AbstractNCDataset
# 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
maskingvalue::Tmaskingvalue
end

const Dataset = NCDataset
14 changes: 7 additions & 7 deletions test/test_copyvar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ using Dates
using NCDatasets
using Test

var = 10.0:10.0:40.0
tax = DateTime(2001,1,1) .+ Day.(Int.(var))
datavar = 10.0:10.0:40.0
tax = DateTime(2001,1,1) .+ Day.(Int.(datavar))
fname = tempname()
fname2 = tempname()
ds = NCDataset(fname, "c")
defDim(ds, "time", Inf) # "unlimited"
defVar(ds, "time", tax, ("time",))
defVar(ds, "var", var, ("time",),deflatelevel=9)
defVar(ds, "datavar", datavar, ("time",),deflatelevel=9)
close(ds)

NCDataset(fname, "r") do ds
time = ds["time"]
var = ds["var"]
datavar = ds["datavar"]
NCDataset(fname2, "c") do ds2
defVar(ds2, "time", time, ("time",))
defVar(ds2, "var", var, ("time",))
defVar(ds2, "datavar", datavar, ("time",))
@test "time" in unlimited(ds)
end
end

NCDataset(fname, "r") do ds
NCDataset(fname2, "c") do ds2
defVar(ds2, ds["time"])
defVar(ds2, ds["var"])
defVar(ds2, ds["datavar"])
@test "time" in unlimited(ds)
isshuffled,isdeflated,deflatelevel = deflate(ds["var"])
isshuffled,isdeflated,deflatelevel = deflate(ds["datavar"])
@test deflatelevel == 9
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_fillvalue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ 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)
ncv = cfvariable(ds,"data",maskingvalue = 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)
ds = NCDataset(fname,"r",maskingvalue = NaN)
ncv = ds["data"]

@test ncv[1,1] == data[1,1]
Expand Down

0 comments on commit 4e13804

Please sign in to comment.