Index: trunk/psModules/src/objects/pmPSF_IO.c
===================================================================
--- trunk/psModules/src/objects/pmPSF_IO.c	(revision 14207)
+++ trunk/psModules/src/objects/pmPSF_IO.c	(revision 14652)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-07-14 03:20:22 $
+ *  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-08-24 00:11:02 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -40,17 +40,17 @@
 #include "pmGrowthCurve.h"
 #include "pmResiduals.h"
+#include "pmPSF.h"
 #include "pmModel.h"
-#include "pmPSF.h"
 #include "pmPSF_IO.h"
 #include "pmSource.h"
-#include "pmModelGroup.h"
+#include "pmModelClass.h"
 #include "pmSourceIO.h"
 
-bool pmFPAviewCheckDataStatusForPSFmodel (const pmFPAview *view, const pmFPAfile *file)
+bool pmPSFmodelCheckDataStatusForView (const pmFPAview *view, const pmFPAfile *file)
 {
     pmFPA *fpa = file->fpa;
 
     if (view->chip == -1) {
-        bool exists = pmFPACheckDataStatusForPSFmodel (fpa);
+        bool exists = pmPSFmodelCheckDataStatusForFPA (fpa);
         return exists;
     }
@@ -62,5 +62,5 @@
 
     if (view->cell == -1) {
-        bool exists = pmChipCheckDataStatusForPSFmodel (chip);
+        bool exists = pmPSFmodelCheckDataStatusForChip (chip);
         return exists;
     }
@@ -69,60 +69,28 @@
         return false;
     }
-    pmCell *cell = chip->cells->data[view->cell];
-
-    if (view->readout == -1) {
-        bool exists = pmCellCheckDataStatusForPSFmodel (cell);
-        return exists;
-    }
-
-    if (view->readout >= cell->readouts->n) {
-        psError(PS_ERR_IO, true, "Requested readout == %d >= cell->readouds->n == %ld", view->readout, cell->readouts->n);
-        return false;
-    }
-    pmReadout *readout = cell->readouts->data[view->readout];
-
-    return pmReadoutCheckDataStatusForPSFmodel (readout);
-}
-
-bool pmFPACheckDataStatusForPSFmodel (const pmFPA *fpa) {
+    psError(PS_ERR_IO, false, "PSF only valid at the chip level");
+    return false;
+}
+
+bool pmPSFmodelCheckDataStatusForFPA (const pmFPA *fpa) {
 
     for (int i = 0; i < fpa->chips->n; i++) {
         pmChip *chip = fpa->chips->data[i];
         if (!chip) continue;
-        if (pmChipCheckDataStatusForPSFmodel (chip)) return true;
+        if (pmPSFmodelCheckDataStatusForChip (chip)) return true;
     }
     return false;
 }
 
-bool pmChipCheckDataStatusForPSFmodel (const pmChip *chip) {
-
-    for (int i = 0; i < chip->cells->n; i++) {
-        pmCell *cell = chip->cells->data[i];
-        if (!cell) continue;
-        if (pmCellCheckDataStatusForPSFmodel (cell)) return true;
-    }
-    return false;
-}
-
-bool pmCellCheckDataStatusForPSFmodel (const pmCell *cell) {
-
-    for (int i = 0; i < cell->readouts->n; i++) {
-        pmReadout *readout = cell->readouts->data[i];
-        if (!readout) continue;
-        if (pmReadoutCheckDataStatusForPSFmodel (readout)) return true;
-    }
-    return false;
-}
-
-bool pmReadoutCheckDataStatusForPSFmodel (const pmReadout *readout) {
+bool pmPSFmodelCheckDataStatusForChip (const pmChip *chip) {
 
     bool status;
 
     // select the psf of interest
-    pmPSF *psf = psMetadataLookupPtr(&status, readout->analysis, "PSPHOT.PSF");
+    pmPSF *psf = psMetadataLookupPtr(&status, chip->analysis, "PSPHOT.PSF");
     return psf ? true : false;
 }
 
-bool pmFPAviewWritePSFmodel (const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelWriteForView (const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
 {
 
@@ -130,5 +98,5 @@
 
     if (view->chip == -1) {
-        if (!pmFPAWritePSFmodel(fpa, view, file, config)) {
+        if (!pmPSFmodelWriteFPA(fpa, view, file, config)) {
             psError(PS_ERR_IO, false, "Failed to write PSF for fpa");
             return false;
@@ -143,5 +111,5 @@
 
     if (view->cell == -1) {
-        if (!pmChipWritePSFmodel (chip, view, file, config)) {
+        if (!pmPSFmodelWriteChip (chip, view, file, config)) {
             psError(PS_ERR_IO, false, "Failed to write PSF for chip");
             return false;
@@ -150,36 +118,15 @@
     }
 
-    if (view->cell >= chip->cells->n) {
-        return false;
-    }
-    pmCell *cell = chip->cells->data[view->cell];
-
-    if (view->readout == -1) {
-        if (!pmCellWritePSFmodel (cell, view, file, config)) {
-            psError(PS_ERR_IO, false, "Failed to write PSF for cell");
-            return false;
-        }
-        return true;
-    }
-
-    if (view->readout >= cell->readouts->n) {
-        return false;
-    }
-    pmReadout *readout = cell->readouts->data[view->readout];
-
-    if (!pmReadoutWritePSFmodel (readout, view, file, config)) {
-        psError(PS_ERR_IO, false, "Failed to write PSF for readout");
-        return false;
-    }
-    return true;
+    psError(PS_ERR_IO, false, "PSF must be written at the chip level");
+    return false;
 }
 
 // read in all chip-level PSFmodel files for this FPA
-bool pmFPAWritePSFmodel (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelWriteFPA (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
 {
 
     for (int i = 0; i < fpa->chips->n; i++) {
         pmChip *chip = fpa->chips->data[i];
-        if (!pmChipWritePSFmodel (chip, view, file, config)) {
+        if (!pmPSFmodelWriteChip (chip, view, file, config)) {
             psError(PS_ERR_IO, false, "Failed to write PSF for %dth chip", i);
             return false;
@@ -190,39 +137,25 @@
 
 // read in all cell-level PSFmodel files for this chip
-bool pmChipWritePSFmodel (pmChip *chip, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
-{
-    for (int i = 0; i < chip->cells->n; i++) {
-        pmCell *cell = chip->cells->data[i];
-        if (!pmCellWritePSFmodel (cell, view, file, config)) {
-            psError(PS_ERR_IO, false, "Failed to write PSF for %dth cell", i);
-            return false;
-        }
+bool pmPSFmodelWriteChip (pmChip *chip, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+{
+    if (!pmPSFmodelWrite (chip->analysis, view, file, config)) {
+	psError(PS_ERR_IO, false, "Failed to write PSF for chip");
+	return false;
     }
     return true;
 }
 
-// read in all readout-level PSFmodel files for this cell
-bool pmCellWritePSFmodel (pmCell *cell, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
-{
-    for (int i = 0; i < cell->readouts->n; i++) {
-        pmReadout *readout = cell->readouts->data[i];
-        if (!pmReadoutWritePSFmodel (readout, view, file, config)) {
-            psError(PS_ERR_IO, false, "Failed to write PSF for %dth readout", i);
-            return false;
-        }
-    }
-    return true;
-}
-
-// for each Readout (ie, analysed image), we write out
+// for a pmPSF supplied on the analysis metadata, we write out
 // - image header        : FITS Image NAXIS = 0
 // - psf table (+header) : FITS Table
 // - psf resid (+header) : FITS Image
 // if needed, we also write out a PHU blank header
-bool pmReadoutWritePSFmodel (pmReadout *readout, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelWrite (psMetadata *analysis, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
 {
     bool status;
     pmHDU *hdu;
     char *headName, *tableName, *residName;
+
+    if (!analysis) return false;
 
     // write a PHU? (only if input image is MEF)
@@ -300,7 +233,7 @@
 
     // select the psf of interest
-    pmPSF *psf = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF");
+    pmPSF *psf = psMetadataLookupPtr (&status, analysis, "PSPHOT.PSF");
     if (!psf) {
-	psError(PS_ERR_UNKNOWN, true, "missing PSF for this readout");
+	psError(PS_ERR_UNKNOWN, true, "missing PSF for this analysis metadata");
 	psFree (tableName);
 	psFree (residName);
@@ -313,10 +246,10 @@
 	psMetadata *header = psMetadataAlloc();
 
-	char *modelName = pmModelGetType (psf->type);
+	char *modelName = pmModelClassGetName (psf->type);
 	psMetadataAddStr (header, PS_LIST_TAIL, "PSF_NAME", 0, "PSF model name", modelName);
 
 	psMetadataAddBool (header, PS_LIST_TAIL, "POISSON",  0, "Use Poisson errors in fits?", psf->poissonErrors);
 
-	int nPar = pmModelParameterCount (psf->type)    ;
+	int nPar = pmModelClassParameterCount (psf->type)    ;
 	psMetadataAdd (header, PS_LIST_TAIL, "PSF_NPAR", PS_DATA_S32, "PSF model parameter count", nPar);
 
@@ -324,5 +257,5 @@
 	for (int i = 0; i < nPar; i++) {
 	    char name[9];
-	    psPolynomial2D *poly = psf->params_NEW->data[i];
+	    psPolynomial2D *poly = psf->params->data[i];
 	    if (poly == NULL) continue;
 	    snprintf (name, 9, "PAR%02d_NX", i);
@@ -344,5 +277,5 @@
 	psArray *psfTable = psArrayAllocEmpty (100);
 	for (int i = 0; i < nPar; i++) {
-	    psPolynomial2D *poly = psf->params_NEW->data[i];
+	    psPolynomial2D *poly = psf->params->data[i];
 	    if (poly == NULL) continue; // skip unset parameters (eg, XPOS)
 	    for (int ix = 0; ix <= poly->nX; ix++) {
@@ -445,5 +378,5 @@
 
 // if this file needs to have a PHU written out, write one
-bool pmPSF_WritePHU (const pmFPAview *view, pmFPAfile *file, const pmConfig *config) {
+bool pmPSFmodelWritePHU (const pmFPAview *view, pmFPAfile *file, const pmConfig *config) {
 
     // not needed if already written
@@ -487,5 +420,5 @@
 }
 
-bool pmFPAviewReadPSFmodel (const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelReadForView (const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
 {
 
@@ -493,5 +426,5 @@
 
     if (view->chip == -1) {
-        return pmFPAReadPSFmodel(fpa, view, file, config);
+        return pmPSFmodelReadFPA(fpa, view, file, config);
     }
 
@@ -502,31 +435,18 @@
 
     if (view->cell == -1) {
-        return pmChipReadPSFmodel(chip, view, file, config);
-    }
-
-    if (view->cell >= chip->cells->n) {
-        psAbort("Programming error: view does not apply to FPA.");
-    }
-    pmCell *cell = chip->cells->data[view->cell];
-
-    if (view->readout == -1) {
-        return pmCellReadPSFmodel(cell, view, file, config);
-    }
-
-    if (view->readout >= cell->readouts->n) {
-        psAbort("Programming error: view does not apply to FPA.");
-    }
-    pmReadout *readout = cell->readouts->data[view->readout];
-
-    return pmReadoutReadPSFmodel(readout, view, file, config);
+        return pmPSFmodelReadChip(chip, view, file, config);
+    }
+
+    psError(PS_ERR_IO, false, "PSF must be read at the chip level");
+    return false;
 }
 
 // read in all chip-level PSFmodel files for this FPA
-bool pmFPAReadPSFmodel (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelReadFPA (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
 {
     bool success = true;                // Was everything successful?
     for (int i = 0; i < fpa->chips->n; i++) {
         pmChip *chip = fpa->chips->data[i];
-        success &= pmChipReadPSFmodel(chip, view, file, config);
+        success &= pmPSFmodelReadChip(chip, view, file, config);
     }
     return success;
@@ -534,28 +454,16 @@
 
 // read in all cell-level PSFmodel files for this chip
-bool pmChipReadPSFmodel (pmChip *chip, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
-{
-    bool success = true;                // Was everything successful?
-    for (int i = 0; i < chip->cells->n; i++) {
-        pmCell *cell = chip->cells->data[i];
-        success &= pmCellReadPSFmodel (cell, view, file, config);
-    }
-    return success;
-}
-
-// read in all readout-level PSFmodel files for this cell
-bool pmCellReadPSFmodel (pmCell *cell, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
-{
-    bool success = true;                // Was everything successful?
-    for (int i = 0; i < cell->readouts->n; i++) {
-        pmReadout *readout = cell->readouts->data[i];
-        success &= pmReadoutReadPSFmodel(readout, view, file, config);
-    }
-    return success;
+bool pmPSFmodelReadChip (pmChip *chip, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+{
+    if (!pmPSFmodelRead (chip->analysis, view, file, config)) {
+	psError(PS_ERR_IO, false, "Failed to write PSF for chip");
+	return false;
+    }
+    return true;
 }
 
 // for each Readout (ie, analysed image), we write out: header + table with PSF model parameters,
 // and header + image for the PSF residual images
-bool pmReadoutReadPSFmodel(pmReadout *readout, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelRead (psMetadata *analysis, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
 {
     bool status;
@@ -599,5 +507,5 @@
     // load the PSF model parameters from the FITS table
     char *modelName = psMetadataLookupStr (&status, header, "PSF_NAME");
-    pmModelType type = pmModelSetType (modelName);
+    pmModelType type = pmModelClassGetType (modelName);
     if (type == -1) {
         psError(PS_ERR_UNKNOWN, true, "invalid model name %s in psf file %s", modelName, file->filename);
@@ -612,5 +520,5 @@
     // check the number of expected parameters
     int nPar = psMetadataLookupS32 (&status, header, "PSF_NPAR");
-    if (nPar != pmModelParameterCount (psf->type))
+    if (nPar != pmModelClassParameterCount (psf->type))
         psAbort("mismatch model par count");
 
@@ -627,5 +535,5 @@
 	    return false;
 	}
-        psf->params_NEW->data[i] = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXorder, nYorder);
+        psf->params->data[i] = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXorder, nYorder);
     }
 
@@ -650,5 +558,5 @@
         // XXX sanity check here
 
-        psPolynomial2D *poly = psf->params_NEW->data[iPar];
+        psPolynomial2D *poly = psf->params->data[iPar];
 	if (poly == NULL) {
 	    psError(PS_ERR_UNKNOWN, true, "values for parameter %d, but missing NX", iPar);
@@ -698,5 +606,5 @@
     }
 
-    psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF",     PS_DATA_UNKNOWN,  "psphot psf", psf);
+    psMetadataAdd (analysis, PS_LIST_TAIL, "PSPHOT.PSF",     PS_DATA_UNKNOWN,  "psphot psf", psf);
     psFree (psf);
 
@@ -708,6 +616,5 @@
 }
 
-/************ old support functions, deprecate? **************/
-
+// create a psMetadata representation (human-readable) of a psf model
 psMetadata *pmPSFtoMetadata (psMetadata *metadata, pmPSF *psf)
 {
@@ -717,12 +624,12 @@
     }
 
-    char *modelName = pmModelGetType (psf->type);
+    char *modelName = pmModelClassGetName (psf->type);
     psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_MODEL_NAME", PS_DATA_STRING, "PSF model name", modelName);
 
-    int nPar = pmModelParameterCount (psf->type)    ;
+    int nPar = pmModelClassParameterCount (psf->type)    ;
     psMetadataAdd (metadata, PS_LIST_TAIL, "PSF_MODEL_NPAR", PS_DATA_S32, "PSF model parameter count", nPar);
 
     for (int i = 0; i < nPar; i++) {
-        psPolynomial2D *poly = psf->params_NEW->data[i];
+        psPolynomial2D *poly = psf->params->data[i];
         if (poly == NULL)
             continue;
@@ -742,4 +649,5 @@
 }
 
+// parse a psMetadata representation (human-readable) of a psf model
 pmPSF *pmPSFfromMetadata (psMetadata *metadata)
 {
@@ -749,5 +657,5 @@
 
     char *modelName = psMetadataLookupPtr (&status, metadata, "PSF_MODEL_NAME");
-    pmModelType type = pmModelSetType (modelName);
+    pmModelType type = pmModelClassGetType (modelName);
 
     bool poissonErrors = psMetadataLookupPtr (&status, metadata, "PSF_POISSON_ERRORS");
@@ -759,5 +667,5 @@
 
     int nPar = psMetadataLookupS32 (&status, metadata, "PSF_MODEL_NPAR");
-    if (nPar != pmModelParameterCount (psf->type))
+    if (nPar != pmModelClassParameterCount (psf->type))
         psAbort("mismatch model par count");
 
@@ -770,6 +678,6 @@
             continue;
         psPolynomial2D *poly = psPolynomial2DfromMetadata (folder);
-        psFree (psf->params_NEW->data[i]);
-        psf->params_NEW->data[i] = poly;
+        psFree (psf->params->data[i]);
+        psf->params->data[i] = poly;
     }
     sprintf (keyword, "APTREND");
@@ -789,64 +697,2 @@
     return (psf);
 }
-
-// read in all readout-level Objects files for this cell
-bool pmReadoutWritePSFmodel_Config (pmReadout *readout, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
-{
-    bool status;
-    char *filename;
-    char *realname;
-
-    pmPSF *psf = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF");
-
-    switch (file->type) {
-      case PM_FPA_FILE_PSF:
-        filename = pmFPAfileNameFromRule (file->filerule, file, view);
-        bool create = file->mode == PM_FPA_MODE_WRITE ? true : false;
-        realname = pmConfigConvertFilename (filename, config, create);
-
-        psMetadata *psfData = pmPSFtoMetadata (NULL, psf);
-        psMetadataConfigWrite (psfData, realname);
-        psFree (psfData);
-        psFree (realname);
-        psFree (filename);
-        return true;
-
-      default:
-        fprintf (stderr, "warning: type mismatch\n");
-        break;
-    }
-    return false;
-}
-
-// read in all readout-level Objects files for this cell
-bool pmReadoutReadPSFmodel_Config (pmReadout *readout, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
-{
-
-    unsigned int Nfail;
-    char *filename;
-    char *realname;
-
-    switch (file->type) {
-      case PM_FPA_FILE_PSF:
-        filename = pmFPAfileNameFromRule (file->filerule, file, view);
-        bool create = file->mode == PM_FPA_MODE_WRITE ? true : false;
-        realname = pmConfigConvertFilename (filename, config, create);
-
-        psMetadata *psfData = psMetadataConfigRead(NULL, &Nfail, realname, FALSE);
-        pmPSF *psf = pmPSFfromMetadata (psfData);
-        psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN, "psphot psf", psf);
-
-        psFree (psf);
-        psFree (psfData);
-        psFree (realname);
-        psFree (filename);
-
-        return true;
-
-      default:
-        fprintf (stderr, "warning: type mismatch\n");
-        break;
-    }
-    return false;
-}
-
