Index: /trunk/Ohana/src/delstar/include/delstar.h
===================================================================
--- /trunk/Ohana/src/delstar/include/delstar.h	(revision 4606)
+++ /trunk/Ohana/src/delstar/include/delstar.h	(revision 4607)
@@ -14,4 +14,10 @@
 int    ORPHAN;
 int    MISSED;
+
+time_t START;
+time_t END;
+
+int    MODE;
+enum {MODE_IMAGENAME, MODE_IMAGEFILE, MODE_TRANGE, MODE_ORPHAN, MODE_MISSED};
 
 typedef struct {
Index: /trunk/Ohana/src/delstar/src/SetSignals.c
===================================================================
--- /trunk/Ohana/src/delstar/src/SetSignals.c	(revision 4607)
+++ /trunk/Ohana/src/delstar/src/SetSignals.c	(revision 4607)
@@ -0,0 +1,118 @@
+# include "delstar.h"
+
+static int Protect = FALSE;
+static int Trapped = FALSE;
+
+void TrapSignal (int sig) {
+    fprintf (stderr, "trapped signal %d\n", sig);
+    if (sig == 11) {
+      fprintf (stderr, "seg fault\n");
+      exit (1);
+    }
+    if (Protect) {
+      Trapped = TRUE;
+      fprintf (stderr, "blocking until protected sections are clear\n");
+      return;
+    }
+    Shutdown ("halted by signal (trapped)");
+}    
+
+void SetProtect (int mode) {
+  Protect = mode;
+  if (Trapped && !Protect) Shutdown ("halted by signal (protect)");
+}
+
+int SetSignals () {
+
+  int i;
+
+  /* disable almost all signal interrupts */
+  for (i = 0; i < 36; i++) {
+    switch (i) {
+      /* can't redirect this signals */
+    case SIGKILL:    /* kill -9: cannot be caught or ignored */
+    case SIGSTOP:    /* SIGSTOP: cannot be caught or ignored */
+      /* ignore these signals */
+    case SIGCHLD:    /* child halted: ignore */
+    case SIGPWR:     /* power failure - why ignore this? */
+    case SIGWINCH:   /* window resized */
+    case SIGCONT:    /* continue - maintain this action */
+    case SIGTSTP:    /* stop signal sent from tty - why ignore? */
+    case SIGURG:     /* socket signal, ignore this */
+      break;
+      
+    default:
+      signal (i, TrapSignal);
+    }
+  }
+  return (TRUE);
+}
+/*
+
+       Signal     Value     Action   Comment
+       -------------------------------------------------------------------------
+       SIGHUP        1        A      Hangup detected on controlling terminal
+                                     or death of controlling process
+       SIGINT        2        A      Interrupt from keyboard
+       SIGQUIT       3        A      Quit from keyboard
+       SIGILL        4        A      Illegal Instruction
+       SIGABRT       6        C      Abort signal from abort(3)
+       SIGFPE        8        C      Floating point exception
+       SIGKILL       9       AEF     Kill signal
+       SIGSEGV      11        C      Invalid memory reference
+       SIGPIPE      13        A      Broken pipe: write to pipe with no readers
+       SIGALRM      14        A      Timer signal from alarm(2)
+       SIGTERM      15        A      Termination signal
+       SIGUSR1   30,10,16     A      User-defined signal 1
+       SIGUSR2   31,12,17     A      User-defined signal 2
+       SIGCHLD   20,17,18     B      Child stopped or terminated
+       SIGCONT   19,18,25            Continue if stopped
+       SIGSTOP   17,19,23    DEF     Stop process
+       SIGTSTP   18,20,24     D      Stop typed at tty
+       SIGTTIN   21,21,26     D      tty input for background process
+       SIGTTOU   22,22,27     D      tty output for background process
+
+       Next various other signals.
+
+       Signal       Value     Action   Comment
+       ---------------------------------------------------------------------
+       SIGTRAP        5         CG     Trace/breakpoint trap
+       SIGIOT         6         CG     IOT trap. A synonym for SIGABRT
+       SIGEMT       7,-,7       G
+       SIGBUS      10,7,10      AG     Bus error
+       SIGSYS      12,-,12      G      Bad argument to routine (SVID)
+       SIGSTKFLT    -,16,-      AG     Stack fault on coprocessor
+       SIGURG      16,23,21     BG     Urgent condition on socket (4.2 BSD)
+       SIGIO       23,29,22     AG     I/O now possible (4.2 BSD)
+       SIGPOLL                  AG     A synonym for SIGIO (System V)
+       SIGCLD       -,-,18      G      A synonym for SIGCHLD
+       SIGXCPU     24,24,30     AG     CPU time limit exceeded (4.2 BSD)
+       SIGXFSZ     25,25,31     AG     File size limit exceeded (4.2 BSD)
+       SIGVTALRM   26,26,28     AG     Virtual alarm clock (4.2 BSD)
+       SIGPROF     27,27,29     AG     Profile alarm clock
+       SIGPWR      29,30,19     AG     Power failure (System V)
+       SIGINFO      29,-,-      G      A synonym for SIGPWR
+       SIGLOST      -,-,-       AG     File lock lost
+       SIGWINCH    28,28,20     BG     Window resize signal (4.3 BSD, Sun)
+       SIGUNUSED    -,31,-      AG     Unused signal
+       (Here - denotes that a signal is absent; there where three values are given, the first one is usually  valid  for  alpha  and
+       sparc,  the  middle  one  for i386 and ppc, the last one for mips. Signal 29 is SIGINFO / SIGPWR on an alpha but SIGLOST on a
+       sparc.)
+
+       The letters in the "Action" column have the following meanings:
+
+       A      Default action is to terminate the process.
+
+       B      Default action is to ignore the signal.
+
+       C      Default action is to dump core.
+
+       D      Default action is to stop the process.
+
+       E      Signal cannot be caught.
+
+       F      Signal cannot be ignored.
+
+       G      Not a POSIX.1 conformant signal.
+
+*/
Index: /trunk/Ohana/src/delstar/src/args.c
===================================================================
--- /trunk/Ohana/src/delstar/src/args.c	(revision 4606)
+++ /trunk/Ohana/src/delstar/src/args.c	(revision 4607)
@@ -4,4 +4,7 @@
 
   fprintf (stderr, "USAGE: delstar (filename) or (start range) or -orphan (region)\n");
+  fprintf (stderr, "       delstar -trange (start) (range)\n");
+  fprintf (stderr, "       delstar -orphan (region)\n");
+  fprintf (stderr, "       delstar -missed (region)\n");
   fprintf (stderr, "  optional flags:\n");
   fprintf (stderr, "  -v (verbose mode)\n");
@@ -11,10 +14,17 @@
 }
 
+void usage () {
+  fprintf (stderr, "USAGE: delstar (filename)\n");
+  fprintf (stderr, "USAGE: delstar -name (imagename)\n");
+  fprintf (stderr, "USAGE: delstar -trange (start) (range)\n");
+  fprintf (stderr, "USAGE: delstar -orphan (region)\n");
+  fprintf (stderr, "USAGE: delstar -missed (region)\n");
+  exit (2);
+}
+
 int args (int *argc, char **argv) {
   
 
   int N;
-
-  ConfigInit (argc, argv);
 
   /* check for help request */
@@ -30,20 +40,66 @@
   }
 
-  ORPHAN = FALSE;
-  if ((N = get_argument (*argc, argv, "-orphan"))) {
-    ORPHAN = TRUE;
+  MODE = MODE_IMAGEFILE;
+  if ((N = get_argument (*argc, argv, "-name"))) {
+    if (MODE != MODE_IMAGEFILE) usage();
+    MODE = MODE_IMAGENAME;
     remove_argument (N, argc, argv);
   }
-
-  MISSED = FALSE;
-  if ((N = get_argument (*argc, argv, "-missed"))) {
-    MISSED = TRUE;
+  if ((N = get_argument (*argc, argv, "-orphan"))) {
+    if (MODE != MODE_IMAGEFILE) usage();
+    MODE = MODE_ORPHAN;
     remove_argument (N, argc, argv);
   }
+  if ((N = get_argument (*argc, argv, "-missed"))) {
+    if (MODE != MODE_IMAGEFILE) usage();
+    MODE = MODE_MISSED;
+    remove_argument (N, argc, argv);
+  }
+  if ((N = get_argument (*argc, argv, "-time"))) {
+    if (MODE != MODE_IMAGEFILE) usage();
+    MODE = MODE_TRANGE;
+    remove_argument (N, argc, argv);
 
-  if ((*argc != 2) && (*argc != 3)) {
-    fprintf (stderr, "ERROR: Usage: delstar (filename) or (start range) or -orphan (region)\n");
-    exit (2);
+    /* tstart */
+    if (!str_to_time (argv[N], &START)) usage ();
+    remove_argument (N, argc, argv);
+
+    /* interpret second value */
+    if (str_to_dtime (argv[N], &trange)) { 
+      if (trange < 0) {
+	END = START;
+	START = END + trange;
+      } else {
+	END = START + trange;
+      }
+      remove_argument (Na, argc, argv);
+      goto goodtime;
+    }
+    if (str_to_time (argv[N], &END)) { 
+      if (START > END) {
+	tmp   = START;
+	START = END;
+	END   = START;
+      }
+      remove_argument (Na, argc, argv);
+      goto goodtime;
+    }
+    usage ();
+
+  goodtime:
+    fprintf (stderr, "start: %d\n", start);
+    fprintf (stderr, "end: %d\n", end);
   }
 
+  /* restrict to a single photcode (not compatible with -image) */
+  PHOTCODE = NULL;
+  if ((N = get_argument (argc, argv, "-photcode"))) {
+    remove_argument (N, &argc, argv);
+    PHOTCODE = GetPhotcodebyName (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  if ((MODE == MODE_TIME) && (*argc != 1)) usage ();
+  if ((MODE != MODE_TIME) && (*argc != 2)) usage ();
+  return (TRUE);
 }
Index: /trunk/Ohana/src/delstar/src/delete_image.c
===================================================================
--- /trunk/Ohana/src/delstar/src/delete_image.c	(revision 4607)
+++ /trunk/Ohana/src/delstar/src/delete_image.c	(revision 4607)
@@ -0,0 +1,43 @@
+
+delete_imagefile (char *filename) {
+
+  int i, Nregions;
+  Image *image;
+  GSCRegion *region;
+  Catalog catalog;
+
+    image = gimages (filename);
+  
+    fprintf (stderr, "deleting %s\n", image[0].name);
+    region = gregions (image, &Nregions);
+
+    for (i = 0; i < Nregions; i++) {
+      fprintf (stderr, "deleting from %s\n", region[i].filename);
+      catalog.filename = region[i].filename;  /* don't free region before catalog! */
+      switch (lock_catalog (&catalog, LCK_SOFT)) {
+      case 0:
+	fprintf (stderr, "ERROR: can't lock file\n");
+	exit (1);
+      case 1:
+	gcatalog (&catalog);
+	break;
+      case 2:
+	fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
+	catalog.Naverage = 0;
+	catalog.Nmeasure = 0;
+	break;
+      default:
+	fprintf (stderr, "weird lock_catalog exit state\n");
+	exit (1);
+      }
+      find_matches (&catalog, image);
+      wcatalog (&catalog);
+    }
+
+    dimages (f, image, 1); 
+
+    /* this probably gets moved inside dimages */
+    unlock_image_db (ImageCat, f, LCK_HARD, &dbstate);
+    fprintf (stderr, "SUCCESS\n");
+    exit (0);
+}
Index: /trunk/Ohana/src/delstar/src/delete_imagename.c
===================================================================
--- /trunk/Ohana/src/delstar/src/delete_imagename.c	(revision 4607)
+++ /trunk/Ohana/src/delstar/src/delete_imagename.c	(revision 4607)
@@ -0,0 +1,48 @@
+# include "delstar.h"
+
+void delete_imagename (char *name) {
+
+  int i, Nregions;
+  Image *image;
+  GSCRegion *region;
+  Catalog catalog;
+
+  fprintf (stderr, "this function needs to be debugged\n");
+  exit (2);
+
+  /* find image in db by name */
+  image = find_image (filename);
+  
+  fprintf (stderr, "deleting %s\n", image[0].name);
+  region = gregions (image, &Nregions);
+
+  for (i = 0; i < Nregions; i++) {
+    fprintf (stderr, "deleting from %s\n", region[i].filename);
+    catalog.filename = region[i].filename;  /* don't free region before catalog! */
+    switch (lock_catalog (&catalog, LCK_SOFT)) {
+      case 0:
+	fprintf (stderr, "ERROR: can't lock file\n");
+	exit (1);
+      case 1:
+	gcatalog (&catalog);
+	break;
+      case 2:
+	fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
+	catalog.Naverage = 0;
+	catalog.Nmeasure = 0;
+	break;
+      default:
+	fprintf (stderr, "weird lock_catalog exit state\n");
+	exit (1);
+    }
+    find_matches (&catalog, image);
+    wcatalog (&catalog);
+  }
+
+  dimages (image, 1); 
+
+  /* this probably gets moved inside dimages */
+  unlock_image_db (ImageCat, f, LCK_HARD, &dbstate);
+  fprintf (stderr, "SUCCESS\n");
+  exit (0);
+}
Index: /trunk/Ohana/src/delstar/src/delete_orphans.c
===================================================================
--- /trunk/Ohana/src/delstar/src/delete_orphans.c	(revision 4606)
+++ /trunk/Ohana/src/delstar/src/delete_orphans.c	(revision 4607)
@@ -1,5 +1,5 @@
 # include "delstar.h"
 
-delete_orphans (Catalog *catalog) {
+delete_orphans (char *name) {
 
   int i, j, k, n, m, N, M, found;
@@ -11,13 +11,22 @@
   Missing *tmpmissing;
   Coords tcoords;
-
+  Catalog catalog;
+
+  /* find and load catalog file */
+  catalog.filename = name;
+  gcatalog (&catalog);
+  gcatstats (&catalog, &catstats);
+
+  /* find images overlapping catalog */
+  image = find_images_region (&catstats, &Nimage);
+  match_images (&catalog, image, Nimage);
 
   /** allocate local arrays **/
-  Nave = catalog[0].Naverage;
-
-  Nmeas = catalog[0].Nmeasure;
+  Nave = catalog.Naverage;
+
+  Nmeas = catalog.Nmeasure;
   ALLOCATE (next, int, Nmeas);
   
-  Nmiss = catalog[0].Nmissing;
+  Nmiss = catalog.Nmissing;
   ALLOCATE (next_miss, int, Nmiss);
   ALLOCATE (ave_miss, int, Nmiss);
@@ -39,14 +48,14 @@
   /* set up references for missing to average */
   for (i = 0; i < Nave; i++) {
-    for (j = 0; j < catalog[0].average[i].Nn; j++) {
-      ave_miss[catalog[0].average[i].missing + j] = i;
+    for (j = 0; j < catalog.average[i].Nn; j++) {
+      ave_miss[catalog.average[i].missing + j] = i;
     }
   }
   Nmeasfound = 0;
-  Nsecfilt = catalog[0].Nsecfilt;
+  Nsecfilt = catalog.Nsecfilt;
 
   /* fprintf (stderr, "fixing the measures...\n"); */
   for (i = 0; (i < Nmeas); i++) {
-    if ((catalog[0].measure[i].t != 0) && (catalog[0].image[i] == -1)) { 
+    if ((catalog.measure[i].t != 0) && (catalog.image[i] == -1)) { 
       /* this star is an orphan */
       Nmeasfound ++;
@@ -68,19 +77,19 @@
       
       /*** fix the corresponding average entry ***/
-      n = catalog[0].measure[i].averef;
-      if (catalog[0].average[n].Nm == 0) { /* this should never happen */
+      n = catalog.measure[i].averef;
+      if (catalog.average[n].Nm == 0) { /* this should never happen */
 	fprintf (stderr, "error? we deleted one too many objects?\n");
 	exit (1);
       }
-      catalog[0].average[n].Nm --;
+      catalog.average[n].Nm --;
       /* this was only entry in list: will be deleted below.  meanwhile, delete all missing entries*/
-      if ((catalog[0].average[n].Nm < 1) && (catalog[0].average[n].Nn > 0)) { 
-	m = catalog[0].average[n].missing;
-	for (j = 0; j < catalog[0].average[n].Nn; j++) {
+      if ((catalog.average[n].Nm < 1) && (catalog.average[n].Nn > 0)) { 
+	m = catalog.average[n].missing;
+	for (j = 0; j < catalog.average[n].Nn; j++) {
 	  M = next_miss[m];
 	  next_miss[m] = -2;
 	  m = M;
 	}
-	m = catalog[0].average[n].missing;
+	m = catalog.average[n].missing;
 	/* fix the list links: connect the previous valid link to the next valid link */
 	for (j = m; (j >= 0) && (next_miss[j] == -2); j--); /* find previous entry to fix link */
@@ -99,9 +108,9 @@
       }
       /* this was first entry in list */
-      if ((catalog[0].average[n].offset == i) && (catalog[0].average[n].Nm > 0)) { 
-	m = catalog[0].average[n].offset;
+      if ((catalog.average[n].offset == i) && (catalog.average[n].Nm > 0)) { 
+	m = catalog.average[n].offset;
 	/* find next valid entry -- notice lack of error checking... */
 	for (j = 0; (j < Nmeas) && (next[m+j] == -2); j++);
-	catalog[0].average[n].offset = m + j;
+	catalog.average[n].offset = m + j;
       }
 
@@ -113,5 +122,5 @@
   /** find missing in time range of image **/
   for (i = 0; (i < Nmiss); i++) {
-    if ((next_miss[i] != -2) && (catalog[0].missing[i].t >= start) && (catalog[0].missing[i].t <= end)) { 
+    if ((next_miss[i] != -2) && (catalog.missing[i].t >= start) && (catalog.missing[i].t <= end)) { 
       /* this star is in this image */
 
@@ -135,5 +144,5 @@
       found = FALSE;
       for (n = 0; !found && (n < Nave); n++) {
-	if ((catalog[0].average[n].missing > 0) && (catalog[0].average[n].missing <= i) && (catalog[0].average[n].missing + catalog[0].average[n].Nn > i)) 
+	if ((catalog.average[n].missing > 0) && (catalog.average[n].missing <= i) && (catalog.average[n].missing + catalog.average[n].Nn > i)) 
 	  found = TRUE;
       }
@@ -141,14 +150,14 @@
       /*** fix the corresponding average entry ***/
       n = ave_miss[i];
-      if (catalog[0].average[n].Nn == 0) { /* this should never happen */
+      if (catalog.average[n].Nn == 0) { /* this should never happen */
 	fprintf (stderr, "error? we deleted one too many missing?\n");
 	exit (1);
       }
-      catalog[0].average[n].Nn --;
+      catalog.average[n].Nn --;
       /* this was first entry in list */
-      if ((catalog[0].average[n].missing == i) && (catalog[0].average[n].Nn > 0)) { 
-	m = catalog[0].average[n].missing;
+      if ((catalog.average[n].missing == i) && (catalog.average[n].Nn > 0)) { 
+	m = catalog.average[n].missing;
 	for (j = 0; (j < Nmiss) && (next_miss[m+j] == -2); j++);
-	catalog[0].average[n].missing = m + j;
+	catalog.average[n].missing = m + j;
       }
 
@@ -161,9 +170,9 @@
   /* this image star matches more than one catalog star */
   if (stars[N].found > -1) {
-    catalog[0].measure[stars[N].found].flags |= BLEND_IMAGE;
-    catalog[0].measure[Nmeas].flags |= BLEND_IMAGE;
+    catalog.measure[stars[N].found].flags |= BLEND_IMAGE;
+    catalog.measure[Nmeas].flags |= BLEND_IMAGE;
   } 
   if (stars[N].found == -2) { /* this image star matches a catalog star on a neighboring catalog */
-    catalog[0].measure[Nmeas].flags |= BLEND_IMAGE_NEIGHBOR;
+    catalog.measure[Nmeas].flags |= BLEND_IMAGE_NEIGHBOR;
   } 
   if (stars[N].found == -1) { /* this image star matches only this star */
@@ -171,9 +180,9 @@
   }
   /* this catalog star matches more than one image star */
-  if (catalog[0].found[n] > -1) {
-    catalog[0].measure[catalog[0].found[n]].flags |= BLEND_CATALOG;
-    catalog[0].measure[Nmeas].flags |= BLEND_CATALOG;
+  if (catalog.found[n] > -1) {
+    catalog.measure[catalog.found[n]].flags |= BLEND_CATALOG;
+    catalog.measure[Nmeas].flags |= BLEND_CATALOG;
   } else {
-    catalog[0].found[n] = Nmeas;
+    catalog.found[n] = Nmeas;
   }
 # endif  
@@ -182,9 +191,9 @@
   /* fix Average list: delete entries with Nm == 0 */
   for (i = j = 0; (i < Nave) && (j < Nave); i++, j++) {
-    for (; (j < Nave) && (catalog[0].average[j].Nm == 0); j++);
+    for (; (j < Nave) && (catalog.average[j].Nm == 0); j++);
     if ((i != j) && (j < Nave)) {
-      catalog[0].average[i] = catalog[0].average[j];
-      for (k = 0; k < catalog[0].Nsecfilt; k++) {
-	catalog[0].secfilt[i*Nsecfilt + k] = catalog[0].secfilt[j*Nsecfilt + k];
+      catalog.average[i] = catalog.average[j];
+      for (k = 0; k < catalog.Nsecfilt; k++) {
+	catalog.secfilt[i*Nsecfilt + k] = catalog.secfilt[j*Nsecfilt + k];
       }
     }
@@ -192,6 +201,6 @@
   }
   Nave = i;
-  REALLOCATE (catalog[0].average, Average, Nave);
-  REALLOCATE (catalog[0].secfilt, SecFilt, MAX (1, Nave*Nsecfilt));
+  REALLOCATE (catalog.average, Average, Nave);
+  REALLOCATE (catalog.secfilt, SecFilt, MAX (1, Nave*Nsecfilt));
   
   /* fprintf (stderr, "fixing the measure order...\n"); */
@@ -200,12 +209,12 @@
   ALLOCATE (tmpmeasure, Measure, Nmeas);
   for (i = 0; i < Nave; i++) {
-    n = catalog[0].average[i].offset;
-    catalog[0].average[i].offset = N;
-    for (k = 0; k < catalog[0].average[i].Nm; k++, N++) {
+    n = catalog.average[i].offset;
+    catalog.average[i].offset = N;
+    for (k = 0; k < catalog.average[i].Nm; k++, N++) {
       if ((n == -1) || (n == -2)) {
 	fprintf (stderr, "error: linked list is confused\n");
 	exit (1);
       }
-      tmpmeasure[N] = catalog[0].measure[n]; 
+      tmpmeasure[N] = catalog.measure[n]; 
       tmpmeasure[N].averef = i;
       n = next[n];
@@ -213,7 +222,7 @@
   }
   Nmeas = N;
-  free (catalog[0].measure);
-  catalog[0].measure = tmpmeasure;
-  REALLOCATE (catalog[0].measure, Measure, Nmeas);
+  free (catalog.measure);
+  catalog.measure = tmpmeasure;
+  REALLOCATE (catalog.measure, Measure, Nmeas);
     
   /* fprintf (stderr, "fixing the mising order...\n"); */
@@ -222,13 +231,13 @@
   ALLOCATE (tmpmissing, Missing, Nmiss);
   for (i = 0; i < Nave; i++) {
-    if (catalog[0].average[i].Nn > 0) {
-      n = catalog[0].average[i].missing;
-      catalog[0].average[i].missing = N;
-      for (k = 0; k < catalog[0].average[i].Nn; k++, N++) {
+    if (catalog.average[i].Nn > 0) {
+      n = catalog.average[i].missing;
+      catalog.average[i].missing = N;
+      for (k = 0; k < catalog.average[i].Nn; k++, N++) {
 	if ((n == -1) || (n == -2)) {
 	  fprintf (stderr, "error: linked list is confused\n");
 	  exit (1);
 	}
-	tmpmissing[N] = catalog[0].missing[n]; 
+	tmpmissing[N] = catalog.missing[n]; 
 	n = next_miss[n];
       }
@@ -236,13 +245,12 @@
   }
   Nmiss = N;
-  free (catalog[0].missing);
-  catalog[0].missing = tmpmissing;
-  REALLOCATE (catalog[0].missing, Missing, Nmiss);
-
-  catalog[0].Naverage = Nave;
-  catalog[0].Nmeasure = Nmeas;
-  catalog[0].Nmissing = Nmiss;
+  free (catalog.missing);
+  catalog.missing = tmpmissing;
+  REALLOCATE (catalog.missing, Missing, Nmiss);
+
+  catalog.Naverage = Nave;
+  catalog.Nmeasure = Nmeas;
+  catalog.Nmissing = Nmiss;
   if (VERBOSE) fprintf (stderr, "  ending with Nave, Nmeas, Nmiss: %d %d %d\n", Nave, Nmeas, Nmiss);
 
 }
-
Index: /trunk/Ohana/src/delstar/src/delete_times.c
===================================================================
--- /trunk/Ohana/src/delstar/src/delete_times.c	(revision 4607)
+++ /trunk/Ohana/src/delstar/src/delete_times.c	(revision 4607)
@@ -0,0 +1,58 @@
+# include "delstar.h"
+
+void delete_times () {
+
+    Nregions = 0;
+    NREGIONS = 10;
+    ALLOCATE (region, GSCRegion, NREGIONS);
+
+    /* find image for time range, delete each image */ 
+    image = gtimes (&Nimage);
+    for (j = 0; j < Nimage; j++) {
+      fprintf (stderr, "finding regions for %s\n", image[j].name);
+      tregion = gregions (&image[j], &Ntregions);
+      for (i = 0; i < Ntregions; i++) {
+	found = FALSE;
+	for (k = 0; (k < Nregions) && !found; k++) {
+	  found = !strcmp (region[k].filename, tregion[i].filename);
+	}
+	if (!found) {
+	  strcpy (region[Nregions].filename, tregion[i].filename);
+	  region[Nregions].RA[0] = tregion[i].RA[0];
+	  region[Nregions].RA[1] = tregion[i].RA[1];
+	  region[Nregions].DEC[0] = tregion[i].DEC[0];
+	  region[Nregions].DEC[1] = tregion[i].DEC[1];
+	  Nregions ++;
+	  if (Nregions >= NREGIONS) {
+	    NREGIONS += 10;
+	    REALLOCATE (region, GSCRegion, NREGIONS);
+	  }
+	}
+      }
+    }
+    for (i = 0; i < Nregions; i++) {
+      fprintf (stderr, "deleting from %s\n", region[i].filename);
+      catalog.filename = region[i].filename;  /* don't free region before catalog! */
+      switch (lock_catalog (&catalog, LCK_SOFT)) {
+      case 0:
+	fprintf (stderr, "ERROR: can't lock file\n");
+	exit (1);
+      case 1:
+	gcatalog (&catalog);
+	break;
+      case 2:
+	fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
+	catalog.Naverage = 0;
+	catalog.Nmeasure = 0;
+	break;
+      default:
+	fprintf (stderr, "weird lock_catalog exit state\n");
+	exit (1);
+      }
+      find_matches (&catalog, &image[0], start, end);
+      wcatalog (&catalog);
+    }
+    dimages (image, Nimage);
+    fprintf (stderr, "SUCCESS\n");
+    exit (0);
+}
Index: /trunk/Ohana/src/delstar/src/delstar.c
===================================================================
--- /trunk/Ohana/src/delstar/src/delstar.c	(revision 4606)
+++ /trunk/Ohana/src/delstar/src/delstar.c	(revision 4607)
@@ -1,212 +1,32 @@
 # include "delstar.h"
-
-/* these variables are needed by Shutdown */
-static FILE *f = (FILE *) NULL;
-static int Protect = FALSE;
-static int Trapped = FALSE;
-
-/* clean up open / locked ImageCat before shutting down */
-int Shutdown () {  
-  int dbstate;
-
-  Protect = TRUE;
-  fclearlockfile (ImageCat, f, LCK_HARD, &dbstate);
-  fprintf (stderr, "ERROR: delstar halted\n");
-  exit (1);
-}
-
-void TrapSignal (int sig) {
-    fprintf (stderr, "trapped signal %d, exiting gracefully\n", sig);
-    if (sig == 11) {
-      fprintf (stderr, "seg fault\n");
-      exit (1);
-    }
-    if (Protect) {
-      Trapped = TRUE;
-      fprintf (stderr, "blocking until protected sections are clear\n");
-      return;
-    }
-    Shutdown ();
-}    
 
 main (int argc, char **argv) {
 
-  int i, j, k, found, Nimage, Nregions, Ntregions, NREGIONS;
-  int start, end, dbstate;
-  Image *image, *gimages(), *gtimes(), *find_images();
-  Catalog catalog;
-  CatStats catstats;
-  GSCRegion *region, *tregion, *gregions();
-  
   SetSignals ();
+  ConfigInit (argc, argv);
   args (&argc, argv);
 
-  /* lock the image catalog */
-  f = fsetlockfile (ImageCat, 3600.0, LCK_HARD, &dbstate);
-  if (f == (FILE *) NULL) {
-    fprintf (stderr, "ERROR: can't lock image catalog\n");
-    exit (1);
-  }
-  fseek (f, 0, SEEK_SET);
+  lock_image_db ();
 
   /* delete orphaned measurements (no image) from catalog */ 
-  if (ORPHAN) {
-    ALLOCATE (region, GSCRegion, 1);
-    catalog.filename = argv[1];
-    gcatalog (&catalog);
-    gcatstats (&catalog, &catstats);
-    image = find_images (f, &catstats, &Nimage);
-    match_images (&catalog, image, Nimage);
-    delete_orphans (&catalog);
-    wcatalog (&catalog);
-    fclearlockfile (ImageCat, f, LCK_HARD, &dbstate);
-    fprintf (stderr, "SUCCESS\n");
-    exit (0);
-  } 
-
-  /* delete missed measurements from catalog */ 
-  if (MISSED) {
-    catalog.filename = argv[1];
-    switch (lock_catalog (&catalog, LCK_XCLD)) {
-    case 0:
-      fprintf (stderr, "ERROR: can't lock file\n");
-      exit (1);
-    case 1:
-      gcatalog (&catalog);
+  switch (MODE) {
+    case MODE_IMAGEFILE:
+      delete_imagefile (argv[1]);
       break;
-    case 2:
-      fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
-      catalog.Naverage = 0;
-      catalog.Nmeasure = 0;
+    case MODE_IMAGENAME:
+      delete_imagename (argv[1]);
+      break;
+    case MODE_ORPHANS:
+      delete_orphans (argv[1]);
+      break;
+    case MODE_MISSED:
+      delete_missed (argv[1]);
+      break;
+    case MODE_TIME:
+      delete_time ();
       break;
     default:
-      fprintf (stderr, "weird lock_catalog exit state\n");
-      exit (1);
-    }
-    delete_missed (&catalog);
-    wcatalog (&catalog);
-    fclearlockfile (ImageCat, f, LCK_HARD, &dbstate);
-    fprintf (stderr, "SUCCESS\n");
-    exit (0);
-  } 
-
-  /* delete given image from catalogs */
-  if (argc == 2) {
-    image = gimages (argv[1]);
-  
-    fprintf (stderr, "deleting %s\n", image[0].name);
-    region = gregions (&image[0], &Nregions);
-
-    for (i = 0; i < Nregions; i++) {
-      fprintf (stderr, "deleting from %s\n", region[i].filename);
-      catalog.filename = region[i].filename;  /* don't free region before catalog! */
-      switch (lock_catalog (&catalog, LCK_SOFT)) {
-      case 0:
-	fprintf (stderr, "ERROR: can't lock file\n");
-	exit (1);
-      case 1:
-	gcatalog (&catalog);
-	break;
-      case 2:
-	fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
-	catalog.Naverage = 0;
-	catalog.Nmeasure = 0;
-	break;
-      default:
-	fprintf (stderr, "weird lock_catalog exit state\n");
-	exit (1);
-      }
-      find_matches (&catalog, &image[0], 0, 0);
-      wcatalog (&catalog);
-    }
-
-    dimages (f, image, 1); 
-    fclearlockfile (ImageCat, f, LCK_HARD, &dbstate);
-    fprintf (stderr, "SUCCESS\n");
-    exit (0);
+      usage ();
   }
-
-  /* delete measurements in time range */
-  if (argc == 3) {
-    Nregions = 0;
-    NREGIONS = 10;
-    ALLOCATE (region, GSCRegion, NREGIONS);
-
-    /* find image for time range, delete each image */ 
-    image = gtimes (f, argv, &Nimage, &start, &end);
-    for (j = 0; j < Nimage; j++) {
-      fprintf (stderr, "finding regions for %s\n", image[j].name);
-      tregion = gregions (&image[j], &Ntregions);
-      for (i = 0; i < Ntregions; i++) {
-	found = FALSE;
-	for (k = 0; (k < Nregions) && !found; k++) {
-	  found = !strcmp (region[k].filename, tregion[i].filename);
-	}
-	if (!found) {
-	  strcpy (region[Nregions].filename, tregion[i].filename);
-	  region[Nregions].RA[0] = tregion[i].RA[0];
-	  region[Nregions].RA[1] = tregion[i].RA[1];
-	  region[Nregions].DEC[0] = tregion[i].DEC[0];
-	  region[Nregions].DEC[1] = tregion[i].DEC[1];
-	  Nregions ++;
-	  if (Nregions >= NREGIONS) {
-	    NREGIONS += 10;
-	    REALLOCATE (region, GSCRegion, NREGIONS);
-	  }
-	}
-      }
-    }
-    for (i = 0; i < Nregions; i++) {
-      fprintf (stderr, "deleting from %s\n", region[i].filename);
-      catalog.filename = region[i].filename;  /* don't free region before catalog! */
-      switch (lock_catalog (&catalog, LCK_SOFT)) {
-      case 0:
-	fprintf (stderr, "ERROR: can't lock file\n");
-	exit (1);
-      case 1:
-	gcatalog (&catalog);
-	break;
-      case 2:
-	fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
-	catalog.Naverage = 0;
-	catalog.Nmeasure = 0;
-	break;
-      default:
-	fprintf (stderr, "weird lock_catalog exit state\n");
-	exit (1);
-      }
-      find_matches (&catalog, &image[0], start, end);
-      wcatalog (&catalog);
-    }
-    dimages (f, image, Nimage);
-    fclearlockfile (ImageCat, f, LCK_HARD, &dbstate);
-    fprintf (stderr, "SUCCESS\n");
-    exit (0);
-  }
+  exit (1);
 }
-
-int SetSignals () {
-
-  int i;
-
-  /* disable almost all signal interrupts */
-  for (i = 0; i < 36; i++) {
-    switch (i) {
-      /* can't redirect this signals */
-    case SIGKILL:    /* kill -9: cannot be caught or ignored */
-    case SIGSTOP:    /* SIGSTOP: cannot be caught or ignored */
-      /* ignore these signals */
-    case SIGCHLD:    /* child halted: ignore */
-    case SIGPWR:     /* power failure - why ignore this? */
-    case SIGWINCH:   /* window resized */
-    case SIGCONT:    /* continue - maintain this action */
-    case SIGTSTP:    /* stop signal sent from tty - why ignore? */
-    case SIGURG:     /* socket signal, ignore this */
-      break;
-      
-    default:
-      signal (i, TrapSignal);
-    }
-  }
-
-}
Index: /trunk/Ohana/src/delstar/src/dimages.c
===================================================================
--- /trunk/Ohana/src/delstar/src/dimages.c	(revision 4606)
+++ /trunk/Ohana/src/delstar/src/dimages.c	(revision 4607)
@@ -1,5 +1,5 @@
 # include "addstar.h"
 
-dimages (FILE *f, Image *image, int Nimages) {
+int dimages (Image *image, int Nimages) {
   
   int i, j, Ntimage, ntimage, mode;
@@ -9,4 +9,11 @@
   Image *timage;
   Header header;
+
+  /* get image db file pointer */
+  f = GetDB (&dbstate);
+  if (dbstate == LCK_EMPTY) {
+    Shutdown ("database is empty");
+  }
+  fseek (f, 0, SEEK_SET);
 
   /* use cp to make backup copy */
@@ -70,3 +77,8 @@
   Fwrite (timage, sizeof(Image), Ntimage, f, "image");
 
+  return (TRUE);
 }
+
+/* this is kind of silly: we match images in both gimages and dimages 
+ *
+ */
Index: /trunk/Ohana/src/delstar/src/gimages.c
===================================================================
--- /trunk/Ohana/src/delstar/src/gimages.c	(revision 4606)
+++ /trunk/Ohana/src/delstar/src/gimages.c	(revision 4607)
@@ -1,3 +1,5 @@
 # include "delstar.h"
+
+/* load information about image from image header */
 
 Image *gimages (char *filename) {
Index: /trunk/Ohana/src/delstar/src/gtimes.c
===================================================================
--- /trunk/Ohana/src/delstar/src/gtimes.c	(revision 4606)
+++ /trunk/Ohana/src/delstar/src/gtimes.c	(revision 4607)
@@ -1,37 +1,25 @@
 # include "delstar.h"
 
-Image *gtimes (FILE *f, char **argv, int *NIMAGE, int *START, int *END) {
+Image *gtimes (int *NIMAGE) {
  
   int i, npimage, NPIMAGE, ntimage, Ntimage, NTIMAGE, Nimage;
-  time_t start, end, tstart, tend;
-  double Ra, Dec, range;
+  time_t tstart, tend;
+  double range;
   Image *pimage, *timage;
   Header header;
+  FILE *f;
 
-  if (!str_to_time (argv[1], &start)) { 
-    fprintf (stderr, "syntax error\n");
-    return (FALSE);
+  /* globals: START, END, PHOTCODE */
+
+  /* get image db file pointer */
+  f = GetDB (&dbstate);
+  if (dbstate == LCK_EMPTY) {
+    Shutdown ("database is empty");
   }
-  if (!str_to_dtime (argv[2], &range)) { 
-    fprintf (stderr, "syntax error\n");
-    return (FALSE);
-  }
-  if (range < 0) {
-    range = fabs (range);
-    start -= range;
-  }
-  end = start + range;
+  fseek (f, 0, SEEK_SET);
 
-  fprintf (stderr, "start: %d\n", start);
-  fprintf (stderr, "end: %d\n", end);
-
-  *START = start;
-  *END = end;
-
-  /* get image header */
-  fseek (f, 0, SEEK_SET); 
+  /* read header */
   if (!fits_fread_header (f, &header)) {
-    fprintf (stderr, "Can't find image catalog: %s\n", ImageCat);
-    exit (1);
+    Shutdown ("can't read image catalog %s", ImageCat);
   }
   fseek (f, header.size, SEEK_SET); 
@@ -47,4 +35,5 @@
   ALLOCATE (pimage, Image, NPIMAGE);
 
+  /* add match on photcode */
   for (Ntimage = 0; Ntimage < Nimage; Ntimage += ntimage) {
     ntimage = Fread (timage, sizeof(Image), NTIMAGE, f, "image");
@@ -52,5 +41,5 @@
       tstart = timage[i].tzero;
       tend = timage[i].tzero + 1e-4*timage[i].trate*timage[i].NY;
-      if ((tstart <= end) && (tend >= start)) {
+      if ((tstart <= END) && (tend >= START)) {
 	pimage[npimage] = timage[i];
 	npimage ++;
@@ -64,9 +53,12 @@
   free (timage);
 
-  fprintf (stderr, "found the following images:\n");
-  for (i = 0; i < npimage; i++) {
-    XY_to_RD (&Ra, &Dec, 0.5*pimage[i].NX, 0.5*pimage[i].NY, &pimage[i].coords);
-    fprintf (stderr, "%3d %s %8.4f %8.4f %d %d\n", i, pimage[i].name, Ra, Dec,
-	     pimage[i].tzero, pimage[i].nstar);
+  { 
+    double Ra, Dec;
+    fprintf (stderr, "found the following images:\n");
+    for (i = 0; i < npimage; i++) {
+      XY_to_RD (&Ra, &Dec, 0.5*pimage[i].NX, 0.5*pimage[i].NY, &pimage[i].coords);
+      fprintf (stderr, "%3d %s %8.4f %8.4f %d %d\n", i, pimage[i].name, Ra, Dec,
+	       pimage[i].tzero, pimage[i].nstar);
+    }
   }
 
Index: /trunk/Ohana/src/delstar/src/image-db.c
===================================================================
--- /trunk/Ohana/src/delstar/src/image-db.c	(revision 4607)
+++ /trunk/Ohana/src/delstar/src/image-db.c	(revision 4607)
@@ -0,0 +1,59 @@
+# include "delstar.h"
+
+# define LOCK LCK_XCLD
+
+/* file-pointer to Image.db table. if image db does not yet exist */
+static FILE *f = (FILE *) NULL;
+static int dbstate = LCK_UNLOCK;
+
+FILE *GetDB (int *state) {
+  *state = dbstate;
+  return (f);
+}
+
+/* clean up open / locked ImageCat before shutting down */
+int Shutdown (char *format, ...) {  
+  va_list argp;
+  char *formatplus;
+  
+  ALLOCATE (formatplus, char, strlen(format));
+  strcpy (formatplus, format);
+  strcat (formatplus, "\n");
+
+  va_start (argp, format);
+  vfprintf (stderr, formatplus, argp);
+  free (formatplus);
+  va_end (argp);
+
+  SetProtect (TRUE);
+  fclearlockfile (ImageCat, f, LOCK, &dbstate);
+  fprintf (stderr, "ERROR: addstar halted\n");
+  exit (1);
+}
+
+void lock_image_db () {
+  /* lock the image catalog */
+  check_permissions (ImageCat);
+  f = fsetlockfile (ImageCat, 3600.0, LOCK, &dbstate);
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't lock image catalog\n");
+    exit (1);
+  }
+  fseek (f, 0, SEEK_SET);
+}
+
+void unlock_image_db (Image *image) {
+
+  mode_t mode;
+
+  /* protect wimage from interrupt signals */
+  if (MODE == M_IMAGE) {
+    SetProtect (TRUE);
+    wimage (image); 
+    SetProtect (FALSE);
+  }
+
+  fclearlockfile (ImageCat, f, LOCK, &dbstate);
+  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+  chmod (ImageCat, mode);
+}
Index: /trunk/Ohana/src/delstar/src/missed.c
===================================================================
--- /trunk/Ohana/src/delstar/src/missed.c	(revision 4607)
+++ /trunk/Ohana/src/delstar/src/missed.c	(revision 4607)
@@ -0,0 +1,24 @@
+delete_missed () {
+    catalog.filename = argv[1];
+    switch (lock_catalog (&catalog, LCK_XCLD)) {
+    case 0:
+      fprintf (stderr, "ERROR: can't lock file\n");
+      exit (1);
+    case 1:
+      gcatalog (&catalog);
+      break;
+    case 2:
+      fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
+      catalog.Naverage = 0;
+      catalog.Nmeasure = 0;
+      break;
+    default:
+      fprintf (stderr, "weird lock_catalog exit state\n");
+      exit (1);
+    }
+    delete_missed (&catalog);
+    wcatalog (&catalog);
+    fclearlockfile (ImageCat, f, LCK_HARD, &dbstate);
+    fprintf (stderr, "SUCCESS\n");
+    exit (0);
+}
Index: /trunk/Ohana/src/delstar/src/orphans.c
===================================================================
--- /trunk/Ohana/src/delstar/src/orphans.c	(revision 4607)
+++ /trunk/Ohana/src/delstar/src/orphans.c	(revision 4607)
@@ -0,0 +1,14 @@
+
+delete_orphans () {
+    ALLOCATE (region, GSCRegion, 1);
+    catalog.filename = argv[1];
+    gcatalog (&catalog);
+    gcatstats (&catalog, &catstats);
+    image = find_images_region (&catstats, &Nimage);
+    match_images (&catalog, image, Nimage);
+    delete_orphans (&catalog);
+    wcatalog (&catalog);
+    fclearlockfile (ImageCat, f, LCK_HARD, &dbstate);
+    fprintf (stderr, "SUCCESS\n");
+    exit (0);
+}
