"t.rast.extract",
gs.run_command(input="lst_daily",
="lst_daily_summer",
output="strftime('%m',start_time)='06' or strftime('%m',start_time)='07' or strftime('%m', start_time)='08'") where
Time series subset, import and export
In this seventh time series tutorial, we will go through time series subset, import and export.
This tutorial can be run locally or in Google Colab. However, make sure you install GRASS 8.4+, download the LST sample dataset and set up your project as explained in the first time series tutorial.
Subset
For extracting a subset from a STRDS, we use t.rast.extract. The subset is based on temporal variables like start_time
, start_doy
, end_week
, etc. This tool outputs a new STRDS and offers the possibility to apply a mapcalc operation on the fly. If no r.mapcalc expression is defined, the selected maps are simply registered in the newly created output STRDS to avoid data duplication.
Let’s see a couple of examples. Suppose we are only interested in northern summer months, i.e., June, July, August. The maps matching the where condition will be registered in the new output STRDS.
If you remember, we went through several listing examples in the Time series management and visualization tutorial. To check the subset will actually give us what we want, we can run t.rast.list with the same where
condition as above.
"t.rast.list",
gs.run_command(input="lst_daily_summer",
="name,min,max") columns
To check whether a map is registered in different time series objects, we can use t.info.
"t.info",
gs.run_command(input="lst_2018.243_avg",
type="raster")
Let’s see now an example including a mapcalc operation. We still want the daily maps of summer, but we are only interested in areas where LST was higher than 25 degrees. Note that in this second case, we are creating a new STRDS, i.e., we modify the original with the mapcalc expression, so we need to provide basename
and suffix
for the newly created maps.
"t.rast.extract",
gs.run_command(input="lst_daily",
="lst_daily_summer_higher_25",
output="lst_daily",
basename="gran",
suffix="strftime('%m',start_time)='06' or strftime('%m',start_time)='07' or strftime('%m', start_time)='08'",
where="if(lst_daily < 25.0, null(), lst_daily)") expression
Check that the minimum value of all extracted maps are actually above 25 degrees.
"t.rast.list",
gs.run_command(input="lst_daily_summer_higher_25",
="name,min,max") columns
What tools would you use now if you wanted to know how many summer days had temperatures above 25 degrees each year and how does the maximum number of days with LST > 25 varies regionally?
Small hint? Go back to the time series aggregation tutorial.
# Get number of summer days with LST > 25 per year
"t.rast.aggregate",
gs.run_command(input="lst_daily_summer_higher_25",
="count_lst_daily_summer_higher_25",
output="count_lst_daily_summer_higher_25",
basename="gran",
suffix="count",
method="1 year") granularity
# Get maximum number of days with LST > 25
"t.rast.series",
gs.run_command(input="count_lst_daily_summer_higher_25",
="max_count_summer_days_lst_higher_25",
output="maximum") method
# Mask zero values
"r.mask",
gs.run_command(="max_count_summer_days_lst_higher_25",
raster=0,
maskcats="i") flags
# Visualize the result
= gj.InteractiveMap(tiles="CartoDB.DarkMatter")
max_count "max_count_summer_days_lst_higher_25")
max_count.add_raster( max_count.show()
Export
There are three tools to export raster time series in different formats.
- t.rast.export exports a strds as a tar archive containing raster maps either as GeoTIFF or GRASS binary files and several metadata files such as: timestamps, CRS info, strds and raster info. The archive can be compressed with gzip or bzip2. The output of t.rast.export can then be imported with t.rast.import.
- t.rast.out.vtk exports a strds as a VTK file to be visualized with any VTK visualizer.
- t.rast.out.xyz exports a strds to a CSV file of the form x coord, y coord, value.
Let’s see some examples.
# Export strds as an archive
"t.rast.export",
gs.run_command(input="lst_daily_summer",
="lst_daily_summer_2014.tar.bzip2",
output="start_time < '2015-01-01'") where
!tar tjf lst_daily_summer_2014.tar.bzip2
lst_2014.152_avg.tif
lst_2014.152_avg.color
...
lst_2014.243_avg.tif
lst_2014.243_avg.color
list.txt
proj.txt
init.txt
readme.txt
metadata.txt
# Export strds as VTK
"t.rast.out.vtk",
gs.run_command(input="count_lst_daily_summer_higher_25",
="/tmp",
directory="elevation") elevation
The tool t.rast.out.xyz is an addon, so we first need to install it with g.extension.
# Install extension
"g.extension", extension="t.rast.out.xyz") gs.run_command(
# Export strds as xyz CSV file
"t.rast.out.xyz",
gs.run_command(="count_lst_daily_summer_higher_25",
strds="count_lst_daily_summer_higher_25.csv") output
!head count_lst_daily_summer_higher_25.csv
Import
There are two tools to import raster time series into GRASS:
- t.rast.import imports strds that were exported with t.rast.export. It allows to create a new GRASS project with the imported data CRS and set the computational region to the raster maps imported.
- t.rast.import.netcdf imports the content of one or more netCDF files that adhere to the CF convention into a strds. Data can be imported or linked via r.external.
Let’s see an example with t.rast.import.
# Import the exported strds into a new GRASS project
"t.rast.import",
gs.run_command(input="lst_daily_summer_2014.tar.bzip2",
="lst_daily_summer_new",
output="Daily summer LST",
title="Daily summer LST for 2014") description
# Check the new strds was created
"t.list", where="NAME LIKE '%summer%'") gs.run_command(
While not covered in this tutorial, there are also dedicated tools for subsetting, importing and exporting vector time series objects. These are:
References
- Gebbert, S., Pebesma, E. 2014. TGRASS: A temporal GIS for field based environmental modeling. Environmental Modelling & Software 53, 1-12. DOI.
- Gebbert, S., Pebesma, E. 2017. The GRASS GIS temporal framework. International Journal of Geographical Information Science 31, 1273-1292. DOI.
- Temporal data processing wiki page.
The development of this tutorial was funded by the US National Science Foundation (NSF), award 2303651.