#!/bin/ksh
#
set -x
export ARCH=Linux
modhome=`dirname $0`
cd $modhome
export MODHOME=`pwd`
echo ' '
echo 'Model top source directory is '$MODHOME
export MODDIR=`basename $MODHOME`
echo 'Model (directory) name is ' $MODDIR
cd ../../..
export SRCROOT=`pwd`
echo 'Source root directory is '$SRCROOT
#
### *- Define the path of testinterp and working directory
#
export HOMETEST=$SRCROOT/util/running/testinterp
export DIRDATA=$SRCROOT/util/running/testinterp
export DIRWORK=/space/valcke/wkdir_testinterp_MANY
export BINDIR=/space/valcke/Linux
#
### *- Define which test to performed, either many interpolations for one time
###    occurrence of different fields (TIME=ONE), either the same interpolation
###    of difference time occurrences of one field (TIME=MANY)
#
export TIME=ONE
#
### *- Create working directory if needed and copy input and restart files
#
[ -d $DIRWORK ] || mkdir $DIRWORK
cd $DIRWORK
#
\rm areas.nc fld*.nc grids.nc anaisout *weights masks.nc namcouple oasis3
\rm a*in.nc a*ou.nc c*in.nc c*ou.nc a*out.nc

ln -s $DIRDATA/input/cf_name_table.txt ./cf_name_table.txt

if [ $TIME == ONE ]; then
  ln -s $DIRDATA/input/namcouple ./namcouple
elif [ $TIME == MANY ]; then
  ln -s $DIRDATA/input/namcouple_MANY ./namcouple
#elif [ $TIME == MYCASE ]; then
#  ln -s $DIRDATA/input/namcouple_MYCASE ./namcouple
else
  echo 'Bad value of TIME'
  exit
fi

ln -s $DIRDATA/input/grids.nc .
ln -s $DIRDATA/input/areas.nc .

if [ $TIME == ONE ]; then
  ln -s $DIRDATA/input/masks.nc ./masks.nc
  for nom in flda1.nc fldb1.nc fldo1.nc fldz1.nc
    do
      ln -s $DIRDATA/restart/${nom} ./${nom}
    done
  for nom in aaIin.nc caJin.nc
    do
      ln -s $DIRDATA/restart/vector/${nom} ./${nom}
    done
elif [ $TIME == MANY ]; then
  ln -s $DIRDATA/input/masks.nc_MANY ./masks.nc
  ln -s $DIRDATA/restart/fldin.nc ./fldin.nc
#elif [ $TIME == MYCASE ]; then
#  ln -s $DIRDATA/input/masks.nc ./masks.nc
#  ln -s $DIRDATA/restart/flda1.nc ./flda1.nc
#  ln -s $DIRDATA/restart/fldo1.nc ./fldo1.nc
fi
#
### *- Run oasis3
#
\cp  $BINDIR/bin/oasis3.NONE.x  ./oasis3
./oasis3
#
### *- for TIME=ONE, perform error calculation
#
if [ $TIME == ONE ]; then

\cp $HOMETEST/error/gen_error .

\cp $HOMETEST/error/error_in_1 error_in
./gen_error
\mv error.nc error_at42_torc_distwgt.nc
\mv error_log log_at42_torc_distwgt

\cp $HOMETEST/error/error_in_2 error_in
./gen_error
\mv error.nc error_torc_at42_gauswgt.nc
\mv error_log log_torc_at42_gauswgt

\cp $HOMETEST/error/error_in_3 error_in
./gen_error
\mv error.nc error_lmdz_torc_bilin.nc
\mv error_log log_lmdz_torc_bilin

\cp $HOMETEST/error/error_in_4 error_in
./gen_error
\mv error.nc error_bt42_torc_bilin.nc
\mv error_log log_bt42_torc_bilin

\cp $HOMETEST/error/error_in_5 error_in
./gen_error
\mv error.nc error_at42_torc_bicub.nc
\mv error_log log_at42_torc_bicub

\cp $HOMETEST/error/error_in_6 error_in
./gen_error
\mv error.nc error_torc_at42_conservfrac.nc
\mv error_log log_torc_at42_conservfrac

\cp $HOMETEST/error/error_in_7 error_in
./gen_error
\mv error.nc error_bt42_torc_distwgt.nc
\mv error_log log_bt42_torc_distwgt

\cp $HOMETEST/error/error_in_8 error_in
./gen_error
\mv error.nc error_at42_torc_conservdest.nc
\mv error_log log_at42_torc_conservdest

\cp $HOMETEST/error/gen_error_vector .

\cp $HOMETEST/error/compute_error compute_error
./gen_error_vector
\mv error.nc error_at42_torc_distwgt_acvector.nc
\mv error_log log_at42_torc_distwgt_acvector

#elif [ $TIME == MYCASE ]; then

#\cp $HOMETEST/error/gen_error .

#\cp $HOMETEST/error/error_in_1 error_in
#./gen_error
#\mv error.nc error_at42_torc_distwgt.nc
#\mv error_log log_at42_torc_distwgt

fi

