[GRASS-SVN] r34233 - grass-addons/raster/r.colors.quantiles

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Nov 10 09:52:58 EST 2008


Author: mathieug
Date: 2008-11-10 09:52:58 -0500 (Mon, 10 Nov 2008)
New Revision: 34233

Added:
   grass-addons/raster/r.colors.quantiles/r.colors.quantiles
Log:


Added: grass-addons/raster/r.colors.quantiles/r.colors.quantiles
===================================================================
--- grass-addons/raster/r.colors.quantiles/r.colors.quantiles	                        (rev 0)
+++ grass-addons/raster/r.colors.quantiles/r.colors.quantiles	2008-11-10 14:52:58 UTC (rev 34233)
@@ -0,0 +1,206 @@
+#!/bin/sh
+#
+############################################################################
+#
+# MODULE:       r.colors.quantiles
+# AUTHOR(S):	Mathieu Grelier (greliermathieu at gmail.com)
+# PURPOSE:	colouring from quantiles based color scheme file
+# REQUIREMENTS:
+# - R (http://www.r-project.org/) with spgrass6 package (http://cran.r-project.org/web/packages/spgrass6/index.html)
+# COPYRIGHT:	(C) 2008 Mathieu Grelier
+#
+#		This program is free software under the GNU General Public
+#		License (>=v2). Read the file COPYING that comes with GRASS
+#		for details.
+#
+#############################################################################
+
+
+#%Module
+#%  description: colouring from quantiles based color scheme file 
+#%  keywords: raster, colors, quantiles 
+#%End
+#%option
+#% key: rastername
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: input raster 
+#% required : yes
+#%end
+#%option
+#% key: nquantiles
+#% type: double
+#% description: how many quantiles
+#% required : yes
+#% options: 3,4,5
+#% answer: 4
+#%end
+#%flag
+#% key: k
+#% description: keep recode rules file
+#%end
+#%flag
+#% key: f
+#% description: flip colors 
+#%end
+
+if  [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program."
+ 	exit 1
+fi   
+
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+    exec g.parser "$0" "$@"
+fi
+
+## GRASS team recommandations
+#####################################
+
+PROG=`basename $0`
+
+## check if we have awk
+if [ ! -x "`which awk`" ] ; then
+    echo "$PROG: awk required, please install awk or gawk first"
+    exit 1
+fi
+
+## setting environment, so that awk works properly in all languages
+unset LC_ALL
+LC_NUMERIC=C
+export LC_NUMERIC
+
+## what to do in case of user break:
+exitprocedure()
+{
+    echo "User break!"
+    exit 1
+}
+## shell check for user break (signal list: trap -l)
+trap "exitprocedure" 2 3 15
+
+##Config and general procedures
+####################################
+
+##fix this path
+if [ -z "$PROCESSDIR" ] ; then
+	PROCESSDIR="$HOME"
+fi
+
+#fix this path
+if [ -z "$LOGDIR" ] ; then
+	LOGDIR="$HOME"
+fi
+LOGFILE="$LOGDIR/r.colors.quantiles.log"
+
+echo "r.colors.quantiles :" >> "$LOGFILE"
+
+
+## R script
+#####################################
+
+writeScript(){ 
+cat > $1 << "EOF"
+options(echo = FALSE)
+args <- commandArgs()
+##start at index 5 because first arguments are R options
+rastername <- args[5]
+quantilesbreaksfile <- args[6]
+nquantiles <- as.numeric(args[7])
+##libraries
+library(spgrass6)
+rasterdata <- readRAST6(c(rastername))
+##create correct string with this trick
+cat (paste("rasterdata$", as.character(rastername), sep=""), file="tmp") 
+s <- eval(parse(file = "tmp", n = 1))
+probas <- switch(nquantiles, NULL, NULL, c(0,0.33,0.67,1), c(0,0.25,0.5,0.75,1), c(0,0.2,0.4,0.6,0.8,1))
+#calculate breaks
+breaks<-quantile(s, probs = probas,na.rm=T,type = 3)
+write(breaks, file = quantilesbreaksfile, ncolumns = 6)
+unlink("tmp")
+EOF
+}
+
+## RGrass script generation
+#####################################
+RGRASSSCRIPT="`g.tempfile pid=$$`"
+if [ $? -ne 0 ] || [ -z "$RGRASSSCRIPT" ] ; then
+	echo "ERROR: unable to create temporary file for RGrass script" 1>&2
+    exit 1
+fi
+writeScript "$RGRASSSCRIPT"
+
+## RGrass call
+#####################################
+echo "calculating quantiles"
+##using R for nquantiles calculations
+
+R --vanilla --slave --args "$GIS_OPT_RASTERNAME" "$PROCESSDIR/quantilesbreaks" "$GIS_OPT_NQUANTILES" < "$RGRASSSCRIPT" >> "$LOGFILE" 2>&1
+if [ $? -ne 0 ] ; then
+	echo "ERROR: an error occurred during R script execution" 1>&2
+    exit 1
+fi
+
+##R script creates the quantilesbreaks file, with space separated quantiles values
+q1=$(awk -F " " '{print $1}' "quantilesbreaks")
+q2=$(awk -F " " '{print $2}' "quantilesbreaks")
+q3=$(awk -F " " '{print $3}' "quantilesbreaks")
+q4=$(awk -F " " '{print $4}' "quantilesbreaks")
+q5=$(awk -F " " '{print $5}' "quantilesbreaks")
+q6=$(awk -F " " '{print $6}' "quantilesbreaks")
+
+if [ "$GIS_OPT_NQUANTILES" -eq 3 ] && [ "$GIS_FLAG_F" -ne 1 ] ; then
+	##cyor
+	echo "$q1"" cyan" > quantiles
+	echo "$q2"" yellow" >> quantiles
+	echo "$q3"" orange" >> quantiles
+	echo "$q4"" red" >> quantiles
+elif [ "$GIS_OPT_NQUANTILES" -eq 3 ] && [ "$GIS_FLAG_F" -eq 1 ] ; then
+	##royc
+	echo "$q1"" red " > quantiles
+	echo "$q2"" orange" >> quantile
+	echo "$q3"" yellow" >> quantiles
+	echo "$q4"" cyan" >> quantiles
+fi
+
+if [ "$GIS_OPT_NQUANTILES" -eq 4 ] && [ "$GIS_FLAG_F" -ne 1 ] ; then
+	##bcyor
+	echo "$q1"" blue" > quantiles
+	echo "$q2"" cyan" >> quantiles
+	echo "$q3"" yellow" >> quantiles
+	echo "$q4"" orange" >> quantiles
+	echo "$q5"" red" >> quantiles
+elif [ "$GIS_OPT_NQUANTILES" -eq 4 ] && [ "$GIS_FLAG_F" -eq 1 ] ; then
+	##roycb
+	echo "$q1"" red " > quantiles
+	echo "$q2"" orange " >> quantiles
+	echo "$q3"" yellow" >> quantile
+	echo "$q4"" cyan" >> quantiles
+	echo "$q5"" blue" >> quantiles
+fi
+
+if [ "$GIS_OPT_NQUANTILES" -eq 5 ] && [ "$GIS_FLAG_F" -ne 1 ] ; then
+	##bcgyor
+	echo "$q1"" blue" > quantiles
+	echo "$q2"" cyan" >> quantiles
+	echo "$q3"" green" >> quantiles
+	echo "$q4"" yellow" >> quantiles
+	echo "$q5"" orange" >> quantiles
+	echo "$q6"" red" >> quantiles
+elif [ "$GIS_OPT_NQUANTILES" -eq 5 ] && [ "$GIS_FLAG_F" -eq 1 ] ; then
+	##roygcb
+	echo "$q1"" red" > quantiles
+	echo "$q2"" orange" >> quantiles
+	echo "$q3"" yellow" >> quantiles
+	echo "$q4"" green" >> quantiles
+	echo "$q5"" cyan" >> quantiles
+	echo "$q6"" blue" >> quantiles
+fi
+
+## raster colors
+#####################################
+echo "coloring raster"
+r.colors map="$GIS_OPT_RASTERNAME" color=rules < quantiles >> "$LOGFILE" 2>&1
+if [ "$GIS_FLAG_K" -ne 1 ] ; then
+	\rm -f quantiles
+fi
+echo "done"


Property changes on: grass-addons/raster/r.colors.quantiles/r.colors.quantiles
___________________________________________________________________
Name: svn:executable
   + *



More information about the grass-commit mailing list