Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/src/tutorials/esdl/examples_from_esdl_study_1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ vars = ["gross_primary_productivity", "air_temperature_2m", "surface_moisture"]
time_overlap = Date("2001-01-01")..Date("2020-12-31")

# So we "virtually get" the cube data virtually:
cube_subset = subsetcube(cube_handle, variable=vars, time=time_overlap)
cube_subset = cube_handle[Variable=At(vars), time=time_overlap]

# The next function estimates the median seasonal cycle. This changes the dimension of the cube, as the time domain is replaced by day of year (doy); Eq. 9 in the manuscript:
# $$
Expand Down
9 changes: 6 additions & 3 deletions docs/src/tutorials/intro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# In this tutorial we will explore the features of the YAXArrays package.

using YAXArrays, EarthDataLab, Zarr, NetCDF
using DimensionalData: Where

# ## Use data larger than RAM#
#
Expand All @@ -19,8 +20,10 @@ c = esdc(res="low")

# ## Subsets happen lazily

europe = subsetcube(c, region="Europe", time=2000:2016,
Variable=["air_temperature_2m", "net_ecosystem", "soil_moisture"])

plot(europe.time.values,europe[Variable="air_temperature_2m", lat=50, lon=11].data)
europe = c[region="Europe", time=2000:2016,
Variable=Where( x-> any(contains.((x,),["air_temperature_2m", "net_ecosystem", "moisture"])))]


plot(lookup(europe, Ti).data,europe[Variable=At("air_temperature_2m"), lat=50, lon=11].data)

122 changes: 1 addition & 121 deletions src/Cubes/Cubes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,10 @@ import DimensionalData: name

export concatenatecubes, caxes, subsetcube, readcubedata, renameaxis!, YAXArray, setchunks, cache

"""
This function calculates a subset of a cube's data
"""
function subsetcube end

"Returns the axes of a Cube"
function caxes end

# TODO: Give Axes an own module in YAXArrays
#=
include("Axes.jl")
using .Axes:
CubeAxis,
RangeAxis,
CategoricalAxis,
findAxis,
getAxis,
axVal2Index,
axname,
axsym,
axVal2Index_lb,
axVal2Index_ub,
renameaxis,
axcopy

"""
The `Axes` module handles the Axes of a data cube.
It provides the following exports:

$(EXPORTS)
"""
Axes
=#
"""
mutable struct CleanMe

Expand Down Expand Up @@ -272,16 +243,6 @@ cubechunks(c) = approx_chunksize(eachchunk(c))
DiskArrays.eachchunk(c::YAXArray) = c.chunks
getindex_all(a) = getindex(a, ntuple(_ -> Colon(), ndims(a))...).data

#=
function Base.getindex(x::YAXArray, i...)
if length(i)==1 && istable(first(i))
batchextract(x,first(i))
else
getdata(x)[i...]
end
end
=#


function batchextract(x,i)
# This function should be documented and moved to DimensionalData
Expand Down Expand Up @@ -388,88 +349,11 @@ _iscompressed(c::DiskArrays.PermutedDiskArray) = _iscompressed(c.a.parent)
_iscompressed(c::DiskArrays.SubDiskArray) = _iscompressed(c.v.parent)
_iscompressed(c) = YAXArrayBase.iscompressed(c)

# lift renameaxis functionality from Axes.jl to YAXArrays
# lift renameaxis functionality from DimensionalData to YAXArrays
renameaxis!(c::YAXArray, p::Pair) = DD.set(c, Symbol(first(p)) => last(p))

#=
function renameaxis!(c::YAXArray, p::Pair)
#This needs to be deleted, because DimensionalData cannot update the axlist
# Because this is a tuple instead of a vector
axlist = caxes(c)
i = findAxis(p[1], axlist)
axlist[i] = renameaxis(axlist[i], p[2])
c
end
function renameaxis!(c::YAXArray, p::Pair{<:Any,<:CubeAxis})
i = findAxis(p[1], caxes(c))
i === nothing && throw(ArgumentError("$(p[1]) Axis not found"))
length(caxes(c)[i].values) == length(p[2].values) ||
throw(ArgumentError("Length of replacement axis must equal length of old axis"))
caxes(c)[i] = p[2]
c
end
=#
function _subsetcube end

function subsetcube(z::YAXArray{T}; kwargs...) where {T}
newaxes, substuple = _subsetcube(z, collect(Any, map(Base.OneTo, size(z))); kwargs...)
newdata = view(getdata(z), substuple...)
YAXArray(newaxes, newdata, z.properties, cleaner=z.cleaner)
end

sorted(x, y) = x < y ? (x, y) : (y, x)

#TODO move everything that is subset-related to its own file or to axes.jl
#=
interpretsubset(subexpr::Union{CartesianIndices{1},LinearIndices{1}}, ax) =
subexpr.indices[1]
interpretsubset(subexpr::CartesianIndex{1}, ax) = subexpr.I[1]
interpretsubset(subexpr, ax) = axVal2Index(ax, subexpr, fuzzy=true)
function interpretsubset(subexpr::NTuple{2,Any}, ax)
x, y = sorted(subexpr...)
Colon()(sorted(axVal2Index_lb(ax, x), axVal2Index_ub(ax, y))...)
end
interpretsubset(subexpr::NTuple{2,Int}, ax::RangeAxis{T}) where {T<:TimeType} =
interpretsubset(map(T, subexpr), ax)
interpretsubset(subexpr::UnitRange{<:Integer}, ax::RangeAxis{T}) where {T<:TimeType} =
interpretsubset(T(first(subexpr)) .. T(last(subexpr) + 1), ax)
interpretsubset(subexpr::Interval, ax) = interpretsubset((subexpr.left, subexpr.right), ax)
interpretsubset(subexpr::AbstractVector, ax::CategoricalAxis) =
axVal2Index.(Ref(ax), subexpr, fuzzy=true)
=#

function _subsetcube(z, subs; kwargs...)
kwargs = Dict{Any,Any}(kwargs)
for f in YAXDefaults.subsetextensions
f(kwargs)
end
newaxes = deepcopy(collect(DD.Dimension, caxes(z)))
foreach(kwargs) do kw
axdes, subexpr = kw
axdes = string(axdes)
iax = findAxis(axdes, caxes(z))
if isa(iax, Nothing)
throw(ArgumentError("Axis $axdes not found in cube"))
else
oldax = newaxes[iax]
subinds = interpretsubset(subexpr, oldax)
subs2 = subs[iax][subinds]
subs[iax] = subs2
if !isa(subinds, AbstractVector) && !isa(subinds, AbstractRange)
newaxes[iax] = axcopy(oldax, oldax.values[subinds:subinds])
else
newaxes[iax] = axcopy(oldax, oldax.values[subinds])
end
end
end
substuple = ntuple(i -> subs[i], length(subs))
inewaxes = findall(i -> isa(i, AbstractVector), substuple)
newaxes = newaxes[inewaxes]
@assert length.(newaxes) ==
map(length, filter(i -> isa(i, AbstractVector), collect(substuple)))
newaxes, substuple
end


function Base.getindex(a::YAXArray, args::DD.Dimension...; kwargs...)
kwargsdict = Dict{Any,Any}(kwargs...)
Expand Down Expand Up @@ -502,10 +386,6 @@ end
cubesize(c::YAXArray{T}) where {T} = (sizeof(T)) * prod(map(length, caxes(c)))
cubesize(::YAXArray{T,0}) where {T} = sizeof(T)

getCubeDes(::DD.Dimension) = "Cube axis"
getCubeDes(::YAXArray) = "YAXArray"
getCubeDes(::Type{T}) where {T} = string(T)

loadingstatus(x) = "loaded in memory"
loadingstatus(x::DiskArrays.AbstractDiskArray) = "loaded lazily"

Expand Down
2 changes: 0 additions & 2 deletions src/DAT/DAT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ using Distributed:
import ..Cubes: cubechunks, iscompressed, chunkoffset, YAXArray, caxes
import ..Cubes: cubechunks, iscompressed, chunkoffset, YAXArray, caxes
import ..YAXArrays: findAxis, getOutAxis, getAxis
#import ..Cubes.Axes:
# AxisDescriptor, axname, ByInference, axsym, getOutAxis, getAxis, findAxis, match_axis
import ..Datasets: Dataset, createdataset
using ..YAXArrays: ByInference, YAXArrays
import ...YAXArrays.workdir
Expand Down
1 change: 0 additions & 1 deletion src/DAT/dciterators.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import YAXArrays.DAT: DATConfig
import YAXArrays.YAXTools: PickAxisArray
#using YAXArrays.Cubes.Axes: axcopy
using DiskArrays: GridChunks, AbstractDiskArray
using Tables: Tables, Schema, AbstractColumns

Expand Down
1 change: 0 additions & 1 deletion src/DAT/registration.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export InDims, OutDims, MovingWindow
#using ..Cubes.Axes: get_descriptor, findAxis, Axes
import ..YAXArrays: get_descriptor, findAxis, AxisDescriptor
using ..YAXArrays: YAXDefaults
using YAXArrayBase: yaxcreate
Expand Down
1 change: 0 additions & 1 deletion src/DAT/tablestats.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import OnlineStats: OnlineStat, Extrema, fit!, value, HistogramStat, Ash
#import ...Cubes.Axes: CategoricalAxis, RangeAxis
import IterTools
using WeightedOnlineStats
using Distributed: nworkers
Expand Down
4 changes: 1 addition & 3 deletions src/DatasetAPI/Datasets.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Datasets
#import ..Cubes.Axes: axsym, axname, CubeAxis, findAxis, CategoricalAxis, RangeAxis, caxes
import ..Cubes: Cubes, YAXArray, concatenatecubes, CleanMe, subsetcube, copy_diskarray, setchunks, caxes, readcubedata, cubesize, formatbytes
import ..Cubes: Cubes, YAXArray, concatenatecubes, CleanMe, copy_diskarray, setchunks, caxes, readcubedata, cubesize, formatbytes
using ...YAXArrays: YAXArrays, YAXDefaults, findAxis
using DataStructures: OrderedDict, counter
using Dates: Day, Hour, Minute, Second, Month, Year, Date, DateTime, TimeType, AbstractDateTime, Period
Expand Down Expand Up @@ -479,7 +478,6 @@ function open_dataset(g; skip_keys=(), driver = :all)
Dataset(allcubes, sdimlist,gatts)
end
end
#Base.getindex(x::Dataset; kwargs...) = subsetcube(x; kwargs...)
YAXDataset(; kwargs...) = Dataset(YAXArrays.YAXDefaults.cubedir[]; kwargs...)


Expand Down
108 changes: 0 additions & 108 deletions test/Cubes/axes.jl

This file was deleted.

25 changes: 1 addition & 24 deletions test/Cubes/cubes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ using DimensionalData
@test YAXArrayBase.iscompressed(a) == false
end


@testset "cubesize" begin
@test Cubes.cubesize(a) == 160
a32 = map(Float32, a)
Expand All @@ -104,28 +105,4 @@ using DimensionalData
@test endswith(Cubes.formatbytes(1205), "KB")
@test endswith(Cubes.formatbytes(1200000), "MB")
end
#=
@testset "Subsets" begin
s = YAXArrays.Cubes.subsetcube(a, X = 1.5..3.5)
@test s.data == [2 6 10 14 18; 3 7 11 15 19]
@test s.axes[1] == X(2.0:3.0)
@test s.axes[2] == Y([1, 2, 3, 4, 5])
ax = a.axes[1]
@test YAXArrays.Cubes.interpretsubset(CartesianIndices((1:2,)), ax) == 1:2
@test YAXArrays.Cubes.interpretsubset(CartesianIndex((2,)), ax) == 2
@test YAXArrays.Cubes.interpretsubset(2.1, ax) == 2
@test YAXArrays.Cubes.interpretsubset((3.5, 1.5), ax) == 2:3
@test YAXArrays.Cubes.interpretsubset(0.8..2.2, ax) == 1:2
tax = RangeAxis("ADate", Date(2001):Day(1):Date(2003, 2, 28))
@test YAXArrays.Cubes.interpretsubset((Date(2001, 1, 2), Date(2001, 1, 5)), tax) ==
2:4
@test YAXArrays.Cubes.interpretsubset(2001:2002, tax) == 1:730
@test YAXArrays.Cubes.interpretsubset([1, 3, 5], a.axes[2]) == [1, 3, 5]

s2 = a[X = 0.5..3.5, Y = [1, 5, 4]]
@test s2.data == [1 17 13; 2 18 14; 3 19 15]
@test s2.axes[1] == X(1.0:3.0)
@test s2.axes[2] == Y([1, 5, 4])
end
=#
end
3 changes: 0 additions & 3 deletions test/Datasets/datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,13 @@ end
@testset "collectdims" begin
dcollect = YAXArrays.Datasets.collectdims(m)
@test dcollect["time"].ax isa DD.Dimension
#@test YAXArrays.Cubes.Axes.axname(dcollect["time"].ax) == "time"
@test DD.lookup(dcollect["time"].ax) ==
DateTime(2001, 1, 4):Day(1):DateTime(2001, 1, 13)
@test dcollect["time"].offs == 2
@test dcollect["d2"].ax isa DD.Dimension
#@test YAXArrays.Cubes.Axes.axname(dcollect["d2"].ax) == "d2"
@test DD.lookup(dcollect["d2"].ax) == 0.1:0.1:0.5
@test dcollect["d2"].offs == 0
@test dcollect["d3"].ax isa DD.Dimension
#@test YAXArrays.Cubes.Axes.axname(dcollect["d3"].ax) == "d3"
@test DD.lookup(dcollect["d3"].ax) == ["One", "Two"]
@test dcollect["d3"].offs == 0
a1 = [0.1, 0.2, 0.3, 0.4]
Expand Down
Loading
Loading