; PCLOD
;---------------------------------------------------------------
;! Reads ascii file containing pulse-cal info to PC table.
;# Task Calibration VLBI
;-----------------------------------------------------------------------
;;  Copyright (C) 1995-1999, 2008, 2012, 2016
;;  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
;-----------------------------------------------------------------------
PCLOD     LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
PCLOD     Task which reads pulse-cal information.
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 #
CALIN                              Text file name.
SUBARRAY                           Subarray number (0=>1)
FQTOL                              Freq. tol. in IF match (kHz)
                                   <=0 => 0.1*WBAND
                                   works in majority cases
OUTVER            0.0    46655.0   Output PC table, 0=>new table
                                   If selected table exists, the
                                   new data added to it.
----------------------------------------------------------------
PCLOD
Task:  This task reads an ascii file containing phase (pulse) cal
       information .  It can read the modern DiFX creatted file which
       has a simple format and it can read the old KEYIN-style tables
       generated by the VLBA monitor.  The latter contain cable cal
       and state count data as well. It is purely a format transfer
       task, just producing an AIPS PC table which is a copy of the
       data in the ascii file.
          Phase cal data from VLBA antennas is included in FITS files
       from the VLBA correlator from April 1 1999 onwards.  The modern
       DiFX file contains a pulse cal at every MHz while the PC table
       in the FITS-IDI file may contain only a few tones.
          The UV data set must exist and is used to convert antenna
       names to antenna numbers and times to source and FQID numbers.
       The code will adopt sensible defaults if the input data set is
       not specific to the current PC table.
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.
  CALIN......Phase (pulse) - cal text file.
             If the name ends in an _ (underscore) the the files with
             BR, FD, HN, ... SC will be read in order and having a
             file missing will generate a warning but not a failure.
  SUBARRAY...Subarray number                 0 => 1
  FQTOL......Freq. tol. in IF match (KHz)    <=0 => 0.1*WBAND
                                             works in majority cases
             Ignored for DiFX files.
  OUTVER.....Desired output PC table.
             0 means create a new PC table under following number.
             If selected table exists, the new data added to it
----------------------------------------------------------------
PCLOD : Reads pulse-cal info into AIPS PC table.
DOCUMENTOR : P.J.Diamond, A.J. Kemball  NRAO/AOC

                        COMMENTS

Phase calibration information for VLBA antennas is included in FITS
files from the VLBA correlator from April 1 1999 onwards.  PCLOD need
only be run to load amplitude calibration data that was not
transferred automatically.  This may include PC data with a tone
overey MHz from the DiFX correlator.

CALIN:

NEW FORMAT

# DiFX-derived pulse cal data
# File version = 1
# Start MJD = 57687
# Start seconds = 36858
# Telescope name = BR
BR 57687.4266088 0.0000231 0 8 64 4773 R -1.76082e-03 -2.41309e-03
   where the line above extends many thousands of characters listing
   all  tones in the first IF for R and then all tones in the first IF
   for L.  The second IF follows etc.  The first data values in the
   line are the antenna name, the time in days, the time interval in
   days, ??, the number of IFs times the number of polarizations, and
   the number of tones per IF.  The next NumIF * NumPol * NumTone
   groups of 4 data values are the frequency (MHz), the polarization,
   the real and the imaginary value.

   This line is followed by similar lines for each time for the
   particular antenna.  Each antenna is in a separate file.  The files
   for the VLBA will be read in order of the antenna names if CALIN
   ends in an _.

OLD FORMAT:

     PCLOD requires an ASCII file containing the VLBA pulse
calibration data. This file has the format given below and can be
obtained from the VLBA correlator (the VLBA technical contact person
assigned to the project can provide information in this regard).

     The are two type of "groups" in the PCLOD ascii file.
A FREQUENC group that describes the frequency structure of the
table and a PULSE group that contains the data.
The format of a file is shown below:


  FREQUENC SC /
 ! Tone  Chan  SB    Pol ToneSkyFreq #bits   LoFreq     BW
 ! A ToneSkyFreq of 0.00 means that that tone number carries
 ! the state count information.
   'T01'   1  'U'   'RCP'    8418.00   1    8417.49   8000.0
   'T02'   2  'U'   'LCP'    8418.00   1    8417.49   8000.0
   'T03'   3  'U'   'RCP'    8426.00   1    8425.49   8000.0
   'T04'   4  'U'   'LCP'    8426.00   1    8425.49   8000.0
   'T05'   1  'U'   'RCP'    8425.00   1    8417.49   8000.0
   'T06'   2  'U'   'LCP'    8425.00   1    8417.49   8000.0
   'T07'   3  'U'   'RCP'    8433.00   1    8425.49   8000.0
   'T08'   4  'U'   'LCP'    8433.00   1    8425.49   8000.0
   'T09'   1  'U'   'RCP'       0.00   2    8417.49   8000.0
   'T10'   2  'U'   'LCP'       0.00   2    8417.49   8000.0
   'T11'   3  'U'   'RCP'       0.00   2    8425.49   8000.0
   'T12'   4  'U'   'LCP'       0.00   2    8425.49   8000.0
   /
 PULSE-CA  SC  /
! CC indicates the cable-cal, first number is the cable-cal itself in
! picoseconds, second is the integration time for the measurment in
! seconds. The numbers following the Txx indicators are the amplitude
! and phase of the phase-cal signal.
282 13:08:06 'CC'  798.39 126.0
282 13:08:06 'T01'   2.5824  -1.851
282 13:08:06 'T02'   2.3248 -60.500
282 13:08:06 'T03'   3.0140  91.939
282 13:08:06 'T04'   3.0608 103.487
282 13:08:06 'T05'   2.7398  50.074
282 13:08:06 'T06'   2.1943  -5.036
282 13:08:06 'T07'   2.5192 147.145
282 13:08:06 'T08'   2.2222 171.769
! The numbers following the state-count indicators are the percentage
! of time that the sampler spends in one of the allowed states.
282 13:08:06 'T09'  18.9481  30.560  32.7219  17.770
282 13:08:06 'T10'  15.2009  35.609  34.6223  14.568
282 13:08:06 'T11'  19.8558  29.822  32.0483  18.274
282 13:08:06 'T12'  15.6230  35.287  34.1405  14.950
282 13:10:05 'CC'  798.23  99.0
282 13:10:05 'T01'   2.5835  -1.867
282 13:10:05 'T02'   2.3312 -60.507
282 13:10:05 'T03'   3.0155  91.729
282 13:10:05 'T04'   3.0630 103.533
282 13:10:05 'T05'   2.7371  50.022
282 13:10:05 'T06'   2.1990  -5.029
282 13:10:05 'T07'   2.5227 147.145
282 13:10:05 'T08'   2.2262 171.817
282 13:10:05 'T09'  18.8935  30.615  32.7767  17.715
282 13:10:05 'T10'  15.1552  35.657  34.6652  14.523
282 13:10:05 'T11'  19.8069  29.872  32.0961  18.225
282 13:10:05 'T12'  15.5761  35.337  34.1825  14.904
   /
  FREQUENC SC /
 ! Tone  Chan  SB    Pol ToneSkyFreq #bits   LoFreq     BW
   'T01'   1  'U'   'RCP'    1664.00   1    1663.49   8000.0
   'T02'   2  'U'   'LCP'    1664.00   1    1663.49   8000.0
   'T03'   3  'U'   'RCP'    1672.00   1    1671.49   8000.0
   'T04'   4  'U'   'LCP'    1672.00   1    1671.49   8000.0
   'T05'   1  'U'   'RCP'    1671.00   1    1663.49   8000.0
   'T06'   2  'U'   'LCP'    1671.00   1    1663.49   8000.0
   'T07'   3  'U'   'RCP'    1679.00   1    1671.49   8000.0
   'T08'   4  'U'   'LCP'    1679.00   1    1671.49   8000.0
   'T09'   1  'U'   'RCP'       0.00   2    1663.49   8000.0
   'T10'   2  'U'   'LCP'       0.00   2    1663.49   8000.0
   'T11'   3  'U'   'RCP'       0.00   2    1671.49   8000.0
   'T12'   4  'U'   'LCP'       0.00   2    1671.49   8000.0
   /
 PULSE-CA  SC  /
282 13:12:56 'CC'  798.53 126.0
282 13:12:56 'T01'   2.6047  -1.572
282 13:12:56 'T02'   2.3082 -60.806
282 13:12:56 'T03'   3.0314  91.596
282 13:12:56 'T04'   3.0592 104.250
282 13:12:56 'T05'   2.7418  49.977
282 13:12:56 'T06'   2.1886  -5.497
282 13:12:56 'T07'   2.5224 146.614
282 13:12:56 'T08'   2.2276 172.430
282 13:12:56 'T09'  18.8792  30.628  32.7908  17.702
282 13:12:56 'T10'  15.1499  35.663  34.6708  14.516
282 13:12:56 'T11'  19.7975  29.880  32.1077  18.215
282 13:12:56 'T12'  15.5682  35.347  34.1906  14.894
282 13:13:36 'CC'  798.54  27.0
282 13:13:36 'T01'   2.6047  -1.572
282 13:13:36 'T02'   2.3082 -60.806
282 13:13:36 'T03'   3.0314  91.596
282 13:13:36 'T04'   3.0592 104.250
282 13:13:36 'T05'   2.7418  49.977
282 13:13:36 'T06'   2.1886  -5.497
282 13:13:36 'T07'   2.5224 146.614
282 13:13:36 'T08'   2.2276 172.430
282 13:13:36 'T09'  18.8792  30.628  32.7908  17.702
282 13:13:36 'T10'  15.1499  35.663  34.6708  14.516
282 13:13:36 'T11'  19.7975  29.880  32.1077  18.215
282 13:13:36 'T12'  15.5682  35.347  34.1906  14.894
282 13:17:46 'CC'  798.76 126.0
282 13:17:46 'T01'   4.2002 136.092
282 13:17:46 'T02'   3.8132-156.255
282 13:17:46 'T03'   4.2956 106.033
282 13:17:46 'T04'   4.1356 130.609
282 13:17:46 'T05'   3.0393-116.729
282 13:17:46 'T06'   3.2789 -61.897
282 13:17:46 'T07'   2.8807-153.038
282 13:17:46 'T08'   2.7692-121.834
282 13:17:46 'T09'  19.6543  30.302  32.0068  18.037
282 13:17:46 'T10'  15.3838  35.226  34.5318  14.859
282 13:17:46 'T11'  18.7419  30.857  32.1079  18.293
282 13:17:46 'T12'  17.0928  32.913  33.7245  16.270
   /
