#!/bin/csh

if ( ! -e /ptmp/$USER ) then
   mkdir /ptmp/$USER
endif
set TMPDIR=/ptmp/$USER


# The necessary arguments below, or specify them on the command line
#   ie,
#   get_toga.csh 1994-03-13_00 1994-03-14_00 12
#   or
#   get_toga.csh 1994-03-13_00 1994-03-14_00 12 /ptmp/$USER/myDATA
#   Command line entries will overwrite entries below

set startdate = 1993-03-13+00    # YYYY-MM-DD+HH
set enddate =   1993-03-14+00    # YYYY-MM-DD+HH
set itimint =   12               # 12

### If you uncomment this line, also uncomment the line containing
### "rcp" later in the script
#set Host = username@host.domain:/local_directory


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

if ( $1 != '') then
   echo "Change Date Information to command line input"
   set startdate = $1    # YYYY-MM-DD+HH
   set enddate =   $2    # YYYY-MM-DD+HH
   set itimint =   $3    # 12
   if ( $2 == '') then
      echo "MUST specify enddate on command line if statedate was specified"
      exit
   endif
   if ( $3 == '') then
      echo "MUST specify interval on command line if statedate and enddate were specified"
      exit
   endif
endif
echo "State Date" $startdate
echo "  End Date" $enddate
echo "  Interval" $itimint
if ( $4 != '') then
   echo "Change TMPDIR to command line input"
   set TMPDIR=$4
   echo $TMPDIR
endif

cd $TMPDIR
cp /mmm/users/mesouser/MM5V3/Util/geth_newdate .
cp /mmm/users/mesouser/MM5V3/Util/newdate.awpr .

set curn = `echo ${startdate} | cut -c 1-4,6-7,9-10,12-13`
set endn =   `echo ${enddate} | cut -c 1-4,6-7,9-10,12-13`

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

set num = 0
set idth = -$itimint
set mdate = ( )
while ( $curn < $endn )
   @ idth = $idth + $itimint
   @ num ++
   set ndate = `./geth_newdate ${startdate} ${idth}`
   set mdate = ( ${mdate} ${ndate} )
   set curn = `echo ${ndate} | cut -c 1-4,6-7,9-10,12-13`
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 roy-s
user anonymous@ncardata.ucar.edu ${user}@ucar.edu
get datasets/ds111.2/MSS-file-list toga.master
quit
EOF1

# Now invoke ftp to get the archive lists.

if ( ! -e toga.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 << EOF
   {if ( ( substr(\$1,1,1) != "Y") ) next}
   { hold = \$2
     gsub( "jan", ",",","01", \$2)
     gsub( "feb", ",",","02", \$2)
     gsub( "mar", ",",","03", \$2)
     gsub( "apr", ",",","04", \$2)
     gsub( "may", ",",","05", \$2)
     gsub( "jun", ",",","06", \$2)
     gsub( "jul", ",",","07", \$2)
     gsub( "aug", ",",","08", \$2)
     gsub( "sep", ",",","09", \$2)
     gsub( "oct", ",",","10", \$2)
     gsub( "nov", ",",","11", \$2)
     gsub( "dec", ",",","12", \$2) }

     { if (( substr(\$2,1,6) == IDATE) )
       {
	 {print substr(hold,1,9), \$1}
       }
    } 
EOF

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

# Get the upper-air analysis files.

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

   if ($MssFile[2] == ) then
cat << EOF
**********************************************************
**********************************************************
*******                                            *******
*******  ERROR:                                    *******
*******  Date ${YYYY}${Mm} not found in archive list.  *******
*******                                            *******
**********************************************************
**********************************************************

Here's the archive list:

EOF
      cat toga.master
      exit (1)
   endif
   set Local  = TOGA_GRIB.${YYYY}${Mm}
   set Remote = /DSS/$MssFile[2]
   if ( ! -e $Local ) then
     echo "msread -f BI $Local $Remote"
     msread -f BI  $Local  $Remote
#     rcp $Local $Host
   endif
#
end

rm gnc.awpr toga.master
rm geth_newdate 
rm newdate.awpr 

echo "DONE"

exit