diff --git a/src/DatasetAPI/Datasets.jl b/src/DatasetAPI/Datasets.jl index ffaad506..30b11810 100644 --- a/src/DatasetAPI/Datasets.jl +++ b/src/DatasetAPI/Datasets.jl @@ -37,7 +37,7 @@ function Dataset(; properties = Dict{String,Any}(), cubes...) foreach(a -> push!(axesall, a), ax) end axesall = collect(axesall) - axnameall = DD.dim2key.(axesall) + axnameall = DD.name.(axesall) axesnew = Dict{Symbol,DD.Dimension}(axnameall[i] => axesall[i] for i = 1:length(axesall)) Dataset(OrderedDict(cubes), axesnew, properties) end @@ -559,7 +559,7 @@ function savedataset( end axesall = values(ds.axes) - chunkoffset = [alloffsets[k] for k in keys(ds.axes)] + chunkoffset = [alloffsets[k] for k in DD.name.(axesall)] # keys(ds.axes) axdata = arrayfromaxis.(axesall, chunkoffset) diff --git a/test/Datasets/datasets.jl b/test/Datasets/datasets.jl index 4e9ccef5..5952db3d 100644 --- a/test/Datasets/datasets.jl +++ b/test/Datasets/datasets.jl @@ -1,7 +1,58 @@ using DataStructures: OrderedDict +using DimensionalData using DimensionalData: DimensionalData as DD using Dates + +@testset "Datasets axes Ti" begin + using Zarr, NetCDF + + ## first example + data = [rand(4, 5, 12), rand(4, 5, 12), rand(4, 5)] + # dim_time = DD.Dim{:Time}(Date(2001, 1, 15):Month(1):Date(2001, 12, 15)) + dim_time = Ti(Date(2001, 1, 15):Month(1):Date(2001, 12, 15)) + axlist1 = ( + DD.Dim{:XVals}(1.0:4.0), + DD.Dim{:YVals}([1, 2, 3, 4, 5]), + dim_time + ) + axlist2 = (DD.Dim{:XVals}(1.0:4.0), DD.Dim{:YVals}([1, 2, 3, 4, 5])) + props = [Dict("att$i" => i) for i = 1:3] + c1, c2, c3 = ( + YAXArray(axlist1, data[1], props[1]), + YAXArray(axlist1, data[2], props[2]), + YAXArray(axlist2, data[3], props[3]), + ) + ds = Dataset(avar=c1, something=c2, smaller=c3) + # previous version will throw this error: `KeyError: key :Ti not found` + f = "./temp.zarr" + @test_nowarn savedataset(ds; path=f) + rm(f, recursive=true, force=true) + + + ## second example + using Downloads + path2file = "https://www.unidata.ucar.edu/software/netcdf/examples/sresa1b_ncar_ccsm3-example.nc" + filename = Downloads.download(path2file, "sresa1b_ncar_ccsm3-example.nc") + ds = open_dataset(filename) + f = "./temp.zarr" + savedataset(ds, path=f, driver=:zarr, overwrite=true) + rm(f, recursive=true, force=true) + rm(filename) + + ## third example + # using EarthDataLab + # using DimensionalData + # using Zarr, YAXArrays, ra = esdc(res="tiny") + # ra_tair = ra[variable=At("air_temperature_2m")] + # ra_resp = ra[variable=At("terrestrial_ecosystem_respiration")] + # ds = Dataset(tair=ra_tair, resp=ra_resp) + # f = "./temp.zarr" + # savedataset(ds, path=f, driver=:zarr, overwrite=true) + # rm(f, recursive=true, force=true) +end + + @testset "Datasets" begin data = [rand(4, 5, 12), rand(4, 5, 12), rand(4, 5)] axlist1 = ( @@ -168,7 +219,7 @@ using Dates ar = Cube(ds) @test ar isa YAXArray @test size(ar) == (10, 5, 2, 2) - @test DD.name.(ar.axes) == (:time, :d2, :d3, :Variable) + @test DD.name.(ar.axes) == (:Time, :d2, :d3, :Variable) @test DD.lookup(ar.axes[4]) == ["Var1", "Var3"] end @testset "Dataset creation" begin @@ -179,7 +230,7 @@ using Dates ) # Basic newds, newds2 = YAXArrays.Datasets.createdataset(MockDataset, al) - @test DD.dim2key.(newds2.axes) == [:Time, :Xvals, :Variable] + @test DD.dim2key.(newds2.axes) == (:Time, :Xvals, :Variable) @test DD.lookup(newds2.axes[1]) == Date(2001):Month(1):Date(2001, 12, 31) @test DD.lookup(newds2.axes[3]) == ["A", "B"] @test DD.lookup(newds2.axes[2]) == 1:10 @@ -221,7 +272,7 @@ end ax1 = Dim{:Ax1}(string.(1:10)) ax2 = Dim{:Ax2}(1:5) p = string(tempname(),".zarr") - savecube(YAXArray([ax1, ax2], x), p, backend = :zarr) + savecube(YAXArray((ax1, ax2), x), p, backend = :zarr) @test ispath(p) cube1 = Cube(p) @test cube1.Ax1 == ax1 @@ -380,10 +431,10 @@ end c = Cube(ds) @test size(c) == (10,10,4) @test eltype(c) <: Float64 - x = c[var="c"][:,:] + x = c[var=At("c")][:,:] @test eltype(x) <: Float64 @test x == Float64.(a3.data) ds = Dataset(a=a1, b=a2,c=a3,d=a4,e=a5) @test_throws ArgumentError Cube(ds) -end \ No newline at end of file +end diff --git a/test/Project.toml b/test/Project.toml index 10a35a8c..a7486127 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -4,6 +4,7 @@ DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0" DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3" +Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6" NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" OnlineStats = "a15396b6-48d5-5d58-9928-6d29437db91e" diff --git a/test/runtests.jl b/test/runtests.jl index 4154c968..c34a3291 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,7 +12,7 @@ include("Cubes/cubes.jl") include("Cubes/transformedcubes.jl") include("Cubes/batchextraction.jl") -#include("Datasets/datasets.jl") +include("Datasets/datasets.jl") include("DAT/PickAxisArray.jl") include("DAT/MovingWindow.jl")