########################### BIASUPDATE ############################################################
echo "-------------------------------------------------------------------------"
echo "Update Bias Estimation of Global Ensemble Forecast & GFS Forecast Daily"
echo "-------------------------------------------------------------------------"
echo "History: May 2006 - First implementation of this new script."
echo "         Dec 2006 - Modified script for GEFS system upgrade (20 members)"
echo "         May 2007 - Modified script to include GFS forecast bias estimation"
echo "         Jan 2008 - Add bias estimation calculation between NCEP and CMC analysis (CMC-NCEP)"
echo "         Apr 2009 - Add 14 new variables for bias estimation "
echo "         Dec 2010 - Add 1 new variables for bias estimation "
echo "AUTHOR: Bo Cui  (wx20cb)"
########################### BIASUPDATE ############################################################

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

### 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=gefs_bias
. prep_step

########################################################
### define the days for searching bias estimation backup
########################################################
###
ymdh=${PDY}${cyc}
export PDYm8=`$NDATE -192 $ymdh | cut -c1-8`
export PDYm9=`$NDATE -216 $ymdh | cut -c1-8`
export PDYm10=`$NDATE -240 $ymdh | cut -c1-8`
export PDYm11=`$NDATE -264 $ymdh | cut -c1-8`
export PDYm12=`$NDATE -288 $ymdh | cut -c1-8`
export PDYm13=`$NDATE -312 $ymdh | cut -c1-8`
export PDYm14=`$NDATE -336 $ymdh | cut -c1-8`
export PDYm15=`$NDATE -360 $ymdh | cut -c1-8`
export PDYm16=`$NDATE -384 $ymdh | cut -c1-8`
export PDYm17=`$NDATE -408 $ymdh | cut -c1-8`
export PDYm18=`$NDATE -432 $ymdh | cut -c1-8`
                                                                                                             
###
################################################################
### calculate bias estimation for different forecast lead time
################################################################

if [ "$BIASMEM" = "YES" ]; then
  memberlist="c00 p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"
fi

if [ "$BIASAVG" = "YES" ]; then
  memberlist="avg"
fi

if [ "$BIASC00" = "YES" ]; then
  memberlist="c00"
fi

if [ "$BIASGFS" = "YES" ]; then
  memberlist="gfs"
fi

if [ "$BIASAVG" = "YES" -a "$BIASGFS" = "YES" ]; then
  memberlist="avg gfs"
fi

if [ "$BIASAVG" = "YES" -a "$BIASGFS" = "YES" -a "$BIASC00" = "YES" ]; then
  memberlist="avg gfs c00"
fi

###
# input basic information, member and forecast lead time
###

if [ "$BIASC00" = "YES" -o "$BIASAVG" = "YES" -o "$BIASMEM" = "YES" -o "$BIASGFS" = "YES" ]; then

for nens in $memberlist; do

  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"

  if [ "$RUNJOB" = "EXTEND" ]; then
    hourlist="  390 396 402 408 414 420 426 432 438 444 450 456 462 468 474 480 \
                486 492 498 504 510 516 522 528 534 540 546 552 558 564 570 576 \
                582 588 594 600 606 612 618 624 630 636 642 648 654 660 666 672 \
                678 684 690 696 702 708 714 720 726 732 738 744 750 756 762 768 \
                774 780 786 792 798 804 810 816 822 828 834 840 "
  fi

  if [ "$nens" = "gfs" ]; then
    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 "
  fi

  for nfhrs in $hourlist; do

###
# analysis files entry
###

  fymdh=${PDYm1}18
  fymdh=`$NDATE -$nfhrs $fymdh `
  fymd=`echo $fymdh | cut -c1-8`

  aymdh=`$NDATE +$nfhrs $fymd$cyc `
  aymd=`echo $aymdh | cut -c1-8`
  acyc=`echo $aymdh | cut -c9-10`

  aymdh_m06=`$NDATE -6 $aymdh `
  aymd_m06=`echo $aymdh_m06 | cut -c1-8`
  acyc_m06=`echo $aymdh_m06 | cut -c9-10`

  afile=$COM_GEFS/gefs.${aymd}/${acyc}/pgrba/gec00.t${acyc}z.pgrbaf00
  afile_m06=$COM_GEFS/gefs.${aymd_m06}/${acyc_m06}/pgrba/gec00.t${acyc_m06}z.pgrbaf06

  if [ "$nens" = "gfs" ]; then
    afile=$COM_GEFS/gefs.${aymd}/${acyc}/pgrba/gegfs.t${acyc}z.pgrbaf00
    afile_m06=$COM_GEFS/gefs.${aymd_m06}/${acyc_m06}/pgrba/gegfs.t${acyc_m06}z.pgrbaf06
  fi

  if [ -s $afile ]; then
    echo " "
  else
    echo " There is no Analysis data, Stop! for " $acyc 
  fi

  if [ -s $afile_m06 ]; then
    echo " "
  else
    echo " There is no Analysis data for Tmax and Tmin, Stop! for " $acyc_m06 
  fi

###
# forecast files entry
###

  cfile=$COM_GEFS/gefs.${fymd}/${cyc}/pgrba/ge$nens.t${cyc}z.pgrbaf$nfhrs  

###
# get initialized bias for $nens at $nfhrs, set the no cold start index as default, 0
###

  cstart=0

  ifile=ge${nens}.t${cyc}z.pgrba_mef${nfhrs}
  pgbme=bias.ge${nens}.t${cyc}z.f$nfhrs

  if [ -s $COM_GEFS/gefs.$PDYm1/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm1/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm2/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm2/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm3/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm3/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm4/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm4/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm5/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm5/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm6/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm6/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm7/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm7/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm8/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm8/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm9/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm9/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm10/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm10/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm11/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm11/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm12/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm12/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm13/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm13/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm14/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm14/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm15/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm15/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm16/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm16/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm17/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm17/${cyc}/pgrba/$ifile $pgbme
  elif [ -s $COM_GEFS/gefs.$PDYm18/${cyc}/pgrba/$ifile ]; then
    cp $COM_GEFS/gefs.$PDYm18/${cyc}/pgrba/$ifile $pgbme
  else
    echo "Cold Start for Bias Estimation between Analysis and NCEP Forecast At " $nfhrs " For " $nens
    cstart=1
  fi

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

  ofile=ge${nens}.t${cyc}z.pgrba_mef${nfhrs}

  rm fort.*
 
  odate=`$NDATE -24 $PDY$cyc `

  echo "&message"  >input.$nfhrs.$nens
  echo " icstart=${cstart}," >> input.$nfhrs.$nens
  echo " nens='$nens'," >>input.$nfhrs.$nens
  echo " dec_w=0.02," >> input.$nfhrs.$nens
  echo " odate=$odate," >>input.$nfhrs.$nens
  echo "/" >>input.$nfhrs.$nens
  
  ln -sf $pgbme     fort.11
  ln -sf $afile     fort.12
  ln -sf $afile_m06 fort.13
  ln -sf $cfile     fort.14
  ln -sf $ofile     fort.51

  startmsg
  $EXECGEFS/$pgm  <input.$nfhrs.$nens > $pgmout.$nfhrs.${nens} 2> errfile
  export err=$?;err_chk

 done
done

fi

###
#  calculate bias estimation between CDAS and GDAS            
###

if [ "$BIASCDAS" = "YES" ]; then

rm fort.*

for nfhrs in 00; do
 for nens in mdf; do

###
#  set the no cold start index as default, 0
###
  cstart=0

###
# gdas operational analysis file entry
###

  aymdh=$PDYm2$cyc
  aymd=`echo $aymdh | cut -c1-8`
  acyc=`echo $aymdh | cut -c9-10`
  echo $aymdh $aymd $acyc

  aymdh_m06=`$NDATE -6 $aymdh `
  aymd_m06=`echo $aymdh_m06 | cut -c1-8`
  acyc_m06=`echo $aymdh_m06 | cut -c9-10`
                                                           
  afile=$COM_GEFS/gefs.${aymd}/${acyc}/pgrba/gec00.t${acyc}z.pgrbaf00
  afile_m06=$COM_GEFS/gefs.${aymd_m06}/${acyc_m06}/pgrba/gec00.t${acyc_m06}z.pgrbaf06

  if [ -s $afile ]; then
    echo " "
  else
    echo " There is no Analysis data, Stop! for " $acyc
#   exit
  fi

###
# cdas reanalysi file entry
###

  $COPYGB -g3 -i1,1 -x $COMINCDAS/cdas.${aymd}/cdas.t${acyc}z.pgrbf00              cdas.t${acyc}z.pgrbf00.$aymd 
  $COPYGB -g3 -i1,1 -x $COMINCDAS/cdas.${aymd_m06}/cdas.t${acyc_m06}z.sfluxgrbf06  cdas.t${acyc_m06}z.sfluxgrbf06.$aymd_m06

  rfile=cdas.t${acyc}z.pgrbf00.$aymd
  rfile_m06=cdas.t${acyc_m06}z.sfluxgrbf06.$aymd_m06

###
#  get initialized bias between analyais and reanalysis entry
###

  pgbmdf=glbanl.t${cyc}z.pgrba_mdf00
  pgbmean=glbanl.t${cyc}z.pgrba_meandif

  if [ -s $COM/gefs.$PDYm3/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm3/${cyc}/pgrba/$pgbmdf $pgbmean
  elif [ -s $COM/gefs.$PDYm4/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm4/${cyc}/pgrba/$pgbmdf $pgbmean
  elif [ -s $COM/gefs.$PDYm5/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm5/${cyc}/pgrba/$pgbmdf $pgbmean
  elif [ -s $COM/gefs.$PDYm6/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm6/${cyc}/pgrba/$pgbmdf $pgbmean
  elif [ -s $COM/gefs.$PDYm7/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm7/${cyc}/pgrba/$pgbmdf $pgbmean
  else
    echo " Cold Start for Bias Estimation between CDAS and GDAS Fcst. Hour " $nfhrs 
    cstart=1
  fi

###
#  output bias estimation between CDAS and GDAS            
###

  ofile=glbanl.t${cyc}z.pgrba_mdf00                        

  echo "&message"  >input.$nfhrs.$nens
  echo " icstart=${cstart}," >> input.$nfhrs.$nens
  echo " nens='$nens'," >>input.$nfhrs.$nens
  echo " dec_w=0.02," >> input.$nfhrs.$nens
  echo " odate=$odate," >>input.$nfhrs.$nens
  echo "/" >>input.$nfhrs.$nens

  ln -sf $pgbmean   fort.11
  ln -sf $afile     fort.12
  ln -sf $afile_m06 fort.13
  ln -sf $rfile     fort.14
  ln -sf $rfile_m06 fort.15
  ln -sf $ofile     fort.51

  startmsg
  $EXECGEFS/$pgm  <input.$nfhrs.$nens > $pgmout.$nfhrs.${nens} 2> errfile
  export err=$?;err_chk

 done
done
fi

###
#  calculate bias estimation between NCEP and CMC analysis: CMC anl - NCEP anl
###

if [ "$BIASANL" = "YES" ]; then

rm fort.*

for nfhrs in 00; do
 for nens in anl; do

###
#  set the no cold start index as default, 0
###
  cstart=0

###
# gdas NCEP operational analysis file entry
###

  aymdh=$PDYm2$cyc
  aymd=`echo $aymdh | cut -c1-8`
  acyc=`echo $aymdh | cut -c9-10`

  aymdh_m06=`$NDATE -6 $aymdh `
  aymd_m06=`echo $aymdh_m06 | cut -c1-8`
  acyc_m06=`echo $aymdh_m06 | cut -c9-10`

  echo $aymdh $aymd $acyc

  nfile=$COM_GEFS/gefs.${aymd}/${acyc}/pgrba/gec00.t${acyc}z.pgrbaf00
  nfile_m06=$COM_GEFS/gefs.${aymd_m06}/${acyc_m06}/pgrba/gec00.t${acyc_m06}z.pgrbaf06

  if [ -s $nfile ]; then
    echo " "
  else
    echo " There is no Analysis data, Stop! for " $acyc
#   exit
  fi

###
# CMC analysi file entry
###

  cfile=$COM/cmce.${aymd}/${acyc}/pgrba/cmc_gec00.t${acyc}z.pgrbaanl

  if [ $acyc -eq 00 ]; then
    cfile_m12=$COM/cmce.${PDYm3}/12/pgrba/cmc_geavg.t12z.pgrbaf12
  elif [ $acyc -eq 06 ]; then
    cfile_m12=$COM/cmce.${PDYm2}/00/pgrba/cmc_geavg.t00z.pgrbaf06
  elif [ $acyc -eq 12 ]; then
    cfile_m12=$COM/cmce.${PDYm2}/00/pgrba/cmc_geavg.t00z.pgrbaf12
  elif [ $acyc -eq 18 ]; then
    cfile_m12=$COM/cmce.${PDYm2}/12/pgrba/cmc_geavg.t12z.pgrbaf06
  fi

###
#  get initialized bias between NCEP and CMC analysis 
###

  pgbmdf=ncepcmc_glbanl.t${cyc}z.pgrba_mdf00
  pgbmean=glbanl.t${cyc}z.pgrba_meandif

  if [ -s $COM/gefs.$PDYm3/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm3/${cyc}/pgrba/$pgbmdf $pgbmean
  elif [ -s $COM/gefs.$PDYm4/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm4/${cyc}/pgrba/$pgbmdf $pgbmean
  elif [ -s $COM/gefs.$PDYm5/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm5/${cyc}/pgrba/$pgbmdf $pgbmean
  elif [ -s $COM/gefs.$PDYm6/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm6/${cyc}/pgrba/$pgbmdf $pgbmean
  elif [ -s $COM/gefs.$PDYm7/${cyc}/pgrba/$pgbmdf ]; then
    cp $COM/gefs.$PDYm7/${cyc}/pgrba/$pgbmdf $pgbmean
  else
    echo " Cold Start for Bias Estimation between CDAS and GDAS Fcst. Hour " $nfhrs 
    cstart=1
  fi

###
#  output bias estimation between NCEP and CMC analysis   
###

  ofile=ncepcmc_glbanl.t${cyc}z.pgrba_mdf00                        

  echo "&message"  >input.$nfhrs.$nens
  echo " icstart=${cstart}," >> input.$nfhrs.$nens
  echo " nens='$nens'," >>input.$nfhrs.$nens
  echo " dec_w=0.30," >> input.$nfhrs.$nens
  echo " odate=$odate," >>input.$nfhrs.$nens
  echo "/" >>input.$nfhrs.$nens

  ln -sf $pgbmean       fort.11
  ln -sf $nfile         fort.12
  ln -sf $nfile_m06     fort.13
  ln -sf $cfile         fort.14
  ln -sf $cfile_m12     fort.15
  ln -sf $ofile         fort.51

  startmsg
  $EXECGEFS/$pgm  <input.$nfhrs.$nens > $pgmout.$nfhrs.${nens} 2> ercfile
  export err=$?;err_chk

 done
done
fi

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"

if [ "$SENDCOM" = "YES" ]; then
  for nfhrs in $hourlist
  do
    for nens in $memberlist; do
      if [ -s ge${nens}.t${cyc}z.pgrba_mef${nfhrs} ]; then
        mv ge${nens}.t${cyc}z.pgrba_mef${nfhrs} $COMOUT/
      fi
    done
  done
  if [ -s glbanl.t${cyc}z.pgrba_mdf00 ]; then
    mv glbanl.t${cyc}z.pgrba_mdf00 $COMOUT_M2/
  fi
  if [ -s ncepcmc_glbanl.t${cyc}z.pgrba_mdf00 ]; then
    cp ncepcmc_glbanl.t${cyc}z.pgrba_mdf00 $COMOUT_M2/
  fi
fi

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