Index: /trunk/Ohana/src/imregister/Makefile
===================================================================
--- /trunk/Ohana/src/imregister/Makefile	(revision 3592)
+++ /trunk/Ohana/src/imregister/Makefile	(revision 3593)
@@ -81,6 +81,4 @@
 
 DETOBJ  = \
-$(DETREND)/db.$(ARCH).o        \
-$(DETREND)/define.$(ARCH).o    \
 $(DETREND)/criteria.$(ARCH).o  \
 $(DETREND)/define.$(ARCH).o    \
Index: /trunk/Ohana/src/imregister/detrend/altpath.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/altpath.c	(revision 3592)
+++ /trunk/Ohana/src/imregister/detrend/altpath.c	(revision 3593)
@@ -2,16 +2,12 @@
 # include "detrend.h"
 
-int SetAltpath (Match *match, int Nmatch) {
+int SetAltpath (FITS_DB *db, DetReg *image, int Nimage, Match *match, int Nmatch) {
   
-  int i, j, n, found, status;
-  int Nimage, Nlist;
+  int i, j, n, found, status, Nlist;
   int *list, *current;
   char *dBPath, infile[256], outfile[256], line[1024];
-  DetReg *image;
   struct stat statbuf;
 
-  image = get_images (&Nimage);
   ALLOCATE (current, int, Nimage);
-
   ALLOCATE (list, int, Nimage);
   Nlist = 0;
@@ -49,6 +45,14 @@
       image[i].altpath = TRUE;
     }
-    update_db (list, Nlist);
-    close_db ();
+
+    /** we may later want to pull this out and put it elsewhere **/
+    fits_vtable_from_ftable (&db[0].ftable, &vtable, list, Nlist);
+    for (i = 0; i < Nmatch; i++) {
+      fits_convert_DetReg ((DetReg *) vtable.buffer[i], sizeof (DetReg), 1);
+    }
+    fits_db_update (db, &vtable);
+    fits_db_close (db);
+    fits_db_free (db);
+
     fprintf (stderr, "SUCCESS\n");
     exit (0);
@@ -78,6 +82,14 @@
       }
     }
-    update_db (list, Nlist);
-    close_db ();
+
+    /** we may later want to pull this out and put it elsewhere **/
+    fits_vtable_from_ftable (&db[0].ftable, &vtable, list, Nlist);
+    for (i = 0; i < Nmatch; i++) {
+      fits_convert_DetReg ((DetReg *) vtable.buffer[i], sizeof (DetReg), 1);
+    }
+    fits_db_update (db, &vtable);
+    fits_db_close (db);
+    fits_db_free (db);
+
     fprintf (stderr, "SUCCESS\n");
     exit (0);
@@ -99,6 +111,14 @@
       image[i].altpath = found;
     }
-    update_db (list, Nlist);
-    close_db ();
+
+    /** we may later want to pull this out and put it elsewhere **/
+    fits_vtable_from_ftable (&db[0].ftable, &vtable, list, Nlist);
+    for (i = 0; i < Nmatch; i++) {
+      fits_convert_DetReg ((DetReg *) vtable.buffer[i], sizeof (DetReg), 1);
+    }
+    fits_db_update (db, &vtable);
+    fits_db_close (db);
+    fits_db_free (db);
+
     fprintf (stderr, "SUCCESS\n");
     exit (0);
Index: /trunk/Ohana/src/imregister/detrend/criteria.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/criteria.c	(revision 3592)
+++ /trunk/Ohana/src/imregister/detrend/criteria.c	(revision 3593)
@@ -60,14 +60,12 @@
 */
 
-Match *ExptimeCriteria (Match *match, int *nmatch) {
-
-  int i, j, Nimage, Nmatch, Nnew, NNEW, entry;
+Match *ExptimeCriteria (DetReg *image, int Nimage, Match *match, int *nmatch) {
+
+  int i, j, Nmatch, Nnew, NNEW, entry;
   unsigned long dtime;
   float Chi, ChiMin;
-  DetReg *image;
   Match *new;
 
   Nmatch = *nmatch;
-  image = get_images (&Nimage);
 
   Nnew = 0;
@@ -134,12 +132,9 @@
 }
 
-Match *CloseCriteria (Match *match, int *nmatch) {
-
-  int i, j, Nimage, Nmatch, Nnew, NNEW, Ngood, entry;
+Match *CloseCriteria (DetReg *image, int Nimage, Match *match, int *nmatch) {
+
+  int i, j, Nmatch, Nnew, NNEW, Ngood, entry;
   unsigned long dmin, dtime;
   Match *new;
-  DetReg *image;
-
-  image = get_images (&Nimage);
 
   Nmatch = *nmatch;
Index: /trunk/Ohana/src/imregister/detrend/db_names.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/db_names.c	(revision 3593)
+++ /trunk/Ohana/src/imregister/detrend/db_names.c	(revision 3593)
@@ -0,0 +1,56 @@
+# include "imregister.h"
+# include "detrend.h"
+
+static char *dBFile  = NULL;
+static char *dBTrash = NULL;
+
+/*** these are functions to handle the special detrend.db file/trash names ****/
+
+int delete_image (DetReg *item) {
+  
+  int status;
+  char line[256];
+
+  if (output.verbose) fprintf (stderr, "deleting %s\n", item[0].filename);
+  sprintf (line, "mv -f %s/%s %s", DetrendDB, item[0].filename, dBTrash);
+  status = system (line);
+  if (status) fprintf (stderr, "trouble moving %s to trash (%s)\n", item[0].filename, dBTrash);
+
+  if (status) 
+    return (FALSE);
+  else 
+    return (TRUE);
+}
+
+char *set_db_name () {
+
+  struct stat statbuf;
+  int status;
+
+  /* define dBFile based on config data */
+  ALLOCATE (dBFile, char, strlen (DetrendDB) + 15);
+  sprintf (dBFile, "%s/detrend.db", DetrendDB);;
+  ALLOCATE (dBTrash, char, strlen (DetrendDB) + 15);
+  sprintf (dBTrash, "%s/trash", DetrendDB);
+
+  /* check on directories */
+  status = stat (DetrendDB, &statbuf);
+  if (output.Modify) {
+    if (status == -1) {
+      if (mkdirhier (DetrendDB) == -1) {
+	fprintf (stderr, "ERROR: can't find or create path %s\n", DetrendDB);
+	exit (1);
+      }
+    }
+  }
+  if (output.Delete) {
+    status = stat (dBTrash, &statbuf);
+    if (status == -1) {
+      if (mkdirhier (dBTrash) == -1) {
+	fprintf (stderr, "ERROR: detrend dB trash not found %s\n", dBTrash);
+	exit (1);
+      }
+    }
+  }
+  return (dBFile);
+}
Index: /trunk/Ohana/src/imregister/detrend/delete.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/delete.c	(revision 3592)
+++ /trunk/Ohana/src/imregister/detrend/delete.c	(revision 3593)
@@ -2,11 +2,9 @@
 # include "detrend.h"
 
-void DeleteSubset (Match *match, int Nmatch) {
+void DeleteSubset (FITS_DB *db, DetReg *image, int Nimage, Match *match, int Nmatch) {
 
   int i, j;
-  int *keep, Ndel, Nimage, Nsubset;
-  DetReg *image, *subset;
-
-  image = get_images (&Nimage);
+  int *keep, Ndel, Nsubset;
+  DetReg *subset;
 
   ALLOCATE (keep, int, MAX (Nimage, 1));
@@ -23,8 +21,7 @@
   }
   fprintf (stderr, "delete %d images\n", Ndel);
-  
   if (Ndel == 0) { 
     fprintf (stderr, "SUCCESS\n");
-    close_db ();
+    fits_close_db (&db);
     exit (0);
   }
@@ -39,10 +36,14 @@
     j ++;
   }
-
   free (keep);
   free (image);
-  set_images (subset, Nsubset);
 
-  save_db ();
+  /** we may later want to pull this out and put it elsewhere **/
+  /** free db[0].theader, db[0].table.buffer? **/
+  fits_table_set_DetReg (&db[0].ftable, subset, Nsubset);
+  fits_db_save (db);
+  fits_db_close (db);
+  fits_db_free (db);
+
   fprintf (stderr, "SUCCESS\n");
   exit (0);
Index: /trunk/Ohana/src/imregister/detrend/match.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/match.c	(revision 3592)
+++ /trunk/Ohana/src/imregister/detrend/match.c	(revision 3593)
@@ -2,12 +2,9 @@
 # include "detrend.h"
 
-Match *MatchCriteria (int *nmatch) {
+Match *MatchCriteria (DetReg *image, int Nimage, int *nmatch) {
 
   int i;
-  int Nmatch, NMATCH, Nimage;
+  int Nmatch, NMATCH;
   Match result, *match;
-  DetReg *image;
-
-  image = get_images (&Nimage);
 
   /* find entries that matches criteria */
Index: /trunk/Ohana/src/imregister/detrend/modify.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/modify.c	(revision 3592)
+++ /trunk/Ohana/src/imregister/detrend/modify.c	(revision 3593)
@@ -2,12 +2,10 @@
 # include "detrend.h"
 
-int ModifySubset (Match *match, int Nmatch) {
+int ModifySubset (FITS_DB *db, DetReg *image, int Nimage, Match *match, int Nmatch) {
   
-  int i, j, Nimage, value;
+  int i, j, value;
   int *list;
-  DetReg *image;
   
   value = M_UNDEF;
-  image = get_images (&Nimage);
 
   ALLOCATE (list, int, Nimage);
@@ -45,6 +43,13 @@
   }
 
-  update_db (list, Nmatch);
-  close_db ();
+  /** 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_DetReg ((DetReg *) vtable.buffer[i], sizeof (DetReg), 1);
+  }
+  fits_db_update (db, &vtable);
+  fits_db_close (db);
+  fits_db_free (db);
+
   fprintf (stderr, "SUCCESS\n");
   exit (0);
Index: /trunk/Ohana/src/imregister/detrend/output.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/output.c	(revision 3592)
+++ /trunk/Ohana/src/imregister/detrend/output.c	(revision 3593)
@@ -5,8 +5,5 @@
 extern double drand48();
 
-int OutputSubset (Match *match, int Nmatch) {
-
-  int Nimage;
-  DetReg *image;
+int OutputSubset (DetReg *image, int Nimage, Match *match, int Nmatch) {
 
   image = get_images (&Nimage);
@@ -32,7 +29,5 @@
   FTable table;
   DetReg *subset;
-
-  /* define table layout */
-  define_table (&header, &matrix, &theader, &table);
+  FILE *f;
 
   ALLOCATE (subset, DetReg, MAX (1, Nmatch));
@@ -41,11 +36,26 @@
     memcpy (&subset[i], &image[j], sizeof (DetReg));
   }
-  ConvertStruct ((char *) subset, sizeof (DetReg), Nmatch, "detreg");
-  fits_add_rows (&table, (char *) subset, Nmatch, sizeof (DetReg));
-
-  fits_write_header  (filename, &header);
-  fits_write_matrix  (filename, &matrix);
-  fits_write_Theader (filename, &theader);
-  fits_write_table   (filename, &table);
+
+  /* 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_DetReg (&ftable, subset, Nmatch);
+
+  fits_write_header  (f, &header);
+  fits_write_matrix  (f, &matrix);
+  fits_write_Theader (f, &theader);
+  fits_write_table   (f, &table);
   exit (0);
 }
@@ -78,4 +88,5 @@
   fits_modify (&header,  "DATE", "%s", 1, datestr);
   fits_modify (&theader, "DATE", "%s", 1, datestr);
+  free (datestr);
 
   /* define table layout */
Index: /trunk/Ohana/src/imregister/detrend/unique.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/unique.c	(revision 3592)
+++ /trunk/Ohana/src/imregister/detrend/unique.c	(revision 3593)
@@ -3,16 +3,13 @@
 
 /* define a list of best images which have unique properties */ 
-Match *UniqueSubset (Match *match, int *nmatch) {
+Match *UniqueSubset (DetReg *image, int Nimage, Match *match, int *nmatch) {
 
   int Nsubset, Nunique, Ncrit, NCRIT;
-  int i, j, N, Nmatch, Nimage, found;
+  int i, j, N, Nmatch, found;
   Criteria *crit;
   Match *subset, *unique, *local;
-  DetReg *image;
 
   Nmatch = *nmatch;
   ALLOCATE (local, Match, Nmatch);
-
-  image = get_images (&Nimage);
 
   /* create a set of complete criteria derived from the images */
Index: /trunk/Ohana/src/imregister/include/detrend.h
===================================================================
--- /trunk/Ohana/src/imregister/include/detrend.h	(revision 3592)
+++ /trunk/Ohana/src/imregister/include/detrend.h	(revision 3593)
@@ -72,36 +72,27 @@
 int args (int argc, char **argv);
 int DefineImage (char *filename, Descriptor *descriptor);
-int define_table (Header *header, Matrix *matrix, Header *theader, FTable *table);
+DetReg DefineEntry (Descriptor descriptor);
+
 char *get_dBPath ();
-DetReg DefineEntry (Descriptor descriptor);
 int SaveEntry (char *input, DetReg *newdata, char *ID);
-
-void init_db ();
-int load_db ();
-int save_db ();
-int update_db (int *match, int Nmatch);
-int append_db (DetReg *new, int Nnew);
-int create_db ();
-int close_db ();
 
 char **LoadRecipe (char *filter, int *nrecipe);
 
-Match *MatchCriteria (int *nmatch);
-Match *UniqueSubset (Match *match, int *nmatch);
-Match *CloseCriteria (Match *match, int *nmatch);
-Match *ExptimeCriteria (Match *match, int *nmatch);
+Match *MatchCriteria (DetReg *image, int Nimage, int *nmatch);
+Match *UniqueSubset (DetReg *image, int Nimage, Match *match, int *nmatch);
+Match *ExptimeCriteria (DetReg *image, int Nimage, Match *match, int *nmatch);
+Match *CloseCriteria (DetReg *image, int Nimage, Match *match, int *nmatch);
 
 Match CheckCriteria (DetReg *image);
-DetReg *get_images (int *N);
-int set_images (DetReg *new, int Nnew);
 
 int delete_image (DetReg *image);
-int OutputSubset (Match *match, int Nmatch);
+int OutputSubset (DetReg *image, int Nimage, Match *match, int Nmatch);
 int DumpFitsTable (char *filename, DetReg *detdata, Match *match, int Nmatch);
 int PrintSubset (DetReg *detdata, Match *match, int Nmatch);
 Match SelectEntry (Match *list, int Nlist, Criteria *crit);
 
-int ModifySubset (Match *match, int Nmatch);
-void DeleteSubset (Match *match, int Nmatch);
+int ModifySubset (DetReg *image, int Nimage, Match *match, int Nmatch);
+void DeleteSubset (DetReg *image, int Nimage, Match *match, int Nmatch);
+
 int usage ();
 Criteria *MosaicCriteria (Criteria base, char *filename, int *ncrit);
Index: /trunk/Ohana/src/imregister/spreg/delete.c
===================================================================
--- /trunk/Ohana/src/imregister/spreg/delete.c	(revision 3592)
+++ /trunk/Ohana/src/imregister/spreg/delete.c	(revision 3593)
@@ -20,4 +20,9 @@
   }
   fprintf (stderr, "delete %d spectra\n", Nbad);
+  if (Nbad == 0) { 
+    fprintf (stderr, "SUCCESS\n");
+    fits_close_db (&db);
+    exit (0);
+  }
 
   Nsubset = Nspectrum - Nbad;
@@ -29,6 +34,6 @@
     j++;
   }
-
   free (keep);
+  free (spectrum);
 
   /** we may later want to pull this out and put it elsewhere **/
Index: /trunk/Ohana/src/imregister/src/detsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/src/detsearch.c	(revision 3592)
+++ /trunk/Ohana/src/imregister/src/detsearch.c	(revision 3593)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "detrend.h"
-static char *version = "detsearch $Revision: 3.3 $";
+static char *version = "detsearch $Revision: 3.4 $";
 
 int main (int argc, char **argv) {
@@ -15,31 +15,40 @@
   if (output.Criteria) PrintCriteria ();
 	
-  init_db ();
-  status = load_db ();
-  if (!status) {
-    close_db ();
-    fprintf (stderr, "database missing or empty\n");
-    exit (0);
+  db.lockstate = (output.modify || output.delete) ? LCK_HARD : LCK_SOFT;
+  db.timeout   = 300.0;
+  fits_db_init (&db);
+
+  dBFile = set_db_name ();
+  if (!fits_db_lock (&db, dBFile)) {
+    fits_db_close (&db);
+    exit (1);
   }
-  if (!output.Modify && !output.Delete && !output.Altpath) close_db ();
   
-  match = MatchCriteria (&Nmatch);          /* match basic criteria */
-  match = ExptimeCriteria (match, &Nmatch); /* reduce matches based on Exptime */
-  match = CloseCriteria (match, &Nmatch);   /* reduce matches based on closeness */
+  if (!fits_db_load (&db)) {
+    fits_db_close (&db);
+    exit (1);
+  }
+
+  if (!output.Modify && !output.Delete && !output.Altpath) fits_close_db (&db);
+  detrend = fits_table_get_DetReg (&db.ftable, &Ndetrend);
+  
+  match = MatchCriteria (detrend, Ndetrend, &Nmatch);          /* match basic criteria */
+  match = ExptimeCriteria (detrend, Ndetrend, match, &Nmatch); /* reduce matches based on Exptime */
+  match = CloseCriteria (detrend, Ndetrend, match, &Nmatch);   /* reduce matches based on closeness */
 
   if (output.Select) {
-    match = UniqueSubset (match, &Nmatch);
+    match = UniqueSubset (detrend, Ndetrend, match, &Nmatch);
     if (Nmatch == 0) {
       if (output.verbose) fprintf (stderr, "ERROR: can't find any valid detrend files (%s %s %d)\n", get_type_name(criteria[0].Type), filtername[criteria[0].Filter], criteria[0].CCD);
-      close_db ();
+      fits_close_db (&db);
       exit (1);
     }
   }
 
-  if (output.Altpath) SetAltpath (match, Nmatch);
-  if (output.Modify) ModifySubset (match, Nmatch);
-  if (output.Delete) DeleteSubset (match, Nmatch);
+  if (output.Altpath) SetAltpath (&db, detrend, Ndetrend, match, Nmatch);
+  if (output.Modify) ModifySubset (&db, detrend, Ndetrend, match, Nmatch);
+  if (output.Delete) DeleteSubset (&db, detrend, Ndetrend, match, Nmatch);
 
-  OutputSubset (match, Nmatch);
+  OutputSubset (detrend, Ndetrend, match, Nmatch);
   exit (0);
 }
Index: /trunk/Ohana/src/libautocode/Makefile
===================================================================
--- /trunk/Ohana/src/libautocode/Makefile	(revision 3592)
+++ /trunk/Ohana/src/libautocode/Makefile	(revision 3593)
@@ -29,4 +29,5 @@
 # remember to add entries here to libohana/Makefile:AOBJ as well
 LIBOBJ = \
+$(SRC)/detreg.$(ARCH).o \
 $(SRC)/photreg.$(ARCH).o \
 $(SRC)/photreg-old.$(ARCH).o \
@@ -36,4 +37,5 @@
 
 LIBINC = \
+$(INC)/detreg.h \
 $(INC)/photreg.h \
 $(INC)/photreg-old.h \
Index: /trunk/Ohana/src/libautocode/def/detreg.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/detreg.d	(revision 3593)
+++ /trunk/Ohana/src/libautocode/def/detreg.d	(revision 3593)
@@ -0,0 +1,21 @@
+STRUCT  DetReg
+EXTNAME DETREND_DATABASE
+TYPE    BINTABLE
+SIZE    416
+
+# elements of data structure / FITS table
+
+FIELD tstart,         START_TIME, e_time,    start time of measurement, seconds since 1970 Jan 01 UT
+FIELD tstop,          STOP_TIME,  e_time,    stop time of measurement,  seconds since Jan 1, 1970 UT
+FIELD treg,           REG_TIME,   e_time,    time of registration,      seconds since Jan 1, 1970 UT
+FIELD exptime,        EXPTIME,    float,     exposure time,             seconds
+FIELD type,           IMAGETYP,   int,       detrend type number
+FIELD filter,         FILTER,     int,       filter number
+FIELD ccd,            CCDNUM,     int,       ccd number
+FIELD Nentry,         VERSION,    int,       image version number
+FIELD Norder,         ORDER,      int,       selection order
+FIELD mode,           MODE,       char,      image mode
+FIELD altpath,        ALTPATH,    char,      available on alt db paths, true for data on alt db paths
+FIELD dummy,          RESERVED,   char[58],  space for additions,       for future expansion
+FIELD label,          LABEL,      char[64],  data label
+FIELD filename,       PATH,       char[256], filename in db
Index: /trunk/Ohana/src/libohana/Makefile
===================================================================
--- /trunk/Ohana/src/libohana/Makefile	(revision 3592)
+++ /trunk/Ohana/src/libohana/Makefile	(revision 3593)
@@ -48,4 +48,5 @@
 
 AOBJ = \
+$(ASRC)/detreg.$(ARCH).o \
 $(ASRC)/photreg.$(ARCH).o \
 $(ASRC)/photreg-old.$(ARCH).o \
