Index: /trunk/Ohana/src/imregister/Makefile
===================================================================
--- /trunk/Ohana/src/imregister/Makefile	(revision 4023)
+++ /trunk/Ohana/src/imregister/Makefile	(revision 4024)
@@ -17,4 +17,5 @@
 PHOTREG =	$(HOME)/photreg
 IMREG   =	$(HOME)/imreg
+IMPHOT  =	$(HOME)/imphot
 SPREG   =	$(HOME)/spreg
 
@@ -33,11 +34,18 @@
 photsearch   photreg      \
 cameraconfig filtnames    \
-imphotsearch imphotmerge  \
-imphotset	          
-
-# spregister   spsearch
+imphotsearch 
 
 # program under development, not installed
-DEVEL = spregister spsearch
+DEVEL = spregister spsearch imphotmerge
+
+IMPHOTOBJ = \
+$(IMPHOT)/args.$(ARCH).o \
+$(IMPHOT)/db_load.$(ARCH).o \
+$(IMPHOT)/dumpfits.$(ARCH).o \
+$(IMPHOT)/modify.$(ARCH).o \
+$(IMPHOT)/output.$(ARCH).o \
+$(IMPHOT)/rfits.$(ARCH).o \
+$(IMPHOT)/rtext.$(ARCH).o \
+$(IMPHOT)/subset.$(ARCH).o
 
 IMOBJ  = \
@@ -116,4 +124,5 @@
 SR = $(SPREG)/args.spregister.$(ARCH).o    $(SPOBJ)   $(BASEOBJ)
 SS = $(SPREG)/args.spsearch.$(ARCH).o      $(SPOBJ)   $(BASEOBJ)
+IP = $(IMPHOTOBJ) $(BASEOBJ)
 
 $(SR) : $(INC)/spreg.h $(INC)/imregister.h
@@ -136,4 +145,6 @@
 $(BIN)/spsearch.$(ARCH)     : $(SRC)/spsearch.$(ARCH).o     $(SS) ; $(CC) $^ -o $@ $(LFLAGS)
 $(BIN)/spregister.$(ARCH)   : $(SRC)/spregister.$(ARCH).o   $(SR) ; $(CC) $^ -o $@ $(LFLAGS)
+
+$(BIN)/imphotsearch.$(ARCH) : $(SRC)/imphotsearch.$(ARCH).o $(IP) ; $(CC) $^ -o $@ $(LFLAGS)
 
 # dependancy rules for binary code #########################
Index: /trunk/Ohana/src/imregister/base/fits_scan_nchar.c
===================================================================
--- /trunk/Ohana/src/imregister/base/fits_scan_nchar.c	(revision 4023)
+++ /trunk/Ohana/src/imregister/base/fits_scan_nchar.c	(revision 4024)
@@ -1,4 +1,3 @@
 # include "imregister.h"
-# include "imreg.h"
 
 /* scan and give a warning for missing entries */
Index: /trunk/Ohana/src/imregister/imphot/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/ConfigInit.c	(revision 4024)
+++ /trunk/Ohana/src/imregister/imphot/ConfigInit.c	(revision 4024)
@@ -0,0 +1,108 @@
+# include "imregister.h"
+# include "imphot.h"
+
+int success;
+
+void ConfigInitImphot (int *argc, char **argv) {
+
+  int i, NDB;
+  char *config, *file, ElixirBase[80];
+
+  /*** load configuration info ***/
+  file = SelectConfigFile (argc, argv, "ptolemy");
+  config = LoadConfigFile (file);
+  if (config == (char *) NULL) {
+    fprintf (stderr, "ERROR: can't find configuration file %s\n", file);
+    if (file != (char *) NULL) free (file);
+    exit (0);
+  }
+
+  success = TRUE;
+
+  WarnConfig (config, "IMAGE_CATALOG",               "%s", 0, ImPhotDB);
+
+  /* small text databases: filters, camera defs */ 
+  WarnConfig (config, "PHOTCODE_FILE",               "%s", 0, PhotCodeFile);
+  WarnConfig (config, "TEMPERATURE_LOG",             "%s", 0, TempLogFile);
+  WarnConfig (config, "FILTER_LIST",                 "%s", 0, FilterList);
+  WarnConfig (config, "CAMERA_CONFIG",               "%s", 0, CameraConfig);
+  WarnConfig (config, "DETREND_RECIPES",             "%s", 0, RecipeFile);
+						   
+  /* pixel scale for FWHM */ 
+  WarnConfig (config, "ASEC_PIX",                    "%lf", 0, &ARCSEC_PIXEL);
+
+  /* keyword abstractions for parse_time */	   
+  WarnConfig (config, "DATE-KEYWORD",                "%s", 0, DateKeyword);
+  WarnConfig (config, "DATE-MODE",                   "%s", 0, DateMode);
+  WarnConfig (config, "UT-KEYWORD",                  "%s", 0, UTKeyword);
+  WarnConfig (config, "MJD-KEYWORD",                 "%s", 0, MJDKeyword);
+  WarnConfig (config, "JD-KEYWORD",                  "%s", 0, JDKeyword);
+						   
+  /* keyword abstractions for iminfo */		   
+  WarnConfig (config, "EXPTIME-KEYWORD",             "%s", 0, ExptimeKeyword);
+  WarnConfig (config, "IMAGETYPE-KEYWORD",           "%s", 0, ImagetypeKeyword);
+  WarnConfig (config, "CCDNUM-KEYWORD",              "%s", 0, CCDnumKeyword);
+  WarnConfig (config, "FILTER-KEYWORD",              "%s", 0, FilterKeyword);
+  WarnConfig (config, "AIRMASS-KEYWORD",             "%s", 0, AirmassKeyword);
+  WarnConfig (config, "FOCUS-KEYWORD",               "%s", 0, FocusKeyword);
+  WarnConfig (config, "ROTATION-KEYWORD",            "%s", 0, RotationKeyword);
+  WarnConfig (config, "DETTEMP-KEYWORD",             "%s", 0, DettempKeyword);
+  WarnConfig (config, "TELDATA1-KEYWORD",            "%s", 0, Teldata1Keyword);
+  WarnConfig (config, "TELDATA2-KEYWORD",            "%s", 0, Teldata2Keyword);
+  WarnConfig (config, "TELDATA3-KEYWORD",            "%s", 0, Teldata3Keyword);
+  WarnConfig (config, "CAMERA-KEYWORD",              "%s", 0, CameraKeyword);
+
+  ScanConfig (config, "CAMERA",                      "%s", 0, Camera);
+  ScanConfig (config, "SEEING_REF_CCD",              "%s", 0, SeeingREFCCD);
+
+  /* optional values */
+  ScanConfig (config, "RA-DDD-KEYWORD",              "%s", 0, RADecDegKeyword);
+  ScanConfig (config, "DEC-DDD-KEYWORD",             "%s", 0, DECDecDegKeyword);
+  ScanConfig (config, "RA-HMS-KEYWORD",              "%s", 0, RASexigKeyword);
+  ScanConfig (config, "DEC-DMS-KEYWORD",             "%s", 0, DECSexigKeyword);
+
+  if (!RADecDegKeyword[0] & !DECDecDegKeyword[0] && !RASexigKeyword[0] && !DECSexigKeyword[0]) {
+    fprintf (stderr, "missing astrometry configuration information\n");
+    success = FALSE;
+  }
+						   
+  WarnConfig (config, "imstats",                     "%s", 0, ElixirBase);
+  sprintf (ImstatFifo, "%s.source", ElixirBase);   
+  WarnConfig (config, "ptolemy",                     "%s", 0, ElixirBase);
+  sprintf (PtolemyFifo, "%s.source", ElixirBase);
+
+  if (!ScanConfig (config, "CONNECT", "%s",  0, CONNECT)) {
+    sprintf (CONNECT, "/usr/bin/rsh");
+  }
+
+  /* load Detrend Alt Databases paths */
+  NDB = 10;
+  NDetrendAltDB = 0;
+  ALLOCATE (DetrendAltDB, char *, NDB);
+  ALLOCATE (DetrendAltDB[NDetrendAltDB], char, 256);
+  for (i = 1; ScanConfig (config, "DETREND_ALT_DB", "%s", i, DetrendAltDB[NDetrendAltDB]); i++) {
+    NDetrendAltDB ++;
+    if (NDetrendAltDB == NDB) {
+      NDB += 10;
+      REALLOCATE (DetrendAltDB, char *, NDB);
+    }
+    ALLOCATE (DetrendAltDB[NDetrendAltDB], char, 256);
+  }
+  free (DetrendAltDB[NDetrendAltDB]);
+
+  if (! success) {
+    fprintf (stderr, "ERROR: problem with elixir configuration\n");
+    exit (1);
+  }
+
+  free (config);
+  free (file);
+
+}
+
+void WarnConfig (char *config, char *key, char * mode, int N, void *var) {
+  if (!ScanConfig (config, key, mode, N, var)) {
+    fprintf (stderr, "missing config variable %s\n", key);
+    success = FALSE;
+  }
+}
Index: /trunk/Ohana/src/imregister/imphot/args.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/args.c	(revision 4023)
+++ /trunk/Ohana/src/imregister/imphot/args.c	(revision 4024)
@@ -4,20 +4,4 @@
 /* criteria struct is global */
 int args (int argc, char **argv) {
-
-
-  FILE *f;
-  Header header;
-  int i, j, status, N;
-  char *dBFile, *timestr, *photstr;
-  int Nimage, nimage, *index, Ntimes;
-  unsigned long *tstart, *tstop;
-  int VERBOSE, FORCE_READ, PHOTCODE, dbstate;
-  Image *image;
-  int PhotCodeSelect, Nin;
-  int CodeSelect, Code;
-  char *FitsOutput, *NameSelect;
-  int NameSelectLength;
-  struct stat filestatus;
-  int size;
 
   int N;
@@ -32,5 +16,5 @@
 
   /* interpret command-line arguments */
-  if (!get_trange_arguments (&argc, argv, &tstart, &tstop, &Ntimes)) {
+  if (!get_trange_arguments (&argc, argv, &criteria.tstart, &criteria.tstop, &criteria.Ntimes)) {
     fprintf (stderr, "ERROR: syntax error\n");
     exit (1);
@@ -38,41 +22,59 @@
 
   /* select by image photcode */
-  PhotCodeSelect = FALSE;
-  PHOTCODE = 0;
+  criteria.PhotcodeSelect = FALSE;
+  criteria.photcode = 0;
   if ((N = get_argument (argc, argv, "-photcode"))) {
     remove_argument (N, &argc, argv);
-    if (!(PHOTCODE = GetPhotcodeCodebyName (argv[N]))) {
+    if (!(criteria.photcode = GetPhotcodeCodebyName (argv[N]))) {
       fprintf (stderr, "ERROR: photcode not found in photcode table\n");
       exit (1);
     }
     remove_argument (N, &argc, argv);
-    PhotCodeSelect = TRUE;
+    criteria.PhotcodeSelect = TRUE;
   }
 
   /* string in image name */
-  NameSelect = (char *) NULL;
-  NameSelectLength = 0;
+  criteria.NameSelect = FALSE;
   if ((N = get_argument (argc, argv, "-name"))) {
     remove_argument (N, &argc, argv);
-    NameSelect = strcreate (argv[N]);
+    criteria.Name = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
-    NameSelectLength = strlen (NameSelect);
+    criteria.NameSelect = TRUE;
   }
 
   /* string in image name */
-  CodeSelect = FALSE;
-  Code = 0;
+  criteria.CodeSelect = FALSE;
+  criteria.Code = 0;
   if ((N = get_argument (argc, argv, "-code"))) {
-    CodeSelect = TRUE;
+    criteria.CodeSelect = TRUE;
     remove_argument (N, &argc, argv);
-    Code = atoi (argv[N]);
+    criteria.Code = atoi (argv[N]);
     remove_argument (N, &argc, argv);
   }
 
-  FitsOutput = (char *) NULL;
+  options.table = (char *) NULL;
   if ((N = get_argument (argc, argv, "-fits"))) {
     remove_argument (N, &argc, argv);
-    FitsOutput = strcreate (argv[N]);
+    options.table = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
+  }
+
+  options.bintable = (char *) NULL;
+  if ((N = get_argument (argc, argv, "-binfits"))) {
+    remove_argument (N, &argc, argv);
+    options.bintable = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  /* desired action */
+  options.modify = FALSE;
+  options.ModifyValue = options.ModifyEntry = NULL;
+  if ((N = get_argument (argc, argv, "-flag"))) {
+    remove_argument (N, &argc, argv);
+    options.ModifyEntry = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+    options.ModifyValue = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+    options.modify = TRUE;
   }
 
@@ -93,2 +95,3 @@
     exit (1);
   }
+}
Index: /trunk/Ohana/src/imregister/imphot/db_load.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/db_load.c	(revision 4023)
+++ /trunk/Ohana/src/imregister/imphot/db_load.c	(revision 4024)
@@ -1,5 +1,11 @@
+# include "imregister.h"
+# include "imphot.h"
 
-/* load the complete db table into memory - load first extension, do not validate EXTNAME */
+enum {FITS, TEXT};
+
 int db_load (FITS_DB *db) {
+
+  int Nx, Ny, Naxis;
+  int mode, status;
 
   /* database name must be set first */
@@ -15,4 +21,13 @@
   }
 
+  fits_scan (&db[0].header, "NAXIS",  "%d", 1, &Naxis);
+  fits_scan (&db[0].header, "NAXIS1", "%d", 1, &Nx);
+  fits_scan (&db[0].header, "NAXIS2", "%d", 1, &Ny);
+  
+  mode = FITS;
+  if ((Naxis == 2) && (Nx == 1106) && (Ny == 1024)) {
+    mode = TEXT;
+  }
+  
   /* how do we decide if it is text or fits? must examine header */
   if (FITS) {
@@ -23,26 +38,2 @@
   return (TRUE);
 }
-
-/* load the complete db table into memory - load first extension, do not validate EXTNAME */
-int rfits (FITS_DB *db) {
-
-  /* database name must be set first */
-  if (db == NULL) {
-    fprintf (stderr, "db handle is not set\n");
-    return (FALSE);
-  }
-  if (!fits_fread_matrix (db[0].f, &db[0].matrix, &db[0].header)) {
-    fprintf (stderr, "can't read primary matrix");
-    return (FALSE);
-  }
-  if (!fits_fread_header (db[0].f, &db[0].theader)) {
-    fprintf (stderr, "can't read table header");
-    return (FALSE);
-  }
-  if (!fits_fread_ftable_data (db[0].f, &db[0].ftable)) {
-    fprintf (stderr, "can't read table data");
-    return (FALSE);
-  }
-  return (TRUE);
-}
-
Index: /trunk/Ohana/src/imregister/imphot/dumpfits.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/dumpfits.c	(revision 4023)
+++ /trunk/Ohana/src/imregister/imphot/dumpfits.c	(revision 4024)
@@ -1,8 +1,54 @@
-int DumpFitsTable (char *filename, Image *image, int Nentry, int *index, int Nkeep) {
+# include "imregister.h"
+# include "imphot.h"
+
+/* write out complete binary FITS table in format of db */
+int DumpFitsBintable (char *filename, Image *image, int *match, int Nmatch) {
+
+  int i, j;
+  FILE *f;
+  Header header;
+  Matrix matrix;
+  Header theader;
+  FTable ftable;
+  Image *subset;
+
+  ALLOCATE (subset, Image, MAX (1, Nmatch));
+  for (i = 0; i < Nmatch; i++){
+    j = match[i];
+    memcpy (&subset[i], &image[j], sizeof (Image));
+  }
+
+  /* open file for output */
+  f = fopen (filename, "w");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't open output file %s\n", filename);
+    exit (1);
+  }
+
+  /* create primary header */
+  fits_init_header (&header);    
+  header.extend = TRUE;
+  fits_create_header (&header);
+  fits_create_matrix (&header, &matrix);
+  fits_print (&header, "NEXTEND", "%d", 1, 1);
+
+  ftable.header = &theader;
+  fits_table_set_Image (&ftable, subset, Nmatch);
+
+  fits_fwrite_header  (f, &header);
+  fits_fwrite_matrix  (f, &matrix);
+  fits_fwrite_Theader (f, &theader);
+  fits_fwrite_table   (f, &ftable);
+  fclose (f);
+  exit (0);
+}
+
+int DumpFitsTable (char *filename, Image *image, int *match, int Nmatch) {
   
-  Header header, theader;
+  Header header;
   Matrix matrix;
-  FTable table;
-  Image *newdata;
+  Header theader;
+  FTable ftable;
+  Image *subset;
   FILE *f;
   char *startstr, *filtstr, *datestr, *line;
@@ -18,7 +64,11 @@
   fits_print (&header, "NEXTEND", "%d", 1, 1);
   
+  /* the ASCII table is always a little harder than the binary:
+   * we need to build the data line a bit carefully 
+   */
+
   /* create table header */
   fits_create_table_header (&theader, "TABLE", "ZPTS");
-    
+
   /* add current date/time to header */
   str_to_time ("now", &tsecond);
@@ -37,5 +87,5 @@
   fits_define_table_column (&theader, "F7.1",  "SKY",        "median sky flux",           "counts");
   fits_define_table_column (&theader, "I6",    "NSTAR",      "Number of stars in image",  "stars");
-  
+
   /* define TNULL, TNVAL values */
   fits_modify (&theader, "TNULL1",  "%s", 1, "NULL"); /* START_TIME */
@@ -59,51 +109,33 @@
   fits_modify (&theader, "TNVAL9",  "%s", 1,  "-2");  /* NSTAR      */
 
-  /* create table, add data values */
-  fits_create_table (&theader, &table);
-  
-  /* add selected data to table */
-  for (i = 0; i < Nkeep; i++) {
-    newdata  = &image[index[i]];
-    startstr = sec_to_date (newdata[0].tzero);
-    filtstr  = GetPhotcodeNamebyCode (newdata[0].source);
-    zp       = 0.001*newdata[0].Mcal;
-    dzp      = 0.001*newdata[0].dMcal;
-    
-    XY_to_RD (&ra, &dec, 0.0, 0.0, &newdata[0].coords);
-    airmass  = 0.001*newdata[0].secz;
-    sky      = newdata[0].Myyyy + 0x8000;
+  /* add data to table */
+  for (i = 0; i < Nmatch; i++) {
+    subset   = &image[match[i]];
+    startstr = sec_to_date (subset[0].tzero);
+    filtstr  = GetPhotcodeNamebyCode (subset[0].source);
+    zp       = 0.001*subset[0].Mcal;
+    dzp      = 0.001*subset[0].dMcal;
+    XY_to_RD (&ra, &dec, 0.0, 0.0, &subset[0].coords);
+    airmass  = 0.001*subset[0].secz;
+    sky      = subset[0].Myyyy + 0x8000;
 
-    line = fits_table_print (&table, startstr, filtstr, zp, dzp, ra, dec, airmass, sky, newdata[0].nstar);
-    fits_add_rows (&table, line, 1, strlen(line));
-
+    /* we should get an error here if we don't construct this line correctly */
+    line = fits_table_print (&ftable, startstr, filtstr, zp, dzp, ra, dec, airmass, sky, subset[0].nstar);
+    fits_add_rows (&ftable, line, 1, strlen(line));
     free (line);
     free (startstr);
   }
-    
+
   /* write data to output file */
   f = fopen (filename, "w");
   if (f == (FILE *) NULL) {
     fprintf (stderr, "Failure writing fits table\n");
-    return (FALSE);
+    exit (1);
   }
   fits_fwrite_header  (f, &header);
   fits_fwrite_matrix  (f, &matrix);
   fits_fwrite_Theader (f, &theader);
-  fits_fwrite_table   (f, &table);
+  fits_fwrite_table   (f, &ftable);
   fclose (f);
   return (TRUE);
 }
-
-/*
-
-  selection options:
-
-  CCDSelect
-  TypeSelect
-  TimeSelect
-  FilterSelect
-  ExptimeSelect
-  EntrySelect
-  LabelSelect
-
-*/
Index: /trunk/Ohana/src/imregister/imphot/modify.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/modify.c	(revision 4024)
+++ /trunk/Ohana/src/imregister/imphot/modify.c	(revision 4024)
@@ -0,0 +1,40 @@
+# include "imregister.h"
+# include "imphot.h"
+
+void ModifySubset (FITS_DB *db, Image *image, int Nimage, int *match, int Nmatch) {
+
+  int i, j;
+  VTable vtable;
+
+  /* modify the selected entries */
+  for (j = 0; j < Nmatch; j++) {
+
+    i = match[j];
+
+    if (options.modify) {
+      if (!strcasecmp (options.ModifyEntry, "and")) {
+	image[i].code &= atoi (options.ModifyValue);
+      }
+      if (!strcasecmp (options.ModifyEntry, "or")) {
+	image[i].code |= atoi (options.ModifyValue);
+      }
+      if (!strcasecmp (options.ModifyEntry, "xor")) {
+	image[i].code ^= atoi (options.ModifyValue);
+      }
+      if (!strcasecmp (options.ModifyEntry, "=")) {
+	image[i].code = atoi (options.ModifyValue);
+      }
+    }
+  }
+
+  /** we may later want to pull this out and put it elsewhere **/
+  fits_vtable_from_ftable (&db[0].ftable, &vtable, match, Nmatch);
+  for (i = 0; i < Nmatch; i++) {
+    fits_convert_Image ((Image *) vtable.buffer[i], sizeof (Image), 1);
+  }
+  fits_db_update (db, &vtable);
+  fits_db_close (db);
+  fits_db_free (db);
+
+  exit (0);
+}
Index: /trunk/Ohana/src/imregister/imphot/output.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/output.c	(revision 4023)
+++ /trunk/Ohana/src/imregister/imphot/output.c	(revision 4024)
@@ -1,24 +1,45 @@
-int DumpFitsTable (char *filename, Image *image, int Nentry, int *index, int Nkeep);
+# include "imregister.h"
+# include "imphot.h"
 
+int output (Image *image, int *match, int Nmatch) {
 
-output () {
+  int status;
+
   /* output the selected entries */
-  if (FitsOutput != (char *) NULL) {
-    status = DumpFitsTable (FitsOutput, image, Nimage, index, Nin);
-  } else {
-    for (j = 0; j < Nin; j++) {
-      i = index[j];
+  if (options.table != (char *) NULL) {
+    status = DumpFitsTable (options.table, image, match, Nmatch);
+    return (TRUE);
+  } 
+
+  /* output the selected entries */
+  if (options.bintable != (char *) NULL) {
+    status = DumpFitsBintable (options.bintable, image, match, Nmatch);
+    return (TRUE);
+  } 
+
+  PrintSubset (image, match, Nmatch);
+  return (TRUE);
+}
+
+int PrintSubset (Image *image, int *match, int Nmatch) {
+
+  int i, j;
+  char *timestr, *photstr;
+  static char PhotError[] = "unknown";
+
+  for (j = 0; j < Nmatch; j++) {
+    i = match[j];
       
-      /* convert UNIX time to Elixir-style date string */
-      timestr = sec_to_date (image[i].tzero);
+    /* convert UNIX time to Elixir-style date string */
+    timestr = sec_to_date (image[i].tzero);
       
-      /* convert photcode to filter name */
-      photstr = GetPhotcodeNamebyCode (image[i].source);
-      if (photstr == (char *) NULL) photstr = PhotError;
+    /* convert photcode to filter name */
+    photstr = GetPhotcodeNamebyCode (image[i].source);
+    if (photstr == (char *) NULL) photstr = PhotError;
       
-      fprintf (stdout, "%s %s %s  %7.4f %7.4f  %7.4f %5d %02x\n", image[i].name, photstr, timestr, 
-	       0.001*image[i].Mcal, 0.001*image[i].dMcal, 0.001*image[i].secz, image[i].nstar, image[i].code); 
-      free (timestr);
-    }
+    fprintf (stdout, "%s %s %s  %7.4f %7.4f  %7.4f %5d %02x\n", image[i].name, photstr, timestr, 
+	     0.001*image[i].Mcal, 0.001*image[i].dMcal, 0.001*image[i].secz, image[i].nstar, image[i].code); 
+    free (timestr);
   }
+  return (TRUE);
 }
Index: /trunk/Ohana/src/imregister/imphot/rfits.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/rfits.c	(revision 4024)
+++ /trunk/Ohana/src/imregister/imphot/rfits.c	(revision 4024)
@@ -0,0 +1,25 @@
+# include "imregister.h"
+
+/* load the rest of the db table into memory (first extension only) */
+int rfits (FITS_DB *db) {
+
+  /* database name must be set first */
+  if (db == NULL) {
+    fprintf (stderr, "db handle is not set\n");
+    return (FALSE);
+  }
+  if (!fits_fread_matrix (db[0].f, &db[0].matrix, &db[0].header)) {
+    fprintf (stderr, "can't read primary matrix");
+    return (FALSE);
+  }
+  if (!fits_fread_header (db[0].f, &db[0].theader)) {
+    fprintf (stderr, "can't read table header");
+    return (FALSE);
+  }
+  if (!fits_fread_ftable_data (db[0].f, &db[0].ftable)) {
+    fprintf (stderr, "can't read table data");
+    return (FALSE);
+  }
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/imregister/imphot/rtext.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/rtext.c	(revision 4023)
+++ /trunk/Ohana/src/imregister/imphot/rtext.c	(revision 4024)
@@ -1,17 +1,23 @@
+# include "imregister.h"
+# include "imphot.h"
 
 int rtext (FITS_DB *db) {
+
+  int Nimage, size, nimage;
+  struct stat filestatus;
+  Image *image;
 
   /* check that file size makes sense */
   Nimage = 0;
   fits_scan (&db[0].header, "NIMAGES", "%d", 1, &Nimage);
-  if (stat (dBFile, &filestatus) == -1) {
+  if (stat (db[0].filename, &filestatus) == -1) {
     if (VERBOSE) fprintf (stderr, "ERROR: failed to get status of image catalog\n");
     exit (1);
   }
-  size = Nimage*sizeof(Image) + header.size;
+  size = Nimage*sizeof(Image) + db[0].header.size;
   if (size != filestatus.st_size) {
     int Ndata;
 
-    Ndata = (filestatus.st_size - header.size) / sizeof (Image);
+    Ndata = (filestatus.st_size - db[0].header.size) / sizeof (Image);
     if (VERBOSE) fprintf (stderr, "ERROR: image catalog has inconsistent size\n");
     if (VERBOSE) fprintf (stderr, "header: %d, data: %d\n", Nimage, Ndata);
@@ -27,6 +33,4 @@
   db[0].ftable.header = &db[0].theader;
 
-  /* set theader size information to match loaded data */
-
   /* alloc, read images */
   ALLOCATE (image, Image, MAX (Nimage, 1));
@@ -36,5 +40,9 @@
     exit (1);
   } 
-  db[0].ftable.buffer = image;
+  db[0].ftable.buffer = (char *) image;
+  fits_modify (&db[0].theader, "NAXIS2", "%d", 1, Nimage);
+  db[0].theader.Naxis[1] = Nimage;
+  db[0].ftable.size = fits_matrix_size (&db[0].theader);
+  
   return (TRUE);
 }
Index: /trunk/Ohana/src/imregister/imphot/subset.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/subset.c	(revision 4023)
+++ /trunk/Ohana/src/imregister/imphot/subset.c	(revision 4024)
@@ -1,21 +1,32 @@
+# include "imregister.h"
+# include "imphot.h"
 
-subset () {
+int *subset (Image *image, int Nimage, int *nsubset) {
+
+  int i, j, status;
+  int Nsubset, NameSelectLength;
+  int *index;
 
   /* allocate space for reference lists */
-  Nin = 0;
+  Nsubset = 0;
   ALLOCATE (index, int, Nimage);
   
-  /* note that we are looking for overlap only with tstart */
+  if (criteria.NameSelect) {
+    NameSelectLength = strlen(criteria.Name);
+  }
+
   for (i = 0; i < Nimage; i++) {
-    for (j = 0, status = FALSE; !status && (j < Ntimes); j++) {
-      status = (image[i].tzero >= tstart[j]) && (image[i].tzero <= tstop[j]);
+    for (j = 0, status = FALSE; !status && (j < criteria.Ntimes); j++) {
+      status = (image[i].tzero >= criteria.tstart[j]) && (image[i].tzero <= criteria.tstop[j]);
     }
-    if (!status && Ntimes) continue;
-    if (PhotCodeSelect && (image[i].source != PHOTCODE)) continue;
-    if ((NameSelect != (char *) NULL) && (strncasecmp (image[i].name, NameSelect, NameSelectLength))) continue;
-    if (CodeSelect && (image[i].code != Code)) continue;
+    if (!status && criteria.Ntimes) continue;
+    if (criteria.PhotcodeSelect && (image[i].source != criteria.photcode)) continue;
+    if (criteria.CodeSelect     && (image[i].code != criteria.Code)) continue;
+    if (criteria.NameSelect     && strncasecmp (image[i].name, criteria.Name, NameSelectLength)) continue;
 
-    index[Nin] = i;
-    Nin ++;
+    index[Nsubset] = i;
+    Nsubset ++;
   }
+  *nsubset = Nsubset;
+  return (index);
 }
Index: /trunk/Ohana/src/imregister/include/imphot.h
===================================================================
--- /trunk/Ohana/src/imregister/include/imphot.h	(revision 4024)
+++ /trunk/Ohana/src/imregister/include/imphot.h	(revision 4024)
@@ -0,0 +1,29 @@
+
+struct {
+  int PhotcodeSelect; int photcode;
+  int NameSelect;     char *Name;
+  int Ntimes;         unsigned long *tstart, *tstop;
+  int CodeSelect;     int Code;
+} criteria;
+
+struct {
+  int   modify;
+  char *ModifyValue;
+  char *ModifyEntry;
+  char *table;
+  char *bintable;
+} options;
+
+int VERBOSE;
+int FORCE_READ;
+
+int db_load (FITS_DB *db);
+int args (int argc, char **argv);
+int DumpFitsBintable (char *filename, Image *image, int *match, int Nmatch);
+int DumpFitsTable (char *filename, Image *image, int *match, int Nmatch);
+void ModifySubset (FITS_DB *db, Image *image, int Nimage, int *match, int Nmatch);
+int PrintSubset (Image *image, int *match, int Nmatch);
+int output (Image *image, int *match, int Nmatch);
+int rfits (FITS_DB *db);
+int rtext (FITS_DB *db);
+int *subset (Image *image, int Nimage, int *nsubset);
Index: /trunk/Ohana/src/imregister/src/imphotsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imphotsearch.c	(revision 4023)
+++ /trunk/Ohana/src/imregister/src/imphotsearch.c	(revision 4024)
@@ -1,12 +1,18 @@
 # include "imregister.h"
+# include "imphot.h"
 
-static char *version = "imphotsearch $Revision: 1.6 $";
+static char *version = "imphotsearch $Revision: 1.7 $";
 
 int main (int argc, char **argv) {
  
+  int Nmatch, Nimage;
+  int *match;
+  Image *image;
+  FITS_DB db;
+
   get_version (argc, argv, version);
-  args (&argc, argv);
+  args (argc, argv);
  
-  db.lockstate = (output.modify || output.delete) ? LCK_HARD : LCK_SOFT;
+  db.lockstate = (options.modify) ? LCK_HARD : LCK_SOFT;
   db.timeout   = 300.0;
   fits_db_init (&db);
@@ -23,15 +29,15 @@
     exit (1);
   }
+  if (!options.modify) fits_db_close (&db);
 
-  if (!output.modify && !output.delete) fits_db_close (&db);
+  image = fits_table_get_Image (&db.ftable, &Nimage);
 
-  image = fits_table_get_RegImage (&db.ftable, &Nimage);
+  match = subset (image, Nimage, &Nmatch);
+  if (options.modify) ModifySubset (&db, image, Nimage, match, Nmatch);
 
-  subset ();
-
-  output ();
+  output (image, match, Nmatch);
 
   if (VERBOSE) fprintf (stderr, "SUCCESS\n");
   exit (0);
+}
 
-}
