; OOSUB
;---------------------------------------------------------------
;! Subtracts/divides a model from/into a uv data base
;# TASK UV IMAGING MODELING CALIBRATION OOP
;-----------------------------------------------------------------------
;;  Copyright (C) 2007-2010, 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
;;  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
;-----------------------------------------------------------------------
OOSUB     LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
OOSUB     Task to subtract CLEAN components from a uv data base.
                                   (or, to divide observed visi-
                                   bility by model visibility)
INNAME                             Input UV file name (name)
INCLASS                            Input UV file name (class)
INSEQ             0.0     9999.0   Input UV file name (seq. #)
INDISK                             Input UV file disk unit #
CHANNEL          -1.0     9999.0   Spectral channel (0=>all)
                                   Use 0 for continuum
BIF               0.0     9999.0   First IF (0=>1)
EIF               0.0     9999.0   Highest IF (0=>BIF to last)
IN2NAME                            Cleaned map name (name)
IN2CLASS                           Cleaned map name (class)
IN2SEQ            0.0     9999.0   Cleaned map name (seq. #)
IN2DISK                            Cleaned map disk unit #
NMAPS             0.0     4096.0   No. maps to use for model.
ONEFREQ          -1.0        1.0   > 0 => CC model from only
                                   one frequency (group)
INVERS            0.0    46655.0   CC file version #. 0 -> 1
OUTNAME                            Output UV file name (name)
OUTCLASS                           Output UV file name (class)
OUTSEQ           -1.0     9999.0   Output UV file name (seq. #)
OUTDISK                            Output UV file disk unit #.
BCOMP                              First CLEAN comp to sub.
                                   1 per field.
NCOMP                              Last CLEAN comp to sub.
                                   to use (0 => all)
FLUX                               Lowest CC component used.
CMETHOD                            Modeling method:
                                   'DFT','GRID','    '
CMODEL                             Model type: 'COMP','IMAG'
                                   (see HELP re images)
FACTOR                             Factor times CLEAN fluxes.
                                   0->1.0  Subtract
                                     -1.0  Add
OPCODE                             'DIV ' => divide visibility
                                     observation by model vis.
                                   'MODL' => replace visibility
                                     with model visibility
                                   anything else => subtract
SMODEL                             Source model, 1=flux,2=x,3=y
                                   See HELP SMODEL for models.
BPARM                              Task enrichment parameters
                                   (1) Antenna diameter (m)
                                        0 -> no correction
                                   (2) Omit CC options
                                   (3) spectral index radius
                                        0 -> no correction
FQTOL                              Frequency tolerance in kHz
                                   (primary beam & spec index)
IN3NAME                            Spectral index image name
IN3CLASS                           Spectral index image class
IN3SEQ                             Spectral index image sequence
                                   number
IN3DISK                            Spectral index image disk
IN4NAME                            Spectral curvature name
IN4CLASS                           Spectral curvature class
IN4SEQ                             Spectral curvature sequence
                                   number
IN4DISK                            Spectral curvature disk
BADDISK                            Disks to avoid for scratch
----------------------------------------------------------------
OOSUB
Task:  Subtracts/divides a model from/into a uv data base.  The model
       may be a specific model, a set of CLEAN components files, or a
       set of images.  "CLEAN" models may be points, Gaussians or
       uniform, optically thin spheres.  The task will also subtract the
       model and then re-read the result replacing the difference with
       the model value.
            Model images made with both values of IMAGR's DO3DIMAG
       option are handled correctly, as are multi-scale images.  Set
       NMAPS = NFIELD * NGAUSS.
            OOSUB works only on single-source files.
            OOSUB differs from UVSUB in that it offers the
       frequency-dependent primary beam and spectral index options
       found in IMAGR.

       NOTE: this task does NOT apply flagging or calibration tables
       to the input UV data.  Run SPLIT first if that operation is
       desired.
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.
  CHANNEL....Frequency channel, 0 => all (use 0 for continuum)
             If > 0, then subtract/divide only this channel but copy
             all channels with the others unchanged.  NOTE WELL:
             EIF will be set to BIF if CHANNEL >= 1.
  BIF........First IF to process. 0=>1
  EIF........Highest IF to process 0=> do BIF to highest.
             Note: not all data sets will have IFs.  See note under
             CHANNEL directly above too.  If the IF axis precedes the
             FREQ axis in the header, then all IFs must be done if all
             spectral channels are to be done.
  IN2NAME....Model map name (name).      Standard defaults.
  IN2CLASS...Model map name (class).     Standard defaults.
  IN2SEQ.....Model map name (seq. #).    0 => highest.
  IN2DISK....Disk drive # of model map.  0 => any.
  NMAPS......Number of image files to use for model.  For multi-scale
             models, set NMAPS = NFIELD * NGAUSS to include the Clean
             components of the extended resolutions.  If more than one
             file is to be used, the NAME, CLASS, DISK and SEQ of the
             subsequent image files will be the same as the first file
             except that the LAST 3 or 4 characters of the CLASS will
             be an increasing sequence above that in IN2CLASS.  Thus,
             if INCLASS='ICL005', classes 'ICL005' through 'ICLnnn'
             or 'ICnnnn', where nnn = 5 + NMAPS - 1 will be used.  Old
             names (in which the 4'th character is not a number) are
             also supported: the last two characters are '01' through
             'E7' for fields 2 through 512.  In old names, the highest
             field number allowed is 512; in new names it is 4096.
  ONEFREQ....In IMAGR, a CC file is made from the "average" of all
             channels included in the bandwidth synthesis.  But it is
             also possible to make the model image(s) from a single
             frequency (or from frequencies within FQTOL anyway).
             Set ONEFREQ = 1 if the model was made this way, leave it
             zero if all frequencies were included in the model.
  INVERS.....CC file version #.  You should specify this.  0 -> 1
  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
  BCOMP......The first clean component to process. One value is
             specified for each field used.
  NCOMP......Number of Clean components to use for the model, one
             value per field.  If all values are zero, then all
             components in all fields are used.  If any value is not
             zero, then abs(NCOMP(i)) (or fewer depending on FLUX and
             negativity) components are used for field i, even if
             NCOMP(i) is zero.  If any of the NCOMP is less than 0,
             then components are only used in each field i up to
             abs(NCOMP(i)), FLUX, or the first negative whichever
             comes first.  If abs(NCOMP(i)) is greater than the number
             of components in field i, the actual number is used.  For
             example
                   NCOMP = -1,0
             says to use one component from field one unless it is
             negative or < FLUX and no components from any other
             field.  This would usually not be desirable.
                   NCOMP = -1000000
             says to use all components from each field up to the
             first negative in that field.
                   NCOMP = -200 100 23 0 300 5
             says to use no more than 200 components from field 1, 100
             from field 2, 23 from field 3, 300 from field 5, 5 from
             field 6 and none from any other field.  Fewer are used if
             a negative is encountered or the components go below
             FLUX.
  FLUX.......Only components > FLUX in absolute value are used in the
             model.
  CMETHOD....This determines the method used to compute the
             model visibility values.
             'DFT' uses the direct Fourier transform, this
             method is the most accurate.
             'GRID' does a gridded-FFT interpolation model
             computation.
             '    ' allows the program to use the fastest
             method.
             NOTE: data in any sort order may be used by the
             'DFT' method but only 'XY' sorted data may be used
             by the 'GRID' method.
             NOTE: CMETHOD='GRID' does not work correctly for RL
             and LR data; DO NOT USE CMETHOD='GRID' for RL, LR!
  CMODEL.....This indicates the type of input model; 'COMP' means that
             the input model consists of Clean components, 'IMAG'
             indicates that the input model consists of images.  If
             CMODEL is '   ' Clean components will be used if present
             and the image if not.  Note that Clean images do not make
             good models.  The Clean components have been convolved with
             the Gaussian Clean beam making their Fourier transform be
             rather tapered compared to the original uv data.
  FACTOR.....This value will be multiplied times the CLEAN component
             flux densities before subtraction.  The default 0->1.0, so
             the clean component model will be subtracted from the UV
             data.  FACTOR=-1 will add the clean component model to the
             UV data.  FACTOR will be adjusted by OOSUB for image
             models that are in JY/BEAM.  FACTOR is used with all
             OPCODEs.
  OPCODE.....OPCODE='DIV ' => divide observed visibility by model
             visibility.  OPCODE='MODL' => replace the visibility with
             the model visibility.
             Any other setting of OPCODE causes the task to subtract the
             model visibility from the observed visibility (the normal
             mode of operation).
  SMODEL.....A single component model to be used instead of a CLEAN
             components model; if abs (SMODEL) > 0 then use of this
             model is requested.
                SMODEL(1) = flux density (Jy)
                SMODEL(2) = X offset in sky (arcsec)
                SMODEL(3) = Y offset in sky (arcsec)
                SMODEL(4) = Model type:
                  0 => point model
                  1 => elliptical Gaussian and
                       SMODEL(5) = major axis size (arcsec)
                       SMODEL(6) = minor axis size (arcsec)
                       SMODEL(7) = P. A. of major axis (degrees)
                  2 => uniform sphere and
                       SMODEL(5) = radius (arcsec)
  BPARM......Correction control parameters (SEE EXPLAIN IMAGR):
             (1) If > 0 then make frequency dependent primary beam
               corrections assuming an antenna diameter of IMAGRPRM(1)
               meters.  Note that VLA and ATCA arrays (TELESCOPE
               header parameter) use the default primary beam
               parameters defined elsewhere in AIPS, while other
               antennas actually use IMAGRPRM(1) as the diameter of a
               "standard" telescope.  See FQTOL below also.
             (2) If BPARM(1) > 0, you may omit selected CCs from the
               operation based on position:
                  BPARM(2) <= 0  : Include all CCs
                            = 1  : Omit CCs within the main beam at
                                   all frequencies
                            = 2  : Omit CCs within the main beam at
                                   some frequncies
                            = 3  : Omit Ccs outside the main beam at
                                   some frequencies
                            = 4  : Omit CCs outside the main beam at
                                   all frequencies
             (3) 1 => use a spectral-index image represented in
               IN3NAME, IN3CLASS, IN3SEQ, IN3DISK below to correct the
               Clean component model for each channel.  IN4NAME et al
               will also be used as a curvature image iff IN3NAME are
               specified.
               bparm(3)-0.5 is used as a radius in pixels over which
               the spectral index image is averaged.  When it is small
               (0 < BPARM(3) <~ 1), the spectral index is interpolated
               rather than averaged.  See FQTOL below as well.  When
               doing spectral index, the primary beam correction
               (BPARM(1)) costs very little extra.  This parameter is
               IMAGRPRM(17) in IMAGR.
  FQTOL......Frequency tolerance in kHz.  Spectral channels with FQTOL
             are handled together (use the same average CC model) when
             applying the primary beam and spectral index
             corrections.  Default is to do each channel separately
             which can take a long time.
  IN3NAME....Image name of spectral index image; no default.
  IN3CLASS...Image class of spectral index image; no default.
  IN3SEQ.....Image sequence of spectral index image; 0 -> highest.
  IN3DISK....Disk of spectral image image; 0 -> any.
  IN4NAME....Image name of spectral index curvature image; no default.
             Curvature images should be base 10 rather than base e -
             they differ by a factor of 2.3.  Also the reference
             frequency is 1.0 GHz.  These are changes done 2010-07-13.
  IN4CLASS...Image class of spectral index curvature image; no
             default.
  IN4SEQ.....Image sequence of spectral index curvature image;
             0 -> highest.
  IN4DISK....Disk of spectral curvature image image; 0 -> any.
  BADDISK....The disk numbers to avoid for scratch files.
----------------------------------------------------------------
