#!/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_fnl.csh 1994-03-13_00 1994-03-14_00 12
#   or
#   get_fnl.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 = 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"

# 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 YY = `echo $ndate | cut -b 3-4`
   set Mm = `echo $ndate | cut -b 6-7`
   set Dd = `echo $ndate | cut -b 9-10`
   set HH = `echo $ndate | cut -b 12-13`

   echo $YY $Mm $Dd $HH

# Get the final analysis data files.

   set local = fnl_${YY}${Mm}${Dd}_${HH}_00
   if ( ! -e $local ) then 
      echo "msread $local /DSS/DS083.2/data/${local}"
      msread $local /DSS/DS083.2/data/${local}
#      rcp $local ${Host}
   endif

end

rm geth_newdate 
rm newdate.awpr

echo "#####################################################"
echo " "
echo " The data required to run pregrid will be saved  "
echo " as fnl_YYMMDD_HH_00"
echo " "
echo "#####################################################"

echo "DONE"

exit