#!/bin/ksh

###############################################################################
# Script for installing a 1D version of LMDZ.
# LMDZ team. For questions : lmdz-svp@lmd.jussieu.fr
# valid for svn version from 21/12/2012 (end of the world) or svn 1700
#
# 1) Modifies the directory LMDZ.../modipsl/modeles/LMDZ5/libf/phy1d
# which  contains the single column version of the model.
# 2) installs a series of test cases on LMDZ.../1dcases/
# 3) compile the lmdz1.F with vertical resolution L39 and L40
# 4) runs a subset of the cases
###############################################################################

PATH=~/bin:$PATH
listecas="toga twpice arm_cu" # list of cases to be benched (all cases are
                              # downloaded
interne=`pwd`/1D # path of the local directory for lmdz1d_...
                 # if not found, the directory is automatically donwloaded
                 # and kept at the given location
                 # interne =no forces to remove the direrctory after
                 # each instal1d
version1d=20140428 # Check in ~lmdz/WWW/DistribG95 which 1d version to use
modele=""        # The directory containing LMDZ can be specified.
                 # If not, some automatic procedure will try to find it.



if [ "$modele" = "" ] ; then
   if [ -f install.sh ] ; then
      # If install.sh exists, the name of the model is taken there
      version=`sed -n "s/^version=//p" install.sh | tail -1`
      modele=`pwd`/`grep 'MODEL=' install.sh | sed -e 's/.version/'$version'/' | sed -e 's/MODEL=//' | tail  -2 | head -1`
   elif [ -d modipsl ] ; then
      # if instal1d.sh is directly placed in the LMDZ tree
      modele=`pwd`
   else
      echo "You should run instal1d.sh either in the directory"
      echo "containing install.sh, or in that containing modipsl"
      echo "or specify the path of LMDZ. modele=LMDZ... in instal1d.sh"
      exit
   fi
fi

###############################################################################
# Paths of main directories
# Directories of simulations will be placed in
# $modele/1dcases
# Sources of physical routines in  $modele/modipsl/modeles/LMDZ5/libf/phy1d
###############################################################################
cd $modele
LMDGCM=`pwd`
LMDlibf=$LMDGCM/modipsl/modeles/LMDZ5/libf/
cd $LMDlibf
rev=`svn info -r BASE | grep vision | tail -1 | awk ' { print $NF } '`
cd $LMDGCM

if [ $rev -gt 2015 ] ; then 
    if [ $version1d -lt 20140416 ] ; then
       echo Avec une revision svn de LMDZ plus recente que 2015, il faut
       echo version1d=20140416 ou plus recente. Changer dans le script
       echo et relancer.
       exit
    fi
fi




###############################################################################
# Check if the svn version is more recent than 1700
# If not, an  older script must be used
###############################################################################
cd $LMDGCM/modipsl/modeles
new1d=`svn info LMDZ5 | grep Revision | awk ' { fl = 0 ; if ( $2 > 1699 )  fl=1 ; print fl } '`
echo new1d $new1d
if [ $new1d = 0 ] ; then
   echo Your model verison is too old for this instal1d.sh script
   echo You should use instead
   echo http://www.lmd.jussieu.fr/~lmdz/Distrib/install_old_archive/instal1d.sh
   exit
fi

###############################################################################
# Downloading modified source files and input files
###############################################################################
if [ $interne = no ] ; then
   source=$LMDGCM/lmdz1d_source_${version1d}
else
   source=${interne}/lmdz1d_source_${version1d}
   mkdir -p $interne
fi


if [ ! -d $source ] ; then
   wget http://www.lmd.jussieu.fr/~lmdz/DistribG95/lmdz1d_source_${version1d}.tar.gz
   tar xvfz lmdz1d_source_${version1d}.tar.gz
   rm -f lmdz1d_source_${version1d}.tar.gz
   mv lmdz1d_source_${version1d} $source
fi

################################################################################
# Creating directory for 1d cases
################################################################################

cd $LMDGCM
if [ -d 1dcases ] ; then
  echo '1dcases existe deja'
  echo 'on continue'
else
  echo 'creation du directory 1dcases'
  mkdir 1dcases
fi
cd 1dcases

for cas in amma arm_cu ayotte case_e eq_rd_cv fire hapex92_init rico toga sanduref sandufast sanduslow twpice ; do
   if [ -d $cas ] ; then
      echo $cas existe deja
      echo 'on continue, mais il  faudra verifier qu il ne manque rien dans '$cas
   else
      echo 'creation du directory de simulation '$cas
      cp -a ${source}/$cas .
   fi
   dir $cas
done



################################################################################
# Installing source files and compiling
# Obsolete after rev 2014
################################################################################

if [ $rev -le 2015 ] ; then 
cd $LMDlibf
if [ -f phy1d/thermcell_dq.F90 ] ; then
   echo phy1d a deja ete modifie. On repart d une version sauvegardee
   if [ ! -d phy1dorig ] ; then
      echo Cas a retravailler : le phy1d d origine n est plus dispo
      exit
   fi
   mv phy1d phy1d$$
   cp -r phy1dorig phy1d
else
   cp -r phy1d phy1dorig # sauvegarde du phy1d d'origine
fi
chmod +x ../tools/install_1d_src.sh 
../tools/install_1d_src.sh 
fi

if [ $interne = no ] ; then
##   Nettoyage
  \rm -r ${source}
fi

echo '#########################################################################'
echo '   1D test cases'
echo '#########################################################################'

for cas in $listecas ; do
   echo ====================================
   echo TEST CASE : $cas' (1 month)'
   echo ====================================
   echo if the last line \"SIMULATION FINISHED\" is
   echo \"Everything is cool!\", the situation went to its end
   cd ${LMDGCM}/1dcases/$cas
   ./compile.x
   ./xqt.x NPv3.1
   \rm tmp.jnl ; touch tmp.jnl
   rm -f var.gif
   cat <<...eod>> tmp.jnl
   use histhf_NPv3.1.nc
   shade/title="cas $cas: cloud fraction (-)" rneb
   contour/o geop/9.8
   frame/file=var.gif
...eod

   ferret <<...eod
   go tmp.jnl
   quit
...eod

   display var.gif :&
   rm -f tmp.jnl
done

