; VLBAMPCL
;---------------------------------------------------------------
;! Calculates and applies manual instrumental phase calibration
;# PROCEDURE VLBI UTILITY CALIBRATION
;-----------------------------------------------------------------------
;;  Copyright (C) 2002, 2006, 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
;-----------------------------------------------------------------------
;---------------------------------------------------------------
VLBAMPCL  LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
VLBAMPCL: Procedure to manually correct instrumental phases
                                   You must type RUN VLBAUTIL
                                   to see all the inputs to
                                   this procedure.
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 #
TIMERANG                           Time range of a calibrator
                                    1-4 = start day,hr,min,sec
                                    5-8 = end   day,hr,min,sec
                                   MUST BE SPECIFIED
BCHAN             0.0     2048.0   Lowest channel number 0=>all
ECHAN             0.0     2048.0   Highest channel number
REFANT             0.0      20.0   Ref. antenna, 0=>the first
SUBARRAY           0.0    9999.0   Subarray;  0 => all.
CALSOUR                            Calibrator source name;
                                   blank => any
GAINUSE                            CL table to use; 0=>highest
                                   ADVERBS IF 2 SCANS MUST BE
                                   USED TO CORRECT ALL ANTENNAS:
OPCODE                             'CALP' if you are using 2
                                   scans, otherwise ''
TIME2                              Time range of a calibrator
                                    1-4 = start day,hr,min,sec
                                    5-8 = end   day,hr,min,sec
SOURCES                            Calibrator source name for
                                   TIME2 scan; blank => any
ANTENNAS                           antennas for which manual
                                   phase corrections should be
                                   obtained for the scan in
                                   TIME2 (i.e. that cannot be
                                   corrected with TIMERANG).

----------------------------------------------------------------
VLBAMPCL
Type: Procedure
Use:  VLBAMPCL is a procedure that creates a CL table which
      corrects the instrumental phases and delays using
      FRING.  It will run FRING twice if there are no
      scans that have good fringes on all antenna.  This
      should be run after VLBACALA (or VLBAPANG for
      polarization experiments) but before VLBAFRNG/VLBAKRNG.
      Type RUN VLBAUTIL to make the VLBAMPCL procedure
      available.

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.
  TIMERANG...Time range of the calibrator to be used. In order:
             Start day, hour, min. sec,
             End day, hour, min. sec. Days relative to
             reference date.
             MUST BE SPECIFIED.  Pick a scan on a calibrator where
             ALL antennas are present and have strong fringes.  If
             there is no such scan, then use OPCODE, TIME2 and ANTENNAS
             another scan and set of antennas.
  BCHAN......First channel to use. 0=>all.
  ECHAN......Highest channel to use. 0=>all higher than BCHAN
             You may want to select BCHAN and ECHAN to leave out
             edge channels if those are bad and will affect the
             phase and delay solutions.
  REFANT.....Reference antenna, 0 => the first in the antenna list
             Set to a stable antenna that is preferably in the entire
             experiment
  SUBARRAY...The subarray to use. 0 => all
  CALSOUR....Calibrator source name for scan in TIMERANG; blank => any.
  GAINUSE....Number of CL table with all calibration up to this point.
             0 => highest
  OPCODE.....OPCODE in CLCAL; use the default if only one scan is
             needed to correct all the antennas.  If OPCODE is set
             to 'CALP' and ANTENNAS and TIME2 are set then
             instrumental phase correction will be found for a
             second set of antennas (listed in ANTENNAS) for
             another scan (specified by TIME2).  If OPCODE='CALP'
             but ANTENNAS and TIME2 are not set then only the
             phase corrections that are corrected by the scan
             specified in TIMERANG will be corrected.  See EXPLAIN
             file for a detailed explanation of how to deal with
             antennas which are not corrected after VLBAMPCL.
  TIME2......Time range of the calibrator to be used for second scan.
             In order: Start day, hour, min, sec, End day, hour, min,
             sec. Days relative to reference date.
             MUST BE SPECIFIED IF OPCODE AND ANTENNAS ARE SPECIFIED.
             Pick a scan on a calibrator where antennas listed in
             ANTENNAS have strong fringes.
  SOURCES....Calibrator source name for scan in TIME2; blank => any.
  ANTENNAS...Antennas that will not be corrected with the scan
             specified in TIMERANG but will be corrected by the
             scan specified in TIME2.  OPCODE must be set to
             'CALP' for this option.  0=> do not find manual phase
             correction for a second scan.  MUST BE SPECIFIED IF
             TIME2 IS SPECIFIED.
----------------------------------------------------------------
VLBAMPCL:           Procedure to correct the instrumental phases
                    using FRING.  It can find corrections for
                    a second scan if there are no scans that contain
                    all antennas.
Documenter:         Amy Mioduszewski
Related Programs:   VLBAUTIL, CLCAL, FRING, VLBAPCOR

This procedure should be used if there are no good phase
cals (e.g. spectral line experiments), otherwise use
VLBAPCOR.

This should be done after sampler corrections (VLBACCOR) and
before the bandpass (VLBABPSS).  After VLBAMPCL is run it is
ESSENTIAL to check the solutions in POSSM setting GAINUSE to
the CL table output by VLBAMPCL.

VLBAMPCL assumes:

1. there is only one FREQID

2. that all IFs should be calibrated

6. that the highest FG table is the one that should be applied

Steps in VLBAMPCL:

 1. Runs FRING, TIMERANG, CALSOUR, GAINUSE and REFANT as
    specified; DOCAL=2, APARM(1)=2 and DPARM=1 0 0 0 0 0 0 1.

 2. Runs CLCAL, with input OPCODE; ANTENNAS=0; INTERPOL='';
    SNVER= SN table out of FRING; GAINVER = input GAINUSE;
    and GAINUSE= highest CL table +1.  Output should be highest
    CL table.

*3. Runs FRING, using the GAINUSE used in CLCAL in step 2.
    ANTENNAS=ANTENNAS, REFANT, rest the same as in step 1.
    If FRING reports any "failed solutions" you MUST find
    another scan and run again, after you have deleted the SN
    and CL tables that VLBAPCOR has created.  Unless there is a
    good reason for failed solutions, such as an entire IF has
    been flagged for a certain antenna.

*4. Runs CLCAL with ANTENNAS equal to the input ANTENNAS
    GAINVER equal to input GAINUSE; SNVER equal to output
    SN table from FRING; GAINUSE equal to the CL table
    which is output in step 2; TIMERANG =0; and
    CALSOUR = ''.

*NOTE: Steps 3 and 4 only take place if OPCODE='CALP' and
TIME2 and ANTENNAS <> 0.
