| 1 | # Shell script for converting GRASS-output DEM files into files that can be read by az* apps |
|---|
| 2 | # (ie squashes header onto one line) |
|---|
| 3 | # |
|---|
| 4 | # Author: Ben Taylor |
|---|
| 5 | # Date: 29/11/2007 |
|---|
| 6 | # |
|---|
| 7 | # 24/June/2008 mggr amended temporary filenames to make the script safe to run multiple instances |
|---|
| 8 | # 25/June/2008 benj Added adjustable resolution argument (no checking for wrong arguments though) |
|---|
| 9 | # |
|---|
| 10 | # Arguments: |
|---|
| 11 | # $1: Input file name |
|---|
| 12 | # $2: Output file name |
|---|
| 13 | # $3: -del to delete GRASS dem when done (default) or -nodel to leave it there |
|---|
| 14 | # $4: -res <resolution>, resolution of dem in metres (optional), Nextmap is 5m (default) |
|---|
| 15 | |
|---|
| 16 | RESOLUTION=5 #Res of grid (5 for Nextmap) |
|---|
| 17 | |
|---|
| 18 | if [ ! $2 ] ; then |
|---|
| 19 | echo "Not enough arguments. Usage: demheaderconvert.sh Input_File Output_File [-del|-nodel] [-res <resolution>]" |
|---|
| 20 | exit 1 |
|---|
| 21 | elif [ $3 ] ; then |
|---|
| 22 | if [ $3 == "-nodel" ] || [ $5 == "-nodel" ] ; then |
|---|
| 23 | NODELETEFILE=true |
|---|
| 24 | fi |
|---|
| 25 | |
|---|
| 26 | if [ $3 == "-res" ] ; then |
|---|
| 27 | RESOLUTION=$4 |
|---|
| 28 | elif [ $4 == "-res" ] ; then |
|---|
| 29 | RESOLUTION=$5 |
|---|
| 30 | fi |
|---|
| 31 | fi |
|---|
| 32 | |
|---|
| 33 | TEMPDIR=/tmp #Temp directory location - must exist |
|---|
| 34 | |
|---|
| 35 | #Temporary files |
|---|
| 36 | HEADERFILE=${TEMPDIR}/tmp_header-$PPID-`date +%s`.txt |
|---|
| 37 | LINEHEADER=${TEMPDIR}/tmp_lineheader-$PPID-`date +%s`.txt |
|---|
| 38 | TAILFILE=${TEMPDIR}/tmp_tail-$PPID-`date +%s`.txt |
|---|
| 39 | |
|---|
| 40 | FILEORDER=1 #1 for N->S, 0 for S->N |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | INPUTFILE=$1 |
|---|
| 44 | OUTPUTFILE=$2 |
|---|
| 45 | |
|---|
| 46 | # Split the input file - top six lines are the header info, rest are the DEM data |
|---|
| 47 | head -n 6 ${INPUTFILE} > ${HEADERFILE} |
|---|
| 48 | tail -n +7 ${INPUTFILE} > ${TAILFILE} |
|---|
| 49 | |
|---|
| 50 | # Run AWK script to extract data from header |
|---|
| 51 | awk '{ \ |
|---|
| 52 | if ($1 == "north:") NORTH=$2; \ |
|---|
| 53 | if ($1 == "south:") SOUTH=$2; \ |
|---|
| 54 | if ($1 == "east:") EAST=$2; \ |
|---|
| 55 | if ($1 == "west:") WEST=$2; \ |
|---|
| 56 | if ($1 == "rows:") ROWS=$2; \ |
|---|
| 57 | if ($1 == "cols:") COLS=$2; \ |
|---|
| 58 | } \ |
|---|
| 59 | END { printf "%s %s %s %s %s %s", COLS, ROWS, WEST, SOUTH, EAST, NORTH }' \ |
|---|
| 60 | ${HEADERFILE} > ${LINEHEADER} |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | # Put data together in output |
|---|
| 64 | echo ${FILEORDER} `cat ${LINEHEADER}` ${RESOLUTION} > ${OUTPUTFILE} |
|---|
| 65 | cat ${TAILFILE} >> ${OUTPUTFILE} |
|---|
| 66 | |
|---|
| 67 | # Delete original DEM file unless user opted not to (don't need it any more) |
|---|
| 68 | if [ ! $NODELETEFILE ] ; then |
|---|
| 69 | rm -f ${INPUTFILE} |
|---|
| 70 | fi |
|---|
| 71 | |
|---|
| 72 | # Clean up temporary files when done |
|---|
| 73 | rm -f ${HEADERFILE} |
|---|
| 74 | rm -f ${LINEHEADER} |
|---|
| 75 | rm -f ${TAILFILE} |
|---|