Index: /trunk/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 12686)
+++ /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 12687)
@@ -133,19 +133,19 @@
     // do we need to open this file?
     if (level >= file->fileLevel) {
-	// we are allowed to open a file at a level which is not the fileLevel, but we need to
-	// supply a view at the fileLevel for the file lookup functions below
-	pmFPAview *fileView = pmFPAviewForLevel (file->fileLevel, view);
-	if (!pmFPAfileOpen (file, fileView, config)) {
+        // we are allowed to open a file at a level which is not the fileLevel, but we need to
+        // supply a view at the fileLevel for the file lookup functions below
+        pmFPAview *fileView = pmFPAviewForLevel (file->fileLevel, view);
+        if (!pmFPAfileOpen (file, fileView, config)) {
             psError(PS_ERR_IO, false, "failed to open file %s (%s)", file->filename, file->name);
-	    psFree (fileView);
-            return false;
-        }
-	psFree (fileView);
+            psFree (fileView);
+            return false;
+        }
+        psFree (fileView);
     }
 
     // We need to read it --- double-check it's open!
     if (file->state == PM_FPA_STATE_CLOSED) {
-	psError(PS_ERR_IO, false, "failed to open file %s when attempting to read", file->name);
-	return false;
+        psError(PS_ERR_IO, false, "failed to open file %s when attempting to read", file->name);
+        return false;
     }
 
@@ -154,24 +154,24 @@
     switch (file->type) {
       case PM_FPA_FILE_IMAGE:
-	status = pmFPAviewReadFitsImage(view, file);
-	break;
+        status = pmFPAviewReadFitsImage(view, file);
+        break;
       case PM_FPA_FILE_MASK:
-	status = pmFPAviewReadFitsMask(view, file);
+        status = pmFPAviewReadFitsMask(view, file);
         break;
       case PM_FPA_FILE_WEIGHT:
-	status = pmFPAviewReadFitsWeight(view, file);
+        status = pmFPAviewReadFitsWeight(view, file);
         break;
       case PM_FPA_FILE_HEADER:
-	status = pmFPAviewReadFitsHeaderSet(view, file);
+        status = pmFPAviewReadFitsHeaderSet(view, file);
         break;
       case PM_FPA_FILE_FRINGE:
-	status = pmFPAviewReadFitsImage (view, file);
-	if (status) {
-	    if (!pmFPAviewReadFitsTable(view, file, "FRINGE")) {
-		psError(PS_ERR_UNKNOWN, false, "Unable to read fringe data from %s.\n", file->filename);
-		return false;
-	    }
-	}
-	break;
+        status = pmFPAviewReadFitsImage (view, file);
+        if (status) {
+            if (!pmFPAviewReadFitsTable(view, file, "FRINGE")) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to read fringe data from %s.\n", file->filename);
+                return false;
+            }
+        }
+        break;
       case PM_FPA_FILE_SX:
       case PM_FPA_FILE_RAW:
@@ -182,5 +182,5 @@
         break;
       case PM_FPA_FILE_PSF:
-	status = pmFPAviewReadPSFmodel (view, file, config);
+        status = pmFPAviewReadPSFmodel (view, file, config);
         break;
       case PM_FPA_FILE_JPEG:
@@ -192,6 +192,6 @@
     }
     if (!status) {
-	psError(PS_ERR_UNKNOWN, false, "failed to read %s (%s)\n", file->filename, file->name);
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "failed to read %s (%s)\n", file->filename, file->name);
+        return false;
     }
     psTrace ("psModules.camera", 5, "read %s (%s)\n", file->filename, file->name);
@@ -318,13 +318,13 @@
     // XXX do we need to test mosaicLevel?
     if (level >= file->fileLevel) {
-	// we are allowed to open a file at a level which is not the fileLevel, but 
-	// we need to supply view at the fileLevel for the file lookup functions below
-	pmFPAview *fileView = pmFPAviewForLevel (file->fileLevel, view);
+        // we are allowed to open a file at a level which is not the fileLevel, but
+        // we need to supply view at the fileLevel for the file lookup functions below
+        pmFPAview *fileView = pmFPAviewForLevel (file->fileLevel, view);
         if (!pmFPAfileOpen (file, fileView, config)) {
             psError(PS_ERR_IO, false, "failed to open %s (%s)", file->filename, file->name);
-	    psFree (fileView);
-            return false;
-        }
-	psFree (fileView);
+            psFree (fileView);
+            return false;
+        }
+        psFree (fileView);
     }
 
@@ -344,20 +344,20 @@
         break;
       case PM_FPA_FILE_MASK:
-	status = pmFPAviewWriteFitsMask(view, file, config);
+        status = pmFPAviewWriteFitsMask(view, file, config);
         break;
       case PM_FPA_FILE_WEIGHT:
-	status = pmFPAviewWriteFitsWeight(view, file, config);
+        status = pmFPAviewWriteFitsWeight(view, file, config);
         break;
       case PM_FPA_FILE_HEADER:
-	psAbort ("no HEADER write functions defined");
+        psAbort ("no HEADER write functions defined");
         break;
       case PM_FPA_FILE_FRINGE:
-	status = pmFPAviewWriteFitsImage (view, file, config);
-	if (status) {
-	    if (!pmFPAviewWriteFitsTable(view, file, "FRINGE")) {
-		psError(PS_ERR_UNKNOWN, false, "Unable to write fringe data from %s.\n", file->filename);
-		return false;
-	    }
-	}
+        status = pmFPAviewWriteFitsImage (view, file, config);
+        if (status) {
+            if (!pmFPAviewWriteFitsTable(view, file, "FRINGE")) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to write fringe data from %s.\n", file->filename);
+                return false;
+            }
+        }
         break;
       case PM_FPA_FILE_SX:
@@ -386,6 +386,6 @@
     }
     if (!status) {
-	psError(PS_ERR_UNKNOWN, false, "failed to write %s (%s)\n", file->filename, file->name);
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "failed to write %s (%s)\n", file->filename, file->name);
+        return false;
     }
     psTrace ("psModules.camera", 5, "wrote %s (fpa: %p)\n", file->filename, file->fpa);
@@ -452,6 +452,6 @@
     }
     if (!status) {
-	psError(PS_ERR_UNKNOWN, false, "failed to close %s (%s)\n", file->filename, file->name);
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "failed to close %s (%s)\n", file->filename, file->name);
+        return false;
     }
     return true;
@@ -488,5 +488,5 @@
       case PM_FPA_FILE_HEADER:
       case PM_FPA_FILE_FRINGE:
-	status = pmFPAviewFreeData(view, file);
+        status = pmFPAviewFreeData(view, file);
         break;
       case PM_FPA_FILE_SX:
@@ -495,10 +495,10 @@
       case PM_FPA_FILE_CMP:
       case PM_FPA_FILE_CMF:
-	psTrace ("psModules.camera", 5, "NOT freeing %s (%s) : save for further analysis\n", file->filename, file->name);
+        psTrace ("psModules.camera", 5, "NOT freeing %s (%s) : save for further analysis\n", file->filename, file->name);
         break;
       case PM_FPA_FILE_PSF:
       case PM_FPA_FILE_JPEG:
       case PM_FPA_FILE_KAPA:
-	psTrace ("psModules.camera", 5, "nothing to free for %s (%s)\n", file->filename, file->name);
+        psTrace ("psModules.camera", 5, "nothing to free for %s (%s)\n", file->filename, file->name);
         break;
       default:
@@ -507,6 +507,6 @@
     }
     if (!status) {
-	psError(PS_ERR_UNKNOWN, false, "failed to read %s (%s)\n", file->filename, file->name);
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "failed to read %s (%s)\n", file->filename, file->name);
+        return false;
     }
     psTrace ("psModules.camera", 5, "freed %s (%s)\n", file->filename, file->name);
@@ -558,5 +558,5 @@
     }
 
-    // indirect filenames: these come from a list on the command line or elsewhere 
+    // indirect filenames: these come from a list on the command line or elsewhere
     if (!strcasecmp (file->filename, "@FILES")) {
         char *filesrc = pmFPAfileNameFromRule (file->filesrc, file, view);
@@ -626,5 +626,5 @@
 
     switch (file->type) {
-	// open the FITS types:
+        // open the FITS types:
       case PM_FPA_FILE_IMAGE:
       case PM_FPA_FILE_MASK:
@@ -641,26 +641,26 @@
         file->state = PM_FPA_STATE_OPEN;
 
-	// if needed, set the optional EXTWORD field based on the camera value
-	if (config && config->format) {
-	    psMetadata *fileMenu = psMetadataLookupMetadata (NULL, config->format, "FILE");
-	    if (!fileMenu) {
-		psError (PS_ERR_IO, true, "FILE METADATA missing from camera format %s\n", config->formatName);
-		return false;
-	    }
-	    char *extword = psMetadataLookupStr (&status, fileMenu, "EXTWORD");
-	    if (status) {
-		psFitsSetExtnameWord (file->fits, extword);
-	    }
-	}
-
-	// In some cases, we need to read the PHU after we've opened the file.  This happens for
-	// the images supplied by the detrend database, which are only identified here (above).
-	if (!pmFPAfileReadPHU (file, view, config)) {
-	    psError (PS_ERR_IO, true, "error reading PHU for %s (%s)\n", file->filename, file->name);
-	    return false;
-	}
-        break;
-
-	// defer opening TEXT types:
+        // if needed, set the optional EXTWORD field based on the camera value
+        if (config && config->format) {
+            psMetadata *fileMenu = psMetadataLookupMetadata (NULL, config->format, "FILE");
+            if (!fileMenu) {
+                psError (PS_ERR_IO, true, "FILE METADATA missing from camera format %s\n", config->formatName);
+                return false;
+            }
+            char *extword = psMetadataLookupStr (&status, fileMenu, "EXTWORD");
+            if (status) {
+                psFitsSetExtnameWord (file->fits, extword);
+            }
+        }
+
+        // In some cases, we need to read the PHU after we've opened the file.  This happens for
+        // the images supplied by the detrend database, which are only identified here (above).
+        if (!pmFPAfileReadPHU (file, view, config)) {
+            psError (PS_ERR_IO, true, "error reading PHU for %s (%s)\n", file->filename, file->name);
+            return false;
+        }
+        break;
+
+        // defer opening TEXT types:
       case PM_FPA_FILE_SX:
       case PM_FPA_FILE_OBJ:
@@ -692,21 +692,24 @@
     switch (file->fileLevel) {
       case PM_FPA_LEVEL_FPA:
-	if (file->fpa->hdu) return true;
-	break;
+        if (file->fpa->hdu) return true;
+        break;
       case PM_FPA_LEVEL_CHIP: {
-	  pmChip *chip = pmFPAviewThisChip(view, file->fpa);
-	  if (!chip) psAbort ("inconsistent file/fpa: fileLevel is CHIP, view is FPA");
-	  if (chip->hdu) return true;
-	  break;
+          pmChip *chip = pmFPAviewThisChip(view, file->fpa);
+          if (!chip) psAbort ("inconsistent file/fpa: fileLevel is CHIP, view is FPA");
+          if (chip->hdu) return true;
+          break;
       }
       case PM_FPA_LEVEL_CELL: {
-	  pmCell *cell = pmFPAviewThisCell(view, file->fpa);
-	  if (!cell) psAbort ("inconsistent file/fpa: fileLevel is CELL, view is FPA");
-	  if (cell->hdu) return true;
-	  break;
+          pmCell *cell = pmFPAviewThisCell(view, file->fpa);
+          if (!cell) psAbort ("inconsistent file/fpa: fileLevel is CELL, view is FPA");
+          if (cell->hdu) return true;
+          break;
       }
+      case PM_FPA_LEVEL_NONE:
+        // Might get here immediately after opening a file selected from the detrend database.
+        break;
       default:
-	psAbort("fileLevel not correctly set");
-	break;
+        psAbort("fileLevel not correctly set");
+        break;
     }
 
@@ -714,22 +717,22 @@
     psMetadata *phu = psFitsReadHeader (NULL, file->fits);
     if (!file->format) {
-	file->format = pmConfigCameraFormatFromHeader (config, phu);
-	if (!file->format) {
-	    psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", file->filename);
-	    psFree(phu);
-	    return false;
-	}
+        file->format = pmConfigCameraFormatFromHeader (config, phu);
+        if (!file->format) {
+            psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", file->filename);
+            psFree(phu);
+            return false;
+        }
     } else {
-	bool valid;
-	if (!pmConfigValidateCameraFormat (&valid, file->format, phu)) {
-	    psError (PS_ERR_UNKNOWN, false, "Error in camera configuration\n");
-	    psFree (phu);
-	    return false;
-	}
-	if (!valid) {
-	    psError(PS_ERR_IO, false, "file %s is not from the required camera", file->filename);
-	    psFree (phu);
-	    return false;
-	}
+        bool valid;
+        if (!pmConfigValidateCameraFormat (&valid, file->format, phu)) {
+            psError (PS_ERR_UNKNOWN, false, "Error in camera configuration\n");
+            psFree (phu);
+            return false;
+        }
+        if (!valid) {
+            psError(PS_ERR_IO, false, "file %s is not from the required camera", file->filename);
+            psFree (phu);
+            return false;
+        }
     }
     pmFPAview *thisView = pmFPAAddSourceFromHeader (file->fpa, phu, file->format);
@@ -740,5 +743,5 @@
 }
 
-// XXX this function is only called from pmFPAfileWrite 
+// XXX this function is only called from pmFPAfileWrite
 bool pmFPAfileWritePHU(pmFPAfile *file, const pmFPAview *view, pmConfig *config)
 {
@@ -752,36 +755,36 @@
       case PM_FPA_FILE_WEIGHT:
       case PM_FPA_FILE_FRINGE:
-	if (file->mode == PM_FPA_MODE_WRITE) {
-	    // Want to write out any potential blank
-	    // XXX why do we need to construct a new fpa?? 
-	    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config); // FPA, ensuring it's ready to write a blank
-
-	    switch (file->fileLevel) {
-	      case PM_FPA_LEVEL_FPA:
-		if (file->fpa->wrote_phu) break;
-		status = pmFPAWrite(fpa, file->fits, NULL, true, false);
-		file->fpa->wrote_phu = true;
-		break;
-	      case PM_FPA_LEVEL_CHIP: {
-		  pmChip *chip = pmFPAviewThisChip(view, fpa);
-		  if (chip->wrote_phu) break;
-		  status = pmChipWrite(chip, file->fits, NULL, true, false);
-		  chip->wrote_phu = true;
-		  break;
-	      }
-	      case PM_FPA_LEVEL_CELL: {
-		  pmCell *cell = pmFPAviewThisCell(view, fpa);
-		  if (cell->wrote_phu) break;
-		  status = pmCellWrite(cell, file->fits, NULL, true);
-		  cell->wrote_phu = true;
-		  break;
-	      }
-	      default:
-		psAbort("fileLevel not correctly set");
-		break;
-	    }
-
-	    psFree(fpa);
-	}
+        if (file->mode == PM_FPA_MODE_WRITE) {
+            // Want to write out any potential blank
+            // XXX why do we need to construct a new fpa??
+            pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config); // FPA, ensuring it's ready to write a blank
+
+            switch (file->fileLevel) {
+              case PM_FPA_LEVEL_FPA:
+                if (file->fpa->wrote_phu) break;
+                status = pmFPAWrite(fpa, file->fits, NULL, true, false);
+                file->fpa->wrote_phu = true;
+                break;
+              case PM_FPA_LEVEL_CHIP: {
+                  pmChip *chip = pmFPAviewThisChip(view, fpa);
+                  if (chip->wrote_phu) break;
+                  status = pmChipWrite(chip, file->fits, NULL, true, false);
+                  chip->wrote_phu = true;
+                  break;
+              }
+              case PM_FPA_LEVEL_CELL: {
+                  pmCell *cell = pmFPAviewThisCell(view, fpa);
+                  if (cell->wrote_phu) break;
+                  status = pmCellWrite(cell, file->fits, NULL, true);
+                  cell->wrote_phu = true;
+                  break;
+              }
+              default:
+                psAbort("fileLevel not correctly set");
+                break;
+            }
+
+            psFree(fpa);
+        }
       case PM_FPA_FILE_SX:
       case PM_FPA_FILE_RAW:
@@ -792,5 +795,5 @@
       case PM_FPA_FILE_JPEG:
       case PM_FPA_FILE_KAPA:
-	break;
+        break;
       default:
         fprintf (stderr, "warning: type mismatch\n");
@@ -798,6 +801,6 @@
     }
     if (!status) {
-	psError(PS_ERR_UNKNOWN, false, "failed to write PHU for %s (%s)\n", file->filename, file->name);
-	return false;
+        psError(PS_ERR_UNKNOWN, false, "failed to write PHU for %s (%s)\n", file->filename, file->name);
+        return false;
     }
     return true;
