# QSUB -r GETNNRP                       # request name
# QSUB -q reg                           # job queue class
# QSUB -eo                              # stdout and stderr together
# QSUB -lM 2Mw                          # maximum memory
# QSUB -lT 1200                         # time limit
# QSUB -lt 1200                         # 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 = 1998-12-01+00
set ndates = 5
set itimint = 6

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/MM5V3
get REGRID.TAR.gz regrid.tar.gz
EOF0

ftp -n < ftp.cmd
rm ftp.cmd

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

cd REGRID/pregrid/nnrp

# Set up the lists "mdate" and "sfcdate".  "mdate" will hold all the 
# upper-air times we need to get.  "sfcdate" will hold all the surface
# times we need to get.  "mdate" and "sfcdate" are not the same, because
# some of the surface analyses are actually 6-hour forecasts.

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

echo "mdate = $mdate"
echo "sfcdate = $sfcdate"

# 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
EOF1

# Construct the dates as they appear in the upper-air archive titles.

set YearSave = 0

set DssFile3d = ( )
set DssFile2d = ( )

foreach ndate ( $mdate ) 

#   @ YYYY = ${ndate} / 1000000
#   @ YYYY = 1900 + ${YYYY}
    set YYYY = `echo $ndate | cut -b 1-4`
#   @ YYMM = ${ndate} / 10000
    set YYMM = `echo $ndate | cut -b 3-4,6-7`

   if ( $YYYY != $YearSave ) then
      set YearSave = $YYYY

      set File3d = ${YYYY}_A.list

      cat >> ftp.cmd << EOF2
ls datasets/ds090.0/inventories
get datasets/ds090.0/inventories/${File3d} ${File3d}
EOF2

   endif

end

# Construct the dates as they appear in the surface archive titles.

set YearSave = 0
foreach ndate ( $sfcdate ) 

#    @ YYYY = ${ndate} / 1000000
#    @ YYYY = 1900 + ${YYYY}
    set YYYY = `echo $ndate | cut -b 1-4`
#    @ YYMM = ${ndate} / 10000
    set YYMM = `echo $ndate | cut -b 3-4,6-7`

   if ( $YYYY != $YearSave ) then
      set YearSave = $YYYY

      set File3d = ${YYYY}_A.list
      set File2d = ${YYYY}.2D

      cat >> ftp.cmd << EOF2
ls datasets/ds090.0/inventories
get datasets/ds090.0/inventories/${File3d} ${File2d}
EOF2

   endif

end

# Close off the ftp command-script.

cat >> ftp.cmd << EOF3
quit
EOF3

# Now invoke ftp to get the archive lists.

ftp -n < ftp.cmd
echo "status = $status"
rm ftp.cmd

# Use awk (nawk) to get the actual MSS filenames from the archive lists.

   # Get the upper-air analysis tar files.

set YearSave = 0
foreach ndate ( $mdate ) 

#    @ YYYY = ${ndate} / 1000000
#    @ YYYY = 1900 + ${YYYY}
#    @ YYMM = ${ndate} / 10000
    set YYYY = `echo $ndate | cut -b 1-4`
    set YYMM = `echo $ndate | cut -b 3-4,6-7`

   if ( $YYMM != $YearSave ) then
      set YearSave = $YYMM

      set File3d = ${YYYY}_A.list

      if ( ! -e $File3d ) then
         echo "Archive listing file $File3d not found"
         exit (2)
      endif

      set ssFile3d = `nawk -v YYMM=$YYMM '{ if ( $6 == YYMM".pgb.f00" ) { print substr($1,3,6) } }' $File3d `

      if ( $ssFile3d == ) then
cat <> FLWANT_SFC.${YYMM}
     end

  endif

end

ja -st