Index: branches/eam_branches/gDiff.20200202/src/gDiffLoop.c
===================================================================
--- branches/eam_branches/gDiff.20200202/src/gDiffLoop.c	(revision 41260)
+++ branches/eam_branches/gDiff.20200202/src/gDiffLoop.c	(revision 41413)
@@ -1,3 +1,376 @@
+/** @file gDiffLoop.c
+ *  @brief: image subtraction based on matched Gaussians profiles
+ *  @author Eugene Magnier @ IfA
+ *  @version $Revision: 1.12 $
+ *  @date $Date: 2020-01-25 $
+ *  Copyright 2020 Institute for Astronomy, University of Hawaii
+ */
+
 #include "gDiff.h"
+
+# define HDUCHECK(A)							\
+    fprintf (stderr, "%s\n", A);					\
+    listhdu (config, "GDIFF.OUTPUT"); listhdu (config, "PSPHOT.INPUT");
+
+// internal functions:
+bool dumpout(pmConfig *config, char *name);
+bool listhdu(pmConfig *config, char *name);
+
+bool gDiffLoop(gDiffData *data)
+{
+    bool mdok = false;
+    bool success = true;
+
+    psAssert(data, "Require processing data");
+    pmConfig *config = data->config;    // Configuration
+    psAssert(config, "Require configuration.");
+
+    HDUCHECK ("0a: start gDiffLoop");
+
+    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, GDIFF_RECIPE); // Recipe for ppSim
+    psAssert(recipe, "We checked this earlier, so it should be here.");
+    bool noConvolve = psMetadataLookupBool(&mdok, recipe, "NOCONVOLVE"); // Do not use convolved images.
+
+    pmConfigCamerasCull(config, NULL);
+    pmConfigRecipesCull(config, "GDIFF,PPSTATS,PSPHOT,PSASTRO,MASKS,JPEG");
+
+    pmFPAfile *input     = psMetadataLookupPtr(NULL, config->files, "GDIFF.INPUT");
+    pmFPAfile *reference = psMetadataLookupPtr(NULL, config->files, "GDIFF.REF");
+    pmFPAfile *output    = psMetadataLookupPtr(NULL, config->files, "GDIFF.OUTPUT");
+    psAssert(input && reference && output, "Require files");
+
+    // load input files : we are required to load a complete set of files that describe the input.
+    // we are not able to process subdivisions of an image (e.g., cells vs chips)
+    if (!gDiffFilesIterateDown(config, GDIFF_FILES_INPUT | GDIFF_FILES_CONV)) {
+        psError(GDIFF_ERR_IO, false, "Unable to load files.");
+        return false;
+    }
+
+    psTimerStart("GDIFF_MATCH");
+
+    HDUCHECK ("0b: gDiffSetMasks");
+
+    if (!gDiffSetMasks(config)) {
+        psError(psErrorCodeLast(), false, "Unable to set masks.");
+        return false;
+    }
+
+    HDUCHECK ("0b.1: gDiffInputDetections");
+    if (data->forcedPhot1) {
+	bool foundDetections = false;
+	if (!gDiffInputDetections(&foundDetections, "GDIFF.POS1.SOURCES", "GDIFF.INPUT", data)) {
+	    psError(psErrorCodeLast(), false, "Unable to measure positive detections (1)");
+	    success = false;
+            goto ESCAPE;
+	}
+	// if nothing was found, don't bother doing the forced photometry below
+	if (!foundDetections) {
+	    psWarning ("no sources found in positive image 1, skipping forced photometry");
+	    data->forcedPhot1 = false;
+	}
+    }
+    HDUCHECK ("0b.2: gDiffInputDetections");
+    if (data->forcedPhot2) {
+	// XXX why is this only done for Phot2 and not Phot1 ??
+
+        // Change the recipe to use a higher nsigma limit and quit after pass1
+        psMetadata *psphotRecipe = psMetadataLookupPtr (NULL, config->recipes, PSPHOT_RECIPE);
+
+        psF32 nsigma_peak_save = psMetadataLookupF32 (NULL, psphotRecipe, "PEAKS_NSIGMA_LIMIT");
+        char *breakPt_save =  psMetadataLookupStr (NULL, psphotRecipe, "BREAK_POINT");
+
+        psF32 pos2_nsigma_peak = psMetadataLookupF32 (&mdok, psphotRecipe, "PEAKS_POS2_NSIGMA_LIMIT");
+        if (!mdok) {
+            psWarning("PEAKS_POS2_NSIGMA_LIMIT not found in psphotRecipe. Will use 25.\n");
+            pos2_nsigma_peak = 25.;
+        }
+        psMetadataAddF32(psphotRecipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", pos2_nsigma_peak);
+        psMetadataAddStr(psphotRecipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", "PASS1");
+
+	bool foundDetections = false;
+	if (!gDiffInputDetections(&foundDetections, "GDIFF.POS2.SOURCES", "GDIFF.REF", data)) {
+	    psError(psErrorCodeLast(), false, "Unable to measure positive detections (2)");
+            psMetadataAddF32(psphotRecipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", nsigma_peak_save);
+            psMetadataAddStr(psphotRecipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", breakPt_save);
+	    success = false;
+            goto ESCAPE;
+	}
+        psMetadataAddF32(psphotRecipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", nsigma_peak_save);
+        psMetadataAddStr(psphotRecipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", breakPt_save);
+	// if nothing was found, don't bother doing the forced photometry below
+	if (!foundDetections) {
+	    psWarning ("no sources found in positive image 2, skipping forced photometry");
+	    data->forcedPhot2 = false;
+	}
+    }
+
+    // XXX if it exists, use the POS1, POS2 successs for the FWHMs
+
+    /**** The PSF matching process is performed in this function below. ****
+     **** This is the core of the PSF-matched image subtraction code.   ****/
+
+    HDUCHECK ("1: before gDiffMatchPSFs");
+
+    if (!gDiffMatchPSFs(data)) {
+	psError(psErrorCodeLast(), false, "Unable to match PSFs.");
+	success = false;
+	goto ESCAPE;
+    }
+    if (data->quality) {
+        // Can't do anything at all
+        success = false;
+        goto ESCAPE;
+    }
+
+    // the functions in this block are not critical for initial testing, so block them out for now
+
+    // generate the residual stamp grid for visualization
+    if (!gDiffResidualSampleJpeg(config)) {
+        psError(psErrorCodeLast(), false, "Unable to update.");
+        success = false;
+        goto ESCAPE;
+    }
+
+    // XXX add in a positive image detection step here (if needed)
+    // XXX EAM what does this mean???
+
+    psMetadataAddF32(data->stats, PS_LIST_TAIL, "TIME_MATCH", 0, "Time to match PSFs", psTimerClear("GDIFF_MATCH"));
+
+    // Close input files (freeing up space) : for the noConvolve case, we cannot close the
+    // inputs since we will use them for subtraction below.  wait until later to do the work
+    if (!noConvolve) {
+	if (!gDiffFilesIterateUp(config, GDIFF_FILES_INPUT)) {
+	    psError(GDIFF_ERR_IO, false, "Unable to close input files.");
+	    success = false;
+	    goto ESCAPE;
+	}
+    }
+    if (!gDiffLowThreshold(data)) {
+        psError(psErrorCodeLast(), false, "Unable to threshold images.");
+        success = false;
+        goto ESCAPE;
+    }
+
+    HDUCHECK ("2: after gDiffLowThreshold");
+
+    // Set up subtraction files
+    if (!gDiffFilesIterateDown(config, GDIFF_FILES_SUB)) {
+        psError(GDIFF_ERR_IO, false, "Unable to set up subtraction files.");
+        success = false;
+        goto ESCAPE;
+    }
+
+    if (!gDiffDefineOutput("GDIFF.OUTPUT", config)) {
+        psError(psErrorCodeLast(), false, "Unable to define output.");
+        success = false;
+        goto ESCAPE;
+    }
+
+    HDUCHECK ("2a: gDiffMakePSF");
+
+    // the functions in this block are not critical for initial testing, so block them out for now
+    if (!data->quality && !gDiffMakePSF(data)) {
+        psError(psErrorCodeLast(), false, "Unable to generate PSF.");
+        success = false;
+        goto ESCAPE;
+    }
+
+    // Now we've got a PSF, blow away detections in case they're confused with real output detections
+    {
+        pmFPAview *view = gDiffViewReadout(); // View to readout
+        pmReadout *out = pmFPAfileThisReadout(config->files, view, "GDIFF.OUTPUT");
+        if (psMetadataLookup(out->analysis, "PSPHOT.DETECTIONS")) {
+            psMetadataRemoveKey(out->analysis, "PSPHOT.DETECTIONS");
+        }
+        psFree(view);
+    }
+
+    HDUCHECK ("3: before gDiffReadoutSubtract");
+
+    if (!gDiffReadoutSubtract(config)) {
+        psError(psErrorCodeLast(), false, "Unable to subtract images.");
+        success = false;
+        goto ESCAPE;
+    }
+
+    // Close convolved files
+    if (!gDiffFilesIterateUp(config, GDIFF_FILES_PSF | GDIFF_FILES_CONV)) {
+        psError(GDIFF_ERR_IO, false, "Unable to close input files.");
+        success = false;
+        goto ESCAPE;
+    }
+    // Close unconvolved input files if we did the noConvolve subtraction
+    // (otherwise they are closed above)
+    if (noConvolve && !gDiffFilesIterateUp(config, GDIFF_FILES_INPUT)) {
+	psError(GDIFF_ERR_IO, false, "Unable to close input files.");
+	success = false;
+	goto ESCAPE;
+    }
+    
+    HDUCHECK ("4: before gDiffBackground");
+
+    // Higher order background subtraction using psphot
+    if (!gDiffBackground(config)) {
+        psError(psErrorCodeLast(), false, "Unable to subtract background.");
+        success = false;
+        goto ESCAPE;
+    }
+    // dumpout(config, "diff.2b.fits");
+
+    HDUCHECK ("5: before gDiffVarianceRescale");
+
+    // Perform Variance correction (rescale within a modest range)
+    if (!gDiffVarianceRescale(config, data)) {
+        psError(psErrorCodeLast(), false, "Unable to rescale variance.");
+        success = false;
+        goto ESCAPE;
+    }
+    // dumpout(config, "diff.2c.fits");
+
+    if (data->quality) {
+        // Done all we can do up to this point
+        success = false;
+        goto ESCAPE;
+    }
+
+    if (!gDiffFilesIterateDown(config, GDIFF_FILES_PHOT_SUB)) {
+        psError(GDIFF_ERR_IO, false, "Unable to set up photometry files.");
+        success = false;
+        goto ESCAPE;
+    }
+
+    HDUCHECK ("6: before gDiffReadoutPhotometry");
+
+    if (!data->quality && !gDiffReadoutPhotometry("GDIFF.OUTPUT", data)) {
+        psError(psErrorCodeLast(), false, "Unable to perform photometry.");
+        success = false;
+        goto ESCAPE;
+    }
+    // dumpout(config, "diff.3.fits");
+
+    HDUCHECK ("7: before gDiffReadoutForcedPhot (1)");
+
+    // forced photometry for positive image 1
+    if (data->forcedPhot1 && !data->quality) {
+	if (!gDiffReadoutForcedPhot("GDIFF.FORCED1.SOURCES", "GDIFF.OUTPUT", "GDIFF.POS1.SOURCES", data)) {
+	    psError(psErrorCodeLast(), false, "Unable to perform photometry.");
+	    success = false;
+            goto ESCAPE;
+	}
+    }
+
+    // forced photometry for positive image 2
+    if (data->forcedPhot2 && !data->quality) {
+	if (!gDiffReadoutForcedPhot("GDIFF.FORCED2.SOURCES", "GDIFF.OUTPUT", "GDIFF.POS2.SOURCES", data)) {
+	    psError(psErrorCodeLast(), false, "Unable to perform photometry.");
+	    success = false;
+            goto ESCAPE;
+	}
+    }
+
+    if (!gDiffFilesIterateUp(config, GDIFF_FILES_PHOT_SUB)) {
+        psError(GDIFF_ERR_IO, false, "Unable to set up photometry files.");
+        success = false;
+        goto ESCAPE;
+    }
+
+    // Perform statistics on the cell
+    if (!gDiffReadoutStats(data)) {
+        psError(psErrorCodeLast(), false, "Unable to collect statistics");
+        success = false;
+        goto ESCAPE;
+    }
+    // Do Mask Stats
+    {
+	pmFPAview *view = gDiffViewReadout(); // View to readout
+	if (!gDiffMaskStats(config, view,data->stats)) {
+	    psError(psErrorCodeLast(), false, "Unable to generate mask statistics");
+	    success = false;
+	    psFree(view);
+	    goto ESCAPE;
+	}
+	psFree(view);
+    }
+    // dumpout(config, "diff.4.fits");
+    
+    // generate the binned image used to write the jpeg
+    if (!gDiffReadoutJpeg(config)) {
+        psError(psErrorCodeLast(), false, "Unable to update.");
+        success = false;
+        goto ESCAPE;
+    }
+
+    if (data->inverse) {
+        // Set up inverse subtraction files
+        if (!gDiffFilesIterateDown(config, GDIFF_FILES_INV)) {
+            psError(GDIFF_ERR_IO, false, "Unable to set up inverse files.");
+            success = false;
+            goto ESCAPE;
+        }
+
+        if (data->inverse && !gDiffDefineOutput("GDIFF.INVERSE", config)) {
+            psError(psErrorCodeLast(), false, "Unable to define inverse.");
+            success = false;
+            goto ESCAPE;
+        }
+
+        if (!gDiffReadoutInverse(config)) {
+            psError(psErrorCodeLast(), false, "Unable to invert images.");
+            success = false;
+            goto ESCAPE;
+        }
+
+        //MEH -- need to also clear out detections or inv.cmf corrupted on update
+        pmFPAview *viewinv = gDiffViewReadout(); // View to readout
+        pmReadout *inv = pmFPAfileThisReadout(config->files, viewinv, "GDIFF.INVERSE");
+        if (psMetadataLookup(inv->analysis, "PSPHOT.DETECTIONS")) {
+            psMetadataRemoveKey(inv->analysis, "PSPHOT.DETECTIONS");
+        }
+        psFree(viewinv);
+
+        // Close subtraction files and open inverse photometry files
+        if (!gDiffFilesIterateUp(config, GDIFF_FILES_SUB)) {
+            psError(GDIFF_ERR_IO, false, "Unable to close subtraction files.");
+            success = false;
+            goto ESCAPE;
+        }
+
+        if (!gDiffFilesIterateDown(config, GDIFF_FILES_PHOT_INV)) {
+            psError(GDIFF_ERR_IO, false, "Unable to set up inverse files.");
+            success = false;
+            goto ESCAPE;
+        }
+
+        if (!data->quality && !gDiffReadoutPhotometry("GDIFF.INVERSE", data)) {
+            psError(psErrorCodeLast(), false, "Unable to perform photometry.");
+            success = false;
+            goto ESCAPE;
+        }
+
+        // Close inverse subtraction files
+        if (!gDiffFilesIterateUp(config, GDIFF_FILES_INV | GDIFF_FILES_PHOT_INV)) {
+            psError(GDIFF_ERR_IO, false, "Unable to close subtraction files.");
+            success = false;
+            goto ESCAPE;
+        }
+    } else {
+        // Close subtraction files
+	// dumpout(config, "diff.5.fits");
+        if (!gDiffFilesIterateUp(config, GDIFF_FILES_SUB)) {
+            psError(GDIFF_ERR_IO, false, "Unable to close subtraction files.");
+            success = false;
+            goto ESCAPE;
+        }
+    }
+
+ ESCAPE:
+    pmFPAfileDropInternal(config->files, "PSPHOT.BACKGND");
+    pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL");
+    pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL.STDEV");
+
+    return success;
+}
+
+/**** internal functions ****/
 
 bool dumpout(pmConfig *config, char *name) 
@@ -10,327 +383,8 @@
 }
 
-
-bool gDiffLoop(gDiffData *data)
+bool listhdu(pmConfig *config, char *name) 
 {
-    bool mdok = false;
-    bool success = true;
-
-    psAssert(data, "Require processing data");
-    pmConfig *config = data->config;    // Configuration
-    psAssert(config, "Require configuration.");
-
-    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, GDIFF_RECIPE); // Recipe for ppSim
-    psAssert(recipe, "We checked this earlier, so it should be here.");
-    bool noConvolve = psMetadataLookupBool(&mdok, recipe, "NOCONVOLVE"); // Do not use convolved images.
-
-    pmConfigCamerasCull(config, NULL);
-    pmConfigRecipesCull(config, "GDIFF,PPSTATS,PSPHOT,PSASTRO,MASKS,JPEG");
-
-    pmFPAfile *input = psMetadataLookupPtr(NULL, config->files, "GDIFF.INPUT");
-    pmFPAfile *reference = psMetadataLookupPtr(NULL, config->files, "GDIFF.REF");
-    pmFPAfile *output = psMetadataLookupPtr(NULL, config->files, "GDIFF.OUTPUT");
-    psAssert(input && reference && output, "Require files");
-
-    if (!gDiffFilesIterateDown(config, GDIFF_FILES_INPUT | GDIFF_FILES_CONV)) {
-        psError(GDIFF_ERR_IO, false, "Unable to load files.");
-        return false;
-    }
-
-    psTimerStart("GDIFF_MATCH");
-
-    if (!gDiffSetMasks(config)) {
-        psError(psErrorCodeLast(), false, "Unable to set masks.");
-        return false;
-    }
-
-    if (data->forcedPhot1) {
-	bool foundDetections = false;
-	if (!gDiffInputDetections(&foundDetections, "GDIFF.POS1.SOURCES", "GDIFF.INPUT", data)) {
-	    psError(psErrorCodeLast(), false, "Unable to measure positive detections (1)");
-	    success = false;
-            goto ESCAPE;
-	}
-	// if nothing was found, don't bother doing the forced photometry below
-	if (!foundDetections) {
-	    psWarning ("no sources found in positive image 1, skipping forced photometry");
-	    data->forcedPhot1 = false;
-	}
-    }
-    if (data->forcedPhot2) {
-        // Change the recipe to use a higher nsigma limit and quit after pass1
-        psMetadata *psphotRecipe = psMetadataLookupPtr (NULL, config->recipes, PSPHOT_RECIPE);
-
-        psF32 nsigma_peak_save = psMetadataLookupF32 (NULL, psphotRecipe, "PEAKS_NSIGMA_LIMIT");
-        char *breakPt_save =  psMetadataLookupStr (NULL, psphotRecipe, "BREAK_POINT");
-
-        psF32 pos2_nsigma_peak = psMetadataLookupF32 (&mdok, psphotRecipe, "PEAKS_POS2_NSIGMA_LIMIT");
-        if (!mdok) {
-            psWarning("PEAKS_POS2_NSIGMA_LIMIT not found in psphotRecipe. Will use 25.\n");
-            pos2_nsigma_peak = 25.;
-        }
-        psMetadataAddF32(psphotRecipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", pos2_nsigma_peak);
-        psMetadataAddStr(psphotRecipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", "PASS1");
-
-	bool foundDetections = false;
-	if (!gDiffInputDetections(&foundDetections, "GDIFF.POS2.SOURCES", "GDIFF.REF", data)) {
-	    psError(psErrorCodeLast(), false, "Unable to measure positive detections (2)");
-            psMetadataAddF32(psphotRecipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", nsigma_peak_save);
-            psMetadataAddStr(psphotRecipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", breakPt_save);
-	    success = false;
-            goto ESCAPE;
-	}
-        psMetadataAddF32(psphotRecipe, PS_LIST_TAIL, "PEAKS_NSIGMA_LIMIT", PS_META_REPLACE, "", nsigma_peak_save);
-        psMetadataAddStr(psphotRecipe, PS_LIST_TAIL, "BREAK_POINT", PS_META_REPLACE, "", breakPt_save);
-	// if nothing was found, don't bother doing the forced photometry below
-	if (!foundDetections) {
-	    psWarning ("no sources found in positive image 2, skipping forced photometry");
-	    data->forcedPhot2 = false;
-	}
-    }
-
-    // XXX if it exists, use the POS1, POS2 successs for the FWHMs
-    if (!gDiffMatchPSFs(data)) {
-	psError(psErrorCodeLast(), false, "Unable to match PSFs.");
-	success = false;
-	goto ESCAPE;
-    }
-
-    if (data->quality) {
-        // Can't do anything at all
-        success = false;
-        goto ESCAPE;
-    }
-    // generate the residual stamp grid for visualization
-    if (!gDiffResidualSampleJpeg(config)) {
-        psError(psErrorCodeLast(), false, "Unable to update.");
-        success = false;
-        goto ESCAPE;
-    }
-
-    // XXX add in a positive image detection step here (if needed)
-    
-
-    psMetadataAddF32(data->stats, PS_LIST_TAIL, "TIME_MATCH", 0, "Time to match PSFs",
-                     psTimerClear("GDIFF_MATCH"));
-
-    // Close input files (freeing up space) : for the noConvolve case, we cannot close the
-    // inputs since we will use them for subtraction below.  wait until later to do the work
-    if (!noConvolve) {
-	if (!gDiffFilesIterateUp(config, GDIFF_FILES_INPUT)) {
-	    psError(GDIFF_ERR_IO, false, "Unable to close input files.");
-	    success = false;
-	    goto ESCAPE;
-	}
-    }
-    if (!gDiffLowThreshold(data)) {
-        psError(psErrorCodeLast(), false, "Unable to threshold images.");
-        success = false;
-        goto ESCAPE;
-    }
-
-    // Set up subtraction files
-    if (!gDiffFilesIterateDown(config, GDIFF_FILES_SUB)) {
-        psError(GDIFF_ERR_IO, false, "Unable to set up subtraction files.");
-        success = false;
-        goto ESCAPE;
-    }
-
-    if (!gDiffDefineOutput("GDIFF.OUTPUT", config)) {
-        psError(psErrorCodeLast(), false, "Unable to define output.");
-        success = false;
-        goto ESCAPE;
-    }
-
-    if (!data->quality && !gDiffMakePSF(data)) {
-        psError(psErrorCodeLast(), false, "Unable to generate PSF.");
-        success = false;
-        goto ESCAPE;
-    }
-
-    // Now we've got a PSF, blow away detections in case they're confused with real output detections
-    {
-        pmFPAview *view = gDiffViewReadout(); // View to readout
-        pmReadout *out = pmFPAfileThisReadout(config->files, view, "GDIFF.OUTPUT");
-        if (psMetadataLookup(out->analysis, "PSPHOT.DETECTIONS")) {
-            psMetadataRemoveKey(out->analysis, "PSPHOT.DETECTIONS");
-        }
-        psFree(view);
-    }
-
-    if (!gDiffReadoutSubtract(config)) {
-        psError(psErrorCodeLast(), false, "Unable to subtract images.");
-        success = false;
-        goto ESCAPE;
-    }
-    // dumpout(config, "diff.1.fits");
-
-    // Close convolved files
-    if (!gDiffFilesIterateUp(config, GDIFF_FILES_PSF | GDIFF_FILES_CONV)) {
-        psError(GDIFF_ERR_IO, false, "Unable to close input files.");
-        success = false;
-        goto ESCAPE;
-    }
-    // dumpout(config, "diff.2a.fits");
-    if (noConvolve) {
-	if (!gDiffFilesIterateUp(config, GDIFF_FILES_INPUT)) {
-	    psError(GDIFF_ERR_IO, false, "Unable to close input files.");
-	    success = false;
-	    goto ESCAPE;
-	}
-    }
-    
-    // Higher order background subtraction using psphot
-    if (!gDiffBackground(config)) {
-        psError(psErrorCodeLast(), false, "Unable to subtract background.");
-        success = false;
-        goto ESCAPE;
-    }
-    // dumpout(config, "diff.2b.fits");
-
-    // Perform Variance correction (rescale within a modest range)
-    if (!gDiffVarianceRescale(config, data)) {
-        psError(psErrorCodeLast(), false, "Unable to rescale variance.");
-        success = false;
-        goto ESCAPE;
-    }
-    // dumpout(config, "diff.2c.fits");
-
-    if (data->quality) {
-        // Done all we can do up to this point
-        success = false;
-        goto ESCAPE;
-    }
-
-    if (!gDiffFilesIterateDown(config, GDIFF_FILES_PHOT_SUB)) {
-        psError(GDIFF_ERR_IO, false, "Unable to set up photometry files.");
-        success = false;
-        goto ESCAPE;
-    }
-
-    if (!data->quality && !gDiffReadoutPhotometry("GDIFF.OUTPUT", data)) {
-        psError(psErrorCodeLast(), false, "Unable to perform photometry.");
-        success = false;
-        goto ESCAPE;
-    }
-    // dumpout(config, "diff.3.fits");
-
-    // forced photometry for positive image 1
-    if (data->forcedPhot1 && !data->quality) {
-	if (!gDiffReadoutForcedPhot("GDIFF.FORCED1.SOURCES", "GDIFF.OUTPUT", "GDIFF.POS1.SOURCES", data)) {
-	    psError(psErrorCodeLast(), false, "Unable to perform photometry.");
-	    success = false;
-            goto ESCAPE;
-	}
-    }
-
-    // forced photometry for positive image 2
-    if (data->forcedPhot2 && !data->quality) {
-	if (!gDiffReadoutForcedPhot("GDIFF.FORCED2.SOURCES", "GDIFF.OUTPUT", "GDIFF.POS2.SOURCES", data)) {
-	    psError(psErrorCodeLast(), false, "Unable to perform photometry.");
-	    success = false;
-            goto ESCAPE;
-	}
-    }
-
-    if (!gDiffFilesIterateUp(config, GDIFF_FILES_PHOT_SUB)) {
-        psError(GDIFF_ERR_IO, false, "Unable to set up photometry files.");
-        success = false;
-        goto ESCAPE;
-    }
-
-    // Perform statistics on the cell
-    if (!gDiffReadoutStats(data)) {
-        psError(psErrorCodeLast(), false, "Unable to collect statistics");
-        success = false;
-        goto ESCAPE;
-    }
-    // Do Mask Stats
-    {
-	pmFPAview *view = gDiffViewReadout(); // View to readout
-	if (!gDiffMaskStats(config, view,data->stats)) {
-	    psError(psErrorCodeLast(), false, "Unable to generate mask statistics");
-	    success = false;
-	    psFree(view);
-	    goto ESCAPE;
-	}
-	psFree(view);
-    }
-    // dumpout(config, "diff.4.fits");
-    
-    // generate the binned image used to write the jpeg
-    if (!gDiffReadoutJpeg(config)) {
-        psError(psErrorCodeLast(), false, "Unable to update.");
-        success = false;
-        goto ESCAPE;
-    }
-
-    if (data->inverse) {
-        // Set up inverse subtraction files
-        if (!gDiffFilesIterateDown(config, GDIFF_FILES_INV)) {
-            psError(GDIFF_ERR_IO, false, "Unable to set up inverse files.");
-            success = false;
-            goto ESCAPE;
-        }
-
-        if (data->inverse && !gDiffDefineOutput("GDIFF.INVERSE", config)) {
-            psError(psErrorCodeLast(), false, "Unable to define inverse.");
-            success = false;
-            goto ESCAPE;
-        }
-
-        if (!gDiffReadoutInverse(config)) {
-            psError(psErrorCodeLast(), false, "Unable to invert images.");
-            success = false;
-            goto ESCAPE;
-        }
-
-        //MEH -- need to also clear out detections or inv.cmf corrupted on update
-        pmFPAview *viewinv = gDiffViewReadout(); // View to readout
-        pmReadout *inv = pmFPAfileThisReadout(config->files, viewinv, "GDIFF.INVERSE");
-        if (psMetadataLookup(inv->analysis, "PSPHOT.DETECTIONS")) {
-            psMetadataRemoveKey(inv->analysis, "PSPHOT.DETECTIONS");
-        }
-        psFree(viewinv);
-
-        // Close subtraction files and open inverse photometry files
-        if (!gDiffFilesIterateUp(config, GDIFF_FILES_SUB)) {
-            psError(GDIFF_ERR_IO, false, "Unable to close subtraction files.");
-            success = false;
-            goto ESCAPE;
-        }
-
-        if (!gDiffFilesIterateDown(config, GDIFF_FILES_PHOT_INV)) {
-            psError(GDIFF_ERR_IO, false, "Unable to set up inverse files.");
-            success = false;
-            goto ESCAPE;
-        }
-
-        if (!data->quality && !gDiffReadoutPhotometry("GDIFF.INVERSE", data)) {
-            psError(psErrorCodeLast(), false, "Unable to perform photometry.");
-            success = false;
-            goto ESCAPE;
-        }
-
-        // Close inverse subtraction files
-        if (!gDiffFilesIterateUp(config, GDIFF_FILES_INV | GDIFF_FILES_PHOT_INV)) {
-            psError(GDIFF_ERR_IO, false, "Unable to close subtraction files.");
-            success = false;
-            goto ESCAPE;
-        }
-    } else {
-        // Close subtraction files
-	// dumpout(config, "diff.5.fits");
-        if (!gDiffFilesIterateUp(config, GDIFF_FILES_SUB)) {
-            psError(GDIFF_ERR_IO, false, "Unable to close subtraction files.");
-            success = false;
-            goto ESCAPE;
-        }
-    }
-
- ESCAPE:
-    pmFPAfileDropInternal(config->files, "PSPHOT.BACKGND");
-    pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL");
-    pmFPAfileDropInternal(config->files, "PSPHOT.BACKMDL.STDEV");
-
-    return success;
+    pmFPAfile *file = psMetadataLookupPtr(NULL, config->files, name);
+    fprintf (stderr, "HDU: %s, %llx\n", name, (long long unsigned int) file->fpa->hdu);
+    return true;
 }
