Index: trunk/Ohana/src/imregister/imphot/dumpfits.c
===================================================================
--- trunk/Ohana/src/imregister/imphot/dumpfits.c	(revision 3650)
+++ 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
-
-*/
