
########################### RTMA_BIAS ################################################
echo "------------------------------------------------"
echo "Update Downscaling Vector For Alaska between RTMA and NCEP operational analysis"
echo "------------------------------------------------"
echo "History: Jan 2010 - First implementation of this new script."
echo "AUTHOR: Bo Cui  (wx20cb)"
######################################################################################

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

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

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

export pgm=dvrtma_bias          
. prep_step

#################################################
### calculate bias estimation for different cycle                   
#################################################

###
# set the decaying average weight
###

weight=0.30

###
# rtma analysis files entry
###

afile=rtma.t${cyc}z.2dvaranl_ndfd.grb1
ln -fs $COMINRTMA/rtma.t${cyc}z.2dvaranl_ndfd.grb1 $afile

###
# NCEP operational analysis file entry, interpolate it on grids of 5.953km for Alaska region
###

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

###

infile=$COMINGEFS/gec00.t${cyc}z.pgrbaf00
outfile=gec00.t${cyc}z.pgrbaf00_temp
cfile=gec00.t${cyc}z.pgrbaf00_5km

if [[ ! -s $infile ]]
then
   echo "Input GEFS files not available"
   export err=1; err_chk
fi

>$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

###
# get initialized bias for $cyc, cstart: 1= cold start if bias accumulation
###

cstart=0

ifile=dvrtma.t${cyc}z.ndgd_alaska
pgbme=bias_rtma_opranl

if [ -s $COM/naefs.$PDYm1/${cyc}/ndgd/$ifile ]; then
  cp $COM/naefs.$PDYm1/${cyc}/ndgd/$ifile $pgbme
elif [ -s $COM/naefs.$PDYm2/${cyc}/ndgd/$ifile ]; then
  cp $COM/naefs.$PDYm2/${cyc}/ndgd/$ifile $pgbme
elif [ -s $COM/naefs.$PDYm3/${cyc}/ndgd/$ifile ]; then
  cp $COM/naefs.$PDYm3/${cyc}/ndgd/$ifile $pgbme
elif [ -s $COM/naefs.$PDYm4/${cyc}/ndgd/$ifile ]; then
  cp $COM/naefs.$PDYm4/${cyc}/ndgd/$ifile $pgbme
elif [ -s $COM/naefs.$PDYm5/${cyc}/ndgd/$ifile ]; then
  cp $COM/naefs.$PDYm5/${cyc}/ndgd/$ifile $pgbme
elif [ -s $COM/naefs.$PDYm6/${cyc}/ndgd/$ifile ]; then
  cp $COM/naefs.$PDYm6/${cyc}/ndgd/$ifile $pgbme
elif [ -s $COM/naefs.$PDYm7/${cyc}/ndgd/$ifile ]; then
  cp $COM/gefs.$PDYm7/${cyc}/ndgd/$ifile $pgbme
else
  echo "Cold Start for Bias Estimation between RTMA and NCEP Operational Analysis"
  cstart=1
fi

###
#  output ensemble forecasting bias estimation
###

ofile=dvrtma.t${cyc}z.ndgd_alaska

###

echo "&message"  >input
echo " icstart=${cstart}," >> input
echo " dec_w=${weight}," >> input
echo "/" >>input

ln -sf $pgbme fort.11
ln -sf $afile fort.12
ln -sf $cfile fort.13
ln -sf $ofile fort.51

startmsg
$EXECRTMA/dvrtma_bias  <input > $pgmout.$cyc
export err=$?;err_chk

if [ "$SENDCOM" = "YES" ]; then
  if [ -s  $ofile ]; then
    cp $ofile $COMOUT_GEFS/
    mv $ofile $COMOUT/$ofile
    $EXECUTIL/cnvgrib -g12 -p40 $COMOUT/$ofile $COMOUT_GB2/${ofile}.grib2
    if [ "$SENDDBN" = "YES" ]; then
      $DBNROOT/bin/dbn_alert MODEL NAEFS_RTMANDGD_GB2 $job $COMOUT_GB2/${ofile}.grib2
    fi
  fi
fi

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

