#!/bin/bash
echo //////////////////////////////////////////
echo            Generating topography
echo //////////////////////////////////////////
echo ' ' 

divatopolog='./output/divatopo.log'

echo ////////////////////////////////////////// > $divatopolog
echo            Generating topography >> $divatopolog
echo ////////////////////////////////////////// >> $divatopolog
echo ' '  >> $divatopolog
echo Saving original input files >> $divatopolog
echo ' ' 
cp ./input/param.par ./input/param.par.ori
cp ./input/coast.cont ./input/coast.cont.ori
cp ./input/data.dat ./input/data.dat.ori
cp ./input/valatxyascii.anl ./input/valatxyascii.anl.ori
if [ -f ./input/topo.dat ]
then 
cp ./input/topo.dat ./input/data.dat
else
echo Need to provide topo.dat in ./input
fi
echo 0 0 > ./input/valatxyascii.anl
cp ./input/param.par ./divawork/fort.10
cd ./divawork
#
echo ////////////////////////////////////////// >> ../output/diva.log
echo            Generating topography >> ../output/diva.log
echo ////////////////////////////////////////// >> ../output/diva.log
echo ' '  >> ../output/diva.log
../../bin/topoprep.a >> ../output/diva.log
if [ $? -ne 0 ];then
echo ' '
echo --------------------------------------------
echo A problem was encountered during execution !
echo          divatopo      topoprep.a
echo Check execution track
echo --------------------------------------------
echo ' ' >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
echo A problem was encountered during execution ! >> ../output/diva.log
echo          divatopo      topoprep.a  >> ../output/diva.log
echo Check execution track >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
echo ' ' >> ../$divatopolog
echo -------------------------------------------- >> ../$divatopolog
echo A problem was encountered during execution ! >> ../$divatopolog
echo          divatopo      topoprep.a  >> ../$divatopolog
echo Check execution track >> ../$divatopolog
echo -------------------------------------------- >> ../$divatopolog
fi
cd ..
mv ./divawork/fort.11 ./input/param.par
mv ./divawork/fort.20 ./output/TopoInfo.dat
mv ./divawork/fort.30 ./input/coast.cont
divamesh
divacalc
if [ -f ./output/fieldgher.anl ] 
then
cp ./output/fieldgher.anl ./output/topo.grd
echo ' '
echo -----------------------
echo Topography is generated 
echo -----------------------
echo ' '
if [ "$1" == "-r" ]
then
echo 'Copying newly created topography into ./input' >> $divatopolog
cp -v ./output/topo.grd ./input
cp -v ./output/TopoInfo.dat ./input
fi
else
echo ' ' >> $divatopolog
echo -------------------------------------------- >> $divatopolog
echo A problem was encountered during execution ! >> $divatopolog
echo Check execution track >> $divatopolog
echo -------------------------------------------- >> $divatopolog
echo ' ' >> $divatopolog
echo ' '
echo --------------------------------------------
echo A problem was encountered during execution !
echo Check execution track
echo --------------------------------------------
echo ' '
fi
echo Restoring original input files
mv -v ./input/param.par.ori ./input/param.par
mv -v ./input/coast.cont.ori ./input/coast.cont 
mv -v ./input/data.dat.ori ./input/data.dat
mv -v ./input/valatxyascii.anl.ori ./input/valatxyascii.anl