#!/bin/bash
#
#################################################
# batch settings for int2lm run on Vilje at NTNU
#################################################
#PBS -N int2lm_sp002
#PBS -l select=2:ncpus=32:mpiprocs=8
#PBS -l walltime=00:50:00
#PBS -o /work/bhuang/cclm-sp_2.4/chain/work/sp002/joblogs/int2lm/int2lm_sp002_2007_01.o.o
#PBS -e /work/bhuang/cclm-sp_2.4/chain/work/sp002/joblogs/int2lm/int2lm_sp002_2007_01.o.e
#PBS -m e
#PBS -M bo.huang@ntnu.no
#PBS -A ntnu919

module load intelcomp/17.0.0 mpt/2.14 grib/1.19.0 hdf5/1.8.18 netcdf/4.4.1 
# ulimit: the setting of the stack size instead of using "ulimit -s unlimited",
#         together with the --propagate=STACK in the "srun" command (see below),
#         speeds up the job considerably; in my test cases between 10% and 15%;
#         the value 102400 is the default maximum stack size on MISTRAL
#
#ulimit -s 102400

#
export FORT_BUFFERED=true
export decfort_dump_flag=true
#
export MXM_LOG_LEVEL=ERROR

set -e

echo START 2007 01  >> /work/bhuang/cclm-sp_2.4/chain/work/sp002/joblogs/int2lm/finish_joblist

cd /work/bhuang/cclm-sp_2.4/chain/work/sp002/joboutputs/int2lm/2007_01


#################################################
# loop over data sets
#################################################

HSTOP=$(/work/bhuang/cclm-sp_2.4/src/cfu/bin/cfu get_hours 2007010100 2007020100 0)

#HADD=0
#while [ ${HADD} -le ${HSTOP} ]
#do

if [ -f YUDEBUG ]
then
  rm YUDEBUG
fi

#YDATE_INI=$(/work/bhuang/cclm-sp_2.4/src/cfu/bin/cfu add_hours 2007010100 ${HADD} 0)
YDATE_INI=2007010100
echo YDATE_INI ${YDATE_INI}
#################################################
# cat together the INPUT*-files
#################################################

cat > INPUT <<**
 &CONTRL
!  ydate_ini='${YDATE_INI}', hstart=0.0, hstop=0.0, hincbound=06,
!  linitial=.TRUE.,  lboundaries =.FALSE.,
 ydate_ini='${YDATE_INI}', hstart=0.0, hstop=${HSTOP}, hincbound=06,
  linitial=.TRUE.,  lboundaries =.TRUE., 
  yinput_model='CM',   
  itype_rootdp = 3,
  itype_t_cl = 1, 
  itype_w_so_rel = 1,
  l_cressman = .FALSE., 
  lbdclim = .TRUE.,  
  lboundaries =.TRUE., 
  lfilter_oro=.FALSE.,
  linitial=.TRUE.,
  lmulti_layer_in = .TRUE., 
  lmulti_layer_lm = .TRUE.,
  lprog_qi = .FALSE.,
  lreorder = .TRUE.,
  ltime_mean = .TRUE., 
  luse_t_skin = .TRUE.,
  nincwait = 5, nmaxwait = 20, 
 itype_albedo=2,
 itype_aerosol=1,
 lforest = .TRUE.,
 llake = .FALSE., 
 llake_coldstart = .FALSE.,
 lsso = .TRUE.
 nprocx = 2, nprocy = 8,
 itype_calendar=0,
/END
 &GRID_IN
 pcontrol_fi =30000., 
  ie_in_tot = 512, je_in_tot = 256, ke_in_tot = 60,
  startlat_in_tot = -89.46282, startlon_in_tot = -180.,
  endlat_in_tot = 89.46282, endlon_in_tot = 179.2969,
  pollat_in = 90.0, pollon_in = 180.0,
  lushift_in=.FALSE.,.FALSE., lvshift_in=.FALSE.,.FALSE.,
  ke_soil_in=3,   
  czml_soil_in=0.035,0.175,0.640,1.775,
 /END
 &LMGRID
  ivctype = 2,
  irefatm = 2,
  ielm_tot=101, jelm_tot=111, kelm_tot=40,
  pollat =  39.25, pollon = -162.0, polgam = 0., 
  dlon=0.44, dlat=0.44, 
  startlat_tot  = -24.09, startlon_tot  = -25.13,
  ke_soil_lm=9,   
  czml_soil_lm = 0.005,0.025,0.07,0.16,0.34,0.7,1.42,2.86,5.74,11.5,
 /END
 &DATABASE
 /
 &DATA
  yinput_type='analysis',
  ytunit_in='d',
  ytunit_out='d',
  ylmext_form_read='ncdf',
  yinext_form_read='ncdf',
  yin_form_read='ncdf',
  ylm_form_write='ncdf',
  ie_ext=105, je_ext=115,
  ylmext_lfn='europe_0440.nc',
  ylmext_cat='/work/bhuang/cclm-sp/data/ext/',
!... attention: in yinext_lfn you may use "cas" instead of "caf"
!...            depending on the coarse data
  yinext_lfn='caf${YDATE_INI}.nc',
  yinext_cat='/work/bhuang/cclm-sp_2.4/chain/scratch/sp002/input/int2lm/2007_01',
  yin_cat='/work/bhuang/cclm-sp_2.4/chain/scratch/sp002/input/int2lm/2007_01',
  ylm_cat='/work/bhuang/cclm-sp_2.4/chain/scratch/sp002/output/int2lm/2007_01/',
  nprocess_ini = 131, nprocess_bd = 132, 
  yncglob_institution='The name of your institution',
  yncglob_title='INT2LM',
  yncglob_source='/work/bhuang/cclm-sp_2.4/src/int2lm/bin/int2lm.exe',
  yncglob_contact='bo.huang@ntnu.no',
 /END
 &PRICTR
  igp_tot = 36,
  jgp_tot = 30,
  lchkin=.TRUE., lchkout=.TRUE.,
 /END
**


#################################################
# run the program
#################################################

# you have to adapt the way how to invoke the binary
export MPI_COREDUMP=FIRST
echo ----- start INT2CLM
mpiexec_mpt -stats /work/bhuang/cclm-sp_2.4/src/int2lm/bin/int2lm.exe
echo ----- INT2CLM finished

#srun -l --propagate=STACK --cpu_bind=verbose,cores --distribution=block:cyclic  /work/bhuang/cclm-sp_2.4/src/int2lm/bin/int2lm.exe

#let "HADD = HADD + 06"
#done

### submit a CCLM job if
### it is the first INT2LM job in the chain OR
### the in parallel runnig CCLM job was faster than the INTLM job
DATELOG=$(cat /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/sp002/date.log | cut -c1-6)0100
if [[ 2007010100 -eq 2007010100 || 2007010100 -eq ${DATELOG} ]]
then
  #################################################
  # submit CCLM job
  #################################################
  cd /work/bhuang/cclm-sp_2.4/chain/gcm_to_cclm/sp002 ; subchain cclm
fi

echo "END  " 2007 01  >> /work/bhuang/cclm-sp_2.4/chain/work/sp002/joblogs/int2lm/finish_joblist

