Index: /branches/eam_branches/ipp-20110505/Ohana/src/libfits/Makefile
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/libfits/Makefile	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/libfits/Makefile	(revision 31493)
@@ -54,5 +54,5 @@
 $(TABL)/F_set_column.$(ARCH).o			$(TABL)/F_get_column.$(ARCH).o   \
 $(TABL)/F_table_row.$(ARCH).o			$(TABL)/F_free_T.$(ARCH).o       \
-$(TABL)/F_table_varlength.$(ARCH).o
+$(TABL)/F_table_varlength.$(ARCH).o		$(TABL)/F_copy_T.$(ARCH).o
 
 EXTERN_OBJ = \
Index: /branches/eam_branches/ipp-20110505/Ohana/src/libfits/include/gfitsio.h
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/libfits/include/gfitsio.h	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/libfits/include/gfitsio.h	(revision 31493)
@@ -63,6 +63,6 @@
   Header                 *header;
   char                   *buffer;
-  off_t                   datasize;
-  off_t                   validsize;
+  off_t                   datasize; // size of the buffer (including block padding at the end)
+  off_t                   validsize;  // size of the valid portion of the table (< validsize if file is short)
 } FTable;
 
@@ -199,4 +199,6 @@
 int     gfits_vtable_from_ftable       PROTO((FTable *ftable, VTable *vtable, off_t *row, off_t Nrow));
 int     gfits_write_table              PROTO((char *filename, FTable *ftable)); 
+int     gfits_copy_ftable              PROTO((FTable *in, FTable *out));
+int     gfits_copy_vtable              PROTO((VTable *in, VTable *out));
 
 int     gfits_create_Theader           PROTO((Header *header, char *type));
Index: /branches/eam_branches/ipp-20110505/Ohana/src/libfits/matrix/F_copy_M.c
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/libfits/matrix/F_copy_M.c	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/libfits/matrix/F_copy_M.c	(revision 31493)
@@ -3,21 +3,21 @@
 
 /*********************** fits copy header ***********************************/
-int gfits_copy_matrix (Matrix *matrix1, Matrix *matrix2) {
+int gfits_copy_matrix (Matrix *in, Matrix *out) {
 
   int i;
 
-  matrix2[0].unsign   = matrix1[0].unsign;
-  matrix2[0].bitpix   = matrix1[0].bitpix;
-  matrix2[0].datasize = matrix1[0].datasize;
-  matrix2[0].bzero    = matrix1[0].bzero;
-  matrix2[0].bscale   = matrix1[0].bscale;
-  matrix2[0].Naxes    = matrix1[0].Naxes;
+  out[0].unsign   = in[0].unsign;
+  out[0].bitpix   = in[0].bitpix;
+  out[0].datasize = in[0].datasize;
+  out[0].bzero    = in[0].bzero;
+  out[0].bscale   = in[0].bscale;
+  out[0].Naxes    = in[0].Naxes;
   for (i = 0; i < FT_MAX_NAXES; i++) 
-    matrix2[0].Naxis[i] = matrix1[0].Naxis[i];
+    out[0].Naxis[i] = in[0].Naxis[i];
 
-  if (matrix2[0].buffer != NULL) free (matrix2[0].buffer);
-  ALLOCATE (matrix2[0].buffer, char, matrix2[0].datasize);
+  if (out[0].buffer != NULL) free (out[0].buffer);
+  ALLOCATE (out[0].buffer, char, out[0].datasize);
   
-  memcpy (matrix2[0].buffer, matrix1[0].buffer, matrix2[0].datasize);
+  memcpy (out[0].buffer, in[0].buffer, out[0].datasize);
 
   return (TRUE);
Index: /branches/eam_branches/ipp-20110505/Ohana/src/libfits/table/F_copy_T.c
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/libfits/table/F_copy_T.c	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/libfits/table/F_copy_T.c	(revision 31493)
@@ -2,36 +2,44 @@
 # include <gfitsio.h>
 
-/*********************** fits copy header ***********************************/
-int gfits_copy_ftable_data (FTable *table1, FTable *table2) {
-
-  off_t Nbytes, Nread;
-  char string[128];
+/*********************** copy ftable (does not copy the header pointer) ***********************************/
+int gfits_copy_ftable (FTable *in, FTable *out) {
 
   /* find buffer size */
-  Nbytes = gfits_data_size (table[0].header);
-  ALLOCATE (table[0].buffer, char, Nbytes);
+  out[0].validsize = in[0].validsize;
+  out[0].datasize  = in[0].datasize;
 
-  Nread = fread (table[0].buffer, sizeof (char), Nbytes, f);
-  if (Nread != Nbytes) {
-    snprintf (string, 128, "FITS file is short (%s)", __func__);
-    perror (string);
-    if (Nread < gfits_data_min_size (table[0].header)) {
-      fprintf (stderr, "error: fits read error in %s, read "OFF_T_FMT", need "OFF_T_FMT"\n", __func__,  Nread,  gfits_data_min_size (table[0].header));
-      if (padIfShort) {
-	memset (&table[0].buffer[Nread], 0, Nbytes - Nread);
-	fprintf (stderr, "warning: file missing data, padding with zeros: USE AT YOUR OWN RISK!\n");
-	table[0].validsize = Nread;
-	table[0].datasize = Nbytes;
-	return (TRUE);
-      } else {
-	gfits_free_table  (table);
-	return (FALSE);
-      }
-    }
-    fprintf (stderr, "warning: file missing pad\n");
-  }
-  table[0].validsize = Nread;
-  table[0].datasize = Nbytes;
+  ALLOCATE (out[0].buffer, char, out[0].datasize);
+  memcpy (out[0].buffer, in[0].buffer, out[0].datasize);
   return (TRUE);
 }	
 
+/*********************** copy ftable (does not copy the header pointer) ***********************************/
+int gfits_copy_vtable (VTable *in, VTable *out) {
+
+  off_t i, Nx, Ny, Nrows;
+
+  /* find buffer size */
+  Nx = in[0].header[0].Naxis[0];
+  Ny = in[0].header[0].Naxis[1];
+
+  // validate these two?
+  // table[0].datasize = gfits_data_size (table[0].header);
+  // table[0].pad = table[0].datasize - Nx*Ny;
+
+  /* find buffer size */
+  out[0].datasize = in[0].datasize;
+  out[0].pad      = in[0].pad;
+  out[0].Nrow     = in[0].Nrow;
+
+  Nrows = out[0].Nrow;
+
+  ALLOCATE (out[0].row, off_t, MAX (Nrows, 1));
+  ALLOCATE (out[0].buffer, char *, MAX (Nrows, 1));
+  for (i = 0; i < Nrows; i++) {
+    out[0].row[i] = in[0].row[i];
+    ALLOCATE (out[0].buffer[i], char, MAX (Nx, 1));
+    memcpy (out[0].buffer[i], in[0].buffer[i], Nx);
+  }
+  return (TRUE);
+}	
+
Index: /branches/eam_branches/ipp-20110505/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/relphot/include/relphot.h	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/relphot/include/relphot.h	(revision 31493)
@@ -63,4 +63,5 @@
 int    RESET;
 int    UPDATE;
+int    SAVE_IMAGE_UPDATES;
 int    PLOTSTUFF;
 int    SAVEPLOT;
@@ -160,10 +161,10 @@
 float         getMrel             PROTO((Catalog *catalog, off_t meas, int cat));
 Image        *getimage            PROTO((off_t N));
-Image        *getimages           PROTO((off_t *N));
+Image        *getimages           PROTO((off_t *N, off_t **LineNumber));
 void          global_stats        PROTO((Catalog *catalog, int Ncatalog));
 void          initGrid            PROTO((int dX, int dY));
 void          initGridBins        PROTO((Catalog *catalog, int Ncatalog));
 void          initImageBins       PROTO((Catalog *catalog, int Ncatalog));
-void          initImages          PROTO((Image *input, off_t N));
+void          initImages          PROTO((Image *input, off_t *LineNumber, off_t N));
 void          initMosaicBins      PROTO((Catalog *catalog, int Ncatalog));
 void          initMosaicGrid      PROTO((Image *image, off_t Nimage));
Index: /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/ImageOps.c	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/ImageOps.c	(revision 31493)
@@ -9,4 +9,5 @@
 static Image        *image;   // list of available images  
 static off_t        Nimage;   // number of available images
+static off_t       *LineNumber; // match of subset to full image table
 
 // to search by image ID, we sort (imageIDs, imageIdx) by imageIDs to get a sorted index
@@ -14,7 +15,8 @@
 static off_t        *imageIdx; // list of index for image IDs 
 
-Image *getimages (off_t *N) {
+Image *getimages (off_t *N, off_t **line_number) {
 
   *N = Nimage;
+  if (line_number) *line_number = LineNumber;
   return (image);
 }
@@ -24,9 +26,10 @@
 }
 
-void initImages (Image *input, off_t N) {
+void initImages (Image *input, off_t *line_number, off_t N) {
 
   off_t i;
 
   image = input;
+  LineNumber = line_number;
   Nimage = N;
 
Index: /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/MosaicOps.c	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/MosaicOps.c	(revision 31493)
@@ -195,5 +195,5 @@
   if (!MOSAIC_ZEROPT) return;
 
-  image = getimages (&Nimage);
+  image = getimages (&Nimage, NULL);
 
   for (i = 0; i < Nmosaic; i++) {
@@ -361,5 +361,5 @@
   if (FREEZE_MOSAICS) return (FALSE);
 
-  image = getimages (&N);
+  image = getimages (&N, NULL);
 
   Nsecfilt = GetPhotcodeNsecfilt ();
@@ -482,5 +482,5 @@
   if (FREEZE_MOSAICS) return (FALSE);
 
-  image = getimages (&Nimage);
+  image = getimages (&Nimage, NULL);
 
   ALLOCATE (imageOffset, double, Nmosaic);
@@ -853,5 +853,5 @@
   if (!MOSAIC_ZEROPT) return;
 
-  image = getimages (&Nimage);
+  image = getimages (&Nimage, NULL);
 
   N = 0;
Index: /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/args.c
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/args.c	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/args.c	(revision 31493)
@@ -128,4 +128,10 @@
   }
 
+  SAVE_IMAGE_UPDATES = TRUE;
+  if ((N = get_argument (argc, argv, "-skip-image-updates"))) {
+    remove_argument (N, &argc, argv);
+    SAVE_IMAGE_UPDATES = FALSE;
+  }
+
   MaxDensityUse = FALSE;
   if ((N = get_argument (argc, argv, "-max-density"))) {
Index: /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/load_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/load_catalogs.c	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/load_catalogs.c	(revision 31493)
@@ -14,4 +14,5 @@
   // load data from each region file, only use bright stars
   for (i = 0; i < skylist[0].Nregions; i++) {
+    dvo_catalog_init (&catalog[i], TRUE);
 
     // set up the basic catalog info
Index: /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/load_images.c
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/load_images.c	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/load_images.c	(revision 31493)
@@ -35,4 +35,5 @@
 
   // convert database table to internal structure (binary to Image)
+  // 'image' points to the same memory as db->ftable->buffer
   image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
   if (!image) {
@@ -43,4 +44,5 @@
 
   // select the images which overlap the selected sky regions
+  // 'subset' points to a new copy of the data (different from 'image')
   subset = select_images (skylist, image, Nimage, &LineNumber, &Nsubset);
   MARKTIME("selected %d overlapping images: %f sec\n", (int) Nsubset, dtime);
@@ -49,9 +51,12 @@
   // XXX does this simply duplicate the memory needlessly?  we recreate these lines
   // in reload_images.  If we had saved the line numbers, we could avoid this
-  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, LineNumber, Nsubset);
-  MARKTIME("converted ftable to vtable: %f sec\n", dtime);
+  // vtable points *another* copy of the subset rows
+  // (the later call to 'reload_images' copies the subset elements back on top of 
+  // the rows of the vtable)
+  // gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, LineNumber, Nsubset);
+  // MARKTIME("converted ftable to vtable: %f sec\n", dtime);
 
   // save the subset of images in the static reference in ImageOps, set up indexes
-  initImages (subset, Nsubset);
+  initImages (subset, LineNumber, Nsubset);
   MARKTIME("init images: %f sec\n", dtime);
 
@@ -66,9 +71,10 @@
 
   Image     *image;
-  off_t     Nimage, Nx, i;
+  off_t     Nimage, Nx, i, *LineNumber;
   VTable    *vtable;
 
-  image = getimages (&Nimage);
+  image = getimages (&Nimage, &LineNumber);
 
+  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, LineNumber, Nimage);
   vtable = &db[0].vtable;
 
Index: /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/relphot.c
===================================================================
--- /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/relphot.c	(revision 31492)
+++ /branches/eam_branches/ipp-20110505/Ohana/src/relphot/src/relphot.c	(revision 31493)
@@ -147,29 +147,5 @@
   }
   
-  if (SOMETHING) {
-    FITS_DB dbX;
-    dbX.lockstate = LCK_XCLD;
-    dbX.timeout   = 60.0;
-    gfits_db_init (&dbX);
-    char filename[1024];
-    snprintf (filename, 1024, "%s.bck", ImageCat);
-    if (!gfits_db_lock (dbX, filename)) {
-      fprintf (stderr, "can't lock backup image image catalog\n");
-      return (FALSE);
-    }
-    
-    // copy header
-    gfits_copy_header (dbX.header, db.header);
-    gfits_copy_header (dbX.theader, db.theader);
-    gfits_copy_matrix (dbX.matrix, db.matrix);
-    gfits_copy_table (dbX.matrix, db.matrix);
-
-    // copy Images.dat to another structure
-    dvo_image_update (&db, VERBOSE);
-    dvo_image_unlock (&db); 
-  }
-
   if (USE_GRID) dump_grid ();
-  if (!UPDATE) exit (0);
 
   /* set Mcal & Mmos for bad images */
@@ -178,4 +154,64 @@
   MARKTIME("-- finalize Mcal values: %f sec\n", dtime);
 
+  setMcalFinal (); // copy per-mosaic calibrations to the images
+
+  if (SAVE_IMAGE_UPDATES) {
+
+    FITS_DB dbX;
+    off_t *LineNumber;
+    Image *image, *subset;
+    off_t Nimage, Nsubset, i, Nx;
+    char filename[1024];
+
+    gfits_db_init (&dbX);
+    dbX.lockstate = LCK_XCLD;
+    dbX.timeout   = 60.0;
+    dbX.mode      = db.mode;
+    dbX.format    = db.format;
+
+    snprintf (filename, 1024, "%s.bck", ImageCat);
+    if (!gfits_db_lock (&dbX, filename)) {
+      fprintf (stderr, "can't lock backup image image catalog\n");
+      return (FALSE);
+    }
+    
+    // apply the changes from the image subset to the full table:
+
+    // convert database table to internal structure (binary to Image)
+    // 'image' points to the same memory as db->ftable->buffer
+    image = gfits_table_get_Image (&db.ftable, &Nimage, &db.swapped);
+    if (!image) {
+	fprintf (stderr, "ERROR: failed to read images\n");
+	exit (2);
+    }
+    gfits_scan (db.ftable.header, "NAXIS1", OFF_T_FMT, 1,  &Nx);
+
+    subset = getimages (&Nsubset, &LineNumber);
+    for (i = 0; i < Nsubset; i++) {
+      if (LineNumber[i] == -1) continue;
+      memcpy (&image[LineNumber[i]], &subset[i], Nx);
+    }
+
+    // copy Images.dat data (all or only the subset / vtable elements?)  I think I need to dump
+    // the entire Image table, but with the updates in place I think this says: re-work the
+    // ftable/vtable usage in this program to be more sensible...
+    gfits_copy_header (&db.header,  &dbX.header);
+    gfits_copy_matrix (&db.matrix,  &dbX.matrix);
+    gfits_copy_header (&db.theader, &dbX.theader);
+    gfits_copy_ftable (&db.ftable,  &dbX.ftable);
+
+    dbX.ftable.header = &dbX.theader;
+    dbX.virtual = FALSE;
+
+    // copy Images.dat to another structure
+    dvo_image_save (&dbX, VERBOSE);
+    dvo_image_unlock (&dbX); 
+  }
+
+  // only change the real database if -update is requested
+  if (!UPDATE) exit (0);
+  
+  reload_images (&db);
+
   /* at this point, we have correct cal coeffs in the image/mosaic structures */
   for (i = 0; i < Ncatalog; i++) {
@@ -191,7 +227,4 @@
   MARKTIME("-- updated all catalogs: %f sec\n", dtime);
 
-  setMcalFinal ();
-  reload_images (&db);
-  
   dvo_image_update (&db, VERBOSE);
   dvo_image_unlock (&db); 
