Index: trunk/magic/remove/src/streaksastrom.c
===================================================================
--- trunk/magic/remove/src/streaksastrom.c	(revision 20842)
+++ trunk/magic/remove/src/streaksastrom.c	(revision 21156)
@@ -17,5 +17,9 @@
 
     if (fromCell) {
-        // The metadata is the cell concepts
+        // XXX: I think that this is not needed
+        // We are only asked to get the concepts from the cell when we're reading from a pmfile (skycell)
+        // In that case we (x0,y0) = (0,0) and the parirties are positive
+        // If the x0, y0 are not zero they are probably in the FITS unity based array format
+        // We should probably assert that these are zero
         cell_x0 =  psMetadataLookupS32(&mdok, md, "CELL.X0");
         if (!mdok) {
@@ -28,5 +32,4 @@
             return NULL;
         }
-
         xParityCell = psMetadataLookupS32(&mdok, md, "CELL.XPARITY");
         if (!mdok || (xParityCell != 1 && xParityCell != -1)) {
@@ -65,7 +68,7 @@
             return false;
         }
-
     } else {
         // no metadata regular cell
+        // used by warpedpixels
         cell_x0 = 0;
         cell_y0 = 0;
@@ -89,4 +92,50 @@
 }
 
+void
+chipToCell(double *xCell, double *yCell, strkAstrom *astrom, double xChip, double yChip)
+{
+    // convert from chip to cell
+    if (astrom->xParity > 0) {
+        *xCell = xChip - astrom->cell_x0;
+    } else {
+        *xCell = astrom->cell_x0 - 1 - xChip;
+    }
+    if (astrom->yParity > 0) {
+        *yCell = yChip - astrom->cell_y0;
+    } else {
+        *yCell = astrom->cell_y0 - 1 - yChip;
+    }
+}
+
+void
+cellToChip(double *xChip, double *yChip, strkAstrom *astrom, double xCell, double yCell)
+{
+    if (astrom->xParity > 0) {
+        *xChip = xCell + astrom->cell_x0;
+    } else {
+        *xChip = astrom->cell_x0 - 1 - xCell;
+    }
+    if (astrom->yParity > 0) {
+        *yChip = yCell + astrom->cell_y0;
+    } else {
+        *yChip =  astrom->cell_y0 - 1 - yCell;
+    }
+}
+
+void
+cellToChipInt(int *xChip, int *yChip, strkAstrom *astrom, int xCell, int yCell)
+{
+    if (astrom->xParity > 0) {
+        *xChip = xCell + astrom->cell_x0;
+    } else {
+        *xChip = astrom->cell_x0 - 1 - xCell;
+    }
+    if (astrom->yParity > 0) {
+        *yChip = yCell + astrom->cell_y0;
+    } else {
+        *yChip =  astrom->cell_y0 - 1 - yCell;
+    }
+}
+ 
 bool
 skyToCell(strkPt *outPt, strkAstrom *astrom, double ra, double dec)
@@ -107,7 +156,5 @@
     psPlaneTransformApply(pt->chip, chip->fromFPA, pt->FP);
 
-    // convert from chip to cell
-    outPt->x = (pt->chip->x - astrom->cell_x0) * astrom->xParity;
-    outPt->y = (pt->chip->y - astrom->cell_y0) * astrom->yParity;
+    chipToCell(&outPt->x, &outPt->y, astrom, pt->chip->x, pt->chip->y);
 
 //    printf("cell: %f %f chip: %f %f\n", outPt->x, outPt->y, pt->chip->x, pt->chip->y);
@@ -115,5 +162,5 @@
     return true;
 }
- 
+
 bool
 cellToSky(strkPt *outPt, strkAstrom *astrom, double x, double y)
@@ -123,7 +170,5 @@
     pmAstromObj *pt = (pmAstromObj *) astrom->pt;
     
-    // XXX: TODO: confirm that this cell to chip transformation is correct
-    pt->chip->x = (x * astrom->xParity) + astrom->cell_x0;
-    pt->chip->y = (y * astrom->yParity) + astrom->cell_y0;
+    cellToChip(&pt->chip->x, &pt->chip->y, astrom, x, y);
 
     pt->chip->xErr = 0;  // Is setting these errors to zero the right thing to do?
@@ -142,11 +187,4 @@
 }
 
-void
-cellToChip(PixelPos *chip, strkAstrom *astrom, PixelPos *cell)
-{
-    // TODO: do I need to worry about going from int to float and back again here?
-    chip->x = (cell->x * astrom->xParity) + astrom->cell_x0;
-    chip->y = (cell->y * astrom->yParity) + astrom->cell_y0;
-}
  
 static bool
@@ -166,5 +204,4 @@
     PS_ASSERT_PTR_NON_NULL(hdu->header, 1)
     if (sf->bilevelAstrometry) {
-        // Do we get here for GPC1 ? I don't necessarily have an fpa for the input image
         if (!pmAstromReadBilevelMosaic(sf->inAstrom->fpa, phu->header)) {
             psError(PS_ERR_UNKNOWN, false, "Unable to read bilevel mosaic astrometry for input FPA.");
@@ -232,2 +269,10 @@
     }
 }
+
+void
+linearizeTransforms(strkAstrom *astrom)
+{
+    if (!pmAstromLinearizeTransforms((pmFPA *) astrom->fpa, (pmChip *) astrom->chip)) {
+        streaksExit("linear fit to astrometry failed\n", PS_EXIT_UNKNOWN_ERROR);
+    }
+}
