#!/bin/bash
nx=1
ny=1
echo //////////////////////////////////////////////////
echo     Generating topography from GEBCO data
echo //////////////////////////////////////////////////
echo ' '

divagebcolog='./output/divagebco.log'
echo ////////////////////////////////////////////////// >$divagebcolog
echo     Generating topography from GEBCO data >>$divagebcolog
echo ////////////////////////////////////////////////// >>$divagebcolog
echo ' ' >>$divagebcolog
#
echo Cleaning up old files  >>$divagebcolog
echo ' ' >>$divagebcolog
rm -f ./divawork/fort.10
rm -f ./divawork/fort.12
rm -f ./divawork/fort.20
echo Finished cleaning  >>$divagebcolog
cp ./input/topo.gebco  ./divawork/fort.10

cd ./divawork

ndd=$(cat fort.10 | wc -l)
if [ "$1" != "-r" ] 
then
 if [ "$1" != "" ]
 then
 nx=$1
 fi
 if [ "$2" != "" ]
 then
 ny=$2
 fi
else
 if [ "$2" != "" ]
 then
 nx=$2
 fi
 if [ "$3" != "" ]
 then
 ny=$3
 fi
 
fi
echo ////////////////////////////////////////// >> ../output/diva.log
echo            Generating diva topography (.grd) >> ../output/diva.log
echo ////////////////////////////////////////// >> ../output/diva.log
echo ' '  >> ../output/diva.log

echo $ndd $nx $ny | ../../bin/gebco2diva.a >> ../output/diva.log
if [ $? -ne 0 ];then
echo ' '
echo --------------------------------------------
echo A problem was encountered during execution !
echo                gebco2diva.a
echo Check execution track
echo --------------------------------------------
echo ' ' >>../$divagebcolog
echo -------------------------------------------- >>../$divagebcolog
echo A problem was encountered during execution ! >>../$divagebcolog
echo                gebco2diva.a >>../$divagebcolog
echo Check execution track >>../$divagebcolog
echo -------------------------------------------- >>../$divagebcolog
echo ' ' >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
echo A problem was encountered during execution ! >> ../output/diva.log
echo                gebco2diva.a  >> ../output/diva.log
echo Check execution track >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
fi
 cp -f ../output/diva.log ../output/.
cd ..
mv ./divawork/fort.12 ./output/topo.grd
mv ./divawork/fort.20 ./output/TopoInfo.dat
rm ./divawork/fort.10
echo -----------------------
echo Topography is generated 
echo -----------------------

echo ----------------------- >>$divagebcolog
echo Topography is generated  >>$divagebcolog
echo ----------------------- >>$divagebcolog

ls -l ./output/topo.grd
if [ "$1" == "-r" ]
then
echo 'Moving topography files into ./input directory'
cp -v ./output/topo.grd ./input
cp -v ./output/TopoInfo.dat ./input
fi