Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/Makefile
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/Makefile	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/Makefile	(revision 33254)
@@ -1,3 +1,3 @@
-default: relphot
+default: relphot relphot_client
 help:
 	@echo "make options: relphot (default)"
@@ -18,5 +18,7 @@
 
 relphot: $(BIN)/relphot.$(ARCH)
-install: $(DESTBIN)/relphot
+relphot_client: $(BIN)/relphot_client.$(ARCH)
+
+install: $(DESTBIN)/relphot $(DESTBIN)/relphot_client
 
 RELPHOT = \
@@ -24,4 +26,6 @@
 $(SRC)/GridOps.$(ARCH).o	 \
 $(SRC)/ImageOps.$(ARCH).o	 \
+$(SRC)/ImageSubset.$(ARCH).o	 \
+$(SRC)/BrightCatalog.$(ARCH).o	 \
 $(SRC)/MosaicOps.$(ARCH).o	 \
 $(SRC)/SetSignals.$(ARCH).o 	 \
@@ -48,2 +52,28 @@
 $(RELPHOT): $(INC)/relphot.h
 $(BIN)/relphot.$(ARCH): $(RELPHOT)
+
+RELPHOT_CLIENT = \
+$(SRC)/ConfigInit.$(ARCH).o	 \
+$(SRC)/GridOps.$(ARCH).o	 \
+$(SRC)/ImageOps.$(ARCH).o	 \
+$(SRC)/ImageSubset.$(ARCH).o	 \
+$(SRC)/BrightCatalog.$(ARCH).o	 \
+$(SRC)/MosaicOps.$(ARCH).o	 \
+$(SRC)/SetSignals.$(ARCH).o 	 \
+$(SRC)/Shutdown.$(ARCH).o 	 \
+$(SRC)/StarOps.$(ARCH).o	 \
+$(SRC)/bcatalog.$(ARCH).o	 \
+$(SRC)/args.$(ARCH).o		 \
+$(SRC)/help.$(ARCH).o		 \
+$(SRC)/plotstuff.$(ARCH).o	 \
+$(SRC)/liststats.$(ARCH).o	 \
+$(SRC)/initialize.$(ARCH).o	 \
+$(SRC)/load_catalogs.$(ARCH).o	 \
+$(SRC)/reload_catalogs.$(ARCH).o \
+$(SRC)/relphot_client.$(ARCH).o	 \
+$(SRC)/setExclusions.$(ARCH).o 	 \
+$(SRC)/setMrelFinal.$(ARCH).o    \
+$(SRC)/write_coords.$(ARCH).o
+
+$(RELPHOT_CLIENT): $(INC)/relphot.h
+$(BIN)/relphot_client.$(ARCH): $(RELPHOT_CLIENT)
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/doc/parallel.txt
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/doc/parallel.txt	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/doc/parallel.txt	(revision 33254)
@@ -1,2 +1,18 @@
+
+2012.02.13
+
+Image fields needed by relphot_client (saved by ImageSubsetSave):
+
+imageID
+photom_map_id
+flags
+Mcal
+dMcal
+tzero
+trate
+
+(if we have USE_GRID == T):
+photcode
+
 
 2012.02.10
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/include/relphot.h	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/include/relphot.h	(revision 33254)
@@ -7,4 +7,10 @@
 # define GRID_V2
 # define NO_IMAGE -100
+
+typedef enum {
+    MODE_NONE   = 0,
+    MODE_LOAD   = 1,
+    MODE_UPDATE = 2,
+} ModeType;
 
 typedef struct {
@@ -34,4 +40,22 @@
 } StatType;
 
+typedef struct {
+    AverageTiny *average;
+    MeasureTiny *measure;
+    SecFilt     *secfilt;
+    off_t Naverage;
+    off_t Nmeasure;
+} BrightCatalog;
+
+typedef struct {
+  float Mcal;
+  float dMcal;
+  unsigned int imageID;
+  unsigned int photom_map_id;
+  unsigned int flags;
+  unsigned int tzero;
+  unsigned char trate;
+} ImageSubset;
+
 /* global variables set in parameter file */
 char   ImageCat[256];
@@ -45,4 +69,10 @@
 char   SKY_TABLE[256];
 int    SKY_DEPTH;  /** XXX EAM : depth of catalog tables, fix usage */
+
+int          HOST_ID;
+char        *HOSTDIR;
+char        *IMAGES;
+char        *BCATALOG;
+ModeType     MODE;
 
 double MAG_LIM;
@@ -133,4 +163,5 @@
 off_t        *SelectRefMosaic     PROTO((Mosaic **refmosaic, off_t *Nimage));
 int           args                PROTO((int argc, char **argv));
+int           args_client         PROTO((int argc, char **argv));
 int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog));
 void          clean_images        PROTO((void));
@@ -165,8 +196,10 @@
 Image        *getimage            PROTO((off_t N));
 Image        *getimages           PROTO((off_t *N, off_t **LineNumber));
+ImageSubset  *getimages_subset    PROTO((off_t *N));
 void          global_stats        PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
 void          initGrid            PROTO((int dX, int dY));
 void          initGridBins        PROTO((Catalog *catalog, int Ncatalog));
 void          initImageBins       PROTO((Catalog *catalog, int Ncatalog, int doImageList));
+void          initImagesSubset    PROTO((ImageSubset *input, off_t *line_number, off_t N));
 void          initImages          PROTO((Image *input, off_t *LineNumber, off_t N));
 void          initMosaicBins      PROTO((Catalog *catalog, int Ncatalog, int doMosaicList));
@@ -175,9 +208,12 @@
 void          initMrel            PROTO((Catalog *catalog, int Ncatalog));
 void          initialize          PROTO((int argc, char **argv));
+void          initialize_client   PROTO((int argc, char **argv));
 void          initstats           PROTO((char *mode));
 int           liststats           PROTO((double *value, double *dvalue, int N, StatType *stats));
-Catalog       *load_catalogs      PROTO((SkyList *skylist, int *Ncatalog));
+Catalog      *load_catalogs       PROTO((SkyList *skylist, int *Ncatalog, int hostID, char *hostpath));
+Catalog      *load_catalogs_parallel PROTO((SkyList *sky, int *Ncatalog));
+
 SkyList      *load_images         PROTO((FITS_DB *db, char *regionName, SkyRegion *region, int RegionSelect));
-Image         *select_images      PROTO((SkyList *skylist, Image *timage, off_t Ntimage, off_t **LineNumber, off_t *Nimage));
+Image        *select_images       PROTO((SkyList *skylist, Image *timage, off_t Ntimage, off_t **LineNumber, off_t *Nimage));
 
 int           main                PROTO((int argc, char **argv));
@@ -196,5 +232,6 @@
 void          plot_star_coords    PROTO((Catalog *catalog, int Ncatalog));
 void          plot_stars          PROTO((Catalog *catalog, int Ncatalog));
-void          reload_catalogs     PROTO((SkyList *skylist, FlatCorrectionTable *flatcorr));
+void          reload_catalogs     PROTO((SkyList *skylist, FlatCorrectionTable *flatcorr, int hostID, char *hostpath));
+int           reload_catalogs_parallel PROTO((SkyList *sky));
 int           reload_images       PROTO((FITS_DB *db));
 int           setExclusions       PROTO((Catalog *catalog, int Ncatalog));
@@ -234,4 +271,7 @@
 void relphot_help (int argc, char **argv);
 
+void relphot_client_usage (void);
+void relphot_client_help (int argc, char **argv);
+
 off_t getImageByID (off_t ID);
 
@@ -241,2 +281,12 @@
 int populate_tiny_values (Catalog *catalog);
 int free_tiny_values (Catalog *catalog);
+
+BrightCatalog *BrightCatalogLoad(char *filename);
+int BrightCatalogSave(char *filename, BrightCatalog *catalog);
+BrightCatalog *BrightCatalogMerge (Catalog *catalog, int Ncatalog);
+
+int ImageSubsetSave(char *filename, ImageSubset *image, off_t Nimage);
+ImageSubset *ImageSubsetLoad(char *filename, off_t *nimage);
+
+int CopyAverageTiny (AverageTiny *averageT, Average *average);
+int CopyMeasureTiny (MeasureTiny *measureT, Measure *measure);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/BrightCatalog.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/BrightCatalog.c	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/BrightCatalog.c	(revision 33254)
@@ -8,13 +8,5 @@
 // reassign correctly on read
 
-typedef struct {
-    AverageTiny *average;
-    MeasureTiny *measure;
-    Secfilt     *secfilt;
-    off_t Naverage;
-    off_t Nmeasure;
-} BrightCatalog;
-
-# define GET_COLUMN(OUT,NAME,TYPE) \
+# define GET_COLUMN(OUT,NAME,TYPE)					\
   TYPE *OUT = gfits_get_bintable_column_data (&theader, &ftable, NAME, type, &Nrow, &Ncol); \
   myAssert (!strcmp(type, #TYPE), "wrong column type");
@@ -26,5 +18,5 @@
 BrightCatalog *BrightCatalogLoad(char *filename) {
 
-  int i, Ncol, Ncol2;
+  int i, Ncol;
   off_t Nrow;
   char type[16];
@@ -61,148 +53,157 @@
   ftable.header = &theader;
 
-  // load data for this header 
-  if (!gfits_load_header (f, &theader)) return (NULL);
-
-  // read the fits table bytes
-  if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
+  // *** Measure Data *** 
+  { 
+    // load data for this header 
+    if (!gfits_load_header (f, &theader)) return (NULL);
+
+    // read the fits table bytes
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
  
-  // need to create and assign to flat-field correction
-  GET_COLUMN(dR,       "RA_OFF",   float);
-  GET_COLUMN(dD,       "DEC_OFF",  float);
-  GET_COLUMN(M,        "MAG_SYS",  float);
-  GET_COLUMN(Mcal,     "MAG_CAL",  float);
-  GET_COLUMN(dM,       "MAG_ERR",  float);
-  GET_COLUMN(airmass,  "AIRMASS",  float);
-  GET_COLUMN(Xccd,     "X_CCD",    float);
-  GET_COLUMN(Yccd,     "Y_CCD",    float);
-  GET_COLUMN(dt,       "EXPTIME",  float);
-  GET_COLUMN(t,        "TIME",     int);
-  GET_COLUMN(averef,   "AVE_REF",  int); // XXX signed vs unsigned?
-  GET_COLUMN(imageID,  "IMAGE_ID", int);
-  GET_COLUMN(dbFlags,  "DB_FLAGS", int);
-  GET_COLUMN(catID,    "CAT_ID",   int);
-  GET_COLUMN(photcode, "PHOTCODE", short);
-  // XXX free the fits table data here 
-
-  ALLOCATE (measure, MeasureTiny, Nrow);
-  for (i = 0; i < Nrow; i++) {
-    measure[i].dR       = dR[i];
-    measure[i].dD       = dD[i];
-    measure[i].M        = M[i];
-    measure[i].Mcal     = Mcal[i];
-    measure[i].dM       = dM[i];
-    measure[i].airmass  = airmass[i];
-    measure[i].Xccd     = Xccd[i];
-    measure[i].Yccd     = Yccd[i];
-    measure[i].dt       = dt[i];
-    measure[i].t        = t[i];
-    measure[i].averef   = averef[i];
-    measure[i].imageID  = imageID[i];
-    measure[i].dbFlags  = dbFlags[i];
-    measure[i].catID    = catID[i];
-    measure[i].photcode = photcode[i];
-  }
-  fprintf (stderr, "loaded data for %lld measures\n", (long long) Nrow);
-
-  free (dR      );
-  free (dD      );
-  free (M       );
-  free (Mcal    );
-  free (dM      );
-  free (airmass );
-  free (Xccd    );
-  free (Yccd    );
-  free (dt      );
-  free (t       );
-  free (averef  );
-  free (imageID );
-  free (dbFlags );
-  free (catID   );
-  free (photcode);
-
-  catalog->measure = measure;
-  catalog->Nmeasure = Nrow;
+    // need to create and assign to flat-field correction
+    GET_COLUMN(dR,       "RA_OFF",   float);
+    GET_COLUMN(dD,       "DEC_OFF",  float);
+    GET_COLUMN(M,        "MAG_SYS",  float);
+    GET_COLUMN(Mcal,     "MAG_CAL",  float);
+    GET_COLUMN(dM,       "MAG_ERR",  float);
+    GET_COLUMN(airmass,  "AIRMASS",  float);
+    GET_COLUMN(Xccd,     "X_CCD",    float);
+    GET_COLUMN(Yccd,     "Y_CCD",    float);
+    GET_COLUMN(dt,       "EXPTIME",  float);
+    GET_COLUMN(t,        "TIME",     int);
+    GET_COLUMN(averef,   "AVE_REF",  int); // XXX signed vs unsigned?
+    GET_COLUMN(imageID,  "IMAGE_ID", int);
+    GET_COLUMN(dbFlags,  "DB_FLAGS", int);
+    GET_COLUMN(catID,    "CAT_ID",   int);
+    GET_COLUMN(photcode, "PHOTCODE", short);
+    // XXX free the fits table data here 
+
+    MeasureTiny *measure = NULL;
+    ALLOCATE (measure, MeasureTiny, Nrow);
+    for (i = 0; i < Nrow; i++) {
+      measure[i].dR       = dR[i];
+      measure[i].dD       = dD[i];
+      measure[i].M        = M[i];
+      measure[i].Mcal     = Mcal[i];
+      measure[i].dM       = dM[i];
+      measure[i].airmass  = airmass[i];
+      measure[i].Xccd     = Xccd[i];
+      measure[i].Yccd     = Yccd[i];
+      measure[i].dt       = dt[i];
+      measure[i].t        = t[i];
+      measure[i].averef   = averef[i];
+      measure[i].imageID  = imageID[i];
+      measure[i].dbFlags  = dbFlags[i];
+      measure[i].catID    = catID[i];
+      measure[i].photcode = photcode[i];
+    }
+    fprintf (stderr, "loaded data for %lld measures\n", (long long) Nrow);
+
+    free (dR      );
+    free (dD      );
+    free (M       );
+    free (Mcal    );
+    free (dM      );
+    free (airmass );
+    free (Xccd    );
+    free (Yccd    );
+    free (dt      );
+    free (t       );
+    free (averef  );
+    free (imageID );
+    free (dbFlags );
+    free (catID   );
+    free (photcode);
+
+    catalog->measure = measure;
+    catalog->Nmeasure = Nrow;
+  }
 
   /*** load Average values ***/
-
-  // load data for this header 
-  if (!gfits_load_header (f, &theader)) return (NULL);
-
-  // read the fits table bytes
-  if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
+  { 
+
+    // load data for this header 
+    if (!gfits_load_header (f, &theader)) return (NULL);
+
+    // read the fits table bytes
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
  
-  // need to create and assign to flat-field correction
-  GET_COLUMN(R,             "RA",   	   double);
-  GET_COLUMN(D,             "DEC",  	   double);
-  GET_COLUMN(Nmeasure,      "NMEAS",       int);
-  GET_COLUMN(measureOffset, "MEASURE_OFF", int);
-  GET_COLUMN(flags,         "FLAGS",       int);
-  GET_COLUMN(catID,         "CAT_ID",      int);
-  // XXX free the fits table data here 
-
-  ALLOCATE (average, AverageTiny, Nrow);
-  for (i = 0; i < Nrow; i++) {
-    average[i].R              = R[i];
-    average[i].D              = D[i];
-    average[i].Nmeasure       = Nmeasure[i];
-    average[i].measureOffset  = measureOffset[i];
-    average[i].flags          = flags[i];
-    average[i].catID          = catID[i];
-  }
-  fprintf (stderr, "loaded data for %lld averages\n", (long long) Nrow);
-
-  free (R             );
-  free (D             );
-  free (Nmeasure      );
-  free (measureOffset );
-  free (flags         );
-  free (catID         );
-
-  catalog->average = average;
-  catalog->Naverage = Nrow;
+    // need to create and assign to flat-field correction
+    GET_COLUMN(R,             "RA",   	   double);
+    GET_COLUMN(D,             "DEC",  	   double);
+    GET_COLUMN(Nmeasure,      "NMEAS",       int);
+    GET_COLUMN(measureOffset, "MEASURE_OFF", int);
+    GET_COLUMN(flags,         "FLAGS",       int);
+    GET_COLUMN(catID,         "CAT_ID",      int);
+    // XXX free the fits table data here 
+
+    AverageTiny *average = NULL;
+    ALLOCATE (average, AverageTiny, Nrow);
+    for (i = 0; i < Nrow; i++) {
+      average[i].R              = R[i];
+      average[i].D              = D[i];
+      average[i].Nmeasure       = Nmeasure[i];
+      average[i].measureOffset  = measureOffset[i];
+      average[i].flags          = flags[i];
+      average[i].catID          = catID[i];
+    }
+    fprintf (stderr, "loaded data for %lld averages\n", (long long) Nrow);
+
+    free (R             );
+    free (D             );
+    free (Nmeasure      );
+    free (measureOffset );
+    free (flags         );
+    free (catID         );
+
+    catalog->average = average;
+    catalog->Naverage = Nrow;
+  }
 
   /*** load Secfilt values ***/
-
-  // load data for this header 
-  if (!gfits_load_header (f, &theader)) return (NULL);
-
-  // read the fits table bytes
-  if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
+  {
+
+    // load data for this header 
+    if (!gfits_load_header (f, &theader)) return (NULL);
+
+    // read the fits table bytes
+    if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL);
  
-  // need to create and assign to flat-field correction
-  GET_COLUMN(M,     "MAG",      float);
-  GET_COLUMN(dM,    "MAG_ERR",  float);
-  GET_COLUMN(Xm,    "MAG_CHI",  float);
-  GET_COLUMN(flags, "FLAGS",    int);
-  GET_COLUMN(Ncode, "NCODE",    short);
-  GET_COLUMN(Nused, "NUSED",    short);
-  GET_COLUMN(M_20,  "MAG_20",   short);
-  GET_COLUMN(M_80,  "MAG_80",   short);
-  // XXX free the fits table data here 
-
-  ALLOCATE (secfilt, SecFilt, Nrow);
-  for (i = 0; i < Nrow; i++) {
-    secfilt[i].M     = M[i];         
-    secfilt[i].dM    = dM[i];
-    secfilt[i].Xm    = Xm[i];
-    secfilt[i].flags = flags[i];
-    secfilt[i].Ncode = Ncode[i];
-    secfilt[i].Nused = Nused[i];
-    secfilt[i].M_20  = M_20[i];
-    secfilt[i].M_80  = M_80[i];
-  }
-  fprintf (stderr, "loaded data for %lld averages\n", (long long) Nrow);
-
-  free (M    );
-  free (dM   );
-  free (Xm   );
-  free (flags);
-  free (Ncode);
-  free (Nused);
-  free (M_20 );
-  free (M_80 );
-
-  catalog->secfilt = secfilt;
-  // assert Nsecfilt * Naverage = Nrow?
+    // need to create and assign to flat-field correction
+    GET_COLUMN(M,     "MAG",      float);
+    GET_COLUMN(dM,    "MAG_ERR",  float);
+    GET_COLUMN(Xm,    "MAG_CHI",  float);
+    GET_COLUMN(flags, "FLAGS",    int);
+    GET_COLUMN(Ncode, "NCODE",    short);
+    GET_COLUMN(Nused, "NUSED",    short);
+    GET_COLUMN(M_20,  "MAG_20",   short);
+    GET_COLUMN(M_80,  "MAG_80",   short);
+    // XXX free the fits table data here 
+
+    SecFilt *secfilt = NULL;
+    ALLOCATE (secfilt, SecFilt, Nrow);
+    for (i = 0; i < Nrow; i++) {
+      secfilt[i].M     = M[i];         
+      secfilt[i].dM    = dM[i];
+      secfilt[i].Xm    = Xm[i];
+      secfilt[i].flags = flags[i];
+      secfilt[i].Ncode = Ncode[i];
+      secfilt[i].Nused = Nused[i];
+      secfilt[i].M_20  = M_20[i];
+      secfilt[i].M_80  = M_80[i];
+    }
+    fprintf (stderr, "loaded data for %lld averages\n", (long long) Nrow);
+
+    free (M    );
+    free (dM   );
+    free (Xm   );
+    free (flags);
+    free (Ncode);
+    free (Nused);
+    free (M_20 );
+    free (M_80 );
+    catalog->secfilt = secfilt;
+    // assert Nsecfilt * Naverage = Nrow?
+  }
 
   // close files
@@ -237,122 +238,127 @@
 
   /*** MeasureTiny ***/
-  gfits_create_table_header (&theader, "BINTABLE", "MEASURE_TINY");
-
-  gfits_define_bintable_column (&theader, "E", "RA_OFF",   "ra offset", 	       "arcsec", 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "DEC_OFF",  "dec offset",	       "arcsec", 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "MAG_SYS",  "magnitude (sys)", 	       NULL, 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "MAG_CAL",  "magnitude (cal)", 	       NULL, 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "MAG_ERR",  "magnitude (err)", 	       NULL, 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "AIRMASS",  "airmass",                  NULL, 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "X_CCD",    "ccd x coord",   	       "pix", 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "Y_CCD",    "ccd y coord",   	       "pix", 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "EXPTIME",  "-2.5 * log (exposure time)", "sec",	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "J", "TIME",     "time of exp",              "sec", 	 1.0, 1.0*0x8000);
-  gfits_define_bintable_column (&theader, "J", "AVE_REF",  "pointer to average table", NULL, 	 1.0, 1.0*0x8000);
-  gfits_define_bintable_column (&theader, "J", "IMAGE_ID", "image",    		       NULL, 	 1.0, 1.0*0x8000);
-  gfits_define_bintable_column (&theader, "J", "DB_FLAGS", "flags",    		       NULL, 	 1.0, 1.0*0x8000);
-  gfits_define_bintable_column (&theader, "J", "CAT_ID",   "catalog",  		       NULL, 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "K", "PHOTCODE", "photcode", 		       NULL, 	 1.0, 0.0);
-
-  // generate the output array that carries the data
-  gfits_create_table (&theader, &ftable);
-
-  // create intermediate storage arrays
-  float *dR        ; ALLOCATE (dR      ,  float, catalog->Nmeasure);
-  float *dD        ; ALLOCATE (dD      ,  float, catalog->Nmeasure);
-  float *M         ; ALLOCATE (M       ,  float, catalog->Nmeasure);
-  float *Mcal      ; ALLOCATE (Mcal    ,  float, catalog->Nmeasure);
-  float *dM        ; ALLOCATE (dM      ,  float, catalog->Nmeasure);
-  float *airmass   ; ALLOCATE (airmass ,  float, catalog->Nmeasure);
-  float *Xccd      ; ALLOCATE (Xccd    ,  float, catalog->Nmeasure);
-  float *Yccd      ; ALLOCATE (Yccd    ,  float, catalog->Nmeasure);
-  float *dt        ; ALLOCATE (dt      ,  float, catalog->Nmeasure);
-  int   *t         ; ALLOCATE (t       ,  int  , catalog->Nmeasure);
-  int   *averef    ; ALLOCATE (averef  ,  int  , catalog->Nmeasure);
-  int   *imageID   ; ALLOCATE (imageID ,  int  , catalog->Nmeasure);
-  int   *dbFlags   ; ALLOCATE (dbFlags ,  int  , catalog->Nmeasure);
-  int   *catID     ; ALLOCATE (catID   ,  int  , catalog->Nmeasure);
-  short *photcode  ; ALLOCATE (photcode,  short, catalog->Nmeasure);
-
-  // assign the storage arrays
-  for (i = 0; i < catalog->Nmeasure; i++) {
-    dR[i]       = measure[i].dR       ;
-    dD[i]       = measure[i].dD       ;
-    M[i] 	= measure[i].M        ;
-    Mcal[i]     = measure[i].Mcal     ;
-    dM[i]       = measure[i].dM       ;
-    airmass[i]  = measure[i].airmass  ;
-    Xccd[i]     = measure[i].Xccd     ;
-    Yccd[i]     = measure[i].Yccd     ;
-    dt[i]       = measure[i].dt       ;
-    t[i] 	= measure[i].t        ;
-    averef[i]   = measure[i].averef   ;
-    catID[i]    = measure[i].catID    ;
-    imageID[i]  = measure[i].imageID  ;
-    dbFlags[i]  = measure[i].dbFlags  ;
-    photcode[i] = measure[i].photcode ;
-  }
-
-  // add the columns to the output array
-  gfits_set_bintable_column (&theader, &ftable, "RA_OFF",   dR,       catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "DEC_OFF",  dD,       catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "MAG_SYS",  M,        catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "MAG_CAL",  Mcal,     catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "MAG_ERR",  dM,       catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "AIRMASS",  airmass,  catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "X_CCD",    Xccd,     catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "Y_CCD",    Yccd,     catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "EXPTIME",  dt,       catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "TIME",     t,        catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "AVE_REF",  averef,   catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "IMAGE_ID", imageID,  catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "DB_FLAGS", dbFlags,  catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "CAT_ID",   catID,    catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "PHOTCODE", photcode, catalog->Nmeasure);
-
-  free (dR      );
-  free (dD      );
-  free (M       );
-  free (Mcal    );
-  free (dM      );
-  free (airmass );
-  free (Xccd    );
-  free (Yccd    );
-  free (dt      );
-  free (t       );
-  free (averef  );
-  free (imageID );
-  free (dbFlags );
-  free (catID   );
-  free (photcode);
-
-  gfits_fwrite_Theader (f, &theader);
-  gfits_fwrite_table  (f, &ftable);
-  gfits_free_header (&theader);
-  gfits_free_table (&ftable);
+  {
+    gfits_create_table_header (&theader, "BINTABLE", "MEASURE_TINY");
+
+    gfits_define_bintable_column (&theader, "E", "RA_OFF",   "ra offset", 	       "arcsec", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "DEC_OFF",  "dec offset",	       "arcsec", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "MAG_SYS",  "magnitude (sys)", 	       NULL, 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "MAG_CAL",  "magnitude (cal)", 	       NULL, 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "MAG_ERR",  "magnitude (err)", 	       NULL, 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "AIRMASS",  "airmass",                  NULL, 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "X_CCD",    "ccd x coord",   	       "pix", 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "Y_CCD",    "ccd y coord",   	       "pix", 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "EXPTIME",  "-2.5 * log (exposure time)", "sec",	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "TIME",     "time of exp",              "sec", 	 1.0, 1.0*0x8000);
+    gfits_define_bintable_column (&theader, "J", "AVE_REF",  "pointer to average table", NULL, 	 1.0, 1.0*0x8000);
+    gfits_define_bintable_column (&theader, "J", "IMAGE_ID", "image",    		       NULL, 	 1.0, 1.0*0x8000);
+    gfits_define_bintable_column (&theader, "J", "DB_FLAGS", "flags",    		       NULL, 	 1.0, 1.0*0x8000);
+    gfits_define_bintable_column (&theader, "J", "CAT_ID",   "catalog",  		       NULL, 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "K", "PHOTCODE", "photcode", 		       NULL, 	 1.0, 0.0);
+
+    // generate the output array that carries the data
+    gfits_create_table (&theader, &ftable);
+
+    // create intermediate storage arrays
+    float *dR        ; ALLOCATE (dR      ,  float, catalog->Nmeasure);
+    float *dD        ; ALLOCATE (dD      ,  float, catalog->Nmeasure);
+    float *M         ; ALLOCATE (M       ,  float, catalog->Nmeasure);
+    float *Mcal      ; ALLOCATE (Mcal    ,  float, catalog->Nmeasure);
+    float *dM        ; ALLOCATE (dM      ,  float, catalog->Nmeasure);
+    float *airmass   ; ALLOCATE (airmass ,  float, catalog->Nmeasure);
+    float *Xccd      ; ALLOCATE (Xccd    ,  float, catalog->Nmeasure);
+    float *Yccd      ; ALLOCATE (Yccd    ,  float, catalog->Nmeasure);
+    float *dt        ; ALLOCATE (dt      ,  float, catalog->Nmeasure);
+    int   *t         ; ALLOCATE (t       ,  int  , catalog->Nmeasure);
+    int   *averef    ; ALLOCATE (averef  ,  int  , catalog->Nmeasure);
+    int   *imageID   ; ALLOCATE (imageID ,  int  , catalog->Nmeasure);
+    int   *dbFlags   ; ALLOCATE (dbFlags ,  int  , catalog->Nmeasure);
+    int   *catID     ; ALLOCATE (catID   ,  int  , catalog->Nmeasure);
+    short *photcode  ; ALLOCATE (photcode,  short, catalog->Nmeasure);
+
+    // assign the storage arrays
+    MeasureTiny *measure = catalog->measure;
+    for (i = 0; i < catalog->Nmeasure; i++) {
+      dR[i]       = measure[i].dR       ;
+      dD[i]       = measure[i].dD       ;
+      M[i]  	  = measure[i].M        ;
+      Mcal[i]     = measure[i].Mcal     ;
+      dM[i]       = measure[i].dM       ;
+      airmass[i]  = measure[i].airmass  ;
+      Xccd[i]     = measure[i].Xccd     ;
+      Yccd[i]     = measure[i].Yccd     ;
+      dt[i]       = measure[i].dt       ;
+      t[i] 	  = measure[i].t        ;
+      averef[i]   = measure[i].averef   ;
+      catID[i]    = measure[i].catID    ;
+      imageID[i]  = measure[i].imageID  ;
+      dbFlags[i]  = measure[i].dbFlags  ;
+      photcode[i] = measure[i].photcode ;
+    }
+
+    // add the columns to the output array
+    gfits_set_bintable_column (&theader, &ftable, "RA_OFF",   dR,       catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "DEC_OFF",  dD,       catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "MAG_SYS",  M,        catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "MAG_CAL",  Mcal,     catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "MAG_ERR",  dM,       catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "AIRMASS",  airmass,  catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "X_CCD",    Xccd,     catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "Y_CCD",    Yccd,     catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "EXPTIME",  dt,       catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "TIME",     t,        catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "AVE_REF",  averef,   catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "IMAGE_ID", imageID,  catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "DB_FLAGS", dbFlags,  catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "CAT_ID",   catID,    catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "PHOTCODE", photcode, catalog->Nmeasure);
+
+    free (dR      );
+    free (dD      );
+    free (M       );
+    free (Mcal    );
+    free (dM      );
+    free (airmass );
+    free (Xccd    );
+    free (Yccd    );
+    free (dt      );
+    free (t       );
+    free (averef  );
+    free (imageID );
+    free (dbFlags );
+    free (catID   );
+    free (photcode);
+
+    gfits_fwrite_Theader (f, &theader);
+    gfits_fwrite_table  (f, &ftable);
+    gfits_free_header (&theader);
+    gfits_free_table (&ftable);
+  }
 
   /*** AverageTiny ***/
-  gfits_create_table_header (&theader, "BINTABLE", "AVERAGE_TINY");
-
-  gfits_define_bintable_column (&theader, "D", "RA",   	      "ra (J2000)", 	       "degree", 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "D", "DEC",  	      "dec (J2000)",	       "degree", 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "J", "NMEAS",       "number of measures",     NULL, 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "J", "MEASURE_OFF", "index to measurements",  NULL, 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "J", "FLAGS",       "flags",                  NULL, 	 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "J", "CAT_ID",      "catalog ref",            NULL, 	 1.0, 0.0);
-
-  // generate the output array that carries the data
-  gfits_create_table (&theader, &ftable);
-
-  // create intermediate storage arrays
-  double *R             ; ALLOCATE (R,             double, catalog->Naverage);
-  double *D             ; ALLOCATE (D,             double, catalog->Naverage);
-  int   *Nmeasure       ; ALLOCATE (Nmeasure,      int,    catalog->Naverage);
-  int   *measureOffset  ; ALLOCATE (measureOffset, int,    catalog->Naverage);
-  int   *flags          ; ALLOCATE (flags,         int,    catalog->Naverage);
-  int   *catID          ; ALLOCATE (catID,         int,    catalog->Naverage);
-
-  // assign the storage arrays
-  for (i = 0; i < catalog->Nmeasure; i++) {
+  {
+    gfits_create_table_header (&theader, "BINTABLE", "AVERAGE_TINY");
+
+    gfits_define_bintable_column (&theader, "D", "RA",   	      "ra (J2000)", 	       "degree", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "D", "DEC",  	      "dec (J2000)",	       "degree", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "NMEAS",       "number of measures",     NULL, 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "MEASURE_OFF", "index to measurements",  NULL, 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "FLAGS",       "flags",                  NULL, 	 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "CAT_ID",      "catalog ref",            NULL, 	 1.0, 0.0);
+
+    // generate the output array that carries the data
+    gfits_create_table (&theader, &ftable);
+
+    // create intermediate storage arrays
+    double *R             ; ALLOCATE (R,             double, catalog->Naverage);
+    double *D             ; ALLOCATE (D,             double, catalog->Naverage);
+    int   *Nmeasure       ; ALLOCATE (Nmeasure,      int,    catalog->Naverage);
+    int   *measureOffset  ; ALLOCATE (measureOffset, int,    catalog->Naverage);
+    int   *flags          ; ALLOCATE (flags,         int,    catalog->Naverage);
+    int   *catID          ; ALLOCATE (catID,         int,    catalog->Naverage);
+
+    // assign the storage arrays
+    AverageTiny *average = catalog->average;
+    for (i = 0; i < catalog->Naverage; i++) {
       R[i]           	= average[i].R       ;
       D[i]           	= average[i].D       ;
@@ -361,91 +367,100 @@
       flags[i]          = average[i].flags;
       catID[i]          = average[i].catID;
-  }
-
-  // add the columns to the output array
-  gfits_set_bintable_column (&theader, &ftable, "RA",          R,             catalog->Naverage);
-  gfits_set_bintable_column (&theader, &ftable, "DEC",         D,             catalog->Naverage);
-  gfits_set_bintable_column (&theader, &ftable, "NMEAS",       Nmeasure,      catalog->Naverage);
-  gfits_set_bintable_column (&theader, &ftable, "MEASURE_OFF", measureOffset, catalog->Naverage);
-  gfits_set_bintable_column (&theader, &ftable, "FLAGS",       flags,         catalog->Naverage);
-  gfits_set_bintable_column (&theader, &ftable, "CAT_ID",      catID,         catalog->Naverage);
-
-  free (R             );
-  free (D             );
-  free (Nmeasure      );
-  free (measureOffset );
-  free (flags         );
-  free (catID         );
-
-  gfits_fwrite_Theader (f, &theader);
-  gfits_fwrite_table  (f, &ftable);
-  gfits_free_header (&theader);
-  gfits_free_table (&ftable);
+    }
+
+    // add the columns to the output array
+    gfits_set_bintable_column (&theader, &ftable, "RA",          R,             catalog->Naverage);
+    gfits_set_bintable_column (&theader, &ftable, "DEC",         D,             catalog->Naverage);
+    gfits_set_bintable_column (&theader, &ftable, "NMEAS",       Nmeasure,      catalog->Naverage);
+    gfits_set_bintable_column (&theader, &ftable, "MEASURE_OFF", measureOffset, catalog->Naverage);
+    gfits_set_bintable_column (&theader, &ftable, "FLAGS",       flags,         catalog->Naverage);
+    gfits_set_bintable_column (&theader, &ftable, "CAT_ID",      catID,         catalog->Naverage);
+
+    free (R             );
+    free (D             );
+    free (Nmeasure      );
+    free (measureOffset );
+    free (flags         );
+    free (catID         );
+
+    gfits_fwrite_Theader (f, &theader);
+    gfits_fwrite_table  (f, &ftable);
+    gfits_free_header (&theader);
+    gfits_free_table (&ftable);
+  }
 
   /*** SecFilt ***/
-  gfits_create_table_header (&theader, "BINTABLE", "SECFILT");
-
-  gfits_define_bintable_column (&theader, "E", "MAG",      "ra offset",                "arcsec", 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "MAG_ERR",  "dec offset",               "arcsec", 1.0, 0.0);
-  gfits_define_bintable_column (&theader, "E", "MAG_CHI",  "magnitude (sys)",          NULL,     1.0, 0.0);
-  gfits_define_bintable_column (&theader, "J", "FLAGS",    "magnitude (cal)",          NULL,     1.0, 0.0);
-  gfits_define_bintable_column (&theader, "I", "NCODE",    "magnitude (err)",          NULL,     1.0, 0.0);
-  gfits_define_bintable_column (&theader, "I", "NUSED",    "airmass",                  NULL,     1.0, 0.0);
-  gfits_define_bintable_column (&theader, "I", "MAG_20",   "ccd x coord",              "pix",    1.0, 0.0);
-  gfits_define_bintable_column (&theader, "I", "MAG_80",   "ccd y coord",              "pix",    1.0, 0.0);
-
-  // generate the output array that carries the data
-  gfits_create_table (&theader, &ftable);
-
-  // create intermediate storage arrays
-  float *M        ; ALLOCATE (M      ,  float, catalog->Nmeasure);
-  float *dM       ; ALLOCATE (dM     ,  float, catalog->Nmeasure);
-  float *Xm       ; ALLOCATE (Xm     ,  float, catalog->Nmeasure);
-  int   *flags    ; ALLOCATE (flags  ,  int,   catalog->Nmeasure);
-  short *Ncode    ; ALLOCATE (Ncode  ,  short, catalog->Nmeasure);
-  short *Nused    ; ALLOCATE (Nused  ,  short, catalog->Nmeasure);
-  short *M_20     ; ALLOCATE (M_20   ,  short, catalog->Nmeasure);
-  short *M_80     ; ALLOCATE (M_80   ,  short, catalog->Nmeasure);
-
-  // assign the storage arrays
-  for (i = 0; i < catalog->Nmeasure; i++) {
-    M     [i]       = secfilt[i]. M      ;
-    dM    [i]       = secfilt[i]. dM     ;
-    Xm    [i]       = secfilt[i]. Xm     ;
-    flags [i]       = secfilt[i]. flags  ;
-    Ncode [i]       = secfilt[i]. Ncode  ;
-    Nused [i]       = secfilt[i]. Nused  ;
-    M_20  [i]       = secfilt[i]. M_20   ;
-    M_80  [i]       = secfilt[i]. M_80   ;
-  }
-
-  // add the columns to the output array
-  gfits_set_bintable_column (&theader, &ftable, "MAG",      M     ,catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "MAG_ERR",  dM    ,catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "MAG_CHI",  Xm    ,catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "FLAGS",    flags ,catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "NCODE",    Ncode ,catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "NUSED",    Nused ,catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "MAG_20",   M_20  ,catalog->Nmeasure);
-  gfits_set_bintable_column (&theader, &ftable, "MAG_80",   M_80  ,catalog->Nmeasure);
-
-  free (M      );
-  free (dM     );
-  free (Xm     );
-  free (flags  );
-  free (Ncode  );
-  free (Nused  );
-  free (M_20   );
-  free (M_80   );
-
-  gfits_fwrite_Theader (f, &theader);
-  gfits_fwrite_table  (f, &ftable);
-  gfits_free_header (&theader);
-  gfits_free_table (&ftable);
-
+  {
+    gfits_create_table_header (&theader, "BINTABLE", "SECFILT");
+
+    gfits_define_bintable_column (&theader, "E", "MAG",      "ra offset",                "arcsec", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "MAG_ERR",  "dec offset",               "arcsec", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "MAG_CHI",  "magnitude (sys)",          NULL,     1.0, 0.0);
+    gfits_define_bintable_column (&theader, "J", "FLAGS",    "magnitude (cal)",          NULL,     1.0, 0.0);
+    gfits_define_bintable_column (&theader, "I", "NCODE",    "magnitude (err)",          NULL,     1.0, 0.0);
+    gfits_define_bintable_column (&theader, "I", "NUSED",    "airmass",                  NULL,     1.0, 0.0);
+    gfits_define_bintable_column (&theader, "I", "MAG_20",   "ccd x coord",              "pix",    1.0, 0.0);
+    gfits_define_bintable_column (&theader, "I", "MAG_80",   "ccd y coord",              "pix",    1.0, 0.0);
+
+    // generate the output array that carries the data
+    gfits_create_table (&theader, &ftable);
+
+    // Nsecfilt is number of average filters, Nsec is total number of values
+    int Nsecfilt = GetPhotcodeNsecfilt();
+    int Nsec = Nsecfilt * catalog->Naverage;
+
+    // create intermediate storage arrays
+    float *M        ; ALLOCATE (M      ,  float, Nsec);
+    float *dM       ; ALLOCATE (dM     ,  float, Nsec);
+    float *Xm       ; ALLOCATE (Xm     ,  float, Nsec);
+    int   *flags    ; ALLOCATE (flags  ,  int,   Nsec);
+    short *Ncode    ; ALLOCATE (Ncode  ,  short, Nsec);
+    short *Nused    ; ALLOCATE (Nused  ,  short, Nsec);
+    short *M_20     ; ALLOCATE (M_20   ,  short, Nsec);
+    short *M_80     ; ALLOCATE (M_80   ,  short, Nsec);
+
+    // assign the storage arrays
+    SecFilt *secfilt = catalog->secfilt;
+    for (i = 0; i < Nsec; i++) {
+      M     [i]       = secfilt[i]. M      ;
+      dM    [i]       = secfilt[i]. dM     ;
+      Xm    [i]       = secfilt[i]. Xm     ;
+      flags [i]       = secfilt[i]. flags  ;
+      Ncode [i]       = secfilt[i]. Ncode  ;
+      Nused [i]       = secfilt[i]. Nused  ;
+      M_20  [i]       = secfilt[i]. M_20   ;
+      M_80  [i]       = secfilt[i]. M_80   ;
+    }
+
+    // add the columns to the output array
+    gfits_set_bintable_column (&theader, &ftable, "MAG",      M    , Nsec);
+    gfits_set_bintable_column (&theader, &ftable, "MAG_ERR",  dM   , Nsec);
+    gfits_set_bintable_column (&theader, &ftable, "MAG_CHI",  Xm   , Nsec);
+    gfits_set_bintable_column (&theader, &ftable, "FLAGS",    flags, Nsec);
+    gfits_set_bintable_column (&theader, &ftable, "NCODE",    Ncode, Nsec);
+    gfits_set_bintable_column (&theader, &ftable, "NUSED",    Nused, Nsec);
+    gfits_set_bintable_column (&theader, &ftable, "MAG_20",   M_20 , Nsec);
+    gfits_set_bintable_column (&theader, &ftable, "MAG_80",   M_80 , Nsec);
+
+    free (M      );
+    free (dM     );
+    free (Xm     );
+    free (flags  );
+    free (Ncode  );
+    free (Nused  );
+    free (M_20   );
+    free (M_80   );
+
+    gfits_fwrite_Theader (f, &theader);
+    gfits_fwrite_table  (f, &ftable);
+    gfits_free_header (&theader);
+    gfits_free_table (&ftable);
+  }
   return TRUE;
 }
 
 BrightCatalog *BrightCatalogMerge (Catalog *catalog, int Ncatalog) {
+
+  off_t i, j, k;
 
   BrightCatalog *bcatalog = NULL;
@@ -466,5 +481,7 @@
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Naverage; j++) {
-      bcatalog[0].average[Na] = catalog[i].average[j];
+      CopyAverageTiny (&bcatalog[0].average[Na], &catalog[i].average[j]);
+      bcatalog[0].average[Na].catID = catalog[i].catID;
+      int Nsecfilt = catalog[i].Nsecfilt;
       for (k = 0; k < Nsecfilt; k++) {
 	bcatalog[0].secfilt[Nsecfilt*Na + k] = catalog[i].secfilt[Nsecfilt*j + k];
@@ -474,5 +491,6 @@
     }
     for (j = 0; j < catalog[i].Nmeasure; j++) {
-      bcatalog[0].measure[Nm] = catalog[i].measure[j];
+      CopyMeasureTiny (&bcatalog[0].measure[Nm], &catalog[i].measure[j]);
+      bcatalog[0].measure[Nm].catID = catalog[i].catID;
       Nm++;
       assert (Nm <= Nmeas);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageOps.c	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageOps.c	(revision 33254)
@@ -58,4 +58,55 @@
   }
   llsortpair (imageIDs, imageIdx, Nimage);
+}
+
+void initImagesSubset (ImageSubset *input, off_t *line_number, off_t N) {
+
+  off_t i;
+
+  // we have been given an ImageSubset array, containing a reduced set of image fields
+  // create full a Image array and save the needed values
+  ALLOCATE (image, Image, N);
+  for (i = 0; i < N; i++) {
+    image[i].imageID       = input[i].imageID      ;
+    image[i].photom_map_id = input[i].photom_map_id;
+    image[i].flags         = input[i].flags        ;
+    image[i].Mcal          = input[i].Mcal         ;
+    image[i].dMcal         = input[i].dMcal        ;
+    image[i].tzero         = input[i].tzero        ;
+    image[i].trate         = input[i].trate        ;
+  }
+  LineNumber = line_number;
+  Nimage = N;
+
+  ALLOCATE (imageIDs, off_t, Nimage);
+  ALLOCATE (imageIdx, off_t, Nimage);
+
+  for (i = 0; i < Nimage; i++) {
+    imageIdx[i] = i;
+    imageIDs[i] = image[i].imageID;
+  }
+  llsortpair (imageIDs, imageIdx, Nimage);
+}
+
+ImageSubset *getimages_subset (off_t *N) {
+
+  *N = Nimage;
+
+  off_t i;
+
+  // we have been given an ImageSubset array, containing a reduced set of image fields
+  // create full a Image array and save the needed values
+  ImageSubset *subset = NULL;
+  ALLOCATE (subset, ImageSubset, Nimage);
+  for (i = 0; i < Nimage; i++) {
+    subset[i].imageID       = image[i].imageID      ;
+    subset[i].photom_map_id = image[i].photom_map_id;
+    subset[i].flags         = image[i].flags        ;
+    subset[i].Mcal          = image[i].Mcal         ;
+    subset[i].dMcal         = image[i].dMcal        ;
+    subset[i].tzero         = image[i].tzero        ;
+    subset[i].trate         = image[i].trate        ;
+  }
+  return subset;
 }
 
@@ -131,5 +182,5 @@
 }
 
-/* select all image equivalent to the active photcode set */
+/* select all images equivalent to the active photcode set */
 void findImages (Catalog *catalog, int Ncatalog, int doImageList) {
 
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageSubset.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageSubset.c	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageSubset.c	(revision 33254)
@@ -1,9 +1,13 @@
 # include "relphot.h"
 
-// this is (nearly?) identical to the one in 'uniphot/src' used by setphot / setphot_client
-// there may be difference in the specific columns used, however (if not, we can move to libdvo?)
+# define GET_COLUMN(OUT,NAME,TYPE) \
+  TYPE *OUT = gfits_get_bintable_column_data (&theader, &ftable, NAME, type, &Nrow, &Ncol); \
+  myAssert (!strcmp(type, #TYPE), "wrong column type");
+
+// this is nearly identical to the one in 'uniphot/src' used by setphot / setphot_client.
+// Here, we use a handful of different columns (if not, we could move to libdvo)
 ImageSubset *ImageSubsetLoad(char *filename, off_t *nimage) {
 
-  int i, Ncol, Ncol2;
+  int i, Ncol;
   off_t Nrow;
   Header header;
@@ -44,28 +48,16 @@
   // XXX close the fits file here
 
-  // a bit annoying : we read the entire block of data (180M), then extract the columns, then set the image structure values.
+  // a bit annoying : we read the entire block of data, then extract the columns, then set the image structure values.
   // this means I need 3 copies in memory at some point.  ugh.
 
   char type[16];
 
-  // need to create and assign to flat-field correction
-  float *Mcal = gfits_get_bintable_column_data (&theader, &ftable, "MCAL", type, &Nrow, &Ncol);
-  myAssert (!strcmp(type, "float"), "wrong column type");
-
-  float *dMcal = gfits_get_bintable_column_data (&theader, &ftable, "MCAL_ERR", type, &Nrow, &Ncol2);
-  myAssert (!strcmp(type, "float"), "wrong column type");
-  myAssert (Ncol == Ncol2, "table length mismatch");
-
-  unsigned int *imageID = gfits_get_bintable_column_data (&theader, &ftable, "IMAGE_ID", type, &Nrow, &Ncol2);
-  myAssert (!strcmp(type, "int"), "wrong column type");
-  myAssert (Ncol == Ncol2, "table length mismatch");
-
-  unsigned int *map = gfits_get_bintable_column_data (&theader, &ftable, "PHOTOM_MAP", type, &Nrow, &Ncol2);
-  myAssert (!strcmp(type, "int"), "wrong column type");
-  myAssert (Ncol == Ncol2, "table length mismatch");
-
-  unsigned int *flags = gfits_get_bintable_column_data (&theader, &ftable, "FLAGS", type, &Nrow, &Ncol2);
-  myAssert (!strcmp(type, "int"), "wrong column type");
-  myAssert (Ncol == Ncol2, "table length mismatch");
+  GET_COLUMN (Mcal,    "MCAL",       float);
+  GET_COLUMN (dMcal,   "MCAL_ERR",   float);
+  GET_COLUMN (imageID, "IMAGE_ID",   int);
+  GET_COLUMN (map,     "PHOTOM_MAP", int);
+  GET_COLUMN (flags,   "FLAGS",      int);
+  GET_COLUMN (tzero,   "TZERO",      int);
+  GET_COLUMN (trate,   "TRATE",      char);
 
   // XXX free the fits table data here 
@@ -73,9 +65,11 @@
   ALLOCATE (image, ImageSubset, Nrow);
   for (i = 0; i < Nrow; i++) {
+    image[i].Mcal          = Mcal[i];
+    image[i].dMcal         = dMcal[i];
     image[i].imageID       = imageID[i];
     image[i].photom_map_id = map[i];
     image[i].flags         = flags[i];
-    image[i].Mcal          = Mcal[i];
-    image[i].dMcal         = dMcal[i];
+    image[i].tzero         = tzero[i];
+    image[i].trate         = trate[i];
   }
   fprintf (stderr, "loaded data for %lld images\n", (long long) Nrow);
@@ -86,4 +80,6 @@
   free (map);
   free (flags);
+  free (tzero);
+  free (trate);
 
   *nimage = Nrow;
@@ -91,5 +87,5 @@
 }
 
-int ImageSubsetSave(char *filename, Image *image, off_t Nimage) {
+int ImageSubsetSave(char *filename, ImageSubset *image, off_t Nimage) {
 
   int i;
@@ -109,8 +105,10 @@
   gfits_define_bintable_column (&theader, "E", "MCAL_ERR", "zero point error", "magnitudes", 1.0, 0.0);
 
-  // an unsigned int needs to have bzero of 0x8000
   gfits_define_bintable_column (&theader, "J", "IMAGE_ID", "image ID", NULL, 1.0, 1.0*0x8000);
   gfits_define_bintable_column (&theader, "J", "PHOTOM_MAP", "map", NULL, 1.0, 1.0*0x8000);
   gfits_define_bintable_column (&theader, "J", "FLAGS", "flags", NULL, 1.0, 1.0*0x8000);
+
+  gfits_define_bintable_column (&theader, "J", "TZERO", "exposure start", NULL, 1.0, 1.0*0x8000);
+  gfits_define_bintable_column (&theader, "I", "TRATE", "tti rate", NULL, 1.0, 0.0);
 
   // generate the output array that carries the data
@@ -118,20 +116,25 @@
 
   float *Mcal, *dMcal;
-  unsigned int *imageID, *map, *flags;
+  unsigned int *imageID, *map, *flags, *tzero;
+  unsigned char *trate;
 
-  // create intermediate storage arrays
-  ALLOCATE (Mcal,  float, Nimage);
-  ALLOCATE (dMcal, float, Nimage);
+  // create interme  diate storage arrays
+  ALLOCATE (Mcal,    float, 	   Nimage);
+  ALLOCATE (dMcal,   float, 	   Nimage);
   ALLOCATE (imageID, unsigned int, Nimage);
   ALLOCATE (map,     unsigned int, Nimage);
   ALLOCATE (flags,   unsigned int, Nimage);
+  ALLOCATE (tzero,   unsigned int, Nimage);
+  ALLOCATE (trate,   unsigned char, Nimage);
 
   // assign the storage arrays
   for (i = 0; i < Nimage; i++) {
+    Mcal[i]    = image[i].Mcal;
+    dMcal[i]   = image[i].dMcal;
     imageID[i] = image[i].imageID;
     map[i]     = image[i].photom_map_id;
     flags[i]   = image[i].flags;
-    Mcal[i]    = image[i].Mcal;
-    dMcal[i]   = image[i].dMcal;
+    tzero[i]   = image[i].tzero;
+    trate[i]   = image[i].trate;
   }
 
@@ -142,4 +145,6 @@
   gfits_set_bintable_column (&theader, &ftable, "PHOTOM_MAP", map,     Nimage);
   gfits_set_bintable_column (&theader, &ftable, "FLAGS",      flags,   Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "TZERO",      tzero,   Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "TRATE",      trate,   Nimage);
 
   free (Mcal);
@@ -148,4 +153,6 @@
   free (map);
   free (flags);
+  free (tzero);
+  free (trate);
 
   FILE *f = fopen (filename, "w");
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/args.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/args.c	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/args.c	(revision 33254)
@@ -311,5 +311,5 @@
     if (MODE) {
       fprintf (stderr, "ERROR: cannot use both -load and -update options!\n");
-      usage();
+      relphot_client_usage();
     }
     MODE = MODE_UPDATE;
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/initialize.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/initialize.c	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/initialize.c	(revision 33254)
@@ -96,7 +96,5 @@
 }
 
-void initialize_relphot_client (int argc, char **argv) {
-
-  int N;
+void initialize_client (int argc, char **argv) {
 
   relphot_client_help (argc, argv);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/load_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/load_catalogs.c	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/load_catalogs.c	(revision 33254)
@@ -16,4 +16,5 @@
 
   // XXX need to decide how to determine PARALLEL mode...
+# define PARALLEL 1
   if (PARALLEL & !hostID) {
     catalog = load_catalogs_parallel (skylist, Ncatalog);
@@ -30,4 +31,5 @@
   // load data from each region file, only use bright stars
   for (i = 0; i < skylist[0].Nregions; i++) {
+    // XXX keep in mind that not all catalogs are loaded
     dvo_catalog_init (&catalog[i], TRUE);
 
@@ -90,5 +92,8 @@
 
 // CATDIR is supplied globally
-Catalog *load_catalogs_parallel (SkyTable *sky, int *Ncatalog) {
+# define DEBUG 1
+Catalog *load_catalogs_parallel (SkyList *sky, int *Ncatalog) {
+
+  int Nsecfilt  = GetPhotcodeNsecfilt ();               // set the desired number in case we need to create the catalog
 
   // launch the setphot_client jobs to the parallel hosts
@@ -130,6 +135,6 @@
   // we don't know how many catalogs we actually need.  allocate a single entry, then
   // below we will allocate the max number needed after the BrightCatalog is loaded
-  int Ncatalog = 0;
   int NCATALOG = 1;
+  Catalog *catalog = NULL;
   ALLOCATE (catalog, Catalog, NCATALOG);
 
@@ -157,4 +162,10 @@
     assert (bcatalog);
     
+    // XXX this is wrong : I need to distinguish between the array of catalogs
+    // and the range of catID values.
+
+    // I need to determine the set of unique catID values and count them,
+    // I'll also need a lookup table from catID -> index.
+
     // XXX put the code below into a function equivalent to BrightCatalogMerge?
 
@@ -162,4 +173,5 @@
     // just average?)
     int catIDmax = 0;
+    int j;
     for (j = 0; j < bcatalog->Naverage; j++) {
       catIDmax = MAX(catIDmax, bcatalog->average[j].catID);
@@ -173,12 +185,12 @@
     REALLOCATE (NAVERAGE, int, NCATALOG);
     REALLOCATE (NMEASURE, int, NCATALOG);
-    for (i = NCATALOG_OLD; i < NCATALOG; i++) {
-      NAVERAGE[i] = 100;
-      NMEASURE[i] = 100;
-      catalog[i].Naverage = 0;
-      catalog[i].Nmeasure = 0;
-      ALLOCATE (catalog[i].averageT, AverageTiny, NAVERAGE[i]);
-      ALLOCATE (catalog[i].measureT, MeasureTiny, NMEASURE[i]);
-      ALLOCATE (catalog[i].secfilt, SecFilt, Nsecfilt*NAVERAGE[i]);
+    for (j = NCATALOG_OLD; j < NCATALOG; j++) {
+      NAVERAGE[j] = 100;
+      NMEASURE[j] = 100;
+      catalog[j].Naverage = 0;
+      catalog[j].Nmeasure = 0;
+      ALLOCATE (catalog[j].averageT, AverageTiny, NAVERAGE[j]);
+      ALLOCATE (catalog[j].measureT, MeasureTiny, NMEASURE[j]);
+      ALLOCATE (catalog[j].secfilt, SecFilt, Nsecfilt*NAVERAGE[j]);
     }
 
@@ -189,10 +201,11 @@
     // assign the averages to the corresponding catalog
     for (j = 0; j < bcatalog->Naverage; j++) {
-      Nc = bcatalog->average[j].catID;
+      int Nc = bcatalog->average[j].catID;
       assert (Nc < NCATALOG);
-      Na = catalog[Nc].Naverage;
+      int Na = catalog[Nc].Naverage;
       catalog[Nc].averageT[Na] = bcatalog->average[j];
 
       // secfilt entries are grouped in blocks for each average
+      int k;
       for (k = 0; k < Nsecfilt; k++) {
 	catalog[Nc].secfilt[Na*Nsecfilt + k] = bcatalog->secfilt[j*Nsecfilt + k];
@@ -200,5 +213,5 @@
 
       catalog[Nc].Naverage ++;
-      if (catalog[Nc].Naverage >= NAVERAGE[N]) {
+      if (catalog[Nc].Naverage >= NAVERAGE[Nc]) {
 	NAVERAGE[Nc] += 100;
 	REALLOCATE (catalog[Nc].averageT, AverageTiny, NAVERAGE[Nc]);
@@ -210,10 +223,10 @@
     // XXX what about averef and related links?  Do I need them?
     for (j = 0; j < bcatalog->Nmeasure; j++) {
-      Nc = bcatalog->measure[j].catID;
+      int Nc = bcatalog->measure[j].catID;
       assert (Nc < NCATALOG);
-      Na = catalog[Nc].Nmeasure;
+      int Na = catalog[Nc].Nmeasure;
       catalog[Nc].measureT[Na] = bcatalog->measure[j];
       catalog[Nc].Nmeasure ++;
-      if (catalog[Nc].Nmeasure >= NMEASURE[N]) {
+      if (catalog[Nc].Nmeasure >= NMEASURE[Nc]) {
 	NMEASURE[Nc] += 100;
 	REALLOCATE (catalog[Nc].measureT, MeasureTiny, NMEASURE[Nc]);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/reload_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/reload_catalogs.c	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/reload_catalogs.c	(revision 33254)
@@ -1,3 +1,5 @@
 # include "relphot.h"
+
+# define DEBUG 1
 
 # define TIMESTAMP(TIME) \
@@ -7,5 +9,5 @@
     gettimeofday (&start, (void *) NULL);
 
-void reload_catalogs (SkyList *skylist, FlatCorrectionTable *flatcorr) {
+void reload_catalogs (SkyList *skylist, FlatCorrectionTable *flatcorr, int hostID, char *hostpath) {
 
   int i;
@@ -25,10 +27,23 @@
   double time7 = 0.0;
 
+  // XXX need to decide how to determine PARALLEL mode...
+# define PARALLEL 1
+  if (PARALLEL & !hostID) {
+    reload_catalogs_parallel (skylist);
+    return;
+  }
+
   if (VERBOSE) fprintf (stderr, "re-loading catalog data\n");
 
   /* load data from each region file */
   for (i = 0; i < skylist[0].Nregions; i++) {
+    if (hostID && (skylist[0].regions[i]->hostID != hostID)) continue;
+
     gettimeofday (&start, (void *) NULL);
-    catalog.filename = skylist[0].filename[i];
+
+    // set up the basic catalog info
+    char hostfile[1024];
+    snprintf (hostfile, 1024, "%s/%s.cpt", hostpath, skylist[0].regions[i]->name);
+    catalog.filename = hostID ? hostfile : skylist[0].filename[i];
 
     // only update existing db tables
@@ -60,4 +75,5 @@
     TIMESTAMP(time3a);
 
+    // XXX need to worry about the image subset data
     initImageBins  (&catalog, 1, FALSE);
     initMosaicBins (&catalog, 1, FALSE);
@@ -95,5 +111,9 @@
 }
 
-int reload_catalogs_parallel (SkyTable *sky, Image *image, off_t Nimage) {
+// XXX Image to Image Subset
+int reload_catalogs_parallel (SkyList *sky) {
+
+  off_t Nimage;
+  ImageSubset *image = getimages_subset (&Nimage);
 
   // write out the subset table of image information
@@ -104,4 +124,5 @@
     exit (1);
   }
+  free (image);
 
   // now launch the relphot_client jobs to the parallel hosts
@@ -114,6 +135,6 @@
 
     char command[1024];
-    snprintf (command, 1024, "relphot_client -reload -hostname %s -catdir %s -hostdir %s -images %s", 
-	      table->hosts[i].hostname, CATDIR, table->hosts[i].pathname, imageFile);
+    snprintf (command, 1024, "relphot_client -reload %s -hostname %s -catdir %s -hostdir %s", 
+	      imageFile, table->hosts[i].hostname, CATDIR, table->hosts[i].pathname);
 
     fprintf (stderr, "command: %s\n", command);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot.c	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot.c	(revision 33254)
@@ -69,6 +69,6 @@
   FlatCorrectionTable *flatcorr = FlatCorrectionLoad (flatcorrfile, VERBOSE);
 
-  /* load catalog data from region files */
-  catalog = load_catalogs (skylist, &Ncatalog);
+  /* load catalog data from region files (hostID is 0 since we are not a client */
+  catalog = load_catalogs (skylist, &Ncatalog, 0, NULL);
   MARKTIME("-- load catalog data: %f sec\n", dtime);
   
@@ -241,9 +241,5 @@
 
   /* load catalog data from region files, update Mrel include all data */
-  if (PARALLEL) {
-    reload_catalogs_parallel ();
-  } else {
-    reload_catalogs (skylist, flatcorr);
-  }
+  reload_catalogs (skylist, flatcorr, 0, NULL);
   MARKTIME("-- updated all catalogs: %f sec\n", dtime);
 
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot_client.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot_client.c	(revision 33253)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/relphot_client.c	(revision 33254)
@@ -17,23 +17,18 @@
 int main (int argc, char **argv) {
 
-  // get configuration info, args, lockfile (set CATDIR, HOSTDIR, HOSTNAME, IMAGES) 
-  // XXX pass in both HOSTDIR and HOSTID or just HOSTNAME (and read the host table)?
-  initialize_relphot_client (argc, argv);
-
-  // need to supply the region selected by the calling program -> UserPatch
+  // get configuration info, args, lockfile (set CATDIR, HOST_ID, HOSTDIR, etc) 
+  initialize_client (argc, argv);
 
   // load the current sky table (layout of all SkyRegions) 
-  sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, TRUE, -1, VERBOSE);
+  SkyTable *sky = SkyTableLoadOptimal (CATDIR, NULL, NULL, TRUE, -1, VERBOSE);
   SkyTableSetFilenames (sky, CATDIR, "cpt");
 
-  skylist = SkyListByPatch (sky, -1, &UserPatch);
+  SkyList *skylist = SkyListByPatch (sky, -1, &UserPatch);
   
   switch (MODE) {
     case MODE_LOAD: {
-      // need to pass in HOSTDIR, HOST_ID,
+      int Ncatalog;
       Catalog *catalog = load_catalogs (skylist, &Ncatalog, HOST_ID, HOSTDIR);
-   
-      BrightCatalog *bcatalog = BrightCatalogMerge (Catalog *catalog, int Ncatalog);
-
+      BrightCatalog *bcatalog = BrightCatalogMerge (catalog, Ncatalog);
       if (!BrightCatalogSave (BCATALOG, bcatalog)) {
 	abort();
@@ -47,8 +42,13 @@
       ImageSubset *image = ImageSubsetLoad (IMAGES, &Nimage);
       
+      // save the available image information in the static array in ImageOps.c
+      initImagesSubset (image, NULL, Nimage);
+
       // load the flat-field correction table from CATDIR
       char flatcorrFile[1024];
       snprintf (flatcorrFile, 1024, "%s/flatcorr.fits", CATDIR);
-      FlatCorrectionTable *flatcorrTable = FlatCorrectionLoad (flatcorrFile, VERBOSE);
+      FlatCorrectionTable *flatcorr = FlatCorrectionLoad (flatcorrFile, VERBOSE);
+
+      reload_catalogs (skylist, flatcorr, HOST_ID, HOSTDIR);
       break;
     }
