Skip to content

Commit 2f7ea94

Browse files
authored
Make EarthDataLab work for DimDataYAXArrays (#298)
* First tests pass * Access and analysis ok * All tests pass * All tests pass * bump minimum julia version
1 parent b25ad6e commit 2f7ea94

File tree

14 files changed

+92
-136
lines changed

14 files changed

+92
-136
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
version:
16-
- '1.6'
1716
- '1'
17+
- '1.8'
1818
os:
1919
- ubuntu-latest
2020
- macOS-latest

Project.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name = "EarthDataLab"
22
uuid = "359177bc-a543-11e8-11b7-bb015dba3358"
33
authors = ["Fabian Gans <[email protected]>"]
4-
version = "0.12.1"
4+
version = "0.13.0"
55

66
[deps]
77
CFTime = "179af706-886a-5703-950a-314cd64e0468"
88
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
9+
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
910
DiskArrayTools = "fcd2136c-9f69-4db6-97e5-f31981721d63"
1011
DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
1112
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
@@ -32,9 +33,9 @@ Polynomials = "1, 2.0, 3"
3233
StatsBase = "0.32, 0.33, 0.34"
3334
Tables = "0.2, 1.0"
3435
WeightedOnlineStats = "0.5, 0.6"
35-
YAXArrays = "0.4"
36+
YAXArrays = "0.5"
3637
Zarr = "0.9"
37-
julia = "1.6"
38+
julia = "1.8"
3839

3940
[extras]
4041
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"

src/EarthDataLab.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,28 @@ include("esdc.jl")
1212
include("ESDLTools.jl")
1313

1414
using YAXArrays: @reexport
15+
import DimensionalData as DD
1516

1617
@reexport using Dates: Date, DateTime
1718
@reexport using YAXArrays: (..)
1819
@reexport using YAXArrays: concatenatecubes, caxes,
1920
subsetcube, readcubedata,renameaxis!, YAXArray
20-
@reexport using YAXArrays: CubeAxis, RangeAxis, CategoricalAxis,
21-
getAxis
2221

2322
@reexport using YAXArrays: mapCube, getAxis, InDims, OutDims, Dataset,
2423
CubeTable, cubefittable, fittable, savecube, Cube, open_dataset #From DAT module
2524
@reexport using .Proc: removeMSC, gapFillMSC,normalizeTS,
2625
getMSC, filterTSFFT, getNpY,interpolatecube,
27-
getMedSC, extractLonLats, cubefromshape,
26+
getMedSC, cubefromshape,
2827
gapfillpoly, spatialinterp #From Proc module # from ESDL Tools
2928

3029
@reexport using .ESDC: esdc, esdd
3130

31+
#For backwards compatibility:
32+
RangeAxis(name,vals) = DD.Dim{Symbol(name)}(vals)
33+
CategoricalAxis(name,vals) = DD.Dim{Symbol(name)}(vals)
34+
35+
export RangeAxis, CategoricalAxis
36+
3237
using NetCDF: NetCDF
3338
using Zarr: Zarr
3439

src/MSC.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Returns the mean annual cycle from each time series.
124124
"""
125125
function getMSC(c;kwargs...)
126126
N = getNpY(c)
127-
outdims = OutDims(RangeAxis("MSC",DateTime(1900):Day(ceil(Int,366/N)):DateTime(1900,12,31,23,59,59)),
127+
outdims = OutDims(DD.Dim{:MSC}(DateTime(1900):Day(ceil(Int,366/N)):DateTime(1900,12,31,23,59,59)),
128128
outtype = mscouttype(eltype(c)))
129129
indims = InDims("Time")
130130
mapCube(getMSC,c,getNpY(c);indims=indims,outdims=outdims,kwargs...)
@@ -174,7 +174,7 @@ Returns the median annual cycle from each time series.
174174
"""
175175
function getMedSC(c;kwargs...)
176176
N = getNpY(c)
177-
outdims = OutDims(RangeAxis("MSC",DateTime(1900):Day(ceil(Int,366/N)):DateTime(1900,12,31,23,59,59)),
177+
outdims = OutDims(DD.Dim{:MSC}(DateTime(1900):Day(ceil(Int,366/N)):DateTime(1900,12,31,23,59,59)),
178178
outtype = mscouttype(eltype(c)))
179179
indims = InDims("Time")
180180
mapCube(getMedSC,c;indims=indims,outdims=outdims,kwargs...)

src/Proc.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
module Proc
22
using YAXArrays.Cubes: YAXArray, cubechunks, caxes
3-
using YAXArrays.Cubes.Axes: getAxis, findAxis, CategoricalAxis, axVal2Index,
4-
RangeAxis, get_bb, axisfrombb, CubeAxis, axname
53
using YAXArrays.DAT: mapCube, InDims, OutDims, NValid, AnyMissing
64
using YAXArrays.Datasets: getsavefolder, Cube
5+
using YAXArrays: getAxis
76
using Dates: year, Date, DateTime
87
"""
98
getNpY(cube)
@@ -12,7 +11,7 @@ Get the number of time steps per year
1211
"""
1312
function getNpY(cube)
1413
timax = getAxis("Time",cube)
15-
years = year.(timax.values)
14+
years = year.(timax.val)
1615
years[end] > years[1] + 1 || error("Must have at least 3 years to estimate number of time steps per year")
1716
return count(i -> i == years[1] + 1, years)
1817
end
@@ -22,6 +21,5 @@ include("Stats.jl")
2221
include("TSDecomposition.jl")
2322
include("remap.jl")
2423
include("Shapes.jl")
25-
include("extractlonlats.jl")
2624

2725
end

src/extractlonlats.jl

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/remap.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
using DiskArrayTools: InterpolatedDiskArray, DiskArrayTools
22
using DiskArrayTools.Interpolations: Linear, Flat, Constant, NoInterp
33
using DiskArrays: eachchunk, GridChunks, approx_chunksize, grid_offset
4+
using YAXArrays: findAxis, getAxis
5+
import DimensionalData as DD
46
"""
57
spatialinterp(c,newlons::AbstractRange,newlats::AbstractRange;order=Linear(),bc = Flat())
68
"""
79
function spatialinterp(c,newlons::AbstractRange,newlats::AbstractRange;order=Linear(),bc = Flat())
810
interpolatecube(c,Dict("Lon"=>newlons, "Lat"=>newlats), order = Dict("Lon"=>order,"Lat"=>order))
911
end
10-
spatialinterp(c,newlons::CubeAxis,newlats::CubeAxis;kwargs...)=
11-
spatialinterp(c,newlons.values,newlats.values;kwargs...)
12+
spatialinterp(c,newlons::DD.Dim,newlats::DD.Dim;kwargs...)=
13+
spatialinterp(c,newlons.val.data,newlats.val.data;kwargs...)
1214

1315

1416
function getsteprat(newax::AbstractRange, oldax::AbstractRange)
@@ -30,7 +32,7 @@ end
3032
chunkold = eachchunk(c.data)
3133
axold = caxes(c)
3234
axinds = map(i->findAxis(i,c),k)
33-
steprats = map((inew,iold)->getsteprat(newaxdict[inew], axold[iold].values),k,axinds)
35+
steprats = map((inew,iold)->getsteprat(newaxdict[inew], axold[iold].val.data),k,axinds)
3436
newcs = ntuple(ndims(c)) do i
3537
ii = findfirst(isequal(i),axinds)
3638
round(Int,approx_chunksize(chunkold.chunks[i]) * (ii==nothing ? 1 : steprats[ii]))
@@ -54,7 +56,7 @@ function interpolatecube(c,
5456
if ai === nothing
5557
nothing,NoInterp(),nothing,nothing
5658
else
57-
oldvals = caxes(c)[i].values
59+
oldvals = caxes(c)[i].val.data
5860
newvals = newaxes[ii[ai][1]]
5961
getinterpinds(oldvals, newvals),get(order,ii[ai][1],Constant()),get(bc,ii[ai][1],Flat()),newvals
6062
end
@@ -68,7 +70,7 @@ function interpolatecube(c,
6870
if val === nothing
6971
ax
7072
else
71-
RangeAxis(axname(ax),val)
73+
DD.rebuild(ax,val)
7274
end
7375
end
7476
YAXArray(newax,ar,c.properties, cleaner = c.cleaner)

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
33
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
44
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
5+
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
56
DiskArrayTools = "fcd2136c-9f69-4db6-97e5-f31981721d63"
67
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
78
EarthDataLab = "359177bc-a543-11e8-11b7-bb015dba3358"

test/access.jl

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@ using Dates
33
c=Cube()
44

55
@testset "Access single variable" begin
6-
d = subsetcube(c,variable="air_temperature_2m",lon=(10,11),lat=(51,50),
7-
time=(Date("2002-01-01"),Date("2008-12-31")))
8-
9-
d.data.v.indices==(18:21, 17:20, 93:414)
10-
@test d.axes[1].values==10.125:0.25:10.875
11-
@test d.axes[2].values==50.875:-0.25:50.125
12-
@test d.axes[1] == RangeAxis("lon", 10.125:0.25:10.875)
13-
@test d.axes[2] == RangeAxis("lat", 50.875:-0.25:50.125)
6+
d = c[variable=DD.At("air_temperature_2m"),lon=10..11,lat=50..51,
7+
time=Date("2002-01-01")..Date("2008-12-31")]
8+
9+
d.data.v.indices==(18:21, 17:20, 93:414)
10+
@test d.axes[1].val==10.125:0.25:10.875
11+
@test d.axes[2].val==50.875:-0.25:50.125
12+
@test d.axes[1] == Dim{:lon}(10.125:0.25:10.875)
13+
@test d.axes[2] == Dim{:lat}(50.875:-0.25:50.125)
1414
end
1515

1616

1717

1818
@testset "Access multiple variables" begin
19-
d2 = subsetcube(c,variable=["air_temperature_2m","gross_primary_productivity"],lon=(10,11),lat=(50,51),
20-
time=(Date("2002-01-01"),Date("2008-12-31")))
21-
22-
@test d2.axes[4].values == ["air_temperature_2m", "gross_primary_productivity"]
23-
@test d2.data.arrays[1].v.indices ==(18:21, 17:20, 93:414)
24-
@test d2.axes[1].values==10.125:0.25:10.875
25-
@test d2.axes[2].values==50.875:-0.25:50.125
26-
@test first(d2.axes[3].values) == Dates.Date(2002,1,5)
27-
@test last(d2.axes[3].values) == Dates.Date(2008, 12, 30)
19+
d2 = c[variable=DD.At(["air_temperature_2m","gross_primary_productivity"]),lon=10..11,lat=50..51,
20+
time=Date("2002-01-01")..Date("2008-12-31")]
21+
22+
@test d2.axes[4].val == ["air_temperature_2m", "gross_primary_productivity"]
23+
@test d2.data.arrays[1].v.indices ==(18:21, 17:20, 93:414)
24+
@test d2.axes[1].val==10.125:0.25:10.875
25+
@test d2.axes[2].val==50.875:-0.25:50.125
26+
@test first(d2.axes[3].val) == Dates.Date(2002,1,5)
27+
@test last(d2.axes[3].val) == Dates.Date(2008, 12, 30)
2828
end
2929

3030
@testset "Test values in MemCube" begin
31-
d = subsetcube(c,variable="air_temperature_2m",lon=(10,11),lat=(51,50),
32-
time=(Date("2002-01-01"),Date("2008-12-31")))
33-
d2 = subsetcube(c,variable=["air_temperature_2m","gross_primary_productivity"],lon=(10,11),lat=(50,51),
34-
time=(Date("2002-01-01"),Date("2008-12-31")))
31+
d = c[variable=DD.At("air_temperature_2m"),lon=10..11,lat=50..51,
32+
time=Date("2002-01-01")..Date("2008-12-31")]
33+
d2 = c[variable=DD.At(["air_temperature_2m","gross_primary_productivity"]),lon=10..11,lat=50..51,
34+
time=Date("2002-01-01")..Date("2008-12-31")]
3535
data1=readcubedata(d)
3636
data2=readcubedata(d2)
3737

@@ -40,52 +40,42 @@ data2=readcubedata(d2)
4040

4141

4242
@test isapprox(data1.data[1,1,1:10],Float32[267.9917, 269.9631, 276.71036, 280.88998,
43-
280.90665, 277.02243, 274.5466, 276.919, 279.96243, 279.42276])
43+
280.90665, 277.02243, 274.5466, 276.919, 279.96243, 279.42276])
4444

4545
@test isapprox(data2.data[1,1,1:10,1],Float32[267.9917, 269.9631, 276.71036, 280.88998,
46-
280.90665, 277.02243, 274.5466, 276.919, 279.96243, 279.42276])
46+
280.90665, 277.02243, 274.5466, 276.919, 279.96243, 279.42276])
4747

48-
@test caxes(data1)[1:2]==CubeAxis[RangeAxis("lon",10.125:0.25:10.875),RangeAxis("lat",50.875:-0.25:50.125)]
48+
@test caxes(data1)[1:2]==(Dim{:lon}(10.125:0.25:10.875),Dim{:lat}(50.875:-0.25:50.125))
4949

5050
tax = caxes(data1)[3]
51-
@test YAXArrays.Cubes.Axes.axsym(tax)==:time
52-
@test tax.values[1] == Date(2002,1,5)
53-
@test tax.values[end] == Date(2008,12,30)
54-
@test length(tax.values) == 7*46
51+
@test DD.name(tax)==:Time
52+
@test tax.val[1] == Date(2002,1,5)
53+
@test tax.val[end] == Date(2008,12,30)
54+
@test length(tax.val) == 7*46
5555

56-
@test caxes(data2)[[1,2,4]]==CubeAxis[RangeAxis("lon",10.125:0.25:10.875),RangeAxis("lat",50.875:-0.25:50.125),CategoricalAxis("Variable",["air_temperature_2m","gross_primary_productivity"])]
56+
@test caxes(data2)[[1,2,4]]==(Dim{:lon}(10.125:0.25:10.875),Dim{:lat}(50.875:-0.25:50.125),Dim{:Variable}(["air_temperature_2m","gross_primary_productivity"]))
5757

5858
tax = caxes(data2)[3]
59-
@test YAXArrays.Cubes.Axes.axsym(tax)==:time
60-
@test tax.values[1] == Date(2002,1,5)
61-
@test tax.values[end] == Date(2008,12,30)
62-
@test length(tax.values) == 7*46
59+
@test DD.name(tax)==:Time
60+
@test tax.val[1] == Date(2002,1,5)
61+
@test tax.val[end] == Date(2008,12,30)
62+
@test length(tax.val) == 7*46
6363

64-
end
6564

66-
@testset "Coordinate extraction" begin
67-
d = subsetcube(c,variable="air_temperature_2m",lon=(10,11),lat=(51,50),
68-
time=(Date("2002-01-01"),Date("2008-12-31")))
69-
data1=readcubedata(d)
70-
# Test reading of coordinate list
71-
ll=[10.1 50.2;10.5 51.1;10.8 51.1]
72-
llcube = readcubedata(extractLonLats(data1,ll))
73-
@test llcube.data[1,:]==data1.data[1,4,:]
74-
@test llcube.data[2,:]==data1.data[3,1,:]
75-
@test llcube.data[3,:]==data1.data[4,1,:]
7665
end
7766

7867
@testset "Accessing regions" begin
7968
#Test access datacube by region
80-
d3 = subsetcube(c,variable="gross_primary_productivity",region="Austria",time=Date("2005-01-01"))
81-
@test d3.axes==[RangeAxis("lon",9.625:0.25:14.875),RangeAxis("lat",48.875:-0.25:47.375)]
69+
d3 = c[variable=DD.At("gross_primary_productivity"),region="Austria",time=DD.Near(DateTime("2005-01-01"))]
70+
@test d3.axes==(Dim{:lon}(9.625:0.25:14.875),RangeAxis("lat",48.875:-0.25:47.375))
71+
8272
end
8373

8474
using DiskArrayTools: DiskArrayStack
8575

8676
@testset "Saving and loading cubes" begin
87-
d = subsetcube(c,variable="air_temperature_2m",lon=(10,31),lat=(51,50),
88-
time=(Date("2002-01-01"),Date("2008-12-31")))
77+
d = c[variable=DD.At("air_temperature_2m"),lon=(10..31),lat=(50..51),
78+
time=(Date("2002-01-01")..Date("2008-12-31"))]
8979
data1=readcubedata(d)
9080
#Test saving cubes
9181
dire=tempname()
@@ -97,7 +87,7 @@ using DiskArrayTools: DiskArrayStack
9787
@test data1.data==data3.data
9888

9989
# Test loadOrGenerate macro
100-
d=subsetcube(c,time=Date(2001)..Date(2005),lon=(10,11),lat=(50,51),variable=["gross_primary_productivity","net_ecosystem_exchange"])
90+
d=c[time=Date(2001)..Date(2005),lon=(10..11),lat=(50..51),variable=DD.At(["gross_primary_productivity","net_ecosystem_exchange"])]
10191

10292
danom = removeMSC(d)
10393

@@ -108,7 +98,7 @@ using DiskArrayTools: DiskArrayStack
10898
danom=readcubedata(danom)
10999
danom2=readcubedata(Cube(zp))
110100

111-
@test danom.axes==danom2.axes
101+
@test danom.axes[1:3]==danom2.axes[1:3]
112102
@test all(map(isequal,danom.data,danom2.data))
113103

114104
ncf = string(tempname(),".nc")
@@ -118,8 +108,8 @@ using DiskArrayTools: DiskArrayStack
118108
#Test exportcube
119109
@test ncread(ncf,"lon") == 10.125:0.25:10.875
120110
@test ncread(ncf,"lat") == 50.875:-0.25:50.125
121-
@test ncgetatt(ncf,"time","units") == "days since 1980-01-01"
122-
@test getAxis("time",danom).values .- DateTime(1980) == Millisecond.(Day.(ncread(ncf,"time")))
111+
@test ncgetatt(ncf,"Time","units") == "days since 1980-01-01"
112+
@test getAxis("Time",danom).val .- DateTime(1980) == Millisecond.(Day.(ncread(ncf,"Time")))
123113

124114
anc = replace(ncread(ncf,"gross_primary_productivity")[:,:,:],-9999.0=>missing)
125115
@test all(isequal.(anc, danom.data[:,:,:,1]))
@@ -131,9 +121,9 @@ end
131121

132122
@testset "ESDC v3" begin
133123
c = esdd(res="low")
134-
d = c.gross_primary_productivity[time=Date(2005)][443:444,139:140]
124+
d = c.gross_primary_productivity[time=DD.Near(DateTime(2005))].data[443:444,139:140]
135125
d == Float32[3.1673577 3.7342484; 3.3267372 4.0305696]
136126

137127
c = esdd(res="tiny")
138-
c.gross_primary_productivity[time=Date(2005)][44,14] == 2.3713999f0
128+
c.gross_primary_productivity[time=DD.Near(DateTime(2005))].data[44,14] == 2.3713999f0
139129
end

0 commit comments

Comments
 (0)