#!/bin/bash -x
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 'COSMO-CLM System'

#change directories for CLM version
DIR_COSMO=/mnt/msu/users/vplatonov/COSMO_CLM/CLM
DIR_EXP=$DIR_COSMO/EXPERIMENTS/AMAP
DIR_EXTPAR=$DIR_COSMO/PEPext
DIR_EXE=$DIR_COSMO/ext
IN_DIR=$DIR_COSMO/IN_DIR/historical
LM_DIR=$DIR_EXP/LM

cd $DIR_EXP

if [ ! -d LM ]
	then
	mkdir LM
fi

# lomonosov variant
NPX1=8
NPY1=4
NP1=$(($NPX1 * $NPY1))

SQUEUE=gpu

YDATE_INI=2005010100
HSTART=0
HSTOP=8760

if [ -d int2lm_text_out ]
	then
	rm -r int2lm_text_out
fi

mkdir int2lm_text_out
cd int2lm_text_out
lfs setstripe -c 16 .

echo 'hello'

#################################################
# cat together the INPUT*-files
#################################################

cat > INPUT <<**
 &CONTRL
  ydate_ini='${YDATE_INI}', hstart=${HSTART}, hstop=${HSTOP}, hincbound=6,
  linitial=.TRUE.,
  lboundaries=.TRUE.,
  lfilter_oro=.TRUE.,
   numfilt_oro=1,
   ilow_pass_oro=4,
   lxso_first=.FALSE.,
   numfilt_xso=1,
   ilow_pass_xso=5,
   ifill_valley=1,
   eps_filter=0.1,
   rfill_valley=0.0,
   rxso_mask=750.0,
  yinput_model='CM',
  l_cressman=.FALSE.,
  lbdclim=.TRUE.,
  ltime_mean=.TRUE.,
  lmulti_layer_in=.TRUE.,
  lmulti_layer_lm=.TRUE.,
 !itype_w_so_rel=1,
  itype_t_cl=1,
  itype_rootdp=3,
  lprog_qi=.TRUE.,
  lforest=.TRUE.,
  llake=.FALSE.,
  lsso=.TRUE.,
  nprocx=${NPX1},
  nprocy=${NPY1},
  nincwait=5,
  nmaxwait=20,
  luse_t_skin=.TRUE.,
  idbg_level=2,
  itype_calendar=0,
/END
 &GRID_IN
  pcontrol_fi =30000.,
  ie_in_tot = 192, je_in_tot = 96, ke_in_tot = 47,
  startlat_in_tot = -88.5722, startlon_in_tot = -180.,
  endlat_in_tot = 88.5722, endlon_in_tot = 178.125,
  pollat_in = 90.0, pollon_in = 180.0,
  ke_soil_in=4,
  czml_soil_in=0.03, 0.19, 0.78, 2.68, 6.98,
 /END
 &LMGRID
  startlat_tot=-14.67,
  startlon_tot=-12.67,
  pollat=20.0,
  pollon=5.0,
  polgam=0,
  dlon=0.165,
  dlat=0.165,
  ielm_tot=161,
  jelm_tot=136,
  kelm_tot=40,
  ke_soil_lm=9,
  czml_soil_lm=0.005,0.025,0.07,0.16,0.34,0.70,1.42,2.86,5.74,11.5,
  irefatm=2,
  ivctype=2,
 /END
 &DATABASE
 /
 &DATA
  ie_ext=165, je_ext=140,
  yinput_type='analysis',
  ylmext_form_read='ncdf',
  yinext_form_read='ncdf',
  yin_form_read='ncdf',
  ylm_form_write='ncdf',
  ytunit_in='d',
  ytunit_out='d',
  ylmext_lfn='chukchi_domain_ecoclimap.nc',
  ylmext_cat='$DIR_EXTPAR',
  yinext_lfn='caf${YDATE_INI}.nc',
  yinext_cat='${IN_DIR}',
  yin_cat='${IN_DIR}',
  ylm_cat='$LM_DIR',
  nprocess_ini = 131, nprocess_bd = 132,
  nl_soil_in=2, nl_soil_lm=2,
 /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
	sbatch -N $NP1 -p $SQUEUE ompi $DIR_EXE/int2lm_v5
echo ----- INT2CLM finished