Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/Makefile
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/Makefile	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/Makefile	(revision 36569)
@@ -77,4 +77,5 @@
 $(SRC)/share_images_pos.$(ARCH).o \
 $(SRC)/ImagePosIO.$(ARCH).o \
+$(SRC)/ImageTable.$(ARCH).o \
 $(SRC)/markObjects.$(ARCH).o \
 $(SRC)/indexCatalogs.$(ARCH).o \
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/include/relastro.h
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/include/relastro.h	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/include/relastro.h	(revision 36569)
@@ -6,4 +6,13 @@
 # include <pthread.h>
 
+# define MARKTIME(MSG,...) {			\
+    gettimeofday (&stopTimer, (void *) NULL);	\
+    float dtime = DTIME (stopTimer, startTimer);	\
+    fprintf (stderr, MSG, __VA_ARGS__); }
+
+# define INITTIME \
+  struct timeval startTimer, stopTimer; \
+  gettimeofday (&startTimer, (void *) NULL);
+
 // choose off_t or int depending on full-scale relphot analysis resources
 // # define IDX_T off_t
@@ -20,5 +29,5 @@
 typedef enum {FIT_NONE, FIT_AVERAGE, FIT_PM_ONLY, FIT_PAR_ONLY, FIT_PM_AND_PAR} FitMode;
 
-typedef enum {TARGET_NONE, TARGET_SIMPLE, TARGET_CHIPS, TARGET_MOSAICS, TARGET_HIGH_SPEED, TARGET_MERGE_SOURCE, TARGET_UPDATE_OBJECTS, TARGET_UPDATE_OFFSETS, TARGET_LOAD_OBJECTS, TARGET_HPM} FitTarget;
+typedef enum {TARGET_NONE, TARGET_SIMPLE, TARGET_CHIPS, TARGET_MOSAICS, TARGET_HIGH_SPEED, TARGET_MERGE_SOURCE, TARGET_UPDATE_OBJECTS, TARGET_UPDATE_OFFSETS, TARGET_LOAD_OBJECTS, TARGET_HPM, TARGET_PARALLEL_REGIONS, TARGET_PARALLEL_IMAGES} FitTarget;
 
 typedef enum {
@@ -129,10 +138,4 @@
 } StatType;
 
-# define MARKTIME(MSG,...) { \
-  float dtime; \
-  gettimeofday (&stop, (void *) NULL); \
-  dtime = DTIME (stop, start); \
-  fprintf (stderr, MSG, __VA_ARGS__); }
-
 /* global variables set in parameter file */
 # define DVO_MAX_PATH 1024
@@ -145,4 +148,10 @@
 char   SKY_TABLE[DVO_MAX_PATH];
 int    SKY_DEPTH;  /** XXX EAM : depth of catalog tables, fix usage */
+
+// globals for parallel region operations
+char  *REGION_FILE;
+char  *IMAGE_TABLE;
+int    REGION_HOST_ID;
+int    PARALLEL_REGIONS_MANUAL;
 
 int          HOST_ID;
@@ -486,5 +495,5 @@
 int share_mean_pos (Catalog *catalog, int Ncatalog, RegionHostTable *regionHosts, int nloop);
 int slurp_mean_pos (Catalog *catalog, int Ncatalog, RegionHostTable *regionHosts, int nloop);
-int set_mean_pos (MeanPos *meanpos, AverageTiny *average, SecFilt *secfilt, int Nsec);
+int set_mean_pos (MeanPos *meanpos, AverageTiny *average);
 MeanPos *merge_mean_pos (MeanPos *target, int *ntarget, MeanPos *source, int Nsource);
 
@@ -505,2 +514,4 @@
 int set_image_pos (ImagePos *image_pos, Image *image);
 
+Image *ImageTableLoad(char *filename, off_t *nimage);
+int ImageTableSave (char *filename, Image *images, off_t Nimages);
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/ImageTable.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/ImageTable.c	(revision 36569)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/ImageTable.c	(revision 36569)
@@ -0,0 +1,62 @@
+# include "relastro.h"
+
+Image *ImageTableLoad(char *filename, off_t *nimage) {
+
+  int status;
+  off_t Nimage;
+  FITS_DB db;
+
+  db.mode   = dvo_catalog_catmode (CATMODE);
+  db.format = dvo_catalog_catformat (CATFORMAT);
+  
+  /* lock and load the image db table */
+  status = dvo_image_lock (&db, filename, 60.0, LCK_SOFT);
+  if (!status) {
+    Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
+  }
+
+  // if the file is missing, db.dbstate will have a value of either:
+  // LCK_EMPTY (if UPDATE) or LCK_MISSING (if !UPDATE)
+  if ((db.dbstate == LCK_EMPTY) || (db.dbstate == LCK_MISSING)) {
+    Shutdown ("ERROR: database %s contains no image data", CATDIR);
+  }
+
+  // read data from Image.dat file
+  if (!dvo_image_load (&db, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", db.filename);
+
+  // convert database table to internal structure (binary to Image)
+  // 'image' points to the same memory as db->ftable->buffer
+  Image *image = gfits_table_get_Image (&db.ftable, &Nimage, &db.swapped);
+  if (!image) {
+    Shutdown ("ERROR: failed to read images");
+  }
+
+  *nimage = Nimage;
+  return image;
+}
+
+int ImageTableSave (char *filename, Image *images, off_t Nimages) {
+
+  int status;
+  FITS_DB db;
+
+  // setup image table format and lock 
+  db.mode   = dvo_catalog_catmode (CATMODE);
+  db.format = dvo_catalog_catformat (CATFORMAT);
+  status    = dvo_image_lock (&db, filename, 60.0, LCK_XCLD);  // shorter timeout?
+  if (!status) Shutdown ("ERROR: failure to lock image catalog %s", db.filename);
+
+  // load or create the image table 
+  if (db.dbstate != LCK_EMPTY) {
+    Shutdown ("image catalog already exists %s", db.filename);
+  }
+
+  dvo_image_create (&db, GetZeroPoint());
+
+  /* add the new images and save */
+  dvo_image_addrows (&db, images, Nimages);
+  dvo_image_update (&db, VERBOSE);
+  dvo_image_unlock (&db);
+
+  return TRUE;
+}
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/StarMaps.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/StarMaps.c	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/StarMaps.c	(revision 36569)
@@ -59,7 +59,6 @@
   off_t i, N, Nimages;
   int xbin, ybin;
-  struct timeval start, stop;
 
-  gettimeofday (&start, (void *) NULL);
+  INITTIME;
 
   // Images *images = getimages(&Nimages, NULL); return value ignored
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/args.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/args.c	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/args.c	(revision 36569)
@@ -49,4 +49,40 @@
     remove_argument (N, &argc, argv);
     FIT_TARGET = TARGET_UPDATE_OFFSETS;
+  }
+
+  // elements needed for parallel regions / parallel images
+  REGION_FILE = NULL;
+  if ((N = get_argument (argc, argv, "-region-hosts"))) {
+    remove_argument (N, &argc, argv);
+    REGION_FILE = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  REGION_HOST_ID = 0;
+  if ((N = get_argument (argc, argv, "-region-hostID"))) {
+    remove_argument (N, &argc, argv);
+    REGION_HOST_ID = atoi (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  IMAGE_TABLE = NULL;
+  if ((N = get_argument (argc, argv, "-parallel-images"))) {
+    remove_argument (N, &argc, argv);
+    FIT_TARGET = TARGET_PARALLEL_IMAGES;
+    if (N >= argc) usage();
+    IMAGE_TABLE = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+    if (!REGION_FILE) usage();
+  }
+
+  PARALLEL_REGIONS_MANUAL = FALSE;
+  if ((N = get_argument (argc, argv, "-parallel-regions"))) {
+    remove_argument (N, &argc, argv);
+    FIT_TARGET = TARGET_PARALLEL_REGIONS;
+    if (!REGION_FILE) usage();
+    if ((N = get_argument (argc, argv, "-parallel-regions-manual"))) {
+      remove_argument (N, &argc, argv);
+      PARALLEL_REGIONS_MANUAL = TRUE;
+    }
   }
 
@@ -436,4 +472,9 @@
 
   BCATALOG = NULL;
+
+  REGION_FILE = NULL;
+  REGION_HOST_ID = 0;
+  IMAGE_TABLE = NULL;
+  PARALLEL_REGIONS_MANUAL = FALSE;
 
   HOST_ID = 0;
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/assign_images.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/assign_images.c	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/assign_images.c	(revision 36569)
@@ -38,5 +38,6 @@
   // supply the mosaics to the image table for the regionHosts : we already have the image
   // <-> mosaic relationship, we just need to select these mosaics (once per regionHost)
-  select_mosaics_hostregion (regionHosts, image, Nimage);
+  // XXX DEFINE ME: select_mosaics_hostregion (regionHosts, image, Nimage);
+  abort ();
 
   return TRUE;
@@ -48,5 +49,4 @@
 int select_images_hostregion (RegionHostTable *regionHosts, Image *image, off_t Nimage) {
 
-  int ecode, found;
   off_t i, j;
 
@@ -61,12 +61,22 @@
   for (j = 0; j < Nimage; j++) {
     
-    /* exclude images by photcode */
-    ecode = GetPhotcodeEquivCodebyCode (image[j].photcode);
-    found = FALSE;
-    int Ns;
-    for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
-      if (ecode == photcodes[Ns][0].code) found = TRUE;
-    }
-    if (!found) continue;
+    /* select images by photcode, or equiv photcode, if specified */
+    if (NphotcodesKeep > 0) {
+      int found = FALSE;
+      // XXX this bit of code excludes DIS mosaics and should be fixed
+      for (i = 0; (i < NphotcodesKeep) && !found; i++) {
+	if (photcodesKeep[i][0].code == image[j].photcode) found = TRUE;
+	if (photcodesKeep[i][0].code == GetPhotcodeEquivCodebyCode(image[j].photcode)) found = TRUE;
+      }
+      if (!found) continue;
+    }
+    if (NphotcodesSkip > 0) {
+      int found = FALSE;
+      for (i = 0; (i < NphotcodesSkip) && !found; i++) {
+	if (photcodesSkip[i][0].code == image[j].photcode) found = TRUE;
+	if (photcodesSkip[i][0].code == GetPhotcodeEquivCodebyCode(image[j].photcode)) found = TRUE;
+      }
+      if (found) continue;
+    }
 
     /* exclude images by time */
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/launch_region_hosts.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/launch_region_hosts.c	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/launch_region_hosts.c	(revision 36569)
@@ -55,16 +55,16 @@
 
     char command[1024];
-    snprintf (command, 1024, "relastro %s -parallel-images %s -region-hosts %s -region-hostID %d -D CATDIR %s -region %f %f %f %f -statmode %s -D CAMERA %s -D STAR_TOOFEW %d -minerror %f", 
-	      PhotcodeList, filename, REGION_FILE, host->hostID, CATDIR, host->RminCat, host->RmaxCat, host->DminCat, host->DmaxCat, STATMODE, CAMERA, STAR_TOOFEW, MIN_ERROR);
+    snprintf (command, 1024, "relastro -parallel-images %s -region-hosts %s -region-hostID %d -D CATDIR %s -region %f %f %f %f -statmode %s -minerror %f", 
+	      filename, REGION_FILE, host->hostID, CATDIR, host->RminCat, host->RmaxCat, host->DminCat, host->DmaxCat, STATMODE, MIN_ERROR);
 
-    if (VERBOSE)       	 strextend (command, "-v");
-    if (VERBOSE2)      	 strextend (command, "-vv");
-    if (RESET)         	 strextend (command, "-reset");
-    if (RESET_ZEROPTS) 	 strextend (command, "-reset-zpts");
-    if (UPDATE)        	 strextend (command, "-update");
-    if (!KEEP_UBERCAL) 	 strextend (command, "-reset-ubercal");
-    if (PARALLEL)      	 strextend (command, "-parallel");
-    if (PARALLEL_MANUAL) strextend (command, "-parallel-manual");
-    if (PARALLEL_SERIAL) strextend (command, "-parallel-serial");
+    if (VERBOSE)       	    strextend (command, "-v");
+    if (VERBOSE2)      	    strextend (command, "-vv");
+    if (RESET)         	    strextend (command, "-reset");
+    if (UPDATE)        	    strextend (command, "-update");
+    if (PARALLEL)      	    strextend (command, "-parallel");
+    if (PARALLEL_MANUAL)    strextend (command, "-parallel-manual");
+    if (PARALLEL_SERIAL)    strextend (command, "-parallel-serial");
+    if (PHOTCODE_KEEP_LIST) strextend (command, "+photcode %s", PHOTCODE_KEEP_LIST); 
+    if (PHOTCODE_SKIP_LIST) strextend (command, "-photcode %s", PHOTCODE_SKIP_LIST);
 
     fprintf (stderr, "command: %s\n", command);
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/load_images.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/load_images.c	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/load_images.c	(revision 36569)
@@ -1,9 +1,3 @@
 # include "relastro.h"
-
-# define MARKTIME(MSG,...) { \
-  float dtime; \
-  gettimeofday (&stop, (void *) NULL); \
-  dtime = DTIME (stop, start); \
-  fprintf (stderr, MSG, __VA_ARGS__); }
 
 int load_images (FITS_DB *db, SkyList *skylist, int UseFullOverlap) {
@@ -12,7 +6,6 @@
   off_t      Nimage, Nsubset;
   off_t     *LineNumber;
-  struct timeval start, stop;
 
-  gettimeofday (&start, (void *) NULL);
+  INITTIME;
 
   // convert database table to internal structure
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/relastro_images.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/relastro_images.c	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/relastro_images.c	(revision 36569)
@@ -6,7 +6,6 @@
   Catalog *catalog;
   FITS_DB db;
-  struct timeval start, stop;
 
-  gettimeofday (&start, (void *) NULL);
+  INITTIME;
 
   /* register database handle with shutdown procedure */
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/resort_catalog.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/resort_catalog.c	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/resort_catalog.c	(revision 36569)
@@ -30,6 +30,5 @@
   if (catalog[0].sorted == TRUE) return;
 
-  // struct timeval start, stop;
-  // gettimeofday (&start, NULL);
+  // INITTIME;
 
   /* internal counters */
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/select_images.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/select_images.c	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/select_images.c	(revision 36569)
@@ -15,10 +15,4 @@
 void dsortindex (double *X, off_t *Y, int N);
 off_t getRegionStartByRA (double R, double *Rref, off_t Nregions);
-
-# define MARKTIME(MSG,...) { \
-  float dtime; \
-  gettimeofday (&stop, (void *) NULL); \
-  dtime = DTIME (stop, start); \
-  fprintf (stderr, MSG, __VA_ARGS__); }
 
 Image *select_images (SkyList *skylist, Image *timage, off_t Ntimage, off_t **LineNumber, off_t *Nimage, int UseFullOverlap) {
@@ -31,5 +25,4 @@
   Coords tcoords;
   SkyRegionCoords *skycoords;
-  struct timeval start, stop;
   
   double RmaxSkyRegion, RminSkyRegion, RmidSkyRegion, DminSkyRegion, DmaxSkyRegion;
@@ -50,5 +43,5 @@
   }
 
-  gettimeofday (&start, (void *) NULL);
+  INITTIME;
 
   // the comparison is made in the catalog local projection. below we set crval1,2
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/share_mean_pos.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/share_mean_pos.c	(revision 36568)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/share_mean_pos.c	(revision 36569)
@@ -13,7 +13,4 @@
   MeanPos *meanpos = NULL;
   ALLOCATE (meanpos, MeanPos, NMEANPOS);
-
-  int Ns;
-  int Nsecfilt = GetPhotcodeNsecfilt();
 
   int myHost = regionHosts->index[REGION_HOST_ID];
@@ -38,11 +35,7 @@
       if (catalog[i].averageT[j].nOwn == catalog[i].averageT[j].Nmeasure) continue;
 
-      for (Ns = 0; Ns < Nphotcodes; Ns++) {
-	int thisCode = photcodes[Ns][0].code;
-	int Nsec = GetPhotcodeNsec(thisCode);
-	set_mean_pos (&meanpos[Nmeanpos], &catalog[i].averageT[j], &catalog[i].secfilt[Nsecfilt*j + Nsec], Nsec);
-	Nmeanpos ++;
-	CHECK_REALLOCATE (meanpos, MeanPos, NMEANPOS, Nmeanpos, D_NMEANPOS);
-      }
+      set_mean_pos (&meanpos[Nmeanpos], &catalog[i].averageT[j]);
+      Nmeanpos ++;
+      CHECK_REALLOCATE (meanpos, MeanPos, NMEANPOS, Nmeanpos, D_NMEANPOS);
     }
   }
@@ -71,6 +64,4 @@
 
   fprintf (stderr, "grabbing mean object pos from other hosts...\n");
-
-  int Nsecfilt = GetPhotcodeNsecfilt();
 
   for (i = 0; i < regionHosts->Nhosts; i++) {
@@ -112,5 +103,5 @@
 }
 
-int set_mean_pos (MeanPos *meanpos, AverageTiny *average, SecFilt *secfilt, int Nsec) {
+int set_mean_pos (MeanPos *meanpos, AverageTiny *average) {
 
   meanpos->R     = average->R;
