#!/bin/bash
#
# This script will:
# (1) scp IMERG files over from sportrd1,
# (2) rename them to support grads convention,
# (3) create grads control files from templates,
# (4) generate multi-panel precip comparisons 
#     for various accumulation intervals.
#
# Author: Jonathan Case (ENSCO, Inc./NASA SPoRT Center)
#
# Creation date: 1 July 2015
#
# Supports upcoming summer evaluation of IMERG data at
# select NWS/RFC offices.
#

realtimeMode=n
doscp=y

export GADDIR="/data/sport/people/casejl/WRF/UEMS371/uems/util/grads/data"
SRCDIR=/usr/people/casejl/scripts/IMERG
RUNDIR=/raid2/sport/people/casejl/DATA/IMERG

gribmapexe="/data/sport/people/casejl/WRF/UEMS371/uems/util/grads/bin/gribmap -v -i"
gradsexe="/data/sport/people/casejl/WRF/UEMS371/uems/grads/bin/grads -blc"

# Data directories
REM_IMERGEDIR=/mnt/raid1/realtime/nasa/imerg/nrtData/early/raw
REM_IMERGLDIR=/mnt/raid1/realtime/nasa/imerg/nrtData/late/raw
LOC_IMERGEDIR=/raid2/sport/people/casejl/DATA/IMERG/NRTE
LOC_IMERGLDIR=/raid2/sport/people/casejl/DATA/IMERG/NRTL
REM_HOST="sportrd1"
REM_USER="casejl"
scpCmd="scp -q -o ConnectTimeout=5 -o ConnectionAttempts=1"
STIVDIR=/raid2/sport/people/casejl/DATA/STIV
MRMSDIR=/raid2/sport/people/casejl/DATA/MRMS

if [ $realtimeMode = "y" ] ; then
  yyyymmdd=`date -u +%Y%m%d`
  yyyymmddm1=`date -u +%Y%m%d -d "-1 days"`
  yyyymmddm2=`date -u +%Y%m%d -d "-2 days"`
  hh=`date -u +%H`
else
  if [ $# -lt 2 ] ; then
    echo "Usage: make_precipplot_compare.sh YYYYMMDD HH"
    echo "Exiting script...."
    exit
  else
    yyyymmdd=$1
    yyyymmddm1=`date -u +%Y%m%d -d "${yyyymmdd}-1 days"`
    yyyymmddm2=`date -u +%Y%m%d -d "${yyyymmdd}-2 days"`
    hh=$2
  fi
fi

yyyy=`echo $yyyymmdd | cut -c1-4`
mm=`echo $yyyymmdd | cut -c5-6`
dd=`echo $yyyymmdd | cut -c7-8`
yyyym1=`echo $yyyymmddm1 | cut -c1-4`
mmm1=`echo $yyyymmddm1 | cut -c5-6`
ddm1=`echo $yyyymmddm1 | cut -c7-8`
yyyym2=`echo $yyyymmddm2 | cut -c1-4`
mmm2=`echo $yyyymmddm2 | cut -c5-6`
ddm2=`echo $yyyymmddm2 | cut -c7-8`

# scp over all IMERG files for $yyyymmddm2, $yyyymmddm1, and $yyyymmdd
if [ $doscp = "y" ] ; then
for yyyymmddwork in $yyyymmddm2 $yyyymmddm1 $yyyymmdd ; do
  yyyywork=`echo $yyyymmddwork | cut -c1-4`
  mmwork=`echo $yyyymmddwork | cut -c5-6`
  ddwork=`echo $yyyymmddwork | cut -c7-8`
  echo "Secure copying IMERG-Early files for $yyyymmddwork"
  if [ ! -d ${LOC_IMERGEDIR}/${yyyywork}${mmwork} ] ; then
    mkdir -p ${LOC_IMERGEDIR}/${yyyywork}${mmwork}
  fi
  fileList=`ssh -q ${USER}@${REM_HOST} ls ${REM_IMERGEDIR} | grep ${yyyymmddwork}`
  for remFile in $fileList ; do
    locFile=`echo $remFile | cut -c1-39`".HDF5"
    echo "Secure copying $remFile to ${LOC_IMERGEDIR}/${yyyywork}${mmwork}/${locFile}"
    $scpCmd ${USER}@${REM_HOST}:${REM_IMERGEDIR}/${remFile} ${LOC_IMERGEDIR}/${yyyywork}${mmwork}/${locFile}
  done
  echo "Secure copying IMERG-Late files for $yyyymmddwork"
  if [ ! -d ${LOC_IMERGLDIR}/${yyyywork}${mmwork} ] ; then
    mkdir -p ${LOC_IMERGLDIR}/${yyyywork}${mmwork}
  fi
  fileList=`ssh -q ${USER}@${REM_HOST} ls ${REM_IMERGLDIR} | grep ${yyyymmddwork}`
  for remFile in $fileList ; do
    locFile=`echo $remFile | cut -c1-39`".HDF5"
    echo "Secure copying $remFile to ${LOC_IMERGLDIR}/${yyyywork}${mmwork}/${locFile}"
    $scpCmd ${USER}@${REM_HOST}:${REM_IMERGLDIR}/${remFile} ${LOC_IMERGLDIR}/${yyyywork}${mmwork}/${locFile}
  done
done
fi ## doscp flag

# Now, create grads control files from generic templates, based on hour of day.
# Use $yyyymmddm2 if $hh < 13; otherwise use $yyyymmddm1 as the starting date.
# May need to modify this logic to get timely plots for shorter time intervals....
# This logic should work best for 12,24 h accumulation intervals.

yyyymmddwork=$yyyymmddm2
yyyywork=$yyyym2
mmwork=$mmm2
ddwork=$ddm2

case $mmwork in
	01) monwork="jan" ;;
	02) monwork="feb" ;;
	03) monwork="mar" ;;
	04) monwork="apr" ;;
	05) monwork="may" ;;
	06) monwork="jun" ;;
	07) monwork="jul" ;;
	08) monwork="aug" ;;
	09) monwork="sep" ;;
	10) monwork="oct" ;;
	11) monwork="nov" ;;
	12) monwork="dec" ;;
esac

# imerge.ctl file generation
sed 's;DD;'$ddwork';g' ${SRCDIR}/imerge.ctl.sed |\
  sed 's;MON;'$monwork';g' |\
  sed 's;YYYY;'$yyyywork';g' > ${RUNDIR}/imerge.ctl
# imergl.ctl file generation
sed 's;DD;'$ddwork';g' ${SRCDIR}/imergl.ctl.sed |\
  sed 's;MON;'$monwork';g' |\
  sed 's;YYYY;'$yyyywork';g' > ${RUNDIR}/imergl.ctl
# stiv.ctl file generation (must also re-run gribmap)
sed 's;DD;'$ddwork';g' ${SRCDIR}/stiv.ctl.sed |\
  sed 's;MON;'$monwork';g' |\
  sed 's;YYYY;'$yyyywork';g' > ${RUNDIR}/stiv.ctl
echo "Running gribmap on STIV GRIB1 dataset"
${gribmapexe} ${RUNDIR}/stiv.ctl >& gribmap_stiv.log

# mrms.ctl file generation (must also gunzip binary files)
sed 's;DD;'$ddwork';g' ${SRCDIR}/mrms.ctl.sed |\
  sed 's;MON;'$monwork';g' |\
  sed 's;YYYY;'$yyyywork';g' > ${RUNDIR}/mrms.ctl
echo "gunzip MRMS files for ${yyyymmddm2}"
gunzip -f ${MRMSDIR}/${yyyym2}${mmm2}/1HGC.${yyyymmddm2}*.gz
echo "gunzip MRMS files for ${yyyymmddm1}"
gunzip -f ${MRMSDIR}/${yyyym1}${mmm1}/1HGC.${yyyymmddm1}*.gz
echo "gunzip MRMS files for ${yyyymmdd}"
gunzip -f ${MRMSDIR}/${yyyy}${mm}/1HGC.${yyyymmdd}*.gz
echo

# Now, generate the accumulated precip comparison maps.
cd ${RUNDIR}
for acc in 24h 12h 6h 3h 1h ; do
 for domain in seus swus ; do
  echo "Doing ${acc} precip for $domain"
  sed 's;DOMAIN;'$domain';g' ${SRCDIR}/precplot_compare_apcp${acc}.gs.sed > ${RUNDIR}/prec${acc}.gs
  ${gradsexe} prec${acc}.gs >& ${RUNDIR}/prec${acc}_${domain}_${yyyymmdd}.log
 done
done

# scp newly-minted PNG files to SPoRT web page.
echo "scp-ing seus files to SPoRT web page"
${scpCmd} precCompare-seus-*_${yyyymmddm1}_*.png webload@weather:incoming/qpeCompare_seus/
${scpCmd} precCompare-seus-*_${yyyymmdd}_*.png webload@weather:incoming/qpeCompare_seus/
echo "scp-ing swus files to SPoRT web page"
${scpCmd} precCompare-swus-*_${yyyymmddm1}_*.png webload@weather:incoming/qpeCompare_swus/
${scpCmd} precCompare-swus-*_${yyyymmdd}_*.png webload@weather:incoming/qpeCompare_swus/

# Archive PNG files by date
if [ ! -d ${RUNDIR}/${yyyymmddm1} ] ; then
  mkdir -p ${RUNDIR}/${yyyymmddm1}
fi
if [ ! -d ${RUNDIR}/${yyyymmdd} ] ; then
  mkdir -p ${RUNDIR}/${yyyymmdd}
fi
if [ ! -d ${RUNDIR}/LOG ] ; then
  mkdir -p ${RUNDIR}/LOG
fi
mv -f precCompare-*_${yyyymmddm1}_*.png ${RUNDIR}/${yyyymmddm1}/
mv -f precCompare-*_${yyyymmdd}_*.png   ${RUNDIR}/${yyyymmdd}/
mv -f prec*.log ${RUNDIR}/LOG/

# Finally, gzip back up the MRMS binary files to save on disk space.
echo "gzip MRMS files for ${yyyymmddm2}"
gzip -f ${MRMSDIR}/${yyyym2}${mmm2}/1HGC.${yyyymmddm2}*
echo "gzip MRMS files for ${yyyymmddm1}"
gzip -f ${MRMSDIR}/${yyyym1}${mmm1}/1HGC.${yyyymmddm1}*
echo "gzip MRMS files for ${yyyymmdd}"
gzip -f ${MRMSDIR}/${yyyy}${mm}/1HGC.${yyyymmdd}*

exit
