#!/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 nam_$1
  mkdir nam_$1
  cd nam_$1

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

# 0.b Check if time set

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

# --------------------------------------------------------------------------- #
# 1.  Run getbestndas.sh or copy nam forecast file

  if [ "$ymdh" -le "$PDY$cyc" ]
  then
    $USHwave/wave_getbestndas_g110.sh $ymdh > getnam.out 2> getnam.err
    err=$?
  else
    fcsth=`$utilexec/nhour $ymdh $YMDH`
    cp $COMNAM/nam.t${cyc}z.awldas${fcsth}.tm00 awldas00.$ymdh > getnam.out 2> getnam.err
    err=$?
  fi

  if [ "$err" != '0' ]
  then
    cat getnam.out
    cat getnam.err
    set $setoff
    echo ' '
    echo '*************************************'
    echo '*** FATAL ERROR IN getbestndas.sh ***'
    echo '*************************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" "ERROR IN getbestndas.sh for $ymdh"
    exit 2
  fi

  fname=awldas00.$ymdh

  spec_file=`ls $fname`
  rm -f getnam.out getnam.err

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


# Determine forecast hour
  fcsth=`$utilexec/nhour $ymdh $YMDH`
# Add Date/Time tag
#  cat > namf << EOF
#  $ymdh $fcsth
#  EOF
   echo "$ymdh $fcsth" > namf

# Get U and V components
  $utilexec/wgrib -s ${fname} | grep "UGRD" | grep "10 m above gnd" | $utilexec/wgrib -s -i ${fname} -text -o dumf > dum
  sed '2,$!d' dumf >> namf
  $utilexec/wgrib -s ${fname} | grep "VGRD" | grep "10 m above gnd" | $utilexec/wgrib -s -i ${fname} -text -o dumf > dum
  sed '2,$!d' dumf >> namf
# Get air T
  $utilexec/wgrib -s ${fname} | grep "TMP" | grep "2 m above gnd" | $utilexec/wgrib -s -i ${fname} -text -o dumf > dum
  sed '2,$!d' dumf >> namf
# Get SST
  $utilexec/wgrib -s ${fname} | grep "TMP" | grep "sfc" | $utilexec/wgrib -s -i ${fname} -text -o dumf > dum
  sed '2,$!d' dumf >> namf

  rm -f dumf dum

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

  set $setoff
  echo "   File for ${ymdh} : ${spec_file}"
  set $seton
  cp namf ../ewnd.$ymdh
  err=$?

  if [ "$err" != '0' ]
  then
    set $setoff
    echo ' '
    echo '********************************************'
    echo '*** FATAL ERROR IN COPYING SPECTRAL FILE ***'
    echo '********************************************'
    echo ' '
    postmsg "$jlogfile" "ERROR IN COPYING SPECTRAL FILE FOR $ymdh."
    set $seton
    exit 3
  fi

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

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

  cd ..
  rm -rf nam_$ymdh

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

# End of multiwavenam.sh ---------------------------------------------------- #
