#!/bin/sh
###############################################################################
#                                                                             #
# This script develops the bulk statistics from the individual ensemble runs  #
# It is run as a child script by a main script.                               #
#                                                                             #
# Remarks :                                                                   #
#   - The necessary files are retreived by the mother script                  #
#   - The script is called once for each time stamp                           #
#   - This script runs in the work directory designated in the mother script  #
#   - A directory is created in the work directory which is removed on        #
#     successful completion                                                   #
#                                                                             #
#                                                                 June 2008   #
#                                                                             #
###############################################################################

# --------------------------------------------------------------------------- 
# 0. Initialization
# 0.a Basic modes

  seton='+xa'
  setoff='+xa'
  set $seton

  cd $DATA

  hour=$1 
  ihr=$2

  rm -rf ens_$hour
  mkdir ens_$hour
  
  cd ens_$hour

  set $setoff
  echo ' '
  echo '+------------------------------+'
  echo '!   Generate Ensemble Stats.   |'
  echo '+------------------------------+'
  echo "         Hour : $hour"
  set $seton

# 0.b The tested variables should be exported by the calling script.

  if [ -z "$EXECwave" ] || [ -z "$utilexec" ] || \
     [ -z "$memb" ] || [ -z "$cycle" ] 
  then
    set $setoff
    echo ' '
    echo '**********************************************************'
    echo '*** EXPORTED VARIABLES NOT SET in multiwave_ensemble.sh***'
    echo '**********************************************************'
    echo ' '
    exit 1
    set $seton
  fi

#0.c Setting up local variables
  
  para='WIND HTSGW PERPW'
  uscale='3.60 5.65 8.74 11.31 14.39 17.48 21.07 24.67'
  hscale='0.60 1.00 2.00  3.00  4.00  5.50  7.00  9.00'
  tpscale='5.0 7.0 9.0 11.0 13.0 15.0 17.0 19.0'
  tmscale=''
  tsscale=''

# --------------------------------------------------------------------------- #
# 1. Set the links  

  rm -f *.grib2

  for im in $memb
  do 
    ln -s ../$im.grib2 .
  done

  ln -sf ../buoy_file.data .
  ln -sf ../buoy_ratio.data .

# --------------------------------------------------------------------------- #
# 2.  Computing the statistics

  rm -f station.$cycle.f$hour
  rm -f mean.$cycle.f$hour
  rm -f spread.$cycle.f$hour
  rm -f probab.$cycle.f$hour

  set $setoff
  echo ' '
  echo '   Running stats ...'
  set $seton

# 2.a Loop over the fields

  for ip in $para
  do

    scale='     '
    case $ip in
      WIND)    scale=$uscale ; id_para='32'   ;;
      HTSGW)   scale=$hscale ; id_para='100'  ;;
      WVPER)   scale=$tmscale; id_para='103'  ;;
      PERPW)   scale=$tpscale; id_para='108'  ;;
      PERSW)   scale=$tsscale; id_para='110'  ;;
      *)       scale=$tsscale; id_para='   '  ;;
    esac

    rm -f fname_input data_*

    echo $YMDH $hour $ip $id_para  > fname_input
    echo $memb | wc -w            >> fname_input
    echo $memb                    >> fname_input
    echo $scale | wc -w           >> fname_input
    echo $scale                   >> fname_input

# 2.b Loop over the members

    for im in $memb
    do

      infile=$im.grib2

# 2.b.i Convert to grib1

      $utilexec/wgrib2 $infile -s | grep ":${ip}:surface:${ihr}" | $utilexec/wgrib2 -i $infile -bin data_$im
      ls -la data_$im
      ok1=$?
      if [ $ok1 -ne 0 ] ; then
        echo " *** ERROR : ip=$ip, im=$im, ok1=$ok1"
        exit
      fi
      echo data_$im       >> fname_input

    done

# 2.c Execute multiwave_ensemble

    rm -f station_out mean_out spread_out probab_out test_out

    $EXECwave/multiwave_ensemble  < fname_input 

    cat ./station_out >> station.$cycle.text.$hour
    cat ./mean_out   >> mean.$cycle.grib.$hour
    cat ./spread_out >> spread.$cycle.grib.$hour
    cat ./probab_out >> probab.$cycle.grib.$hour

  done

# --------------------------------------------------------------------------- #
# 3. Move files and Clean up

  mv station.$cycle.text.$hour ../. 
  mv mean.$cycle.grib.$hour ../.
  mv spread.$cycle.grib.$hour ../.
  mv probab.$cycle.grib.$hour ../.

  rm -f *.grib2
  
  set $setoff
  echo ' '
  echo "   Removing work directory after success."
  set $seton

  cd ../
  rm -rf ens_$hour

  set $setoff
  echo ' '
  echo 'End of multiwave_ensemble.sh'
  date

#-------------------End of multiwave_ensemble.sh----------------------------------#
