Skip to content

Commit 98088ad

Browse files
authored
Merge pull request #23 from Alexander-Barth/main
allow to use a different masking value than missing
2 parents 7192aec + 763783c commit 98088ad

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
1111
GRIB = "b16dfd50-4035-11e9-28d4-9dfe17e6779b"
1212

1313
[compat]
14-
CommonDataModel = "^0.2.1, 0.3"
14+
CommonDataModel = "0.3.4"
1515
DataStructures = "0.18"
1616
DiskArrays = "0.3"
1717
GRIB = "0.3, 0.4"

src/GRIBDatasets.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ using GRIB
55
using DataStructures
66
import DiskArrays as DA
77
using CommonDataModel: AbstractDataset, AbstractVariable, show_dim, CFVariable
8-
import CommonDataModel: path, name, dimnames, isopen, attribnames, attrib, dataset
8+
import CommonDataModel: path, name, dimnames, isopen, attribnames, attrib,
9+
dataset, maskingvalue
910
import CommonDataModel as CDM
1011

1112
const DEFAULT_EPOCH = DateTime(1970, 1, 1, 0, 0)

src/cfvariables.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11

22
_get_dim(cfvar::CFVariable, dimname) = _get_dim(cfvar.var, dimname)
3-
function cfvariable(ds, varname)
3+
function cfvariable(ds, varname; maskingvalue = maskingvalue(ds))
44
v = Variable(ds, string(varname))
55
misval = missing_value(v)
66
CDM.cfvariable(
77
ds, varname;
88
_v = v,
99
missing_value = isnothing(misval) ? eltype(v)[] : [misval],
1010
attrib = cflayer_attributes(v),
11+
maskingvalue = maskingvalue,
1112
)
1213
end
1314

src/dataset.jl

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ It can be created with the path to the GRIB file:
88
ds = GRIBDataset(example_file);
99
```
1010
"""
11-
struct GRIBDataset{T, N} <: AbstractDataset
11+
struct GRIBDataset{T, N, Tmaskingvalue} <: AbstractDataset
1212
index::FileIndex{T}
1313
dims::NTuple{N, AbstractDim}
1414
attrib::Dict{String, Any}
15+
maskingvalue::Tmaskingvalue
1516
end
1617

1718
const Dataset = GRIBDataset
1819

19-
function GRIBDataset(index::FileIndex)
20-
GRIBDataset(index, _alldims(index), dataset_attributes(index))
20+
function GRIBDataset(index::FileIndex; maskingvalue = missing)
21+
GRIBDataset(index, _alldims(index), dataset_attributes(index), maskingvalue)
2122
end
2223

23-
GRIBDataset(filepath::AbstractString; filter_by_values = Dict()) = GRIBDataset(FileIndex(filepath; filter_by_values))
24+
GRIBDataset(filepath::AbstractString; filter_by_values = Dict(), kwargs...) =
25+
GRIBDataset(FileIndex(filepath; filter_by_values); kwargs...)
2426

2527
Base.keys(ds::Dataset) = getvars(ds)
2628
Base.haskey(ds::Dataset, key) = key in keys(ds)
@@ -46,6 +48,7 @@ dimnames(ds::GRIBDataset) = keys(ds.dims)
4648

4749
attribnames(ds::GRIBDataset) = keys(ds.attrib)
4850
attrib(ds::GRIBDataset, attribname::String) = ds.attrib[attribname]
51+
maskingvalue(ds::GRIBDataset) = ds.maskingvalue
4952

5053
# _dim_values(ds::GRIBDataset, dim::Dimension{Horizontal}) = _dim_values(ds.index, dim)
5154

test/dataset.jl

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using GRIBDatasets: CDM
1212
grib_path = joinpath(dir_testfiles, "era5-levels-members.grib")
1313
ds = GRIBDataset(grib_path)
1414
dsmis = GRIBDataset(joinpath(dir_testfiles, "fields_with_missing_values.grib"))
15+
dsNaN = GRIBDataset(joinpath(dir_testfiles, "fields_with_missing_values.grib"),maskingvalue = NaN)
1516
index = ds.index
1617

1718
varstring = "z"
@@ -87,12 +88,25 @@ using GRIBDatasets: CDM
8788
@testset "cfvariable and missing" begin
8889
cfvar = cfvariable(ds, varstring)
8990
cfvarmis = cfvariable(dsmis, "t2m")
91+
cfvarNaN = cfvariable(dsNaN, "t2m")
9092
A = cfvar[:,:,1,1,1]
9193
Amis = cfvarmis[:,:,1,1]
94+
ANaN = cfvarNaN[:,:,1,1]
9295

9396
# With CommonDataModel, we necessarily get a Union{Missing, Float64}, even if there's no missing.
9497
@test_broken eltype(A) == Float64
9598
@test eltype(Amis) == Union{Missing, Float64}
99+
@test eltype(ANaN) == Float64
100+
101+
# test the use of a different maskingvalue per dataset
102+
@test eltype(dsmis["t2m"]) == Union{Missing,Float64}
103+
@test eltype(dsNaN["t2m"]) == Float64
104+
@test ismissing(Amis[1,1,1])
105+
@test isnan(ANaN[1,1,1])
106+
107+
# test the use of a different maskingvalue per variable
108+
A2NaN = cfvariable(dsmis,"t2m", maskingvalue = NaN)
109+
@test isnan(A2NaN[1,1,1])
96110
end
97111

98112
@testset "cfvariable coordinate" begin
@@ -181,4 +195,4 @@ end
181195
@time ds = GRIBDataset(testfile)
182196
end
183197

184-
end
198+
end

0 commit comments

Comments
 (0)