#!/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 /Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/chain/work/e6001/joboutputs/int2lm/1989_02


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

HSTOP=$(/Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/src/cfu/bin/cfu get_hours 1989020100 1989030100 0)

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

if [ -f YUDEBUG ]
then
  rm YUDEBUG
fi

#YDATE_INI=$(/Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/src/cfu/bin/cfu add_hours 1989020100 ${HADD} 0)
YDATE_INI=1989020100
echo YDATE_INI ${YDATE_INI}
#################################################
# cat together the INPUT*-files
#################################################

cat > INPUT <<**
 &CONTRL
!  ydate_ini='${YDATE_INI}', hstart=0.0, hstop=0.0, hincbound=6,
!  linitial=.TRUE.,  lboundaries =.FALSE.,
 ydate_ini='${YDATE_INI}', hstart=0.0, hstop=${HSTOP}, hincbound=6,
 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 = .TRUE.,
 llake = .FALSE., 
 lsso = .TRUE.
 llbc_smooth = .FALSE., nlbc_smooth = 10,
 nprocx = 3, nprocy = 2,
 itype_calendar=0,
! 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=257, jelm_tot=141, kelm_tot=40,
  pollat =  90.0, pollon = -180.0, polgam = 0., 
  dlon=0.44, dlat=0.44, 
  startlat_tot  = -7.04, startlon_tot  = -38.28,
  ke_soil_lm=9,   
  czml_soil_lm = 0.005,0.025,0.07,0.16,0.34,0.7,1.42,2.86,5.74,11.5,
  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='/Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/chain/scratch/e6001/input/int2lm/1989_02',
  yin_cat='/Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/chain/scratch/e6001/input/int2lm/1989_02',
  ylm_cat='/Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/chain/scratch/e6001/output/int2lm/1989_02/',
  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 /Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/src/int2lm/bin/int2lm -procs 6
mpirun -np 6 /Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/src/int2lm/bin/int2lm

echo ----- INT2CLM finished

#let "HADD = HADD + 6"
#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 /Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/chain/gcm_to_cclm/e6001/date.log | cut -c1-10)
if [[ 1989020100 -eq 1989010100 || 1989020100 -eq ${DATELOG} ]]
then
  #################################################
  # submit CCLM job
  #################################################
  /Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/chain/gcm_to_cclm/e6001/subchain cclm
fi

#  KRICH  echo   int2lm job finished >> /Research/CLIMATE/Giora/COSMO-CLM/cclm-sp_1.4/chain/work/e6001/joblogs/int2lm/finish_joblist

