#!/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.                        #
#                                                                             #
#                                                            April 28, 1999   #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

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

   rm -rf eice_$1
   mkdir eice_$1
   cd eice_$1

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

# 0.b Check if time set

  if [ "$#" -lt '1' ]
  then
    set $setoff
    echo ' '
    echo '**********************************'
    echo '*** TIME IN waveice_grl.sh NOT SET ***'
    echo '**********************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" " TIME IN waveice_grl.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.  Get the necessary files
                                                                                
# 1.b Copy the wave model mask file
                                                                                
  file=$FIXwave/wave_$modID.mask
                                                                                
   ls -la $file
  if [ -f $file ]
  then
    cp $file mask.ww3
  fi
     
  if [ -f mask.ww3 ]
  then
    set $setoff
    echo "   mask.ww3 copied ($file)."
    set $seton
  else
    set $setoff
    echo ' '
    echo '*************************************** '
    echo '*** ERROR : NO WAVE MODEL MASK FILE *** '
    echo '*************************************** '
    echo ' '
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR - NO WAVE MODEL MASK FILE"
    exit 0
  fi
     
     
# 1.c Copy the ice mask file
     
  file=$FIXwave/waveice_grl.mask
     
  if [ -f $file ]
  then
    cp $file mask.ice
  fi
     
  if [ -f mask.ice ]
  then
    set $setoff
    echo "   mask.ice copied ($file)."
    set $seton
  else
    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** ERROR : NO ICE FIELD MASK FILE *** '
    echo '************************************** '
    echo ' '
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR - NO ICE FIELD MASK FILE"
    exit 0
  fi


# --------------------------------------------------------------------------- #
# 2.  Get the data from HPSS and run hindcast 

# Slice and dice ymdh

#   yearw=`echo $ymdh | cut -c 1-4`

   yearw=`echo $PDY$cyc | cut -c 1-4`
   monthw=`echo $PDY$cyc | cut -c 5-6`
   dayw=`echo $PDY$cyc | cut -c 7-8`
   hourw=`echo $PDY$cyc | cut -c 9-10`
   ymdw=`echo $PDY$cyc | cut -c 1-8`

# Get tm tag
   dyt=`$utilexec/nhour $PDY$cyc $ymdh`

# Get fcst tag
   fcst=`$utilexec/nhour $ymdh $PDY$cyc`

   echo "waveice ymdw : $ymdw   dyt: $dyt   hourw: $hourw" 
   echo "ymdh: $ymdh"
   echo "PDYcyc: $PDY$cyc"
if [ "$ymdh" -lt "$PDY$cyc" ]
then
######### Forecast 
   $USHwave/wave_getbestndas_g110.sh $ymdh > getice.out 2> getice.err
  err=$?
else

  fcsth=`$utilexec/nhour $ymdh $YMDH`
#####   forecast   #
    cp $COMNAM/nam.t${cyc}z.awldas${fcsth}.tm00 awldas00.$ymdh > getice.out 2> getice.err
  err=$?
fi

  if [ "$err" != '0' ]
  then
    cat getice.out
    cat getice.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

  #spec_file=`cat getice.out | awk '{ print $1 }'`
  rm -f getice.out getice.err

# --------------------------------------------------------------------------- #
# 3. Extract grib data

fname=awldas00.$ymdh
spec_file=`ls $fname`

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



# Get ICE components
 $utilexec/wgrib -s ${fname} | grep "ICEC" | $utilexec/wgrib -s -i ${fname} -text -o dumf > dum
 sed '2,$!d' dumf >> 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

# 3.  Copy file

  set $setoff
  echo "   File for ${ymdh} : ${spec_file}"
  set $seton
  cp namf ../eice.$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

# --------------------------------------------------------------------------- #
# 4.  Clean up the directory

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

  cd ..

  rm -rf eice_$ymdh

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

# End of waveice_grl.sh --------------------------------------------------------- #
