#!/bin/sh
###############################################################################
#                                                                             #
# This script obtains incremental gfs fields that are then converted to a     #
# WAVEWATCH-III wind file and concatenated to the existing wind file. This    #
# been done so that the wind data can be incorporated as it comes from GFS    #
# and thus allow the wave model to run side by side with the GFS model.       #
# The GFS data is extracted in a sub directory called gfsinc that is removed  #
# at the end of the script.                                                   #
#                                                                             #
# Origination : 09-28-2007                                                    #
# Last update : 09-28-2007                                                    #
#                                                                             #
###############################################################################
# 0. Preparation 
# 0.a Basic modes of operation

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

  export wndID=gfs_30m
  export IDIML=720 ; export JDIML=361 ; export JCLIP=0

  set $setoff
  echo '+-----------------------------------+'
  echo '|   Extract incremental GFS files   |'
  echo '+-----------------------------------+'
  set $seton

# 0.b Check time info

  if [ "$#" -lt '2' ]
  then
    set $setoff
    echo ' '
    echo '*******************************************'
    echo '*** TIME IN multiwavegfs_inc.sh NOT SET ***'
    echo '*******************************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" " TIME IN multiwavegfs_inc.sh NOT SET"
    exit 1
  else
    ymdh_beg=$1
    ymdh_end=$2
    set $setoff
    echo "   Start Time            : $ymdh_beg"
    echo "   End   Time            : $ymdh_end"
    echo ' '
    set $seton
  fi

# 0.c Check exported variables

  if [ -z "$utilexec" ] || [ -z "$USHwave" ] || [ -z "$utilscript" ] \
     || [ -z "$EXECwave" ] || [ -z "$cycle" ]
  then
    set $setoff
    echo ' '
    echo '**********************************'
    echo '*** EXPORTED VARIABLES NOT SET ***'
    echo '**********************************'
    echo ' '
    postmsg "$jlogfile" " EXPORTED VARIABLES NOT SET."
    exit 1
    set $seton
  fi

#---------------------------------------------------------------------------#
# 1. Extract sigma files


  ymdh=$ymdh_beg

  GFSdone='no'

  while [ "$GFSdone" = 'no' ]
  do
  
    rm -f gwnd.*
    GFSfiles='yes'


    $USHwave/multiwavesig.sh $ymdh > sig_$ymdh.out

    if [ -d sig_$ymdh ]
    then
      GFSfiles='no'
      set $setoff
      echo ' ' 
      echo ' Sigma file not generated. See output below '
      echo ' '
      sed "s/^/sig_$ymdh.out : /g" sig_$ymdh.out
      set $seton
      rm -rf sig_$ymdh
      rm -f sig_$ymdh.out
    else
      set $setoff
      echo ' '
      echo " Extracted sigma file for $ymdh at : `date`"
      echo ' '
      set $seton
      rm -f sig_$ymdh.out
    fi


    files=`ls gwnd.* 2> /dev/null`

    rm -rf gfsinc

    if [ -z "$files" ]
    then
      GFSfiles='no'
    else
      set $setoff
      echo "   Found the following files  : $files" 
      mkdir gfsinc
      for file in $files
      do
        cat $file >> gfsinc/gfs.wind
        rm -f $file
      done
    fi

#---------------------------------------------------------------------------#
# 2. Run multiwaveprep to create incremental wind file

    if [ "$GFSfiles" = 'yes' ]
    then
      set $setoff
      echo ' '
      echo '   Found new wind files, running waveprep ... '
      echo ' '
      set $seton

      sed -e "s/HDRFL/F/g" multiwaveprep.$wndID.tmpl > gfsinc/multiwaveprep.inp

      cd gfsinc
    
      ln -sf ../mod_def.$wndID mod_def.ww3
      ln -sf ../sst.$wndID sst.ww3

      $EXECwave/multiwaveprep
      err=$?

      if [ "$err" != '0' ]
      then
        msg="ABNORMAL EXIT: ERROR IN waveprep"
        postmsg "$jlogfile" "$msg"
        set $setoff
        echo ' '
        echo '*************************************** '
        echo '*** FATAL ERROR : ERROR IN waveprep *** '
        echo '*************************************** '
        echo ' '
        echo "$modID prep $date $cycle : error in waveprep." >> $wavelog
        set $seton
        GFSfiles='no'
      fi

    fi

#---------------------------------------------------------------------------#
# 3. Concatanating wind and times files

    if [ "$GFSfiles" = 'yes' ]
    then

      if [ ! -f wind.ww3 ]
      then
        msg="ABNORMAL EXIT: FILE wind.ww3 MISSING"
        postmsg "$jlogfile" "$msg"
        set $setoff
        echo ' '
        cat waveprep.out
        echo ' '
        echo '****************************************'
        echo '*** FATAL ERROR : wind.ww3 NOT FOUND ***'
        echo '****************************************'
        echo ' '
        echo "$modID prep $date $cycle : wind.ww3 missing." >> $wavelog
        set $seton
        GFSfiles='no'
      else
        set $setoff
        file_size=` ls -l wind.ww3 | awk '{print $5}'`
        echo "   wind.ww3 created ; file size = $file_size"
        set $seton
      fi
    fi

#---------------------------------------------------------------------------#
# 4. Append files and Clean-up

    if [ "$GFSfiles" = 'yes' ]
    then
      cat wind.ww3 >> ../wind.$wndID
      rm -f tmp
      touch tmp
      cat tmp >> ../wind.$wndID

      sleep 5

      cat times.WNS >> ../times.$wndID

      echo "   Wind and times file updated "

      rm -f wind.ww3
      rm -f times.WNS
      rm -f sst.ww3
      rm -f *.inp 
      rm -f mod_def.ww3
      rm -f tmp
 
      cd ..
      rm -rf gfsinc

      ymdh_next=`$utilexec/ndate 3 $ymdh`

    else

      sleep 5
      ymdh_next=$ymdh

    fi

#---------------------------------------------------------------------------#
# 5. Update time

    ymdh=$ymdh_next

    if [ "$ymdh" -gt "$ymdh_end" ]
    then
      set $setoff
      echo ' '
      echo " Obtained the required GFS files for run $PDY.$cycle "
      echo ' '
      set $seton
      GFSdone='yes'
      touch $wndID.$cycle.complete
    fi

#    if [ "$GFSdone" = 'no' ]
#    then
#      sleep 5
#    fi

# end of do loop from # 1

  done

#----------------------End of multiwavegfs_inc.sh---------------------------# 
