; PCRMS
;---------------------------------------------------------------
;! Finds statistics of a pulse-cal table; flags bad times and channels
;# Task Calibration VLBI
;-----------------------------------------------------------------------
;;  Copyright (C) 2017
;;  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
;-----------------------------------------------------------------------
PCRMS     LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
PCRMS     Finds statistics of a PC table, then flags output PC
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 #
INVERS            0.0              Input PC table version
APARM                              (1) Amp deviation multiplier
                                   (2) RMS deviation multiplier
                                   (3) antenna for OUTFILE
                                   (4) > 0 -> do NOT write
                                       flagged PC table
                                   (5) > 0 - Report residual
                                       phase statistics
                                   (6) > 0 - ignore the edge
                                       channels but do not flag
PRTLEV           -1.0              Print level: -1 very little,
                                   0 what is flagged, 1 various
                                   statistics: rms, bad channels
OUTFILE
                                   Output text file
----------------------------------------------------------------
PCRMS
Task:  This task sorts the input PC table into antenna-time order.  In
       the first pass through the input PC table, the task computes
       the amplitude and phase statistics of the residual PC values
       in each IF and polarization in each antenna, after the fit
       delays and phases have been removed.  It then looks for
       channels that deviate from the mean and channels that stick up
       compared to their immediate neighbors.  In the second pass
       through the data, the channel in each IF/polarization/antenna
       that sticks out is flagged along with the edge channels.  The
       delays may be recomputed (APARM(5) > 0) which will allow phase
       statistics to be reported along with amplitude ones.  The phase
       statistics are not currently used for flagging, so the expense
       of a second round of delay fitting is only for information
       purposes.  In the third pass through the input PC table, if
       APARM(4) <= 0, the average amplitude and amplitude rms after
       flagging the edge channels and those that stuck out is compared
       to the overall average and rms for that IF, polarization, and
       antenna.  Values that deviate too much (APARM(!), and APARM(2))
       are flagged and a new, edited PC table is written.  At the end
       of the third pass, summaries of the number of samples flagged
       for bad channels and the number of spectral windows flagged for
       bad amplitude and rms are reported.

       A file of all values for a particular antenna may be written in
       the second pass if APARM(3) > 0 and OUTFILE is specified.
       PCHIS is able to plot this file.
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.
  INVERS.....PC table version to be studied.  0 -> highest
  APARM......(1) Amplitude averages over an IF that deviate from the
                 mean for that IF by more than APARM(1) * the rms of
                 that mean will be flagged.    0 -> 5
             (2) Amplitude rmses over an IF that deviate from the
                 mean of the rmses for that IF by more than APARM(2) *
                 the rms of that rms will be flagged.  0 -> 5
             (3) Antenna for text file
             (4) > 0 -> do NOT write a new, flagged PC table
                 otherwise one is written
             (5) > 0 -> Report residual phase statistics - this
                 requires the second pass through to solve for delays
                 which is slow.
             (6) > 0 => ignore the edge channels in computing average
                 amplitude and rms in each spectral window but do NOT
                 flag them in the output pC table.  This is useful for
                 input to PCASS.  <= 0 ignore and flag.
  PRTLEV.....<0 -> print only a brief summary of the flagging
              0 -> print details of what got flagged
             >0 -> print which channels stand out, print statistics by
                   antenna, and print details of what is flagged
  OUTFILE....Write up to 8 amplitudes and rmses for every record if
             not ' '.  The IF average amplitude and its rms for the
             first 4 IFs and 2 polarizations are printed.  The output
             text file may be read by PCHIS to plot histograms.
----------------------------------------------------------------
