######################################################################################
# Script:   dvrtma_debias_alaska_wind10m.sh
# Abstract: Downscale Ensemble Products on Alaska Region for Variables 10m Wdir & Wspd 
# Author:   Bo Cui
# History:  January 2010 - First implementation of this new script
#######################################################################################

echo "----------------------------------------------------------------------"
echo "dvrtma_debias_alaska_wind10m.sh: downscale wind speed & wind direction"
echo "----------------------------------------------------------------------"

set -x

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

export COPYGB=/nwprod/util/exec/copygb

CMCEADJUST=$USHrtma/cmce_adjust_wind10m.sh

pgm=dvrtma_debias_alaska_wind10m
pgmout=output_ds_wind10m 

##########################################################################
#  define ensemble members for NCEP and CMC
#  different from definition of scripts/exdvrtma_prob_avgspr_alaska.sh.sms
##########################################################################

#hourlist=" 00  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_ncep="p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"
memberlist_cmc=" p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"

if [ "$IFNAEFS" = "YES" ]; then
  memberlist="$memberlist_cmc $memberlist_ncep"
  centerlist="naefs"
  out=naefs
fi

if [ "$IFGEFS" = "YES" ]; then
  memberlist="$memberlist_ncep"
  centerlist="gefs"
  out=gefs
fi

if [ "$IFCMCE" = "YES" ]; then
  memberlist="$memberlist_cmc"
  centerlist="cmce"
  out=cmce
fi

####################################
# calculate the ensemble member size
####################################

tmems=0
for imem in $memberlist; do
  (( tmems = tmems + 1 ))
done

########################################################
# set PDS message for grids of 5.953km for Alaska region
########################################################

grid='255 5 825 553 40530 181429 8 210000 5953 5953 0 64'

###################################################################################
# Adjust CMC ensemble forecast, all members are shift to NCEP analysis
# Input CMC Ensemble forecast, all members are saved in one file for one lead time 
###################################################################################

if [ "$IFNAEFS" = "YES" -o  "$IFCMCE" = "YES" ]; then
 
  $CMCEADJUST

  cd $DATA/tmpdir_03 

  for nfhrs in $hourlist; do

  ( outfile=cmce.t${cyc}z.pgrba_bcf${nfhrs}_5km
    rm $outfile
    >$outfile 

    for mem in $memberlist_cmc; do

      file_temp=cmc_ge$mem.t${cyc}z.pgrba_bcf${nfhrs}_temp
      ofile_temp=cmc_ge$mem.t${cyc}z.pgrba_bcf${nfhrs}_5km

      $COPYGB -g"$grid" -i1,1 -x $file_temp $ofile_temp
      cat $ofile_temp >>$outfile     

    done ) &

  done

fi

###################################################################################
# input NCEP Ensemble forecast, all members are saved in one file for one lead time 
###################################################################################

if [ "$IFNAEFS" = "YES" -o  "$IFGEFS" = "YES" ]; then

  for nfhrs in $hourlist; do

  ( file_temp=gefs.t${cyc}z.pgrba_bcf${nfhrs}_temp
    outfile=gefs.t${cyc}z.pgrba_bcf${nfhrs}_5km
    rm $file_temp
    >$file_temp

    for mem in ${memberlist_ncep}; do

      infile=$COMINNCEP/ge${mem}.t${cyc}z.pgrba_bcf${nfhrs}

      $EXECUTIL/wgrib $infile | grep ":UGRD:" | grep "10 m"  | $EXECUTIL/wgrib -i $infile  -grib -append -o $file_temp
      $EXECUTIL/wgrib $infile | grep ":VGRD:" | grep "10 m"  | $EXECUTIL/wgrib -i $infile  -grib -append -o $file_temp

    done ) &

  done     

  wait 

  for nfhrs in $hourlist; do
  ( file_temp=gefs.t${cyc}z.pgrba_bcf${nfhrs}_temp
    outfile=gefs.t${cyc}z.pgrba_bcf${nfhrs}_5km
    $COPYGB -g"$grid" -i1,1 -x $file_temp $outfile  ) &
  done    

fi

wait

#########################################
# Combine GEFS and CMCE ensemble together
#########################################

if [ "$IFNAEFS" = "YES" ]; then
  for nfhrs in $hourlist; do
  ( infile1=gefs.t${cyc}z.pgrba_bcf${nfhrs}_5km
    infile2=cmce.t${cyc}z.pgrba_bcf${nfhrs}_5km
    outfile=$out.t${cyc}z.pgrba_bcf${nfhrs}_5km
    rm $outfile
    >$outfile
    cat $infile1 $infile2 >$outfile  ) &
  done
fi

wait

####################
# set up input files
####################

for nfhrs in $hourlist; do

( echo " &namens" >>namin.wind.prob.$nfhrs

  infile=$out.t${cyc}z.pgrba_bcf${nfhrs}_5km

  echo " tfiles=$tmems," >>namin.wind.prob.$nfhrs
  echo " cfipg1='${infile}'," >>namin.wind.prob.$nfhrs

###########################
# set up downscaling vector
##########################

  cyc_verf=`$NDATE +$nfhrs $PDY$cyc | cut -c9-10`
  echo $nfhrs $cyc_verf

  ifile=dvrtma.t${cyc_verf}z.ndgd_alaska

  cstart=0

  if [ -s $COM/gefs.$PDY/${cyc_verf}/ndgd/$ifile ]; then
    cp $COM/gefs.$PDY/${cyc_verf}/ndgd/$ifile $ifile
  elif [ -s $COM/gefs.$PDYm1/${cyc_verf}/ndgd/$ifile ]; then
    cp $COM/gefs.$PDYm1/${cyc_verf}/ndgd/$ifile $ifile
  elif [ -s $COM/gefs.$PDYm2/${cyc_verf}/ndgd/$ifile ]; then
    cp $COM/gefs.$PDYm2/${cyc_verf}/ndgd/$ifile $ifile
  elif [ -s $COM/gefs.$PDYm3/${cyc_verf}/ndgd/$ifile ]; then
    cp $COM/gefs.$PDYm3/${cyc_verf}/ndgd/$ifile $ifile
  elif [ -s $COM/gefs.$PDYm4/${cyc_verf}/ndgd/$ifile ]; then
    cp $COM/gefs.$PDYm4/${cyc_verf}/ndgd/$ifile $ifile
  elif [ -s $COM/gefs.$PDYm5/${cyc_verf}/ndgd/$ifile ]; then
    cp $COM/gefs.$PDYm5/${cyc_verf}/ndgd/$ifile $ifile
  elif [ -s $COM/gefs.$PDYm6/${cyc_verf}/ndgd/$ifile ]; then
    cp $COM/gefs.$PDYm6/${cyc_verf}/ndgd/$ifile $ifile
  elif [ -s $COM/gefs.$PDYm7/${cyc_verf}/ndgd/$ifile ]; then
    cp $COM/gefs.$PDYm7/${cyc_verf}/ndgd/$ifile $ifile
  else
    echo " There is no Bias Estimation at " ${cyc_verf}Z
    cstart=1
  fi

  echo " icstart=${cstart}," >> namin.wind.prob.$nfhrs 
  echo " cfipg2='${ifile}'," >>namin.wind.prob.$nfhrs

#####################
# set up output files
#####################

  ofile=t${cyc}z.ndgd_alaskaf${nfhrs}_part2

  rm *_$ofile

  if [ "$IFNAEFS" = "YES" ]; then
    echo " cfopg1='naefs_ge10pt.${ofile}'," >>namin.wind.prob.$nfhrs
    echo " cfopg2='naefs_ge90pt.${ofile}'," >>namin.wind.prob.$nfhrs
    echo " cfopg3='naefs_ge50pt.${ofile}'," >>namin.wind.prob.$nfhrs
    echo " cfopg4='naefs_geavg.${ofile}'" >>namin.wind.prob.$nfhrs
    echo " cfopg5='naefs_gespr.${ofile}'," >>namin.wind.prob.$nfhrs
    echo " cfopg6='naefs_gemode.${ofile}'," >>namin.wind.prob.$nfhrs
  fi

  if [ "$IFGEFS" = "YES" ]; then
    echo " cfopg1='ge10pt.${ofile}'," >>namin.wind.prob.$nfhrs
    echo " cfopg2='ge90pt.${ofile}'," >>namin.wind.prob.$nfhrs
    echo " cfopg3='ge50pt.${ofile}'," >>namin.wind.prob.$nfhrs
    echo " cfopg4='geavg.${ofile}'" >>namin.wind.prob.$nfhrs
    echo " cfopg5='gespr.${ofile}'," >>namin.wind.prob.$nfhrs
    echo " cfopg6='gemode.${ofile}'," >>namin.wind.prob.$nfhrs
  fi
   
  echo "/" >>namin.wind.prob.$nfhrs

  startmsg
  $EXECrtma/$pgm  <namin.wind.prob.$nfhrs > $pgmout.$nfhrs  2> errfile
  export err=$?;$DATA/err_chk

  ) &

done

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

