Index: /branches/eam_branches/ipp-20111122/Ohana/src/relastro/include/relastro.h
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relastro/include/relastro.h	(revision 33514)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relastro/include/relastro.h	(revision 33515)
@@ -4,4 +4,8 @@
 # include <signal.h>
 # include <assert.h>
+
+// choose off_t or int depending on full-scale relphot analysis resources
+// # define IDX_T off_t
+# define IDX_T int 
 
 typedef enum {
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/ImageOps.c	(revision 33514)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/ImageOps.c	(revision 33515)
@@ -3,17 +3,22 @@
 # define USE_IMAGE_ID 1
 
-static off_t       **bin;     // link from catalog,measure to image
-static int         **clist;   // catalog which supplied measurement on image
-static off_t       **mlist;   // measure reference for measurement on image
-static off_t        *Nlist;   // number of measurements on image
-static off_t        *NLIST;   // allocated number of measurements on image   
-
+// we have an array of images: image[ImageIndex] (ImageIndex : 0 < Nimage)
 static Image        *image;   // list of available images
 static off_t        Nimage;   // number of available images
+
+// if we read only a subset of the rows from the Image FITS, LineNumber tells us to which row
+// each image belongs
 static off_t       *LineNumber; // match of subset to full image table
 
-static int         *Ncatlist;  // catalogs associated with each image
-static int         *NCATLIST;  // catalogs associated with each image
+static off_t        *N_onImage;   // number of measurements on image
+static off_t        *N_ONIMAGE;   // allocated number of measurements on image   
+
+static int          *Ncatlist;  // catalogs associated with each image
+static int          *NCATLIST;  // catalogs associated with each image
 static int         **catlist;  // catalogs associated with each image
+
+static IDX_T       **MeasureToImage;     // link from catalog,measure to image
+static IDX_T       **ImageToCatalog;   // catalog which supplied measurement on image
+static IDX_T       **ImageToMeasure;   // measure reference for measurement on image
 
 // if we search by image ID, we sort (imageIDs, imageIdx) by imageIDs to get a sorted
@@ -28,4 +33,11 @@
 # endif
 
+// MeasureToImage was 'bin'
+// ImageToCatalog was 'clist'
+// ImageToMeasure was 'mlist'
+
+// N_onImage was 'Nlist'
+// N_ONIMAGE was 'NLIST'
+
 Image *getimages (off_t *N, off_t **line_number) {
 
@@ -105,20 +117,20 @@
   off_t i, j;
 
-  ALLOCATE (bin, off_t *, Ncatalog);
+  ALLOCATE (MeasureToImage, IDX_T *, Ncatalog);
   for (i = 0; i < Ncatalog; i++) {
-    ALLOCATE (bin[i], off_t, MAX (catalog[i].Nmeasure, 1));
-    for (j = 0; j < catalog[i].Nmeasure; j++) bin[i][j] = -1;
-  }
-
-  ALLOCATE (Nlist, off_t,   Nimage);
-  ALLOCATE (NLIST, off_t,   Nimage);
-  ALLOCATE (clist, int *,   Nimage);
-  ALLOCATE (mlist, off_t *, Nimage);
+    ALLOCATE (MeasureToImage[i], IDX_T, MAX (catalog[i].Nmeasure, 1));
+    for (j = 0; j < catalog[i].Nmeasure; j++) MeasureToImage[i][j] = -1;
+  }
+
+  ALLOCATE (N_onImage, off_t,   Nimage);
+  ALLOCATE (N_ONIMAGE, off_t,   Nimage);
+  ALLOCATE (ImageToCatalog, IDX_T *,   Nimage);
+  ALLOCATE (ImageToMeasure, IDX_T *, Nimage);
 
   for (i = 0; i < Nimage; i++) {
-    Nlist[i] =   0;
-    NLIST[i] = 100;
-    clist[i] = NULL;  // we allocate these iff they are needed in matchImage
-    mlist[i] = NULL;  // we allocate these iff they are needed in matchImage
+    N_onImage[i] =   0;
+    N_ONIMAGE[i] =  30;
+    ImageToCatalog[i] = NULL;  // we allocate these iff they are needed in matchImage
+    ImageToMeasure[i] = NULL;  // we allocate these iff they are needed in matchImage
   }
 
@@ -141,15 +153,15 @@
 
   for (i = 0; i < Ncatalog; i++) {
-    free (bin[i]);
-  }
-  free (bin);
+    free (MeasureToImage[i]);
+  }
+  free (MeasureToImage);
   for (i = 0; i < Nimage; i++) {
-    if (clist[i]) { free (clist[i]); }
-    if (mlist[i]) { free (mlist[i]); }
-  }
-  free (clist);
-  free (mlist);
-  free (Nlist);
-  free (NLIST);
+    if (ImageToCatalog[i]) { free (ImageToCatalog[i]); }
+    if (ImageToMeasure[i]) { free (ImageToMeasure[i]); }
+  }
+  free (ImageToCatalog);
+  free (ImageToMeasure);
+  free (N_onImage);
+  free (N_ONIMAGE);
 }
 
@@ -171,5 +183,5 @@
   for (i = 0; VERBOSE2 && (i < Nimage); i++) {
     name = GetPhotcodeNamebyCode (image[i].photcode);
-    fprintf (stderr, "image "OFF_T_FMT" has "OFF_T_FMT" measures (%s, %s)\n",  i,  Nlist[i],
+    fprintf (stderr, "image "OFF_T_FMT" has "OFF_T_FMT" measures (%s, %s)\n",  i,  N_onImage[i],
              ohana_sec_to_date(image[i].tzero), name);
   }
@@ -194,25 +206,25 @@
 
   // index for (catalog, measure) -> image
-  bin[cat][meas] = idx;
+  MeasureToImage[cat][meas] = idx;
 
   // if we need to allocate an image index table, do so here
-  if (!clist[idx]) {
-      ALLOCATE (clist[idx], int, NLIST[idx]);
-  }
-  if (!mlist[idx]) {
-      ALLOCATE (mlist[idx], off_t, NLIST[idx]);
+  if (!ImageToCatalog[idx]) {
+      ALLOCATE (ImageToCatalog[idx], IDX_T, N_ONIMAGE[idx]);
+  }
+  if (!ImageToMeasure[idx]) {
+      ALLOCATE (ImageToMeasure[idx], IDX_T, N_ONIMAGE[idx]);
   }
 
   // index for image, Nentry -> catalog
-  clist[idx][Nlist[idx]] = cat;
+  ImageToCatalog[idx][N_onImage[idx]] = cat;
 
   // index for image, Nentry -> measure
-  mlist[idx][Nlist[idx]] = meas;
-  Nlist[idx] ++;
-
-  if (Nlist[idx] == NLIST[idx]) {
-    NLIST[idx] += 100;
-    REALLOCATE (clist[idx], int,   NLIST[idx]);
-    REALLOCATE (mlist[idx], off_t, NLIST[idx]);
+  ImageToMeasure[idx][N_onImage[idx]] = meas;
+  N_onImage[idx] ++;
+
+  if (N_onImage[idx] == N_ONIMAGE[idx]) {
+    N_ONIMAGE[idx] += 30;
+    REALLOCATE (ImageToCatalog[idx], IDX_T, N_ONIMAGE[idx]);
+    REALLOCATE (ImageToMeasure[idx], IDX_T, N_ONIMAGE[idx]);
   }
 
@@ -255,17 +267,17 @@
 
     // index for (catalog, measure) -> image
-    bin[cat][meas] = i;
+    MeasureToImage[cat][meas] = i;
 
     // index for image, Nentry -> catalog
-    clist[i][Nlist[i]] = cat;
+    ImageToCatalog[i][N_onImage[i]] = cat;
 
     // index for image, Nentry -> measure
-    mlist[i][Nlist[i]] = meas;
-    Nlist[i] ++;
-
-    if (Nlist[i] == NLIST[i]) {
-      NLIST[i] += 100;
-      REALLOCATE (clist[i], int,   NLIST[i]);
-      REALLOCATE (mlist[i], off_t, NLIST[i]);
+    ImageToMeasure[i][N_onImage[i]] = meas;
+    N_onImage[i] ++;
+
+    if (N_onImage[i] == N_ONIMAGE[i]) {
+      N_ONIMAGE[i] += 30;
+      REALLOCATE (ImageToCatalog[i], IDX_T, N_ONIMAGE[i]);
+      REALLOCATE (ImageToMeasure[i], IDX_T, N_ONIMAGE[i]);
     }
     return;
@@ -281,5 +293,5 @@
   off_t i;
 
-  i = bin[cat][meas];
+  i = MeasureToImage[cat][meas];
   if (i == -1) return (NULL);
   return (&image[i].coords);
@@ -402,7 +414,7 @@
   }      
 
-  for (i = 0; i < Nlist[im]; i++) {
-    m = mlist[im][i];
-    c = clist[im][i];
+  for (i = 0; i < N_onImage[im]; i++) {
+    m = ImageToMeasure[im][i];
+    c = ImageToCatalog[im][i];
 
     Measure *measure = &catalog[c].measure[m];
@@ -512,7 +524,7 @@
   imcoords = &image[im].coords;
 
-  for (i = 0; i < Nlist[im]; i++) {
-    m = mlist[im][i];
-    c = clist[im][i];
+  for (i = 0; i < N_onImage[im]; i++) {
+    m = ImageToMeasure[im][i];
+    c = ImageToCatalog[im][i];
 
     // XXX unclear if this should use Measure or MeasureTiny; it is only called by FixProblemImages, which has not been updated
@@ -575,9 +587,9 @@
   }
 
-  ALLOCATE (raw, StarData, Nlist[im]);
-
-  for (i = 0; i < Nlist[im]; i++) {
-    m = mlist[im][i];
-    c = clist[im][i];
+  ALLOCATE (raw, StarData, N_onImage[im]);
+
+  for (i = 0; i < N_onImage[im]; i++) {
+    m = ImageToMeasure[im][i];
+    c = ImageToCatalog[im][i];
 
     MeasureTiny *measure = &catalog[c].measureT[m];
@@ -623,5 +635,5 @@
   }
 
-  *Nstars = Nlist[im];
+  *Nstars = N_onImage[im];
   return (raw);
 }
@@ -649,9 +661,9 @@
   }
 
-  ALLOCATE (ref, StarData, Nlist[im]);
-
-  for (i = 0; i < Nlist[im]; i++) {
-    m = mlist[im][i];
-    c = clist[im][i];
+  ALLOCATE (ref, StarData, N_onImage[im]);
+
+  for (i = 0; i < N_onImage[im]; i++) {
+    m = ImageToMeasure[im][i];
+    c = ImageToCatalog[im][i];
 
     MeasureTiny *measure = &catalog[c].measureT[m];
@@ -687,5 +699,5 @@
   }
 
-  *Nstars = Nlist[im];
+  *Nstars = N_onImage[im];
   return (ref);
 }
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/load_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/load_catalogs.c	(revision 33514)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relastro/src/load_catalogs.c	(revision 33515)
@@ -82,8 +82,12 @@
   }
 
-  // XXX keep this test?
   Nstar = 0;
   for (i = 0; i < Ncat; i++) {
     Nstar += catalog[i].Naverage;
+    if ((sizeof(IDX_T) == 8) && (catalog[i].Nmeasure > 0xffffffff)) {
+      fprintf (stderr, "ERROR: using small-sized IDX_T on data with more than 2G detections per table will cause errors\n");
+      fprintf (stderr, "  If you need to do this, and you can afford the RAM, rebuild with IDX_T set to off_t (see relastro.h, ImagesOps.c)\n");
+      exit (3);
+    }
   }
   if (Nstar < 2) { 
