#!/bin/sh

########################### EXCMCENS ################################
# CMC Ensemble Postprocessing Script Function:
#   calculate relative humidity 
#   convert precipitation to 6h interval accumulations
#   convert precip accumulations to masks
#   create pgrba files directly
#   generate enspost files      
##################################

echo "------------------------------------------------"
echo "CMC Ensemble Postprocessing"
echo "------------------------------------------------"
echo "History: March 2006 - Implementation of this new script."
echo "based on old exenspost"
echo "AUTHOR: Bo Cui    (wx20cb) "
echo "Sept 2004 - Yuejian Zhu - pqpf added"
echo "Sept 2004 - Richard Wobus - igenerate ensemble statistics file"
echo " Dec 2005 - Bo Cui  - new CMC ensemble forecast,384h forecast with 6h interval"

set -x

#---------------------------------------------------
# wait for prep job to generate high resolution data
#---------------------------------------------------
sleep 120

#####################################
# Define Script/Exec Variables
#####################################

export ENSPPF=$USHGLOBAL/global_enspqpf_24h_cmc.sh
export ENSSTAT=$USHGLOBAL/global_ensstat_cmc.sh
export ENSAVGSPR=$USHGLOBAL/cmcens_avgspr.sh

#####################################
# START TO DUMP DATA FOR $cycle CYCLE
#####################################

msg="Starting postprocessing for $cycle Ensemble memebers"
postmsg "$jlogfile" "$msg"

export pgm=cmcens_post
. prep_step

##################################
# Start Processing CMC Analysis at 00z, 06z, 12z and 18z
##################################

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

  iensi=11
  ens2=`echo $iensi | cut -c1-1`
  ens3=`echo $iensi | cut -c2-`

  echo "&message"  >input.ana
  echo " iens2=$ens2,"  >>input.ana
  echo " iens3=$ens3,"  >>input.ana
  echo "/" >>input.ana

###
# set the unit of input and output file 
# unit 11 : cmc analysis data 
# unit 51 : pgrba file
###

  PGB=${DATA_IN}/cmc_gec00.t${cyc}z.pgrbaanl_prep

  ln -sf $PGB fort.11
  ln -sf cmc_gec00.t${cyc}z.pgrbaanl fort.51

###
# check data availability, then start to run
###

  icnt=0
  while [ icnt -le 30 ]; do
    if [ -s $PGB ]; then
      startmsg
      $EXECGLOBAL/$pgm     < input.ana  > $pgmout.ana
      export err=$?;err_chk
      icnt=31
    else
      sleep 10
      icnt=`expr $icnt + 1`
    fi
  done

  rm fort.*

  if [ "$SENDCOM" = "YES" ]; then
    if [ -s cmc_gec00.t${cyc}z.pgrbaanl ]; then
#     cp cmc_gec00.t${cyc}z.pgrbaanl   $COMOUTPGRBA/cmc_gec00.t${cyc}z.pgrbaanl
      cp cmc_gec00.t${cyc}z.pgrbaanl   $COMOUTPGRBAm1/cmc_gec00.t${cyc}z.pgrbaanl
    fi
  fi

fi

##################################
# Begin Processing Ensemble Forecast Data for 00z or 12z cycle
##################################

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

varlboth="z1000 z500 u850 u200 v850 v200 t850 t2m rh700 prmsl"

varlnostat="rain frzr icep snow prcp" 


#export hourlist=" 00  06  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96 \
export hourlist=" 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"

##################################
# Begin Processing Members, Forecast Lead Time
##################################

for file in $varlboth $varlnostat; do
  if [ -e enspostc.${cycle}.${file}hr ]; then
    rm enspostc.${cycle}.${file}hr
  fi
  if [ -e ensstatc.${cycle}.${file}hr ]; then
    rm ensstatc.${cycle}.${file}hr
  fi
  if [ -e enspostc.${cycle}.${file}hri ]; then
    rm enspostc.${cycle}.${file}hri
  fi
  if [ -e ensstatc.${cycle}.${file}hri ]; then
    rm ensstatc.${cycle}.${file}hri
  fi
  if [ -e ensppf.$PDY$cyc              ]; then
    rm ensppf.$PDY$cyc                  
  fi
  for RUNNAME in $memberlist; do
    if [ -e enspostc.ge${RUNNAME}.t${cyc}z.${file}hr ]; then
      rm enspostc.ge${RUNNAME}.t${cyc}z.${file}hr
    fi
  done
done

for FHOUR in $hourlist; do
  for RUNNAME in $memberlist; do

  case $RUNNAME in
    c00) iensi=12;echo $iensi;;
    p01) iensi=31;echo $iensi;;
    p02) iensi=32;echo $iensi;;
    p03) iensi=33;echo $iensi;;
    p04) iensi=34;echo $iensi;;
    p05) iensi=35;echo $iensi;;
    p06) iensi=36;echo $iensi;;
    p07) iensi=37;echo $iensi;;
    p08) iensi=38;echo $iensi;;
    p09) iensi=39;echo $iensi;;
    p10) iensi=310;echo $iensi;;
    p11) iensi=311;echo $iensi;;
    p12) iensi=312;echo $iensi;;
    p13) iensi=313;echo $iensi;;
    p14) iensi=314;echo $iensi;;
    p15) iensi=315;echo $iensi;;
    p16) iensi=316;echo $iensi;;
  esac

    ens2=`echo $iensi | cut -c1-1`
    ens3=`echo $iensi | cut -c2-`

    echo "&message"  >input.$RUNNAME.$FHOUR
    echo " iens2=$ens2,"  >>input.$RUNNAME.$FHOUR
    echo " iens3=$ens3,"  >>input.$RUNNAME.$FHOUR
    echo "/" >>input.$RUNNAME.$FHOUR

    PGB=${DATA_IN}/cmc_ge${RUNNAME}.t${cyc}z.pgrbaf${FHOUR}_prep

###
# set the unit of input file 
# unit 11 -    : cmc forecasts input
# unit 12 - 16 : temporary files for arain afrzr aicep asnow prcp
###

    ln -sf $PGB fort.11

    if [ $FHOUR -ge 12 ]; then   
      PFHOUR=`expr $FHOUR - 06`
      if [ $PFHOUR -le 9 ]; then
	PFHOUR=0$PFHOUR
      fi
      ln -sf temp_arain_ge${RUNNAME}.f${PFHOUR} fort.12
      ln -sf temp_afrzr_ge${RUNNAME}.f${PFHOUR} fort.13
      ln -sf temp_aicep_ge${RUNNAME}.f${PFHOUR} fort.14
      ln -sf temp_asnow_ge${RUNNAME}.f${PFHOUR} fort.15
      ln -sf temp_prcp_ge${RUNNAME}.f${PFHOUR}  fort.16
    fi

###
# set the unit of output pgrba file 
# unit 51 -    : pgrba file
# unit 52 - 56 : temporary files for arain afrzr aicep asnow prcp
###

    ln -sf cmc_ge${RUNNAME}.t${cyc}z.pgrbaf${FHOUR} fort.51
    if [ $FHOUR -ge 06 ]; then
      ln -sf temp_arain_ge${RUNNAME}.f${FHOUR} fort.52
      ln -sf temp_afrzr_ge${RUNNAME}.f${FHOUR} fort.53
      ln -sf temp_aicep_ge${RUNNAME}.f${FHOUR} fort.54
      ln -sf temp_asnow_ge${RUNNAME}.f${FHOUR} fort.55
      ln -sf temp_prcp_ge${RUNNAME}.f${FHOUR}  fort.56
    fi

###
# set the unit of enspost* file
# unit 61 - 70 : z1000 z500 u850 u200 v850 v200 t850 t2m rh700 prmsl
###

    iunit=60
    for file in $varlboth; do
      iunit=`expr $iunit + 1`
      ln -sf enspostc.ge${RUNNAME}.t${cyc}z.f${FHOUR}.${file}hr fort.$iunit
    done

###
# set the unit of enspost* file
# unit 71 - 75 : rain frzr icep snow prcp
###

    iunit=70
    if [ $FHOUR -ge 06 ]; then
      for file in $varlnostat; do
      iunit=`expr $iunit + 1`
      ln -sf enspostc.ge${RUNNAME}.t${cyc}z.f${FHOUR}.${file}hr fort.$iunit
      done
    fi

###
# check data availability, then start to run
###
    icnt=0
    while [ icnt -le 30 ]; do
      if [ -s $PGB ]; then
        startmsg
        $EXECGLOBAL/$pgm     < input.$RUNNAME.$FHOUR  > $pgmout.ge${RUNNAME}.f${FHOUR}
        export err=$?;err_chk
        icnt=31

        if [ "$SENDCOM" = "YES" ]; then
          if [ -s cmc_ge${RUNNAME}.t${cyc}z.pgrbaf${FHOUR} ]; then
            #cp cmc_ge${RUNNAME}.t${cyc}z.pgrbaf${FHOUR} $COMOUTPGRBA/cmc_ge${RUNNAME}.t${cyc}z.pgrbaf${FHOUR}
            mv cmc_ge${RUNNAME}.t${cyc}z.pgrbaf${FHOUR} $COMOUTPGRBA/cmc_ge${RUNNAME}.t${cyc}z.pgrbaf${FHOUR}
	    if [ "$SENDDBN" = "YES" ] ; then
	      $DBNROOT/bin/dbn_alert MODEL CMCENS_PGBA $job $COMOUTPGRBA/cmc_ge${RUNNAME}.t${cyc}z.pgrbaf${FHOUR}
	    fi
          fi
        fi

        for file in $varlboth; do
          cat enspostc.ge${RUNNAME}.t${cyc}z.f${FHOUR}.${file}hr >> enspostc.ge${RUNNAME}.t${cyc}z.${file}hr
          rm enspostc.ge${RUNNAME}.t${cyc}z.f${FHOUR}.${file}hr
        done

        if [ $FHOUR -ge 06 ]; then
          for file in $varlnostat; do
            cat enspostc.ge${RUNNAME}.t${cyc}z.f${FHOUR}.${file}hr >> enspostc.ge${RUNNAME}.t${cyc}z.${file}hr
            rm enspostc.ge${RUNNAME}.t${cyc}z.f${FHOUR}.${file}hr
          done
        fi

        rm fort.* 

      else
        sleep 10
        icnt=`expr $icnt + 1`
      fi
    done
  done

  #-------------------------------------
  #  release the debias job at hour 192
  #-------------------------------------
  if [ $FHOUR -eq 192 ]; then
     $SMSBIN/setev release_debias
  fi
done

###
# cat the enspostc* file (one variable) of all members to one file
###

for RUNNAME in $memberlist; do
  for file in $varlboth $varlnostat; do
    if [ -s enspostc.ge${RUNNAME}.t${cyc}z.${file}hr ]; then
      cat enspostc.ge${RUNNAME}.t${cyc}z.${file}hr >> enspostc.t${cyc}z.${file}hr
    fi
  done
done

###
# copy output enspostc files
###

for file in $varlboth $varlnostat 
do
  if [ "$SENDCOM" = "YES" ]; then
    if [ -s enspostc.${cycle}.${file}hr ]; then
      cp enspostc.${cycle}.${file}hr  $COMOUTENST/enspostc.${cycle}.${file}hr
###      if [ "$SENDDBN" = "YES" ]; then
###        $DBNROOT/bin/dbn_alert MODEL ENS_POST_CMC $job $COMOUTENST/enspostc.${cycle}.${file}hr
###      fi
    fi
  fi
done

###
#  we add a script to calculate ensemble mean and spread
###
                                                                                                             
for nfhrs in $hourlist
do
  export HRLIST=$nfhrs
  $ENSAVGSPR
  if [ "$SENDCOM" = "YES" ]; then
    if [ -s cmc_geavg.t${cyc}z.pgrbaf$nfhrs ]; then
      mv cmc_geavg.t${cyc}z.pgrbaf$nfhrs  $COMOUTPGRBA/cmc_geavg.t${cyc}z.pgrbaf$nfhrs
      if [ "$SENDDBN" = "YES" ] ; then
         $DBNROOT/bin/dbn_alert MODEL CMCENS_PGBA $job $COMOUTPGRBA/cmc_geavg.t${cyc}z.pgrbaf$nfhrs
      fi
    fi
    if [ -s cmc_gespr.t${cyc}z.pgrbaf$nfhrs ]; then
      mv cmc_gespr.t${cyc}z.pgrbaf$nfhrs  $COMOUTPGRBA/cmc_gespr.t${cyc}z.pgrbaf$nfhrs
      if [ "$SENDDBN" = "YES" ] ; then
         $DBNROOT/bin/dbn_alert MODEL CMCENS_PGBA $job $COMOUTPGRBA/cmc_gespr.t${cyc}z.pgrbaf$nfhrs
      fi
    fi
  fi
done
                                                                                                             
###
###
# calculate PQPF (named ensppf ) for each 24 hours period, for 17 ens members 
###

if test "$cycle" = "t00z"
then
  $ENSPPF enspostc.t${cyc}z.prcphr ensppf.$PDY$cyc
  if [ $SENDCOM = "YES" ]; then
    if [ -s ensppf.$PDY$cyc ]; then
      cp ensppf.$PDY$cyc $COMOUTENST/ensstatc.$cycle.pqpfhr_24h
###      if [ "$SENDDBN" = "YES" ]; then
###        $DBNROOT/bin/dbn_alert MODEL ENS_STAT_CMC $job $COMOUTENST/ensstatc.${cycle}.pqpfhr_24h
###      fi
    fi
  fi
fi

###
# create ensemble statistics files
###

export HHRLIST="$hourlist"
export MEMLIST="$memberlist"
export VARLIST="$varlboth prcp"

$ENSSTAT

if [ "$SENDCOM" = "YES" ]; then
  for var in $VARLIST
  do
    cfosg=ensstatc.$cycle.${var}hr
    if [ -s $cfosg ]; then
      cp $cfosg $COMOUTENST/$cfosg
###      if [ "$SENDDBN" = "YES" ] ; then
###	$DBNROOT/bin/dbn_alert MODEL ENS_STAT_CMC $job $COMOUTENST/$cfosg
###      fi
    fi
  done
fi

#############################################
# Run extrkr.sh to compute the TC storm
# tracks for each of the 16 member forecasts,
# then run ens_trak_ave.sh to compute the
# Canadian ensemble mean forecast track.
#############################################
if [ ${RUN_TCTRACK} = 'YES' ]
then
    
  export cmodel=cens
  for pert in p01 p02 p03 p04 p05 p06 p07 p08    \
              p09 p10 p11 p12 p13 p14 p15 p16 c00
  do
    export pert
    sh ${utilscript}/extrkr.sh
  done

  export cmodel=cens
  sh ${utilscript}/ens_trak_ave.sh

fi

###
# end of 00z and 12z cycle only
#
fi

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