#!/bin/ksh -l
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:       exhrrr_analysis_sh.sms
# Script description:  runs the GSI and boundary update for the HRRR cycle
#
# Author:   Ming Hu / Geoff Manikin / Julia Zhu  Org: EMC       Date: 2011-10-01
#
# Script history log:
# 2014-08-01  C Alexander / M Hu / G Manikin / J Yang - HRRRv1
# 2016-02-05  C Alexander / G Manikin -  HRRRv2 
# 2018-01-24  B Blake / G Manikin / C Alexander - HRRRv3

ulimit -s 512000
set -x

# Set paths to unix commands
DATE=/bin/date
AWK="/bin/awk --posix"
ECHO="${ECHO:-/bin/echo}"

# Create the working directory and cd into it
cd ${DATA}

# Set up some environment variables
#export ATP_ENABLED=0
#export MALLOC_MMAP_MAX=0
#export MALLOC_TRIM_THRESHOLD_=134217728
export OMP_STACKSIZE=500M
#export KMP_STACKSIZE=1024m
export OMP_NUM_THREADS=4
#export FORT_BUFFERED=false

# Set to yes for faster collective ops (mpi reduce)
# but may not yield bitwise identical results from
# run to run when yes.
#export MP_COLLECTIVE_OFFLOAD=yes

# Make sure START_TIME is in the correct format
START_TIME=${PDY}' '${cyc}
echo $START_TIME
echo $cyc
START_TIME=`echo "${START_TIME}" | sed 's/\([[:digit:]]\{2\}\)$/ \1/'`
START_TIME=`${DATE} -d "${START_TIME}"`


# Update HRRR fractional sea ice with FVCOM data for every run
# We are currently dealing with an 8h 30 min latency in the files...
# Make sure START_TIME is in the correct format
START_TIME=${PDY}' '${cyc}
START_TIME=`echo "${START_TIME}" | sed 's/\([[:digit:]]\{2\}\)$/ \1/'`
START_TIME=`${DATE} -d "${START_TIME}"`

cp ${COMIN}/hrrr.t${cyc}z.wrf_inout_orig ./wrf_inout

if [ ${cyc} -eq 00 -o ${cyc} -eq 12 ]; then
  hour_diff=4
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 12 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 24 hours ago"`
elif [ ${cyc} -eq 01 -o ${cyc} -eq 13 ]; then
  hour_diff=4
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 13 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 25 hours ago"`
elif [ ${cyc} -eq 02 -o ${cyc} -eq 14 ]; then
  hour_diff=5
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 14 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 26 hours ago"`
elif [ ${cyc} -eq 03 -o ${cyc} -eq 15 ]; then
  hour_diff=5
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 15 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 27 hours ago"`
elif [ ${cyc} -eq 04 -o ${cyc} -eq 16 ]; then
  hour_diff=5
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 16 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 28 hours ago"`
elif [ ${cyc} -eq 05 -o ${cyc} -eq 17 ]; then
  hour_diff=6
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 17 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 29 hours ago"`
elif [ ${cyc} -eq 06 -o ${cyc} -eq 18 ]; then
  hour_diff=6
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 18 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 30 hours ago"`
elif [ ${cyc} -eq 07 -o ${cyc} -eq 19 ]; then
  hour_diff=6
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 19 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 31 hours ago"`
elif [ ${cyc} -eq 08 -o ${cyc} -eq 20 ]; then
  hour_diff=7
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 20 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 32 hours ago"`
elif [ ${cyc} -eq 09 -o ${cyc} -eq 21 ]; then
  hour_diff=3
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 9 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 21 hours ago"`
elif [ ${cyc} -eq 10 -o ${cyc} -eq 22 ]; then
  hour_diff=3
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 10 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 22 hours ago"`
else
  hour_diff=4
  YYYYJJJHH_fvcom=`${DATE} +"%Y%j%H" -d "${START_TIME} 11 hours ago"`
  YYYYJJJHH_fvcom_m1=`${DATE} +"%Y%j%H" -d "${START_TIME} 23 hours ago"`
fi

cat << EOF > fvcom.namelist
   &SETUP
     update_type = 2,
     t2 = ${hour_diff},
   /
EOF

if [ -r "${FVCOM}/tsfc_hrrrgrid_${YYYYJJJHH_fvcom}.nc" ]; then
  fvcomfile=${FVCOM}/tsfc_hrrrgrid_${YYYYJJJHH_fvcom}.nc
elif [ -r "${FVCOM}/tsfc_hrrrgrid_${YYYYJJJHH_fvcom_m1}.nc" ]; then
  fvcomfile=${FVCOM}/tsfc_hrrrgrid_${YYYYJJJHH_fvcom_m1}.nc
elif [ -r "${FVCOMm1}/tsfc_hrrrgrid_${YYYYJJJHH_fvcom}.nc" ]; then
  fvcomfile=${FVCOMm1}/tsfc_hrrrgrid_${YYYYJJJHH_fvcom}.nc
elif [ -r "${FVCOMm1}/tsfc_hrrrgrid_${YYYYJJJHH_fvcom_m1}.nc" ]; then
  fvcomfile=${FVCOMm1}/tsfc_hrrrgrid_${YYYYJJJHH_fvcom_m1}.nc
else
  echo "No FVCOM file in ${FVCOM}!!"
  echo "ERROR: No FVCOM update at ${PDY}${cyc}!!!!"
fi

if [ -r "${fvcomfile}" ]; then
  echo "FVCOM update for fractional lake ice."
  cp ${fvcomfile} ./fvcom.nc
  cp ${FIXhrrr}/hrrr_geo_em.d01.nc ./geo_em.d01.nc
  cp ${EXEChrrr}/hrrr_process_fvcom .
  runline="mpiexec -n 1 -ppn 1 ./hrrr_process_fvcom"
  $runline < fvcom.namelist
  export err=$?; err_chk

  mv fvcom.namelist fvcom.namelist.ice

cat << EOF > fvcom.namelist
   &SETUP
     update_type = 1,
     t2 = ${hour_diff},
   /
EOF

  echo "FVCOM update for lake surface temperatures."
  cp ${fvcomfile} ./fvcom.nc
  runline="mpiexec -n 1 -ppn 1 ./hrrr_process_fvcom"
  $runline < fvcom.namelist
  export err=$?; err_chk

fi


cp wrf_inout ${COMOUT}/hrrr.t${cyc}z.wrf_inout

msg="JOB $job HAS COMPLETED NORMALLY."
postmsg "$jlogfile" "$msg"

exit 0
