Index: /branches/eam_branches/ipp-20150625/Ohana/src/libfits/include/gfitsio.h
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/libfits/include/gfitsio.h	(revision 38965)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/libfits/include/gfitsio.h	(revision 38966)
@@ -237,5 +237,5 @@
 int   	gfits_fread_ftable             PROTO((FILE *f, FTable *ftable, char *extname)); 
 int   	gfits_fread_ftable_data        PROTO((FILE *f, FTable *ftable, int padIfShort));
-int   	gfits_fread_ftable_range       PROTO((FILE *f, int padIfShort, FTable *ftable, off_t start, off_t Nrows));
+int   	gfits_fread_ftable_range       PROTO((FILE *f, int padIfShort, int noSeek, FTable *ftable, off_t start, off_t Nrows));
 int   	gfits_fread_vtable             PROTO((FILE *f, VTable *vtable, char *extname, off_t Nrow, off_t *row));
 int   	gfits_fread_vtable_range       PROTO((FILE *f, VTable *vtable, off_t start, off_t Nrows));
Index: /branches/eam_branches/ipp-20150625/Ohana/src/libfits/table/F_read_T.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/libfits/table/F_read_T.c	(revision 38965)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/libfits/table/F_read_T.c	(revision 38966)
@@ -62,5 +62,5 @@
 
 /*********************** fits read ftable data ***********************************/
-int gfits_fread_ftable_range (FILE *f, int padIfShort, FTable *table, off_t start, off_t Nrows) {
+int gfits_fread_ftable_range (FILE *f, int padIfShort, int noSeek, FTable *table, off_t start, off_t Nrows) {
 
   off_t Nbytes, Nread, Nskip, Nx, Ny;
@@ -88,5 +88,7 @@
   }
 
-  fseeko (f, Nskip, SEEK_CUR);
+  if (!noSeek) {
+    fseeko (f, Nskip, SEEK_CUR);
+  }
 
   Nread = fread (table[0].buffer, sizeof (char), Nbytes, f);
Index: /branches/eam_branches/ipp-20150625/Ohana/src/tools/src/fits_to_mysql.c
===================================================================
--- /branches/eam_branches/ipp-20150625/Ohana/src/tools/src/fits_to_mysql.c	(revision 38966)
+++ /branches/eam_branches/ipp-20150625/Ohana/src/tools/src/fits_to_mysql.c	(revision 38966)
@@ -0,0 +1,535 @@
+# include <ohana.h>
+# include <gfitsio.h>
+# include "inttypes.h"
+
+# define NMAX    0x100
+# define NBUFFER 0x100000
+# define NROWS 10000
+
+# define TO_BUFFER 1
+
+char        *DATABASE_HOST;
+char        *DATABASE_USER;
+char        *DATABASE_PASS;
+char        *DATABASE_NAME;
+
+int args (int *argc, char **argv);
+void usage();
+
+void print_data (FTable *table);
+void *gfits_get_bintable_column_data_by_Ncol (FTable *table, int Nfield, char *type, off_t *Nrow, int *Ncol, char nativeOrder);
+void FFPRINT (char *buffer, int *nbuffer, double value);
+
+int main (int argc, char **argv) {
+
+  args (&argc, argv);
+  if (argc != 2) usage ();
+  char *input = argv[1];
+
+  FTable table;
+  Header header;
+  table.header = &header;
+  table.buffer = NULL;
+
+  FILE *f = fopen (input, "r");
+  if (f == (FILE *) NULL) {
+    fprintf (stderr, "can't open file %s\n", input);
+    exit (1);
+  }
+
+  int Nsection = 0;
+  while (gfits_fread_header (f, &header)) {
+
+    /* extract the EXTNAME for this component (set to PHU for 0th component) */
+    char extname[80];
+    int status = gfits_scan (&header, "EXTNAME", "%s", 1, extname);
+    if (!status) {
+      if (Nsection == 0) {
+	strcpy (extname, "PHU");
+      } else {
+	strcpy (extname, "UNKNOWN");
+      }
+    }
+    fprintf (stdout, "%-30s ", extname);
+
+    /* extract the datatype for this component (IMAGE for 0th component) */
+    char exttype[80];
+    if (Nsection == 0) {
+      strcpy (exttype, "IMAGE");
+    } else {
+      status = gfits_scan (&header, "XTENSION", "%s", 1, exttype);
+      if (!status) {
+	strcpy (exttype, "UNKNOWN");
+      }
+    }
+    fprintf (stdout, "%-15s ", exttype);
+
+    fprintf (stdout, " %4d", header.Naxes);
+
+    /* extract the individual axes */
+    int i;
+    for (i = 0; i < header.Naxes; i++) {
+      fprintf (stdout, " "OFF_T_FMT, header.Naxis[i]);
+    }
+    fprintf (stdout, "\n");
+
+    if (strcmp(exttype, "BINTABLE")) {
+      off_t Nbytes = gfits_data_size (&header);
+      gfits_free_header (&header);
+      fseeko (f, Nbytes, SEEK_CUR);
+      Nsection ++;
+      continue;
+    }
+
+    IOBuffer insert;
+    InitIOBuffer (&insert, 1024);
+
+    create_table (&header. extname, &insert);
+
+    // need to reset this on each pass?
+    myAssert (!table.buffer, "oops");
+
+    // total number of bytes in the data section (including pad at the end)
+    off_t Nbytes = gfits_data_size (&header);
+    
+    off_t Ntotal = header.Naxis[1];
+    int Nrows = NROWS;
+    int start = 0;
+    while (start < Ntotal) {
+      if (!gfits_fread_ftable_range (f, FALSE, TRUE, &table, start, Nrows)) {
+	fprintf (stderr, "error reading table for extension %d\n", Nsection);
+	exit (1);
+      }
+      fprintf (stderr, "read "OFF_T_FMT" rows\n", header.Naxis[1]);
+
+      write_data (&table, &insert);
+
+      start += Nrows;
+      Nbytes -= header.Naxis[0]*header.Naxis[1]*abs(header.bitpix / 8);
+      header.Naxis[1] = Ntotal;
+    }
+
+    gfits_free_table (&table);
+    gfits_free_header (&header);
+
+    // move the pointer to the end of this data segment
+    fseeko (f, Nbytes, SEEK_CUR);
+    Nsection ++;
+
+    FreeIOBuffer (&insert);
+  }
+  gfits_free_header (&header);
+
+  fclose (f);
+
+  ohana_memdump (TRUE);
+
+  exit (0);
+}
+
+// the 'insert' buffer will be used to generate the insert lines downstream (call InitIOBuffer on it first)
+int create_tables (Header *header, char *extname, IOBuffer *insert) {
+
+  int i, j, Nf;
+
+  // print out the columns (need to swap first)
+
+  int Nfields;
+  if (!gfits_scan (header, "TFIELDS", "%d", 1, &Nfields)) return FALSE;
+
+  IOBuffer buffer;
+  InitIOBuffer (&buffer, 1024);
+
+  // drop the table if it exists
+  PrintIOBuffer (&buffer, "DROP TABLE if exists %s\n", extname);
+  status = mysql_query(mysql, buffer.buffer); 
+  if (status) {
+    fprintf (stderr, "failed to drop table:\n");
+    fprintf (stderr, "%s\n", mysql_error(mysql));
+    return FALSE;
+  }
+  buffer.Nbuffer = 0;
+  bzero (buffer.buffer, buffer.Nalloc);
+
+  // Only send the necessary fields (eg, do not sent parallax and pm)
+  PrintIOBuffer (&buffer, "CREATE TABLE %s (\n", extname);
+  PrintIOBuffer (&buffer, "INSERT INTO %s (\n", extname);
+
+  // first, extract the table data:
+  for (i = 0; i < Nfields; i++) {
+
+    snprintf (field, 256, "TTYPE%d", i);
+    gfits_scan (header, field, "%s", 1, tlabel);
+
+    snprintf (field, 256, "TFORM%d", N);
+    gfits_scan (header, field, "%s", 1, format);
+
+    char type[16];
+    if (!gfits_bintable_format (format, type, &Nval, &Nbytes)) return (NULL);
+
+    if (!strcmp (type, "byte"))    {
+      PrintIOBuffer (&buffer, "%s TINYINT", tlabel);
+    }
+    if (!strcmp (type, "short"))   {
+      PrintIOBuffer (&buffer, "%s SMALLINT", tlabel);
+    }
+    if (!strcmp (type, "int"))     {
+      PrintIOBuffer (&buffer, "%s INT", tlabel);
+    }
+    if (!strcmp (type, "int64_t")) {
+      PrintIOBuffer (&buffer, "%s BITINT", tlabel);
+    }
+    if (!strcmp (type, "float"))   {
+      PrintIOBuffer (&buffer, "%s FLOAT", tlabel);
+    }
+    if (!strcmp (type, "double"))  {
+      PrintIOBuffer (&buffer, "%s DOUBLE", tlabel);
+    }
+    if (!strcmp (type, "char"))    {
+      PrintIOBuffer (&buffer, "%s VARCHAR(%d)", tlabel, Nval);
+    }
+
+    PrintIOBuffer (insert, "%s", tlabel);
+    if (i == Nfields - 1)  {
+      PrintIOBuffer (&buffer, ")\n");
+      PrintIOBuffer (insert, ") VALUES \n");
+    } else {
+      PrintIOBuffer (&buffer, ",\n");
+      PrintIOBuffer (insert, ",\n");
+    }
+  }
+
+  if (DEBUG) fprintf (stderr, "%s\n", buffer.buffer);
+  status = mysql_query(mysql, buffer.buffer); 
+  if (status) {
+    fprintf (stderr, "failed to create table:\n");
+    fprintf (stderr, "%s\n", mysql_error(mysql));
+    fprintf (stderr, "Nbuffer: %d\n", buffer.Nbuffer);
+  }
+
+  FreeIOBuffer (&buffer);
+  return TRUE;
+}
+
+typedef enum {
+  TYPE_NONE,  
+  TYPE_CHAR,  
+  TYPE_BYTE,  
+  TYPE_SHORT, 
+  TYPE_INT,   
+  TYPE_INT64, 
+  TYPE_FLOAT, 
+  TYPE_DOUBLE
+} TypeInt;
+
+void write_data (FTable *table, IOBuffer *insert) {
+
+  int i, j, Nf;
+
+  // copy the 'insert' IOBuffer to the output buffer
+  IOBuffer output;
+  InitIOBuffer (&output, 1024);
+  IOBufferCopy (&output, &insert);
+
+  // print out the columns (need to swap first)
+
+  Header *header = table->header;
+  int Ny = table->header->Naxis[1];
+
+  int Nfields;
+  if (!gfits_scan (header, "TFIELDS", "%d", 1, &Nfields)) return;
+
+  ALLOCATE_PTR (Nrow, off_t,  Nfields);
+  ALLOCATE_PTR (Ncol, int,    Nfields);
+  ALLOCATE_PTR (data, void *, Nfields);
+  ALLOCATE_PTR (type, char *, Nfields);
+  ALLOCATE_PTR (Type, int,    Nfields);
+
+  // first, extract the table data: (probably should save this structure and re-use in create_table)
+  for (i = 0; i < Nfields; i++) {
+    ALLOCATE (type[i], char, 16);
+    data[i] = gfits_get_bintable_column_data_by_Ncol (table, i + 1, type[i], &Nrow[i], &Ncol[i], FALSE);
+    myAssert (Nrow[i] == Ny, "oops");
+    Type[i] = TYPE_NONE;
+    if (!strcmp (type[i], "char"))    Type[i] = TYPE_CHAR;
+    if (!strcmp (type[i], "byte"))    Type[i] = TYPE_BYTE;
+    if (!strcmp (type[i], "short"))   Type[i] = TYPE_SHORT;
+    if (!strcmp (type[i], "int"))     Type[i] = TYPE_INT;
+    if (!strcmp (type[i], "int64_t")) Type[i] = TYPE_INT64;
+    if (!strcmp (type[i], "float"))   Type[i] = TYPE_FLOAT;
+    if (!strcmp (type[i], "double"))  Type[i] = TYPE_DOUBLE;
+    myAssert (Type[i] != TYPE_NONE, "oops");
+  }
+
+  // convert the type characters to an enum for speed here
+  // could also generate pointers of all types to the data;
+
+  for (i = 0; i < Ny; i++) {
+    PrintIOBuffer (output, "( ");
+    for (Nf = 0; Nf < Nfields; Nf++) {
+      switch (Type[Nf]) {
+	case TYPE_CHAR: {
+	  char line[1024];
+	  char *value = data[Nf];
+	  memset (line, 0, 128);
+	  memcpy (line, &value[i*Ncol[Nf]], Ncol[Nf]);
+	  PrintIOBuffer (output, "%s", line);
+	  break;
+	} 
+	case TYPE_BYTE: {
+	  char *value = data[Nf];
+	  for (j = 0; j < Ncol[Nf]; j++) {
+	    PrintIOBuffer (output, "%hhd", value[i*Ncol[Nf] + j]);
+	  }
+	  break;
+	}
+	case TYPE_SHORT: {
+	  short *value = data[Nf];
+	  for (j = 0; j < Ncol[Nf]; j++) {
+	    PrintIOBuffer (output, "%hd", value[i*Ncol[Nf] + j]);
+	  }
+	  break;
+	}
+	case TYPE_INT: {
+	  int *value = data[Nf];
+	  for (j = 0; j < Ncol[Nf]; j++) {
+	    PrintIOBuffer (output, "%d", value[i*Ncol[Nf] + j]);
+	  }
+	  break;
+	}
+	case TYPE_INT64: {
+	  int64_t *value = data[Nf];
+	  for (j = 0; j < Ncol[Nf]; j++) {
+	    PrintIOBuffer (output, "%lld", (long long int) value[i*Ncol[Nf] + j]);
+	  }
+	  break;
+	}
+	case TYPE_FLOAT: {
+	  float *value = data[Nf];
+	  for (j = 0; j < Ncol[Nf]; j++) {
+	    PrintIOBuffer (output, "%e", value[i*Ncol[Nf] + j]);
+	  }
+	  break;
+	}
+	case TYPE_DOUBLE: {
+	  double *value = data[Nf];
+	  for (j = 0; j < Ncol[Nf]; j++) {
+	    PrintIOBuffer (output, "%e", value[i*Ncol[Nf] + j]);
+	  }
+	  break;
+	}
+	default:
+	  myAbort ("impossible");
+      }
+      if (Nf < Nfields - 1) {
+	PrintIOBuffer (output, ",\n");
+      } else {
+	PrintIOBuffer (output, ")\n");
+      }
+      if (output->Nbuffer > MAX_BUFFER) {
+      }
+    }
+  }
+
+  for (i = 0 ; i < Nfields; i++) {
+    free (type[i]);
+    free (data[i]);
+  }
+  free (Nrow);
+  free (Ncol);
+  free (data);
+  free (type);
+  free (Type);
+
+  return;
+}
+
+# define SWAP_BYTE {					\
+    char tmp;						\
+    tmp = Pin[0]; Pin[0] = Pin[1]; Pin[1] = tmp; }
+# define SWAP_WORD {					\
+    char tmp;						\
+    tmp = Pin[0]; Pin[0] = Pin[3]; Pin[3] = tmp;	\
+    tmp = Pin[1]; Pin[1] = Pin[2]; Pin[2] = tmp; }
+# define SWAP_DBLE {					\
+    char tmp;						\
+    tmp = Pin[0]; Pin[0] = Pin[7]; Pin[7] = tmp;	\
+    tmp = Pin[1]; Pin[1] = Pin[6]; Pin[6] = tmp;	\
+    tmp = Pin[2]; Pin[2] = Pin[5]; Pin[5] = tmp;	\
+    tmp = Pin[3]; Pin[3] = Pin[4]; Pin[4] = tmp; }
+
+void *gfits_get_bintable_column_data_by_Ncol (FTable *table, int Nfield, char *type, off_t *Nrow, int *Ncol, char nativeOrder) {
+
+  int i;
+  char tlabel[256], field[256], format[256], tmpline[64];
+  char *Pin, *Pout, *array;
+  double Bscale, Bzero;
+
+  Header *header = table->header;
+
+  int Nfields;
+  if (!gfits_scan (header, "TFIELDS", "%d", 1, &Nfields)) return NULL;
+
+  if (Nfield < 1) return NULL;
+  if (Nfield > Nfields) return NULL;
+
+  Bscale = 1; 
+  Bzero  = 0;
+
+  /* get column keywords */
+  snprintf (field, 256, "TTYPE%d", Nfield);
+  gfits_scan (header, field, "%s", 1, tlabel);
+  snprintf (field, 256, "TSCAL%d", Nfield);
+  gfits_scan (header, field, "%lf", 1, &Bscale);
+  snprintf (field, 256, "TZERO%d", Nfield);
+  gfits_scan (header, field, "%lf", 1, &Bzero);
+  snprintf (field, 256, "TFORM%d", Nfield);
+  gfits_scan (header, field, "%s", 1, format);
+
+  int Nval, Nbytes;
+  if (!gfits_bintable_format (format, type, &Nval, &Nbytes)) return (NULL);
+  
+  /* check existing table dimensions */
+  off_t Nx, Ny;
+  gfits_scan (header, "NAXIS1",  OFF_T_FMT, 1,  &Nx);
+  gfits_scan (header, "NAXIS2",  OFF_T_FMT, 1,  &Ny);
+
+  /* scan columns to find insert point */
+  int Nstart = 0;
+  for (i = 1; i < Nfield; i++) {
+    snprintf (field, 256, "TFORM%d", i);
+    gfits_scan (header, field, "%s", 1, format);
+    int Nv, Nb;
+    gfits_bintable_format (format, tmpline, &Nv, &Nb);
+    Nstart += Nv*Nb;
+  }
+
+  /* extract bytes from table into array */
+  ALLOCATE (array, char, Nbytes*Nval*Ny);
+  Pin  = table[0].buffer + Nstart;
+  Pout = array;
+  for (i = 0; i < Ny; i++, Pin += Nx, Pout += Nval*Nbytes) {
+    memcpy (Pout, Pin, Nval*Nbytes);
+  }
+
+  // NOTE: we have already copied the data to 'array', 
+  Pin  = array;
+  Pout = array;
+  int directCopy = (Bzero == 0.0) && (Bscale == 1.0);
+
+  /* convert data in-situ with correct type, byte swap and Bzero/Bscale */
+  if (!strcmp (type, "char")) {
+    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
+      if (!directCopy) { *(char *)Pout = *(char *)Pin*Bscale + Bzero; }
+    }
+  }
+  if (!strcmp (type, "byte")) {
+    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
+      if (!directCopy) { *(char *)Pout = *(char *)Pin*Bscale + Bzero; }
+    }
+  }
+  if (!strcmp (type, "short")) {
+    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
+# ifdef BYTE_SWAP
+      if (!nativeOrder) { SWAP_BYTE; }
+# endif
+      if (!directCopy) { *(short *)Pout = *(short *)Pin*Bscale + Bzero; }
+    }  
+  }
+  if (!strcmp (type, "int")) {
+    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
+# ifdef BYTE_SWAP
+      if (!nativeOrder) { SWAP_WORD; }
+# endif
+      if (!directCopy) { *(int *)Pout = *(int *)Pin*Bscale + Bzero; }
+    }
+  }
+  if (!strcmp (type, "int64_t")) {
+    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
+# ifdef BYTE_SWAP
+      if (!nativeOrder) { SWAP_DBLE; }
+# endif
+      if (!directCopy) { *(int64_t *)Pout = *(int64_t *)Pin*Bscale + Bzero; }
+    }
+  }
+  if (!strcmp (type, "float")) {
+    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
+# ifdef BYTE_SWAP
+      if (!nativeOrder) { SWAP_WORD; }
+# endif
+      if (!directCopy) { *(float *)Pout = *(float *)Pin*Bscale + Bzero; }
+    }
+  }
+  if (!strcmp (type, "double")) {
+    for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) {
+# ifdef BYTE_SWAP
+      if (!nativeOrder) { SWAP_DBLE; }
+# endif
+      if (!directCopy) { *(double *)Pout = *(double *)Pin*Bscale + Bzero; }
+    }
+  }
+
+  // check that we supplied a valid type
+
+  *Ncol = Nval;
+  *Nrow = Ny;
+  return (array);
+}
+
+int CopyIOBuffer (IOBuffer *output, IOBuffer *input) {
+
+  output[0].Nalloc  = input[0].Nalloc; 
+  output[0].Nreset  = input[0].Nreset; 
+  output[0].Nblock  = input[0].Nblock; 
+  output[0].Nbuffer = input[0].Nbuffer;
+
+  if (output->buffer) {
+    REALLOCATE (output[0].buffer, char, output[0].Nalloc);
+  } else {
+    ALLOCATE (output[0].buffer, char, output[0].Nalloc);
+  }
+
+  memcpy (output->buffer, input->buffer, input->Nbuffer);
+
+  return (TRUE);
+}
+
+int args (int *argc, char **argv) {
+
+  int N;
+
+  if ((N = get_argument (*argc, argv, "-dbhost"))) {
+    remove_argument (N, argc, argv);
+    DATABASE_HOST = strcreate (argv[N]);
+    remove_argument (N, argc, argv);
+  } else usage();
+
+  if ((N = get_argument (*argc, argv, "-dbuser"))) {
+    remove_argument (N, argc, argv);
+    DATABASE_USER = strcreate (argv[N]);
+    remove_argument (N, argc, argv);
+  } else usage();
+
+  if ((N = get_argument (*argc, argv, "-dbpass"))) {
+    remove_argument (N, argc, argv);
+    DATABASE_PASS = strcreate (argv[N]);
+    remove_argument (N, argc, argv);
+  } else usage();
+
+  if ((N = get_argument (*argc, argv, "-dbname"))) {
+    remove_argument (N, argc, argv);
+    DATABASE_NAME = strcreate (argv[N]);
+    remove_argument (N, argc, argv);
+  } else usage();
+
+}
+
+void usage () {
+  fprintf (stderr, "USAGE: fits_to_mysql [dbinfo] (filename.fits)\n");
+  fprintf (stderr, "    mysql database info is supplied with these:\n");
+  fprintf (stderr, "    -dbhost : database host machine\n");
+  fprintf (stderr, "    -dbuser : database username\n");
+  fprintf (stderr, "    -dbpass : database password\n");
+  fprintf (stderr, "    -dbname : database name\n\n");
+  exit (2);
+}
