#!/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_nnrp.csh 1994-03-13_00 1994-03-14_00 12
#   or
#   get_nnrp.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 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 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 = ( )
set sfcdate = ( )
while ( $curn < $endn )
   @ idth = $idth + $itimint
   @ num ++
   set ndate = `./geth_newdate ${startdate} ${idth}`
   set sdate = `./geth_newdate ${ndate} -6`
   set mdate = ( ${mdate} ${ndate} )
   set sfcdate = ( ${sfcdate} ${sdate} )
   set curn = `echo ${ndate} | cut -c 1-4,6-7,9-10,12-13`
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 roy-s
user anonymous@ncardata.ucar.edu ${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
      if ( $YYYY == 1976 || $YYYY == 1977 || $YYYY == 1978 ) then
        set File3d = ${YYYY}rerun_A.list
      endif
      if ( $YYYY == 1997 || $YYYY == 1998 || $YYYY == 1999 || $YYYY == 2000 ) then
        set File3d = ${YYYY}tovsrerun_A.list
      endif

      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
      if ( $YYYY == 1976 || $YYYY == 1977 || $YYYY == 1978 ) then
        set File3d = ${YYYY}rerun_A.list
      endif
      if ( $YYYY == 1997 || $YYYY == 1998 || $YYYY == 1999 || $YYYY == 2000 ) then
        set File3d = ${YYYY}tovsrerun_A.list
      endif
      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 ( $YYYY == 1976 || $YYYY == 1977 || $YYYY == 1978 ) then
        set File3d = ${YYYY}rerun_A.list
      endif
      if ( $YYYY == 1997 || $YYYY == 1998 || $YYYY == 1999 || $YYYY == 2000 ) then
        set File3d = ${YYYY}tovsrerun_A.list
      endif

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

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

      if ( $ssFile3d[1] == ) then
cat << EOF
********************************************************
********************************************************
*******                                          *******
*******  ERROR:                                  *******
*******  Date $YYMM not found in archive list.   *******
*******                                          *******
********************************************************
********************************************************

Here's the archive list:

EOF
	 cat $File3d
         echo "File not found - check list"
         exit (1)
      endif

      set NUM_3d_file = 0
      foreach fil ( $ssFile3d )
        @ NUM_3d_file ++
        echo "Working with file number " $NUM_3d_file
        set local = NNRP_GRIB_UPA.${YYMM}_${NUM_3d_file}
        if ( ! -e $local ) then 
         echo "msread -f BI $local /DSS/${fil}"
         msread -f BI $local /DSS/${fil}
        endif
      end

   endif

end

   # Get the surface analysis tar files.

set YearSave = 0
foreach ndate ( $sfcdate ) 
###    @ 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 File2d = ${YYYY}.2D
      set ssFile2d = `nawk -v YYMM=$YYMM  '{ if ( $6 == YYMM".2D" ) { print substr($1,3,6) } }' $File2d `
      set DssFile2d = ( $DssFile2d $ssFile2d )

      set local = NNRP_GRIB_SFC.${YYMM}
      if ( ! -e $local ) then 
         echo "msread -f BI $local /DSS/${ssFile2d}"
         msread -f BI $local /DSS/${ssFile2d}
      endif

   endif

end

# Untar the upper-air analyses that we want from the tarfiles.

foreach  ndate ( $mdate )

###   @ YYMM = ${ndate} / 10000
   set YYMM = `echo $ndate | cut -b 3-4,6-7`
   set mdd = `echo $ndate | cut -b 3-4,6-7,9-10,12-13`
   set flwant = 'pgb.f00'${mdd}
   set count_num_files = 0
   while ( $count_num_files < $NUM_3d_file )
     @ count_num_files ++
        set local = NNRP_GRIB_UPA.${YYMM}_${NUM_3d_file}
     if ( ! -e $flwant ) then
      tar xvf NNRP_GRIB_UPA.${YYMM}_${count_num_files} ${flwant}
      if ( ! -e ${flwant} ) tar xvf NNRP_GRIB_UPA.${YYMM}_${count_num_files} ./${flwant}
     endif
   end
end


# Untar the surface analyses that we want from the tarfiles.

foreach ndate ( $sfcdate )
###   @ YYMM = $ndate / 10000
    set YYMM = `echo $ndate | cut -b 3-4,6-7`
#    set sdate = `get_newdate ${ndate} 6`
   set sdate = `./geth_newdate ${ndate} 6`

  if ( ! -e FLWANT_SFC.${YYMM} ) then

     set flwant = ( )
     set flwant = ( $flwant 'TMPhag.2.'${YYMM} )  # Temperature at 2 m AGL
     set flwant = ( $flwant 'TMPsfc.'${YYMM} )
     set flwant = ( $flwant 'SOILWdlr.10.'${YYMM} )
     set flwant = ( $flwant 'SOILWdlr.200.'${YYMM} )
     set flwant = ( $flwant 'SPFHhag.2.'${YYMM} )
     set flwant = ( $flwant 'TMPdlr.10.'${YYMM} )
     set flwant = ( $flwant 'TMPdlr.200.'${YYMM} )
     set flwant = ( $flwant 'TMPdpl.300.'${YYMM} )
     set flwant = ( $flwant 'ICECsfc.'${YYMM} )
     set flwant = ( $flwant 'LANDsfc.'${YYMM} )     # Land(1)/Sea(0) Mask
     set flwant = ( $flwant 'PRESsfc.'${YYMM} )
     set flwant = ( $flwant 'UGRDhag.10.'${YYMM} )  # U at 10 m AGL
     set flwant = ( $flwant 'VGRDhag.10.'${YYMM} )  # V at 10 m AGL
     set flwant = ( $flwant 'WEASDsfc.'${YYMM} )    # Water Equivalent Snow Depth
     echo $flwant

     touch FLWANT_SFC.${YYMM}
     foreach file ( $flwant )
        if ( ! -e ${file} ) tar xvf NNRP_GRIB_SFC.${YYMM} ${file}
        if ( ! -e ${file} ) tar xvf NNRP_GRIB_SFC.${YYMM} ./${file}
        cat ${file} >> FLWANT_SFC.${YYMM}
     end
     foreach file ( $flwant )
       mv ${file} SFCNNRP_${file}
     end

  endif

end

rm $File3d $File2d
rm NNRP_GRIB*
rm FLWANT*
rm geth_newdate 
rm newdate.awpr

echo "DONE"

exit