#!/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-01: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., lfilter_oro=.FALSE., lfilter_pp=.TRUE.,
  norder_filter=5, ilow_pass_oro=4, ilow_pass_xso=5, 
  yinput_model='CM',
  idbg_level=2,
  ltime_mean=.TRUE., luvcor=.TRUE.,
  luse_t_skin=.TRUE.,
  lbdclim=.TRUE., lreorder=.TRUE.,
  nprocx=@{NPX}, nprocy=@{NPY},
  nincwait=5, nmaxwait=20,
  lmulti_layer_in=.TRUE., lmulti_layer_lm=.TRUE.,
  l_cressman=.FALSE.,
  l_bicub_spl=.FALSE.,
  llbc_smooth=.TRUE.,
  nlbc_smooth=20,
  lprog_qi=.TRUE.,
  lprog_qr_qs=.FALSE.,
  lprog_rho_snow=.FALSE.,
  itype_w_so_rel=1,
  itype_t_cl=0,
  itype_rootdp = 0,
  itype_ndvi=0,
  itype_calendar=@{ITYPE_CALENDAR},
  leps_bc=.FALSE.,
  lforest=@{LFOREST},
  itype_aerosol=2,
  itype_albedo=@{ITYPE_ALBEDO},
  eps_filter=0.1,
  rxso_mask=750,
  lsso=.TRUE.,
  lradtopo=.TRUE.,
  lurban=.TRUE.,
  llake=@{LLAKE},
  llake_coldstart=.TRUE.,
  lvertwind_bd=.FALSE.,
/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,
  dlat_in=0.703125, dlon_in=0.703125,
  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.64,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},
  vcflat=11357.00,
  vcoord_d = 22000.00, 21000.00, 20028.57, 19085.36, 18170.00,
             17282.14, 16421.43, 15587.50, 14780.00, 13998.57,
             13242.86, 12512.50, 11807.14, 11126.43, 10470.00,
              9837.50,  9228.57,  8642.86,  8080.00,  7539.64,
              7021.43,  6525.00,  6050.00,  5596.07,  5162.86,
              4750.00,  4357.14,  3983.93,  3630.00,  3295.00,
              2978.57,  2680.36,  2400.00,  2137.14,  1891.43,
              1662.50,  1450.00,  1253.57,  1072.86,   907.50,
               757.14,   621.43,   500.00,   392.50,   298.57,
               217.86,   150.00,    94.64,    51.43,    20.00,
                 0.00,  
 /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=1808, je_ext=1231,
  ylmext_lfn='ExtData_CECPC5.0_02750_ASTER_plus_SSO_ALB12.nc',
  ylmext_cat='/pf/b/b380794/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='Institut fuer Geographie JLU Giessen',
  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

