From 1418ecb02c73a8d087f37389f3a1039f5471057d Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Tue, 10 Sep 2024 19:33:24 +0200 Subject: [PATCH 1/3] get back simpler mapCube example --- docs/src/.vitepress/config.mts | 2 +- docs/src/UserGuide/compute.md | 90 ++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/docs/src/.vitepress/config.mts b/docs/src/.vitepress/config.mts index b123b84b..12f573ca 100644 --- a/docs/src/.vitepress/config.mts +++ b/docs/src/.vitepress/config.mts @@ -92,7 +92,7 @@ export default defineConfig({ sidebar: [ { text: 'Get Started', link: '/get_started' }, - { text: 'API Reference', link: 'api' }, + { text: 'API Reference', link: '/api' }, { text: 'User Guide', items: [ diff --git a/docs/src/UserGuide/compute.md b/docs/src/UserGuide/compute.md index c7e98034..7b026cd0 100644 --- a/docs/src/UserGuide/compute.md +++ b/docs/src/UserGuide/compute.md @@ -97,6 +97,96 @@ mapslices(mean, a, dims=("lat", "lon")) `mapCube` is the most flexible way to apply a function over subsets of an array. Dimensions may be added or removed. +### Operations over several YAXArrays + +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 Dates +```` + +Define function in space and time + +````@example mapCube +f(lo, la, t) = (lo + la + Dates.dayofyear(t)) +```` + +now, `mapCube` requires this function to be wrapped as follows + +````@example mapCube +function g(xout, lo, la, t) + xout .= f.(lo, la, t) +end +```` + +::: tip +Note the `.` after `f`, this is because we will slice across time, namely, the function is broadcasted along this dimension. +::: + +Here, we do create `YAXArrays` only with the desired dimensions as + +````@ansi mapCube +lon = YAXArray(Dim{:lon}(range(1, 15))) +```` + +````@ansi mapCube +lat = YAXArray(Dim{:lat}(range(1, 10))) +```` + +And a time Cube's Axis + +````@ansi mapCube +tspan = Date("2022-01-01"):Day(1):Date("2022-01-30") +time = YAXArray(Dim{:time}(tspan)) +```` + +note that the following can be extended to arbitrary `YAXArrays` with additional data and dimensions. + +Let's generate a new `cube` using `mapCube` and saving the output directly into disk. + +````@ansi mapCube +gen_cube = mapCube(g, (lon, lat, time); + indims = (InDims(), InDims(), InDims("time")), + outdims = OutDims("time", overwrite=true, path="my_gen_cube.zarr", backend=:zarr, outtype=Float32) + # max_cache=1e9 +) +```` + +::: warning "time axis is first" +Note that currently the `time` axis in the output cube goes first. +::: + +Check that it is working + +````@ansi mapCube +gen_cube.data[1, :, :] +```` + +but, we can generate a another cube with a different `output order` as follows + +````@ansi mapCube +gen_cube = mapCube(g, (lon, lat, time); + indims = (InDims("lon"), InDims(), InDims()), + outdims = OutDims("lon", overwrite=true, path="my_gen_cube.zarr", backend=:zarr, outtype=Float32) + # max_cache=1e9 +) +```` + +::: info +Note that now the broadcasted dimension is `lon`. +::: + +we can see this by slicing on the last dimension now + +````@example mapCube +gen_cube.data[:, :, 1] +```` + +which outputs the same as the `gen_cube.data[1, :, :]` called above. + +### Creating a vector array + Here we transform a raster array with spatial dimension lat and lon into a vector array having just one spatial dimension i.e. region. First, create the raster array: From 81105f68f05e1f490c231994cb33f7485d31ec27 Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Tue, 10 Sep 2024 20:14:43 +0200 Subject: [PATCH 2/3] format --- docs/src/UserGuide/compute.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/src/UserGuide/compute.md b/docs/src/UserGuide/compute.md index 7b026cd0..958d9b62 100644 --- a/docs/src/UserGuide/compute.md +++ b/docs/src/UserGuide/compute.md @@ -121,9 +121,12 @@ end ```` ::: tip + Note the `.` after `f`, this is because we will slice across time, namely, the function is broadcasted along this dimension. + ::: + Here, we do create `YAXArrays` only with the desired dimensions as ````@ansi mapCube @@ -154,9 +157,12 @@ gen_cube = mapCube(g, (lon, lat, time); ```` ::: warning "time axis is first" + Note that currently the `time` axis in the output cube goes first. + ::: + Check that it is working ````@ansi mapCube @@ -174,7 +180,9 @@ gen_cube = mapCube(g, (lon, lat, time); ```` ::: info + Note that now the broadcasted dimension is `lon`. + ::: we can see this by slicing on the last dimension now From 9f20bdaeaa34fdacf6599f7e4687fd3a2969a279 Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Tue, 10 Sep 2024 20:35:49 +0200 Subject: [PATCH 3/3] more format --- docs/src/UserGuide/compute.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/docs/src/UserGuide/compute.md b/docs/src/UserGuide/compute.md index 958d9b62..89bc750e 100644 --- a/docs/src/UserGuide/compute.md +++ b/docs/src/UserGuide/compute.md @@ -120,7 +120,7 @@ function g(xout, lo, la, t) end ```` -::: tip +::: info Note the `.` after `f`, this is because we will slice across time, namely, the function is broadcasted along this dimension. @@ -131,15 +131,12 @@ Here, we do create `YAXArrays` only with the desired dimensions as ````@ansi mapCube lon = YAXArray(Dim{:lon}(range(1, 15))) -```` - -````@ansi mapCube lat = YAXArray(Dim{:lat}(range(1, 10))) ```` And a time Cube's Axis -````@ansi mapCube +````@example mapCube tspan = Date("2022-01-01"):Day(1):Date("2022-01-30") time = YAXArray(Dim{:time}(tspan)) ```` @@ -151,12 +148,13 @@ Let's generate a new `cube` using `mapCube` and saving the output directly into ````@ansi mapCube gen_cube = mapCube(g, (lon, lat, time); indims = (InDims(), InDims(), InDims("time")), - outdims = OutDims("time", overwrite=true, path="my_gen_cube.zarr", backend=:zarr, outtype=Float32) + outdims = OutDims("time", overwrite=true, path="my_gen_cube.zarr", backend=:zarr, + outtype = Float32) # max_cache=1e9 ) ```` -::: warning "time axis is first" +::: info "time axis goes first" Note that currently the `time` axis in the output cube goes first. @@ -174,7 +172,8 @@ but, we can generate a another cube with a different `output order` as follows ````@ansi mapCube gen_cube = mapCube(g, (lon, lat, time); indims = (InDims("lon"), InDims(), InDims()), - outdims = OutDims("lon", overwrite=true, path="my_gen_cube.zarr", backend=:zarr, outtype=Float32) + outdims = OutDims("lon", overwrite=true, path="my_gen_cube.zarr", backend=:zarr, + outtype = Float32) # max_cache=1e9 ) ````