; UVMTH
;---------------------------------------------------------------
;!  Averages one data set and applied it to another.
;# Task UV
;-----------------------------------------------------------------------
;;  Copyright (C) 1995, 2007, 2010, 2018
;;  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
;-----------------------------------------------------------------------
UVMTH     LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
UVMTH     Corrects one uv dataset with the average of another.
INNAME                             File name to the averaged.
INCLASS                            File class to be averaged
INSEQ             0.0     9999.0   File seq. # to be averaged
INDISK            0.0        9.0   File disk number
SOURCES                            Source name
QUAL            -10.0              Calibrator qualifier -1=>all
CALCODE                            Calibrator code '    '=>all
TIMERANG                           Time range to use
SELBAND                            Bandwidth to select (kHz)
SELFREQ                            Frequency to select (MHz)
FREQID                             Freq. ID to select.
SUBARRAY          0.0     1000.0   Sub-array, 0=>all
DOCALIB          -1.0      101.0   > 0 calibrate data & weights
                                   > 99 do NOT calibrate weights
GAINUSE                            CL (or SN) table to apply
DOPOL            -1.0       10.0   If >0.5 correct polarization.
PDVER                              PD table to apply (DOPOL>0)
BLVER                              BL table to apply.
FLAGVER                            Flag table version
DOBAND           -1.0       10.0   If >0.5 apply bandpass cal.
                                   Method used depends on value
                                   of DOBAND (see HELP file).
BPVER                              Bandpass table version
SMOOTH                             Smoothing function. See
                                   HELP SMOOTH for details.
DOACOR                             Include autocorrelations?
IN2NAME                            Input UV file name (name)
IN2CLASS                           Input UV file name (class)
IN2SEQ            0.0     9999.0   Input UV file name (seq. #)
IN2DISK           0.0        9.0   Input UV file disk unit #
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 #.
OPCODE                             Operation 'ADD','SUB','MULT',
                                    'DIV'   (default SUB)
BADDISK                            Disks to avoid for scratch
----------------------------------------------------------------
UVMTH
Task: This task determines the time averaged visibilities for each
      baseline in a uv data set and applies them to another data set.

      The first input data set is averaged over all times after
      application of data selection, flagging, and calibration as
      desired.  Each baseline and correlator (Stokes, spectral
      channel, IF) is kept separately.  Only one subarray and one
      frequency ID are allowed, but multiple sources may be averaged
      together.  These averaged data are then combined with the data
      of the second data set by addition, subtraction, multiplication,
      or division depending on OPCODE.  The data of the second data
      set are not flagged, calibrated, or averaged in any way.  If the
      second data set has more than one subarray or frequency ID, then
      only the subarray and frequency ID used from the first data set
      will be copied.  The two data sets must have the same number of
      correlators in the same order (after the calibration is
      applied).  The axis order is Complex, Stokes, Frequency, IF.
      If the second data set is not in this order, it may be converted
      by SPLAT.
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 number of input UV file.   0 => any.
  =================================================================
     The following adverbs are applied to INNAME during the averaging
     process.

  SOURCES....Source to be baselined.   '  '=> all; if any starts with
             a '-' then all except ANY source named.
  QUAL.......Qualifier of source to be baselined. -1 => all.
  CALCODE....Calibrator code of sources to baseline. ' '=> all.
  TIMERANG...Time range of the data to be copied. In order: Start day,
             hour, min. sec, end day, hour, min. sec. Days relative to
             ref. date.
  SELBAND....Bandwidth of data to be selected. If more than one IF is
             present SELBAND is the width of the first IF required.
             Units = kHz. For data which contain multiple
             bandwidths/frequencies the task will insist that some form
             of selection be made by frequency or bandwidth.
  SELFREQ....Frequency of data to be selected. If more than one IF is
             present SELFREQ is the frequency of the first IF required.
             Units = MHz.
  FREQID.....Frequency identifier to select (you may determine which is
             applicable from the OPTYPE='SCAN' listing produced by
             LISTR). If either SELBAND or SELFREQ are set, their values
             override that of FREQID.  However, setting SELBAND and
             SELFREQ may result in an ambiguity.  In that case, the task
             will request that you use FREQID.
  SUBARRAY...Sub-array number to copy. 0=>1.
  DOCALIB....If true (>0), calibrate the data using information in the
             specified Cal (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).  If
             the data are not in time order, then DOCALIB must be
             false.
  GAINUSE....version number of the CL table to apply to multi-source
             files or the SN table for single source files.
             0 => highest.
  DOPOL......If > 0 then correct data for instrumental polarization as
             represented in the AN or PD table.  This correction is
             only useful if PCAL has been run or feed polarization
             parameters have been otherwise obtained.  See HELP DOPOL
             for available correction modes: 1 is normal, 2 and 3 are
             for VLBI.  1-3 use a PD table if available; 6, 7, 8 are
             the same but use the AN (continuum solution) even if a PD
             table is present.
  PDVER......PD table to apply if PCAL was run with SPECTRAL true and
             0 < DOPOL < 6.  <= 0 => highest.
  BLVER......Version number of the baseline based calibration (BL) table
             to apply. <0 => apply no BL table, 0 => highest.
             If the data are not in time order, then BL tables may not
             be applied and BLVER should be set to -1 if any are
             present.
  FLAGVER....specifies the version of the flagging table to be applied.
              0 => highest numbered table.
             <0 => no flagging to be applied.
  DOBAND.....If true (>0) then correct the data for the shape of the
             antenna bandpasses using the BP table specified by BPVER.
             The correction has five modes:
             (a) if DOBAND=1 all entries for an antenna in the table
             are averaged together before correcting the data.
             (b) if DOBAND=2 the entry nearest in time (including
             solution weights) is used to correct the data.
             (c) if DOBAND=3 the table entries are interpolated in
             time (using solution weights) and the data are then
             corrected.
             (d) if DOBAND=4 the entry nearest in time (ignoring
             solution weights) is used to correct the data.
             (e) if DOBAND=5 the table entries are interpolated in
             time (ignoring solution weights) and the data are then
             corrected.
             If the data are not in time order, then only DOBAND=1 may
             be used.
  BPVER......Specifies the version of the BP table to be applied
                0 => highest numbered table.
               <0 => no bandpass correction to be applied.
  SMOOTH.....Specifies the type of spectral smoothing to be applied to
             a uv database . The default is not to apply any smoothing.
             The elements of SMOOTH are as follows:
             SMOOTH(1) = type of smoothing to apply: 0 => no smoothing
               To smooth before applying bandpass calibration
                 1 => Hanning, 2 => Gaussian, 3 => Boxcar, 4 => Sinc
               To smooth after applying bandpass calibration
                 5 => Hanning, 6 => Gaussian, 7 => Boxcar, 8 => Sinc
             SMOOTH(2) = the "diameter" of the function, i.e. width
               between first nulls of Hanning triangle and sinc
               function, FWHM of Gaussian, width of Boxcar. Defaults
               (if < 0.1) are 4, 2, 2 and 3 channels for SMOOTH(1) =
               1 - 4 and 5 - 8, resp.
             SMOOTH(3) = the diameter over which the convolving
               function has value - in channels.  Defaults: 1,3,1,4
               times SMOOTH(2) used when input SMOOTH(3) < net
               SMOOTH(2).
  DOACOR.....> 0 => include autocorrelations as well as cross
             correlation data.
  =================================================================
  IN2NAME....Input UV file name (name).      Standard defaults.
  IN2CLASS...Input UV file name (class).     Standard defaults.
  IN2SEQ.....Input UV file name (seq. #).    0 => highest.
  IN2DISK....Disk number of input UV file.   0 => any.
  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 number of output UV file.  0 => highest with
             space for the file.
  OPCODE.....The operation desired:
             'ADD' adds the time averaged values of the second
             input data set to the first.
             'SUB' subtracts the time averaged values of the
              second input data set from the first - default.
             'MULT' multiplies the time averaged values of the
              second input data set by the first.
             'DIV' divides the time averaged values of the
             second input data set into the first.
  BADDISK....The disk numbers to avoid for scratch files (sorting
             tables mostly).
----------------------------------------------------------------
A use of this task is to remove correlator offsets.  Ed Fomalont
writes:

We don't know what is producing the correlator offsets for the 25 MHz
VLA bandwidths, but these errors produce artifacts near the center of
an image of about 0.4 mJy (correlator mode 4A, 1.4 GHz) with extended
sidelobes.  The artifacts dominate the noise contribution near the
center of the image after less than 30-minutes of integration.  These
correlator offsets vary with baseline, IF, polarization, and channel
in unpredicatable ways.  However, these offsets appear to be constant
over a period of five hours or longer in any the above data streams.

For weak fields where there is little significant emission near the
phase center, a vector average of the data for each
baseline/IF/pol/Channel over an period longer than one hour will be
dominated by the correlator offsets over the noise/source
contribution.  The offsets can then be removed from the visibility
data.  This software already exists because of GMRT data at 610 MHz
that Frazer Owen obtained about a year ago with the same
defect---correlator offsets for each data stream.

    The data reduction processing needed is as follows.

1.  Calibrate and edit the data in the AIPS multi-source files in the
usual manner.  You will usually have one BP table, one SN table which
contains the temporal amplitude and phase corrections derived from the
primary calibrator, and a final CL table.  Also, FG tables for the
necessary flagging of all sources.

2.  Make an image of the target source, and the defects near the
image center should be obvious.

Revised from Ed's description for the new version of UVMTH:

The input data set must have only one subarray and one frequency ID.

3.  Use the AIPS task UVMTH with the data set as both INNAME and
IN2NAME.  Set SOURCES to select the target data which may be from one
or more "sources".  Note: do not select any "source" having
significant emission at the center.  Apply no calibrations or bandpass.
(DOBAND=-1; DOCAL -1): The target data must be uncalibrated since the
correlator errors are inserted (in some unknown way on-line) and any
further calibration will change the correlator offset phases with
time.  Apply all of the FG tables.

4.  UVMTH will subtract (OPCODE='SUB') the averaged target data from
all data in the file.  It will apply no data selection, flagging, or
calibration during this step.  This will produce an output data set
equal to the input data minus the correlator offsets.

5.  UVMTH will copy all tables from the input data set (IN2NAME) to
the output data set, so one may continue with calibration, editing,
etc. on the offset-corrected data.  The resulting image should be much
better than that from the original multi-source file.


