#!/bin/bash
#set -vx
# Default options
#
#
#
date
#### 1  Set up the compiling options
#### Define some directories
submitdir=$( pwd )
modipsl=$submitdir/../..
arch_path=$submitdir/ARCH 
mysrc_path=$submitdir/SOURCES

#### Set default options 
# Atmospheric resolution, for LMDZ/ORCHIDEE in regular mode
resol_atm=144x142x79
# Oceanic resolution, for NEMO (ORCA2/ORCA1/ORCA025)
resol_oce=ORCA1
# Version of ice model lim2/lim3
icemodel=lim3
# Coupled with ocean biogeochemistry (y/n)
oceanbio=y
# Version ESM CO2: CO2 interactif ocean/atmosphere (y/n)
esmco2=n
# Version ESM AER : Aer interactif chemistry / atmosphere (y/n) 
esmaer=n
# Also compile ce0l subprogram to LMDZ (y/n)
ce0l=n
# choose radiative code compilation option 
rad=rrtm
opt_rad=""
# choose inca chemistry
optchimie=AER

# Optimization mode
# optmode=prod/dev/debug
optmode=prod
# fcm_arch
fcm_arch=default
# Default values to be overritten
parallel=mpi_omp
export fcm_arch parallel xios
full_flag=""
full_nemo=n
full_inca=""
full_xios=""
full_lmdz=""
full_orch=""



# Output text file for compilation of each component
datestr=`LC_ALL=C date +"%Y%m%dT%H%M"`
outfile=$submitdir/out_compile_ipslcm6.$datestr
echo > $outfile
echo; echo "Text output from compilation will be stored in file out_compile_ipslcm6.$datestr"; echo 

#### Read arguments 
# Loop over all arguments to modify default set up
while (($# > 0)) ; do
    case $1 in
	"-h") cat <<end_help
########################################################################
# Usage of the script compile_ipslcm6.sh
#
########################################################################

./compile_ipslcm6.sh [Options] 


Options: [LR / VLR / MR1 / MR025] Model resolution, choose only one. Default: LR.
         [ESMCO2] Compile IPSLCM6 for CO2 interactif ocean/atmosphere.
         [ESMCHEM] Compile IPSLCM6 for chemistry interactif on atmosphere
         [ESMCO2AER] Compile IPSLCM6 for both interactive CO2 and chemistry, and also with transfer of species from PISCES to INCA.
         [-full] Full recompilation of all components. This option can be added to all other options.
         [-cleannemo] Full recompilation of NEMO component  only.
         [-debug / -dev / -prod] Level of optimization. One of these can be added to all other compile options. Default: -prod.


Example 1: Default compilation of IPSLCM6 for resoltion LR 
          (Resolution atmos: 144x142x79, ocean: ORCA1)
./compile_ipslcm6.sh

Example 2: Compilation of IPSLCM6 for resolution MR025
           (atmos: 256x256x79, ocean: ORCA025, NOPISCES)
./compile_ipslcm6.sh MR025

Example 3: Compilation of IPSLCM6 for ESM CO2
./compile_ipslcm6.sh ESMCO2 -cleannemo

Example 4: Default resoltuion (LR) compiled in debug mode
./compile_ipslcm6.sh -debug

Example 5: Default compilation with full recompilation of all components. No clean is needed.
./compile_ipslcm6.sh -full

Example 6: Full recompilation of resolution MR05 in debug mode
./compile_ipslcm6.sh MR025 -debug -full

Example 7: choose radiative schema for LMDZ (oldrad/rrtm/ecrad). Default is rrtm
./compile_ipslcm6.sh MR025 -rad ecrad

Example 8: Compilation of IPSLCM6 for ESM with INCA chemistry (default CHEMISTRY=AER)
./compile_ipslcm6.sh ESMCHEM

Example 9: Compilation of IPSLCM6 for ESM with INCA chemistry and choose the chemistry 
./compile_ipslcm6.sh ESMCHEM -chimie GES


end_help
exit;;
	"VLR")         resol_atm=96x95x39; resol_oce=ORCA2 ; icemodel=lim2; oceanbio=y; shift ;;
	"LR")          resol_atm=144x142x79; resol_oce=ORCA1 ; icemodel=lim3; oceanbio=y; shift ;;
	"MR1")         resol_atm=256x256x79; resol_oce=ORCA1 ; icemodel=lim3; oceanbio=y; shift ;;
	"MR025")       resol_atm=256x256x79; resol_oce=ORCA025 ; icemodel=lim3; oceanbio=n;   shift ;;
	"ESMCO2")      esmco2=y;  shift ;; 
	"ESMCHEM")      esmchem=y; shift ;; 
	"ESMCO2AER")   esmco2=y; esmchem=y; shift ;;
	"CE0L")        ce0l=y ; shift ;;
	"-rad")        rad=$2; shift ; shift;;
	"-parallel")   parallel=$2 ; shift ; shift ;; 
	"-arch")       fcm_arch="$2" ; shift ; shift ;;
	"-debug")      optmode=debug ; shift ;;
	"-dev")	       optmode=dev ; shift ;;
	"-prod")       optmode=prod ; shift ;;
	"-full")       full_flag="-full"; full_nemo=y ; full_xios="--full" ; full_lmdz="-full" ; full_orch="-full" ; shift ;;
	"-full_xios")  full_xios="--full" ; shift ;;  # Note only full_xios is using double dash: --full
	"-full_lmdz")  full_lmdz="-full"  ; shift ;;
	"-full_orch")  full_orch="-full"  ; shift ;;
	"-full_nemo")  full_nemo=y ; shift ;;
	"-full_inca")  full_inca="-clean"; shift ;;
	"-cleannemo")  full_nemo=y ; shift ;;
	"-chimie")     optchimie=$2; shift ; shift;;

	*)	       echo "unknown option "$1" , exiting..." ; exit
    esac
done

echo "Following options are set in current compiling:" >> $outfile
echo "   resol_atm=${resol_atm}, resol_oce=${resol_oce}, icemodel=${icemodel}, oceanbio=${oceanbio}" >> $outfile 
echo "   esmco2=${esmco2}, esmchem=${esmchem}, chemistry=${optchimie}" >> $outfile 
echo "   ce0l=${ce0l}" >> $outfile 
echo "   optmode = $optmode, parallel = $parallel, fcm_arch = $fcm_arch " >> $outfile 
echo "   full_flag=$full_flag, full_xios=$full_xios, full_lmdz=$full_lmdz, full_orch=$full_orch, full_nemo=$full_nemo," >> $outfile 
echo >> $outfile

### Read host dependent default values
### These variables will not be changed if they were set as argument
###./host.sh $host
# Later : Following lines should be set in host.sh file
# begin host.sh
if [ $fcm_arch == default ] ; then
    # Find out current host and source specific paths and commands for the host
    case $( hostname -s ) in
	jean-zay*)
	    fcm_arch=X64_JEANZAY;;
        irene170|irene171|irene190|irene191|irene192|irene193)
            fcm_arch=X64_IRENE;;
        irene172|irene173|irene194|irene195)
            fcm_arch=X64_IRENE-AMD;;
	asterix*|obelix*)
	    fcm_arch=ifort_LSCE
            if [ "${parallel}" == "mpi_omp" ] ; then 
		echo "Warning!! Currently at Obelix hybrid mode is not possible."
		echo "          Option -parallel mpi_omp is now changed to -parallel mpi"
		echo ""
		parallel=mpi
            else
		echo "You used -parallel" $parallel
            fi ;;
	ciclad*|climserv*|loholt*|camelot*)
	    fcm_arch=ifort_CICLAD;;
	*)
	    echo Current host is not known. You must use option -arch to specify which architecuture files to use.
	    echo Exit now.
	    exit
    esac
fi

# Set a link to arch.env if arch-${fcm_arch}.env file exist for current fcm_arch.
# The link arch.env is also set in config.card and will be used by libIGCM to ensure the same running environnement.
if [ -f ARCH/arch-${fcm_arch}.env ] ; then
    echo >> $outfile
    echo "The file ARCH/arch-${fcm_arch}.env will now be sourced with modules needed for compilation for all components."
    echo "Note that this new environement might be kept after compilation." 
    echo "If this is the case, source again your personal environment after compilation. "
    echo " Personal module list before sourcing of ARCH/arch.env file:"    >> $outfile  
    module list   >> $outfile 2>&1 
    # Get initial svn version
    svn_version=$(module list -t | grep subversion)

    # Make a link to this file, to be used also in config.card
    rm -f ARCH/arch.env
    ln -s arch-${fcm_arch}.env ARCH/arch.env

    # Source the file
    source ARCH/arch.env   >> $outfile 2>&1 
    if [ X$svn_version != X ] ; then
        # Put back initial version of svn
        module load ${svn_version}
    fi
    echo >> $outfile 
    echo " New module list after sourcing of ARCH/arch.env file:"    >> $outfile  
    module list   >> $outfile 2>&1 
fi

#### 2 Do the compilation 
## 2.1 Compile ioipsl
cd $modipsl/modeles/IOIPSL
echo; echo "NOW COMPILE IOIPSL"
echo >> $outfile ; echo " NOW COMPILE IOIPSL"   >> $outfile 

# Check if compilation with fcm is included in IOIPSL
if [ ! -f makeioipsl_fcm ] ; then
  echo "The file makeiopsl_fcm do not exist. Probably this is not the right version of IOIPSL."
  echo "Version of IOIPSL with fcm compilation is needed to compile with this script. Stop now."
  exit
fi

echo ./makeioipsl_fcm -$optmode -parallel -arch ${fcm_arch} -arch_path $arch_path -j 8 $full_flag   >> $outfile 
     ./makeioipsl_fcm -$optmode -parallel -arch ${fcm_arch} -arch_path $arch_path -j 8 $full_flag   >> $outfile 2>&1
# Test if compiling succeded 
if [[ $? != 0 ]] ; then 
    echo "THERE IS A PROBLEM IN IOIPSL COMPILATION - STOP"
    exit
fi

## 2.2 Compile oasis3-mct
cd $modipsl/oasis3-mct/util/make_dir
echo; echo "NOW COMPILE OASIS3-MCT"
echo >> $outfile ; echo " NOW COMPILE OASIS3-MCT"   >> $outfile
cp $mysrc_path/OASIS3-MCT/make_${fcm_arch} make.inc

echo make -f TopMakefileOasis3 >> $outfile
     make -f TopMakefileOasis3 >> $outfile 2>&1


## 2.3 Compile xios
cd $modipsl/modeles/XIOS
echo; echo "NOW COMPILE XIOS"
echo >> $outfile ; echo " NOW COMPILE XIOS"   >> $outfile 
echo ./make_xios --use_oasis oasis3_mct --$optmode --arch $fcm_arch --arch_path $arch_path --job 4 $full_xios   >> $outfile 
     ./make_xios --use_oasis oasis3_mct --$optmode --arch $fcm_arch --arch_path $arch_path --job 4 $full_xios   >> $outfile 2>&1
# Test if compiling succeded 
if [[ $? != 0 ]] ; then 
    echo "THERE IS A PROBLEM IN XIOS COMPILATION - STOP"
    exit
fi
# Move executables to modipsl/bin
if [ -f $modipsl/modeles/XIOS/bin/xios_server.exe ] ; then 
    mv $modipsl/modeles/XIOS/bin/xios_server.exe $modipsl/bin/xios_server_${optmode}.exe
else
    echo "THERE IS A PROBLEM IN XIOS COMPILATION EXECUTABLE MISSING - STOP"
    exit
fi


## 2.4 Compile orchidee
cd $modipsl/modeles/ORCHIDEE
echo; echo "NOW COMPILE ORCHIDEE"
echo >> $outfile ; echo " NOW COMPILE ORCHIDEE"   >> $outfile 

# Check if the compilation of ORCHIDEE was previsouly interupted prematured. 
# In that case, the files fcm.bld.lock exist. If the file exist, it is here removed to avoid makeorchidee_fcm 
# to ask question and wait for interactivly answer from the user.
if [ -f build/fcm.bld.lock ] ; then
    echo >> $outfile
    echo " WARNING!! The file ORCHIDEE/build/fcm.bld.lock exist. "   >> $outfile
    echo "           This means that the compilation is either currently on going in another terminal was previous interupted before the end."   >> $outfile
    echo "           The file will now be removed. "   >> $outfile
    echo >> $outfile
    rm -f build/fcm.bld.lock
fi

echo    ./makeorchidee_fcm -j 8 -xios -parallel $parallel -$optmode -arch ${fcm_arch} -arch_path $arch_path $full_orch   >> $outfile 
        ./makeorchidee_fcm -j 8 -xios -parallel $parallel -$optmode -arch ${fcm_arch} -arch_path $arch_path $full_orch   >> $outfile 2>&1
# Test if compiling finished
if [[ $? != 0 ]] ; then
    echo "THERE IS A PROBLEM IN ORCHIDEE COMPILATION - STOP"
    exit
fi


## 2.5 Compile NEMO
nemo_root=$modipsl/modeles/NEMOGCM/CONFIG
if [ ${icemodel} == lim2 ] ; then
   addkeys="key_lim2_vp key_diahth key_oasis3"
   delkeys="key_nosignedzero key_mpp_rep"
else
   addkeys="key_top key_pisces key_age key_cfc key_cpl_carbon_cycle key_gas key_oasis3"
   if [ ${oceanbio} == y ] ; then
      if [ ${esmco2} == y ] ; then
        if [ ${esmchem} == y ] ; then
           delkeys="key_nosignedzero key_mpp_rep"
        else
           delkeys="key_nosignedzero key_mpp_rep"
        fi
      else
         delkeys="key_nosignedzero key_mpp_rep key_cpl_carbon_cycle key_gas"
      fi
   else
      delkeys="key_nosignedzero key_mpp_rep key_top key_pisces key_cfc key_age key_cpl_carbon_cycle key_gas"
   fi
fi

if [ ${oceanbio} == n ] ; then
   echo; echo "NOW COMPILE NEMO. Resolution = ${resol_oce} with icemodel ${icemodel} and without PISCES"
   echo >> $outfile ; echo " NOW COMPILE NEMO. Resolution = ${resol_oce} with icemodel ${icemodel} and without PISCES"   >> $outfile
else
   echo; echo "NOW COMPILE NEMO. Resolution = ${resol_oce} with icemodel ${icemodel} and PISCES"
   echo >> $outfile ; echo " NOW COMPILE NEMO. Resolution = ${resol_oce} with icemodel ${icemodel} and PISCES"   >> $outfile
fi
echo >> $outfile ; echo cd $nemo_root  >> $outfile
echo >> $outfile ; echo cp $modipsl/config/IPSLCM6/SOURCES/NEMO/arch-${fcm_arch}.fcm ../ARCH/.   >> $outfile
echo >> $outfile

cd $nemo_root ; cp $mysrc_path/NEMO/arch-${fcm_arch}.fcm ../ARCH/.


if [ ${resol_oce} == ORCA2 ]   ; then  cfg_wrk=ORCA2_LIM_PISCES    ; fi
if [ ${resol_oce} == ORCA1 ]   ; then  cfg_wrk=ORCA1_LIM3_PISCES    ; fi
if [ ${resol_oce} == ORCA025 ] ; then  cfg_wrk=ORCA025_LIM3_PISCES  ; fi

# Temporary use of SOURCES/ORCA1_LIM3_PISCES to handle modifications needed by pisces coupling with inca
cp $mysrc_path/ORCA1_LIM3_PISCES/* $nemo_root/ORCA1_LIM3_PISCES/MY_SRC


echo cp $modipsl/config/IPSLCM6/SOURCES/NEMO/*.*90 $nemo_root/$cfg_wrk/MY_SRC/   >> $outfile
echo >> $outfile
cp $mysrc_path/NEMO/*.*90  $nemo_root/$cfg_wrk/MY_SRC/

if [ $full_nemo == y ] ; then
   # To make a full compilation, first make a clean to remove all files produced during previous compilation
   echo ./makenemo -m ${fcm_arch} -n $cfg_wrk clean   >> $outfile
   echo >> $outfile
   ./makenemo -m ${fcm_arch} -n $cfg_wrk clean  >> $outfile 2>&1
fi
echo ./makenemo -m ${fcm_arch} -n $cfg_wrk -j16 add_key "$addkeys"  del_key "$delkeys"   >> $outfile
echo >> $outfile
./makenemo -m ${fcm_arch} -n $cfg_wrk -j16 add_key "$addkeys"  del_key "$delkeys"  >> $outfile 2>&1


# Test if compiling finished
if [[ $? != 0 ]] ; then
    echo "THERE IS A PROBLEM IN NEMO COMPILATION - STOP"
    exit
fi

echo >> $outfile
echo "Move nemo executable to modipsl/bin" >> $outfile
echo ls -lrt $nemo_root/$cfg_wrk/BLD/bin   >> $outfile
ls -lrt $nemo_root/$cfg_wrk/BLD/bin  >> $outfile
echo >> $outfile

if [ -f $nemo_root/$cfg_wrk/BLD/bin/nemo.exe ] ; then
    if [ ${esmco2} == y ] && [ ${esmchem} == n ] ;  then 
	mv $nemo_root/$cfg_wrk/BLD/bin/nemo.exe $modipsl/bin/opa.ESMCO2_${resol_oce}_${optmode}
    elif [ ${esmco2} == y ] && [ ${esmchem} == y ] ;  then
    mv $nemo_root/$cfg_wrk/BLD/bin/nemo.exe $modipsl/bin/opa.ESMCO2AER_${resol_oce}_${optmode}
    else
	mv $nemo_root/$cfg_wrk/BLD/bin/nemo.exe $modipsl/bin/opa_${resol_oce}_${optmode}
    fi
fi


## 2.6 Compile LMDZ for regular latlon configuration
# If necessary compile INCA modele with AER configuration 
if [ ${esmchem} == y ] ;  then 
    cd $modipsl/modeles/INCA
    #compile INCA regulat lat_lon chimie library 
    echo; echo "NOW COMPILE INCA ${optchimie} on resolution = ${resol_atm}"
    echo ./makeinca_fcm -chimie ${optchimie} -$optmode -xios -parallel mpi_omp -j 8 -arch ${fcm_arch} -arch_path $arch_path $full_flag $full_inca -cplocninca >> $outfile
         ./makeinca_fcm -chimie ${optchimie} -$optmode -xios -parallel mpi_omp -j 8 -arch ${fcm_arch} -arch_path $arch_path  $full_flag $full_inca -cplocninca >> $outfile 2>&1
    
    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN INCA COMPILATION - STOP"
	exit
    fi

    echo "Move inca.dat modipsl/bin"
    if [[ -f $modipsl/modeles/INCA/SIMULATIONS/$optchimie/inca.dat ]] ;  then  
	mv $modipsl/modeles/INCA/SIMULATIONS/$optchimie/inca.dat $modipsl/bin/inca_${optchimie}.dat ;
    else
	echo "THERE IS A PROBLEM IN INCA COMPILATION - STOP"
	exit	
    fi

    echo "Move tracer.def to  modipsl/bin"
    if [[ -f $modipsl/modeles/INCA/tracer.def ]] ;  then
        mv $modipsl/modeles/INCA/tracer.def $modipsl/bin/tracer_${optchimie}.def ;
    fi


    
fi 


# Compile LMDZ regular lat-lon executable
cd $modipsl/modeles/LMDZ
echo; echo "NOW COMPILE LMDZ REGULAR LAT-LON MODE. Resolution = ${resol_atm}"
echo >> $outfile ; echo " NOW COMPILE LMDZ REGULAR LAT-LON MODE. Resolution = ${resol_atm}"   >> $outfile 

# Temporary use of SOURCES/LMDZ to handle modifications needed by LMDZ rev 4515
cp $mysrc_path/LMDZ/*.*90  libf/phylmd/.


# Check if the compilation of LMDZ was previsouly interupted prematured. 
# In that case, the files dimension.h and .lock exist. If the files exist, they are here removed to avoid makelmdz_fcm 
# to ask question and wait for interactivly answer from the user.
if [ -f libf/grid/dimensions.h ] || [ -f .lock ] ; then
  echo >> $outfile
  echo " WARNING!! The file LMDZ/libf/grid/dimension.h and/or the LMDZ/.lock exist. "   >> $outfile
  echo "           This means that the compilation is either currently on going in another terminal was previous interupted before the end."   >> $outfile
  echo "           The files dimension.h and .lock will now be removed. "   >> $outfile
  echo >> $outfile
  rm -f libf/grid/dimensions.h
  rm -f .lock
fi

# Need to define the proper option for radiative code compilation. By default, we use rrtm
case $rad in
    oldrad) opt_rad="" ;;
    rrtm)    opt_rad="-rad rrtm"  ;;
    ecrad)  opt_rad="-rad ecrad" ;;
    *) echo Only oldrad rrtm ecrad for rad option ; exit
esac

if [ ${esmchem} == n ] ;  then
    echo ./makelmdz_fcm -d ${resol_atm} -c OMCT -cosp true $opt_rad -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -arch $fcm_arch -arch_path $arch_path -j 8 $full_lmdz gcm    >> $outfile
         ./makelmdz_fcm -d ${resol_atm} -c OMCT -cosp true $opt_rad -$optmode -mem -parallel $parallel -io xios -v orchidee2.1 -arch $fcm_arch -arch_path $arch_path -j 8 $full_lmdz gcm    >> $outfile 2>&1
else
    #in esm aer case add chemistry option to lmdz compilation
    echo ./makelmdz_fcm -d ${resol_atm} -c OMCT -cosp true $opt_rad -rrtm true -$optmode -mem -parallel $parallel -chimie INCA -io xios -v orchidee2.1 -arch $fcm_arch -arch_path $arch_path -j 8 $full_lmdz gcm    >> $outfile
         ./makelmdz_fcm -d ${resol_atm} -c OMCT -cosp true $opt_rad -rrtm true -$optmode -mem -parallel $parallel -chimie INCA -io xios -v orchidee2.1 -arch $fcm_arch -arch_path $arch_path -j 8 $full_lmdz gcm    >> $outfile 2>&1
fi

# Test if compiling finished
if [[ $? != 0 ]] ; then
    echo "THERE IS A PROBLEM IN LMDZ REGULAR LATLON COMPILATION - STOP"
    exit
fi


# Move executables to modipsl/bin folder
echo >> $outfile
echo "Move gcm.e executable to modipsl/bin" >> $outfile
echo ls -lrt $modipsl/modeles/LMDZ/bin   >> $outfile
ls -lrt $modipsl/modeles/LMDZ/bin  >> $outfile
echo >> $outfile


suffix=_${resol_atm}_phylmd
suffix=${suffix}_${rad} 
suffix=${suffix}_para_mem_orch_couple
if [ ${esmchem} == n ] ;  then 
    suffix=${suffix}.e
else
    suffix=${suffix}_inca.e
fi

echo gcm suffix = $suffix

if [ -f $modipsl/modeles/LMDZ/bin/gcm${suffix} ] ;  then 
    if [ ${esmchem} == n ] ;  then 
	mv $modipsl/modeles/LMDZ/bin/gcm${suffix} $modipsl/bin/gcm_${resol_atm}_${optmode}.e
    else
	mv $modipsl/modeles/LMDZ/bin/gcm${suffix} $modipsl/bin/gcm_${resol_atm}_${optmode}_${optchimie}.e
    fi
else
    echo "ERROR gcm${suffix} executable does not exist." 
    echo "THERE IS A PROBLEM IN LMDZ REGULAR LATLON COMPILATION - STOP"
    exit
fi  





# Compile ce0l initialization program for LMDZ regular lat-lon exectuable
if [ $ce0l == y ] ; then
    echo; echo "NOW COMPILE CE0L OF LMDZ. Resolution = ${resol_atm}"
    echo >> $outfile ; echo " NOW COMPILE CE0L OF LMDZ. Resolution = ${resol_atm}"   >> $outfile 
    
    echo ./makelmdz_fcm -d ${resol_atm} $opt_rad -$optmode -arch $fcm_arch -arch_path $arch_path -j 8 $full_lmdz ce0l    >> $outfile 
    ./makelmdz_fcm -d ${resol_atm} $opt_rad -$optmode -arch $fcm_arch -arch_path $arch_path -j 8 $full_lmdz ce0l    >> $outfile 2>&1

# 1er aout - remove xios from ce0l compilation #
#    echo ./makelmdz_fcm -d ${resol_atm} $opt_rad -$optmode -io xios -arch $fcm_arch -arch_path $arch_path -j 8 $full_lmdz ce0l    >> $outfile 
#    ./makelmdz_fcm -d ${resol_atm} $opt_rad -$optmode -io xios -arch $fcm_arch -arch_path $arch_path -j 8 $full_lmdz ce0l    >> $outfile 2>&1

    # Test if compiling finished
    if [[ $? != 0 ]] ; then
	echo "THERE IS A PROBLEM IN CE0L (LMDZ) REGULAR LATLON COMPILATION - STOP"
	exit
    fi


    # Move executables to modipsl/bin folder
    echo >> $outfile
    echo "Move ce0l.e executable to modipsl/bin" >> $outfile
    echo ls -lrt $modipsl/modeles/LMDZ/bin   >> $outfile
    ls -lrt $modipsl/modeles/LMDZ/bin  >> $outfile
    echo >> $outfile

    suffix=_${resol_atm}_phylmd_seq.e
    if [ $lmdzsvn -ge 4186 ] ; then suffix=_${resol_atm}_phylmd_${rad}_seq.e ; fi
    echo ce0l suffix = $suffix

    if [ -f $modipsl/modeles/LMDZ/bin/ce0l${suffix} ] ;  then 
	mv $modipsl/modeles/LMDZ/bin/ce0l${suffix} $modipsl/bin/ce0l_${resol_atm}_${optmode}.e
    else
	echo "ERROR ce0l${suffix} executable does not exist." 
	echo "THERE IS A PROBLEM IN CE0L (LMDZ) REGULAR LATLON COMPILATION - STOP"
	exit
    fi  
fi

echo >>$outfile ; echo "ALL COMPILING FINISHED" >> $outfile
echo ls -lrt modipsl/bin >> $outfile
ls -lrt $modipsl/bin >> $outfile

echo; echo "ALL COMPILING FINISHED" ; echo
echo "Executables are found in modipsl/bin"
echo "Check that executable names correspond with the name set in config.card before launching the job"
echo ls -lrt modipsl/bin
ls -lrt $modipsl/bin

date

exit


