; UVLSD
;---------------------------------------------------------------
;! least squares fit to channels and divides the uv data.
;# TASK UV SPECTRAL EDITING
;-----------------------------------------------------------------------
;;  Copyright (C) 1995, 2000
;;  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
;-----------------------------------------------------------------------
UVLSD     LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
UVLSD     Least squares fit baseline and divides the uv data.
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 #
OUTNAME                            Output UV file name (name)
OUTCLASS                           Output UV file name (class)
OUTSEQ           -1.0     9999.0   Output UV file name (seq. #)
OUTDISK           0.0        9.0   Output UV file disk unit #.
BCHAN                              Lowest channel to write
ECHAN                              Highest channel to write
ICHANSEL                           Select channels to fit: NOTE
                                   this is start,end,increment
                                   and IF for each region
ORDER             0.0        1.0   Order of fit line (0 -> DC)
DOOUTPUT         -1.0        1.0   > 0 => write fit baseline as
                                   a continuum uv data base
CHANNEL           0.0     9999.0   Channel of fit to be written
                                   as continuum: 0 -> reference
FLUX              0.0              Flag data if residual flux in
                                   any fit channel is > FLUX
CUTOFF            0.0              Flag data if RMS of residual
                                   in fit channels > CUTOFF
SHIFT                              Shift in asec to RA, DEC
----------------------------------------------------------------
UVLSD
Task:  This task does a spectral baseline division.  It fits a
       straight line to the real and imaginary parts of selected
       channels and divides the fitted baseline into the
       spectrum.  Optionally, it can flag data having excess
       residuals in the chanels used for the baseline fitting.
       This is a good method for detecting and deleting data
       damaged by RFI.
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.
  OUTNAME....Output UV file name (name).     Standard defaults.
  OUTCLASS...Output UV file name (class).    Standard defaults.
  OUTSEQ.....Output UV file name (seq. #).   0 => highest unique
  OUTDISK....Disk drive # of output UV file. 0 => highest with
             space for the file.
  BCHAN......Lowest channel number in the input file to write to
             the output file. 0=> 1.
  ECHAN......Highest channel number in the input file to write
             to the output file. 0=> highest in input data.
  ICHANSEL...Select up to 20 groups of channels/IF(s) to fit as sets
             of (Start,end,inc,IF), i.e., ICHANSEL = 6,37,1,0,
             92,123,1,0 for two regions applyingto all IFs.  The first
             group for which ICHANSEL(2,i) <= 0 ends the list.
             Defaults: Any IF having no group assigned to it, gets a
             group including all channels.
             ICHANSEL(1,j) defaults to 1,
             0 < ICHANSEL(2,j) < ICHANSEL(1,j) defaults to Nchan.
             ICHANSEL(3,j) < 1 or > ICHANSEL(2,j)-ICHANSEL(1,j)+1
             defaults to 1.
             ICHANSEL(4) <= 0 => this group applies to all IFs.
  ORDER......Normally the fit should be done with a first order
             polynomial, namely a DC term and a slope.  If only one box
             is used, the slope may not be adequately defined and a
             simple DC term for the real and for the imaginary parts
             would be more reliable.
  DOOUTPUT...<= 0 => write out only the subtracted uv data.
             > 0 => write also a data set with the fit baseline
             as visibilities.
  CHANNEL....When DOOUTPUT > 0, write the continuum data with
             the fit evaluated at spectral channel CHANNEL.
             0 => reference channel.
  FLUX.......If the residual flux in any channel used to fit the
             baseline exceeds FLUX, then the spectrum for that
             time, IF, and polarization is fully flagged.
  CUTOFF.....If the RMS flux in the channels used to fit the
             baseline exceeds CUTOFF, then the spectrum for that
             time, IF, and polarization is fully flagged.
  SHIFT......Shift in arcseconds during fitting process. The data
             are shifted, fitted, flagged and divided.  They are
             not shifted back except for the fit baseline output.
             These are shifts in RA, DEC (i.e. NOT on the tangent
             plane) and are the same as in UVFIX.
----------------------------------------------------------------
UVLSD: Task which subtracts continuum from channels in UV-plane
DOCUMENTOR: H.J. van Langevelde (Sterrewacht Leiden)
RELATED TASKS:   UVLIN, UVBAS

                          PURPOSE

    UVLSD will estimate the continuum visibilities and subtract
these from a specified range of channels, hopefully leaving only
the information about spectral features in the output UV-file.
It fits, by least squares, a straight line to the to the real and
imaginary parts of the selected channels.  This baseline is
subtracted from the spectrum.

    This can only work properly if the UV coverage is the same
for all spectral line channels. It can, however, deal with
frequency dependent flagging.

                     PHASE SHIFTING

     Since UVLSD works perfectly for a single point source at
the phase center, one should shift any dominant point source to
the phase center prior to the fitting process. Note that UVFIX
now does this correctly since it does the relative phase shift
between channels.  The shift option is offered here for
convenience and since THE SHIFT IS UNDONE AFTER THE FIT. The
sense of the shift is the same as in UVFIX. The code for
computing the shift is:

         RA = RA + SHIFT(1) / 3600.
         DEC = DEC + SHIFT(2) / 3600.
         DTORAD = 3.14159265358979326D0 / 180.0D0
         DXC = SIN (DTORAD * (SHIFT(1)/3600.)) *
     *      COS (DEC * DTORAD)
         DYC = COS ((DEC - SHIFT(2)/3600.) * DTORAD) *
     *      SIN (DEC * DTORAD) - SIN ((DEC  - SHIFT(2)/3600.) *
     *      DTORAD) * COS (DEC * DTORAD) * COS ((SHIFT(1)/3600.) *
     *      DTORAD)
         DZC = SQRT(1.0D0-DBLE(DXC)**2-DBLE(DYC)**2)-1.0D0

For small shifts, the necessary values for SHIFT can be
measured from an image and adjusted by dividing the RA value by
cos(declination).

                          COMMENTS

    This tasks has proved to be powerful in problems where
bandwith is small and extended continuum emission has to be
removed. Its accuracy is limited by the fact that the
visibilities in the uv plane change over the bandwith. The
errors made in the approximation of the continuum visibility
--and thus in the subtraction-- can be estimated as:

               d V      d u             d V      L
       D V  =  ---  *   ---  *  D v  =  ---  *  ---  *  D v
          L    d u      d v             d u      c

Where D V is the error, D v the bandwith over which we try to do
this and u a coordinate in UV-plane. L is the baseline length
specified in the same units as those used for c, the speed of
light.  The formula tells us that UVLSD should do an accurate
job on short baselines and/or small bandwith, provided there is
a reasonably smooth signature in the UV-plane. That means that
it will generally not work for a field dominated by discrete
(point) sources.  It will work better if the dominant point
source is shifted to the filed center.  Use UVFIX which now does
a correct shift for spectral data.

    In comparison with the method of cleaning the background and
UVSUB the components from the spectral line channels, this
method has the main advantages that it is 1) much faster, 2)
will work when the background is difficult to model with clean
components. When point- sources are the main source of continuum
emission the UVSUB method is perfectly suited. In some cases a
hybrid method may be advantageous.

    In comparison with averaging maps to estimate the continuum
this method is again faster and more reliable, since there will
be no sidelobs of the continuum in the map.

    The output data set can also be a powerful diagnostic tool.
In principle, the data can be used to apply selfcal on your
spectral line data.

    UVLSD differs from UVLIN in a number of ways. UVLIN does its
flagging only on the peak residual in the channels selected for
fitting and scales the peak by the square root of the
integration time before deciding on flagging.  This is correct
for peak residuals due solely to thermal noise but is an added
confusion to the user.  In any case, you should set the FLUX
cutoff at a conservative level (6 - 8 times the expected sigma
in the usual integration time).  The CUTOFF test is better for
spectra that do not fit the linear model or are just excessively
noisy for some reason.  UVLSD offers the option of writing the
fit continuum as a UV data set which can be used for self-cal,
TVFLG editing, etc.

     UVBAS fits the linear baseline in amplitude and phase
rather than real and imaginary.  If the emission is well off the
center of the field, the amplitude and phase are more likely to
be nearly linear than the real and imaginary parts.  However,
the amplitude does not have Gaussian noise statistics and UVBAS
is therefore suspect in low signal-to-noise ratio cases.


			REFERENCES

See Cornwell, Uson and Haddad (Astron. Astrophys. 258, 583; 1992) for a
detailed discussion which includes estimates of errors.
