Index: trunk/psphot/src/psphotStackParseCamera.c
===================================================================
--- trunk/psphot/src/psphotStackParseCamera.c	(revision 33690)
+++ trunk/psphot/src/psphotStackParseCamera.c	(revision 33913)
@@ -129,4 +129,9 @@
 	}
 
+        psS64 stack_id = psMetadataLookupS64(&status, input, "STACK_ID");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to find STACK_ID from sources %d", i);
+            return false;
+        }
 	// generate an pmFPAimage for the output convolved image
 	// XXX output of these files should be optional
@@ -139,5 +144,5 @@
 	    }
 	    outputImage->save = true;
-	    outputImage->fileID = i;		// this is used to generate output names
+	    outputImage->fileID = stack_id;		// this is used to generate output names
 
 	    pmFPAfile *outputMask = pmFPAfileDefineOutput(config, outputImage->fpa, "PSPHOT.STACK.OUTPUT.MASK");
@@ -151,5 +156,5 @@
 	    }
 	    outputMask->save = true;
-	    outputMask->fileID = i;		// this is used to generate output names
+	    outputMask->fileID = stack_id;		// this is used to generate output names
 
 	    pmFPAfile *outputVariance = pmFPAfileDefineOutput(config, outputImage->fpa, "PSPHOT.STACK.OUTPUT.VARIANCE");
@@ -163,5 +168,5 @@
 	    }
 	    outputVariance->save = true;
-	    outputVariance->fileID = i;		// this is used to generate output names
+	    outputVariance->fileID = stack_id;		// this is used to generate output names
 
 	    // the output sources are carried on the outputImage->fpa structures
@@ -172,5 +177,5 @@
 	    }
 	    outsources->save = true;
-	    outsources->fileID = i;		// this is used to generate output names
+	    outsources->fileID = stack_id;		// this is used to generate output names
 	}
     }
