#!/bin/bash
#
#################################################
# batch settings for INT2LM run on mistral at DKRZ
#################################################
#SBATCH --job-name=int2lm_@{EXPID}
#SBATCH --partition=@{PARTITION}  # Specify partition name for job execution
#SBATCH --ntasks=@{NP}
#SBATCH --cpus-per-task=@{CPU_PER_TASK}
#SBATCH --time=00-00:30:00
#SBATCH --exclusive
#SBATCH --output=@{JOBLOGFILE}%j
#SBATCH --error=@{JOBLOGFILE}%j
#SBATCH --mail-user=@{EMAIL_ADDRESS}
#SBATCH --mail-type=FAIL
#SBATCH --account=@{PROJECT_ACCOUNT}

# 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 = 4,
  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 = .TRUE.,
  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,
  north_add_in=1, east_add_in=1,
  ke_soil_in=3,
  czml_soil_in=0.0350000001, 0.174999997, 0.639999986,1.77499998,
  /END
 &LMGRID
  ivctype = 2,
  irefatm = 1,
  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=160, je_ext=180,
  ylmext_lfn='domain2017031420057.nc',
  ylmext_cat='/scratch/b/b324045/cclm-sp_2.1/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
echo ----- start INT2CLM
srun -l --propagate=STACK --cpu_bind=verbose,cores --distribution=block:cyclic  @{INT2LM_EXE}
echo ----- INT2CLM finished

#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

