Index: /trunk/Ohana/src/libdvo/src/dvo_catalog_mef.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog_mef.c	(revision 12390)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog_mef.c	(revision 12391)
@@ -170,31 +170,4 @@
   }
 
-  /* make sure header is consistent with data */
-  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
-  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure);
-  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
-  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, catalog[0].Nsecfilt);
-  gfits_modify (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
-
-  f = catalog[0].f;
-  /* rewind file pointers and truncate */
-  fseek (f, 0, SEEK_SET);
-  ftruncate (fileno (catalog[0].f), 0);
-  ftable.header = &header;
-
-  /* write table PHU header */
-  if (!gfits_fwrite_header  (catalog[0].f, &catalog[0].header)) {
-    fprintf (stderr, "can't write primary header");
-    return (FALSE);
-  }
-
-  /* this is probably a NOP, do I have to keep it in? */
-  gfits_create_matrix (&catalog[0].header, &matrix);
-  if (!gfits_fwrite_matrix  (catalog[0].f, &matrix)) {
-    fprintf (stderr, "can't write primary matrix");
-    return (FALSE);
-  }
-  gfits_free_matrix (&matrix);
-
   /* for the appropriate types, pull out the first secfilt and pass to AverageToFtable as primary */
   if ((catalog[0].catformat == DVO_FORMAT_ELIXIR) || // special case for ELIXIR 
@@ -218,13 +191,40 @@
   }
 
+  /* make sure header is consistent with data */
+  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
+  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure);
+  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
+  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, Nsecfilt);
+  gfits_modify (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
+
+  f = catalog[0].f;
+  /* rewind file pointers and truncate */
+  fseek (f, 0, SEEK_SET);
+  ftruncate (fileno (catalog[0].f), 0);
+  ftable.header = &header;
+
+  /* write table PHU header */
+  if (!gfits_fwrite_header  (catalog[0].f, &catalog[0].header)) {
+    fprintf (stderr, "can't write primary header");
+    goto failure;
+  }
+
+  /* this is probably a NOP, do I have to keep it in? */
+  gfits_create_matrix (&catalog[0].header, &matrix);
+  if (!gfits_fwrite_matrix  (catalog[0].f, &matrix)) {
+    fprintf (stderr, "can't write primary matrix");
+    goto failure;
+  }
+  gfits_free_matrix (&matrix);
+
   /* write out Average table (convert to FITS table format) */
   AverageToFtable (&ftable, catalog[0].average, catalog[0].Naverage, catalog[0].catformat, primary);
   if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
     fprintf (stderr, "can't write table header");
-    return (FALSE);
+    goto failure;
   }
   if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
     fprintf (stderr, "can't write table data");
-    return (FALSE);
+    goto failure;
   }
   gfits_free_table (&ftable);
@@ -235,9 +235,9 @@
   if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
     fprintf (stderr, "can't write table header");
-    return (FALSE);
+    goto failure;
   }
   if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
     fprintf (stderr, "can't write table data");
-    return (FALSE);
+    goto failure;
   }
   gfits_free_table (&ftable);
@@ -248,9 +248,9 @@
   if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
     fprintf (stderr, "can't write table header");
-    return (FALSE);
+    goto failure;
   }
   if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
     fprintf (stderr, "can't write table data");
-    return (FALSE);
+    goto failure;
   }
   gfits_free_table (&ftable);
@@ -262,9 +262,9 @@
   if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
     fprintf (stderr, "can't write table header");
-    return (FALSE);
+    goto failure;
   }
   if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
     fprintf (stderr, "can't write table data");
-    return (FALSE);
+    goto failure;
   }
   gfits_free_table (&ftable);
@@ -276,6 +276,13 @@
     free (secfilt);
   }
-
   return (TRUE);
+
+failure:
+  /* free temp storage */
+  if (primary != NULL) {
+    free (primary);
+    free (secfilt);
+  }
+  return (FALSE);
 }
 
Index: /trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c	(revision 12390)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c	(revision 12391)
@@ -230,30 +230,4 @@
   }
 
-  /* make sure header is consistent with data */
-  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
-  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure);
-  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
-  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, catalog[0].Nsecfilt);
-
-  /* specify the appropriate data format */
-  if (catalog[0].catformat == DVO_FORMAT_INTERNAL)  gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "INTERNAL");
-  if (catalog[0].catformat == DVO_FORMAT_LONEOS)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "LONEOS");
-  if (catalog[0].catformat == DVO_FORMAT_ELIXIR)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "ELIXIR");
-  if (catalog[0].catformat == DVO_FORMAT_PANSTARRS_DEV_0) gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PANSTARRS_DEV_0");
-  // if (catalog[0].catformat == DVO_FORMAT_PANSTARRS) gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PANSTARRS");
-  // if (catalog[0].catformat == DVO_FORMAT_PMTEST)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PMTEST");
-
-  /* rewind file pointers and truncate file */
-  f = catalog[0].f;
-  fseek (f, 0, SEEK_SET);
-  ftruncate (fileno (catalog[0].f), 0);
-
-  /* write header data (use gfits_write_header?) */
-  nitems = fwrite (catalog[0].header.buffer, 1, catalog[0].header.size, f);
-  if (nitems != catalog[0].header.size) {
-    if (VERBOSE) fprintf (stderr, "failed to write header\n");
-    return (FALSE);
-  }
-
   /* for the appropriate types, pull out the first secfilt and pass to WriteRawAverage as primary */
   if ((catalog[0].catformat == DVO_FORMAT_ELIXIR) || // special case for ELIXIR
@@ -277,4 +251,30 @@
   }
 
+  /* make sure header is consistent with data */
+  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
+  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure);
+  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
+  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, catalog[0].Nsecfilt);
+
+  /* specify the appropriate data format */
+  if (catalog[0].catformat == DVO_FORMAT_INTERNAL)  gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "INTERNAL");
+  if (catalog[0].catformat == DVO_FORMAT_LONEOS)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "LONEOS");
+  if (catalog[0].catformat == DVO_FORMAT_ELIXIR)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "ELIXIR");
+  if (catalog[0].catformat == DVO_FORMAT_PANSTARRS_DEV_0) gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PANSTARRS_DEV_0");
+  // if (catalog[0].catformat == DVO_FORMAT_PANSTARRS) gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PANSTARRS");
+  // if (catalog[0].catformat == DVO_FORMAT_PMTEST)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PMTEST");
+
+  /* rewind file pointers and truncate file */
+  f = catalog[0].f;
+  fseek (f, 0, SEEK_SET);
+  ftruncate (fileno (catalog[0].f), 0);
+
+  /* write header data (use gfits_write_header?) */
+  nitems = fwrite (catalog[0].header.buffer, 1, catalog[0].header.size, f);
+  if (nitems != catalog[0].header.size) {
+    if (VERBOSE) fprintf (stderr, "failed to write header\n");
+    goto failure;
+  }
+
   /* write averages and measures */
   WriteRawAverage (f, catalog[0].average, catalog[0].Naverage, catalog[0].catformat, primary);
@@ -287,5 +287,5 @@
   if (nitems != Nitems) {
     if (VERBOSE) fprintf (stderr, "failed to write catalog file missing %s\n", catalog[0].filename);
-    return (FALSE);
+    goto failure;
   }
 
@@ -298,6 +298,13 @@
     free (secfilt);
   }
-
   return (TRUE);
+
+failure:
+  /* free temp storage */
+  if (primary != NULL) {
+    free (primary);
+    free (secfilt);
+  }
+  return (FALSE);
 }
 
Index: /trunk/Ohana/src/libdvo/src/dvo_catalog_split.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 12390)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 12391)
@@ -298,23 +298,4 @@
   }
 
-  /* make sure header is consistent with data */
-  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
-  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure);
-  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
-  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, catalog[0].Nsecfilt);
-  gfits_modify (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
-
-  /* rewind file pointers and truncate (file is still open) */
-  fseek (catalog[0].f, 0, SEEK_SET);
-
-  /* write table PHU header - always write this out */
-  /* XXX EAM : check if disk file size has changed */
-  if (!gfits_fwrite_header  (catalog[0].f, &catalog[0].header)) {
-    fprintf (stderr, "can't write primary header");
-    return (FALSE);
-  }
-
-  /* in split mode, we can save only part of the data */ 
-
   /* for the appropriate types, pull out the first secfilt and pass to AverageToFtable as primary */
   if ((catalog[0].catformat == DVO_FORMAT_ELIXIR) || // special case for ELIXIR
@@ -344,4 +325,23 @@
   }
 
+  /* make sure header is consistent with data */
+  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
+  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure);
+  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
+  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, Nsecfilt);
+  gfits_modify (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
+
+  /* rewind file pointers and truncate (file is still open) */
+  fseek (catalog[0].f, 0, SEEK_SET);
+
+  /* write table PHU header - always write this out */
+  /* XXX EAM : check if disk file size has changed */
+  if (!gfits_fwrite_header  (catalog[0].f, &catalog[0].header)) {
+    fprintf (stderr, "can't write primary header");
+    goto failure;
+  }
+
+  /* in split mode, we can save only part of the data */ 
+
   /*** Average Table ***/
 
@@ -353,5 +353,5 @@
     if (!gfits_fwrite_matrix  (catalog[0].f, &matrix)) {
       fprintf (stderr, "can't write primary matrix");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_matrix (&matrix);
@@ -361,9 +361,9 @@
     if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
       fprintf (stderr, "can't write table header");
-      return (FALSE);
+      goto failure;
     }
     if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
       fprintf (stderr, "can't write table data");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_table (&ftable);
@@ -390,5 +390,5 @@
     if (!gfits_fwrite_header  (catfile[0].f, &catfile[0].header)) {
       fprintf (stderr, "can't write primary header");
-      return (FALSE);
+      goto failure;
     }
 
@@ -397,5 +397,5 @@
     if (!gfits_fwrite_matrix  (catfile[0].f, &matrix)) {
       fprintf (stderr, "can't write primary matrix");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_matrix (&matrix);
@@ -405,9 +405,9 @@
     if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
-      return (FALSE);
+      goto failure;
     }
     if (!gfits_fwrite_table (catfile[0].f, &ftable)) {
       fprintf (stderr, "can't write table data");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_table (&ftable);
@@ -428,5 +428,5 @@
     if (!gfits_fwrite_header  (catfile[0].f, &catfile[0].header)) {
       fprintf (stderr, "can't write primary header");
-      return (FALSE);
+      goto failure;
     }
 
@@ -435,5 +435,5 @@
     if (!gfits_fwrite_matrix  (catfile[0].f, &matrix)) {
       fprintf (stderr, "can't write primary matrix");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_matrix (&matrix);
@@ -443,9 +443,9 @@
     if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
-      return (FALSE);
+      goto failure;
     }
     if (!gfits_fwrite_table (catfile[0].f, &ftable)) {
       fprintf (stderr, "can't write table data");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_table (&ftable);
@@ -466,5 +466,5 @@
     if (!gfits_fwrite_header  (catfile[0].f, &catfile[0].header)) {
       fprintf (stderr, "can't write primary header");
-      return (FALSE);
+      goto failure;
     }
 
@@ -473,5 +473,5 @@
     if (!gfits_fwrite_matrix  (catfile[0].f, &matrix)) {
       fprintf (stderr, "can't write primary matrix");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_matrix (&matrix);
@@ -482,9 +482,9 @@
     if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
-      return (FALSE);
+      goto failure;
     }
     if (!gfits_fwrite_table (catfile[0].f, &ftable)) {
       fprintf (stderr, "can't write table data");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_table (&ftable);
@@ -499,4 +499,12 @@
 
   return (TRUE);
+
+failure:
+  /* free temp storage */
+  if (primary != NULL) {
+    free (primary);
+    free (secfilt);
+  }
+  return (FALSE);
 }
 
@@ -524,21 +532,4 @@
     if (VERBOSE) fprintf (stderr, "no stars in catalog, skipping\n");
     return (TRUE);
-  }
-
-  /* make sure header is consistent with data */
-  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
-  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure + catalog[0].Nmeas_off);
-  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
-  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, catalog[0].Nsecfilt);
-  gfits_modify (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
-
-  /* rewind file pointers and truncate (file is still open) */
-  fseek (catalog[0].f, 0, SEEK_SET);
-
-  /* write table PHU header - always write this out */
-  /* XXX EAM : check if disk file size has changed */
-  if (!gfits_fwrite_header  (catalog[0].f, &catalog[0].header)) {
-    fprintf (stderr, "can't write primary header");
-    return (FALSE);
   }
 
@@ -570,4 +561,21 @@
   }
 
+  /* make sure header is consistent with data */
+  gfits_modify (&catalog[0].header, "NSTARS",   "%d", 1, catalog[0].Naverage);
+  gfits_modify (&catalog[0].header, "NMEAS",    "%d", 1, catalog[0].Nmeasure + catalog[0].Nmeas_off);
+  gfits_modify (&catalog[0].header, "NMISS",    "%d", 1, catalog[0].Nmissing);
+  gfits_modify (&catalog[0].header, "NSECFILT", "%d", 1, Nsecfilt);
+  gfits_modify (&catalog[0].header, "EXTEND",   "%t", 1, TRUE);
+
+  /* rewind file pointers and truncate (file is still open) */
+  fseek (catalog[0].f, 0, SEEK_SET);
+
+  /* write table PHU header - always write this out */
+  /* XXX EAM : check if disk file size has changed */
+  if (!gfits_fwrite_header  (catalog[0].f, &catalog[0].header)) {
+    fprintf (stderr, "can't write primary header");
+    goto failure;
+  }
+
   /*** Average Table ***/
   if (catalog[0].average != NULL) {
@@ -587,9 +595,9 @@
     if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
       fprintf (stderr, "can't write table header");
-      return (FALSE);
+      goto failure;
     }
     if (!gfits_fwrite_vtable (catalog[0].f, &vtable)) {
       fprintf (stderr, "can't write table data");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_vtable (&vtable);
@@ -637,9 +645,9 @@
     if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
-      return (FALSE);
+      goto failure;
     }
     if (!gfits_fwrite_vtable (catfile[0].f, &vtable)) {
       fprintf (stderr, "can't write table data");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_vtable (&vtable);
@@ -663,5 +671,5 @@
     if (!gfits_fwrite_header  (catfile[0].f, &catfile[0].header)) {
       fprintf (stderr, "can't write primary header");
-      return (FALSE);
+      goto failure;
     }
 
@@ -670,5 +678,5 @@
     if (!gfits_fwrite_matrix  (catfile[0].f, &matrix)) {
       fprintf (stderr, "can't write primary matrix");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_matrix (&matrix);
@@ -678,9 +686,9 @@
     if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
-      return (FALSE);
+      goto failure;
     }
     if (!gfits_fwrite_table (catfile[0].f, &ftable)) {
       fprintf (stderr, "can't write table data");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_table (&ftable);
@@ -709,9 +717,9 @@
     if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
-      return (FALSE);
+      goto failure;
     }
     if (!gfits_fwrite_vtable (catfile[0].f, &vtable)) {
       fprintf (stderr, "can't write table data");
-      return (FALSE);
+      goto failure;
     }
     gfits_free_vtable (&vtable);
@@ -727,4 +735,12 @@
 
   return (TRUE);
+
+failure:
+  /* free temp storage */
+  if (primary != NULL) {
+    free (primary);
+    free (secfilt);
+  }
+  return (FALSE);
 }
 
