Skip to content

Commit 59d6c85

Browse files
committed
Add imgs, fix typos, remove non-working examples
1 parent 5ea64d8 commit 59d6c85

File tree

3 files changed

+29
-72
lines changed

3 files changed

+29
-72
lines changed
270 KB
Loading
69.8 KB
Loading

content/tutorials/time_series/time_series_algebra.qmd

Lines changed: 29 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Time series algebra"
33
author: "Veronica Andreo"
44
date: 2024-07-24
55
date-modified: today
6-
image: images/trento.png
6+
image: images/month_of_max_lst.png
77
lightbox: true
88
format:
99
ipynb: default
@@ -103,14 +103,14 @@ import subprocess
103103
104104
# Ask GRASS where its Python packages are
105105
sys.path.append(
106-
subprocess.check_output([grassbin, "--config", "python_path"], text=True).strip()
106+
subprocess.check_output(["grass", "--config", "python_path"], text=True).strip()
107107
)
108108
109109
# Import the GRASS packages we need
110110
import grass.script as gs
111111
import grass.jupyter as gj
112112
113-
path_to_project = "eu_laea/italy_LST_daily"
113+
path_to_project = "italy_eu_laea/italy_LST_daily"
114114
115115
# Start the GRASS session
116116
session = gj.init(path_to_project);
@@ -137,7 +137,8 @@ for me in methods:
137137
gs.run_command("t.rast.series",
138138
input="lst_daily",
139139
method=me,
140-
output=f"lst_{me}")
140+
output=f"lst_{me}",
141+
nprocs=6)
141142
```
142143

143144
and then the annual LST average:
@@ -185,6 +186,12 @@ anomalies.d_legend(color="black", at=(5, 50, 2, 6))
185186
anomalies.show()
186187
```
187188

189+
```{python}
190+
# Save animation
191+
anomalies.save("anomalies.gif")
192+
```
193+
194+
![Animation of standardized anomalies](images/anomalies.gif)
188195

189196
## Month with maximum LST
190197

@@ -220,7 +227,7 @@ gs.run_command("t.rast.mapcalc",
220227

221228
```{python}
222229
# Get basic info
223-
print(gs.read_command("t.info", input="month_min_lst"))
230+
print(gs.read_command("t.info", input="month_max_lst"))
224231
```
225232

226233
Now we can aggregate our sparse time series to obtain the earliest month in
@@ -231,7 +238,8 @@ which the maximum LST occurred.
231238
gs.run_command("t.rast.series",
232239
input="month_max_lst",
233240
method="minimum",
234-
output="max_lst_date")
241+
output="max_lst_date",
242+
nprocs=6)
235243
```
236244

237245
We remove the intermediate time series as we were only interested in the
@@ -258,66 +266,15 @@ Let's display the result using the `Map` class from `grass.jupyter` package:
258266
```{python}
259267
mm = gj.Map()
260268
mm.d_rast(map="max_lst_date")
261-
mm.d_vect(map="italy", type="boundary", color="#4D4D4D", width=2)
262-
mm.d_legend(raster="max_lst_date", title="Month", fontsize=10, at=(2,15,2,10))
263-
mm.d_barscale(length=50, units="kilometers", segment=4, fontsize=14, at=(73,7))
264-
mm.d_northarrow(at=(90,15))
269+
mm.d_vect(map="italy_borders_0", type="boundary", color="#4D4D4D", width=2)
270+
mm.d_legend(raster="max_lst_date", title="Month", fontsize=12, at=(2, 35, 1, 20), border_color="white", flags="b")
271+
mm.d_barscale(length=50, units="kilometers", segment=4, fontsize=14, at=(80, 8))
272+
mm.d_northarrow(at=(95, 19))
265273
mm.d_text(text="Month of maximum LST", color="black", font="sans", size=4, bgcolor="white")
266274
mm.show()
267275
```
268276

269-
::: {.callout-caution title="Question"}
270-
How could we have done the same in one single step?
271-
Hint: `t.rast.algebra ... method=during`.
272-
273-
```{python}
274-
# Get time series with month of maximum LST
275-
expression="month_max_lst = if({during}, lst_daily == tmap(lst_maximum), start_month(), null())"
276-
277-
gs.run_command("t.rast.algebra",
278-
basename="month_max_lst",
279-
expression=expression,
280-
suffix="gran",
281-
nprocs=6)
282-
```
283-
284-
Is it really the same?
285-
286-
```{python}
287-
expression="result = max_lst_date - month_max_lst"
288-
gs.rast_mapcalc(exp=expression)
289-
gs.raster_info("result")
290-
```
291-
292-
:::
293-
294-
What if we are interested in knowing the week number in which the annual maximum
295-
LST occurs each year? How would you do that? Can we also know if there's any
296-
trend in the week number? i.e., does it tend to happen earlier or later?
297-
298-
```{python}
299-
# Estimate annual max LST
300-
gs.run_command("t.rast.aggregate",
301-
input="lst_daily",
302-
output="lst_max_annual",
303-
basename="lst_max",
304-
method="maximum",
305-
granularity="1 years",
306-
suffix=gran)
307-
308-
# Week number
309-
exp="week_lst_max_annual = if({contains}, lst_max_annual == lst_daily, start_week(lst_daily))"
310-
gs.run_command("t.rast.algebra",
311-
expression=exp,
312-
basename="week_lst_max_annual",
313-
suffix="gran")
314-
315-
# Is there a trend?
316-
gs.run_command("t.rast.series",
317-
input="week_lst_max_annual",
318-
method="slope",
319-
output="slope_week_lst_max_annual")
320-
```
277+
![Month of maximum LST value](images/month_of_max_lst.png)
321278

322279

323280
## Number of days with LST >= 20 and <= 30
@@ -335,17 +292,17 @@ within this range per year, and then, we'll estimate the median along years.
335292
expression="lst_higher20_lower30 = if(lst_daily >= 20.0 && lst_daily <= 30.0, 1, null())"
336293
337294
gs.run_command("t.rast.algebra",
338-
expression=expression,
295+
expression=expression,
339296
basename="lst_higher20_lower30",
340297
suffix="gran",
341-
nproc=6,
298+
nproc=6,
342299
flags="n")
343300
```
344301

345302
```{python}
346303
# Count how many times per year the condition is met
347304
gs.run_command("t.rast.aggregate",
348-
input="lst_higher20_lower30",
305+
input="lst_higher20_lower30",
349306
output="count_lst_higher20_lower30",
350307
basename="count_lst_higher20_lower30",
351308
suffix="gran",
@@ -355,23 +312,23 @@ gs.run_command("t.rast.aggregate",
355312

356313
```{python}
357314
# Check raster maps in the STRDS
358-
gs.run_command("t.rast.list",
359-
input="count_lst_higher20_lower30",
315+
gs.run_command("t.rast.list",
316+
input="count_lst_higher20_lower30",
360317
columns="name,start_time,min,max")
361318
```
362319

363320
```{python}
364321
# Median number of days with average lst >= 20 and <= 30
365322
gs.run_command("t.rast.series",
366-
input="count_tmean_higher20_lower30",
367-
output="median_count_tmean_higher20_lower30",
323+
input="count_lst_higher20_lower30",
324+
output="median_count_lst_higher20_lower30",
368325
method="median")
369326
```
370327

371328
```{python}
372329
# Display raster map with interactive class
373330
h20_map = gj.InteractiveMap()
374-
h20_map.add_raster("median_count_tmean_higher20_lower30")
331+
h20_map.add_raster("median_count_lst_higher20_lower30")
375332
h20_map.show()
376333
```
377334

@@ -383,7 +340,7 @@ is changing along the years?
383340
## Number of consecutive days with LST <= -10.0
384341

385342
Likewise, there are temperature thresholds that mark a limit to insects survival.
386-
Here, we'll use the lowest temperature threshold to survival. Most importantly,
343+
Here, we'll use the lowest temperature threshold. Most importantly,
387344
we we'll count the number of consecutive days with temperatures below this
388345
threshold.
389346

@@ -428,7 +385,7 @@ gs.run_command("t.rast.algebra",
428385
expression=expression,
429386
basename="lower_m10",
430387
suffix="gran",
431-
nproc=7)
388+
nproc=6)
432389
```
433390

434391
```{python}

0 commit comments

Comments
 (0)