%N model %F $MIR/src/subs/model.for
%D Calculate model visibilities, given a model image.
%P mchw
%: model
%B
        subroutine Model(flags,tvis,tmod,offset,level,tscr,
     *                          nhead,header,calget,nchan,nvis)

        implicit none
        character flags*(*)
        integer tmod,tvis,tscr,nchan,nhead,nvis
        real offset(2),level
        external header,calget

  Calculate the model data corresponding to a visibility data file.
  The model can either be a point source, or a full map. If the model
  is a full image cube, then the image is FFTed, and an interpolation
  scheme is used to determine the corresponding visibility model.

  Input:
    tvis       The visibility data file. The caller can set up the uv file
               using the uvselect and uvset routines.
    tmod       The model image file. If this is 0, then the input model
               is assumed to be a point source.
    flags      This selects extra processing options. It is a character
               string, each character of which has the following meaning:
                'c'  Calibration scaling. Look up the source in the
                     calibrators file, to determine the flux of the source.
                'a'  Autoscale. After model calculation, the model is scaled
                     so that it has the same power as the visibilities.
                'h'  Use image header for phase center.
                'm'  Multi-freq data. The model is either one or two
                     planes, which were formed using INVERT's mfs option.
                'l'  Apply clipping.
                'z'  Use zero if model cannot be calculated (don't flag)
    offset     The offset, in arcsec, in RA and DEC, of the point
               source model. This is only used if tmod.eq.0.
    level      Either a clip level to apply to the data (tmod.ne.0), or
               the amplitude of the point source (tmod.eq.0). If it is
               a clip level, it is applied only if the 'l' flag (see
               above) is given. Additionally, depending on the type of the
               model, then the clip level can be used in one of two ways.
               If the model is Stokes-Q,U or V, or a raw cross-hands
               polarisation type (strange!!), or I-alpha map from multi-freq
               synthesis, then pixels in the range -Level to Level are clipped.
               If the model is none of the above, then pixels below
               Level are clipped.
    nhead      Number of "header" values to write out to the scratch file.
               These are filled in by the "header" routine. If "nhead" is
               zero, header is not called.
    header     A service routine called after each visibility record
               is processed. Arguments to this routine are:
                 subroutine header(tvis,preamble,data,flags,nchan,
                   accept,Out,nhead)
               where
                 Input:
                   tvis        Handle of the visibility file.
                   nhead       The value of nhead
                   nchan       The number of channels.
                 Input/Output(?):
                   preamble    Preamble returned by uvread.
                   data        A complex array of nchan elements, giving
                               the correlation data.
                   flags       The data flags.
                 Output:
                   out         The nhead values to save with the data.
                   accept      This determines whether the data is
                               accepted or discarded.
    calget     Returns the flux for a point source.
  Output:
    tscr       Output scratch file containing the data.
    nchan      The number of channels in the scratch file.
    nvis       The number of visibilities written to the scratch file.

  In the output scratch file, there are nhead + 5*nchan values per
  visibility record processed. The "nhead" values are filled in by
  the "header" routine. The 5 values per channel consist of
       real(vis),aimag(vis),real(model),aimag(model),flag
  Where "vis" and "model" are the visibility and model data. "Flag" is
  either positive or negative, indicating whether this visibility is
  deemed good or bad.

  Bugs and Shortcomings:
    * The FFT of the entire cube must fit into memory.
    * Calibration scaling is not handled yet.
