########################### NDGD PRODUCTS #################################################
echo "------------------------------------------------------------------------------------"
echo "Apply Downscaling Method to Generat Ensemble Forecast on NDGD Grid for Alsaka Region"
echo "Script:  exdvrtma_prob_avgspr_alaska.sh.sms"
echo "Author:  Bo Cui (wx20cb)"
echo "History: Jan. 2010 - first implementation of this new script"
echo "------------------------------------------------------------------------------------"
###########################################################################################

set -x

### To submit this job for T00Z, T06Z T12Z and T18Z, four cycles per day

### need pass the values of PDY, cyc, DATA, COMINNCEP, COMINCMC, COMOUT

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

export ENSDVRTMA=$USHrtma/dvrtma_debias_alaska.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"

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

if [ "$IFNAEFS" = "YES" ]; then
  export 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
# variable include: t2m, u10m, v10m, surface pressure, wind speed, wind direction, tmax and tmin
################################################################################################

$ENSDVRTMA     

#####################
# generate grib2 data
#####################

for nfhrs in $hourlist; do
  for nens in $memberlist; do
    out_file=${nens}.t${cyc}z.ndgd_alaskaf${nfhrs}
    if [ "$SENDCOM" = "YES" ]; then
      if [ -s ${out_file} ]; then
        $EXECUTIL/cnvgrib -g12 -p40 ${out_file} ${out_file}.grib2
        mv ${out_file} $COMOUT/
        mv ${out_file}.grib2 $COMOUT_GB2/
      fi
    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
#######################################################################
 for nfhrs in $hourlist; do
  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_alaskaf${nfhrs}.grib2
           $DBNROOT/bin/dbn_alert MODEL NAEFS_50NDGD_GB2 $job $COMOUT_GB2/naefs_ge50pt.t${cyc}z.ndgd_alaskaf${nfhrs}.grib2
           $DBNROOT/bin/dbn_alert MODEL NAEFS_90NDGD_GB2 $job $COMOUT_GB2/naefs_ge90pt.t${cyc}z.ndgd_alaskaf${nfhrs}.grib2
           $DBNROOT/bin/dbn_alert MODEL NAEFS_AVGNDGD_GB2 $job $COMOUT_GB2/naefs_geavg.t${cyc}z.ndgd_alaskaf${nfhrs}.grib2
           $DBNROOT/bin/dbn_alert MODEL NAEFS_MODENDGD_GB2 $job $COMOUT_GB2/naefs_gemode.t${cyc}z.ndgd_alaskaf${nfhrs}.grib2
           $DBNROOT/bin/dbn_alert MODEL NAEFS_SPRNDGD_GB2 $job $COMOUT_GB2/naefs_gespr.t${cyc}z.ndgd_alaskaf${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_alaskaf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_50NDGD_GB2 $job $COMOUT_GB2/ge50pt.t${cyc}z.ndgd_alaskaf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_90NDGD_GB2 $job $COMOUT_GB2/ge90pt.t${cyc}z.ndgd_alaskaf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_AVGNDGD_GB2 $job $COMOUT_GB2/geavg.t${cyc}z.ndgd_alaskaf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_MODENDGD_GB2 $job $COMOUT_GB2/gemode.t${cyc}z.ndgd_alaskaf${nfhrs}.grib2
      $DBNROOT/bin/dbn_alert MODEL GEFS_SPRNDGD_GB2 $job $COMOUT_GB2/gespr.t${cyc}z.ndgd_alaskaf${nfhrs}.grib2
    fi
  fi
 done

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