Index: trunk/Ohana/src/getstar/Makefile
===================================================================
--- trunk/Ohana/src/getstar/Makefile	(revision 12774)
+++ trunk/Ohana/src/getstar/Makefile	(revision 12840)
@@ -22,4 +22,7 @@
 dvoImageOverlaps: $(BIN)/dvoImageOverlaps.$(ARCH)
 dvoImageOverlaps.install: $(DESTBIN)/dvoImageOverlaps
+
+dvoImageExtract: $(BIN)/dvoImageExtract.$(ARCH)
+dvoImageExtract.install: $(DESTBIN)/dvoImageExtract
 
 install: getstar.install dvoImageOverlaps.install
@@ -52,2 +55,15 @@
 $(OVERLAPS): $(INC)/dvoImageOverlaps.h
 $(BIN)/dvoImageOverlaps.$(ARCH): $(OVERLAPS)
+
+EXTRACT = \
+$(SRC)/dvoImageExtract.$(ARCH).o 	 \
+$(SRC)/ConfigInit_extract.$(ARCH).o 	 \
+$(SRC)/args_extract.$(ARCH).o		 \
+$(SRC)/Shutdown.$(ARCH).o	         \
+$(SRC)/SetSignals.$(ARCH).o	         \
+$(SRC)/WriteImages.$(ARCH).o             \
+$(SRC)/WriteImageFITS.$(ARCH).o          \
+$(SRC)/SelectImages.$(ARCH).o       
+
+$(EXTRACT): $(INC)/dvoImageExtract.h
+$(BIN)/dvoImageExtract.$(ARCH): $(EXTRACT)
Index: trunk/Ohana/src/getstar/include/dvoImageExtract.h
===================================================================
--- trunk/Ohana/src/getstar/include/dvoImageExtract.h	(revision 12840)
+++ trunk/Ohana/src/getstar/include/dvoImageExtract.h	(revision 12840)
@@ -0,0 +1,33 @@
+# include <ohana.h>
+# include <dvo.h>
+# include <signal.h>
+# include <glob.h>
+
+enum {NONE, SIMPLE_CMP, SIMPLE_CMF, SIMPLE_MEF, MOSAIC_CMP, MOSAIC_CMF, MOSAIC_MEF, MOSAIC_PHU};
+
+int       VERBOSE;
+
+char OUTPUT[256];
+char GSCFILE[256];
+char CATDIR[256];
+char CATMODE[16];    /* raw, mef, split, mysql */
+char CATFORMAT[16];  /* internal, elixir, loneos, panstarrs */
+char SKY_TABLE[256];
+int  SKY_DEPTH;
+char   ImageCat[256];
+char *OUTFILE;
+Coords *MOSAIC;         // carries the mosaic into ReadImageHeader
+
+int  args_extract    	 PROTO((int argc, char **argv));
+int  ConfigInit_extract  PROTO((int *argc, char **argv));
+int  Shutdown         	 PROTO((char *format, ...));
+void TrapSignal       	 PROTO((int sig));
+void SetProtect       	 PROTO((int mode));
+int  SetSignals       	 PROTO(());
+
+int GetFileMode (Header *header);
+int edge_check (double *x1, double *y1, double *x2, double *y2);
+
+int  WriteImageFITS (FILE *f, Image *image);
+int  WriteImages (char *filename, Image *images, int Nimages, int *matches, int Nmatches);
+int *SelectImages (char *filename, Image *dbImages, int NdbImages, int *Nmatch);
Index: trunk/Ohana/src/getstar/src/ConfigInit_extract.c
===================================================================
--- trunk/Ohana/src/getstar/src/ConfigInit_extract.c	(revision 12840)
+++ trunk/Ohana/src/getstar/src/ConfigInit_extract.c	(revision 12840)
@@ -0,0 +1,46 @@
+# include "dvoImageExtract.h"
+
+int ConfigInit_extract (int *argc, char **argv) {
+
+  char *config, *file;
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
+
+  /*** 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 (1);
+  }
+  if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
+
+  ScanConfig (config, "GSCFILE",                "%s", 0, GSCFILE);
+  ScanConfig (config, "CATDIR",                 "%s", 0, CATDIR);
+  ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
+  ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
+  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);
+  if (!ScanConfig (config, "SKY_DEPTH",         "%d",  0, &SKY_DEPTH)) {
+    SKY_DEPTH = 2;
+  }
+  if (!ScanConfig (config, "SKY_TABLE",         "%s",  0, SKY_TABLE)) {
+    SKY_TABLE[0] = 0;
+  }
+  sprintf (ImageCat, "%s/Images.dat", CATDIR);
+
+  if (*CATMODE == 0) strcpy (CATMODE, "RAW");
+  if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
+
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
+    exit (1);
+  }
+
+  free (config);
+  free (file);
+
+  return (TRUE);
+}
Index: trunk/Ohana/src/getstar/src/SelectImages.c
===================================================================
--- trunk/Ohana/src/getstar/src/SelectImages.c	(revision 12840)
+++ trunk/Ohana/src/getstar/src/SelectImages.c	(revision 12840)
@@ -0,0 +1,37 @@
+# include "dvoImageExtract.h"
+
+/* given image, find catalog images which overlap it */
+int *SelectImages (char *filename, Image *images, int Nimages, int *Nmatch) {
+  
+  int i, Nchar, status;
+  int NMATCH, nmatch, *match;
+
+  /* matches here are only based on string comparisons */
+
+  /* match represents the subset of matched images */
+  nmatch = 0;
+  NMATCH = 20;
+  ALLOCATE (match, int, NMATCH);
+
+  /* setup links for mosaic WRP and DIS entries */
+  BuildChipMatch (images, Nimages);
+
+  Nchar = strlen (filename);
+
+  for (i = 0; i < Nimages; i++) {
+
+    if (strncmp (images[i].name, filename, Nchar)) continue;
+
+    match[nmatch] = i;
+    nmatch ++;
+    if (nmatch == NMATCH) {
+      NMATCH += 20;
+      REALLOCATE (match, int, NMATCH);
+    }
+  }  
+
+  if (VERBOSE) fprintf (stderr, "found %d matching images\n", nmatch);
+
+  *Nmatch = nmatch;
+  return (match);
+}
Index: trunk/Ohana/src/getstar/src/SetSignals.c
===================================================================
--- trunk/Ohana/src/getstar/src/SetSignals.c	(revision 12774)
+++ trunk/Ohana/src/getstar/src/SetSignals.c	(revision 12840)
@@ -30,14 +30,18 @@
   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 */
+      /* can't redirect these signals */
+    case SIGKILL:    /* kill -9: cannot be caught or ignored (POSIX.1-1990) */
+    case SIGSTOP:    /* SIGSTOP: cannot be caught or ignored (POSIX.1-1990) */
       /* 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 */
+    case SIGCHLD:    /* child halted: ignore (POSIX.1-1990) */
+    case SIGCONT:    /* continue - maintain this action (POSIX.1-1990) */
+    case SIGTSTP:    /* stop signal sent from tty - why ignore? (POSIX.1-1990) */
+    case SIGURG:     /* socket signal, ignore this (POSIX.1-2001) */
+# ifdef (SIGPWR)
+    case SIGPWR:     /* power failure - why ignore this? (Sys V) */
+# endif
+# ifdef (SIGWINCH)
+    case SIGWINCH:   /* window resized (4.3BSD) */
+# endif
       break;
       
Index: trunk/Ohana/src/getstar/src/WriteImageFITS.c
===================================================================
--- trunk/Ohana/src/getstar/src/WriteImageFITS.c	(revision 12840)
+++ trunk/Ohana/src/getstar/src/WriteImageFITS.c	(revision 12840)
@@ -0,0 +1,42 @@
+# include "dvoImageExtract.h"
+
+/* given image, find catalog images which overlap it */
+int WriteImageFITS (FILE *f, Image *image) {
+  
+  int Nstars;
+  Header header;
+  Matrix matrix;
+  Header theader;
+  FTable table;
+
+  gfits_init_header (&header);
+  header.extend = TRUE;
+  header.Naxes = 2;
+  if (image) {
+    header.Naxis[0] = image[0].NX;
+    header.Naxis[1] = image[0].NY;
+  }
+  gfits_create_header (&header);
+
+  if (image) {
+    PutCoords (&image[0].coords, &header);
+  }
+  /* create (NOT empty) data matrix */
+  gfits_create_matrix (&header, &matrix);
+
+  gfits_fwrite_header  (f, &header);
+  gfits_fwrite_matrix  (f, &matrix);
+  return (TRUE);
+
+  Nstars = 0;
+  if (image) Nstars = image[0].nstar;
+
+  table.header = &theader;
+  gfits_table_set_SMPData (&table, NULL, Nstars);
+  gfits_fwrite_Theader (f, &theader);
+  gfits_fwrite_table   (f, &table);
+
+  return TRUE;
+}
+
+// XXX this is a temporary hack to get skycell output working
Index: trunk/Ohana/src/getstar/src/WriteImages.c
===================================================================
--- trunk/Ohana/src/getstar/src/WriteImages.c	(revision 12840)
+++ trunk/Ohana/src/getstar/src/WriteImages.c	(revision 12840)
@@ -0,0 +1,56 @@
+# include "dvoImageExtract.h"
+
+/* given image, find catalog images which overlap it */
+int WriteImages (char *filename, Image *images, int Nimages, int *matches, int Nmatches) {
+  
+  int i, N, status;
+  int NMATCH, nmatch, *match;
+  int nWRP, newWRP, isWRP;
+  FILE *f;
+
+  /* matches here are only based on string comparisons */
+
+  f = fopen (filename, "w");
+  if (f == NULL) {
+    fprintf (stderr, "failed to open output file %s\n", filename);
+    exit (1);
+  }
+
+  nWRP = -1;
+  isWRP = FALSE;
+  for (i = 0; i < Nmatches; i++) {
+    if (!strcmp (&images[0].coords.ctype[4], "-WRP")) {
+      if (!FindMosaicForImage (images, Nimages, i)) {
+	fprintf (stderr, "failed to find matching mosaic\n");
+	exit (1);
+      }
+      if (isWRP) {
+	newWRP = GetRegisteredMosaic();
+	if (newWRP != nWRP) {
+	  fprintf (stderr, "only one mosaic allowed in an output file\n");
+	  exit (1);
+	}
+      } else {
+	nWRP = GetRegisteredMosaic();
+	isWRP = TRUE;
+      }
+    }      
+  }
+
+  if (isWRP) {
+    WriteImageFITS (f, &images[nWRP]);
+  } else {
+    // write a blank
+    if (Nmatches > 1) {
+      WriteImageFITS (f, NULL);
+    }
+  }
+
+  for (i = 0; i < Nmatches; i++) {
+    N = matches[i];
+    WriteImageFITS (f, &images[N]);
+  }  
+
+  fclose (f);
+  return TRUE;
+}
Index: trunk/Ohana/src/getstar/src/args_extract.c
===================================================================
--- trunk/Ohana/src/getstar/src/args_extract.c	(revision 12840)
+++ trunk/Ohana/src/getstar/src/args_extract.c	(revision 12840)
@@ -0,0 +1,38 @@
+# include "dvoImageExtract.h"
+
+void help () {
+  fprintf (stderr, "USAGE: \n"
+	   "dvoExtractImages (imageID) [-o output]\n"
+    );
+  exit (2);
+}
+
+int args_extract (int argc, char **argv) {
+  
+  int N;
+
+  /* check for help request */
+  if (get_argument (argc, argv, "-help") ||
+      get_argument (argc, argv, "-h")) {
+    help ();
+  }
+
+  /* check for command line options */
+  VERBOSE = FALSE;
+  if ((N = get_argument (argc, argv, "-v"))) {
+    VERBOSE = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  /* check for command line options */
+  OUTFILE = NULL;
+  if ((N = get_argument (argc, argv, "-o"))) {
+    remove_argument (N, &argc, argv);
+    OUTFILE = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  if (argc != 2) help();
+
+  return (TRUE);
+}
Index: trunk/Ohana/src/getstar/src/dvoImageExtract.c
===================================================================
--- trunk/Ohana/src/getstar/src/dvoImageExtract.c	(revision 12840)
+++ trunk/Ohana/src/getstar/src/dvoImageExtract.c	(revision 12840)
@@ -0,0 +1,51 @@
+# include "dvoImageExtract.h"
+
+int main (int argc, char **argv) {
+
+  int i, Nimages, status;
+  int Nmatches, *matches;
+  Image *images;
+  Catalog catalog;
+  FITS_DB db;
+
+  SetSignals ();
+  ConfigInit_extract (&argc, argv);
+  args_extract (argc, argv);
+
+  /*** update the image table ***/
+  /* setup image table format and lock */
+  db.mode   = dvo_catalog_catmode (CATMODE);
+  db.format = dvo_catalog_catformat (CATFORMAT);
+  status    = dvo_image_lock (&db, ImageCat, 3600.0, LCK_SOFT);  // shorter timeout?
+  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
+
+  /* load or create the image table */
+  if (db.dbstate == LCK_EMPTY) {
+    fprintf (stderr, "no images in database (%s)\n", ImageCat);
+    exit (1);
+  } else {
+    if (!dvo_image_load (&db, VERBOSE, FALSE)) {
+      Shutdown ("can't read image catalog %s", db.filename);
+    }
+  }
+  dvo_image_unlock (&db);
+
+  // convert database table to internal structure
+  images = gfits_table_get_Image (&db.ftable, &Nimages, &db.swapped);
+  
+  matches = SelectImages (argv[1], images, Nimages, &Nmatches);
+  WriteImages (OUTFILE, images, Nimages, matches, Nmatches);
+
+  exit (0);
+}
+
+/* This program extracts images headers from a DVO database and writes them 
+   in cmf format (headers + object tables).
+
+   If multiple images are selected, they must be from the same exposures (how?).
+
+   If images are selected with WRP astrometry, the associated DIS image header is first written to
+   the output file.
+ 
+   USAGE: dvoExtractImages (imageID[s]) -o output.cmf
+*/
