Index: trunk/Ohana/src/libfits/table/F_read_T.c
===================================================================
--- trunk/Ohana/src/libfits/table/F_read_T.c	(revision 39996)
+++ trunk/Ohana/src/libfits/table/F_read_T.c	(revision 39997)
@@ -1,4 +1,8 @@
 # include <ohana.h>
 # include <gfitsio.h>
+
+# define GFITS_FREAD_COMPLETE_READ 0
+# define GFITS_FREAD_INCOMPLETE_FILE 1
+# define GFITS_FREAD_INCOMPLETE_READ 2
 
 /*********************** fits read table ***********************************/
@@ -49,7 +53,41 @@
 }
 
+// retry several times to read the file, if we hit the EOF, give up, otherwise try again
+// sleep for 500,000 nanoseconds between attempts (or longer?)
+off_t gfits_fread_retry (int *status, char *buffer, off_t Nbytes, FILE *f, int Nretry) {
+  
+  off_t Nread = 0;
+
+  for (int i = 0; (i < Nretry) && (Nread < Nbytes); i++) {
+    off_t Nbyte_left = Nbytes - Nread;
+    off_t Nread_pass = fread (&buffer[Nread], sizeof (char), Nbyte_left, f);
+    
+    Nread += Nread_pass;
+    if (Nread != Nbytes) {
+      if (feof(f)) {
+	*status = GFITS_FREAD_INCOMPLETE_FILE;
+	return Nread;
+      }
+      sleep (2);
+      if (i < Nretry - 1) fprintf (stderr, "incomplete file read, retrying\n");
+    }
+  }
+
+  if (Nread != Nbytes) {
+    if (feof(f)) {
+      *status = GFITS_FREAD_INCOMPLETE_FILE;
+    } else {
+      *status = GFITS_FREAD_INCOMPLETE_READ;
+    }
+  } else {
+    *status = GFITS_FREAD_COMPLETE_READ;
+  }
+  return Nread;
+}
+
 /*********************** fits read ftable data ***********************************/
 int gfits_fread_ftable_data (FILE *f, FTable *table, int padIfShort) {
 
+  int status;
   off_t Nbytes, Nread;
 
@@ -58,16 +96,7 @@
   ALLOCATE (table[0].buffer, char, Nbytes);
 
-  Nread = fread (table[0].buffer, sizeof (char), Nbytes, f);
-  if (Nread != Nbytes) {
-    if (feof(f)) {
-      if (!gfits_fread_incomplete_case(f, table, padIfShort, Nread, Nbytes)) return FALSE;
-    } else {
-      off_t Nextra = Nbytes - Nread;
-      off_t Nxread = fread (&table[0].buffer[Nread], sizeof (char), Nextra, f);
-      Nread += Nxread;
-      if (Nread != Nbytes) {
-	if (!gfits_fread_incomplete_case(f, table, padIfShort, Nread, Nbytes)) return FALSE;
-      }
-    }
+  Nread = gfits_fread_retry (&status, table[0].buffer, Nbytes, f, 5);
+  if (status != GFITS_FREAD_COMPLETE_READ) {
+    if (!gfits_fread_incomplete_case(f, table, padIfShort, Nread, Nbytes)) return FALSE;
   }
   table[0].validsize = Nread;
@@ -108,18 +137,8 @@
   }
 
-  Nread = fread (table[0].buffer, sizeof (char), Nbytes, f);
-  if (Nread != Nbytes) {
-    if (ferror (f)) {
-      perror ("fits read error");
-      free (table[0].buffer);
-      return (FALSE);
-    } 
-    fprintf (stderr, "error: fits read error in %s, read "OFF_T_FMT", need "OFF_T_FMT"\n", __func__,  Nread,  Nbytes);
-    if (!padIfShort) {
-      free (table[0].buffer);
-      return (FALSE);
-    }
-    memset (&table[0].buffer[Nread], 0, Nbytes - Nread);
-    fprintf (stderr, "warning: file missing data, padding with zeros: USE AT YOUR OWN RISK!\n");
+  int status;
+  Nread = gfits_fread_retry (&status, table[0].buffer, Nbytes, f, 5);
+  if (status != GFITS_FREAD_COMPLETE_READ) {
+    if (!gfits_fread_incomplete_case(f, table, padIfShort, Nread, Nbytes)) return FALSE;
   }
 
@@ -157,7 +176,19 @@
   Nread = fread (buffer, sizeof (char), Nbytes, f);
   if (Nread != Nbytes) {
-    perror ("fits read error");
-    free (buffer);
-    return (FALSE);
+    if (feof(f)) {
+      perror ("fits read error");
+      free (buffer);
+      return (FALSE);
+    } else {
+      // try a second time
+      off_t Nextra = Nbytes - Nread;
+      off_t Nxread = fread (&buffer[Nread], sizeof (char), Nextra, f);
+      Nread += Nxread;
+      if (Nread != Nbytes) {
+	perror ("fits read error");
+	free (buffer);
+	return (FALSE);
+      }
+    }
   }
 
@@ -220,6 +251,17 @@
       Nread = fread (table[0].buffer[i], sizeof (char), Nx, f);
       if (Nread != Nx) { 
-	perror ("fits read error");
-	return (FALSE); 
+	if (feof(f)) {
+	  perror ("fits read error");
+	  return (FALSE);
+	} else {
+	  // try a second time
+	  off_t Nextra = Nx - Nread;
+	  off_t Nxread = fread (&table[0].buffer[i][Nread], sizeof (char), Nextra, f);
+	  Nread += Nxread;
+	  if (Nread != Nx) {
+	    perror ("fits read error");
+	    return (FALSE);
+	  }
+	}
       }
     }
