Index: /trunk/psModules/src/astrom/Makefile.am
===================================================================
--- /trunk/psModules/src/astrom/Makefile.am	(revision 6204)
+++ /trunk/psModules/src/astrom/Makefile.am	(revision 6205)
@@ -5,8 +5,10 @@
 libpsmoduleastrom_la_SOURCES  = \
     pmAstrometry.c \
+    pmFPAConstruct.c \
     pmAstrometryObjects.c
 
 psmoduleincludedir = $(includedir)
 psmoduleinclude_HEADERS = \
-  pmAstrometry.h \
-  pmAstrometryObjects.h
+    pmAstrometry.h \
+    pmFPAConstruct.h \
+    pmAstrometryObjects.h
Index: /trunk/psModules/src/astrom/pmAstrometry.c
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometry.c	(revision 6204)
+++ /trunk/psModules/src/astrom/pmAstrometry.c	(revision 6205)
@@ -13,6 +13,6 @@
 * XXX: Should we implement non-linear cell->chip transforms?
 * 
-*  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-12-08 00:00:57 $
+*  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-01-26 21:10:50 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -101,5 +101,5 @@
         psFree(cell->readouts);
         psFree(cell->parent);
-        psFree(cell->private);
+        psFree(cell->hdu);
     }
 }
@@ -125,5 +125,5 @@
         psFree(chip->cells);
         psFree(chip->parent);
-        psFree(chip->private);
+        psFree(chip->hdu);
     }
 }
@@ -151,5 +151,5 @@
         }
         psFree(fpa->chips);
-        psFree(fpa->private);
+        psFree(fpa->hdu);
         psFree(fpa->phu);
     }
@@ -204,5 +204,5 @@
     }
     tmpCell->valid = false;
-    tmpCell->private = NULL;
+    tmpCell->hdu = NULL;
 
     psMemSetDeallocator(tmpCell, (psFreeFunc) cellFree);
@@ -233,5 +233,5 @@
     }
     tmpChip->valid = false;
-    tmpChip->private = NULL;
+    tmpChip->hdu = NULL;
 
     psMemSetDeallocator(tmpChip, (psFreeFunc) chipFree);
@@ -250,5 +250,5 @@
     tmpFPA->camera = camera;
     tmpFPA->chips = psArrayAlloc(0);
-    tmpFPA->private = NULL;
+    tmpFPA->hdu = NULL;
     tmpFPA->phu = NULL;
 
@@ -873,2 +873,3 @@
     return(numChips);
 }
+
Index: /trunk/psModules/src/astrom/pmAstrometry.h
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometry.h	(revision 6204)
+++ /trunk/psModules/src/astrom/pmAstrometry.h	(revision 6205)
@@ -8,6 +8,6 @@
 *  @author GLG, MHPCC
 *
-*  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-12-08 00:00:57 $
+*  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-01-26 21:10:50 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -34,4 +34,7 @@
 }
 p_pmHDU;
+
+p_pmHDU *pmHDUAlloc();
+void *pmHDUFree(p_pmHDU *hdu);
 
 /** Focal plane data structure
@@ -62,5 +65,5 @@
     const psMetadata *camera;           ///< Camera configuration
     psArray *chips;                     ///< The chips
-    p_pmHDU *private;                   ///< FITS data
+    p_pmHDU *hdu;                       ///< FITS data
     psMetadata *phu;                    ///< Primary Header
 }
@@ -95,5 +98,5 @@
     pmFPA *parent;                      ///< Parent FPA
     bool valid;                         ///< Do we bother about reading and working with this chip?
-    p_pmHDU *private;                   ///< FITS data
+    p_pmHDU *hdu;                       ///< FITS data
 }
 pmChip;
@@ -124,5 +127,5 @@
     pmChip *parent;                     ///< Parent chip
     bool valid;                         ///< Do we bother about reading and working with this cell?
-    p_pmHDU *private;                   ///< FITS data
+    p_pmHDU *hdu;                       ///< FITS data
 }
 pmCell;
@@ -436,4 +439,5 @@
 );
 
+
 /**
  * 
@@ -451,4 +455,19 @@
 
 
+/**
+ * 
+ * pmFPAConstruct shall construct a focal plane hierarchy from a camera
+ * configuration. A db handle is also provided so that may be set in the pmFPA.
+ * The resultant pmFPA and its lower-down components shall be ready for to read a
+ * FITS file into it by setting the extname pointers at the appropriate levels to
+ * the appropriate FITS extension name.
+ *  
+ */
+pmFPA *pmFPAConstruct(
+    const psMetadata *camera,
+    psDB *db
+);
+
+
 #endif // #ifndef PS_ASTROMETRY_H
 
Index: /trunk/psModules/src/astrom/pmAstrometryObjects.h
===================================================================
--- /trunk/psModules/src/astrom/pmAstrometryObjects.h	(revision 6204)
+++ /trunk/psModules/src/astrom/pmAstrometryObjects.h	(revision 6205)
@@ -8,6 +8,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-12-05 20:49:30 $
+*  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-01-26 21:10:50 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -183,6 +183,4 @@
  * the GRID.*.ANGLE entries (they will be ignored).
  * 
- * XXX: This function name is different in the SDRS.
- * 
  */
 /* in pmAstromGrid.c */
Index: /trunk/psModules/src/imcombine/pmReadoutCombine.c
===================================================================
--- /trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 6204)
+++ /trunk/psModules/src/imcombine/pmReadoutCombine.c	(revision 6205)
@@ -5,6 +5,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-11-15 20:09:03 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-01-26 21:10:50 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -461,2 +461,123 @@
 }
 
+
+
+/**
+ *
+ * The input array fluxLevels consists of Ni vectors, one per mosaic image.
+ * Each vector consists of Nj elements, each a measurement of the input
+ * flat-field image flux levels. All of these vectors must be constructed with
+ * the same number of elements, or the function will return an error. If a chip
+ * is missing from a particular image, that element should be set to NaN. The
+ * vector chipGains supplies initial guesses for the chip gains. If the vector
+ * contains the values 0.0 or NaN for any of the elements, the gain is set to the
+ * mean of the valid values. If the vector length does not match the number of
+ * chips, an warning is raised, all chip gain guesses will be set to 1.0, and the
+ * vector length modified to match the number of chips defined by the supplied
+ * fluxLevels. The sourceFlux input vector must be allocated (not NULL), but the
+ * routine will set the vector length to the number of source images regardless
+ * of the initial state of the vector. All vectors used by this function must be
+ * of type PS_DATA_F64.
+ *
+ 
+fluxLevels(i, j): for each flat field image i, this psArray contains a vector
+with an elemenmt for each chip j.  So, fluxLevels(i, j) corresponds to the
+measured flux M_(i, j) for flat image i, chip j.
+ 
+chipGains[]: has j elements, one for each chip.
+ 
+ 
+They have the observed flux levels for each chip of each image.  They want to
+solve for the actual flux levels and the gain of each chip.
+ 
+Okay, they want to solve for source fluxes and chip gains.
+ 
+ 
+ 
+ 
+ 
+ 
+ *
+ */
+bool pmFlatNormalization(
+    psVector *sourceFlux,
+    psVector *chipGains,
+    psArray *fluxLevels)
+{
+    PS_ASSERT_PTR_NON_NULL(fluxLevels, false);
+    psS32 numImages = fluxLevels->n;
+    psS32 numChips = ((psVector *) fluxLevels->data[0])->n;
+    for (psS32 i = 0 ; i < numImages ; i++) {
+        psVector *tmpVec = (psVector *) fluxLevels->data[i];
+        PS_ASSERT_VECTOR_NON_NULL(tmpVec, false);
+        PS_ASSERT_VECTOR_TYPE(tmpVec, PS_TYPE_F64, false);
+        PS_ASSERT_VECTOR_SIZE(tmpVec, numChips, false);
+    }
+
+    //
+    // Ensure that *localChipGains points to a vector of the same length as numImages.
+    //
+    PS_ASSERT_PTR_NON_NULL(chipGains, false);
+    PS_ASSERT_VECTOR_TYPE(chipGains, PS_TYPE_F64, false);
+    psVector *localChipGains = chipGains;
+    if (numChips != chipGains->n) {
+        psLogMsg(__func__, PS_LOG_WARN, "WARNING: the chipGains vector length does not match the number of chips.\n");
+        localChipGains = psVectorAlloc(numChips, PS_TYPE_F64);
+        psBool rc = psVectorInit(localChipGains, 1.0);
+        if (rc == false) {
+            printf("XXX: gen error\n");
+        }
+    }
+
+    //
+    // If the chipGains vector contains the values 0.0 or NaN for any of the elements,
+    // the gain is set to the mean of the valid values.
+    //
+    psBool meanFlag = false;
+    psVector *chipGainsMask = psVectorAlloc(chipGains->n, PS_TYPE_U8);
+    for (psS32 i = 0 ; i < chipGains->n ; i++) {
+        if ((fabs(chipGains->data.F64[i]) < FLT_EPSILON) ||
+                (isnan(chipGains->data.F64[i]))) {
+            chipGainsMask->data.U8[i] = 1;
+            meanFlag = true;
+        }
+    }
+    // Must calculate the mean.
+    if (meanFlag == true) {
+        psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
+        stats = psVectorStats(stats, chipGains, NULL, chipGainsMask, 1);
+        if (stats == NULL) {
+            printf("XXX: gen error\n");
+        }
+        psF64 mean;
+        psBool rc = p_psGetStatValue(stats, &mean);
+        if (rc == false) {
+            printf("XXX: gen error\n");
+        }
+        // Set the gain to this mean for chips with a gain of 0.0 or NAN
+
+        for (psS32 i = 0 ; i < chipGains->n ; i++) {
+            if ((fabs(chipGains->data.F64[i]) < FLT_EPSILON) ||
+                    (isnan(chipGains->data.F64[i]))) {
+                chipGains->data.F64[i] = mean;
+            }
+        }
+    }
+
+    //
+    // Assert that sourceFlux is non-NULL, correct type, correct size.
+    //
+    PS_ASSERT_PTR_NON_NULL(sourceFlux, false);
+    PS_ASSERT_VECTOR_TYPE(sourceFlux, PS_TYPE_F64, false);
+    psVectorRealloc(sourceFlux, numImages);
+
+
+
+
+
+
+    psFree(psVector);
+    if (numImages != chipGains->n) {
+        psFree(localChipGains);
+    }
+}
Index: /trunk/psModules/src/imcombine/pmReadoutCombine.h
===================================================================
--- /trunk/psModules/src/imcombine/pmReadoutCombine.h	(revision 6204)
+++ /trunk/psModules/src/imcombine/pmReadoutCombine.h	(revision 6205)
@@ -5,6 +5,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-11-15 20:09:03 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-01-26 21:10:50 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -72,3 +72,29 @@
 pmFringePoint;
 
+
+/**
+ * 
+ * The input array fluxLevels consists of Ni vectors, one per mosaic image.
+ * Each vector consists of Nj elements, each a measurement of the input
+ * flat-field image flux levels. All of these vectors must be constructed with
+ * the same number of elements, or the function will return an error. If a chip
+ * is missing from a particular image, that element should be set to NaN. The
+ * vector chipGains supplies initial guesses for the chip gains. If the vector
+ * contains the values 0.0 or NaN for any of the elements, the gain is set to the
+ * mean of the valid values. If the vector length does not match the number of
+ * chips, an warning is raised, all chip gain guesses will be set to 1.0, and the
+ * vector length modified to match the number of chips defined by the supplied
+ * fluxLevels. The sourceFlux input vector must be allocated (not NULL), but the
+ * routine will set the vector length to the number of source images regardless
+ * of the initial state of the vector. All vectors used by this function must be
+ * of type PS_DATA_F64.
+ * 
+ */
+bool pmFlatNormalization(
+    psVector *sourceFlux,
+    psVector *chipGains,
+    psArray *fluxLevels
+);
+
+
 #endif
Index: /trunk/psModules/src/imsubtract/pmSubtractBias.c
===================================================================
--- /trunk/psModules/src/imsubtract/pmSubtractBias.c	(revision 6204)
+++ /trunk/psModules/src/imsubtract/pmSubtractBias.c	(revision 6205)
@@ -6,6 +6,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-12-24 01:24:20 $
+ *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-01-26 21:10:51 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -274,5 +274,5 @@
     psPolynomial1D *in)
 {
-    psPolynomial1D *out = psPolynomial1DAlloc(in->nX, in->type);
+    psPolynomial1D *out = psPolynomial1DAlloc(in->type, in->nX);
     Polynomial1DCopy(out, in);
     return(out);
@@ -1141,20 +1141,4 @@
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 /******************************************************************************
 pmSubtractBias(....): see SDRS for complete specification.
Index: /trunk/psModules/test/astrom/tst_pmAstrometry.c
===================================================================
--- /trunk/psModules/test/astrom/tst_pmAstrometry.c	(revision 6204)
+++ /trunk/psModules/test/astrom/tst_pmAstrometry.c	(revision 6205)
@@ -9,6 +9,6 @@
  *  XXX: Create the pmHDU alloc/free function, test them here
  *
- *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-12-05 21:28:55 $
+ *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-01-26 21:10:51 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -137,5 +137,5 @@
 
     //
-    // How to test the p_pmHDU *private member?
+    // How to test the p_pmHDU *hdu member?
     //
 
@@ -208,6 +208,6 @@
     }
 
-    if (fpa->private != NULL) {
-        psLogMsg(__func__,PS_LOG_ERROR, "TEST ERROR: pmFPAAlloc did not set ->private to NULL.");
+    if (fpa->hdu != NULL) {
+        psLogMsg(__func__,PS_LOG_ERROR, "TEST ERROR: pmFPAAlloc did not set ->hdu to NULL.");
         return 9;
     }
@@ -258,5 +258,5 @@
 
     //
-    // How to test the p_pmHDU *private member?
+    // How to test the p_pmHDU *hdu member?
     //
 
@@ -325,6 +325,6 @@
     }
 
-    if (chip->private != NULL) {
-        psLogMsg(__func__, PS_LOG_ERROR, "TEST ERROR: chip->private set improperly.\n");
+    if (chip->hdu != NULL) {
+        psLogMsg(__func__, PS_LOG_ERROR, "TEST ERROR: chip->hdu set improperly.\n");
         return 25;
     }
@@ -395,5 +395,5 @@
 
     //
-    // How to test the p_pmHDU *private member?
+    // How to test the p_pmHDU *hdu member?
     //
 
@@ -473,6 +473,6 @@
     }
 
-    if (cell->private != NULL) {
-        psLogMsg(__func__, PS_LOG_ERROR, "TEST ERROR: cell->private set improperly.\n");
+    if (cell->hdu != NULL) {
+        psLogMsg(__func__, PS_LOG_ERROR, "TEST ERROR: cell->hdu set improperly.\n");
         return 27;
     }
Index: /trunk/psModules/test/config/tst_pmConfig.c
===================================================================
--- /trunk/psModules/test/config/tst_pmConfig.c	(revision 6204)
+++ /trunk/psModules/test/config/tst_pmConfig.c	(revision 6205)
@@ -7,6 +7,12 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-12-05 21:41:31 $
+ * XXX: Untested:
+ * pmConfigValidateCamera()
+ * pmConfigCameraFromHeader()
+ * pmConfigRecipeFromCamera()
+ * pmConfigDB()
+ *
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-01-26 21:10:51 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -41,5 +47,5 @@
 
 /******************************************************************************
-test00(): Test the various allocators and deallocators.
+test00():
 XXX: untested:
     TIME:
@@ -129,4 +135,5 @@
 
     printf("----------------------------------------------------------------\n");
+    printf("Calling pmConfigRead() with acceptable arguments.\n");
 
     rc = pmConfigRead(&site, &camera, &recipe, &argc, str, "RecipeName");
Index: /trunk/psModules/test/detrend/tst_pmNonLinear.c
===================================================================
--- /trunk/psModules/test/detrend/tst_pmNonLinear.c	(revision 6204)
+++ /trunk/psModules/test/detrend/tst_pmNonLinear.c	(revision 6205)
@@ -19,6 +19,6 @@
  *  and where the data is outside the pmReadout range.
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-10-20 23:06:24 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-01-26 21:10:51 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -71,5 +71,5 @@
     pmReadout *myReadout = pmReadoutAlloc(NULL);
     myReadout->image = myImage;
-    psPolynomial1D *myPoly = psPolynomial1DAlloc(2, PS_POLYNOMIAL_ORD);
+    psPolynomial1D *myPoly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, 2);
     myPoly->coeff[1] = 1.0;
 
@@ -169,5 +169,5 @@
     pmReadout *rc = NULL;
     myReadout->image = myImage;
-    psPolynomial1D *myPoly = psPolynomial1DAlloc(2, PS_POLYNOMIAL_ORD);
+    psPolynomial1D *myPoly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, 2);
     myPoly->coeff[1] = 1.0;
 
Index: /trunk/psModules/test/imsubtract/tst_pmSubtractBias.c
===================================================================
--- /trunk/psModules/test/imsubtract/tst_pmSubtractBias.c	(revision 6204)
+++ /trunk/psModules/test/imsubtract/tst_pmSubtractBias.c	(revision 6205)
@@ -25,6 +25,6 @@
  *  XXX: Memory leaks are not being detected.
  *
- *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-12-24 01:24:36 $
+ *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-01-26 21:10:51 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -643,5 +643,5 @@
 
     psStats *stat = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
-    psPolynomial1D *myPoly = psPolynomial1DAlloc(POLYNOMIAL_FIT_ORDER, PS_POLYNOMIAL_ORD);
+    psPolynomial1D *myPoly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, POLYNOMIAL_FIT_ORDER);
     psSpline1D *mySpline = NULL;
 
Index: /trunk/psModules/test/imsubtract/tst_pmSubtractSky.c
===================================================================
--- /trunk/psModules/test/imsubtract/tst_pmSubtractSky.c	(revision 6204)
+++ /trunk/psModules/test/imsubtract/tst_pmSubtractSky.c	(revision 6205)
@@ -7,6 +7,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-11-15 20:09:03 $
+ *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-01-26 21:10:51 $
  *
  *  XXX: I added the CELL.TRIMSEC region code but there are not tests for it.
@@ -55,5 +55,5 @@
     myReadout->image = tmpImageF32;
     psStats *myStats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
-    psPolynomial2D *myPoly = psPolynomial2DAlloc(POLY_X_ORDER, POLY_Y_ORDER, PS_POLYNOMIAL_ORD);
+    psPolynomial2D *myPoly = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, POLY_X_ORDER, POLY_Y_ORDER);
 
     printPositiveTestHeader(stdout, "pmSubtractSky", "doSubtractSkySimple");
@@ -98,5 +98,5 @@
     myReadout->image = tmpImageF32;
     psStats *myStats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
-    psPolynomial2D *myPoly = psPolynomial2DAlloc(POLY_X_ORDER, POLY_Y_ORDER, PS_POLYNOMIAL_ORD);
+    psPolynomial2D *myPoly = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, POLY_X_ORDER, POLY_Y_ORDER);
     psImage *trueImage = psImageAlloc(numCols, numRows, PS_TYPE_F32);
     psF32 errorTolerance = ERROR_TOLERANCE * ((psF32) binFactor);
@@ -208,5 +208,5 @@
     pmReadout *rc = NULL;
     psStats *myStats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
-    psPolynomial2D *myPoly = psPolynomial2DAlloc(POLY_X_ORDER, POLY_Y_ORDER, PS_POLYNOMIAL_ORD);
+    psPolynomial2D *myPoly = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, POLY_X_ORDER, POLY_Y_ORDER);
 
     printPositiveTestHeader(stdout, "pmSubtractSky", "Testing bad input parameter conditions.");
