#! /bin/tcsh -f
#
# [Gromov, MPI-C Mainz, 2007-2009]
#
# script to double MECCA kinetic chemistry mechanism
#
# steps:
# initialize (zero-setup)
# choose desired configuration(s)
# double using imdouble
# link generated modules to the corresponding dirs

# --------------------------------------------------------------------------
# VARIABLES
# --------------------------------------------------------------------------

# paths are relative
set batchdir     = "../batch"          # batch files location
set meccadir     = ".."                # where MECCA + equation/species files are located
set f90smcl      = "../smcl"           # where f90-files should go to
set f90box       = "../.."             #                        -- BOX ONLY
set f90si        = "../../../../smil"                           # -- SI ONLY
set xtagdir      = "../tag"            # tagging directory (shared files !)
set box2dbl      = "mecca/dbl"         # relative path from box to dbl
set smcl2dbl     = "../dbl"            #        - " -       smcl to dbl
set si2dbl       = "../mbm/caaba/mecca/dbl" #        - " -       si to dbl

set bindir       = "../../../../../bin" # binaries location
set toolsdir     = "../../../../tools" # tools location
set fpc_call     = "fpc -l -viwnh -B"  # fpc compiler call
set do_recompile = "false"             # set to "true" to recompile the executable(s)

# detecting ECHAM5/MESSy
if ( -d ../../../../echam5 ) then
  set bm3d
endif

# just to initialize to zero configuration and exit
if ( "$1" == "zero" ) goto setzero

# --------------------------------------------------------------------------
# BATCH INFO TO BE READ HERE
# --------------------------------------------------------------------------

if ( "$2" != "" ) then
  # type batchfile with or without extension .bat:
  set batchfile = `basename $2 .bat`.bat
  if ( -f $batchdir/$batchfile ) then
    echo "\nusing batchfile: $batchfile" # | tee -a ../$logfile
    set batch
    source $batchdir/$batchfile
  else
    echo "ERROR: $batchfile does not exist"
    exit 1
  endif
endif

# --------------------------------------------------------------------------
# BEGINNING, LINKING FILES, INITIALIZING CONFs
# --------------------------------------------------------------------------

setzero:

# --------------------------------------------------------------------------
#echo "initializing configuration(s) setup..."

# linking mecca.eqn & mecca.spc to current folder
ln -fs $meccadir/mecca.eqn ./mecca.eqn
ln -fs $meccadir/mecca.spc ./mecca.spc
# linking tracer definitions file
ln -fs $meccadir/gas.tex ./gas.tex

# cleaning setup with default "empty" configuration
rm -f *.cfg *.kie *.log

# linking configuration files from tag
set cfgfiles = ($xtagdir/*.cfg $xtagdir/*.kie)
foreach cfgfile ($cfgfiles)
  ln -fs $cfgfile .
end
# linking required scripts
ln -fs $xtagdir/xchecktool .
ln -fs $xtagdir/xselectcfg .

# cleaning files
rm -f *.log
rm -f messy_mecca_dbl*.f90 messy_mecca_dbl*.inc messy_mecca_dbl*.spc messy_mecca_dbl*.eqn messy_mecca_dbl*.tex
rm -f $f90box/messy_mecca_dbl*.f90 $f90box/messy_mecca_dbl*.inc
rm -f $f90smcl/messy_mecca_dbl*.f90 $f90smcl/messy_mecca_dbl*.inc

# default empty "zero" configuration
cp -f messy_mecca_dbl_box.f90-zero $f90box/messy_mecca_dbl_box.f90
cp -f messy_mecca_dbl_parameters.inc-zero messy_mecca_dbl_parameters.inc
cd $f90smcl; ln -fs $smcl2dbl/messy_mecca_dbl_parameters.inc .; cd -
cd $f90box;  ln -fs $box2dbl/messy_mecca_dbl_parameters.inc .;  cd -

# in ECHAM5/MESSy setup
if (${?bm3d}) then
  rm -f $f90si/messy_mecca_dbl*.f90 $f90si/messy_mecca_dbl*.inc
  cp -f messy_mecca_dbl_si.f90-zero $f90si/messy_mecca_dbl_si.f90
  cd $f90si; ln -fs $si2dbl/messy_mecca_dbl_parameters.inc .; cd -
endif

#echo "done"
#echo ""

# just to initialize to zero configuration and exit
if ( "$1" == "zero" ) exit 0

# checking if imdouble tool is available
source ${xtagdir}/xchecktool imdouble
if ("$?" != "0") exit 1

# --------------------------------------------------------------------------
# SELECTION OF DESIRED CONFIGURATION(S)
# --------------------------------------------------------------------------

echo ""
echo "[ XDBL: script to double selected MECCA kinetic chemistry mechanism ]"
echo "  (note: no aerosol-phase chemistry supported so far)"
echo ""

# invoking configuration selection script
source ${xtagdir}/xselectcfg tag\*.cfg
if ("$?" != "0") exit 1

# --------------------------------------------------------------------------
# DOUBLING
# --------------------------------------------------------------------------

# these params left from older versions, keeping so far
set do_dbl = "true"
set do_expl = "false"

if ("$do_dbl" == "true") then

  # ----------------------------------------------------------------------------
  # run imdouble - doubling

  echo ">>> doubling with imdouble >>>"
  echo ""

  ./imdouble.exe mecca.spc mecca.eqn gas.tex $cfglist | tee -a imdouble.log
  set exitstatus = "$status"
  #  mz_rs_20100923+
  rm imcom.tmp
  #  mz_rs_20100923-

  echo "----- list of warnings: -----"
  grep \<\!\> imdouble.log
  echo ""

  echo "exit status from imdouble is: $exitstatus"
  echo ""
  if ( "$exitstatus" != "0" ) then
    echo "please, see the imdouble.log file"
    exit
  endif

  # ----- distributing files to the directories --------------------------------

  # decapitalizing names of all produced files
  set dblfiles = (messy_mecca_dbl*)
  foreach dblfile ($dblfiles)
    mv -f $dblfile `echo $dblfile | gawk '{print tolower($0)}'` >&! /dev/null
  end

  # applying changes to mecca.eqn & .spc
  cat messy_mecca_dbl.eqn > $meccadir/mecca.eqn
  cat messy_mecca_dbl.spc > $meccadir/mecca.spc
  
  # linking generated code to smcl/xbox directories
  set dblfiles = (messy_mecca_dbl*.f90 messy_mecca_dbl*.inc)
  foreach dblfile ($dblfiles)

    set base = `basename $dblfile .f90`
   #set isbox = `echo $base | awk '{print substr($1,length($1)-2,3)}'`

    set isbox = `echo $dblfile "_box." | awk '{print index($1,$2)}'`
    set issi = `echo $dblfile "_si." | awk '{print index($1,$2)}'`
    set fext = $dblfile:e

    # default directory (smcl), or dedicated box/si
    set destdir = $f90smcl
    if ( "$isbox" != "0" ) set destdir = $f90box
    if ( "$issi" != "0" ) then
      if (${?bm3d}) then
        set destdir = $f90si
      else
        set destdir = ""
      endif
    endif

    foreach ddir ($destdir)
      if ( "$fext" == "inc" ) then
        # linking include files to all dirs, not copying
        cd $f90smcl; ln -fs $smcl2dbl/$dblfile .; cd -
        cd $f90box;  ln -fs $box2dbl/$dblfile .;  cd -
        if (${?bm3d}) then
          cd $f90si; ln -fs $si2dbl/$dblfile .; cd -
        endif  
      else
        # copying to destination, linking back to dbl
        if ("$destdir" != "") then
          if (-e $ddir/$dblfile) rm -f $ddir/$dblfile
          mv -f $dblfile $ddir/.
          ln -fs $ddir/$dblfile .
          echo linking $dblfile from $ddir
        endif
      endif  
    end
  end

  echo ""
  echo "<<< done with doubling <<<"

endif

# --------------------------------------------------------------------------
# EXTENDED BUDGETING
# --------------------------------------------------------------------------

if (! ${?batch}) then
  unset embud
  echo ""
  echo "Perform extended mechanism budgeting [y/n/q, default=n]?:"
  set inputstring = "$<"

  if ( "$inputstring" == "q" ) exit 1
  if ( "$inputstring" == "y" ) set embud = ""
endif

if (${?embud}) then 
  set save_tagcfg = $tagcfg
  set tagcfg = $embud
  source ./xembud
  set tagcfg = $save_tagcfg
  unset save_tagcfg
endif

# --------------------------------------------------------------------------
# ALL DONE
# --------------------------------------------------------------------------

unsetenv $cfglist

echo ""
echo "all done --> returning"
echo ""
exit 0






# - THIS IS LEFT FROM THE PREVIOUS VERSION - please keep so far ------------
# --------------------------------------------------------------------------
if ("$do_recompile" == "true") then
  echo "recompiling imdouble executable..."
  echo "(see compile.log for details)"
  rm ./imdouble.exe
  $fpc_call imdouble -oimdouble.exe > compile.log
  if ("$status" != "0") then
    echo "not done"
    echo "--- listing compile.log ---"
    cat compile.log
    exit 1
  endif
  echo "done"
  echo ""
endif
