Index: branches/eam_branches/ipp-dev-20210817/psModules/src/detrend/pmPattern.c
===================================================================
--- branches/eam_branches/ipp-dev-20210817/psModules/src/detrend/pmPattern.c	(revision 41859)
+++ branches/eam_branches/ipp-dev-20210817/psModules/src/detrend/pmPattern.c	(revision 41861)
@@ -418,5 +418,5 @@
 	  for (int x = 0; x < numCols; x++) {
 	    image->data.F32[y][x] += solution->data.F32[y];
-	    psTrace("pattern",5,"B: %d %d %g\n",x,y,solution->data.F32[x]);
+	    psTrace("pattern",5,"B: %d %d %g\n",x,y,solution->data.F32[y]);
 	  }
 	  corr->data.F64[y][0]  -= solution->data.F32[y];
@@ -444,5 +444,6 @@
 	  for (int x = 0; x < numCols; x++) {
 	    image->data.F32[y][x] += solution->data.F32[y] * indices->data.F32[x];
-	    psTrace("pattern",5,"C: %d %d %g %g\n",x,y,solution->data.F32[x],indices->data.F32[x]);
+	    // XXX EAM : this was [x] which is wrong
+	    psTrace("pattern",5,"C: %d %d %g %g\n",x,y,solution->data.F32[y],indices->data.F32[x]);
 	  }
 	  corr->data.F64[y][1]  -= solution->data.F32[y] ;
@@ -703,4 +704,5 @@
         }
 
+	psAssert (solution->n == numCols, "oops");
         for (int x = 0; x < numCols; x++) {
             image->data.F32[y][x] -= solution->data.F32[x];
@@ -747,8 +749,10 @@
 	}
       } else {
+	psAssert (solution->n == numRows, "oops");
 	for (int y = 0; y < numRows; y++) {
 	  for (int x = 0; x < numCols; x++) {
 	    image->data.F32[y][x] += solution->data.F32[y];
-	    psTrace("pattern",5,"B: %d %d %g\n",x,y,solution->data.F32[x]);
+	    // XXX EAM : this was [x], which is wrong
+	    psTrace("pattern",5,"B: %d %d %g\n",x,y,solution->data.F32[y]);
 	  }
 	  corr->data.F64[y][0]  -= solution->data.F32[y];
@@ -771,8 +775,10 @@
 	psErrorClear();
       } else {
+	psAssert (solution->n == numRows, "oops");
 	for (int y = 0; y < numRows; y++) {
 	  for (int x = 0; x < numCols; x++) {
 	    image->data.F32[y][x] += solution->data.F32[y] * indices->data.F32[x];
-	    psTrace("pattern",5,"C: %d %d %g %g\n",x,y,solution->data.F32[x],indices->data.F32[x]);
+	    // XXX EAM : this was set to [x] which is wrong (numCols > numRows)
+	    psTrace("pattern",5,"C: %d %d %g %g\n",x,y,solution->data.F32[y],indices->data.F32[x]);
 	  }
 	  corr->data.F64[y][1]  -= solution->data.F32[y] ;
Index: branches/eam_branches/ipp-dev-20210817/psModules/src/detrend/pmPatternIO.c
===================================================================
--- branches/eam_branches/ipp-dev-20210817/psModules/src/detrend/pmPatternIO.c	(revision 41859)
+++ branches/eam_branches/ipp-dev-20210817/psModules/src/detrend/pmPatternIO.c	(revision 41861)
@@ -531,4 +531,5 @@
 	for (int i = 0; i < model->n; i++) {
 	    psMetadata *row = model->data[i];
+	    psAssert (row, "missing model row");
 
 	    char *cellName = psMetadataLookupStr(&status, row, "CELL_NAME");
@@ -538,6 +539,10 @@
 
 	    int cellNumber = pmChipFindCell (chip, cellName);
+	    psAssert ((cellNumber >=0) && (cellNumber < chip->cells->n), "invalid cell number");
+
 	    pmCell *cell = chip->cells->data[cellNumber];
 	    if (!cell) continue;
+
+	    psAssert (cell->analysis, "oops");
 
 	    psMetadataAddF32 (cell->analysis, PS_LIST_TAIL, "PTN.ROW.AMP", PS_META_REPLACE, "", amplitude);
