set -xa
#  MAY 2001      BURKE     MDL    HPUX
#  Nov 2001      Kitzmiller  MDL   IBM AIX
 
#  PURPOSE
#   Identifies latest NAM model GRIB files.  Decodes NAM model data 
#   files from GRIB format into REAL*4 binary format

#   Input files: /com/nam/prod/nam.{yyyymmdd}/nam.t{RR}z.grb5fm{hh}.tm00
#   Output files: nam_grib_latest_{hh}

cp $PARMradar/radar_unpkgrb4meg.dat unpkgrb4meg.dat 

#   Check to see if new model run has written out forecasts
#   as far as 18-h projection

latest_done=`ls -t /com/nam/prod/nam.????????/nam.t??z.grb5fm18.tm00 | head -n1`
echo "Latest available GRIB file is " $latest_done

#   New model run is in but not yet decoded.  Get
#   the GRIB data into decoder

#   Remove old GRIB files

rm -f nam_grib_latest_??

edir=`echo $latest_done | cut -c 1-42`
echo "NAM directory files are like: " $edir

hours='00 06 12 18'
for  hh in $hours
do
    echo hh=$hh
    f=$edir$hh'.tm00'
    echo f=$f
    ff='nam_grib_latest_'$hh
    echo ff=$ff
    if test -s $f
    then
      cp $f $ff
    else
      msg="NAM fcst for " $hh " unavailable"
      export err=1; err_chk
    fi
done

#   Decode GRIB files to FORTRAN binary format.
#   Remove copies of GRIB files.

pgm=unpkgrb5meg
export pgm;. prep_step
$EXECradar/radar_unpkgrb5meg >> $pgmout 2>>errout
err=$? ; export err; err_chk

