Index: trunk/pswarp/src/pswarpCleanup.c
===================================================================
--- trunk/pswarp/src/pswarpCleanup.c	(revision 41525)
+++ trunk/pswarp/src/pswarpCleanup.c	(revision 41526)
@@ -86,4 +86,5 @@
 	}
     }
+
     if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
 	psError(psErrorCodeLast(), false, "Unable to write files.");
Index: trunk/pswarp/src/pswarpDefineLayout.c
===================================================================
--- trunk/pswarp/src/pswarpDefineLayout.c	(revision 41525)
+++ trunk/pswarp/src/pswarpDefineLayout.c	(revision 41526)
@@ -174,5 +174,5 @@
 
     psImageInit(readout->image, NAN);
-    psFree(readout);                // Drop reference
+    psFree(readout);                // Drop reference (saved on cell)
 		
     // copy the image concepts from the skycell 
Index: trunk/pswarp/src/pswarpLoadAstrometry.c
===================================================================
--- trunk/pswarp/src/pswarpLoadAstrometry.c	(revision 41525)
+++ trunk/pswarp/src/pswarpLoadAstrometry.c	(revision 41526)
@@ -150,12 +150,22 @@
 	    }
 	}
-	targetChip->toFPA = psMemIncrRefCounter (chip->toFPA);
-	targetChip->fromFPA = psMemIncrRefCounter (chip->fromFPA);
+	if (targetChip != chip) {
+	  psAssert (!targetChip->toFPA, "oops");
+	  psAssert (!targetChip->fromFPA, "oops");
+	  targetChip->toFPA = psMemIncrRefCounter (chip->toFPA);
+	  targetChip->fromFPA = psMemIncrRefCounter (chip->fromFPA);
+	}
     }
 
     // for pswarpLocalFrame, I need transformations and HDUs on a single fpa (so set on astrom as well as target)
-    target->fpa->toTPA = psMemIncrRefCounter (astrom->fpa->toTPA);
-    target->fpa->fromTPA = psMemIncrRefCounter (astrom->fpa->fromTPA);
-    target->fpa->toSky = psMemIncrRefCounter (astrom->fpa->toSky);
+    // But: do not increment the ref counter if this is the same entry
+    if (target->fpa != astrom->fpa) {
+      psAssert (!target->fpa->toTPA, "oops");
+      psAssert (!target->fpa->fromTPA, "oops");
+      psAssert (!target->fpa->toSky, "oops");
+      target->fpa->toTPA = psMemIncrRefCounter (astrom->fpa->toTPA);
+      target->fpa->fromTPA = psMemIncrRefCounter (astrom->fpa->fromTPA);
+      target->fpa->toSky = psMemIncrRefCounter (astrom->fpa->toSky);
+    }
 
     // reset the type to the original value
Index: trunk/pswarp/src/pswarpLoop.c
===================================================================
--- trunk/pswarp/src/pswarpLoop.c	(revision 41525)
+++ trunk/pswarp/src/pswarpLoop.c	(revision 41526)
@@ -15,5 +15,4 @@
 bool pswarpLoop(pmConfig *config, psMetadata *stats)
 {
-
     // load the recipe
     bool status = false;
Index: trunk/pswarp/src/pswarpOptions.c
===================================================================
--- trunk/pswarp/src/pswarpOptions.c	(revision 41525)
+++ trunk/pswarp/src/pswarpOptions.c	(revision 41526)
@@ -70,4 +70,10 @@
     }
 
+    bool applyPixelNaN = psMetadataLookupBool(&status,recipe, "APPLY.PIXELNAN"); ///< apply NaN value to masked pixels
+    if (!status) {
+	applyPixelNaN = true;
+	psWarning("APPLY.PIXELNAN is not set in the %s recipe -- defaulting to %d.", PSWARP_RECIPE, applyPixelNaN);
+    }
+    
     // BACKGROUND.MODEL gets set in config->arguments (to false) if no input model is found
     bool doBKG = psMetadataLookupBool(&status,config->arguments, "BACKGROUND.MODEL"); ///< Generate the warped background model?
@@ -109,4 +115,5 @@
     psMetadataAddBool(recipe, PS_LIST_TAIL, "PSF", PS_META_REPLACE, "Generate a PSF Model?", PSF);
     psMetadataAddBool(recipe, PS_LIST_TAIL, "BACKGROUND.MODEL", PS_META_REPLACE, "Generate the warped background model?", doBKG);
+    psMetadataAddBool(recipe, PS_LIST_TAIL, "APPLY.PIXELNAN", PS_META_REPLACE, "apply NaN values to bad pixels?", applyPixelNaN);
     psMetadataAddS32(recipe, PS_LIST_TAIL, "BKG.XGRID", PS_META_REPLACE, "Xsize of background model", bkgXgrid);
     psMetadataAddS32(recipe, PS_LIST_TAIL, "BKG.YGRID", PS_META_REPLACE, "Ysize of background model", bkgYgrid);
@@ -120,4 +127,5 @@
     psMetadataAddBool(config->arguments, PS_LIST_TAIL, "PSF", PS_META_REPLACE, "Generate a PSF Model?", PSF);
     psMetadataAddBool(config->arguments, PS_LIST_TAIL, "BACKGROUND.MODEL", PS_META_REPLACE, "Generate the warped background model?", doBKG);
+    psMetadataAddBool(config->arguments, PS_LIST_TAIL, "APPLY.PIXELNAN", PS_META_REPLACE, "apply NaN values to bad pixels?", applyPixelNaN);
     psMetadataAddS32(config->arguments, PS_LIST_TAIL, "BKG.XGRID", PS_META_REPLACE, "Xsize of background model", bkgXgrid);
     psMetadataAddS32(config->arguments, PS_LIST_TAIL, "BKG.YGRID", PS_META_REPLACE, "Ysize of background model", bkgYgrid);
Index: trunk/pswarp/src/pswarpTransformReadout.c
===================================================================
--- trunk/pswarp/src/pswarpTransformReadout.c	(revision 41525)
+++ trunk/pswarp/src/pswarpTransformReadout.c	(revision 41526)
@@ -40,5 +40,5 @@
     // output mask bits
     psImageMaskType maskIn   = 0 ;
-    if(doApplyMaskNaN) {
+    if (doApplyMaskNaN) {
       maskIn   = psMetadataLookupImageMask(&mdok, recipe, "MASK.INPUT");
       psAssert(mdok, "MASK.INPUT was not defined");
Index: trunk/pswarp/src/pswarpTransformTile.c
===================================================================
--- trunk/pswarp/src/pswarpTransformTile.c	(revision 41525)
+++ trunk/pswarp/src/pswarpTransformTile.c	(revision 41526)
@@ -132,5 +132,6 @@
             double imageValue, varValue; // Value of image and variance map
             psImageMaskType maskValue = inMaskData ? inMaskData[(int)yIn][(int)xIn] : 0; // Value of mask
-            if (!psImageInterpolate(&imageValue, &varValue, &maskValue, xIn, yIn, args->interp)) {
+
+	    if (!psImageInterpolate(&imageValue, &varValue, &maskValue, xIn, yIn, args->interp)) {
                 psError(psErrorCodeLast(), false, "Unable to interpolate image.");
                 return false;
