#!/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=n
#domlist="seus swus midwest pr"
#domlist="bahamas"
domlist="dorian"
#acclist="24h 12h 6h 3h 1h"
acclist="tot"

# text label positions on GrADS space (8.5" x 11")
xposmax='5.3'
yposmax='7.6'

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

gribmapexe="/data/sport/people/casejl/WRF/UEMS371/uems/util/grads/bin/gribmap -v -i"
gradsexe="/data/sport/people/casejl/WRF/UEMS371/uems/util/grads/bin/grads -blc"
#gribmapexe="/data/sport/people/casejl/WRF/UEMS391/uems/util/grads/bin/gribmap -v -i"
#gradsexe="/data/sport/people/casejl/WRF/UEMS391/uems/util/grads/bin/grads -blc"
#gribmapexe="/data/sport/people/casejl/WRF/UEMS40/uems/util/grads/bin/gribmap -v -i"
#gradsexe="/data/sport/people/casejl/WRF/UEMS40/uems/util/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
STIVDIR=/raid2/sport/people/casejl/DATA/STIV
MRMSDIR=/raid2/sport/people/casejl/DATA/MRMS
REM_HOST="sportrd1"
REM_USER="casejl"
scpCmd="scp -q -o ConnectTimeout=5 -o ConnectionAttempts=1"

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}`
  cd ${LOC_IMERGEDIR}/${yyyywork}${mmwork}
  for remFile in $fileList ; do
    locFile=`echo $remFile | cut -c1-39`".HDF5"
# J.Case (2/7/2018) -- Link to original filename instead of renaming.
    echo "Secure copying $remFile and linking to ${LOC_IMERGEDIR}/${yyyywork}${mmwork}/${locFile}"
    $scpCmd ${USER}@${REM_HOST}:${REM_IMERGEDIR}/${remFile} ./
    ln -fs ${remFile} ${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}`
  cd ${LOC_IMERGLDIR}/${yyyywork}${mmwork}
  for remFile in $fileList ; do
    locFile=`echo $remFile | cut -c1-39`".HDF5"
    echo "Secure copying $remFile and linking to ${LOC_IMERGLDIR}/${yyyywork}${mmwork}/${locFile}"
    $scpCmd ${USER}@${REM_HOST}:${REM_IMERGLDIR}/${remFile} ./
    ln -fs ${remFile} ${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

# imergl.ctl file generation
sed 's;DD;'$ddwork';g' ${SRCDIR}/imergltot.ctl.sed |\
  sed 's;MON;'$monwork';g' |\
  sed 's;YYYY;'$yyyywork';g' > ${RUNDIR}/imergl.ctl
# imergf.ctl file generation
sed 's;DD;'$ddwork';g' ${SRCDIR}/imergftot.ctl.sed |\
  sed 's;MON;'$monwork';g' |\
  sed 's;YYYY;'$yyyywork';g' > ${RUNDIR}/imergf.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

# J.Case (10/11/2018) -- modify such that .gz files are retained, so as to
# avoid the LIS runs not finding MRMS .gz files.
#echo "gunzip MRMS files for ${yyyymmddm2}"
##gunzip -f ${MRMSDIR}/${yyyym2}${mmm2}/1HGC.${yyyymmddm2}*.gz
#cd ${MRMSDIR}/${yyyym2}${mmm2}
#filelist=`ls 1HGC.${yyyymmddm2}*.gz`
#for file in $filelist ; do
#  fileuncompress=`basename $file .gz`
#  gunzip -c $file > $fileuncompress
#done
#
#echo "gunzip MRMS files for ${yyyymmddm1}"
##gunzip -f ${MRMSDIR}/${yyyym1}${mmm1}/1HGC.${yyyymmddm1}*.gz
#cd ${MRMSDIR}/${yyyym1}${mmm1}
#filelist=`ls 1HGC.${yyyymmddm1}*.gz`
#for file in $filelist ; do
#  fileuncompress=`basename $file .gz`
#  gunzip -c $file > $fileuncompress
#done
#
#echo "gunzip MRMS files for ${yyyymmdd}"
##gunzip -f ${MRMSDIR}/${yyyy}${mm}/1HGC.${yyyymmdd}*.gz
#cd ${MRMSDIR}/${yyyy}${mm}
#filelist=`ls 1HGC.${yyyymmdd}*.gz`
#for file in $filelist ; do
#  fileuncompress=`basename $file .gz`
#  gunzip -c $file > $fileuncompress
#done

echo

# Now, generate the accumulated precip comparison maps.
cd ${RUNDIR}
for acc in ${acclist} ; do
 for domain in ${domlist} ; do
  echo "Doing ${acc} precip for $domain"
  if [ $acc =  "24h" ] ; then
# IMERG Late file count for daym1 and current day
    temp1=`ls ${LOC_IMERGEDIR}/${yyyym2}${mmm2}/3B-HHR-L.*.${yyyymmddm2}-S1[2-9]*.HDF5 | wc -l`
    temp2=`ls ${LOC_IMERGEDIR}/${yyyym2}${mmm2}/3B-HHR-L.*.${yyyymmddm2}-S2*.HDF5 | wc -l`
    temp3=`ls ${LOC_IMERGEDIR}/${yyyym1}${mmm1}/3B-HHR-L.*.${yyyymmddm1}-S0*.HDF5 | wc -l`
    temp4=`ls ${LOC_IMERGEDIR}/${yyyym1}${mmm1}/3B-HHR-L.*.${yyyymmddm1}-S1[01]*.HDF5 | wc -l`
    numla=`expr $temp1 + $temp2 + $temp3 + $temp4`
    temp1=`ls ${LOC_IMERGEDIR}/${yyyym1}${mmm1}/3B-HHR-L.*.${yyyymmddm1}-S1[2-9]*.HDF5 | wc -l`
    temp2=`ls ${LOC_IMERGEDIR}/${yyyym1}${mmm1}/3B-HHR-L.*.${yyyymmddm1}-S2*.HDF5 | wc -l`
    temp3=`ls ${LOC_IMERGEDIR}/${yyyy}${mm}/3B-HHR-L.*.${yyyymmdd}-S0*.HDF5 | wc -l`
    temp4=`ls ${LOC_IMERGEDIR}/${yyyy}${mm}/3B-HHR-L.*.${yyyymmdd}-S1[01]*.HDF5 | wc -l`
    numlb=`expr $temp1 + $temp2 + $temp3 + $temp4`
# IMERG Final file count for daym1 and current day
    temp1=`ls ${LOC_IMERGLDIR}/${yyyym2}${mmm2}/3B-HHR.*.${yyyymmddm2}-S1[2-9]*.HDF5 | wc -l`
    temp2=`ls ${LOC_IMERGLDIR}/${yyyym2}${mmm2}/3B-HHR.*.${yyyymmddm2}-S2*.HDF5 | wc -l`
    temp3=`ls ${LOC_IMERGLDIR}/${yyyym1}${mmm1}/3B-HHR.*.${yyyymmddm1}-S0*.HDF5 | wc -l`
    temp4=`ls ${LOC_IMERGLDIR}/${yyyym1}${mmm1}/3B-HHR.*.${yyyymmddm1}-S1[01]*.HDF5 | wc -l`
    numfa=`expr $temp1 + $temp2 + $temp3 + $temp4`
    temp1=`ls ${LOC_IMERGLDIR}/${yyyym1}${mmm1}/3B-HHR.*.${yyyymmddm1}-S1[2-9]*.HDF5 | wc -l`
    temp2=`ls ${LOC_IMERGLDIR}/${yyyym1}${mmm1}/3B-HHR.*.${yyyymmddm1}-S2*.HDF5 | wc -l`
    temp3=`ls ${LOC_IMERGLDIR}/${yyyy}${mm}/3B-HHR.*.${yyyymmdd}-S0*.HDF5 | wc -l`
    temp4=`ls ${LOC_IMERGLDIR}/${yyyy}${mm}/3B-HHR.*.${yyyymmdd}-S1[01]*.HDF5 | wc -l`
    numfb=`expr $temp1 + $temp2 + $temp3 + $temp4`
## STIV file count for daym1 and current day
#    temp1=`ls ${STIVDIR}/${yyyym2}${mmm2}/ST4.${yyyymmddm2}1[3-9]*.01h | wc -l`
#    temp2=`ls ${STIVDIR}/${yyyym2}${mmm2}/ST4.${yyyymmddm2}2*.01h | wc -l`
#    temp3=`ls ${STIVDIR}/${yyyym1}${mmm1}/ST4.${yyyymmddm1}0*.01h | wc -l`
#    temp4=`ls ${STIVDIR}/${yyyym1}${mmm1}/ST4.${yyyymmddm1}1[012]*.01h | wc -l`
#    stiva=`expr $temp1 + $temp2 + $temp3 + $temp4`
#    temp1=`ls ${STIVDIR}/${yyyym1}${mmm1}/ST4.${yyyymmddm1}1[3-9]*.01h | wc -l`
#    temp2=`ls ${STIVDIR}/${yyyym1}${mmm1}/ST4.${yyyymmddm1}2*.01h | wc -l`
#    temp3=`ls ${STIVDIR}/${yyyy}${mm}/ST4.${yyyymmdd}0*.01h | wc -l`
#    temp4=`ls ${STIVDIR}/${yyyy}${mm}/ST4.${yyyymmdd}1[012]*.01h | wc -l`
#    stivb=`expr $temp1 + $temp2 + $temp3 + $temp4`
## MRMS file count for daym1 and current day
#    temp1=`ls ${MRMSDIR}/${yyyym2}${mmm2}/1HGC.${yyyymmddm2}.1[3-9]*0 | wc -l`
#    temp2=`ls ${MRMSDIR}/${yyyym2}${mmm2}/1HGC.${yyyymmddm2}.2*0 | wc -l`
#    temp3=`ls ${MRMSDIR}/${yyyym1}${mmm1}/1HGC.${yyyymmddm1}.0*0 | wc -l`
#    temp4=`ls ${MRMSDIR}/${yyyym1}${mmm1}/1HGC.${yyyymmddm1}.1[012]*0 | wc -l`
#    mrmsa=`expr $temp1 + $temp2 + $temp3 + $temp4`
#    temp1=`ls ${MRMSDIR}/${yyyym1}${mmm1}/1HGC.${yyyymmddm1}.1[3-9]*0 | wc -l`
#    temp2=`ls ${MRMSDIR}/${yyyym1}${mmm1}/1HGC.${yyyymmddm1}.2*0 | wc -l`
#    temp3=`ls ${MRMSDIR}/${yyyy}${mm}/1HGC.${yyyymmdd}.0*0 | wc -l`
#    temp4=`ls ${MRMSDIR}/${yyyy}${mm}/1HGC.${yyyymmdd}.1[012]*0 | wc -l`
#    mrmsb=`expr $temp1 + $temp2 + $temp3 + $temp4`

    sed 's;DOMAIN;'$domain';g' ${SRCDIR}/precplot-final_compare+dorian_apcp${acc}.gs.sed |\
      sed 's;NUMFA;'$numfa';g' | sed 's;NUMFB;'$numfb';g' |\
      sed 's;NUMLA;'$numla';g' | sed 's;NUMLB;'$numlb';g' |\
      sed 's;XPOSMAX;'$xposmax';g' | sed 's;YPOSMAX;'$yposmax';g' |\
      sed 's;STIVA;'$stiva';g' | sed 's;STIVB;'$stivb';g' |\
      sed 's;MRMSA;'$mrmsa';g' | sed 's;MRMSB;'$mrmsb';g' > ${RUNDIR}/prec${acc}+label.gs
#    ${gradsexe} prec${acc}+label.gs >& ${RUNDIR}/prec${acc}+label_${domain}_${yyyymmdd}.log
    ${gradsexe} prec${acc}+label.gs
  else
    sed 's;DOMAIN;'$domain';g' ${SRCDIR}/precplot-final_compare+dorian_apcp${acc}.gs.sed |\
      sed 's;XPOSMAX;'$xposmax';g' | sed 's;YPOSMAX;'$yposmax';g' > ${RUNDIR}/prec${acc}.gs
#    ${gradsexe} prec${acc}.gs >& ${RUNDIR}/prec${acc}_${domain}_${yyyymmdd}.log
    ${gradsexe} prec${acc}.gs
  fi
 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/
#echo "scp-ing pr files to SPoRT web page"
#${scpCmd} precCompare-pr-*_${yyyymmddm1}_*.png webload@weather:incoming/qpeCompare_pr/
#${scpCmd} precCompare-pr-*_${yyyymmdd}_*.png webload@weather:incoming/qpeCompare_pr/
#echo "scp-ing midwest files to SPoRT web page"
#${scpCmd} precCompare-midwest-*_${yyyymmddm1}_*.png webload@weather:incoming/qpeCompare_midwest/
#${scpCmd} precCompare-midwest-*_${yyyymmdd}_*.png webload@weather:incoming/qpeCompare_midwest/

# Archive PNG files by date
if [ ! -d ${OUTDIR}/${yyyymmddm1} ] ; then
  mkdir -p ${OUTDIR}/${yyyymmddm1}
fi
if [ ! -d ${OUTDIR}/${yyyymmdd} ] ; then
  mkdir -p ${OUTDIR}/${yyyymmdd}
fi
if [ ! -d ${OUTDIR}/LOG ] ; then
  mkdir -p ${OUTDIR}/LOG
fi
mv -f precCompare-*_${yyyymmddm1}_*.png ${OUTDIR}/${yyyymmddm1}/
mv -f precCompare-*_${yyyymmdd}_*.png   ${OUTDIR}/${yyyymmdd}/
mv -f prec*.log ${OUTDIR}/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}*
## J.Case (10/11/2018) -- since we are retaining the .gz files above, 
## now simply delete the uncompressed binary files when done.
#rm -f ${MRMSDIR}/${yyyym2}${mmm2}/1HGC.${yyyymmddm2}.*00
#rm -f ${MRMSDIR}/${yyyym1}${mmm1}/1HGC.${yyyymmddm1}.*00
#rm -f ${MRMSDIR}/${yyyy}${mm}/1HGC.${yyyymmdd}.*00

exit

