# QSUB -r GETNCEP                       # request name
# QSUB -q reg                           # job queue class
# QSUB -eo                              # stdout and stderr together
# QSUB -lM 2Mw                          # maximum memory
# QSUB -lT 600                          # time limit
# QSUB -lt 600                          # time limit
# QSUB -mb                              # mail me when it starts
# QSUB -me                              # mail me when it ends
# QSUB                                  # no more qsub commands

ja

set echo

# The necessary arguments:

set startdate = 1997-08-07+00     # YYYY-MM-DD+HH
set ndates = 3
set itimint = 12

set Host = username@host.domain:/local_directory

###############################################################################
######################                         ################################
######################  END USER MODIFICATION  ################################
######################                         ################################
###############################################################################

cd $TMPDIR

cat >! ftp.cmd << EOF0
open ftp.ucar.edu
user anonymous ${user}@ucar.edu
cd mesouser/newprogs
get regrid.tar.gz
EOF0

ftp -n < ftp.cmd
rm ftp.cmd

gzip -cd regrid.tar.gz | tar xf -

cd REGRID/pregrid/ncep.grib

# Set up the list "mdate".  "mdate" will hold all the times we 
# need to get.

set num = 0
set idth = -$itimint
set mdate = ( )
while ( $num < $ndates )
   @ idth = $idth + $itimint
   @ num ++
   set ndate = `../util/geth_newdate ${startdate} ${idth}`
   set mdate = ( ${mdate} ${ndate} )
end

echo "mdate = $mdate"

# Set up the ftp command-script to get the archive lists.  First, set 
# up to login anonymously to ncardata.ucar.edu

cat >! ftp.cmd << EOF1
open ncardata.ucar.edu
user anonymous ${user}@ucar.edu
get datasets/ds083.0/MSS-file-list ncep.master
quit
EOF1

# Now invoke ftp to get the archive lists.

if ( ! -e ncep.master ) then
   ftp -n < ftp.cmd
   rm ftp.cmd
endif

set MNAM = ( JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC )

cat >! gnc.awpr <= IDATE) )
	 {print \$1}
    } 
EOF
endif

# Construct the dates as they appear in the archive titles.  Use awk
# (nawk) to get the actual MSS filenames from the archive lists.  We
# change the text-string month in the archive list to a two-digit 
# number, to make comparisons among dates easier.

foreach ndate ( $mdate ) 

   set YYYY = `echo $ndate | cut -b 1-4`
   set Mm = `echo $ndate | cut -b 6-7`
   set Dd = `echo $ndate | cut -b 9-10`

# Get the upper-air analysis files.


   set MssFile = `nawk -v IDATE=${YYYY}${Mm}${Dd} -f gnc.awpr ncep.master`

   set local = NCEP_GRIB.${YYYY}${MNAM[$Mm]}
   if ( ! -e $local ) then 
      echo "msread -f BI $local /DSS/${MssFile}"
      msread -f BI $local /DSS/${MssFile}
      rcp $local ${Host}
   endif

end

rm gnc.awpr

ja -chlst