Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to fix Ti error #337

Merged
merged 4 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DatasetAPI/Datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down
61 changes: 56 additions & 5 deletions test/Datasets/datasets.jl
Original file line number Diff line number Diff line change
@@ -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 = (
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading