#######################################################################
echo "------------------------------------------------"
echo "Downscale Ensemble Products on NDGD Grid"
echo "------------------------------------------------"
echo "History: August 2007 - First implementation of this new script."
echo "AUTHOR: Bo Cui  (wx20cb)"
########################################################################
set -x
### To submit this job for T00Z and T12Z

### need pass the values of PDY, CYC, DATA, COMIN, COM, COMOUTBC, COMOUTAN and COMOUTWT


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

hourlist=$FHRLIST
memberlist=$MEMLIST

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

export NDATE=$EXECUTIL/ndate
export COPYGB=$EXECUTIL/copygb

export pgm=dvrtma_debias
. prep_step

###########################################################
# Downscale ensemble for each forecast time and each member
###########################################################

for nfhrs in $hourlist
do

  for nens in $memberlist
  do

###
#  set the index ( exist of bias estimation )  as default, 0
###

    cstart=0

###
#  bias estimation entry
###

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

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

    if [ -s $COM/naefs.$PDY/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDY/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm1/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm1/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm2/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm2/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm3/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm3/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm4/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm4/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm5/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm5/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$PDYm6/${cyc_verf}/ndgd/$ifile ]; then
      cp $COM/naefs.$PDYm6/${cyc_verf}/ndgd/$ifile $ifile
    elif [ -s $COM/naefs.$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 "&message"  >input.$nfhrs.$nens
    echo " icstart=${cstart}," >> input.$nfhrs.$nens
    echo "/" >>input.$nfhrs.$nens

###
#  downscale ensemble forecasting output
###

    ofile=${nens}.t${cyc}z.ndgd_conusf${nfhrs}

###
#  check pgrba forecast file, interpolated on grids with 5.079 resolution
###
   
    grid='255 3 1073 689 20192 238446 8 265000 5079 5079 0 64 25000 25000 0 0 0'
    infile=$COMIN/${nens}.t${cyc}z.pgrba_bcf${nfhrs} 
    outfile=${nens}.t${cyc}z.pgrba_bcf${nfhrs}_temp
    cfile=${nens}.t${cyc}z.pgrba_bcf${nfhrs}_5km 

    rm fort.*

    >$outfile
    $EXECUTIL/wgrib $infile | grep ":PRES" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile
    $EXECUTIL/wgrib $infile | grep ":TMP:" | grep "2 m" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile
    $EXECUTIL/wgrib $infile | grep ":UGRD:" | grep "10 m" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile
    $EXECUTIL/wgrib $infile | grep ":VGRD:" | grep "10 m" | $EXECUTIL/wgrib -i $infile  -grib -append -o $outfile

    $COPYGB -g"$grid" -i1,1 -x $outfile $cfile

    if [ "$nens" = "cmc_gespr" -o "$nens" = "gespr" -o "$nens" = "naefs_gespr" ]; then
      cp $cfile $ofile
    else
      ln -sf $ifile fort.11
      ln -sf $cfile fort.12
      ln -sf $ofile fort.51

      startmsg
      $EXECRTMA/$pgm   <input.$nfhrs.$nens  > $pgmout.$nfhrs.$nens  2> errfile
      export err=$?;err_chk
    fi

  done
done

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