From ba58a677ddc36a4286c41174d96f0cf715c1a285 Mon Sep 17 00:00:00 2001 From: Lina Estupinan Date: Fri, 30 Jun 2023 12:15:22 +0200 Subject: [PATCH 1/2] examples for how do i --- docs/examples/HowdoI/howdoi.jl | 36 +++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/examples/HowdoI/howdoi.jl b/docs/examples/HowdoI/howdoi.jl index 93ef18e1..838bb2a9 100644 --- a/docs/examples/HowdoI/howdoi.jl +++ b/docs/examples/HowdoI/howdoi.jl @@ -39,7 +39,7 @@ dsfinal = concatenatecubes([ds1, ds2], dsfinal -# ## Subsetting a Cube +# ## How do I subset a Cube? # Let's start by creating a dummy cube @@ -66,12 +66,42 @@ ctime3 = c[Time=Date(2021-01-05)..Date(2021-01-12)] clonlat = c[Lon=1..5, Lat=5..10] # check even numbers range, it is ommiting them -# ## Applying map algebra +# ## How do I apply map algebra? # Our next step is map algebra computations. This can be done effectively using the 'map' function. For example: -## cubes with only spatio-temporal dimensions +## multiplying cubes with only spatio-temporal dimensions map((x,y)->x*y, ds1, ds2) ## cubes with more than 3 dimensions map((x,y)->x*y, dsfinal[Variables="Var1"], dsfinal[Variables="Var2"]) +# To add some complexity, we will multiply each value for π and then divided for the sum of each time step. We will use the `ds1` cube for this purpose. +mapslices(ds1, dims=("Lon","Lat")) do xin + (xin*π) ./ maximum(skipmissing(xin)) +end + +# ## How do I use the CubeTable function? +# The function "CubeTable" creates an iterable table and the result is a DataCube. It is therefore very handy for grouping data and computing statistics by class. It uses `OnlineStats.jl` to calculate statistics, and weighted statistics can be calculated as well. + +# Here we will use the `ds1` Cube defined previously and we create a mask for data classification. + +## cube containing a mask with classes 1, 2 and 3 +classes = YAXArray([getAxis("lon", dsfinal), getAxis("lat", dsfinal)], rand(1:3, 10, 15)) + +using CairoMakie +# This is how our classification map looks like +heatmap(classes[:,:]) + +# Now we define the input cubes that will be considered for the iterable table +t = CubeTable(values=ds1, classes=classes) + +using DataFrames +using OnlineStats +## visualiztion of the CubeTable +DataFrame(t[1]) + +# In this line we calculate the `Mean` for each class +fitcube = cubefittable(t, Mean, :values, by=(:classes)) + +# We can also use more than one criteria for grouping the values. In the next example, the mean is calculated for each class and timestep. +fitcube = cubefittable(t, Mean, :values, by=(:classes,:time)) From 5f21916825dcbb8d71977d98016ad254de77b055 Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Sat, 1 Jul 2023 12:36:16 +0200 Subject: [PATCH 2/2] test new example --- docs/Project.toml | 1 + docs/examples/HowdoI/howdoi.jl | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 5a684292..94f25fa4 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -18,6 +18,7 @@ Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" MakieTeX = "6d554a22-29e7-47bd-aee5-0c5f06619414" MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411" NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9" +OnlineStats = "a15396b6-48d5-5d58-9928-6d29437db91e" PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043" PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" diff --git a/docs/examples/HowdoI/howdoi.jl b/docs/examples/HowdoI/howdoi.jl index 55fc5cbb..18d41993 100644 --- a/docs/examples/HowdoI/howdoi.jl +++ b/docs/examples/HowdoI/howdoi.jl @@ -6,7 +6,7 @@ # ## Extract the axes names from a Cube using YAXArrays -c = YAXArray(rand(10,10,5)) +c = YAXArray(rand(10, 10, 5)) caxes(c) @@ -21,7 +21,7 @@ collect(getAxis("Dim_1", c).values) collect(c.axes[1].values) ## to collect data from a cube works exactly the same as doing it from an array -c[:,:,1] +c[:, :, 1] @@ -46,7 +46,7 @@ ds2 = YAXArray(axlist, data2) # Now we can concatenate ```ds1``` and ```ds2``` cubes: -dsfinal = concatenatecubes([ds1, ds2], +dsfinal = concatenatecubes([ds1, ds2], CategoricalAxis("Variables", ["var1", "var2"])) dsfinal @@ -58,13 +58,13 @@ dsfinal ## define the time span of the cube using Dates -t = Date("2020-01-01"):Month(1):Date("2022-12-31") +t = Date("2020-01-01"):Month(1):Date("2022-12-31") ## create cube axes axes = [RangeAxis("Lon", 1:10), RangeAxis("Lat", 1:10), RangeAxis("Time", t)] ## assign values to a cube -c = YAXArray(axes, reshape(1:3600, (10,10,36))) +c = YAXArray(axes, reshape(1:3600, (10, 10, 36))) # Now we subset the cube by any dimension @@ -72,25 +72,25 @@ c = YAXArray(axes, reshape(1:3600, (10,10,36))) ctime = c[Time=2021:2022] ## subset cube by a specific date and date range -ctime2 = c[Time=Date(2021-01-05)] -ctime3 = c[Time=Date(2021-01-05)..Date(2021-01-12)] +ctime2 = c[Time=Date(2021 - 01 - 05)] +ctime3 = c[Time=Date(2021 - 01 - 05) .. Date(2021 - 01 - 12)] ## subset cube by longitude and latitude -clonlat = c[Lon=1..5, Lat=5..10] # check even numbers range, it is ommiting them +clonlat = c[Lon=1 .. 5, Lat=5 .. 10] # check even numbers range, it is ommiting them # ## How do I apply map algebra? # Our next step is map algebra computations. This can be done effectively using the 'map' function. For example: ## multiplying cubes with only spatio-temporal dimensions -map((x,y)->x*y, ds1, ds2) +map((x, y) -> x * y, ds1, ds2) ## cubes with more than 3 dimensions -map((x,y)->x*y, dsfinal[Variables="Var1"], dsfinal[Variables="Var2"]) +map((x, y) -> x * y, dsfinal[Variables="Var1"], dsfinal[Variables="Var2"]) # To add some complexity, we will multiply each value for π and then divided for the sum of each time step. We will use the `ds1` cube for this purpose. -mapslices(ds1, dims=("Lon","Lat")) do xin - (xin*π) ./ maximum(skipmissing(xin)) +mapslices(ds1, dims=("Lon", "Lat")) do xin + (xin * π) ./ maximum(skipmissing(xin)) end # ## How do I use the CubeTable function? @@ -102,8 +102,9 @@ end classes = YAXArray([getAxis("lon", dsfinal), getAxis("lat", dsfinal)], rand(1:3, 10, 15)) using CairoMakie +CairoMakie.activate!() # This is how our classification map looks like -heatmap(classes[:,:]) +heatmap(classes[:, :]) # Now we define the input cubes that will be considered for the iterable table t = CubeTable(values=ds1, classes=classes) @@ -117,4 +118,4 @@ DataFrame(t[1]) fitcube = cubefittable(t, Mean, :values, by=(:classes)) # We can also use more than one criteria for grouping the values. In the next example, the mean is calculated for each class and timestep. -fitcube = cubefittable(t, Mean, :values, by=(:classes,:time)) +fitcube = cubefittable(t, Mean, :values, by=(:classes, :time))