#!/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

SQUEUE=gpu

echo 'COSMO-CLM System'

#change directories for CLM version
DIR_COSMO=/mnt/msu/users/vplatonov/CLM
DIR_EXP=$DIR_COSMO/EXPERIMENTS/Arctic/downscaling_2.8km
DIR_EXTPAR=$DIR_COSMO/PEPext
DIR_EXE=$DIR_COSMO/ext
IN_DIR=$DIR_COSMO/EXPERIMENTS/Arctic/OUTmod/out01
LM_DIR=$DIR_EXP/LM
OUT_DIR=$DIR_EXP/OUTmod

cd $DIR_EXP
lfs setstripe -c 16 .

if [ ! -d $OUT_DIR ]
then
mkdir $OUT_DIR
fi

if [ ! -d $OUT_DIR/out01 ]
then
mkdir $OUT_DIR/out01
mkdir $OUT_DIR/out02
mkdir $OUT_DIR/out03
mkdir $OUT_DIR/restarts
fi

cd $OUT_DIR/out01
lfs setstripe -c 16 .

cd $OUT_DIR/out02
lfs setstripe -c 16 .

cd $OUT_DIR/out03
lfs setstripe -c 16 .

cd $OUT_DIR/restarts
lfs setstripe -c 16 .

cd ../..

# lomonosov variant
NPX2=16
NPY2=8
NP2=$(($NPX2 * $NPY2))

YDATE_INI=1997121200
HSTART=0
HSTOP=192

if [ ! -d cclm_text_out ]
then
mkdir cclm_text_out
fi

cd $DIR_EXP/cclm_text_out
lfs setstripe -c 16 .

find -name "YU*" -delete

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

cat > INPUT_ORG << end_input_org
 &LMGRID
  startlat_tot = -4.95, startlon_tot = -4.95,
  pollat = 21.0, pollon = -144.0,
  dlon=0.025, dlat=0.025,
  ie_tot=376, je_tot=396, ke_tot=40,
 /END
 &RUNCTL
  dt=40,
  hstart=$HSTART, hstop=$HSTOP,
  ydate_ini='${YDATE_INI}',
  nprocx=$NPX2, nprocy=$NPY2, nprocio=0,
  hincmxt=24, hincmxu=24,
  itype_calendar=0,
  lreorder = .FALSE., lreproduce = .TRUE.,
  luseobs=.FALSE., lphys=.TRUE.,
  ldiagnos=.TRUE., ldfi=.FALSE.,
  ldebug_io=.TRUE., ldebug_dyn=.FALSE.,
  ldatatypes = .FALSE., itype_timing = 3,
  nboundlines = 3,
  ncomm_type = 3,
  lartif_data = .FALSE.,
  ltime_barrier = .FALSE.,
  idbg_level=2,
 /END
 &TUNING
  rlam_mom=0.0,
  rlam_heat=1.0,
  rat_sea=20.0,
  rat_lam=1.0,
  rat_can=1.0,
  wichfakt=0.0,
  c_lnd=2.0,
  c_sea=1.5,
  c_soil=1.0,
  pat_len=500.0,
  z0m_dia=0.2,
  crsmin=150.0,
  clc_diag=0.5,
  qc0=0.0002,
  q_crit=1.60,
 /END
end_input_org

cat > INPUT_IO  << end_input_io
 &IOCTL
  lasync_io=.FALSE, yform_read='ncdf',
  lbdclim=.TRUE.,
  nhour_restart=0,$HSTOP,24,
  ydir_restart='$OUT_DIR/restarts'
  ytunit_restart='d',
  ngribout=3,
  yncglob_title="Arctic 2.8 km dowscaling from 18 km 12-19 December 1997",
  yncglob_source = "/mnt/data/users/vplatonov/COSMO-CLM/CLM/ext/cosmoclm_5",
  yncglob_project_id = "-",
  yncglob_experiment_id ="Barents Sea wind investigation",
  ncglob_realization = 1,
  yncglob_contact = "vplatonov86@gmail.com",
  yncglob_references = "http://www.clm-community.eu/",
 /END
 &DATABASE
 /END
 &GRIBIN
  lan_rho_snow=.FALSE.,
  lbdana = .FALSE.,
  lana_rho_snow  = .FALSE.,
  lan_t_so0  = .TRUE.,
  lan_t_snow = .TRUE.,
  lan_t_cl   = .TRUE.,
  lan_w_snow = .TRUE.,
  lan_w_i    = .TRUE.,
  lan_w_cl   = .TRUE.,
  lan_vio3   = .TRUE.,
  lan_hmo3   = .TRUE.,
  lan_plcov  = .TRUE.,
  lan_lai    = .TRUE.,
  lan_rootdp = .TRUE.,
  lana_qi    = .TRUE., llb_qi    = .TRUE.,
! lana_qr_qs = .TRUE., llb_qr_qs = .TRUE.,
  lana_qr_qs = .FALSE., llb_qr_qs = .FALSE.,
  lana_qg    = .FALSE., llb_qg    = .FALSE.,
  ydirini='${LM_DIR}',
  lchkini=.TRUE.,
  hincbound=6.0,
  ydirbd='${LM_DIR}',
  lchkbd =.TRUE.,
  ytunitbd='d',
 /END
 &GRIBOUT
  yform_write='ncdf',
  hcomb=0,$HSTOP,1,
  yvarml='PP','FI',
         'QC','QI','QR','QS','QV','QV_S',
         'T','T_S','T_SNOW','T_SO','T_CL',
         'U','V','W',
         'TKE',
  yvarpl=' ',
  yvarzl=' ',
  luvmasspoint=.FALSE.,
  lcheck = .FALSE.,
  lwrite_const=.TRUE.,
  ydir='${OUT_DIR}/out01/',
  ytunit='d',
 /END
 &GRIBOUT
  yform_write='ncdf',
  hcomb=0,$HSTOP,1,
  yvarml='TOT_PREC',
         'ALB_RAD',
         'PMSL','PS','QV_2M','T_2M','U_10M',
         'V_10M','RELHUM_2M', 'TMIN_2M', 'TMAX_2M', 'VMAX_10M',
         'VABSMX_10M','VGUST_DYN','VGUST_CON',
         'TCM','FC',
  yvarpl=' ',
  yvarzl=' ',
! luvmasspoint=.TRUE.,
  luvmasspoint=.FALSE.,
  lcheck = .FALSE.,
  lwrite_const=.FALSE.,
  ydir='${OUT_DIR}/out02/',
  ytunit='d',
 /END
  &GRIBOUT
  yform_write='ncdf',
  hcomb=0,$HSTOP,1,
  yvarml=' ',
  yvarpl='FI','QV','T','U','V','RELHUM',
  yvarzl=' ',
! luvmasspoint=.TRUE.,
  luvmasspoint=.FALSE.,
  lcheck=.FALSE.,
  lwrite_const=.FALSE.,
  ydir='${OUT_DIR}/out03/',
  ytunit='d',
 /END
end_input_io

cat > INPUT_DYN << end_input_dyn
 &DYNCTL
  l2tls = .TRUE.,
  lexpl_lbc = .TRUE.,
  rlwidth = 70000.0,
  itype_lbc_qrsg = 2,
  lcond = .TRUE.,
  ldyn_bbc = .TRUE.,
  iadv_order = 5,
  nrdtau = 6,
 /END
end_input_dyn

cat > INPUT_PHY << end_input_phy
&PHYCTL
  lgsp=.TRUE.,
    itype_gscp=3,
  lrad=.TRUE.,
    nradcoarse=1,
    lradf_avg=.FALSE.
    hincrad=1.0,
!   ico2_rad=2,
!   lforest = .TRUE.,
    lforest = .FALSE.,
  ltur = .TRUE.,
!   ninctura=1,
!   lexpcor=.FALSE.,
!   ltmpcor=.FALSE.,
!   lprfcor=.FALSE.,
!   lnonloc=.FALSE.,
!   lcpfluc=.FALSE.,
    itype_turb=3,
    imode_turb=1,
    itype_tran=2,
    imode_tran=1,
!   itype_wcld=2,
!   icldm_rad =4,
!   icldm_turb=2,
!   icldm_tran=0,
    itype_synd=2,
  lsoil = .TRUE.,
    itype_evsl = 2,
    itype_trvg = 2,
    lmelt = .TRUE.,
    lmelt_var = .TRUE.,
    lmulti_layer = .TRUE.,
    ke_soil = 9,
    czml_soil=0.005,0.025,0.07,0.16,0.34,0.7,1.42,2.86,5.74,11.5,
    czbot_w_so=4.0,
    lsso=.FALSE.,
  llake=.FALSE.,
  lseaice=.FALSE.,
  itype_heatcond=2,
  lconv = .TRUE.,
!   nincconv = 4,
    itype_conv = 0,
!   lconf_avg = .TRUE.,
/END
end_input_phy

cat > INPUT_DIA << end_input_dia
 &DIACTL
  n0meanval=0, nincmeanval=24,
  lgplong=.TRUE., lgpshort=.FALSE., lgpspec=.FALSE.,
  n0gp=0, hincgp=3.0,
  stationlist_tot =0,0,69.202,35.107,'Teriberka',
		   0,0,69.200,33.483,'Polyarnoe',
		   0,0,68.956,33.043,'Murmansk',
		   0,0,69.936,31.979,'Vayda-Guba',
		   0,0,68.150,39.753,'Svyatoy_Nos',

		!  0,0,70.447,59.091,'Im. E.K. Fedorova',
		!  0,0,69.767,61.550,'Amderma',
		!  0,0,72.374,52.716,'Malye Karmakuly',
		!  0,0,69.714,66.814,'Marresala',
		!  0,0,69.533,49.083,'Kolguev Severnyi',
		!  0,0,67.806,46.652,'Mys Mikulkin',
		!  0,0,68.541,55.497,'Mys Konstantinovsky',
		!  0,0,67.625,53.018,'Naryan-Mar',
		!  0,0,67.492,63.972,'Vorkuta',
 /END
end_input_dia

cat > INPUT_INI << end_input_ini
 &INICTL
  ndfi=2,
  tspan=3600.0, taus=3600.0,
  dtbak=40.0, dtfwd=40.0,
 /END
end_input_ini

cat > INPUT_ASS << end_input_ass
 &NUDGING
  lnudge  =.FALSE.,
 /END
end_input_ass

echo 'CM_input  ==>  COSMO-CLM  ==>  CM_output'

#################################################
# run the program
#################################################

# lomonosov variant
echo ----- start CCLM
#sbatch -n $NP2 -p $SQUEUE ompi $DIR_EXE/cosmoclm_v5r_new
sbatch -n $NP2 -p $SQUEUE ompi $DIR_EXE/cosmoclm_v5r_new_options
echo ----- CCLM finished

#cat YUSPECIF YUCHKDAT > OUTPUT