Processing/NextMapDEMs: demheaderconvert.sh

File demheaderconvert.sh, 2.1 KB (added by benj, 11 years ago)

DEM header conversion script

Line 
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
16RESOLUTION=5 #Res of grid (5 for Nextmap)
17
18if [ ! $2 ] ; then
19        echo "Not enough arguments. Usage: demheaderconvert.sh Input_File Output_File [-del|-nodel] [-res <resolution>]"
20        exit 1
21elif [ $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
31fi
32
33TEMPDIR=/tmp #Temp directory location - must exist
34
35#Temporary files
36HEADERFILE=${TEMPDIR}/tmp_header-$PPID-`date +%s`.txt
37LINEHEADER=${TEMPDIR}/tmp_lineheader-$PPID-`date +%s`.txt
38TAILFILE=${TEMPDIR}/tmp_tail-$PPID-`date +%s`.txt
39
40FILEORDER=1 #1 for N->S, 0 for S->N
41
42
43INPUTFILE=$1
44OUTPUTFILE=$2
45
46# Split the input file - top six lines are the header info, rest are the DEM data
47head -n 6 ${INPUTFILE} > ${HEADERFILE}
48tail -n +7 ${INPUTFILE} > ${TAILFILE}
49
50# Run AWK script to extract data from header
51awk '{ \
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
64echo ${FILEORDER} `cat ${LINEHEADER}` ${RESOLUTION} > ${OUTPUTFILE}
65cat ${TAILFILE} >> ${OUTPUTFILE}
66
67# Delete original DEM file unless user opted not to (don't need it any more)
68if [ ! $NODELETEFILE ] ; then
69        rm -f ${INPUTFILE}
70fi
71
72# Clean up temporary files when done
73rm -f ${HEADERFILE}
74rm -f ${LINEHEADER}
75rm -f ${TAILFILE}