#!/bin/sh
###############################################################################
#                                                                             #
# This script finds and copies an NAM file for the wave model.                #
#                                                                             #
# This version extract SST, TAIR, UGRD and VGRD information from the grib     #
# NAM output file in the AWLDAS grid (110), lat/lon at 0.125 deg resolution.  #
#                                                                             #
# Remarks :                                                                   #
# - This script runs in the work directory designated in the mother script in #
#   which it generates its own sub-directory 'nam_yyyymmddhh'. If all is well #
#   the directory 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.                        #
#                                                                             #
#                                                                July, 2007   #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

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

  rm -rf ndfd_$1
  mkdir ndfd_$1
  cd ndfd_$1

  count=$2

  set $setoff
  echo ' '
  echo '+--------------------------------+'
  echo '!     Find and copy ndfd files    |'
  echo '+--------------------------------+'
  set $seton

# 0.b Check if time set

  if [ "$#" -lt '1' ]
  then
    set $setoff
    echo ' '
    echo '***************************************'
    echo '*** TIME IN multiwavendfd.sh NOT SET ***'
    echo '***************************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" " TIME IN multiwavendfd.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 "$utilscript" ] || [ -z "$COMNDFD" ]
  then
    set $setoff
    echo ' '
    echo '**********************************'
    echo '*** EXPORTED VARIABLES NOT SET ***'
    echo '**********************************'
    echo ' '
    postmsg "$jlogfile" " EXPORTED VARIABLES NOT SET."
    exit 1
    set $seton
  fi

# --------------------------------------------------------------------------- #
# 1. Copy the wind file data 

  ymdht=`$utilexec/ndate -1 $ymdh`
  ymdhe=`$utilexec/ndate -4 $ymdh`
  ndfdOK='no'

  while [ "$ndfdOK" = 'no' ] && [ $ymdht -ge $ymdhe ]
  do

    ymdht=`$utilexec/ndate -1 $ymdht`
    ymd=`echo $ymdht | cut -c1-8`
    ext=`echo $ymdht | cut -c9-10`

    if [ -f $COMNDFD/$ymd/wgrbbul/ndfd_gl_${ext}.grib2 ]
    then
      ndfd_file=ndfd_gl_${ext}.grib2

      cp $COMNDFD/$ymd/wgrbbul/$ndfd_file . > getndfd.out 2> getndfd.err
      err=$?

      if [ "$err" != '0' ]
      then
        cat getndfd.out
        cat getndfd.err
        set $setoff
        echo ' '
        echo '****************************************'
        echo '*** FATAL ERROR IN copying grib data ***'
        echo '****************************************'
        echo ' '
        set $seton
        postmsg "$jlogfile" "ERROR IN copying grib data for $ymdh"
      fi

      rm -f getnam.out getnam.err

# --------------------------------------------------------------------------- #
# 1b. Extract grib data

      rm -f *.txt
      rm -f *.dat

      $utilexec/wgrib2 $ndfd_file -match WIND -spread wspd.txt > tmp.txt 
      $utilexec/wgrib2 $ndfd_file -match WDIR -spread wdir.txt > tmp2.txt

      nc1=`wc -l tmp.txt | awk '{print $1}'`
      nc2=`wc -l tmp2.txt | awk '{print $1}'`

      sed 's/d=/ /' tmp.txt | awk '{ print $2}' | cut -f 1,4 -d : | sed 's/:/ /' > stamp.txt

      if [ "$nc1" -ge '30' ] && [ "$nc2" -ge '30' ]
      then
        ndfdOK='yes'
      fi

    fi

  done

# --------------------------------------------------------------------------- #
# 1c. Convert to waveprep format

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

     echo $count > tmp.out

     $EXECwave/convert_ndfd < tmp.out > convert.out
     OK=$?

     if [ "$OK" != 0 ]
     then
       set $setoff
       echo ' '
       echo '***************************************************************'
       echo "*** Fatal Error : Could not convert grib2 files for ${ymdh} ***" 
       echo '***************************************************************'
       echo ' '
       cat convert.out
       ndfdOK='no'
     fi

  else
     set $setoff
     echo ' '
     echo '*************************************************'
     echo "*** EROOR: NO NDFD WINDS FOUND FOR CYCLE $ymdh***"
     echo '*************************************************'
     echo ' '
     set $seton
     postmsg "$jlogfile" "NDFD winds not found for $ymdh in multiwavendfd.sh"
  fi

# --------------------------------------------------------------------------- #
# 2.  Copy file

  if [ "$ndfdOK" = 'yes' ]
  then
    cp ll.dat ../ll.$ymdh
    cp wmask.dat ../wmask.$ymdh

    cp wdata.dat ../wdata.$ymdh
    cp convert.out ../stamp.$ymdh

    err=$?

    if [ "$err" != '0' ]
    then
      set $setoff
      echo ' '
      echo '**************************************************'
      echo '*** FATAL ERROR IN COPYING CONVERTED WIND FILE ***'
      echo '**************************************************'
      echo ' '
      postmsg "$jlogfile" "ERROR IN COPYING CONVERTED FILE FOR $ymdh."
      set $seton
      ndfdOK='no'
    fi

  fi

# --------------------------------------------------------------------------- #
# 3.  Clean up the directory

  if [ "$ndfdOK" = 'yes' ]
  then
    set $setoff
    echo ' '
    echo "   Removing work directory after success."
    set $seton

    cd ..
    rm -rf ndfd_$ymdh

  fi

  set $setoff
  echo ' '
  echo 'End of multiwavendfd.sh at'
  date

# End of multiwavendfd.sh ---------------------------------------------------- #
