#!/bin/sh
#
# revised sample script 
# uses NOMAD3 as data server
#
# Sample script, picks up the 500 mb and 1000 mb TMP and UGRD
# from the GFS model for 00 and 06 hour forecasts
#
# picks up a subregion: 300E-340E 40N-80N
#
# uses curl as download script
# 2/2007 public domain wesley ebisuzaki
#
set -x

# set date code of forecast
date=20070219
HR=12

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

# get URL and parse out the FTP directory

ftpdir=`curl "$URL?\
file=gfs.t${HR}z.pgrbanl&file=gfs.t${HR}z.pgrbf06&wildcard=&\
lev_1000_mb=on&lev_500_mb=on&\
var_HGT=on&var_UGRD=on&\
subregion=&leftlon=300&rightlon=340&toplat=80&bottomlat=40&\
results=SAVE&rtime=1hr&machine=000.0.0.0&\
user=anonymous&passwd=&ftpdir=%2Fincoming_1hr&prefix=&\
dir=%2Fgfs${date}" | \
 grep "Results were saved in" | sed 's=^.*ftp://==' | sed -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
