Skip to content

Commit 916f908

Browse files
add multidimensional spatial means tutorial (#860)
* add multidimensional spatial means tutorial * a couple of text changes * text changes in first half
1 parent f867e06 commit 916f908

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

docs/src/tutorials/spatial_mean.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ You can see here that cells are largest towards the equator, and smallest away f
8888

8989
## Computing the spatial mean
9090

91-
Now we can compute the average precipitation per square meter. First, we compute total precipitation per grid cell:
91+
Now we can compute the average precipitation per square meter. First, we compute total precipitation over each grid cell. (The units of this Raster will be m^2 * mm, which happens to be equal to liter.)
9292

9393
````@example cellarea
9494
precip_per_area = masked_precip .* masked_areas
9595
````
9696

97-
We can sum this to get the total precipitation per square meter across Chile:
97+
We can sum this to get the total precipitation across Chile:
9898

9999
````@example cellarea
100100
total_precip = sum(skipmissing(precip_per_area))
@@ -106,7 +106,7 @@ We can also sum the areas to get the total area of Chile (in this raster, at lea
106106
total_area = sum(skipmissing(masked_areas))
107107
````
108108

109-
And we can convert that to an average by dividing by the total area:
109+
And we can convert that to an average (in mm) by dividing by the total area:
110110

111111
````@example cellarea
112112
avg_precip = total_precip / total_area
@@ -141,3 +141,54 @@ We've also seen how to use the `cellarea` function to compute the area of each c
141141

142142
We've seen that the spatial mean is not the same as the arithmetic mean, and that we need to account for the area of each cell when computing the average.
143143

144+
## Bonus: Computing spatial means across dimensions
145+
146+
As a next step, we would like to know how precipitation will change in Chile until the end of the 21st century. To do this, we can use climate model outputs. This is a bit more complicated than calculating historical precipitation, because the forecast data can come from multiple climate models (GCMs), which each can be run under different socio-economic scenarios (SSPs). Here, we'll show how to use additional dimensions to keep track of this type of data.
147+
148+
To start, we define a simple function that takes an SSP (socioeconomic scenario) and a GCM (climate model) as input, and return the appropriate climate data.
149+
150+
````@example zonal
151+
using Dates
152+
getfutureprec(ssp, gcm) = Raster(WorldClim{Future{Climate, CMIP6, gcm, ssp}}, :prec, date = Date(2090))
153+
````
154+
155+
Rather than having a seperate Raster object for each combination of GCM and SSP, we will do our analysis on a single Raster, which will have `gcm` and `ssp` as additional dimensions. In total, our Raster will have four dimensions: X, Y, gcm, and ssp.
156+
157+
To accomplish this, we will leverage some tools from [DimensionalData](https://github.com/rafaqz/DimensionalData.jl), which is the package that underlies Rasters.jl. We start by defining two dimensions that correspond to the SSPs and GCMs we are interested in, then use the `@d` macro from [DimensionalData](https://github.com/rafaqz/DimensionalData.jl) to preserve these dimensions as we get the data, and then combine all Rasters into a single object using `Rasters.combine`.
158+
159+
````@example cellarea
160+
SSPs = Dim{:ssp}([SSP126, SSP370]) # SSP126 is a low-emission scenario, SSP370 is a high-emission scenario
161+
GCMs = Dim{:gcm}([GFDL_ESM4, IPSL_CM6A_LR]) # These are different general circulation (climate) models
162+
163+
precip_future = (@d getfutureprec.(SSPs, GCMs)) |> RasterSeries |> Rasters.combine
164+
````
165+
166+
Since the format of WorldClim's datasets for future climate is slightly different from the dataset for the historical period, this actually returned a 5-dimensional raster, with a `Band` dimension that represents months. Here we'll just select the 6th month, matching the selection above (but note that the analysis would also work for all Bands simultaneously). We will also replace the `NaN` missing value by the more standard `missing` using [`replace_missing`](@ref).
167+
168+
````@example cellarea
169+
precip_future = precip_future[Band = 6]
170+
precip_future = replace_missing(precip_future)
171+
````
172+
173+
On our 4-dimensional raster, functions like `crop` and `mask`, as well as broadcasting, will still work.
174+
175+
Here we repeat the procedure from above to mask out areas so we only have data for Chile, and then multiply by the cell area.
176+
177+
````@example cellarea
178+
masked_precip_future = mask(crop(precip_future; to = chile); with = chile)
179+
180+
precip_litres_future = masked_precip_future .* areas
181+
````
182+
183+
Now we calculate the average precipitation for each SSP and each GCM. Annoyingly, the future WorldClim doesn't have data for all land pixels, so we have to re-calculate the total area.
184+
185+
````@example cellarea
186+
masked_areas_future = mask(areas, with = masked_precip_future[ssp = 1, gcm = 1])
187+
total_area_f = sum(skipmissing(masked_areas_future))
188+
189+
avg_prec_future = map(eachslice(precip_litres_future; dims = (:ssp, :gcm))) do slice
190+
sum(skipmissing(slice)) / total_area_f
191+
end
192+
````
193+
194+
Which shows us that June rainfall in Chile will be slightly lower in the future, especially under the high-emission SSP370 scenario.

0 commit comments

Comments
 (0)