#!/bin/ksh
################################################
# batch settings for int2lm run on Cray at DWD #
################################################
#PBS -q xc_norm_h
######PBS -N int2lm_@{EXPID}
#PBS -N int_@{EXPID}
#PBS -o @{JOBLOGFILE}
#PBS -j oe
#PBS -l select=@{NODES}
#PBS -l place=pack

set -e

echo int2lm @{YYYY_MM} START $(date) >> @{PFDIR}/@{EXPID}/chain_status.log

#
cd @{WORKDIR}/@{EXPID}/joboutputs/int2lm/@{YYYY_MM}

# path to external parameters
#LM_EXT=/e/uhome/ktrusilo/DATA_EXTPAR/extpar_city_Hamburg
LM_EXT=/e/uwork/sbrienen/projekte/quwind100/ext 
#LM_EXT=/e/uwork/sbrienen/externe_daten

#################################################
# loop over data sets
#################################################

# SB: here we use yyyymmddhh input files for int2lm
currdate_cut=$(echo @{CURRENT_DATE} | cut -c1-10)
nextdate_cut=$(echo @{NEXT_DATE} | cut -c1-10)
#HSTOP=$(@{UTILSBINDIR}/cfu_xce get_hours @{CURRENT_DATE} @{NEXT_DATE} @{ITYPE_CALENDAR})
HSTOP=$(@{UTILSBINDIR}/cfu_xce get_hours ${currdate_cut} ${nextdate_cut} @{ITYPE_CALENDAR})

# a little longer for the last run
# SB: here we use yyyymmddhh input files for int2lm
ydatestop_cut=$(echo @{YDATE_STOP} | cut -c1-10)
#if [[ @{NEXT_DATE} -ge @{YDATE_STOP} ]]; then
if [[ ${nextdate_cut} -ge ${ydatestop_cut} ]]; then
  #let "HSTOP = HSTOP + 12"
  let "HSTOP = HSTOP + 3"
fi

if [ -f YUDEBUG ]
then
  rm YUDEBUG
fi

# SB: here we use yyyymmddhh input files for int2lm
#YDATE_INI=@{CURRENT_DATE}
YDATE_INI=${currdate_cut}
echo YDATE_INI ${YDATE_INI}
#################################################
# cat together the INPUT*-files
#################################################

cat > INPUT <<**
 &CONTRL
  ydate_ini = '${YDATE_INI}',
  hstart = 0.0, hstop = ${HSTOP}, hincbound = @{HINCBOUND},
  linitial = .TRUE.,  lboundaries = .TRUE.,
  nprocx = @{NPX}, nprocy = @{NPY},
  lfilter_oro=.FALSE.,
  lfilter_pp=.TRUE.,     !.FALSE.,
  lbalance_pp=.TRUE.,    !.FALSE.,
  norder_filter=5,ilow_pass_oro=4,ilow_pass_xso=5,
  yinput_model='COSMO',    
  idbg_level=20,
  ltime_mean=.TRUE., 
  luvcor=.FALSE.,      !.TRUE.,
  luse_t_skin=.FALSE., !.TRUE.,    
  lbdclim=.TRUE.,lreorder=.TRUE.,
  nincwait=5, nmaxwait=20,
  lmulti_layer_in=.TRUE., lmulti_layer_lm=.TRUE.,
  l_cressman=.FALSE.,
  l_bicub_spl=.FALSE.,     !.TRUE.,
  llbc_smooth=.TRUE.,    !.FALSE.,
  nlbc_smooth=20,
  lprog_qi=.TRUE.,       !.FALSE.,       
  !lprog_qr_qs=.TRUE.,    ! Note: Direct Downscaling has to use lprog_qr_qs=.FALSE.
  !lprog_rho_snow=.TRUE., ! Note: Direct Downscaling has to use lrpog_rho_snow=.FALSE.
  lprog_qr_qs=.FALSE.,    ! Note: Direct Downscaling has to use lprog_qr_qs=.FALSE.
  lprog_rho_snow=.FALSE., ! Note: Direct Downscaling has to use lrpog_rho_snow=.FALSE.
  itype_w_so_rel=1,
  itype_t_cl=1,     ! Note: Direct Downscaling has to use itype_t_cl=1
  itype_rootdp=3,  !0,
  itype_ndvi=0,
  itype_calendar=@{ITYPE_CALENDAR},      ! Note: ckeck the calendar of your basic (global) forcing daa carefully and adapt itype_calendar if necessary
  leps_bc=.FALSE.,
  lforest=@{LFOREST},
  !itype_aerosol=2,       ! Note: in CCLM itype_aerosol=1 is used
  itype_aerosol=1,
! itype_albedo=2,
!  itype_albedo=3,
  eps_filter=0.1,
  rxso_mask=750,
  !lsso=.TRUE.,           ! Note: in CCLM lsso=.FALSE. is used
  lsso=.FALSE.,           ! Note: in CCLM lsso=.FALSE. is used
  !lradtopo=.TRUE.,       ! Note: needs the arrays HORIZON and SKYVIEW in external data set;
  lradtopo=.FALSE.,       ! Note: needs the arrays HORIZON and SKYVIEW in external data set;
  !lurban=.TRUE.,         ! Note in CCLM lurban=.FALSE., excecpt the CMCC group (Mario)
  lurban=.FALSE.,         ! Note in CCLM lurban=.FALSE., excecpt the CMCC group (Mario)
  llake=@{LLAKE},
  !llake_coldstart=.TRUE. ! Note: this setting is necessary for successful use of llake=.TRUE. in CCLM
  llake_coldstart=.FALSE. ! Note: this setting is necessary for successful use of llake=.TRUE. in CCLM
  lvertwind_ini=@{LVERTWIND_INI},
  lvertwind_bd=.FALSE.    ! Note: direct downscaling has to use lvertwind_bd=.FALSE.
 /END
 &GRID_IN
  pcontrol_fi = 30000.,
  ie_in_tot = @{IE_TOTIN},
  je_in_tot = @{JE_TOTIN},
  ke_in_tot = @{KE_TOTIN},
  dlon_in = @{DLONIN}, dlat_in = @{DLATIN},
  startlat_in_tot = @{STARTLAT_TOTIN},
  startlon_in_tot = @{STARTLON_TOTIN},
  !endlat_in_tot = 23.265,
  !endlon_in_tot = 19.585,
  pollat_in = @{POLLATIN}, pollon_in = @{POLLONIN},
  polgam_in=@{POLGAMIN},
  ke_soil_in = @{KE_SOIL},
  czml_soil_in = @{CZML_SOIL},
  lushift_in=.FALSE.,   !.TRUE.,
  lvshift_in=.FALSE.,   !.TRUE.,
 /END
 &LMGRID
  startlat_tot=@{STARTLAT_TOT}, startlon_tot=@{STARTLON_TOT},
  pollat=@{POLLAT}, pollon=@{POLLON},polgam=@{POLGAM},
  dlon=@{DLON}, dlat=@{DLAT},
  ielm_tot=@{IE_TOT}, jelm_tot=@{JE_TOT}, kelm_tot=@{KE_TOT},
  irefatm=2,  !=1,
  lanalyt_calc_t0p0=.FALSE.,   ! .TRUE.
  ke_soil_lm=@{KE_SOIL},
  czml_soil_lm=@{CZML_SOIL},
  ivctype=2,
  vcflat=11357.00,
  vcoord_d = 22000.00,  21000.00,  20028.57,  19085.36,  18170.00,
             17282.14,  16421.43,  15587.50,  14780.00,  13998.57,
             13242.86,  12512.50,  11807.14,  11126.43,  10470.00,
              9837.50,   9228.57,   8642.86,   8080.00,   7539.64,
              7021.43,   6525.00,   6050.00,   5596.07,   5162.86,
              4750.00,   4357.14,   3983.93,   3630.00,   3295.00,
              2978.57,   2680.36,   2400.00,   2137.14,   1891.43,
              1662.50,   1450.00,   1253.57,   1072.86,    907.50,
               757.14,    621.43,    500.00,    392.50,    298.57,
               217.86,    150.00,     94.64,     51.43,     20.00,
                 0.00
 /END
 &DATABASE
 /
 &DATA
  ie_ext=467, je_ext=487,
  yinput_type='forecast',     
  ytunit_in='d',
  ytunit_out='d',
  ylmext_form_read='ncdf',
  yinext_form_read='ncdf',
  yin_form_read='ncdf',
  ylm_form_write='ncdf',
  ylmext_lfn='pep_cosmo-de-plus-expn_0025_467x487.nc',
  ylmext_cat='$LM_EXT',
  yinext_lfn='lffd1949120100c.nc',  
  !yinext_cat='/e/uwork/sbrienen/projekte/quwind100/forcing/reklies01',
  yinext_cat='/lustre0/uwork1/sbrienen/CLMIN/FPS-CPS/Forcing_HadGEM',
  yin_cat='@{INPDIR}/@{YYYY_MM}',
  ylm_cat='@{OUTDIR}/@{YYYY_MM}/',
  nprocess_ini = 131, nprocess_bd = 132,
  yncglob_institution="DWD (Deutscher Wetterdienst, Offenbach, Germany)",
  yncglob_title="INT2LM forcing for 0.025 Deg Expertennetzwerk Runs",
  yncglob_project_id="Expertennetzwerk",
  yncglob_experiment_id="ExpN Historical run MIROC5-CCLM-CCLM",
  !yncglob_experiment_id="FPS_Convection HadGEM2-ES_CCLM-EUR11_rcp85",
  yncglob_source="int2lm_2.00_clm4, computing system: CRAY XC30, DWD Offenbach",
  yncglob_contact = "@{EMAIL_ADDRESS}",
  !yncglob_contact = "Susanne Brienen (susanne.brienen@dwd.de)",
  yncglob_references="http//www.clm-community.eu, http://www.dwd.de",
 /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
# calculate number of cores (each node has 2 processors, each processor 10 cores)
echo ----- start INT2CLM

export LIBDWD_BITMAP_TYPE=ASCII

#machine specific for Cray
export OMP_NUM_THREADS=1

aprun -n @{NP} -N 24 -j 1 -d 1 -m 1500m  @{INT2LM_EXE}

echo ----- INT2CLM finished

echo int2lm @{YYYY_MM} FINISHED $(date) >> @{PFDIR}/@{EXPID}/chain_status.log


#################################################
# submit CCLM job
#################################################
### submit a CCLM job if
### it is the first INT2LM job in the chain OR
### the in parallel runnig CCLM job was faster than the INTLM job
# evntuell noch:
# DATELOG=$(cat ${PFDIR}/${EXPID}/date.log | cut -c1-6)0100
DATELOG=$(cat @{PFDIR}/@{EXPID}/date.log)
if [[ @{CURRENT_DATE} -eq @{YDATE_START} || @{CURRENT_DATE} -eq ${DATELOG} ]]
then
  @{PFDIR}/@{EXPID}/subchain cclm
fi

