Index: /trunk/Ohana/src/relphot/Makefile
===================================================================
--- /trunk/Ohana/src/relphot/Makefile	(revision 39480)
+++ /trunk/Ohana/src/relphot/Makefile	(revision 39481)
@@ -32,4 +32,8 @@
 $(SRC)/Shutdown.$(ARCH).o 	 \
 $(SRC)/StarOps.$(ARCH).o	 \
+$(SRC)/WarpImageMaps.$(ARCH).o	 \
+$(SRC)/RepairWarpMeasures.$(ARCH).o	 \
+$(SRC)/myIndex.$(ARCH).o	 \
+$(SRC)/psps_ids.$(ARCH).o	 \
 $(SRC)/args.$(ARCH).o		 \
 $(SRC)/help.$(ARCH).o		 \
@@ -83,4 +87,8 @@
 $(SRC)/Shutdown.$(ARCH).o 	 \
 $(SRC)/StarOps.$(ARCH).o	 \
+$(SRC)/WarpImageMaps.$(ARCH).o	 \
+$(SRC)/RepairWarpMeasures.$(ARCH).o	 \
+$(SRC)/myIndex.$(ARCH).o	 \
+$(SRC)/psps_ids.$(ARCH).o	 \
 $(SRC)/bcatalog.$(ARCH).o	 \
 $(SRC)/args.$(ARCH).o		 \
Index: /trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- /trunk/Ohana/src/relphot/include/relphot.h	(revision 39480)
+++ /trunk/Ohana/src/relphot/include/relphot.h	(revision 39481)
@@ -11,7 +11,19 @@
 # define ID_SECF_STACK_PRIMARY 0x00004000
 
+# ifndef MAX_INT
+# define MAX_INT 2147483647
+# endif
+
 // choose off_t or int depending on full-scale relphot analysis resources
 // # define IDX_T off_t
 # define IDX_T int 
+
+typedef struct {
+  int minID;
+  int maxID;
+  int *index;
+  int Nindex;
+  int NINDEX;
+} myIndexType;
 
 typedef enum {
@@ -279,4 +291,5 @@
 int    RESET;
 int    RESET_ZEROPTS;
+int    REPAIR_WARPS;
 int    UPDATE;
 int    SAVE_IMAGE_UPDATES;
@@ -418,5 +431,5 @@
 Catalog      *load_catalogs_parallel PROTO((SkyList *sky, int *Ncatalog, char *syncfile));
 
-int           load_images         PROTO((FITS_DB *db, SkyList *skylist, SkyRegion *region));
+int           load_images         PROTO((FITS_DB *db, SkyList *skylist, SkyRegion *region, int unlockImages));
 Image        *select_images       PROTO((SkyList *skylist, Image *timage, off_t Ntimage, char *inSubset, off_t **LineNumber, off_t *Nimage, SkyRegion *region));
 
@@ -475,5 +488,5 @@
 void          wimages             PROTO((void));
 void          write_coords        PROTO((Header *header, Coords *coords));
-int relphot_objects (SkyTable *sky, SkyList *skylist, int hostID, char *hostpath);
+int relphot_objects (SkyList *skylist, int hostID, char *hostpath);
 int relphot_images (SkyList *skylist);
 
@@ -507,4 +520,5 @@
 
 int load_tess (char *treefile);
+void free_tess (void);
 int get_tess_ids (int *tessID, int *projID, int *skycellID, double ra, double dec);
 int TessellationIDsByImageName (int *tessID, int *projID, int *skycellID, char *name);
@@ -532,5 +546,6 @@
 int add_synthetic_mags (AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, off_t *Nmeasure, off_t *Nm);
 
-int relphot_parallel_regions ();
+int relphot_parallel_regions (SkyTable *sky);
+int relphot_parallel_images (SkyTable *sky);
 
 int assign_images (FITS_DB *db, RegionHostTable *regionHosts);
@@ -546,4 +561,5 @@
 int indexCatalogs (Catalog *catalog, int Ncatalog);
 int catID_and_objID_to_seq (int catID, int objID, int *catSeq, off_t *objSeq);
+void freeCatalogIndexes (int Ncatalog);
 
 int check_sync_file (char *filename, int nloop);
@@ -569,7 +585,4 @@
 
 int markObjects (Catalog *catalog, int Ncatalog);
-
-int relphot_parallel_images ();
-int relphot_parallel_regions ();
 
 // in extra.c
@@ -587,5 +600,5 @@
 SynthZeroPoints *SynthZeroPointsGet ();
 
-int relphot_synthphot (int hostID, char *hostpath);
+int relphot_synthphot (SkyList *sky, int hostID, char *hostpath);
 int relphot_synthphot_parallel (SkyList *sky);
 
@@ -598,7 +611,25 @@
 
 void relphot_free (SkyTable *sky, SkyList *skylist);
-void freeImages ();
+void freeImages (int freeImageData);
 void relphot_client_free (SkyTable *sky, SkyList *skylist);
 void BrightCatalogFree (BrightCatalog *bcatalog);
 
+void put_astrom_table (AstromOffsetTable *myTable);
 void free_astrom_table ();
+
+int RepairWarpMeasures (Catalog *catalog);
+
+int FindWarpGroups (void);
+int GetWarpSeq (Image *image, int obstime, unsigned short photcode, double Rave, double Dave, float X, float Y);
+void FreeWarpGroups (void);
+
+myIndexType *myIndexInit ();
+int myIndexFree (myIndexType *myIndex);
+int myIndexUpdateLimits (myIndexType *myIndex, int value);
+int myIndexSetRange (myIndexType *myIndex);
+int myIndexSetEntry (myIndexType *myIndex, int value, int entry);
+int myIndexGetEntry (myIndexType *myIndex, int value);
+
+uint64_t CreatePSPSObjectID(double ra, double dec);
+uint64_t CreatePSPSStackDetectionID(int sourceID, int imageID, int detID);
+uint64_t CreatePSPSDetectionID(double tobs, int ccdid, int detID);
Index: /trunk/Ohana/src/relphot/src/BoundaryTreeOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/BoundaryTreeOps.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/BoundaryTreeOps.c	(revision 39481)
@@ -55,2 +55,9 @@
   return status;
 }
+
+void free_tess () {
+
+  if (!tess) return;
+  TessellationTableFree (tess, Ntess);
+  free (tess);
+}
Index: /trunk/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 39481)
@@ -24,6 +24,6 @@
 
 // we have an array of images: image[ImageIndex] (ImageIndex : 0 < Nimage)
-static off_t        Nimage;   // number of available images
-static Image        *image;   // array of available images  
+static off_t        Nimage = 0;   // number of available images
+static Image        *image = NULL;   // array of available images  
 
 // if we read only a subset of the rows from the Image FITS, LineNumber tells us to which row
@@ -267,7 +267,7 @@
 }
 
-void freeImages () {
-
-  FREE (image);
+void freeImages (int freeImageData) {
+
+  if (freeImageData) FREE (image);
   FREE (LineNumber);
 
Index: /trunk/Ohana/src/relphot/src/ImageTable.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ImageTable.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/ImageTable.c	(revision 39481)
@@ -33,4 +33,8 @@
   }
 
+  // i want to keep the image data, but free everything else
+  db.ftable.buffer = NULL;
+  gfits_db_free (&db);
+
   // XXX do not make the chip match -- we have not loaded the PHU entries (and do not need them here)
   // BuildChipMatch (image, Nimage);
@@ -65,4 +69,6 @@
   dvo_image_unlock (&db);
 
+  gfits_db_free (&db);
+
   return TRUE;
 }
Index: /trunk/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 39481)
@@ -316,4 +316,6 @@
 
 void freeMosaics () {
+  
+
 }
 
Index: /trunk/Ohana/src/relphot/src/args.c
===================================================================
--- /trunk/Ohana/src/relphot/src/args.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/args.c	(revision 39481)
@@ -193,4 +193,10 @@
     remove_argument (N, &argc, argv);
     RESET_ZEROPTS = TRUE;
+  }
+
+  REPAIR_WARPS = FALSE;
+  if ((N = get_argument (argc, argv, "-repair-warps"))) {
+    remove_argument (N, &argc, argv);
+    REPAIR_WARPS = TRUE;
   }
 
@@ -483,4 +489,5 @@
   FREE (photcodes);
   
+  free_tess();
   SkyTableFree (sky);
   SkyListFree(skylist);
@@ -619,4 +626,10 @@
     remove_argument (N, &argc, argv);
     RESET_ZEROPTS = TRUE;
+  }
+
+  REPAIR_WARPS = FALSE;
+  if ((N = get_argument (argc, argv, "-repair-warps"))) {
+    remove_argument (N, &argc, argv);
+    REPAIR_WARPS = TRUE;
   }
 
@@ -762,4 +775,5 @@
   FREE (photcodes);
   
+  free_tess();
   SkyTableFree (sky);
   SkyListFree(skylist);
Index: /trunk/Ohana/src/relphot/src/assign_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/assign_images.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/assign_images.c	(revision 39481)
@@ -32,4 +32,5 @@
     AstromOffsetTableMatchChips (image, Nimage, table);
   }
+  put_astrom_table (table);
 
   if (MOSAIC_ZEROPT) {
Index: /trunk/Ohana/src/relphot/src/indexCatalog.c
===================================================================
--- /trunk/Ohana/src/relphot/src/indexCatalog.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/indexCatalog.c	(revision 39481)
@@ -63,2 +63,12 @@
   return TRUE;
 }
+
+void freeCatalogIndexes (int Ncatalog) {
+
+  for (int i = 0; i < Ncatalog; i++) {
+    FREE (objIDseq[i]);
+  }
+  FREE (catIDseq);
+  FREE (objIDseq);
+  FREE (objIDmax);
+}
Index: /trunk/Ohana/src/relphot/src/launch_region_hosts.c
===================================================================
--- /trunk/Ohana/src/relphot/src/launch_region_hosts.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/launch_region_hosts.c	(revision 39481)
@@ -81,5 +81,8 @@
     fprintf (stderr, "command: %s\n", command);
     
-    if (PARALLEL_REGIONS_MANUAL) continue;
+    if (PARALLEL_REGIONS_MANUAL) { 
+      free (command);
+      continue;
+    }
 
     // launch the job, then wait for it to be done loading catalogs.  force the remote
@@ -100,4 +103,5 @@
     check_sync_file (syncfile, 1);
     free (syncfile);
+    free (command);
   }
 
Index: /trunk/Ohana/src/relphot/src/load_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/load_images.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/load_images.c	(revision 39481)
@@ -5,5 +5,5 @@
 // This function generates a subset of the images based on selections.  Input db has already
 // been loaded with the raw fits table data
-int load_images (FITS_DB *db, SkyList *skylist, SkyRegion *region) {
+int load_images (FITS_DB *db, SkyList *skylist, SkyRegion *region, int unlockImages) {
 
   Image     *image, *subset;
@@ -26,15 +26,4 @@
   MARKTIME("build chip match for %d images: %f sec\n", (int) Nimage, dtime);
 
-  // XXX consider allowing relphot to skip the AstroMap load (do we need precise astrometry here?)
-
-  char mapfile[DVO_MAX_PATH];
-  snprintf (mapfile, DVO_MAX_PATH, "%s/AstroMap.fits", CATDIR);
-  table = AstromOffsetMapLoad (mapfile, 100000, VERBOSE);
-
-  // assign images.coords.offsetMap -> table->map[i]
-  if (table) {
-    AstromOffsetTableMatchChips (image, Nimage, table);
-  }
-
   // allocate and init an array to identify the images included in the subset
   ALLOCATE (inSubset, char, Nimage);
@@ -49,4 +38,30 @@
     fprintf (stderr, "no images selected for analysis : problem with region or photcode?\n");
     exit (4);
+  }
+
+  // XXX consider allowing relphot to skip the AstroMap load (do we need precise astrometry here?)
+
+  // NOTE: unlock & free when running relphot_client (update-objects), but not when running relphot_images
+  // note that if we have MOSAIC_ZEROPT selected, we cannot free images as the full array is needed by initMosaic
+
+  /* unlock, if we can (else, unlocked below) and free, if we can */
+  if (unlockImages && !MOSAIC_ZEROPT) { 
+    if (subset != image) {
+      // if we have generated an image subset and we are running UPDATE_OFFSETS, the we can free images here
+      free (image);
+      db[0].ftable.buffer = NULL;
+      BuildChipMatch (subset, Nsubset);
+    }
+    dvo_image_unlock (db); 
+    // allocate and init an array to identify the images included in the subset
+  }
+
+  char mapfile[DVO_MAX_PATH];
+  snprintf (mapfile, DVO_MAX_PATH, "%s/AstroMap.fits", CATDIR);
+  table = AstromOffsetMapLoad (mapfile, 100000, VERBOSE);
+
+  // assign images.coords.offsetMap -> table->map[i]
+  if (table) {
+    AstromOffsetTableMatchChips (subset, Nsubset, table);
   }
 
@@ -79,4 +94,8 @@
   }
   return (TRUE);
+}
+
+void put_astrom_table (AstromOffsetTable *myTable) {
+  table = myTable;
 }
 
Index: /trunk/Ohana/src/relphot/src/reload_catalogs.c
===================================================================
--- /trunk/Ohana/src/relphot/src/reload_catalogs.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/reload_catalogs.c	(revision 39481)
@@ -67,5 +67,4 @@
       fprintf (stderr, "ERROR: failure reading catalog %s, skipping\n", catalog.filename);
       continue;
-      // exit (1);
     }
     if (VERBOSE && (catalog.Naverage_disk == 0)) {
@@ -152,11 +151,4 @@
     }
     free (image);
-//  } else {
-//    // load the current sky table (layout of all SkyRegions) 
-//    SkyTable *skytable = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
-//    // XXX kind of hackish...
-//    ALLOCATE (sky, SkyList, 1);
-//    strcpy (sky->hosts, skytable->hosts);
-//    SkyTableFree (skytable);
   }
 
Index: /trunk/Ohana/src/relphot/src/relphot.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/relphot.c	(revision 39481)
@@ -8,4 +8,6 @@
   if (!mode) exit (2);
 
+  SkyList *skylist = NULL;
+
   SkyTable *sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, TRUE, -1, VERBOSE);
   if (!sky) {
@@ -15,15 +17,17 @@
   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);
+  if ((mode != PARALLEL_REGIONS) && (mode != PARALLEL_IMAGES)) {
+    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);
+    }
   }
   
@@ -37,6 +41,5 @@
     case UPDATE_AVERAGES:
       // take the current set of detections and set the mean magnitudes
-      // DOES NOT LOAD THE IMAGE TABLE
-      relphot_objects (sky, skylist, 0, NULL);
+      relphot_objects (skylist, 0, NULL);
       relphot_free (sky, skylist);
       exit (0);
@@ -44,5 +47,5 @@
     case SYNTH_PHOT:
       // set the mean magnitudes ONLY for SYNPHOT objects
-      relphot_synthphot (0, NULL);
+      relphot_synthphot (skylist, 0, NULL);
       relphot_free (sky, skylist);
       exit (0);
@@ -50,5 +53,5 @@
     case PARALLEL_REGIONS:
       // run image updates in parallel across multiple remote machines
-      relphot_parallel_regions ();
+      relphot_parallel_regions (sky);
       relphot_free (sky, skylist);
       exit (0);
@@ -56,5 +59,5 @@
     case PARALLEL_IMAGES:
       // operation on the remote machines in the PARALLEL_REGION mode
-      relphot_parallel_images ();
+      relphot_parallel_images (sky);
       relphot_free (sky, skylist);
       exit (0);
Index: /trunk/Ohana/src/relphot/src/relphot_client.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_client.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/relphot_client.c	(revision 39481)
@@ -83,5 +83,5 @@
       FlatCorrectionTable *flatcorr = NULL;
       reload_catalogs (skylist, flatcorr, HOST_ID, HOSTDIR);
-      freeImages ();
+      freeImages (TRUE);
       free (image);
       client_logger_message ("updated catalogs\n");
@@ -91,5 +91,6 @@
 
     case MODE_UPDATE_OBJECTS: {
-      relphot_objects (sky, skylist, HOST_ID, HOSTDIR);
+      // take the current set of detections and set the mean magnitudes
+      relphot_objects (skylist, HOST_ID, HOSTDIR);
       relphot_client_free (sky, skylist);
       client_logger_message ("updated objects\n");
@@ -99,5 +100,5 @@
     case MODE_SYNTH_PHOT:
       // set the mean magnitudes ONLY for SYNPHOT objects
-      relphot_synthphot (HOST_ID, HOSTDIR);
+      relphot_synthphot (skylist, HOST_ID, HOSTDIR);
       client_logger_message ("set synth photometry\n");
       break;
Index: /trunk/Ohana/src/relphot/src/relphot_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_images.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/relphot_images.c	(revision 39481)
@@ -39,5 +39,5 @@
 
   /* load regions and images based on specified sky patch and/or catalog */
-  load_images (&db, skylist, &UserPatch);
+  load_images (&db, skylist, &UserPatch, FALSE);
   MARKTIME("-- load images: %f sec\n", dtime);
 
@@ -261,5 +261,5 @@
   dvo_image_unlock (&db); 
   gfits_db_free (&db);
-  freeImages();
+  freeImages (TRUE);
 
   return TRUE;
Index: /trunk/Ohana/src/relphot/src/relphot_objects.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 39481)
@@ -8,8 +8,9 @@
 int relphot_objects_parallel (SkyList *sky);
 
-int relphot_objects (SkyTable *sky, SkyList *skylist, int hostID, char *hostpath) {
+int relphot_objects (SkyList *skylist, int hostID, char *hostpath) {
 
   off_t i, j, k;
   int Nsecfilt;
+  struct stat filestat;
 
   Catalog catalog;
@@ -21,6 +22,23 @@
   }
 
+  FITS_DB db;
+      
+  set_db (&db);
+  gfits_db_init (&db);
+
+  /* lock and load the image db table */
+  int status = dvo_image_lock (&db, ImageCat, 60.0, LCK_SOFT);
+  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
+  if (db.dbstate == LCK_EMPTY) Shutdown ("ERROR: No images in catalog %s (1)", db.filename);
+  if (!dvo_image_load (&db, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", db.filename);
+  // the raw FITS data is freed by dvo_image_load, leaving only the Image structure data
+
+  /* load regions and images based on specified sky patch (default depth) */
+  load_images (&db, skylist, &UserPatch, TRUE);
+
   // load the ZP corrections here
   if (SYNTH_ZERO_POINTS) SynthZeroPointsLoad (SYNTH_ZERO_POINTS);
+
+  if (REPAIR_WARPS) FindWarpGroups ();
 
   // load data from each region file, only use bright stars
@@ -36,10 +54,18 @@
     dvo_catalog_init (&catalog, TRUE);
     catalog.filename    = hostID ? hostfile : skylist[0].filename[i];
+
+    // only update existing db tables
+    int status = stat (catalog.filename, &filestat);
+    if ((status == -1) && (errno == ENOENT)) {
+      if (VERBOSE) fprintf (stderr, "no file %s, skipping\n", catalog.filename);
+      continue;
+    }
+
     catalog.catflags    = DVO_LOAD_AVERAGE | DVO_LOAD_MEASURE | DVO_LOAD_MISSING | DVO_LOAD_SECFILT;
     catalog.Nsecfilt    = GetPhotcodeNsecfilt ();
 
     if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "w")) {
-      fprintf (stderr, "ERROR: failure reading catalog %s\n", catalog.filename);
-      exit (1);
+      fprintf (stderr, "ERROR: failure reading catalog %s, skipping\n", catalog.filename);
+      continue;
     }
     if (!catalog.Naverage_disk) {
@@ -80,7 +106,19 @@
     populate_tiny_values(&catalog, DVO_TV_MEASURE | DVO_TV_AVERAGE);
 
+    // XXX need to worry about the image subset data
+    initImageBins  (&catalog, 1, FALSE);
+    initMosaicBins (&catalog, 1, FALSE);
+    initGridBins   (&catalog, 1); 
+
+    findImages (&catalog, 1, FALSE);
+    findMosaics (&catalog, 1, FALSE);
+
+    // update the detection coordinates using the new image parameters
+    if (REPAIR_WARPS) RepairWarpMeasures (&catalog);
+
     initMrel (&catalog, 1);
     setMrelFinal (&catalog, NULL, TRUE);
     // XXX if we want to have options for setting warp, chip, stack independently, we need to init only the desired ones
+    // NOTE flatcorr == NULL, but it should have been applied already by setphot
 
     if (!UPDATE) {
@@ -93,4 +131,13 @@
     if (VERBOSE) fprintf (stderr, "saving catalog %s\n", catalog.filename);
     
+    char history[128];
+    struct timeval now;
+    gettimeofday (&now, (void *) NULL);
+    char *moddate = ohana_sec_to_date (now.tv_sec);
+    gfits_modify (&catalog.header, "RELPHOT", "%s", 1, moddate);      
+    snprintf (history, 128, "repair warp measure.imageID: %s", moddate);
+    gfits_modify_alt (&catalog.header, "HISTORY", "%S", 0, history); // adds a new entry
+    free (moddate);
+
     // we can optionally convert output format here
     // but it would be better to define a dvo crawler program to do this
@@ -100,7 +147,18 @@
     if (!dvo_catalog_unlock (&catalog)) { fprintf (stderr, "ERROR: failed to unlock %s\n", catalog.filename); exit (1); }
     SetProtect (FALSE);
+
     free_tiny_values(&catalog);
     dvo_catalog_free (&catalog);
-  }
+
+    freeImageBins (1, FALSE);
+    freeMosaicBins (1, FALSE);
+    freeGridBins (1);
+  }
+
+  if (REPAIR_WARPS) FreeWarpGroups ();
+
+  gfits_db_free (&db);
+  freeImages(TRUE);
+
   return (TRUE);
 }
@@ -118,4 +176,10 @@
     exit (1);
   }    
+
+  if (BOUNDARY_TREE) {
+    char *tmppath = abspath(BOUNDARY_TREE, DVO_MAX_PATH);
+    free (BOUNDARY_TREE);
+    BOUNDARY_TREE = tmppath;
+  }
 
   int i;
@@ -147,6 +211,9 @@
     if (RESET)         	   { strextend (&command, "-reset"); }
     if (RESET_ZEROPTS) 	   { strextend (&command, "-reset-zpts"); }
+    if (REPAIR_WARPS)      { strextend (&command, "-repair-warps"); }
+    if (IS_DIFF_DB)        { strextend (&command, "-is-diff-db"); }
     if (UPDATE)        	   { strextend (&command, "-update"); }
     if (!KEEP_UBERCAL) 	   { strextend (&command, "-reset-ubercal"); }
+    if (BOUNDARY_TREE)     { strextend (&command, "-boundary-tree %s", BOUNDARY_TREE); }
     if (SYNTH_ZERO_POINTS) { strextend (&command, "-synthphot-zpts %s", SYNTH_ZERO_POINTS); }
 
Index: /trunk/Ohana/src/relphot/src/relphot_parallel_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_parallel_images.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/relphot_parallel_images.c	(revision 39481)
@@ -12,5 +12,5 @@
 int dumpObjects (char *filename, Catalog *catalog, int Ncatalog);
 
-int relphot_parallel_images () {
+int relphot_parallel_images (SkyTable *sky) {
 
   int i, Ncatalog;
@@ -50,7 +50,4 @@
 # endif
   FlatCorrectionTable *flatcorr = NULL;
-
-  SkyTable *sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
-  SkyTableSetFilenames (sky, CATDIR, "cpt");
 
   // UserPatch.Rmin,Rmax may have range from a few degrees < 0.0 to few degrees > 360.0.
@@ -161,6 +158,23 @@
 
   share_image_mags (regionHosts, -1);
+
+  for (i = 0; i < Ncatalog; i++) {
+    // these tiny values are set by BrightCatalogSplit from load_catalogs
+    free_tiny_values (&catalog[i]);
+    dvo_catalog_free (&catalog[i]);
+  }
+  free (catalog);
+  
+  freeCatalogIndexes (Ncatalog);
+  freeImageBins(Ncatalog, TRUE);
+  freeMosaicBins (Ncatalog, TRUE);
+  freeGridBins (Ncatalog);
+  freeImages(TRUE);
+
+  SkyListFree(skylist);
+  FreeRegionHostTable (regionHosts);
+
   client_logger_message ("done with parallel images\n");
 
-  exit (0);
+  return TRUE;
 }
Index: /trunk/Ohana/src/relphot/src/relphot_parallel_regions.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_parallel_regions.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/relphot_parallel_regions.c	(revision 39481)
@@ -2,5 +2,5 @@
 int save_images_backup (FITS_DB *db);
 
-int relphot_parallel_regions () {
+int relphot_parallel_regions (SkyTable *sky) {
 
   int status;
@@ -71,7 +71,9 @@
   }
 
-  SkyTable *sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
-  SkyTableSetFilenames (sky, CATDIR, "cpt");
   SkyList *skylist = SkyListByBounds (sky, -1, regionHosts->Rmin, regionHosts->Rmax, regionHosts->Dmin, regionHosts->Dmax);
+  UserPatch.Rmin = regionHosts->Rmin;
+  UserPatch.Rmax = regionHosts->Rmax;
+  UserPatch.Dmin = regionHosts->Dmin;
+  UserPatch.Dmax = regionHosts->Dmax;
 
   // I have to save a copy because dvo_image_save and _unlock swap and free the data
@@ -82,9 +84,15 @@
 
   // save the changes to the image parameters
-  dvo_image_save (&db, VERBOSE);
+  dvo_image_save (&db, VERBOSE); // this function modifies the db.ftable.buffer: do not free stored Image table
   dvo_image_unlock (&db); 
   MARKTIME ("finished relphot -parallel-regions: %f sec total\n", dtime);
 
-  exit (0);
+  gfits_db_free (&db);
+  freeImages(FALSE);
+
+  SkyListFree(skylist);
+  FreeRegionHostTable (regionHosts);
+
+  return TRUE;
 }
 
@@ -122,4 +130,5 @@
   MARKTIME("-- save Image.dat.bck: %f sec\n", dtime);
 
+  gfits_db_free (&dbX);
   return TRUE;
 }
Index: /trunk/Ohana/src/relphot/src/relphot_synthphot.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_synthphot.c	(revision 39480)
+++ /trunk/Ohana/src/relphot/src/relphot_synthphot.c	(revision 39481)
@@ -6,18 +6,9 @@
 int relphot_synthphot_parallel (SkyList *sky);
 
-int relphot_synthphot (int hostID, char *hostpath) {
+int relphot_synthphot (SkyList *skylist, int hostID, char *hostpath) {
 
   off_t i;
 
-  SkyTable *sky = NULL;
-  SkyList *skylist = NULL;
   Catalog catalog;
-
-  // load the current sky table (layout of all SkyRegions) 
-  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
-  SkyTableSetFilenames (sky, CATDIR, "cpt");
-  
-  // determine the populated SkyRegions overlapping the requested area (default depth)
-  skylist = SkyListByPatch (sky, -1, &UserPatch);
 
   // XXX need to decide how to determine PARALLEL mode...
