Skip to content

Commit f309830

Browse files
meggartfelixcremer
andauthored
Fix newly introduced bug when opening Zarr arrays (#51)
* Fix newly introduced bug when opening Zarr arrays * Fix tests for zarr data and add test on to_dataset for Zgroup * Test rational writing only for zarr --------- Co-authored-by: Felix Cremer <fcremer@bgc-jena.mpg.de>
1 parent 4c28335 commit f309830

3 files changed

Lines changed: 32 additions & 21 deletions

File tree

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "YAXArrayBase"
22
uuid = "90b8fcef-0c2d-428d-9c56-5f86629e9d14"
33
authors = ["Fabian Gans <fgans@bgc-jena.mpg.de>"]
4-
version = "0.7.9"
4+
version = "0.7.10"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
@@ -14,7 +14,7 @@ julia = "1.9"
1414
ArchGDAL = "0.10"
1515
AxisArrays = "0.4"
1616
AxisKeys = "0.2"
17-
DimensionalData = "0.27, 0.28, 0.29"
17+
DimensionalData = "0.27, 0.28, 0.29, 0.30"
1818
NetCDF = "0.11, 0.12"
1919
Zarr = "0.8, 0.9"
2020

ext/ZarrExt.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ end
1515
struct ZarrDataset
1616
g::ZGroup
1717
end
18-
function ZarrDataset(g::String; mode="r", path="", kwargs...)
18+
function ZarrDataset(g::Union{String,ZGroup}; mode="r", path="", kwargs...)
19+
if g isa ZGroup
20+
return ZarrDataset(g)
21+
end
1922
store = if endswith(g, "zip")
2023
ZipStore(ZipReader(SimpleFileDiskArray(g)))
2124
else

test/datasets.jl

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ end
6464
end
6565

6666
@testset "Reading Zarr" begin
67-
p = "gs://cmip6/CMIP6/HighResMIP/CMCC/CMCC-CM2-HR4/highresSST-present/r1i1p1f1/6hrPlev/psl/gn/v20170706/"
68-
ds_zarr = to_dataset(p,driver=:zarr)
69-
vn = get_varnames(ds_zarr)
70-
@test sort(vn) == ["lat", "lat_bnds", "lon", "lon_bnds", "psl", "time", "time_bnds"]
71-
@test get_var_dims(ds_zarr, "psl") == ["lon", "lat", "time"]
72-
@test get_var_dims(ds_zarr, "time") == ["time"]
73-
@test get_var_dims(ds_zarr, "time_bnds") == ["bnds", "time"]
74-
@test get_var_attrs(ds_zarr,"psl")["long_name"] == "Sea Level Pressure"
75-
h = get_var_handle(ds_zarr, "psl")
76-
@test YAXArrayBase.iscompressed(h)
77-
@test all(isapprox.(h[1:2,1:2,1], [99360.8 99334.9; 99360.8 99335.4]))
78-
@test allow_parallel_write(ds_zarr) == true
79-
@test allow_missings(ds_zarr) == false
67+
p = "https://s3.bgc-jena.mpg.de:9000/esdl-esdc-v3.0.2/esdc-16d-2.5deg-46x72x1440-3.0.2.zarr"
68+
for ds_zarr in [to_dataset(p,driver=:zarr), to_dataset(zopen(p))]
69+
vn = get_varnames(ds_zarr)
70+
@test sort(vn) == ["aerosol_optical_thickness_550", "air_temperature_2m", "bare_soil_evaporation", "burnt_area", "cot", "cth", "ctt", "evaporation", "evaporation_era5", "evaporative_stress", "gross_primary_productivity", "interception_loss", "kndvi", "lat", "latent_energy", "lon", "max_air_temperature_2m", "min_air_temperature_2m", "nbar_blue", "nbar_green", "nbar_nir", "nbar_red", "nbar_swir1", "nbar_swir2", "nbar_swir3", "ndvi", "net_ecosystem_exchange", "net_radiation", "nirv", "open_water_evaporation", "potential_evaporation", "precipitation_era5", "radiation_era5", "root_moisture", "sensible_heat", "sif_gome2_jj", "sif_gome2_pk", "sif_gosif", "sif_rtsif", "sm", "snow_sublimation", "surface_moisture", "terrestrial_ecosystem_respiration", "time", "transpiration"]
71+
@test get_var_dims(ds_zarr, "sm") == ["lon", "lat", "time"]
72+
@test get_var_dims(ds_zarr, "time") == ["time"]
73+
@test get_var_attrs(ds_zarr,"sm")["long_name"] == "Volumetric Soil Moisture"
74+
h = get_var_handle(ds_zarr, "air_temperature_2m")
75+
@test YAXArrayBase.iscompressed(h)
76+
@test all(isapprox.(h[1:2,1:2,1], [-28.8662 -23.4534; -28.8041 -23.1681]))
77+
@test allow_parallel_write(ds_zarr) == true
78+
@test allow_missings(ds_zarr) == false
79+
end
8080
end
8181
@testset "Reading ArchGDAL" begin
8282
using ArchGDAL
@@ -99,19 +99,27 @@ function test_write(T)
9999
add_var(ds, 0.5:1:9.5, "lon", ("lon",), Dict("units"=>"degrees_east"))
100100
add_var(ds, 20:-1.0:1, "lat", ("lat",), Dict("units"=>"degrees_north"))
101101
v = add_var(ds, Float32, "tas", (10,20), ("lon", "lat"), Dict{String,Any}("units"=>"Celsius"))
102-
w = add_var(ds, Rational{Int}, "tas2", (10,20), ("lon", "lat"), Dict{String,Any}("units"=>"Celsius"))
102+
# This is needed, since netcdf can not write rationals.
103+
if T == YAXArrayBase.backendlist[:zarr]
104+
w = add_var(ds, Rational{Int}, "tas2", (10,20), ("lon", "lat"), Dict{String,Any}("units"=>"Celsius"))
105+
w[:,:] = collect(reshape(1:200, 10, 20)) .// 2
106+
w = get_var_handle(ds, "tas2")
107+
@test w[1:2,1:2] == [1 11; 2 12] .// 2
108+
w = get_var_handle(ds, "tas2")
109+
@test w[1:2,1:2] == [1 11; 2 12] .// 2
110+
@test sort(get_varnames(ds)) == ["lat","lon","tas", "tas2"]
111+
else
112+
@test sort(get_varnames(ds)) == ["lat","lon","tas"]
113+
end
103114
v[:,:] = collect(reshape(1:200, 10, 20))
104-
w[:,:] = collect(reshape(1:200, 10, 20)) .// 2
105-
@test sort(get_varnames(ds)) == ["lat","lon","tas", "tas2"]
115+
106116
@test get_var_dims(ds, "tas") == ["lon", "lat"]
107117
@test get_var_dims(ds, "lon") == ["lon"]
108118
@test get_var_attrs(ds,"tas")["units"] == "Celsius"
109119
h = get_var_handle(ds, "lon")
110120
@test h[:] == 0.5:1:9.5
111121
v = get_var_handle(ds, "tas")
112122
@test v[1:2,1:2] == [1 11; 2 12]
113-
w = get_var_handle(ds, "tas2")
114-
@test w[1:2,1:2] == [1 11; 2 12] .// 2
115123
end
116124

117125
@testset "Writing NetCDF" begin

0 commit comments

Comments
 (0)