#!/bin/ksh
#
# The LM Job
#
rm -f cosmo_de_job
#################################################
# number of processors
#################################################

NPX=1           ## DO NOT CHANGE (for optimum vactorization)
NPY=16
NPIO=0
NP1=`expr $NPX \* $NPY`
NP=`expr $NP1 + $NPIO`
N1=`expr $NP + 15`
NODES=`expr $N1 \/ 16`

echo 'number of processors:' $NP 
echo 'number of nodes:     ' $NODES

DATE=2009070500

INIDIR=/e/uwork/dlrdahl/COSMO_DE_INPUT/$DATE
BD_DIR=/e/uwork/dlrdahl/COSMO_DE_INPUT/$DATE
OUTPUT=/e/uwork/dlrdahl/COSMO_DE_OUTPUT/$DATE
RESTAR=/e/uwork/dlrdahl/COSMO_DE_restart/$DATE

cat > cosmo_de_job << ****

#################################################
# batch commandos
# "PBS directives"
#################################################

#PBS -q normal@sx9esiox0
#PBS -l cpunum_job=$NPY
#PBS -b $NODES
#PBS -T mpisx
#PBS -j o
#PBS -N cosmo_de_joda

## cd /e/uhome/uschaett/ALL_EXT_USERS/cosmo_080924_4.6/

cd /e/uhome/dlrdahl/cosmo_4.6_modified/

#################################################
# global settings
#################################################

rm -f YU* M_*
rm -f $OUTPUT/l*
rm -f $RESTAR/l*

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

cat > INPUT_ORG << end_input_org
 &LMGRID
  startlat_tot  = -5.0,  startlon_tot  =  -5.0,
  pollat=40.0,        pollon=-170.0,
  dlon=0.025,         dlat=0.025,
  ie_tot=421,         je_tot=461,          ke_tot=50,
 /
 &RUNCTL
  hstart = 0.0,   hstop = 24.0,    dt      = 25.0,    ydate_ini='$DATE',
  nprocx = $NPX,  nprocy = $NPY,   nprocio = $NPIO,
  lphys      = .TRUE.,   luse_rttov    = .TRUE.,  luseobs = .FALSE., leps = .FALSE.,
  lreorder   = .FALSE.,  lreproduce    = .TRUE.,   itype_timing = 4,
  ldatatypes = .FALSE.,   ltime_barrier = .FALSE.,  ncomm_type=3,
  nboundlines= 3, idbg_level=6,
 /
 &TUNING
  clc_diag  =   0.75,
  pat_len   = 500.0,
  rlam_heat =   1.0,
  rlam_mom  =   0.0,
  rat_lam   =   1.0,
  rat_can   =   1.0,
  rat_sea   =  20.0,
  c_lnd     =   2.0,
  c_soil    =   1.0,
  c_sea     =   1.5,
  z0m_dia   =   0.2,
  crsmin    = 150.0,
  rat_sea   =  20.0,
  wichfakt  =   0.0,
  qc0       =   0.0002,
 /
end_input_org

cat > INPUT_SAT  << end_input_sat
 &SATCTL
  num_sensors=2,
  sat_input_01='METEOSAT',7,'MVIRI', 2, .TRUE., .TRUE., .TRUE., .TRUE.,
  nchan_input_01=1,2
  sat_input_02='MSG'     ,2,'SEVIRI',8, .TRUE., .TRUE., .TRUE., .TRUE.,
  nchan_input_02=1,2,3,4,5,6,7,8
  lcon_clw=.TRUE.,
 /
end_input_sat

cat > INPUT_IO  << end_input_io
 &IOCTL
  lgen=.FALSE, lasync_io=.FALSE, ngribout=2,
! yform_write='ncdf',
! nsma_stat=0,
! nhour_restart=6,12,6,
! ydir_restart='$RESTAR',
! ytunit_restart='f',
 /
 &DATABASE
 /
 &GRIBIN
  lan_t_so0=.TRUE., lan_t_cl=.TRUE., lan_w_cl=.TRUE., lan_vio3=.TRUE.,
  lan_hmo3=.TRUE., lan_plcov=.TRUE., lan_lai=.TRUE., lan_rootdp=.TRUE.,
  lan_t_snow=.TRUE., lan_w_i=.TRUE., lan_w_snow=.TRUE., lan_rho_snow=.TRUE.,
  hincbound=1.0,
  lchkini    = .TRUE.,  lchkbd    = .TRUE.,  lbdana=.FALSE.,
  lana_qi    = .TRUE.,  llb_qi    = .TRUE.,  lana_rho_snow=.TRUE., 
  lana_qr_qs = .FALSE,  llb_qr_qs = .FALSE,
  lana_qg    = .FALSE., llb_qg    = .FALSE.,
  ydirini='$INIDIR',
  ydirbd='$BD_DIR',
 /
 &GRIBOUT
    hcomb=0.0, 0.25, 0.25
    lanalysis=.FALSE.,
    lcheck=.TRUE.,
    lwrite_const=.TRUE.,
    l_p_filter=.TRUE.,
    l_z_filter=.TRUE.,
    nunit_of_time=1,
    ysystem='file',
    ydir='$OUTPUT',
    yvarml='CLCT', 'CLCH', 'CLCM', 'CLCL', 'CLC', 'ASOB_S', 'ASOB_T', 'ATHB_S', 'ATHB_T'
!    yvarpl=''
!    yvarzl=''
!    yvarsl='default',
!    zlev=500.0,1000.0,1500.0,2000.0,3000.0,5000.0,
 /
 &GRIBOUT
    hcomb=0.0,0.25,0.25,
    lcheck=.TRUE.,
    lanalysis=.FALSE.,
    lwrite_const=.FALSE.,
    l_p_filter=.TRUE.,
    l_z_filter=.TRUE.,
    nunit_of_time=13,
    ysystem='file',
    ydir='$OUTPUT',
    ysuffix='_2',
    yvarml='W         ','CLC       ','Q_SEDIM   ',
           'RAIN_GSP  ','SNOW_GSP  ','GRAU_GSP  ','PRR_GSP   ',
           'PRS_GSP   ','PRG_GSP   ','TOT_PREC  ','TQV       ',
           'TQC       ','TQI       ','HBAS_SC   ','HTOP_SC   ',
           'DBZ_850   ','DBZ_CMAX  ',
    yvarsl='default',
 /
 &GRIBOUT
    hcomb=0.0,0.25,0.25,
    lcheck=.TRUE.,
    lanalysis=.FALSE.,
    lwrite_const=.FALSE.,
    l_p_filter=.TRUE.,
    l_z_filter=.TRUE.,
    nunit_of_time=13,
    ysystem='file',
    ydir='$OUTPUT',
    ysuffix='_3',
    yvarml='W         ','CLC       ','Q_SEDIM   ','U','V     ',
           'RAIN_GSP  ','SNOW_GSP  ','GRAU_GSP  ','PRR_GSP   ',
           'PRS_GSP   ','PRG_GSP   ','TOT_PREC  ','TQV       ',
           'TQC       ','TQI       ','HBAS_SC   ','HTOP_SC   ',
           'DBZ_850   ','DBZ_CMAX  ',
    yvarsl='default',
 /
end_input_io

cat > INPUT_DYN << end_input_dyn
 &DYNCTL
    l2tls=.TRUE., lcond=.TRUE., ldyn_bbc=.TRUE., ldiabf_lh=.TRUE.,
    lexpl_lbc=.TRUE., lsl_adv_qx=.FALSE., lspubc=.TRUE.,
    betasw=0.4, epsass=0.15,
    lhdiff_mask=.TRUE., itype_hdiff=2,
    hd_corr_q=0.0, hd_corr_t=0.75, hd_corr_u=0.75, hd_dhmax=250.,
    irunge_kutta=1, irk_order=3, iadv_order=5,
    nrdtau=5, xkd=0.1,
    rlwidth=50000.0, itype_lbcqx=2,
 /
end_input_dyn

cat > INPUT_PHY << end_input_phy
 &PHYCTL
    lgsp=.TRUE.,
      lprogprec=.TRUE.,
      ltrans_prec=.TRUE.,
      itype_gscp=4,
    lrad=.TRUE.,
      nradcoarse=2,
      lradf_avg=.TRUE.
      hincrad=0.25,
      lforest=.FALSE.,
    ltur=.TRUE.,
      ninctura=1,
      lexpcor=.TRUE.,
      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,
      lmulti_layer=.TRUE.,
      lmelt=.TRUE.,
      lmelt_var=.TRUE.,
      ke_soil = 7,
      czml_soil = 0.005, 0.02, 0.06, 0.18, 0.54, 1.62, 4.86, 14.58,
    lconv=.TRUE.
      lcape=.FALSE.,
      lconf_avg=.TRUE.,
      lconv_inst=.TRUE.,
!     ltiedtke=.FALSE., lshallow=.TRUE.,
      itype_conv=3,
    lightning=.TRUE.
      itype_light=1
      hinclight=0.25 
 /     
end_input_phy

cat > INPUT_DIA << end_input_dia
 &DIACTL
  n0meanval=0, nincmeanval=1,
  lgplong=.TRUE., lgpshort=.FALSE., lgpspec=.FALSE.,
  n0gp=0,      hincgp=0.25,
  stationlist_tot= 0, 0, 46.700,  6.200, 'RODGAU',
                   0, 0, 50.050,  8.600, 'Frankfurt-Flughafen',
                   0, 0, 52.220, 14.135, 'Lindenberg_Obs',
                   0, 0, 52.167, 14.124, 'Falkenberg',
                   0, 0, 52.182, 13.954, 'Waldstation_Kehrigk',
                   0, 0, 47.800, 10.900, 'Hohenpeissenberg',
                   0, 0, 50.783,  6.100, 'Aachen',
                   0, 0, 48.433, 10.933, 'Augsburg',
                   0, 0, 49.980, 11.682, 'Bayreuth',
                   0, 0, 52.300, 10.450, 'Braunschweig',
                   0, 0, 51.967,  4.933, 'Cabauw',
                   0, 0, 50.283, 10.983, 'Coburg',
                   0, 0, 47.419, 10.982, 'Schneefernerhaus',
                   0, 0, 49.920,  7.960, 'Geisenheim',
                   0, 0, 53.633,  9.983, 'Hamburg',
                   0, 0, 54.750, 17.533, 'Leba',
                   0, 0, 51.200, 16.200, 'Legnica',
                   0, 0, 53.780, 21.580, 'Mikolajki',
                   0, 0, 37.122, -5.579, 'Moron_la_Frontera',
                   0, 0, 53.779,  8.669, 'Nordholz',
                   0, 0, 48.067, 11.267, 'Oberpfaffenhofen',
                   0, 0, 57.425, 18.983, 'Oestergarsholm',
                   0, 0, 46.817,  6.935, 'Payerne',
                   0, 0, 53.133, 16.750, 'Pila',
                   0, 0, 52.383, 13.067, 'Potsdam',
                   0, 0, 54.533,  9.550, 'Schleswig',
                   0, 0, 48.833,  9.200, 'Stuttgart',
                   0, 0, 51.350, 19.870, 'Sulejow',
                   0, 0, 45.217,  7.650, 'Turin-Caselle',
                   0, 0, 63.086, 25.980, 'Viitasaari',
                   0, 0, 54.633, 25.100, 'Vilnius',
                   0, 0, 52.167, 20.967, 'Warschau',
                   0, 0, 48.400, 11.700, 'Weihenstephan',
                   0, 0, 51.217, 18.567, 'Wielun',
                   0, 0, 51.550, 23.530, 'Wlodawa',
                   0, 0, 53.311, 11.838, 'Ziegendorf',
 /
end_input_dia


cat > INPUT_EPS << end_input_eps
 &EPSCTL
  iepsmem=0, iepstyp=55, iepstot=15,
 /
end_input_eps

cat > INPUT_ASS << end_input_ass
 &NUDGING
  lnudge  =.true.,
  hnudgsta= 0.0,  hnudgend = 4.0,  tconbox = 180.0,
  lverif  =.true.,
  llhn    =.false., llhnverif=.false., lhn_wweight=.false.,
! llhn    =.true., llhnverif=.true., lhn_wweight=.true.,
  rqrsgmax= 0.4,
  radar_in='/gtmp/uschaett/COSMO_4.4/RADAR/',
  hversta = 0.001, hverend = 4.0,
  khumbal = 100,
  mruntyp = 2,
  ntpscor = 1,  ptpstop=400.0,  luvgcor=.TRUE.,
  ltipol  =.TRUE.,  tipolmx = 3.0,  wtukrsa = 3.0,  wtukrse = 1.0,
  ltipsu  =.TRUE.,  tipmxsu = 1.0,  wtuksua = 1.5,  wtuksue = 0.5,
                                    wtukara = 1.5,  wtukare = 0.5,
  msprpar = 1,  msprpsu = 0,
  gnudg   = 0.0006, 0.0012, 0.0006, 0.0006,
  gnudgsu = 0.0006, 0.0012, 0.0000, 0.0006,
  gnudgar = .0006, .0000, .0006, .0000,
  vcorls  = .333 , .333 , .04  , .04  ,  vcutof  = 0.75, 0.75, 1.0 , 1.0 ,
  vcorlsu = .013 , .013 , .002 ,.00001,  vcutosu = 0.75, 0.75, 4.0 ,0.001,
  vcsnisu = 2500., 2500., 9.   , 9.   ,
                                         rhvfac  = 1.0 , 0.0 , 0.83, 0.83,
  rhinfl  =    0.,   70.,    0.,    0.,  rhtfac  = 1.3 , 1.43, 1.3 , 1.3 ,
  rhiflsu =   70.,   70.,  100.,   70.,  rhtfsu  = 1.0 , 1.43, 1.0 , 1.0 ,
  fnondiv = 0.8  ,   cnondiv = 0.1    ,  cutofr  = 3.5 , 3.5 , 3.5 , 3.5 ,
  tnondiv = 1.1  ,                       cutofsu = 2.0 , 3.5 , 2.0 , 2.0 ,
  topobs  =  849., 1099.,  799.,  699.,
  botmod  = 1099., 1099., 1099.,  899.,
  lscadj  =.TRUE.,.TRUE.,.TRUE.,.FALSE.,
  dtqc    =  720.,                       qcvf    = 5.00, 1.00,10.00, 1.00,
  qcc     =  0.00,500.00,  0.00,  0.00,
  qccsu   =   12.,  500.,   12.,    .7,
  lsynop  =.true.,
  laircf  =.true.,
  ldribu  =.true.,
  ltemp   =.true.,
  lpilot  =.true.,
  lcd132  =.true., lcd133=.false., lcd136=.false.,
  maxmlo  = 1600,  maxsgo = 5000,  maxuso = 3500,  nolbc = 5,
  altopsu =  100., 5000., 5000., 5000.,  thairh  = 20.,
  exnlat  = 90.,  exslat =-90.,  exwlon = -180.,  exelon = 180.,
  lsurfa  =.false.,
  lt2m    =.false.,   ht2a = 0.,  ht2i = 1.,
  lrh2m   =.false.,  hh2a = 0.,  hh2i = 1.,
  lprecp  =.false., hprc = 0.,  raintp = 12.,
  lpraof  =.false.,  lprodr =.true. ,  ldiasa =.true.,  noctrq = 9,
  dinlat  = 55.,  dislat = 45.,  diwlon =  7.,  dielon = 14.,
  ionl    =167,   jonl =103,     ionl2 =167,    jonl2 =103,
  lff10m  =.true., hffa=0., hffi=1.,
 /
end_input_ass

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

cp /e/rhome/routfor/routfox/lm/const/rtcoef_meteosat_7_mviri.dat .
cp /e/rhome/routfor/routfox/lm/const/rtcoef_msg_2_seviri.dat .

rm -f mon.out.* ftrace.out.*

export MPIPROGINF=yes
#export MPISUSPEND=on
export MPIPROGINF=ALL_DETAIL
#export F_FTRACE=yes
export F_ERROPT1=255,255,0,0,2,2,2,2
export MPIEXPORT="F_ERROPT1 MPIPROGINF F_PROGINF LIBDWD_MPIEXPORT"

mpirun -nn $NODES -nnp $NPY lmparbin
#mpirun -nn $NODES -nnp 8  tstlm_f90

ftrace -all -f ftrace.out* -fmt1
rm -f mon.out.* ftrace.out.*
#prof -m mon.out.0.0 lmparbin

rm -f rtcoef_meteosat_7_mviri.dat rtcoef_msg_2_seviri.dat

#################################################
# cleanup
#################################################

rm M_*

rm -f INPUT_ORG INPUT_IO INPUT_DYN INPUT_DIA INPUT_PHY INPUT_INI
rm -f INPUT_ASS INPUT_SAT INPUT_EPS

****
chmod u+x cosmo_de_job

nqsub cosmo_de_job
rm -f cosmo_de_job

