; ACFIT
;---------------------------------------------------------------
;! Determine antenna gains from autocorrelations
;# TASK CALIBRATION VLBI SPECTRAL
;-----------------------------------------------------------------------
;;  Copyright (C) 1995, 1997, 2000, 2004, 2006
;;  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
;-----------------------------------------------------------------------
ACFIT     LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACFIT     Task to determine antenna gains from autocorrelations
USERID                             User number
INNAME                             Input UV file name (name)
INCLASS                            Input UV file name (class)
INSEQ             0.0     9999.0   Input UV file name (seq. #)
INDISK            0.0        9.0   Input UV file disk unit #

                                   Data Selection
CALSOUR                            Sources to use to determine
                                   gains.
QUAL            -10.0              Calibrator qualifier -1=>all
CALCODE                            Calibrator code '    '=>all
SELBAND                            Bandwidth to select (kHz)
SELFREQ                            Frequency to select (MHz)
FREQID                             Freq. ID to select.
TIMERANG                           Time range to select
SUBARRAY          0.0     1000.0   Subarray, 0=>1
ANTENNAS                           Antennas to select

                                   Template file
IN2NAME                            Template UV file name (name)
IN2CLASS                           Template UV file name (class)
IN2SEQ            0.0     9999.0   Template UV file name (seq.#)
IN2DISK           0.0        9.0   Template UV file disk unit #
REFANT                             Reference antenna.
                                   Control options
DOCALIB          -1.0      101.0   > 0 calibrate data & weights
                                   > 99 do NOT calibrate weights
GAINUSE                            CL (or SN) table to apply
FLAGVER                            Flag table version
SOLINT                             Solution interval
DOBAND                             If > 1 apply BP correction
BPVER                              BP table version to write
SMOOTH                             Smoothing function. See
                                   HELP SMOOTH for details.
                                   Does not apply to template
                                   spectrum.
BCHAN             0.0     2048.0   Start channel for fit 0=>1
ECHAN             0.0     2048.0   Stop channel for fit 0=>all
APARM                              Control information:
                                   (1) Polynomial degree for
                                       source spectrum baseline
                                   (2) Polynomial degree for
                                       template spect. baseline
                                   (3) Rpol sensitivity of
                                       template antenna (Jy/K).
                                       Use this if only one
                                       polzn in data.
                                   (4) Lpol sensitivity of
                                       template antenna (Jy/K)
                                   (5) Minimum allowed relative
                                       gain value, 0 => 0
                                   (6) Maximum allowed relative
                                       gain value, 0 => all
                                   (7) Maximum allowed relative
                                       gain error, 0 => all
                                   (8) Print level, 1=> print
                                       gains.
                                   (9) >0 => Baseline
                                       independent fit.
                                   (10)>0 => write out AC data
                                       after baseline removed
BPARM                              Array of start and stop
                                   channel numbers for baseline
                                   fitting on source spectrum.
CPARM                              Array of start and stop
                                   channel numbers for baseline
                                   fitting on template spectrum.
XPARM                              Array of Rpol Tsys(IF) of
                                   template scan. 0 => 1.0.
                                   Use XPARM if only one polzn
                                   in database.
YPARM                              Array of Lpol Tsys(IF) of
                                   template scan. 0 => 1.0
SNVER             -1.0    46655.0  Output SN table, 0=>new table
OUTNAME                            Output UV file name (name)
OUTCLASS                           Output UV file name (class)
OUTSEQ            0.0     9999.0   Output UV file name (seq. #)
OUTDISK           0.0        9.0   Output UV file disk unit #
BADDISK           0.0     9999.0   Disks to avoid for scratch
----------------------------------------------------------------
ACFIT
Task: To calibrate spectral line data using total-power spectra.
The method used is based on the 'template' spectrum method
developed for the calibration of spectral line VLBI data.
You should generate a 'template' spectrum, i.e. a high-quality
spectrum from a scan taken at a sensitive antenna at a
reasonable elevation. Use SPLIT and UVCOP to write this spectrum
into a seperate uv-file. ACFIT will then fit the template to
all other total-power spectra from all antennas (using a
linear least-squares algorithm) and generate an SN table which
contains the relative gains of the antennas as a function of
time. These can be translated to absolute gains simply by
multiplication by the measured Tsys and Gain of the template
antenna.
Adverbs:
  USERID.....Input file user number.  0 => logon user,
             32000 => any user.
  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.
  CALSOUR....List of sources to be used to determine gains.
             All ' ' = all sources; a "-" before a source name.
             means all except ANY source named. If the data
             file is a single source file no source name need
             be specified.
  QUAL.......Only sources with a source qualifier number in the
             SU table matching QUAL will be used if QUAL is not
             -1.
  CALCODE....Calibrators may be selected on the basis of the
             calibrator code:
                  '    ' => any calibrator code selected
                  '*   ' => any non blank code (cal. only)
                  '-CAL' => blank codes only (no calibrators)
                  anything else = calibrator code to select.
             NB: The CALCODE test is applied in addition to the
             other tests, i.e. CALSOUR and QUAL, in the
             selection of sources for which to determine
             solutions.
  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, 0=> all
  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, 0=> all
  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 overide that of FREQID.
             However, setting SELBAND and SELFREQ may result in
             an ambiguity, in which case the task will request
             that you use FREQID.
  TIMERANG...Time range of the data to be selected. In order:
             Start day, hour, min. sec,
             end day, hour, min. sec. Days relative to ref.
             date.
  SUBARRAY...Subarray number to select. 0=>1.
  ANTENNAS...A list of the antennas for which gains are
             to be determined..
             If any number is negative then all antennas listed
             are NOT to be used and all others are.
The following specify the template spectrum file.
  IN2NAME....Template UV file name (name).      Standard defaults.
  IN2CLASS...Template UV file name (class).     Standard defaults.
  IN2SEQ.....Template UV file name (seq. #).    0 -> highest.
  IN2DISK....Disk drive # of template UV file.  0 => any.
  REFANT.....Antenna to select from the template file whose
             autocorrelation spectrum is to be used as the
             template, useful if have multiple antennas in the
             template file.
  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).
             The calibration is applied prior to the gain
             determination.
  GAINUSE....version number of the CL table to apply to
             multisource files or the SN table for single
             source files.  0 => highest.
  FLAGVER....specifies the version of the flagging table to be
             applied. 0 => highest numbered table.
             <0 => no flagging to be applied.
  SOLINT.....the interval (mins) over which to average the data
             before solving for the gains. (0 => scan)
  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.
  BPVER......the version of the BP table to use. (0 => highest)
  SMOOTH.....Specifies the type of spectral smoothing to be applied to
             a uv database . The default is not to apply any smoothing.
             The smoothing function is only applied to the spectra
             from the main database, it is not applied to the template
             spectrum. That should be smoothed by SPLIT if so desired.
             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).
  BCHAN......Start channel of region of spectrum to be used for
             for fit, 0 => 1.
  ECHAN......End channel of region of spectrum to be used for
             for fit, 0 => all.
  APARM......Task enrichment parameters.
             (1) => degree of polynomial to be removed from
                   the source spectrum. The channel ranges over
                   which to determine polynomial baseline are
                   set by BPARMS.
             (2) => Degree of polynomial to be removed from
                   the template spectrum. Channel range
                   specified in CPARM.
             (3) => The gain of the RHC polzn of the template
                    antenna. (Jy/deg), 0 => 1.0
                    If only one polzn is present in the database
                    use APARM(3) to specify the antenna gain.
             (4) => The gain of the LHC polzn of the template
                    antenna (Jy/deg), 0 => 1.0. Ignored if
                    only one polzn in data.
             (5) => Minimum relative gain accepted (before
                    multiplication by Tsys and Jydeg. 0 => 0
             (6) => Maximum relative gain accepted (before
                    multiplication by Tsys and Jydeg. 0 => all
             (7) => Maximum percentage relative gain error
                    accepted, 0 => all. Recommend APARM(7)=20,
                    i.e. 20%
             (8) => print level, 0 => hardly anything
                    1 => relative gain found for each antenna and
                    each averaging interval.
                    2 => gory details
             (9) => Subtract baselines then fit gain => 0
                    Baseline-independent fit         => 1.
                    If using a baseline-independent fit then
                    BCHAN and ECHAN should include channels
                    containing no emission.
            (10) => If > 0 and APARM(9)=0 then will write out
                    the total power spectra with spectral
                    baselines removed to an output file
                    given by OUTNAME etc.
   BPARM......Array of start and stop channels to use for
              fitting a polynomial to any residual spectral
              baseline. Order of polynomial is given by
              APARM(1). If all 0, no residual baseline will be
              removed.
   CPARM......Same as BPARM but used for the template spectrum
              Order of polynomial is given by  APARM(1). If all
              0, no residual baseline will be
              removed.
   XPARM......Array of RHC Tsys(IF)(K) - system's temperature of
              the template scan for different IFs. 0 => 1.0.
              If only one polzn in the database use XPARM to
              specify the Tsys values.
   YPARM......Array of LHC Tsys(IF)(K) - system's temperature of
              the template scan for different IFs. 0 => 1.0.
              If only one polzn in the database then YPARM
              will be ignored.
   OUTNAME....Output UV file name (name).     Standard defaults.
   OUTCLASS...Output UV file name (class).    Standard defaults.
   OUTSEQ.....Output UV file name (seq. #).   0 => highest.
   OUTDISK....Drive # of output UV file.      0 => highest with
              space for the file.
   BADDISK....A list of disks on which scratch files are not to
              be placed.  This will not affect the output file.
----------------------------------------------------------------
