#!/usr/bin/ksh
#
#################################################
# batch settings for INT2LM run 
#################################################

#PBS -j oe
#PBS -N ccint2lm
#PBS -l walltime=6:01:00
#PBS -l nodes=1:ppn=6
#source $HOME/.bashrc.local
export MPI_DSM_DISTRIBUTE=1
export MPI_BUFS_PER_HOST=1024
export MPI_BUFS_PER_PROC=2048
export MPI_IB_RAILS=1

set -e

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=.TRUE.,
  yinput_model='CM',   
 l_cressman = .TRUE., 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 = .TRUE.,
 luse_t_skin = .TRUE.,
 itype_ndvi = 0,
 nincwait = 5, nmaxwait = 20, 
 lforest = @{LFOREST},
 llake = @{LLAKE}, 
 lsso = @{LSSO}
 llbc_smooth = .FALSE., nlbc_smooth = 10,
 nprocx = @{NPX}, nprocy = @{NPY},
 itype_calendar=@{ITYPE_CALENDAR},
! idbg_level=200,
/END
 &GRID_IN
  pcontrol_fi =30000., 
  ie_in_tot = 512, je_in_tot = 256, ke_in_tot = 60,
  startlat_in_tot = -89.4628219604492, startlon_in_tot = -180.,
  endlat_in_tot = 89.4628219604492, endlon_in_tot = 179.296875000000,
  pollat_in = 90.0, pollon_in = 180.0,
  lushift_in=.FALSE.,.FALSE., lvshift_in=.FALSE.,.FALSE.,
  ke_soil_in=3,   
  czml_soil_in=0.015, 0.1, 0.405, 1.205,
 /END
 &LMGRID
  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},
  ivctype = 2,
  vcflat = 15000.00,
  vcoord_d=
  30000.00, 27800.00, 25710.00, 23730.00, 21860.00,
  20080.00, 18410.00, 16830.00, 15350.00, 13960.00,
  12660.00, 11440.00, 10300.00, 9250.00, 8265.00, 
  7360.00, 6520.00, 5750.00, 5045.00, 4400.00, 
  3820.00, 3290.00, 2815.00, 2390.00, 2015.00, 
  1680.00, 1388.00, 1134.00, 915.00, 729.00, 
  573.00, 443.00, 338.00, 253.00, 186.00, 
  134.00, 95.00, 64.00, 41.00, 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=332, je_ext=218,
  ylmext_lfn='domain2013112115045.nc',
  ylmext_cat='/Research/CLIMATE/Giora/COSMO-CLM/data/ext/MENA50km/',
!... 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
#################################################


source /usr/share/modules/init/bash
#module load compiler/gcc-4.3.4 mpt/2.08
module load mpt intel-compilers-12

# you have to adapt the way how to invoke the binary
echo ----- start INT2CLM
export LD_LIBRARY_PATH=/Research/CLIMATE/Giora/COSMO-CLM/netcdf412/lib:/Research/CLIMATE/Giora/COSMO-CLM/hdf5/lib:/usr/lib64:/lib64:$LD_LIBRARY_PATH

#  KRICH 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-10)
if [[ @{CURRENT_DATE} -eq @{YDATE_START} || @{CURRENT_DATE} -eq ${DATELOG} ]]
then
  #################################################
  # submit CCLM job
  #################################################
  @{PFDIR}/@{EXPID}/subchain cclm
fi

#  KRICH  echo @{YYYY} @{MM} int2lm job finished >> @{WORKDIR}/@{EXPID}/joblogs/int2lm/finish_joblist

