###########################################################################
# This script generates the 6-hourly (12 or 24 hr) pqpf pqif pqrf pqff pqsf
###########################################################################

#!/bin/sh

set +x
echo " "
echo " Entering sub script exgefs_pgrb_enspqpf.sh"
echo " job input forecast interval is: $HRINTER   "
echo " "
set -x

#################################
# set input parameters 
# PDY    : forecast initial time
# cyc    : initial cycle
# HRINTER: forecast hour interval
#################################

pgm=gefs_pgrb_enspqpf
pgmout=output

cd $DATA

hourlist="     06  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 \
          102 108 114 120 126 132 138 144 150 156 162 168 174 180 186 192 198 \
          204 210 216 222 228 234 240 246 252 258 264 270 276 282 288 294 300 \
          306 312 318 324 330 336 342 348 354 360 366 372 378 384"

memberlist="c00 p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"

outfile_prcp=enspost.t${cyc}z.prcp                      
outfile_rain=enspost.t${cyc}z.rain                      
outfile_frzr=enspost.t${cyc}z.frzr                      
outfile_icep=enspost.t${cyc}z.icep                      
outfile_snow=enspost.t${cyc}z.snow                      

>$outfile_prcp
if [ "$HRINTER" = "6" ]; then
  >$outfile_rain
  >$outfile_frzr
  >$outfile_icep
  >$outfile_snow
fi

for nfhrs in $hourlist; do
  for nens in $memberlist; do
    infile=$COMIN/ge${nens}.t${cyc}z.pgrbaf${nfhrs}                  
    $EXECUTIL/wgrib  $infile | grep ":APCP:" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile_prcp
    if [ "$HRINTER" = "6" ]; then
      $EXECUTIL/wgrib  $infile | grep ":CRAIN:" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile_rain
      $EXECUTIL/wgrib  $infile | grep ":CFRZR:" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile_frzr
      $EXECUTIL/wgrib  $infile | grep ":CICEP:" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile_icep
      $EXECUTIL/wgrib  $infile | grep ":CSNOW:" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile_snow
    fi
  done
done

# Specify the input/output file names:

export CPGB=enspost.t${cyc}z.prcp
export CPGO=pqpf   

export CRAIN=enspost.t${cyc}z.rain
export CRAINO=pqrf

export CFRZR=enspost.t${cyc}z.frzr
export CFRZRO=pqff

export CICEP=enspost.t${cyc}z.icep
export CICEPO=pqif 

export CSNOW=enspost.t${cyc}z.snow
export CSNOWO=pqsf  

rm inputpqpf

echo "&namin"                           >inputpqpf
echo "icyc=$cyc"                       >>inputpqpf
echo "hrinter=$HRINTER"                 >>inputpqpf
echo "cpgb='$CPGB',cpge='$CPGO'"        >>inputpqpf
echo "crain='$CRAIN',craino='$CRAINO'"  >>inputpqpf
echo "cfrzr='$CFRZR',cfrzro='$CFRZRO'"  >>inputpqpf
echo "cicep='$CICEP',cicepo='$CICEPO'"  >>inputpqpf
echo "csnow='$CSNOW',csnowo='$CSNOWO'"  >>inputpqpf
echo "/"                                >>inputpqpf

cat inputpqpf

rm $CPGO $CRAINO $CFRZRO $CICEPO $CSNOWO

. prep_step
startmsg
$EXECPQPF/$pgm  <inputpqpf  >$pgmout
export err=$?;err_chk

if [ "$HRINTER" = "6" ]; then
  hourlist="    06   12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 \
            102 108 114 120 126 132 138 144 150 156 162 168 174 180 186 192 198 \
            204 210 216 222 228 234 240 246 252 258 264 270 276 282 288 294 300 \
            306 312 318 324 330 336 342 348 354 360 366 372 378 384"
  var=pqpf
  for var in pqpf pqrf pqsf pqif pqff; do
    for nfhrs in $hourlist; do
      hb=`expr $nfhrs - 6`
      infile=pqpf
      outfile=ge${var}.t${cyc}z.pgrb_${HRINTER}hf$nfhrs
      outfile_gb2=ge${var}.t${cyc}z.pgrb2_${HRINTER}hf$nfhrs
      if [ $nfhrs -eq 6 ]; then 
        outfile=ge${var}.t${cyc}z.pgrb_${HRINTER}hf0$nfhrs
        outfile_gb2=ge${var}.t${cyc}z.pgrb2_${HRINTER}hf0$nfhrs
      fi
      $EXECUTIL/wgrib $infile| grep ":${hb}-${nfhrs}hr" | $EXECUTIL/wgrib -i $infile  -grib -o $outfile
      if [ ! -s $outfile ]; then
        echo "*********** Warning!!! Warning!!! ************"
        echo "**** There is empty file for $outfile ********"
      else
        cp $outfile $COMOUT/
        $EXECUTIL/cnvgrib -g12 -p40 $COMOUT/$outfile $COMOUT/${outfile_gb2}
        if [ "$SENDDBN" = "YES" ]; then
           $DBNROOT/bin/dbn_alert MODEL ENS_PCP_GB2 $job $COMOUT/${outfile_gb2}
        fi
      fi
    done
  done
fi

if [ "$HRINTER" = "12" ]; then
  hourlist="         12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 \
            102 108 114 120 126 132 138 144 150 156 162 168 174 180 186 192 198 \
            204 210 216 222 228 234 240 246 252 258 264 270 276 282 288 294 300 \
            306 312 318 324 330 336 342 348 354 360 366 372 378 384"
  var=pqpf
  for nfhrs in $hourlist; do
    hb=`expr $nfhrs - 12`
    infile=pqpf
    outfile=ge${var}.t${cyc}z.pgrb_${HRINTER}hf$nfhrs
    outfile_gb2=ge${var}.t${cyc}z.pgrb2_${HRINTER}hf$nfhrs
    $EXECUTIL/wgrib $infile| grep ":${hb}-${nfhrs}hr" | $EXECUTIL/wgrib -i $infile  -grib -o $outfile
    if [ ! -s $outfile ]; then
      echo "*********** Warning!!! Warning!!! ************"
      echo "**** There is empty file for $outfile ********"
    else
      cp $outfile $COMOUT/
      $EXECUTIL/cnvgrib -g12 -p40 $COMOUT/$outfile $COMOUT/${outfile_gb2}
      if [ "$SENDDBN" = "YES" ]; then
         $DBNROOT/bin/dbn_alert MODEL ENS_PCP_GB2 $job $COMOUT/${outfile_gb2}
      fi
    fi
  done
fi

if [ "$HRINTER" = "24" ]; then
  hourlist="                 24  30  36  42  48  54  60  66  72  78  84  90  96 \
            102 108 114 120 126 132 138 144 150 156 162 168 174 180 186 192 198 \
            204 210 216 222 228 234 240 246 252 258 264 270 276 282 288 294 300 \
            306 312 318 324 330 336 342 348 354 360 366 372 378 384"
  var=pqpf
  for nfhrs in $hourlist; do
    hb=`expr $nfhrs - 24`
    infile=pqpf
    outfile=ge${var}.t${cyc}z.pgrb_${HRINTER}hf$nfhrs
    outfile_gb2=ge${var}.t${cyc}z.pgrb2_${HRINTER}hf$nfhrs
    $EXECUTIL/wgrib $infile| grep ":${hb}-${nfhrs}hr" | $EXECUTIL/wgrib -i $infile  -grib -o $outfile
    if [ ! -s $outfile ]; then
      echo "*********** Warning!!! Warning!!! ************"
      echo "**** There is empty file for $outfile ********"
    else
      cp $outfile $COMOUT/
      $EXECUTIL/cnvgrib -g12 -p40 $COMOUT/$outfile $COMOUT/${outfile_gb2}
      if [ "$SENDDBN" = "YES" ]; then
         $DBNROOT/bin/dbn_alert MODEL ENS_PCP_GB2 $job $COMOUT/${outfile_gb2}
      fi
    fi
  done
fi

set +x
echo " "
echo "Leaving sub script exgefs_pgrb_enspqpf.sh"
echo " "
set -x


