; DOVLAMP
;---------------------------------------------------------------
;! Produces amp calibration file for phased-VLA VLBI data
;# RUN POPS VLBI VLA UTILITY CALIBRATION
;-----------------------------------------------------------------------
;;  Copyright (C) 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
;-----------------------------------------------------------------------
;---------------------------------------------------------------
DOVLAMP   LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
DOVLAMP : Procedure to produce ANTAB-style file for phased-VLA
ASDMFILE                           Full path name to ASDM dir.
CONFIG            0.0              Select this "configuration"
INNAME                             In/output file name
INCLASS                            In/output file class
INSEQ                              In/output file sequence num
INDISK                             Disk # for in/output file
CALSOUR                            FOR FLUX CALIBRATOR ONLY
REFANT                             Reference antenna
OUTTEXT                            Directory to put output file
----------------------------------------------------------------
DOVLAMP
Type: Procedure
Use: This procedure us usually used by NRAO operations to make
phased-VLA VLBI amplitude calibration files and is not intended for
general usage.  However people with phased-VLA data might want to
improve on their amplitude calibration by, for example, flagging the
switched power before running this procedure.

This procedure optionally loads a SDM (VLA) file and then makes
amplitude calibration information for the phased Jansky VLA (observed
after February 2011) when it is used as a VLBI element.  The procedure
processes the VLA only data from WIDAR and using the switched power
and optional primary amplitude calibrator to produce a text file that
can be loaded into the TY and GC table of the VLBI data with ANTAB.

      Type RUN DOVLAMP to define the DOVLAMP procedure.

  ASDMFILE...To load the data automatically from disk, set this.
             The full path name to and including the top directory of
             the ASDM/BDF data.  Two 64-character strings may be used
             for long names.  The ASDMFILE(2) will be appended to the
             non-blank portion of ASDMFILE(1) to generate the name
             used.  The code will also manage when ASDMFILE(1) begins
             with either '$xxxx/' or 'xxxx:' where xxxx is a logical
             (environment) variable.  The filename sent to OBIT will
             be the translation of $xxxx followed by a '/' followed by
             an remaining non-blank characters in ASDMFILE(1) followed
             by ASDMFILE(2).
             If ASDMFILE is set then CONFIG, INNAME, INCLASS and INSEQ
             must be set.
  CONFIG.....ASDM files often contain more than one "configuration"
             numbered from 0 to N.  Configurations may differ in
             observing band, number of spectral windows, number of
             spectral channels in the spectral windows, etc.  Use
             BDFLIST to see what configurations are in your data set.
             DOVLAMP can load only one configuration at a time.
  INNAME.....The name of the file to be processed.  If ASDMFILE is set
             then this will be the output name.
  INCLASS....The class of the file to be processed.  If ASDMFILE is set
             then this will be the output class.  If INCLASS='TYAPL'
             then it is assumed that TYAPL has been run and the
             procedure will not run it again.
  INSEQ......The sequence number of the file to be processed. Zero
             defaults to the highest sequence number of a file
             matching INNAME, INCLASS, and INDISK.   If ASDMFILE is
             set then this will be the output sequence.
  INDISK.....The disk drive number of the file to be corrected.
  CALSOUR....Primary flux calibrator, if present in observations, if
             left blank a primary flux calibrator will be searched for
             in the SU table.
  REFANT.....Reference antenna.
  OUTTEXT....Directory to put output file.  Path or environmental
             variable will work.  The name of the output file is
             automatically generated.  The path must be shorter than
             27 characters (including final /).
----------------------------------------------------------------
DOVLAMP:            Procedure to create a-priori calibration for the
                       phased VLA
Documentor:         Amy Mioduszewski
Related Programs:   VLAMP, CALIB, CLCAL, CALRD, TYAPL

This procedure is intended to produce the amplitude calibration for
the phased VLA (after February 2011) when it is used as a VLBI
element.  The VLA data can be loaded from disk, if needed.  It
produces a text file that can be loaded with ANTAB.

The main task used in DOVLAMP is VLAMP, which takes the switched power
converts that to the system temperature (Tsys) and then takes the
harmonic sum of the Tsys over all the antennas multiplied by the
amplitude in the first CL table.  The amplitude in the first CL table
has the gain curve polynomial in it.  The "Gain" (which really isn't
the gain because some of that is folded into the harmonic sum of the
Tsys) is the sum of the sensitivities of the antennas in the GC table.
Then VLAMP makes the ANTAB style text file that can be loaded into the
VLBI data.

DOVLAMP adds some steps in order to improve the calibration.  If a
primary amplitude calibrator was observed (3C286, 3C48, 3C147, 3C138
or 3C295) then it will be used to improve the calibration.  This
calibrator can either be specified in CALSOUR or, if CALSOUR is blank,
will be found automatically in the SU table.

If INCLASS='TYAPL' then it is assumed that TYAPL has been run and the
procedure will not run it again.  This is useful if there has been an
problem later in the procedure since the run of TYAPL is usually the
longest part of the procedure.

Basic steps of procedure:

0) Perform basic checks on inputs

1) Determine if file needs to be loaded (i.e., is ASDMFILE set)
   --Yes, run BFD2AIPS, user INNAME, INCLASS and INSEQ as output name
   --No, run rest of script on file set in INNAME, INCLASS and INSEQ

2) If INCLASS is *not* TYAPL then
   --run TYSMO to clip bad points in SY table
   --run TYAPL

From now on everything is done to the TYAPL file:
3) If CALSOUR is not set, search for standard VLA flux calibrator
   (3C48, 3C138, 3C147, 3C286 or 3C295) in the SU table.

4) If flux calibrator exists in data then:
   --Load model for flux calibrator is one exists in the data
   --run SETJY on flux calibrator
   --run CALIB on flux calibrator
   --zero phases on CALIB solutions (want amp only)
   --run CLCAL to apply amp corrections to first CL table

5)  Run VLAMP to create ANTAB style file.
