#! /bin/bash


##################################
# makemeso allows you to compile # 
# WRF with modified LMD physics  #
##################################

##############################
# Author: A. Spiga           #
# New version : October 2008 #
# Last update : January 2009 #
#               November  09 # 
#               January   11 #
##############################

###############################
#### Type makemeso -h for help
###############################



##############################################################################################
##############################################################################################
##############################################################################################
##############################################################################################

donotcompile=0
donotallow=0
config=''
donotcompilephys=0
justphys=0
debug=0
fresh_start=0
phys=""
scenario=""
from_scratch=0
while getopts "drc:njhgpfs:xe" options; do
  case $options in
   d ) donotcompile=1;;       ## just to check the compile folder
   r ) donotallow=1;;         ## allow only known config (useful with 'makemeso < last')
   c ) config="${OPTARG}";;   ## idealized cases
   n ) donotcompilephys=1;;   ## do not recompile physics
   j ) justphys=1;;           ## just compile LMD physics
   g ) debug=1;;              ## debug mode
   p ) phys="newphys_";;      ## with new physics
   f ) fresh_start=1;;        ## a fresh start
   e ) from_scratch=1;;       ## a fresh start with a completely new folder
   s ) scenario="${OPTARG}";; ## a specific scenario, you need a corresponding "mars_lmd_..."
   x ) donotcompile=1;phys="nophys_";donotcompilephys=1;; ## a case with no LMD physics included
   h ) echo "
# Use:    
#
# makemeso                   ## basic use (real-case configuration)
#
# makemeso -d                ## no compilation, just check the name of the compile folder
#
# makemeso -c ideal          ## idealized mode (convective cell, mountain wave, etc...)
# makemeso -c les            ## large-eddy simulations mode based on WRFV3
#
# makemeso -n                ## do not recompile LMD physics (must have been compiled before)
#
# makemeso < last            ## basic use + skip questions [! script must have been executed at least once]
# makemeso -r < last         ## basic use + skip questions + only known config
# makemeso -nr < last        ## basic use + skip questions + only known config + no LMD phys recompile
#
# makemeso -j                ## just compile the LMD physics
#
# makemeso -g                ## debug mode
#
# makemeso -h                ## display options
#
# makemeso -p                ## with new LMD physics
#
# makemeso -f                ## fresh start [clean -a]
#
# makemeso -e                ## a completely new recompile with erasing ALL compiling folder
#
# makemeso -s storm          ## a specific scenario, you need a corresponding mars_lmd_... (only for newphys)
#
# makemeso -x                ## a case with no LMD physics included
" ; exit ;;
  esac
done   

#--------------
# talk w/ user
#--------------
  echo '****************************************'
  echo ' LMD Mesoscale Model Compiler. Welcome.'
  echo '****************************************'
  if [[ "${phys}" == "newphys_" ]]
  then
    echo '***********with new physics*************'
    echo '****************************************'
  fi
  # computer
  uname -a | grep x86_64 > /dev/null
  if [[ "$?" == 0 ]]
  then
    machine='64'
  else
    machine='32'
  fi
  # compiler
  echo "Supported compiler options are "
  echo "       <1> pgf90"
  echo "       <2> g95"
  echo "       <3> pgf90 + mpi"
  echo "       <4> ifort"
  echo "       <5> ifort + mpi"
  echo "       <7> xlf + mpi (IBM AIX)"
  echo "Your choice ?" ; read reply
  case ${reply} in
    1) compilo='pgf' ; numproc=1 ;;
    2) compilo='g95' ; numproc=1 ;;
    3) compilo='mpi'
       if [[ "${WHERE_MPI}" = "" ]] 
       then
         echo Please initialize the variable WHERE_MPI in your environnement
         exit
       fi 
       echo How many processors ? e.g. 1, 2, 4, 6, 8, 12, 16, 20, 24, 32, 64, 128 ; read numproc ;;
    4) compilo='ifort' ; numproc=1 ;;
    5) compilo='mpifort' ; echo How many processors ? e.g. 1, 2, 4, 6, 8, 12, 16, 20, 24, 32, 64, 128 ; read numproc ;;
    7) compilo='mpixlf' ; echo How many processors ? e.g. 1, 2, 4, 6, 8, 12, 16, 20, 24, 32, 64, 128 ; read numproc ;;
### for tests 
99) compilo='gnu' ; numproc=1 ;;
    *) echo not supported by this script ; exit ;;
  esac
  # dimensions
  if [ ${donotcompile} -eq 0 ]
  then	  
  echo Grid points in longitude ? ; read lon 
  echo Grid points in latitude ? ; read lat 
  # ngrid: now dynamically set in newphys 03/2014
   if [[ "${phys}" == "newphys_" ]]
   then
      lon=999
      lat=999
      numproc=999
   fi
  echo Number of vertical levels ? ; read level 
  fi
  echo Number of domains ? ; read dom 

###PB lecture dom si < last

    case ${dom} in
       1) single='_single' ;;
       *) single='_nest' ;;
    esac
      if [ "${compilo}" = "mpi" -o "${compilo}" = "mpifort" -o "${compilo}" = "mpixlf" ]
      then
           single=''
      fi

testflag=''
####
#testflag='_test'
####

    conf_wrf="${config}${scenario}${phys}${compilo}_${machine}${single}${testflag}"

    if [ ${debug} -eq 1 ]
    then
     conf_wrf="debug_${conf_wrf}"
    fi

    \rm what_folder 2> /dev/null
    echo ${conf_wrf} > what_folder
         if [ ${donotcompile} -eq 1 ]
         then
            #\rm what_folder 2> /dev/null
            #echo '**********************' 
            #echo '*** Your folder is ...'
            #echo '**********************'
            #echo ${conf_wrf} | tee what_folder
            #cat what_folder
            #echo ${conf_wrf} > what_folder
	    #echo ${reply} > what_compilo
	    #echo ${numproc} > what_numproc
            if [[ "${phys}" != "nophys_" ]]
            then
              exit
            fi
         else
            echo Number of tracers ? ; read tra
            # tracers: now dynamically set in newphys 09/2013
            if [[ "${phys}" == "newphys_" ]]
            then
              tra=999
            else
              if [ ${tra} -eq 0 ]
              then 
                tra=1	 
              fi
            fi
            # scatterers: a specific stuff for newphys
            if [[ "${phys}" == "newphys_" ]]
            then
              echo "How many types of scatterer are you using ? 1 [dust] or 2 [dust + water ice]" ; read scat
            else
              scat=1 ## dummy stuff
            fi
         fi
  # 'from scratch' case
  if [ ${from_scratch} -eq 1 ]
  then
      echo "***** I ERASE THE FOLDER "${conf_wrf}
      \rm -rf ${conf_wrf}
  fi
  # folder
  mkdir ${conf_wrf} 2> /dev/null
  if [[ "$?" == 0 ]]
  then
    echo new folder ... link sources
    if [[ "${phys}" == "newphys_" ]]
    then
     \rm copy_model_tmp > /dev/null 2> /dev/null
        if [[ "${scenario}" == "" ]]
        then
          sed s+"mars_lmd"+"mars_lmd_new"+g             SRC/SCRIPTS/copy_model > copy_model_tmp
        else
          echo SCENARIO ${scenario} DID YOU INCLUDE THE SPECIFIC folder ? ; read dummy 
          sed s+"mars_lmd"+"mars_lmd_new_${scenario}"+g SRC/SCRIPTS/copy_model > copy_model_tmp
             if [[ "${config}" == "les" ]] 
             then
              echo "NOT SUPPORTED, check options" 
              exit
             fi
        fi
        if [[ "${config}" == "les" ]] 
        then
         sed s+"PWD/SRC/"+"PWD/SRC/LES/"+g copy_model_tmp > yeah
         mv yeah copy_model_tmp
        fi
     chmod 755 copy_model_tmp
     ./copy_model_tmp
     \rm copy_model_tmp
     mv zeWRFV2 ${conf_wrf}/WRFV2
     cd ${conf_wrf}/WRFV2
        if [[ "${scenario}" == "" ]]
        then
          ln -sf mars_lmd_new mars_lmd 
          if [[ "${phys}" == "newphys_" ]]
          then
              cp ../../SRC/WRFV2/Registry/Registry.EM.newphys Registry/Registry.EM
          fi
          cd phys
          ln -sf module_lmd_driver.F.new module_lmd_driver.F
          cd ..
        else
          ln -sf mars_lmd_new_${scenario} mars_lmd 
          cp ../../SRC/WRFV2/mars_lmd_new_${scenario}/Registry.EM mars_lmd_new_${scenario}/
          cp ../../SRC/WRFV2/mars_lmd_new_${scenario}/*.inc mars_lmd_new_${scenario}/
          cd Registry ; ln -sf ../mars_lmd_new_${scenario}/Registry.EM . ; cd ..
        fi 
     #ln -sf meso_callkeys_newphys.h meso_callkeys.h
     cd Registry ; Registry.bash ; cd ..

     # remove files adapted to parallel GCM and useless for mesoscale
     cd mars_lmd
     cd libf/phymars
     \rm tabfi* soil_settings* phyetat0* phyredem* iostart* writediagfi* mkstat* writediagsoil* eofdump* wstats* inistats* mod_*
     \rm iniphysiq* init_phys_lmdz* comgeomphy* planetwide_mod*
     mv ../dyn3d/control_mod.F90 .
     cd ../..
     cd ..

    else
         if [[ "${config}" == "les" ]] 
         then
           ### NB: With physics: LES folder // Without physics : LESnophys_ folder
           ### ---- because differences in 'modif' folder ----
           sed s+"PWD/SRC/"+"PWD/SRC/LES$phys/"+g SRC/SCRIPTS/copy_model > copy_model_tmp
         else
           cp SRC/SCRIPTS/copy_model copy_model_tmp
         fi   
     chmod 755 copy_model_tmp
     ./copy_model_tmp
     \rm copy_model_tmp
     mv zeWRFV2 ${conf_wrf}/WRFV2
     cd ${conf_wrf}/WRFV2
     cd Registry ; ./Registry.bash ; cd .. 
     fi
          #### sparadrap consequent a l'utilisation de copy_model pour les liens
          #### -- car alors il manque fftpack
          if [[ "${config}" == "les" ]]
          then 
            if [[ "${phys}" == "nophys_" ]]
            then
              ## voir ci-dessus pour le folder
              cp ../../SRC/LES$phys/correcfft ./
            else
              cp ../../SRC/LES/correcfft ./
            fi  
            ./correcfft
            \rm correcfft
          fi
  else
    cd ${conf_wrf}/WRFV2 
    if [ ${fresh_start} -eq 1 ] 
    then
      echo '*** FRESH START, I clean everything'
      ### clean the whole place  
      clean -a > /dev/null 2> /dev/null
      ### get a possibly modified registry !
      if [[ "${scenario}" == "" ]]
      then
        if [[ "${phys}" == "newphys_" ]]
        then
            cp ../../SRC/WRFV2/Registry/Registry.EM.newphys Registry/Registry.EM
        else
            cp ../../SRC/WRFV2/Registry/Registry.EM Registry/
        fi
      fi
      if [[ "${config}" == "les" ]]
      then
        cp ../../SRC/LES/WRFV2/Registry/Registry.EM Registry/
      fi
      ### ensure the model will be recompiled from scratch with correct registry
      cd Registry ; mv Registry Registry.bak ; Registry.bash ; cd ..
#     answer='y' ## a voir... pas si sur
#    else
#            if [[ "${phys}" != "nophys_" ]]
#            then
#      echo Did you modify anything in the Registry or clean ? y for yes, any key for no ; read answer
#      case ${answer} in
#        y) cd Registry ; mv Registry Registry.bak ; Registry.bash ; cd .. ;;
#        *) answer='no' ;;
#      esac
#            fi
    fi 
  fi

  # summary
  echo '**********************' 
  echo '*** Your folder is ...'
  echo '**********************'
  echo ${conf_wrf} 
  echo '**********************'
  echo '****************************************'
  echo so ...
  echo your computer is ${machine} bits
  echo ${compilo} is your compiler
       if [[ "${compilo}" = "mpi" ]]
       then
         echo MPICH is in ${WHERE_MPI}
       fi
  echo you will use ${numproc} processors
  echo you have ${lon} x points
  echo '         '${lat} y points
  echo '         '${level} z points
  echo '         '${dom} domains
  echo '         '${tra} tracers
  if [[ "${phys}" == "newphys_" ]]
  then
    echo '         '${scat} scatterers
  fi
  echo '****************************************'
  # save answer
  \rm last 2> /dev/null
  touch last
  echo ${reply} >> last
     if [ "${compilo}" = "mpi" -o "${compilo}" = "mpifort" -o "${compilo}" = "mpixlf" ]
     then
     echo ${numproc} >> last
     fi
  echo ${lon} >> last
  echo ${lat} >> last
  echo ${level} >> last
  echo ${dom} >> last
  echo ${tra} >> last
  echo ${scat} >> last
  #echo ${answer} >> last

#------------
# log files
#------------
  \rm log_compile 2> /dev/null
  \rm log_error 2> /dev/null

#-----------------
# configure WRF
#-----------------
  mv configure.wrf configure.wrf.bak 2> /dev/null
  ########################
  conf_wrf_forall="${compilo}_${machine}${single}${testflag}"

         #### in this case options are different because for LES, WRFV3 is used
         if [[ "${config}" == "les" ]]
         then
         conf_wrf_forall="${config}${compilo}_${machine}${single}${testflag}"
         fi

  case ${conf_wrf_forall} in

     #######LES-specific (WRFV3-based) -- previously in 'makeles'
     #######

     lesmpi_64)        ## MPI (dm) 64 bits [PS: remplacer 3 par 4 pour openMP]
                       \rm conf > /dev/null 2> /dev/null ; touch conf ; echo 3 >> conf ; echo 1 >> conf
                       \rm configure.wrf > /dev/null 2> /dev/null ; ./configure < conf > /dev/null 2> /dev/null
                       sed s+"-lnetcdf"+"-lnetcdf -L../mars_lmd/libo -llmd"+g   configure.wrf > yeah ; mv -f yeah configure.wrf
                       if [[ "$(hostname)" == "ciclad1.ipsl.jussieu.fr" ]]
                       then
            echo "SPECIFIC CHANGES FOR CICLAD CLUSTER. EDIT makemeso IF YOU ENCOUNTER PROBLEMS."
            #sed s+"mpif90 -f90=$(SFC)"+"/usr/lib64/openmpi/1.4.2-pgf/bin/mpif90"+g                      configure.wrf > yeah ; mv -f yeah configure.wrf
            #sed s+"mpicc -cc=$(SCC)"+"/usr/lib64/openmpi/1.4.2-gfortran/bin/mpicc -DMPI2_SUPPORT"+g     configure.wrf > yeah ; mv -f yeah configure.wrf
            sed s+"mpif90 -f90=\$(SFC)"+"/usr/lib64/openmpi/1.4.3-pgfgcc/bin/mpif90"+g                   configure.wrf > yeah ; mv -f yeah configure.wrf 
            sed s+"mpicc -cc=\$(SCC)"+"/usr/lib64/openmpi/1.4.3-pgfgcc/bin/mpicc -DMPI2_SUPPORT"+g       configure.wrf > yeah ; mv -f yeah configure.wrf
                       else
            #sed s+"mpif90"+"$WHERE_MPI/mpif90"+g                 configure.wrf > yeah ; mv -f yeah configure.wrf
            #sed s+"mpicc"+"$WHERE_MPI/mpicc -DMPI2_SUPPORT"+g    configure.wrf > yeah ; mv -f yeah configure.wrf
            #### OPENMPI
            sed s+"mpif90 -f90=\$(SFC)"+"$WHERE_MPI/mpif90"+g                 configure.wrf > yeah ; mv -f yeah configure.wrf
            sed s+"mpicc -cc=\$(SCC)"+"$WHERE_MPI/mpicc -DMPI2_SUPPORT"+g    configure.wrf > yeah ; mv -f yeah configure.wrf
                       fi
                       sed s+"-fastsse"+" "+g                               configure.wrf > yeah ; mv -f yeah configure.wrf
                          ### pas forcement necessaire ici mais OK
                          sed s+"-llmd"+"-llmd $NETCDF/lib/libnetcdf.a"+g     configure.wrf > yeah ; mv -f yeah configure.wrf 
                       \rm conf > /dev/null ;;

     lesmpifort_64)    ## MPI (dm) 64 bits IFORT
                       \rm conf > /dev/null 2> /dev/null ; touch conf ; echo 7 >> conf ; echo 1 >> conf
                       \rm configure.wrf > /dev/null 2> /dev/null ; ./configure < conf > /dev/null 2> /dev/null
                       sed s+"-lnetcdf"+"-lnetcdf -L../mars_lmd/libo -llmd"+g   configure.wrf > yeah ; mv -f yeah configure.wrf
                       sed s+"mpif90"+"$WHERE_MPI/mpif90"+g                 configure.wrf > yeah ; mv -f yeah configure.wrf
                       sed s+"mpicc"+"$WHERE_MPI/mpicc -DMPI2_SUPPORT"+g    configure.wrf > yeah ; mv -f yeah configure.wrf
                       sed s+"-fastsse"+" "+g                               configure.wrf > yeah ; mv -f yeah configure.wrf
                       sed s+"O3"+"O3 -mcmodel=large -shared-intel"+g configure.wrf > yeah ; mv -f yeah configure.wrf 
                       sed s+"-llmd"+"-llmd $NETCDF/lib/libnetcdf.a"+g     configure.wrf > yeah ; mv -f yeah configure.wrf
                       if [ ${debug} -ne 0 ]   # not working for xlf!
                             then
                             echo 'DEBUG DEBUG DEBUG DEBUG'
                             sed s+"# -g"+" -g -no-ftz -traceback -ftrapuv -fp-stack-check -check"+g configure.wrf > yeah
                             mv -f yeah configure.wrf
                       fi
                       \rm conf > /dev/null ;;

     #######
     #######LES-specific (WRFV3-based) -- previously in 'makeles'    

#######TEST TEST
                     # GFORTRAN, 64 bits, no nesting      
     gnu_64_single)    cd arch ; ln -sf ../configure.defaults.gfortran64 configure.defaults ; cd ..
                       echo 15 | configure > log_compile 2> log_error
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf  
                       sed s+"-L../mars_lmd/libo -llmd"+"-L../mars_lmd/libo -llmd $NETCDF/lib/libnetcdf.a"+g configure.wrf > yeah ; mv -f yeah configure.wrf ;;
#######TEST TEST
                     # PGF90, 32 bits, no nesting
     pgf_32_single)    echo 1 | configure > log_compile 2> log_error 
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf ;;
                     # PGF90, 32 bits, nesting
     pgf_32_nest)      echo 2 | configure > log_compile 2> log_error 
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf ;;
                     # PGF90, 64 bits, no nesting
     pgf_64_single)    echo 1 | configure > log_compile 2> log_error 
                       #### the following line is necessary because readtesassim has a line "use netcdf" in it!
                       #sed s+"-L../mars_lmd/libo -llmd"+"-L../mars_lmd/libo -llmd $NETCDF/lib/libnetcdf.a"+g configure.wrf > yeah ; mv -f yeah configure.wrf
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf ;;
                     # PGF90, 64 bits, nesting
     pgf_64_nest)      cd arch ; ln -sf ../configure.defaults.fix64nest configure.defaults ; cd ..
		       echo 4 | configure > log_compile 2> log_error 
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf ;;
                     # G95, 32 bits, no nesting
     g95_32_single)    echo 13 | configure > log_compile 2> log_error 
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf ;; 
                     # G95, 64 bits, no nesting  ### we modify configure.defaults to add x86_64 next to g95 in the header comment
     g95_64_single)    cd arch ; sed s/"PC Linux i486 i586 i686, g95 compiler"/"PC Linux i486 i586 i686 x86_64, g95 compiler"/g configure.defaults > yeahyeahyeah ; rm configure.defaults ; mv yeahyeahyeah configure.defaults ; cd ..
                       echo 14 | configure > log_compile 2> log_error
                         ##### marche pas ....
                         #sed s+"-fno-second-underscore"+""+g configure.wrf > yeah ; mv -f yeah configure.wrf
                         #sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf
                         #sed s+"-L../mars_lmd/libo -llmd"+"-L../mars_lmd/libo -llmd $NETCDF/lib/libnetcdf.a"+g configure.wrf > yeah ; mv -f yeah configure.wrf ;;
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf ;;
                       #sed s+"=       g95"+"=       /home/physastro/aspiga/mysoft/g95/g95-install/bin/x86_64-unknown-linux-gnu-g95"+g configure.wrf > yeah ; mv -f yeah configure.wrf;;
                     # IFORT, 64 bits, no nesting
     ifort_64_single)  echo 5 | configure > log_compile 2> log_error 
                       sed s+"-DIFORT_KLUDGE"+" "+g configure.wrf > yeah ; mv -f yeah configure.wrf 
                       if [ ${debug} -ne 0 ]   # not working for xlf!
                             then
                             echo 'DEBUG DEBUG DEBUG DEBUG'
                             sed s+"# -g"+" -g -no-ftz -traceback -ftrapuv -fp-stack-check -check"+g configure.wrf > yeah
                             mv -f yeah configure.wrf
                       fi
                         ## !!! NETCDF must be defined
                         sed s+"lio_grib_share"+"lio_grib_share -L../mars_lmd/libo -llmd $NETCDF/lib/libnetcdf.a"+g configure.wrf > yeah 
                         mv -f yeah configure.wrf ;;
     mpifort_64)     # MPI+IFORT, 64 bits, no nesting / nesting
                       echo 9 | configure > log_compile 2> log_error 
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf 
                       sed s+"-f90=ifort"+" "+g configure.wrf > yeah ; mv -f yeah configure.wrf 
                       sed s+"-cc=icc"+" "+g configure.wrf > yeah ; mv -f yeah configure.wrf 
                       sed s+"-DIFORT_KLUDGE"+" "+g configure.wrf > yeah ; mv -f yeah configure.wrf 
                       if [ ${debug} -ne 0 ]   # not working for xlf!
                             then
                             echo 'DEBUG DEBUG DEBUG DEBUG'
                             sed s+"# -g"+" -g -no-ftz -traceback -ftrapuv -fp-stack-check -check"+g configure.wrf > yeah
                             mv -f yeah configure.wrf
                       fi
                       sed s+"O3"+"O2"+g configure.wrf > yeah ; mv -f yeah configure.wrf 
                                        #sed s+"O3"+"O0"+g configure.wrf > yeah ; mv -f yeah configure.wrf  ### pour compilation rapide
                                        #-w -ftz -align all -fno-alias -fp-model precise >>> options indiquees dans WRF3
                                        #http://software.intel.com/en-us/articles/performance-tools-for-software-developers-building-wrf-with-the-intel-compilers/
                            #### NB: RSL is replaced by RSL_LITE, better memory capacity, less seg fault
                            sed s+"../external/RSL/RSL"+"../external/RSL_LITE"+g configure.wrf > yeah ; mv -f yeah configure.wrf
                            sed s+"-DWRF_RSL_IO"+" "+g configure.wrf > yeah ; mv -f yeah configure.wrf
                            sed s+"-DRSL"+" "+g configure.wrf > yeah ; mv -f yeah configure.wrf
                            sed s+"librsl.a"+"librsl_lite.a"+g configure.wrf > yeah ; mv -f yeah configure.wrf
                            sed s+"../external/RSL/gen"+"../external/RSL_LITE/gen"+g configure.wrf > yeah ; mv -f yeah configure.wrf
                            sed s+"../external/RSL/module"+"../external/RSL_LITE/module"+g configure.wrf > yeah ; mv -f yeah configure.wrf
                            sed s+"-lrsl"+"-lrsl_lite"+g configure.wrf > yeah ; mv -f yeah configure.wrf
                            sed s+"linux"+" "+g configure.wrf > yeah ; mv -f yeah configure.wrf
                         ### necessary even if mpi-selector is used (no need in makegcm though)
                         sed s+"mpif90"+"${WHERE_MPI}/mpif90"+g configure.wrf | sed s+"mpicc"+"${WHERE_MPI}/mpicc"+g > yeah ; mv -f yeah configure.wrf
                         sed s+"mpicc"+"mpicc -DMPI2_SUPPORT"+g configure.wrf > yeah ; mv -f yeah configure.wrf
                         ## !!! NETCDF must be defined
                         sed s+"-L../mars_lmd/libo -llmd"+"-L../mars_lmd/libo -llmd $NETCDF/lib/libnetcdf.a"+g configure.wrf > yeah 
                         mv -f yeah configure.wrf 
                         #### POUR LE TRAITEMENT PARTICULIERS des NESTS sur iDATAPLEX [cf. module_lmd_driver]
                         sed s+"ARCHFLAGS       =       "+"ARCHFLAGS       =       -DSPECIAL_NEST_SAVE "+g configure.wrf > yeah
                         mv -f yeah configure.wrf ;;
                     # MPICH, 64 bits, no nesting / nesting
     mpi_64)           cd arch ; ln -sf ../configure.defaults.fix64nest configure.defaults ; cd ..
                       echo 3 | configure > log_compile 2> log_error 
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf 
                       ### the following line is necessary because readtesassim has a line "use netcdf" in it!
                       sed s+"-L../mars_lmd/libo -llmd"+"-L../mars_lmd/libo -llmd $NETCDF/lib/libnetcdf.a"+g configure.wrf > yeah ; mv -f yeah configure.wrf
                       sed s+"mpif90"+"${WHERE_MPI}/mpif90"+g configure.wrf | sed s+"mpicc"+"${WHERE_MPI}/mpicc"+g | sed s+"mpicc"+"mpicc -DMPI2_SUPPORT"+g > yeah
                       #sed s+"mpif90"+"${WHERE_MPI}/mpif90"+g configure.wrf | sed s+"mpicc"+"${WHERE_MPI}/mpicc"+g | sed s+"mpicc"+"mpicc -DMPI2_SUPPORT -w -O3"+g > yeah
                                #| sed s+"fastsse"+"fast"+g > yeah
                       mv -f yeah configure.wrf ;;
                       #    #sed s+"-fastsse"+"-O2 -Munroll -Mcache_align"+g configure.wrf > yeah 
                       #    ##sed s+"-fastsse"+"-O2 -fpic"+g configure.wrf > yeah ## marche pas, ILM problem
                       #    ##sed s+"-fastsse"+"-mcmodel=medium -Mlarge_arrays"+g configure.wrf > yeah ## marche pas, ILM problem
                       #sed s+"-fastsse"+"-O3 -tp=nehalem-64 -fastsse -Mscalarsse -Mvect=sse -Mipa=fast"+g configure.wrf > yeah
                       #sed s+"-fastsse"+"-O2 -Munroll -Mcache_align"+g configure.wrf > yeah
                       #sed s+"-fastsse"+""+g configure.wrf > yeah
                       #sed s+"-fastsse"+"-fast"+g configure.wrf > yeah
                       #sed s+"-fastsse"+"-Mvect=noaltcode -Msmartalloc -Mprefetch=distance:8 -Mfprelaxed"+g configure.wrf > yeah
                       #mv -f yeah configure.wrf ;; 
                     # MPICH, 64 bits, OK with periodic BC but no positive definite
     mpi_64_test)      cd arch ; ln -sf ../configure.defaults.fix64nest configure.defaults ; cd ..
                       echo 2 | configure > log_compile 2> log_error 
                       sed -f mars.sed configure.wrf > yeah ; mv -f yeah configure.wrf 
                       sed s+"mpif90"+"${WHERE_MPI}/mpif90"+g configure.wrf | sed s+"mpicc"+"${WHERE_MPI}/mpicc"+g | sed s+"mpicc"+"mpicc -DMPI2_SUPPORT"+g > yeah
                       mv -f yeah configure.wrf ;;
                     # XLF+MPICH on AIX machine (64 bits) with nesting, NB: the 32bits notice is not to be considered
     mpixlf_32)        cd arch ; ln -sf ../configure.defaults.fix64nest configure.defaults ; cd ..
                       echo 3 | configure > log_compile 2> log_error
                       sed -f mars.sed configure.wrf > yeah 
                       mv -f yeah configure.wrf ;;
                     # ANYTHING ELSE
     *)                echo NO PRESETS ...
                       if [ ${donotallow} -eq 0 ]  
                       then
                         configure 
                       else
                         # problem when an input file is used
                         echo 'please cd to '$PWD' and type ./configure'  
		         exit	
                       fi ;;
  esac
  ########################


  ### here a case structure would be great

  if [[ "${phys}" == "newphys_" ]]
  then
     if [[ "${config}" == "les" ]]  ### LES is different because of WRFV3
     then
       sed s+"ARCH_LOCAL      =       "+"ARCH_LOCAL      =       -DNEWPHYS "+g configure.wrf > yeah
     else
       sed s+"ARCHFLAGS       =       "+"ARCHFLAGS       =       -DNEWPHYS "+g configure.wrf > yeah 
     fi
     mv -f yeah configure.wrf
  fi

        if [[ ! ("${scenario}" == "") ]]   ### not supported with LES for the moment
        then
          sed s+"ARCHFLAGS       =       "+"ARCHFLAGS       =       -D${scenario} "+g configure.wrf > yeah
          mv -f yeah configure.wrf
        fi

  ################
  if [[ "${phys}" == "nophys_" ]]
  then
     if [[ "${config}" == "les" ]]  ### LES is different because of WRFV3
     then
       sed s+"ARCH_LOCAL      =       "+"ARCH_LOCAL      =       -DNOPHYS "+g configure.wrf > yeah ; mv -f yeah configure.wrf
       sed s+"-L../mars_lmd/libo -llmd"+""+g configure.wrf > yeah ; mv -f yeah configure.wrf
     else
       ### not tested yet but should be working  
       echo CAUTION CAUTION CAUTION NOT FULLY TESTED
       sed s+"ARCHFLAGS       =       "+"ARCHFLAGS       =       -DNOPHYS "+g configure.wrf > yeah ; mv -f yeah configure.wrf
       sed s+"-L../mars_lmd/libo -llmd"+""+g configure.wrf > yeah ; mv -f yeah configure.wrf
     fi
  fi
  ################

  if [ ${debug} -ne 0 ]   # not working for xlf!
  then
    echo 'DEBUG DEBUG DEBUG DEBUG'
    sed s+"# -g"+" -g -no-ftz -traceback -ftrapuv -fp-stack-check -check"+g configure.wrf > yeah
    #sed s+"# -g"+"-g -Ktrap=fp -Mbounds"+g configure.wrf > yeah 
    #sed s+"# -g"+"-g"+g configure.wrf > yeah
    mv -f yeah configure.wrf
  fi


##################################################
# compile physics
##################################################
if [ ${donotcompilephys} -eq 0 ]
then

  cd mars_lmd/

 if [[ "${phys}" == "newphys_" ]]
 then
  divx=1
  divy=1
 else
  # required size
  #----------------
  case ${numproc} in
      1) divx=1 ; divy=1  ;;
      2) divx=1 ; divy=2  ;;
      4) divx=2 ; divy=2  ;;
      6) divx=2 ; divy=3  ;;
      8) divx=2 ; divy=4  ;;
     12) divx=3 ; divy=4  ;;
     16) divx=4 ; divy=4  ;;
     20) divx=4 ; divy=5  ;;
     24) divx=4 ; divy=6  ;;
     32) divx=4 ; divy=8  ;;
     64) divx=8 ; divy=8  ;;
    128) divx=8 ; divy=16 ;;
      *) echo not yet supported ; exit ;;
  esac
 fi
  physx=$(expr ${lon} - 1)
  restex=$(expr ${physx} \% ${divx})   
  physy=$(expr ${lat} - 1)
  restey=$(expr ${physy} \% ${divy})
  if [[ ${restex} != 0 || ${restey} != 0 ]]
  then
     echo nx-1 shall be divided by ${divx}
     echo ny-1 shall be divided by ${divy}
     exit
  fi
  physx=$(expr ${physx} \/ ${divx})
  physy=$(expr ${physy} \/ ${divy})
  physz=$(expr ${level} - 1)

  # change this if you change num_soil_layers in WRF
  # -- default is 10
  if [[ "${phys}" == "newphys_" ]]
  then
     soilsize=18 ## nouvelle physique. ne sert a rien ici. voir comsoil.
  else
     soilsize=10
  fi

  # GCM environment variables
  #--------------------------
  export LMDGCM=$PWD
  export LIBOGCM=$PWD/libo

  # generate the appropriate dimphys
  #---------------------------------
  cd libf/phymars
  \rm dimphys.h 2> /dev/null
  if [[ "${phys}" == "newphys_" ]]
  then
    ln -sf meso_inc/meso_dimphys.h_ref .
  fi
  physize=$(expr ${physx} \* ${physy})
  sed s/--xsize--/${physx}/g meso_dimphys.h_ref | sed s/--ysize--/${physy}/g | sed s/--physize--/${physize}/g | sed s/--zsize--/${physz}/g | sed s/--soilsize--/${soilsize}/g > dimphys.h
  head -15 dimphys.h
     ### TEST new new phys
     if [[ "${phys}" == "newphys_" ]]
     then
       touch gr_fi_dyn.F.lien
         ln -sf ../dyn3d/gr_fi_dyn.F .  ## dommage, a corriger 
     ###############################
\rm scatterers.h
cat << EOF > scatterers.h
!-----------------------------------------------------------------------
!   INCLUDE 'scatterers.h'
!
! Number of kind of tracer radiative properties 
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! (ex: naerkind=1 if you use one dust mode without ice ...)
! (ex: naerkind=2 if you use one dust mode and active ice ...)
!-----------------------------------------------------------------------

      integer, parameter :: naerkind=${scat}

!-----------------------------------------------------------------------
EOF
     ###############################
     ###############################
     fi 
     ###
     ###
  cd ../..

  # prepare for nesting
  #---------------------
  cd libf
  duplicate${dom} 2> /dev/null
  cd ..

  # compile physics
  #-------------------------- 
  \rm libf/grid/dimensions.h 2> /dev/null
  \rm -rf libo/* 2> /dev/null
  echo 1. compiling LMD physics ...
  echo compilation info in: 
  echo $PWD/libo/log_compile_phys

  if [ ${debug} -ne 0 ]
  then
    echo 'DEBUG DEBUG DEBUG DEBUG'
    nohup makegcm_${compilo} -debug -t ${tra} -p mars -d ${physz} testphys1d | tee libo/log_compile_phys | grep 'warnings' >> libo/log_compile_phys 
  else
    nohup makegcm_${compilo} -t ${tra} -p mars -d ${physz} testphys1d | tee libo/log_compile_phys | grep 'warnings' >> libo/log_compile_phys
  fi
  echo ... done
        # clean the duplicate routines
        cd libf
        duplicate1 2> /dev/null
        cd ..

  # merge LMD executables in one lib
  #--------------------------------------
  cd libo
  mkdir temp
  #cp -f LINUXfastI._${physz}_t${tra}_reg/*.a temp
  cp -f LINUX*/*.a temp/
  cd temp
  ar x libbibio.a
  ar x libphymars.a
  ar x libaeronomars.a
  \rm *.a
  ar r liblmd.a *
  cp -f liblmd.a ..
  cd ..
  \rm -r temp
  nm liblmd.a > liblmd_content
  # finish merge
  cd ..

  if [[ "${phys}" == "newphys_" ]]
  then
    # include modules for interfacing
    mv *.mod ../inc/
  fi

  # save a copy 
  #--------------
  cp -f libo/liblmd.a libo/liblmd.a_${lon}_${lat}_${level}_${dom}_${tra}_${scat} 
  echo '****************************************'

  # ok
  #----
  cd ..

  # manage nest includes in module_lmd_driver.F
  #---- 
  cp call_meso_inifis$dom.inc call_meso_inifis.inc
  cp call_meso_physiq$dom.inc call_meso_physiq.inc


fi
##################################################
# END compile physics
##################################################

#------------------
# compile WRF
#------------------
if [ ${justphys} -eq 0 ]
then

  echo 2. compiling WRF dynamical core ...

  if [[ "${phys}" == "nophys_" ]]
  then
   echo 'NO LMD PHYSICS included'
  else
   if [[ ! ( -f "call_meso_physiq.inc" ) ]]
   then
    echo 'did you compile the physics ? no call_meso_physiq.inc !'
    exit
   fi
  fi

  # be sure to compile with the most recent physics
  touch phys/module_lmd_driver.F

  # talk to user
  echo '>>> compiling ... this may be long ... <<<'
  echo check progress in:
  echo $PWD/log_compile
  echo check possible errors in:
  echo $PWD/log_error
  
  # compile ...
  case ${config} in
    '')         ./compile em_real > log_compile 2> log_error 
                    # save executables  
                    cd main
                      if [[ -f real.exe ]]
                      then
                        echo 'Looks good ! real.exe is here...' 
                      fi
                      if [[ -f wrf.exe ]]
                      then
                        echo 'Looks good ! wrf.exe is here...' 
                      fi
                    cp -f real.exe ../../real_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}_s${scat}.exe
                    cp -f wrf.exe ../../wrf_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}_s${scat}.exe
                    cd ..  ;;
    'ideal')        #mkdir 'test/em_quarter_ss' 2> /dev/null
                    echo '>>> YOUR CONFIG IS : '${config}
                    ./compile em_quarter_ss > log_compile 2> log_error 
                    # save executables  
                    cd main
                      if [[ -f ideal.exe ]]
                      then
                        echo 'Looks good ! ideal.exe is here...' 
                      fi
                      if [[ -f wrf.exe ]]
                      then
                        echo 'Looks good ! wrf.exe is here...' 
                      fi
                    cp -f ideal.exe ../../ideal_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}_s${scat}.exe
                    cp -f wrf.exe ../../wrf_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}_s${scat}.exe
                    cd ..  ;;
    'les')          echo '>>> YOUR CONFIG IS : '${config}
                    mkdir 'test/em_les' 2> /dev/null                    
                    ./compile em_les > log_compile 2> log_error
                    # save executables  
                    cd main
                      if [[ -f ideal.exe ]]
                      then
                        echo 'Looks good ! ideal.exe is here...' 
                      fi
                      if [[ -f wrf.exe ]]
                      then
                        echo 'Looks good ! wrf.exe is here...' 
                      fi
                    cp -f ideal.exe ../../ideal_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}_s${scat}.exe
                    cp -f wrf.exe ../../wrf_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}_s${scat}.exe
                    cd ..  ;;
    *)              echo not supported... please use ; echo ideal les ; exit ;;
  esac
  echo '*******last lines from log_error*********'
  tail -n 20 log_error
fi

  # the end
  echo '****************************************'
  echo 'done.'
  echo '****************************************'
  cp last ../makemeso_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}_s${scat}
  mv last ../../
  svn info ../../ > ../makemeso_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}_s${scat}.svn.info

  # add here specific messages
  if [[ "${dom}" != "1" ]]
  then
  nest=$(expr ${lon} + 4)
  echo NB: in namelist.input, please set:
  echo '
  max_dom = '$dom'
  s_we    = 1,1,
  e_we    = '$lon','$nest',
  s_sn    = 1,1, 
  e_sn    = '$lat','$nest',
  s_vert  = 1,1,
  e_vert  = '$level','$level','
  fi

