#!/bin/sh

# Sample script, picks upt the 500 mb height and 2 m temperature
# from the GFS model for 3 forecast hours (0, 3, and 6)
#
# use this script as an example on how to use the NEW
# and recommended way of downloading data

set -x

# define some constants .. change email address
URL="http://wesley.ncep.noaa.gov/cgi-bin/ftp2u_avn.sh"
email=yourname@yourisp.com

# get URL and parse out the FTP directory
# ftpdir=`wwwgrab "$URL?\
ftpdir=`wget --output-document=- "$URL?\
file=gblav.t00z.pgrbf00&\
file=gblav.t00z.pgrbf03&\
file=gblav.t00z.pgrbf06&\
wildcard=&\
lev_500_mb=on&lev_2_m_above_gnd=on&\
var_HGT=on&\
var_TMP=on&\
results=SAVE&rtime=1hr&" |\
grep "Results were saved in" | sed -e 's-^.*href="ftp://--' -e 's/".*$//'`

# extract the machine and directory 
machine=`echo $ftpdir | sed 's-/.*--'`
ftpdir=`echo $ftpdir | sed 's-[^/]*/--'`
export ftpdir machine
echo "data is in ftp://$machine/$ftpdir"

if [ "$ftpdir" = '' ] ; then
  echo "failed"
  exit 9
fi

# use FTP to get the data
# Note: in the future, the ftpdir may be on a different machine
# than the web server.

ftp -v -n $machine <<EOF
user anonymous $email
bin
prompt
cd $ftpdir
mget *
EOF
