#!/bin/ksh
#
set -x
modhome=`dirname $0`
cd $modhome
export MODHOME=`pwd`
echo 'Model top source directory is '$MODHOME
export DIRWORK=/space/valcke/wkdir/
export BINDIR=/space/valcke/Linux
cd ../../..
export SRCROOT=`pwd`
#
####################### User specifications ########################
export ARCH=Linux  #Linux/SX9
if [ ${ARCH} == Linux ]; then
NODE=`uname -n`
export NODE=`echo ${NODE} | cut -c1-6`
if   [ ${NODE} = natuna ]; then
   export F90=pgf90
   export NETCDF_LIB="-L/usr/local/lib -lnetcdf "
   export NETCDF_INC="-I/usr/local/include"
elif [ ${NODE} = elnino  ]; then
   export F90=f90
   export NETCDF_LIB="-L/usr/local/lib -lnetcdf "
   export NETCDF_INC="-I/usr/local/include"
else
   echo '* The script sc_run_NONE must be adapted for your platform'
fi
elif [ ${ARCH} = SX9 ]; then
   export F90=/SX/opt/sxf90/inst/bin/sxf90
   export LINKOPT="-Wl,-h size_t64 -Wl,-h nosize_terr"
   export NETCDF_LIB="-L/usr/local/SX/lib/NETCDF/lib -lnetcdf" 
   export NETCDF_INC="-I/usr/local/SX/lib/NETCDF/include"
fi
# 
# Location of grids.nc, masks.nc and areas.nc files
#
export DIR_GRD=$SRCROOT/data/testinterp/input
#
# Location of files cf_name_table.txt and namcouple
#
export DIR_INP=$SRCROOT/util/running/testNONE/INPUT
#
# Source and target grids in namcouple, grids.nc, masks.nc and areas.nc files
#
export SRC_GRID=bt42
export TGT_GRID=torc
#
# Analytical field number:
#             1) F = 2 + cos[Pi*acos(cos(lat)cos(lon)]
#             2) F = 2 + [(cos(lat))**2]*cos(2*lon)
#             3) F = 2 + [(sin(2*lon))**16]*cos(16lon)
#
export FLD_NBR=1
#
# Mask error field with target mask (YES or NOT)
#
export MASKERROR=YES
#
####################End of user specifications #####################
#
export DIR_PROG=$SRCROOT/util/running/testNONE/PROG
#
[ -d $DIRWORK ] || mkdir $DIRWORK
cd $DIRWORK
#
\rm *
#
# Copy all required files
\cp $DIR_GRD/grids.nc ./grids.nc
\cp $DIR_GRD/masks.nc ./masks.nc
\cp $DIR_INP/cf_name_table.txt ./cf_name_table.txt
\cp $DIR_INP/namcouple ./namcouple
\cp $DIR_GRD/areas.nc ./areas.nc
#
### *- Compile and run the programme that creates the input field
cp $DIR_PROG/create_inputfield.f90 .
$F90 -c create_inputfield.f90 $NETCDF_INC
$F90 $LINKOPT -o create_inputfield create_inputfield.o $NETCDF_LIB
#
### *- Compile and run the programme that calculates the error field
cp $DIR_PROG/calc_errorfield.f90 .
$F90 -c calc_errorfield.f90 $NETCDF_INC
$F90 $LINKOPT -o calc_errorfield calc_errorfield.o $NETCDF_LIB
#
\cp  ${BINDIR}/bin/oasis3.NONE.x  ./oasis3
##############################################################
#
if [ ${ARCH} == Linux ]; then
./create_inputfield $SRC_GRID $FLD_NBR
#
### *- Run oasis3
#
./oasis3
#
### *- Run the programme that calculates the error field
#
./calc_errorfield  $TGT_GRID $FLD_NBR $MASKERROR
#
##############################################################
#
elif [ ${ARCH} == SX9 ]; then
cat <<EOF >  $DIRWORK/run_testNONE.${ARCH}
#!/bin/ksh
#PBS -T mpisx
#PBS -q vector
#PBS -b 1                       # number of nodes
#PBS -l cpunum_job=1            # number of CPUs (maximum number of CPUs 8)
#PBS -l elapstim_req=02:00:00   # real time
#PBS -l cputim_job=01:00:00     # cputime
#PBS -l memsz_job=100gb          # 12 Gbyte memory
#PBS -j o                       # join err and out to out
#PBS -N OASIS3                  # job name
#PBS -m e                       # E-mail is sent when request execution ends
#PBS -M valcke@cerfacs.fr        # email address for error messages etc
#
\cd $DIRWORK
./create_inputfield ${SRC_GRID} ${FLD_NBR}
#
### *- Run oasis3
#
mpirun -np 1 oasis3
#
### *- run the programme that calculates the error field
./calc_errorfield  ${TGT_GRID} ${FLD_NBR} ${MASKERROR}

EOF
#
qsub $DIRWORK/run_testNONE.${ARCH}
fi
