From 2adb67c409c0b5454097ced51836c03bc55e794b Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Sun, 15 Dec 2024 18:48:02 +0100 Subject: [PATCH] add YAX --- README.md | 6 +++-- docs/src/UserGuide/combine.md | 12 ++++----- docs/src/UserGuide/compute.md | 46 +++++++++++++++++------------------ docs/src/UserGuide/create.md | 8 +++--- docs/src/UserGuide/faq.md | 12 ++++----- docs/src/get_started.md | 4 +-- src/dims.jl | 3 +-- test/Datasets/datasets.jl | 9 ++++--- 8 files changed, 51 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 9ff6ef55..dc1f1539 100644 --- a/README.md +++ b/README.md @@ -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"])) diff --git a/docs/src/UserGuide/combine.md b/docs/src/UserGuide/combine.md index 73bde3c5..33ee037b 100644 --- a/docs/src/UserGuide/combine.md +++ b/docs/src/UserGuide/combine.md @@ -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) ```` @@ -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) diff --git a/docs/src/UserGuide/compute.md b/docs/src/UserGuide/compute.md index 89229c31..3510b2c2 100644 --- a/docs/src/UserGuide/compute.md +++ b/docs/src/UserGuide/compute.md @@ -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") @@ -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 ```` @@ -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. @@ -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"]) ) @@ -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)) ```` @@ -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) @@ -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) @@ -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") diff --git a/docs/src/UserGuide/create.md b/docs/src/UserGuide/create.md index 835c7436..b2ee4771 100644 --- a/docs/src/UserGuide/create.md +++ b/docs/src/UserGuide/create.md @@ -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)) ```` @@ -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") diff --git a/docs/src/UserGuide/faq.md b/docs/src/UserGuide/faq.md index cd8d394e..4fc0f650 100644 --- a/docs/src/UserGuide/faq.md +++ b/docs/src/UserGuide/faq.md @@ -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) @@ -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()) @@ -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 diff --git a/docs/src/get_started.md b/docs/src/get_started.md index 7a885704..e3b4bf70 100644 --- a/docs/src/get_started.md +++ b/docs/src/get_started.md @@ -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)) ``` @@ -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"]) diff --git a/src/dims.jl b/src/dims.jl index cde346bd..10fb78da 100644 --- a/src/dims.jl +++ b/src/dims.jl @@ -24,5 +24,4 @@ export height, depth @dim depth ZDim @dim time TimeDim "time" -@dim Time TimeDim "time" - +@dim Time TimeDim "time" \ No newline at end of file diff --git a/test/Datasets/datasets.jl b/test/Datasets/datasets.jl index 21493d21..6d69d2b0 100644 --- a/test/Datasets/datasets.jl +++ b/test/Datasets/datasets.jl @@ -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)) @@ -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")