Index: /trunk/doc/release.2015/ps1.analysis/analysis.tex
===================================================================
--- /trunk/doc/release.2015/ps1.analysis/analysis.tex	(revision 41346)
+++ /trunk/doc/release.2015/ps1.analysis/analysis.tex	(revision 41347)
@@ -45,5 +45,5 @@
 \def\Princeton{2}
 \def\DUR{3}
-\def\MPIA{4}
+\def\MPE{4}
 \def\CfA{5}
 
@@ -61,5 +61,5 @@
 L. Denneau,\altaffilmark{\IfA}
 P.~W. Draper,\altaffilmark{\DUR}
-D. Farrow,\altaffilmark{\DUR,\MPIA}
+D. Farrow,\altaffilmark{\DUR,\MPE}
 R. Jedicke,\altaffilmark{\IfA}
 K. W. Hodapp,\altaffilmark{\IfA}
@@ -88,5 +88,6 @@
 % \altaffiltext{\USNO}{US Naval Observatory, Flagstaff Station, Flagstaff, AZ 86001, USA}
 % \altaffiltext{\JHU}{Department of Physics and Astronomy, Johns Hopkins University, 3400 North Charles Street, Baltimore, MD 21218, USA}
-\altaffiltext{\MPIA}{Max Planck Institute for Astronomy, K\"onigstuhl 17, D-69117 Heidelberg, Germany}
+% \altaffiltext{\MPIA}{Max Planck Institute for Astronomy, K\"onigstuhl 17, D-69117 Heidelberg, Germany}
+\altaffiltext{\MPE}{Max-Planck-Institut f\"ur extraterrestrische Physik, Giessenbachstrasse 1, 85748 Garching, Germany}
 \begin{abstract}
 
@@ -207,7 +208,7 @@
 the analysis parameters to better suite the longer exposures.  This
 program as well as the rest of the Pan-STARRS Image Processing
-Pipeline (IPP) software suite is available for download from \url{http:ipp.ifa.hawaii.edu}}.
-
-\note{Generate a tarball of just the programs (skip certain directories)}
+Pipeline (IPP) software suite is available for download from \url{http://ipp.ifa.hawaii.edu}}.
+
+% \note{Generate a tarball of just the programs (skip certain directories)}
 
 %Chambers et al. 2017 (Paper I)
@@ -538,6 +539,6 @@
   individual astrometric measurements is 16 milliarcseconds and the
   Pan-STARRS Data Release 2 (DR2) astrometric system is tied to the
-  Gaia DR1 coordinate frame with a systematic uncertainty of $\sim 5$
-  milliarcseconds. }
+  Gaia DR1 \citep{2016AA...595A...4L} coordinate frame with a
+  systematic uncertainty of $\sim 5$ milliarcseconds. }
 
 \section{Basic Analysis}
@@ -1473,4 +1474,27 @@
 follow some of the observed PSF variations in the images.
 
+Figure~\ref{fig:iq.exposure} illustrates the 2D variations in the PSF
+shapes seen in PS1 data.  This figure shows the FWHM, $e_1$, and $e_2$
+polarizations of the stars as a function of position in 4 exposures.
+For images with good image quality, variations of the PSF shape due to
+the optical aberrations can be see.  The optical aberrations vary as
+the active collimation and alignment are adjusted and as the focus
+changes.  These aberrations are coupled to the piston of the chips,
+which have been adjusted to crudely follow the focal surface
+\citep{chambers2017}.  During regular operations, image with large
+PSFs are usually caused by the atmosphere (seeing) or by telescope
+tracking errors, both of which result in common shapes across the
+field of the camera.  In the figure, the top panel shows the
+circularization of the PSF due to the atmosphere washes out the
+lower-level variations caused by the optics.
+
+Examples of 2D PSF variations.
+    Each row represents an exposure.  The left-most column shows the
+    distribution of FWHM across the camera; the median value in
+    arcseconds is given in the inset.  The middle column gives the
+    $e_1$ polarization measured from second moments (see
+    Section~\ref{sec:lensing.params} while the right column gives the
+    $e_2$ polarization.
+
 % \note{write up the fitting process to define the grid?}
 
@@ -1498,4 +1522,19 @@
 quality of the PSF fits.
 
+% Figure 3: ** repaired PDF text **
+% pueo:psphot.iq.20200413/mana.sh : show.e12 (iq.exposures.pdf)
+\begin{figure}[htbp]
+  \begin{center}
+  \includegraphics[width=\hsize]{{\picdir/iq.exposures}.\plotext}
+  \caption{\label{fig:iq.exposure} Examples of 2D PSF variations.
+    Each row represents an exposure.  The left-most column shows the
+    distribution of FWHM across the camera; the median value in
+    arcseconds is given in the inset.  The middle column gives the
+    $e_1$ polarization measured from second moments (see
+    Section~\ref{sec:lensing.params}) while the right column gives the
+    $e_2$ polarization. }
+  \end{center}
+\end{figure}
+
 For the PS1 GPC1 analysis, we used the \code{PS1_V1} model, which we
 found by experimentation to match well to the observed profiles
@@ -1515,4 +1554,5 @@
 % buonanno : 1983A&AS...51...83B
 
+% Figure 4:
 % /data/kukui.3/eugene/psphot.20161214/mana.sh
 \begin{figure}[htbp]
@@ -1575,4 +1615,21 @@
 \subsubsection{Candidate PSF Source Selection}
 \label{sec:psf.source.selection}
+
+% Figure 5:
+% /data/kukui.3/eugene/psphot.20161214/mana.sh
+\begin{figure}[htbp]
+  \begin{center}
+  \includegraphics[width=\hsize]{{\picdir/moment.class}.\plotext}
+  \caption{\label{fig:moment.class} Illustration of PSF star selection
+    using the second moments. \textadd{Each point represents the
+      second moments in the $X_{\rm ccd}$ and $Y_{\rm ccd}$ directions
+      for sources measured in one chip (XY32) from a particular PS\,1
+      exposure (o6065g0428o)}.  The dominant clump is located in this
+    diagram \textadd{to identify the stars.}  Galaxies tend to have a range of
+    sizes and thus spread out above the stars.  Cosmic rays also have
+    a range of sizes, with one dimension smaller than the PSF.  The
+    red circle represents the PSF star candidates. }
+  \end{center}
+\end{figure}
 
 The first stage of determining the PSF model for an image is to
@@ -1618,20 +1675,4 @@
 most additional analyses and are marked with the flag bit
 \code{PM_SOURCE_MODE_SATURATED}.
-
-% /data/kukui.3/eugene/psphot.20161214/mana.sh
-\begin{figure}[htbp]
-  \begin{center}
-  \includegraphics[width=\hsize]{{\picdir/moment.class}.\plotext}
-  \caption{\label{fig:moment.class} Illustration of PSF star selection
-    using the second moments. \textadd{Each point represents the
-      second moments in the $X_{\rm ccd}$ and $Y_{\rm ccd}$ directions
-      for sources measured in one chip (XY32) from a particular PS\,1
-      exposure (o6065g0428o)}.  The dominant clump is located in this
-    diagram \textadd{to identify the stars.}  Galaxies tend to have a range of
-    sizes and thus spread out above the stars.  Cosmic rays also have
-    a range of sizes, with one dimension smaller than the PSF.  The
-    red circle represents the PSF star candidates. }
-  \end{center}
-\end{figure}
 
 \subsubsection{Candidate PSF Source Model Fits}
@@ -2039,5 +2080,5 @@
 \code{PM_SOURCE_MODE_EXT_LIMIT} is set for the source.
 
-\textmod{We decided to use $\delta M_{\rm KP}$ metric for this
+\textmod{We decided to use the $\delta M_{\rm KP}$ metric for this
   assessment after we tested several possible star-galaxy separation
   statistics.  We found that the Kron-PSF comparison was more reliable
@@ -2084,23 +2125,37 @@
 % apScale = 4.5
 
-Once a PSF model has been selected for an image, \ippprog{psphot}
-attempts to fit all of the detected sources, with signal-to-noise
-ratio greater than a user-defined limit, with the PSF model.  In the
-PV3 analysis of the $3\pi$ survey data, this limit was set to a
-signal-to-noise ratio of 20.0 for all analysis stages.  In these fits,
-the dependent parameters are fixed by the PSF model and only \textmod{the 3
-independent source model parameters (position in $X$ and $Y$ and flux
-normalization) are allowed to vary in the fit.  Note that we do {\em
-  not} allow the local sky to be fitted as a free parameters.  Since
-we have subtracted a model for the background, allowing the sky to be
-again at this stage is redundant.  In fact, in our testing, we found
-that allowing the sky to float resulted in higher scatter for the flux
-normalizations.}  \ippprog{psphot} again uses Levenberg-Marquardt
-minimization for the non-linear fitting.  The sources are fitted in
-their S/N order, starting with the brightest and working down to the
-user-specified limit, with the other sources subtracted as discussed
-above.  All sources for which a non-linear PSF model has been
-attempted have the flag bit \code{PM_SOURCE_MODE_FITTED} set,
-regardless of the quality of that fit.
+\textadd{At this point, we have a PSF model for the image, we have an
+assessment of the size (PSF-like, extended, or cosmic-ray) for each
+object, and we have fitted the PSF model for the normalization to each
+source (Section~\ref{sec:ensemble.fitting}).  However, the positions
+for the sources have been fixed to the position determined from the
+peak detection stage (Section~\ref{sec:peaks}) or the centroid from
+the analysis of the moments (Section~\ref{sec:moments}).  A better
+position, and thus a better normalization, can be determined by
+simultaneously fitting for all three parameters.  We therefore go
+through the image and re-fit the PSF model to each source
+one-at-a-time with all other sources subtracted based on the earlier
+fit.}
+
+\textmod{This re-fitting analysis is performed for all of the sources
+  with signal-to-noise ratio greater than a user-defined limit.  In
+  the PV3 analysis of the $3\pi$ survey data, this limit was set to a
+  signal-to-noise ratio of 20.0 for the \ippstage{chip} and
+  \ippstage{stack} analysis stages.  In these fits, the dependent
+  parameters are fixed by the PSF model and only the 3 independent
+  source model parameters (position in $X$ and $Y$ and flux
+  normalization) are allowed to vary in the fit.  Note that we do {\em
+    not} allow the local sky to be fitted as a free parameters.  Since
+  we have subtracted a model for the background, allowing the sky to
+  be again at this stage is redundant.  In fact, in our testing, we
+  found that allowing the sky to float resulted in higher scatter for
+  the flux normalizations.  For the non-linear fitting,
+\ippprog{psphot} again uses the Levenberg-Marquardt technique.}  The
+sources are fitted in their S/N order, starting with the brightest and
+working down to the user-specified limit, with the other sources
+subtracted as discussed above.  All sources for which a non-linear PSF
+model has been attempted have the flag bit
+\code{PM_SOURCE_MODE_FITTED} set, regardless of the quality of that
+fit.
 
 Since the PSF model describes the variation of the PSF across the
@@ -2121,22 +2176,4 @@
 Section~\ref{sec:moments}).  For the PV3 $3\pi$ analysis, the PSF fit
 window radius is $7 \times \sigma_w$.
-
-Sources which are blended with other sources may be fitted together as a
-set of PSFs.  Blended objects are identified by first searching for
-objects for which the PSF fit windows overlap.  For a group of such
-neighboring objects, a contour is determined in the flux image at
-$25\%$ of the peak of the brightest source in the group.  All objects
-lying within this contour are treated as blends of this brightest
-source.  If other objects in this group exist, the brightest object
-not already assigned to a blend is used to define the contour for
-blends of this next object.  All objects in the image are tested as
-possible blends.  A single multi-source fit is performed on each group
-of blended peaks.  Sources which are identified as members of a
-blended group have the flag bit \code{PM_SOURCE_MODE_BLEND} set, while
-those for which a blended PSF fit succeeds have the flag bit
-\code{PM_SOURCE_MODE_BLEND_FIT} set.  {\em Note that for DR1 \& DR2,
-  this option was not used because it tended to prevent galaxies from
-  being fitted as extended objects; the rules for identifying blended
-  stars and galaxies will be revisited in future re-analyses.}
 
 %% Once a solution has been achieved for a source, \ippprog{psphot} attempts to
@@ -2226,45 +2263,72 @@
 represented and may have larger residual significance.
 
-For sources in groups of blended stars, the resulting fits are
-evaluated independently.  Any which are determined to be valid PSF
-fits are subtracted from the image and kept for future analysis.
-
-\subsubsection{Double and Extended Sources}
-
-Sources which are judged to be non-PSF-like are confronted with two
-possible alternative choices.  First, the source is fitted with a
-double-source model.  In this pass, the assumption is made that there
-are two neighboring sources, but the peaks are not resolved.  The
-initial guess for the two peaks is made by splitting the flux of the
-single source in half and locating the two starting peaks at +/- 2
-pixels from the original peak along the direction of the semi-major
-axis of the sources, as measured from the second moments.  In order
-for the two-source model to be accepted, both sources must be judged
-as a valid PSF source.  Otherwise, the double-PSF model is rejected
-and the source is fitted with the available non-PSF model or models.
-Sources for which a double-PSF model is fitted have the flag bit
-\code{PM_SOURCE_MODE_PAIR} set.
+\subsubsection{Double and Blended Sources}
+
+\textmod{In fields with high stellar density, the non-linear source fitting can
+be adversely affected by close neighbors.  We implemented two
+modifications of the non-linear fitting code to address this issue for
+different scales to the nearby neighbors.  One version addresses the
+case of nearby sources which are separately detected in the
+peak-detection stage; the other version of the analysis attempts to
+fit a pair of PSFs for sources which are apparently extended.  {\em
+  Note that for DR1 \& DR2, neither of these options were used because
+  they tended to prevent galaxies from being fitted as extended
+  objects; these rules for distinguishing blended stars and galaxies will
+  be revisited in future re-analyses.}  We outline the strategy below
+although it was not used for these data releases.}
+
+{\TEXTADD Sources which are blended with other sources may be fitted together as
+a set of PSFs.  Blended objects are identified by first searching for
+objects for which the PSF fit windows overlap.  For a group of such
+neighboring objects, a contour is determined in the flux image at
+$25\%$ of the peak of the brightest source in the group.  All objects
+lying within this contour are treated as blends of this brightest
+source.  If other objects in this group exist, the brightest object
+not already assigned to a blend is used to define the contour for
+blends of this next object.  All objects in the image are tested as
+possible blends.  A single multi-source fit is performed on each group
+of blended peaks.  Sources which are identified as members of a
+blended group have the flag bit \code{PM_SOURCE_MODE_BLEND} set, while
+those for which a blended PSF fit succeeds have the flag bit
+\code{PM_SOURCE_MODE_BLEND_FIT} set.  For sources in groups of blended
+stars, the resulting fits are evaluated independently.  Any which are
+determined to be valid PSF fits are subtracted from the image and kept
+for future analysis.}
+
+{\TEXTADD Sources which are judged to be non-PSF-like are confronted with two
+possible alternative choices: double-star or extended source model
+(see next section).  For the double-star model, the assumption is made
+that there are two neighboring PSF-like sources, but the peaks are not
+resolved.  The initial guess for the two peaks is made by splitting
+the flux of the single source in half and locating the two starting
+peaks at +/- 2 pixels from the original peak along the direction of
+the semi-major axis of the sources, as measured from the second
+moments.  In order for the two-source model to be accepted, both
+sources must be judged as a valid PSF source.  Otherwise, the
+double-PSF model is rejected and the source is fitted with the
+available non-PSF model or models.  Sources for which a double-PSF
+model is fitted have the flag bit \code{PM_SOURCE_MODE_PAIR} set. }
 
 \subsubsection{Non-PSF Sources}
 \label{sec:nonlinear.galaxy.model}
 
-Once every source (above the S/N cutoff) has been confronted with the
+\textmod{Once every source (above the S/N cutoff) has been confronted with the
 PSF model, the sources which are thought to be extended (resolved) can
 now be fit with an appropriate model (e.g., galaxy profile or other
-likely extended shapes).  Again, the fitting stage starts with the
+likely extended shapes).}  Again, the fitting stage starts with the
 brightest sources (as judged by the rough S/N measured from the
 moments aperture) and working to a user defined S/N limit.
 
-\ippprog{psphot} will use the user-selected extended source model to
-attempt these fits.  In the configuration system, the keyword
-\code{EXT_MODEL} is set to the model of interest.  All suspected
-extended sources are fitted with the model, allowing all of the
-parameters to float.  The initial parameter guesses are critical here
-to achieving convergence on the model fits in a reasonable time.  The
-moments and the pixel flux distribution are used to make the initial
-parameter guess.  Many of the source parameters can be accurately
-guessed from the first and second moments.  The power-law slope can be
-guessed by measuring the isophotal level at two elliptical radii and
-comparing the ratio to that expected.
+{\TEXTADD The choice of extended source model or models is set by the user for a given
+analysis.  In the configuration system, the keyword \code{EXT_MODEL}
+is set to the model of interest.}  All suspected extended sources are
+fitted with the model, allowing all of the parameters to float.  The
+initial parameter guesses are critical here to achieving convergence
+on the model fits in a reasonable time.  The moments and the pixel
+flux distribution are used to make the initial parameter guess.  Many
+of the source parameters can be accurately guessed from the first and
+second moments.  The power-law slope can be guessed by measuring the
+isophotal level at two elliptical radii and comparing the ratio to
+that expected.
 
 For each type of extended source model (in fact for all source
@@ -2303,39 +2367,4 @@
 \subsection{Faint Source Analysis}
 \label{sec:faint.psf.model}
-
-% pueo:/home/real/eugene/ppsim.20200407
-\begin{figure}[htbp]
-  \begin{center}
- \includegraphics[width=\hsize,clip]{\picdir/{completion.ppsim}.pdf}
-  \caption{\label{fig:complete.ppsim} Completeness as a function of
-    magnitude (blue curves) for different stellar densities in
-    simulated data.  The curves are labeled with the logarithm of the
-    stellar density at the detection threshold of the low-density
-    image.  The dotted red line shows the detection limit expected for
-    the sky level and seeing.  The solid red curve shows the
-    completeness estimated for the low-density image based on
-    injection and recovery.}
-  \end{center}
-\end{figure}
-
-% pueo:/home/real/eugene/ppsim.20200407
-\begin{figure}[htbp]
-  \begin{center}
- \includegraphics[width=\hsize,clip]{\picdir/{psphot.complete.pv3}.pdf}
-  \caption{\label{fig:complete.pv3} Completeness and bogus fraction
-    as a function of magnitude for different stellar densities in real
-    PS1 exposures.  Each panel represents an exposure at different
-    Galactic latitudes towards anti-center, labeled by the density of
-    stars at the detection limit of the low-density exposure.  In each
-    panel, the completeness (compared to deep stack data) and fraction
-    of false detections (bogus fraction) is shown for a series of
-    cuts.  The gold curves show all detections in the exposures.  The
-    dotted black curve shows the impact of cutting detections
-    identified by {\tt psphot} as cosmic rays.  The blue curve
-    excludes cosmic rays and detections with {\tt PSF\_QF} $< 0.95$
-    while the red curve excludes cosmic rays and detections with {\tt
-      PSF\_QF\_PERFECT} $< 0.95$.}
-  \end{center}
-\end{figure}
 
 After a first pass through the image, in which the brighter sources
@@ -2437,5 +2466,220 @@
 actual source flux.
 
-% /data/kukui.1/eugene/psphot.examples.20190423/compare.sh
+Aperture photometry attempts to avoid these problems, but introduces
+other difficulties.  In aperture photometry, if a large enough
+aperture is chosen, the amount of flux which is lost will be a small
+fraction of the total source flux.  Even more importantly, as the
+image conditions change, the amount lost will change by an even
+smaller fraction, at least for a large aperture.  
+%
+% This can be seen by
+% the fact that the dominant variations in the image quality are in the
+% focus, tracking and seeing.  All of these errors initially affect the
+% cores of the stellar images, rather than the wide wings.  The wide
+% wings are largely dominated by scattering in the optics and scattering
+% in the atmosphere.  The amplitude and distribution of these two
+% scattering functions do not change significantly or quickly for a
+% single telescope and site.  
+%
+Aperture photometry can then be used to
+correct the PSF photometry.
+
+The difficulty for aperture photometry is the need to make an accurate
+measurement of the local background for each source.  As the aperture
+grows, errors in the measurement of the sky flux start to become
+dominant.  If the aperture is too small, then variations in the image
+quality are dominant.  The brighter is the source, the smaller is the
+error introduced by the large size of the aperture.  However, the
+number of very bright stars is limited in any image, and of course the
+brighter stars are more likely to suffer from non-linearity or
+saturation.  
+
+In order to thread the needle between these effects, \ippprog{psphot}
+measures the aperture photometry on a modest-sized aperture, and then
+uses the PSF model to extrapolate to a large aperture.  When the PSF
+fluxes are calculated, the aperture flux for the modest-sized aperture
+is also determined.  The aperture is a circular aperture with radius
+set to a fixed multiple (\code{PSF_APERTURE_SCALE}) of $\sigma_w$, the
+width of the Gaussian window function determined based on the analysis
+of the second moments (see Section~\ref{sec:moments}).  For the PV3
+$3\pi$ analysis, the aperture window radius is $4.5 \times \sigma_w$,
+while the large reference aperture radius is set to 25 pixels
+(\code{PSF_REF_RADIUS} = 6\farcs4).  These corrected aperture
+magnitudes are saved in the output catalogs as \code{AP_MAG}, the
+uncorrected aperture magnitudes are saved as \code{AP_MAG_RAW}, and
+the radius used to measure the raw aperture flux is saved as
+\code{AP_MAG_RADIUS}.  The corresponding flux and the flux error are
+saved as \code{AP_FLUX} and \code{AP_FLUX_SIG}.
+
+% Figure 6:  ** repaired PDF text **
+% pueo:/home/real/eugene/ppsim.20200407/tap_psphot_deteff.pro : all.complete
+\begin{figure}[htbp]
+  \begin{center}
+ \includegraphics[width=\hsize,clip]{\picdir/{completion.ppsim}.pdf}
+  \caption{\label{fig:complete.ppsim} Completeness as a function of
+    magnitude (blue curves) for different stellar densities in
+    simulated data.  The curves are labeled with the logarithm of the
+    stellar density at the detection threshold of the low-density
+    image.  The dotted red line shows the detection limit expected for
+    the sky level and seeing.  The solid red curve shows the
+    completeness estimated for the low-density image based on
+    injection and recovery.}
+  \end{center}
+\end{figure}
+
+With these aperture magnitudes in hand, it is now possible to make an
+average correction to the PSF magnitudes to bring the PSF and aperture
+magnitudes to the same system.  This correction is measured using the
+same stars from which the PSF model is measured, as long as the PSF
+magnitude error for the star is less than 0.03 mag.  The correction is
+calculated using the weighted average of the values $m_{\rm AP} -
+m_{\rm PSF}$.  Since the PSF may vary across the image, the correction
+is determined as a function of position in the image.  Like the PSF
+model, the 2D variations of the aperture correction may be modeled as
+a polynomial or via interpolation in a grid.  For the $3\pi$ PV3
+analysis, a grid with a maximum of $6\times 6$ samples per GPC1 chip
+image was used.  The reported PSF magnitudes for all objects have this
+aperture correction applied.  \textadd{Note that an initial aperture correction was
+measured during the initial steps of the analysis before the PSF model
+was chosen.  However, since the sources in the image were not yet
+measured and subtracted, that aperture could be contaminated by
+neighbors.  The analysis here is performed one fairly bright star at a
+time with all other sources subtracted in order to minimize such contamination.}
+
+% growth curve analysis in psphot:
+% in psphotChoosePSF : call psphotMakeGrowthCurve
+% in psphotMakeGrowthCurve : boolean GROWTH_FROM_SOURCES, use
+%% pmGrowthCurveGenerateFromSources or
+%% pmGrowthCurveGenerate (uses PSF model only)
+%% GROWTH_FROM_SOURCES is set to TRUE for default recipe
+
+%% ApTrend:
+%% in psphotApResid, called by psphotReadout near the end of the
+%% analysis
+%% ApTrend = f(x,y) for (apMag - psfMag) for psfMagErr <= 0.03
+%% apMag is growth curve corrected
+%% psfMag is raw
+
+%% raw psfMag and raw apMag are measured
+%% apMag = apMagRaw + growth curve correction (from apRadius to 25 pix
+%% = PSF_REF_RADIUS)
+%% psfMag = psfMagRaw + aperture trend (<ap - psf> + growth curve)
+
+% How important is this effect?  Consider a typical bright source with a
+% flux of (say) 40,000 counts in an image of background 1000 counts per
+% pixel, with FWHM of 4 pixels.  In principle, the flux of this source
+% should be measurable with an accuracy of roughly 0.57\%
+% ($\frac{\sqrt{40000 + 1000 \times 12}}{40000}$).  However, the
+% measurement of the sky is limited at some finite level by Poisson
+% statistics.  If we are required to use an aperture of (say) 25 pixels
+% in radius (eg, 5 arcseconds for an 0.2 arcsec / pixel detector), and
+% we have an annulus of twice this radius to measure the local sky, then
+% we will have an error of XXX.
+% 
+% \note{outline the variation of {\em ApResid} as a function of
+% magnitude}.
+
+%%% \ippprog{psphot} measures the aperture correction ({\em ApResid}) for every PSF
+%%% candidate source, then calculates the trend of this correction as a
+%%% function of the magnitude.  This trend is fitted with a line.  The
+%%% resulting function can be used to determine the effective aperture
+%%% correction for an infinite flux source and the average bias inherent
+%%% in the sky measurement for the image.  The scatter of the
+%%% PSF-candidate source measurements about this trend is a measure of how
+%%% well we can measure photometry from the image by applying the specific
+%%% PSF model.  The slope of this trend is a measure of the bias in the
+%%% local sky measurment for each source.  In principal, the measured sky
+%%% levels could be modified by this bias.  More generally, the measured
+%%% bias in a collection of images could be used to improve the model
+%%% fitting or sky fitting portion of the software the remove the bias
+%%% term.
+
+\subsection{Completeness \& Contamination}
+
+{\TEXTADD At the end of the \ippprog{psphot} analysis of the sources in the
+image, an analysis is performed to test the detection efficiency.  A
+number of fake PSF sources are injected into the image and the peak
+detection analysis (Section~\ref{sec:peaks}) is use to determine if
+these sources would have been recovered.  The PSF model fluxes are
+measured for the source which are detected.  For a given image, the
+detection threshold is predicted based on the median image variance
+and the seeing.  A series of brightness bins straddling the threshold
+are defined and a number of sources are injected with magnitudes
+corresponding to each of these bin values.  The \ippprog{psphot}
+recipe value \code{EFF.NUM} specifies the number of sources in each
+brightness bin (500 the PV3), and the value \code{@EFF.MAG} specifies
+the bins as magnitudes above and below the threshold.  For PV3, the 13
+magnitude offsets were (-2.0, -1.0, -0.5, -0.25, -0.1, -0.05, 0.0,
+0.05, 0.1, 0.25, 0.5, 1.0, 2.0), providing fine sampling near the
+limit, but more coarse coverage further away.  Poisson noise
+appropriate to the photon counts of the injected sources are included
+in the image.  Injected sources are uniformly distributed across the
+image in $X$ and $Y$ pixel coordinates {\em without any consideration
+  of the masked regions}.  This last point means the recovered
+fraction in the bright bins can be used to test the masking fraction.}
+
+{\TEXTADD As the stellar density increases, the completeness suffers due to
+crowding and confusion.  Since the injection and recovery analysis of
+the fake sources operates on the source-subtracted image and does not
+attempt to fully discovery the sources, this analysis over-estimates
+the completeness in crowded fields.  To explore the completeness in
+crowded field images, we generate a series of simulated images using a
+Gaussian PSF with FWHM = 1\arcsec\ for a range of stellar densities.
+We generate fake stars with fluxes as faint as $\frac{1}{5}$ of the
+flux as the low-density detection limit, with densities ranging from
+\approx 14,000 stars per square degree at low-density detection limit
+to \approx 4.8 million stars per square degree at the low-density
+detection limit.  The latter is comparable to observed densities in
+the Galactic plane.  We run the \ippprog{psphot} analysis on these
+simulated images and compare the detected stars to those injected to
+calculate the completeness for each image as a function of the true
+magnitude of the stars.  Figure~\ref{fig:complete.ppsim} shows the measured
+completeness for each of the six simulated images, labeled by the
+logarithm of their faint-end stellar density. The red dashed line
+shows the expected detection limit based on the background and seeing,
+while the red curve shows the completeness curve calculated
+automatically by \ippprog{psphot} using the injection and recovery
+analysis.}
+
+{\TEXTADD For low-density fields, the completeness function determined by
+injection and recovery is similar to that measured by the simulation,
+with the 50\% completeness threshold roughly 0.3 magnitudes too faint.
+As the stellar density increases, the true 50\% completeness magnitude
+rises relative to the value estimated by injection and recovery.}
+
+{\TEXTADD Ideally, all sources detected by \ippprog{psphot} would correspond to
+real astrophysical objects.  In reality, many sources are detected in
+the images which do not correspond to real sources in the sky.  In the
+very simplified simulations discussed above, which do not include
+realistic detector artifacts, we find that the fraction of bogus
+detections is extremely low, even at the very faint end.  In real
+data, bogus detections are due to a variety of typical instrumental
+features including cosmic rays, diffraction spikes, satelite tracks,
+glows, non-Gaussian noise, variance mis-estimation, etc.  See paper III
+for extensive discussion of instrumental artifacts in the Pan-STARRS images.}
+
+% Figure 7: ** repaired PDF text **
+% pueo:/home/real/eugene/psphot.complete.20200407/complete.sh : full.figure.all
+\begin{figure}[htbp]
+  \begin{center}
+ \includegraphics[width=\hsize,clip]{\picdir/{psphot.complete.pv3}.pdf}
+  \caption{\label{fig:complete.pv3} Completeness and bogus fraction
+    as a function of magnitude for different stellar densities in real
+    PS1 exposures.  Each panel represents an exposure at different
+    Galactic latitudes towards anti-center, labeled by the density of
+    stars at the detection limit of the low-density exposure.  In each
+    panel, the completeness (compared to deep stack data) and fraction
+    of false detections (bogus fraction) is shown for a series of
+    cuts.  The gold curves show all detections in the exposures.  The
+    dotted black curve shows the impact of cutting detections
+    identified by {\tt psphot} as cosmic rays.  The blue curve
+    excludes cosmic rays and detections with {\tt PSF\_QF} $< 0.95$
+    while the red curve excludes cosmic rays and detections with {\tt
+      PSF\_QF\_PERFECT} $< 0.95$.}
+  \end{center}
+\end{figure}
+
+% Figure 8:  ** repaired PDF text **
+% /data/kukui.1/eugene/psphot.examples.20190423/compare.sh : mkfigure
 \begin{figure*}[htbp]
   \begin{center}
@@ -2460,241 +2704,5 @@
 \end{figure*}
 
-% /data/kukui.1/eugene/psphot.examples.20190423/compare.sh
-\begin{figure*}[htbp]
-  \begin{center}
- \includegraphics[width=\hsize,clip]{\picdir/{mag.resid.aper.v1}.\plotext}
-  \caption{\label{fig:mag.resid.aper} Aperture Photometry
-    demonstration.  The plots show identical measurements to those in
-    Figure~\ref{fig:mag.resid.psf}, but for aperture photometry, as discussed in
-    Section~\ref{sec:aperture.correction}, rather than PSF photometry.}
-  \end{center}
-\end{figure*}
-
-% on pueo ~eugene
-% /data/kukui.1/eugene/psphot.examples.20190423/compare.sh
-\begin{figure}[htbp]
-  \begin{center}
- \includegraphics[width=\hsize,clip]{\picdir/{bright.mag.resid}.\plotext}
-  \caption{\label{fig:mag.resid.stdevs} Demonstration of photometric
-    accuracy using the image sequence from
-    Figure~\ref{fig:mag.resid.psf}. Using only bright stars (7 - 8
-    magnitudes above the detection threshold), we calculate the
-    difference between the magnitudes in the first image and the other
-    17 images.  The plotted dots show for each pair the mean
-    difference vs the standard deviation of the difference.  Red dots
-    show the PSF magnitudes and blue dots show aperture
-    magnitudes. Despite real transparency variations of 0.4 over the
-    50 minutes of this sequence, magnitudes are consistent at the few
-    millimagnitude level.  Aperture magnitudes have scatter in
-    the 2 - 7 millimagnitude range, while the PSF magnitudes have
-    scatter of 7 - 14 millimagntiudes.  
-}
-\end{center}
-\end{figure}
-
-% on pueo ~eugene/zpts.20200406/mana.sh
-\begin{figure*}[htbp]
-  \begin{center}
- \includegraphics[width=\hsize,clip]{\picdir/{zpt.mjd.v0.i}.\plotext}
-  \caption{\label{fig:zpt.iband} Historical \ips-band zero points.
-    Blue dots are the individual exposure zero points, corrected to
-    airmass at the zenith.  Red dots are the median of zero points
-    from images groups in bins of 10 nights.  The grey line is a
-    spline fit to these median values.  }
-\end{center}
-\end{figure*}
-
-% on pueo ~eugene/zpts.20200406/mana.sh
-\begin{figure}[htbp]
-  \begin{center}
- \includegraphics[width=\hsize,clip]{\picdir/{zptres.hist.v0.i}.\plotext}
-  \caption{\label{fig:zpt.resid.hist} Historical \ips-band zero-point
-    residual variations.  Log-histogram (black line) of the
-    per-exposure zero points, corrected to the zenith, after
-    subtracting a spline fit to the median of image groups in bins of
-    10 nights.  The inset shows the core of the distribution.  In
-    both, the red line is a Gaussian fit to the distribution.  The
-    large negative tails are due to clouds and haze.  }
-\end{center}
-\end{figure}
-
-Aperture photometry attempts to avoid these problems, but introduces
-other difficulties.  In aperture photometry, if a large enough
-aperture is chosen, the amount of flux which is lost will be a small
-fraction of the total source flux.  Even more importantly, as the
-image conditions change, the amount lost will change by an even
-smaller fraction, at least for a large aperture.  
-%
-% This can be seen by
-% the fact that the dominant variations in the image quality are in the
-% focus, tracking and seeing.  All of these errors initially affect the
-% cores of the stellar images, rather than the wide wings.  The wide
-% wings are largely dominated by scattering in the optics and scattering
-% in the atmosphere.  The amplitude and distribution of these two
-% scattering functions do not change significantly or quickly for a
-% single telescope and site.  
-%
-Aperture photometry can then be used to
-correct the PSF photometry.
-
-The difficulty for aperture photometry is the need to make an accurate
-measurement of the local background for each source.  As the aperture
-grows, errors in the measurement of the sky flux start to become
-dominant.  If the aperture is too small, then variations in the image
-quality are dominant.  The brighter is the source, the smaller is the
-error introduced by the large size of the aperture.  However, the
-number of very bright stars is limited in any image, and of course the
-brighter stars are more likely to suffer from non-linearity or
-saturation.  
-
-In order to thread the needle between these effects, \ippprog{psphot}
-measures the aperture photometry on a modest-sized aperture, and then
-uses the PSF model to extrapolate to a large aperture.  When the PSF
-fluxes are calculated, the aperture flux for the modest-sized aperture
-is also determined.  The aperture is a circular aperture with radius
-set to a fixed multiple (\code{PSF_APERTURE_SCALE}) of $\sigma_w$, the
-width of the Gaussian window function determined based on the analysis
-of the second moments (see Section~\ref{sec:moments}).  For the PV3
-$3\pi$ analysis, the aperture window radius is $4.5 \times \sigma_w$,
-while the large reference aperture radius is set to 25 pixels
-(\code{PSF_REF_RADIUS} = 6\farcs4).  These corrected aperture
-magnitudes are saved in the output catalogs as \code{AP_MAG}, the
-uncorrected aperture magnitudes are saved as \code{AP_MAG_RAW}, and
-the radius used to measure the raw aperture flux is saved as
-\code{AP_MAG_RADIUS}.  The corresponding flux and the flux error are
-saved as \code{AP_FLUX} and \code{AP_FLUX_SIG}.
-
-With these aperture magnitudes in hand, it is now possible to make an
-average correction to the PSF magnitudes to bring the PSF and aperture
-magnitudes to the same system.  This correction is measured using the
-same stars from which the PSF model is measured, as long as the PSF
-magnitude error for the star is less than 0.03 mag.  The correction is
-calculated using the weighted average of the values $m_{\rm AP} -
-m_{\rm PSF}$.  Since the PSF may vary across the image, the correction
-is determined as a function of position in the image.  Like the PSF
-model, the 2D variations of the aperture correction may be modeled as
-a polynomial or via interpolation in a grid.  For the $3\pi$ PV3
-analysis, a grid with a maximum of $6\times 6$ samples per GPC1 chip
-image was used.  The reported PSF magnitudes for all objects have this
-aperture correction applied.  \textadd{Note that an initial aperture correction was
-measured during the initial steps of the analysis before the PSF model
-was chosen.  However, since the sources in the image were not yet
-measured and subtracted, that aperture could be contaminated by
-neighbors.  The analysis here is performed one fairly bright star at a
-time with all other sources subtracted in order to minimize such contamination.}
-
-% growth curve analysis in psphot:
-% in psphotChoosePSF : call psphotMakeGrowthCurve
-% in psphotMakeGrowthCurve : boolean GROWTH_FROM_SOURCES, use
-%% pmGrowthCurveGenerateFromSources or
-%% pmGrowthCurveGenerate (uses PSF model only)
-%% GROWTH_FROM_SOURCES is set to TRUE for default recipe
-
-%% ApTrend:
-%% in psphotApResid, called by psphotReadout near the end of the
-%% analysis
-%% ApTrend = f(x,y) for (apMag - psfMag) for psfMagErr <= 0.03
-%% apMag is growth curve corrected
-%% psfMag is raw
-
-%% raw psfMag and raw apMag are measured
-%% apMag = apMagRaw + growth curve correction (from apRadius to 25 pix
-%% = PSF_REF_RADIUS)
-%% psfMag = psfMagRaw + aperture trend (<ap - psf> + growth curve)
-
-% How important is this effect?  Consider a typical bright source with a
-% flux of (say) 40,000 counts in an image of background 1000 counts per
-% pixel, with FWHM of 4 pixels.  In principle, the flux of this source
-% should be measurable with an accuracy of roughly 0.57\%
-% ($\frac{\sqrt{40000 + 1000 \times 12}}{40000}$).  However, the
-% measurement of the sky is limited at some finite level by Poisson
-% statistics.  If we are required to use an aperture of (say) 25 pixels
-% in radius (eg, 5 arcseconds for an 0.2 arcsec / pixel detector), and
-% we have an annulus of twice this radius to measure the local sky, then
-% we will have an error of XXX.
-% 
-% \note{outline the variation of {\em ApResid} as a function of
-% magnitude}.
-
-%%% \ippprog{psphot} measures the aperture correction ({\em ApResid}) for every PSF
-%%% candidate source, then calculates the trend of this correction as a
-%%% function of the magnitude.  This trend is fitted with a line.  The
-%%% resulting function can be used to determine the effective aperture
-%%% correction for an infinite flux source and the average bias inherent
-%%% in the sky measurement for the image.  The scatter of the
-%%% PSF-candidate source measurements about this trend is a measure of how
-%%% well we can measure photometry from the image by applying the specific
-%%% PSF model.  The slope of this trend is a measure of the bias in the
-%%% local sky measurment for each source.  In principal, the measured sky
-%%% levels could be modified by this bias.  More generally, the measured
-%%% bias in a collection of images could be used to improve the model
-%%% fitting or sky fitting portion of the software the remove the bias
-%%% term.
-
-\subsection{Completeness \& Contamination}
-
-At the end of the \ippprog{psphot} analysis of the sources in the
-image, an analysis is performed to test the detection efficiency.  A
-number of fake PSF sources are injected into the image and the peak
-detection analysis (Section~\ref{sec:peaks}) is use to determine if
-these sources would have been recovered.  The PSF model fluxes are
-measured for the source which are detected.  For a given image, the
-detection threshold is predicted based on the median image variance
-and the seeing.  A series of brightness bins straddling the threshold
-are defined and a number of sources are injected with magnitudes
-corresponding to each of these bin values.  The \ippprog{psphot}
-recipe value \code{EFF.NUM} specifies the number of sources in each
-brightness bin (500 the PV3), and the value \code{@EFF.MAG} specifies
-the bins as magnitudes above and below the threshold.  For PV3, the 13
-magnitude offsets were (-2.0, -1.0, -0.5, -0.25, -0.1, -0.05, 0.0,
-0.05, 0.1, 0.25, 0.5, 1.0, 2.0), providing fine sampling near the
-limit, but more coarse coverage further away.  Poisson noise
-appropriate to the photon counts of the injected sources are included
-in the image.  Injected sources are uniformly distributed across the
-image in $X$ and $Y$ pixel coordinates {\em without any consideration
-  of the masked regions}.  This last point means the recovered
-fraction in the bright bins can be used to test the masking fraction.
-
-As the stellar density increases, the completeness suffers due to
-crowding and confusion.  Since the injection and recovery analysis of
-the fake sources operates on the source-subtracted image and does not
-attempt to fully discovery the sources, this analysis over-estimates
-the completeness in crowded fields.  To explore the completeness in
-crowded field images, we generate a series of simulated images using a
-Gaussian PSF with FWHM = 1\arcsec for a range of stellar densities.
-We generate fake stars with fluxes as faint as $\frac{1}{5}$ of the
-flux as the low-density detection limit, with densities ranging from
-\approx 14,000 stars per square degree at low-density detection limit
-to \approx 4.8 million stars per square degree at the low-density
-detection limit.  The latter is comparable to observed densities in
-the Galactic plane.  We run the \ippprog{psphot} analysis on these
-simulated images and compare the detected stars to those injected to
-calculate the completeness for each image as a function of the true
-magnitude of the stars.  Figure~\ref{fig:complete.ppsim} shows the measured
-completeness for each of the six simulated images, labeled by the
-logarithm of their faint-end stellar density. The red dashed line
-shows the expected detection limit based on the background and seeing,
-while the red curve shows the completeness curve calculated
-automatically by \ippprog{psphot} using the injection and recovery
-analysis.
-
-For low-density fields, the completeness function determined by
-injection and recovery is similar to that measured by the simulation,
-with the 50\% completeness threshold roughly 0.3 magnitudes too faint.
-As the stellar density increases, the true 50\% completeness magnitude
-rises relative to the value estimated by injection and recovery.
-
-Ideally, all sources detected by \ippprog{psphot} would correspond to
-real astrophysical objects.  In reality, many sources are detected in
-the images which do not correspond to real sources in the sky.  In the
-very simplified simulations discussed above, which do not include
-realistic detector artifacts, we find that the fraction of bogus
-detections is extremely low, even at the very faint end.  In real
-data, bogus detections are due to a variety of typical instrumental
-features including cosmic rays, diffraction spikes, satelite tracks,
-glows, non-Gaussian noise, variance mis-estimation, etc.  See paper III
-for extensive discussion of instrumental artifacts in the Pan-STARRS images.
-
-Figure~\ref{fig:complete.pv3} illustrates the completeness and bogus
+{\TEXTADD Figure~\ref{fig:complete.pv3} illustrates the completeness and bogus
 detection fraction for a set of 4 real PS1 exposures from the $3\pi$
 Survey.  This figure uses \ips-band exposures with Galactic longitude
@@ -2717,7 +2725,19 @@
 also exclude  detections with \ippmisc{PSF_QF_PERFECT} less than
 0.95.  This cut removes detections on residual persistent glows and
-diffraction spikes.
-
-For the exposures at high-Galactic latitude, with a relatively low
+diffraction spikes.}
+
+% Figure 9: ** repaired PDF text **
+% /data/kukui.1/eugene/psphot.examples.20190423/compare.sh
+\begin{figure*}[htbp]
+  \begin{center}
+ \includegraphics[width=\hsize,clip]{\picdir/{mag.resid.aper.v1}.\plotext}
+  \caption{\label{fig:mag.resid.aper} Aperture Photometry
+    demonstration.  The plots show identical measurements to those in
+    Figure~\ref{fig:mag.resid.psf}, but for aperture photometry, as discussed in
+    Section~\ref{sec:aperture.correction}, rather than PSF photometry.}
+  \end{center}
+\end{figure*}
+
+{\TEXTADD For the exposures at high-Galactic latitude, with a relatively low
 density of sources, the cosmic rays represent a significant
 contamination, as seen in the excess of bogus sources with \ips-band
@@ -2730,5 +2750,5 @@
 because the chance of having a source lie on the diffraction spikes or
 persistence glows is greatly increased at higher stellar densities.
-The impact of the crowding on the completeness is also clear in this dataset.
+The impact of the crowding on the completeness is also clear in this dataset.}
 
 \subsection{Stellar Photometry Example}
@@ -2773,4 +2793,27 @@
 the reported photometry for both PSF and aperture magnitudes.
 
+% Figure 10: ** repaired PDF text **
+% on pueo ~eugene
+% /data/kukui.1/eugene/psphot.examples.20190423/compare.sh : figure.resids
+\begin{figure}[t]
+  \begin{center}
+ \includegraphics[width=\hsize,clip]{\picdir/{bright.mag.resid}.\plotext}
+  \caption{\label{fig:mag.resid.stdevs} Demonstration of photometric
+    accuracy using the image sequence from
+    Figure~\ref{fig:mag.resid.psf}. Using only bright stars (7 - 8
+    magnitudes above the detection threshold), we calculate the
+    difference between the magnitudes in the first image and the other
+    17 images.  The plotted dots show for each pair the mean
+    difference vs the standard deviation of the difference.  Red dots
+    show the PSF magnitudes and blue dots show aperture
+    magnitudes. Despite real transparency variations of 0.4 over the
+    50 minutes of this sequence, magnitudes are consistent at the few
+    millimagnitude level.  Aperture magnitudes have scatter in
+    the 2 - 7 millimagnitude range, while the PSF magnitudes have
+    scatter of 7 - 14 millimagntiudes.  
+}
+\end{center}
+\end{figure}
+
 We believe the observed behavior at the faint end is primarily a
 result of the increased crowding.  Aperture photometry is more
@@ -2779,4 +2822,17 @@
 with the aperture photometry degrading rapidly as the flux of the star
 decreases.  
+
+% Figure 11: ** repaired PDF text **
+% on pueo ~eugene/zpts.20200406/mana.sh : go.zpt.stats i
+\begin{figure*}[tb]
+  \begin{center}
+ \includegraphics[width=\hsize,clip]{\picdir/{zpt.mjd.v0.i}.\plotext}
+  \caption{\label{fig:zpt.iband} Historical \ips-band zero points.
+    Blue dots are the individual exposure zero points, corrected to
+    airmass at the zenith.  Red dots are the median of zero points
+    from images groups in bins of 10 nights.  The grey line is a
+    spline fit to these median values.  }
+\end{center}
+\end{figure*}
 
 {\TEXTADD The figures above show the relative photometric accuracy for
@@ -2832,7 +2888,7 @@
   of the \ips-band zero points after subtracting a smoothly varying
   spline fit to the median of groups of 10 nights.  A Gaussian fit to
-  this distribution has $\sigma = 28.4$ millimags.  If we
+  this distribution has $\sigma = 26.6$ millimags.  If we
   alternatively subtract a median zero point for each night, the
-  standard deviation is reduced to 18.9 millimags.  These values can be
+  standard deviation is reduced to 17.6 millimags.  These values can be
   compared to the results of \cite{2012ApJ...756..158S} in which only
   photometric nights were included, yielding a standard deviation of
@@ -2842,4 +2898,19 @@
   which are not expected from the normal effects of weather.  We
   believe these are largely due to aperture correction errors.}
+
+% Figure 12: ** repaired PDF text **
+% on pueo ~eugene/zpts.20200406/mana.sh
+\begin{figure}[b]
+  \begin{center}
+ \includegraphics[width=\hsize,clip]{\picdir/{zptres.hist.v0.i}.\plotext}
+  \caption{\label{fig:zpt.resid.hist} Historical \ips-band zero-point
+    residual variations.  Log-histogram (black line) of the
+    per-exposure zero points, corrected to the zenith, after
+    subtracting a spline fit to the median of image groups in bins of
+    10 nights.  The inset shows the core of the distribution.  In
+    both, the red line is a Gaussian fit to the distribution.  The
+    large negative tails are due to clouds and haze.  }
+\end{center}
+\end{figure}
 
 \subsection{Basic Analysis Summary}
@@ -2907,12 +2978,53 @@
 cut was defined by $|b| > b_{\rm min}$ where $b_{\rm min} = b_0 + r_b
 e^{\frac{-l^2}{2 \sigma_b^2}}$.  For the PV3 analysis, $b_0 =
-$20\degree, $r_b = $15\degree, $\sigma_b = $50\degree.  \textadd{The Galactic plane cut is made on an object-by-object basis.}  This contour
-avoids the denser portions of the Galactic plane and bulge, limiting
-the total time spent on the galaxy modeling analysis at the expense of
-galaxy photometry in the plane (though Kron photometry is available
-for those sources).  
-
+$20\degree, $r_b = $15\degree, $\sigma_b = $50\degree.  See
+Figure~\ref{fig:galplanecut} for an illustration of the cut used for PV3.  \textadd{The
+  Galactic plane cut is made on an object-by-object basis.}  This
+contour avoids the denser portions of the Galactic plane and bulge,
+limiting the total time spent on the galaxy modeling analysis at the
+expense of galaxy photometry in the plane (though Kron photometry is
+available for those sources).
+
+% galaxy model fits performed based on limits set in psphotChooseAnalysisOptions.c
+
+% petrosian analysis performed on same objects as galaxy model fits
+% if EXTENDED_SOURCE_PETROSIAN == TRUE (TRUE for PV3 stack - STACKPHOT)
+
+% galaxy model fits are performed on :
+% all if (PSPHOT.EXT.FIT.ALL.SOURCES == TRUE) (FALSE for PV3 stack)
+%   (even so, if density is higher than PSPHOT.EXT.FIT.ALL.THRESH, skip)
+
+% only extended sources (based on EXT.NSIGMA) if EXT.NSIGMA.LIMIT.USE
+% == TRUE (FALSE for PV3 stacks)
+
+% fit sources / measure petrosian to fixed flux limit if limits are
+% defined (they are for PV3)
+
+% mag limits by filter, e.g., : petro 25, extfit 21.5
+% are translated to flux in counts and compared to Kron flux
+
+% SN limit is used only if fixed flux limits are not set
+% SN limit set to EXTENDED_SOURCE_SN_LIM (10.0 for PV3)
+% S/N limit for Kron flux, 
+
+% S/N lim values set to 0.0 for all models in PV3
+
+% galaxy coordinate limits:
+% if |b| > 20.0 + 15.0 exp(-long^2 / (2 * 50^2))
+
+\subsection{Radial Profiles}
+\label{sec:radial.profile.v2}
+
+Galaxies with regular profiles, such as elliptical galaxies and
+regular spiral galaxies, may be described as primarily a radial
+surface brightness profile, with additional structure acting as a
+perturbation on that profile.  For many galaxies, the azimuthal shape
+at a given isophotal level may be described as an elliptical contour.
+To first order, a galaxy may be well described with a single elliptical
+contour and radial profile.  
+
+% Figure 13
 % uses plots.sh in this directory
-\begin{figure}[htbp]
+\begin{figure}[b]
  \begin{center}
  \includegraphics[width=\hsize,clip]{\picdir/galplanecut.pdf}
@@ -2923,41 +3035,17 @@
 \end{figure}
 
-% galaxy model fits performed based on limits set in psphotChooseAnalysisOptions.c
-
-% petrosian analysis performed on same objects as galaxy model fits
-% if EXTENDED_SOURCE_PETROSIAN == TRUE (TRUE for PV3 stack - STACKPHOT)
-
-% galaxy model fits are performed on :
-% all if (PSPHOT.EXT.FIT.ALL.SOURCES == TRUE) (FALSE for PV3 stack)
-%   (even so, if density is higher than PSPHOT.EXT.FIT.ALL.THRESH, skip)
-
-% only extended sources (based on EXT.NSIGMA) if EXT.NSIGMA.LIMIT.USE
-% == TRUE (FALSE for PV3 stacks)
-
-% fit sources / measure petrosian to fixed flux limit if limits are
-% defined (they are for PV3)
-
-% mag limits by filter, e.g., : petro 25, extfit 21.5
-% are translated to flux in counts and compared to Kron flux
-
-% SN limit is used only if fixed flux limits are not set
-% SN limit set to EXTENDED_SOURCE_SN_LIM (10.0 for PV3)
-% S/N limit for Kron flux, 
-
-% S/N lim values set to 0.0 for all models in PV3
-
-% galaxy coordinate limits:
-% if |b| > 20.0 + 15.0 exp(-long^2 / (2 * 50^2))
-
-\subsection{Radial Profiles}
-\label{sec:radial.profile.v2}
-
-Galaxies with regular profiles, such as elliptical galaxies and
-regular spiral galaxies, may be described as primarily a radial
-surface brightness profile, with additional structure acting as a
-perturbation on that profile.  For many galaxies, the azimuthal shape
-at a given isophotal level may be described as an elliptical contour.
-To first order, a galaxy may be well described with a single elliptical
-contour and radial profile.  
+% Figure 14  ** repaired PDF text **
+% on pueo ~eugene/sdss.psphot.2020414/mana.sh : go.figure
+\begin{figure*}[htbp]
+ \begin{center}
+ \includegraphics[width=\hsize,clip]{\picdir/{petrosians.mags}.pdf}
+  \caption{\label{fig:petrosians} Comparison of PS1 ({\tt psphot}) and
+    SDSS Petrosian parameters for objects identified as galaxies by
+    SDSS.  Panel (a) shows the difference in the measured Petrosian
+    magnitudes as a function of the Petrosian magnitude.  Panel (b)
+    shows the magnitude difference as a function of the measured
+    difference in the Petrosian radius.  }
+  \end{center}
+\end{figure*}
 
 In order to facilitate the Petrosian photometry analysis below, \ippprog{psphot}
@@ -3079,4 +3167,24 @@
 available from the PSPS \ippdbtable{StackPetrosian} table.}
 
+Our implementation of the Petrosian apertures and fluxes is designed
+to match the SDSS implementation \citep{2002AJ....123..485S} and
+therefore the measured parameters should be quite comparable between
+the two surveys.  Figure~\ref{fig:petrosians} compare the Petrosian
+magnitudes and radii as measured by \ippprog{psphot} on the $3\pi$
+Survey observations and the values measured by SDSS for the same
+objects.  Objects identified by SDSS as galaxies ({\tt probPSF\_r} $<
+0.5$) near the Galactic north pole ($\alpha$ = 180\degrees\ to
+190\degrees, $\delta$ = 25\degrees\ to 35\degrees) are selected from
+the PS1 $3\pi$ Survey dataset base on positional coincidence.  The
+figure shows the difference in the $r$-band Petrosian magnitudes as a
+function of the Petrosian magnitude and as a function of the
+difference in the measured Petrosian radii.  Differences in the
+measured magnitudes are driven by differences in the size estimates
+from the two datasets and analysis methods.  The PS1 analysis tends to
+find larger radii for the same objects than the SDSS analysis, with
+a mean difference of 0.3 arcseconds.  The larger aperture results in
+more flux captured in the aperture and thus brighter magnitudes for
+the same object: the mean difference is -0.23 magnitude in the sense
+of larger fluxes for the PS1 measurements.
 
 \subsection{Convolved Galaxy Model Fits}
@@ -3270,23 +3378,35 @@
 %% about the center of the pixel.  do this?
 
-In order to accurately compare the observed galaxy flux distribution
+\textmod{In order to accurately compare the observed galaxy flux distribution
 to a model, it is necessary to integrate the pixel flux for a given
-set of model parameter values.  This could be done in a numerical
-fashion, but in practice brute-force evaluation of the numerical
-integral is computationally very expensive, requiring many evaluations
-of the model function.  Within \ippprog{psphot}, we bypass this
-problem by defining a set of pre-calculated images for the central 9
-pixels (the $3 \times 3$ grid of pixels centered on the peak).  These
-pixel images are defined at higher resolution, with 11 subpixels per
-real CCD pixel.  The pre-calculated images are generated for a series
-of values for the following parameters: S\'ersic index, effective
-radius, axial ratio.  We then select the closest image to our specific
-case, and integrate over the true sub-pixels relevant for our position
-and model.  We have thus turned the problem from thousands of
-evaluations of the full galaxy model to \approx 100 straight
-additions, or up to $6 \times$ that number if we interpolate between
-any of the parameters.
-
-\note{how much error does this approximation introduce?}
+set of model parameter values.  In the \ippprog{psphot}
+implementation, we currently use a brute-force numerical evaluation of
+the integral, dividing the central pixel into a grid of subpixels,
+with the sampling set by the S\'ersic index of the model being
+evaluated as $N_{\rm sub} = 2 Integer(6n / R_{\rm min})$ where $N_{\rm sub}$
+is subpixel scale $n$ is the S\'ersic index and $R_{\rm min}$ is the
+size of the minor axis in pixel units.  The value of $N_{\rm sub}$ is
+constrained to be in the range 11 to 121, so the number of subpixels
+evaluations ranges from 121 to $121^2 = 14,641$.  Faster
+approximations to this analysis were explored but they resulted in
+unsatisfactory results.  This is definitely an area where
+\ippprog{psphot} could benefit from some of the lessons in the
+literature \citep[e.g.][]{2013PASP..125..719H}.}
+
+%% This could be done in a numerical
+%% fashion, but in practice brute-force evaluation of the numerical
+%% integral is computationally very expensive, requiring many evaluations
+%% of the model function.  Within \ippprog{psphot}, we bypass this
+%% problem by defining a set of pre-calculated images for the central 9
+%% pixels (the $3 \times 3$ grid of pixels centered on the peak).  These
+%% pixel images are defined at higher resolution, with 11 subpixels per
+%% real CCD pixel.  The pre-calculated images are generated for a series
+%% of values for the following parameters: S\'ersic index, effective
+%% radius, axial ratio.  We then select the closest image to our specific
+%% case, and integrate over the true sub-pixels relevant for our position
+%% and model.  We have thus turned the problem from thousands of
+%% evaluations of the full galaxy model to \approx 100 straight
+%% additions, or up to $6 \times$ that number if we interpolate between
+%% any of the parameters.
 
 The convolved galaxy model fit results are available in one of three
@@ -3294,5 +3414,4 @@
 \ippdbtable{StackModelFitDeV}, \ippdbtable{StackModelFitSer} for the
 Exponential, DeVaucouleur, and S\'ersic models, respectively.
-
 
 \subsection{Fixed Aperture Photometry}
@@ -3377,5 +3496,5 @@
  sets of measurements joined together for ease of access.}
 
-\note{test SDSS radial apertures?}
+% \note{test SDSS radial apertures?}
 
 % at least out to aperture # RADIAL_AP_MIN (= 4), but no further than
@@ -3451,5 +3570,7 @@
 earlier work were generally compact.
 
-% /data/kukui.1/eugene/galaxies.20190425/tap_psphot_galaxies.pro : go.bigtest.ckgalaxy
+% Figure 15: ** repaired PDF text **
+% was /data/kukui.1/eugene/galaxies.20190425/tap_psphot_galaxies.pro : go.bigtest.ckgalaxy
+% is pueo:galaxies.20190425/tap_psphot_galaxies.pro : go.bigtest.ckgalaxy
 \begin{figure}[htbp]
   \begin{center}
@@ -3497,9 +3618,10 @@
 accurate for the larger galaxies.
 
-% /data/kukui.1/eugene/galaxies.20190425/tap_psphot_galaxies.pro : go.bigtest.ckgalaxy
+% Figure 16 ** repaired PDF text **
+% was /data/kukui.1/eugene/galaxies.20190425/tap_psphot_galaxies.pro : go.bigtest.ckgalaxy
+% is pueo:galaxies.20190425/tap_psphot_galaxies.pro : go.bigtest.ckgalaxy
 \begin{figure*}[htbp]
   \begin{center}
  \includegraphics[width=\hsize,clip]{\picdir/{galaxy.exp.params}.\plotext}
-
   \caption{\label{fig:exp.params} Parameter recovery for simulated
     galaxies with Exponential profiles.  In each panel, we show
@@ -3519,5 +3641,7 @@
 \end{figure*}
 
-% /data/kukui.1/eugene/galaxies.20190425/tap_psphot_galaxies.pro : go.bigtest.ckgalaxy
+% Figure 17 ** repaired PDF text **
+% was /data/kukui.1/eugene/galaxies.20190425/tap_psphot_galaxies.pro : go.bigtest.ckgalaxy
+% is pueo:galaxies.20190425/tap_psphot_galaxies.pro : go.bigtest.ckgalaxy
 \begin{figure*}[htbp]
   \begin{center}
@@ -3532,10 +3656,8 @@
 \label{sec:psf.forced.fit}
 
-\note{reference to multifit / cfht lens?}
-
 Traditionally, projects which use multiple exposures to increase the
 depth and sensitivity of the observations have generated something
 equivalent to the stack images produced by the IPP analysis,
-\textadd{as done for example by the CFHT Legacy Survey
+\textadd{as done for example by the Canada-France-Hawaii Telescope (CFHT) Legacy Survey
   \citep{2006ApJ...647..116H} or the Cosmic Evolution Survey
   \citep[COSMOS][]{2007ApJS..172...99C}}.  In theory, the photometry
@@ -3641,6 +3763,57 @@
 \ippdbtable{ForcedMeanObject} tables.}
 
-\note{discuss the relative quality of average exposure, forced warp
-  average, and stack photometry. reference to Best et al}
+% Figure 18 ** repaired PDF text **
+% on pueo ~eugene/sdss.psphot.2020414/photcompare.sh : go.figure
+\begin{figure}[htbp]
+ \begin{center}
+ \includegraphics[width=\hsize,clip]{\picdir/{compare.mags}.pdf}
+  \caption{\label{fig:compare.mags} Comparison of {\tt psphot} average
+    chip photometry, average forced-warp photometry, and stack
+    photometry from $3\pi$ Survey data to average forced-warp
+    photometry from the Pan-STARRS\,1 Medium-Deep Survey field MD06
+    At bright magnitudes, average chip photometry is the most
+    accurate while the stack photometry is degraded by the
+    highly-texturd PSF.  At faint magnitudes, average chip magnitudes
+    are biased to artifically bright values.}
+  \end{center}
+\end{figure}
+
+{\TEXTADD With the inclusion of the forced-warp photometry, we have three
+distinct methods for measuring the PSF photometry of stars in the
+Pan-STARRS survey data: the average of the \ippstage{chip}-stage
+photometry from the individual exposures; the measurement from the
+stacks, and the average of the forced-warp photometry described here.
+It is worth considering which of these should be used in which
+circumstance.  Figure~\ref{fig:compare.mags} shows a comparison of
+these three different methods to deeper data from the Medium Deep
+Survey observations (MD06 field).  Our conclusion from this and other
+analysis is that the average \ippstage{chip}-stage photometry is the
+best (most accurate) measurement for brighter objects, where the
+signal-to-noise is roughly 10 or more.  This is the photometry source
+which was used for the global photometry solution discussed by
+\cite{2012ApJ...756..158S} and used in the overall calibration (see
+Paper V).}
+
+{\TEXTADD As can be clearly seen in the figure, the average from the forced-warp
+photometry is slightly worse than the chip photometry, while the stack
+PSF photometry is significantly degraded.  We attribute the latter
+effect to the highly-textured PSF observed in the stack images due to
+the combination of variable PSFs in each exposure and significant
+masking fraction in the PS1 camera.  At the faint end, the chip
+photometry is significantly worse that both average warp and stack
+photometry.  First, in order to have a measurement, a source must be
+detected above the detection threshold in at least one of the
+exposures, limiting the depth possible of the average chip
+photometry. Second, at the faint end, only bright fluctuations will be
+detected, resulting in a bright bias. This latter effect is clearly
+seen in Figure~\ref{fig:compare.mags} as the average chip magnitudes
+diverge from the deeper Medium Deep photometry measurements.  As has
+been noted elsewhere \citep{2018ApJS..234....1B}, the warp and stack
+photometry is also degraded for objects which have significant proper
+motion over the course of the $3\pi$ Survey since the position is held
+constant for all epochs, while the average chip photometry is
+calculated on detections which are cross-matched in the database.
+Thus, warp and stack photometry should be avoided for sources with
+proper motion greater than roughly 100 milliarcseconds per year.}
 
 \subsection{Forced Galaxy Models}
@@ -3663,4 +3836,6 @@
 the same time the best normalization corresponding to the best
 elliptical shape, and thus the best galaxy magnitude value.
+\textadd{This technique is similar to the joint fitting of multiple
+  exposures performed by the CFHT Lensing Survey team \citep{2013MNRAS.429.2858M}.}
 
 For each warp image, the stack values for the major and minor axis are
@@ -3894,5 +4069,8 @@
 from the PSPS database \ippdbtable{ForcedWarpLensing} table while the
 average values calculated over the warps is found in the
-\ippdbtable{ForcedMeanLensing} tables.
+\ippdbtable{ForcedMeanLensing} tables.  \textadd{Although the software used
+here was not involved in any of the GRavitational lEnsing Accuracy
+Testing (GREAT) challenges, it is similar to the code of the EPFL\_KSB
+team \citep{2015MNRAS.450.2963M} and likely to perform similarly.}
 
 % \note{example of using the lensing elements for binaries?}
@@ -4042,19 +4220,4 @@
 \section{Conclusions}
 
-\note{add lessons learned here}
-
-\begin{verbatim}
-Suggestions for improvements / changes
-* use more external knowledge: 
-  ** Gaia or PS1 to select stars as PSF sources
-  ** pre-seed information about the very bright or very crowded
-                regions
-* background model
-  ** allow the superpixel scale to change as a function of environment
-  ** do not use the lower-end model unless region is known to be dense
-* use galactic latitude or local stellar density to smoothly
-  transition from double / multi-PSF to galaxy model fitting
-\end{verbatim}
-
 The Pan-STARRS Image Processing Pipeline has used the \ippprog{psphot}
 software to detect and characterize astronomical sources in images
@@ -4069,4 +4232,36 @@
 million PS\,1 exposures have been characterized (some representing
 repeated measurements of the same exposures).  
+
+There is always room for improvement, however.  A number of
+possible improvements to \ippprog{psphot} have been identified which
+could result in more reliable measurements for either stars or
+galaxies.  Here we discuss improvements beyond simply tuning
+parameters for a specific dataset.
+
+In general, the improvements we identify share the characteristic of
+making use of external information in the analysis.  As described
+above, essentially all operations of \ippprog{psphot}, except in the
+context of forced photometry, approach each image with no prior
+knowledge.  This was necessary in the early stages of the Pan-STARRS
+project when we had not yet observed the sky with our instrument and
+comparable observations were only available in the SDSS Galactic cap
+regions.  However, the sky is now much better known, not only from
+PS1, but also for example due to Gaia.
+
+Several improvements to the \ippprog{psphot} analysis could be made by
+including as much information from external catalogs about the
+positions and characteristics of sources in the images as possible.
+For example, known stars (e.g., based on proper motions from Gaia or
+colors and morphology from PS1) could be used for PSF sources.  In
+areas of high density, especially in known globular or even open
+clusters, existing high-resolution imagery could be used to provide a
+constraint on location of stars.  External information could also be
+used to control the scale on which the background is modelled: a finer
+sampling is helpful in regions of known nebulosity and large galaxies
+such as M31.  Finally, the galactic latitude or the externally-defined
+stellar density could be used to control the choice of fitting double
+stars or galaxy models.  This would be a step beyond the current
+capability of choosing to fit galaxy models as a function of galactic
+latitude.
 
 % PS2 reference:
Index: /trunk/doc/release.2015/ps1.analysis/response.txt
===================================================================
--- /trunk/doc/release.2015/ps1.analysis/response.txt	(revision 41346)
+++ /trunk/doc/release.2015/ps1.analysis/response.txt	(revision 41347)
@@ -1,4 +1,3 @@
 
----------------------------------------------------------------------
 Referee Report
 Reviewer's Comments:
@@ -98,5 +97,5 @@
 that the photometric goals are achieved
 
-**** TBD : discuss relative quality of chip, forced, stack photometry
+** added comparion discussion of chip, warp, stack photometry at the end of Sec 6.1
 
 - Sec 7, where the image differencing detections and photometry is used
@@ -126,5 +125,5 @@
 in one place would be a useful service.
 
-**** TBD : summarize the lessons learned
+** added suggested improvements in conclusion
 
 Abstract:
@@ -331,7 +330,6 @@
 for a typical exposure.
 
-**** TBD: SHOW SOME EXAMPLES of PSF variations 
-     choose 3 exposures: 1 with good IQ, one with bad IQ, but round, one with bad IQ but not round,
-     plot some IQ stats (Mxx - Myy) / (Mxx + Myy)
+** we have added a figure to show examples of the image quality
+   variations observed in PS1 in both good and bad seeing data.
 
 - Please state whether the PSF model is this set of formulae
@@ -435,5 +433,7 @@
 and presented as a future development effort.
 
-**** TBD : wording of full PSF model section 4.6.6
+** reworded to explain that this step, unlike 4.6.2, does a
+   simultaneous fit to the position and normalization for sources
+   one-at-a-time.
 
 - Remind the reader that the 4 independent parameters includes a local sky
@@ -455,5 +455,9 @@
 range.
 
-**** TBD: double-star mode: was this turned on for PV3? ppSim to show recovery
+** In reviewing the code, we discovered that this approach to close
+   neighbors was turned off for PV3, similar to the blend fits
+   discussed above.  We have moved both of these crowded field
+   analysis concepts to a single section, identified as deactivated
+   for PV3.
 
 Sec 4.7:
@@ -558,5 +562,7 @@
 compare well to those in the PS1 catalog?
 
-**** TBD: compare Petrosian mags to SDSS for some example
+** These agree to first order, but there is a tendency for the PS1
+   measurements to have larger radii and smaller (brighter)
+   magnitudes.  Added text and a figure to illustrate
 
 Sec 5.3:
@@ -585,5 +591,10 @@
 error of this approximation should be stated.
 
-**** TBD: model central pixel errors for Sersic models
+** In trying to answer this question, we realized that, while we
+   experimented with this technique, the as implemented psphot in fact
+   used brute-force numerical evaluation.  These implementation
+   experiments did not pan out so we went ahead with something that
+   worked, even if it was slower.  We have updated the text to
+   describe the actual implementation.
 
 Sec 5.4:
@@ -688,5 +699,6 @@
 and if not, which code would it be most similar to?
 
-**** TBD : check on GREAT challenge to compare code 
+** psphot was not used in any of the GREAT challenges, but is similar
+   to the EPFL_KSB team's code.  added this to the text
 
 - Define "KSB" and "HFK" references in-line
