; UVCRS
;---------------------------------------------------------------
;! Finds the crossing points of UV-ellipses.
;# TASK UV CALIBRATION
;-----------------------------------------------------------------------
;;  Copyright (C) 1995
;;  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
;-----------------------------------------------------------------------
;---------------------------------------------------------------
UVCRS     LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
UVCRS     Task to find crossing points of UV-ellipses
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 #
BIF                                IF number; 0 => 1
BCHAN                              Channel number; 0 => 1
STOKES                             Stokes parameter; Recognized 
                                   values are: 'RR  ', 'LL  '
ANTENNAS                           The well calibrated antennas
                                   whose gains are forced to 1 
                                   and not corrected
SNVER                              SN table number; 
                                   <0 => SN table is not made
                                    0 => highest 
OUTFILE                            Name of the file to write the
                                   crossings and ampl. ratios
APARM                              Control parameters
                                   APARM(1) UVRADX in megalamda; 
                                            0 => 0.5
                                   APARM(2) UVRADY in megalamda;
                                            0 => 0.5
                                   APARM(3) 0 => rect. area;
                                            1 => elliptical area
                                   APARM(4) 0 => no print  
                                            1 => print results
                                                 on screen
----------------------------------------------------------------
UVCRS
Task:  This task finds the crossing points of UV-ellipses, 
       calculates the average ratios of the amplitudes at the
       crossing points and finds the solution for the gains of
       antennas by a least squares approach minimizing the
       deviation of the ratios from 1	
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.
  BIF........IF number; 0 => 1
  BCHAN......Channel number; 0 => 1
  STOKES.....Stokes type. Recognized values are: 'RR  ', 'LL  '
  ANTENNAS...The numbers of well calibrated antennas whose gains 
             are forced to 1 and not corrected
  SNVER......SN table number; <0 => SN table is not made; 
             0 => highest;  
  OUTFILE....Name of the file in which to write the crossing 
             points and ratios. Standard form, e.g 'MYAREA:OUT'
             where OUT is the name of the file to be created,
             and MYAREA is an environment variable defined
             before starting up AIPS.
  APARM......Control parameters
  APARM(1)...UVRADX at megalamda; 0 => 0.5
  APARM(2)...UVRADY at megalamda; 0 => 0.5
  APARM(3)...Type of crossings region; 0 => rectangular;
                                       1 => ellipse
  APARM(4)...To print or not some intermidiate results on the 
             screen; 0 => no print;
                     1 => print
----------------------------------------------------------------
	The program finds all crossings points of UV-ellipses.
The two points from two UV-ellipses are defined as 'crossing' 
if the vector connecting them is located inside a given region. 
The region can be rectangular or elliptical. The type of the
region and its size are determined by the input parameters 
APARM. The logarithm of a ratio of an amplitude of a given point
and the amplitudes of all 'crossing' points of a neighbouring
UV ellipse is calculated. Then the average logarithm of the 
ratios for a given point is constructed. Finally all these 
logarithms for all crossingspoint of a given UV-ellipse are 
averaged. This final average logarithm of the amplitude ratio
is associated with the given pair of baselines. The logarithm
of the amplitude ratios is preferable than the ratios themselves
for several reasons:

1..We have to consider the ratios 0.1 and 10 to be equally bad 
   rather than 0.1 and 1.9
2..Averaging of logarithms (for ratios close to 1) is equivalent
   to averaging of deviations of the ratios from 1.
3..Averaging of logarithm excludes the ambiguity occuring when 
   we are using the reciprocals of ratios. For example 
   0.5(1 + 3)=2. But 0.5(1/1 + 1/3)=2/3; And 1/2 is not equal 
   2/3. 
   The same example when averaging logarithms:
   0.5(log(1)+log(3))=0.5log(3); average ratio = exp(0.5log(3))=
   sqrt(3); reciprocal case: 0.5(log(1)-log(3))=-0.5log(3);
   average ratio = exp(-0.5log(3))=1/sqrt(3);
4..The use of logarithms produces a linear system of equations 
   connecting logarithms of antennas gains and ratios. So we 
   can use a linear least square method to determine the
   solution for the antennas gains minimizing the deviation
   of the ratios from 1.

The program prints the results in the OUTFILE. The sign '?' 
before a ratios lines indicates the case of too large a 
deviation of the ratios from 1 (RATIO > 2; or RATIO < 0.5). 
The second column's integer indicates the number of crossings
that occured for a given pair of baselines. The last two 
columns in the table represent the original ratios and the 
ratios after applying the solution found for the antennas 
gains.

If you desire to use the solution for different IFs and
different polarizations you have to run the program for 
the first IF and polarization from your list with 
'snver'=0 and then repeat the program in consecutive order for
all the polarizations and IFs in your data using the same 
number of 'snver'. 

			Limitations

1.Number of visibilities less than 50000.
2.Duration of an experiment has to be less than 24 hours


