Index: trunk/psphot/src/psphotOutput.c
===================================================================
--- trunk/psphot/src/psphotOutput.c	(revision 6495)
+++ trunk/psphot/src/psphotOutput.c	(revision 6522)
@@ -14,16 +14,34 @@
 static char *psfSample  = NULL;
 
-void psphotOutputCleanup () {
-
-    psFree (outputRoot);
-    psFree (outputName);
-    psFree (outputMode);
-    psFree (outputFormat);
-    psFree (extNumberFormat);
-    psFree (extNameKey);
-    psFree (extRoot);
-    psFree (psfFile);
-    psFree (psfSample);
-    return;
+static psFits *outputFits = NULL;
+static FILE   *outputFile = NULL;
+
+bool psphotDataIO (psphotData *data, ppConfig *config, int chip, int cell) {
+
+    // load input data, if depth is appropriate
+    ppImageLoadDepth loadDepth = ppImageCheckDepth (config->recipe, "LOAD.DEPTH");
+    if ((chip == -1) && (cell == -1) && (loadDepth == PP_LOAD_FPA)) {
+        psTrace(__func__, 1, "Loading pixels for FPA...\n");
+        ppImageLoadPixels(data->input, config->database, -1, -1);
+    }
+    if ((chip != -1) && (cell == -1) && (loadDepth == PP_LOAD_CHIP)) {
+	psTrace(__func__, 1, "Loading pixels for chip %d...\n", i);
+        ppImageLoadPixels(data->input, config->database, chip, -1);
+    }
+    if ((chip != -1) && (cell != -1) && (loadDepth == PP_LOAD_CELL)) {
+	psTrace(__func__, 1, "Loading pixels for chip %d, cell %d...\n", i, j);
+        ppImageLoadPixels(data->input, config->database, chip, cell);
+    }
+
+
+
+    if (saveDepth == PP_LOAD_FPA) {
+        psTrace(__func__, 1, "Saving objects for FPA...\n");
+        psphotOutputOpen (file->fpa->phu);
+    }
+
+    ppImageLoadDepth saveDepth = ppImageCheckDepth (config->recipe, "SAVE.DEPTH");
+    
+
 }
 
@@ -46,4 +64,7 @@
     extNameKey      = psMetadataLookupStr (&status, config->recipe, "EXTNAME");
     extRoot         = psMetadataLookupStr (&status, config->recipe, "EXTROOT");
+
+    headExtname     = psMetadataLookupStr (&status, config->recipe, "HEAD_EXTNAME");
+    dataExtname     = psMetadataLookupStr (&status, config->recipe, "DATA_EXTNAME");
 
     if (extNumberFormat == NULL) {
@@ -76,9 +97,11 @@
     }
 
-    // for MEF output, we need to open a file up front
+    // for MEF output, we only allow certain output formats
     if (!strcasecmp (outputMode, "MEF")) {
+	if (!strcasecmp (outputFormat, "CMF")) goto valid_format;
 	psLogMsg ("psphot", PS_LOG_ERROR, "MEF output mode unavailable");
 	exit (1);
     }
+valid_format:
 
     // register background image-file names
@@ -99,74 +122,31 @@
 }
 
-// generate the SPLIT filenames
-char *psphotSplitName (psMetadata *header) {
-
-    bool status;
-
-    char *newName = NULL;     // destination for resulting name
-    char *extNameWord = NULL; // this contains the per-extension word to add
-    char  extNumberWord[16];  // this will store the string-formatted number
-
-    // extNumberFormat must be set to a valid entry in psphotOutputPrep
-    sprintf (extNumberWord, extNumberFormat, extNumber);
-
-    // find the extname:
-    char *extNameVal = psMetadataLookupStr (&status, header, extNameKey);
-	    
-    extNameWord = extNameVal;
-    if ((extRoot != NULL) && (extNameVal != NULL)) {
-	// check that the extNameVal matches the expected root 
-	if (strncmp (extNameVal, extRoot, strlen(extRoot))) {
-	    psLogMsg ("psphotSplitName", PS_LOG_WARN, "header entry does not match expected format");
+bool psphotOutputOpen (psMetadata *phu){
+
+    /* outputFile is used by text-format outputs */
+
+    psFree (outputFile);
+    outputFile = psphotOutputName (phu, outputName);
+
+    /* for fits-format output, open outputFits */
+    if (!strcasecmp (outputFormat, "CMF")) {
+	if (outputFits != NULL) {
+	    psFitsClose (outputFits);
 	}
-	extNameWord = extNameVal + strlen(extRoot);
-    }
-
-    if (extNameWord != NULL) {
-	psStringStrip (extNameWord);
-    }
-
-    // note : if the user mis-specifies the output name, we will happily overwrite files
-    newName = psStringCopy (outputName);
-    newName = psphotNameSubstitute (newName, outputRoot, "%r");
-    newName = psphotNameSubstitute (newName, extNameWord, "%x");
-    newName = psphotNameSubstitute (newName, extNumberWord, "%n");
-
-    return newName;
-}
-
-
-// given the input string, search for the key, and replace with the replacement
-// the input string may be freed if not needed
-char *psphotNameSubstitute (char *input, char *replace, char *key) {
-
-    char *p;
-
-    if (key == NULL) return input;
-    if (strlen(key) == 0) return input;
-
-    p = strstr (input, key);
-    if (p == NULL) return input;
-
-    // we have input = xxxkeyxxx
-    // we want output = xxxreplacexxx
-
-    // this is safe since we will subtract strlen(key) elements from input
-    char *output = psAlloc(strlen(input) + strlen(replace) + 1);
-    int Nc = p - input;
-
-    // copy the first segement into 'output'
-    strncpy (output, input, Nc);
-
-    // copy the key replacement to the start of the key
-
-    strcpy (&output[Nc], replace);
-    Nc += strlen (replace);
-    
-    // copy the remainder to the end of the replacement
-    strcpy (&output[Nc], p + strlen(key));
-
-    psFree (input);
-    return output;
+	outputFits = psFitsOpen (outputFile, "w");
+
+	if (!strcasecmp (outputMode, "MEF")) {
+	    psFitsWriteHeader (outputFits, phu);
+	}
+    }
+    return true;
+}
+
+bool psphotOutputClose (void) {
+
+    if (outputFile != NULL) {
+	psFitsClose (outputFile);
+    }
+    return true;
 }
 
@@ -190,6 +170,13 @@
     } 
 
+    if (!strcasecmp (outputMode, "MEF")) {
+	headExt = psphotOutputName (header, headExtname);
+	dataExt = psphotOutputName (header, dataExtname);
+    } 
+    if (!strcasecmp (outputMode, "SPLIT")) {
+	dataExt = psphotOutputName (header, dataExtname);
+    } 
+
     char *residImage = psMetadataLookupStr (&status, arguments, "RESID_IMAGE");
-
     if (psfSample != NULL) psphotSamplePSFs (psf, readout->image, psfSample);
     if (residImage != NULL) psphotSaveImage (header, readout->image, residImage);
@@ -225,5 +212,5 @@
     }
     if (!strcasecmp (outputFormat, "CMF")) {
-	pmSourcesWriteCMF (sources, outputFile, header);
+	pmSourcesWriteCMF (sources, header, headExt, dataExt);
 	psFree (outputFile);
 	return;
@@ -483,8 +470,8 @@
 	psMetadataAdd (row, PS_LIST_TAIL, "MAG_RAW", PS_DATA_F32, "", source->fitMag + ZERO_POINT);
 	psMetadataAdd (row, PS_LIST_TAIL, "MAG_ERR", PS_DATA_F32, "", (int)(1000*dmag));
-	psMetadataAdd (row, PS_LIST_TAIL, "MAG_GAL", PS_DATA_F32, "", type);
-	psMetadataAdd (row, PS_LIST_TAIL, "MAG_AP",  PS_DATA_F32, "", lsky);
-	psMetadataAdd (row, PS_LIST_TAIL, "LOG_SKY", PS_DATA_F32, "", 32.0);
-	psMetadataAdd (row, PS_LIST_TAIL, "FWHM_X",  PS_DATA_F32, "", source->apMag + ZERO_POINT); 
+	psMetadataAdd (row, PS_LIST_TAIL, "MAG_GAL", PS_DATA_F32, "", 32.0);
+	psMetadataAdd (row, PS_LIST_TAIL, "MAG_AP",  PS_DATA_F32, "", source->apMag + ZERO_POINT);
+	psMetadataAdd (row, PS_LIST_TAIL, "LOG_SKY", PS_DATA_F32, "", lsky);
+	psMetadataAdd (row, PS_LIST_TAIL, "FWHM_X",  PS_DATA_F32, "", type); 
 	psMetadataAdd (row, PS_LIST_TAIL, "FWHM_Y",  PS_DATA_F32, "", PAR[4]);
 	psMetadataAdd (row, PS_LIST_TAIL, "THETA",   PS_DATA_F32, "", PAR[5]);
