Skip to content

Commit

Permalink
add YAX
Browse files Browse the repository at this point in the history
  • Loading branch information
lazarusA committed Dec 15, 2024
1 parent 7c99072 commit 2adb67c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 49 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ using YAXArrays
Let's assemble a `YAXArray` with 4 dimensions i.e. time, x,y and a variable dimension with two variables.

```julia
using YAXArrays, DimensionalData
using YAXArrays: YAXArrays as YAX, YAXArrays
using DimensionalData

axlist = (
Dim{:time}(range(1, 20, length=20)),
YAX.time(range(1, 20, length=20)),
X(range(1, 10, length=10)),
Y(range(1, 5, length=15)),
Dim{:Variable}(["var1", "var2"]))
Expand Down
12 changes: 6 additions & 6 deletions docs/src/UserGuide/combine.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ We glue the arrays along the first dimension using `dims = 1`:
The resulting array `whole_year` still has one dimension, i.e. time, but with 12 instead of 6 elements.

````@example cat
using YAXArrays
using YAXArrays: YAXArrays as YAX, YAXArrays
first_half = YAXArray((Dim{:time}(1:6),), rand(6))
second_half = YAXArray((Dim{:time}(7:12),), rand(6))
first_half = YAXArray((YAX.time(1:6),), rand(6))
second_half = YAXArray((YAX.time(7:12),), rand(6))
whole_year = cat(first_half, second_half, dims = 1)
````

Expand All @@ -24,10 +24,10 @@ The resulting array `combined` has an additional dimension `variable` indicating
Note that using a `Dataset` instead is a more flexible approach in handling different variables.

````@example concatenatecubes
using YAXArrays
using YAXArrays: YAXArrays as YAX, YAXArrays
temperature = YAXArray((Dim{:time}(1:6),), rand(6))
precipitation = YAXArray((Dim{:time}(1:6),), rand(6))
temperature = YAXArray((YAX.time(1:6),), rand(6))
precipitation = YAXArray((YAX.time(1:6),), rand(6))
cubes = [temperature,precipitation]
var_axis = Dim{:variable}(["temp", "prep"])
combined = concatenatecubes(cubes, var_axis)
Expand Down
46 changes: 23 additions & 23 deletions docs/src/UserGuide/compute.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ This section describes how to create new YAXArrays by performing operations on t
Let's start by creating an example dataset:

````@example compute
using YAXArrays
using YAXArrays: YAXArrays as YAX, YAXArrays
using Dates
axlist = (
Dim{:time}(Date("2022-01-01"):Day(1):Date("2022-01-30")),
Dim{:lon}(range(1, 10, length=10)),
Dim{:lat}(range(1, 5, length=15)),
YAX.time(Date("2022-01-01"):Day(1):Date("2022-01-30")),
lon(range(1, 10, length=10)),
lat(range(1, 5, length=15)),
)
data = rand(30, 10, 15)
properties = Dict(:origin => "user guide")
Expand Down Expand Up @@ -102,7 +102,7 @@ Dimensions may be added or removed.
Here, we will define a simple function, that will take as input several `YAXArrays`. But first, let's load the necessary packages.

````@example mapCube
using YAXArrays, Zarr
using YAXArrays: YAXArrays as YAX, YAXArrays, Zarr
using Dates
````

Expand Down Expand Up @@ -130,15 +130,15 @@ Note the `.` after `f`, this is because we will slice across time, namely, the f
Here, we do create `YAXArrays` only with the desired dimensions as

````@ansi mapCube
lon = YAXArray(Dim{:lon}(range(1, 15)))
lat = YAXArray(Dim{:lat}(range(1, 10)))
lon = YAXArray(lon(range(1, 15)))
lat = YAXArray(lat(range(1, 10)))
````

And a time Cube's Axis

````@example mapCube
tspan = Date("2022-01-01"):Day(1):Date("2022-01-30")
time = YAXArray(Dim{:time}(tspan))
time = YAXArray(YAX.time(tspan))
````

note that the following can be extended to arbitrary `YAXArrays` with additional data and dimensions.
Expand Down Expand Up @@ -197,14 +197,14 @@ which outputs the same as the `gen_cube.data[1, :, :]` called above.
Here, we will consider different scenarios, namely how we deal with different input cubes and how to specify the output ones. We will illustrate this with the following test example and the subsequent function definitions.

````@example outdims
using YAXArrays, Dates
using YAXArrays: YAXArrays as YAX, YAXArrays, Dates
using Zarr
using Random
axlist = (
Dim{:time}(Date("2022-01-01"):Day(1):Date("2022-01-05")),
Dim{:lon}(range(1, 4, length=4)),
Dim{:lat}(range(1, 3, length=3)),
YAX.time(Date("2022-01-01"):Day(1):Date("2022-01-05")),
lon(range(1, 4, length=4)),
lat(range(1, 3, length=3)),
Dim{:variables}(["a", "b"])
)
Expand Down Expand Up @@ -308,7 +308,7 @@ Here, the goal is to operate at the pixel level (longitude, latitude), and then
Random.seed!(123)
data = rand(3.0:5.0, 5, 4, 3)
axlist = (Dim{:lon}(1:4), Dim{:lat}(1:3), Dim{:depth}(1:7),)
axlist = (lon(1:4), lat(1:3), Dim{:depth}(1:7),)
yax_2d = YAXArray(axlist, rand(-3.0:0.0, 4, 3, 7))
````

Expand All @@ -318,8 +318,8 @@ and
Random.seed!(123)
data = rand(3.0:5.0, 5, 4, 3)
axlist = (Dim{:time}(Date("2022-01-01"):Day(1):Date("2022-01-05")),
Dim{:lon}(1:4), Dim{:lat}(1:3),)
axlist = (YAX.time(Date("2022-01-01"):Day(1):Date("2022-01-05")),
lon(1:4), lat(1:3),)
properties = Dict("description" => "multi dimensional test cube")
yax_test = YAXArray(axlist, data, properties)
Expand Down Expand Up @@ -358,14 +358,14 @@ Here we transform a raster array with spatial dimension lat and lon into a vecto
First, create the raster array:

````@example compute_mapcube
using YAXArrays
using YAXArrays: YAXArrays as YAX, YAXArrays
using DimensionalData
using Dates
axlist = (
Dim{:time}(Date("2022-01-01"):Day(1):Date("2022-01-30")),
Dim{:lon}(range(1, 10, length=10)),
Dim{:lat}(range(1, 5, length=15)),
YAX.time(Date("2022-01-01"):Day(1):Date("2022-01-30")),
lon(range(1, 10, length=10)),
lat(range(1, 5, length=15)),
)
data = rand(30, 10, 15)
raster_arr = YAXArray(axlist, data)
Expand Down Expand Up @@ -431,14 +431,14 @@ For example, we can execute each date of a time series in a different CPU thread
The following code does a time mean over all grid points using multiple CPUs of a local machine:

````julia
using YAXArrays
using YAXArrays: YAXArrays as YAX, YAXArrays
using Dates
using Distributed

axlist = (
Dim{:time}(Date("2022-01-01"):Day(1):Date("2022-01-30")),
Dim{:lon}(range(1, 10, length=10)),
Dim{:lat}(range(1, 5, length=15)),
YAX.time(Date("2022-01-01"):Day(1):Date("2022-01-30")),
lon(range(1, 10, length=10)),
lat(range(1, 5, length=15)),
)
data = rand(30, 10, 15)
properties = Dict(:origin => "user guide")
Expand Down
8 changes: 4 additions & 4 deletions docs/src/UserGuide/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This section describes how to create arrays and datasets by filling values direc
We can create a new YAXArray by filling the values directly:

````@example create
using YAXArrays
using YAXArrays: YAXArrays as YAX, YAXArrays
a1 = YAXArray(rand(10, 20, 5))
````

Expand All @@ -18,9 +18,9 @@ We can also specify the dimensions with custom names enabling easier access:
using Dates
axlist = (
Dim{:time}(Date("2022-01-01"):Day(1):Date("2022-01-30")),
Dim{:lon}(range(1, 10, length=10)),
Dim{:lat}(range(1, 5, length=15)),
YAX.time(Date("2022-01-01"):Day(1):Date("2022-01-30")),
lon(range(1, 10, length=10)),
lat(range(1, 5, length=15)),
)
data2 = rand(30, 10, 15)
properties = Dict(:origin => "user guide")
Expand Down
12 changes: 6 additions & 6 deletions docs/src/UserGuide/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ It is possible to concatenate several cubes that shared the same dimensions usin

Let's create two dummy cubes
````@example howdoi
using YAXArrays
using YAXArrays: YAXArrays as YAX, YAXArrays
axlist = (
Dim{:time}(range(1, 20, length=20)),
Dim{:lon}(range(1, 10, length=10)),
Dim{:lat}(range(1, 5, length=15))
YAX.time(range(1, 20, length=20)),
lon(range(1, 10, length=10)),
lat(range(1, 5, length=15))
)
data1 = rand(20, 10, 15)
Expand Down Expand Up @@ -324,7 +324,7 @@ You will not be able to save this dataset, first you will need to rename those `
In this section we will use `MarketData.jl` and `TimeSeries.jl` to simulate some stocks.

````@example howdoi
using YAXArrays, DimensionalData
using YAXArrays: YAXArrays as YAX, YAXArrays, DimensionalData
using MarketData, TimeSeries
stocks = Dict(:Stock1 => random_ohlcv(), :Stock2 => random_ohlcv(), :Stock3 => random_ohlcv())
Expand All @@ -334,7 +334,7 @@ d_keys = keys(stocks)
currently there is not direct support to obtain `dims` from a `TimeArray`, but we can code a function for it

````@example howdoi
getTArrayAxes(ta::TimeArray) = (Dim{:time}(timestamp(ta)), Dim{:variable}(colnames(ta)), );
getTArrayAxes(ta::TimeArray) = (YAX.time(timestamp(ta)), Dim{:variable}(colnames(ta)), );
nothing # hide
````
then, we create the `YAXArrays` as
Expand Down
4 changes: 2 additions & 2 deletions docs/src/get_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Pkg; Pkg.add("YAXArrays")
Create a simple array from random numbers given the size of each dimension or axis:

```@example quickstart
using YAXArrays
using YAXArrays: YAXArrays as YAX, YAXArrays
a = YAXArray(rand(2,3))
```
Expand All @@ -31,7 +31,7 @@ using DimensionalData
# axes or dimensions with name and tick values
axlist = (
Dim{:time}(range(1, 20, length=20)),
YAX.time(range(1, 20, length=20)),
X(range(1, 10, length=10)),
Y(range(1, 5, length=15)),
Dim{:variable}(["temperature", "precipitation"])
Expand Down
3 changes: 1 addition & 2 deletions src/dims.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ export height, depth
@dim depth ZDim

@dim time TimeDim "time"
@dim Time TimeDim "time"

@dim Time TimeDim "time"
9 changes: 5 additions & 4 deletions test/Datasets/datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ end
end

@testset "Saving, OutDims" begin
using YAXArrays, Zarr, NetCDF, ArchGDAL
using YAXArrays: YAXArrays as YAX, YAXArrays
using Zarr, NetCDF, ArchGDAL
using Dates

flolat(lo, la, t) = (lo + la + Dates.dayofyear(t))
Expand All @@ -430,10 +431,10 @@ end
xout .= flola.(lo, la)
end

lon = YAXArray(Dim{:lon}(range(1, 15)))
lat = YAXArray(Dim{:lat}(range(1, 10)))
lon = YAXArray(lon(range(1, 15)))
lat = YAXArray(lat(range(1, 10)))
tspan = Date("2022-01-01"):Day(1):Date("2022-01-30")
time = YAXArray(Dim{:time}(tspan))
time = YAXArray(YAX.time(tspan))

properties = Dict{String, Any}("name" => "out_array")

Expand Down

0 comments on commit 2adb67c

Please sign in to comment.