Index: trunk/psModules/src/pmAstrometry.c
===================================================================
--- trunk/psModules/src/pmAstrometry.c	(revision 4754)
+++ trunk/psModules/src/pmAstrometry.c	(revision 4770)
@@ -8,6 +8,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-08-11 00:03:51 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-08-16 01:10:34 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -23,12 +23,11 @@
 #include "pmAstrometry.h"
 
-psDB wow;
-
 /*****************************************************************************
-checkValidImageCoords(): this is a private function which simply
-determines if the supplied x,y coordinates are in the range for the supplied
-psImage.
+checkValidImageCoords(): this is a private function which simply determines if
+the supplied x,y coordinates are in the range for the supplied psImage.
+ 
+XXX: What about col0 and row0
+XXX: This should return a psBool.
  *****************************************************************************/
-/*
 static psS32 checkValidImageCoords(double x,
                                    double y,
@@ -36,501 +35,333 @@
 {
     PS_ASSERT_IMAGE_NON_NULL(tmpImage, 0);
- 
+
     if ((x < 0.0) || (x > (double)tmpImage->numCols) ||
             (y < 0.0) || (y > (double)tmpImage->numRows)) {
         return (0);
     }
- 
+
     return (1);
 }
-*/
-/*
-static void FPAFree(psFPA* fpa)
+
+/******************************************************************************
+XXX: Is this the correct way to free metadata?
+XXX: Is this the correct way to free database structs?
+ *****************************************************************************/
+
+static void FPAFree(pmFPA *fpa)
 {
     if (fpa != NULL) {
-        psFree(fpa->chips);
-        psFree(fpa->grommit);
-        psFree(fpa->exposure);
-        psFree(fpa->metadata);
         psFree(fpa->fromTangentPlane);
         psFree(fpa->toTangentPlane);
-        psFree(fpa->pattern);
-        psFree(fpa->colorPlus);
-        psFree(fpa->colorMinus);
         psFree(fpa->projection);
-    }
-}
-*/
-
-/*
-static void chipFree(psChip* chip)
+        psFree(fpa->concepts);
+        psFree(fpa->analysis);
+        psFree(fpa->camera);
+        psFree(fpa->chips);
+        psFree(fpa->header);
+        psFree(fpa->db);
+    }
+}
+
+static void chipFree(pmChip* chip)
 {
     if (chip != NULL) {
-        psFree(chip->cells);
-        psFree(chip->metadata);
         psFree(chip->toFPA);
         psFree(chip->fromFPA);
-    }
-}
-*/
-
-/*
-static void cellFree(psCell* cell)
+        psFree(chip->concepts);
+        psFree(chip->analysis);
+        psFree(chip->cells);
+        psFree(chip->parent);
+        psFree(chip->extname);
+        psFree(chip->pixels);
+        psFree(chip->header);
+    }
+}
+
+static void cellFree(pmCell *cell)
 {
     if (cell != NULL) {
+        psFree(cell->toChip);
+        psFree(cell->toFPA);
+        psFree(cell->toSky);
+        psFree(cell->concepts);
+        psFree(cell->analysis);
         psFree(cell->readouts);
-        psFree(cell->metadata);
-        psFree(cell->toChip);
-        psFree(cell->fromChip);
-        psFree(cell->toFPA);
-        psFree(cell->toTP);
-        psFree(cell->toSky);
-    }
-}
-*/
-
-static void readoutFree(psReadout* readout)
+        psFree(cell->parent);
+        psFree(cell->extname);
+        psFree(cell->pixels);
+        psFree(cell->header);
+    }
+}
+
+static void readoutFree(pmReadout *readout)
 {
     if (readout != NULL) {
         psFree(readout->image);
         psFree(readout->mask);
-        psFree(readout->objects);
-        psFree(readout->metadata);
-    }
-}
-
-/*
-static void observatoryFree(psObservatory* obs)
-{
-    if (obs != NULL) {
-        psFree(obs->name);
-    }
-}
-*/
-/*
-static void exposureFree(psExposure* exp)
-{
-    if (exp != NULL) {
-        psFree(exp->time);
-        psFree(exp->observatory);
-        psFree(exp->cameraName);
-        psFree(exp->telescopeName);
-    }
-}
-*/
-/*
-static void fixedPatternFree(psFixedPattern* fp)
-{
-    if (fp != NULL) {
-        for (psS32 i = 0; i < fp->p_ps_xRows; i++) {
-            psFree(fp->x[i]);
-        }
- 
-        for (psS32 j = 0; j < fp->p_ps_yRows; j++) {
-            psFree(fp->y[j]);
-        }
- 
-        psFree(fp->x);
-        psFree(fp->y);
-    }
-}
-*/
+        psFree(readout->analysis);
+        psFree(readout->concepts);
+        psFree(readout->parent);
+    }
+}
+
+
+pmReadout *pmReadoutAlloc(pmCell *cell)
+{
+    pmReadout *tmpReadout = (pmReadout *) psAlloc(sizeof(pmReadout));
+
+    tmpReadout->col0 = -1;
+    tmpReadout->row0 = -1;
+    tmpReadout->colBins = -1;
+    tmpReadout->rowBins = -1;
+    tmpReadout->image = NULL;
+    tmpReadout->mask = NULL;
+    tmpReadout->analysis = psMetadataAlloc();
+    tmpReadout->concepts = psMetadataAlloc();
+    tmpReadout->parent = cell;
+
+    if (cell != NULL) {
+        cell->readouts = psArrayAdd(cell->readouts, 1, (psPtr) tmpReadout);
+    }
+    psMemSetDeallocator(tmpReadout, (psFreeFunc) readoutFree);
+    return(tmpReadout);
+}
+
+pmCell *pmCellAlloc(pmChip *chip)
+{
+    pmCell *tmpCell = (pmCell *) psAlloc(sizeof(pmCell));
+
+    tmpCell->col0 = -1;
+    tmpCell->row0 = -1;
+    tmpCell->toChip = NULL;
+    tmpCell->toFPA = NULL;
+    tmpCell->toSky = NULL;
+    tmpCell->analysis = psMetadataAlloc();
+    tmpCell->concepts = psMetadataAlloc();
+    tmpCell->readouts = psArrayAlloc(0);
+    tmpCell->parent = chip;
+    if (chip != NULL) {
+        chip->cells = psArrayAdd(chip->cells, 1, (psPtr) tmpCell);
+    }
+    tmpCell->valid = false;
+    tmpCell->extname = NULL;
+    tmpCell->pixels = NULL;
+    tmpCell->header = NULL;
+
+    psMemSetDeallocator(tmpCell, (psFreeFunc) cellFree);
+    return(tmpCell);
+}
+
+pmChip *pmChipAlloc(pmFPA *fpa)
+{
+    pmChip *tmpChip = (pmChip *) psAlloc(sizeof(pmChip));
+
+    tmpChip->col0 = -1;
+    tmpChip->row0 = -1;
+    tmpChip->toFPA = NULL;
+    tmpChip->fromFPA = NULL;
+    tmpChip->concepts = psMetadataAlloc();
+    tmpChip->analysis = psMetadataAlloc();
+    tmpChip->cells = psArrayAlloc(0);
+    tmpChip->parent = fpa;
+    if (fpa != NULL) {
+        fpa->chips = psArrayAdd(fpa->chips, 1, (psPtr) tmpChip);
+    }
+    tmpChip->valid = false;
+    tmpChip->extname = NULL;
+    tmpChip->pixels = NULL;
+    tmpChip->header = NULL;
+
+    psMemSetDeallocator(tmpChip, (psFreeFunc) chipFree);
+    return(tmpChip);
+}
+
+pmFPA *pmFPAAlloc(
+    const psMetadata *camera,
+    psDB *db)
+{
+    pmFPA *tmpFPA = (pmFPA *) psAlloc(sizeof(pmFPA));
+
+    tmpFPA->fromTangentPlane = NULL;
+    tmpFPA->toTangentPlane = NULL;
+    tmpFPA->projection = NULL;
+    tmpFPA->concepts = psMetadataAlloc();
+    tmpFPA->analysis = psMetadataAlloc();
+    tmpFPA->camera = camera;
+    tmpFPA->chips = psArrayAlloc(0);
+    tmpFPA->header = NULL;
+    tmpFPA->db = db;
+
+    psMemSetDeallocator(tmpFPA, (psFreeFunc) FPAFree);
+    return(tmpFPA);
+}
+
+bool cellCheckParents(pmCell *cell)
+{
+    if (cell == NULL) {
+        return(true);
+    }
+    psBool flag = true;
+
+    for (psS32 i = 0 ; i < cell->readouts->n ; i++) {
+        pmReadout *tmpReadout = (pmReadout *) cell->readouts->data[i];
+        PS_ASSERT_PTR_NON_NULL(tmpReadout, false);
+        if (tmpReadout->parent != cell) {
+            tmpReadout->parent = cell;
+            flag = false;
+        }
+    }
+    return(flag);
+}
+
+bool chipCheckParents(pmChip *chip)
+{
+    if (chip == NULL) {
+        return(true);
+    }
+    psBool flag = true;
+
+    for (psS32 i = 0 ; i < chip->cells->n ; i++) {
+        pmCell *tmpCell = (pmCell *) chip->cells->data[i];
+        PS_ASSERT_PTR_NON_NULL(tmpCell, false);
+        if (tmpCell->parent != chip) {
+            tmpCell->parent = chip;
+            flag = false;
+        }
+
+        flag &= cellCheckParents(tmpCell);
+    }
+    return(flag);
+}
+
+bool pmFPACheckParents(pmFPA *fpa)
+{
+    if (fpa == NULL) {
+        return(true);
+    }
+    psBool flag = true;
+
+    for (psS32 i = 0 ; i < fpa->chips->n ; i++) {
+        pmChip *tmpChip = (pmChip *) fpa->chips->data[i];
+        PS_ASSERT_PTR_NON_NULL(tmpChip, false);
+        if (tmpChip->parent != fpa) {
+            tmpChip->parent = fpa;
+            flag = false;
+        }
+
+        flag &= chipCheckParents(tmpChip);
+    }
+    return(flag);
+}
+
+
+
+// HEY
 /*****************************************************************************/
 /* FUNCTION IMPLEMENTATION - PUBLIC                                          */
 /*****************************************************************************/
 
-/*
- * XXX: Verify that you interpreted the SDR correctly.
- *
- * XXX: This assumes that x,y must be of type F64
- */
-/*
-psFixedPattern* psFixedPatternAlloc(double x0,
-                                   double y0,
-                                   double xScale,
-                                   double yScale,
-                                   const psImage *x,
-                                   const psImage *y)
-{
-   psFixedPattern *tmp;
-   psS32 i;
-   psS32 j;
- 
-   PS_ASSERT_IMAGE_NON_NULL(x, NULL);
-   PS_ASSERT_IMAGE_NON_NULL(y, NULL);
-   PS_ASSERT_IMAGE_TYPE(x, PS_TYPE_F64, NULL);
-   PS_ASSERT_IMAGE_TYPE(y, PS_TYPE_F64, NULL);
- 
-   tmp = (psFixedPattern *) psAlloc(sizeof(psFixedPattern));
-   // XXX: Is this correct?
-   tmp->nX = (x->numCols * x->numRows);
-   tmp->nY = (y->numCols * y->numRows);
-   tmp->x0 = x0;
-   tmp->y0 = y0;
-   tmp->xScale = xScale;
-   tmp->yScale = yScale;
-   tmp->p_ps_xRows = x->numRows;
-   tmp->p_ps_xCols = x->numCols;
-   tmp->p_ps_yRows = y->numRows;
-   tmp->p_ps_yCols = y->numCols;
-   tmp->x = (double **) psAlloc(x->numRows * sizeof(double *));
-   for (i=0;i<x->numRows;i++) {
-       (tmp->x)[i] = (double *) psAlloc(x->numCols * sizeof(double));
-   }
-   for (i=0;i<x->numRows;i++) {
-       for (j=0;j<x->numCols;j++) {
-           (tmp->x)[i][j] = x->data.F64[i][j];
-       }
-   }
- 
-   tmp->y = (double **) psAlloc(y->numRows * sizeof(double *));
-   for (i=0;i<y->numRows;i++) {
-       (tmp->y)[i] = (double *) psAlloc(y->numCols * sizeof(double));
-   }
-   for (i=0;i<y->numRows;i++) {
-       for (j=0;j<y->numCols;j++) {
-           (tmp->y)[i][j] = y->data.F64[i][j];
-       }
-   }
- 
-   psMemSetDeallocator(tmp,(psFreeFunc)fixedPatternFree);
- 
-   return(tmp);
-}
-*/
-/*
-psExposure* psExposureAlloc(double ra,
-                            double dec,
-                            double hourAngle,
-                            double zenithDistance,
-                            double azimuth,
-                            const psTime* time,
-                            float rotAngle,
-                            float temperature,
-                            float pressure,
-                            float humidity,
-                            float exposureTime,
-                            float wavelength,
-                            const psObservatory* observatory)
-{
-    PS_ASSERT_PTR_NON_NULL(observatory, NULL);
- 
-    psExposure* exp = psAlloc(sizeof(psExposure));
-    *(double *)&exp->ra = ra;
-    *(double *)&exp->dec = dec;
-    *(double *)&exp->hourAngle = hourAngle;
-    *(double *)&exp->zenithDistance = zenithDistance;
-    *(double *)&exp->azimuth = azimuth;
-    *(float *)&exp->rotAngle = rotAngle;
-    *(float *)&exp->temperature = temperature;
-    *(float *)&exp->pressure = pressure;
-    *(float *)&exp->humidity = humidity;
-    *(float *)&exp->exposureTime = exposureTime;
-    *(float *)&exp->wavelength = wavelength;
- 
-    exp->time = psMemIncrRefCounter((psPtr)time);
-    exp->observatory = psMemIncrRefCounter((psPtr)observatory);
- 
-    // XXX: how is this value derived?
-    *(double *)&exp->lst = psTimeToLMST((psTime*)time,observatory->longitude);
-    *(float *)&exp->positionAngle = 0.0f; // XXX: need input, see Bug #207
-    *(float *)&exp->parallacticAngle = 0.0f; // XXX: need input, see Bug #207
-    *(float *)&exp->airmass = 0.0f; // XXX: needs calculation!  = slaAirmas(zenithDistance);
-    *(float *)&exp->parallacticFactor = 0.0f;
-    exp->cameraName = NULL;
-    exp->telescopeName = NULL;
- 
-    psMemSetDeallocator(exp,(psFreeFunc)exposureFree);
- 
-    return exp;
-}
-*/
-/*
-psObservatory* psObservatoryAlloc(const char* name,
-                                  double latitude,
-                                  double longitude,
-                                  double height,
-                                  double tlr)
-{
-    psObservatory* obs = psAlloc(sizeof(psObservatory));
- 
-    if (name == NULL) {
-        obs->name = NULL;
-    } else {
-        obs->name = psAlloc(strlen(name)+1);
-        strcpy((char*)obs->name, name);
-    }
- 
-    *(double *)&obs->latitude = latitude;
-    *(double *)&obs->longitude = longitude;
-    *(double *)&obs->height = height;
-    *(double *)&obs->tlr = tlr;
- 
-    psMemSetDeallocator(obs,(psFreeFunc)observatoryFree);
- 
-    return obs;
-}
-*/
-/*
-psFPA* psFPAAlloc(psS32 nChips,
-                  const psExposure* exp)
-{
-    PS_ASSERT_INT_NONNEGATIVE(nChips, NULL);
- 
-    psFPA* newFPA = psAlloc(sizeof(psFPA));
- 
-    // create array of NULL chips of the size nChips
-    newFPA->chips = psArrayAlloc(nChips);
-    psPtr* chips = newFPA->chips->data;
-    for (psS32 i=0;i<nChips;i++) {
-        chips[i] = NULL;
-    }
-    newFPA->chips->n = 0; // per requirement
- 
-    newFPA->metadata = NULL;
-    newFPA->fromTangentPlane = NULL;
-    newFPA->toTangentPlane = NULL;
-    newFPA->pattern = NULL;
- 
-    if (exp != NULL) {
-        newFPA->exposure = psMemIncrRefCounter((psExposure*)exp);
-        newFPA->grommit = psGrommitAlloc(exp);
-    } else {
-        newFPA->exposure = NULL;
-        newFPA->grommit = NULL;
-    }
- 
-    newFPA->colorPlus = NULL;
-    newFPA->colorMinus = NULL;
-    newFPA->projection = NULL;
- 
-    newFPA->rmsX = 0.0f;
-    newFPA->rmsY = 0.0f;
-    newFPA->chi2 = 0.0f;
- 
-    psMemSetDeallocator(newFPA,(psFreeFunc)FPAFree);
- 
-    return newFPA;
-}
-*/
-/*
- * psChip constructor
- */
-/*
-psChip* psChipAlloc(psS32 nCells,
-                    psFPA *parentFPA)
-{
-    PS_ASSERT_INT_NONNEGATIVE(nCells, NULL);
- 
-    psChip* chip = psAlloc(sizeof(psChip));
- 
-    // create array of NULL psCells
-    int n = (nCells > 0) ? nCells : 1;
-    chip->cells = psArrayAlloc(n);
-    psPtr* cells = chip->cells->data;
-    for (psS32 i=0;i<n;i++) {
-        cells[i] = NULL;
-    }
-    chip->cells->n = 0; // per requirement
- 
-    *(int*)&chip->row0 = 0;
-    *(int*)&chip->col0 = 0;
- 
-    chip->metadata = NULL;
- 
-    chip->toFPA = NULL;
-    chip->fromFPA = NULL;
- 
-    chip->parent = parentFPA;
- 
-    psMemSetDeallocator(chip,(psFreeFunc)chipFree);
- 
-    return chip;
- 
-}
-*/
-/*
- * psCell constructor
- */
-/*
-psCell* psCellAlloc(psS32 nReadouts,
-                    psChip* parentChip)
-{
-    PS_ASSERT_INT_NONNEGATIVE(nReadouts, NULL);
- 
-    psCell* cell = psAlloc(sizeof(psCell));
- 
-    // create array of NULL psReadouts
-    int n = (nReadouts > 0) ? nReadouts : 1;
-    cell->readouts = psArrayAlloc(n);
-    psPtr* readouts = cell->readouts->data;
-    for (psS32 i=0;i<n;i++) {
-        readouts[i] = NULL;
-    }
-    cell->readouts->n = 0; // per requirement
- 
-    *(int*)&cell->row0 = 0;
-    *(int*)&cell->col0 = 0;
- 
-    cell->metadata = NULL;
- 
-    cell->toChip = NULL;
-    cell->fromChip = NULL;
-    cell->toFPA = NULL;
-    cell->toTP = NULL;
-    cell->toSky = NULL;
- 
-    cell->parent = parentChip;
- 
-    psMemSetDeallocator(cell,(psFreeFunc)cellFree);
- 
-    return cell;
- 
- 
-}
-*/
-
-psReadout *pmReadoutAlloc(pmCell *cell)
-{
-    psReadout* readout = psAlloc(sizeof(psReadout));
-
-    *(psU32*)&readout->colBins = 1;
-    *(psU32*)&readout->rowBins = 1;
-    *(psU32*)&readout->rowParity = 0;
-    *(psU32*)&readout->colParity = 0;
-    *(psS32*)&readout->col0 = 0;
-    *(psS32*)&readout->row0 = 0;
-
-    readout->image = NULL;
-    readout->mask = NULL;
-    readout->objects = NULL;
-    readout->metadata = NULL;
-
-    psMemSetDeallocator(readout,(psFreeFunc)readoutFree);
-
-    return readout;
-}
-
-/*
-psGrommit* psGrommitAlloc(const psExposure* exp)
-{
-    PS_ASSERT_PTR_NON_NULL(exp, NULL);
- 
-    psSphere* polarMotion = p_psTimeGetPoleCoords(exp->time);
- 
-    psGrommit* grommit = (psGrommit* ) psAlloc(sizeof(psGrommit));
- 
-    *(double*)&grommit->latitude = exp->observatory->latitude;
-    *(double*)&grommit->longitude = exp->observatory->longitude;
-    *(double*)&grommit->height = exp->observatory->height;
-    *(double*)&grommit->abberationMag = 0.0; // XXX: need to figure out what to set here.
-    *(double*)&grommit->temperature = exp->temperature;
-    *(double*)&grommit->pressure = exp->pressure;
-    *(double*)&grommit->humidity = exp->humidity;
-    *(double*)&grommit->wavelength = exp->wavelength;
-    *(double*)&grommit->lapseRate = exp->observatory->tlr;
-    *(double*)&grommit->refractA = polarMotion->r; // XXX: need to figure out what to set here too.
-    *(double*)&grommit->refractB = polarMotion->d; // XXX: need to figure out what to set here too.
-    *(double*)&grommit->siderealTime = psTimeToMJD(exp->time); // XXX: this is probably not correct
- 
-    psFree(polarMotion);
- 
-    return (grommit);
-}
-*/
-/*
-psCell* psCellInFPA(const psPlane* fpaCoord,
-                    const psFPA* FPA)
+pmCell* pmCellInFPA(const psPlane* fpaCoord,
+                    const pmFPA* FPA)
 {
     PS_ASSERT_PTR_NON_NULL(fpaCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(FPA, NULL);
- 
-    psChip* tmpChip = NULL;
+
+    pmChip* tmpChip = NULL;
     psPlane chipCoord;
-    psCell* outCell = NULL;
- 
+    pmCell* outCell = NULL;
+
     // Determine which chip contains the fpaCoords.
-    tmpChip = psChipInFPA(fpaCoord, FPA);
+    tmpChip = pmChipInFPA(fpaCoord, FPA);
     if (tmpChip == NULL) {
         return(NULL);
     }
- 
+
     // Convert to those chip coordinates.
     psCoordFPAToChip(&chipCoord, fpaCoord, tmpChip);
- 
+
     // Determine which cell contains those chip coordinates.
-    outCell = psCellInChip(&chipCoord, tmpChip);
- 
+    outCell = pmCellInChip(&chipCoord, tmpChip);
+
     return (outCell);
 }
-*/
-/*
-psChip* psChipInFPA(const psPlane* fpaCoord,
-                    const psFPA* FPA)
+
+pmChip* pmChipInFPA(const psPlane* fpaCoord,
+                    const pmFPA* FPA)
 {
     PS_ASSERT_PTR_NON_NULL(fpaCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(FPA, NULL);
     PS_ASSERT_PTR_NON_NULL(FPA->chips, NULL);
- 
+
     psArray* chips = FPA->chips;
     psS32 nChips = chips->n;
     psPlane chipCoord;
-    psCell *tmpCell = NULL;
- 
+    pmCell *tmpCell = NULL;
+
     // Loop through every chip in this FPA.  Convert the original FPA
     // coordinates to chip coordinates for that chip.  Then, determine if any
     // cells in that chip contain those chip coordinates.
- 
+
     for (psS32 i = 0; i < nChips; i++) {
-        psChip* tmpChip = chips->data[i];
+        pmChip* tmpChip = chips->data[i];
         PS_ASSERT_PTR_NON_NULL(tmpChip, NULL);
         PS_ASSERT_PTR_NON_NULL(tmpChip->fromFPA, NULL);
- 
+
         psPlaneTransformApply(&chipCoord, tmpChip->fromFPA, fpaCoord);
- 
-        tmpCell = psCellInChip(&chipCoord, tmpChip);
+
+        tmpCell = pmCellInChip(&chipCoord, tmpChip);
         if (tmpCell != NULL) {
             return(tmpChip);
         }
     }
- 
+
     // XXX: Print warning here?
     return (NULL);
 }
-*/
-/*
-psCell* psCellInChip(const psPlane* chipCoord,
-                     const psChip* chip)
+
+
+pmCell* pmCellInChip(const psPlane* chipCoord,
+                     const pmChip* chip)
 {
     PS_ASSERT_PTR_NON_NULL(chipCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(chip, NULL);
- 
+
     psPlane cellCoord;
     psArray* cells;
- 
+
     cells = chip->cells;
     if (cells == NULL) {
         return NULL;
     }
- 
+
     // We loop over each cell in the chip.  We transform the chipCoord into
     // a cellCoord for that cell and determine if that cellCoord is valid.
     // If so, then we return that cell.
- 
+
     for (psS32 i = 0; i < cells->n; i++) {
-        psCell* tmpCell = (psCell* ) cells->data[i];
+        pmCell* tmpCell = (pmCell* ) cells->data[i];
         PS_ASSERT_PTR_NON_NULL(tmpCell, NULL);
-        PS_ASSERT_PTR_NON_NULL(tmpCell->fromChip, NULL);
+
+        psPlaneTransform *chipToCell = NULL;
+        if (1 != p_psIsProjectionLinear(tmpCell->toChip)) {
+            // XXX: Generate warning message.
+            // XXX: Can we use the following function to derive a transform?
+            // chipToCell = psPlaneTransformInvert(NULL, tmpCell->toChip, NULL, -1);
+        } else {
+            chipToCell = p_psPlaneTransformLinearInvert(tmpCell->toChip);
+        }
+
+        PS_ASSERT_PTR_NON_NULL(chipToCell, NULL);
         psArray* readouts = tmpCell->readouts;
- 
+
         if (readouts != NULL) {
             for (psS32 j = 0; j < readouts->n; j++) {
-                psReadout* tmpReadout = readouts->data[j];
+                pmReadout* tmpReadout = readouts->data[j];
                 PS_ASSERT_READOUT_NON_NULL(tmpReadout, NULL);
- 
+
                 psPlaneTransformApply(&cellCoord,
-                                      tmpCell->fromChip,
+                                      chipToCell,
                                       chipCoord);
- 
+
                 if (checkValidImageCoords(cellCoord.x,
                                           cellCoord.y,
@@ -540,85 +371,88 @@
             }
         }
-    }
- 
+        psFree(chipToCell);
+    }
+
     return (NULL);
 }
-*/
-/*
+
+
 psPlane* psCoordCellToChip(psPlane* outCoord,
                            const psPlane* inCoord,
-                           const psCell* cell)
+                           const pmCell* cell)
 {
     PS_ASSERT_PTR_NON_NULL(inCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(cell, NULL);
- 
+
     return (psPlaneTransformApply(outCoord, cell->toChip, inCoord));
 }
-*/
-/*
+
+
 psPlane* psCoordChipToFPA(psPlane* outCoord,
                           const psPlane* inCoord,
-                          const psChip* chip)
+                          const pmChip* chip)
 {
     PS_ASSERT_PTR_NON_NULL(inCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(chip, NULL);
- 
+
     return (psPlaneTransformApply(outCoord, chip->toFPA, inCoord));
 }
-*/
-/*
+
+
+
 psPlane* psCoordFPAToTP(psPlane* outCoord,
                         const psPlane* inCoord,
                         double color,
                         double magnitude,
-                        const psFPA* fpa)
+                        const pmFPA* fpa)
 {
     PS_ASSERT_PTR_NON_NULL(inCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(fpa, NULL);
- 
+
     return(psPlaneDistortApply(outCoord, fpa->toTangentPlane, inCoord,
                                color, magnitude));
 }
-*/
+
 /*****************************************************************************
 XXX: What about units for the (x,y) coords?
  *****************************************************************************/
-/*
 psSphere* psCoordTPToSky(psSphere* outSphere,
                          const psPlane* tpCoord,
-                         const psGrommit* grommit)
+                         const psProjection *projection
+                        )
 {
     PS_ASSERT_PTR_NON_NULL(tpCoord, NULL);
-    PS_ASSERT_PTR_NON_NULL(grommit, NULL);
- 
+
     if (outSphere == NULL) {
         outSphere = (psSphere* ) psAlloc(sizeof(psSphere));
     }
- 
+
     // XXX: this was done by a SLALIB call -- needs to be reimplemented
     psWarning("Warning!  psCoordTPToSky functionality is no longer implemented");
     // slaAopqk(tpCoord->x, tpCoord->y, (double*)grommit,
     //         &AOB, &ZOB, &HOB, &outSphere->r, &outSphere->d);
- 
+
     return (outSphere);
 }
-*/
-/*
+
+
+
 psPlane* psCoordCellToFPA(psPlane* fpaCoord,
                           const psPlane* cellCoord,
-                          const psCell* cell)
+                          const pmCell* cell)
 {
     PS_ASSERT_PTR_NON_NULL(cellCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(cell, NULL);
- 
+
     return (psPlaneTransformApply(fpaCoord, cell->toFPA, cellCoord));
 }
-*/
-/*
+
+
+
 psSphere* psCoordCellToSky(psSphere* skyCoord,
                            const psPlane* cellCoord,
                            double color,
                            double magnitude,
-                           const psCell* cell)
+                           const pmCell* cell)
 {
     PS_ASSERT_PTR_NON_NULL(cellCoord, NULL);
@@ -628,36 +462,35 @@
     PS_ASSERT_PTR_NON_NULL(cell->parent->parent, NULL);
     PS_ASSERT_PTR_NON_NULL(cell->parent->parent->toTangentPlane, NULL);
-    PS_ASSERT_PTR_NON_NULL(cell->parent->parent->exposure, NULL);
- 
+    //    PS_ASSERT_PTR_NON_NULL(cell->parent->parent->exposure, NULL);
+
     psPlane* fpaCoord = NULL;
     psPlane* tpCoord = NULL;
-    psFPA* parFPA = (cell->parent)->parent;
-    psGrommit* tmpGrommit = NULL;
- 
+    pmFPA* parFPA = (cell->parent)->parent;
+    //    psGrommit* tmpGrommit = NULL;
+
     // Convert the input cell coordinates to FPA coordinates.
     fpaCoord = psPlaneTransformApply(fpaCoord, cell->toFPA, cellCoord);
- 
+
     // Convert the FPA coordinates to tangent plane Coordinates.
     tpCoord = psPlaneDistortApply(tpCoord, parFPA->toTangentPlane,
                                   fpaCoord, color, magnitude);
- 
+
     // Generate a grommit for this FPA.
-    tmpGrommit = psGrommitAlloc(parFPA->exposure);
- 
+    //    tmpGrommit = psGrommitAlloc(parFPA->exposure);
+
     // Convert the tangent plane Coordinates to sky coordinates.
-    skyCoord = psCoordTPToSky(skyCoord, tpCoord, tmpGrommit);
- 
+    //    skyCoord = psCoordTPToSky(skyCoord, tpCoord, tmpGrommit);
+
     psFree(fpaCoord);
     psFree(tpCoord);
-    psFree(tmpGrommit);
- 
+    //    psFree(tmpGrommit);
+
     return(skyCoord);
 }
- 
-*/
-/*
+
+
 psSphere* psCoordCellToSkyQuick(psSphere* outSphere,
                                 const psPlane* cellCoord,
-                                const psCell* cell)
+                                const pmCell* cell)
 {
     PS_ASSERT_PTR_NON_NULL(cellCoord, NULL);
@@ -667,108 +500,119 @@
     PS_ASSERT_PTR_NON_NULL(cell->parent->parent, NULL);
     PS_ASSERT_PTR_NON_NULL(cell->parent->parent->projection, NULL);
-    if (cell->toSky) {
-        // XXX: Should we use toTP or toSky?
-        psLogMsg(__func__, PS_LOG_WARN,
-                 "WARNING: psCoordCellToSkyQuick(): The cell->toSky transform is ignored.  The cell->toTP transform is being used.");
-    }
- 
-    psPlane *tpCoord = NULL;
-    psChip *chip = cell->parent;
-    psFPA *FPA = chip->parent;
-    psProjectionType oldProjectionType;
- 
-    if (outSphere == NULL) {
-        outSphere = (psSphere* ) psAlloc(sizeof(psSphere));
-    }
- 
-    // Determine the tangent plane coordinates.
-    tpCoord = psPlaneTransformApply(NULL, cell->toTP, cellCoord);
- 
-    // Save the old projection type and set the new projection type to TAN.
-    oldProjectionType = FPA->projection->type;
-    FPA->projection->type = PS_PROJ_TAN;
- 
-    // Deproject the tangent plane coordinates a sphere.
-    outSphere = psDeproject(tpCoord, FPA->projection);
- 
-    // Restore old projection type.  Free memory.
-    FPA->projection->type = oldProjectionType;
-    psFree(tpCoord);
- 
-    return (outSphere);
-}
-*/
+
+    psLogMsg(__func__, PS_LOG_WARN,
+             "WARNING: psCoordCellToSkyQuick(): This function is not fully specified in the SDRS.  Returning NULL.\n");
+    return(NULL);
+
+    /*
+        if (cell->toSky) {
+            // XXX: Should we use toTP or toSky?
+            psLogMsg(__func__, PS_LOG_WARN,
+                     "WARNING: psCoordCellToSkyQuick(): The cell->toSky transform is ignored.  The cell->toTP transform is being used.");
+        }
+     
+        psPlane *tpCoord = NULL;
+        pmChip *chip = cell->parent;
+        pmFPA *FPA = chip->parent;
+        psProjectionType oldProjectionType;
+     
+        if (outSphere == NULL) {
+            outSphere = (psSphere* ) psAlloc(sizeof(psSphere));
+        }
+     
+        // Determine the tangent plane coordinates.
+        tpCoord = psPlaneTransformApply(NULL, cell->toTP, cellCoord);
+     
+        // Save the old projection type and set the new projection type to TAN.
+        oldProjectionType = FPA->projection->type;
+        FPA->projection->type = PS_PROJ_TAN;
+     
+        // Deproject the tangent plane coordinates a sphere.
+        outSphere = psDeproject(tpCoord, FPA->projection);
+     
+        // Restore old projection type.  Free memory.
+        FPA->projection->type = oldProjectionType;
+        psFree(tpCoord);
+     
+        return (outSphere);
+    */
+}
+
 
 /*****************************************************************************
 XXX: What about units for the (x,y) coords?
  *****************************************************************************/
-/*
 psPlane* psCoordSkyToTP(psPlane* tpCoord,
                         const psSphere* in,
-                        const psGrommit* grommit)
+                        const psProjection *projection)
 {
     PS_ASSERT_PTR_NON_NULL(in, NULL);
-    PS_ASSERT_PTR_NON_NULL(grommit, NULL);
- 
+    //    PS_ASSERT_PTR_NON_NULL(grommit, NULL);
+
     // char* type = "RA";
- 
+
     if (tpCoord == NULL) {
         tpCoord = (psPlane* ) psAlloc(sizeof(psPlane));
     }
- 
+
     // XXX: this was done by a SLALIB call -- needs to be reimplemented
     psWarning("Warning!  psCoordSkyToTP functionality is no longer implemented");
     // slaOapqk(type, in->r, in->d, (double*)grommit, &tpCoord->x, &tpCoord->y);
- 
+
     return(tpCoord);
 }
-*/
-/*
+
+
 psPlane* psCoordTPToFPA(psPlane* fpaCoord,
                         const psPlane* tpCoord,
                         double color,
                         double magnitude,
-                        const psFPA* fpa)
+                        const pmFPA* fpa)
 {
     PS_ASSERT_PTR_NON_NULL(tpCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(fpa, NULL);
     PS_ASSERT_PTR_NON_NULL(fpa->fromTangentPlane, NULL);
- 
+
     return (psPlaneDistortApply(fpaCoord, fpa->fromTangentPlane,
                                 tpCoord, color, magnitude));
 }
-*/
-/*
+
+
 psPlane* psCoordFPAToChip(psPlane* chipCoord,
                           const psPlane* fpaCoord,
-                          const psChip* chip)
+                          const pmChip* chip)
 {
     PS_ASSERT_PTR_NON_NULL(fpaCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(chip, NULL);
     PS_ASSERT_PTR_NON_NULL(chip->fromFPA, NULL);
- 
+
     chipCoord = psPlaneTransformApply(chipCoord, chip->fromFPA, fpaCoord);
     return(chipCoord);
 }
-*/
-/*
+
+
 psPlane* psCoordChipToCell(psPlane* cellCoord,
                            const psPlane* chipCoord,
-                           const psCell* cell)
+                           const pmCell* cell)
 {
     PS_ASSERT_PTR_NON_NULL(chipCoord, NULL);
     PS_ASSERT_PTR_NON_NULL(cell, NULL);
-    PS_ASSERT_PTR_NON_NULL(cell->fromChip, NULL);
- 
-    cellCoord = psPlaneTransformApply(cellCoord, cell->fromChip, chipCoord);
+    PS_ASSERT_PTR_NON_NULL(cell->parent, NULL);
+
+    pmCell *tmpCell = pmCellInChip(chipCoord, cell->parent);
+    PS_ASSERT_PTR_NON_NULL(tmpCell->toChip, NULL);
+    psPlaneTransform *tmpChipToCell = p_psPlaneTransformLinearInvert(tmpCell->toChip);
+    PS_ASSERT_PTR_NON_NULL(tmpChipToCell, NULL);
+    cellCoord = psPlaneTransformApply(cellCoord, tmpChipToCell, chipCoord);
+    psFree(tmpChipToCell);
     return(cellCoord);
 }
-*/
-/*
+
+
 psPlane* psCoordSkyToCell(psPlane* cellCoord,
                           const psSphere* skyCoord,
-                          double color,
-                          double magnitude,
-                          const psCell* cell)
+                          float color,
+                          float magnitude,
+                          const pmCell* cell)
 {
     PS_ASSERT_PTR_NON_NULL(skyCoord, NULL);
@@ -776,34 +620,32 @@
     PS_ASSERT_PTR_NON_NULL(cell->parent, NULL);
     PS_ASSERT_PTR_NON_NULL(cell->parent->parent, NULL);
-    PS_ASSERT_PTR_NON_NULL(cell->parent->parent->grommit, NULL);
- 
-    psChip *parChip = cell->parent;
-    psFPA *parFPA = parChip->parent;
-    psGrommit* grommit = parFPA->grommit;
- 
+    //    PS_ASSERT_PTR_NON_NULL(cell->parent->parent->grommit, NULL);
+
+    pmChip *parChip = cell->parent;
+    pmFPA *parFPA = parChip->parent;
+
     // Convert the skyCoords to tangent plane coords.
-    psPlane *tpCoord = psCoordSkyToTP(tpCoord, skyCoord, grommit);
- 
+    psPlane *tpCoord = psCoordSkyToTP(NULL, skyCoord, parFPA->projection);
+
     // Convert the tangent plane coords to FPA coords.
-    psPlane *fpaCoord = psCoordTPToFPA(fpaCoord, tpCoord, color,
-                                       magnitude, parFPA);
- 
+    psPlane *fpaCoord = psCoordTPToFPA(NULL, tpCoord, color, magnitude, parFPA);
+
     // Convert the FPA coords to chip coords.
-    psPlane *chipCoord = psCoordFPAToChip(chipCoord, fpaCoord, parChip);
- 
+    psPlane *chipCoord = psCoordFPAToChip(NULL, fpaCoord, parChip);
+
     // Convert the chip coords to cell coords.
     cellCoord = psCoordChipToCell(cellCoord, chipCoord, cell);
- 
+
     psFree(tpCoord);
     psFree(fpaCoord);
     psFree(chipCoord);
- 
+
     return (cellCoord);
 }
-*/
-/*
+
+
 psPlane* psCoordSkyToCellQuick(psPlane* cellCoord,
                                const psSphere* skyCoord,
-                               const psCell* cell)
+                               const pmCell* cell)
 {
     PS_ASSERT_PTR_NON_NULL(skyCoord, NULL);
@@ -812,43 +654,50 @@
     PS_ASSERT_PTR_NON_NULL(cell->parent->parent, NULL);
     PS_ASSERT_PTR_NON_NULL(cell->parent->parent->projection, NULL);
-    PS_ASSERT_PTR_NON_NULL(cell->toTP, NULL);
-    if (cell->toSky) {
-        // XXX: Should we use toTP or toSky?
-        psLogMsg(__func__, PS_LOG_WARN,
-                 "WARNING: psCoordSkyToCellQuick(): The cell->toSky transform is ignored.  The cell->toTP transform is being used.");
-    }
- 
-    psPlane *tpCoord = NULL;
-    psChip *whichChip = cell->parent;
-    psFPA *whichFPA = whichChip->parent;
-    psProjectionType oldProjectionType;
-    psPlaneTransform *TPtoCell = NULL;
- 
-    // Save the old projection type and set the new projection type to TAN.
-    oldProjectionType = whichFPA->projection->type;
-    whichFPA->projection->type = PS_PROJ_TAN;
- 
-    if (cellCoord == NULL) {
-        cellCoord = (psPlane* ) psAlloc(sizeof(psPlane));
-    }
- 
-    tpCoord = psProject(skyCoord, whichFPA->projection);
- 
-    // generate an error if cell->toTP is not linear.
-    if (0 == p_psIsProjectionLinear(cell->toTP)) {
-        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
-                PS_ERRORTEXT_psAstrometry_NONLINEAR_TRANSFORM,
-                "cell to tangent plane");
-    }
- 
-    TPtoCell = p_psPlaneTransformLinearInvert(cell->toTP);
-    cellCoord = psPlaneTransformApply(cellCoord, TPtoCell, tpCoord);
- 
-    // Restore old projection type.  Free memory.
-    whichFPA->projection->type = oldProjectionType;
-    psFree(tpCoord);
-    return (cellCoord);
-}
-*/
-
-
+
+
+    psLogMsg(__func__, PS_LOG_WARN,
+             "WARNING: psCoordSkyToCellQuick(): This function is not fully specified in the SDRS.  Returning NULL.\n");
+    return(NULL);
+    /*
+     
+        if (cell->toSky) {
+            // XXX: Should we use toTP or toSky?
+            psLogMsg(__func__, PS_LOG_WARN,
+                     "WARNING: psCoordSkyToCellQuick: The cell->toSky transform is ignored.  The cell->toTP transform is being used.");
+        }
+     
+        psPlane *tpCoord = NULL;
+        pmChip *whichChip = cell->parent;
+        pmFPA  *whichFPA = whichChip->parent;
+        psProjectionType oldProjectionType;
+        psPlaneTransform *TPtoCell = NULL;
+     
+        // Save the old projection type and set the new projection type to TAN.
+        oldProjectionType = whichFPA->projection->type;
+        whichFPA->projection->type = PS_PROJ_TAN;
+     
+        if (cellCoord == NULL) {
+            cellCoord = (psPlane* ) psAlloc(sizeof(psPlane));
+        }
+     
+        tpCoord = psProject(skyCoord, whichFPA->projection);
+     
+        // generate an error if cell->toTP is not linear.
+        if (0 == p_psIsProjectionLinear(cell->toTP)) {
+            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                    PS_ERRORTEXT_psAstrometry_NONLINEAR_TRANSFORM,
+                    "cell to tangent plane");
+        }
+     
+        TPtoCell = p_psPlaneTransformLinearInvert(cell->toTP);
+        cellCoord = psPlaneTransformApply(cellCoord, TPtoCell, tpCoord);
+     
+        // Restore old projection type.  Free memory.
+        whichFPA->projection->type = oldProjectionType;
+        psFree(tpCoord);
+        return (cellCoord);
+    */
+}
+
+
+
