Index: /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycell.h
===================================================================
--- /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycell.h	(revision 36367)
+++ /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycell.h	(revision 36368)
@@ -10,5 +10,4 @@
 typedef struct {
     psString imagesName;                // Filename with images
-    psString masksName;                 // Filename with masks
     psString wcsrefName;                // Filename with WCS references
     psString outRoot;                   // Output root name
@@ -17,5 +16,6 @@
     int bin1, bin2;                     // Binning factors
     pmConfig *config;                   // Configuration
-  bool doFits;                          // hold whether to do fits as well.
+    bool doFits;                        // hold whether to do fits as well.
+    int  exptimeOrder;                  // Order of exptime scaling.
 } ppSkycellData;
 
Index: /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellArguments.c
===================================================================
--- /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellArguments.c	(revision 36367)
+++ /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellArguments.c	(revision 36368)
@@ -29,5 +29,5 @@
 {
     fprintf(stderr, "\nPan-STARRS skycell JPEGifier\n\n");
-    fprintf(stderr, "Usage: %s -images INPUT.list [-masks MASK.list] OUTPUT_ROOT\n\n",
+    fprintf(stderr, "Usage: %s -images INPUT.list OUTPUT_ROOT\n\n",
             program);
     fprintf(stderr, "\n");
@@ -50,6 +50,6 @@
     psMetadata *arguments = psMetadataAlloc(); // Command-line arguments
     psMetadataAddStr(arguments, PS_LIST_TAIL, "-images", 0, "Filename with input images", NULL);
-    psMetadataAddStr(arguments, PS_LIST_TAIL, "-masks", 0, "Filename with input masks", NULL);
     psMetadataAddStr(arguments, PS_LIST_TAIL, "-wcsref", 0, "Filename with WCS references", NULL);
+    psMetadataAddS16(arguments, PS_LIST_TAIL, "-exptimeOrder", 0, "Order of exptime scaling", 0);
     if (argc == 1 || !psArgumentParse(arguments, &argc, argv) || argc != 2) {
         usage(argv[0], arguments, data);
@@ -57,6 +57,6 @@
 
     data->imagesName = psMemIncrRefCounter(psMetadataLookupStr(NULL, arguments, "-images"));
-    data->masksName = psMemIncrRefCounter(psMetadataLookupStr(NULL, arguments, "-masks"));
     data->wcsrefName = psMemIncrRefCounter(psMetadataLookupStr(NULL, arguments, "-wcsref"));
+    data->exptimeOrder = psMetadataLookupS16(NULL,arguments,"-exptimeOrder");
     
     data->outRoot = psStringCopy(argv[1]);
Index: /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellCamera.c
===================================================================
--- /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellCamera.c	(revision 36367)
+++ /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellCamera.c	(revision 36368)
@@ -56,21 +56,4 @@
     data->numInputs = images->n;
 
-    psArray *masks = NULL;              // Mask names
-    if (data->masksName) {
-        masks = fileList(data->masksName);
-        if (!masks) {
-            psError(psErrorCodeLast(), false, "No masks provided.");
-            psFree(images);
-            return false;
-        }
-        if (masks->n != data->numInputs) {
-            psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Number of images (%ld) and masks (%ld) do not match",
-                    images->n, masks->n);
-            psFree(images);
-            psFree(masks);
-            return false;
-        }
-    }
-
     psArray *wcsref = NULL;             // Names of WCS reference images
     if (data->wcsrefName) {
@@ -79,5 +62,4 @@
 	psError(psErrorCodeLast(), false, "No WCSrefs provided.");
 	psFree(images);
-	psFree(masks);
 	return(false);
       }
@@ -86,5 +68,4 @@
 		images->n, wcsref->n);
 	psFree(images);
-	psFree(masks);
 	psFree(wcsref);
 	return(false);
@@ -104,13 +85,4 @@
         }
 
-        if (data->masksName) {
-            fileArguments("MASK", masks->data[i], "Name of the mask", data->config);
-            if (!pmFPAfileBindFromArgs(&status, image, data->config, "PPSKYCELL.MASK", "MASK") || !status) {
-                psError(PS_ERR_IO, false, "Failed to build file from PPSKYCELL.MASK");
-                // XXX Cleanup
-                return false;
-            }
-        }
-
 	if (data->wcsrefName) {
 	  fileArguments("WCSREF", wcsref->data[i], "Name of the WCS reference", data->config);
@@ -120,10 +92,5 @@
 	    return(false);
 	  }
-/* 	  if (!pmFPAfileBindFromArgs(&status, image, data->config, "PPSKYCELL.WCSREF", "WCSREF") || !status) { */
-/* 	    psError(PS_ERR_IO, false, "Failed to build file from PPSKYCELL.WCSREF"); */
-/* 	    return false; */
-/* 	  } */
 	}
-	
     }
 
@@ -145,18 +112,4 @@
         psError(psErrorCodeLast(), false, "Unable to define output.");
         return false;
-    }
-    if (data->masksName) {
-      pmFPAfile *mask1 = pmFPAfileDefineOutput(data->config, bin1->fpa , "PPSKYCELL.BIN1.MASK");
-      if (!mask1) {
-        psError(psErrorCodeLast(), false, "Unable to define output.");
-        return false;
-      }
-      mask1->save = true;
-      pmFPAfile *mask2 = pmFPAfileDefineOutput(data->config, bin2->fpa , "PPSKYCELL.BIN2.MASK");
-      if (!mask2) {
-        psError(psErrorCodeLast(), false, "Unable to define output.");
-        return false;
-      }
-      mask2->save = true;
     }
 
Index: /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellData.c
===================================================================
--- /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellData.c	(revision 36367)
+++ /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellData.c	(revision 36368)
@@ -14,5 +14,4 @@
 {
     psFree(data->imagesName);
-    psFree(data->masksName);
     psFree(data->wcsrefName);
     psFree(data->outRoot);
@@ -28,5 +27,4 @@
 
     data->imagesName = NULL;
-    data->masksName = NULL;
     data->wcsrefName = NULL;
     data->outRoot = NULL;
@@ -36,5 +34,5 @@
     data->bin2 = 0;
     data->config = NULL;
-
+    data->exptimeOrder = 0;
     return data;
 }
Index: /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellLoop.c
===================================================================
--- /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellLoop.c	(revision 36367)
+++ /branches/eam_branches/ipp-20130904/ppSkycell/src/ppSkycellLoop.c	(revision 36368)
@@ -11,51 +11,4 @@
 #define BUFFER 16                       // Size of buffer for projections
 
-#if (0)
-// This is all junk that is basically useless, because it calculates things we already know.
-
-static void regionMinMax(psRegion *base,// Base region; modified
-                         const psRegion *compare // Comparison region
-                         )
-{
-    base->x0 = PS_MAX(base->x0, compare->x0);
-    base->x1 = PS_MIN(base->x1, compare->x1);
-    base->y0 = PS_MIN(base->y0, compare->y0);
-    base->y1 = PS_MAX(base->y1, compare->y1);
-    return;
-}
-
-
-static psRegion *skycellRegion(pmAstromWCS *wcs, // World Coordinate System
-                               int numCols, int numRows // Size of image
-    )
-{
-    psPlane *fromCoords = psPlaneAlloc(), *toCoords = psPlaneAlloc(); // Coordinates for transforms
-    psRegion *region = psRegionAlloc(-INFINITY, INFINITY, INFINITY, -INFINITY); // Region for skycell
-
-// Limit the region using the nominated point (X,Y)
-#define REGION_RANGE(X,Y) \
-    fromCoords->x = (X); \
-    fromCoords->y = (Y); \
-    psPlaneTransformApply(toCoords, wcs->trans, fromCoords); \
-    printf("%f %f %f %f\n",toCoords->x,toCoords->y,fromCoords->x,fromCoords->y); \
-    toCoords->x /= wcs->cdelt1; \
-    toCoords->y /= wcs->cdelt2; \
-    toCoords->x -= wcs->crpix1;		\
-    toCoords->y += wcs->crpix2;		\
-    region->x0 = PS_MAX(region->x0, toCoords->x); \
-    region->x1 = PS_MIN(region->x1, toCoords->x); \
-    region->y0 = PS_MIN(region->y0, toCoords->y); \
-    region->y1 = PS_MAX(region->y1, toCoords->y);
-    //
-    REGION_RANGE(0, 0);                 // Lower left
-    psTrace("ppSkycell", 9, "Start: %.0f %.0f", toCoords->x, toCoords->y);
-    REGION_RANGE(0, numRows);           // Upper left
-    REGION_RANGE(numCols, 0);           // Lower right
-    REGION_RANGE(numCols, numRows);     // Upper right
-    psTrace("ppSkycell", 9, "Stop: %.0f %.0f\n", toCoords->x, toCoords->y);
-    psTrace("ppSkycell", 7, "%g %g %g %g\n",wcs->cdelt1,wcs->cdelt2,wcs->crpix1,wcs->crpix2);
-    return region;
-}
-#endif
 
 // Activate/deactivate a single element for a list
@@ -139,14 +92,11 @@
     psArray *imageRegions = psArrayAlloc(data->numInputs); // Region for image
     psArray *regionHDUs = psArrayAlloc(data->numInputs);
+
+    psVector *exptimes = psVectorAlloc(data->numInputs, PS_TYPE_F64);
+    
     // Determine which projection cells we have to deal with.
     for (int i = 0; i < data->numInputs; i++) {
         pmFPAfile *file = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.IMAGE", i); // File to examine
         // Header in the FPA should have been read as a part of defining the file...
-#if 0
-        bool pmFPAReadHeaderSet(pmFPA *fpa,     // FPA to read into
-                                psFits *fits,   // FITS file from which to read
-                                pmConfig *config // Configuration
-            );
-#endif
         pmHDU *hdu = file->fpa->hdu;    // Header of interest
 
@@ -187,10 +137,11 @@
             }
         }
-
-        if (!found) {
+	
+        if (!found) { // Add new projection cell if we didn't find one.
             psVectorAppend(crval1, wcs->crval1);
             psVectorAppend(crval2, wcs->crval2);
             psVectorAppend(cdelt1, wcs->cdelt1);
             psVectorAppend(cdelt2, wcs->cdelt2);
+
             psRegion *projRegion = psRegionAlloc(region->x0, region->x1, region->y0, region->y1);
             psArrayAdd(projRegions, projRegions->n, projRegion);
@@ -200,36 +151,44 @@
 	    psArrayAdd(regionHDUs,1,hdu);
             numProj++;
+
         }
-    }
-
+
+	// Save exptime
+	exptimes->data.F64[i] = psMetadataLookupF64(NULL, hdu->header, "EXPTIME");
+    }
+
+    float exptime_target = 1.0;
+
+    //    psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN);
+    //    psVectorStats(stats,exptimes,NULL, NULL, 0);
+    //    exptime_target = stats->robustMedian;
+    
     pmFPAfileActivate(data->config->files, false, NULL);
     // Loop over projections
     for (int i = 0; i < numProj; i++) {
         psRegion *projRegion = projRegions->data[i]; // Region for skycell projection
-	//	projRegion->x0 += 5760;
-	//projRegion->x1 += 5760;
-	//	projRegion->x0 += 
-	//	projRegion->x1 += 
         psTrace("ppSkycell", 2, "Projection %d: [%.0f:%.0f,%.0f:%.0f]\n",
                 i, projRegion->x0, projRegion->x1, projRegion->y0, projRegion->y1);
-        int xSize = projRegion->x1 - projRegion->x0 + 1; // Size of unbinned image
-        int ySize = projRegion->y1 - projRegion->y0 + 1; // Size of unbinned image
-        // Size of binned image 1
+
+	// Size of unbinned image
+        int xSize = projRegion->x1 - projRegion->x0 + 1; 
+        int ySize = projRegion->y1 - projRegion->y0 + 1; 
+
+        // Size of binned images
         int numCols1 = xSize / (float)data->bin1 + 1.5, numRows1 = ySize / (float)data->bin1 + 1.5;
-        // Size of binned image 2
         int numCols2 = numCols1 / (float)data->bin2 + 1.5, numRows2 = numRows1 / (float)data->bin2 + 1.5;
 
-        psImage *image1 = psImageAlloc(numCols1, numRows1, PS_TYPE_F32); // Binned image
-        psImage *image2 = psImageAlloc(numCols2, numRows2, PS_TYPE_F32); // Binned image
+	// Binned image containers
+        psImage *image1 = psImageAlloc(numCols1, numRows1, PS_TYPE_F32); 
+        psImage *image2 = psImageAlloc(numCols2, numRows2, PS_TYPE_F32); 
         psImageInit(image1,NAN);
         psImageInit(image2,NAN);
+
+	// Binned image radius values.  Used to determine primacy.
+	psImage *radius1 = psImageAlloc(numCols1, numRows1, PS_TYPE_F32);
+	psImage *radius2 = psImageAlloc(numCols2, numRows2, PS_TYPE_F32);
+	psImageInit(radius1,pow(numCols1 + numRows1,2)); // These values can be anything, just need to be larger than a binned radius.
+	psImageInit(radius2,pow(numCols1 + numRows1,2));
 	
-        psImage *mask1 = NULL, *mask2 = NULL; // Binned masks
-        if (data->masksName) {
-            mask1 = psImageAlloc(numCols1, numRows1, PS_TYPE_IMAGE_MASK);
-            mask2 = psImageAlloc(numCols2, numRows2, PS_TYPE_IMAGE_MASK);
-            psImageInit(mask1, 0xFF);
-            psImageInit(mask2, 0xFF);
-        }
 	// HDU containing the WCS we plan on using
 	pmHDU *projhdu = NULL;
@@ -249,7 +208,4 @@
             }
             pmFPAfileActivateSingle(data->config->files, true, "PPSKYCELL.IMAGE", j);
-            if (data->masksName) {
-                pmFPAfileActivateSingle(data->config->files, true, "PPSKYCELL.MASK", j);
-            }
 
             pmFPAview *view = filesIterateDown(data->config); // View to readout
@@ -304,30 +260,75 @@
             }
 
-#if (0)
-            psRegion *imageRegion = imageRegions->data[j]; // Region for image
-	    fprintf(stderr,"%d IM %f %f %f %f PR %f %f %f %f\n",
-		    j,
-		    imageRegion->x0,imageRegion->x1,
-		    imageRegion->y0,imageRegion->y1,
-		    projRegion->x0,projRegion->x1,
-		    projRegion->y0,projRegion->y1);
-#endif
+	    // Scale by exposure time
+	    double exptime_factor = pow(exptimes->data.F64[j]/exptime_target,data->exptimeOrder);
+	    if ((isfinite(exptime_factor))&&(exptime_factor != 0.0)) {
+	      psBinaryOp(bin1RO->image,bin1RO->image,"/",
+			 psScalarAlloc(exptime_factor,  PS_TYPE_F32));
+	      psBinaryOp(bin2RO->image,bin2RO->image,"/",
+			 psScalarAlloc(exptime_factor,  PS_TYPE_F32));
+	    }
+	    // End scaling
+
             // Offsets for image on this projection cell are just differences in CRPIX positions.
 	    int xOffset1 = (-1 * wcs->crpix1 - projRegion->x0) / (float)(data->bin1);
 	    int yOffset1 = (-1 * wcs->crpix2 - projRegion->y0) / (float)(data->bin1);
 	    
-            int xOffset2 = xOffset1 / (float)data->bin2, yOffset2 = yOffset1 / (float)data->bin2;
+            int xOffset2 = xOffset1 / (float)data->bin2;
+	    int yOffset2 = yOffset1 / (float)data->bin2;
 	    psTrace("ppSkycell",5,"Offsets: %d %d : %d %d",
 		    xOffset1,yOffset1,xOffset2,yOffset2);
 
+	    // Check each pixel for primacy.  A pixel is primary
+	    // if it is closer to the central pixel of its skycell
+	    // than any other pixel is to theirs.
+
+	    // Let's just do the overlay here, instead of doing math, then handing it
+	    // off to another function.  That seems silly.
+
+	    int u,v,x,y;
+
+	    double u0 = xOffset1 + (bin1RO->image->numCols) / 2.0;
+	    double v0 = yOffset1 + (bin1RO->image->numRows) / 2.0;
+	    for (x = 0; x < bin1RO->image->numCols; x++) {
+	      for (y = 0; y < bin1RO->image->numRows; y++) {
+		if (!isfinite(bin1RO->image->data.F32[y][x])) {
+		  continue;
+		}
+		u = x + xOffset1;
+		v = y + yOffset1;
+		double R2 = pow(u - u0,2) + pow(v - v0,2);
+		
+		if (R2 < radius1->data.F32[v][u]) {
+		  radius1->data.F32[v][u] = R2;
+		  image1->data.F32[v][u] = bin1RO->image->data.F32[y][x];
+		}
+	      }
+	    }
+
+	    u0 = xOffset2 + (bin2RO->image->numCols) / 2.0;
+	    v0 = yOffset2 + (bin2RO->image->numRows) / 2.0;
+	    for (x = 0; x < bin2RO->image->numCols; x++) {
+	      for (y = 0; y < bin2RO->image->numRows; y++) {
+		if (!isfinite(bin2RO->image->data.F32[y][x])) {
+		  continue;
+		}
+		u = x + xOffset2;
+		v = y + yOffset2;
+		double R2 = pow(u - u0,2) + pow(v - v0,2);
+		if (R2 < radius2->data.F32[v][u]) {
+		  radius2->data.F32[v][u] = R2;
+		  image2->data.F32[v][u] = bin2RO->image->data.F32[y][x];
+		}
+	      }
+	    }
+	    
+		
+		
+	    
 	    // Overlay the data onto the appropriate pixels in the final outputs
             // XXX Completely neglecting rotations
             // The skycells are divided up neatly with them all having the same orientation
-	    psImageOverlaySection(image1, bin1RO->image, xOffset1, yOffset1, "E");
-	    psImageOverlaySection(image2, bin2RO->image, xOffset2, yOffset2, "E");
-            if (data->masksName) {
-                psImageOverlaySection(mask1, bin1RO->mask, xOffset1, yOffset1, "M");
-                psImageOverlaySection(mask2, bin2RO->mask, xOffset2, yOffset2, "M");
-            }
+		//	    psImageOverlaySection(image1, bin1RO->image, xOffset1, yOffset1, "E");
+	    //	    psImageOverlaySection(image2, bin2RO->image, xOffset2, yOffset2, "E");
 
 	    // Cleanup on input loop.
@@ -337,9 +338,4 @@
             psFree(file->fpa);
             file->fpa = NULL;
-            if (data->masksName) {
-                pmFPAfile *file = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.MASK", j);
-                psFree(file->fpa);
-                file->fpa = NULL;
-            }
             pmFPAfileActivate(data->config->files, false, NULL);
         }
@@ -350,8 +346,4 @@
 	  pmFPAfileActivate(data->config->files, true, "PPSKYCELL.BIN1");
 	  pmFPAfileActivate(data->config->files, true, "PPSKYCELL.BIN2");
-	  if (data->masksName) {
-	    pmFPAfileActivate(data->config->files, true, "PPSKYCELL.BIN1.MASK");
-	    pmFPAfileActivate(data->config->files, true, "PPSKYCELL.BIN2.MASK");
-	  }
 	}
 	
@@ -366,6 +358,4 @@
         ro1->image = image1;
         ro2->image = image2;
-        ro1->mask = mask1;
-        ro2->mask = mask2;
 
         ro1->data_exists = cell1->data_exists = cell1->parent->data_exists = true;
@@ -386,17 +376,11 @@
 	  fits1->fpa->hdu->header = psMetadataAlloc();
 	  psMetadataCopy(fits1->fpa->hdu->header,projhdu->header);
-
+	  
 	  // Change wcs here
-#define WCS_DEBUG 0
 	  if (modify_wcs1) {
 	    pmAstromWCS *WCS = pmAstromWCSfromHeader(fits1->fpa->hdu->header);
 	    double cd1f = 1.0 * data->bin1;
 	    double cd2f = 1.0 * data->bin1;
-#if WCS_DEBUG
-	    fprintf(stderr,">>> %d %d (%g %g) (%g %g) (%g %g)\n",data->bin1,data->bin2,
-		    cd1f,cd2f,WCS->cdelt1,WCS->cdelt2,
-		    WCS->crpix1,WCS->crpix2
-		    );
-#endif
+
 	    WCS->cdelt1 *= cd1f;
 	    WCS->cdelt2 *= cd2f;
@@ -414,20 +398,8 @@
 	      WCS->crpix2 = WCS->crpix2 / cd2f;
 	    }
-#if WCS_DEBUG
-	    fprintf(stderr,">>> %d %d (%g %g) (%g %g) (%g %g) %d %d %d %d\n",data->bin1,data->bin2,
-		    cd1f,cd2f,WCS->cdelt1,WCS->cdelt2,
-		    WCS->crpix1,WCS->crpix2,
-		    WCS->trans->x->nX,		    WCS->trans->x->nY,
-		    WCS->trans->y->nX,		    WCS->trans->y->nY
-		    );
-#endif
+
 	    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] *= pow(cd1f,q) * pow(cd2f,r);
-#if WCS_DEBUG
-		fprintf(stderr,"PC: %d %d %g %g\n",
-			q,r,pow(cd1f,q) * pow(cd2f,r),
-			WCS->trans->x->coeff[q][r]);
-#endif
 	      }
 	    }
@@ -435,19 +407,7 @@
 	      for (int r = 0; r <= WCS->trans->y->nY; r++) {
 		WCS->trans->y->coeff[q][r] *= pow(cd1f,q) * pow(cd2f,r);
-#if WCS_DEBUG
-		fprintf(stderr,"PC: %d %d %g %g\n",
-			 q,r,pow(cd1f,q) * pow(cd2f,r),
-			 WCS->trans->y->coeff[q][r]);
-#endif
 	      }
 	    }
 	    pmAstromWCStoHeader (fits1->fpa->hdu->header,WCS);
-#if WCS_DEBUG
-	    WCS = pmAstromWCSfromHeader(fits1->fpa->hdu->header);
-	    fprintf(stderr,">>> %d %d (%g %g) (%g %g) (%g %g)\n",data->bin1,data->bin2,
-		    cd1f,cd2f,WCS->cdelt1,WCS->cdelt2,
-		    WCS->crpix1,WCS->crpix2
-		    );
-#endif
 	    modify_wcs1 = 0;
 	  }
@@ -459,12 +419,16 @@
 
 	  pmCell *Fcell1 = pmFPAfileThisCell(data->config->files, view, "PPSKYCELL.BIN1"); // Rebinned cell 1
-/* 	  // This is a hack to get a functioning header created so the fits images can be written out. */
+ 	  // This is a hack to get a functioning header created so the fits images can be written out. 
 	  psMetadataAddS32(Fcell1->concepts,PS_LIST_TAIL,"CELL.XPARITY", PS_META_REPLACE,"",1);
 	  psMetadataAddS32(Fcell1->concepts,PS_LIST_TAIL,"CELL.YPARITY", PS_META_REPLACE,"",1);
 	  psMetadataAddS32(Fcell1->concepts,PS_LIST_TAIL,"CELL.READDIR", PS_META_REPLACE,"",1);
 
+	  // I am baffled that this is the way to get the exposure time updated correctly.
+	  psMetadataItem *item1;
+	  item1 = psMetadataLookup(Fcell1->concepts,"CELL.EXPOSURE");
+	  item1->data.F32 = exptime_target;
+
 	  pmReadout *Fro1 = pmReadoutAlloc(Fcell1);
 	  Fro1->image = image1;
-	  Fro1->mask = mask1;
 	  Fro1->data_exists = Fcell1->data_exists = Fcell1->parent->data_exists = true;
 	  
@@ -472,31 +436,4 @@
 	  fits1->fileIndex = i;
 
-	  // Do the mask if we need to
-	  if (data->masksName) {
-/* 	    pmFPAfile *Mask1 = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.BIN1.MASK", 0); */
-/* 	    //	    Mask1->fpa->hdu = pmHDUAlloc(NULL); */
-/* 	    //	    Mask1->fpa->hdu->header = psMetadataAlloc(); */
-/* 	    //	    psMetadataCopy(Mask1->fpa->hdu->header,fits1->fpa->hdu->header); */
-/* 	    pmChip *Mchip1 = pmFPAfileThisChip(data->config->files, view, "PPSKYCELL.BIN1.MASK"); */
-/* 	    psMetadataAddS32(Mchip1->concepts,PS_LIST_TAIL,"CHIP.XPARITY", PS_META_REPLACE,"",1); */
-/* 	    psMetadataAddS32(Mchip1->concepts,PS_LIST_TAIL,"CHIP.YPARITY", PS_META_REPLACE,"",1); */
-	    
-/* 	    pmCell *Mcell1 = pmFPAfileThisCell(data->config->files, view, "PPSKYCELL.BIN1.MASK"); // Rebinned cell 1 */
-/* 	    /\* 	  // This is a hack to get a functioning header created so the fits images can be written out. *\/ */
-/* 	    psMetadataAddS32(Mcell1->concepts,PS_LIST_TAIL,"CELL.XPARITY", PS_META_REPLACE,"",1); */
-/* 	    psMetadataAddS32(Mcell1->concepts,PS_LIST_TAIL,"CELL.YPARITY", PS_META_REPLACE,"",1); */
-/* 	    psMetadataAddS32(Mcell1->concepts,PS_LIST_TAIL,"CELL.READDIR", PS_META_REPLACE,"",1); */
-	    
-/* 	    pmReadout *Mro1 = pmReadoutAlloc(Mcell1); */
-/* 	    Mro1->image = image1; */
-/* 	    Mro1->mask = mask1; */
-/* 	    Mro1->data_exists = Mcell1->data_exists = Mcell1->parent->data_exists = true; */
-	    
-/* 	    Mask1->save = true; */
-/* 	    Mask1->fileIndex = i; */
-	  }
-	    
-	    
-	  
 	  // Repeat with second binned image
 	  pmFPAfile *fits2 = pmFPAfileSelectSingle(data->config->files, "PPSKYCELL.BIN2", 0);
@@ -504,5 +441,5 @@
 	  fits2->fpa->hdu->header = psMetadataAlloc();
 	  psMetadataCopy(fits2->fpa->hdu->header,projhdu->header);
-
+	  
 	  if (modify_wcs2) {
 	    pmAstromWCS *WCS = pmAstromWCSfromHeader(fits2->fpa->hdu->header);
@@ -538,8 +475,10 @@
 	  psMetadataAddS32(Fcell2->concepts,PS_LIST_TAIL,"CELL.YPARITY", PS_META_REPLACE,"",1);
 	  psMetadataAddS32(Fcell2->concepts,PS_LIST_TAIL,"CELL.READDIR", PS_META_REPLACE,"",1);
+
+	  psMetadataItem *item2 = psMetadataLookup(Fcell2->concepts,"CELL.EXPOSURE");
+	  item2->data.F32 = exptime_target;
 	  
 	  pmReadout *Fro2 = pmReadoutAlloc(Fcell2); 
 	  Fro2->image = image2;
-	  Fro2->mask = mask2;
 	  Fro2->data_exists = Fcell2->data_exists = Fcell2->parent->data_exists = true;
 
@@ -551,23 +490,4 @@
 
         psFree(view);
-#if 0
-        {
-            psString filename = NULL;   // Filename for image
-            psStringAppend(&filename, "skycell_%d_1.fits", i);
-            psFits *fits = psFitsOpen(filename, "w");
-            psFree(filename);
-            psFitsWriteImage(fits, NULL, image1, 0, NULL);
-            psFitsClose(fits);
-        }
-
-        {
-            psString filename = NULL;   // Filename for image
-            psStringAppend(&filename, "skycell_%d_2.fits", i);
-            psFits *fits = psFitsOpen(filename, "w");
-            psFree(filename);
-            psFitsWriteImage(fits, NULL, image2, 0, NULL);
-            psFitsClose(fits);
-        }
-#endif
 
         filesIterateUp(data->config);
Index: /branches/eam_branches/ipp-20130904/psconfig/tagsets/ipp-3.0.libs
===================================================================
--- /branches/eam_branches/ipp-20130904/psconfig/tagsets/ipp-3.0.libs	(revision 36367)
+++ /branches/eam_branches/ipp-20130904/psconfig/tagsets/ipp-3.0.libs	(revision 36368)
@@ -34,5 +34,5 @@
 lib libpng               NONE           NONE   libpng-1.2.15.tar.gz     libpng-1.2.15    N NONE NONE NONE
 lib libjpeg              NONE           jpeg   jpegsrc.v8c.tar.gz       jpeg-8c          N --enable-shared NONE NONE
-lib libcfitsio           NONE           NONE   cfitsio3100-p4.tar.gz    cfitsio3100-p4   N --enable-shared shared,all NONE
+lib libcfitsio           NONE           NONE   cfitsio3100-p5.tar.gz    cfitsio3100-p5   N --enable-shared shared,all NONE
 lib libmysqlclient       NONE           mysql  mysql-5.0.51a.tar.gz     mysql-5.0.51a    N --with-unix-socket-path=/var/run/mysqld/mysqld.sock NONE NONE
 lib libgsl               NONE           NONE   gsl-1.11.tar.gz          gsl-1.11         N NONE NONE NONE
Index: /branches/eam_branches/ipp-20130904/pstamp/scripts/ftpsrequest
===================================================================
--- /branches/eam_branches/ipp-20130904/pstamp/scripts/ftpsrequest	(revision 36368)
+++ /branches/eam_branches/ipp-20130904/pstamp/scripts/ftpsrequest	(revision 36368)
@@ -0,0 +1,203 @@
+#!/usr/bin/perl
+
+# ftpsrequest
+
+# Creates a PS1 postage stamp request table using the FTOOLS program ftcreate.
+# The input file format is one line per row with columns described in the array columnsAndTypes given
+# below. 
+#
+# Note: The input format is the same as that used for the rows by the IPP program pstamp_request_file with
+# the exception that the comment is contained in the last column as a quoted string rather than by the |
+# character.
+
+# the option --print-sample may be used to print out an example of a line in an input file to stdout.
+#
+
+# The program ftcreate is part of the FTOOLS package which is described at http://heasarc.gsfc.nasa.gov/ftools
+# and in
+# Blackburn, J. K. 1995, in ASP Conf. Ser., Vol. 77, Astronomical Data Analysis Software and Systems IV, 
+# ed. R. A. Shaw, H. E. Payne, and J. J. E. Hayes (San Francisco: ASP), 367. 
+
+use strict;
+use warnings;
+
+# using a minimal number of perl modules for ease of portablity
+
+use File::Temp qw(tempfile);
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+# Array describing the columns in the request table and the order of the columns in the input file
+# third column is value for sample request. A stamp for an i band stack of the tadpole galaxy.
+my @columnsAndTypes = qw(
+        ROWNUM      J   1
+        CENTER_X    D   241.516417
+        CENTER_Y    D   55.425369
+        WIDTH       D   1000
+        HEIGHT      D   1000
+        COORD_MASK  J   2
+        JOB_TYPE    16A stamp
+        OPTION_MASK J   65
+        PROJECT     16A gpc1
+        SURVEY_NAME 16A 3PI
+        IPP_RELEASE 64A null
+        REQ_TYPE    16A bycoord
+        IMG_TYPE    16A stack
+        ID          16A null
+        TESS_ID     64A null
+        COMPONENT   64A null
+        DATA_GROUP  64A null
+        REQFILT     16A i
+        MJD_MIN     D   0
+        MJD_MAX     D   0
+        RUN_TYPE    16A null
+        FWHM_MIN    D   0
+        FWHM_MAX    D   0
+        COMMENT     64A tadpole_galaxy
+);
+
+
+my ( $input,			# Name of input text file
+     $output,			# Name of output table
+     $req_name, 
+     $print_sample,
+     $email, 
+     $clobber,
+     $help,
+     $verbose,
+     $save_temps,
+     );
+
+
+GetOptions(
+	   'input|i=s'      => \$input,
+	   'output|o=s'     => \$output,
+	   'req_name|r=s'   => \$req_name,
+	   'email=s'        => \$email,
+           'help'           => \$help,
+           'print-sample'   => \$print_sample,
+           'clobber'        => \$clobber,
+	   'save-temps'     => \$save_temps,
+	   'verbose'        => \$verbose,
+) or pod2usage( 2 );
+
+my $usageMessage = "$0: --input <input> --output <output> -req_name <request_name>";
+if ($help) {
+    # pod2usage( -msg => $usageMessage, -exitval => 0);
+    pod2usage( -exitval => 0);
+}
+if ($print_sample) {
+    printSample();
+}
+
+pod2usage ( -msg => "Required options: --input --output --req_name\n", -exitval => 1)
+    unless 
+        defined $input and
+        defined $output and
+        defined $req_name;
+
+
+system "which ftcreate >& /dev/null";
+if ($?){
+    die "Can't find required program ftcreate\n";
+}
+
+# temp files for header keyword file and column descriptor file
+my ($hfile, $hfilename) = tempfile( "/tmp/hfile.XXXX", UNLINK => !$save_temps, SUFFIX => '.txt');
+my ($cfile, $cfilename) = tempfile( "/tmp/cfile.XXXX", UNLINK => !$save_temps, SUFFIX => '.txt');
+
+# create the temp files
+makeHeaderFile($hfile, $req_name, $email);
+makeColumnDescriptorFile($cfile);
+
+# here is the command that builds the fits table
+my $cmd = "ftcreate extname=PS1_PS_REQUEST headfile=$hfilename $cfilename $input $output";
+$cmd .= ' clobber=yes' if $clobber;
+
+print STDERR "$cmd\n" if $verbose;
+
+my $rc = system $cmd;
+if ($rc) {
+    my $status = $rc >> 8;
+    die "ftcreate exited with $status ($rc)\n";
+}
+
+exit 0; 
+
+############################
+
+sub makeHeaderFile {
+    my ($hfile, $req_name, $email) = @_;
+
+    $email = 'null' if !defined $email;
+
+    print $hfile "EXTVER=2 / PSTAMP request format version\n";
+    print $hfile "ACTION=PROCESS\n";
+    print $hfile "REQ_NAME=$req_name / unique name for the postage stamp request\n";
+    print $hfile "EMAIL=$email\n";
+    close $hfile;
+}
+
+
+
+sub makeColumnDescriptorFile {
+    my $cfile = shift;
+
+    my $n = scalar @columnsAndTypes;
+
+    die "ERROR: number of columnsAndTypes must be multiple of 3\n" if $n % 3;
+
+    for (my $i = 0; $i < $n; $i += 3) {
+        print $cfile "$columnsAndTypes[$i]\t$columnsAndTypes[$i+1]\n";
+    }
+    close $cfile;
+}
+
+sub printSample {
+    # header line as a comment
+    print "#";
+    for (my $i = 0; $i < scalar @columnsAndTypes; $i += 3) {
+        print " $columnsAndTypes[$i]";
+    }
+    print "\n";
+
+    # sample values
+    my $i;
+    for ($i = 0; $i < scalar @columnsAndTypes - 3; $i += 3) {
+        print " $columnsAndTypes[$i+2]";
+    }
+    # last column is the COMMENT and it needs to be quoted
+    print " '$columnsAndTypes[$i+2]'";
+    print "\n";
+
+    exit 0;
+}
+
+__END__
+
+=pod
+
+=head1 NAME
+
+ftpsrequest - use ftcreate to create a postage stamp request fits table from a textual description
+
+=head1 SYNOPSIS
+
+  ftpsrequest --req_name <request name> --input <input text file> --output <output file name>
+
+    Options:
+        --req_name      REQ_NAME value for the fits header
+        --input         name of ASCII file giving the table values
+        --output        name of output fits table
+        --email         email address of user (optional)
+        --clobber       overwrite existing output file
+        --help          print usage message
+        --print-sample  print out an sample input text file
+
+=head1 NOTE
+
+  In order for ftpsrequest to succeed the ftools program ftcreate must be found in the users path.
+
+  FTOOLS are described at http://heasarc.gsfc.nasa.gov/ftools
+
+=cut
Index: /branches/eam_branches/ipp-20130904/tools/neb_rawOTA_host_scan.pl
===================================================================
--- /branches/eam_branches/ipp-20130904/tools/neb_rawOTA_host_scan.pl	(revision 36368)
+++ /branches/eam_branches/ipp-20130904/tools/neb_rawOTA_host_scan.pl	(revision 36368)
@@ -0,0 +1,140 @@
+#!/usr/bin/env perl 
+
+use warnings;
+use DBI;
+
+use strict;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+
+use constant DB_SOCKET => '/var/run/mysqld/mysqld.sock';
+my $dbname   = 'nebulous';
+my $dbserver = $ENV{NEB_DBSERVER};
+my $dbuser = $ENV{NEB_USER};
+my $dbpass = $ENV{NEB_PASS};
+my $db = DBI->connect("DBI:mysql:database=${dbname};host=${dbserver};" .
+                   "mysql_socket=" . DB_SOCKET(),
+                   ${dbuser},${dbpass},
+		   { RaiseError => 1, AutoCommit => 1}
+    ) or die "Unable to connect to database $DBI::errstr\n";
+
+my ($host,$min_ins_id,$verbose,$limit,$continue);
+$min_ins_id = 0;
+$verbose = 0;
+$limit = 10000;
+$continue = 0;
+
+GetOptions(
+    'host|h=s'      => \$host,
+    'min|m=s'       => \$min_ins_id,
+    'verbose|v'     => \$verbose,
+    'limit|l=s'     => \$limit,
+    'continue|c'    => \$continue,
+    ) or pod2usage( 2 );
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage( -msg => "Required options: --host",
+	   -exitval => 3) unless
+    defined($host);
+
+my $volume = $host . ".0";
+my $rr;
+
+# Get this host volume
+my $vol_id_sth = "SELECT vol_id FROM volume where name = '${volume}'";
+my $r_vol      = $db->selectall_arrayref( $vol_id_sth );
+unless (defined(${ $r_vol }[0])) {
+    die "Volume $volume does not exist.";
+}
+my $vol_id = shift( @{ ${ $r_vol }[0] });
+
+if ($verbose) {
+    print "$host $volume $vol_id $min_ins_id\n";
+}
+
+
+# Get storage node volumes;
+my %b_nodes = ();
+my $vol_id_sth2 = "SELECT vol_id FROM volume WHERE name LIKE 'ippb%'";
+my $r_vol2      = $db->selectall_arrayref( $vol_id_sth2 );
+foreach $rr (@{ $r_vol2 }) {
+    my $b_vol_id = shift( @{ $rr });
+    $b_nodes{$b_vol_id} = 1;
+    if ($verbose) {
+	print " b_node: $b_vol_id\n";
+    }
+}
+
+# Get the files on this host
+my $number = 0;
+do {
+    my $ins_id_sth = "SELECT ins_id,so_id,uri FROM instance WHERE vol_id = ${vol_id} AND ins_id > $min_ins_id LIMIT $limit";
+    my $r_ins      = $db->selectall_arrayref( $ins_id_sth );
+    my $last_ins_id = 0;
+    $number = 0;
+    foreach $rr (@{ $r_ins }) {
+	my ($ins_id,$so_id,$uri) = @{ $rr };
+	if ($verbose) {
+#	print ("  $ins_id $so_id $uri\n");
+	}
+	$number++;
+	$last_ins_id = $ins_id;
+	if ($uri !~ /ota...fits/) { next; }
+	my $have_b_node = 0;
+	my ($ext_id,$occ_id,$oth_id,$name,$value) = ('',0,0,'','');
+	my $rr2;
+	
+	my $so_id_sth = "SELECT ext_id,so_id,vol_id,ins_id,name,value FROM storage_object JOIN instance USING(so_id) LEFT JOIN storage_object_xattr USING(so_id) WHERE so_id = ${so_id} AND ins_id != ${ins_id} AND (name IS NULL OR name = 'user.copies')";
+	
+	my $r_so      = $db->selectall_arrayref( $so_id_sth );
+	
+	foreach $rr2 (@{ $r_so }) {
+	    ($ext_id,$so_id,$occ_id,$oth_id,$name,$value) = @{ $rr2 };
+	    
+	    unless(defined($name)) {
+		$name = 'UNDEF';
+		$value = 1;
+	    }
+	    
+	    if ($name eq 'user.copies') {
+		if ($value == 1) {
+		    $have_b_node = 1;
+		}
+	    }
+	    else {
+		$have_b_node = 1;
+	    }
+	    
+	    if (defined($b_nodes{$occ_id})) {
+		$have_b_node = 1;
+	    }
+	    if ($verbose) {
+		print("   $number $ext_id $so_id $occ_id $oth_id $have_b_node $name $value\n");
+	    }
+	    
+	    if ($have_b_node == 1) {
+		last;
+	    }
+	}
+	if ($oth_id) {
+	    if ($have_b_node != 1) {
+		my $rep_host = int(rand() + 0.5) + 4;
+		my $rep_part = int(rand(3));
+		my $rep_vol  = "ippb0" . $rep_host . "." . $rep_part;
+		
+		print("neb-replicate --volume $rep_vol $ext_id\n");
+		system("neb-replicate --volume $rep_vol $ext_id");
+	    }
+	}
+	$last_ins_id = $ins_id;
+    }
+    print "## LAST: $last_ins_id $number\n";
+    print "## TRY:  neb_rawOTA_host_scan.pl --host $host --limit $limit --min $last_ins_id\n";
+    if ($continue) {
+	if ($verbose) {
+	    print("RESET $min_ins_id -> $last_ins_id $number\n");
+	}
+	$min_ins_id = $last_ins_id;
+    }
+} while ($continue && $number != 0);
