Index: /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c
===================================================================
--- /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c	(revision 6112)
+++ /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.c	(revision 6113)
@@ -132,7 +132,7 @@
 
 // Mosaic a chip together into a single image
-static bool mosaicChip(pmChip *chip,// Chip to mosaic
-                       int xBinChip, int yBinChip // Binning of mosaic image in x and y
-                      )
+int pmChipMosaic(pmChip *chip,// Chip to mosaic
+                 int xBinChip, int yBinChip // Binning of mosaic image in x and y
+                )
 {
 
@@ -149,9 +149,10 @@
 
     // Set up the required inputs
+    psTrace(__func__, 1, "Mosaicking %d cells...\n", cells->n);
     for (int i = 0; i < cells->n; i++) {
         pmCell *cell = cells->data[i];  // The cell of interest
         x0->data.S32[i] = psMetadataLookupS32(NULL, cell->concepts, "CELL.X0");
         y0->data.S32[i] = psMetadataLookupS32(NULL, cell->concepts, "CELL.Y0");
-        psTrace(__func__, 9, "Cell %d: x0=%d y0=%d\n", i, x0->data.S32[i], y0->data.S32[i]);
+        psTrace(__func__, 5, "Cell %d: x0=%d y0=%d\n", i, x0->data.S32[i], y0->data.S32[i]);
         xBin->data.S32[i] = psMetadataLookupS32(NULL, cell->concepts, "CELL.XBIN");
         yBin->data.S32[i] = psMetadataLookupS32(NULL, cell->concepts, "CELL.XBIN");
@@ -204,4 +205,6 @@
     psFree(weights);
     psFree(masks);
+    int nCells = cells->n;
+    psFree(cells);
 
     // Chop off all the component cells, and put in a new one
@@ -228,9 +231,9 @@
     //psFree(readout);
 
-    return true;
-}
-
-
-int pmFPAMosaicChips(pmFPA *fpa,        // FPA
+    return nCells;
+}
+
+
+int pmFPAMosaicCells(pmFPA *fpa,        // FPA
                      int xBinChip, int yBinChip // Binning of mosaic image in x and y
                     )
@@ -247,5 +250,5 @@
 
         numChips++;
-        mosaicChip(chip, xBinChip, yBinChip); // Mosaic of cells within the chip
+        pmChipMosaic(chip, xBinChip, yBinChip); // Mosaic of cells within the chip
 
     }
Index: /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.h
===================================================================
--- /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.h	(revision 6112)
+++ /branches/eam_rel9_p0/psModules/src/astrom/pmChipMosaic.h	(revision 6113)
@@ -14,6 +14,11 @@
                         );
 
+// Mosaic a chip together into a single cell with single readout
+int pmChipMosaic(pmChip *chip,// Chip to mosaic
+                 int xBinChip, int yBinChip // Binning of mosaic image in x and y
+                );
+
 // Mosaic all the cells in all (valid) chips together (neglecting the overscans); return number of chips
-int pmFPAMosaicChips(pmFPA *fpa,        // FPA
+int pmFPAMosaicCells(pmFPA *fpa,        // FPA
                      int xBinChip, int yBinChip // Binning of mosaic image in x and y
                     );
Index: /branches/eam_rel9_p0/psModules/src/astrom/pmFPA.c
===================================================================
--- /branches/eam_rel9_p0/psModules/src/astrom/pmFPA.c	(revision 6112)
+++ /branches/eam_rel9_p0/psModules/src/astrom/pmFPA.c	(revision 6113)
@@ -12,6 +12,6 @@
 * XXX: Should we implement non-linear cell->chip transforms?
 *
-*  @version $Revision: 1.1.2.2 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-01-20 09:47:06 $
+*  @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-01-21 04:33:38 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -36,14 +36,24 @@
 {
     if (readout != NULL) {
+        psTrace(__func__, 9, "Removing readout %x from cell %x...\n", (unsigned)readout, (unsigned)readout->parent);
+        if (readout->parent) {
+            psArray *readouts = readout->parent->readouts;
+            for (int i = 0; i < readouts->n; i++) {
+                if (readouts->data[i] == readout) {
+                    readouts->data[i] = NULL;
+                    break;
+                }
+            }
+        }
+        psTrace(__func__, 9, "Freeing readout %x\n", (unsigned)readout);
         psFree(readout->image);
         psFree(readout->mask);
         psFree(readout->weight);
         psFree(readout->analysis);
-        #if 0
-
-        psFree(readout->parent);
+        #if 1
+
+        psMemDecrRefCounter(readout->parent);
         #endif
 
-        readout->parent = NULL;
     }
 }
@@ -52,4 +62,15 @@
 {
     if (cell != NULL) {
+        psTrace(__func__, 9, "Removing cell %x from chip %x...\n", (unsigned)cell, (unsigned)cell->parent);
+        if (cell->parent) {
+            psArray *cells = cell->parent->cells;
+            for (int i = 0; i < cells->n; i++) {
+                if (cells->data[i] == cell) {
+                    cells->data[i] = NULL;
+                    break;
+                }
+            }
+        }
+        psTrace(__func__, 9, "Freeing cell %x\n", (unsigned)cell);
         psFree(cell->toChip);
         psFree(cell->toFPA);
@@ -58,25 +79,23 @@
         psFree(cell->analysis);
         psFree(cell->camera);
+        #if 1
         //
         // Set the parent to NULL in all cell->readouts before psFree(cell->readouts)
         // in order to avoid memory reference counter problems.
         //
-        #if 0
-
-        for (psS32 i = 0 ; i < cell->readouts->n ; i++) {
-            pmReadout *tmpReadout = (pmReadout *) cell->readouts->data[i];
+        psArray *readouts = cell->readouts;
+        for (psS32 i = 0 ; i < readouts->n ; i++) {
+            pmReadout *tmpReadout = readouts->data[i];
+            readouts->data[i] = NULL;
             tmpReadout->parent = NULL;
             if (PS_FREE_HIERARCHY == 1) {
+                psTrace(__func__, 9, "Will now free readout %x...\n", (unsigned)tmpReadout);
                 psFree(tmpReadout);
             }
         }
-        psFree(cell->parent);
+        psMemDecrRefCounter(cell->parent);
         #endif
 
-        cell->parent = NULL;
-
-        psFree(cell->readouts);
         psFree(cell->hdu);
-
     }
 }
@@ -85,16 +104,29 @@
 {
     if (chip != NULL) {
+        psTrace(__func__, 9, "Removing chip %x from fpa %x...\n", (unsigned)chip, (unsigned)chip->parent);
+        if (chip->parent) {
+            psArray *chips = chip->parent->chips;
+            for (int i = 0; i < chips->n; i++) {
+                if (chips->data[i] == chip) {
+                    chips->data[i] = NULL;
+                    break;
+                }
+            }
+        }
+        psTrace(__func__, 9, "Freeing chip %x\n", (unsigned)chip);
+
         psFree(chip->toFPA);
         psFree(chip->fromFPA);
         psFree(chip->concepts);
         psFree(chip->analysis);
+        #if 1
         //
         // Set the parent to NULL in all chip->cells before psFree(chip->cells)
         // in order to avoid memory reference counter problems.
         //
-        #if 0
-
-        for (psS32 i = 0 ; i < chip->cells->n ; i++) {
-            pmCell *tmpCell = (pmCell *) chip->cells->data[i];
+        psArray *cells = chip->cells;
+        for (psS32 i = 0 ; i < cells->n ; i++) {
+            pmCell *tmpCell = cells->data[i];
+            cells->data[i] = NULL;
             tmpCell->parent = NULL;
             if (PS_FREE_HIERARCHY == 1) {
@@ -102,9 +134,7 @@
             }
         }
-        psFree(chip->parent);
+        psMemDecrRefCounter(chip->parent);
         #endif
 
-        chip->parent = NULL;
-        psFree(chip->cells);
         psFree(chip->hdu);
     }
@@ -115,4 +145,5 @@
 {
     if (fpa != NULL) {
+        psTrace(__func__, 9, "Freeing fpa %x\n", (unsigned)fpa);
         psFree(fpa->fromTangentPlane);
         psFree(fpa->toTangentPlane);
@@ -121,12 +152,13 @@
         psFree(fpa->analysis);
         psFree(fpa->camera);
+        #if 1
         //
         // Set the parent to NULL in all fpa->chips before psFree(fpa->chips)
         // in order to avoid memory reference counter problems.
         //
-        #if 0
-
-        for (psS32 i = 0 ; i < fpa->chips->n ; i++) {
-            pmChip *tmpChip = (pmChip *) fpa->chips->data[i];
+        psArray *chips = fpa->chips;
+        for (psS32 i = 0 ; i < chips->n ; i++) {
+            pmChip *tmpChip = chips->data[i];
+            chips->data[i] = NULL;
             tmpChip->parent = NULL;
             if (PS_FREE_HIERARCHY == 1) {
@@ -135,5 +167,4 @@
         }
         #endif
-        psFree(fpa->chips);
         psFree(fpa->hdu);
         psFree(fpa->phu);
@@ -167,5 +198,5 @@
     tmpReadout->bias = psListAlloc(NULL);
     tmpReadout->analysis = psMetadataAlloc();
-    tmpReadout->parent = cell;
+    tmpReadout->parent = psMemIncrRefCounter(cell);
     if (cell != NULL) {
         cell->readouts = psArrayAdd(cell->readouts, 1, (psPtr) tmpReadout);
@@ -197,5 +228,5 @@
     tmpCell->analysis = psMetadataAlloc();
     tmpCell->readouts = psArrayAlloc(0);
-    tmpCell->parent = chip;
+    tmpCell->parent = psMemIncrRefCounter(chip);
     if (chip != NULL) {
         chip->cells = psArrayAdd(chip->cells, 1, (psPtr) tmpCell);
@@ -230,5 +261,5 @@
     tmpChip->analysis = psMetadataAlloc();
     tmpChip->cells = psArrayAlloc(0);
-    tmpChip->parent = fpa;
+    tmpChip->parent = psMemIncrRefCounter(fpa);
     if (fpa != NULL) {
         fpa->chips = psArrayAdd(fpa->chips, 1, (psPtr) tmpChip);
@@ -562,6 +593,7 @@
 
         // Set weight image to the variance = g*f + rn^2
-        psBinaryOp(readout->weight, image, "*", psScalarAlloc(gain, PS_TYPE_F32));
-        psBinaryOp(readout->weight, readout->weight, "+", psScalarAlloc(readnoise*readnoise, PS_TYPE_F32));
+        psBinaryOp(readout->weight, image, "/", psScalarAlloc(gain, PS_TYPE_F32));
+        psBinaryOp(readout->weight, readout->weight, "+",
+                   psScalarAlloc(readnoise*readnoise/gain/gain, PS_TYPE_F32));
     }
 
