#!/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
dogetimerg=y
dounzipmrms=y
domakeimages=y
docp=y

domlist="seus swus midwest pr"
#domlist="pr"
#acclist="24h 12h 6h 3h 1h"
acclist="24h 12h 6h 3h 1h"

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"
#wgrib2exe="/data/sport/apps/mrms/wgrib2_mrms/wgrib2/wgrib2mrms"
wgrib2exe="/data/sport/people/casejl/WRF/UEMS371/uems/util/bin/wgrib2"

# Data directories
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

# esops directory for web imagery
distDir=/raid4/esops/distribution/qpe/web_imagery

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

yyyymmddm1=`date -u +%Y%m%d -d "${yyyymmdd}-1 days"`
yyyymmddm2=`date -u +%Y%m%d -d "${yyyymmdd}-2 days"`
yyyymmddm3=`date -u +%Y%m%d -d "${yyyymmdd}-3 days"`

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`
yyyym3=`echo $yyyymmddm3 | cut -c1-4`
mmm3=`echo $yyyymmddm3 | cut -c5-6`
ddm3=`echo $yyyymmddm3 | cut -c7-8`

# Download all IMERG files for $yyyymmddm2, $yyyymmddm1, and $yyyymmdd
if [ $dogetimerg = "y" ] ; then
  for yyyymmddwork in $yyyymmddm2 $yyyymmddm1 $yyyymmdd ; do
    echo "Downloading IMERG-Early for $yyyymmddwork"
    ${SRCDIR}/wget_imerg_early_v6.sh $yyyymmddwork
    echo "Downloading IMERG-Late for $yyyymmddwork"
    ${SRCDIR}/wget_imerg_late_v6.sh $yyyymmddwork
  done
fi ## dogetimerg 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 CONUS file generation (must also re-run gribmap)
# J.Case (11/10/2020) -- modified for new GRIB2 Stage IV files,
# implemented on 28 July 2020. (modified stiv.ctl.sed)
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"
echo "Running gribmap on STIV GRIB2 dataset"
${gribmapexe} ${RUNDIR}/stiv.ctl >& gribmap_stiv.log

# stiv_pr.ctl Puerto Rico file generation (must also re-run gribmap)
# J.Case (11/10/2020) -- modified for new GRIB2 Stage IV PR files,
# implemented on 28 July 2020. (modified stiv.ctl.sed)
sed 's;DD;'$ddwork';g' ${SRCDIR}/stiv_pr.ctl.sed |\
  sed 's;MON;'$monwork';g' |\
  sed 's;YYYY;'$yyyywork';g' > ${RUNDIR}/stiv_pr.ctl
echo "Running gribmap on STIV Puerto Rico GRIB2 dataset"
${gribmapexe} ${RUNDIR}/stiv_pr.ctl >& gribmap_stiv_pr.log

# mrms.ctl CONUS 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

# mrms_pr_bin.ctl for PR files.
sed 's;DD;'$ddwork';g' ${SRCDIR}/mrms_pr_bin.ctl.sed |\
  sed 's;MON;'$monwork';g' |\
  sed 's;YYYY;'$yyyywork';g' > ${RUNDIR}/mrms_pr_bin.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.

if [ $dounzipmrms = 'y' ] ; then
  for yyyymmddwork in $yyyymmddm2 $yyyymmddm1 $yyyymmdd ; do
    yyyymmwork=`echo $yyyymmddwork | cut -c1-6`
    echo "gunzip CONUS MRMS files for ${yyyymmddwork}"
    cd ${MRMSDIR}/${yyyymmwork}
    filelist=`ls 1HGC.${yyyymmddwork}*.gz`
    for file in $filelist ; do
      fileuncompress=`basename $file .gz`
      gunzip -c $file > $fileuncompress
    done

    echo "gunzip PR MRMS files for ${yyyymmddwork}"
    filelist=`ls 1HGC_pr.${yyyymmddwork}*.gz`
    for file in $filelist ; do
      fileuncompress=`basename $file .gz`
      gunzip -c $file > $fileuncompress
    done
  done
fi ## dogunzipmrms block

# copy in environ and shapefiles into $RUNDIR
cp ${SRCDIR}/environ_shaded.ch ${RUNDIR}/
cp ${SRCDIR}/cbar.gs ${RUNDIR}/
cp /raid2/sport/people/casejl/DATA/SHAPEFILES/c_15au13* ${RUNDIR}/
cp /raid2/sport/people/casejl/DATA/SHAPEFILES/s_06se12* ${RUNDIR}/

# Now, generate the accumulated precip comparison maps.
cd ${RUNDIR}

if [ $domakeimages = 'y' ] ; then

for acc in ${acclist} ; do
 for domain in ${domlist} ; do

  echo "Doing ${acc} precip for $domain"

  if [ $acc =  "24h" ] ; then

    # IMERG Early file count for daym1 and current day
    temp1=`ls ${LOC_IMERGEDIR}/${yyyym2}${mmm2}/3B-HHR-E.*.${yyyymmddm2}-S1[2-9]*0.HDF5 | wc -l`
    temp2=`ls ${LOC_IMERGEDIR}/${yyyym2}${mmm2}/3B-HHR-E.*.${yyyymmddm2}-S2*0.HDF5 | wc -l`
    temp3=`ls ${LOC_IMERGEDIR}/${yyyym1}${mmm1}/3B-HHR-E.*.${yyyymmddm1}-S0*0.HDF5 | wc -l`
    temp4=`ls ${LOC_IMERGEDIR}/${yyyym1}${mmm1}/3B-HHR-E.*.${yyyymmddm1}-S1[01]*0.HDF5 | wc -l`
    numea=`expr $temp1 + $temp2 + $temp3 + $temp4`
    temp1=`ls ${LOC_IMERGEDIR}/${yyyym1}${mmm1}/3B-HHR-E.*.${yyyymmddm1}-S1[2-9]*0.HDF5 | wc -l`
    temp2=`ls ${LOC_IMERGEDIR}/${yyyym1}${mmm1}/3B-HHR-E.*.${yyyymmddm1}-S2*0.HDF5 | wc -l`
    temp3=`ls ${LOC_IMERGEDIR}/${yyyy}${mm}/3B-HHR-E.*.${yyyymmdd}-S0*0.HDF5 | wc -l`
    temp4=`ls ${LOC_IMERGEDIR}/${yyyy}${mm}/3B-HHR-E.*.${yyyymmdd}-S1[01]*0.HDF5 | wc -l`
    numeb=`expr $temp1 + $temp2 + $temp3 + $temp4`

    # IMERG Late file count for daym1 and current day
    temp1=`ls ${LOC_IMERGLDIR}/${yyyym2}${mmm2}/3B-HHR-L.*.${yyyymmddm2}-S1[2-9]*0.HDF5 | wc -l`
    temp2=`ls ${LOC_IMERGLDIR}/${yyyym2}${mmm2}/3B-HHR-L.*.${yyyymmddm2}-S2*0.HDF5 | wc -l`
    temp3=`ls ${LOC_IMERGLDIR}/${yyyym1}${mmm1}/3B-HHR-L.*.${yyyymmddm1}-S0*0.HDF5 | wc -l`
    temp4=`ls ${LOC_IMERGLDIR}/${yyyym1}${mmm1}/3B-HHR-L.*.${yyyymmddm1}-S1[01]*0.HDF5 | wc -l`
    numla=`expr $temp1 + $temp2 + $temp3 + $temp4`
    temp1=`ls ${LOC_IMERGLDIR}/${yyyym1}${mmm1}/3B-HHR-L.*.${yyyymmddm1}-S1[2-9]*0.HDF5 | wc -l`
    temp2=`ls ${LOC_IMERGLDIR}/${yyyym1}${mmm1}/3B-HHR-L.*.${yyyymmddm1}-S2*0.HDF5 | wc -l`
    temp3=`ls ${LOC_IMERGLDIR}/${yyyy}${mm}/3B-HHR-L.*.${yyyymmdd}-S0*0.HDF5 | wc -l`
    temp4=`ls ${LOC_IMERGLDIR}/${yyyy}${mm}/3B-HHR-L.*.${yyyymmdd}-S1[01]*0.HDF5 | wc -l`
    numlb=`expr $temp1 + $temp2 + $temp3 + $temp4`

    # STIV file count for daym1 and current day
    # J.Case (10/6/2023) -- adjust for availability of ST4 analyses over Puerto Rico
    if [ $domain = 'pr' ] ; then
      header='st4_pr'
    else
      header='st4_conus'
    fi
    temp1=`ls ${STIVDIR}/${yyyym2}${mmm2}/${header}.${yyyymmddm2}1[3-9]*.01h.grb2 | wc -l`
    temp2=`ls ${STIVDIR}/${yyyym2}${mmm2}/${header}.${yyyymmddm2}2*.01h.grb2 | wc -l`
    temp3=`ls ${STIVDIR}/${yyyym1}${mmm1}/${header}.${yyyymmddm1}0*.01h.grb2 | wc -l`
    temp4=`ls ${STIVDIR}/${yyyym1}${mmm1}/${header}.${yyyymmddm1}1[012]*.01h.grb2 | wc -l`
    stiva=`expr $temp1 + $temp2 + $temp3 + $temp4`
    temp1=`ls ${STIVDIR}/${yyyym1}${mmm1}/${header}.${yyyymmddm1}1[3-9]*.01h.grb2 | wc -l`
    temp2=`ls ${STIVDIR}/${yyyym1}${mmm1}/${header}.${yyyymmddm1}2*.01h.grb2 | wc -l`
    temp3=`ls ${STIVDIR}/${yyyy}${mm}/${header}.${yyyymmdd}0*.01h.grb2 | wc -l`
    temp4=`ls ${STIVDIR}/${yyyy}${mm}/${header}.${yyyymmdd}1[012]*.01h.grb2 | wc -l`
    stivb=`expr $temp1 + $temp2 + $temp3 + $temp4`

    # MRMS CONUS file count for daym1 and current day
    if [ $domain = 'pr' ] ; then
      header='1HGC_pr'
    else
      header='1HGC'
    fi
    temp1=`ls ${MRMSDIR}/${yyyym2}${mmm2}/${header}.${yyyymmddm2}.1[3-9]*0 | wc -l`
    temp2=`ls ${MRMSDIR}/${yyyym2}${mmm2}/${header}.${yyyymmddm2}.2*0 | wc -l`
    temp3=`ls ${MRMSDIR}/${yyyym1}${mmm1}/${header}.${yyyymmddm1}.0*0 | wc -l`
    temp4=`ls ${MRMSDIR}/${yyyym1}${mmm1}/${header}.${yyyymmddm1}.1[012]*0 | wc -l`
    mrmsa=`expr $temp1 + $temp2 + $temp3 + $temp4`
    temp1=`ls ${MRMSDIR}/${yyyym1}${mmm1}/${header}.${yyyymmddm1}.1[3-9]*0 | wc -l`
    temp2=`ls ${MRMSDIR}/${yyyym1}${mmm1}/${header}.${yyyymmddm1}.2*0 | wc -l`
    temp3=`ls ${MRMSDIR}/${yyyy}${mm}/${header}.${yyyymmdd}.0*0 | wc -l`
    temp4=`ls ${MRMSDIR}/${yyyy}${mm}/${header}.${yyyymmdd}.1[012]*0 | wc -l`
    mrmsb=`expr $temp1 + $temp2 + $temp3 + $temp4`

    sed 's;DOMAIN;'$domain';g' ${SRCDIR}/precplot_compare+label_apcp${acc}.gs.sed |\
      sed 's;NUMEA;'$numea';g' | sed 's;NUMEB;'$numeb';g' |\
      sed 's;NUMLA;'$numla';g' | sed 's;NUMLB;'$numlb';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

  else

    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

  fi
 done  ## domain loop
done  ## acc loop

fi ## domakeimages


if [ $docp = 'y' ] ; then
  # Copy newly-minted PNG files to SPoRT web page.
  for domain in ${domlist} ; do
    echo "Copying $domain files to ${distDir}/qpeCompare_${domain}/"
    cp precCompare-${domain}-*_${yyyymmddm1}_*.png ${distDir}/qpeCompare_${domain}/
    cp precCompare-${domain}-*_${yyyymmdd}_*.png ${distDir}/qpeCompare_${domain}/
  done
fi

# 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/

# 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
rm -f ${MRMSDIR}/${yyyym2}${mmm2}/1HGC_pr.${yyyymmddm2}.*00
rm -f ${MRMSDIR}/${yyyym1}${mmm1}/1HGC_pr.${yyyymmddm1}.*00
rm -f ${MRMSDIR}/${yyyy}${mm}/1HGC_pr.${yyyymmdd}.*00

exit
