Index: trunk/ppSub/src/ppSubCamera.c
===================================================================
--- trunk/ppSub/src/ppSubCamera.c	(revision 42096)
+++ trunk/ppSub/src/ppSubCamera.c	(revision 42279)
@@ -177,5 +177,7 @@
     altconfig->user   = psMemIncrRefCounter(config->user);   // inherit from primary camera
     altconfig->site   = psMemIncrRefCounter(config->site);   // inherit from primary camera
-    altconfig->system = psMemIncrRefCounter(config->system); // inherit from primary camera
+
+    psFree (altconfig->system);
+    altconfig->system = psMetadataCopy(NULL, config->system); // container for camera-specific recipe values (to be dropped)
 
     psFree (altconfig->files);
@@ -188,8 +190,7 @@
     altconfig->recipes = psMetadataCopy(NULL, config->recipes); // container for camera-specific recipe values (to be dropped)
 
-# if (0)
-    // XXX ppStack seems to work without removing PSCAMERA
-    // remove PSCAMERA entries from *-SKYCELL cameras
-    // XXX make this optional to force matching cameras if desired?
+    // remove PSCAMERA entries from *-SKYCELL cameras.  This allows skycell images from
+    // one camera to match those of another camera.  XXX Make this optional to force
+    // matching cameras if desired?
     bool mdok = false;
     psMetadata *cameras = psMetadataLookupMetadata(&mdok, altconfig->system, "CAMERAS");
@@ -229,7 +230,23 @@
       psMetadataRemoveKey (rule, "PSCAMERA"); // allow any camera skycell to match
     }
-# endif
+    psFree (camerasIter);
 
     return (altconfig);
+}
+
+bool ppSubCopyPSCamera (pmFPAfile *tgt, pmFPAfile *src) {
+
+    bool success;
+    psMetadata *ruleSrc = psMetadataLookupMetadata(&success, src->format, "RULE"); // one true format for skycells
+    psAssert (ruleSrc, "missing RULE in src->format");
+
+    psString pscameraSrc = psMetadataLookupStr(&success, ruleSrc, "PSCAMERA"); // one true format for skycells
+    psAssert (pscameraSrc, "missing PSCAMERA in src file"); 
+
+    psMetadata *ruleTgt = psMetadataLookupMetadata(&success, tgt->format, "RULE"); // one true format for skycells
+    psAssert (ruleTgt, "missing RULE in tgt->format");
+
+    psMetadataAddStr (ruleTgt, PS_LIST_TAIL, "PSCAMERA", PS_META_REPLACE, "", pscameraSrc);
+    return true;
 }
 
@@ -271,24 +288,16 @@
     // different camera than the input image.  NOTE: there is no check that these two
     // images match in terms of size, pixel scale, etc. That is up to the user.
+    // The temporary config structure has PSCAMERA entries removed from the SKYCELL rules
+    // to allow subtraction between cameras.
     
     pmConfig *refconfig = pmConfigMakeTemp(config);
 
-# if (0)
-    // EAM TEST: can we bind the ref to the input to force the right skycell format?
-    // Reference image
-    pmFPAfile *ref = defineInputFile(&success, config, input, "PPSUB.REF", "REF", PM_FPA_FILE_IMAGE);
-    if (!success) {
-        psError(psErrorCodeLast(), false, "Failed to build FPA from PPSUB.REF");
-        return false;
-    }
-# else
     // Reference image
     pmFPAfile *ref = defineInputFile(&success, refconfig, NULL, "PPSUB.REF", "REF", PM_FPA_FILE_IMAGE);
     if (!success) {
-        psError(psErrorCodeLast(), false, "Failed to build FPA from PPSUB.REF");
-        return false;
-    }
-# endif
-
+	psError(psErrorCodeLast(), false, "Failed to build FPA from PPSUB.REF");
+	return false;
+    }
+    // Reference mask
     defineInputFile(&success, refconfig, ref, "PPSUB.REF.MASK", "REF.MASK", PM_FPA_FILE_MASK);
     if (!success) {
@@ -296,5 +305,5 @@
         return false;
     }
-
+    // Reference variance
     pmFPAfile *refVar = defineInputFile(&success, refconfig, ref, "PPSUB.REF.VARIANCE", "REF.VARIANCE", PM_FPA_FILE_VARIANCE);
     if (!success) {
@@ -302,4 +311,6 @@
         return false;
     }
+    // copy ref->format(RULE) PSCAMERA from input->format(RULE)
+    ppSubCopyPSCamera (ref, input);
     psFree (refconfig);
 
@@ -308,9 +319,12 @@
     pmConfig *srcconfig = pmConfigMakeTemp(config);
 
-    defineInputFile(&success, srcconfig, NULL, "PPSUB.REF.SOURCES", "REF.SOURCES", PM_FPA_FILE_CMF);
-    if (!success) {
-        psError(psErrorCodeLast(), false, "Failed to build FPA from PPSUB.REF.SOURCES");
-        return false;
-    }
+    // Sources input file (CMF)
+    pmFPAfile *src = defineInputFile(&success, srcconfig, NULL, "PPSUB.REF.SOURCES", "REF.SOURCES", PM_FPA_FILE_CMF);
+    if (!success) {
+	psError(psErrorCodeLast(), false, "Failed to build FPA from PPSUB.REF.SOURCES");
+	return false;
+    }
+    // XXX do we need to copy the PSCAMERA across to src?  
+    ppSubCopyPSCamera (src, input);
     psFree (srcconfig);
     
