; COMB
;---------------------------------------------------------------
;! combines two images by a variety of mathematical methods
;# TASK ANALYSIS POLARIZATION
;-----------------------------------------------------------------------
;;  Copyright (C) 1995, 1997, 2001, 2004, 2008
;;  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
;-----------------------------------------------------------------------
COMB      LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
COMB: Task to combine in many ways two overlapping images
USERID     -32000.0     32000.0    User ID. 0 => current user,
                                     32000 => any user.
INNAME                             First image name
INCLASS                            First image class
INSEQ           0.0      9999.0    First image seq. #
INDISK          0.0         9.0    First image disk drive #
IN2NAME                            Second image name
IN2CLASS                           Second image class
IN2SEQ          0.0      9999.0    Second image seq. #
IN2DISK         0.0         9.0    Second image disk drive #
IN3NAME                            First noise image name
IN3CLASS                           First noise image class
IN3SEQ          0.0      9999.0    First noise image seq. #
IN3DISK         0.0         9.0    First noise image disk #
IN4NAME                            Second noise image name
IN4CLASS                           Second noise image class
IN4SEQ          0.0      9999.0    Second noise image seq. #
IN4DISK         0.0         9.0    Second  noiseimage disk #
DOALIGN        -2.0         1.0    Should images be coincident?
                                   (See HELP.)
OUTNAME                            Output image name
OUTCLASS                           Output image class
OUTSEQ         -1.0      9999.0    Output image seq. #
OUTDISK         0.0         9.0    Output image disk drive #
BLC             0.0      4096.0    Bottom left corner
TRC             0.0      4096.0    Top right corner
OPCODE                             Algorithm type:
                                   'SUM ','DIV ','SPIX','POLI',
                                   'POLA','MULT','OPTD','CLIP'
                                   'REAL','IMAG','MEAN','RM  '
                                   'POLC','SUMM'
APARM                              Parameters for algorithm:
                                     (1) - (4) scale and offset
                                     (8) > 0 => blank with 0.0
                                     (9)  Map1 clip level
                                     (10) Map2 clip level
                                     see HELP COMB
BPARM                              Noise/control parameters:
                                     (1)  Map1 noise level
                                          -1 use 3rd image
                                          0 ignore noise
                                     (2)  Map2 noise level
                                          -1 use 4th image
                                          0 ignore noise
                                     (3)  > 0 => output noise
                                          in addition
                                     (4)  < 0.5 => clip w inputs
                                          > 1.5 => clip w S/N
                                          else  => clip w noise
                                     (5)  minimum ok S/N   or
                                          maximum ok noise
                                     (6)  max output noise
                                          0 -> any
                                     see HELP COMB
DOHIST         -3.0         1.0    -2 => copy 1st HI only
                                   -3 => copy no HI files
----------------------------------------------------------------
COMB
Type: Task
      COMB is a generalized task which combines two input images and,
      optionally two input noise images, pixel by pixel to produce a new
      cataloged image and, optionally, an image of the uncertainty in
      the main output image.  Numerous mathematical methods for
      combining the images are provided.  COMB has the ability to blank
      the output values of the derived map on the basis of either its
      error, its signal/noise ratio, or various cutoff levels.

      The two input maps should be coincident.  One may combine only
      subimages if desired.  COMB will combine two fully overlapping
      n-dimensional images and it can combine each n-m dimension "plane"
      of the first n-dimensional image with the single n-m dimension
      "plane" of an n-m dimensional second image.
Adverbs:
  USERID......The ID of the owner of the images. 0 => current
              user, 32000 => any user.
  INNAME......First image name.     Standard defaults.
  INCLASS.....First image class.    Standard defaults.
  INSEQ.......First image seq. #.   0 => highest.
  INDISK......Disk drive # for the first image.  0 => any.
  IN2NAME.....Second image name.    Standard defaults.
  IN2CLASS....Second image class.   Standard defaults.
  IN2SEQ......Second image seq. #.  0 => highest.
  IN2DISK.....Disk drive # for the second image.  0 => any.
  IN3NAME.....First noise image name.     Standard defaults.
  IN3CLASS....First noise image class.    Standard defaults.
  IN3SEQ......First noise image seq. #.   0 => highest.
  IN3DISK.....Disk # for first noise image.  0 => any.
  IN4NAME.....Second noise image name.    Standard defaults.
  IN4CLASS....Second noise image class.   Standard defaults.
  IN4SEQ......Second noise image seq. #.  0 => highest.
  IN4DISK.....Disk # for second noise image.  0 => any.
  DOALIGN.....Controls how the four images are to be aligned (see HELP
              DOALIGN).  True (>.1) means that the images must agree in
              their coordinates, though not necessarily in the reference
              pixel position.  Alignment is by coordinate values (if
              DOALIGN > -0.1) or by offsets from the reference pixel
              positions (if DOALIGN <= -0.1).  NOTE: all real axes (>1
              point) are aligned.  If DOALIGN = -2, the headers are
              ignored and the images are aligned at pixel (1,1,...).
  OUTNAME.....Output image name.    Standard defaults.
  OUTCLASS....Output image class.   Standard behavior with default =
              either the output STOKES in string form or the OPCODE if
              the output STOKES is the same as the first input image.
              The noise image has the 6th character of class set to N.
  OUTSEQ......Output image seq. #.  0 => highest unique.
  OUTDISK.....Output disk number. 0 => highest with space.
  BLC.........Bottom left corner of the 1st input image. The other
              images are aligned by coordinates (see DOALIGN) on all
              axes having > 1 point.  The other images may have fewer
              real axes than the 1st.  The 4 windows must have the same
              dimension on the first 2 axes, but the task will select a
              smaller window than was specified if needed to overlap the
              4 images.
  TRC.........Top right corner of input images. (See BLC.)
  OPCODE......The combination algorithm specification: (A=APARM)
     ='SUM ': Linear sum   A(1)*MAP(1) + A(2)*MAP(2) + A(3)
                           If MAP(1) or MAP(2) is blank then
                           the output is blank
     ='DIV ': Division     A(1)*MAP(1) / MAP(2) + A(2)
     ='SPIX': Sp. Index    A(1)*SP.IND(MAP(1),MAP(2)) + A(2)
                           where MAP(1)>A(3)>=0 and MAP(2)>A(4)>=0
     ='POLI': Pol. Inten.  A(1)*SQRT(MAP(1)**2 + MAP(2)**2))
                              + A(2)
     ='POLC': Pol. Inten.  A(1)*SQRT(MAP(1)**2 + MAP(2)**2))*C
                              + A(2)  where C = noise correction
     ='POLA': Pol. Angle   A(1)*ATAN2(MAP(2),MAP(1)) + A(2)
                           where A(3) < SQRT (MAP(1)**2 + MAP(2)**2)
                           MAP(1)=QPOL, MAP(2)=UPOL usually.
     ='MULT': Multiplic.   A(1)*MAP(1)*MAP(2) + A(2)
     ='OPTD': Opacity      A(1) * LN (A(3)*MAP(1)/MAP(2)+A(4))
                              + A(2)
                           where MAP(1) > A(5) and MAP(2) > A(6)
     ='CLIP': Clipping     MAP(1) except where A(1) > MAP(2)
                              > A(2)  or  A(1) < A(2) and
                              either MAP(2) < A(1) or > A(2)
     ='REAL': Rect. conv.  A(1)*(MAP(1)*COS(A(2)*MAP(2)) + A(3)
     ='IMAG': Rect. conv.  A(1)*(MAP(1)*SIN(A(2)*MAP(2)) + A(3)
                              (MAP(2) assumed in degrees)
     ='MEAN': weighted mean AA(1)*MAP(1) + AA(2)*MAP(2)
                            AA(1) = A(1)/(A(1)+A(2))
                            AA(2) = A(2)/(A(1)+A(2))
                            IF (both MAP(1) and MAP(2) are blank) THEN
                              OUTPUT is blank
                            ELSE
                              IF (MAP(1) is blank) then OUTPUT = MAP(2)
                              IF (MAP(2) is blank) then OUTPUT = MAP(1)
     ='RM  ': Rot. Meas.   A(1)*Rot.Meas.Map + A(2)
                              test angle diff +- 180,360 to bring closer
                              to A(3) (in degrees) RM in RADians/M/M
                              with A(1)=1 (A(2) in RAD/M/M).  A(2) used
                              also to bring angle diff closer to A(3).
     ='SUMM': Linear sum   A(1)*MAP(1) + A(2)*MAP(2) + A(3).
                           Almoust identical to SUM, but different
                           treatment of blank points:
                           IF (both MAP(1) and MAP(2) are blank) THEN
                              OUTPUT is blank
                           ELSE
                              IF (MAP(1) is blank) then MAP(1) = 0
                              IF (MAP(2) is blank) then MAP(2) = 0
                           OUTPUT = A(1)*MAP(1) + A(2)*MAP(2) + A(3)
  APARM.......Parameters needed for algorithm:
     APARM(1), APARM(2), APARM(3), APARM(4) used as above.
          APARM(1) = 0 => APARM(1) = 1.0 (except 'POLA','CLIP')
          APARM(1) = 0 => APARM(1) = 28.648 (for 'POLA')
          APARM(2) = 0 => APARM(2) = 1.0 (FOR 'SUM ')
          APARM(3) = 0 => APARM(3) = 1.0 (for 'OPTD')
     APARM(8) >  0  => Use 0.0 for clipped & illegal values
              <= 0  => Use blanking for clipped & illegal values
     APARM(9) = Clip if Abs (MAP(1)) < APARM(9) - image units.
     APARM(10) = Clip if Abs(MAP(2)) < APARM(10) - image units.
          There are no defaults for APARM(9) and (10) and a zero
          value means no clipping.  Used only if BPARM(4) <= 0.5
  BPARM.......Parameters needed noise calculation and control:
     BPARM(1) = 1-sigma level on 1st input map.  < 0 => use third image
                Zero is not allowed for 'POLC' and whenever BPARM(3) > 0
                or BPARM(4) > 0.5.
     BPARM(2) = 1-sigma level on 2nd input map.  < 0 => use fourth image
                Zero is not allowed for 'POLC' and whenever BPARM(3) > 0
                or BPARM(4) > 0.5.
     BPARM(3) = false (<= 0) => output normal image
              = true  (>  0) => output normal and sigma image
                Blanking is the same for both settings of B(3).
     BPARM(4) <= 0.5  => Blank output map using input map values
              else    => Blank output map using output map sigma
              >= 1.5  => Blank output map using output map S/N.
     BPARM(5) = Error on output map value above which output
                pixel is blanked (if BPARM(4) = 1) 0 -> ignore
              = S/N ratio of output map value below which output
                pixel is blanked (if BPARM(4) = 2) 0 -> ignore
     BPARM(6) = Maximum value of sigma to be output (used if > 0 and
                BPARM(3) > 0 only).

     NOTE: certain combinations of BPARM(3), BPARM(4) and OPCODE do not
     make much sense but all are allowed.  S/N blanking may not be
     desirable for POLI, SPIX and OPTD, for example.  Output images
     which are constant will be written and a warning message will
     appear.  The noise on RM images is determinable from the inputs
     of COMB only when BPARM(1) = BPARM(2) = -1.  Consider using task RM
     for rotation measures since a 2-frequency RM computation is very,
     very unreliable anyway.

     In the descriptions above, the term FMAXn means
     MAX( ABS(DATAMAX), ABS(DATAMIN) ) over the n'th map.

     Task MATHS is used to do mathematical operations on single images.

     The task POLCO performs the same operation as OPCODE 'POLC' with
     a somewhat different algorithm for the correction.
  DOHIST.......Normally the HI file of input 1 is copied to the output
               history file and the HI file of the second input is
               appended.  If you are doing many COMBs this can lead to
               immense history files of little use to anyone.  Thus,
               DOHIST=-2 => copy the first HI file only.
               DOHIST=-3 => copy no HI file, write COMB HI only.
----------------------------------------------------------------
