[GRASS-SVN] r54698 - in grass-addons/grass6/raster: . r.niche.similarity

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jan 18 03:39:21 PST 2013


Author: pvanbosgeo
Date: 2013-01-18 03:39:21 -0800 (Fri, 18 Jan 2013)
New Revision: 54698

Added:
   grass-addons/grass6/raster/r.niche.similarity/
   grass-addons/grass6/raster/r.niche.similarity/r.niche.similarity
   grass-addons/grass6/raster/r.niche.similarity/r.niche.similarity.html
Log:
New script to calculate similarity between two raster layers (niche similarity or overlap)

Added: grass-addons/grass6/raster/r.niche.similarity/r.niche.similarity
===================================================================
--- grass-addons/grass6/raster/r.niche.similarity/r.niche.similarity	                        (rev 0)
+++ grass-addons/grass6/raster/r.niche.similarity/r.niche.similarity	2013-01-18 11:39:21 UTC (rev 54698)
@@ -0,0 +1,227 @@
+#!/bin/sh
+# 
+#set -vx
+
+########################################################################
+# 
+# MODULE:       r.niche.similarity
+# AUTHOR(S):    Paulo van Breugel <p.vanbreugel AT gmail.com>
+# PURPOSE:      Compute  degree of niche overlap using the statistics D
+#               and I (as proposed by Warren et al., 2008) based on 
+#               Schoeners D (Schoener, 1968) and Hellinger Distances 
+#               (van der Vaart, 1998)
+#
+# COPYRIGHT: (C) 2013 Paulo van Breugel
+#            http://ecodiv.org
+#            http://pvanb.wordpress.com/
+# 
+#            This program is free software under the GNU General Public 
+#            License (>=v2). Read the file COPYING that comes with GRASS 
+#            for details. 
+# 
+########################################################################
+#
+#%Module 
+#% description: Computes niche overlap or similarity
+#%End 
+
+#%option
+#% key: maps
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: Suitability distribution maps
+#% key_desc: name
+#% required: yes
+#% multiple: yes
+#% guisection: Suitability distribution maps
+#%end
+
+#%option
+#% key: output_file
+#% type: string
+#% gisprompt: new
+#% description: Name of output text file
+#% key_desc: name
+#% required: no
+#%end
+
+#%flag
+#% key: i
+#% description: Calculate I niche similarity
+#% guisection: Statistics
+#%end
+
+#%flag
+#% key: d
+#% description: Calculate D niche similarity
+#% guisection: Statistics
+#%end
+
+
+#=======================================================================
+## GRASS team recommandations
+#=======================================================================
+
+## Check if in GRASS
+if  [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program." 1>&2
+    exit 1
+fi
+
+## check for awk
+if [ ! -x "$(which awk)" ] ; then
+    g.message -e "<awk> required, please install <awk> or <gawk> first"
+    exit 1
+fi
+
+## To parse the code into interactive menu
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+    exec g.parser "$0" "$@"
+fi
+
+## set environment so that awk works properly in all languages ##
+unset LC_ALL
+export LC_NUMERIC=C
+
+
+## what to do in case of user break:
+exitprocedure()
+{
+    echo "User break!"
+    cleanup
+    exit 1
+}
+
+## shell check for user break (signal list: trap -l)
+trap "exitprocedure" 2 3 15
+
+#=======================================================================
+## function
+#=======================================================================
+
+cleanup()
+{
+   g.mremove -f rast="rnicheOVERLAP$$_*" --quiet
+   g.mremove -f rast="rnicheEQUIV$$_*" --quiet
+}
+trap "cleanup" 2 3 15
+
+
+#=======================================================================
+## Check input
+#=======================================================================
+
+# for testing
+# GIS_OPT_MAPS="rast1 at test,rast2 at test,rast3 at test"
+INMAPS=${GIS_OPT_MAPS}
+# echo $INMAPS
+OPF=${GIS_OPT_OUTPUT_FILE}
+# Include check if the given file already exists?
+
+if [ $GIS_FLAG_I -eq 0 -a $GIS_FLAG_D -eq 0 ]; 
+then
+    g.message -e message='Please do not forget to select at least one of the statistics. Exiting'
+    exit 1
+fi
+
+NLAY=`echo $INMAPS | tr ',' '\n' | wc -l`
+if [ "$NLAY" -lt 2 ] ; then
+   g.message -e "More than one map is required."
+   exit 1
+fi
+
+#=======================================================================
+## Test if file exists and if not, write file with column headers
+#=======================================================================
+
+if [ -f "$OPF" ]; then
+   g.message -e "There is already a file $OPF
+   Please provide another name"
+   exit 1
+fi
+
+if [ -n "$OPF" ]; then 
+        echo "raster1:raster2:statistic:value" > "$OPF"
+fi
+
+#=======================================================================
+## Input for both D and I
+#=======================================================================
+
+TEMP1=`g.tempfile pid=$$`
+
+OIFS=$IFS
+IFS=,
+for nvar in ${INMAPS} ; do
+    tmp1=$(echo $nvar | awk 'BEGIN{FS="@"}{print $1}')
+    tmp2=`r.univar -g map=$tmp1 | awk -F\= '$1=="sum" { print $2 }'`
+    echo $nvar":"$tmp1":"$tmp2 >> $TEMP1
+done
+IFS=$OIFS
+
+
+i=1
+while [ $i -lt $NLAY ]
+do
+    j=$((i + 1))
+    SUM1=`awk 'NR=='$i $TEMP1 | awk -F: '{ print $3 }'`
+    NAME1=`awk 'NR=='$i $TEMP1 | awk -F: '{ print $2 }'`
+    MAP1=`awk 'NR=='$i $TEMP1 | awk -F: '{ print $1 }'`
+    
+    while [ $j -le $NLAY ]
+        do
+           SUM2=`awk 'NR=='$j $TEMP1 | awk -F: '{ print $3 }'`
+           NAME2=`awk 'NR=='$j $TEMP1 | awk -F: '{ print $2 }'`
+           MAP2=`awk 'NR=='$j $TEMP1 | awk -F: '{ print $1 }'`
+           
+               #=======================================================================
+               ## Calculate D (Schoener's 1968)
+               #=======================================================================
+
+               if [ $GIS_FLAG_D -eq 1 ] ; then
+                   TMPFILE1=rnicheOVERLAP$$_$$
+                   r.mapcalc "$TMPFILE1 = abs($MAP1/$SUM1 - $MAP2/$SUM2)"
+                   NO=`r.univar -g map=$TMPFILE1 | awk -F\= '$1=="sum" { print $2 }'`
+                   NOV=$(echo "1 - 0.5 * $NO" | bc)
+                   g.remove --q rast=$TMPFILE1
+                   if [ -n "$OPF" ] ; then 
+                       echo "$NAME1:$NAME2:D:$NOV" >> "$OPF"
+                   fi
+                   echo "niche overlap (D) of $NAME1 and $NAME2: $NOV"
+               fi
+           
+               #=======================================================================
+               ## Calculate I (Warren et al. 2008), but note that the original formulation
+               ## was corrected in erratum by Warren et al, using I = 1 - H^2 * 0.5
+               ## The sqrt in the H formulation and this new ^2 cancel each other out,
+               ## leading to the formulation used below.
+               #=======================================================================
+               
+               if [ $GIS_FLAG_I -eq 1 ] ; then
+                   TMPFILE2=rnicheEQUIV$$_$$
+                   r.mapcalc "$TMPFILE2 = (sqrt($MAP1/$SUM1) - sqrt($MAP2/$SUM2))^2"
+                   NE=`r.univar -g map=$TMPFILE2 | awk -F\= '$1=="sum" { print $2 }'`
+                   NEQ=$(echo "1 - (0.5 * $NE)" | bc)
+                   g.remove --q rast=$TMPFILE2
+                   if [ -n "$OPF" ] ; then 
+                       echo "$NAME1:$NAME2:I:$NEQ" >> "$OPF"
+                   fi
+                   echo "niche overlap (I) of $NAME1 and $NAME2: $NEQ"
+               fi
+               j=$((j + 1))
+        done
+     i=$((i + 1))
+done
+
+cleanup
+
+exit
+
+
+
+
+
+
+
+
+    

Added: grass-addons/grass6/raster/r.niche.similarity/r.niche.similarity.html
===================================================================
--- grass-addons/grass6/raster/r.niche.similarity/r.niche.similarity.html	                        (rev 0)
+++ grass-addons/grass6/raster/r.niche.similarity/r.niche.similarity.html	2013-01-18 11:39:21 UTC (rev 54698)
@@ -0,0 +1,29 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.niche.similarity</em> 
+
+<p>This module will compute two metrics to quantify niche similarity or overlap between all pairs of input raster layers.
+
+<p>One is the niche equivalency or similarity for two species following Warren et al. (2009) based on Schoeners D (Schoener, 1968).
+This metric ranges from 0 to 1, representing respectively no overlap and an identical distribution. 
+
+<p>The other is the niche overlap metric which indicates the niche overlap from predictions of species 
+distributions with the I similarity statistic of Warren et al. (2009), which is based on Hellinger Distances (van der Vaart,
+1998). Note that the corrected I similarity statistic, as given in an erratum of the article, was used, The statistic ranges from 0 (no overlap) to 1 (the distributions are identical). 
+
+<p>The I statistic is also implemented in ENMTools and a number of R packages, including phyloclim and dismo. I would like to thank Christoph Heibl, author of the phyloclim package, for pointing me out the corrected formulation of I.
+
+<h2>AUTHOR</h2>
+
+Contact: <a href="http://ecodiv.org/contact.html">Paulo van Breugel</a>
+
+<h2>REFERENCES</h2>
+<ul>
+<li>Warren, D. L., Glor, R. E., & Turelli, M. 2008. Environmental Niche Equivalency Versus Conservatism: Quantitative Approaches to Niche Evolution. Evolution 62(11): 2868–2883</li>
+<li>Warren, D. L., R. E. Glor, and M. Turelli. 2010. ENMTools: a toolbox for comparative studies of environmental niche models. Ecography 33:607–611.</li>
+<li>Robert J. Hijmans, Steven Phillips, John Leathwick and Jane Elith (2013). dismo: Species distribution
+  modeling. R package version 0.8-5. http://CRAN.R-project.org/package=dismo</li>
+<li>Christoph Heibl and Clement Calenge (2012). phyloclim: Integrating Phylogenetics and Climatic Niche
+  Modeling. R package version 0.9-0. http://CRAN.R-project.org/package=phyloclim</li>
+</ul>
+



More information about the grass-commit mailing list