#!/bin/bash

# Le mode auqaplanete plannte avec pbl_surface actif et pas le mode
# physique complet ...

parallel=none

while (($# > 0))
   do
   case $1 in
     "-parallel") parallel=$2 ; shift ; shift ;;
   esac
done

if [ $parallel = none ] ; then
   gcm="../gcm.e"
else
   gcm="../run_local.sh 2 2 ../gcm.e"
fi

iflag_phys=1 # 0: sans physique, 1: avec, 101: aquaplanete
nday=1
iflag_radia=1
nbapp_rad=12
iphysiq=10
day_step=480

echo ATTENTION : il faut imposer la frequence de dissip a iperiod
echo PROBLEME A RESOUDRE



\rm -r SIM*

((nd2=2*$nday))
echo $nd2


#########################################################################################
# Realisation de la premiere simulation
#########################################################################################

mkdir SIM1
cd SIM1
cp ../*def .
sed -e 's/nday=.*.$/nday='$nday'/' ../run.def >| run.def
sed -e 's/iflag_phys=.*.$/iflag_phys='$iflag_phys'/' \
    -e 's/iphysiq=.*.$/iphysiq='$iphysiq'/' \
    -e 's/nsplit_phys=.*.$/nsplit_phys='$nsplit_phys'/' \
    -e 's/day_step=.*.$/day_step='$day_step'/' \
    ../gcm.def >| gcm.def

ln -s ../start.nc .
if [ $iflag_phys != 0 ] ; then
   ln -s ../startphy.nc .
   ln -s ../sechiba_rest_in.nc .
   ln -s ../aer*nc .
   ln -s ../limit.nc .
   sed -e 's/iflag_radia=.*.$/iflag_radia='$iflag_radia'/'  \
       -e '/phys_out_filelevels/s/=.*.$/= 5 5 0 4 4/' \
       -e 's/nbapp_rad=.*.$/nbapp_rad='$nbapp_rad'/' \
      ../config.def >| config.def
fi
cp -f gcm.def tmp
if [ $iflag_phys -gt 100 ] ; then
sed -e 's/read_start=.*.$/read_start=n/' tmp >| gcm.def ; rm -f limit.nc
fi
$gcm >| listing
cp -f tmp gcm.def


#########################################################################################
# Realisation de la seconde simulation
#########################################################################################

mkdir ../SIM2
cp *def ../SIM2
cd ../SIM2
ln -s ../SIM1/restart.nc start.nc
if [ $iflag_phys != 0 ] ; then
   ln -s ../SIM1/restartphy.nc startphy.nc
   ln -s ../SIM1/sechiba_rest_out.nc sechiba_rest_in.nc
   ln -s ../aer*nc .
   ln -s ../SIM1/limit.nc .
fi
$gcm >| listing

# Extraction ascii de ucov dans restart.nc
ncdump -v ucov restart.nc > u.txt

# Extraction du champ de pression de surface dans le restart
ncks -M -m -h -v ps restart.nc -O ps_end.nc


#########################################################################################
# Realisation de la simulation complete
#########################################################################################

mkdir ../SIM1+1
cp *def ../SIM1+1
sed -e 's/nday=.*.$/nday='$nd2'/' run.def >| ../SIM1+1/run.def
cd ../SIM1+1
ln -s ../start.nc .
if [ $iflag_phys != 0 ] ; then
   ln -s ../startphy.nc startphy.nc
   ln -s ../sechiba_rest_in.nc .
   ln -s ../aer*nc .
   ln -s ../SIM1/limit.nc .
fi
cp -f gcm.def tmp
if [ $iflag_phys = 101 ] ; then
sed -e 's/read_start=.*.$/read_start=n/' tmp >| gcm.def ; rm -f limit.nc
fi
$gcm >| listing
ncdump -v ucov restart.nc > u.txt
ncks -M -m -h -v ps restart.nc -O ps_end.nc


cd ..
echo DIFF DE SIM2/u.txt et SIM1+1/u.txt pour verifier la convergence numerique
echo u.txt contient une extraction de ucov dans les restart.nc
diff SIM2/u.txt SIM1+1/
echo LE DIFF des restart.nc ajoute en plus le test sur les champs non
echo vus par la dynamique comme les traceurs.
diff SIM2/restart.nc SIM1+1/

cd SIM1
for i in fort*  ; do cat $i ../SIM2/$i >| ../$i ; done
cd ..
