
########################### NDGD PRODUCTS ###############################
echo "------------------------------------------------------------------"
echo "Apply Downscaling Method to Generat Ensemble Forecast on NDGD Grid"
echo "-------------------------------------------------------------------"
echo "History: Aguust 2007 - First implementation of this new script."
echo "AUTHOR: Bo Cui  (wx20cb)"
#########################################################################
set -x
### To submit this job for T00Z, T06Z T12Z and T18Z, four cycles per day

### need pass the values of PDY, CYC, DATA, COMIN, COMOUT

##############################################
# define exec variable, and entry grib utility
##############################################

export ENSDVRTMA=$USHRTMA/dvrtma_debias.sh           

########################################
#  define ensemble members and lead time
########################################

export 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"

export memberlist_cmc="cmc_ge10pt cmc_ge50pt cmc_ge90pt cmc_gemode cmc_geavg cmc_gespr"
export memberlist_ncep="ge10pt ge50pt ge90pt gemode geavg gespr"
export memberlist_naefs="naefs_ge10pt naefs_ge50pt naefs_ge90pt naefs_gemode naefs_geavg naefs_gespr"

if [ "$IFNAEFS" = "YES" ]; then
   memberlist=$memberlist_naefs
fi

if [ "$IFGEFS" = "YES" ]; then
  export memberlist=$memberlist_ncep
fi

if [ "$IFCMCE" = "YES" ]; then
  export memberlist=$memberlist_cmc  
fi

######################################################################
### downscale ensemble probability forecast, ensemble average & spread
######################################################################

for nfhrs in $hourlist; do

  for nens in $memberlist; do

    export FHRLIST=$nfhrs
    export MEMLIST=$nens 

    in_file=$COMIN/${nens}.t${cyc}z.pgrba_bcf$nfhrs
    out_file=${nens}.t${cyc}z.ndgd_conusf${nfhrs}

    icnt=0

    while [ icnt -le 12 ]; do
      if [ -s ${in_file} ]; then
        $ENSDVRTMA     
        if [ "$SENDCOM" = "YES" ]; then
          if [ -s ${out_file} ]; then
            mv ${out_file}  $COMOUT/
            $EXECUTIL/cnvgrib -g12 -p40 $COMOUT/${out_file} $COMOUT_GB2/${out_file}.grib2
          fi
        fi
        icnt=13
      else
        sleep 10
        icnt=`expr $icnt + 1`
        echo $icnt
      fi
    done

  done

#######################################################################
# Send NAEFS alerts only for the 00z and 12z late runs, since CMC data 
# is only available then; send GEFS for all cycles
#######################################################################
if [ "$SENDDBN" = "YES" ]; then
   if [ "$IFNAEFS" = "YES" ]; then
      if [ "$cyc" = "00" -o "$cyc" = "12" ]; then
         if [ "$runlabel" = "late" ]; then
      $DBNROOT/bin/dbn_alert MODEL NAEFS_10NDGD_GB2 $job $COMOUT_GB2/naefs_ge10pt.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL NAEFS_50NDGD_GB2 $job $COMOUT_GB2/naefs_ge50pt.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL NAEFS_90NDGD_GB2 $job $COMOUT_GB2/naefs_ge90pt.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL NAEFS_AVGNDGD_GB2 $job $COMOUT_GB2/naefs_geavg.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL NAEFS_MODENDGD_GB2 $job $COMOUT_GB2/naefs_gemode.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL NAEFS_SPRNDGD_GB2 $job $COMOUT_GB2/naefs_gespr.t${cyc}z.ndgd_conusf${nfhrs}.grib2
         fi
      fi
   fi
   if [ "$IFGEFS" = "YES" ]; then
      $DBNROOT/bin/dbn_alert MODEL GEFS_10NDGD_GB2 $job $COMOUT_GB2/ge10pt.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_50NDGD_GB2 $job $COMOUT_GB2/ge50pt.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_90NDGD_GB2 $job $COMOUT_GB2/ge90pt.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_AVGNDGD_GB2 $job $COMOUT_GB2/geavg.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_MODENDGD_GB2 $job $COMOUT_GB2/gemode.t${cyc}z.ndgd_conusf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_SPRNDGD_GB2 $job $COMOUT_GB2/gespr.t${cyc}z.ndgd_conusf${nfhrs}.grib2
   fi
fi
done

###############################
###  final check up and make up
###############################

for nfhrs in $hourlist; do
  for nens in $memberlist; do

    in_file=$COMIN/${nens}.t${cyc}z.pgrba_bcf$nfhrs
    out_file=${nens}.t${cyc}z.ndgd_conusf${nfhrs}

    if [ ! -s $COMOUT/${out_file} ]; then

      export FHRLIST=$nfhrs
      export MEMLIST=$nens 

      $ENSDVRTMA     
      if [ "$SENDCOM" = "YES" ]; then
        if [ -s ${out_file} ]; then
          mv ${out_file} $COMOUT/
          $EXECUTIL/cnvgrib -g12 -p40 $COMOUT/${out_file} $COMOUT_GB2/${out_file}.grib2
        fi
      fi

    fi

  done
done

msg="HAS COMPLETED NORMALLY!"
postmsg "$jlogfile" "$msg"
