#!/bin/bash
#
#################################################
# batch settings for int2lm run on Vilje at NTNU
#################################################
#PBS -N int2lm_@{EXPID}
#PBS -l select=2:ncpus=32:mpiprocs=8
#PBS -l walltime=00:50:00
#PBS -o @{JOBLOGFILE}.o
#PBS -e @{JOBLOGFILE}.e
#PBS -m e
#PBS -M @{EMAIL_ADDRESS}
#PBS -A @{PROJECT_ACCOUNT}

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 @{YYYY} @{MM}  >> @{WORKDIR}/@{EXPID}/joblogs/int2lm/finish_joblist

cd @{WORKDIR}/@{EXPID}/joboutputs/int2lm/@{YYYY_MM}


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

HSTOP=$(@{UTILS_BINDIR}/cfu get_hours @{CURRENT_DATE} @{NEXT_DATE} @{ITYPE_CALENDAR})

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

if [ -f YUDEBUG ]
then
  rm YUDEBUG
fi

#YDATE_INI=$(@{UTILS_BINDIR}/cfu add_hours @{CURRENT_DATE} ${HADD} @{ITYPE_CALENDAR})
YDATE_INI=@{CURRENT_DATE}
echo YDATE_INI ${YDATE_INI}
#################################################
# cat together the INPUT*-files
#################################################

cat > INPUT <<**
 &CONTRL
!  ydate_ini='${YDATE_INI}', hstart=0.0, hstop=0.0, hincbound=@{HINCBOUND},
!  linitial=.TRUE.,  lboundaries =.FALSE.,
 ydate_ini='${YDATE_INI}', hstart=0.0, hstop=${HSTOP}, hincbound=@{HINCBOUND},
  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=@{ITYPE_ALBEDO},
 itype_aerosol=@{ITYPE_AEROSOL},
 lforest = @{LFOREST},
 llake = @{LLAKE}, 
 llake_coldstart = @{LLAKE},
 lsso = @{LSSO}
 nprocx = @{NPX}, nprocy = @{NPY},
 itype_calendar=@{ITYPE_CALENDAR},
/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=@{IE_TOT}, jelm_tot=@{JE_TOT}, kelm_tot=@{KE_TOT},
  pollat =  @{POLLAT}, pollon = @{POLLON}, polgam = @{POLGAM}, 
  dlon=@{DLON}, dlat=@{DLAT}, 
  startlat_tot  = @{STARTLAT_TOT}, startlon_tot  = @{STARTLON_TOT},
  ke_soil_lm=@{KE_SOIL},   
  czml_soil_lm = @{CZML_SOIL},
 /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='@{INPDIR}/@{YYYY_MM}',
  yin_cat='@{INPDIR}/@{YYYY_MM}',
  ylm_cat='@{OUTDIR}/@{YYYY_MM}/',
  nprocess_ini = 131, nprocess_bd = 132, 
  yncglob_institution='The name of your institution',
  yncglob_title='INT2LM',
  yncglob_source='@{INT2LM_EXE}',
  yncglob_contact='@{EMAIL_ADDRESS}',
 /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 @{INT2LM_EXE}
echo ----- INT2CLM finished

#srun -l --propagate=STACK --cpu_bind=verbose,cores --distribution=block:cyclic  @{INT2LM_EXE}

#let "HADD = HADD + @{HINCBOUND}"
#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 @{PFDIR}/@{EXPID}/date.log | cut -c1-6)0100
if [[ @{CURRENT_DATE} -eq @{YDATE_START} || @{CURRENT_DATE} -eq ${DATELOG} ]]
then
  #################################################
  # submit CCLM job
  #################################################
  cd @{PFDIR}/@{EXPID} ; subchain cclm
fi

echo "END  " @{YYYY} @{MM}  >> @{WORKDIR}/@{EXPID}/joblogs/int2lm/finish_joblist

