Index: /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.c
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.c	(revision 6826)
+++ /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.c	(revision 6827)
@@ -7,4 +7,5 @@
 #include "pmFPAview.h"
 #include "pmFPAfile.h"
+#include "pmFPACopy.h"
 #include "pmFPARead.h"
 #include "pmFPAWrite.h"
@@ -18,4 +19,5 @@
 
     psFree (file->fpa);
+    psFree (file->readout);
     psFree (file->names);
 
@@ -46,4 +48,5 @@
 
     file->phu = NULL;
+    file->readout = NULL;
     file->header = NULL;
 
@@ -94,4 +97,8 @@
 
     pmFPAfile *file = pmFPAfileAlloc ();
+
+    // save the name of this pmFPAfile
+    file->name = psStringCopy (name);
+
     file->filerule = psMemIncrRefCounter(psMetadataLookupStr (&status, data, "FILENAME.RULE"));
     file->filextra = psMemIncrRefCounter(psMetadataLookupStr (&status, data, "FILENAME.XTRA"));
@@ -213,4 +220,7 @@
 
     if (file->mode == PM_FPA_MODE_NONE) {
+        return false;
+    }
+    if (file->mode == PM_FPA_MODE_INTERNAL) {
         return false;
     }
@@ -350,4 +360,42 @@
         pmFPAviewWriteObjects (view, file);
         psTrace ("pmFPAfile", 5, "wrote object %s (fpa: %p)\n", file->filename, file->fpa);
+        break;
+
+    default:
+        fprintf (stderr, "warning: type mismatch\n");
+        return false;
+    }
+    return true;
+}
+
+// create the data elements (headers, images) appropriate for this view
+bool pmFPAfileCreate (pmFPAfile *file, const pmFPAview *view)
+{
+    if (file->mode != PM_FPA_MODE_WRITE)
+        return false;
+
+    // get the current depth
+    pmFPAdepth depth = pmFPAviewDepth (view);
+
+    // do we need to write this file?
+    if (depth != file->dataDepth)
+        return false;
+
+    // XXX is this a sufficient check to avoid creating elements?
+    if (file->src == NULL)
+        return false;
+
+    switch (file->type) {
+    case PM_FPA_FILE_IMAGE:
+        /* create a PHU for thie file, if it does not exist */
+        pmFPAfileCopyView (file->src, file->fpa, view);
+        psTrace ("pmFPAfile", 5, "created fpa data elements for %s (fpa: %p)\n", file->filename, file->fpa);
+        break;
+
+    case PM_FPA_FILE_SX:
+    case PM_FPA_FILE_RAW:
+    case PM_FPA_FILE_OBJ:
+    case PM_FPA_FILE_CMP:
+    case PM_FPA_FILE_CMF:
         break;
 
@@ -409,4 +457,5 @@
         if (place == PM_FPA_BEFORE) {
             pmFPAfileRead (file, view);
+            pmFPAfileCreate (file, view);
         } else {
             pmFPAfileWrite (file, view);
@@ -418,37 +467,54 @@
 }
 
+// create a file with the given name, assign it type "INTERNAL", and supply it with an image
+// of the requested dimensions. (image only, mask and weight are ignored)
+pmReadout *pmFPAfileCreateInternal (psMetadata *files, char *name, int Nx, int Ny, int type)
+{
+    pmReadout *readout = pmReadoutAlloc (NULL);
+    readout->image = psImageAlloc (Nx, Ny, type);
+
+    // I want an image from the
+    pmFPAfile *file = pmFPAfileAlloc();
+    file->mode = PM_FPA_MODE_INTERNAL;
+
+    file->readout = readout;
+    psMetadataAddPtr (files, PS_LIST_TAIL, name, PS_DATA_UNKNOWN, "", file);
+    psFree (file);
+    // we free this copy of file, but 'files' still has a copy
+
+    return (readout);
+}
+
+bool pmFPAfileDropInternal (psMetadata *files, char *name)
+{
+    bool status;
+
+    pmFPAfile *file = psMetadataLookupPtr (&status, files, name);
+    if (file == NULL)
+        return false;
+
+    if (file->mode != PM_FPA_MODE_INTERNAL)
+        return false;
+
+    psMetadataRemoveKey (files, name);
+    return true;
+}
+
 // select the readout from the named pmFPAfile; if the named file does not exist,
-// create an image of the requested dimensions (save this image and file?)
-psImage *pmFPAfileReadoutImage (psMetadata *files, const pmFPAview *view, char *name, int Nx, int Ny, int type)
+pmReadout *pmFPAfileThisReadout (psMetadata *files, const pmFPAview *view, const char *name)
 {
     bool status;
-    psImage *image = NULL;
-
-    // I want an image from the
+
     pmFPAfile *file = psMetadataLookupPtr (&status, files, name);
-    if (file == NULL) {
-        // use Nx or Ny == 0 to avoid creating an image
-        if (Nx*Ny <= 0) {
-            return NULL;
-        }
-        image = psImageAlloc (Nx, Ny, type);
-        return (image);
-    }
-
-    // make an fpa consistent with this view
-    // save the image in the fpa
-    // save the fpa in the file
-
-    // inconsistent result: return NULL
+    if (file == NULL)
+        return NULL;
+
+    // internal files have the readout as a separate element:
+    if (file->mode == PM_FPA_MODE_INTERNAL) {
+        return file->readout;
+    }
+
     pmReadout *readout = pmFPAviewThisReadout (view, file->fpa);
-    if (readout == NULL) {
-        return NULL;
-    }
-
-    // resize the existing readout??? this is wrong...
-    // need to allocate it if it does not exist...
-    image = psImageRecycle (readout->image, Nx, Ny, type);
-    psMemIncrRefCounter (image);
-    return (image);
+    return readout;
 }
 
@@ -649,4 +715,6 @@
 }
 
+// XXX this this function through, then finish
+# if 0
 // look for the given name on the argument list.
 // returns the file (a view to the one saved on config->files)
@@ -734,4 +802,5 @@
     return file;
 }
+# endif
 
 // look for the given name on the argument list.
@@ -884,2 +953,53 @@
 }
 
+// given an already-opened fits file, write the components corresponding
+// to the specified view
+bool pmFPAfileCopyView (pmFPA *out, pmFPA *in, const pmFPAview *view)
+{
+    // XXX how to we transmit the required binning factor?
+    // pmFPAWrite takes care of all PHUs as needed
+    if (view->chip == -1) {
+        pmFPACopy (out, in);
+        return true;
+    }
+    if (view->chip >= in->chips->n) {
+        return false;
+    }
+    pmChip *inChip = in->chips->data[view->chip];
+    pmChip *outChip = out->chips->data[view->chip];
+
+    if (view->cell == -1) {
+        pmChipCopy (outChip, inChip);
+        return true;
+    }
+    if (view->cell >= inChip->cells->n) {
+        return false;
+    }
+    pmCell *inCell = inChip->cells->data[view->cell];
+    pmCell *outCell = outChip->cells->data[view->cell];
+
+    if (view->readout == -1) {
+        pmCellCopy (outCell, inCell);
+        return true;
+    }
+    return false;
+
+    // XXX disable readout write for now
+    # if (0)
+
+        if (view->readout >= cell->readouts->n) {
+            return false;
+        }
+    pmReadout *readout = cell->readouts->data[view->readout];
+
+    if (view->nRows == 0) {
+        pmReadoutWrite (readout, fits, NULL, NULL);
+    } else {
+        pmReadoutWriteSegment (readout, fits, view->nRows, view->iRows, NULL, NULL);
+    }
+    return true;
+    # endif
+}
+
+// need to distinguish between a "copy structure" and a "copy data"
+// need to allow for binning when performing the "copy structure"
Index: /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.h
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.h	(revision 6826)
+++ /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.h	(revision 6827)
@@ -7,6 +7,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.1.2.9 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-04-08 20:13:03 $
+*  @version $Revision: 1.1.2.10 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-04-10 20:22:42 $
 *
 *  Copyright 2004-2005 Institute for Astronomy, University of Hawaii
@@ -40,4 +40,5 @@
     PM_FPA_MODE_READ,
     PM_FPA_MODE_WRITE,
+    PM_FPA_MODE_INTERNAL,
 } pmFPAfileMode;
 
@@ -49,25 +50,35 @@
 typedef struct
 {
-    pmFPAdepth fileDepth;
-    pmFPAdepth dataDepth;
+    pmFPAfileMode mode;   // is this file read, written, or only used internally?
+    pmFPAfileType type;   // what type of data is read from / written to disk?
+    pmFPAfileState state;  // have we opened the file, etc?
 
-    psMetadata *phu;
-    psMetadata *header;
+    pmFPAdepth fileDepth;  // what depth in the FPA hierarchy represents a unique file?
+    pmFPAdepth dataDepth;  // at what depth do we read/write the data segment?
 
-    pmFPA *fpa;
-    psFits *fits;
-    psMetadata *names;
+    pmFPA *fpa;    // for I/O files, we carry a pointer to the complete fpa
+    psFits *fits;   // for I/O files of fits type (IMAGE, CMP, CMF), we carry a file handle
 
-    char *filerule;
-    char *filextra;
-    char *extrule;
-    char *extxtra;
+    psMetadata *phu;   // pointer (view) to the current phu header
+    psMetadata *header;   // pointer (view) to the current hdu header
 
-    char *filename;
-    char *extname;
+    pmReadout *readout;   // for internal files, we only carry a single readout
 
-    pmFPAfileType type;
-    pmFPAfileMode mode;
-    pmFPAfileState state;
+    psMetadata *names;   // filenames supplied by the cmdline or detdb are saved here
+
+    char *filerule;   // rule for constructing a filename when needed
+    char *filextra;   // additional information used to define filenames (context dependent)
+    char *extrule;   // rule for constructing an extension name when needed
+    char *extxtra;   // additional information used to define extension names (context dependent)
+
+    char *name;    // the name of the rule (useful for debugging / tracing)
+    char *filename;   // the current name of an active file
+    char *extname;   // the current name of an active file extension
+
+    // the following elements are used for WRITE-mode IMAGE-type pmFPAfiles to inform
+    // the creation of a new image based on an existing image
+    pmFPA *src;    // if an output FPA, inherit from this FPA
+    int xBin;    // desired binning in x direction
+    int yBin;    // desired binning in y direction
 }
 pmFPAfile;
@@ -81,5 +92,5 @@
 // load the pmFPAfile information from the camera configuration data, constructing the needed fpa structure
 // XXX deprecate this function?
-pmFPAfile *pmFPAfileConstruct (psMetadata *files, psMetadata *format, psMetadata *camera, char *name);
+// pmFPAfile *pmFPAfileConstruct (psMetadata *files, psMetadata *format, psMetadata *camera, char *name);
 
 // open the real file corresponding to the given pmFPAfile appropriate to the current view
@@ -88,4 +99,6 @@
 // read from the real file corresponding to the given pmFPAfile for the current view
 bool pmFPAfileRead (pmFPAfile *file, const pmFPAview *view);
+
+bool pmFPAfileCreate (pmFPAfile *file, const pmFPAview *view);
 
 // write to the real file corresponding to the given pmFPAfile for the current view
@@ -100,5 +113,15 @@
 // return an image corresponding to the current readout, from the specified file.  if the pmFPAfile does not
 // exist, construct the image using the given size and type (save it in a pmFPAfile??)
-psImage *pmFPAfileReadoutImage (psMetadata *files, const pmFPAview *view, char *name, int Nx, int Ny, int type);
+// psImage *pmFPAfileReadoutImage (psMetadata *files, const pmFPAview *view, char *name, int Nx, int Ny, int type);
+
+// select the readout from the named pmFPAfile; if the named file does not exist,
+pmReadout *pmFPAfileThisReadout (psMetadata *files, const pmFPAview *view, const char *name);
+
+// create a file with the given name, assign it type "INTERNAL", and supply it with an image
+// of the requested dimensions. (image only, mask and weight are ignored)
+pmReadout *pmFPAfileCreateInternal (psMetadata *files, char *name, int Nx, int Ny, int type);
+
+// delete the INTERNAL file of the given name (if it exists)
+bool pmFPAfileDropInternal (psMetadata *files, char *name);
 
 // read an image into the current view
@@ -120,3 +143,5 @@
 char *pmFPAfileNameFromRule (char *rule, pmFPAfile *file, const pmFPAview *view);
 
+bool pmFPAfileCopyView (pmFPA *out, pmFPA *in, const pmFPAview *view);
+
 # endif
