#! /bin/ksh

################################################################
# hwrf_post_deliver.sh
####################
# This script takes output of hwrf_post_process.sh and copies it to
# the relevant locations in the HWRF runtime directories.  This is part
# of a rewrite of Vijay Tallapragada's original hwrf_post.sh script.
# This script also requires hwrf_post_master.sh and hwrf_post_process.sh.
#
# Written March 2011 by Sam Trahan
################################################################

set -x

function die {
    sh ${utilscript}/setup.sh
    err=911
    echo "$*"
    err_exit "$*"
    exit 2
}

if [[ "$#" -ne 8 ]] ; then
    cat<<EOF
Format: $0 mode STORMNAME STORMID /path/to/wrfout/dir/ YYYYMMDDHH fhour COMOUT [maxdom]

where mode is "post" or "satpost", STORMNAME is the storm name,
STORMID is the stormid, /path/to/wrfout/dir/ is the path to the
directory that contains wrfout files, YYYYMMDDHH is the analysis time,
and fhour is the forecast hour.  COMOUT is the path to the directory
to receive output files, and maxdom is the number of atmospheric
domains.  All arguments are mandatory except for maxdom, which defaults
to 2.

This script expects the following environment variables will be set:

\$PARAFLAG -- YES, unless you are NCO, running operationally
\$FIXhwrf -- location of HWRF fix/ directory
\$EXEChwrf -- location of HWRF exec/ directory
\$utilexec -- location of utility programs wgrib, ndate and copygb
\$NWPROD -- used if \$utilexec is unset.  Location of nwprod or nwport
\$IO_FORM -- type of wrfout file: 1=binary, 2=netcdf.  Default: 1 (binary)

Special NCO operational environment variables:

\$SENDDBN -- YES = run \$DBNROOT/bin/dbn_alert
\$DBNROOT -- path to the dbn root
\$job -- third argument to \$DBNROOT/bin/dbn_alert
EOF
    die SCRIPT IS ABORTING DUE TO INCORRECT ARGUMENTS
fi

cnvit=${MAKE_GRIB2:-YES}
DELIVER_EPRS=NO
DELIVER_ESAT=NO
DELIVER_D23=GRIB2
#DELIVER_D23=YES

mode="$1"
STORM="$2"
STORMID="$3"
wrfdir="$4"
ymdh="$5"
hour="$6"
COMOUT="$7"
maxdom="${8:-2}"

d1=p  ;   d23=m  ;  d123=c

if [[ "$maxdom" == 3 ]] ; then
    d2=i
    d3=n
else
    d2=n
    d3=x
fi

D1=`echo $d1 | tr 'a-z' 'A-Z'`
D2=`echo $d2 | tr 'a-z' 'A-Z'`
D3=`echo $d3 | tr 'a-z' 'A-Z'`
D23=`echo $d23 | tr 'a-z' 'A-Z'`
D123=`echo $d123 | tr 'a-z' 'A-Z'`

hour00=$( printf %02d "$hour" )

########################################################################
# Determine file types and executable locations:

if [[ "$PARAFLAG" != YES ]] ; then
    FIXhwrf="${FIXhwrf:-${NWPROD}/hwrf.${HWRF_VERSION}/fix/}"
    EXEChwrf="${EXEChwrf:-${NWPROD}/hwrf.${HWRF_VERSION}/exec/}"
fi
utilexec=${utilexec:-${NWPROD}/util/exec}
ndate=${utilexec}/ndate
grb2ctl=grib2ctl
wgrib=${utilexec}/wgrib
cgb=${utilexec}/copygb
grbmap=gribmap
namelist=''

if [[ "$PARAFLAG" == YES ]] ; then
    if [[ "$IO_FORM" == 2 ]] ; then
        wrf_out_type=netcdf
        post_exec=$EXEChwrf/hwrf_post
    else
        wrf_out_type=binarympiio
        post_exec=$EXEChwrf/hwrf_post
    fi
else
    wrf_out_type=binarympiio
    post_exec=$EXEChwrf/hwrf_post
fi

storm=`echo ${STORM} | tr '[A-Z]' '[a-z]'`
stormid=`echo ${STORMID} | tr '[A-Z]' '[a-z]'`
DATE=$ymdh
nid=${storm}${stormid}
nidymdh=${nid}.${DATE}

########################################################################
# Handle satellite products.  These get a different name for product
# delivery purposes.
if [[ "$mode" == satpost ]] ; then
    if [[ "$IDIAG" == 1 ]] ; then
        /bin/mv -f HWRF_PARENT.Grb10thF$hour00 \
            ${COMOUT}/${nidymdh}.hwrfsat_$d1.grb10thf$hour00
    fi
    if [[ "$maxdom" -eq 2 ]] ; then
        if [[ "$IDIAG" == 1 ]] ; then
            /bin/mv -f HWRF_NEST.Grb10thF$hour00 \
                ${COMOUT}/${nidymdh}.hwrfsat_$d2.grb10thf${hour00}
            /bin/mv -f $STORM$STORMID.$ymdh.combine_grb10thf$hour00 \
                ${COMOUT}/${nidymdh}.hwrfsat_$d123.grb10thf$hour00
        fi
        if [[ "$DELIVER_ESAT" == YES ]] ; then
            cp HWRFPRS_NEST.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfesat_$d2.grbf${hour00}
        fi
        cp HWRF_NEST.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d2.grbf${hour00}
        cp $STORM$STORMID.$ymdh.combine_grbf$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d123.grbf${hour00}
    fi
    if [[ "$maxdom" -eq 3 ]] ; then
        if [[ "$IDIAG" == 1 ]] ; then
            /bin/mv -f HWRF_DOM3.Grb10thF$hour00 \
                ${COMOUT}/${nidymdh}.hwrfsat_$d3.grb10thf${hour00}
            /bin/mv -f $STORM$STORMID.$ymdh.moving_grb10thf$hour00 \
                ${COMOUT}/${nidymdh}.hwrfsat_$d23.grb10thf$hour00
        fi
        if [[ "$DELIVER_ESAT" == YES ]] ; then
            cp HWRFPRS_DOM3.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfesat_$d3.grbf${hour00}
        fi
        cp HWRF_DOM3.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d3.grbf${hour00}
        cp HWRF_MOVE.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d23.grbf${hour00}
        cp HWRF_NEST.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d2.grbf${hour00}
        cp $STORM$STORMID.$ymdh.combine_grbf$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d123.grbf${hour00}
    fi

    cp HWRF_PARENT.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d1.grbf${hour00}

    # Always convert to grib2, even in retrospectives, to test complicated
    # grib2 conversion for satellite grib files.
    if [[ "$cnvit" != NO ]] ; then # unless we're on Jet or Zeus, where we have no cnvgrib
    if [ $IDIAG = 1 ]; then
        $utilexec/cnvgrib -g12 -p32 -nv HWRF_PARENT.Grb10thF$hour00 HWRF_PARENT.Grb210thF$hour00
        /bin/mv -f HWRF_PARENT.Grb210thF$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d1.grb210thf${hour00}
    else
        $utilexec/cnvgrib -g12 -p32 -nv HWRF_PARENT.GrbF$hour00 HWRF_PARENT.Grb2F$hour00
        /bin/mv -f HWRF_PARENT.Grb2F$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d1.grb2f${hour00}
    fi
    if [[ "$maxdom" -gt 1 ]] ; then
        $utilexec/cnvgrib -g12 -p32 -nv HWRF_NEST.GrbF$hour00 HWRF_NEST.Grb2F$hour00
        /bin/mv -f HWRF_NEST.Grb2F$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d2.grb2f${hour00}
        $utilexec/cnvgrib -g12 -p32 -nv $STORM$STORMID.$ymdh.combine_grbf$hour00 $STORM$STORMID.$ymdh.combine_grb2f$hour00
        /bin/mv -f $STORM$STORMID.$ymdh.combine_grb2f$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d123.grb2f${hour00}
    fi
    if [[ "$maxdom" -gt 2 ]] ; then
        $utilexec/cnvgrib -g12 -p32 -nv HWRF_DOM3.GrbF$hour00 HWRF_DOM3.Grb2F$hour00
        /bin/mv -f HWRF_DOM3.Grb2F$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d3.grb2f${hour00}
        $utilexec/cnvgrib -g12 -p32 -nv HWRF_MOVE.GrbF$hour00 HWRF_MOVE.Grb2F$hour00
        /bin/mv -f HWRF_MOVE.Grb2F$hour00 ${COMOUT}/${nidymdh}.hwrfsat_$d23.grb2f${hour00}
    fi
    if [[ "$SENDDBN" == YES && "$PARAFLAG" != YES ]] ; then
        $DBNROOT/bin/dbn_alert MODEL HWRF_SAT_GRIB2P $job ${COMOUT}/${nidymdh}.hwrfsat_$d1.grb2f${hour00}
        if [[ "$maxdom" -gt 1 ]] ; then
            $DBNROOT/bin/dbn_alert MODEL HWRF_SAT_GRIB2I $job ${COMOUT}/${nidymdh}.hwrfsat_$d2.grb2f${hour00}
            if [[ "$maxdom" -gt 2 ]] ; then
                $DBNROOT/bin/dbn_alert MODEL HWRF_SAT_GRIB2C $job ${COMOUT}/${nidymdh}.hwrfsat_$d123.grb2f${hour00}
                $DBNROOT/bin/dbn_alert MODEL HWRF_SAT_GRIB2M $job ${COMOUT}/${nidymdh}.hwrfsat_$d23.grb2f${hour00}
                $DBNROOT/bin/dbn_alert MODEL HWRF_SAT_GRIB2N  $job ${COMOUT}/${nidymdh}.hwrfsat_$d3.grb2f${hour00}
            fi
        fi
    fi
    fi
    
    # Exit here so we don't deliver this as a non-satellite product.
    exit 0
fi

########################################################################
# Handle everything other than satellite products.

# Copy Grib Files on E-grid as well as lat/lon grid for Parent
# and Nest, and selected variables on Combined domain  to
# COMOUT soon after these files are created. (for use by TPC)

if [[ $IDIAG == 1 ]] ; then
    if [[ "$DIAG0" == 1 || "$IDIAG" == 1 ]] ; then
        /bin/mv -f HWRF_PARENT.Grb10thF$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d1.grb10thf${hour00}
    fi
else
    if [[ $hour00 == 00 && ( "$DIAG0" == 1 || "$IDIAG" == 1 ) ]]; then
        /bin/mv -f HWRF_PARENT.Grb10thF00 ${COMOUT}/${nidymdh}.hwrfprs_$d1.grb10thf00
        /bin/mv -f HWRF_NEST.Grb10thF$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d2.grb10thf${hour00}
        /bin/mv -f $STORM$STORMID.$ymdh.combine_grb10thf00 ${COMOUT}/${nidymdh}.hwrfprs_$d123.grb10thf00
        if [[ "$HRD_DELIVERY" == YES && "$CASE_ROOT" == FORECAST ]] ; then
            #Now copy the combined grib file to RZDM for access by HRD in real-time mode only
            ssh -l wd20vxt rzdm.ncep.noaa.gov rm -f \
                /home/others/people/emc/www/htdocs/HWRF/messages/*.grb10thf00
            scp -p ${COMOUT}/${nidymdh}.hwrfprs_$d123.grb10thf00 \
                wd20vxt@rzdm.ncep.noaa.gov:/home/others/people/emc/www/htdocs/HWRF/messages/${nidymdh}.hwrfprs_$d123.grb10thf00
        fi
    fi
    cp HWRF_PARENT.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d1.grbf${hour00}
fi

if [[ "$hour00" == 00 || "$hour00" == 06 ]] ; then
    for wrffile in $( cat wrfout.lst  ) ; do
        cp -pf $wrffile $COMOUT/$stormid.$( basename $wrffile )
    done
fi

if [[ "$DELIVER_EPRS" == YES ]] ; then
    cp HWRFPRS_NEST.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfeprs_$d2.grbf${hour00}
    cp HWRFPRS_DOM3.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfeprs_$d3.grbf${hour00}
fi
cp HWRF_NEST.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d2.grbf${hour00}
if [[ "$DELIVER_D23" == YES ]] ; then
    cp HWRF_MOVE.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d23.grbf${hour00}
fi
cp HWRF_DOM3.GrbF$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d3.grbf${hour00}
cp $STORM$STORMID.$ymdh.combine_grbf$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d123.grbf${hour00}

          ### convert to grib2 data if requested
if [[ "$cnvit" == YES ]] ; then
    if [ $IDIAG = 1 ]; then
        $utilexec/cnvgrib -g12 -p32 -nv HWRF_PARENT.Grb10thF$hour00 HWRF_PARENT.Grb210thF$hour00
        /bin/mv -f HWRF_PARENT.Grb210thF$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d1.grb210thf${hour00}
    else
        $utilexec/cnvgrib -g12 -p32 -nv HWRF_PARENT.GrbF$hour00 HWRF_PARENT.Grb2F$hour00
        /bin/mv -f HWRF_PARENT.Grb2F$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d1.grb2f${hour00}
    fi
    $utilexec/cnvgrib -g12 -p32 -nv HWRF_NEST.GrbF$hour00 HWRF_NEST.Grb2F$hour00
    $utilexec/cnvgrib -g12 -p32 -nv HWRF_MOVE.GrbF$hour00 HWRF_MOVE.Grb2F$hour00
    $utilexec/cnvgrib -g12 -p32 -nv HWRF_DOM3.GrbF$hour00 HWRF_DOM3.Grb2F$hour00
    /bin/mv -f HWRF_NEST.Grb2F$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d2.grb2f${hour00}
    $utilexec/cnvgrib -g12 -p32 -nv $STORM$STORMID.$ymdh.combine_grbf$hour00 $STORM$STORMID.$ymdh.combine_grb2f$hour00
    /bin/mv -f $STORM$STORMID.$ymdh.combine_grb2f$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d123.grb2f${hour00}
    if [[ "$DELIVER_D23" == YES || "$DELIVER_D23" == GRIB2 ]] ; then
        /bin/cp    HWRF_MOVE.Grb2F$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d23.grb2f${hour00}
    fi
    /bin/mv -f HWRF_DOM3.Grb2F$hour00 ${COMOUT}/${nidymdh}.hwrfprs_$d3.grb2f${hour00}
          #### add DBN_ALERT by WANG 20070528
    if [[ "$SENDDBN" == YES && "$PARAFLAG" == NO ]] ; then
    #$DBNROOT/bin/dbn_alert MODEL HWRF_GRIB $job ${COMOUT}/${nidymdh}.hwrfeprs_$d1.grbf${hour00}
    #$DBNROOT/bin/dbn_alert MODEL HWRF_GRIB $job ${COMOUT}/${nidymdh}.hwrfprs_$d1.grbf${hour00}
    #$DBNROOT/bin/dbn_alert MODEL HWRF_GRIB $job ${COMOUT}/${nidymdh}.hwrfeprs_$d2.grbf${hour00}
    #$DBNROOT/bin/dbn_alert MODEL HWRF_GRIB $job ${COMOUT}/${nidymdh}.hwrfprs_$d2.grbf${hour00}
    #$DBNROOT/bin/dbn_alert MODEL HWRF_GRIB $job ${COMOUT}/${nidymdh}.hwrfprs_$d123.grbf${hour00}
        $DBNROOT/bin/dbn_alert MODEL HWRF_GRIB2P $job ${COMOUT}/${nidymdh}.hwrfprs_$d1.grb2f${hour00}
        $DBNROOT/bin/dbn_alert MODEL HWRF_GRIB2I $job ${COMOUT}/${nidymdh}.hwrfprs_$d2.grb2f${hour00}
        $DBNROOT/bin/dbn_alert MODEL HWRF_GRIB2C $job ${COMOUT}/${nidymdh}.hwrfprs_$d123.grb2f${hour00}
        if [[ "$DELIVER_D23" == YES || "$DELIVER_D23" == GRIB2 ]] ; then
            $DBNROOT/bin/dbn_alert MODEL HWRF_GRIB2M $job ${COMOUT}/${nidymdh}.hwrfprs_$d23.grb2f${hour00}
        fi
        $DBNROOT/bin/dbn_alert MODEL HWRF_GRIB2N $job ${COMOUT}/${nidymdh}.hwrfprs_$d3.grb2f${hour00}
    fi
fi

################################################################
# Move files to respective directories and produce cycle files
# for use by the Tracker Script
################################################################

/bin/mv -f $STORM$STORMID.$ymdh.parent_grbf$hour00   $wrfdir/PARENT
/bin/mv -f HWRF_PARENT.GrbF$hour00                   $wrfdir/PARENT
/bin/mv -f HWRF_PARENT.GrbF$hour00.ctl               $wrfdir/PARENT
/bin/mv -f HWRF_PARENT.GrbF$hour00.idx               $wrfdir/PARENT
/bin/mv -f HWRFPRS_PARENT.GrbF$hour00                $wrfdir/PARENT
/bin/mv -f HWRF_NEST.GrbF$hour00                     $wrfdir/NEST 
/bin/mv -f HWRF_NEST.GrbF$hour00.ctl                 $wrfdir/NEST 
/bin/mv -f HWRF_NEST.GrbF$hour00.idx                 $wrfdir/NEST 
/bin/mv -f $STORM$STORMID.$ymdh.nest_grbf$hour00     $wrfdir/NEST
/bin/mv -f HWRFPRS_NEST.GrbF$hour00                  $wrfdir/NEST
/bin/mv -f $STORM$STORMID.$ymdh.combine_grbf$hour00  $wrfdir/COMBINE
/bin/mv -f $STORM$STORMID.$ymdh.combine12_grbf$hour00  $wrfdir/DOM12
/bin/mv -f HWRF_MOVE.GrbF$hour00                     $wrfdir/COMBINE

echo 'SUCCESSFUL COMPLETION OF POST PROCESSING SCRIPT hwrf_post_deliver.sh'
