Plot of temporal data sets in GRASS GIS

Author: Paulo van Breugel
Updated on: 2016-01-03


Since GRASS GIS 7.03 the temporal data processing framework (TGRASS) includes the function g.gui.tplot to plot the values of one or more temporal datasets for a specific point. It furthermore allows plotting data of vector dataset for a defined categories and attribute. In this post I'll use the tool to plot changes in the annual mean temperature and annual minimum temperature of the coldest month in the last century, using the CRU climate data set. First I'll provide a brief overview of how to import and prepare the data and finally how to plot a time series using the g.gui.tplot module.

The data

The Climate Research Unit (CRU) of the University of East Anglia provides a number of climate data sets that are free to use, provided the sources are acknowledged. Below I am using monthly climate data for the period 1901-2014 from the CRU TS (version 3.23), described by Harris et al. (2014).

The climate data can be downloaded as compressed NetCDF files. If you are not familiar with this format I suggest you first read more about how to import these files in a GRASS GIS database on the GRASS Wiki. For each climate variable (e.g., mean, minimum and maximum monthly temperature, and total monthly rainfall) the file contains a raster layer for every month in the period 1901-2014. After uncompressing the file, all these layers can be imported all in once using the command below (using as example the file with the mean monthly temperature).

# Unzip the files and import in grass
gunzip cru_ts3.23.1901.2014.tmp.dat.nc.gz
r.in.gdal -o \
input=NETCDF:"cru_ts3.23.1901.2014.tmp.dat.nc" output=tmp title="Mean monthly temperature"

The temporal dataset

After they are imported, the layers need to be registered in a temporal database. First step is to create this temporal database, which can be done using the t.create function. Below I am using the command line, but as usual, there is also a gui. Just go the the temporal menu and then to GUI tools, or type t.create on the command line, without further arguments.

t.create output=tmp semantictype=mean title='Mean monthly TMP' \
description='Mean monthly potential temperature' temporaltype=absolute

The names of the data layers for e.g., the mean temperature are tmp.1 to tmp.1368. This isn't a terribly informative, so it might be a good idea to rename them using a more convenient naming scheme. In the example below I am renaming them so they include the variable abbreviation, year and month (e.g., tmp_y2014m10). When that is done, the layers are registered in the temporal database. Given the large number of layers you probably want to write a small script to do this, like a shell or python script. I am doing this in R, just because I find it easier to work in.


# Rename the layers
maplist <- rep("NA",1368)
for(i in 1:114){
    for(j in 1:12){
        k <- k + 1
        input <- paste("tmp", k, sep=".")
        output <- paste("tmp_y", 1900 + i, "m", j, sep="")
        execGRASS("g.rename", raster=paste(input, output, sep=","))
        maplist[k] <- output

# Register the layers in a temporal dataset
execGRASS("t.register", flags="i", type="raster", input="tmpc", maps=maplist,
start="1901-01-01", increment="1 months")

Aggregation by year

It is very easy now to compute mean annual temperature or total annual rainfall data layers using the t.rast.aggregate function. This function aggregates temporally the maps of a space time raster dataset by a user defined granularity. As input it requires the name of the temporal database, the output name, granularity (one year in this case) and the aggregation method.

# Mean annual rainfall
g.region raster=pre_y1901m1
t.rast.aggregate -s input=prec@CRU output=prec_annual basename=prec granularity="1 years" method=sum file_limit=1500

# Average annual temperature
g.region raster=tmp_y1901m1
t.rast.aggregate -s input=temp@CRU output=temp_annual basename=temp granularity="1 years" method=average file_limit=1500

Plotting the data

Now we are finally ready to plot the data. You can open the gui by going to the temporal menu > GUI tools > Temporal plot tool or you can type g.gui.tplot on the command line or in the command console.

Open the g.gui.tplot function in the temporal menu

This will open a new window like the one below.

The GRASS GIS temporal plot tool

To plot a time series for the location of your interest, see the screenshot below. Select the temporal data set (1), click on the little arrow (2a) and then on the map (2b) to select your location of interest. This will automatically fill in the X and Y coordinates (3). You can alternatively fill in the coordinates manually. Now click on Draw (4) and the plot will be drawn (5)

You can plot multiple times series, pan and zoom and get information for specific points in time.

And of course you can save the plot as an image. The nice thing is that you cannot only save it as a regular image (png or jpg) but also as svg so you adjust the image to fit your needs in e.g., Inkscape.

Time series plot created with the GRASS GIS g.gui.tplot function and further edited in Inkscape


Software: @grassgis
Tools: @t_rast_aggregate @g_gui_tplot @t_register
Subject: @plotting @time_series