User Tools

Site Tools


wiki:grassrivarvariable

Calculation of stream-specific variables

GRASS GIS add-ons r.stream.watersheds & r.stream.variables

Script written by Sami Domisch, October 2015

Grass hydrological commands

https://grass.osgeo.org/grass70/manuals/topic_hydrology.html
https://grasswiki.osgeo.org/wiki/R.stream.*_modules

This example contains the following steps:

  • Download an exemplary digital elevation model (DEM)
  • Extract the stream network from the DEM
  • Delineate major drainage basins
  • Crop data to a sub-basin
  • Calculate the sub-watersheds for each stream grid cell (r.stream.watersheds)
  • Calculate contiguous stream-specific variables (r.stream.variables)
The add-ons have been tested in the Linux environment. Microsoft Windows users: consider installing the OSGEO-Live Virtual machine that has GRASS GIS 7 already installed and is ready to use.

Update April 2016: the add-ons are currently being tested under Windows, please check back later for more details

Create and enter the folder where the data will be stored:

 !#/bin/bash
 export INDIR=$HOME/grass_hydro
 mkdir $INDIR 
 cd $INDIR

Download and unzip a DEM from WorldClim (http://www.worldclim.org/tiles.php):

 wget -O  $INDIR/alt_16_tif.zip  "http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/tiles/cur/alt_16_tif.zip"
 unzip  -o $INDIR/alt_16_tif.zip  -d  $INDIR/dem
 gdalinfo $INDIR/dem/alt_16.tif        # check data

Create the GRASS GIS data base and enter GRASS:

 grass72  -text -c -e  $INDIR/dem/alt_16.tif  $INDIR/grass_location
 grass72 -text $INDIR/grass_location/PERMANENT  # enter GRASS

Read data into GRASS:

 r.in.gdal input=$INDIR/dem/alt_16.tif    output=elevation
 r.info elevation # check data

Open GUI and visualize the layers:

 g.gui wxpython

Run hydrological conditioning:

 g.extension  extension=r.hydrodem # download extension
 r.hydrodem  input=elevation  output=elevation_cond

Extract drainage direction and stream network

 r.watershed  --h  # see help regarding the options and flags
 r.watershed  elevation=elevation_cond  drainage=drainage   stream=stream  accumulation=accumulation  threshold=100  --o

Get drainage basins (last downstream segment: -l flag)

 g.extension  extension=r.stream.basins
 r.stream.basins  direction=drainage  stream_rast=stream  basins=basins_last  -l  --o

Categorize the single basins:

 r.clump -d input=basins_last  output=basins_cat  --o

Extract the data for one basin:

 r.mapcalc "drainage_sub = if (basins_cat==798, drainage, null() ) "  --o
 r.mapcalc "stream_sub = if (basins_cat==798, stream, null() ) "  --o

Write files to disk:

 r.out.gdal  input=drainage_sub   output=$INDIR/drainage_sub.tif  type=Int32  nodata=-9999  --o  -c    createopt="COMPRESS=LZW,ZLEVEL=9"
 r.out.gdal  input=stream_sub     output=$INDIR/stream_sub.tif    type=Int32  nodata=-9999  --o  -c    createopt="COMPRESS=LZW,ZLEVEL=9"

Create stream-specific variables, and limit the calculation only to a single basin.

Download and install the GRASS add-ons:

 g.extension  extension=r.stream.watersheds  # use work-around below
 g.extension  extension=r.stream.variables   # use work-around below

GRASS70; Work-around (October 2015):

 mkdir -p $HOME/.grass7/addons/scripts
 wget -O $HOME/.grass7/addons/scripts/r.stream.watersheds   "http://trac.osgeo.org/grass/export/66488/grass-addons/grass7/raster/r.stream.watersheds/r.stream.watersheds"
 chmod 777  $HOME/.grass7/addons/scripts/r.stream.watersheds
 wget -O $HOME/.grass7/addons/scripts/r.stream.variables  "https://www.dropbox.com/s/kdn71mxdnumfqm7/r.stream.variables"
 chmod 777  $HOME/.grass7/addons/scripts/r.stream.variables

GRASS72; Work-around (June 2017):*

 mkdir $HOME/.grass7/addons/scripts
 wget -O $HOME/.grass7/addons/scripts/r.stream.watersheds   "https://www.dropbox.com/s/0d68r615mvkr9ve/r.stream.watersheds"
 chmod 777  $HOME/.grass7/addons/scripts/r.stream.watersheds
 wget -O $HOME/.grass7/addons/scripts/r.stream.variables  "https://www.dropbox.com/s/a7e3irt9vngbul0/r.stream.variables"
 chmod 777  $HOME/.grass7/addons/scripts/r.stream.variables

https://www.dropbox.com/s/0d68r615mvkr9ve/r.stream.watersheds

Add-on 1: Calculate the sub-watershed and sub-stream sections for each stream grid cell using 2 processors:

 r.stream.watersheds stream=stream_sub  drainage=drainage_sub  cpu=2

Add-on 2: Create stream-specific variables based on elevation

 r.stream.variables  variable=elevation  output=cells,min,max,range,mean,stddev,coeff_var,sum  area=watershed  scale=1  cpu=2

Rename to the “elevation_cells” to “flow_accumulation” and check variables:

 r.mapcalc "flow_accummulation = elevation_cells"  --o
 r.info  flow_accummulation
 r.info  elevation_range

Get stream length (similar procedure as above):

 r.stream.variables  variable=elevation  output=cells  area=stream  scale=1  cpu=2
 r.mapcalc "stream_length = elevation_cells"  --o
 r.info  stream_length 

Calculate the amount of rainfall within the basin:

 wget -O  $INDIR/prec_16_tif.zip  "http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/tiles/cur/prec_16_tif.zip"
 unzip -q  -o $INDIR/prec_16_tif.zip  -d  $INDIR/prec
 r.in.gdal input=$INDIR/prec/prec6_16.tif   output=prec6   # rainfall in June
 r.stream.variables  variable=prec6  output=sum  area=watershed  scale=1  cpu=2

Run all 12 months in a loop:

 for var in $(seq 1 12); do
 r.in.gdal input=$INDIR/prec/prec${var}_16.tif   output=prec$var
 r.stream.variables  variable=prec$var  output=sum  area=watershed  scale=1  cpu=2
 done

Other useful add-ons for hydrological applications: http://grasswiki.osgeo.org/wiki/Hydrological_Sciences

Examples

Get the stream order:

 r.stream.order  stream_rast=stream_sub   direction=drainage_sub   strahler=str_order  --o

Get only streams and basins >=3rd order

 r.mapcalc "str_order_3rd = if(str_order >=3, str_order, null() ) "  --o
 r.stream.basins  direction=drainage_sub  stream_rast=str_order_3rd  basins=basins_3rd_last   -l  --o

Stream distance - Calculates distance to and elevation above streams and outlet

 g.extension extension=r.stream.distance
 r.stream.distance   stream_rast=stream_sub   direction=drainage_sub   elevation=elevation_cond  method=downstream   distance=str_distance    difference=str_elevation_diff  --o

Stream order statistics - Calculates Horton's statistics for Strahler and Horton ordered networks created with r.stream.order Print only order statistics:

 r.stream.stats  stream_rast=stream_sub   direction=drainage_sub   elevation=elevation_cond  output=str_stats_o.txt    -o   
 more $INDIR/str_stats_o.txt

Print only catchment statistics:

 r.stream.stats  stream_rast=stream_sub   direction=drainage_sub   elevation=elevation_cond  output=str_stats_c.txt    -c 
 more $INDIR/str_stats_c.txt
wiki/grassrivarvariable.txt · Last modified: 2017/06/08 09:06 by new_ost4sem_admin