#! /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 #
##############################

#############################
# Use:    
#
# makemeso                   ## basic use (real configuration)
#
# makemeso -d                ## no compilation, just check the name of the compile folder
#
# makemeso -c real           ## real-case mode [default]
# makemeso -c convection     ## idealized mode: convective cell
#
# 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
#
#############################


# todo: netcdf support


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

donotcompile=0
donotallow=0
config='real'
donotcompilephys=0
justphys=0
debug=0
phys=""
while getopts "drc:njhgp" 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_";;
   h ) echo "
# Use:    
#
# makemeso                   ## basic use (real configuration)
#
# makemeso -d                ## no compilation, just check the name of the compile folder
#
# makemeso -c real           ## real-case mode [default]
# makemeso -c convection     ## idealized mode: convective cell
#
# 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
" ; 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 ? 1, 2, 4, 6, 8, 12, 16 ; read numproc ;;
    4) compilo='ifort' ; numproc=1 ;;
    5) compilo='mpifort' ; echo How many processors ? 1, 2, 4, 6, 8, 12, 16 ; read numproc ;;
    7) compilo='mpixlf' ; echo How many processors ? 1, 2, 4, 6, 8, 12, 16 ; 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 
  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="${phys}${compilo}_${machine}${single}${testflag}"
    \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
            exit
         fi
  echo Number of tracers ? ; read tra 
         if [ ${tra} -eq 0 ]
         then 
	    tra=1	 
         fi
  # folder
  mkdir ${conf_wrf} 2> /dev/null
  if [[ "$?" == 0 ]]
  then
    echo new folder ... link sources
    if [[ "${phys}" == "newphys_" ]]
    then
    sed s+"mars_lmd"+"mars_lmd_new"+g copy_model > copy_model_tmp
    chmod 755 copy_model_tmp
    ./copy_model_tmp
    \rm copy_model_tmp
    mv WRFV2 ${conf_wrf}/
    cd ${conf_wrf}/WRFV2
    ln -sf mars_lmd_new mars_lmd
    ln -sf meso_callkeys_newphys.h meso_callkeys.h
    cd Registry ; Registry.bash ; cd ..
    else
    ./copy_model
    mv WRFV2 ${conf_wrf}/
    cd ${conf_wrf}/WRFV2
    cd Registry ; Registry.bash ; cd .. 
    fi
  else
    cd ${conf_wrf}/WRFV2 
    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
  # 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
  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 ${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}"
  case ${conf_wrf_forall} in
#######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 
                       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
                       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 
                         ## !!! 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 
                       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 ;;
                     # 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 
                       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+"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
                       #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
  ########################

  if [[ "${phys}" == "newphys_" ]]
  then
     sed s+"ARCHFLAGS       =       "+"ARCHFLAGS       =       -DNEWPHYS "+g configure.wrf > yeah 
     mv -f yeah configure.wrf
  fi

  if [ ${debug} -ne 0 ]   # not working for xlf!
  then
    echo 'DEBUG DEBUG DEBUG DEBUG'
    sed s+"#-g"+"-g"+g configure.wrf > yeah
    mv -f yeah configure.wrf
  fi


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

  cd mars_lmd/

  # required size
  #----------------
  case ${numproc} in
    1) physx=$(expr ${lon} - 1)
       physy=$(expr ${lat} - 1)
       physz=$(expr ${level} - 1) ;;
    2) physx=$(expr ${lon} - 1)
       physy=$(expr ${lat} - 1) ; physy=$(expr ${physy} \/ 2)
       physz=$(expr ${level} - 1) ;;
    4) physx=$(expr ${lon} - 1) ; physx=$(expr ${physx} \/ 2)
       physy=$(expr ${lat} - 1) ; physy=$(expr ${physy} \/ 2)
       physz=$(expr ${level} - 1) ;;
    6) physx=$(expr ${lon} - 1) ; physx=$(expr ${physx} \/ 2)
       physy=$(expr ${lat} - 1) ; physy=$(expr ${physy} \/ 3)
       physz=$(expr ${level} - 1) ;;
    8) physx=$(expr ${lon} - 1) ; physx=$(expr ${physx} \/ 2)
       physy=$(expr ${lat} - 1) ; physy=$(expr ${physy} \/ 4)
       physz=$(expr ${level} - 1) ;;
    12) physx=$(expr ${lon} - 1) ; physx=$(expr ${physx} \/ 3)
        physy=$(expr ${lat} - 1) ; physy=$(expr ${physy} \/ 4)
        physz=$(expr ${level} - 1) ;;
    16) physx=$(expr ${lon} - 1) ; physx=$(expr ${physx} \/ 4)
        physy=$(expr ${lat} - 1) ; physy=$(expr ${physy} \/ 4)
        physz=$(expr ${level} - 1) ;;
    *) echo not supported ; exit
  esac

  # change this if you change num_soil_layers in WRF
  # -- default is 10
  if [[ "${phys}" == "newphys_" ]]
  then
     soilsize=18 ## nouvelle physique
  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
  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
  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} meso_testphys1d | tee libo/log_compile_phys | grep 'warnings' >> libo/log_compile_phys 
  else
    nohup makegcm_${compilo} -t ${tra} -p mars -d ${physz} meso_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 ..

  # save a copy 
  #--------------
  cp -f libo/liblmd.a libo/liblmd.a_${lon}_${lat}_${level}_${dom}_${tra} 
  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 [[ ! ( -f "call_meso_physiq.inc" ) ]]
  then
   echo 'did you compile the physics ? no call_meso_physiq.inc !'
   exit
  fi

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

  # talk to user
  echo '>>> YOUR CONFIG IS : '${config}
  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
    'real')         compile em_real > log_compile 2> log_error 
                    # save executables  
                    cd main
                      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}.exe
                    cp -f wrf.exe ../../wrf_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}.exe
                    cd ..  ;;
    'convection')   #mkdir 'test/em_quarter_ss' 2> /dev/null
                    compile em_quarter_ss > log_compile 2> log_error 
                    # save executables  
                    cd main
                      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}.exe
                    cp -f wrf.exe ../../wrf_x${lon}_y${lat}_z${level}_d${dom}_t${tra}_p${numproc}.exe
                    cd ..  ;;
    *)              echo not supported please use ; echo real convection ; 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}
  mv last ../../

  # 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

