Index: /trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- /trunk/Ohana/src/relphot/include/relphot.h	(revision 39478)
+++ /trunk/Ohana/src/relphot/include/relphot.h	(revision 39479)
@@ -418,5 +418,5 @@
 Catalog      *load_catalogs_parallel PROTO((SkyList *sky, int *Ncatalog, char *syncfile));
 
-SkyList      *load_images         PROTO((FITS_DB *db, SkyTable *sky, char *regionName, SkyRegion *region));
+int           load_images         PROTO((FITS_DB *db, SkyList *skylist, SkyRegion *region));
 Image        *select_images       PROTO((SkyList *skylist, Image *timage, off_t Ntimage, char *inSubset, off_t **LineNumber, off_t *Nimage, SkyRegion *region));
 
@@ -476,5 +476,5 @@
 void          write_coords        PROTO((Header *header, Coords *coords));
 int relphot_objects (SkyTable *sky, SkyList *skylist, int hostID, char *hostpath);
-int relphot_images (void);
+int relphot_images (SkyList *skylist);
 
 void relphot_usage (void);
Index: /trunk/Ohana/src/relphot/src/load_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/load_images.c	(revision 39478)
+++ /trunk/Ohana/src/relphot/src/load_images.c	(revision 39479)
@@ -5,12 +5,10 @@
 // This function generates a subset of the images based on selections.  Input db has already
 // been loaded with the raw fits table data
-SkyList *load_images (FITS_DB *db, SkyTable *sky, char *regionName, SkyRegion *region) {
+int load_images (FITS_DB *db, SkyList *skylist, SkyRegion *region) {
 
   Image     *image, *subset;
-  off_t      Nimage, Nsubset, Nchar;
+  off_t      Nimage, Nsubset;
   off_t     *LineNumber;
-  char *inSubset;
-
-  SkyList *skylist = NULL;
+  char      *inSubset;
 
   INITTIME;
@@ -43,66 +41,8 @@
   memset (inSubset, 0, Nimage);
 
-  // determine the populated SkyRegions overlapping the requested area
-
-  // if the user selects a specific catalog, do that one; 
-  // otherwise limit to the selection region
-  if (regionName) {
-    Nchar = strlen(regionName);
-    if (!strcmp (&regionName[Nchar-4], ".cpt")) regionName[Nchar-4] = 0;
-    skylist = SkyListByName (sky, regionName);
-
-    // select the images which overlap the selected sky regions
-    // 'subset' points to a new copy of the data (different from 'image')
-    subset = select_images (skylist, image, Nimage, inSubset, &LineNumber, &Nsubset, NULL);
-    MARKTIME("selected %d overlapping images: %f sec\n", (int) Nsubset, dtime);
-  } else {
-    if (region[0].Rmin > region[0].Rmax) {
-      SkyRegion subregion;
-      subregion = *region;
-      subregion.Rmin = 0.0; subregion.Rmax = region[0].Rmax;
-      skylist = SkyListByPatch (sky, -1, &subregion);
-
-      // select the images which overlap the selected sky regions
-      // 'subset' points to a new copy of the data (different from 'image')
-      subset = select_images (skylist, image, Nimage, inSubset, &LineNumber, &Nsubset, &subregion);
-      MARKTIME("selected %d overlapping images: %f sec\n", (int) Nsubset, dtime);
-
-      Image *subsetExtra;
-      off_t NsubsetExtra;
-      off_t *LineNumberExtra, i;
-
-      subregion.Rmin = region[0].Rmin; subregion.Rmax = 360.0; 
-      SkyList *extraList = SkyListByPatch (sky, -1, &subregion);
-
-      subsetExtra = select_images (extraList, image, Nimage, inSubset, &LineNumberExtra, &NsubsetExtra, &subregion);
-      MARKTIME("selected %d overlapping images: %f sec\n", (int) Nsubset, dtime);
-
-      REALLOCATE (subset, Image, MAX (Nsubset + NsubsetExtra, 1));
-      REALLOCATE (LineNumber, off_t, MAX (Nsubset + NsubsetExtra, 1));
-      
-      for (i = 0; i < NsubsetExtra; i++) {
-	subset[i+Nsubset] = subsetExtra[i];
-	LineNumber[i+Nsubset] = LineNumberExtra[i];
-      }
-      Nsubset += NsubsetExtra;
-      MARKTIME("selected %d overlapping images: %f sec\n", (int) NsubsetExtra, dtime);
-    } else {
-      skylist = SkyListByPatch (sky, -1, region);
-
-      // select the images which overlap the selected sky regions
-      // 'subset' points to a new copy of the data (different from 'image')
-      subset = select_images (skylist, image, Nimage, inSubset, &LineNumber, &Nsubset, region);
-      MARKTIME("selected %d overlapping images: %f sec\n", (int) Nsubset, dtime);
-    }
-  } 
-
-# if (0)
-  FILE *ftest = fopen ("skydump.dat", "w");
-  int i;
-  for (i = 0; i < skylist[0].Nregions; i++) {
-    fprintf (ftest, "%s : %f %f : %f %f\n", skylist[0].regions[i][0].name, skylist[0].regions[i][0].Rmin, skylist[0].regions[i][0].Rmax, skylist[0].regions[i][0].Dmin, skylist[0].regions[i][0].Dmax);
-  }
-  fclose (ftest);
-# endif
+  // select the images which overlap the selected sky regions
+  // 'subset' points to a new copy of the data (different from 'image')
+  subset = select_images (skylist, image, Nimage, inSubset, &LineNumber, &Nsubset, region);
+  MARKTIME("selected %d overlapping images: %f sec\n", (int) Nsubset, dtime);
 
   if (Nimage == 0) {
@@ -120,5 +60,5 @@
   MARKTIME("init images: %f sec\n", dtime);
 
-  return (skylist);
+  return TRUE;
 }
 
@@ -146,2 +86,43 @@
   free (table);
 }
+
+
+
+// *** old example code ***
+  // determine the populated SkyRegions overlapping the requested area
+# if (0)    
+  // XXX note : this chunk selects catalogs for a region across the 0,360 boundary (e.g., 350 10)
+  // it has largely been obviated, but I'm keeping the code in case I need to reinstate it.
+  if (region[0].Rmin > region[0].Rmax) {
+    SkyRegion subregion;
+    subregion = *region;
+    subregion.Rmin = 0.0; subregion.Rmax = region[0].Rmax;
+    skylist = SkyListByPatch (sky, -1, &subregion);
+
+    // select the images which overlap the selected sky regions
+    // 'subset' points to a new copy of the data (different from 'image')
+    subset = select_images (skylist, image, Nimage, inSubset, &LineNumber, &Nsubset, &subregion);
+    MARKTIME("selected %d overlapping images: %f sec\n", (int) Nsubset, dtime);
+
+    Image *subsetExtra;
+    off_t NsubsetExtra;
+    off_t *LineNumberExtra, i;
+
+    subregion.Rmin = region[0].Rmin; subregion.Rmax = 360.0; 
+    SkyList *extraList = SkyListByPatch (sky, -1, &subregion);
+
+    subsetExtra = select_images (extraList, image, Nimage, inSubset, &LineNumberExtra, &NsubsetExtra, &subregion);
+    MARKTIME("selected %d overlapping images: %f sec\n", (int) Nsubset, dtime);
+
+    REALLOCATE (subset, Image, MAX (Nsubset + NsubsetExtra, 1));
+    REALLOCATE (LineNumber, off_t, MAX (Nsubset + NsubsetExtra, 1));
+      
+    for (i = 0; i < NsubsetExtra; i++) {
+      subset[i+Nsubset] = subsetExtra[i];
+      LineNumber[i+Nsubset] = LineNumberExtra[i];
+    }
+    Nsubset += NsubsetExtra;
+    MARKTIME("selected %d overlapping images: %f sec\n", (int) NsubsetExtra, dtime);
+  } 
+# endif
+
Index: /trunk/Ohana/src/relphot/src/relphot.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot.c	(revision 39478)
+++ /trunk/Ohana/src/relphot/src/relphot.c	(revision 39479)
@@ -2,7 +2,4 @@
 
 int main (int argc, char **argv) {
-
-  SkyTable *sky = NULL;
-  SkyList *skylist = NULL;
 
   // get configuration info, args
@@ -11,8 +8,29 @@
   if (!mode) exit (2);
 
+  SkyTable *sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, TRUE, -1, VERBOSE);
+  if (!sky) {
+    fprintf (stderr, "ERROR running loading sky table from %s\n", CATDIR);
+    exit (2);
+  }
+  SkyTableSetFilenames (sky, CATDIR, "cpt");
+  
+  SkyList *skylist = NULL;
+  if (UserCatalog) {
+    int Nchar = strlen(UserCatalog);
+    if (!strcmp (&UserCatalog[Nchar-4], ".cpt")) UserCatalog[Nchar-4] = 0;
+    skylist = SkyListByName (sky, UserCatalog);
+  } else {
+    skylist = SkyListByPatch (sky, -1, &UserPatch);
+  }
+  if (!skylist) {
+    fprintf (stderr, "ERROR setting up skylist for %s\n", CATDIR);
+    exit (2);
+  }
+  
   switch (mode) {
     case UPDATE_IMAGES:
       // IF CALLED WITH NLOOP == 0, DOES NOT LOAD bcatalog, just loads image table and generates ImageSubset.dat
-      relphot_images ();
+      relphot_images (skylist);
+      relphot_free (sky, skylist);
       exit (0);
 
@@ -20,16 +38,4 @@
       // take the current set of detections and set the mean magnitudes
       // DOES NOT LOAD THE IMAGE TABLE
-      sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, TRUE, -1, VERBOSE);
-      if (!sky) {
-	fprintf (stderr, "ERROR running loading sky table from %s\n", CATDIR);
-	exit (2);
-      }
-      SkyTableSetFilenames (sky, CATDIR, "cpt");
-      
-      skylist = SkyListByPatch (sky, -1, &UserPatch);
-      if (!skylist) {
-	fprintf (stderr, "ERROR setting up skylist for %s\n", CATDIR);
-	exit (2);
-      }
       relphot_objects (sky, skylist, 0, NULL);
       relphot_free (sky, skylist);
@@ -39,5 +45,5 @@
       // set the mean magnitudes ONLY for SYNPHOT objects
       relphot_synthphot (0, NULL);
-      relphot_free (NULL, NULL);
+      relphot_free (sky, skylist);
       exit (0);
 
@@ -45,5 +51,5 @@
       // run image updates in parallel across multiple remote machines
       relphot_parallel_regions ();
-      relphot_free (NULL, NULL);
+      relphot_free (sky, skylist);
       exit (0);
 
@@ -51,5 +57,5 @@
       // operation on the remote machines in the PARALLEL_REGION mode
       relphot_parallel_images ();
-      relphot_free (NULL, NULL);
+      relphot_free (sky, skylist);
       exit (0);
 
@@ -60,18 +66,4 @@
 	exit (2);
       }
-
-      sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, TRUE, -1, VERBOSE);
-      if (!sky) {
-	fprintf (stderr, "ERROR running loading sky table from %s\n", CATDIR);
-	exit (2);
-      }
-      SkyTableSetFilenames (sky, CATDIR, "cpt");
-      
-      skylist = SkyListByPatch (sky, -1, &UserPatch);
-      if (!skylist) {
-	fprintf (stderr, "ERROR setting up skylist for %s\n", CATDIR);
-	exit (2);
-      }
-
       reload_catalogs (skylist, NULL, 0, NULL);
       relphot_free (sky, skylist);
Index: /trunk/Ohana/src/relphot/src/relphot_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_images.c	(revision 39478)
+++ /trunk/Ohana/src/relphot/src/relphot_images.c	(revision 39479)
@@ -7,5 +7,5 @@
 // measure.M contains: -2.5*log(DN) + 2.5*log(exptime) + True Zero Point (but exptime and ZP are known)
 
-int relphot_images () {
+int relphot_images (SkyList *skylist) {
 
   int i, status, Ncatalog;
@@ -14,8 +14,4 @@
 
   INITTIME;
-
-  // load the current sky table (layout of all SkyRegions) 
-  SkyTable *sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
-  SkyTableSetFilenames (sky, CATDIR, "cpt");
 
   /* register database handle with shutdown procedure */
@@ -43,5 +39,5 @@
 
   /* load regions and images based on specified sky patch and/or catalog */
-  SkyList *skylist = load_images (&db, sky, UserCatalog, &UserPatch);
+  load_images (&db, skylist, &UserPatch);
   MARKTIME("-- load images: %f sec\n", dtime);
 
@@ -267,6 +263,5 @@
   freeImages();
 
-  relphot_free (sky, skylist);
-  exit (0);
+  return TRUE;
 }
 
