Index: /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/BrightCatalog.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/BrightCatalog.c	(revision 33484)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/BrightCatalog.c	(revision 33485)
@@ -28,4 +28,8 @@
   FTable ftable;
 
+  header.buffer = NULL;
+  matrix.buffer = NULL;
+  ftable.buffer = NULL;
+  theader.buffer = NULL;
   BrightCatalog *catalog = NULL;
 
@@ -33,5 +37,5 @@
   if (!f) {
     fprintf (stderr, "ERROR: cannot open image subset file %s\n", filename);
-    return NULL;
+    goto escape;
   }
 
@@ -40,5 +44,5 @@
     if (VERBOSE) fprintf (stderr, "can't read image subset header\n");
     fclose (f);
-    return NULL;
+    goto escape;
   }
   if (!gfits_fread_matrix (f, &matrix, &header)) {
@@ -46,5 +50,5 @@
     gfits_free_header (&header);
     fclose (f);
-    return NULL;
+    goto escape;
   }
 
@@ -58,8 +62,8 @@
   { 
     // load data for this header 
-    if (!gfits_load_header (f, &theader)) return (NULL);
+    if (!gfits_load_header (f, &theader)) goto escape;
 
     // read the fits table bytes
-    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) goto escape;
  
     // need to create and assign to flat-field correction
@@ -80,5 +84,6 @@
     GET_COLUMN(catID,     "CAT_ID",     int);
     GET_COLUMN(photcode,  "PHOTCODE",   short);
-    // XXX free the fits table data here 
+    gfits_free_header (&theader);
+    gfits_free_table  (&ftable);
 
     MeasureTiny *measure = NULL;
@@ -129,8 +134,8 @@
 
     // load data for this header 
-    if (!gfits_load_header (f, &theader)) return (NULL);
+    if (!gfits_load_header (f, &theader)) goto escape;
 
     // read the fits table bytes
-    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) goto escape;
 
     GET_COLUMN(R,              "RA",          double);
@@ -163,5 +168,6 @@
     GET_COLUMN(catID,          "CAT_ID",      int);
     GET_COLUMN(extID,          "EXT_ID",      int64_t);
-    // XXX free the fits table data here 
+    gfits_free_header (&theader);
+    gfits_free_table  (&ftable);
 
     Average *average = NULL;
@@ -238,8 +244,8 @@
 
     // load data for this header 
-    if (!gfits_load_header (f, &theader)) return (NULL);
+    if (!gfits_load_header (f, &theader)) goto escape;
 
     // read the fits table bytes
-    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) goto escape;
  
     // need to create and assign to flat-field correction
@@ -252,5 +258,6 @@
     GET_COLUMN(M_20,  "MAG_20",   short);
     GET_COLUMN(M_80,  "MAG_80",   short);
-    // XXX free the fits table data here 
+    gfits_free_header (&theader);
+    gfits_free_table  (&ftable);
 
     SecFilt *secfilt = NULL;
@@ -280,7 +287,20 @@
   }
 
-  // close files
+
+  gfits_free_header (&header);
+  gfits_free_matrix (&matrix);
+  fclose (f);
 
   return catalog;
+
+escape:
+  gfits_free_header (&header);
+  gfits_free_matrix (&matrix);
+  gfits_free_header (&theader);
+  gfits_free_table  (&ftable);
+  if (catalog) free (catalog);
+
+  fclose (f);
+  return NULL;
 }
 
@@ -304,5 +324,5 @@
     return FALSE;
   }
-
+ 
   gfits_fwrite_header  (f, &header);
   gfits_fwrite_matrix  (f, &matrix);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/select_images.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/select_images.c	(revision 33484)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/select_images.c	(revision 33485)
@@ -25,5 +25,5 @@
   
   Image *image;
-  off_t i, j, k, m, nStart, iSky, nimage, NIMAGE;
+  off_t i, j, k, m, nStart, iSky, nimage, NIMAGE, D_NIMAGE;
   off_t *line_number;
   int InRange, found;
@@ -68,4 +68,6 @@
   DminSkyRegion = +90.0;
   DmaxSkyRegion = -90.0;
+
+  D_NIMAGE = 6000;
 
   /* compare with each region file */
@@ -113,5 +115,5 @@
 
   nimage = 0;
-  NIMAGE = 100;
+  NIMAGE = D_NIMAGE;
   ALLOCATE (image, Image, NIMAGE);
   ALLOCATE (line_number, off_t, NIMAGE);
@@ -167,6 +169,6 @@
 
     /* transform corners to ra,dec -- costs ~3sec for 3M images (pikake) */
-    double RminImage = 360.0;
-    double RmaxImage =   0.0;
+    double RminImage = RmidSkyRegion + 180.0;
+    double RmaxImage = RmidSkyRegion - 180.0;
     double DminImage = +90.0;
     double DmaxImage = -90.0;
@@ -249,5 +251,6 @@
     nimage ++;
     if (nimage == NIMAGE) {
-      NIMAGE += 100;
+      NIMAGE += D_NIMAGE;
+      D_NIMAGE = MAX (100000, D_NIMAGE * 1.5);
       REALLOCATE (image, Image, NIMAGE);
       REALLOCATE (line_number, off_t, NIMAGE);
@@ -261,4 +264,6 @@
   REALLOCATE (line_number, off_t, MAX (nimage, 1));
   free (skycoords);
+  free (RmaxSky);
+  free (index);
 
   *Nimage  = nimage;
