Index: trunk/doc/release.2015/ps1.analysis/analysis.tex
===================================================================
--- trunk/doc/release.2015/ps1.analysis/analysis.tex	(revision 40609)
+++ trunk/doc/release.2015/ps1.analysis/analysis.tex	(revision 40610)
@@ -116,8 +116,4 @@
 %   * real example of oversubtracted galaxy
 % \end{verbatim}
-
-\note{define psfQF, psfQFperfect}
-\note{identify when each PM\_SOURCE\_MODE bit is raised}
-\note{map PM\_SOURCE\_MODE bits from this paper to PSPS paper}
 
 The 1.8m Pan-STARRS\,1 telescope is located on the summit of Haleakala
@@ -543,38 +539,37 @@
 {\bf Flag Name} & {\bf Flag Value} & {\bf Description} \\
 \hline
-*   PM\_SOURCE\_MODE\_PSFMODEL         	  & 0x00000001 & Source fitted with a psf model (linear or non-linear) \\
-*   PM\_SOURCE\_MODE\_EXTMODEL         	  & 0x00000002 & Source fitted with an extended-source model \\
-*   PM\_SOURCE\_MODE\_FITTED           	  & 0x00000004 & Source fitted with non-linear model (PSF or EXT; good or bad) \\
-*   PM\_SOURCE\_MODE\_FAIL             	  & 0x00000008 & Fit (non-linear) failed (non-converge, off-edge, run to zero) \\
-*   PM\_SOURCE\_MODE\_POOR             	  & 0x00000010 & Fit succeeds, but low-SN, high-Chisq, or large (for PSF -- drop?) \\
-*   PM\_SOURCE\_MODE\_PAIR             	  & 0x00000020 & Source fitted with a double psf \\
-*   PM\_SOURCE\_MODE\_PSFSTAR          	  & 0x00000040 & Source used to define PSF model \\
-*   PM\_SOURCE\_MODE\_SATSTAR          	  & 0x00000080 & Source model peak is above saturation \\
-*   PM\_SOURCE\_MODE\_BLEND            	  & 0x00000100 & Source is a blend with other sources$^1$ \\
-*   PM\_SOURCE\_MODE\_EXTERNAL         	  & 0x00000200 & Source based on supplied input position \\
-*   PM\_SOURCE\_MODE\_BADPSF           	  & 0x00000400 & Failed to get good estimate of object's PSF \\
-*   PM\_SOURCE\_MODE\_DEFECT           	  & 0x00000800 & Source is thought to be a defect \\
-*   PM\_SOURCE\_MODE\_SATURATED        	  & 0x00001000 & Source is thought to be saturated pixels (bleed trail) \\
-*   PM\_SOURCE\_MODE\_CR\_LIMIT           & 0x00002000 & Source has crNsigma above limit \\
-*   PM\_SOURCE\_MODE\_EXT\_LIMIT          & 0x00004000 & Source has extNsigma above limit \\
-*   PM\_SOURCE\_MODE\_MOMENTS\_FAILURE    & 0x00008000 & could not measure the moments \\
-*   PM\_SOURCE\_MODE\_SKY\_FAILURE        & 0x00010000 & could not measure the local sky \\
-*   PM\_SOURCE\_MODE\_SKYVAR\_FAILURE     & 0x00020000 & could not measure the local sky variance \\
-x   PM\_SOURCE\_MODE\_BELOW\_MOMENTS\_SN  & 0x00040000 & moments not measured due to low S/N.$^1$ \\
-*   PM\_SOURCE\_MODE\_BIG\_RADIUS         & 0x00100000 & poor moments for small radius, try large radius \\
-*   PM\_SOURCE\_MODE\_AP\_MAGS            & 0x00200000 & source has an aperture magnitude \\
-*   PM\_SOURCE\_MODE\_BLEND\_FIT          & 0x00400000 & source was fitted as a blend \\
-*   PM\_SOURCE\_MODE\_EXTENDED\_FIT       & 0x00800000 & full extended fit was used \\
-*   PM\_SOURCE\_MODE\_EXTENDED\_STATS     & 0x01000000 & extended aperture stats calculated \\
-*   PM\_SOURCE\_MODE\_LINEAR\_FIT         & 0x02000000 & source fitted with the linear fit \\
-*   PM\_SOURCE\_MODE\_NONLINEAR\_FIT      & 0x04000000 & source fitted with the non-linear fit \\
-*   PM\_SOURCE\_MODE\_RADIAL\_FLUX        & 0x08000000 & radial flux measurements calculated \\
-x   PM\_SOURCE\_MODE\_SIZE\_SKIPPED       & 0x10000000 & size could not be determined$^2$ \\
-    PM\_SOURCE\_MODE\_ON\_SPIKE           & 0x20000000 & peak lands on diffraction spike \\
-    PM\_SOURCE\_MODE\_ON\_GHOST           & 0x40000000 & peak lands on ghost or glint \\
-    PM\_SOURCE\_MODE\_OFF\_CHIP           & 0x80000000 & peak lands off edge of chip \\
+ PM\_SOURCE\_MODE\_PSFMODEL            & 0x00000001 & Source fitted with a psf model (linear or non-linear) \\
+ PM\_SOURCE\_MODE\_EXTMODEL            & 0x00000002 & Source fitted with an extended-source model \\
+ PM\_SOURCE\_MODE\_FITTED              & 0x00000004 & Source fitted with non-linear model (PSF or EXT; good or bad) \\
+ PM\_SOURCE\_MODE\_FAIL                & 0x00000008 & Fit (non-linear) failed (non-converge, off-edge, run to zero) \\
+ PM\_SOURCE\_MODE\_POOR                & 0x00000010 & Fit succeeds, but low-SN, high-Chisq, or large (for PSF -- drop?) \\
+ PM\_SOURCE\_MODE\_PAIR                & 0x00000020 & Source fitted with a double psf \\
+ PM\_SOURCE\_MODE\_PSFSTAR             & 0x00000040 & Source used to define PSF model \\
+ PM\_SOURCE\_MODE\_SATSTAR             & 0x00000080 & Source model peak is above saturation \\
+ PM\_SOURCE\_MODE\_BLEND               & 0x00000100 & Source is a blend with other sources$^1$ \\
+ PM\_SOURCE\_MODE\_EXTERNAL            & 0x00000200 & Source based on supplied input position \\
+ PM\_SOURCE\_MODE\_BADPSF              & 0x00000400 & Failed to get good estimate of object's PSF \\
+ PM\_SOURCE\_MODE\_DEFECT              & 0x00000800 & Source is thought to be a defect \\
+ PM\_SOURCE\_MODE\_SATURATED           & 0x00001000 & Source is thought to be saturated pixels (bleed trail) \\
+ PM\_SOURCE\_MODE\_CR\_LIMIT           & 0x00002000 & Source has crNsigma above limit \\
+ PM\_SOURCE\_MODE\_EXT\_LIMIT          & 0x00004000 & Source has extNsigma above limit \\
+ PM\_SOURCE\_MODE\_MOMENTS\_FAILURE    & 0x00008000 & could not measure the moments \\
+ PM\_SOURCE\_MODE\_SKY\_FAILURE        & 0x00010000 & could not measure the local sky \\
+ PM\_SOURCE\_MODE\_SKYVAR\_FAILURE     & 0x00020000 & could not measure the local sky variance \\
+ PM\_SOURCE\_MODE\_BELOW\_MOMENTS\_SN  & 0x00040000 & moments not measured due to low S/N.$^1$ \\
+ PM\_SOURCE\_MODE\_BIG\_RADIUS         & 0x00100000 & poor moments for small radius, try large radius \\
+ PM\_SOURCE\_MODE\_AP\_MAGS            & 0x00200000 & source has an aperture magnitude \\
+ PM\_SOURCE\_MODE\_BLEND\_FIT          & 0x00400000 & source was fitted as a blend \\
+ PM\_SOURCE\_MODE\_EXTENDED\_FIT       & 0x00800000 & full extended fit was used \\
+ PM\_SOURCE\_MODE\_EXTENDED\_STATS     & 0x01000000 & extended aperture stats calculated \\
+ PM\_SOURCE\_MODE\_LINEAR\_FIT         & 0x02000000 & source fitted with the linear fit \\
+ PM\_SOURCE\_MODE\_NONLINEAR\_FIT      & 0x04000000 & source fitted with the non-linear fit \\
+ PM\_SOURCE\_MODE\_RADIAL\_FLUX        & 0x08000000 & radial flux measurements calculated \\
+ PM\_SOURCE\_MODE\_SIZE\_SKIPPED       & 0x10000000 & size could not be determined$^1$ \\
+ PM\_SOURCE\_MODE\_ON\_SPIKE           & 0x20000000 & peak lands on diffraction spike \\
+ PM\_SOURCE\_MODE\_ON\_GHOST           & 0x40000000 & peak lands on ghost or glint \\
+ PM\_SOURCE\_MODE\_OFF\_CHIP           & 0x80000000 & peak lands off edge of chip \\
 \hline
-\multicolumn{3}{l}{$^1$ not used for DR1 or DR2.} \\
-\multicolumn{3}{l}{$^2$ should not normally occur, does it?} \\
+\multicolumn{3}{l}{$^1$ Not used for DR1 or DR2.} \\
 \hline
 \end{tabular}
@@ -591,27 +586,29 @@
 {\bf Flag Name} & {\bf Flag Value} & {\bf Description} \\
 \hline
-PM\_SOURCE\_MODE2\_DIFF\_WITH\_SINGLE 	   & 0x00000001 & diff source matched to a single positive detection \\
-PM\_SOURCE\_MODE2\_DIFF\_WITH\_DOUBLE 	   & 0x00000002 & diff source matched to positive detections in both images \\
-PM\_SOURCE\_MODE2\_MATCHED          	   & 0x00000004 & source generated based on another image \\
-PM\_SOURCE\_MODE2\_ON\_SPIKE         	   & 0x00000008 & $> 25\%$ of (PSF-weighted) pixels land on diffraction spike \\
-PM\_SOURCE\_MODE2\_ON\_STARCORE      	   & 0x00000010 & $> 25\%$ of (PSF-weighted) pixels land on starcore \\
-PM\_SOURCE\_MODE2\_ON\_BURNTOOL      	   & 0x00000020 & $> 25\%$ of (PSF-weighted) pixels land on burntool \\
-PM\_SOURCE\_MODE2\_ON\_CONVPOOR      	   & 0x00000040 & $> 25\%$ of (PSF-weighted) pixels land on convpoor \\
-PM\_SOURCE\_MODE2\_PASS1\_SRC              & 0x00000080 & source detected in first pass analysis \\
-PM\_SOURCE\_MODE2\_HAS\_BRIGHTER\_NEIGHBOR & 0x00000100 & peak is not the brightest in its footprint \\
-PM\_SOURCE\_MODE2\_BRIGHT\_NEIGHBOR\_1     & 0x00000200 & $flux_{\rm n} / (r^2 flux_{\rm p}) > 1$ \\
-PM\_SOURCE\_MODE2\_BRIGHT\_NEIGHBOR\_10    & 0x00000400 & $flux_{\rm n} / (r^2 flux_{\rm p}) > 10$ \\
-PM\_SOURCE\_MODE2\_DIFF\_SELF\_MATCH   	   & 0x00000800 & positive detection match is probably this source \\
-PM\_SOURCE\_MODE2\_SATSTAR\_PROFILE        & 0x00001000 & saturated source is modeled with a radial profile \\
-PM\_SOURCE\_MODE2\_ECONTOUR\_FEW\_PTS      & 0x00002000 & too few points to measure the elliptical contour \\
-PM\_SOURCE\_MODE2\_RADBIN\_NAN\_CENTER     & 0x00004000 & radial bins failed with too many NaN center bin \\
-PM\_SOURCE\_MODE2\_PETRO\_NAN\_CENTER      & 0x00008000 & petrosian radial bins failed with too many NaN center bin \\
-PM\_SOURCE\_MODE2\_PETRO\_NO\_PROFILE      & 0x00010000 & petrosian not build because radial bins missing \\
-PM\_SOURCE\_MODE2\_PETRO\_INSIG\_RATIO     & 0x00020000 & insignificant measurement of petrosian ratio \\
-PM\_SOURCE\_MODE2\_PETRO\_RATIO\_ZEROBIN   & 0x00040000 & petrosian ratio in the 0th bin (likely bad) \\
-PM\_SOURCE\_MODE2\_EXT\_FITS\_RUN          & 0x00080000 & we attempted to run extended fits on this source \\
-PM\_SOURCE\_MODE2\_EXT\_FITS\_FAIL         & 0x00100000 & at least one of the model fits failed \\
-PM\_SOURCE\_MODE2\_EXT\_FITS\_RETRY        & 0x00200000 & one of the model fits was re-tried with new window \\
-PM\_SOURCE\_MODE2\_EXT\_FITS\_NONE         & 0x00400000 & ALL of the model fits failed \\
+ PM\_SOURCE\_MODE2\_DIFF\_WITH\_SINGLE      & 0x00000001 & diff source matched to a single positive detection \\
+ PM\_SOURCE\_MODE2\_DIFF\_WITH\_DOUBLE      & 0x00000002 & diff source matched to positive detections in both images \\
+ PM\_SOURCE\_MODE2\_MATCHED          	    & 0x00000004 & source generated based on another image \\
+ PM\_SOURCE\_MODE2\_ON\_SPIKE               & 0x00000008 & $> 25\%$ of (PSF-weighted) pixels land on diffraction spike \\
+ PM\_SOURCE\_MODE2\_ON\_STARCORE            & 0x00000010 & $> 25\%$ of (PSF-weighted) pixels land on starcore \\
+ PM\_SOURCE\_MODE2\_ON\_BURNTOOL            & 0x00000020 & $> 25\%$ of (PSF-weighted) pixels land on burntool \\
+ PM\_SOURCE\_MODE2\_ON\_CONVPOOR            & 0x00000040 & $> 25\%$ of (PSF-weighted) pixels land on convpoor \\
+ PM\_SOURCE\_MODE2\_PASS1\_SRC              & 0x00000080 & source detected in first pass analysis \\
+ PM\_SOURCE\_MODE2\_HAS\_BRIGHTER\_NEIGHBOR & 0x00000100 & peak is not the brightest in its footprint \\
+ PM\_SOURCE\_MODE2\_BRIGHT\_NEIGHBOR\_1     & 0x00000200 & $flux_{\rm n} / (r^2 flux_{\rm p}) > 1$ \\
+ PM\_SOURCE\_MODE2\_BRIGHT\_NEIGHBOR\_10    & 0x00000400 & $flux_{\rm n} / (r^2 flux_{\rm p}) > 10$ \\
+ PM\_SOURCE\_MODE2\_DIFF\_SELF\_MATCH       & 0x00000800 & positive detection match is probably this source \\
+ PM\_SOURCE\_MODE2\_SATSTAR\_PROFILE        & 0x00001000 & saturated source is modeled with a radial profile \\
+ PM\_SOURCE\_MODE2\_ECONTOUR\_FEW\_PTS      & 0x00002000 & too few points to measure the elliptical contour \\
+ PM\_SOURCE\_MODE2\_RADBIN\_NAN\_CENTER     & 0x00004000 & radial bins failed with too many NaN center bin \\
+ PM\_SOURCE\_MODE2\_PETRO\_NAN\_CENTER      & 0x00008000 & petrosian radial bins failed with too many NaN center bin$^1$ \\
+ PM\_SOURCE\_MODE2\_PETRO\_NO\_PROFILE      & 0x00010000 & petrosian not build because radial bins missing \\
+ PM\_SOURCE\_MODE2\_PETRO\_INSIG\_RATIO     & 0x00020000 & insignificant measurement of petrosian ratio \\
+ PM\_SOURCE\_MODE2\_PETRO\_RATIO\_ZEROBIN   & 0x00040000 & petrosian ratio in the 0th bin (likely bad) \\
+ PM\_SOURCE\_MODE2\_EXT\_FITS\_RUN          & 0x00080000 & we attempted to run extended fits on this source \\
+ PM\_SOURCE\_MODE2\_EXT\_FITS\_FAIL         & 0x00100000 & at least one of the model fits failed \\
+ PM\_SOURCE\_MODE2\_EXT\_FITS\_RETRY        & 0x00200000 & trailed asteroid model fit was re-tried with new window \\
+ PM\_SOURCE\_MODE2\_EXT\_FITS\_NONE         & 0x00400000 & ALL of the model fits failed \\
+\hline
+\multicolumn{3}{l}{$^1$ Not used for DR1 or DR2.} \\
 \hline
 \end{tabular}
@@ -915,4 +912,18 @@
 as locally insignificant.
 
+Sometimes it is useful to know if a source has a near neighbor which
+may be affecting the photometry.  Three flag bits are used to identify
+such possible situations.  Peaks which are {\em not} the brightest peak
+within a single footprint have the flag bit
+\code{PM_SOURCE_MODE2_HAS_BRIGHTER_NEIGHBOR} set.  This is a fairly
+common situation.  We also define the following ratio to compare the
+flux of the bright source to the flux of a neighbor scaled by
+intervening area: $R = \frac{f_{\rm n}}{r^2 f_{\rm p}}$ where $f_{\rm
+  n}$ is the flux of the brightest neighbor in the footprint, $f_{\rm
+  p}$ is the flux of the source of interest, and $r$ is the separation
+between the two sources.  If $R > 1$, the flag bit
+\code{PM_SOURCE_MODE2_HAS_BRIGHT_NEIGHBOR_1} is set. If $R > 10$, the flag bit
+\code{PM_SOURCE_MODE2_HAS_BRIGHT_NEIGHBOR_10} is set. 
+
 \subsubsection{Centroid and Higher-Order Moments}
 \label{sec:moments}
@@ -1420,4 +1431,27 @@
 values (see Section~\ref{sec:image.preparation}).
 
+Several flag bits are raised based on statistics which are similar to
+the \code{PSF_QF} measurement.  First, \ippprog{psphot} calculates the
+normalized, PSF-weighted fraction of pixels which are masked due to
+one of the following four causes: a diffraction spike (\code{SPIKE}),
+the core of a saturated star (\code{CORE}), burntool-subtracted region
+(\code{BURNTOOL}), or a pixel for which, due to interpolation or
+convolution, a significant fraction of the pixel flux comes from a
+masked pixel.  These masking conditions are all treated as ``suspect''
+by \ippprog{psphot}, which means they are {\em included} in the analysis of
+the source pixels.  However, since they may potentially affect the
+photometry (or astrometry), it is useful to note of a source has a
+non-trivial fraction of these poor mask pixels.  If the normalized
+PSF-weighted fraction of pixels masked due to any of these four
+conditions exceeds 25\%, then one of the following bits is raised for
+the corresponding condition:  \code{PM_SOURCE_MODE2_ON_SPIKE},
+\code{PM_SOURCE_MODE2_ON_STARCORE},
+\code{PM_SOURCE_MODE2_ON_BURNTOOL},
+\code{PM_SOURCE_MODE2_ON_CONVPOOR}.  In addition, the following flag
+bits may also be raised if the central pixel
+of a source lands on a pixel masked for a diffraction spike
+(\code{PM_SOURCE_MODE_ON_SPIKE}), an optical ghost
+(\code{PM_SOURCE_MODE_ON_GHOST}), or off the active pixels of the CCD (\code{PM_SOURCE_MODE_OFF_CHIP}).
+
 \subsection{Bright Source Analysis}
 
@@ -1472,5 +1506,7 @@
 in the central regions are largely masked, because they are
 saturated.  Thus in these cases, the psf-weighted masked fraction (see
-Section~\ref{XX}) is generally quite low or 0.0.
+Section~\ref{sec:psf.model.choice}) is generally quite low or 0.0.
+Sources for which this radial profile is subtracted have the flag bit
+\code{PM_SOURCE_MODE2_SATSTAR_PROFILE} set.
 
 % logRdel = 0.1
@@ -1877,8 +1913,26 @@
 The sources which are measured in this faint-source stage are clearly
 low significance detections.  The PV3 threshold for the bright source
-analysis is a signal-to-noise of 20.  The lower limit cutoff for the
-faint source analysis in PV3 is a signal-to-noise of 5.0.  Sources
-detected in the faint source stage are fitted with the PSF model using
-the linear, ensemble fitting process.
+analysis is a signal-to-noise of 20.  The flag bit
+\code{PM_SOURCE_MODE2_PASS1_SRC} is raised for sources detected in
+this initial analysis stage.  The lower limit cutoff for the faint
+source analysis in PV3 is a signal-to-noise of 5.0.  Sources detected
+in the faint source stage are fitted with the PSF model using the
+linear, ensemble fitting process.
+
+In the \ippprog{psphotStack} version of the code, the 5 filter images
+are processed together.  In this case, any source which is detected in
+at least two of the five filters are then also measured on the other
+filter images in which it was not detected above the signal-to-noise
+limit.  The position in the other stack images is fixed based on the
+pixel coordinates in the images in which the source was detected.
+Detection in two filters is required in order to avoid excessive
+forced photometry of spurious detections.  There is an interesting
+class of astronomical objects which are extremely red (\eg, brown
+dwarfs and high-redshift quasars).  Such sources are expected to be
+detected only in the reddest filter (\yps).  For the $3\pi$ PV3
+processing, we therefore also force the photometry in all filters for
+sources which are only detected in \yps.  All sources which are forced
+on one image based on detections in other images have the flag bit
+\code{PM_SOURCE_MODE2_MATCHED} set.
 
 \subsection{Extended Source Analysis}
@@ -1987,4 +2041,13 @@
 of the form $f_i(r_i)$.  In this case, $f_i$ is effectively the
 surface brightness for each radius in instrumental counts per pixel.
+If fewer than 4 radial surface-brightness values are available for the
+analysis, the source is skipped and the flag bit
+\code{PM_SOURCE_MODE2_ECONTOUR_FEW_PTS} is set.  Some apparently
+extended sources are in fact bright stars with central saturation.
+These sources show up in this analysis as having many NAN-valued
+pixels in the central regions.  During the radial profile analysis,
+such sources are flagged with the bit
+\code{PM_SOURCE_MODE2_RADBIN_NAN_CENTER} and are skipped from the rest
+of the analysis.
 
 The surface brightness profiles are then used to define the azimuthal
@@ -2043,5 +2106,8 @@
   max}$ for the annulus for which the Petrosian Ratio = 0.2, i.e., the
 point on the galaxy radial profile at which the surface brightness is
-20\% of the average surface brightness at that point.  
+20\% of the average surface brightness at that point.  If the profile
+falls below the Petrosian ratio for the first radial bin, the flag bit
+\code{PM_SOURCE_MODE2_PETRO_RATIO_ZEROBIN} is set to note that the
+Petrosian radius may be poorly determined.
 
 We determine the Petrosian Radius for the galaxy by quadratic
@@ -2054,6 +2120,9 @@
 well as drops.  We track the Petrosian Ratio until the value is no
 longer significant ($\sigma_{\rm Ratio} < 2 {\rm Ratio}$).  If the
-Petrosian Ratio drops below 0.2 for more than one radius, we choose
-the largest such radius.  
+Petrosian ratio drops below 0.2 for more than one radius, we choose
+the largest such radius.  If the Petrosian ratio does not fall below
+0.2 for any of the measured radii, the annulus for which the ratio
+falls to the lowest (yet still significant) value.  In such a case,
+the flag bit \code{PM_SOURCE_MODE2_PETRO_INSIG_RATIO} is set.  
 
 Once the Petrosian Radius has been determined, we can now measure the
@@ -2064,5 +2133,8 @@
 Petrosian flux is contained.  Sources for which the Petrosian
 parameters are successfully measured have the flag bit
-\code{PM_SOURCE_MODE_EXTENDED_STATS} set.
+\code{PM_SOURCE_MODE_EXTENDED_STATS} set.  Sources for which the Petrosian
+parameters were attempted, but for which the radial profile analysis
+failed have the flag bit
+\code{PM_SOURCE_MODE2_PETRO_NO_PROFILE} set.  
 
 
@@ -2096,5 +2168,11 @@
 (Section~\ref{sec:source.size}) or (b) the peak of the PSF profile was
 above the saturation limit for the chip \citep[see the discussion
-  in][regarding the masking of saturated pixels]{waters2017}.  
+  in][regarding the masking of saturated pixels]{waters2017}.  All
+sources for which the extended source model fits were attempted the
+flag bit \code{PM_SOURCE_MODE2_EXT_FITS_RUN} set.  If any of the
+attempted model fits failed, then the flag bit
+\code{PM_SOURCE_MODE2_EXT_FITS_FAIL} is set.  If all model fits
+failed, then the flag bit \code{PM_SOURCE_MODE2_EXT_FITS_NONE} is
+set.  
 
 % \note{need a discussion of the detector saturation behavior?}
