; MODIM
;---------------------------------------------------------------
;! adds images of model objects to image cubes in IQU polarization
;# TASK ANALYSIS MODELING POLARIZATION
;-----------------------------------------------------------------------
;;  Copyright (C) 2012
;;  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
;-----------------------------------------------------------------------
MODIM     LLLLLLLLLLLLUUUUUUUUUUUU CCCCCCCCCCCCCCCCCCCCCCCCCCCCC
MODIM     Task to alter I, Q, U images adding models
INNAME                             Input I image name (name).
INCLASS                            Input I image name (class).
INSEQ             0.0     9999.0   Input I image name (seq. #).
INDISK            0.0        9.0   Input I image disk unit #.
IN2NAME                            Input Q image name (name).
IN2CLASS                           Input Q image name (class).
IN2SEQ            0.0     9999.0   Input Q image name (seq. #).
IN2DISK           0.0        9.0   Input Q image disk unit #.
IN3NAME                            Input U image name (name).
IN3CLASS                           Input U image name (class).
IN3SEQ            0.0     9999.0   Input U image name (seq. #).
IN3DISK           0.0        9.0   Input U image disk unit #.
OUTNAME                            Output image name (name).
OUTSEQ           -1.0     9999.0   Output image name (seq. #).
OUTDISK           0.0        9.0   Output image disk unit #.
BLC                                Bottom left corner of input.
TRC                                Top right corner of input.
FLUX                               Noise level to add
FACTOR                             Multiplication factor.
OPCODE                             Model types are:
                                   'GAUS', 'DISK', 'RECT',
                                   'SPHE', 'EXPD', 'POIN'
INLIST                             List of sources up to 9999
                New image parameters:
COORDINA                           Ra, Dec coordinates
IMSIZE                             Ra, Dec number of pixels
CELLSIZE                           Ra, Dec increments in asec
APARM                              (1) Frequency (GHz) plane 1
                                   (2) increment (GHz)
                                   (3) number channels
----------------------------------------------------------------
MODIM
Type: TASK
Use: This task can be used to modify a pair of existing image cubes of
     Q and U polarization or create new ones.
Adverbs:
  INNAME.....Input I image name (name).     No default.
  INCLASS....Input I image name (class).    Standard defaults.
  INSEQ......Input I image name (seq. #).   0 => highest.
  INDISK.....Disk drive # of input I image. 0 => any.
  IN2NAME....Input Q image name (name).     No default.
  IN2CLASS...Input Q image name (class).    Standard defaults.
  IN2SEQ.....Input Q image name (seq. #).   0 => highest.
  IN3DISK....Disk drive # of input Q image. 0 => any.
  IN3NAME....Input U image name (name).     No default.
  IN3CLASS...Input U image name (class).    Standard defaults.
  IN3SEQ.....Input U image name (seq. #).   0 => highest.
  IN2DISK....Disk drive # of input U image. 0 => any.
  OUTNAME....Output image name (name).    Standard defaults.
             Outclasses are IMODEL, QMODEL, and UMODEL
  OUTSEQ.....Output image name (seq. #).  0 => highest unique.
  OUTDISK....Disk drive # of output image.  0 => highest
             number with sufficient space.
  BLC........Bottom right corner in input image of desired
             subimage.  Default is entire image.
  TRC........Top right corner in input image of desired
             subimage.  Default is entire image.
  FLUX.......Noise level to be added (in the units of the image)
  FACTOR.....Factor by which the original data is multiplied
             before it is added to the model.  0 => drop original
             data.
  OPCODE.....Operation code to determine the type of model.  Any
             undefined OPCODE => Gaussian.  The available opcodes are
             'POIN' => Point, 'GAUS' => Gaussian, 'DISK' => Solid Disk,
             'RECT' => Solid Rectangle, 'SPHE' optically thin sphere,
             'EXPD' => Exponential
  INLIST.....Text file containing one line per source, giving
                I, Q, U, Spix, RM, DX, DY, Maj, Min, PA Type#
             blank separated free format and trailing zeros may be
             omitted.  I, Q, and U are those that would be measured at
             1 GHz wavelength.  The resulting number of components
             will be the number of lines in INLIST neglecting comments
             (which start with # in col 1) and lines with format
             errors.  Units are Jy/beam, Jy/beam, Jy/beam, unitless,
             radians/m^2, pixels, pixels, pixels, pixels, degrees, and
             unitless.  Types are point, Gaussian, uniform disk,
             uniform rectangle, optically thin sphere, and exponential
             for code numbers 1 through 6.  Default value for code is
             the code selected by OPCODE.
             The Clean beam is substituted if Maj and/or Min are 0 or
             missing.   Limit 9999.  If all of Q and U are zero then
             only I polarization is done.

        If making a new image (INNAME blank with I polarization or
        INNAME and/or IN2NAME and/or IN3NAME blank with I, Q, U) use
  COORDINA...RA (hours minutes seconds of time), declination (degrees,
             minutes, seconds of arc) for reference pixel (at image
             center).
  IMSIZE.....X, Y image size in pixels
  CELLSIZE...X, Y image spacing in arc sec
  APARM......(1) Reference frequency at first output plane in GHz
             (2) Increment in frequency in GHz
             (3) Number of frequency pixels
----------------------------------------------------------------
MODIM: Task which modifies map images by scaling the existing
       map and adding a specific model (See also UVMOD).
DOCUMENTOR: Eric R. Nelson NRAO/VLA/UNM
DATE OF DOCUMENTATION: 14 JUNE 1983
RELATED PROGRAMS: UVMOD, APCLN, UVMAP, COMB
VERSION 12june83

                  PURPOSE

     IMMOD modifes an already existing map image by the addition of one
of four model types.  The original map points may be scaled by a
multiplicative factor, including negative values and zero, before they
are added to the model.  Random noise may also be added to the map.  The
four available models to choose from are 1) point source, 2) Gaussian,
3) solid disk, 4) solid rectangle, 5) optically thin sphere, 6)
exponential disk..  The dimensions, offset and position angle are input
by the user.

                  COMMENTS

OPCODE :
          The type model to be used can be selected by OPCODE.  The
allowed models, and their corresponding OPCODEs are listed below.

   1) Point -> 'POIN'
         Only the specified point is altered.  The position is
         determined by FPOS.  The criterion for the point selection is
                 Sqr(X^2 + Y^2) = 0
         where (X,Y) is the current pixel location after correction for
         any offset.  Parameters BMAJ, BMIN and BPA have no affect on
         this model.  The value added is
                 ZEROSP(1) * CBarea / Cellx / Celly
         where CBarea is the Clean beam area and Cellx and Celly are the
         x and y cell sizes.

   2) Gaussian -> 'GAUS'
         The function
                 ZEROSP(1) * (CBarea/NBarea) * EXP (-4Ln(2) * R**2)
         is added to the map where
                 R = Sqrt(XX^2 + YY^2)                     with
                 XX = (Y*Cos(BPA) + X*Sin(BPA))/BMAJ
                 YY = (X*Cos(BPA) - Y*Sin(BPA))/BMIN
         where X and Y are the same as above, but in units of arcsec.
         NBarea is the new area defined by (1.1331 * BMAJ * BMIN)

   3) Disk -> 'DISK'
         A solid disk is added to the map.  The amplitude is
                 ZEROSP(1) * BMarea / (Pi * BMAJ * BMIN / 4)

   4) Rectangle -> 'RECT'
         A solid rectangle is added to the map.  The amplitude is
                 ZEROSP(1) * BMarea / (BMAJ * BMIN)

   5) Sphere -> 'SPHE'
         A circular function is added to the map:
                 ZEROSP(1) * BMarea / NBarea * sqrt (1-R*R)
         where
                 R = sqrt (X*X + Y*Y) / BMAJ
                 NBarea = 2.094 *BMAJ * BMAJ

   6) Exponential
         The function
                 ZEROSP(1) * (CBarea/NBarea) * EXP (-2ln(2) * R)
         is added to the map where R is defined above (in 2) and
                 NBarea = 3.2699 * BMAJ * BMAJ

BMAJ, BMIN, BPA (actually FWIDTH(1 through 3, i) for i'th component

     The dimensions of the resulting functions are determined by BMAJ,
BMIN and BPA (position angle).  For the Gaussian and exponential, the
first two numbers are the FWHM of the two axes.  For the disk and
rectangle, the first two values are the absolute dimensions of the two
available axes.  For the sphere, only BMAJ is used and it is the
diameter of the sphere.

     If either BMAJ or BMIN is zero, then all the models reduce to the
point model.

FACTOR :

     The FACTOR term allows one to add a scaled version of original data
to the model.  FACTOR is simply multiplied by the original data which is
then added to the model.  If FACTOR = 0 then only a map of the model
will remain.
