Index: trunk/Ohana/src/libdvo/include/cmf-ps1-v5-r2-lensing.h
===================================================================
--- trunk/Ohana/src/libdvo/include/cmf-ps1-v5-r2-lensing.h	(revision 38472)
+++ trunk/Ohana/src/libdvo/include/cmf-ps1-v5-r2-lensing.h	(revision 38553)
@@ -95,4 +95,6 @@
 
 CMF_PS1_V5_R2_Lensing *gfits_table_get_CMF_PS1_V5_R2_Lensing (FTable *table, off_t *Ndata, char *swapped);
+CMF_PS1_V5_R2_Lensing *gfits_table_get_CMF_PS1_V5_R2x_Lensing (FTable *ftable, off_t *Ndata, char *swapped);
+
 int      gfits_table_set_CMF_PS1_V5_R2_Lensing (FTable *ftable, CMF_PS1_V5_R2_Lensing *data, off_t Ndata);
 int      gfits_table_mkheader_CMF_PS1_V5_R2_Lensing (Header *header);
Index: trunk/Ohana/src/libdvo/include/convert.h
===================================================================
--- trunk/Ohana/src/libdvo/include/convert.h	(revision 38472)
+++ trunk/Ohana/src/libdvo/include/convert.h	(revision 38553)
@@ -13,6 +13,6 @@
 int           hh_hms                PROTO((double hh, int *hr, int *mn, double *sc));
 int           dd_dms                PROTO((double dd, int *dg, int *mn, double *sc));
-int           hms_format            PROTO((char *line, double value));
-int           dms_format            PROTO((char *line, double value));
+int           hms_format            PROTO((char *line, int length, double value));
+int           dms_format            PROTO((char *line, int length, double value));
 int           hh_hm                 PROTO((double hh, int *hr, double *mn));
 int           day_to_sec            PROTO((char *string, time_t *second));
Index: trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- trunk/Ohana/src/libdvo/include/dvo.h	(revision 38472)
+++ trunk/Ohana/src/libdvo/include/dvo.h	(revision 38553)
@@ -259,4 +259,5 @@
 typedef struct {
   off_t Nregions;
+  off_t Nalloc;
   char **filename;
   SkyRegion *regions;
@@ -920,4 +921,6 @@
 PhotCodeData *GetPhotcodeTable (void);
 void SetPhotcodeTable (PhotCodeData *);
+void FreePhotcodeData (PhotCodeData *myPhotcodes);
+void FreePhotcodeTable (void);
 
 int *GetSecFiltMap(PhotCodeData *ouput, PhotCodeData* input);
Index: trunk/Ohana/src/libdvo/src/HostTable.c
===================================================================
--- trunk/Ohana/src/libdvo/src/HostTable.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/HostTable.c	(revision 38553)
@@ -62,6 +62,7 @@
   char *filename = NULL;
 
-  ALLOCATE (filename, char, strlen(catdir) + strlen(rootname) + 2); // one slash and one EOL
-  sprintf (filename, "%s/%s", catdir, rootname);
+  int Nchar = strlen(catdir) + strlen(rootname) + 16;
+  ALLOCATE (filename, char, Nchar); // one slash and one EOL
+  snprintf (filename, Nchar, "%s/%s", catdir, rootname);
 
   FILE *f = fopen (filename, "r");
Index: trunk/Ohana/src/libdvo/src/LoadImages.c
===================================================================
--- trunk/Ohana/src/libdvo/src/LoadImages.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/LoadImages.c	(revision 38553)
@@ -23,5 +23,5 @@
 
   catdir = dvo_get_catdir ();
-  sprintf (filename, "%s/Images.dat", catdir);
+  snprintf (filename, 256, "%s/Images.dat", catdir);
 
   if (lastFilename) {
Index: trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c
===================================================================
--- trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 38553)
@@ -51,5 +51,4 @@
     }									\
     photcode = PhotCode_##FORMAT##_To_Internal (photcode_input, Ncode); \
-    free (photcode_input);						\
   }
 
@@ -65,4 +64,6 @@
   CONVERT_FORMAT("DVO_PHOTCODE_PS1_V4",    PS1_V4);
   CONVERT_FORMAT("DVO_PHOTCODE_PS1_V5",    PS1_V5);
+
+  gfits_db_free (&db);
 
   table = GetPhotcodeTable ();
Index: trunk/Ohana/src/libdvo/src/RegionHostTable.c
===================================================================
--- trunk/Ohana/src/libdvo/src/RegionHostTable.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/RegionHostTable.c	(revision 38553)
@@ -64,6 +64,8 @@
   char *filename = NULL;
 
-  ALLOCATE (filename, char, strlen(catdir) + strlen(rootname) + 2); // one slash and one EOL
-  sprintf (filename, "%s/%s", catdir, rootname);
+  int Nchar = strlen(catdir) + strlen(rootname) + 16;
+
+  ALLOCATE (filename, char, Nchar); // one slash and one EOL
+  snprintf (filename, Nchar, "%s/%s", catdir, rootname);
 
   FILE *f = fopen (filename, "r");
Index: trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c
===================================================================
--- trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 38553)
@@ -37,4 +37,5 @@
   if (!gfits_db_save (&db)) return (FALSE);
   if (!gfits_db_close (&db)) return (FALSE);
+  if (!gfits_db_free (&db)) return (FALSE);
 
   free (photcode_output);
Index: trunk/Ohana/src/libdvo/src/cmf-ps1-v5-r2-lensing.c
===================================================================
--- trunk/Ohana/src/libdvo/src/cmf-ps1-v5-r2-lensing.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/cmf-ps1-v5-r2-lensing.c	(revision 38553)
@@ -480,2 +480,54 @@
 }
 
+/** ****/
+int fix_lensing (char *data, int Nrows) {
+
+  off_t i;
+
+  int Nbytes = sizeof (CMF_PS1_V5_R2_Lensing);
+
+  char buffer[64];
+
+  char *ptr = data;
+  for (i = 0; i < Nrows; i++, ptr += Nbytes) {
+    // bytes 184 - 224 (exclusive) are now at 288 - 328 (exclusive)
+    memcpy (buffer, &ptr[288], 40);
+    memmove (&ptr[224], &ptr[184], 104);
+    memcpy (&ptr[184], buffer, 40);
+  }
+  return TRUE;
+}
+
+/**************************************/
+CMF_PS1_V5_R2_Lensing *gfits_table_get_CMF_PS1_V5_R2x_Lensing (FTable *ftable, off_t *Ndata, char *swapped) {
+
+  int Ncols;
+  CMF_PS1_V5_R2_Lensing *data;
+
+  Ncols = ftable[0].header[0].Naxis[0];
+  if (Ncols != ST_SIZE) {
+    fprintf (stderr, "ERROR: mis-match in table size: width is %d but should be %d bytes\n", Ncols, ST_SIZE);
+    return NULL;
+  }
+
+  *Ndata = ftable[0].header[0].Naxis[1];
+
+  fix_lensing (ftable[0].buffer, *Ndata);
+  data = (CMF_PS1_V5_R2_Lensing *) ftable[0].buffer;
+  if ((swapped == NULL) || (*swapped == FALSE)) {
+    if (!gfits_convert_CMF_PS1_V5_R2_Lensing ((unsigned char *) data, sizeof (CMF_PS1_V5_R2_Lensing), *Ndata, TRUE)) {
+      return NULL;
+    }
+    off_t i;
+    for (i = 0; i < *Ndata; i++) {
+      data[i].detID   += 0x80000000;
+      data[i].flags   += 0x80000000;
+      data[i].flags2  += 0x80000000;
+      data[i].nFrames += 0x8000;
+    }
+    // gfits_table_scale_data (ftable);
+    if (swapped != NULL) *swapped = TRUE;
+  }
+  return (data);
+}
+
Index: trunk/Ohana/src/libdvo/src/convert.c
===================================================================
--- trunk/Ohana/src/libdvo/src/convert.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/convert.c	(revision 38553)
@@ -46,5 +46,5 @@
 }
  
-int hms_format (char *line, double value) {
+int hms_format (char *line, int length, double value) {
 
   int hr, mn;
@@ -54,10 +54,10 @@
   hr = (int) value;
   if (isnan (value))
-    sprintf (line, "xx:xx:xx.xx");
+    snprintf (line, length, "xx:xx:xx.xx");
   else {
     if (value < 0) {
-      sprintf (line, "-%02d:%02d:%05.2f", abs(hr), mn, sc);
+      snprintf (line, length, "-%02d:%02d:%05.2f", abs(hr), mn, sc);
     } else {
-      sprintf (line, "+%02d:%02d:%05.2f", hr, mn, sc);
+      snprintf (line, length, "+%02d:%02d:%05.2f", hr, mn, sc);
     }
   }      
@@ -65,5 +65,5 @@
 }
 
-int dms_format (char *line, double value) {
+int dms_format (char *line, int length, double value) {
 
   int dg, mn;
@@ -72,7 +72,7 @@
   dd_dms (value, &dg, &mn, &sc);
   if (value < 0) {
-    sprintf (line, "-%02d:%02d:%05.2f", abs(dg), mn, sc);
+    snprintf (line, length, "-%02d:%02d:%05.2f", abs(dg), mn, sc);
   } else {
-    sprintf (line, "+%02d:%02d:%05.2f", dg, mn, sc);
+    snprintf (line, length, "+%02d:%02d:%05.2f", dg, mn, sc);
   }
   return (TRUE);
@@ -134,5 +134,5 @@
   ALLOCATE (line, char, 64);
   gmt   = gmtime (&second);
-  sprintf (line, "%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
+  snprintf (line, 64, "%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
   return (line);
 }
@@ -148,23 +148,23 @@
   switch (gmt[0].tm_wday) {
     case 0:
-      sprintf (line, "Sun@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
+      snprintf (line, 64, "Sun@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
       break;
     case 1:
-      sprintf (line, "Mon@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
+      snprintf (line, 64, "Mon@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
       break;
     case 2:
-      sprintf (line, "Tue@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
+      snprintf (line, 64, "Tue@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
       break;
     case 3:
-      sprintf (line, "Wed@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
+      snprintf (line, 64, "Wed@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
       break;
     case 4:
-      sprintf (line, "Thu@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
+      snprintf (line, 64, "Thu@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
       break;
     case 5:
-      sprintf (line, "Fri@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
+      snprintf (line, 64, "Fri@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
       break;
     case 6:
-      sprintf (line, "Sat@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
+      snprintf (line, 64, "Sat@%02d:%02d:%02d", gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec); 
       break;
   }
@@ -195,5 +195,5 @@
   hh_hm (deg, &hr, &mn);
 
-  sprintf (line, "%03d:%04.1f", abs(hr), mn);
+  snprintf (line, 16, "%03d:%04.1f", abs(hr), mn);
   return (line);
 }
@@ -209,5 +209,5 @@
   hh_hm (deg/15.0, &hr, &mn);
 
-  sprintf (line, "%02d:%04.1f", abs(hr), mn);
+  snprintf (line, 16, "%02d:%04.1f", abs(hr), mn);
   return (line);
 }
@@ -224,7 +224,7 @@
 
   if (deg < 0) {
-    sprintf (line, "-%02d:%04.1f", abs(hr), mn);
+    snprintf (line, 16, "-%02d:%04.1f", abs(hr), mn);
   } else {
-    sprintf (line, "+%02d:%04.1f", hr, mn);
+    snprintf (line, 16, "+%02d:%04.1f", hr, mn);
   }      
   return (line);
Index: trunk/Ohana/src/libdvo/src/coordops.c
===================================================================
--- trunk/Ohana/src/libdvo/src/coordops.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/coordops.c	(revision 38553)
@@ -740,5 +740,5 @@
 	for (i = 0; i < 14; i++) {
 	  char name[64];
-	  sprintf (name, "PV2_%d", i);
+	  snprintf (name, 64, "PV2_%d", i);
 	  if (i < 7) {
 	    found = gfits_scan (header, name, "%f", 1, &coords[0].polyterms[0][i]);
@@ -834,5 +834,5 @@
     if (!strncmp (coords[0].ctype, "SLAT", 4)) strcpy (csys, "SLON");
     if (!strcmp (csys, "NONE")) return (FALSE);
-    sprintf (ctype, "%s-%s", csys, &coords[0].ctype[5]);
+    snprintf (ctype, 16, "%s-%s", csys, &coords[0].ctype[5]);
     gfits_modify (header, "CTYPE1",   "%s",  1, ctype);
   }    
Index: trunk/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 38553)
@@ -85,28 +85,27 @@
 
 // we can return a static string here unless we run multiple outputs threads at once
-static char compress_string[16];
 char *dvo_catalog_compress_string (DVOCatCompress catcompress) {
-
+  char *compress_string = NULL;
   switch (catcompress) {
     case DVO_COMPRESS_NONE:
-      strcpy (compress_string, "NONE");
+      compress_string = strcreate("NONE");
       break;
     case DVO_COMPRESS_AUTO:
-      strcpy (compress_string, "AUTO");
+      compress_string = strcreate("AUTO");
       break;
     case DVO_COMPRESS_GZIP_1:
-      strcpy (compress_string, "GZIP_1");
+      compress_string = strcreate("GZIP_1");
       break;
     case DVO_COMPRESS_GZIP_2:
-      strcpy (compress_string, "GZIP_2");
+      compress_string = strcreate("GZIP_2");
       break;
     case DVO_COMPRESS_NONE_1:
-      strcpy (compress_string, "NONE_1");
+      compress_string = strcreate("NONE_1");
       break;
     case DVO_COMPRESS_NONE_2:
-      strcpy (compress_string, "NONE_2");
+      compress_string = strcreate("NONE_2");
       break;
     case DVO_COMPRESS_RICE_1:
-      strcpy (compress_string, "RICE_1");
+      compress_string = strcreate("RICE_1");
       break;
     default:
@@ -532,5 +531,5 @@
   galphot->minorAxisErr = NAN;
   galphot->theta        = NAN;
-  galphot->theta_err    = NAN;
+  galphot->thetaErr     = NAN;
   galphot->index        = NAN;
   galphot->chisq        = NAN;
@@ -541,4 +540,5 @@
   galphot->catID   = -1;
   galphot->imageID = -1;
+  galphot->averef  = 0;
 
   galphot->photcode = 0;
@@ -1157,9 +1157,11 @@
     return FALSE;
   }
-      
+  
+  int outStatus = TRUE;
   status = unlink (tmpfilename);
   if (status) {
+    perror ("unlink: ");
     fprintf (stderr, "failed to unlink catalog %s~\n", catalog->filename);
-    return FALSE;
+    outStatus = FALSE;
   }
       
@@ -1167,40 +1169,40 @@
     if (catalog[0].measure_catalog != NULL) {
       if (!dvo_catalog_unlink_backup (catalog[0].measure_catalog, FALSE)) {
-	return FALSE;
+	outStatus = FALSE;
       }
     }
     if (catalog[0].missing_catalog != NULL) {
       if (!dvo_catalog_unlink_backup (catalog[0].missing_catalog, FALSE)) {
-	return FALSE;
+	outStatus = FALSE;
       }
     }
     if (catalog[0].secfilt_catalog != NULL) {
       if (!dvo_catalog_unlink_backup (catalog[0].secfilt_catalog, FALSE)) {
-	return FALSE;
+	outStatus = FALSE;
       }
     }
     if ((catalog[0].lensing_catalog != NULL) && (catalog[0].Nlensing_disk > 0)) {
       if (!dvo_catalog_unlink_backup (catalog[0].lensing_catalog, FALSE)) {
-	return FALSE;
+	outStatus = FALSE;
       }
     }
     if ((catalog[0].lensobj_catalog != NULL) && (catalog[0].Nlensobj_disk > 0)) {
       if (!dvo_catalog_unlink_backup (catalog[0].lensobj_catalog, FALSE)) {
-	return FALSE;
+	outStatus = FALSE;
       }
     }
     if ((catalog[0].starpar_catalog != NULL) && (catalog[0].Nstarpar_disk > 0)) {
       if (!dvo_catalog_unlink_backup (catalog[0].starpar_catalog, FALSE)) {
-	return FALSE;
+	outStatus = FALSE;
       }
     }
     if ((catalog[0].galphot_catalog != NULL) && (catalog[0].Ngalphot_disk > 0)) {
       if (!dvo_catalog_unlink_backup (catalog[0].galphot_catalog, FALSE)) {
-	return FALSE;
-      }
-    }
-  }
-  return TRUE;
-}
-
-
+	outStatus = FALSE;
+      }
+    }
+  }
+  return outStatus;
+}
+
+
Index: trunk/Ohana/src/libdvo/src/dvo_catalog_create.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_catalog_create.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/dvo_catalog_create.c	(revision 38553)
@@ -105,5 +105,6 @@
   char *path = pathname (catalog[0].filename);
   char *root = filerootname (catalog[0].filename);
-  int length = strlen(path) + strlen(root) + 6;
+
+  int length = strlen(path) + strlen(root) + 16;
     
   Catalog *subcat;
@@ -120,5 +121,5 @@
   ALLOCATE (subcat->filename, char, length);
 
-  sprintf (subcat->filename, "%s/%s.%s", path, root, ext);
+  snprintf (subcat->filename, length, "%s/%s.%s", path, root, ext);
 
   char *file = filebasename (subcat->filename);
Index: trunk/Ohana/src/libdvo/src/dvo_catalog_split.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 38553)
@@ -90,7 +90,5 @@
 int dvo_catalog_save_subcat (Catalog *catalog, FTable *ftable, off_t start, off_t Nrows, off_t Ndisk, off_t Ntotal) {
 
-  Matrix matrix;
-  
-  if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+  if (OHANA_MEMCHECK) ohana_memcheck (TRUE);
   
   /* rewind file pointers and truncate (file is still open) */
@@ -108,5 +106,7 @@
   off_t fullsize = catalog->header.datasize;
 
+# if (1)
   // write the PHU matrix; this is probably a NOP, do I have to keep it in?
+  Matrix matrix;
   gfits_create_matrix (&catalog->header, &matrix);
   if (!gfits_fwrite_matrix  (catalog->f, &matrix)) {
@@ -117,4 +117,5 @@
   fullsize += matrix.datasize;
   gfits_free_matrix (&matrix);
+# endif
 
   FTable *outtable = ftable;
@@ -128,5 +129,5 @@
   int isCompressed = output_is_compressed (start, Nrows, Ntotal, catalog->catcompress);
 
-  if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+  if (OHANA_MEMCHECK) ohana_memcheck (TRUE);
 
   if (isCompressed) {
@@ -136,18 +137,22 @@
     if (!gfits_compress_table (ftable, &cmptable, 1000, compressMode)) {
       fprintf (stderr, "compression failure\n");
+      free (compressMode);
       return (FALSE);
     }
     if (!byteswap_varlength_ftable (&cmpheader, &cmptable)) {
       fprintf (stderr, "failed to swap varlength column\n");
+      free (compressMode);
       return FALSE;
     }
     if (!gfits_modify (cmptable.header, "DVO_CMP", "%s", 1, compressMode)) {
       fprintf (stderr, "can't save compression mode\n");
-      return (FALSE);
-    }
+      free (compressMode);
+      return (FALSE);
+    }
+    free (compressMode);
     outtable = &cmptable;
   }
 
-  if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+  if (OHANA_MEMCHECK) ohana_memcheck (TRUE);
 
   // write the table data
@@ -169,5 +174,31 @@
   fullsize += outtable->datasize + outtable->header->datasize;
 
-  if (OHANA_MEMCHECK) ohana_memcheck_func (TRUE);
+  // XXX test of repeated failure
+  if (0) {
+    Matrix myMatrix;
+
+    // write the PHU matrix; this is probably a NOP, do I have to keep it in?
+    gfits_create_matrix (&catalog->header, &myMatrix);
+
+    int niter;
+    for (niter = 0; niter < 5; niter ++) {
+      char name[1024];
+      snprintf (name, 1024, "%s.v.%d", catalog->filename, niter);
+      FILE *f = fopen (name, "w");
+
+      myAssert (gfits_fwrite_header  (f, &catalog->header), "failed to write header");
+      myAssert (gfits_fwrite_matrix  (f, &myMatrix), "failed to write matrix");
+      myAssert (gfits_fwrite_Theader (f, outtable->header), "can't write table header");
+      myAssert (gfits_fwrite_table (f, outtable), "can't write table data");
+      myAssert (!fflush (f), "failed to flush");
+      myAssert (!fclose (f), "failed to close");
+      int fd = fileno(f);
+      myAssert (fsync(fd), "failed to sync");
+      fprintf (stderr, "wrote to %s\n", name);
+    }
+    gfits_free_matrix (&myMatrix);
+  }
+
+  if (OHANA_MEMCHECK) ohana_memcheck (TRUE);
 
   // since we init'ed the structures above, these operations are safe whether or not we compressed the table
@@ -191,5 +222,5 @@
   off_t Nskip;
   int status;
-  char *path, string[80];
+  char *path, string[256];
   Catalog *subcat;
 
@@ -215,6 +246,7 @@
     return (DVO_CAT_OPEN_FAIL);
   }
-  ALLOCATE (subcat[0].filename, char, strlen(path) + strlen(string) + 2);
-  sprintf (subcat[0].filename, "%s/%s", path, string);
+  int Nchar = strlen(path) + strlen(string) + 10;
+  ALLOCATE (subcat[0].filename, char, Nchar);
+  snprintf (subcat[0].filename, Nchar, "%s/%s", path, string);
   free (path);
 
@@ -282,5 +314,5 @@
     }
 
-    char compressMode[80];
+    char compressMode[256];
     if (!gfits_scan (ftable->header, "DVO_CMP", "%s", 1, compressMode)) {
       strcpy (compressMode, "AUTO");
@@ -759,4 +791,21 @@
 }
 
+# if (0)
+void compare_file_ptr (FILE *copy_ptr, FILE *real_ptr, char *name) {
+  return;
+
+  int i;
+  
+  char *real = (char *) real_ptr;
+  char *copy = (char *) copy_ptr;
+
+  for (i = 0; i < sizeof(FILE); i++, real++, copy++) {
+    if (*real != *copy) {
+      fprintf (stderr, "file pointers %s differ @ %d\n", name, (int) ((char *) real - (char *) real_ptr));
+    }
+  }
+}
+# endif
+
 /* save_catalog_split writes all data currently in memory to disk */
 int dvo_catalog_save_split (Catalog *catalog, char VERBOSE) {
@@ -845,4 +894,5 @@
       goto failure;
     }
+
     gfits_free_header (&header);
     gfits_free_table (&ftable);
@@ -887,4 +937,5 @@
       goto failure;
     }
+
     gfits_free_header (&header);
     gfits_free_table (&ftable);
Index: trunk/Ohana/src/libdvo/src/dvo_convert.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/dvo_convert.c	(revision 38553)
@@ -1035,12 +1035,13 @@
   Nimage = theader[0].Naxis[1];
 
-# define FORMAT_CASE(FORMAT, TYPE) \
-    case DVO_FORMAT_##FORMAT: { \
-      Image_##TYPE *tmpImage; \
+# define FORMAT_CASE(FORMAT, TYPE)		\
+  case DVO_FORMAT_##FORMAT: {			\
+    Image_##TYPE *tmpImage;						\
       tmpImage = ImageInternalTo_##TYPE ((Image *) ftable[0].buffer, Nimage); \
-      free (ftable[0].buffer); \
-      gfits_table_set_Image_##TYPE (ftable, tmpImage, Nimage, TRUE); \
-      free (tmpImage); \
-      break; }
+	free (ftable[0].buffer);					\
+	gfits_free_header (ftable->header);				\
+	gfits_table_set_Image_##TYPE (ftable, tmpImage, Nimage, TRUE);	\
+	  free (tmpImage);						\
+	  break; }
 
   /* convert from the internal format */
Index: trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 38553)
@@ -990,22 +990,23 @@
 
     out[i].Xfit       	 = in[i].Xfit;
-    out[i]. Yfit      	 = in[i]. Yfit;
-    out[i]. mag       	 = in[i]. mag;
-    out[i]. magErr    	 = in[i]. magErr;
-    out[i]. majorAxis 	 = in[i]. majorAxis;
-    out[i]. minorAxis 	 = in[i]. minorAxis;
-    out[i]. majorAxisErr = in[i]. majorAxisErr;
-    out[i]. minorAxisErr = in[i]. minorAxisErr;
-    out[i]. theta        = in[i]. theta;
-    out[i]. theta_err    = in[i]. theta_err;
-    out[i]. index        = in[i]. index;
-    out[i]. chisq	 = in[i]. chisq;
-    out[i]. Npix 	 = in[i]. Npix;
-    out[i]. objID	 = in[i]. objID;
-    out[i]. catID        = in[i]. catID;
-    out[i]. detID	 = in[i]. detID;
-    out[i]. imageID	 = in[i]. imageID;
-    out[i]. photcode	 = in[i]. photcode;
-    out[i]. modelType	 = in[i]. modelType;
+    out[i].Yfit      	 = in[i].Yfit;
+    out[i].mag       	 = in[i].mag;
+    out[i].magErr    	 = in[i].magErr;
+    out[i].majorAxis 	 = in[i].majorAxis;
+    out[i].minorAxis 	 = in[i].minorAxis;
+    out[i].majorAxisErr  = in[i].majorAxisErr;
+    out[i].minorAxisErr  = in[i].minorAxisErr;
+    out[i].theta         = in[i].theta;
+    out[i].thetaErr      = in[i].thetaErr;
+    out[i].index         = in[i].index;
+    out[i].chisq	 = in[i].chisq;
+    out[i].Npix 	 = in[i].Npix;
+    out[i].objID	 = in[i].objID;
+    out[i].catID         = in[i].catID;
+    out[i].detID	 = in[i].detID;
+    out[i].imageID	 = in[i].imageID;
+    out[i].averef	 = in[i].averef;
+    out[i].photcode	 = in[i].photcode;
+    out[i].modelType	 = in[i].modelType;
   }
   return (out);
@@ -1029,5 +1030,5 @@
     out[i].minorAxisErr  = in[i].minorAxisErr;
     out[i].theta         = in[i].theta;
-    out[i].theta_err     = in[i].theta_err;
+    out[i].thetaErr      = in[i].thetaErr;
     out[i].index         = in[i].index;
     out[i].chisq	 = in[i].chisq;
@@ -1037,4 +1038,5 @@
     out[i].detID	 = in[i].detID;
     out[i].imageID	 = in[i].imageID;
+    out[i].averef	 = in[i].averef;
     out[i].photcode	 = in[i].photcode;
     out[i].modelType	 = in[i].modelType;
Index: trunk/Ohana/src/libdvo/src/dvo_image.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_image.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/dvo_image.c	(revision 38553)
@@ -231,5 +231,5 @@
 int dvo_image_createID (Header *header) {
   
-  char dbID[33];
+  char dbID[64];
 
   if (!header->buffer) return FALSE;
@@ -259,5 +259,5 @@
   int i;
   for (i = 0; i < 32; i++) {
-    sprintf (&dbID[i], "%1x", (int)(16.0*drand48()));
+    myAssert (snprintf (&dbID[i], 2, "%1x", (int)(16.0*drand48())) < 2, "overflow");
   }
 
Index: trunk/Ohana/src/libdvo/src/dvo_photcode_ops.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_photcode_ops.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/dvo_photcode_ops.c	(revision 38553)
@@ -43,5 +43,19 @@
 }
 
-// set the photcode table. This is used to switch between serveral previously
+// free a specified photcode set
+void FreePhotcodeData (PhotCodeData *myPhotcodes) {
+  if (!myPhotcodes) return;
+  FREE(myPhotcodes->code);
+  FREE(myPhotcodes);
+}
+
+// free the internal table
+void FreePhotcodeTable (void) {
+  FreePhotcodeData (photcodes);
+  FREE(genericCodeMag);
+  FREE(genericCodeFlux);
+}
+
+// set the photcode table. This is used to switch between several previously
 // allocated tables
 void SetPhotcodeTable (PhotCodeData *new) {
Index: trunk/Ohana/src/libdvo/src/dvo_util.c
===================================================================
--- trunk/Ohana/src/libdvo/src/dvo_util.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/dvo_util.c	(revision 38553)
@@ -63,5 +63,5 @@
     }
 
-    sprintf (dvoConfig->photcodeFile, "%s/Photcodes.dat", dvoConfig->catdir);
+    snprintf (dvoConfig->photcodeFile, 256, "%s/Photcodes.dat", dvoConfig->catdir);
     if (!LoadPhotcodes (dvoConfig->photcodeFile, MasterPhotcodeFile, FALSE)) {
         fprintf (stderr, "error loading photcode table %s or master file %s\n",
@@ -101,5 +101,5 @@
     char filename[256];
   
-    sprintf (filename, "%s/Images.dat", dvoConfig->catdir);
+    snprintf (filename, 256, "%s/Images.dat", dvoConfig->catdir);
 
     gfits_db_init (&dvoConfig->imageDB);
Index: trunk/Ohana/src/libdvo/src/skydb.c
===================================================================
--- trunk/Ohana/src/libdvo/src/skydb.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/skydb.c	(revision 38553)
@@ -224,5 +224,5 @@
   /* north dec bands */
   for (dec = 0; dec < 90; dec += 7.5) {
-    sprintf (db[N].name, "n%04d.cpt", (int) 100*dec);
+    myAssert (snprintf (db[N].name, 18, "n%04d.cpt", (int) 100*dec) < 18, "overflow");
     db[N].Rmin =   0; db[N].Rmax = 360;
     db[N].Dmin = dec; db[N].Dmax = dec + 7.5;
@@ -233,5 +233,5 @@
   /* south dec bands */
   for (dec = 0; dec > -90; dec -= 7.5) {
-    sprintf (db[N].name, "s%04d.cpt", (int) 100*dec);
+    myAssert (snprintf (db[N].name, 18, "s%04d.cpt", (int) 100*dec) < 18, "overflow");
     db[N].Rmin =   0;       db[N].Rmax = 360;
     db[N].Dmin = dec - 7.5; db[N].Dmax = dec;
@@ -276,5 +276,5 @@
       new[Nnew].child = FALSE;
       strncpy (root, db[i].name, 5); root[5] = 0;
-      sprintf (new[Nnew].name, "%s/r%04d.cpt", root, Rnumber);
+      myAssert (snprintf (new[Nnew].name, 18, "%s/r%04d.cpt", root, Rnumber) < 18, "overflow");
       Rnumber ++;      
       Nnew ++;
@@ -365,5 +365,5 @@
 	db[N].child = FALSE;
 	strncpy (root, db[i].name, 10); root[10] = 0;
-	sprintf (db[N].name, "%s.%02d.cpt", root, Rnumber);
+	myAssert (snprintf (db[N].name, 18, "%s.%02d.cpt", root, Rnumber) < 18, "overflow");
 	Rnumber ++;
 	N ++;
Index: trunk/Ohana/src/libdvo/src/skyregion_gsc.c
===================================================================
--- trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 38553)
@@ -46,5 +46,4 @@
   FTable ftable;
   SkyTable *skytable;
-  SkyTable *band;
   SkyTable L0, L1, L2, L3, L4;
   SkyRegionZone *zones;
@@ -79,5 +78,6 @@
 /* L0 : full sky */
   L0.Nregions = 1;
-  ALLOCATE (L0.regions, SkyRegion, L0.Nregions);
+  L0.Nalloc = 1;
+  ALLOCATE (L0.regions, SkyRegion, L0.Nalloc);
   L0.regions[0].Rmin 	=   0;
   L0.regions[0].Rmax 	= 360;
@@ -99,8 +99,8 @@
   /* allocate space for all levels */
   L1.Nregions = L2.Nregions = L3.Nregions = L4.Nregions = 0;
-  ALLOCATE (L1.regions, SkyRegion, 1);
-  ALLOCATE (L2.regions, SkyRegion, 1);
-  ALLOCATE (L3.regions, SkyRegion, 1);
-  ALLOCATE (L4.regions, SkyRegion, 1);
+  L1.Nalloc = NDECBANDS;         ALLOCATE (L1.regions, SkyRegion, L1.Nalloc);
+  L2.Nalloc = NDECBANDS*0x10;    ALLOCATE (L2.regions, SkyRegion, L2.Nalloc);
+  L3.Nalloc = NDECBANDS*0x100;   ALLOCATE (L3.regions, SkyRegion, L3.Nalloc);
+  L4.Nalloc = NDECBANDS*0x1000;  ALLOCATE (L4.regions, SkyRegion, L4.Nalloc);
 
   // skipLines = 0;
@@ -130,5 +130,5 @@
 
     /* load all GSC Regions in this band */
-    band = SkyRegionForDecBand (&ftable.buffer[skipLines*48], DecLines[i], DecNames[i], L1.regions[i].Dmin, L1.regions[i].Dmax);
+    SkyTable *band = SkyRegionForDecBand (&ftable.buffer[skipLines*48], DecLines[i], DecNames[i], L1.regions[i].Dmin, L1.regions[i].Dmax);
     skipLines += DecLines[i];
 
@@ -151,5 +151,5 @@
 
     L1.Nregions ++;
-    REALLOCATE (L1.regions, SkyRegion, L1.Nregions + 1);
+    myAssert (L1.Nregions <= NDECBANDS, "too many L1 regions");
   }
 
@@ -225,5 +225,5 @@
 
     temp[5] = 0;
-    sprintf (name, "%s/%s", DecName, &temp[1]);
+    snprintf (name, 80, "%s/%s", DecName, &temp[1]);
     strcpy (regions[i].name, name);
   }
@@ -231,4 +231,5 @@
   band[0].regions = regions;
   band[0].Nregions = Nregions;
+  band[0].Nalloc   = Nregions;
   return (band);
 }
@@ -277,5 +278,5 @@
   Nregions = band[0].Nregions;
 
-  NZ = 10;
+  NZ = 100;
   ALLOCATE (zones, SkyRegionZone, NZ);
  
@@ -312,5 +313,5 @@
       /* go to the next zone */
       Nz++;
-      CHECK_REALLOCATE (zones, SkyRegionZone, NZ, Nz, 10);
+      CHECK_REALLOCATE (zones, SkyRegionZone, NZ, Nz, 100);
 
       /* start info for the new zone */
@@ -365,5 +366,5 @@
 	band[0].regions[Nregions + i].Dmax = -86.250;
       }
-      sprintf (band[0].regions[Nregions + i].name, "%s.%d", basename, i);
+      myAssert (snprintf (band[0].regions[Nregions + i].name, 18, "%s.%d", basename, i) < 18, "overflow");
     }
     band[0].regions[Nregions + i].Rmin = 0.0;
@@ -376,5 +377,5 @@
       band[0].regions[Nregions + i].Dmax = -88.125;
     }
-    sprintf (band[0].regions[Nregions + i].name, "%s.%d", basename, i);
+    myAssert (snprintf (band[0].regions[Nregions + i].name, 18, "%s.%d", basename, i) < 18, "overflow");
 
     zones[0].Nset = 6;
@@ -401,9 +402,9 @@
 void SkyTableL2fromZone (SkyTable *L2, SkyTable *L3, SkyTable *L4, SkyTable *band, SkyRegionZone *zone, int parent) {
 
-  int i, Nr, Ns, Ne;
+  int i, Ns, Ne;
   char *p, name[80];
 
-  Nr = L2[0].Nregions;
-  REALLOCATE (L2[0].regions, SkyRegion, Nr + 1);
+  int Nr = L2[0].Nregions;
+  CHECK_REALLOCATE (L2[0].regions, SkyRegion, L2[0].Nalloc, L2[0].Nregions, 100);
   
   /* divide this zone into L2 regions with Nset L3 regions each (fewer on ends) */
@@ -435,5 +436,5 @@
     }
     *p = 0;
-    sprintf (name, "%s/z%03d", band[0].regions[Ns].name, Nr);
+    myAssert (snprintf (name, 80, "%s/z%03d", band[0].regions[Ns].name, Nr) < 80, "overflow");
     *p = '/';
     strcpy (L2[0].regions[Nr].name, name);
@@ -444,5 +445,5 @@
 
     Nr++;
-    REALLOCATE (L2[0].regions, SkyRegion, Nr + 1);
+    CHECK_REALLOCATE (L2[0].regions, SkyRegion, L2[0].Nalloc, Nr, 100);
   }
   L2[0].Nregions = Nr;
@@ -456,5 +457,5 @@
   Nr = L3[0].Nregions;
   L3[0].Nregions += Ne - Ns;
-  REALLOCATE (L3[0].regions, SkyRegion, L3[0].Nregions);
+  CHECK_REALLOCATE (L3[0].regions, SkyRegion, L3[0].Nalloc, L3[0].Nregions, 0.5*L3[0].Nalloc);
 
   L2[0].child  = TRUE;
@@ -498,5 +499,5 @@
   Nr = L4[0].Nregions;
   L4[0].Nregions += NDIV*NDIV;
-  REALLOCATE (L4[0].regions, SkyRegion, L4[0].Nregions);
+  CHECK_REALLOCATE (L4[0].regions, SkyRegion, L4[0].Nalloc, L4[0].Nregions, 0.5*L4[0].Nalloc);
 
   L3[0].child  = TRUE;
@@ -531,5 +532,5 @@
       L4[0].regions[Nr].backupID  = 0;
 
-      sprintf (name, "%s.%02d", L3[0].name, Nbox);
+      myAssert (snprintf (name, 80, "%s.%02d", L3[0].name, Nbox) < 80, "overflow");
       strcpy (L4[0].regions[Nr].name, name);
       if (DEBUG >= 4) SkyRegionPrint (&L4[0].regions[Nr]);
Index: trunk/Ohana/src/libdvo/src/skyregion_io.c
===================================================================
--- trunk/Ohana/src/libdvo/src/skyregion_io.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/skyregion_io.c	(revision 38553)
@@ -113,4 +113,9 @@
   fclose (f);
 
+  gfits_free_header  (&header);
+  gfits_free_matrix  (&matrix);
+  gfits_free_header  (&theader);
+  gfits_free_table   (&ftable);
+
   return (TRUE);
 }
@@ -131,4 +136,5 @@
     exit (1);
   }
+  free (filename);
   return (sky);
 
@@ -195,5 +201,5 @@
   // this generates the names, be sure to free when not needed
   for (i = 0; i < list[0].Nregions; i++) {
-    sprintf (line, "%s/%s.%s", path, list[0].regions[i][0].name, ext);
+    snprintf (line, 256, "%s/%s.%s", path, list[0].regions[i][0].name, ext);
     list[0].filename[i] = strcreate (line);
   }
@@ -209,5 +215,5 @@
   // this generates the names, be sure to free when not needed
   for (i = 0; i < sky[0].Nregions; i++) {
-    sprintf (line, "%s/%s.%s", path, sky[0].regions[i].name, ext);
+    snprintf (line, 256, "%s/%s.%s", path, sky[0].regions[i].name, ext);
     sky[0].filename[i] = strcreate (line);
   }
@@ -222,6 +228,8 @@
   
   char *skyfile;
-  ALLOCATE (skyfile, char, strlen(catdir) + strlen("/SkyTable.fits") + 1);
-  sprintf (skyfile, "%s/SkyTable.fits", catdir);
+
+  int Nchar = strlen(catdir) + strlen("/SkyTable.fits") + 16;
+  ALLOCATE (skyfile, char, Nchar);
+  snprintf (skyfile, Nchar, "%s/SkyTable.fits", catdir);
 
   return skyfile;
Index: trunk/Ohana/src/libdvo/src/skyregion_ops.c
===================================================================
--- trunk/Ohana/src/libdvo/src/skyregion_ops.c	(revision 38472)
+++ trunk/Ohana/src/libdvo/src/skyregion_ops.c	(revision 38553)
@@ -550,4 +550,5 @@
     }
     free (list[0].regions);
+    free (list[0].filename);
   }
   free (list);
