Index: /branches/eam_branches/ipp-20101205/psphot/doc/stack.txt
===================================================================
--- /branches/eam_branches/ipp-20101205/psphot/doc/stack.txt	(revision 30005)
+++ /branches/eam_branches/ipp-20101205/psphot/doc/stack.txt	(revision 30006)
@@ -1,2 +1,68 @@
+
+20101207
+
+  header fields for PSPS:
+
+  * stack needs to count # of inputs
+  * propagate the input header to the output image (DONE)
+  * save PSF parameters in header 
+  * stack type is only known to the launcher
+
+  * what are we doing for the stack calibrations??
+
+skycellID    : code
+surveyID     : code
+filterID     : code
+stackMetaID  : stack_id 
+photoCalID   : photcode -> number
+magSat       : FSATUR       saturation magnitude level ** not correctly set
+completMag   : FLIMIT       95% completion level in mag ** not correctly set 
+stackTypeID  :   	    stack type identifier ** deep stack, nightly stack, best IQ stack?
+refImageID   :   	    identifier of image used as reference for analysis
+subtrImageID : N/A (stack)  identifier of image subtracted to generate difference image
+analVer      :   	    analysis version index  ** index for tess_id + skycell_id + filter?
+nP2Images    :   	    number of P2 images contributing to this cell ** missing from input stack
+astroScat    :   	    astrometric scatter for chip ** measure scatter on stack creation?
+photoScat    :   	    photometric scatter for chip ** internal scatter?  
+nAstroRef    :   	    number of astrometric reference sources ** (connected to above)
+nPhoRef	     :   	    number of photometric reference sources ** same
+psfFwhm	     :   	    PSF full width at half maximum ** 0.5(FWHM_MAJ + FWHM_MIN)
+psfmodelID   : * PSFMODEL   PSF model identifier ** save the PSF model name in the header
+psfWidMajor  : * FWHM_MAJ     PSF parameters
+psfWidMinor  : * FWHM_MIN     PSF parameters
+psfTheta     : * ANGLE        PSF parameters
+psfExtra1    : * PSF_EXT1   PSF parameters ** (at field center?)
+psfExtra2    : * PSF_EXT2   PSF parameters ** (not set for all models)
+photoZero    :   	    local derived photometric zero point 
+photoColor   :   	    local derived photometric color term
+ctype1	     : * CTYPE1  	    name of astrometric projection in RA ** propagate from input stacks
+ctype2	     : * CTYPE2  	    name of astrometric projection in DEC
+crval1	     : * CRVAL1  	    RA corresponding to reference pixel
+crval2	     : * CRVAL2  	    DEC corresponding to reference pixel
+crpix1	     : * CRPIX1  	    reference pixel value for RA
+crpix2	     : * CRPIX2  	    reference pixel value for DEC
+cdelt1	     : * CDELT1  	    scale factor for RA
+cdelt2	     : * CDELT2  	    scale factor for DEC
+pc001001     : * PC001001	    elements of rotation/Dcale matrix
+pc001002     : * PC001002	    elements of rotation/Dcale matrix
+pc002001     : * PC002001	    elements of rotation/Dcale matrix
+pc002002     : * PC002002	    elements of rotation/Dcale matrix
+polyOrder    : * NPLYTERM	    polynomial order of astrometry fit ** default to 1
+pca1x3y0     : * PCA1X3Y0     polynomial coefficients for the astrometric fit
+pca1x2y1     : * PCA1X2Y1     polynomial coefficients for the astrometric fit
+pca1x1y2     : * PCA1X1Y2     polynomial coefficients for the astrometric fit
+pca1x0y3     : * PCA1X0Y3     polynomial coefficients for the astrometric fit
+pca1x2y0     : * PCA1X2Y0     polynomial coefficients for the astrometric fit
+pca1x1y1     : * PCA1X1Y1     polynomial coefficients for the astrometric fit
+pca1x0y2     : * PCA1X0Y2     polynomial coefficients for the astrometric fit
+pca2x3y0     : * PCA2X3Y0     polynomial coefficients for the astrometric fit
+pca2x2y1     : * PCA2X2Y1     polynomial coefficients for the astrometric fit
+pca2x1y2     : * PCA2X1Y2     polynomial coefficients for the astrometric fit
+pca2x0y3     : * PCA2X0Y3     polynomial coefficients for the astrometric fit
+pca2x2y0     : * PCA2X2Y0     polynomial coefficients for the astrometric fit
+pca2x1y1     : * PCA2X1Y1     polynomial coefficients for the astrometric fit
+pca2x0y2     : * PCA2X0Y2     polynomial coefficients for the astrometric fit
+calibModNum  :   	    calibration modification number
+dataRelease  :   	    Data release
 
 20100506:
Index: /branches/eam_branches/ipp-20101205/psphot/src/psphotChoosePSF.c
===================================================================
--- /branches/eam_branches/ipp-20101205/psphot/src/psphotChoosePSF.c	(revision 30005)
+++ /branches/eam_branches/ipp-20101205/psphot/src/psphotChoosePSF.c	(revision 30006)
@@ -434,4 +434,6 @@
     psVector *fwhmMajor = psVectorAllocEmpty (100, PS_DATA_F32);
     psVector *fwhmMinor = psVectorAllocEmpty (100, PS_DATA_F32);
+    psVector *psfExtra1 = psVectorAllocEmpty (100, PS_DATA_F32);
+    psVector *psfExtra2 = psVectorAllocEmpty (100, PS_DATA_F32);
 
     for (float ix = -0.4; ix <= +0.4; ix += 0.1) {
@@ -466,4 +468,11 @@
             psVectorAppend (fwhmMajor, FWHM_MAJOR);
             psVectorAppend (fwhmMinor, FWHM_MINOR);
+
+	    if (modelPSF->params->n >= 7) {
+	      psVectorAppend (psfExtra1, modelPSF->params->data.F32[7]);
+	    }
+	    if (modelPSF->params->n >= 8) {
+	      psVectorAppend (psfExtra2, modelPSF->params->data.F32[8]);
+	    }
         }
     }
@@ -499,7 +508,22 @@
     }
 
+    if (!psVectorStats (stats, psfExtra1, NULL, NULL, 0)) {
+        psError(PS_ERR_UNKNOWN, false, "failure to measure stats for PSF EXTRA 1");
+        return false;
+    }
+    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "PSF_EXT1", PS_META_REPLACE, "PSF extra param 1", stats->sampleMean);
+
+    if (!psVectorStats (stats, psfExtra2, NULL, NULL, 0)) {
+        psError(PS_ERR_UNKNOWN, false, "failure to measure stats for PSF EXTRA 2");
+        return false;
+    }
+    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "PSF_EXT2", PS_META_REPLACE, "PSF extra param 2", stats->sampleMean);
+
     psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "ANGLE",    PS_META_REPLACE, "PSF angle",           axes.theta);
     psMetadataAddS32 (readout->analysis, PS_LIST_TAIL, "NPSFSTAR", PS_META_REPLACE, "Number of stars used to make PSF", psf->nPSFstars);
-    psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "Valid PSF Model?", true);
+
+    char *psfModelName = pmModelClassGetName(psf->type);
+    psMetadataAddStr(readout->analysis,  PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "PSF Model Name", psfModelName);
+    psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSF_OK",   PS_META_REPLACE, "Valid PSF Model?", true);
 
     psFree (fwhmMajor);
@@ -566,7 +590,8 @@
     psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_LQ",   PS_META_REPLACE, "PSF FWHM Minor axis (lower quartile)", 0);
     psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_UQ",   PS_META_REPLACE, "PSF FWHM Minor axis (upper quartile)", 0);
-    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "ANGLE",    PS_META_REPLACE, "PSF angle",           FWHM_T);
-    psMetadataAddS32 (readout->analysis, PS_LIST_TAIL, "NPSFSTAR", PS_META_REPLACE, "Number of stars used to make PSF", 0);
-    psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "Valid PSF Model?", false);
+    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "ANGLE",      PS_META_REPLACE, "PSF angle",           FWHM_T);
+    psMetadataAddS32 (readout->analysis, PS_LIST_TAIL, "NPSFSTAR",   PS_META_REPLACE, "Number of stars used to make PSF", 0);
+    psMetadataAddStr(readout->analysis,  PS_LIST_TAIL, "PSFMODEL",   PS_META_REPLACE, "PSF Model Name", "NONE");
+    psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSF_OK",     PS_META_REPLACE, "Valid PSF Model?", false);
 
     return true;
Index: /branches/eam_branches/ipp-20101205/psphot/src/psphotOutput.c
===================================================================
--- /branches/eam_branches/ipp-20101205/psphot/src/psphotOutput.c	(revision 30005)
+++ /branches/eam_branches/ipp-20101205/psphot/src/psphotOutput.c	(revision 30006)
@@ -261,4 +261,7 @@
     psMetadataItemSupplement (&status, header, analysis, "ANGLE");
 
+    psMetadataItemSupplement (&status, header, analysis, "PSFMODEL");
+    psMetadataItemSupplement (&status, header, analysis, "PSF_OK");
+
     // Image Quality measurements
     psMetadataItemSupplement (&status, header, analysis, "IQ_NSTAR");
Index: /branches/eam_branches/ipp-20101205/psphot/src/psphotStackArguments.c
===================================================================
--- /branches/eam_branches/ipp-20101205/psphot/src/psphotStackArguments.c	(revision 30005)
+++ /branches/eam_branches/ipp-20101205/psphot/src/psphotStackArguments.c	(revision 30006)
@@ -41,5 +41,5 @@
     if ((N = psArgumentGet (argc, argv, "-break"))) {
 	if (argc <= N+1) {
-	  psErrorStackPrint(stderr, "Expected to see 1 more argument; saw %d", argc - 1);
+	  psErrorStackPrint(stderr, "Expected to see an argument for -break");
 	  exit(PS_EXIT_CONFIG_ERROR);
 	}
Index: /branches/eam_branches/ipp-20101205/psphot/src/psphotStackImageLoop.c
===================================================================
--- /branches/eam_branches/ipp-20101205/psphot/src/psphotStackImageLoop.c	(revision 30005)
+++ /branches/eam_branches/ipp-20101205/psphot/src/psphotStackImageLoop.c	(revision 30006)
@@ -101,5 +101,5 @@
 */
 
-# define UPDATE_HEADER 0
+# define UPDATE_HEADER 1
 
 bool GetAstrometryFPA (pmConfig *config, pmFPAview *view) {
@@ -174,12 +174,13 @@
 	pmChip *outChip = pmFPAviewThisChip(view, output->fpa); ///< Chip in the output
 
-# if (UPDATE_HEADER)
 	pmHDU *outHDU = pmFPAviewThisHDU (view, output->fpa);
 	if (!outHDU) {
-	    pmFPAAddSourceFromView(output->fpa, "name", view, output->format);
+	    pmFPAAddSourceFromView(output->fpa, view, output->format);
 	    outHDU = pmFPAviewThisHDU (view, output->fpa);
 	    psAssert (outHDU, "failed to make HDU");
 	}
-# endif
+	if (!outHDU->header) {
+	  outHDU->header = psMetadataAlloc();
+	}
 
         if (bilevelAstrometry) {
@@ -188,10 +189,8 @@
 		continue;
 	    }
-# if (UPDATE_HEADER)
 	    if (!pmAstromWriteBilevelChip(outHDU->header, outChip, WCS_NONLIN_TOL)) {
 		psWarning("Unable to generate WCS header.");
 		continue;
 	    }
-# endif
         } else {
             // we use a default FPA pixel scale of 1.0
@@ -200,10 +199,8 @@
 		continue;
             }
-# if (UPDATE_HEADER)
-	    if (UPDATE_HEADER && !pmAstromWriteWCS(outHDU->header, output->fpa, outChip, WCS_NONLIN_TOL)) {
+	    if (!pmAstromWriteWCS(outHDU->header, output->fpa, outChip, WCS_NONLIN_TOL)) {
 		psWarning("Unable to generate WCS header.");
 		continue;
 	    }
-# endif
 	}
     }
@@ -225,16 +222,17 @@
 	psAssert (output, "missing file?");
 
-# if (UPDATE_HEADER)
 	pmHDU *PHU = pmFPAviewThisPHU(view, output->fpa);
 	if (!PHU) {
-	    pmFPAAddSourceFromView(output->fpa, "name", view, output->format);
+	    pmFPAAddSourceFromView(output->fpa, view, output->format);
 	    PHU = pmFPAviewThisPHU (view, output->fpa);
 	    psAssert (PHU, "failed to make PHU");
 	}
+	if (!PHU->header) {
+	  PHU->header = psMetadataAlloc();
+	}
 
 	if (!pmAstromWriteBilevelMosaic(PHU->header, output->fpa, WCS_NONLIN_TOL)) {
 	    psWarning("Unable to generate WCS header.");
 	}
-# endif
     }
 
Index: /branches/eam_branches/ipp-20101205/psphot/src/psphotStackMatchPSFs.c
===================================================================
--- /branches/eam_branches/ipp-20101205/psphot/src/psphotStackMatchPSFs.c	(revision 30005)
+++ /branches/eam_branches/ipp-20101205/psphot/src/psphotStackMatchPSFs.c	(revision 30006)
@@ -65,4 +65,13 @@
 bool psphotStackMatchPSFsReadout (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index) {
 
+    psImageMaskType maskValue;
+    psImageMaskType markValue;
+
+    // get the PSPHOT.MASK value from the config
+    if (!pmConfigMaskSetBits (&maskValue, &markValue, config)) {
+	psError (PS_ERR_UNKNOWN, true, "Unable to define the mask bit values");
+	return false;
+    }
+
     pmFPAfile *fileSrc = psphotStackGetConvolveSource(config, options, index);
     if (!fileSrc) {
@@ -84,7 +93,6 @@
 
     // set NAN pixels to 'SAT'
-    // XXX replace this is pmReadoutMaskInvalid?
-    psImageMaskType maskVal = pmConfigMaskGet("SAT", config);
-    if (!pmReadoutMaskNonfinite(readoutSrc, maskVal)) {
+    psImageMaskType maskSat = pmConfigMaskGet("SAT", config);
+    if (!pmReadoutMaskInvalid(readoutSrc, maskValue, maskSat)) {
         psError(psErrorCodeLast(), false, "Unable to mask non-finite pixels in readout.");
         return false;
@@ -95,13 +103,5 @@
 	matchKernel(config, readoutOut, readoutSrc, options, index);
 	saveMatchData(readoutOut, options, index);
-	// renormKernel(readoutCnv, options, index);
-    } else {
-        // only match the flux (NO! not for multi-filter, at least!)
-	// XXX do not generate readoutCnv in this case?
-        // float norm = powf(10.0, -0.4 * options->norm->data.F32[index]); // Normalisation
-        // psBinaryOp(readoutRaw->image, readoutRaw->image, "*", psScalarAlloc(norm, PS_TYPE_F32));
-        // psBinaryOp(readoutRaw->variance, readoutRaw->variance, "*", psScalarAlloc(PS_SQR(norm), PS_TYPE_F32));
     }
-
     rescaleData(readoutOut, config, options, index);
 
@@ -110,12 +110,2 @@
     return true;
 }
-
-
-# if (0)
-// Read previously produced kernel
-if (psMetadataLookupBool(NULL, config->arguments, "PPSTACK.DEBUG.STACK")) {
-    loadKernel(config, readoutCnv, options, index);
-} else {
-    matchKernel(config, readoutCnv, readoutRaw, options, index);
-}
-# endif
