
########################### CMCENS_DEBIAS ###########################
echo "------------------------------------------------"
echo "Bias Correct CMC Global Ensemble"
echo "------------------------------------------------"
echo "History: March 2006 - First implementation of this new script."
echo "AUTHOR: Bo Cui  (wx20cb)"

### To submit this job for T00Z and T12Z

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

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

export NDATE=$EXECUTIL/ndate
export ENSANOMALY=$USHCMCE/cmce_climate_anomaly.sh
export ENSWEIGHTS=$USHCMCE/cmce_weights.sh
export ENSAVGSPR=$USHCMCE/cmce_bc_avgspr.sh

export pgm=cmcens_debias
. prep_step

###    
################################################################
# define the days for searching bias estimation backup : YMD1 to YMD16                         
#   ymdh :   time that ens. fcst. will be bias corrected, $PDY$cyc
################################################################
###

export 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`

###    
################################################################
# bias correct NCEP global ensemble for each forecast time and each member
################################################################
###    
export 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"

export memberlist="c00 p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20"

for nfhrs in $hourlist
do
  echo "&option"  >input1.$nfhrs
  echo " idate=$ymdh,"  >>input1.$nfhrs
  echo " ifhr=$nfhrs,"  >>input1.$nfhrs
  echo "/" >>input1.$nfhrs

  for nens in $memberlist
  do
###
#  set the index ( exist of bias estimation )  as default, 0
###

    cstart=0

###
#  bias estimation entry
###

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

    if [ -s $COM/cmce.$PDY/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDY/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm1/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm1/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm2/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm2/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm3/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm3/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm4/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm4/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm5/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm5/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm6/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm6/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm7/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm7/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm8/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm8/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm9/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm9/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm10/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm10/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm11/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm11/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm12/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm12/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm13/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm13/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm14/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm14/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm15/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm15/${cyc}/pgrba/$pgb $pgb
    elif [ -s $COM/cmce.$PDYm16/${cyc}/pgrba/$pgb ]; then
      cp $COM/cmce.$PDYm16/${cyc}/pgrba/$pgb $pgb
    else
      echo " There is no Bias Estimation at " ${nfhrs}H " For Member " $nens 
      cstart=1
    fi

    echo "&files"   >input2.$nfhrs.$nens
    echo " ifile1='$pgb'," >> input2.$nfhrs.$nens

###
#  forecasting file entry
###
    pgb=$COMIN/cmc_ge${nens}.t${cyc}z.pgrbaf${nfhrs} 
    echo " ifile2='$pgb'," >> input2.$nfhrs.$nens

###
#  bias corrected ensemble forecasting output
###

    pgb=cmc_ge${nens}.t${cyc}z.pgrba_bcf${nfhrs}
    echo " ofile1='$pgb'," >> input2.$nfhrs.$nens
    echo " icstart=$cstart," >> input2.$nfhrs.$nens

    echo "/" >>input2.$nfhrs.$nens

    cat input1.$nfhrs input2.$nfhrs.$nens >input.$nfhrs.$nens

###
#  check available pgrba forecast file
###
   
    pgb=$COMIN/cmc_ge${nens}.t${cyc}z.pgrbaf${nfhrs} 

    icnt=0
    while [ icnt -le 12 ]; do
      if [ -s $pgb ]; then
        startmsg
        $EXECCMCE/$pgm          <input.$nfhrs.$nens > $pgmout.$nfhrs.$nens
        export err=$?;err_chk

        export MEMLIST=$nens
        $ENSANOMALY $PDY$cyc $nfhrs

        $ENSWEIGHTS $PDY$cyc $nfhrs

        icnt=13

#### sendcom  bc and an forecast, ensemble member weights

        if [ "$SENDCOM" = "YES" ]; then
          if [ -s cmc_ge${nens}.t${cyc}z.pgrba_anf$nfhrs ]; then  
            mv cmc_ge${nens}.t${cyc}z.pgrba_anf$nfhrs $COMOUTAN/cmc_ge${nens}.t${cyc}z.pgrba_anf${nfhrs}  
          fi
          if [ -s cmc_ge${nens}.t${cyc}z.pgrba_bcf$nfhrs ]; then  
            mv cmc_ge${nens}.t${cyc}z.pgrba_bcf$nfhrs $COMOUTBC/cmc_ge${nens}.t${cyc}z.pgrba_bcf${nfhrs}  
	  fi
          if [ -s cmc_ge${nens}.t${cyc}z.pgrba_wtf$nfhrs ]; then  
            mv cmc_ge${nens}.t${cyc}z.pgrba_wtf$nfhrs $COMOUTWT/cmc_ge${nens}.t${cyc}z.pgrba_wtf${nfhrs}  
          fi
        fi

      else

        sleep 10
        icnt=`expr $icnt + 1`
	echo $icnt

      fi
    done

  done
done

###
#  final check up and make up
###

for nfhrs in $hourlist
do
  for nens in $memberlist
  do
    if [ ! -s $COMOUTBC/cmc_ge${nens}.t${cyc}z.pgrba_bcf$nfhrs ]; then  
      startmsg
      $EXECCMCE/$pgm          <input.$nfhrs.$nens > $pgmout.$nfhrs.$nens
      export err=$?;err_chk
      if [ "$SENDCOM" = "YES" ]; then
        if [ -s cmc_ge${nens}.t${cyc}z.pgrba_bcf$nfhrs ]; then  
          cp cmc_ge${nens}.t${cyc}z.pgrba_bcf$nfhrs $COMOUTBC/cmc_ge${nens}.t${cyc}z.pgrba_bcf${nfhrs}  
        fi
      fi
    fi
    if [ ! -s $COMOUTAN/cmc_ge${nens}.t${cyc}z.pgrba_anf$nfhrs ]; then  
      export MEMLIST=$nens
      $ENSANOMALY $PDY$cyc $nfhrs
      if [ "$SENDCOM" = "YES" ]; then
        if [ -s cmc_ge${nens}.t${cyc}z.pgrba_anf$nfhrs ]; then  
          mv cmc_ge${nens}.t${cyc}z.pgrba_anf$nfhrs $COMOUTAN/cmc_ge${nens}.t${cyc}z.pgrba_anf${nfhrs}  
        fi
      fi
    fi
    if [ ! -s $COMOUTWT/cmc_ge${nens}.t${cyc}z.pgrba_wtf$nfhrs ]; then  
      export MEMLIST=$nens
      $ENSWEIGHTS $PDY$cyc $nfhrs
      if [ "$SENDCOM" = "YES" ]; then
        if [ -s cmc_ge${nens}.t${cyc}z.pgrba_wtf$nfhrs ]; then  
          mv cmc_ge${nens}.t${cyc}z.pgrba_wtf$nfhrs $COMOUTWT/cmc_ge${nens}.t${cyc}z.pgrba_wtf${nfhrs}  
        fi
      fi
    fi

  done
###
#  we add a script to calculate ensemble mean ans spread
###
  export HRLIST=$nfhrs
  $ENSAVGSPR
  if [ "$SENDCOM" = "YES" ]; then
    if [ -s cmc_geavg.t${cyc}z.pgrba_bcf$nfhrs ]; then
      mv cmc_geavg.t${cyc}z.pgrba_bcf$nfhrs  $COMOUTBC/cmc_geavg.t${cyc}z.pgrba_bcf$nfhrs
    fi
    if [ -s cmc_gespr.t${cyc}z.pgrba_bcf$nfhrs ]; then
      mv cmc_gespr.t${cyc}z.pgrba_bcf$nfhrs  $COMOUTBC/cmc_gespr.t${cyc}z.pgrba_bcf$nfhrs
    fi
  fi
###
done

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