Index: trunk/psLib/test/astronomy/tst_psAstrometry01.c
===================================================================
--- trunk/psLib/test/astronomy/tst_psAstrometry01.c	(revision 2083)
+++ trunk/psLib/test/astronomy/tst_psAstrometry01.c	(revision 2096)
@@ -5,6 +5,6 @@
 *  @author GLG, MHPCC
 *
-*  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2004-10-13 21:08:11 $
+*  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2004-10-13 23:58:20 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -40,71 +40,147 @@
 #define HEIGHT  3055.0
 #define LAPSERATE 20.0
-#define NUM_CHIPS 2
-#define NUM_CELLS 4
+
 #define NUM_READOUTS 2
 #define READOUT_NUM_ROWS 8
 #define READOUT_NUM_COLS 8
+
+#define CELL_WIDTH READOUT_NUM_COLS
+#define CELL_HEIGHT READOUT_NUM_ROWS
+
+#define CELL_GAP 2
+
+#define NUM_CELLS 4
+#define CELL_MIN_X 0
+#define CELL_MAX_X CELL_WIDTH
+#define CELL_MIN_Y 0
+#define CELL_MAX_Y CELL_HEIGHT
+
+#define NUM_CHIPS 2
+#define CHIP_MIN_X 0
+#define CHIP_MAX_X ((NUM_CELLS * CELL_WIDTH) + ((NUM_CELLS) * CELL_GAP))
+#define CHIP_MIN_Y 0
+#define CHIP_MAX_Y CELL_HEIGHT
+
+#define CHIP_WIDTH CHIP_MAX_X
+#define CHIP_HEIGHT CHIP_MAX_Y
+#define CHIP_GAP 2
+
+#define FPA_MIN_X 0
+#define FPA_MAX_X ((NUM_CHIPS * CHIP_WIDTH) + ((NUM_CHIPS) * CHIP_GAP))
+#define FPA_MIN_Y 0
+#define FPA_MAX_Y CHIP_HEIGHT
+
 #define ROW0  0
 #define COL0  0
 
-int genSystem()
+static void psPlaneDistortFree(psPlaneDistort *pd)
+{
+    psFree(pd->x);
+    psFree(pd->y);
+}
+
+static void psPlaneTransformFree(psPlaneTransform *pd)
+{
+    psFree(pd->x);
+    psFree(pd->y);
+}
+
+#define PS_CREATE_2D_IDENTITY_PLANE_TRANSFORM(NAME) \
+{\
+    NAME = (psPlaneTransform *) psAlloc(sizeof(psPlaneTransform)); \
+    p_psMemSetDeallocator(NAME, (psFreeFcn) psPlaneTransformFree); \
+    NAME->x = psDPolynomial2DAlloc(2, 2, PS_POLYNOMIAL_ORD); \
+    NAME->y = psDPolynomial2DAlloc(2, 2, PS_POLYNOMIAL_ORD); \
+    NAME->x->coeff[1][0] = 1.0; \
+    NAME->y->coeff[0][1] = 1.0; \
+}
+
+#define PS_CREATE_4D_IDENTITY_PLANE_DISTORT(NAME) \
+{\
+    NAME = (psPlaneDistort *) psAlloc(sizeof(psPlaneDistort)); \
+    p_psMemSetDeallocator(NAME, (psFreeFcn) psPlaneDistortFree); \
+    NAME->x = psDPolynomial4DAlloc(2, 2, 2, 2, PS_POLYNOMIAL_ORD); \
+    NAME->y = psDPolynomial4DAlloc(2, 2, 2, 2, PS_POLYNOMIAL_ORD); \
+    NAME->x->coeff[1][0][0][0] = 1.0; \
+    NAME->y->coeff[0][1][0][0] = 1.0; \
+}
+
+/*
+    This routine will create a system of FPAs/Chips/Cells/Readouts.  For
+    simplicity, an FPA is defined as a linear array of chips, and a chip is
+    defined as a linear array of cells, both in the x direction.  The transforms
+    between the various layers take into account the cell/chip and the boundaries
+    between each.
+*/
+psFPA *genSystem()
 {
     int i;
     int j;
-    psTime* now = psTimeGetTime(PS_TIME_UTC);
-    psObservatory* obs = psObservatoryAlloc(NAME, LATITUDE, LONGITUDE, HEIGHT, LAPSERATE);
-    psExposure* exp = psExposureAlloc(RA, DEC, HA, ZD, AZ,
-                                      now, ROT_ANGLE, TEMPERATURE, PRESSURE, HUMIDITY,
-                                      EXP_TIME, WAVELENGTH, obs);
+    int k;
+    psImage *tmpImage;
+    //    psTime* now = psTimeGetTime(PS_TIME_UTC);
+    //    psObservatory* obs = psObservatoryAlloc(NAME, LATITUDE, LONGITUDE, HEIGHT, LAPSERATE);
+    //    psExposure* exp = psExposureAlloc(RA, DEC, HA, ZD, AZ,
+    //                                      now, ROT_ANGLE, TEMPERATURE, PRESSURE, HUMIDITY,
+    //                                      EXP_TIME, WAVELENGTH, obs);
     //    psGrommit *grom = psGrommitAlloc(exp);
-    psFPA *myFPA = psFPAAlloc(NUM_CHIPS, exp);
+    psFPA *myFPA = psFPAAlloc(NUM_CHIPS, NULL);
     psChip **chips;
     psCell **cells;
-
-    myFPA->fromTangentPlane = NULL;
-    myFPA->toTangentPlane = NULL;
+    psReadout **readouts;
+
+    // We create the transforms between the FPA and tangent plane.  The
+    // transform is a simple identity transform.
+    PS_CREATE_4D_IDENTITY_PLANE_DISTORT(myFPA->fromTangentPlane);
+    PS_CREATE_4D_IDENTITY_PLANE_DISTORT(myFPA->toTangentPlane);
 
     chips = (psChip **) myFPA->chips->data;
     for (i=0;i<NUM_CHIPS;i++) {
         chips[i] = psChipAlloc(NUM_CELLS, myFPA);
-        chips[i]->toFPA = NULL;
-        chips[i]->fromFPA = NULL;
+        // We create the transforms between the chip and FPA.  The
+        // transform is a simple identity transform.
+        PS_CREATE_2D_IDENTITY_PLANE_TRANSFORM(chips[i]->toFPA);
+        chips[i]->toFPA->x->coeff[0][0] = (double) (i * (CHIP_WIDTH + CHIP_GAP));
+        chips[i]->toFPA->x->coeff[1][1] = 0.0;
+        PS_CREATE_2D_IDENTITY_PLANE_TRANSFORM(chips[i]->fromFPA);
+        chips[i]->fromFPA->x->coeff[0][0] = (double) (-i * (CHIP_WIDTH + CHIP_GAP));
+        chips[i]->fromFPA->x->coeff[1][1] = 0.0;
+
         cells = (psCell **) chips[i]->cells->data;
 
         for (j=0;j<NUM_CELLS;j++) {
             cells[j] = psCellAlloc(NUM_READOUTS, chips[i]);
-            cells[j]->toChip = NULL;
-            cells[j]->fromChip = NULL;
-            cells[j]->toFPA = NULL;
-            cells[j]->toTP = NULL;
-            /*
-                        readouts = (psReadout **) cells[j]->readouts->data;
-                        for (k=0;k<NUM_READOUTS;k++) {
-                            tmpImage = psImageAlloc(READOUT_NUM_COLS, READOUT_NUM_ROWS, PS_TYPE_F32);
-                            readouts[k] = psReadoutAlloc(COL0, ROW0, tmpImage);
-                        }
-            */
-        }
-    }
-
-    psFree(now);
-    psFree(obs);
-    psFree(exp);
-    chips = (psChip **) myFPA->chips->data;
-    for (i=0;i<NUM_CHIPS;i++) {
-        cells = (psCell **) chips[i]->cells->data;
-        for (j=0;j<NUM_CELLS;j++) {
-            /*
-                        readouts = (psReadout **) cells[j]->readouts->data;
-                        for (k=0;k<NUM_READOUTS;k++) {
-                            psFree(readouts[k]);
-                        }
-            */
-            psFree(cells[j]);
-        }
-        psFree(chips[i]);
-    }
-    psFree(myFPA);
-    return(0);
+            // We create the transforms between the cell and the chip.  The
+            // transform is a simple identity transform.
+            PS_CREATE_2D_IDENTITY_PLANE_TRANSFORM(cells[j]->toChip);
+            cells[j]->toChip->x->coeff[0][0] = (double) (j * (CELL_WIDTH + CELL_GAP));
+            cells[j]->toChip->x->coeff[1][1] = 0.0;
+            PS_CREATE_2D_IDENTITY_PLANE_TRANSFORM(cells[j]->fromChip);
+            cells[j]->fromChip->x->coeff[0][0] = (double) (-j * (CELL_WIDTH + CELL_GAP));
+            cells[j]->fromChip->x->coeff[1][1] = 0.0;
+            PS_CREATE_2D_IDENTITY_PLANE_TRANSFORM(cells[j]->toFPA);
+            cells[j]->toFPA->x->coeff[0][0] = (double) ((i * (CHIP_WIDTH + CHIP_GAP)) +
+                                              (j * (CELL_WIDTH + CELL_GAP)));
+            cells[j]->toFPA->x->coeff[1][1] = 0.0;
+            PS_CREATE_2D_IDENTITY_PLANE_TRANSFORM(cells[j]->toTP);
+            cells[j]->toTP->x->coeff[0][0] = cells[j]->toFPA->x->coeff[0][0];
+            cells[j]->toTP->x->coeff[1][1] = 0.0;
+
+            readouts = (psReadout **) cells[j]->readouts->data;
+            for (k=0;k<NUM_READOUTS;k++) {
+                tmpImage = psImageAlloc(READOUT_NUM_COLS, READOUT_NUM_ROWS, PS_TYPE_F32);
+                for (int row=0;row<READOUT_NUM_ROWS;row++) {
+                    for(int col=0;col<READOUT_NUM_COLS;col++) {
+                        tmpImage->data.F32[row][col] = (float) ((i * (CHIP_WIDTH + CHIP_GAP)) +
+                                                                (j * (CELL_WIDTH + CELL_GAP)));
+                    }
+                }
+                readouts[k] = psReadoutAlloc((j * (CELL_WIDTH + CELL_GAP)), 0, tmpImage);
+
+            }
+        }
+    }
+
+    return(myFPA);
 }
 
@@ -295,7 +371,194 @@
 }
 
+int test3a( void )
+{
+    int x;
+    int y;
+    psPlane fpaCoord;
+    psFPA *myFPA = genSystem();
+    psCell *myCell = NULL;
+    psPlane chipCoord;
+    psPlane cellCoord;
+    psPlane testCoord;
+
+    int xReadout = 0;
+    int xFPA = 0;
+    for (int chip=0;chip<NUM_CHIPS;chip++) {
+        for (int cell=0;cell<NUM_CELLS;cell++) {
+            for(x=0;x<CELL_WIDTH;x++) {
+                for (y=0;y<CELL_HEIGHT;y++) {
+                    fpaCoord.x = (double) xFPA;
+                    fpaCoord.y = (double) y;
+                    printf("------------------ (%f, %f) ------------------\n", fpaCoord.x, fpaCoord.y);
+                }
+                xFPA++;
+                xReadout++;
+            }
+            // We skip the gaps between cells.
+            for(x=0;x<CELL_GAP;x++) {
+                printf("Column %d is CELL GAP\n", xFPA);
+                xFPA++;
+                xReadout=0;
+            }
+        }
+        // We skip the gaps between chips.
+        for (x=0;x<CHIP_GAP;x++) {
+            printf("Column %d is CHIP GAP\n", xFPA);
+            xFPA++;
+            xReadout=0;
+        }
+    }
+
+    psFree(myFPA);
+    return(0);
+}
+
+int printCell(psCell *cell)
+{
+    psReadout **readouts = (psReadout **) cell->readouts->data;
+    psReadout *readout = readouts[0];
+
+    printf("-------------------------------\n");
+    for (int i = 0; i < readout->image->numRows ; i++) {
+        for (int j = 0; j < readout->image->numCols ; j++) {
+            printf("(%.1f) ", readout->image->data.F32[i][j]);
+        }
+        printf("\n");
+    }
+    return(0);
+}
+
+
+/******************************************************************************
+This routine tests many Astrometry functions:
+    psChipInFPA()
+    psCellInFPA()
+    psCoordFPAToChip()
+    psCoordChipToCell()
+This routine loops through all valid cell coordinates and maintains a running
+set of (x,y) coordinates in the FPA plane.  It calls the first two functions
+with each FPA coordinate and determines the chip/cell that that coordinate
+corresponds to.  It then calls the last two functions to determine the actual
+chip and cell coordinates.  The cell coordinate is verified for correctness.
+ 
+It then calls
+    psCoordCellToChip()
+to determine if the chip coordinates that were generated agree with the chip
+coords returned above.
+ 
+Then, it calls 
+    psCellInChip()
+and flags an ERROR if the cell it returns is different from that of
+psCellInFPA().
+ 
+Then, it calls
+    psCoordChipToFPA
+to determine if the FPA coordinates are correct.
+ 
+ 
+ *****************************************************************************/
 int test3( void )
 {
-    genSystem();
+    int x;
+    int y;
+    psPlane fpaCoord;
+    psFPA *myFPA = genSystem();
+    psCell *myCell = NULL;
+    psPlane chipCoord;
+    psPlane cellCoord;
+    psPlane testCoord;
+
+    int xReadout = 0;
+    int xFPA = 0;
+    for (int chip=0;chip<NUM_CHIPS;chip++) {
+        for (int cell=0;cell<NUM_CELLS;cell++) {
+            for(x=0;x<CELL_WIDTH;x++) {
+                for (y=0;y<CELL_HEIGHT;y++) {
+                    fpaCoord.x = (double) xFPA;
+                    fpaCoord.y = (double) y;
+                    //printf("------------------ (%f, %f) ------------------\n", fpaCoord.x, fpaCoord.y);
+                    psChip* tmpChip = psChipInFPA(&fpaCoord, myFPA);
+                    myCell = psCellInFPA(&fpaCoord, myFPA);
+
+                    if ((myCell == NULL) || (tmpChip == NULL)) {
+                        printf("ERROR: NULL\n");
+                    } else {
+                        psCoordFPAToChip(&chipCoord, &fpaCoord, tmpChip);
+                        psCoordChipToCell(&cellCoord, &chipCoord, myCell);
+
+                        if (x != (int) cellCoord.x) {
+                            printf("ERROR: x coord was %f, should be %d\n", cellCoord.x, x);
+                        }
+                        if (y != (int) cellCoord.y) {
+                            printf("ERROR: y coord was %f, should be %d\n", cellCoord.y, y);
+                        }
+
+                        psCoordCellToChip(&testCoord, &cellCoord, myCell);
+                        if (testCoord.x != chipCoord.x) {
+                            printf("ERROR: psCoordCellToChip() x coord was %f, should be %d\n", cellCoord.x, x);
+                        }
+                        if (testCoord.y != chipCoord.y) {
+                            printf("ERROR: psCoordCellToChip() y coord was %f, should be %d\n", cellCoord.y, y);
+                        }
+
+                        psCell *myCell2 = psCellInChip(&chipCoord, tmpChip);
+                        if (myCell2 != myCell) {
+                            printf("ERROR: psCellInFPA() != psCellInChip(psChipInFPA()) (%d %d)\n", myCell2, myCell);
+                            printCell(myCell2);
+                            printCell(myCell);
+
+                        }
+
+                        psCoordChipToFPA(&testCoord, &chipCoord, tmpChip);
+                        if (testCoord.x != xFPA) {
+                            printf("ERROR: psCoordChipToFPA() x coord was %f, should be %d\n", cellCoord.x, x);
+                        }
+                        if (testCoord.y != y) {
+                            printf("ERROR: psCoordChipToFPA() y coord was %f, should be %d\n", cellCoord.y, y);
+                        }
+
+                        psCoordFPAToTP(&testCoord, &fpaCoord, 0.0, 0.0, myFPA);
+                        if (testCoord.x != fpaCoord.x) {
+                            printf("ERROR: psCoordFPAToTP() x coord was %f, should be %d\n", cellCoord.x, x);
+                        }
+                        if (testCoord.y != fpaCoord.y) {
+                            printf("ERROR: psCoordFPAToTP() y coord was %f, should be %d\n", cellCoord.y, y);
+                        }
+
+                        psCoordCellToFPA(&testCoord, &cellCoord, myCell);
+                        if (testCoord.x != fpaCoord.x) {
+                            printf("ERROR: psCoordFPAToTP() x coord was %f, should be %d\n", cellCoord.x, x);
+                        }
+                        if (testCoord.y != fpaCoord.y) {
+                            printf("ERROR: psCoordFPAToTP() y coord was %f, should be %d\n", cellCoord.y, y);
+                        }
+
+                        psCoordTPToFPA(&testCoord, &fpaCoord, 0.0, 0.0, myFPA);
+                        if (testCoord.x != fpaCoord.x) {
+                            printf("ERROR: psCoordFPAToTP() x coord was %f, should be %d\n", cellCoord.x, x);
+                        }
+                        if (testCoord.y != fpaCoord.y) {
+                            printf("ERROR: psCoordFPAToTP() y coord was %f, should be %d\n", cellCoord.y, y);
+                        }
+                    }
+                }
+                xFPA++;
+                xReadout++;
+            }
+            // We skip the gaps between cells.
+            for(x=0;x<CELL_GAP;x++) {
+                xFPA++;
+                xReadout=0;
+            }
+        }
+        // We skip the gaps between chips.
+        for (x=0;x<CHIP_GAP;x++) {
+            xFPA++;
+            xReadout=0;
+        }
+    }
+
+    psFree(myFPA);
     return(0);
 }
+//This code is
