Index: /branches/eam_branches/ipp-20110213/Ohana/src/relphot/src/load_images.c
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/relphot/src/load_images.c	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/relphot/src/load_images.c	(revision 30976)
@@ -7,4 +7,6 @@
   fprintf (stderr, MSG, __VA_ARGS__); }
 
+// This function generates a subset of the images based on selections.  Input db has already
+// been loaded with the raw fits table data
 SkyList *load_images (FITS_DB *db, char *regionName, SkyRegion *region, int RegionSelect) {
 
@@ -32,5 +34,5 @@
   }
 
-  // convert database table to internal structure
+  // convert database table to internal structure (binary to Image)
   image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
   if (!image) {
@@ -44,10 +46,15 @@
   MARKTIME("selected images: %f sec\n", dtime);
 
+  // generate db->vtable from db->ftable based on the selection
+  // 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);
 
+  // save the subset of images in the static reference in ImageOps, set up indexes
   initImages (subset, Nsubset);
   MARKTIME("init images: %f sec\n", dtime);
 
+  // match chips to mosaics (if applicable)
   initMosaics (subset, Nsubset);
   MARKTIME("init mosaics: %f sec\n", dtime);
Index: /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/Makefile
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/Makefile	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/Makefile	(revision 30976)
@@ -34,4 +34,5 @@
 $(SRC)/update.$(ARCH).o		    \
 $(SRC)/update_catalog.$(ARCH).o	    \
+$(SRC)/convert.$(ARCH).o	    \
 $(SRC)/SetSignals.$(ARCH).o	    \
 $(SRC)/Shutdown.$(ARCH).o	    \
Index: /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/include/uniphot.h
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/include/uniphot.h	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/include/uniphot.h	(revision 30976)
@@ -29,4 +29,6 @@
 
 typedef struct {
+  char tstart[64];
+  char tstop[64];
   char label[64];
   float M;
@@ -124,2 +126,23 @@
 int           update_setphot         PROTO((Image *image, off_t Nimage));
 void          update_catalog_setphot PROTO((Catalog *catalog, Image *image, off_t *index, off_t Nimage));
+
+/*** time/coord conversion functions not supplied by libohana ***/
+time_t        TimeRef               PROTO((double time, time_t TimeReference, int TimeFormat));
+double        TimeValue             PROTO((time_t time, time_t TimeReference, int TimeFormat));
+
+int           hh_hms                PROTO((double hh, int *hr, int *mn, double *sc));
+int           dd_dms                PROTO((double dd, int *dg, int *mn, double *sc));
+int           hms_format            PROTO((char *line, double value));
+int           dms_format            PROTO((char *line, double value));
+int           hh_hm                 PROTO((double hh, int *hr, double *mn));
+int           day_to_sec            PROTO((char *string, time_t *second));
+int           hms_to_sec            PROTO((char *string, time_t *second));
+char         *ohana_sec_to_hms      PROTO((time_t second));
+char         *ohana_sec_to_day      PROTO((time_t second));
+
+char         *meade_deg_to_str      PROTO((double deg));
+char         *meade_ra_to_str       PROTO((double deg));
+char         *meade_dec_to_str      PROTO((double deg));
+char         *strptime              PROTO((const char *s, const char *format, struct tm *tm));
+time_t        GetTimeReference      PROTO((char *reference));
+int           GetTimeUnits          PROTO((char *name));
Index: /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/dumpresult.c
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/dumpresult.c	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/dumpresult.c	(revision 30976)
@@ -17,5 +17,5 @@
       Mgrp = tgrp[0].M;
       fprintf (f, "%7.4f %7.4f %7.4f %7.4f   %10.6f %10.6f  %f %s\n", 
-	       0.001*Mcal, 0.001*Mgrp, 0.001*Mset, 0.001*sgroup[i].image[j][0].dMcal, 
+	       Mcal, Mgrp, Mset, sgroup[i].image[j][0].dMcal, 
 	       sgroup[i].image[j][0].coords.crval1, sgroup[i].image[j][0].coords.crval2, (sgroup[i].image[j][0].tzero-915148800)/86400.0, tgrp[0].label);
     }
Index: /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/find_image_sgroups.c
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/find_image_sgroups.c	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/find_image_sgroups.c	(revision 30976)
@@ -3,6 +3,6 @@
 Group *find_image_sgroups (FITS_DB *db, ImageLink **Imlink, int *Nsgroup) {
 
-  off_t i, Nimage;
-  int j, Ngroup, Nentry, NENTRY;
+  off_t i, j, Nimage;
+  int Ngroup, Nentry, NENTRY;
   double r, d, x, y, radius;
   Group *group;
@@ -27,4 +27,8 @@
   ALLOCATE (group, Group, Nimage);
 
+  if (VERBOSE) fprintf (stderr, "finding images\n");
+  BuildChipMatch (image, Nimage);
+  // MARKTIME("build chip match: %f sec\n", dtime);
+
   /* set imlink.sgroups = NULL as a marker */
   for (i = 0; i < Nimage; i++) imlink[i].sgroup = NULL;
@@ -34,6 +38,22 @@
     if (image[i].flags & ID_IMAGE_PHOTOM_NOCAL) continue;
 
+    // XXX optionally, we should be able to use ONLY the DIS or NOT the DIS images
+    // NOCAL above is used to mark images which do not match the photcode (including the DIS)
+    // if (!strcmp(&image[i].coords.ctype[4], "-DIS")) continue;
+
+    // this adds 1.3 sec for 3M images
+    if (!FindMosaicForImage (image, Nimage, i)) {
+      fprintf (stderr, "cannot find mosaic for "OFF_T_FMT"\n", i);
+      continue;
+    }
+
+    /* define image center - note the DIS images (mosaic phu) are special */
+    if (!strcmp(&image[i].coords.ctype[4], "-DIS")) {
+	XY_to_RD (&r, &d, 0.0, 0.0, &image[i].coords);
+    } else {
+	XY_to_RD (&r, &d, 0.5*image[i].NX, 0.5*image[i].NX, &image[i].coords);
+    }
+
     /* new sgroup, set ref coords */
-    XY_to_RD (&r, &d, 0.5*image[i].NX, 0.5*image[i].NX, &image[i].coords);
     coords.crval1 = r;
     coords.crval2 = d;
@@ -59,8 +79,22 @@
       if (image[j].flags & ID_IMAGE_PHOTOM_NOCAL) continue;
       if (imlink[j].sgroup != NULL) continue;
+      // XXX optionally, we should be able to use ONLY the DIS or NOT the DIS images
+      // NOCAL above is used to mark images which do not match the photcode (including the DIS)
+      // if (!strcmp(&image[j].coords.ctype[4], "-DIS")) continue;
+
+      // this adds 1.3 sec for 3M images
+      if (!FindMosaicForImage (image, Nimage, j)) {
+	  fprintf (stderr, "cannot find mosaic for "OFF_T_FMT"\n", j);
+	  continue;
+      }
 
       /* project image center to local coords, check radius */
-      XY_to_RD (&r, &d, 0.5*image[j].NX, 0.5*image[j].NX, &image[j].coords);
+      if (!strcmp(&image[j].coords.ctype[4], "-DIS")) {
+	  XY_to_RD (&r, &d, 0.0, 0.0, &image[j].coords);
+      } else {
+	  XY_to_RD (&r, &d, 0.5*image[j].NX, 0.5*image[j].NX, &image[j].coords);
+      }
       if (!RD_to_XY (&x, &y, r, d, &coords)) continue; 
+
       /* RD_to_XY returns FALSE if opposite hemispheres */
       radius = hypot (x, y);
Index: /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/find_image_tgroups.c
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/find_image_tgroups.c	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/find_image_tgroups.c	(revision 30976)
@@ -5,5 +5,5 @@
   char *start, *stop;
   int j, Ngroup, NGROUP, Nentry, NENTRY;
-  off_t i, Nimage;
+  off_t i, Nimage, Ntime;
   unsigned int *time, *tmin, *tmax;
   Group *group;
@@ -21,8 +21,12 @@
   /* sort time list (use only valid images?) */
   ALLOCATE (time, unsigned int, Nimage);
+  Ntime = 0;
   for (i = 0; i < Nimage; i++) {
-    time[i] = image[i].tzero;
+    if (image[i].flags & ID_IMAGE_PHOTOM_NOCAL) continue;
+    if (!strcmp(&image[i].coords.ctype[4], "-DIS")) continue;
+    time[Ntime] = image[i].tzero;
+    Ntime ++;
   }
-  sort_time (time, Nimage);
+  sort_time (time, Ntime);
 
   /* find groups with dt < TRANGE */
@@ -34,5 +38,5 @@
 
   /* generate tgroups */
-  for (i = 0; i < Nimage - 1; i++) {
+  for (i = 0; i < Ntime - 1; i++) {
     if (time[i+1] - time[i] < TRANGE) continue;
     
@@ -47,5 +51,5 @@
     tmin[Ngroup] = time[i + 1];
   }
-  tmax[Ngroup] = time[Nimage - 1];
+  tmax[Ngroup] = time[Ntime - 1];
   Ngroup ++;
   ALLOCATE (group, Group, Ngroup);
@@ -63,4 +67,6 @@
     stop = ohana_sec_to_date (tmax[i]);
     snprintf (group[i].label, 64, "%s - %s", start, stop);
+    strcpy(group[i].tstart, start);
+    strcpy(group[i].tstop, stop);
     free (start);
     free (stop);
@@ -70,4 +76,5 @@
       if (image[j].tzero > tmax[i]) continue;
       if (image[j].flags & ID_IMAGE_PHOTOM_NOCAL) continue;
+      if (!strcmp(&image[j].coords.ctype[4], "-DIS")) continue;
       
       group[i].image[Nentry] = &image[j];
Index: /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/fit_groups.c
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/fit_groups.c	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/fit_groups.c	(revision 30976)
@@ -29,4 +29,6 @@
     tgroup[i].Ngood = stats.Nmeas;
     
+    // fprintf (stderr, "tgroup %d : %f +/- %f : %d stars\n", i, stats.mean, stats.sigma, stats.Nmeas);
+
     initstats ("MEAN");
     liststats (mlist, dlist, Nlist, &stats);
@@ -67,4 +69,6 @@
     sgroup[i].Ngood = stats.Nmeas;
 
+    // fprintf (stderr, "sgroup %d : %f +/- %f : %d stars\n", i, stats.mean, stats.sigma, stats.Nmeas);
+
     initstats ("MEAN");
     liststats (mlist, dlist, Nlist, &stats);
Index: /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/subset_images.c
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/subset_images.c	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/subset_images.c	(revision 30976)
@@ -3,9 +3,9 @@
 int subset_images (FITS_DB *db) {
 
-  off_t i, Nimage, Nkeep, *keep;
+  off_t i, Nimage;
   int equiv;
   Image *image;
 
-  /* use a vtable to keep the images to be calibrated */
+  // convert from the binary I/O format to the internal structure (Image)
   image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
   if (!image) {
@@ -13,7 +13,4 @@
       exit (2);
   }
-
-  Nkeep = 0;
-  ALLOCATE (keep, off_t, Nimage);
 
   /* mark images to be calibrated */
@@ -32,9 +29,5 @@
     }
     image[i].flags &= ~ID_IMAGE_PHOTOM_NOCAL;
-    keep[Nkeep] = i;
-    Nkeep ++;
   }
-
-  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, keep, Nkeep);
   return (TRUE);
 }
Index: /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/uniphot.c
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/uniphot.c	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/uniphot.c	(revision 30976)
@@ -1,3 +1,6 @@
 # include "uniphot.h"
+
+static char *timeref  = "2000/01/01,00:00:00";
+static char *timeunit = "days";
 
 int main (int argc, char **argv) {
@@ -8,4 +11,8 @@
   FITS_DB db;
 
+  // set up time format stuff
+  time_t TimeReference = GetTimeReference (timeref);
+  int TimeUnits = GetTimeUnits (timeunit);
+
   /* get configuration info, args, lockfile */
   initialize_uniphot (argc, argv);
@@ -15,8 +22,8 @@
   if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
   if (db.dbstate == LCK_EMPTY) Shutdown ("ERROR: No images in catalog %s (1)", db.filename);
-  if (!UPDATE) dvo_image_unlock (&db); 
 
   /* load images */
   load_images_uniphot (&db);
+  if (!UPDATE) dvo_image_unlock (&db); 
   
   /* filter image list by selection */
@@ -38,7 +45,23 @@
   fprintf (stdout, "# NLOOP: %d\n", NLOOP);
   fprintf (stdout, "# time groups : %d\n", Ntgroup);
+  fprintf (stdout, "# TIMEREF : %s\n", timeref);
+  fprintf (stdout, "# TIMEFORMAT : %s\n", timeunit);
   for (i = 0; i < Ntgroup; i++) {
-    fprintf (stdout, "%s %5d %5d %7.4f  %7.4f %7.4f\n", tgroup[i].label, 
-	     tgroup[i].Nimage, tgroup[i].Ngood, 0.001*tgroup[i].M, 0.001*tgroup[i].dM, 0.001*tgroup[i].dMsub);
+
+    double tstart = NAN;
+    double tstop = NAN;
+
+    time_t time;
+    if (ohana_str_to_time (tgroup[i].tstart, &time)) { 
+      tstart = TimeValue (time, TimeReference, TimeUnits);
+    }
+    if (ohana_str_to_time (tgroup[i].tstop, &time)) { 
+      tstop = TimeValue (time, TimeReference, TimeUnits);
+    }
+
+    fprintf (stdout, "%s : %12.6f %12.6f : %5d %5d %7.4f  %7.4f %7.4f\n", 
+	     tgroup[i].label, tstart, tstop,
+	     tgroup[i].Nimage, tgroup[i].Ngood, 
+	     tgroup[i].M, tgroup[i].dM, tgroup[i].dMsub);
   }
   fprintf (stdout, "\n");
@@ -47,5 +70,5 @@
   for (i = 0; i < Nsgroup; i++) {
     fprintf (stdout, "%s %5d %5d %7.4f  %7.4f %7.4f\n", sgroup[i].label, 
-	     sgroup[i].Nimage, sgroup[i].Ngood, 0.001*sgroup[i].M, 0.001*sgroup[i].dM, 0.001*sgroup[i].dMsub);
+	     sgroup[i].Nimage, sgroup[i].Ngood, sgroup[i].M, sgroup[i].dM, sgroup[i].dMsub);
   }
   if (!UPDATE) exit (0);
Index: /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/update.c
===================================================================
--- /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/update.c	(revision 30975)
+++ /branches/eam_branches/ipp-20110213/Ohana/src/uniphot/src/update.c	(revision 30976)
@@ -4,5 +4,5 @@
 void update (FITS_DB *db, Group *sgroup, int Nsgroup) {
 
-  off_t i, Nimage;
+  off_t i, Nimage, Nkeep, *keep;
   int j, status, Nmin;
   char line[256];
@@ -19,7 +19,16 @@
   }
 
-  /* clear the NOCAL flags */
+  // create a subset list so we can make a vtable
+  Nkeep = 0;
+  ALLOCATE (keep, off_t, Nimage);
+
+  // identify the images used and clear the NOCAL flags on the rest
   for (i = 0; i < Nimage; i++) {
-    image[i].flags &= ~ID_IMAGE_PHOTOM_NOCAL;
+      if (image[i].flags & ID_IMAGE_PHOTOM_NOCAL) {
+	  image[i].flags &= ~ID_IMAGE_PHOTOM_NOCAL;
+	  continue;
+      }
+      keep[Nkeep] = i;
+      Nkeep ++;
   }
 
@@ -31,6 +40,12 @@
   }
 
-  /** write image table **/
+  // save the rows in the image table which were used in this analysis
+  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, keep, Nkeep);
+
+  // write image table
   dvo_image_update (db, VERBOSE);
+
+  // XXX need to fix the update for the catalog (or make it optional)
+  return;
 
   // XXX this process uses the existence of the file to perform the update
