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

INIDIR=/pfs/imk/imk-tro/Gruppe_Khodayar/S.H/CCLM_simulation/autumn2012/sensitvity_test_in2lm/PWP_run
OUTPUT=/work/kit/imk-tro/vt6615/cosmo_clm/cclm/autumn2012/PWP_7km

cd $PWD

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

NPX=8
NPY=8
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  =-23.04632, startlon_tot  =-26.36062,
  pollat = 40., pollon = -170.0, 
  dlon=0.0625, dlat=0.0625, 
  ie_tot=580, je_tot=420, ke_tot=40,
 /END
 &RUNCTL
  dt=60,
  hstart=0,   hstop=48,
  ydate_ini='2012080800', ydate_end='2012080800',
  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.,
  ngribout=1,
 ! nhour_restart=744,744,744,
 ! ydir_restart='${SPDIR}/step_by_step/cclm_to_cclm/data/cclm_output/restarts/'
 ! ytunit_restart='d',
 /END
 &DATABASE
 /END
 &GRIBIN
  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 = .FALSE.,  llb_qr_qs = .FALSE.,
  lana_qg    = .FALSE., llb_qg    = .FALSE.,
  ydirini='$INIDIR' 
  lchkini=.TRUE.,
  hincbound=6,
  ydirbd='$INIDIR',
  lchkbd =.TRUE.,
  ytunitbd='d',
 /END
 &GRIBOUT
  yform_write='ncdf',
  hcomb=0,2760,1,
 yvarml= 'U         ','V         ','W         ','T         ',        ! Wind and temperature
         'QV        ','QC        ','QI        ','QS        ',        ! Specific rain/snow content
         'QR        ','P         ','PP        ','QVSFLX    ',        ! 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
         'ASOB_S    ','ATHB_S    ','ASOB_T    ','ATHB_T    ',        ! Averaged surface/TOA shortwave and longwave radiation
        !'ASWDIR_S  ','ASWDIFD_S ','ASWDIFU_S ',         
         '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_ML   ','CIN_ML    ','CAPE_MU   ','CIN_MU    ',        ! CAPE/CIN of mean surface layer (ML) and for most unstable parcel (MU)
         'CAPE_CON  ','QCVG_CON  ','TKE       ','TKE_CON   ',        ! CAPE specific(CON),moisture convergence in the air for kuo type closure(QCVG_CON)
         'SLI       ','SI        ','EVATRA_SUM','AEVAP_S   ',        ! Showalter index (SI)and surface lifted index (SLI) and Evaporation products        
         'BAS_CON   ','TOP_CON   ','HPBL      ','TRA_SUM   ',                     ! Index convective cloud base (BAS), cloud top(TOP),Height of boundary layer(HPBL) 
         'HTOP_DC   ','HTOP_CON  ','HBAS_CON  ',                     ! Height of dry convective top (HTOP_DC)and cloud top (HTOP_CON),cloud base(HBAS),  
         'PMSL      ','LCL_ML    ','LFC_ML    ',                     ! Mean sea level presure(PMSL),Lifting condensation level(LCL_ML),
         'RUNOFF_S  ','RUNOFF_G  ',                                  ! Surface water drainage, soil water drainage
         'RLAT','RLON','FR_LAND','SOILTYP','HSURF','Z0','FIS',       ! Constants
         'LAI','PLCOV','T_SO','T_G','W_I','W_SO','FOR_D','FOR_E',    ! Soil and vegetation parameters
         'ROOTDP','ALB_RAD','W_SO_ICE','FRESHSNW','RHO_SNOW',        ! Soil and vegetation parameters
  yvarpl='T','U','V','W','RELHUM','FI','QI','QV',  !'QS','QR',
  plev=100,150,200,250,300,400,500,600,700,850,900,925,950,975,1000,1050,
  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=85000.0,
  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_root = 2,
    itype_heatcond = 2,
    itype_evsl = 3,
    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,
  lconv = .TRUE.,
    nincconv = 4,
    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


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

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

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

cat YUSPECIF YUCHKDAT > OUTPUT
