; STUFFR
;---------------------------------------------------------------
;! averages together data sets in hour angle
;# Procedure UV IMAGING
;-----------------------------------------------------------------------
;;  Copyright (C) 2006-2007, 2013, 2015
;;  Associated Universities, Inc. Washington DC, USA.
;;
;;  This program is free software; you can redistribute it and/or
;;  modify it under the terms of the GNU General Public License as
;;  published by the Free Software Foundation; either version 2 of
;;  the License, or (at your option) any later version.
;;
;;  This program is distributed in the hope that it will be useful,
;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;  GNU General Public License for more details.
;;
;;  You should have received a copy of the GNU General Public
;;  License along with this program; if not, write to the Free
;;  Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
;;  MA 02139, USA.
;;
;;  Correspondence concerning AIPS should be addressed as follows:
;;         Internet email: aipsmail@nrao.edu.
;;         Postal address: AIPS Project Office
;;                         National Radio Astronomy Observatory
;;                         520 Edgemont Road
;;                         Charlottesville, VA 22903-2475 USA
;-----------------------------------------------------------------------
STUFFR     LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
STUFFR    RUN STUFFR, proc averages data sets together in H.A.
INNAME                             Input UV file name (name)
INCLASS                            Input UV file name (class)
INSEQ             0.0    99999.0   Input UV file name (seq. #)
INDISK            0.0        9.0   Input UV file disk unit #
IN2SEQ            0.0    99999.0   Highest input seq number
DOCALIB          -1.0      101.0   > 0 calibrate data & weights
                                   > 99 do NOT calibrate weights
FLAGVER          -1.0        0.0   = 0 => flag w highest vers
                                   -1 => do no flagging
OUTNAME                            Output UV file name (name)
OUTCLASS                           Output UV file name (class)
OUTSEQ           -1.0     9999.0   Output UV file name (seq. #)
OUTDISK           0.0        9.0   Output UV file disk unit #.
APARM                              (1) max. time (sec)
                                   (2) = fov radius (arc min)
BADDISK                            Disk #'s to avoid
----------------------------------------------------------------
STUFFR
Task:  This procedure runs TI2HA on each, single-source input UV data
       set, INSEQ through IN2SEQ of the same INNAME and INCLASS, to
       convert all times to hour angles (+ 1 day).  It will sort the
       output files to BT order with UVSRT and then concatenate the
       altered/sorted files as it procedes using DBCON with
       DOARRAY=-1.  Finally, it will run UBAVG on the full
       concatenated data set to average the data with
       baseline-dependent time averaging.  It ends with a UVSRT to
       sort to TB order for imaging and self-calibration.

       It is not required that all sequence numbers from INSEQ through
       IN2SEQ actually occur.  The loop will skip over missing ones.

       You must first compile the procedure via
            RUN STUFFR
       Then you may execute it by simply entering the word STUFFR.
       The procedure will be remembered in the lASTEXIT SAVE/GET file
       and so does not need to be re-RUN.

       The input files must be single-source files having the same
       antennas.  The output should be used for self-calibration only
       with care since times from multiple days will be mixed
       together.  Note that STUFFR now applies FG tables and SN
       tables in the TI2HA step.  Also note that flag, calibration,
       weather, et al. tables must be discarded at that step since the
       times in them no longer correlate with the "times' (actually
       hour angles +1 day) in the data.

       The procedure makes lots of intermediate UV files which it
       deletes as soon as possible while it runs.  This means that
       certain names may not occur on your data disks.  These are
               name         class        seq numbers
               ------       ------      ---------------
               INNAME       HATMP       INSEQ - IN2SEQ
               INNAME       SRTEMP      INSEQ - IN2SEQ
               INNAME       DBCTMP      INSEQ - IN2SEQ
               OUTNAME      UBATMP      <GETPOPSN> (the aips number)
               OUTNAME      OUTCLASS    OUTSEQ
       The procedure will check for these names and quit if any of
       them are present at the start of the procedure.
Adverbs:
  INNAME.....Input UV file name (name).      Standard defaults.
  INCLASS....Input UV file name (class).     Standard defaults.
  INSEQ......Input UV file name (seq. #).    0 => highest.
  INDISK.....Disk drive # of input UV file.  0 => any.
  IN2SEQ.....Highest sequence number for input UV files.
  DOCALIB....If true (>0), calibrate the data using information in the
             highest numbered (CL) table for multi-source or SN table
             for single-source data.  Also calibrate the weights
             unless DOCALIB > 99 (use this for old non-physical
             weights).  Calibration and flagging are applied to the
             inputs to TI2HA.
  FLAGVER....0 => flag each data set with its highest numbered flag
             table; -1 => do no flagging.
  OUTNAME....Output UV file name (name).     Standard defaults.
  OUTCLASS...Output UV file name (class).    Standard defaults.
  OUTSEQ.....Output UV file name (seq. #).   0 => highest unique
  OUTDISK....Disk drive # of output UV file. 0 => highest with
             space for the file.
  APARM......UBAVG enrichment parameters.
             (1)  This gives the maximum averaging time in seconds,
                  0 => infinite.  This parameter may be necessary if
                  subsequent editing may be required.
             (2)  This is the desired field of view (radius) which is
                  not to be distorted due to time averaging in minutes
                  of arc.   0 = > 20.
                  The field of view is the region in which averaging
                  is not to reduce the amplitude by more than 1% on
                  any baseline.   No data separated by more than 268.5
                  wavelengths divided by APARM(2) are averaged
                  together.
                  sinc(.0781) = 0.99 and .0781 * 60 *180 / pi = 268.5.
  BADDISK....Disk #'s to avoid for scratch files
----------------------------------------------------------------
