Skip to content

Commit fa679c3

Browse files
authored
Merge pull request #274 from JuliaDataCubes/lina_slices_table
Lina slices table
2 parents 08f1d8f + 5f21916 commit fa679c3

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
1818
MakieTeX = "6d554a22-29e7-47bd-aee5-0c5f06619414"
1919
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
2020
NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9"
21+
OnlineStats = "a15396b6-48d5-5d58-9928-6d29437db91e"
2122
PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043"
2223
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
2324
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"

docs/examples/HowdoI/howdoi.jl

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# ## Extract the axes names from a Cube
77

88
using YAXArrays
9-
c = YAXArray(rand(10,10,5))
9+
c = YAXArray(rand(10, 10, 5))
1010

1111
caxes(c)
1212

@@ -21,7 +21,7 @@ collect(getAxis("Dim_1", c).values)
2121
collect(c.axes[1].values)
2222

2323
## to collect data from a cube works exactly the same as doing it from an array
24-
c[:,:,1]
24+
c[:, :, 1]
2525

2626

2727

@@ -46,45 +46,76 @@ ds2 = YAXArray(axlist, data2)
4646

4747
# Now we can concatenate ```ds1``` and ```ds2``` cubes:
4848

49-
dsfinal = concatenatecubes([ds1, ds2],
49+
dsfinal = concatenatecubes([ds1, ds2],
5050
CategoricalAxis("Variables", ["var1", "var2"]))
5151

5252
dsfinal
5353

5454

55-
# ## Subsetting a Cube
55+
# ## How do I subset a Cube?
5656

5757
# Let's start by creating a dummy cube
5858

5959
## define the time span of the cube
6060
using Dates
61-
t = Date("2020-01-01"):Month(1):Date("2022-12-31")
61+
t = Date("2020-01-01"):Month(1):Date("2022-12-31")
6262

6363
## create cube axes
6464
axes = [RangeAxis("Lon", 1:10), RangeAxis("Lat", 1:10), RangeAxis("Time", t)]
6565

6666
## assign values to a cube
67-
c = YAXArray(axes, reshape(1:3600, (10,10,36)))
67+
c = YAXArray(axes, reshape(1:3600, (10, 10, 36)))
6868

6969
# Now we subset the cube by any dimension
7070

7171
## subset cube by years
7272
ctime = c[Time=2021:2022]
7373

7474
## subset cube by a specific date and date range
75-
ctime2 = c[Time=Date(2021-01-05)]
76-
ctime3 = c[Time=Date(2021-01-05)..Date(2021-01-12)]
75+
ctime2 = c[Time=Date(2021 - 01 - 05)]
76+
ctime3 = c[Time=Date(2021 - 01 - 05) .. Date(2021 - 01 - 12)]
7777

7878
## subset cube by longitude and latitude
79-
clonlat = c[Lon=1..5, Lat=5..10] # check even numbers range, it is ommiting them
79+
clonlat = c[Lon=1 .. 5, Lat=5 .. 10] # check even numbers range, it is ommiting them
8080

8181

82-
# ## Applying map algebra
82+
# ## How do I apply map algebra?
8383
# Our next step is map algebra computations. This can be done effectively using the 'map' function. For example:
8484

85-
## cubes with only spatio-temporal dimensions
86-
map((x,y)->x*y, ds1, ds2)
85+
## multiplying cubes with only spatio-temporal dimensions
86+
map((x, y) -> x * y, ds1, ds2)
8787

8888
## cubes with more than 3 dimensions
89-
map((x,y)->x*y, dsfinal[Variables="Var1"], dsfinal[Variables="Var2"])
89+
map((x, y) -> x * y, dsfinal[Variables="Var1"], dsfinal[Variables="Var2"])
9090

91+
# 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.
92+
mapslices(ds1, dims=("Lon", "Lat")) do xin
93+
(xin * π) ./ maximum(skipmissing(xin))
94+
end
95+
96+
# ## How do I use the CubeTable function?
97+
# 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.
98+
99+
# Here we will use the `ds1` Cube defined previously and we create a mask for data classification.
100+
101+
## cube containing a mask with classes 1, 2 and 3
102+
classes = YAXArray([getAxis("lon", dsfinal), getAxis("lat", dsfinal)], rand(1:3, 10, 15))
103+
104+
using CairoMakie
105+
CairoMakie.activate!()
106+
# This is how our classification map looks like
107+
heatmap(classes[:, :])
108+
109+
# Now we define the input cubes that will be considered for the iterable table
110+
t = CubeTable(values=ds1, classes=classes)
111+
112+
using DataFrames
113+
using OnlineStats
114+
## visualiztion of the CubeTable
115+
DataFrame(t[1])
116+
117+
# In this line we calculate the `Mean` for each class
118+
fitcube = cubefittable(t, Mean, :values, by=(:classes))
119+
120+
# 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.
121+
fitcube = cubefittable(t, Mean, :values, by=(:classes, :time))

0 commit comments

Comments
 (0)