Index: /trunk/Ohana/src/libohana/src/dvo_image.c
===================================================================
--- /trunk/Ohana/src/libohana/src/dvo_image.c	(revision 4851)
+++ /trunk/Ohana/src/libohana/src/dvo_image.c	(revision 4851)
@@ -0,0 +1,112 @@
+# include <ohana.h>
+# include <dvo.h>
+
+enum {FITS, TEXT};
+
+int dvo_image_load (FITS_DB *db, int VERBOSE) {
+
+  int Nx, Ny, Naxis;
+  int mode, status;
+
+  /* database name must be set first */
+  if (db == NULL) {
+    fprintf (stderr, "db handle is not set\n");
+    return (FALSE);
+  }
+
+  /* init & load in FITS table data - return FALSE on error */
+  if (!fits_fread_header (db[0].f, &db[0].header)) {
+    fprintf (stderr, "can't read primary header\n"); 
+    return (FALSE);
+  }
+
+  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 (mode == FITS) {
+    status = dvo_image_load_fits (db, VERBOSE);
+  } else {
+    status = dvo_image_load_rtext (db, VERBOSE);
+  }
+  return (TRUE);
+}
+
+/* the old Image.dat files used a fake FITS header defining a finite data block
+ * this function reads in the data portion, assuming header already loaded
+ */
+
+int dvo_image_load_text (FITS_DB *db, int VERBOSE) {
+
+  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 (db[0].filename, &filestatus) == -1) {
+    if (VERBOSE) fprintf (stderr, "ERROR: failed to get status of image catalog\n");
+    exit (1);
+  }
+  size = Nimage*sizeof(Image) + db[0].header.size;
+  if (size != filestatus.st_size) {
+    int Ndata;
+
+    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);
+    if (!FORCE_READ) exit (1);
+    Nimage = Ndata;
+  } 
+
+  /* create a dummy set of table information */
+  /* (original table has NAXIS = 2, change to 0) */
+  fits_modify (&db[0].header, "NAXIS", "%d", 1, 0);
+  fits_create_matrix (&db[0].header, &db[0].matrix);
+  fits_table_mkheader_Image (&db[0].theader);
+  db[0].ftable.header = &db[0].theader;
+
+  /* alloc, read images */
+  ALLOCATE (image, Image, MAX (Nimage, 1));
+  nimage = fread (image, sizeof(Image), Nimage, db[0].f);
+  if (nimage != Nimage) {
+    if (VERBOSE) fprintf (stderr, "ERROR: problem loading image catalog\n");
+    exit (1);
+  } 
+  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);
+}
+
+/* load the rest of the db table into memory (first extension only) */
+int rfits (FITS_DB *db, int VERBOSE) {
+
+  /* database name must be set first */
+  if (db == NULL) {
+    if (VERBOSE) fprintf (stderr, "db handle is not set\n");
+    return (FALSE);
+  }
+  if (!fits_fread_matrix (db[0].f, &db[0].matrix, &db[0].header)) {
+    if (VERBOSE) fprintf (stderr, "can't read primary matrix");
+    return (FALSE);
+  }
+  if (!fits_fread_header (db[0].f, &db[0].theader)) {
+    if (VERBOSE) fprintf (stderr, "can't read table header");
+    return (FALSE);
+  }
+  if (!fits_fread_ftable_data (db[0].f, &db[0].ftable)) {
+    if (VERBOSE) fprintf (stderr, "can't read table data");
+    return (FALSE);
+  }
+  return (TRUE);
+}
