[GRASS-SVN] r46733 - in grass-addons/raster: . r.stack

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Jun 18 02:38:35 EDT 2011


Author: hamish
Date: 2011-06-17 23:38:35 -0700 (Fri, 17 Jun 2011)
New Revision: 46733

Added:
   grass-addons/raster/r.stack/
   grass-addons/raster/r.stack/Makefile
   grass-addons/raster/r.stack/r.stack
Log:
new module to stack a bunch of input rasters vertically, for common analyses when group option has not been added to the desired processing module yet

Added: grass-addons/raster/r.stack/Makefile
===================================================================
--- grass-addons/raster/r.stack/Makefile	                        (rev 0)
+++ grass-addons/raster/r.stack/Makefile	2011-06-18 06:38:35 UTC (rev 46733)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.game_of_life
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script


Property changes on: grass-addons/raster/r.stack/Makefile
___________________________________________________________________
Added: svn:mime-type
   + text/x-makefile
Added: svn:eol-style
   + native

Added: grass-addons/raster/r.stack/r.stack
===================================================================
--- grass-addons/raster/r.stack/r.stack	                        (rev 0)
+++ grass-addons/raster/r.stack/r.stack	2011-06-18 06:38:35 UTC (rev 46733)
@@ -0,0 +1,146 @@
+#!/bin/sh
+
+############################################################################
+#
+# MODULE:       r.stack
+# AUTHOR(S):    M. Hamish Bowman, Dunedin, New Zealand
+# PURPOSE:      
+# COPYRIGHT:    (C) 2011 Hamish Bowman, and the GRASS Development Team
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+############################################################################
+#%Module
+#% description: Patch all raster maps in a time series into a vertical stack to aid mutli-map analyses.
+#% keywords: raster, series
+#%End
+#%Option
+#% key: input
+#% type: string
+#% required: yes
+#% multiple: yes
+#% key_desc: name
+#% description: Name of input raster maps
+#% gisprompt: old,cell,raster
+#%End
+#%Option
+#% key: output
+#% type: string
+#% required: yes
+#% multiple: yes
+#% key_desc: name
+#% description: Name for output raster map
+#% gisprompt: new,cell,raster
+#%End
+
+if [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program." 1>&2
+    exit 1
+fi
+
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+    exec g.parser "$0" "$@"
+fi
+
+
+cleanup()
+{
+   unset WIND_OVERRIDE
+   g.remove region="tmp_rstack.$$" --quiet
+   g.mremove -f rast="tmp_rstack_*.$$" --quiet
+}
+trap "cleanup" 2 3 15
+
+
+# check if we have awk
+if [ ! -x "`which awk`" ] ; then
+    g.message "awk required, please install awk or gawk first" 
+    exit 1
+fi
+
+# set environment so that awk works properly from all locales
+unset LC_ALL
+LC_NUMERIC=C
+export LC_NUMERIC
+
+
+NUM_INPUTS=`echo "$GIS_OPT_INPUT" | tr ',' '\n' | wc -l`
+if [ "$NUM_INPUTS" -lt 2 ] ; then
+   g.message -e "More than one map is required."
+   exit 1
+fi
+
+PROJ_TYPE=`g.region -p | grep '^projection:' | cut -f2 -d" "`
+if [ "$PROJ_TYPE" -eq 3 ] ; then
+   g.message -w "This module will not work in lat/lon locations if the stack exceeds geographic limits."
+fi
+
+
+# calc new bounds (south)
+eval `g.region -g`
+eval `g.region -eg`
+NEW_SOUTH=`echo "$s $ns_extent $NUM_INPUTS" | awk '{print $1 - ($2 * ($3 - 1))}'`
+
+# if s<-90, exit with error.
+if [ "$PROJ_TYPE" -eq 3 ] && [ `echo "$NEW_SOUTH" | cut -f1 -d'.'` -le -90 ] ; then
+   g.message -e "Requested stack spans beyond geographic limits. (Can't go south of 90 degrees)"
+   exit 1
+fi
+
+
+# setup internal region
+g.region save="tmp_rstack.$$"
+WIND_OVERRIDE="tmp_rstack.$$"
+export WIND_OVERRIDE
+
+g.region s="$NEW_SOUTH"
+
+#g.copy maps>1 to temporary maps, and set their bounds
+g.message "Prepatation ..."
+NUM=0
+ORIG_IFS="$IFS"
+IFS=,
+for MAP in $GIS_OPT_INPUT ; do
+   NUM=`expr $NUM + 1`
+   if [ "$NUM" = 1 ] ; then
+      continue
+   fi
+
+   NUMSTR=`echo $NUM | awk '{printf("%.05d", $1)}'`
+   TMP_MAP="tmp_rstack_$NUMSTR.$$"
+
+   g.copy "$MAP,$TMP_MAP" --quiet
+
+   NEW_NORTH=`echo "$s $ns_extent $NUM" | awk '{print $1 - ($2 * ($3 - 2))}'`
+   NEW_SOUTH=`echo "$s $ns_extent $NUM" | awk '{print $1 - ($2 * ($3 - 1))}'`
+   r.region map="$TMP_MAP" n="$NEW_NORTH" s="$NEW_SOUTH" --quiet
+done
+IFS="$ORIG_IFS"
+
+FIRST_MAP=`echo "$GIS_OPT_INPUT" | cut -f1 -d','`
+TEMP_MAPS=`g.mlist type=rast pattern="tmp_rstack_*.$$" sep=,`
+
+g.message "Patching ..."
+
+# todo?: if number of maps,rows is excessive, perhaps it is more efficient to
+#  build up by chunks, then patch the chunks together.
+#  Empty rows are skipped over quickly though. (this is why the stack is vertical)
+r.patch input="$FIRST_MAP,$TEMP_MAPS" output="$GIS_OPT_OUTPUT"
+
+# r.patch uses the color map from the first map, which is unwanted here.
+#r.colors -r "$GIS_OPT_OUTPUT"
+# try for colors which best fit all input maps:
+r.colors -e "$GIS_OPT_OUTPUT" color=rainbow
+
+cleanup
+
+g.message "Done."
+exit


Property changes on: grass-addons/raster/r.stack/r.stack
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-sh
Added: svn:eol-style
   + native



More information about the grass-commit mailing list