# QSUB -r fetch                         # request name
# QSUB -q prem                          # job queue class
# QSUB -o fetch.SEQN
# QSUB -eo                              # stdout and stderr together
# QSUB -lM 10Mw                         # maximum memory
# QSUB -lT 3000                         # time limit
# QSUB                                  # no more qsub commands

ja
set echo

#               ********************************************
#               ****** fetch interactive/batch C shell *****
#               *******          V2 System            ******
#               *******           f90 only            ******
#               ********************************************

#       Note: when running this deck interactively, it assumes
#       the source files are local and un-tared.

#       This shell fetches ADP data from the NCAR MSS system and
#       converts it into a format suitable for the little_r
#       program.  The data are sent back to the local machine and
#       stored on the NCAR MSS.

#       Location where data are to be returned on local machine.

set Host    = username@host.domain:/local_directory

#       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

set lon_e         =  180
set lon_w         = -180
set lat_s         =  -90
set lat_n         =   90

set lon_e         =  -50
set lon_w         = -180
set lat_s         =   20
set lat_n         =   70

#       Is there a personal copy of the source?

set UseMySource = no

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

unalias cd ls rm

if ( $?ENVIRONMENT ) then
   echo "environment variable defined as $ENVIRONMENT"
else
   setenv ENVIRONMENT INTERACTIVE
   echo "environment variable defined as $ENVIRONMENT"
endif

if ( $ENVIRONMENT == BATCH ) then
   cd $TMPDIR
endif

#       Get source code.

if ( $ENVIRONMENT == BATCH ) then
   if ( $UseMySource == yes ) then
      rcp $Host/fetch.tar .
   else
      cp /fs/othrorgs/home0/mesouser/MM5V2/Fetch/fetch.tar fetch.tar
      if ( ! -e fetch.tar ) then
         msread fetch.tar /MESOUSER/MM5V2/FETCH/fetch.tar
      endif
   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
endif

#       Pull ADP surface data from the MSS.

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

wait
ls -ls

#       Send surface data to local machine and to MSS.

if ( -e A.1 ) then
   foreach file ( A.[1-9] )
        set num = $file:e
        rcp A.$num        $Host
        rcp B.$num        $Host
   end
endif

if ( -e E.1 ) then
   foreach file ( E.[1-9] )
        set num = $file:e
        rcp E.$num        $Host
        rcp F.$num        $Host
   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

#       Send upper-air data to local machine and to MSS.

if ( -e UPA.1 ) then
   foreach file ( UPA.[1-9] )
        set num = $file:e
        rcp UPA.$num        $Host
   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

#       Accounting and clean-up.

if ( $ENVIRONMENT == BATCH ) then
        ja -s
endif