#!/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_era.csh 1994-03-13_00 1994-03-14_00 12
#   or
#   get_era.csh 1994-03-13_00 1994-03-14_00 12 /ptmp/$USER/myDATA
#   Command line entries will overwrite entries below
# Note: The ERA data are only available from Jan 1979 to Dec 1993.
set startdate = 1987-08-25+12     # YYYY-MM-DD+HH
set enddate   = 1993-03-14+00     # YYYY-MM-DD+HH
set itimint = 6

### 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 up the list "mdate".  "mdate" will hold all the times we 
# need to get.

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 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/ds115.0/MSS-file-list sfc.master
get datasets/ds115.1/MSS-file-list upa.master
quit
EOF1

# Now invoke ftp to get the archive lists.

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


set MNAM = ( jan feb mar apr may jun jul aug sep oct nov dec )
set NAMM = ( jan jan jan jan jan jan jul jul jul jul jul jul )

# Construct the dates as they appear in the upper-air archive titles.
# Use awk (nawk) to get the actual MSS filenames from the archive lists.


foreach ndate ( $mdate ) 

   set YYYY = `echo $ndate | cut -b 1-4`
   set Mm = `echo $ndate | cut -b 6-7`
   set HH   = `echo $ndate | cut -b 12-13`

# Get the upper-air analysis files.
   set EraFile = `nawk -v NAM=${YYYY}${MNAM[$Mm]} -v HH=${HH}Z \\
      '{ if (( $2 == NAM ) && ($3 == HH )) { print $1 } }' upa.master `

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

# Get the surface analysis files.
   set EraFile = `nawk -v NAM=${YYYY}${NAMM[$Mm]} -v HH=${HH}Z \\
      '{ if (( substr($2,1,7) == NAM ) && ($3 == HH )) { print $1 } }' \\
       sfc.master `

   set local = ERA_GRIB_SFC.${YYYY}${NAMM[$Mm]}${HH}Z
   if ( ! -e $local ) then 
      echo "msread -f BI $local /DSS/${EraFile}"
      msread -f BI $local /DSS/${EraFile}
#      rcp $local ${Host}
   endif

end

rm *master
rm geth_newdate 
rm newdate.awpr 
echo "DONE"

exit