#!/bin/csh

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

cd $TMPDIR


#               ********************************************
#               ****** fetch interactive/batch C shell *****
#               *******        NCAR IBM's only        ******
#               *******           f90 only            ******
#               ********************************************

#	This shell fetches ADP data from the NCAR MSS system for 
#	use in RAWINS. 


#	The only user inputs to the fetch program are the beginning
#	and ending dates of the observations, and a bounding box for the
#	observation search.  These dates are given in YYYYMMDDHH.  The 
#	ADP data are global, and include the surface observations and 
#	upper-air soundings.  A restrictive bounding box (where 
#	possible) reduces the cost substantially.

set starting_date = 1993031300
set ending_date   = 1993031400

#	Is there a personal copy of the source?

set UseMySource = no

#	########################################################
#	########                                       #########
#	########       END OF USER  MODIFICATIONS      #########
#	########                                       #########
#	########################################################

#       Get source code.

   if ( $UseMySource == yes ) then
      cp $HOME/fetch.tar .
   else
      cp /mmm/users/mesouser/MM5V3/FETCH.TAR.gz fetch.tar.gz
#      if ( ! -e fetch.tar.gz ) then
#         msread fetch.tar.gz /MESOUSER/MM5V3/FETCH.TAR.gz
#      endif
      gunzip fetch.tar
   endif
   if ( ! -e fetch.tar ) then
      echo "The fetch.tar file does not exist.  Stopping."
      exit ( 1 )
   endif
   tar -xf fetch.tar
   rm fetch.tar
   ls -ls
   cd FETCH

set LETTERS = (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)

#	Pull ADP surface data from the MSS.

cd adp_sfc
./fetch_adp_sfc.csh $starting_date $ending_date >&! fetch_sfc_print &

wait
ls -ls

#	Save files to names used in RAWINS

if ( -e A.1 ) then
   foreach file ( A.[1-9] )
        set num = $file:e
        echo "Save surface data to sfc3hr$LETTERS[${num}] and sfc3hr$LETTERS[${num}] "
        mv A.$num        sfc6hr$LETTERS[${num}]
        mv B.$num        sfc3hr$LETTERS[${num}]
   end
endif
if ( -e E.1 ) then
   foreach file ( E.[1-9] )
        set num = $file:e
        echo "Save surface data to sfc3hr$LETTERS[${num}] and sfc3hr$LETTERS[${num}] "
        mv E.$num        sfc6hr$LETTERS[${num}]
        mv F.$num        sfc3hr$LETTERS[${num}]
   end
endif


#	Pull ADP upper-air data from the MSS

cd ../adp_upa
./fetch_adp_upa.csh $starting_date $ending_date >&! fetch_upa_print &

wait
ls -ls

#	Save files to names used in RAWINS

if ( -e UPA.1 ) then
   foreach file ( UPA.[1-9] )
        set num = $file:e
        echo "Save upper-air data to raobs$LETTERS[${num}] "
        mv UPA.$num        raobs$LETTERS[${num}]
   end
endif

#	Put additional files on MSS.  These are the standard out
#	files from the ADP program processing.

cd ..
cp adp_sfc/fetch_sfc_print .
cp adp_upa/fetch_upa_print .
#tar -cvf fetch_info.tar fetch_sfc_print fetch_upa_print 
#mswrite -t ${RetPd} fetch_info.tar ${ExpName}/FETCH_INFO.TAR &

#	Print the standard out in this file for the user's benefit.

echo ADP_SFC fetch print
cat fetch_sfc_print

echo ADP_UPA fetch print
cat fetch_upa_print

echo " "
echo " #########################################################################
########"
echo " Files will aso be retained on $TMPDIR/FETCH/adp_sfc and $TMPDIR/FETCH/adp_upa"
echo " #########################################################################
########"


echo "DONE"

exit