#!/bin/bash
#
##########################################
if [ -d ./3DWORK ];then
 rm -rf ./3DWORK/*
else
 mkdir ./3DWORK
fi
#
  Fileinf=./input/STBinfo
{
read comment
read psal
read comment
read temp
read comment
read lay1
read comment
read lay2
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read maxit
} < $Fileinf
#
if [ -f  ./input/3Dinfo ]
then
rm  ./input/3Dinfo
fi
if [ -f ./input/3Dinfosalinity ]
then
cp ./input/3Dinfosalinity ./input/3Dinfo
else
echo 'DIVAERRS2 divadoTS Missing file 3Dinfo file salinity'
echo 'Please provide information file 3Dinfosalinity in ./input'
exit
fi
#
  Fileinf=./input/3Dinfo
{
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read acts
} < $Fileinf
#
echo ' divaTSdress: Going to 3Danalyse salinity'
diva3Ddress
#
#
if [ -f  ./input/3Dinfo ]
then
rm  ./input/3Dinfo
fi
if [ -f ./input/3Dinfotemper ]
then
cp ./input/3Dinfotemper ./input/3Dinfo
else
echo 'DIVAERRS2 divadoTS Missing file 3Dinfo file temperature'
echo 'Please provide information file 3Dinfotemper in ./input'
exit
fi
#
  Fileinf=./input/3Dinfo
{
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read comment
read actp
} < $Fileinf
#
echo ' divaTSdress: Going to 3Danalyse temperature'
diva3Ddress
#
if [ "$actp" == "1" ];then
echo ' divaTSdress: Going to write TEMP_PSAL.nc'
./dvtsdwrt
fi
#
if [ $maxit -ge 1 ];then
#
./dvstbdat $lay1 $lay2 $temp $psal
#
./dv3d2stb $lay1 $lay2 $temp $psal $actp $acts
#
./dvstblize $acts $actp
#
cp ./input/3Dinfosalinity ./input/3Dinfo
if [  $acts == "2" ];then
if [ -d ./input/divarefe_all ] ;then
dep=$lay1
while [ $dep -le $lay2 ]
do
let lev=10000+$dep
mv ./input/divarefe_all/$psal'.'$lev'.datapoint.ref' ./input/divarefe_all/$psal'.'$lev'.datapoint.ref.notstb'
mv ./input/divarefe_all/$psal'.'$lev'.ascii.ref' ./input/divarefe_all/$psal'.'$lev'.ascii.ref.notstb'
mv ./input/divarefe_all/$psal'.'$lev'.ref' ./input/divarefe_all/$psal'.'$lev'.ref.notstb'
dep=`expr $dep + 1`
done
fi
fi
diva3Ddress
#
cp ./input/3Dinfotemper ./input/3Dinfo
if [  $actp == "2" ];then
if [ -d ./input/divarefe_all ] ;then
dep=$lay1
while [ $dep -le $lay2 ]
do
let lev=10000+$dep
mv ./input/divarefe_all/$temp'.'$lev'.datapoint.ref' ./input/divarefe_all/$temp'.'$lev'.datapoint.ref.notstb'
mv ./input/divarefe_all/$temp'.'$lev'.ascii.ref' ./input/divarefe_all/$temp'.'$lev'.ascii.ref.notstb'
mv ./input/divarefe_all/$temp'.'$lev'.ref' ./input/divarefe_all/$temp'.'$lev'.ref.notstb'
dep=`expr $dep + 1`
done
fi
fi
diva3Ddress
#
if [ "$actp" == "1" ];then
./dvtsdwrt
fi
#
fi
