#!/bin/ksh
#
#################################################
# batch settings for INT2LM run on blizzard at DKRZ
#################################################
# @ shell = /bin/ksh
#### @ class = cluster
# @ class = express
# @ job_type = parallel
# @ job_name = int2lm_@{EXPID}
# @ node_usage= shared
# @ network.MPI = sn_all,shared,us
# @ rset = rset_mcm_affinity                    
# @ mcm_affinity_options = mcm_accumulate      
# @ node = @{NODES}
### @ tasks_per_node = 64 # for SMT
# @ tasks_per_node = @{PPN}
# @ resources = ConsumableMemory(750mb)
#### @ task_affinity = cpu(1) # for SMT
# @ task_affinity = @{TASK_AFFINITY}
# @ wall_clock_limit = 00:15:00
# @ output = @{JOBLOGFILE}$(jobid)
# @ error = @{JOBLOGFILE}$(jobid)
# @ notification     = never
# @ notify_user      = @{EMAIL_ADDRESS}
# @ account_no = @{PROJECT_ACCOUNT}
# @ queue
export MEMORY_AFFINITY=MCM
export MP_PRINTENV=YES
export MP_LABELIO=YES
export MP_INFOLEVEL=0
export MP_EAGER_LIMIT=64k
export MP_BUFFER_MEM=64M,256M
export MP_USE_BULK_XFER=NO
export MP_BULK_MIN_MSG_SIZE=128k
export MP_RFIFO_SIZE=4M
export MP_SHM_ATTACH_THRESH=500000
export LAPI_DEBUG_STRIPE_SEND_FLIP=8

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.,
  yinput_model='CM',   
 l_cressman = .FALSE., l_bicub_spl = .FALSE., lbdclim = .TRUE.,  
 ltime_mean = .TRUE., luvcor = .TRUE. , lreorder = .TRUE.,
 lmulti_layer_in = .TRUE., lmulti_layer_lm = .TRUE.,
 itype_w_so_rel = 1,
 itype_t_cl = 1, 
 itype_rootdp = 3,
 lprog_qi = .FALSE.,
 luse_t_skin = .TRUE.,
 itype_ndvi = 0,
 nincwait = 5, nmaxwait = 20, 
 itype_albedo=@{ITYPE_ALBEDO},
 itype_aerosol=@{ITYPE_AEROSOL},
 lforest = @{LFOREST},
 llake = @{LLAKE}, 
 lsso = @{LSSO}
 llbc_smooth = .FALSE., nlbc_smooth = 10,
 nprocx = @{NPX}, nprocy = @{NPY},
 itype_calendar=@{ITYPE_CALENDAR},
 idbg_level=150,
/END
 &GRID_IN
  pcontrol_fi =30000., 
  ie_in_tot = 192, je_in_tot = 94, ke_in_tot = 28,
  startlat_in_tot = -88.542, startlon_in_tot = -180.,
  endlat_in_tot = 88.542, endlon_in_tot = 178.125,
  pollat_in = 90.0, pollon_in = 180.0,
  lushift_in=.FALSE.,.FALSE., lvshift_in=.FALSE.,.FALSE.,
  ke_soil_in=1,   
  czml_soil_in=0.05, 1.05,
 /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='@{SPDIR}/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, 
 /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
#poe @{INT2LM_EXE} -procs @{NP}
mpirun -np @{NP} @{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
  #################################################
  @{PFDIR}/@{EXPID}/subchain cclm
fi

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

