#!/bin/bash

model=LMDZ
cas=ARMCU/REF
param=param_cldb

if [ $# -eq 1 ] ; then
  model=ECRAD
  cas=ARMCU/008
  param=param
fi

./setup.sh $model BENCH$model

cd WORK/BENCH$model

./param2R.sh 30 3 $param              # generate first wave parameters 
if [ $? -eq 0 ]; then
  sed -i "s/WAVEN=.*/WAVEN=1/g" expe_setup.R  # setup experiment
  ./serie_$model.sh $cas 1            # run model with parameters 
  if [ $? -eq 0 ]; then
    Rscript htune_ncdf2Rdata.R        # read models outputs and compute metrics
    Rscript htune_EmulatingSCM_CS2.R  # emulate and history match

    pdfjam Rplots.pdf 2 
    pdf2ps Rplots-2-pdfjam.pdf
    ps2epsi Rplots-2-pdfjam.ps
    epstopdf Rplots-2-pdfjam.epsi
    pdfjam Rplots-2-pdfjam.pdf InputSpace_wave1.pdf --nup 2x1 --landscape --outfile result_wave1.pdf
    evince result_wave1.pdf &

    ./param2Rwave.sh 2 Wave2.RData    # generate new wave parameters
    sed -i "s/WAVEN=.*/WAVEN=2/g" expe_setup.R  # setup experiment
    ./serie_$model.sh $cas 2          # run model with parameters
    if [ $? -eq 0 ]; then
      Rscript htune_ncdf2Rdata.R 
      Rscript htune_EmulatingSCM_CS2.R
    
      pdfjam Rplots.pdf 2 
      pdf2ps Rplots-2-pdfjam.pdf
      ps2epsi Rplots-2-pdfjam.ps
      epstopdf Rplots-2-pdfjam.epsi
      pdfjam Rplots-2-pdfjam.pdf InputSpace_wave2.pdf --nup 2x1 --landscape --outfile result_wave2.pdf
      evince result_wave2.pdf &

      echo '##########################################################'
      echo '    Next version will start heating water at tea time     '
      echo '##########################################################'
  
    fi
  fi
fi
