#!/bin/sh
#
# sample scripts that downloads selected fields from NCDC's NARR archive
#
# range of dates to download (every 3 hours)
date0=2006010118
date1=2006010203

# check for bad dates
date=$date0
if [ $date -lt 1979010100 ] ; then
  echo "bad date YYYYMMDDHH $date"
  exit 88
fi
if [ $date -gt 2018010100 ] ; then
  echo "bad date YYYYMMDDHH $date"
  exit 88
fi
while [ $date -le $date1 ]
do
#  check if file had already been downloaded
   if [ ! -f narr_${date}.grb ] ; then
#     don't over load the server
      sleep 1
      echo ">> $date"
      yymmdd=`echo $date | cut -c1-8`
      yymm=`echo $date | cut -c1-6`
      hr=`echo $date | cut -c9-10`

      URL0="http://nomads.ncdc.noaa.gov/data/narr/$yymm/$yymmdd"
      URL="$URL0/narr-a_221_${yymmdd}_${hr}00_000"

#     $URL = location of grib file
#     $URL.inv = location of inventory file
#     this code downloads the file
      get_inv.pl "$URL.inv" | \
         egrep ':(TMP|UGRD|VGRD|PRES:sfc):' | \
         egrep -v ':(max wind lev|tropopause|cld top):' | \
         get_grib.pl "$URL.grb"  narr_$yymmdd${hr}.grb >/dev/null
   fi

#   increment by 3 hours
#   this version requires GNU date command
    d="`echo $date | cut -c1-8` `echo $date | cut -c9-10`"
    date=`date --date="$d +3 hours" "+%Y%m%d%H"`
done
