########################### BIASUPDATE ############################################################
echo "-------------------------------------------------------------"
echo "Update Bias Estimation of CMC Global Ensemble Forecast Daily"
echo "-------------------------------------------------------------"
echo "History: May 2006 - First implementation of this new script."
echo "         Dec 2006 - Modified script for CMC system upgrade (20 members)"
echo "         Oct 2010 - Add 15 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=cmcens_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`
                                                                                                             
###
################################################################
### 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

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

if [ $cyc -eq 00 -o $cyc -eq 12 ]; then

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

for nens in $memberlist; do

  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"

  for nfhrs in $hourlist; do

###
# analysis files entry
###

  fymdh=${PDYm2}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`

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

###
# set the forecast file as analysis for ULWRF
# CMC ensemble has no 06z and 18z cycle forecasts
###

  if [ $acyc -eq 00 ]; then
    if [ $cyc -eq 00 ]; then
      afile_m12=$COM/cmce.$PDYm3/12/pgrba/cmc_ge$nens.t12z.pgrbaf12
      if [ $nfhrs -eq 00 ]; then
        afile_m12=$COM/cmce.$PDYm2/00/pgrba/cmc_ge$nens.t00z.pgrbaf00
      fi
    fi
    if [ $cyc -eq 12 ]; then
      afile_m12=$COM/cmce.$PDYm2/12/pgrba/cmc_ge$nens.t12z.pgrbaf12
    fi
  elif [ $acyc -eq 06 ]; then
    if [ $cyc -eq 00 ]; then
      afile_m12=$COM/cmce.$PDYm2/00/pgrba/cmc_ge$nens.t00z.pgrbaf06
    fi
    if [ $cyc -eq 12 ]; then
      afile_m12=$COM/cmce.$PDYm1/00/pgrba/cmc_ge$nens.t00z.pgrbaf06
    fi
  elif [ $acyc -eq 12 ]; then
    afile_m12=$COM/cmce.$PDYm2/00/pgrba/cmc_ge$nens.t00z.pgrbaf12
    if [ $nfhrs -eq 00 ]; then
      afile_m12=$COM/cmce.$PDYm2/12/pgrba/cmc_ge$nens.t12z.pgrbaf00
    fi
  elif [ $acyc -eq 18 ]; then
    afile_m12=$COM/cmce.$PDYm2/12/pgrba/cmc_ge$nens.t12z.pgrbaf06
  fi

###

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

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

###
# forecast files entry
###

  cfile=$COM/cmce.${fymd}/${cyc}/pgrba/cmc_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=cmc_ge${nens}.t${cyc}z.pgrba_mef${nfhrs}
  pgbme=bias.ge${nens}.t${cyc}z.f$nfhrs

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

#################################################################
#  input bias estimation 6h ago for tmax and tmin bias estimation
#################################################################

  if [ $nfhrs -ge 06 ]; then

    nfhrsm06=`expr $nfhrs - 06`
    if [ $nfhrsm06 -le 09 ]; then
      bias_m06=cmc_ge${nens}.t${cyc}z.pgrba_mef0$nfhrsm06
    else
      bias_m06=cmc_ge${nens}.t${cyc}z.pgrba_mef$nfhrsm06
    fi

  fi

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

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

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

  echo "&message"  >input.$nfhrs.$nens
  echo " icstart=${cstart}," >> input.$nfhrs.$nens
  echo " nens='$nens'," >>input.$nfhrs.$nens
  echo " ifhr=$nfhrs," >>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_m12 fort.13
  ln -sf $cfile     fort.14
  if [ $nfhrs -ge 06 ]; then
    ln -sf $bias_m06  fort.15
  fi
  ln -sf $ofile     fort.51

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

 done
done

fi
fi

###
#  calculate bias estimation between CDAS and CMC analysis    
###

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

###
# CMC 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`
                                                           
  afile=$COM/cmce.${aymd}/${acyc}/pgrba/cmc_gec00.t${acyc}z.pgrbaanl

  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=cmc_glbanl.t${cyc}z.pgrba_mdf00
  pgbmean=glbanl.t${cyc}z.pgrba_meandif

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

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

  ofile=cmc_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

# rm $pgbmean $afile $rfile $rfile_m06 $ofile fort.*

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

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

 done
done

fi

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

cat $pgmout.00.mdf 
cat $pgmout.00.c00 
cat $pgmout.24.p01 



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