#!/bin/sh
###############################################################################
#                                                                             #
# This script generates a blended GFS GFDL wind field.                        #
#                                                                             #
# Remarks :                                                                   #
# - This script runs in the work directory designated in the mother script in #
#   which it generates its own sub-directory 'hur_yyyymmddhh'. If all is well #
#   the derectory is removed at the end of the script.                        #
# - The time group yyyymmddhh is the first parameter passed to the script.    #
# - See section 0.c for variables that need to be set.                        #
#                                                                             #
#                                                                April 2003   #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

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

  rm -rf hur_$1
  mkdir hur_$1
  cd hur_$1

  set $setoff
  echo ' '
  echo '+--------------------------------+'
  echo '! Blend GFDL and GFS wind field  |'
  echo '+--------------------------------+'
  set $seton

  pmax=1015.0           # defined maximum sea level pressure of storm
  rmax=12.0             # defined maximum radius of storm in deg
  rmin=3.0              # defined minimum radius of storm in deg
# vmin=7.5              # defined minimum wind speed of a storm in m/s
  vmin=10.5              # defined minimum wind speed of a storm in m/s

# 0.b Check if time set

  if [ "$#" -lt '1' ]
  then
    set $setoff
    echo ' '
    echo '************************************'
    echo '*** TIME IN wavehur2p.sh NOT SET ***'
    echo '************************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" " TIME IN wavehur2p.sh NOT SET"
    exit 1
  else
    ymdh=$1
    set $setoff
    echo "   Time : $ymdh"
    echo ' '
    set $seton
  fi

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

  if [ -z "$utilexec" ] || [ -z "$YMDH" ] || [ -z "$date" ] || \
     [ -z "$cycle" ] || [ -z "$wavelog" ] 
  then
    set $setoff
    echo ' '
    echo '**********************************'
    echo '*** EXPORTED VARIABLES NOT SET ***'
    echo '**********************************'
    echo ' '
    postmsg "$jlogfile" " EXPORTED VARIABLES NOT SET."
    exit 2
    set $seton
  fi

# 0.d Link to files from mother directory

  ln -s ../wind.ww3 .

# --------------------------------------------------------------------------- #
# 1. Get GFS wind field from ENP file

  set $setoff
  echo "   Extracting GFS wind from ENP file ..."
  echo ' '
  set $seton

  time="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"

  echo $time | $EXECwave/wave_nphgfs > wave_nphgfs.out 2>&1
  OK=$?

  if [ "$OK" != '0' ] || [ ! -f gfs.wind ] || [ ! -f gfs.astd ]
  then
    cat wave_nphgfs.out
    set $setoff
    echo ' '
    echo '   **************************** '
    echo '   *** ERROR IN wave_nphgfs *** '
    echo '   **************************** '
    echo ' '
    exit 3
  fi

  rm -f wave_nphgfs.out wind.ww3

# --------------------------------------------------------------------------- #
# 2.  Look for GDFDL files.

  set $setoff
  echo "   Get GFDL files ..."
  set $seton

# 2.a Get GFDL run cycle and forecast hour

  YMDH_hur=$YMDH
  until [ "$YMDH_hur" -le "$ymdh" ]
  do
    YMDH_hur=`$utilexec/ndate -6 $YMDH_hur`
  done

  date_hur=`echo $YMDH_hur | cut -c1-8`
  cyc_hur=`echo $YMDH_hur | cut -c9-10`
  cycle_hur=t${cyc_hur}z

  fh_hur=0
  step=1

  until [ "`$utilexec/ndate $fh_hur $YMDH_hur`" = "$ymdh" ] 
  do
    fh_hur=`expr $fh_hur + $step`
  done
  fhc=$fh_hur

  if [ "$fh_hur" -lt '100' ] ; then
    fh_hur="0$fh_hur" ; fi
  if [ "$fh_hur" -lt '10' ] ; then
    fh_hur="0$fh_hur" ; fi
  fh_hur="f$fh_hur"

  set $setoff
  echo "      Using $date_hur $cycle_hur cycle ${fh_hur}h forecast."
  set $seton

# 2.b Get number of hurricanes and names

  names=
  files=
  nr_hur=0

  if [ -d $COMGFDL/hur.$YMDH_hur ]
  then
    cd $COMGFDL/hur.$YMDH_hur
    files=`ls *.gfdl_omb_ocean.tar 2> /dev/null | sort `
    cd $DATA/hur_$1
  fi

  if [ -n "$files" ]
  then
    for file in $files
    do
      stormID=`echo $file | sed 's/\./ /g' | awk '{ print $1 }'`
      nc=`echo $stormID | wc -c`
      nc=`expr $nc - 1`
      testID=`echo $stormID | cut -c$nc-$nc`
      if [ "$testID" = 'e' ]
      then
        nr_hur=`expr $nr_hur + 1`
        names="$names $stormID"
      fi
    done
  fi

  set $setoff
  echo "      $ymdh : $nr_hur storm(s) :$names"
  set $seton

# 2.c Get data files

  if [ "$nr_hur" != '0' ]
  then
    set $setoff
    echo "      Get data files ..."
    set $seton
    mkdir gfdl_data

    for name in $names
    do
      cp $COMGFDL/hur.$YMDH_hur/$name.$YMDH_hur.gfdl_omb_ocean.tar ./gfdl_data/. 2> /dev/null
      cp $COMGFDL/hur.$YMDH_hur/$name.$YMDH_hur.stats.complete     ./$name.stats 2> /dev/null

      if [ ! -s ./gfdl_data/$name.$YMDH_hur.gfdl_omb_ocean.tar ] || \
         [ ! -s $name.stats ]
      then
        set $setoff
        echo ' '
        echo "      *** Data for $name.$YMDH_hur not FOUND ***"
        echo "      *** Will ignore storm $name ***"
        echo ' '
        set $seton
        rm -f $name.*
        names=`echo "$names" | sed "s/$name//g"`
        nr_hur=`expr $nr_hur - 1`
        echo "$modID prep $date $cycle : Data missing $name $ymdh." >> $wavelog
      fi

      fhours="`wc -l $name.stats | awk '{ print $1}'`"
      if [ "`expr $fhours + 1`" = "$fhc" ]
      then
        echo "$modID prep $date $cycle : No data for $name past ${fhours}h." >> $wavelog
      fi
      if [ "$fhours" -lt "$fhc" ]
      then
        rm -f $name.*
        names=`echo "$names" | sed "s/$name//g"`
        nr_hur=`expr $nr_hur - 1`
      fi
    done

  fi

# --------------------------------------------------------------------------- #
# 3.  Putting individual hurricanes together
# 3.a Basic looping

  if [ "$nr_hur" != '0' ]
  then
    i_hur=0

    for name in $names
    do
      i_hur=`expr $i_hur + 1`
      set $setoff
      echo ' '
      echo "   Making wind fields for $name ($i_hur) ..."

# 3.b Unpacking the data

      echo '      Unpakking the GFDL data ...'
      set $seton

      cd gfdl_data
      tar -xf $name.$YMDH_hur.gfdl_omb_ocean.tar   2> /dev/null
      mv *.gfdl_omb_ocean.$fh_hur ../$name.data    2> /dev/null
      rm -f $name.*
      cd ..

      if [ ! -f $name.data ] || [ ! -f $name.stats ]
      then
        set $setoff
        echo ' '
        echo "      *** Data for $name.$YMDH_hur $fh_hur not FOUND ***"
        echo "      *** Will ignore storm $name ***"
        echo ' '
        set $seton
        rm -f $name.*
        names=`echo "$names" | sed "s/$name//g"`
        nr_hur=`expr $nr_hur - 1`
        i_hur=`expr $i_hur - 1`
        echo "$modID prep $date $cycle : Data missing $name $YMDH_hur $fh_hur." >> $wavelog
      else

# 3.c Converting to wave model grid

        set $setoff
        echo '      Converting to wave model grid ...'
        set $seton

        ln -sf $name.data        fort.10
        ln -sf storm$i_hur.nph   fort.53

        echo $fhc | $EXECwave/wave_nphgfdl > wave_nphgfdl.out 2>&1
        OK=$?

        if [ "$OK" != '0' ] || [ ! -f storm$i_hur.nph ]
        then
          set $setoff
          echo ' '
          echo "      *** wave_nphgfdl failed for $name ***"
          echo "      *** Will ignore storm $name ***"
          echo ' '
          set $seton
          rm -f $name.*
          names=`echo "$names" | sed "s/$name//g"`
          nr_hur=`expr $nr_hur - 1`
          i_hur=`expr $i_hur - 1`
          echo "$modID prep $date $cycle : wave_nphgfdl failed $name $YMDH_hur $fh_hur." >> $wavelog
        else
          rm -f fort.*
          rm -f wave_nphgfdl.out
          rm -f $name.data
          mv $name.stats storm$i_hur.stats

          set $setoff
          echo "         $name is now storm$i_hur ..."
          set $seton
        fi

      fi
    done
  fi

  rm -rf gfdl_data

# --------------------------------------------------------------------------- #
# 4.  Blending GFS and GFDL
# 4.a Basic looping

  if [ "$nr_hur" != '0' ]
  then
    set $setoff
    echo ' '
    echo "   Blending GFS and GFDL field(s) ..."
    echo "      Hooking up GFS and output ..."
    set $seton

    ln -sf gfs.wind          fort.15
    ln -sf huruv             fort.71

    i_hur=0
    while [ "$i_hur" -lt "$nr_hur" ]
    do
      i_hur=`expr $i_hur + 1`
      set $setoff
      echo "      Hooking up storm$i_hur ..."
      set $seton
      in1=`expr $i_hur + 20`
      in2=`expr $i_hur + 30`
      ln -sf storm$i_hur.nph   fort.$in1
      ln -sf storm$i_hur.stats fort.$in2
    done

    set $setoff
    echo "      Running wave_nphblend ..."
    set $seton

    echo "$YMDH_hur $fhc $nr_hur $pmax $rmax $vmin $rmin" | \
        $EXECwave/wave_nphblend > wave_nphblend.out 2>&1
    OK="$?"

    if [ "$OK" != '0' ] || [ ! -f huruv ]
    then
      cat wave_nphblend.out
      set $setoff
      echo ' '
      echo "      *** wave_nphblend failed ***"
      echo ' '
      set $seton
      echo "$modID prep $date $cycle : wave_nphblend failed $YMDH_hur $fh_hur." >> $wavelog
      rm -f huruv
    else
      rm -f fort.*
      rm -f wave_nphblend.out
      rm -f storm*.nph storm*.stats
    fi

  fi

# --------------------------------------------------------------------------- #
# 5.  Put correct file back in mother directory and clean up

  outfile="../ghur.$ymdh"

  set $setoff
  echo ' '
  echo "   Put file in mother directory as $outfile ..."
  echo "      $ymdh : $nr_hur GFDL run(s) :$names"
  set $seton

  if [ -f huruv ]
  then
    used=huruv
    mv huruv $outfile 
    rm -f gfs.wind
  else
    used=gfs.wind
    mv gfs.wind $outfile
  fi
  cat gfs.astd >> $outfile
  rm -f gfs.astd

  set $setoff
  echo "      Done, used : $used"
  echo "      Cleaning up ..."
  set $seton

# --------------------------------------------------------------------------- #
# 6.  Clean up the directory

  set $setoff
  echo ' '
  echo "   Removing work directory after success."
  set $seton

  cd ..
  rm -rf hur_$ymdh

  set $setoff
  echo ' '

# End of wavehur2p.sh ------------------------------------------------------- #
