cp.colr.sh

A couple of weeks ago I was struggling with copying of color tables in
Grass. I was a little disapointed since I didn't get a single reply.
Are you people out there not writing any scripts any more ?

Well I came to a final solution I think. Here is the script if someone
is interested. It has some modification compared to the earlier ones
so those you that have saved those could throw them away I think.

Lars

Lars Schylberg Email: larss@fmi.kth.se
Dept. of Geodesy and Photogrammetry
Royal Institute of Technology (KTH) Tel. +46 8 790 86 33
S-100 44 STOCKHOLM, SWEDEN Fax. +46 8 790 66 10

#!/bin/sh
#
# cp.colr.sh
#
# Script to copy one color table from one raster map to another raster map
#
# The script checks if there exists a colr2 file in current mapset
# for the input map and uses this file in that case, since this is the
# file that is used by d.rast, otherwise we use the colr file.
#
# Copies the colortable to: $LOCATION/colr2/mapset/$out_name unless the output
# map is in the current mapset and there exist no colr file for the output
# then it is written to $LOCATION/colr/$out_name.
#
# Read 4.0 Programmers Manual 12.10.3.1 for details about colr2 files
#
# Author: Lars Schylberg ( larss@fmi.kth.se )
# Department of Geodesy and Photogrammetry
# Royal Inst. of Technolgy
# Stockholm, Sweden
#
# Date: 930325
# History of changes:
#
#--------------------------------------------------------------------------
# Check if GRASS is running
#
test "$GISRC" || echo "GRASS is not running" || exit 2
#----------------------------------------------------------------------------
#
# Evaluate arguments
#
if [ $# != 2 ]
then
    echo
    echo Usage: `basename $0`
    echo ' input=mapname '
    echo ' output=mapname '
    echo
    exit 1
fi
#
# parse input arguments
#
for i do
  case $i in
    input*)
      IN=`echo $i | sed s/input=//` ;;
    in*)
      IN=`echo $i | sed s/in=//` ;;
    i*)
      IN=`echo $i | sed s/i=//` ;;
    output*)
      OUT=`echo $i | sed s/output=//` ;;
    out*)
      OUT=`echo $i | sed s/out=//` ;;
    o*)
      OUT=`echo $i | sed s/o=//` ;;
    *)
      echo ""
      echo "Unrecognized option: $i"
      echo 'Options: input=mapname '
      echo ' output=mapname '
                        echo
      exit 1
  esac
done
#-----------------------------------------------------------------------

# Check the input arguments

eval `g.findfile element=cell file=$OUT`
if [ ! "$file" ] ; then
   echo "$OUT - cell file not found"
   exit 2
fi

eval `g.findfile element=cell file=$IN`
if [ ! "$file" ] ; then
   echo "$IN - cell file not found"
   exit 2
fi

# Look for input colr file

eval `g.findfile element=colr file=$IN`
if [ ! "$file" ] ; then
   echo "$IN - color file not found"
   exit 2
fi

IN_FILE=$file
IN_MAPSET=$mapset

# Use only the first part of the file
# name since the file parameter requires that

IN_NAME=`echo $IN | awk -F@ '{ printf "%s\n", $1 }'`

# Check if the input map has a colr2 file in
# current mapset. Use this if it exists.
# Here we shouldn't use the mapset
# specification in the file parameter
# if one it is given on the command line

eval `g.findfile element=colr2/$IN_MAPSET file=$IN_NAME`
if [ "$file" ] ; then
   IN_FILE=$file
fi
#-----------------------------------------------------------------------------

# Check for existance of colr file and if output
# file is in current mapset.

OUT_NAME=`echo $OUT | awk -F@ '{ printf "%s\n", $1 }'`

# Here we shouldn't use the mapset
# specification in the file parameter
# if one it is given on the command line

eval `g.findfile element=cell file=$OUT`
OUT_MAPSET=$mapset

# Here we should use the mapset
# specification in the file parameter
# if one it is given on the command line

eval `g.findfile element=colr mapset=$OUT_MAPSET file=$OUT_NAME`

# case 1: no colr file but same mapset
# case 2: colr file exist, we don't want
# to overwrite or different mapsets

if [ ! "$file" -a "$OUT_MAPSET" = "$MAPSET" ] ; then
   OUT_FILE=$LOCATION/colr/$OUT_NAME
   SAMEMAPSET=1
else
   OUT_FILE=$LOCATION/colr2/$OUT_MAPSET/$OUT_NAME
   SAMEMAPSET=0
fi

# Make sure that output path exists
# if the output is in another mapset.

if [ ! -d $LOCATION/colr2 -a $SAMEMAPSET -eq 0 ] ; then
   echo "Creating: $LOCATION/colr2"
   mkdir $LOCATION/colr2
fi

if [ ! -d $LOCATION/colr2/$OUT_MAPSET -a $SAMEMAPSET -eq 0 ] ; then
   echo "Creating: $LOCATION/colr2/$OUT_MAPSET"
   mkdir $LOCATION/colr2/$OUT_MAPSET
fi

# Copy the color file

echo "Executing: cp $IN_FILE $OUT_FILE"
cp $IN_FILE $OUT_FILE