IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 37865


Ignore:
Timestamp:
Jan 18, 2015, 1:13:42 PM (12 years ago)
Author:
eugene
Message:

insert the psphot manual text, needs cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/release.2015/ps1.analysis/analysis.tex

    r37862 r37865  
    105105\section{INTRODUCTION}\label{sec:intro}
    106106
    107 \section{Pan-STARRS1}
    108 
    109 \section{Discussion}
    110 
    111 \section{Conclusion}
     107The Pan-STARRS Image Processing Pipeline is responsible for the basic
     108analysis of images from the Pan-STARRS telescopes Gigapixel Camera.
     109The overall goals and requirements of the Image Processing Pipeline
     110are described in the IPP System/Subsystem Design Description (SSDD;
     111PSDC-430-XXX) and the IPP System Requirements Specification (SRS;
     112PSDC-430-XXX).  Among the Pan-STARRS project survey goals is a
     113repeated all-sky survey in 5 filters, {\it grizy}, beginning with a
     114pre-survey with the prototype telescope PS-1.  The photometric and
     115astrometric precision goals for the all-sky surveys, as well as the
     116other survey components, are quite stringent:
     117
     118\begin{itemize}
     119\item relative photometry: 10 millimagnitudes scatter for bright stars
     120across the sky in the internal photometric system;
     121
     122\item relative astrometry; 10 milliarcseconds scatter for individual
     123stars between repeated images.
     124
     125\item absolute astrometry: 100 milliarcseconds scatter for all ICRS
     126  reference stars (Tycho).
     127\end{itemize}
     128
     129An additional constraint on the Pan-STARRS system comes from the high
     130data rate.  The prototype telescope alone is expected to produce
     131typically $\sim 700$ GB per night of imaging data.  These images will
     132not be limited to high galactic latitudes, so large numbers of
     133measurable stars can be expected in much of the data.  The combination
     134of the high precision goals of the astrometric and photometric
     135measurements and the high data rate (and a finite computing budget)
     136mean that the process of detecting, classifying, and measuring the
     137astronomical objects in the image data stream will be a significant
     138challenge. 
     139
     140In order to achieve these ambitious goals, the object detection,
     141classification, and measurement process must be both precise and
     142efficient.  Not only is it necessary to make a careful measurement of
     143the flux of individual objects, it is also critical to characterize
     144the image point-spread-function, and its variations across the field
     145and from image to image.  Since comparisons between images must be
     146reliable, the measurements must be stable for both photometry and
     147astrometry.
     148
     149\subsection{Comparable Programs}
     150
     151A variety of astronomical software packages perform the basic object
     152detection, measurement, and classification tasks needed by the
     153Pan-STARRS IPP.  Each of these programs have their own advantages and
     154disadvantages.  Below we discuss some of the most widely used of these
     155other packages, highlighting the features of the programs which are
     156particularly desirable, and noting aspects of the programs which are
     157problematic for the IPP.
     158
     159\begin{itemize}
     160
     161\item DoPhot : analytical fitted model with aperture corrections.
     162  pro: well-tested, stable code.  con: limited range of models,
     163  algorithm converges slowly to a PSF model, limited tests of PSF
     164  validity, inflexible code base, fortran (P. Schechter)
     165
     166\item DAOPhot : Pixel-map PSF model with analytical component.  pro:
     167  well-tested, high-quality photometry.  con: Difficult to use in an
     168  automated fashion, does it handle 2D variations well? (P. Stetson)
     169
     170\item Sextractor : pure aperture measurement with rudimentary
     171  object subtraction.  pro: fast, widely used, easy to automate.  con:
     172  poor object separation in crowded regions, PSF-modeling is only
     173  beta (psfex), what models are available? (E. Bertin)
     174
     175\item apphot : IRAF-based aperture photometry.  pro: widely used.
     176  con: IRAF-based, aperture photometry. (???)
     177
     178\item galfit : detailed galaxy modeling.  not a multi-object PSF
     179  analysis tool.  con: does not provide a PSF model, not easily
     180  automated.  very detailed results in very slow processing.  only a
     181  galaxy analysis program. (C. Impey)
     182
     183\item SDSS phot : con: tightly integrated into the SDSS software
     184  environment.  (R. Lupton)
     185
     186\end{itemize}
     187
     188\note{discussion of these packages is insufficient: flesh out
     189  discussion and add in the references.}
     190
     191\note{Add discussion of the lessons learned from experience with previous
     192  analysis programs}
     193
     194The Pan-STARRS IPP team decided that none of the existing packages met
     195all of their needs, particularly given the very challenging goals of
     196the project.  We decided to redesign the photometry analysis from
     197scratch, using the lessons learned from the existing photometry
     198systems.  In the process, the object analysis software would be
     199written using the data analysis C-code library written for the IPP,
     200\code{psLib}, and the components of the photometry code would be
     201integrated into the IPP's mid-level astronomy data analysis toolkit
     202called \code{psModules}.  The result is 'PSPhot', which can be used
     203either as a stand-alone C program, or as one of the high-level IPP
     204components of \code{psModules}, available to programmers either via a
     205C interface or through a SWIG interface in Perl (or potentially
     206Python).
     207
     208\note{discuss the psphot program varients}
     209
     210\section{PSPhot Design Goals}
     211
     212PSPhot has a number of important requirements that it must meet, and a
     213number of design goals which we believe will help to make usable in a
     214wide range of circumstances.  The critical requirements of the
     215Pan-STARRS IPP which drive the requirements for PSPhot:
     216
     217\begin{itemize}
     218\item {\bf 10 millimagnitude photometric accuracy}.  For PSPhot, this
     219  implies that the measured photometry of stellar objects must be
     220  substantially better than this 10 mmag since the photometry error
     221  per image is combined with an error in the flat-field calibration
     222  and an error in measuring the atmospheric effects.  We have set a
     223  goal for PSPhot of 3mmag photometric consistency for bright stars
     224  between pairs of images obtained in photometric conditions at the
     225  same pointing, ie to remove sensitivity to flat-field errors.  This
     226  goal splits the difference between the three main contributors and
     227  still allows some leeway.  This requirement must be met for
     228  well-sampled images and images with only modest undersampling.
     229
     230\item {\bf 10 milliarcsecond astrometric accuracy}. Relative
     231  astrometric calibration depends on the consistency of the individual
     232  measurements.  The measurements from PSPhot must be sufficiently
     233  representative of the true object position to enable astrometric
     234  calibration at the 10mas level.  The error in the individual
     235  measurements will be folded together with the errors introduced by
     236  the optical system, the effects of seeing, and by the available
     237  reference catalogs.  We have set a goal for PSPhot of 5mas
     238  consistency between the true source postion and the measured
     239  position given reasonable PSF variations under simulations.  This
     240  level must be reached for images with 250 mas pixels, implying
     241  PSPhot must introduce measurement errors less than 1/50th of a
     242  pixel. \note{the choice of F32 parameters places a numerical limit
     243  of 1e-7 on the accuracy of a pixel relative to the size of a chip
     244  (since a single data value is used for X or Y).  For the $4800^2$
     245  GPC chips, this yields a limit of about 0.25 milliarcsecond.}
     246
     247\item {\bf processing time of 45 seconds} This requirement depends
     248  strongly on the hardware organization, the amount of time spent on
     249  other analysis steps, the density of stars per image, and the depth
     250  for a given type of image.  For the sources at the faint limit (eg,
     251  $5\sigma$), the average density of sources is expected to be roughly
     252  $3\times10^5$ per square degree, while sources at the 20 $\sigma$
     253  level may have densities of $\sim 5\times10^4$ per square degree.
     254  Allowing 30 seconds for the PSPhot portion of the analysis, of which
     255  15 is used for careful analysis of the brighter sources, 10 seconds
     256  is used for PSF modeling and other overheads, and the remaining 5
     257  seconds is used for the PSF fitting of the faintest source implies
     258  that the detailed modelling may take roughly 3msec per source, and
     259  the basic PSF fitting may be allowed 150 usec per source.
     260\end{itemize}
     261
     262The design goals for PSPhot are chosen to make the program flexible,
     263general, and able to meet the unknown usages cases future projects may
     264require:
     265
     266\begin{itemize}
     267\item {\bf Flexible PSF model} Different image sources require
     268  different ways of representing the PSF.  Ideally, both analytical
     269  and pixel-based versions should be possible.
     270
     271\item {\bf PSF spatial variation} Most images result in some spatial
     272  PSF variations at a certain level.  The PSF representation should
     273  naturally incorporate 2-D variations.
     274
     275\item {\bf Flexible non-PSF models} PSPhot must be able to represent
     276  PSF-like objects as well as non-PSF sources.  It must be easy to add
     277  new object models as interesting representations of sources are
     278  invented.
     279
     280\item {\bf Clean code base} PSPhot should incorporate a high-degree of
     281  abstraction and encapsulation so that changes to the code structure
     282  can be performed without pulling the code apart and starting from scratch.
     283
     284\item {\bf PSF validity tests} PSPhot should include the ability to
     285  choose different types of PSF models for diffent situations, or to
     286  provide the user with methods for assessing the different PSF models.
     287
     288\item {\bf Careful aperture corrections} PSPhot must carefully measure
     289  and correct for the photometric and astrometric trends introduced by
     290  using analytical PSF models.
     291
     292\item {\bf User Configurable} PSPhot should allow users to change the
     293  options easily and to allow different approaches to the analysis.
     294
     295\end{itemize}
     296
     297\section{PSPhot Analysis Process}
     298
     299\subsection{Overview}
     300
     301The PSPhot analysis is divided into several major stages:
     302
     303\begin{itemize}
     304\item {\bf Image preparation} Load data, characterize the image
     305  background, load or construct noise and mask images.
     306
     307\item {\bf Initial object detection} Smooth, find peaks, measure basic
     308  properties
     309
     310\item {\bf PSF determination} Select PSF candidates, perform model
     311  fits, build PSF model from fits, select best PSF model class.
     312
     313\item {\bf Bright object analysis} Fit objects with PSFs, determine
     314  PSF validity, subtract PSF-like objects, fit non-PSF model(s),
     315  select best model class, subtract model.
     316
     317\item {\bf Low S/N sources} Detect low-level sources, measure
     318  properties (aperture or PSF)
     319
     320\item {\bf Aperture corrections} Measure the curve-of-growth, spatial
     321  aperture variations, and background-error corrections. 
     322
     323\item {\bf Output} Write out objects in selected format, write out
     324  difference image, noise image, etc, as selected.
     325\end{itemize}
     326
     327Note that a given run of PSPhot allows the user to perform many of
     328these stages only if needed.  For example, the PSF model may already be
     329available from external information, in which case the PSF modeling
     330stage can be skipped.  Or, when used as a library function, the image
     331may have already been loaded and the mask and weight images
     332constructed.  In some implementations, it may be possible to skip the
     333initial object detection stage because only supplied sources are
     334measured.  These are only some of the possible configurations.  The
     335use of these different configurations depends on the source of the
     336image, the desired detail and speed of the processing, and the level
     337of accuracy desired from the analysis.
     338
     339\subsection{Image Preparation}
     340
     341The first step is to prepare the image for detection of the
     342astronomical objects.  We need three separate images: the measured
     343flux, the corresponding variance image, and a mask defining which
     344pixels are valid and which should be ignored.  For the stand-alone
     345program, the input flux image is a required program argument.  When it
     346is loaded, it is converted by default to 32-bit floating point
     347representation.  In the function-call form of PSPhot, the image must
     348be supplied by the user in 32-bit floating point format.  The noise
     349and mask images may either be provided by the user, or they may be
     350automatically generated from the input image, based on
     351configuration-defined values for the image gain, read-noise,
     352saturation, and so forth.  For the function-call form of the program,
     353the flux image is provided in the API, and references to the mask and
     354noise are provided in the configuration information.  As in the
     355stand-alone C-program, the noise and mask may be constructed
     356automatically by PSPhot.
     357
     358\note{describe the use of the covariance image}
     359\note{describe the difference between 'bad' and 'suspect' pixels}
     360
     361For the mask, we use a 16-bit image in which a value of 0 represents a
     362valid pixel.  We use each of the 16 bits to define different reasons a
     363pixel should be ignored.  This allows us to optionally respect or
     364ignore the mask depending on the circumstance.  For example, in some
     365cases, we ignore saturated pixels completely while in other
     366circumstances, it may be useful to know the flux value of the
     367saturated pixel.  In addition, the mask pixels are used to define the
     368pixels available during a model fit, and which should be ignored for
     369that specific fit.  The initial mask, if not supplied by the user, is
     370constructed by default from the image by applying three rules: 1)
     371Pixels which are above a specified saturation level are marked as
     372saturated (configuration keyword: \code{SATURATE}).  2) Pixels which
     373are below a user-defined value are considered unresponsive and masked
     374as dead.  3) Pixels which lie outside of a user-defined window are
     375considered non-data pixels (eg, overscan) and are marked as invalid.
     376The valid window is defined by the configuration variables
     377\code{XMIN}, \code{XMAX}, \code{YMIN}, \code{YMAX}.
     378
     379\note{discuss the mask.config file, in which the mask meanings are assigned to bit values}
     380
     381The noise image, if not supplied is constructed by default from the
     382flux image using the configuration supplied values of \code{GAIN} and
     383\code{READ\_NOISE} to calculate the appropriate Poisson statistics for
     384each pixel.  In this case, the image is assumed to represent the
     385readout from a single detector, with well-defined gain and read noise
     386characteristics.  In some obvious cases, this assumption will not be
     387valid.  For example, if the input flux image is the result of an image
     388stack with significantly variable number of input measurements per
     389pixel, it will be necessary to supply a noise image which accurately
     390represents the noise as a function of position in the image.
     391
     392\subsection{Initial Object Detection}
     393
     394The objects are initially detected by finding the location of local
     395peaks in the image.  The flux and variance images are smoothed with a
     396small circularly symmetric kernel using a two-pass 1D Gaussian
     397(\note{KEYWORD?}).  The smoothed flux and variance images are combined
     398to generate a significance image in signal-to-noise units
     399\note{including correction for the covariance, if known}. At this
     400stage, the goal is only to detect the brighter sources, above a user
     401defined S/N limit (configuration keyword: \code{PEAK\_NSIGMA}).  The
     402detection efficiency for the brighter sources is not strongly
     403dependent on the form of this smoothing function.
     404
     405The local peaks in the smoothed image are found by first detecting
     406local peaks in each row.  For each peak, the neighboring pixels are
     407then examined and the peak is accepted or rejected depending on a set
     408of simple rules.  First, any peak which is greater than all 8
     409neighboring pixels is kept.  Any peak which is lower than any of the 8
     410neighboring pixels is rejected.  Any peak which has the same value as
     411any of the other 8 pixels is kept if the pixel $X$ and $Y$ coordinates
     412are greater than or equal to the other equal value pixels.  This
     413simple rule set means that a flat-topped region will maintain peaks at
     414the maximum $X$ and $Y$ corners of the region.
     415
     416\subsection{Footprints}
     417
     418\note{need to describe the process of generating the source footprints
     419  and then culling the insignificant peaks}
     420
     421\subsubsection{Moments and related}
     422
     423\note{disucss the Kron mags}
     424
     425\note{this section is wrong: we no longer use S/N clipping, but a
     426  Gaussian window function, chosed based on the measured moment}
     427
     428Once a collection of peaks have been identified, basic properties of
     429the objects are measured.  First, the local sky flux is measured
     430within a square annulus with user-defined dimensions
     431(\code{INNER\_RADIUS} and \code{OUTER\_RADIUS}), using the sample
     432median.  This local background value is then used to calculate the
     433object first and second moments within a small user-defined aperture
     434(\code{MOMENT\_RADIUS}).  The first-order moments are a good
     435representation of the object position, while the second-order moments
     436are a measure of the object shape.  The second-order moments are
     437somewhat sensitive to the size of the aperture and the accuracy of the
     438background measurement.  The moment calculation is only performed
     439using pixels which exceed a S/N of 1.  If, in the process of
     440calculating the source moments, the S/N limits reject all but \note{3}
     441or fewer of the source pixels, the peak is identified as being
     442suspect, and is not used for further analysis.  If the measured
     443centroid coordinates differ from the peak coordinates be a large
     444amount (\code{MOMENT\_RADIUS}), then the peak is again identified as
     445being of poor quality and is rejected.  In both of these cases, it is
     446likely that the `peak' was identified in a region of flat flux
     447distribution or many saturated or edge pixels.
     448
     449\subsubsection{Determination of the Peak Coordinates and Errors}
     450
     451\note{this section is wrong: it is a poor estimator of the source
     452  position errors.  we gave up a reverted to using the FWHM / (S/N)}
     453
     454We use the 9 pixels which include the source peak to fit for the
     455position and position errors.  We model the peak of the sources as a
     4562D quadratic polynomial, and use a very simple bi-quadratic fit to
     457these pixels.  We use the following function to describe the peak
     458
     459\[ f(x,y) = C_{00} + C_{10}x + C_{01} y + C_{11} x y + C_{20} x^2 + C_{02} y^2 \]
     460
     461and write the Chi-Square equation:
     462
     463\[ \chi^2 = \sum_{i,j} (F_{i,j} - f(x,y))^2 / \sigma_{i,j}^2 \]
     464
     465By approximating the error per pixel as the error on just the peak,
     466and pulling that term out of the above equation, and recognizing that
     467the values x,y in the 3x3 grid centered on the peak pixel have values
     468of only 0 or 1, we can greatly simplify the chi-square equation to a
     469square matrix equation with the following values:
     470
     471%% fix this:
     472\begin{verbatim}
     473| 9 0 0 0 6 6 | C_00 | = \sum F_{i,j}
     474| 0 6 0 0 0 0 | C_10 | = \sum F_{i,j} x
     475| 0 0 6 0 0 0 | C_01 | = \sum F_{i,j} y
     476| 0 0 0 6 0 0 | C_11 | = \sum F_{i,j} x y
     477| 6 0 0 0 6 4 | C_20 | = \sum F_{i,j} x^2
     478| 6 0 0 0 4 6 | C_02 | = \sum F_{i,j} y^2
     479\end{verbatim}
     480
     481The inverse of the 3x3 matrix terms for $C_{00}$, $C_{20}$, and $C_{02}$ is:
     482\begin{verbatim}
     483| +5/9 -1/3 -1/3 |
     484| -1/3 +1/2    0 |
     485| -1/3    0 +1/2 |
     486\end{verbatim}
     487
     488which can be used to determine the errors on the coefficients:
     489
     490\begin{eqnarray}
     491\sigma^2_{00} & = & \sigma^2 (5/9) \\
     492\sigma^2_{10} & = & \sigma^2 (1/6) \\
     493\sigma^2_{01} & = & \sigma^2 (1/6) \\
     494\sigma^2_{11} & = & \sigma^2 (1/6) \\
     495\sigma^2_{20} & = & \sigma^2 (1/2) \\
     496\sigma^2_{02} & = & \sigma^2 (1/2) \\
     497\end{eqnarray}
     498
     499The location of the peak is determined from the minimum of the
     500bi-quadratic function above, and is given by:
     501
     502\begin{eqnarray}
     503Det    & = & 4 C_{20} C_{02} - C_{11}^2 \\
     504x_{min} & = & (C_{11} C_{01} - 2 C_{02} C_{10}) / Det \\
     505y_{min} & = & (C_{11} C_{10} - 2 C_{20} C_{01}) / Det \\
     506\end{eqnarray}
     507
     508Applying error propagation to the above, we find:
     509
     510\begin{eqnarray}
     511\sigma_{Det}^2  & = & \sigma_{11}^2 (4 C_{11}^2) + \sigma_{20}^2 (16 C_{02}^2) + \sigma_{02}^2 (16 C_{20}^2) \\
     512\sigma_{xn}^2   & = & \sigma_{11}^2 C_{01}^2 + \sigma_{01}^2 C_{11}^2 + \sigma_{02}^2 (4 C_{10}^2) + \sigma_{10}^2 (4 C_{02}^2) \\
     513\sigma_{yn}^2   & = & \sigma_{11}^2 C_{10}^2 + \sigma_{10}^2 C_{11}^2 + \sigma_{20}^2 (4 C_{01}^2) + \sigma_{01}^2 (4 C_{20}^2) \\
     514\sigma_{x}^2    & = & x^2 (\sigma_{xn}^2 / xn^2 + \sigma_{Det}^2 / Det^2) \\
     515\sigma_{y}^2    & = & y^2 (\sigma_{yn}^2 / yn^2 + \sigma_{Det}^2 / Det^2) \\
     516\end{eqnarray}
     517
     518\subsection{PSF Determination}
     519
     520\subsubsection{PSF Model vs Object Model}
     521
     522PSPhot uses an analytical model to represent the shape and flux of an
     523object.  An important concept within the PSPhot code is the
     524distinction between a model which describes an object on an image and
     525a model with describes the point-spread-function (PSF) across an
     526image.
     527
     528Any object in an image may be represented by some analytical model,
     529for example, a 2-D elliptical Gaussian:
     530\begin{eqnarray}
     531f(x,y) & = & I_o exp (-z) + S  \\
     532    R  & = & \frac{(x - x_o)^2}{2\sigma_x^2} + \frac{(y -
     533    y_o)^2}{2\sigma_y^2} + \sigma_{\rm xy}(x - x_o)(y - y_o)
     534\end{eqnarray}
     535The object model will have a variety of model parameters, in this case
     536the centroid coordinates ($x_o, y_o$), the elliptical shape parameters
     537($\sigma_x, \sigma_y, \sigma_{\rm xy}$), the model normalization
     538($I_o$) and the local value of the background ($S$).  A specific
     539object will have a particular set of values for these different
     540parameters.
     541
     542The point-spread-function (PSF) of an image describes the shape of all
     543unresolved objects in the image.  In a typical image, the shape of
     544point sources is not well described by a single functional form;
     545rather, the shape will vary as a function of position in the image.
     546The PSF model therefore must describe the parameter variation as a
     547function of the position of the object on the image.  Note that the
     548object model consists of a certain number of parameters which are
     549defined by the PSF model, and another set of parameters which are
     550independent from object to object.  For the case of the elliptical
     551Gaussian model, the PSF parameters would be the shape terms
     552($\sigma_x, \sigma_y, \sigma_{\rm xy}$) while the independent
     553parameters would be the centroid, normalization and local sky values
     554($x_o, y_o, I_o, S$).  PSPhot uses a 2-D polynomial to specify the
     555variation in the PSF parameters as a function of position in the
     556image.  In the case of the elliptical Gaussian, this implies that the
     557parameters are each a function of the object centroid coordinates:
     558\begin{eqnarray}
     559\sigma_x    & = & f_1(x,y) \\
     560\sigma_y    & = & f_2(x,y) \\
     561\sigma_{xy} & = & f_3(x,y) \\
     562\end{eqnarray}
     563
     564PSPhot uses a single structure to represent the object model and
     565another structure to represent the PSF model.  The object model
     566structure consists of the collection of measured object model
     567parameters, carried as a \code{psLib} vector (\code{psVector}) along
     568with an equal-length vector with the parameter errors.  The structure
     569also includes an integer giving the identifier of the model used in
     570the particular case, as well as model fit statistics such as the
     571Chi-Square of the fit and the magnitude representation of the ratio
     572between the model flux and an aperture flux (see below for more
     573details on this value).
     574
     575The PSPhot representation of the PSF consists of an array of
     576polynomials, each representing the variation in the object model PSF
     577parameters (\code{psArray} of \code{psPolynomial2D}).  The PSF model
     578structure also includes the same integer used to identify which model
     579corresponds to particular instance of the PSF.  At the moment, the
     580number of PSF parameters is a fixed number (4) fewer than the number
     581of parameters of the corresponding object model.  For example, the
     582elliptical Gaussian model uses 7 parameters to represent the object and
     5833 for the PSF model. 
     584
     585PSPhot is written so that the object detection, measurement, and
     586classification code does not depend on the specific form of the
     587available object model functions.  Access to the characteristics of
     588the models is provided through a simple function abstraction method.
     589Throughout PSPhot, there are many places where it is necessary for the
     590code to refer to an aspect of the object or PSF model.  Often, these
     591quantities are needed deep within other parts of the code.  For
     592example, when attempting to fit the pixel flux values for an object,
     593it is necessary to generate a guess for the model parameters.  Or, in
     594order to limit the domain of the fit, it is necessary to determine an
     595isophotal radius for a model. 
     596
     597In order to avoid having the code depend on the specific form of a
     598model, the function calls needed in these types of circumstances are
     599abstracted, and a method is provided to return the necessary function
     600to the higher-level software.  For example, each model type has its
     601own function to define an initial guess for the model, or a function
     602to determine the radius for a given flux level.  These are then
     603registered as part of the model function code.  Another function is
     604then used to return the appropriate function for a specific model
     605type.  For example, the \code{psModelLookup\_GetFunction} will return
     606the \code{psModelLookup} function for a given model type.  This
     607mechanism makes it very easy to add new model functions into the
     608PSPhot code base.  To add a new model function, the programmer simply
     609defines a new model name (a string), the set of all necessary model
     610lookup functions, and places the reference to the model code at the
     611appropriate location in the psModelInit.c routine.
     612
     613When a new model is provided to PSPhot, it is not necessary to specify
     614the intended use of the object model function (ie, PSF-like object,
     615galaxy, comet, etc).  Any model can be used for the PSF model, or to
     616describe the flux distributions of the non-PSF objects.  The code
     617currently uses a fixed translation between the object model parameters
     618and the PSF model parameters.  It also defines a specific order for
     619the 4 independent parameters. 
     620
     621\note{the code may also require that two of the PSF-like parameters
     622represent the shape in some way}.
     623
     624\subsubsection{PSF Candidate Object Selection}
     625
     626The first stage of determining the PSF model for an image is to
     627identify a collection of objects in the image which are {\em likely}
     628to be PSF-like.  PSPhot uses the object moments to make the initial
     629guess at a collection of PSF-like objects.  At this point, the program
     630has measured the second order moments for all objects identified by
     631their peaks, as well as an approximate signal-to-noise ratio.  All
     632objects with a S/N ratio greater than a user-defined parameter
     633(\code{PSF\_SHAPE\_NSIGMA} ???) are selected by PSPhot, though objects
     634which have more than a certain number of saturated pixels are excluded
     635at this stage.  PSPhot then examines the 2-D plane of $\sigma_x,
     636\sigma_y$ in search of a concentrated clump of objects.  To do this,
     637it constructs an artificial image with pixels representing the value
     638of $\sigma_x, \sigma_y$, using a user-defined scale for the size of a
     639pixel in this artificial image (note that the units of the $\sigma_x,
     640\sigma_y$ plane are the size of the second-moment in pixels in the
     641original image).  A typical value for the bin size is approximately
     6420.1 image pixels.  The binned $\sigma_x, \sigma_y$ plane is then
     643examined to find a peak which has a significance greater than XXX.
     644Unless the image is extremely sparse, such a peak will be well-defined
     645and should represent the objects which are all very similar in shape.
     646Other objects in the image will tend to land in very different
     647locations, failing to produce a single peak.  To avoid detecting a
     648peak from the unresolved cosmic rays, objects which have
     649second-moments very close to 0 are ignored.  The only danger is if the
     650PSF is very small and too many of these objects are rejected as cosmic
     651rays.
     652
     653Once a peak has been detected in this plane, the centroid and second
     654moments of this peak are measured.  All objects which land within XXX
     655$\sigma$ of this centroid are selected as likely PSF-like objects in
     656the image. 
     657
     658\subsubsection{PSF Candidate Object Model Fits}
     659
     660All candidate PSF objects are then fitted with the selected object
     661model, allowing all of the parameters (PSF and independent) to vary in
     662the fit.  PSPhot uses the Levenberg-Marqardt process for the
     663non-linear fitting.  Non-linear fitting can be very computationally
     664intensive, particularly for if the starting parameters are far from
     665the minimization values.  PSPhot uses the first and second moments to
     666make a good guess for the centroid and shape parameters for the PSF
     667models.  \note{still true? In order to minimize the impact of close
     668  neighbors, the noise values used in the fit are enhanced by a
     669  fraction of the deviation of the particular pixel value from the
     670  model guess.}  Any objects which fail to converge in the fit are
     671flagged as invalid.
     672
     673\note{does the noise enhancement introduce too much bias?}
     674
     675\note{discuss the convergence criteria, model parameter guesses}
     676
     677For the resulting collection of object model parameters, the
     678PSF-dependent parameters of the models are all fitted as a function of
     679position to a 2-D polynomial.  The order of this polynomial is (should
     680be?) a user-defined parameter.  The fitting process for these
     681polynomials is iterative, and rejects the $3-\sigma$ outliers in each
     682of three passes.  This fitting technique results in a robust
     683measurement of the variation of the PSF model parameters as a function
     684of position without being excessively biased by individual objects
     685which fail drastically.  Objects whose model parameters are rejected
     686by this iterative fitting technique are also marked as invalid and
     687ignored in the later PSF model fitting stages.
     688
     689All of the PSF-candidate objects are then re-fitted using the PSF
     690model to specify the dependent model parameter values for each object.
     691For example, in the case of the elliptical Gaussian model, the shape
     692parameters ($\sigma_x, \sigma_y, \sigma_{xy}$) for each object are
     693set by the coordinates of the object centroid and fixed (not allowed
     694to vary) in the fitting procedure.  The resulting fitted models are
     695then used to determine a metric which tests the quality of the PSF
     696model for this particular image. 
     697
     698The metric used by PSPhot to assess the PSF model is the scatter in
     699the differences between the aperture and fit magnitudes for the PSF
     700objects.  The difference between the aperture and fit magnitudes ({\em
     701ApResid}) is a critical parameter for any PSF modeling software which
     702uses an analytical model to represent the flux distribution of the
     703objects in an image.  An approximate correction is measured here, with
     704a more detailed correction measured after all object analysis is
     705performed.  The PSF model with the best consistency of the aperture
     706correction is judged to be the best model.
     707
     708\subsubsection{Basic Deblending}
     709
     710The collection of identified peaks is examined to find peaks which are
     711'blended', that is, they are close enough together to make the
     712analysis of one of the sources difficult if performed in isolation.
     713Saturated stars also result in additional peaks which are likely to be
     714invalid; it is useful to restrict a saturated star to a single primary
     715position with associated neighboring peaks.
     716
     717The deblending process first searches for any peaks which are within
     718the image cell of another peak.  All such groups are examined,
     719starting with the brightest source.  An isophot is found about the
     720primary peak which is at least \code{DEBLEND\_SKY\_NSIGMA} times the sky
     721sigma above the local background and which is otherwise
     722\code{DEBLEND\_PEAK\_FRACTION} of the primary peak central pixel flux.
     723Any secondary sources which are contained within this isophot are
     724considered to be blended peaks associated with the primary peak. 
     725
     726\subsection{Bright Source Analysis}
     727
     728After a PSF model has been determined, PSPhot performs the analysis of
     729the bright objects in the image.  In this stage, all of the objects
     730with an estimated signal to noise (based on the moments analysis)
     731greater than a user-set threshold are analysed and subtracted from the
     732image.  An optional successive stage then finds fainter sources and
     733measures them as well (see Faint Source Analysis,
     734Section~\ref{faintsources}).  In the bright source analysis stage, two
     735major varients are available.  In the primary version, all objects are
     736examined (in decending order of brightness) and an appropriate models
     737is determined for each object which is then subtracted; in the
     738alternate version, the objects are examined (in decending order of
     739brightness) and the PSF-like objects subtracted first, then the
     740extended objects are analysed on a second pass.
     741
     742\subsubsection{Fast Ensemble PSF Fitting}
     743
     744Before the detailed analysis of the objects is performed, it is
     745convenient to subtract off all of the sources, at least as well as
     746possible at this stage.  We make the assumption that all sources are
     747PSF-like.  We also assume their position can be taken as the peak of a
     7482D quadratic function fitted to the peak pixel and its surrounding 8
     749pixels.  A single linear fit is used to simultaneously measure all
     750source fluxes.  Since the local sky has been subtracted, this
     751measurement assumes the local sky is zero. 
     752
     753\[
     754\chi^2 = \sum_{\rm pixels} (F_{x,y} - \sum_{\rm sources} A_i PSF[x,y])^2
     755\]
     756
     757Minimizing this equation with respect to each of the $A_i$ values
     758results in a matrix equation:
     759\[ M_{i,j} \bar{A_i} = \bar{F_j}\]
     760where $\bar{A_i}$ is the vector of $A_i$ values, the elements of
     761$M_{i,j}$ consist of the dot product of the unit-flux PSF for source
     762$i$ and source $i$, and $\bar{F_j}$ is the dot product of the
     763unit-flux PSF for source $i$ with the pixels corresponding to source
     764$i$.  The dot products are calculated only using pixels within the
     765source apertures.  Since most sources have no overlap with most other
     766sources, this matrix equation results in a very sparse, mostly
     767diagonal square matrix.  The dimension is the number of sources,
     768likely to be 1000s or 10,000s.  Such a matrix does not lend itself to
     769direct inversion.  However, an interative solution quickly yields a
     770result with sufficient accuracy.  In the iterative solution, a guess
     771at the solution is made; the guess is multiplied by the matrix, and
     772the result compared with the observed vector $\bar{F_j}$.  The
     773difference is used to modify the initial guess. This proces is
     774repeated several times to achieve a good convergence. 
     775
     776Once a solution set for $A_i$ is found, all of the objects are
     777subtracted from the by applying these values to the unit-flux PSF.
     778
     779\subsubsection{PSF Model applied to detected objects}
     780
     781Once a PSF model has been selected for an image, PSPhot attempts to
     782fit all of the detected objects, above a user-defined signal-to-noise
     783ratio (\note{KEYWORD}) with the PSF model.  For these fits, the
     784dependent parameters are fixed by the PSF model and only the 4
     785independent object model parameters are allowed to vary in the fit.
     786PSPhot again uses the Levenberg-Marqardt process for the non-linear
     787fitting.  The objects are fitted in their S/N order, starting with the
     788brightest and working down to the user-specified limit.
     789
     790Once a solution has been achieved, PSPhot attempts to judge the
     791quality of the PSF model as a representation of the object shape.  To
     792do this, it calculates the next step of the minimization {\em allowing
     793the shape parameters to vary}.  This step, essentially the
     794Gauss-Newton minimization distance from the current local minimum,
     795should be very small if the object is well represented by the PSF, but
     796large if the PSF is not a good representation of the object flux.  The
     797model quality is judged by the change in the two shape parameters
     798which represent the 2D size of the object.  For the case of the
     799elliptical Gaussian, these two parameters are $\sigma_x$ and
     800$\sigma_y$.  For a generic model, the shape parameters may be defined
     801differently, but the should always be two parameters which scale the
     802object size in two dimensions (what about a polar-coordinate form?)
     803Currently, PSPhot requires the two relevant shape parameters to be the
     804first two dependent parameters in the list of model parameters (ie,
     805parameters 4 \& 5).
     806
     807The expected distribution of the variation of the two shape parameters
     808will be a function of the signal-to-noise of the object in question
     809and the value of the shape parameter itself.  The expected standard
     810deviation on the shape parameter is, eg, $\sigma_x / {\rm SN}$.  If
     811the object is well-represented by the PSF, then the shape parameter
     812values should be close to their minimization value.  We can thus ask,
     813for each object, given the measured amplitude of the Gauss-Newton
     814step, how many standard deviations from the expected value (of 0.0) is
     815this particular value?  Objects for which the variation in the shape
     816parameters is a large positive number of standard deviations are
     817likely to be better represented by a larger flux distribution than the
     818PSF (eg, a Galaxy or Comet, etc).  Objects for which the variation in
     819the shape parameters is a large negative number of standard deviations
     820are likely to be better represented by a smaller flux distribution
     821than the PSF (ie, a cosmic ray or other defect).  A user-defined
     822number of standard deviations is used to select these two cases, and
     823to flag the object as a likely galaxy (really meaning 'extended') or
     824as a likely defect. 
     825
     826At this stage of the analysis, PSPhot uses two additional indicators
     827to identify good and poor PSF fits.  The first of these is the
     828signal-to-noise ratio.  It is possible for the peak finding algorithm
     829to identify peaks in locations which are not actually a normal peak.
     830Some of these cases are in the edges of saturated, bleeding columns
     831from bright stars, in the nearly flat halos of very bright stars, and
     832so on.  In these cases, a local peak exists, with a lower nearby sky
     833region.  However, the fitted PSF model cannot converge on the peak
     834because it is very poorly defined (perhaps only existing in the
     835smoothed image).  The fit can either fail to converge or it can
     836converge on a fit with very low or negative peak flux / flux
     837normalization.  PSPhot will flag any non-convergent PSF fit and any
     838object with PSF S/N ratio lower than a user-defined cutoff.  It is
     839also useful to identify very poor fits by setting a maximum Chi-Square
     840cutoff for objects. 
     841
     842As the objects are fitted to the PSF model, those which survive the
     843exclusion stage are subtracted from the image.  The subtraction
     844process modifies the image pixels (removing the fitted flux, though
     845not the locally fitted background) but does not modify the mask or the
     846noise images.  The signal-to-noise ratio in the image after
     847subtraction represents the significance of the remaining flux.  If the
     848subtractions are sufficiently accurate models of the PSF flux
     849distribution, the remaining flux should be below 1 $\sigma$
     850significance.  In practice the cores of bright stars are poorly
     851represented and may have larger residual significance. \note{in future
     852work, we may choose to enhance the noise to minimize detection of
     853objects in the residuals of brighter objects}.
     854
     855\subsubsection{Blended Sources}
     856
     857Sources which are blended with other sources are fitted together as a set of
     858PSFs.  A single multi-object fit is performed on all blended peaks.
     859The resulting fits are evaluated independently and any which are
     860determined to be PSFs are subtracted from the image.
     861
     862\subsubsection{Double Sources}
     863
     864Sources which are judged to be non-PSF-like are confronted with two
     865possible alternative choices.  First, the object is fitted with a
     866double-source model.  In this pass, the assumption is made that there
     867are two neighboring sources, but the peaks are blended together, or
     868otherwise not distinguished.  The initial guess for the two peaks is
     869made by splitting the flux of the single source in half and locating
     870the two starting peaks at +/- 2 pixels from the original peak along
     871the direction of the semi-major axis of the sources, as measured from
     872the second moments.  In order for the two-source model to be accepted,
     873both sources must be judged as a valid PSF source.  Otherwise, the
     874double-PSF model is rejected and the source is fitted with the
     875available non-PSF model or models.
     876
     877\note{better description of the acceptance criteria; the FLT model is
     878  tried before the DBL is accepted or rejected}.
     879
     880\subsubsection{Non-PSF Objects}
     881
     882Once every object (above the S/N cutoff) has been confronted with the
     883PSF model, the objects which are thought to be galaxies (extended) can
     884now be fit with appropriate models for the galaxies (or other likely
     885extended shapes).  Again, the fitting stage starts with the brightest
     886sources (as judged by the rough S/N measured from the moments
     887aperture) and working to a user defined S/N limit. 
     888
     889PSPhot will use the user-selected galaxy model to attempt the galaxy
     890model fits.  In the configuration system, the keyword \code{GAL\_MODEL}
     891is set to the model of interest.  All suspected extended objects are
     892fitted with the model, allowing all of the parameters to float.  The
     893initial parameter guesses are critical here to achieving convergence
     894on the model fits in a reasonable time.  The moments and the pixel
     895flux distribution are used to make the initial parameter guess.  Many
     896of the object parameters can be accurately guessed from the first and
     897second moments.  The power-law slope can be guessed by measuring the
     898isophotal level at two elliptical radii and comparing the ratio to
     899that expected.
     900
     901For each of the galaxy models (in fact for all object models), a
     902function is defined which examines the fit results and determines if
     903the fit can be consider as a success or a failure.  The exact criteria
     904for this decision will depend on the details of the model, and so this
     905level of abstraction is needed.  For example, in some case, the range
     906of valid values for each of the parameters must be considered in the
     907fit assessment.  In other cases, we may choose to use only the
     908parameter errors and the fit Chi-Square value.
     909
     910All galaxy model fits which are successful are then subtracted from
     911the image as is done for the successful PSF model fits.  Of course,
     912the background flux is retained, with the result that only the object
     913is subtracted from the image.  Again, the noise image is (currently)
     914not modified. 
     915
     916\note{we have no code yet to select the best of several models for a
     917  given objects.  The relative value of the Chi-Square is the obvious
     918  test in this case}.
     919
     920\subsection{Faint Sources}
     921
     922\note{this is not done : should use the ensemble PSF fitting to fit
     923  just the new significant peaks}
     924
     925After a first pass through the image, in which the brighter sources
     926above a high threshold level have been detected, measured, and
     927subtracted, PSPhot optionally begins a second pass at the image.  In
     928this stage, the new peaks are detected on the image with the bright
     929objects subtracted.  In this pass, the peak detection process uses the
     930noise image to test the validity of the individual peaks.  All peaks
     931with a significance greater than a user-defined minimum threshold are
     932accepted as objects of potential interest. 
     933
     934The objects which are measured in this faint-object stage are clearly
     935low significance detections.  A typical threshold for the bright
     936object analysis would S/N of 5 - 10.  The lower limit cutoff for the
     937faint object analysis would typically be S/N of 2 - 4.  In this stage,
     938PSPhot can perform one of three types of analysis.  The difference
     939between these options is one of speed vs detail.
     940
     941In the first option, PSPhot can repeat the analysis described above in
     942sections XXX and XXX, performing a PSF fit followed by a non-PSF fit
     943to the objects which are not PSF-like, and subtracting them.  The
     944advantage of this option is that the faint objects are treated
     945identically to the bright objects, and all potential parameters are
     946measured, even for marginally extended sources.  The disadvantage of
     947this option is that the low signal-to-noise of the objects in this
     948stage limits the amount of information which can be extracted from
     949them.  The marginal gain may not be worth the large expense of
     950processing time.
     951
     952In the second option, PSPhot can perform only the PSF model fit to the
     953remaining peaks, but ignore any further questions of the shape of the
     954objects.  The advantage of this option is that it is substantially
     955faster than performing the more complex (and less stable)
     956multi-parameter non-linear fits on all faint objects.  On the
     957downside, less information is learned about the objects.
     958
     959Finally, PSPhot can simply ignore the fitting process and instead
     960glean information about the fainter sources on the basis of the peak
     961characteristics.  In this option, the image is smoothed with the PSF
     962model, and the peak for each object is measured.  The peak flux and
     963the local peak curvature theoretically give sufficient information to
     964recover the object flux, the centroid coordinates, and the centroid
     965errors.  The advantage of the stage is speed, especially for the very
     966faintest levels: if the lower limit is not sufficiently faint, the
     967time spent in performing the smoothing (3 FFTs) cannot make up for the
     968time which would have been spent applying the PSF model to the peaks.
     969The downside of this method is an increased sensitivity to the local
     970sky model (the local sky must be correctly subtracted) and fewer
     971constraints on the quality of the detection (no Chi-Square is
     972measured, for example).
     973
     974\note{In the ideal case, if we were only interested in detecting PSFs,
     975and we had a good model for the PSF, we could optimally find the
     976sources by smoothing the image and the noise image with the PSF model.
     977\em write out the description of Nick's optimal PSF finding}.
     978
     979PSPhot allows the user to select between these three options for the
     980analysis of the faint sources.  Three separate user-controlled
     981signal-to-noise ratio limits are defined.  One specifies the depth to
     982which the PSF / non-PSF analysis is performed.  A second (which must
     983be smaller) specifies the depth to which only the PSF is fitted.  A
     984third specifies the depth to which the analysis is performed using on
     985the peak statistics.  If two of these are identical, then certain of
     986these options are simply skipped.  For example, if the peak analysis
     987threshold is set to the same value as the PSF-only threshold, no peak
     988analysis is performed.
     989
     990\subsection{Aperture Correction Measurement}
     991
     992The important concept here is that an analytical model will always
     993fail to describe the flux of the objects at some level.  In the end,
     994all astronomical photometry is in some sense a relative measurement
     995between two images.  Whether the goal is calibration of a science
     996image taken at one location to a standard star image at another
     997location, or the goal is simply the repetitive photometry of the same
     998star at the same location in the image, it is always necessary to
     999compare the photometry between two images.  If this measurement is to
     1000be consistent, then the measurement must represent the flux of the
     1001stars in the same way regardless of the conditions under which the
     1002images were taken, at least within some range of normal image
     1003conditions.  So, for example, two images with different image quality,
     1004or with different tracking and focus errors, will have different PSF
     1005models.  Since an analytical model will always fail to represent the
     1006flux of the star at some level, the measured flux of the same object
     1007in the two images will be different (even assuming all other
     1008atmospheric and instrumental effects have been corrected).  The
     1009amplitude of the error will by determined by how inconsistently the
     1010models represent the actual object flux.  For example, if the first
     1011image PSF model flux is consistently 10\% too low and the second is 5\%
     1012too high, then the comparison between the two images will be in error
     1013by 15\%. 
     1014
     1015Aperture photometry avoids these problems, by trading for other
     1016difficulties.  In aperture photometry, if a large enough aperture is
     1017chosen, the amount of flux which is lost will be a small fraction of
     1018the total object flux.  Even more importantly, as the image conditions
     1019change, the amount lost will change by an even smaller fraction, at
     1020least for a large aperture.  This can be seen by the fact that the
     1021dominant variations in the image quality are in the focus, tracking
     1022and seeing.  All of these errors initially affect the cores of the
     1023stellar images, rather than the wide wings.  The wide wings are
     1024largely dominated by scattering in the optics and scattering in the
     1025atmosphere.  The amplitude and distribution of these two scattering
     1026functions do not change significantly or quickly for a single
     1027telescope and site. 
     1028
     1029The difficulty for aperture photometry is the need to make an accurate
     1030measurement of the local background for each object.  As the aperture
     1031grows, errors in the measurement of the sky flux start to become
     1032dominant.  If the aperture is too small, then variation in the image
     1033quality are dominant.  The brighter is the object, the smaller is the
     1034error introduced by the large size of the aperture.  However, the
     1035number of very bright stars is limited in any image, and of course the
     1036brighter stars are more likely to suffer from non-linearity or
     1037saturation. 
     1038
     1039\note{this discussion sucks: put in some more details of my point:
     1040  amplitude of systematic vs random sky errors}
     1041
     1042How important is this effect?  Consider a typical bright object with a
     1043flux of (say) 40,000 counts in an image of background 1000 counts per
     1044pixel, with FWHM of 4 pixels.  In principle, the flux of this object
     1045should be measurable with an accuracy of roughly 0.57\%
     1046($\frac{\sqrt{40000 + 1000 \times 12}}{40000}$).  However, the
     1047measurement of the sky is limited at some finite level by Poisson
     1048statistics.  If we are required to use an aperture of (say) 25 pixels
     1049in radius (eg, 5 arcseconds for an 0.2 arcsec / pixel detector), and
     1050we have an annulus of twice this radius to measure the local sky, then
     1051we will have an error of XXX.
     1052
     1053\note{outline the variation of {\em ApResid} as a function of
     1054magnitude}.
     1055
     1056PSPhot measures the aperture correction ({\em ApResid}) for every PSF
     1057candidate object, then calculates the trend of this correction as a
     1058function of the magnitude.  This trend is fitted with a line.  The
     1059resulting function can be used to determine the effective aperture
     1060correction for an infinite flux object and the average bias inherent
     1061in the sky measurement for the image.  The scatter of the
     1062PSF-candidate object measurements about this trend is a measure of how
     1063well we can measure photometry from the image by applying the specific
     1064PSF model.  The slope of this trend is a measure of the bias in the
     1065local sky measurment for each object.  In principal, the measured sky
     1066levels could be modified by this bias.  More generally, the measured
     1067bias in a collection of images could be used to improve the model
     1068fitting or sky fitting portion of the software the remove the bias
     1069term.
     1070
     1071PSPhot allows a collection of PSF model functions to be tried on all
     1072PSF candidate objects.  For each model test, the above corrected
     1073ApResid scatter is measured.  The PSF model function with the smallest
     1074value for the ApResid scatter is then used by PSPhot as the best PSF
     1075model for this image.  The number of models to be tested is specified
     1076by the configuration keyword \code{PSF\_MODEL\_N}.  The configuration
     1077variables \code{PSF\_MODEL\_0}, \code{PSF\_MODEL\_1}, through
     1078\code{PSF\_MODEL\_N - 1} specify the names of the models which should be
     1079tested.
     1080
     1081\subsubsection{Types of Object / PSF models currently available}
     1082
     1083\note{the discussion of the model types needs to be extended}
     1084
     1085\begin{itemize}
     1086\item GAUSS  : Pure elliptical Gaussian
     1087\item PGAUSS : polynomial approximation to a Gaussian (PGAUSS)
     1088\item QGAUSS : power law with variable exponential term
     1089\item SGAUSS :
     1090\end{itemize}
     1091
     1092\note{discuss the stability issues with the galaxy model(s)}
     1093
     1094\subsection{Output Options}
     1095
     1096\note{need to discuss tests}
     1097
     1098\note{need to discuss failings and holes}
     1099
     1100\section{Alternative Scenarios}
     1101
     1102\subsection{Trailed Sources}
     1103
     1104\subsection{Fixed / Known-position Sources}
     1105
     1106\subsection{Difference Images}
     1107
     1108The noise map for a difference image must be generated from the two
     1109images use to construct the difference.  Otherwise, the low sky level
     1110will automatically result in inconsistent interpretation of the noise.
     1111
     1112For a difference image, both positive and negative objects will be
     1113present.  The basic peak detection algorithm will only trigger for the
     1114positive sources.  One solution is to simply apply PSPhot to both the
     1115difference image and its negative value.  \note{do we want to code in
     1116an automatic switch to get both positive and negative excursions in
     1117the single pass?}.
     1118
     1119In the case of a difference image, the PSF model construction stage
     1120will probably fail for lack of valid sources.  It is better in these
     1121cases to provide PSF model from some other source.  For example, the
     1122two images which are combined to generate the difference image
     1123represent the PSF.  Presumably, one or both have been convolved with a
     1124PSF-matching kernel.  The images which result from the convolution
     1125should be used to measure the PSF model. 
     1126
     1127The object classification scheme defaults to the galaxy models for
     1128objects which are not well represented by the PSF model.  In a
     1129properly-constructed difference image, galaxies are unlikely to remain
     1130behind as significant sources.  Most real objects in the difference
     1131image will be PSF-like and will consist of photometrically variable
     1132objects (flare stars, supernovae, etc) or astrometrically variable
     1133objects (high-proper motion stars or solar-system objects).  There are
     1134three likely classes of objects which will not be well represented by
     1135the PSF model.  1) Fast-moving solar-system objects will appear as
     1136short streaks.  For example, a fast solar system object would have an
     1137apparent rate of 0.5 degrees per hour, translating to 15 arcseconds in
     1138a 30 second exposure.  Even a main belt asteroid at roughly 1 AU would
     1139have reflect motion of approximately 1 degree per day, equivalent to
     11401.25 arcsec in a 30 second exposure, and could be noticeably smeared
     1141and non-PSF-like.  A trailed-star model can be used to characterize
     1142these types of objects.  2) Small offset stars, either due to
     1143atmospheric / color effects or modest proper motion will appear as PSF
     1144dipoles in the difference images.  The positive and the negative
     1145images will have stellar profiles, but they will be significantly
     1146offset and will not subtract well.  The two components may not have
     1147the same amplitude.  A PSF-dipole model can be used to fit these types
     1148of objects, with free parameters of the two centroids and the two
     1149fluxes.  3) Comets will appear in the difference images as a non-PSF
     1150objects.  Their 2-D structure includes both the flux from the coma
     1151(with a typical power-law profile) and flux from the tail (with a more
     1152complex flux distribution).  A comet flux model can be used to
     1153characterize these objects in difference images.  A major difficulty
     1154in applying these three types of models is in making a robust test of
     1155which model should be used.  This problem is akin to the issue of
     1156selecting and distinguishing between multiple galaxy models, as
     1157discussed in the section on Galaxy models.
     1158
     1159\section{PSPhot Structures and Data Elements}
     1160
     1161The following structures are described in detail in the document
     1162`Pan-STARRS PS-1 Image Processing Pipeline Modules Supplementary
     1163Design Requirements' (psModules SDRS; PSDC-430-012).
     1164
     1165\begin{verbatim}
     1166 pmModel
     1167 pmModelGroup
     1168 pmGrowthCurve
     1169 pmPSF
     1170 pmPSFTry
     1171 pmSource
     1172 pmPeak
     1173 pmMoments
     1174\end{verbatim}
     1175
     1176\note{psphot is supposed to operate on individual readouts, and use
     1177  the techniques used by ppImage to extract header-related metadata.
     1178  currently, psphot uses an alternative to the psReadout until the
     1179  ppImage code can be folded together with psphot}.
     1180
     1181\subsection{Top-Level APIs}
     1182
     1183\begin{verbatim}
     1184psMetadata     *psphotArguments (int *argc, char **argv);
     1185\end{verbatim}
     1186Load the command-line arguments, parse the configuration file, and
     1187place the configuration information on a single metadata structure.
     1188This function searches for the following command line option flags,
     1189and places their corresponding values on the output metadata with the
     1190given name.  These options override any such values in the
     1191configuration file.
     1192\begin{verbatim}
     1193-mask (filename)      : MASK_IMAGE
     1194-weight (filename)    : WEIGHT_IMAGE
     1195-resid (filename)     : RESID_IMAGE
     1196-region [x0:x1,y0:y1] : ANALYSIS_REGIONP
     1197-photcode (code)      : PHOTCODE
     1198-psf (filename)       : PSF_INPUT_FILE
     1199-modeltest x y        : TEST_FIT_X, TEST_FIT_Y
     1200-model (name)         : TEST_FIT_MODEL
     1201-fitmode (name)       : TEST_FIT_MODE
     1202-fitset (name)        : TEST_FIT_SET
     1203\end{verbatim}
     1204
     1205The following option flags can be used to set any option:
     1206\begin{verbatim}
     1207-D  (key) (value)      : any string value
     1208-Df (key) (value)      : any F32 value
     1209-Di (key) (value)      : any S32 value
     1210\end{verbatim}
     1211
     1212The function next examines the remaining command-line arguments and
     1213complains if there are not exactly 3 arguments, reporting the program
     1214usage.  It sets default configuration variables, and then loads the
     1215configuration file specified as the third command-line option.
     1216Finally, it sets the \code{IMAGE} and \code{OUTPUT\_FILE} config
     1217options to arguments 1 and 2, respecitively.
     1218
     1219\begin{verbatim}
     1220eamReadout     *psphotSetup (psMetadata *config);
     1221\end{verbatim}
     1222This function examines the configuration data in \code{config} and
     1223loads the image into memory.  It constructs the weight and mask images
     1224if they have not been specified, or loads the specified images.  The
     1225weight image is built based on the read noise and gain of the image,
     1226as extracted from the header or from the configuration options
     1227directly.  It defines the mask based on the selection image region,
     1228the values for saturation and the \code{min\_VALID\_PIXEL}. 
     1229
     1230\begin{verbatim}
     1231bool            psphotModelTest (eamReadout *imdata, psMetadata *config);
     1232\end{verbatim}
     1233This function is an optional test mode for psphot.  If the test mode
     1234has been selected, this function will attempt to fit a single object
     1235with the requested model.  It writes out subimage containing the
     1236source, the difference, the mask, and the weight.  This function may
     1237load a PSF model or fit a floating model.
     1238
     1239\begin{verbatim}
     1240psStats        *psphotImageStats (eamReadout *imdata, psMetadata *config);
     1241\end{verbatim}
     1242Measure the basic image properties: median sky, expected sky sigma
     1243
     1244\begin{verbatim}
     1245psPolynomial2D *psphotImageBackground (eamReadout *imdata, psMetadata *config, psStats *sky);
     1246\end{verbatim}
     1247Model the image background as a 2D polynomial and subtract from the
     1248image.   The should use a more sophisticated model and return the
     1249subtracted image.
     1250
     1251\begin{verbatim}
     1252psArray        *psphotFindPeaks (eamReadout *imdata, psMetadata *config, psStats *sky);
     1253\end{verbatim}
     1254Create a smoothed image and find all local peaks above the threshold
     1255level (uses: \code{PEAKS\_SMOOTH\_SIGMA, PEAKS\_SMOOTH\_NSIGMA,
     1256PEAKS\_NSIGMA\_LIMIT, PEAKS\_OUTPUT\_FILE})
     1257
     1258\begin{verbatim}
     1259psArray        *psphotSourceStats (eamReadout *imdata, psMetadata *config, psArray *allpeaks);
     1260\end{verbatim}
     1261Create the basic source structures for all peaks, define the initial
     1262pixels, measure the local sky (sky offset) and the source moments.
     1263
     1264\begin{verbatim}
     1265bool            psphotRoughClass (psArray *sources, psMetadata *config);
     1266\end{verbatim}
     1267Find the PSF clump and make the first cut source identifications
     1268
     1269\begin{verbatim}
     1270bool            psphotBasicDeblend (psArray *sources, psMetadata *config, psStats *sky);
     1271\end{verbatim}
     1272Find all blended peaks and tag, group with single primary source.
     1273
     1274\begin{verbatim}
     1275pmPSF          *psphotChoosePSF (psMetadata *config, psArray *sources, psStats *sky);
     1276\end{verbatim}
     1277Try each of the selected PSF models on a subset of likely PSF stars.
     1278Measure the metric (aperture residual scatter) for each PSF model and
     1279choose the best model. 
     1280
     1281\begin{verbatim}
     1282bool            psphotEnsemblePSF (eamReadout *imdata, psMetadata *config, psArray *sources, pmPSF *psf, psStats *sky);
     1283\end{verbatim}
     1284Perform simultaneous fitting to all sources in the array using a
     1285linear fitting process which assumes all sources are PSFs and their
     1286positions are fixed.  Set the positions based on the bilinear
     1287interpolation of the peak implied by the 3x3 square of pixels
     1288containing the peak.  Local sky is also assumed to be correctly subtracted.
     1289
     1290\begin{verbatim}
     1291bool            psphotFullFit (eamReadout *imdata, psMetadata *config, psArray *sources, pmPSF *psf, psStats *sky);
     1292\end{verbatim}
     1293Fit all sources in sequence starting from the brightest, and
     1294subtracting the sources as they are fitted.  This function only
     1295attempts single PSF and single EXT models and chooses between them.
     1296The sources are assumed to have been subtracted in advance (ie, using
     1297psphotEnsembleFit).  The models which do not succeed are re-subtracted
     1298using the prior model.
     1299
     1300\begin{verbatim}
     1301bool            psphotBlendFit (eamReadout *imdata, psMetadata *config, psArray *sources, pmPSF *psf, psStats *sky);
     1302\end{verbatim}
     1303Fit all sources in sequence starting from the brightest, and
     1304subtracting the sources as they are fitted.  This function attempts a
     1305multi-source fit for blended sources, or a single PSF if it is not a
     1306blend, followed by both EXT and DBL models and chooses between them.
     1307The sources are assumed to have been subtracted in advance (ie, using
     1308psphotEnsembleFit).  The models which do not succeed are re-subtracted
     1309using the prior model.
     1310
     1311\begin{verbatim}
     1312bool            psphotReplaceUnfit (psArray *sources);
     1313\end{verbatim}
     1314After models have been attempted for all sources, this function
     1315replaces the sources which were temporarily subtracted, but which did
     1316not succeed or converge on a good solution.
     1317
     1318\begin{verbatim}
     1319bool            psphotApplyPSF (eamReadout *imdata, psMetadata *config, psArray *sources, pmPSF *psf, psStats *sky);
     1320\end{verbatim}
     1321Attempt to fit the PSF model to all sources in brightness order,
     1322subtracting the resulting model if successful.  Only attempts single
     1323PSF models.   
     1324
     1325\begin{verbatim}
     1326bool            psphotFitExtended (eamReadout *imdata, psMetadata *config, psArray *sources, psStats *skyStats);
     1327\end{verbatim}
     1328Attempt to fit the PSF model to all sources in brightness order,
     1329subtracting the resulting model if successful.  Only attempts single
     1330EXT models.
     1331
     1332\begin{verbatim}
     1333bool            psphotApResid (eamReadout *imdata, psArray *sources, psMetadata *config, pmPSF *psf);
     1334 \end{verbatim}
     1335Measure the curve-of-growth and the aperture correction trend.
     1336
     1337\begin{verbatim}
     1338void            psphotOutput (eamReadout *imdata, psMetadata *config, psArray *sources, pmPSF *psf, psStats *sky);
     1339\end{verbatim}
     1340Write out data in various formats as selected.
     1341
     1342\section{User's Guide}
     1343
     1344\subsection{Configuration Parameters}
     1345
     1346\begin{verbatim}
     1347FAINT_SN_LIM
     1348FIT_MAX_CHI
     1349FIT_MIN_SN
     1350FIT_NSIGMA
     1351FIT_PADDING
     1352FIT_RADIUS
     1353GAIN
     1354GAL_MODEL
     1355GAL_MOMENTS_RADIUS
     1356INNER_RADIUS
     1357INPUT
     1358MASK
     1359NOISE
     1360NSUBSET
     1361OUTER_RADIUS
     1362OUTPUT
     1363OUTPUT_MODE
     1364PEAK_NSIGMA
     1365PSF_MODEL_N
     1366PSF_MOMENTS_RADIUS
     1367PSF_SHAPE_NSIGMA
     1368RDNOISE
     1369SATURATE
     1370SMOOTH_NSIGMA
     1371SMOOTH_SIGMA
     1372XMAX
     1373XMIN
     1374YMAX
     1375YMIN
     1376\end{verbatim}
     1377
     1378\subsection{Command-Line Arguments and Options}
     1379
     1380\subsection{Input \& Output Data Formats}
     1381
     1382\section{Sample Tests}
     1383
     1384\section{Further Work to be Completed}
     1385
     1386\begin{itemize}
     1387\item convert to pmCell as input data
     1388\item loop over all readouts in a pmCell
     1389\item write out multiple files?
     1390\item better method for defining the recipe?
     1391\item additional options for image background
     1392\item image background should return a background image
     1393\end{itemize}
    1121394
    1131395\end{document}
Note: See TracChangeset for help on using the changeset viewer.