Index: trunk/psphot/src/psphot.h
===================================================================
--- trunk/psphot/src/psphot.h	(revision 27819)
+++ trunk/psphot/src/psphot.h	(revision 28013)
@@ -37,6 +37,6 @@
 bool            psphotReadoutMinimal(pmConfig *config, const pmFPAview *view);
 
-bool            psphotReadoutCleanup (pmConfig *config, const pmFPAview *view);
-bool            psphotReadoutCleanupReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+bool            psphotReadoutCleanup (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotReadoutCleanupReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
 
 bool            psphotDefineFiles (pmConfig *config, pmFPAfile *input);
@@ -51,86 +51,86 @@
 
 // psphotReadout functions
-bool            psphotAddPhotcode (pmConfig *config, const pmFPAview *view);
+bool            psphotAddPhotcode (pmConfig *config, const pmFPAview *view, const char *filerule);
 bool            psphotAddPhotcodeReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
 
-bool            psphotSetMaskAndVariance (pmConfig *config, const pmFPAview *view);
-bool            psphotSetMaskAndVarianceReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool            psphotModelBackground (pmConfig *config, const pmFPAview *view);
-bool            psphotModelBackgroundReadoutFileIndex (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool            psphotSubtractBackground (pmConfig *config, const pmFPAview *view);
-bool            psphotSubtractBackgroundReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool            psphotFindDetections (pmConfig *config, const pmFPAview *view, bool firstPass);
-bool            psphotFindDetectionsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool firstPass);
-
-bool            psphotSourceStats (pmConfig *config, const pmFPAview *view, bool setWindow);
-bool            psphotSourceStatsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool setWindow);
-
-bool            psphotDeblendSatstars (pmConfig *config, const pmFPAview *view);
-bool            psphotDeblendSatstarsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool            psphotBasicDeblend (pmConfig *config, const pmFPAview *view);
-bool            psphotBasicDeblendReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool            psphotRoughClass (pmConfig *config, const pmFPAview *view);
-bool            psphotRoughClassReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+bool            psphotSetMaskAndVariance (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotSetMaskAndVarianceReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+
+bool            psphotModelBackground (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotModelBackgroundReadoutFileIndex (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
+
+bool            psphotSubtractBackground (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotSubtractBackgroundReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+
+bool            psphotFindDetections (pmConfig *config, const pmFPAview *view, const char *filerule, bool firstPass);
+bool            psphotFindDetectionsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe, bool firstPass);
+
+bool            psphotSourceStats (pmConfig *config, const pmFPAview *view, const char *filerule, bool setWindow);
+bool            psphotSourceStatsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe, bool setWindow);
+
+bool            psphotDeblendSatstars (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotDeblendSatstarsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
+
+bool            psphotBasicDeblend (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotBasicDeblendReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
+
+bool            psphotRoughClass (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotRoughClassReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
 bool            psphotRoughClassRegion (int nRegion, psRegion *region, psArray *sources, psMetadata *analysis, psMetadata *recipe, const bool havePSF);
 
-bool            psphotImageQuality (pmConfig *config, const pmFPAview *view);
-bool            psphotImageQualityReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool            psphotChoosePSF (pmConfig *config, const pmFPAview *view);
-bool            psphotChoosePSFReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool            psphotGuessModels (pmConfig *config, const pmFPAview *view);
-bool            psphotGuessModelsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool            psphotMergeSources (pmConfig *config, const pmFPAview *view);
-bool            psphotMergeSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool            psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, bool final);
+bool            psphotImageQuality (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotImageQualityReadout(pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+
+bool            psphotChoosePSF (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotChoosePSFReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+
+bool            psphotGuessModels (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotGuessModelsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
+
+bool            psphotMergeSources (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotMergeSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
+
+bool            psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, const char *filerule, bool final);
 bool            psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final);
 
-bool            psphotSourceSize (pmConfig *config, const pmFPAview *view, bool getPSFsize);
-bool            psphotSourceSizeReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool getPSFsize);
-
-bool            psphotBlendFit (pmConfig *config, const pmFPAview *view);
-bool            psphotBlendFitReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+bool            psphotSourceSize (pmConfig *config, const pmFPAview *view, const char *filerule, bool getPSFsize);
+bool            psphotSourceSizeReadout(pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe, bool getPSFsize);
+
+bool            psphotBlendFit (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotBlendFitReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
 bool            psphotBlendFit_Threaded (psThreadJob *job);
 
-bool            psphotReplaceAllSources (pmConfig *config, const pmFPAview *view);
-bool            psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool            psphotAddNoise (pmConfig *config, const pmFPAview *view);
-bool            psphotSubNoise (pmConfig *config, const pmFPAview *view);
-bool            psphotAddOrSubNoise (pmConfig *config, const pmFPAview *view, bool add);
-bool            psphotAddOrSubNoiseReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool add);
-
-bool            psphotExtendedSourceAnalysis (pmConfig *config, const pmFPAview *view);
-bool            psphotExtendedSourceAnalysisReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool            psphotExtendedSourceFits (pmConfig *config, const pmFPAview *view);
-bool            psphotExtendedSourceFitsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool            psphotApResid (pmConfig *config, const pmFPAview *view);
-bool            psphotApResidReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
-
-bool            psphotMagnitudes (pmConfig *config, const pmFPAview *view);
+bool            psphotReplaceAllSources (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+
+bool            psphotAddNoise (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotSubNoise (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotAddOrSubNoise (pmConfig *config, const pmFPAview *view, const char *filerule, bool add);
+bool            psphotAddOrSubNoiseReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe, bool add);
+
+bool            psphotExtendedSourceAnalysis (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotExtendedSourceAnalysisReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+
+bool            psphotExtendedSourceFits (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotExtendedSourceFitsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+
+bool            psphotApResid (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotApResidReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+
+bool            psphotMagnitudes (pmConfig *config, const pmFPAview *view, const char *filerule);
 bool            psphotMagnitudesReadout(pmConfig *config, psMetadata *recipe, const pmFPAview *view, pmReadout *readout, psArray *sources, pmPSF *psf);
 bool            psphotMagnitudes_Threaded (psThreadJob *job);
 
-bool            psphotEfficiency (pmConfig *config, const pmFPAview *view);
-bool            psphotEfficiencyReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe);
+bool            psphotEfficiency (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotEfficiencyReadout(pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
 
 bool            psphotPSFWeights(pmConfig *config, pmReadout *readout, const pmFPAview *view, psArray *sources);
 bool            psphotPSFWeights_Threaded (psThreadJob *job);
 
-bool            psphotSkyReplace (pmConfig *config, const pmFPAview *view);
-bool            psphotSkyReplaceReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index);
-
-bool            psphotSourceFreePixels (pmConfig *config, const pmFPAview *view);
-bool            psphotSourceFreePixelsReadout(pmConfig *config, const pmFPAview *view, const char *filename, int index);
+bool            psphotSkyReplace (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotSkyReplaceReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
+
+bool            psphotSourceFreePixels (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotSourceFreePixelsReadout(pmConfig *config, const pmFPAview *view, const char *filerule, int index);
 
 // in psphotSourceStats.c:
@@ -147,11 +147,11 @@
 
 // in psphotMergeSources.c:
-bool            psphotLoadExtSources (pmConfig *config, const pmFPAview *view);
+bool            psphotLoadExtSources (pmConfig *config, const pmFPAview *view, const char *filerule);
 psArray        *psphotLoadPSFSources (pmConfig *config, const pmFPAview *view);
-bool            psphotRepairLoadedSources (pmConfig *config, const pmFPAview *view);
-bool            psphotCheckExtSources (pmConfig *config, const pmFPAview *view);
+bool            psphotRepairLoadedSources (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool            psphotCheckExtSources (pmConfig *config, const pmFPAview *view, const char *filerule);
 
 // generate the detection structure for the supplied array of sources
-bool            psphotDetectionsFromSources (pmConfig *config, const pmFPAview *view, psArray *sources);
+bool            psphotDetectionsFromSources (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *sources);
 
 // generate the detection structure for the supplied array of sources
@@ -349,16 +349,16 @@
 bool psphotStackImageLoop (pmConfig *config);
 bool psphotStackReadout (pmConfig *config, const pmFPAview *view);
-bool psphotStackChisqImage (pmConfig *config, const pmFPAview *view);
+bool psphotStackChisqImage (pmConfig *config, const pmFPAview *view, const char *ruleDet, const char *ruleCnv);
 bool psphotStackChisqImageAddReadout(const pmConfig *config, // Configuration
 				     const pmFPAview *view,
+				     const char *filename, 
 				     pmReadout **chiReadout,
-				     char *filename, 
 				     int index);
 
-bool psphotStackRemoveChisqFromInputs (pmConfig *config);
+bool psphotStackRemoveChisqFromInputs (pmConfig *config, const char *filerule);
 bool pmFPAfileRemoveSingle(psMetadata *files, const char *name, int num);
 
-psArray *psphotMatchSources (pmConfig *config, const pmFPAview *view);
-bool psphotMatchSourcesReadout (psArray *objects, pmConfig *config, const pmFPAview *view, char *filename, int index);
+psArray *psphotMatchSources (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool psphotMatchSourcesReadout (psArray *objects, pmConfig *config, const pmFPAview *view, const char *filerule, int index);
 bool psphotMatchSourcesToObjects (psArray *objects, psArray *sources, float RADIUS);
 
@@ -367,3 +367,98 @@
 int pmPhotObjSortByX (const void **a, const void **b);
 
+typedef enum {
+    PSPHOT_CNV_SRC_NONE,
+    PSPHOT_CNV_SRC_AUTO,
+    PSPHOT_CNV_SRC_CNV,
+    PSPHOT_CNV_SRC_RAW,
+} psphotStackConvolveSource;
+
+/// Options for stacking process
+typedef struct {
+    // Setup
+    
+    int numCols;                            // size of image (X)
+    int numRows;                            // size of image (Y)
+
+    int num;                            // Number of inputs
+    bool convolve;                      // Convolve images?
+    psphotStackConvolveSource convolveSource;
+    // psArray *convImages, *convMasks, *convVariances; // Filenames for the temporary convolved images
+
+    // bool matchZPs;                      // Adjust relative fluxes based on transparency analysis?
+    // bool photometry;                    // Perform photometry?
+    // psMetadata *stats;                  // Statistics for output
+    // FILE *statsFile;                    // File to which to write statistics
+    // psArray *origImages, *origMasks, *origVariances; // Filenames of the original images
+    // psArray *origCovars;                // Original covariances matrices
+    // int quality;                        // Bad data quality flag
+
+    // Prepare
+    pmPSF *psf;                         // Target PSF
+    psVector *inputSeeing;              // Input seeing FWHMs
+    psVector *inputMask;                // Mask for inputs
+
+    float targetSeeing;                 // Target seeing FWHM
+    psArray *sourceLists;               // Individual lists of sources for matching
+    psVector *norm;                     // Normalisation for each image
+    psArray *psfs;
+
+    // psVector *exposures;                // Exposure times
+    // float sumExposure;                  // Sum of exposure times
+    // float zp;                           // Zero point for output
+    // psVector *inputMask;                // Mask for inputs
+    // psArray *sources;                   // Matched sources
+
+    // Convolve
+    psArray *kernels;                   // PSF-matching kernels --- required in the stacking
+    psArray *regions;                   // PSF-matching regions --- required in the stacking
+    psVector *matchChi2;                // chi^2 for stamps from matching
+    psVector *weightings;               // Combination weightings for images (1/noise^2)
+    // psArray *cells;                     // Cells for convolved images --- a handle for reading again
+    // int numCols, numRows;               // Size of image
+    // psArray *convCovars;                // Convolved covariance matrices
+
+    // Combine initial
+    // pmReadout *outRO;                   // Output readout
+    // pmReadout *expRO;                   // Exposure readout
+    // psArray *inspect;                   // Array of arrays of pixels to inspect
+
+    // Rejection
+    // psArray *rejected;                  // Rejected pixels
+} psphotStackOptions;
+
+/*** psphotStackMatchPSF prototypes ***/
+bool psphotStackMatchPSFs (pmConfig *config, const pmFPAview *view);
+bool psphotStackMatchPSFsReadout (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index);
+bool psphotStackMatchPSFsPrepare (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index);
+
+// psphotStackMatchPSFsUtils
+psVector *SetOptWidths (bool *optimum, psMetadata *recipe);
+pmReadout *makeFakeReadout(pmConfig *config, pmReadout *raw, psArray *sources, pmPSF *psf, psImageMaskType maskVal, int fullSize);
+bool rescaleData(pmReadout *readout, pmConfig *config, psphotStackOptions *options, int index);
+bool renormKernel(pmReadout *readout, psphotStackOptions *options, int index);
+bool saveMatchData (pmReadout *readout, psphotStackOptions *options, int index);
+bool matchKernel(pmConfig *config, pmReadout *cnv, pmReadout *raw, psphotStackOptions *options, int index);
+bool dumpImageDiff(pmReadout *readoutConv, pmReadout *readoutFake, pmReadout *readoutRef, int index, char *rootname);
+bool dumpImage(pmReadout *readoutOut, pmReadout *readoutRef, int index, char *rootname);
+bool loadKernel (pmConfig *config, pmReadout *readoutCnv, psphotStackOptions *options, int index);
+
+pmPSF *psphotStackPSF(const pmConfig *config, int numCols, int numRows, const psArray *psfs, const psVector *inputMask);
+
+psphotStackOptions *psphotStackOptionsAlloc (int num);
+psphotStackConvolveSource psphotStackConvolveSourceFromString (const char *string);
+pmFPAfile *psphotStackGetConvolveSource (pmConfig *config, psphotStackOptions *options, int index);
+
+bool psphotCopySources (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc);
+bool psphotCopySourcesReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index);
+
+bool psphotRadialApertures (pmConfig *config, const pmFPAview *view, const char *filerule);
+bool psphotRadialAperturesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
+bool psphotRadialApertureSource (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal, const psVector *radMax);
+
+bool psphotExtendedSourceAnalysisByObject (pmConfig *config, psArray *objects, const pmFPAview *view, const char *filerule);
+bool psphotRadialAperturesByObject (pmConfig *config, psArray *objects, const pmFPAview *view, const char *filerule);
+
+bool psphotStackObjectsUnifyPosition (psArray *objects);
+
 #endif
