Index: /branches/czw_branch/20120906/pswarp/src/pswarpLoop.c
===================================================================
--- /branches/czw_branch/20120906/pswarp/src/pswarpLoop.c	(revision 34486)
+++ /branches/czw_branch/20120906/pswarp/src/pswarpLoop.c	(revision 34487)
@@ -458,5 +458,4 @@
     bool status;
     bool mdok;                          // Status of MD lookup
-
     const char *skyCamera = psMetadataLookupStr(NULL, config->arguments,
                                                 "SKYCELL.CAMERA");  // Name of camera for skycell
@@ -505,5 +504,4 @@
     }
     psFree (view);
-
     // Turn all skycell files on to generate them, and then turn them off for the loop over the input images
     // the input, which is in a different format.
@@ -519,9 +517,9 @@
         pswarpFileActivation(config, skycellFiles, false);
     }
-
     // Read the input astrometry
     // XXX rather than use the activations here, this should just explicitly loop over the desired filerule
     {
-        pmFPAfileActivate(config->files, true, "PSWARP.ASTROM");
+
+      pmFPAfileActivate(config->files, true, "PSWARP.ASTROM");
 
         pmChip *chip;
@@ -531,6 +529,8 @@
             goto DONE;
         }
+
         while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
-	  psTrace ("pswarp", 4, "AChip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
+#if 0
+	  // This needs to be removed because otherwise it throws an error of duplicate PSPHOT.DETECTIONS.
             if (!chip->process || !chip->file_exists) { continue; }
             if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
@@ -538,7 +538,8 @@
                 goto DONE;
             }
+#endif
             pmCell *cell;
             while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
-                psTrace ("pswarp", 4, "ACell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
+	      psTrace ("pswarp", 4, "ACell %d: %x %x %d\n", view->cell, cell->file_exists, cell->process,psErrorCodeLast());
                 if (!cell->process || !cell->file_exists) { continue; }
                 if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE) ||
@@ -558,11 +559,7 @@
         }
         psFree(view);
-
         pswarpFileActivation(config, detectorFiles, true);
         pmFPAfileActivate(config->files, false, "PSWARP.ASTROM");
     }
-
-/*     // Turn on the source output --- we need to get rid of these so that we can measure the PSF */
-/*     pmFPAfileActivate(config->files, true, "PSWARP.OUTPUT.SOURCES"); */
 
     // Don't care about the skycell anymore --- we've read it, and that's all we need to do.
@@ -574,64 +571,5 @@
     pmHDU *phu = pmFPAviewThisPHU(view, astrom->fpa);
 
-    pmAstromWCS *WCSF = pmAstromWCSfromHeader(phu->header);
-    
-    fprintf(stderr,"WCSF: %g %g : %g %g : %g %g : %g %g %g %g : %g %g\n",
-	    WCSF->crval1,WCSF->crval2,
-	    WCSF->crpix1,WCSF->crpix2,
-	    WCSF->cdelt1,WCSF->cdelt2,
-	    WCSF->trans->x->coeff[1][0],
-	    WCSF->trans->x->coeff[0][1],
-	    WCSF->trans->y->coeff[1][0],
-	    WCSF->trans->y->coeff[0][1],
-	    -1.0,-1.0);
-/* #define CORRECT_INPUT_WCS 1 */
-/* #if CORRECT_INPUT_WCS */
-/* 	// Correct the input WCS */
-/* 	pmAstromWCS *WCS = pmAstromWCSfromHeader(phu->header); */
-
-/* 	double cd1f = 1.0 * 400; */
-/* 	double cd2f = 1.0 * 400; */
-/* 	fprintf(stderr,"WCS: %g %g : %g %g : %g %g : %g %g %g %g : %g %g\n", */
-/* 		WCS->crval1,WCS->crval2, */
-/* 		WCS->crpix1,WCS->crpix2, */
-/* 		WCS->cdelt1,WCS->cdelt2, */
-/* 		WCS->trans->x->coeff[1][0], */
-/* 		WCS->trans->x->coeff[0][1], */
-/* 		WCS->trans->y->coeff[1][0], */
-/* 		WCS->trans->y->coeff[0][1], */
-/* 		cd1f,cd2f); */
-
-/* 	WCS->crpix1 = WCS->crpix1 / cd1f; */
-/* 	WCS->crpix2 = WCS->crpix2 / cd2f; */
-
-/* 	WCS->cdelt1 *= cd1f; */
-/* 	WCS->cdelt2 *= cd2f; */
-
-/* 	// WCS->trans->x->nX/nY */
-/* 	for (int q = 0; q < WCS->trans->x->nX; q++) { */
-/* 	  for (int r = 0; r < WCS->trans->x->nY; r++) { */
-/* 	    WCS->trans->x->coeff[q][r] *= cd1f; */
-/* 	  } */
-/* 	} */
-/* 	for (int q = 0; q < WCS->trans->y->nX; q++) { */
-/* 	  for (int r = 0; r < WCS->trans->y->nY; r++) { */
-/* 	    WCS->trans->y->coeff[q][r] *= cd2f; */
-/* 	  } */
-/* 	} */
-/* 	fprintf(stderr,"WCS: %g %g : %g %g : %g %g : %g %g %g %g : %g %g\n", */
-/* 		WCS->crval1,WCS->crval2, */
-/* 		WCS->crpix1,WCS->crpix2, */
-/* 		WCS->cdelt1,WCS->cdelt2, */
-/* 		WCS->trans->x->coeff[1][0], */
-/* 		WCS->trans->x->coeff[0][1], */
-/* 		WCS->trans->y->coeff[1][0], */
-/* 		WCS->trans->y->coeff[0][1], */
-/* 		cd1f,cd2f); */
-/* 	pmAstromWCStoHeader (phu->header,WCS); */
-/* 	// End WCS work. */
-/* #endif */
-
-
-    
+    //    pmAstromWCS *WCSF = pmAstromWCSfromHeader(phu->header);
     
     if (phu) {
@@ -649,6 +587,4 @@
         }
     }
-
-
 
     psList *cells = psListAlloc(NULL);  // List of cells, for concepts averaging
@@ -676,14 +612,4 @@
 	double cd1f = 1.0 * 400;
 	double cd2f = 1.0 * 400;
-	fprintf(stderr,"WCSC: %g %g : %g %g : %g %g : %g %g %g %g : %g %g\n",
-		WCS->crval1,WCS->crval2,
-		WCS->crpix1,WCS->crpix2,
-		WCS->cdelt1,WCS->cdelt2,
-		WCS->trans->x->coeff[1][0],
-		WCS->trans->x->coeff[0][1],
-		WCS->trans->y->coeff[1][0],
-		WCS->trans->y->coeff[0][1],
-		cd1f,cd2f);
-
 
 	WCS->cdelt1 *= cd1f;
@@ -695,27 +621,12 @@
 	for (int q = 0; q <= WCS->trans->x->nX; q++) {
 	  for (int r = 0; r <= WCS->trans->x->nY; r++) {
-	    fprintf(stderr,"%d %d %g\n",q,r,WCS->trans->x->coeff[q][r]);
 	    WCS->trans->x->coeff[q][r] *= pow(cd1f,q) * pow(cd2f,r);
-	    fprintf(stderr,"%d %d %g\n",q,r,WCS->trans->x->coeff[q][r]);
 	  }
 	}
 	for (int q = 0; q <= WCS->trans->y->nX; q++) {
 	  for (int r = 0; r <= WCS->trans->y->nY; r++) {
-	    fprintf(stderr,"%d %d %g\n",q,r,WCS->trans->y->coeff[q][r]);
 	    WCS->trans->y->coeff[q][r] *= pow(cd1f,q) * pow(cd2f,r);
-	    fprintf(stderr,"%d %d %g\n",q,r,WCS->trans->y->coeff[q][r]);
 	  }
 	}
-	fprintf(stderr,"WCSO: %g %g : %g %g : %g %g : %g %g %g %g : %g %g\n",
-		WCS->crval1,WCS->crval2,
-		WCS->crpix1,WCS->crpix2,
-		WCS->cdelt1,WCS->cdelt2,
-		WCS->trans->x->coeff[1][0],
-		WCS->trans->x->coeff[0][1],
-		WCS->trans->y->coeff[1][0],
-		WCS->trans->y->coeff[0][1],
-		cd1f,cd2f);
-
-	fprintf(stderr,"Size: %d %d %d %d\n",WCS->trans->x->nX,WCS->trans->x->nY,WCS->trans->y->nX,WCS->trans->y->nY);
 	pmAstromWCStoHeader (hdu->header,WCS);
 	
@@ -761,12 +672,4 @@
                 }
 
-                // Copy the detections from the astrometry carrier to the input, so they can be accessed by
-                // pswarpTransformReadout
-/*                 pmReadout *astromRO = pmFPAviewThisReadout(view, astrom->fpa); // Readout for astrometry */
-/*                 pmDetections *detections = psMetadataLookupPtr(&mdok, astromRO->analysis, "PSPHOT.DETECTIONS"); // Sources from astrometry */
-/*                 if (detections) { */
-/*                     psMetadataAddPtr(readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_DATA_ARRAY | PS_META_REPLACE, "Sources from input astrometry", detections); */
-/*                 } */
-		
 		for (int x = 0; x < readout->image->numCols; x++) {
 		  for (int y = 0; y < readout->image->numRows; y++) {
@@ -778,5 +681,4 @@
 		
 		psMetadataAddS32(config->arguments,PS_LIST_TAIL, "INTERPOLATION.MODE", PS_META_REPLACE, "", 8);
-
 		fprintf(stderr,"Transforming Readout!\n");
 
@@ -784,5 +686,4 @@
                 pswarpTransformReadout(output, readout, config);
 		psMetadataAddBool(config->arguments,PS_LIST_TAIL, "BACKGROUND_WARPING", PS_META_REPLACE, "", false);
-		
                 if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
                     psError(psErrorCodeLast(), false, "Unable to write files.");
@@ -800,11 +701,7 @@
         }
     }
-
+    fprintf(stderr,"End readout transformations!\n");
     if (!output->data_exists) {
         psWarning("No overlap between input and skycell.");
-/*         if (stats) { */
-/*             psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", PS_META_REPLACE, */
-/*                              "No overlap between input and skycell", PSWARP_ERR_NO_OVERLAP); */
-/*         } */
         psphotFilesActivate(config, false);
         psFree(cells);
@@ -812,5 +709,4 @@
         goto DONE;
     }
-    
     pmCell *outCell = output->parent;   ///< Output cell
     pmChip *outChip = outCell->parent;  ///< Output chip
@@ -823,43 +719,4 @@
         goto DONE;
     }
-/*     bool doStats = psMetadataLookupBool(&mdok,recipe,"MASK.STATS"); */
-/*     if (doStats) { */
-/*       if (!pswarpMaskStats(output, stats, config)) { */
-/* 	psError(psErrorCodeLast(), false, "Unable to calculate mask stats."); */
-/* 	psFree(cells); */
-/* 	psFree(view); */
-/* 	goto DONE; */
-/*       } */
-/*     } */
-/*     // Set covariance matrix for output */
-/*     { */
-/*         psList *covariances = psMetadataLookupPtr(&mdok, output->analysis, */
-/*                                                   PSWARP_ANALYSIS_COVARIANCES); // Covariance matrices */
-/*         psAssert(covariances, "Should be there"); */
-/*         psArray *covars = psListToArray(covariances); // Array of covariance matrices */
-/*         psKernel *covar = psImageCovarianceAverage(covars); */
-/*         psFree(covars); */
-/*         psMetadataRemoveKey(output->analysis, PSWARP_ANALYSIS_COVARIANCES); */
-
-/*         // Correct covariance matrix scale for the mean (square root of the) Jacobian */
-/*         double jacobian = psMetadataLookupF64(NULL, output->analysis, PSWARP_ANALYSIS_JACOBIAN); // Jacobian */
-/*         int goodPixels = psMetadataLookupS32(NULL, output->analysis, PSWARP_ANALYSIS_GOODPIX);   // Good pixels */
-/*         jacobian /= goodPixels; */
-/*         output->covariance = psImageCovarianceScale(covar, jacobian); */
-/*         psFree(covar); */
-
-/*         if (output->variance) { */
-/*             psImageCovarianceTransfer(output->variance, output->covariance); */
-/*         } */
-/*     } */
-
-/*     if (!pmConceptsAverageCells(outCell, cells, NULL, NULL, false)) { */
-/*         psError(psErrorCodeLast(), false, "Unable to average cell concepts."); */
-/*         psFree(stats); */
-/*         psFree(cells); */
-/*         psFree(view); */
-/*         goto DONE; */
-/*     } */
-/*     psFree(cells); */
 
     psRegion *trimsec = psMetadataLookupPtr(NULL, outCell->concepts, "CELL.TRIMSEC"); ///< Trim section
@@ -872,10 +729,4 @@
         goto DONE;
     }
-
-    // Update ZP from the astrometry
-/*     { */
-/*         psMetadataItem *item = psMetadataLookup(outFPA->concepts, "FPA.ZP"); */
-/*         item->data.F32 = psMetadataLookupF32(NULL, astrom->fpa->concepts, "FPA.ZP"); */
-/*     } */
 
     pmHDU *hdu = outFPA->hdu;           ///< HDU for the output warped image
@@ -895,5 +746,4 @@
         hdu->header = psMetadataCopy(hdu->header, skyHDU->header);
     }
-
     pswarpVersionHeader(hdu->header);
     
@@ -903,88 +753,11 @@
         goto DONE;
     }
-
     if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
         psError(psErrorCodeLast(), false, "Unable to write files.");
         goto DONE;
     }
-
     // Done with the detector side of things
     pswarpFileActivation(config, detectorFiles, false);
     pswarpFileActivation(config, independentFiles, false);
-
-
-    // We need a new PSF model for the warped frame.  It would be good to generate this analytically, but
-    // that's going to be tricky.  We have a list of sources, so we use those to redetermine the PSF model.
-
-/*     if (psMetadataLookupBool(&mdok, recipe, "PSF")) { */
-/*         pswarpFileActivation(config, photFiles, true); */
-/*         if (!pswarpIOChecksBefore(config)) { */
-/*             psError(psErrorCodeLast(), false, "Unable to read files."); */
-/*             goto DONE; */
-/*         } */
-
-/*         // supply the readout and fpa of interest to psphot */
-/*         pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT"); */
-/*         pmFPACopy(photFile->fpa, outFPA); */
-
-/*         pmFPAview *view = pmFPAviewAlloc(0); ///< View into skycell */
-/*         view->chip = view->cell = view->readout = 0; */
-
-/*         // grab the sources of interest from the storage location (pmFPAfile PSPHOT.INPUT.CMF) */
-/*         psArray *sources = psphotLoadPSFSources (config, view); */
-/*         if (!sources) { */
-/*             psError(psErrorCodeLast(), false, "No sources supplied to measure PSF"); */
-/*             goto DONE; */
-/*         } */
-
-/*         pmModelClassSetLimits(PM_MODEL_LIMITS_STRICT); */
-
-/*         // measure the PSF using these sources */
-/*         if (!psphotReadoutFindPSF(config, view, "PSPHOT.INPUT", sources)) { */
-/*             // This is likely a data quality issue */
-/*             // XXX Split into multiple cases using error codes? */
-/*             psErrorStackPrint(stderr, "Unable to determine PSF"); */
-/*             psWarning("Unable to determine PSF --- suspect bad data quality."); */
-/*             if (stats && psMetadataLookupS32(NULL, stats, "QUALITY") == 0) { */
-/*                 psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", PS_META_REPLACE, */
-/*                                  "Unable to determine PSF", psErrorCodeLast()); */
-/*             } */
-/*             psErrorClear(); */
-/*             psphotFilesActivate(config, false); */
-/*         } */
-
-/*         // Ensure seeing is carried over */
-/*         pmChip *photChip = pmFPAviewThisChip(view, photFile->fpa);                 // Chip with seeing */
-/*         psMetadataItem *item = psMetadataLookup(outChip->concepts, "CHIP.SEEING"); // Concept with seeing */
-/*         item->data.F32 = psMetadataLookupF32(NULL, photChip->concepts, "CHIP.SEEING"); */
-
-/* // XXX EAM : put this in a visualization function */
-/* #if (TESTING) */
-/*         { */
-/*             #define PSF_SIZE 20         ///< Half-size of PSF */
-/*             #define PSF_FLUX 10000      ///< Central flux for PSF */
-/*             pmChip *photChip = pmFPAviewThisChip(view, photFile->fpa); */
-/*             pmPSF *psf = psMetadataLookupPtr(NULL, photChip->analysis, "PSPHOT.PSF"); */
-/*             psImage *image = psImageAlloc(2 * PSF_SIZE + 1, 2 * PSF_SIZE + 1, PS_TYPE_F32); */
-/*             psImageInit(image, 0); */
-/*             pmModel *model = pmModelFromPSFforXY(psf, PSF_SIZE, PSF_SIZE, PSF_FLUX); */
-/*             pmModelAdd(image, NULL, model, PM_MODEL_OP_FULL, 0); */
-/*             psFree(model); */
-/*             psFits *fits = psFitsOpen("psf.fits", "w"); */
-/*             psFitsWriteImage(fits, NULL, image, 0, NULL); */
-/*             psFitsClose(fits); */
-/*             psFree(image); */
-/*         } */
-/* #endif */
-
-/*         psFree(view); */
-/*     } */
-
-/*     // Perform statistics on the output image */
-/*     if (stats) { */
-/*         if (!ppStatsFPA(stats, output->parent->parent->parent, view, maskValue, config)) { */
-/*             psWarning("Unable to perform statistics on warped image."); */
-/*         } */
-/*     } */
     
 
@@ -1002,5 +775,4 @@
     psFree(headerName);
     psFree(view);
-
  DONE:
 
Index: /branches/czw_branch/20120906/pswarp/src/pswarpTransformReadout.c
===================================================================
--- /branches/czw_branch/20120906/pswarp/src/pswarpTransformReadout.c	(revision 34486)
+++ /branches/czw_branch/20120906/pswarp/src/pswarpTransformReadout.c	(revision 34487)
@@ -202,8 +202,10 @@
 
     if (goodPixels > 0 && psMetadataLookupBool(&mdok, recipe, "SOURCES")) {
+      if (!psMetadataLookupBool(NULL,config->arguments,"BACKGROUND_WARPING")) {
         if (!pswarpTransformSources(output, input, config)) {
-            psError(psErrorCodeLast(), false, "Unable to interpolate image.");
-            return false;
+	  psError(psErrorCodeLast(), false, "Unable to interpolate image.");
+	  return false;
         }
+      }
     }
 
