#!/bin/bash
#

INIDIR=/work/kit/imk-tro/vt6615/cosmo_clm/int2lm/test_SOP1_044Grad/CCLM
OUTPUT=/work/kit/imk-tro/vt6615/cosmo_clm/cclm/test_SOP1

export LIBDWD_BITMAP_TYPE=ASCII
export LIBDWD_FORCE_CONTROLWORDS=1


#### !!!! Adopt this directory !!!!
SPDIR=/home/kit/imk-tro/vt6615/COSMO_CLM/cclm-sp_2.1

cd ${SPDIR}/step_by_step/gcm_to_cclm/Runscripts/test_SOP1/cclm_7km/test_cclm_run


#################################################
# number of processors
#################################################

NPX=4
NPY=4
NPIO=0
NP1=`expr $NPX \* $NPY`
NP=`expr $NP1 + $NPIO`
N1=`expr $NP + 15`
NODES=`expr $N1 \/ 16`
#NN=`expr $NPY \/ $NNP`
anz_procs=$((${NPX}*${NPY}+${NPIO}))  # Anzahl Prozessoren

#################################################
# global settings
#################################################
#DATE=2012102000

class=d
wtime=60 # Zeit, wie lange Job rechnen darf

#################################################
# cleanup
#################################################
rm -f YU* OUTPUT
rm -f cclm_job
mv -f Job*.* ./Jobs/
rm -f $LM_DIR/l*

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

cat > INPUT_ORG << end_input_org
 &LMGRID
  startlat_tot  =-24.26132, startlon_tot  =-25.96312,
  pollat = 40, pollon = -170.0, 
  dlon=0.44, dlat=0.44, 
  ie_tot=80, je_tot=50, ke_tot=40,
 /END
 &RUNCTL
  dt=300,
  hstart=0,   hstop=2184,
  ydate_ini='2012090100', ydate_end='2012113018',
  nprocx= ${NPX}, nprocy= ${NPY}, nprocio=${NPIO},
  hincmxt=24, hincmxu=24,
  itype_calendar=0,
  lreorder = .FALSE.,  lreproduce = .TRUE.,
  ldatatypes = .FALSE.,
  nboundlines = 3, 
  ncomm_type = 3,
  lartif_data = .FALSE.,
  ldump_ascii = .FALSE.,
 /END
 &TUNING
  wichfakt=0.,
  tur_len=500.,
  v0snow=20.,
  tkhmin=0.35,
  tkmmin=1.,
  rlam_heat=0.5249,
  mu_rain=0.5,
  entr_sc=0.0002,
  uc1=0.0626,
  fac_rootdp2=0.9000,
  soilhyd=1.6200
 /END
end_input_org

cat > INPUT_IO  << end_input_io
 &IOCTL
  lasync_io=.FALSE., yform_read='ncdf',  
  lbdclim=.TRUE.,
  !nhour_restart=744,744,744,
  !ydir_restart='${SPDIR}/step_by_step/gcm_to_cclm/data/cclm_output/restarts/'
  !ytunit_restart='d',
  ngribout=1,
  !yncglob_title="Europe 0.44 Starter Package",
  !yncglob_source = "${SPDIR}/src/cclm/cclm.exe}",
  !yncglob_project_id = "-",
  !yncglob_experiment_id ="spcclm",
  !ncglob_realization = 1,
  !yncglob_contact = "YourAddress",
  !yncglob_references = "http://www.clm-community.eu/",
 /END
 &DATABASE
 /END
 &GRIBIN
  lbdana = .FALSE.,
  lana_qi = .FALSE.,
  llb_qi = .TRUE.,
  lana_qr_qs = .FALSE.,
  llb_qr_qs = .FALSE.,
  lana_qg = .FALSE.,
  llb_qg = .FALSE.,
  lana_rho_snow  = .FALSE.,
  lan_t_s    = .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.,
  lan_rho_snow= .FALSE.,
  ydirini='$INIDIR',
  lchkini=.TRUE.,
  hincbound=6,
  ydirbd='$INIDIR',
  lchkbd =.FALSE.,
  ytunitbd='d',
 /END
&GRIBOUT
  yform_write='ncdf',
  hcomb=0,2184,3,
  yvarml='U         ','V         ','W         ','T         ',        ! Wind and temperature
         'QV        ','QC        ','QI        ','QS        ',        ! Specific rain/snow content
         'QR        ','P         ','PP        ',                     ! PP=deviation from reference pressure
         'PS        ','T_S       ','QV_S      ','T_SNOW    ',        ! Surface pressure,temperature,specific water content          
         'W_SNOW    ','U_10M     ','V_10M     ','VMAX_10M  ',        ! Wind in 10m
         'T_2M      ','TD_2M     ','TMIN_2M   ','TMAX_2M   ',        ! Temperature 2m
         'QV_2M     ','RELHUM_2M ','CLDEPTH   ',                     ! Specific and relative humidity, normalized cloud depth
         'CLCT      ','CLCH      ','CLCM      ','CLCL      ',        ! Total cloud cover=CLCT,Cloud cover high/middle/low clouds
         'RAIN_GSP  ','SNOW_GSP  ','RAIN_CON  ','SNOW_CON  ',        ! Sum of rain/snow precipitation and rain/snow convective
         'TOT_PREC  ','TWATER    ','TQV       ','TOT_PR    ',        ! Total precipitation and total water content(TWATER) and precipitable water(TQV) 
         'ASHFL_S   ','ALHFL_S   ','SHFL_S    ','LHFL_S    ',        ! Average sensible and latent heat flux,surface sensible and latent heat flux
         'CAPE_CON  ','CAPE_ML   ','CIN_ML    ',                     ! CAPE specific(CON),CAPE/CIN of mean surface layer (ML)
         'SLI       ','SI        ','EVATRA_SUM',                                  ! Showalter index (SI)and surface lifted index (SLI)        
         'BAS_CON   ','TOP_CON   ',                                  ! Index convective cloud base (BAS), cloud top(TOP)
         'HTOP_DC   ','HTOP_CON  ','HBAS_CON  ',                     ! Height of dry convective top (HTOP_DC)and cloud top (HTOP_CON),cloud base(HBAS),  
         'PMSL      ','LCL_ML    ','HPBL      ','AEVAP_S',           ! Mean sea level presure(PMSL),Liftin condensation level(LCL_ML),Height of boundary layer(HPBL) 
         'RUNOFF_S  ','RUNOFF_G  ',                                  ! Surface water drainage, soil water drainage
         'RLAT','RLON','FR_LAND','SOILTYP','HSURF','Z0',             ! Constants
         'LAI','PLCOV','T_SO','T_G','W_I','W_SO','FIS','FOR_D',
         'FOR_E','ROOTDP','ALB_RAD','W_SO_ICE','FRESHSNW','RHO_SNOW', ! Soil and vegetation parameters
  yvarpl='FI','QV','T','U','V','RELHUM',
  plev=200.,500.,850.,925.,
  luvmasspoint=.FALSE.,
  lcheck = .FALSE.,
  lwrite_const=.TRUE.,
  ydir='$OUTPUT',
  ytunit='d',
 /END
end_input_io

cat > INPUT_DYN << end_input_dyn
 &DYNCTL
  alphaass=0.5300,
  ldiabf_lh = .TRUE.,
  ldyn_bbc = .TRUE.,
  y_scalar_advect='BOTT2',
  lexpl_lbc = .TRUE.,
  l2tls = .TRUE.,
  ldyn_bbc = .FALSE.,
  rlwidth = 500000.,
  nrdtau = 6,
  iadv_order = 3,
  itype_bbc_w = 1,
  l_diff_Smag = .FALSE.,
  itype_fast_waves = 1,
  lspecnudge=.FALSE.,alpha_sn=0.05,
  isc_sn=6, jsc_sn=6,
  yvarsn='U     ','V    ',
 /END
end_input_dyn

cat > INPUT_PHY << end_input_phy
&PHYCTL
  lgsp=.TRUE.,
    itype_gscp=3,
  lrad=.TRUE.,
    hincrad=1.0,
    ico2_rad=2,
    lforest = .TRUE.,
  ltur = .TRUE.,
    limpltkediff = .FALSE.,
    lexpcor = .FALSE.,
  lsoil = .TRUE.,
    lmulti_snow = .FALSE.,
    itype_heatcond = 2,
    itype_evsl = 3,
    itype_trvg = 2,
    lmelt = .TRUE.,
    lmelt_var = .TRUE.,
    lmulti_layer = .TRUE.,
 !  ke_soil = 7,
 !  czml_soil = 0.005, 0.02, 0.06, 0.18, 0.54, 1.62, 4.86, 14.58, 
!   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,
  lconv = .TRUE.,
    nincconv = 2,
    itype_conv = 0,
    lconf_avg = .TRUE.,
  lsso=.TRUE.,
  llake=.FALSE.,
  lseaice=.FALSE.,
  itype_albedo=2,
/END
end_input_phy

cat > INPUT_DIA << end_input_dia
 &DIACTL
  n0meanval=0, nincmeanval=24,
  lgplong=.FALSE., lgpshort=.TRUE., lgpspec=.FALSE.,
  n0gp=0,      hincgp=6.0,
  stationlist_tot= 0, 0, 52.220, 14.135, 'Lindenberg_Obs',
                   0, 0, 53.633,  9.983, 'Hamburg',
 /END
end_input_dia

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

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

#echo starte Job...s


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

cat > cclm_job << ****
job_submit -p${anz_procs} -c $class -t $wtime -m 3000 mpirun ${SPDIR}/src/cclm/bin/cclm.exe

****
chmod u+x cclm_job
./cclm_job
job_queue
