Index: /trunk/Ohana/configure.tcsh
===================================================================
--- /trunk/Ohana/configure.tcsh	(revision 21507)
+++ /trunk/Ohana/configure.tcsh	(revision 21508)
@@ -12,4 +12,5 @@
 set memcheck = 0
 set use_tcmalloc = 0
+set use_gnu99 = 0
 
 set prefix  = ""
@@ -70,4 +71,7 @@
    set use_tcmalloc = 1
    breaksw;
+  case --use-gnu99
+   set use_gnu99 = 1
+   breaksw;
   case --pedantic
    set pedantic = 1
@@ -167,4 +171,5 @@
   set CFLAGS = "-g -O0"
 endif  
+
 # optimize overrides user-supplied CFLAGS
 if ($optimize) set CFLAGS = "-O2"
@@ -172,4 +177,7 @@
 # profiler code
 if ($profile) set CFLAGS = "$CFLAGS -pg"
+
+# use_gnu99
+if ($use_gnu99) set CFLAGS = "$CFLAGS -std=gnu99"
 
 if (! $?CPPFLAGS) then
Index: /trunk/Ohana/src/addstar/Makefile
===================================================================
--- /trunk/Ohana/src/addstar/Makefile	(revision 21507)
+++ /trunk/Ohana/src/addstar/Makefile	(revision 21508)
@@ -27,5 +27,9 @@
 mkcmf       : $(BIN)/mkcmf.$(ARCH)
 
-all: addstar addstarc addstard addstart sedstar load2mass skycells
+all: addstar sedstar load2mass skycells
+
+# I need to fix the client/server version of addstar now that I have dropped Stars
+# as an autocode type and have converted to carrying average and measure
+# all: addstar addstarc addstard addstart sedstar load2mass skycells
 
 ADDSTAR = \
@@ -48,9 +52,4 @@
 $(SRC)/find_matches_refstars.$(ARCH).o \
 $(SRC)/find_subset.$(ARCH).o \
-$(SRC)/get2mass.$(ARCH).o \
-$(SRC)/get2mass_as.$(ARCH).o \
-$(SRC)/get2mass_as_raw.$(ARCH).o \
-$(SRC)/get2mass_dr2.$(ARCH).o \
-$(SRC)/get2mass_ops.$(ARCH).o \
 $(SRC)/getgsc.$(ARCH).o \
 $(SRC)/gettycho.$(ARCH).o \
@@ -71,4 +70,5 @@
 $(SRC)/replace_match.$(ARCH).o \
 $(SRC)/resort_catalog.$(ARCH).o \
+$(SRC)/StarOps.$(ARCH).o \
 $(SRC)/ReadStarsFITS.$(ARCH).o \
 $(SRC)/ReadStarsTEXT.$(ARCH).o \
@@ -106,9 +106,4 @@
 $(SRC)/find_matches_refstars.$(ARCH).o \
 $(SRC)/find_subset.$(ARCH).o \
-$(SRC)/get2mass.$(ARCH).o \
-$(SRC)/get2mass_as.$(ARCH).o \
-$(SRC)/get2mass_as_raw.$(ARCH).o \
-$(SRC)/get2mass_dr2.$(ARCH).o \
-$(SRC)/get2mass_ops.$(ARCH).o \
 $(SRC)/getgsc.$(ARCH).o \
 $(SRC)/gettycho.$(ARCH).o \
@@ -141,9 +136,4 @@
 $(SRC)/find_matches_refstars.$(ARCH).o \
 $(SRC)/find_subset.$(ARCH).o \
-$(SRC)/get2mass.$(ARCH).o \
-$(SRC)/get2mass_as.$(ARCH).o \
-$(SRC)/get2mass_as_raw.$(ARCH).o \
-$(SRC)/get2mass_dr2.$(ARCH).o \
-$(SRC)/get2mass_ops.$(ARCH).o \
 $(SRC)/getgsc.$(ARCH).o \
 $(SRC)/getusno.$(ARCH).o \
@@ -209,4 +199,5 @@
 $(SRC)/replace_match.$(ARCH).o \
 $(SRC)/update_coords.$(ARCH).o \
+$(SRC)/StarOps.$(ARCH).o \
 $(SRC)/ConfigInit.$(ARCH).o \
 $(SRC)/Shutdown.$(ARCH).o \
@@ -252,5 +243,10 @@
 $(BIN)/mkcmf.$(ARCH)       : $(MKCMF)
 
-INSTALL = addstar addstarc addstard addstart sedstar load2mass skycells mkcmf
+INSTALL = addstar sedstar load2mass skycells mkcmf
+
+# I need to fix the client/server version of addstar now that I have dropped Stars
+# as an autocode type and have converted to carrying average and measure
+# all: addstar addstarc addstard addstart sedstar load2mass skycells
+# INSTALL = addstar addstarc addstard addstart sedstar load2mass skycells mkcmf
 
 # dependancy rules for binary code #########################
Index: /trunk/Ohana/src/addstar/doc/notes.txt
===================================================================
--- /trunk/Ohana/src/addstar/doc/notes.txt	(revision 21507)
+++ /trunk/Ohana/src/addstar/doc/notes.txt	(revision 21508)
@@ -1,2 +1,28 @@
+
+2009.02.11
+
+  updating DVO tables with more fields: cleaning up the conversions
+  from external formats to the DVO internal formats:  I am changing
+  the APIs to convert directly to the Measure structure, including
+  conversion functions for the compressed fields (ie, pixels carried
+  as short).
+
+  A few remaining issues:
+
+  * t_msec : I've added a field to carry the fractional seconds, but
+    nothing is yet setting this value, and the query functions do not
+    use this value.
+
+  * various fields are reported in either arcseconds or pixels (eg,
+    FWHMx, moments, error in centroid).  These are currently not very
+    consistent, and should be tightened up...
+
+    ** if we store these values in pixels, then cameras with a wide
+       range of plate scales (GPC1 << SkyProbe << AllSky) will all fit
+       in field with a small dynamic range; since we also provide the
+       plate scale (as a float), conversions are easy:
+       GPC1     : 0.26 arcsec / pixel
+       SkyProbe : 4.5 arcsec / pixel
+       AllSky   : 3600.0 arcsec / pixel 
 
 2008.10.11
Index: /trunk/Ohana/src/addstar/include/addstar.h
===================================================================
--- /trunk/Ohana/src/addstar/include/addstar.h	(revision 21507)
+++ /trunk/Ohana/src/addstar/include/addstar.h	(revision 21508)
@@ -28,4 +28,18 @@
     fprintf (stderr, "ERROR:  photcode %s not found in photcode table\n", NAME); \
     exit (0); }
+
+typedef struct {
+  char *exthead;
+  char *extdata;
+  char *exttype;
+  int extnum_head;
+  int extnum_data;
+} HeaderSet;
+
+typedef struct {
+  Average average;
+  Measure measure;
+  int found;
+} Stars;
 
 typedef struct {
@@ -39,12 +53,4 @@
   char *refcat;
 } DVO_DATA;
-
-typedef struct {
-  char *exthead;
-  char *extdata;
-  char *exttype;
-  int extnum_head;
-  int extnum_data;
-} HeaderSet;
 
 typedef struct sockaddr_in SockAddress;
@@ -92,4 +98,5 @@
 /* these globals modify the behavior of gstars (KEEP) */
 double 	SNLIMIT;
+int     PHOTFLAG_EXCLUDE;
 int    	ACCEPT_ASTROM;  // accept even bad astrometry solutions (NASTRO == 0)
 int    	ACCEPT_MOTION;  // accept reference proper motion measurements
@@ -137,6 +144,8 @@
 /*** addstar prototypes ***/
 
+AddstarClientOptions ConfigInit   PROTO((int *argc, char **argv));
+AddstarClientOptions args         PROTO((int argc, char **argv, AddstarClientOptions options));
+
 void       AddToCalibration       PROTO((Average *average, SecFilt *secfilt, Measure *measure, Measure *new, int *next, int Nstar));
-AddstarClientOptions ConfigInit   PROTO((int *argc, char **argv));
 void       FindCalibration        PROTO((Image *image));
 FILE      *GetDB                  PROTO((int *state));
@@ -145,27 +154,31 @@
 void       SetProtect             PROTO((int mode));
 int        SetSignals             PROTO((void));
-int        Shutdown               PROTO((char *message, ...); ) 
+int        Shutdown               PROTO((char *message, ...));
 void       TrapSignal             PROTO((int sig));
 float      airmass                PROTO((float secz_image, double ra, double dec, double st, double latitude));
+float      azimuth                PROTO((double ha, double dec, double latitude));
 void       SetAirmassQuality      PROTO((int quality));
 SkyTable  *SkyTableFromTychoIndex PROTO((char *filename, int VERBOSE));
-AddstarClientOptions args         PROTO((int argc, char **argv, AddstarClientOptions options));
 void       check_permissions      PROTO((char *basefile));
 int        dump_rawstars          PROTO((Stars *stars, int Nstars));
 int        edge_check             PROTO((double *x1, double *y1, double *x2, double *y2));
 Image     *fakeimage              PROTO((char *rootname, int *Nimage, int photcode));
+
 int        find_matches           PROTO((SkyRegion *region, Stars *stars, int Nstars, Catalog *catalog, AddstarClientOptions options));
 int        find_matches_closest   PROTO((SkyRegion *region, Stars *stars, int Nstars, Catalog *catalog, AddstarClientOptions options));
 int        find_matches_refstars  PROTO((SkyRegion *region, Stars **stars, int Nstars, Catalog *catalog, AddstarClientOptions options));
+
 Stars    **find_subset            PROTO((SkyRegion *region, Stars *stars, int Nstars, int *NSTARS));
 int        gcatalog               PROTO((Catalog *catalog));
 Stars     *get2mass               PROTO((SkyRegion *patch, int photcode, int mode, int *NSTARS));
 double     get_subpix             PROTO((double x, double y));
+
+Stars     *greference             PROTO((char *Refcat, SkyRegion *catstats, int photcode, int *nstars));
 Stars     *getgsc                 PROTO((SkyRegion *patch, int *NSTARS));
 Stars     *gettycho               PROTO((SkyRegion *catstats, int photcode, int *Nstars));
 Stars     *getusno                PROTO((SkyRegion *catstats, int photcode, int *Nstars));
 Stars     *getusnob               PROTO((SkyRegion *catstats, int photcode, int *Nstars));
-Image     *gimages                PROTO((FITS_DB *db, Image *image, Coords *mosaic, int *Npimage));
-Stars     *grefcat                PROTO((char *Refcat, SkyRegion *catstats, int photcode, int *nstars));
+
+// load text-based stars (REF only in the sense of REF photcodes)
 Stars     *grefstars              PROTO((char *file, int photcode, int *Nstars));
 
@@ -249,4 +262,9 @@
 Stars     *Convert_PS1_DEV_0	  PROTO((FTable *table, int *nstars));
 Stars     *Convert_PS1_DEV_1	  PROTO((FTable *table, int *nstars));
+Stars     *Convert_PS1_V1	  PROTO((FTable *table, int *nstars));
+
+int        InitStar               PROTO((Stars *star));
+short      ShortPixels            PROTO((float pixels));
+short      ShortDegree            PROTO((float degrees));
 
 int args_skycells (int argc, char **argv);
Index: /trunk/Ohana/src/addstar/include/skycells.h
===================================================================
--- /trunk/Ohana/src/addstar/include/skycells.h	(revision 21507)
+++ /trunk/Ohana/src/addstar/include/skycells.h	(revision 21508)
@@ -30,5 +30,5 @@
   int NX;
   int NY;
-  int code;
+  int photcode;
   char name[64];
 } SkyRectangle;
Index: /trunk/Ohana/src/addstar/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ConfigInit.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/ConfigInit.c	(revision 21508)
@@ -39,4 +39,8 @@
   ScanConfig (config, "MIN_SN_FSTAT",           "%lf", 0, &SNLIMIT);
   ScanConfig (config, "ADDSTAR_SNLIMIT",        "%lf", 0, &SNLIMIT);
+
+  /* exclude stars with bits that match the given photFlags bits */
+  PHOTFLAG_EXCLUDE = 0;
+  ScanConfig (config, "ADDSTAR_PHOTFLAG_EXCLUDE", "%d", 0, &PHOTFLAG_EXCLUDE);
 
   MAX_CERROR = 0.5; // arcseconds
Index: /trunk/Ohana/src/addstar/src/FilterStars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/FilterStars.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/FilterStars.c	(revision 21508)
@@ -1,4 +1,13 @@
 # include "addstar.h"
+# define EXTERNAL_ID FALSE
 
+/* exclude some detections based on various filter options:
+ * *** S/N limit
+ * *** Acceptable window on detector
+ * *** bad photFlags
+ */
+
+// the imageID supplied here is the sequence **within this set**
+// this value is updated based on the image table later
 Stars *FilterStars (Stars *instars, Image *image, unsigned int imageID) {
 
@@ -19,33 +28,39 @@
   ALLOCATE (stars, Stars, image[0].nstar);
   for (N = j = 0; j < image[0].nstar; j++) {
+
     /* allow for some dynamic filtering of star list */
-    if (SNLIMIT && instars[j].dM > SNLIMIT) continue;
-    if (XMAX && (instars[j].X > XMAX)) continue;
-    if (XMIN && (instars[j].X < XMIN)) continue;
-    if (YMAX && (instars[j].Y > YMAX)) continue;
-    if (YMIN && (instars[j].Y < YMIN)) continue;
+    if (SNLIMIT && instars[j].measure.dM > SNLIMIT) continue;
+    if (XMAX && (instars[j].measure.Xccd > XMAX)) continue;
+    if (XMIN && (instars[j].measure.Xccd < XMIN)) continue;
+    if (YMAX && (instars[j].measure.Yccd > YMAX)) continue;
+    if (YMIN && (instars[j].measure.Yccd < YMIN)) continue;
+    if (PHOTFLAG_EXCLUDE && (instars[j].measure.photFlags & PHOTFLAG_EXCLUDE)) continue;
+
     stars[N] = instars[j];
 
-    XY_to_RD (&stars[N].R, &stars[N].D, stars[N].X, stars[N].Y, &image[0].coords);
-    stars[N].R = ohana_normalize_angle (stars[N].R);
-    stars[N].found = -1;
-    stars[N].code = image[0].photcode;
+    XY_to_RD (&stars[N].average.R, &stars[N].average.D, stars[N].measure.Xccd, stars[N].measure.Yccd, &image[0].coords);
+    stars[N].average.R = ohana_normalize_angle (stars[N].average.R);
 
-    RMIN = MIN (RMIN, stars[N].R);
-    RMAX = MAX (RMAX, stars[N].R);
-    DMIN = MIN (DMIN, stars[N].D);
-    DMAX = MAX (DMAX, stars[N].D);
+    stars[N].measure.photcode = image[0].photcode;
+
+    // determine the full coverage of this set of measurements
+    RMIN = MIN (RMIN, stars[N].average.R);
+    RMAX = MAX (RMAX, stars[N].average.R);
+    DMIN = MIN (DMIN, stars[N].average.D);
+    DMAX = MAX (DMAX, stars[N].average.D);
+
     /** additional quantities to supply to Stars based on the image data **/
 
-    /* calculate accurate per-star airmass */
-    stars[N].airmass = airmass (image[0].secz, stars[N].R, stars[N].D, image[0].sidtime, image[0].latitude);
-    stars[N].Mcal    = image[0].Mcal;
-    stars[N].t       = image[0].tzero + 1e-4*stars[N].Y*image[0].trate;  /* trate is in 0.1 msec / row */
-    stars[N].dt      = MTIME;
+    /* calculate accurate per-star airmass and azimuth */
+    stars[N].measure.airmass = airmass (image[0].secz, stars[N].average.R, stars[N].average.D, image[0].sidtime, image[0].latitude);
+    stars[N].measure.az      = azimuth (image[0].sidtime - stars[N].average.R, stars[N].average.D, image[0].latitude);
+    stars[N].measure.Mcal    = image[0].Mcal;
+    stars[N].measure.t       = image[0].tzero + 1e-4*stars[N].measure.Yccd*image[0].trate;  /* trate is in 0.1 msec / row */
+    stars[N].measure.dt      = MTIME;
 
-    if ((stars[N].M > 25.0) && (stars[N].M < 32.0)) {
+    // watch out for any strange values:
+    if ((stars[N].measure.M > 25.0) && (stars[N].measure.M < 32.0)) {
       fprintf (stderr, "*");
     }
-
     // stars->M is either NAN or a valid inst magnitude
     // stars->dM is either NAN or a valid error
@@ -54,22 +69,33 @@
     dMx = 0.0;
     if (SUBPIX) {
-      dMs = get_subpix (stars[N].X, stars[N].Y);
-      dMx = scat_subpix (stars[N].X, stars[N].Y);
-      if (!isnan(stars[N].dM)) {
-	stars[N].dM = hypot (stars[N].dM, dMx);
+      dMs =  get_subpix (stars[N].measure.Xccd, stars[N].measure.Yccd);
+      dMx = scat_subpix (stars[N].measure.Xccd, stars[N].measure.Yccd);
+      if (!isnan(stars[N].measure.dM)) {
+	stars[N].measure.dM = hypot (stars[N].measure.dM, dMx);
       }
     }
 
-    if (!isnan(stars[N].M)) {
-      stars[N].M += MTIME - dMs;
+    if (!isnan(stars[N].measure.M)) {
+      stars[N].measure.M   += MTIME - dMs;
     }
-    if (!isnan(stars[N].Map)) {
-      stars[N].Map += MTIME - dMs;
+    if (!isnan(stars[N].measure.Map)) {
+      stars[N].measure.Map += MTIME - dMs;
     }
     
-    // XXX currently, this ID is internal only; 
-    // should we use the psphot / other external ID, if available?
-    stars[N].detID = N; // sequence number within image
-    stars[N].imageID = imageID; // does this need to be updated?
+    // the external ID is supplied, but do we trust it?
+    if (!EXTERNAL_ID) {
+      stars[N].measure.detID = N; // sequence number within image
+    }
+
+# if (0) 
+    if (PSPS_ID) {
+      double mjd;
+      mjd = ohana_sec_to_mjd (image[0].tzero);
+      // XXX this is clearly wrong : what does PSPS want?
+      stars[N].measure.extID = PSPS_create_detectid (mjd, imageID);
+    }
+# endif
+
+    stars[N].measure.imageID = imageID; // this value is update in UpdateImageIDs
 
     N ++;
Index: /trunk/Ohana/src/addstar/src/GetFileMode.c
===================================================================
--- /trunk/Ohana/src/addstar/src/GetFileMode.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/GetFileMode.c	(revision 21508)
@@ -7,4 +7,5 @@
   int Naxis;
   int simple, extend, haveNaxis, haveCTYPE;
+  int tmp, havePHOT_VER, haveTARG_VER;
 
   gfits_scan (header, "SIMPLE", "%t", 1, &simple);
@@ -14,13 +15,9 @@
   gfits_scan (header, "EXTEND", "%t", 1, &extend);
     
-{
-    int tmp, havePHOT_VER, haveTARG_VER;
-
-    // SDSS tsObj files have a version number for the PHOTO and 
-    // TS (target selection) pipelines present as header keywords
-    havePHOT_VER = gfits_scan (header, "PHOT_VER", "%s", 1, &tmp);
-    haveTARG_VER = gfits_scan (header, "TARG_VER", "%s", 1, &tmp);
-    if (havePHOT_VER && haveTARG_VER) return SDSS_OBJ;
-}
+  // SDSS tsObj files have a version number for the PHOTO and 
+  // TS (target selection) pipelines present as header keywords
+  havePHOT_VER = gfits_scan (header, "PHOT_VER", "%s", 1, &tmp);
+  haveTARG_VER = gfits_scan (header, "TARG_VER", "%s", 1, &tmp);
+  if (havePHOT_VER && haveTARG_VER) return SDSS_OBJ;
 
   if ((Naxis == 2) || TEXTMODE || !simple) {
Index: /trunk/Ohana/src/addstar/src/LoadData.c
===================================================================
--- /trunk/Ohana/src/addstar/src/LoadData.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/LoadData.c	(revision 21508)
@@ -41,4 +41,5 @@
     // I need to detect them here and load them with ReadStarsTEXT instead of calling the code
     // below.
+    // inStars = ReadStarsFITS (f, headers[Nhead], headers[Ndata], &images[0][Nvalid].nstar);
 
     // XXX use something to set the chip name? EXTNAME?
Index: /trunk/Ohana/src/addstar/src/LoadDataPMM.c
===================================================================
--- /trunk/Ohana/src/addstar/src/LoadDataPMM.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/LoadDataPMM.c	(revision 21508)
@@ -82,11 +82,9 @@
 
     for (i = 0; i < Nline; i++) {
-      // fscanf (f, "%lf %lf %lf", &ra, &dec, &mag) != EOF) {
-
       dparse (&ra,  1, &buffer[i*NBYTE_ASC_TABLE]);
       dparse (&dec, 2, &buffer[i*NBYTE_ASC_TABLE]);
       dparse (&mag, 3, &buffer[i*NBYTE_ASC_TABLE]);
 
-      memset (&inStars[Ninstars], 0, sizeof(Stars));
+      InitStar (&inStars[Ninstars]);
 
       if (ra > 180) {
@@ -100,13 +98,14 @@
       maxD = MAX(maxD, dec);
 
-      inStars[Ninstars].M       = mag - ZeroPoint + ZeroPt;
-      inStars[Ninstars].R       = ra;
-      inStars[Ninstars].D       = dec;
-      inStars[Ninstars].t       = images[0][0].tzero;
-      inStars[Ninstars].dt      = images[0][0].exptime;
-      inStars[Ninstars].code    = code;
-      inStars[Ninstars].airmass = airmass;
-      inStars[Ninstars].az      = az;
-      inStars[Ninstars].found   = -1; // found starts at -1 == not yet found
+      inStars[Ninstars].average.R        = ra;
+      inStars[Ninstars].average.D        = dec;
+
+      inStars[Ninstars].measure.M        = mag - ZeroPoint + ZeroPt;
+      inStars[Ninstars].measure.t        = images[0][0].tzero;
+      inStars[Ninstars].measure.dt       = images[0][0].exptime;
+      inStars[Ninstars].measure.photcode = code;
+      inStars[Ninstars].measure.airmass  = airmass;
+      inStars[Ninstars].measure.az       = az;
+
       Ninstars++;
       CHECK_REALLOCATE (inStars, Stars, NINSTARS, Ninstars, 10000);
@@ -238,5 +237,5 @@
     image[0].Mcal = 0.0;
     image[0].Xm   = NAN_S_SHORT;
-    image[0].code = 0;
+    image[0].flags = 0;
 
     image[0].nstar = 0;
Index: /trunk/Ohana/src/addstar/src/LoadStars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/LoadStars.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/LoadStars.c	(revision 21508)
@@ -63,4 +63,5 @@
     }
 
+    // load PMM data if specified (these are not stored as FITS-tables)
     if (PMM_CCD_TABLE != NULL) {
       LoadDataPMM (f, file[i], images, Nimages, &stars, Nstars);
@@ -68,4 +69,5 @@
     }
 
+    // otherwise, we have FITS-table files: parse their headers to determine the contents
     headers = LoadHeaders (f, &mode, &Nheaders);
     headerSets = MatchHeaders (&extsize, &NheaderSets, mode, headers, Nheaders);
@@ -86,4 +88,5 @@
     }
 
+    // if these are SDSS data, load with SDSS-specific wrapper
     if (headerSets[0].exttype && !strcmp (headerSets[0].exttype, "SDSS_OBJ")) {
       LoadDataSDSS (f, file[i], images, Nimages, &stars, Nstars, headers, extsize, headerSets, NheaderSets);
Index: /trunk/Ohana/src/addstar/src/MatchHeaders.c
===================================================================
--- /trunk/Ohana/src/addstar/src/MatchHeaders.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/MatchHeaders.c	(revision 21508)
@@ -51,4 +51,5 @@
     if (!strcmp (exttype, "PS1_DEV_0")) goto keep;
     if (!strcmp (exttype, "PS1_DEV_1")) goto keep;
+    if (!strcmp (exttype, "PS1_V1")) goto keep;
     continue;
 
Index: /trunk/Ohana/src/addstar/src/PSPS_IDs.c
===================================================================
--- /trunk/Ohana/src/addstar/src/PSPS_IDs.c	(revision 21508)
+++ /trunk/Ohana/src/addstar/src/PSPS_IDs.c	(revision 21508)
@@ -0,0 +1,71 @@
+# include "addstar.h"
+
+uint64_7 PSPS_create_detectID (double MJDobs, int imageID) {
+
+  // MJD has a PS lifetime range of : 3288 (2009/01/01) to 9132 (2025/01/01) (< 2^14)
+
+  static double t0 = 51544.5; // MJD @ 2000/01/01,12:00:00
+
+  uint64_t iTime;
+  uint64_t detectID;
+
+  iTime = floor(tobs - t0);
+
+  // iTime takes the upper 16 bits, imageID can take up to lower 48 bits
+  detectID = (iTime << 48) | imageID;
+
+  return detectID;
+}
+
+uint64_t PSPS_create_objID (double ra, double dec) {
+
+  static double zh = 0.0083333; // zone height = 30 arcsec
+
+  uint64_t objID;
+  uint64_t part1, part2, part3;
+
+  double zid, zresid;
+  int izone;
+
+  zid = (dec + 90.0) / zh; // 0 - 180*60*2 = 21600 (< 15 bits)
+  izone = (int) floor(zid);
+  zresid = zid -  ((float) izone); // 0 - 1.0 
+  part1 = (uint64_t)( izone  * 10000000000000) ;
+  part2 = ((uint64_t)(ra * 1 000 000.)) * 10000 ; // 0 - 360.0*1e6 = 3.6e8 (< 29 bits)
+  part3 = (int) (zresid * 10000.0) ; // 0 - 10000 (1 bit == 30/10000 arcsec = 0.003 arcsec) (< 14 bits)
+
+  objID = part1 + part2 + part3;
+
+  return objID;
+}
+
+uint64_t PSPS_create_objID_eam (double ra, double dec) {
+
+  static double zh = 0.0083333; // zone height = 30 arcsec
+
+  uint64_t objID;
+  uint64_t part1, part2, part3;
+
+  double zid, zresid;
+  int izone, ira;
+
+  zid = (dec + 90.0) / zh; // 0 - 180*60*2 = 21600 (< 15 bits)
+  izone = (int) floor(zid);
+  zresid = zid -  ((float) izone); // 0 - 1.0 
+
+  ira = ra * 1 000 000; // 0 - 360.0*1e6 = 3.6e8 (< 29 bits) 
+
+  part1 = (izone << 48);
+  part2 = (ira << 16);
+  part3 = (int) (zresid * 10000.0) ; // 0 - 10000 (1 bit == 30/10000 arcsec = 0.003 arcsec) (< 14 bits)
+
+  objID = part1 | part2 | part3;
+
+  return objID;
+}
+
+// part1 : 15 bits
+// part2 : 29 bits
+// part3 : 14 bits
+// 15 + 14 + 29 bits = 58 bits...
+
Index: /trunk/Ohana/src/addstar/src/ReadImageHeader.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ReadImageHeader.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/ReadImageHeader.c	(revision 21508)
@@ -200,5 +200,5 @@
   image[0].Mcal = 0.0;
   image[0].Xm   = NAN_S_SHORT;
-  image[0].code = 0;
+  image[0].flags = 0;
 
   /* find expected number of stars */
Index: /trunk/Ohana/src/addstar/src/ReadStarsFITS.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 21508)
@@ -40,4 +40,7 @@
     stars = Convert_PS1_DEV_1 (&table, &Nstars);
   }
+  if (!strcmp (type, "PS1_V1")) {
+    stars = Convert_PS1_V1 (&table, &Nstars);
+  }
   if (stars == NULL) {
     fprintf (stderr, "ERROR: invalid table type %s\n", type);
@@ -62,26 +65,27 @@
   ZeroPt = GetZeroPoint();
 
-  /* XXX we need to check at least the size of the loaded table */
-  // XXX use memset to zero-out the elements before setting
-
-  ALLOCATE (stars, Stars, Nstars);
-  for (i = 0; i < Nstars; i++) {
-    stars[i].X      = smpdata[i].X;
-    stars[i].Y      = smpdata[i].Y;
+  ALLOCATE (stars, Stars, Nstars);
+  for (i = 0; i < Nstars; i++) {
+    InitStar (&stars[i]);
+
+    stars[i].measure.Xccd      = smpdata[i].X;
+    stars[i].measure.Yccd      = smpdata[i].Y;
 
     if ((smpdata[i].M >= ZeroPt) || isnan(smpdata[i].M)) {
-      stars[i].M    = NAN;
-      stars[i].Map  = NAN;
-    } else {
-      stars[i].M    = smpdata[i].M;
-      stars[i].Map  = smpdata[i].M;
-    }
-
-    stars[i].dM     = smpdata[i].dM*0.001;
-    stars[i].dophot = smpdata[i].dophot;
-
-    stars[i].fx     = smpdata[i].fx;
-    stars[i].fy     = smpdata[i].fy;
-    stars[i].df     = smpdata[i].df;
+      stars[i].measure.M       = NAN;
+      stars[i].measure.Map     = NAN;
+    } else {
+      stars[i].measure.M       = smpdata[i].M;
+      stars[i].measure.Map     = smpdata[i].M;
+    }
+
+    stars[i].measure.dM        = smpdata[i].dM*0.001;
+
+    // the dophot type information gets pushed into the upper 2 bytes of photFlags
+    stars[i].measure.photFlags = (smpdata[i].dophot << 16);
+
+    stars[i].measure.FWx       = ShortPixels (smpdata[i].fx);
+    stars[i].measure.FWy       = ShortPixels (smpdata[i].fy);
+    stars[i].measure.theta     = ShortDegree (smpdata[i].df);
   }    
   *nstars = Nstars;
@@ -101,52 +105,29 @@
   ALLOCATE (stars, Stars, Nstars);
   for (i = 0; i < Nstars; i++) {
-    stars[i].X       = ps1data[i].X;
-    stars[i].Y       = ps1data[i].Y;
-    stars[i].dX      = ps1data[i].dX;
-    stars[i].dY      = ps1data[i].dY;
+    InitStar (&stars[i]);
+    stars[i].measure.Xccd     = ps1data[i].X;
+    stars[i].measure.Yccd     = ps1data[i].Y;
+
+    stars[i].measure.dXccd    = ShortPixels(ps1data[i].dX);
+    stars[i].measure.dYccd    = ShortPixels(ps1data[i].dY);
+   
     if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
-      stars[i].M     = NAN;
-    } else {
-      stars[i].M     = ps1data[i].M + ZeroPt;
-    }
-    stars[i].dM      = ps1data[i].dM;
-    stars[i].Mpeak   = ps1data[i].Mpeak;
-
-    stars[i].sky     = ps1data[i].sky;
-    stars[i].dsky    = ps1data[i].dSky;
-
-    stars[i].fx      = ps1data[i].fx;
-    stars[i].fy      = ps1data[i].fy;
-    stars[i].df      = ps1data[i].df;
-
-    stars[i].psfChisq = ps1data[i].psfChisq;
-    stars[i].psfQual  = ps1data[i].psfQual;
-
-    stars[i].detID   = ps1data[i].detID;
-
-    /* these are set elsewhere */
-    stars[i].R       = 0.0;
-    stars[i].D       = 0.0;
-    stars[i].dR      = 0.0;
-    stars[i].dD      = 0.0;
-
-    stars[i].uR      = 0.0;
-    stars[i].uD      = 0.0;
-    stars[i].duR     = 0.0;
-    stars[i].duD     = 0.0;
-
-    stars[i].P       = 0.0;
-    stars[i].dP      = 0.0;
-
-    stars[i].Mcal    = 0;
-    stars[i].t       = 0;
-    stars[i].dt      = 0;
-    stars[i].airmass = 0;
-    stars[i].code    = 0;
-    stars[i].found   = 0;
-
-    /* these are not used */
-    stars[i].Map     = NAN;
-    stars[i].dophot  = 0;
+      stars[i].measure.M      = NAN;
+    } else {
+      stars[i].measure.M      = ps1data[i].M + ZeroPt;
+    }
+    stars[i].measure.Map      = NAN;
+    stars[i].measure.dM       = ps1data[i].dM;
+    stars[i].measure.Sky      = ps1data[i].sky;
+    stars[i].measure.dSky     = ps1data[i].dSky;
+
+    stars[i].measure.FWx      = ShortPixels(ps1data[i].fx);
+    stars[i].measure.FWy      = ShortPixels(ps1data[i].fy);
+    stars[i].measure.theta    = ShortDegree(ps1data[i].df);
+
+    stars[i].measure.psfChisq = ps1data[i].psfChisq;
+    stars[i].measure.psfQual  = ps1data[i].psfQual;
+
+    stars[i].measure.detID    = ps1data[i].detID;
   }    
   *nstars = Nstars;
@@ -168,61 +149,97 @@
   ALLOCATE (stars, Stars, Nstars);
   for (i = 0; i < Nstars; i++) {
-    stars[i].X       = ps1data[i].X;
-    stars[i].Y       = ps1data[i].Y;
-    stars[i].dX      = ps1data[i].dX;
-    stars[i].dY      = ps1data[i].dY;
+    InitStar (&stars[i]);
+    stars[i].measure.Xccd       = ps1data[i].X;
+    stars[i].measure.Yccd       = ps1data[i].Y;
+
+    stars[i].measure.dXccd      = ShortPixels(ps1data[i].dX);
+    stars[i].measure.dYccd      = ShortPixels(ps1data[i].dY);
+
     if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
-	stars[i].M   = NAN;
-    } else {
-	stars[i].M   = ps1data[i].M + ZeroPt;
-    }
-    stars[i].dM      = ps1data[i].dM;
-    stars[i].Mpeak   = ps1data[i].Mpeak;
-
-    stars[i].sky     = ps1data[i].sky;
-    stars[i].dsky    = ps1data[i].dSky;
-
-    stars[i].fx      = ps1data[i].fx;
-    stars[i].fy      = ps1data[i].fy;
-    stars[i].df      = ps1data[i].df;
-
-    stars[i].psfChisq  = ps1data[i].psfChisq;
-    stars[i].psfQual   = ps1data[i].psfQual;
-    stars[i].crNsigma  = ps1data[i].crNsigma;
-    stars[i].extNsigma = ps1data[i].extNsigma;
-
-    stars[i].detID     = ps1data[i].detID;
-    stars[i].flags     = ps1data[i].flags;
-
-    // XXX not defined anyway
-    // stars[i].stargal   = ps1data[i].stargal;
-
-    /* these are set elsewhere */
-    stars[i].R       = 0.0;
-    stars[i].D       = 0.0;
-    stars[i].dR      = 0.0;
-    stars[i].dD      = 0.0;
-
-    stars[i].uR      = 0.0;
-    stars[i].uD      = 0.0;
-    stars[i].duR     = 0.0;
-    stars[i].duD     = 0.0;
-
-    stars[i].P       = 0.0;
-    stars[i].dP      = 0.0;
-
-    stars[i].Mcal    = 0;
-    stars[i].t       = 0;
-    stars[i].dt      = 0;
-    stars[i].airmass = 0;
-    stars[i].az      = 0;
-    stars[i].code    = 0;
-    stars[i].found   = 0;
-
-    /* these are not used */
-    stars[i].Map     = NAN;
-    stars[i].dophot  = 0;
-  }    
-  *nstars = Nstars;
-  return (stars);
-}
+	stars[i].measure.M      = NAN;
+    } else {
+	stars[i].measure.M      = ps1data[i].M + ZeroPt;
+    }
+    stars[i].measure.Map        = NAN;
+    stars[i].measure.dM         = ps1data[i].dM;
+    stars[i].measure.Sky        = ps1data[i].sky;
+    stars[i].measure.dSky       = ps1data[i].dSky;
+
+    stars[i].measure.FWx        = ShortPixels(ps1data[i].fx);
+    stars[i].measure.FWy        = ShortPixels(ps1data[i].fy);
+    stars[i].measure.theta      = ShortDegree(ps1data[i].df);
+
+    stars[i].measure.psfChisq  	= ps1data[i].psfChisq;
+    stars[i].measure.psfQual   	= ps1data[i].psfQual;
+    stars[i].measure.crNsigma  	= ps1data[i].crNsigma;
+    stars[i].measure.extNsigma 	= ps1data[i].extNsigma;
+
+    stars[i].measure.detID     	= ps1data[i].detID;
+    stars[i].measure.photFlags  = ps1data[i].flags;
+  }    
+  *nstars = Nstars;
+  return (stars);
+}
+
+Stars *Convert_PS1_V1 (FTable *table, int *nstars) {
+
+  int i, Nstars;
+  double ZeroPt;
+  Stars *stars;
+  CMF_PS1_V1 *ps1data;
+
+  ps1data = gfits_table_get_CMF_PS1_V1 (table, &Nstars, NULL);
+  ZeroPt = GetZeroPoint();
+
+  ALLOCATE (stars, Stars, Nstars);
+  for (i = 0; i < Nstars; i++) {
+    InitStar (&stars[i]);
+    stars[i].measure.Xccd       = ps1data[i].X;
+    stars[i].measure.Yccd       = ps1data[i].Y;
+    stars[i].measure.dXccd      = ShortPixels(ps1data[i].dX);
+    stars[i].measure.dYccd      = ShortPixels(ps1data[i].dY);
+
+    stars[i].measure.posangle   = ShortDegree(ps1data[i].posangle);
+    stars[i].measure.pltscale   = ps1data[i].pltscale;
+
+    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
+	stars[i].measure.M      = NAN;
+    } else {
+	stars[i].measure.M      = ps1data[i].M + ZeroPt;
+    }
+    stars[i].measure.dM         = ps1data[i].dM;
+    stars[i].measure.dMcal      = ps1data[i].dMcal;
+    stars[i].measure.Map        = ps1data[i].Map + ZeroPt;
+		        
+    stars[i].measure.Sky        = ps1data[i].sky;
+    stars[i].measure.dSky       = ps1data[i].dSky;
+		        
+    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
+    stars[i].measure.psfQual    = ps1data[i].psfQual;
+    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
+    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
+    stars[i].measure.crNsigma   = ps1data[i].crNsigma;
+    stars[i].measure.extNsigma  = ps1data[i].extNsigma;
+
+    stars[i].measure.FWx        = ShortPixels(ps1data[i].fx);
+    stars[i].measure.FWy        = ShortPixels(ps1data[i].fy);
+    stars[i].measure.theta      = ShortDegree(ps1data[i].df);
+
+    stars[i].measure.Mxx        = ShortPixels(ps1data[i].Mxx);
+    stars[i].measure.Mxy        = ShortPixels(ps1data[i].Mxy);
+    stars[i].measure.Myy        = ShortPixels(ps1data[i].Myy);
+		        
+    stars[i].measure.photFlags  = ps1data[i].flags;
+
+    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
+    stars[i].measure.detID      = ps1data[i].detID; 
+
+    // the Average fields and the following Measure fields are set in FilterStars after
+    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID, 
+    // averef is set in find_matches, dbFlags is zero on ingest.
+
+    // the following fields are currently not being set anywhere: t_msec
+  }    
+  *nstars = Nstars;
+  return (stars);
+}
Index: /trunk/Ohana/src/addstar/src/ReadStarsSDSS.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ReadStarsSDSS.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/ReadStarsSDSS.c	(revision 21508)
@@ -140,35 +140,27 @@
     for (j = 0; j < NFILTER; j++) {
       N = NFILTER*i + j;
-      stars[N].X         = colc[N];
-      stars[N].Y         = rowc[N];
-      stars[N].dX        = colcErr[N];
-      stars[N].dY        = rowcErr[N];
-      stars[N].M         = psfCounts[N] + ZeroPt - zeropt[j];
-      stars[N].dM        = psfCountsErr[N];
-      stars[N].Map       = fiberCounts[N] + ZeroPt - zeropt[j];
-      stars[N].Mpeak     = NAN;
-      stars[N].sky       = sky[N]; // adjust this to counts?
-      stars[N].dsky      = skyErr[N];
-      stars[N].fx        = seeing[j]; // reported in arcsec?
-      stars[N].fy        = seeing[j];
-      stars[N].df        = 0.0;
-      stars[N].psfChisq  = prob_psf[N]; // XXX not really the correct value...
-      stars[N].psfQual   = 0.0;
-      stars[N].crNsigma  = 0.0;
-      stars[N].extNsigma = 0.0;
-      stars[N].detID     = N;
-      stars[N].R         = ra[i] + dCOS(dec[i]) * offsetRa[N] / 3600.0;
-      stars[N].D         = dec[i] + offsetDec[N] / 3600.0;
-      stars[N].dR        = NAN;
-      stars[N].dD        = NAN;
-      stars[N].uR        = 0.0; // I don't trust the SDSS proper motions
-      stars[N].uD        = 0.0; // I don't trust the SDSS proper motions
-      stars[N].duR       = 0.0;
-      stars[N].duD       = 0.0;
-      stars[N].P         = 0.0;
-      stars[N].dP        = 0.0;
-      stars[N].Mcal      = 0.0;
-      stars[N].t         = tzero[j] + clockRate*rowc[N]; // time since row 0
-      stars[N].dt        = 53.907456; // is this 2048*clockRate ?
+      InitStar (&stars[N]);
+      
+      // any values not explicitly set are left at 0.0
+      stars[N].average.R         = ra[i] + dCOS(dec[i]) * offsetRa[N] / 3600.0;
+      stars[N].average.D         = dec[i] + offsetDec[N] / 3600.0;
+      stars[N].average.dR        = NAN;
+      stars[N].average.dD        = NAN;
+
+      stars[N].measure.Xccd      = colc[N];
+      stars[N].measure.Yccd      = rowc[N];
+      stars[N].measure.dXccd     = ShortPixels(colcErr[N]);
+      stars[N].measure.dYccd     = ShortPixels(rowcErr[N]);
+      stars[N].measure.M         = psfCounts[N] + ZeroPt - zeropt[j];
+      stars[N].measure.dM        = psfCountsErr[N];
+      stars[N].measure.Map       = fiberCounts[N] + ZeroPt - zeropt[j];
+      stars[N].measure.Sky       = sky[N]; // adjust this to counts?
+      stars[N].measure.dSky      = skyErr[N];
+      stars[N].measure.FWx       = ShortPixels(seeing[j]); // reported in arcsec?
+      stars[N].measure.FWy       = ShortPixels(seeing[j]);
+      stars[N].measure.psfChisq  = prob_psf[N]; // XXX not really the correct value...
+      stars[N].measure.detID     = N;
+      stars[N].measure.t         = tzero[j] + clockRate*rowc[N]; // time since row 0
+      stars[N].measure.dt        = 53.907456; // is this 2048*clockRate ?
 
       SetSDSSFlags (&stars[N], flags[N], flags2[N]);
@@ -182,14 +174,11 @@
       double Latitude = 32.7803611755; // degrees
 
-      jd = ohana_sec_to_jd (stars[N].t);
+      jd = ohana_sec_to_jd (stars[N].measure.t);
       sidtime  = 15.0*ohana_lst (jd, Longitude); // sidtime in degrees
-      altaz (&alt, &az, sidtime - stars[N].R, stars[N].D, Latitude);
-
-      stars[N].airmass   = 1.0 / dCOS(90.0 - alt);
-      stars[N].az        = az;
-
-      stars[N].code      = photcode[j];
-      stars[N].found     = -1;
-      stars[N].dophot    = 0;
+      altaz (&alt, &az, sidtime - stars[N].average.R, stars[N].average.D, Latitude);
+
+      stars[N].measure.airmass   = 1.0 / dCOS(90.0 - alt);
+      stars[N].measure.az        = az;
+      stars[N].measure.photcode  = photcode[j];
     }
   }    
@@ -202,9 +191,9 @@
     strcpy (images[N].coords.ctype, "RA---TAN");
     
-    images[N].coords.crval1 = stars[0].R;
-    images[N].coords.crval2 = stars[0].D;
+    images[N].coords.crval1 = stars[0].average.R;
+    images[N].coords.crval2 = stars[0].average.D;
     
-    images[N].coords.crpix1 = stars[0].X;
-    images[N].coords.crpix2 = stars[0].Y;
+    images[N].coords.crpix1 = stars[0].measure.Xccd;
+    images[N].coords.crpix2 = stars[0].measure.Yccd;
     images[N].coords.cdelt1 = images[N].coords.cdelt2 = 0.4 / 3600.0;
 
@@ -251,5 +240,5 @@
 
     images[N].trate = clockRate * 1e-4;
-    images[N].secz = stars[0].airmass;
+    images[N].secz = stars[0].measure.airmass;
     images[N].ccdnum = camcol;
 
@@ -257,5 +246,5 @@
     images[N].Mcal = 0.0;
     images[N].Xm   = NAN_S_SHORT;
-    images[N].code = 0;
+    images[N].flags = 0;
 
     images[N].nstar = Nstars;
@@ -297,19 +286,19 @@
 
   // XXX this is wrong, need to roll left to set the correct bit 
-  if (flags1 & 0x00000002) star[0].flags |= 0x0001; // BRIGHT            - 1  1
-  if (flags1 & 0x00000004) star[0].flags |= 0x0002; // EDGE              - 1  2
-  if (flags1 & 0x00000008) star[0].flags |= 0x0004; // BLENDED           - 1  3
-  if (flags1 & 0x00000010) star[0].flags |= 0x0008; // CHILD             - 1  4
-  if (flags1 & 0x00000020) star[0].flags |= 0x0010; // PEAKCENTER        - 1  5
-  if (flags1 & 0x00000040) star[0].flags |= 0x0020; // NODEBLEND         - 1  6
-  if (flags1 & 0x00040000) star[0].flags |= 0x0040; // SATUR             - 1 18
-  if (flags1 & 0x00080000) star[0].flags |= 0x0080; // NOTCHECKED        - 1 19
-  if (flags1 & 0x10000000) star[0].flags |= 0x0100; // BINNED1           - 1 28
-  if (flags1 & 0x20000000) star[0].flags |= 0x0200; // BINNED2           - 1 29
-  if (flags1 & 0x40000000) star[0].flags |= 0x0400; // BINNED4           - 1 30
-  if (flags2 & 0x00000040) star[0].flags |= 0x0800; // LOCAL_EDGE        - 2  7
-  if (flags2 & 0x00000800) star[0].flags |= 0x1000; // INTERP_CENTER     - 2 12
-  if (flags2 & 0x00002000) star[0].flags |= 0x2000; // DEBLEND_NOPEAK    - 2 14
-  if (flags2 & 0x02000000) star[0].flags |= 0x4000; // NOTCHECKED_CENTER - 2 26
+  if (flags1 & 0x00000002) star[0].measure.photFlags |= 0x0001; // BRIGHT            - 1  1
+  if (flags1 & 0x00000004) star[0].measure.photFlags |= 0x0002; // EDGE              - 1  2
+  if (flags1 & 0x00000008) star[0].measure.photFlags |= 0x0004; // BLENDED           - 1  3
+  if (flags1 & 0x00000010) star[0].measure.photFlags |= 0x0008; // CHILD             - 1  4
+  if (flags1 & 0x00000020) star[0].measure.photFlags |= 0x0010; // PEAKCENTER        - 1  5
+  if (flags1 & 0x00000040) star[0].measure.photFlags |= 0x0020; // NODEBLEND         - 1  6
+  if (flags1 & 0x00040000) star[0].measure.photFlags |= 0x0040; // SATUR             - 1 18
+  if (flags1 & 0x00080000) star[0].measure.photFlags |= 0x0080; // NOTCHECKED        - 1 19
+  if (flags1 & 0x10000000) star[0].measure.photFlags |= 0x0100; // BINNED1           - 1 28
+  if (flags1 & 0x20000000) star[0].measure.photFlags |= 0x0200; // BINNED2           - 1 29
+  if (flags1 & 0x40000000) star[0].measure.photFlags |= 0x0400; // BINNED4           - 1 30
+  if (flags2 & 0x00000040) star[0].measure.photFlags |= 0x0800; // LOCAL_EDGE        - 2  7
+  if (flags2 & 0x00000800) star[0].measure.photFlags |= 0x1000; // INTERP_CENTER     - 2 12
+  if (flags2 & 0x00002000) star[0].measure.photFlags |= 0x2000; // DEBLEND_NOPEAK    - 2 14
+  if (flags2 & 0x02000000) star[0].measure.photFlags |= 0x4000; // NOTCHECKED_CENTER - 2 26
   return (TRUE);
 
Index: /trunk/Ohana/src/addstar/src/ReadStarsTEXT.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ReadStarsTEXT.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/ReadStarsTEXT.c	(revision 21508)
@@ -7,7 +7,7 @@
 
   int j, N, Nextra, Ninstar, Nskip, Nbytes, nbytes;
-  int done;
+  int done, itmp;
   char *buffer, *c, *c2;
-  double tmp;
+  double tmp, fx, fy, df;
   double ZeroPt;
   Stars *stars;
@@ -66,17 +66,20 @@
     Nextra = nbytes % BYTES_STAR;
     for (j = 0; (j < Ninstar) && (N < *nstars); j++, N++) {
-      dparse (&stars[N].X,  1, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].Y,  2, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].M,  3, &buffer[j*BYTES_STAR]);
-      if ((stars[N].M > ZeroPt) || isnan(stars[N].M)) {
-	stars[N].M = NAN;
+      InitStar (&stars[N]);
+      fparse (&stars[N].measure.Xccd,  1, &buffer[j*BYTES_STAR]);
+      fparse (&stars[N].measure.Yccd,  2, &buffer[j*BYTES_STAR]);
+      fparse (&stars[N].measure.M,  3, &buffer[j*BYTES_STAR]);
+      if ((stars[N].measure.M > ZeroPt) || isnan(stars[N].measure.M)) {
+	stars[N].measure.M = NAN;
       }
 
       /* cmp files carry dM in millimags */
       dparse (&tmp, 4, &buffer[j*BYTES_STAR]);
-      stars[N].dM = 0.001*tmp;
+      stars[N].measure.dM = 0.001*tmp;
 
+      // the dophot type information get pushed into the upper 2 bytes of photFlags
       dparse (&tmp,         5, &buffer[j*BYTES_STAR]);
-      stars[N].dophot = tmp;
+      itmp = tmp;
+      stars[N].measure.photFlags = (itmp << 16);
 
       // XXX I've removed the Mgal field from the measure.d table, and am using Map
@@ -87,8 +90,12 @@
 
       // dparse (&stars[N].Mgal, 7, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].Map,  8, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].fx,   9, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].fy,  10, &buffer[j*BYTES_STAR]);
-      dparse (&stars[N].df,  11, &buffer[j*BYTES_STAR]);
+      fparse (&stars[N].measure.Map,  8, &buffer[j*BYTES_STAR]);
+      dparse (&fx,   9, &buffer[j*BYTES_STAR]);
+      dparse (&fy,  10, &buffer[j*BYTES_STAR]);
+      dparse (&df,  11, &buffer[j*BYTES_STAR]);
+
+      stars[N].measure.FWx   = ShortPixels (fx);
+      stars[N].measure.FWy   = ShortPixels (fy);
+      stars[N].measure.theta = ShortDegree (df);
     }
   }
Index: /trunk/Ohana/src/addstar/src/SEDfit.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SEDfit.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/SEDfit.c	(revision 21508)
@@ -164,5 +164,5 @@
     outcat[0].average[Nave].measureOffset = Nmeas;
     outcat[0].average[Nave].missingOffset = -1;
-    outcat[0].average[Nave].code          = 0;
+    outcat[0].average[Nave].flags         = 0;
 
     for (j = 0; j < Nsec; j++) {
@@ -181,20 +181,20 @@
     for (j = 0; valid && (j < Nmodel); j++) {
       n = modelRow[j];
-      outcat[0].measure[Nmeas].dR       = 0.0;
-      outcat[0].measure[Nmeas].dD       = 0.0;
-      outcat[0].measure[Nmeas].M        = table[0].row[minFit.row][0].mags[n] + minFit.Md;
-      outcat[0].measure[Nmeas].dM       = 0.0;
-      outcat[0].measure[Nmeas].Mcal     = 0;
-      outcat[0].measure[Nmeas].t        = TIMEREF;
-      outcat[0].measure[Nmeas].averef   = Nave;
-      outcat[0].measure[Nmeas].photcode = table[0].code[n];
-      outcat[0].measure[Nmeas].dophot   = 0;
-      outcat[0].measure[Nmeas].dbFlags  = 0;
-      outcat[0].measure[Nmeas].dt       = 0xffff;
-
-      outcat[0].measure[Nmeas].airmass  = 0;
-      outcat[0].measure[Nmeas].FWx      = NAN_S_SHORT;
-      outcat[0].measure[Nmeas].FWy      = NAN_S_SHORT;
-      outcat[0].measure[Nmeas].theta    = NAN_S_SHORT;
+      outcat[0].measure[Nmeas].dR        = 0.0;
+      outcat[0].measure[Nmeas].dD        = 0.0;
+      outcat[0].measure[Nmeas].M         = table[0].row[minFit.row][0].mags[n] + minFit.Md;
+      outcat[0].measure[Nmeas].dM        = 0.0;
+      outcat[0].measure[Nmeas].Mcal      = 0;
+      outcat[0].measure[Nmeas].t         = TIMEREF;
+      outcat[0].measure[Nmeas].averef    = Nave;
+      outcat[0].measure[Nmeas].photcode  = table[0].code[n];
+      outcat[0].measure[Nmeas].photFlags = 0;
+      outcat[0].measure[Nmeas].dbFlags   = 0;
+      outcat[0].measure[Nmeas].dt        = 0xffff;
+					 
+      outcat[0].measure[Nmeas].airmass   = 0;
+      outcat[0].measure[Nmeas].FWx       = NAN_S_SHORT;
+      outcat[0].measure[Nmeas].FWy       = NAN_S_SHORT;
+      outcat[0].measure[Nmeas].theta     = NAN_S_SHORT;
 
       outcat[0].average[Nave].Nmeasure++;
Index: /trunk/Ohana/src/addstar/src/SkyListForStars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SkyListForStars.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/SkyListForStars.c	(revision 21508)
@@ -1,3 +1,15 @@
 # include "addstar.h"
+
+void sort_stars_ra (Stars *stars, int N) {
+
+# define SWAPFUNC(A,B){ Stars tmp; tmp = stars[A]; stars[A] = stars[B]; stars[B] = tmp; }
+# define COMPARE(A,B)(stars[A].average.R < stars[B].average.R)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
 
 SkyList *SkyListForStars (SkyTable *table, int depth, Stars *stars, int Nstars) {
@@ -17,14 +29,14 @@
   sort_stars_ra (stars, Nstars); /* sort by RA */
   for (i = 0; i < Nstars; i++) {
-    if (stars[i].found) continue;
-    here = SkyRegionByPoint (table, depth, stars[i].R, stars[i].D);
-    stars[i].found = TRUE;
+    if (stars[i].found == -2) continue;
+    here = SkyRegionByPoint (table, depth, stars[i].average.R, stars[i].average.D);
+    stars[i].found = -2;
     /* search forward for all contained stars */
     for (j = i; j < Nstars; j++) {
-      if (stars[j].R >= here[0].regions[0][0].Rmax) break;
-      if (stars[j].R <  here[0].regions[0][0].Rmin) break;
-      if (stars[j].D <  here[0].regions[0][0].Dmin) continue;
-      if (stars[j].D >= here[0].regions[0][0].Dmax) continue;
-      stars[j].found = TRUE;
+      if (stars[j].average.R >= here[0].regions[0][0].Rmax) break;
+      if (stars[j].average.R <  here[0].regions[0][0].Rmin) break;
+      if (stars[j].average.D <  here[0].regions[0][0].Dmin) continue;
+      if (stars[j].average.D >= here[0].regions[0][0].Dmax) continue;
+      stars[j].found = -2;
     }
     list[0].regions[Nr] = here[0].regions[0];
Index: /trunk/Ohana/src/addstar/src/StarOps.c
===================================================================
--- /trunk/Ohana/src/addstar/src/StarOps.c	(revision 21508)
+++ /trunk/Ohana/src/addstar/src/StarOps.c	(revision 21508)
@@ -0,0 +1,28 @@
+# include "addstar.h"
+
+int InitStar (Stars *star) {
+
+    memset (&star[0].average, 0, sizeof(Average));
+    memset (&star[0].measure, 0, sizeof(Measure));
+    star[0].found = -1; // found == -1 -> not yet found (use enums?)
+
+    return TRUE;
+}
+
+short ShortPixels (float pixels) {
+
+    short value;
+
+    value = 100*pixels;
+
+    return value;
+}
+
+short ShortDegree (float degrees) {
+
+    short value;
+
+    value = (0xffff/360.0)*degrees;
+
+    return value;
+}
Index: /trunk/Ohana/src/addstar/src/UpdateImageIDs.c
===================================================================
--- /trunk/Ohana/src/addstar/src/UpdateImageIDs.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/UpdateImageIDs.c	(revision 21508)
@@ -43,10 +43,9 @@
 
   for (i = 0; i < Nstars; i++) {
-    stars[i].imageID += imageID;
+    stars[i].measure.imageID += imageID;
   }
 
   imageID += Nimages;
   status = gfits_modify (&db.header, "IMAGEID", "%u", 1, imageID);
- 
 
   if (isEmpty) {
Index: /trunk/Ohana/src/addstar/src/addstar.c
===================================================================
--- /trunk/Ohana/src/addstar/src/addstar.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/addstar.c	(revision 21508)
@@ -135,5 +135,5 @@
 	break;
       case M_REFCAT:
-	stars = grefcat (argv[1], skylist[0].regions[i], options.photcode, &Nstars);
+	stars = greference (argv[1], skylist[0].regions[i], options.photcode, &Nstars);
       case M_REFLIST:
 	subset = find_subset (skylist[0].regions[i], stars, Nstars, &Nsubset);
Index: /trunk/Ohana/src/addstar/src/airmass.c
===================================================================
--- /trunk/Ohana/src/addstar/src/airmass.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/airmass.c	(revision 21508)
@@ -1,5 +1,5 @@
 # include "addstar.h"
 
-static int AirmassQuality = FALSE;
+static int AirmassQuality = TRUE;
 
 void SetAirmassQuality (int quality) {
@@ -25,2 +25,21 @@
   return (secz);
 }
+
+// ha/dec -> az
+float azimuth (double ha, double dec, double latitude) {
+
+  double rdec, rlat, rha;
+  double sinh, cosh;
+  float az;
+
+  rdec = RAD_DEG*dec;
+  rha = RAD_DEG*ha;
+  rlat = RAD_DEG*latitude;
+
+  sinh = - cos (rdec) * sin (rha);
+  cosh =   sin (rdec) * cos (rlat) - cos (rdec) * cos (rha) * sin (rlat);
+
+  az = DEG_RAD * atan2 (sinh, cosh);
+
+  return az;
+}
Index: /trunk/Ohana/src/addstar/src/args.c
===================================================================
--- /trunk/Ohana/src/addstar/src/args.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/args.c	(revision 21508)
@@ -28,4 +28,8 @@
     remove_argument (N, &argc, argv);
   }
+  if ((N = get_argument (argc, argv, "-resort"))) {
+    options.mode = M_RESORT;
+    remove_argument (N, &argc, argv);
+  }
   if ((N = get_argument (argc, argv, "-fakeimage"))) {
     options.mode = M_FAKEIMAGE;
@@ -36,8 +40,4 @@
     remove_argument (N, &argc, argv);
     FAKE_THETA = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-  if ((N = get_argument (argc, argv, "-resort"))) {
-    options.mode = M_RESORT;
     remove_argument (N, &argc, argv);
   }
@@ -221,8 +221,12 @@
   /*** optional situations ***/
   /* choose high quality airmass vs low quality airmass (per-star vs per-image) */
-  QUALITY_AIRMASS = FALSE;
+  QUALITY_AIRMASS = TRUE;
   if ((N = get_argument (argc, argv, "-quality-airmass"))) {
     remove_argument (N, &argc, argv);
     QUALITY_AIRMASS = TRUE;
+  }
+  if ((N = get_argument (argc, argv, "-quick-airmass"))) {
+    remove_argument (N, &argc, argv);
+    QUALITY_AIRMASS = FALSE;
   }
   /* choose high quality airmass vs low quality airmass (per-star vs per-image) */
@@ -310,8 +314,10 @@
 
   fprintf (stderr, "USAGE: addstar (filename)\n");
+  fprintf (stderr, "USAGE: addstar -list (filename)\n");
   fprintf (stderr, "USAGE: addstar -cat (catalog)\n");
   fprintf (stderr, "USAGE: addstar -ref (filename)\n");
   fprintf (stderr, "USAGE: addstar -fakeimage (ra) (dec) (theta) (name)\n");
   fprintf (stderr, "USAGE: addstar -resort (SkyRegion)\n");
+  fprintf (stderr, "USAGE: addstar -ppm (filename)\n");
   exit (2);
 }
@@ -322,4 +328,6 @@
   fprintf (stderr, "  addstar (filename)\n");
   fprintf (stderr, "     add specified image (cmp format) to database\n\n");
+  fprintf (stderr, "  addstar -list (filename)\n");
+  fprintf (stderr, "     add list of images (cmp format) to database\n\n");
   fprintf (stderr, "  addstar -ref (filename)");
   fprintf (stderr, "     add ASCII data (ra dec mag dmag) to database\n\n");
@@ -328,4 +336,8 @@
   fprintf (stderr, "  addstar -resort (SkyRegion)");
   fprintf (stderr, "     perform measure sorting for the specified catalog\n\n");
+  fprintf (stderr, "  addstar -fakeimage (ra) (dec) (theta) (name)");
+  fprintf (stderr, "     insert a fake image in the db\n\n");
+  fprintf (stderr, "  addstar -pmm (filename)");
+  fprintf (stderr, "     insert pmm table into database\n\n");
 
   fprintf (stderr, "  optional flags:\n");
@@ -340,14 +352,25 @@
   fprintf (stderr, "  -missed               	  : skipped 'missed' entries\n");
   fprintf (stderr, "  -replace              	  : replace time/photcode measurements (no duplication)\n");
-  fprintf (stderr, "  -closest             	  : use closest-star algorith\n");
+  fprintf (stderr, "  -closest             	  : use closest-star algorith (default)\n");
+  fprintf (stderr, "  -all-matches             	  : use all-matches algorith\n");
   fprintf (stderr, "  -nosort             	  : don't re-sort the measure entries (improves speed)\n");
   fprintf (stderr, "  -update             	  : only update the new rows (forces -nosort)\n");
+  fprintf (stderr, "  -force-sort             	  : \n");
   fprintf (stderr, "  -image                	  : only insert image data\n");
   fprintf (stderr, "  -cal                  	  : perform zero-point calibration\n");
+  fprintf (stderr, "  -excal                  	  : apply supplied zero-point calibration\n");
+  fprintf (stderr, "  -incal                  	  : perform zero-point calibration\n");
+  fprintf (stderr, "  -quality-airmass            : use per-star airmass values (default)\n");
+  fprintf (stderr, "  -quick-airmass              : use per-image airmass values\n");
+  fprintf (stderr, "  -subpix             	  : apply subpixel corrections\n");
   fprintf (stderr, "  -skyprobe             	  : specify skyprobe mode\n");
+  fprintf (stderr, "  -2massquality            	  : define 2MASS quality flags to keep\n");
   fprintf (stderr, "  -accept               	  : accept bad astrometry from header\n");
   fprintf (stderr, "  -accept-astrom          	  : accept bad astrometry from header\n");
   fprintf (stderr, "  -accept-motion           	  : accept proper-motion data from reference\n");
+  fprintf (stderr, "  -accept-time           	  : use TZERO supplied in header\n");
+  fprintf (stderr, "  -no-stars                	  : skip the stars\n");
   fprintf (stderr, "  -force                	  : force read of database with inconsistent info\n");
+  fprintf (stderr, "  -textmode                	  : force textmode for file (ignore header clues)\n");
   fprintf (stderr, "  -v                    	  : verbose mode\n");
   fprintf (stderr, "  -dump (mode)          	  : output test data. Currently supported values for mode are: 'rawdata', 'cal'.\n");
Index: /trunk/Ohana/src/addstar/src/dump.c
===================================================================
--- /trunk/Ohana/src/addstar/src/dump.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/dump.c	(revision 21508)
@@ -11,7 +11,7 @@
     fprintf (f, "%4d  %10.6f %10.6f  %8.2f %8.2f  %7.2f %7.2f\n", 
 	     i, 
-	     stars[i].R, stars[i].D,
-	     stars[i].X, stars[i].Y,
-	     stars[i].M, stars[i].dM);
+	     stars[i].average.R, stars[i].average.D,
+	     stars[i].measure.Xccd, stars[i].measure.Yccd,
+	     stars[i].measure.M, stars[i].measure.dM);
   }
 
Index: /trunk/Ohana/src/addstar/src/fakeimage.c
===================================================================
--- /trunk/Ohana/src/addstar/src/fakeimage.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/fakeimage.c	(revision 21508)
@@ -129,7 +129,7 @@
     image[i+1].ccdnum = 0xff;
 
-    image[i+1].Mcal = 0.0;
-    image[i+1].Xm   = NAN_S_SHORT;
-    image[i+1].code = 0;
+    image[i+1].Mcal  = 0.0;
+    image[i+1].Xm    = NAN_S_SHORT;
+    image[i+1].flags = 0;
 
     image[i+1].nstar = 0;
@@ -189,5 +189,5 @@
   image[0].Mcal = 0.0;
   image[0].Xm   = NAN_S_SHORT;
-  image[0].code = 0;
+  image[0].flags = 0;
   image[0].nstar = 0;
   image[0].Myyyy = 0;
Index: /trunk/Ohana/src/addstar/src/find_matches.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/find_matches.c	(revision 21508)
@@ -62,8 +62,8 @@
   strcpy (tcoords.ctype, "RA---ARC");
 
-  /* build spatial index (RA sort) */
+  /* build spatial index (RA sort) referencing input array sequence */
   Nstars = 0;
   for (i = 0; i < NstarsIn; i++) {
-    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
+    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].average.R, stars[i].average.D, &tcoords);
     if (!status) continue;
     N1[Nstars] = i;
@@ -104,5 +104,5 @@
   /** find matched stars **/
   for (i = j = 0; (i < Nstars) && (j < Nave); ) {
-        if (!finite(X1[i]) || !finite(Y1[i])) { 
+    if (!finite(X1[i]) || !finite(Y1[i])) { 
       i++; 
       continue;
@@ -147,60 +147,35 @@
 
       /** add measurements for this star **/
-      /** dR,dD now represent arcsec **/
-      catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N].R);
+
+      // set the new measurements
+      catalog[0].measure[Nmeas]          = stars[N].measure;
+
+      // the following measure elements cannot be set until here:
+      catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N].average.R);
+      catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N].average.D);
+      catalog[0].measure[Nmeas].dbFlags  = 0;
+      catalog[0].measure[Nmeas].averef   = n; // this must be an absolute sequence number, if partial average is loaded 
+      catalog[0].measure[Nmeas].objID    = catalog[0].average[n].objID;
+      catalog[0].measure[Nmeas].catID    = catalog[0].catID;
+
+      // rationalize dR:
       if (catalog[0].measure[Nmeas].dR > +180.0*3600.0) {
 	  // average on high end of boundary, move star up
-	  stars[N].R += 360.0;
-	  catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].R);
+	  stars[N].average.R += 360.0;
+	  catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].average.R);
       }
       if (catalog[0].measure[Nmeas].dR < -180.0*3600.0) {
 	  // average on low end of boundary, move star down
-	  stars[N].R -= 360.0;
-	  catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].R);
+	  stars[N].average.R -= 360.0;
+	  catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].average.R);
       }
       if (fabs(catalog[0].measure[Nmeas].dR) > 10*RADIUS) {
 	  fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
 		   catalog[0].average[n].R, catalog[0].average[n].D, 
-		   stars[N].R, stars[N].D,
+		   stars[N].average.R, stars[N].average.D,
 		   X1[i], X2[J], 
 		   Y1[i], Y2[J]);
       }
-      catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N].D);
-
-      catalog[0].measure[Nmeas].Xccd     = stars[N].X;
-      catalog[0].measure[Nmeas].Yccd     = stars[N].Y;
-
-      catalog[0].measure[Nmeas].M        = stars[N].M;
-      catalog[0].measure[Nmeas].dM       = stars[N].dM;  /* error in input files stored in thousandths of mag */
-      catalog[0].measure[Nmeas].Mcal     = stars[N].Mcal;
-      catalog[0].measure[Nmeas].t        = stars[N].t;
-      catalog[0].measure[Nmeas].averef   = n;              /* this must be an absolute sequence number, if partial average is loaded */
-      catalog[0].measure[Nmeas].photcode = stars[N].code;  /* photcode */
-      catalog[0].measure[Nmeas].dophot   = stars[N].dophot;  
-      catalog[0].measure[Nmeas].dbFlags  = 0;
-      catalog[0].measure[Nmeas].dt       = stars[N].dt;
-      catalog[0].measure[Nmeas].airmass  = stars[N].airmass;
-
-      catalog[0].measure[Nmeas].photFlags = stars[N].flags;
-      catalog[0].measure[Nmeas].qPSF      = stars[N].psfQual;
-      catalog[0].measure[Nmeas].psfChisq  = stars[N].psfChisq;
-      catalog[0].measure[Nmeas].crNsigma  = stars[N].crNsigma;
-      catalog[0].measure[Nmeas].extNsigma = stars[N].extNsigma;
-      catalog[0].measure[Nmeas].Sky       = stars[N].sky;
-      catalog[0].measure[Nmeas].dSky      = stars[N].dsky;
-
-      catalog[0].measure[Nmeas].stargal   = 0; // XXX not yet set
-
-      catalog[0].measure[Nmeas].detID     = stars[N].detID;
-      catalog[0].measure[Nmeas].imageID   = stars[N].imageID;
-
-      catalog[0].measure[Nmeas].dXccd     = stars[N].dX;
-      catalog[0].measure[Nmeas].dYccd     = stars[N].dY;
-
-      catalog[0].measure[Nmeas].Map      = stars[N].Map;
-      catalog[0].measure[Nmeas].FWx      = 100*stars[N].fx;
-      catalog[0].measure[Nmeas].FWy      = 100*stars[N].fy;
-      catalog[0].measure[Nmeas].theta    = (0xffff/360.0)*stars[N].df;
-	
+
       /* adds the measurement to the calibration if appropriate color terms are found */
       /* we call this before (optionally) setting the average magnitude to avoid auto-correlations */
@@ -266,71 +241,56 @@
     N = N1[i];
     if (stars[N].found >= 0) continue;
-    if (!IN_REGION (stars[N].R, stars[N].D)) continue;
-
-    catalog[0].average[Nave].R         	   = stars[N].R;
-    catalog[0].average[Nave].D         	   = stars[N].D;
-    catalog[0].average[Nave].Xp        	   = 0;
+    if (!IN_REGION (stars[N].average.R, stars[N].average.D)) continue;
+
+    catalog[0].average[Nave].R         	   = stars[N].average.R;
+    catalog[0].average[Nave].D         	   = stars[N].average.D;
+    catalog[0].average[Nave].dR        	   = 0;
+    catalog[0].average[Nave].dD        	   = 0;
+
     catalog[0].average[Nave].Nmeasure      = 1;
     catalog[0].average[Nave].Nmissing      = 0;
+    catalog[0].average[Nave].Nextend       = 0;
+
     catalog[0].average[Nave].measureOffset = Nmeas;
     catalog[0].average[Nave].missingOffset = -1;
-    catalog[0].average[Nave].code          = 0;
-
-    catalog[0].average[Nave].dR        = 0;
-    catalog[0].average[Nave].dD        = 0;
-    catalog[0].average[Nave].uR        = 0;
-    catalog[0].average[Nave].uD        = 0;
-    catalog[0].average[Nave].duR       = 0;
-    catalog[0].average[Nave].duD       = 0;
-    catalog[0].average[Nave].P         = 0;
-    catalog[0].average[Nave].dP        = 0;
-
-    catalog[0].average[Nave].objID     = objID;
-    catalog[0].average[Nave].catID     = catID;
+    catalog[0].average[Nave].extendOffset  = -1;
+
+    catalog[0].average[Nave].uR        	   = 0;
+    catalog[0].average[Nave].uD        	   = 0;
+    catalog[0].average[Nave].duR       	   = 0;
+    catalog[0].average[Nave].duD       	   = 0;
+    catalog[0].average[Nave].P         	   = 0;
+    catalog[0].average[Nave].dP        	   = 0;
+
+    catalog[0].average[Nave].Xp        	   = 0;
+    catalog[0].average[Nave].ChiSq    	   = 0.0;
+    catalog[0].average[Nave].Npos    	   = 0;
+
+    catalog[0].average[Nave].objID     	   = objID;
+    catalog[0].average[Nave].catID     	   = catID;
+    catalog[0].average[Nave].flags         = 0;
+
     objID ++;
 
     for (j = 0; j < Nsecfilt; j++) {
-      catalog[0].secfilt[Nave*Nsecfilt+j].M  = NAN;
-      catalog[0].secfilt[Nave*Nsecfilt+j].dM = NAN;
-      catalog[0].secfilt[Nave*Nsecfilt+j].Xm = NAN_S_SHORT;
-    }
-
-    catalog[0].measure[Nmeas].Xccd     = stars[N].X;
-    catalog[0].measure[Nmeas].Yccd     = stars[N].Y;
-
+      catalog[0].secfilt[Nave*Nsecfilt+j].M    	= NAN;
+      catalog[0].secfilt[Nave*Nsecfilt+j].dM   	= NAN;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Xm   	= NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsecfilt+j].M_20 	= NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsecfilt+j].M_80 	= NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Ncode = 0;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Nused = 0;
+    }
+
+    // supply the measurments from this detection
+    catalog[0].measure[Nmeas]           = stars[N].measure;
+
+    // the following measure elements cannot be set until here:
     catalog[0].measure[Nmeas].dR       	= 0.0;
     catalog[0].measure[Nmeas].dD       	= 0.0;
-    catalog[0].measure[Nmeas].M        	= stars[N].M;
-    catalog[0].measure[Nmeas].dM       	= stars[N].dM;
-    catalog[0].measure[Nmeas].Mcal  	= stars[N].Mcal;
-    catalog[0].measure[Nmeas].t        	= stars[N].t;
-    catalog[0].measure[Nmeas].averef   	= Nave;           /* XXX EAM : must be absolute Nave if partial read */
-    catalog[0].measure[Nmeas].photcode  = stars[N].code;  /* photcode */
-    catalog[0].measure[Nmeas].dophot   	= stars[N].dophot;  
     catalog[0].measure[Nmeas].dbFlags 	= 0;
-    catalog[0].measure[Nmeas].dt    	= stars[N].dt;
-    catalog[0].measure[Nmeas].airmass   = stars[N].airmass;
-
-    catalog[0].measure[Nmeas].photFlags = stars[N].flags;
-    catalog[0].measure[Nmeas].qPSF      = stars[N].psfQual;
-    catalog[0].measure[Nmeas].psfChisq  = stars[N].psfChisq;
-    catalog[0].measure[Nmeas].crNsigma  = stars[N].crNsigma;
-    catalog[0].measure[Nmeas].extNsigma = stars[N].extNsigma;
-    catalog[0].measure[Nmeas].Sky       = stars[N].sky;
-    catalog[0].measure[Nmeas].dSky      = stars[N].dsky;
-
-    catalog[0].measure[Nmeas].stargal   = 0; // XXX not yet set
-
-    catalog[0].measure[Nmeas].detID     = stars[N].detID;
-    catalog[0].measure[Nmeas].imageID   = stars[N].imageID;
-
-    catalog[0].measure[Nmeas].dXccd     = stars[N].dX;
-    catalog[0].measure[Nmeas].dYccd     = stars[N].dY;
-
-    catalog[0].measure[Nmeas].Map  	= stars[N].Map;
-    catalog[0].measure[Nmeas].FWx      	= 100*stars[N].fx;
-    catalog[0].measure[Nmeas].FWy      	= 100*stars[N].fy;
-    catalog[0].measure[Nmeas].theta    	= (0xffff/360.0)*stars[N].df;
-    /* XXX replace df here with theta, right? */
+    catalog[0].measure[Nmeas].averef   	= Nave; // XXX EAM : must be absolute Nave if partial read
+    catalog[0].measure[Nmeas].objID     = catalog[0].average[Nave].objID;
+    catalog[0].measure[Nmeas].catID     = catalog[0].catID;
 
     /* set the average magnitude if not already set and the photcode.equiv is not 0 */
Index: /trunk/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 21508)
@@ -7,6 +7,6 @@
   double *X1, *Y1, *X2, *Y2;
   double dX, dY, dR;
-  int *N1, *N2,  *next_meas, *next_miss;
-  int Nave, NAVE, Nmeas, NMEAS, Nmiss, NMISS, Nmatch;
+  int *N1, *N2,  *next_meas;
+  int Nave, NAVE, Nmeas, NMEAS, Nmatch;
   int Nsecfilt, Nsec;
   unsigned int objID, catID;
@@ -37,5 +37,4 @@
   Nmatch = 0;
   NMEAS = Nmeas = catalog[0].Nmeasure;
-  NMISS = Nmiss = catalog[0].Nmissing;
 
   // current max obj ID for this catalog
@@ -66,5 +65,5 @@
   Nstars = 0;
   for (i = 0; i < NstarsIn; i++) {
-    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].R, stars[i].D, &tcoords);
+    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].average.R, stars[i].average.D, &tcoords);
     if (!status) continue;
     N1[Nstars] = i;
@@ -92,5 +91,5 @@
   if (Nave > 1) sort_coords_index (X2, Y2, N2, Nave);
 
-  /* set up pointers for linked list of measure, missing */
+  /* set up pointers for linked list of measure */
   if (catalog[0].sorted && (catalog[0].Nmeasure == catalog[0].Nmeas_disk)) {
     // this version is only valid if we have done a full catalog load, and if the catalog
@@ -100,6 +99,4 @@
     next_meas = build_measure_links (catalog[0].average, Nave, catalog[0].measure, Nmeas);
   }    
-  next_miss = init_missing_links (catalog[0].average, Nave, catalog[0].missing, Nmiss);
-  /* missing MUST be written 'sorted', or not at all */
 
   /* choose a radius for matches */
@@ -167,65 +164,42 @@
 	
     /** add measurements for this star **/
+
+    // set the new measurements
+    catalog[0].measure[Nmeas]          = stars[N].measure;
+
     /** dR,dD now represent arcsec **/
-    catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N].R);
+    catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N].average.R);
+    catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N].average.D);
+    catalog[0].measure[Nmeas].dbFlags  = 0;
+    catalog[0].measure[Nmeas].averef   = n;
+    catalog[0].measure[Nmeas].objID    = catalog[0].average[n].objID;
+    catalog[0].measure[Nmeas].catID    = catalog[0].catID;
+
+    // rationalize dR
     if (catalog[0].measure[Nmeas].dR > +180.0*3600.0) {
       // average on high end of boundary, move star up
-      stars[N].R += 360.0;
-      catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].R);
+      stars[N].average.R += 360.0;
+      catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].average.R);
     }
     if (catalog[0].measure[Nmeas].dR < -180.0*3600.0) {
       // average on low end of boundary, move star down
-      stars[N].R -= 360.0;
-      catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].R);
+      stars[N].average.R -= 360.0;
+      catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].average.R);
     }
     if (fabs(catalog[0].measure[Nmeas].dR) > 10*RADIUS) {
       fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
 	       catalog[0].average[n].R, catalog[0].average[n].D, 
-	       stars[N].R, stars[N].D,
+	       stars[N].average.R, stars[N].average.D,
 	       X1[i], X2[Jmin], 
 	       Y1[i], Y2[Jmin]);
     }
-    catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N].D);
-
-    /* XXX need to add dX, dY : need to load into stars[N].dX,dY */
-    /* XXX need to add stargal, Sky, dSky, qPSF, detID, imageID */
-    catalog[0].measure[Nmeas].Xccd     = stars[N].X;
-    catalog[0].measure[Nmeas].Yccd     = stars[N].Y;
-
-    catalog[0].measure[Nmeas].M        = stars[N].M;
-    catalog[0].measure[Nmeas].dM       = stars[N].dM;  /* error in input files stored in thousandths of mag */
-    catalog[0].measure[Nmeas].Mcal     = stars[N].Mcal;
-    catalog[0].measure[Nmeas].t        = stars[N].t;
-    catalog[0].measure[Nmeas].averef   = n;
-    catalog[0].measure[Nmeas].photcode = stars[N].code;  /* photcode */
-    catalog[0].measure[Nmeas].dophot   = stars[N].dophot;  
-    catalog[0].measure[Nmeas].dbFlags  = 0;
-    catalog[0].measure[Nmeas].dt       = stars[N].dt;
-    catalog[0].measure[Nmeas].airmass  = stars[N].airmass;
-
-    catalog[0].measure[Nmeas].photFlags = stars[N].flags;
-    catalog[0].measure[Nmeas].qPSF      = stars[N].psfQual;
-    catalog[0].measure[Nmeas].psfChisq  = stars[N].psfChisq;
-    catalog[0].measure[Nmeas].crNsigma  = stars[N].crNsigma;
-    catalog[0].measure[Nmeas].extNsigma = stars[N].extNsigma;
-    catalog[0].measure[Nmeas].Sky       = stars[N].sky;
-    catalog[0].measure[Nmeas].dSky      = stars[N].dsky;
-
-    catalog[0].measure[Nmeas].stargal   = 0;
-
-    catalog[0].measure[Nmeas].detID     = stars[N].detID;
-    catalog[0].measure[Nmeas].imageID   = stars[N].imageID;
-
-    catalog[0].measure[Nmeas].dXccd     = stars[N].dX;
-    catalog[0].measure[Nmeas].dYccd     = stars[N].dY;
-
-    catalog[0].measure[Nmeas].Map       = stars[N].Map;
-
-    // XXX saturate range for FWx, FWy, theta
-    // XXX convert to unsigned int for these...
-    catalog[0].measure[Nmeas].FWx      = 100*stars[N].fx;
-    catalog[0].measure[Nmeas].FWy      = 100*stars[N].fy;
-    catalog[0].measure[Nmeas].theta    = (0xffff/360.0)*stars[N].df;
-	
+
+
+    /* adds the measurement to the calibration if appropriate color terms are found */
+    /* we call this before (optionally) setting the average magnitude to avoid auto-correlations */
+    if (options.calibrate) {
+      AddToCalibration (&catalog[0].average[n], &catalog[0].secfilt[n*Nsecfilt], catalog[0].measure, &catalog[0].measure[Nmeas], next_meas, N);
+    }
+
     /* set the average magnitude if not already set and the photcode.equiv is not 0 */
     /* in UPDATE mode, this value is not saved; use relphot to recalculate */
@@ -234,9 +208,4 @@
 	catalog[0].secfilt[n*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas]);
       }
-    }
-
-    /* adds the measurement to the calibration if appropriate color terms are found */
-    if (options.calibrate) {
-      AddToCalibration (&catalog[0].average[n], &catalog[0].secfilt[n*Nsecfilt], catalog[0].measure, &catalog[0].measure[Nmeas], next_meas, N);
     }
 
@@ -270,26 +239,34 @@
     N = N1[i];
     if (stars[N].found >= 0) continue;
-    if (!IN_REGION (stars[N].R, stars[N].D)) continue;
-
-    catalog[0].average[Nave].R         	   = stars[N].R;
-    catalog[0].average[Nave].D         	   = stars[N].D;
-    catalog[0].average[Nave].Xp        	   = 0;
+    if (!IN_REGION (stars[N].average.R, stars[N].average.D)) continue;
+
+    catalog[0].average[Nave].R         	   = stars[N].average.R;
+    catalog[0].average[Nave].D         	   = stars[N].average.D;
+    catalog[0].average[Nave].dR        	   = 0;
+    catalog[0].average[Nave].dD        	   = 0;
+
     catalog[0].average[Nave].Nmeasure  	   = 1;
     catalog[0].average[Nave].Nmissing  	   = 0;
+    catalog[0].average[Nave].Nextend       = 0;
+
     catalog[0].average[Nave].measureOffset = Nmeas;
     catalog[0].average[Nave].missingOffset = -1;
-    catalog[0].average[Nave].code          = 0;
-
-    catalog[0].average[Nave].dR        = 0;
-    catalog[0].average[Nave].dD        = 0;
-    catalog[0].average[Nave].uR        = 0;
-    catalog[0].average[Nave].uD        = 0;
-    catalog[0].average[Nave].duR       = 0;
-    catalog[0].average[Nave].duD       = 0;
-    catalog[0].average[Nave].P         = 0;
-    catalog[0].average[Nave].dP        = 0;
-
-    catalog[0].average[Nave].objID     = objID;
-    catalog[0].average[Nave].catID     = catID;
+    catalog[0].average[Nave].extendOffset  = -1;
+
+    catalog[0].average[Nave].uR        	   = 0;
+    catalog[0].average[Nave].uD        	   = 0;
+    catalog[0].average[Nave].duR       	   = 0;
+    catalog[0].average[Nave].duD       	   = 0;
+    catalog[0].average[Nave].P         	   = 0;
+    catalog[0].average[Nave].dP        	   = 0;
+
+    catalog[0].average[Nave].Xp        	   = 0;
+    catalog[0].average[Nave].ChiSq    	   = 0.0;
+    catalog[0].average[Nave].Npos    	   = 0;
+
+    catalog[0].average[Nave].objID     	   = objID;
+    catalog[0].average[Nave].catID     	   = catID;
+    catalog[0].average[Nave].flags         = 0;
+
     objID ++;
 
@@ -298,42 +275,20 @@
       catalog[0].secfilt[Nave*Nsecfilt+j].dM = NAN;
       catalog[0].secfilt[Nave*Nsecfilt+j].Xm = NAN_S_SHORT;
-    }
-
-    catalog[0].measure[Nmeas].Xccd     = stars[N].X;
-    catalog[0].measure[Nmeas].Yccd     = stars[N].Y;
-
+      catalog[0].secfilt[Nave*Nsecfilt+j].M_20 	= NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsecfilt+j].M_80 	= NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Ncode = 0;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Nused = 0;
+    }
+
+    // supply the measurments from this detection
+    catalog[0].measure[Nmeas]           = stars[N].measure;
+
+    // the following measure elements cannot be set until here:
     catalog[0].measure[Nmeas].dR       = 0.0;
     catalog[0].measure[Nmeas].dD       = 0.0;
-    catalog[0].measure[Nmeas].M        = stars[N].M;
-    catalog[0].measure[Nmeas].dM       = stars[N].dM;
-    catalog[0].measure[Nmeas].Mcal     = stars[N].Mcal;
-    catalog[0].measure[Nmeas].t        = stars[N].t;
+    catalog[0].measure[Nmeas].dbFlags  = 0;
     catalog[0].measure[Nmeas].averef   = Nave;
-    catalog[0].measure[Nmeas].photcode = stars[N].code;  /* photcode */
-    catalog[0].measure[Nmeas].dophot   = stars[N].dophot;  
-    catalog[0].measure[Nmeas].dbFlags  = 0;
-    catalog[0].measure[Nmeas].dt       = stars[N].dt;
-    catalog[0].measure[Nmeas].airmass  = stars[N].airmass;
-
-    catalog[0].measure[Nmeas].photFlags = stars[N].flags;
-    catalog[0].measure[Nmeas].qPSF      = stars[N].psfQual;
-    catalog[0].measure[Nmeas].psfChisq  = stars[N].psfChisq;
-    catalog[0].measure[Nmeas].crNsigma  = stars[N].crNsigma;
-    catalog[0].measure[Nmeas].extNsigma = stars[N].extNsigma;
-    catalog[0].measure[Nmeas].Sky       = stars[N].sky;
-    catalog[0].measure[Nmeas].dSky      = stars[N].dsky;
-
-    catalog[0].measure[Nmeas].stargal   = 0;
-
-    catalog[0].measure[Nmeas].detID     = stars[N].detID;
-    catalog[0].measure[Nmeas].imageID   = stars[N].imageID;
-
-    catalog[0].measure[Nmeas].dXccd     = stars[N].dX;
-    catalog[0].measure[Nmeas].dYccd     = stars[N].dY;
-
-    catalog[0].measure[Nmeas].Map      = stars[N].Map;
-    catalog[0].measure[Nmeas].FWx      = 100*stars[N].fx;
-    catalog[0].measure[Nmeas].FWy      = 100*stars[N].fy;
-    catalog[0].measure[Nmeas].theta    = (0xffff/360.0)*stars[N].df;
+    catalog[0].measure[Nmeas].objID    = catalog[0].average[Nave].objID;
+    catalog[0].measure[Nmeas].catID    = catalog[0].catID;
 
     /* set the average magnitude if not already set and the photcode.equiv is not 0 */
Index: /trunk/Ohana/src/addstar/src/find_matches_refstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches_refstars.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/find_matches_refstars.c	(revision 21508)
@@ -69,5 +69,5 @@
   
   for (i = 0; i < Nstars; i++) {
-    RD_to_XY (&X1[i], &Y1[i], stars[i][0].R, stars[i][0].D, &tcoords);
+    RD_to_XY (&X1[i], &Y1[i], stars[i][0].average.R, stars[i][0].average.D, &tcoords);
     N1[i] = i;
   }
@@ -134,56 +134,48 @@
       /* find last measurement of this star */
       for (k = 0; k < catalog[0].average[n].Nmeasure - 1; k++) m = next[m];
+
       /* set up references */
       next[Nmeas] = next[m];
       next[m] = Nmeas;
+
       /* last just was moved */
       if (next[Nmeas] == -1) last = Nmeas;
 	
       /** add measurements for this star **/
+
+      // set the new measurements
+      catalog[0].measure[Nmeas]          = stars[N][0].measure;
+
       /** *** dR,dD now in arcsec *** **/
-      catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N][0].R);
-      catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N][0].D);
-      catalog[0].measure[Nmeas].M        = stars[N][0].M;
-      catalog[0].measure[Nmeas].dM       = stars[N][0].dM;
-      catalog[0].measure[Nmeas].Mcal     = 0;
-      catalog[0].measure[Nmeas].t        = (TIMEREF == 0) ? stars[N][0].t : TIMEREF; /** careful : time_t vs e_time **/
+      catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N][0].average.R);
+      catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N][0].average.D);
+
+      // rationalize dR:
+      if (catalog[0].measure[Nmeas].dR > +180.0*3600.0) {
+	  // average on high end of boundary, move star up
+	  stars[N][0].average.R += 360.0;
+	  catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N][0].average.R);
+      }
+      if (catalog[0].measure[Nmeas].dR < -180.0*3600.0) {
+	  // average on low end of boundary, move star down
+	  stars[N][0].average.R -= 360.0;
+	  catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N][0].average.R);
+      }
+
       catalog[0].measure[Nmeas].averef   = n;
-      catalog[0].measure[Nmeas].photcode = stars[N][0].code;
-      catalog[0].measure[Nmeas].dophot   = 0;
       catalog[0].measure[Nmeas].dbFlags  = 0;
-      catalog[0].measure[Nmeas].dt       = 0xffff;
-
-      catalog[0].measure[Nmeas].Map      = NAN;
-      catalog[0].measure[Nmeas].airmass  = 0;
-      catalog[0].measure[Nmeas].FWx      = stars[N][0].fx; // XXX make sure these are zero'ed as needed
-      catalog[0].measure[Nmeas].FWy      = stars[N][0].fy; // XXX make sure these are zero'ed as needed
-      catalog[0].measure[Nmeas].theta    = stars[N][0].df; // XXX make sure these are zero'ed as needed
-	
-      catalog[0].measure[Nmeas].photFlags = stars[N][0].flags; // XXX make sure these are zero'ed as needed
-      catalog[0].measure[Nmeas].qPSF      = 0;
-      catalog[0].measure[Nmeas].psfChisq  = 0;
-      catalog[0].measure[Nmeas].crNsigma  = 0;
-      catalog[0].measure[Nmeas].extNsigma = 0;
-      catalog[0].measure[Nmeas].Sky       = 0;
-      catalog[0].measure[Nmeas].dSky      = 0;
-
-      catalog[0].measure[Nmeas].stargal   = 0; // XXX not yet set
-
-      catalog[0].measure[Nmeas].detID     = 0;
-      catalog[0].measure[Nmeas].imageID   = 0;
-
-      catalog[0].measure[Nmeas].dXccd     = 0;
-      catalog[0].measure[Nmeas].dYccd     = 0;
-
-      catalog[0].measure[Nmeas].Xccd     = stars[N][0].X; // XXX make sure these are zero'ed as needed
-      catalog[0].measure[Nmeas].Yccd     = stars[N][0].Y; // XXX make sure these are zero'ed as needed
+      catalog[0].measure[Nmeas].objID    = catalog[0].average[n].objID;
+      catalog[0].measure[Nmeas].catID    = catalog[0].catID;
+
+      catalog[0].measure[Nmeas].t        = (TIMEREF == 0) ? stars[N][0].measure.t      : TIMEREF; /** careful : time_t vs e_time **/
+      catalog[0].measure[Nmeas].t_msec   = (TIMEREF == 0) ? stars[N][0].measure.t_msec : TIMEREF; /** careful : time_t vs e_time **/
 
       if (ACCEPT_MOTION) {
-	catalog[0].average[n].uR         = stars[N][0].uR;
-	catalog[0].average[n].uD         = stars[N][0].uD;
-	catalog[0].average[n].duR        = stars[N][0].duR;
-	catalog[0].average[n].duD        = stars[N][0].duD;
-	catalog[0].average[n].P          = stars[N][0].P;
-	catalog[0].average[n].dP         = stars[N][0].dP;
+	catalog[0].average[n].uR         = stars[N][0].average.uR;
+	catalog[0].average[n].uD         = stars[N][0].average.uD;
+	catalog[0].average[n].duR        = stars[N][0].average.duR;
+	catalog[0].average[n].duD        = stars[N][0].average.duD;
+	catalog[0].average[n].P          = stars[N][0].average.P;
+	catalog[0].average[n].dP         = stars[N][0].average.dP;
       }
 
@@ -233,86 +225,72 @@
   for (i = 0; (i < Nstars) && !options.only_match; i+=NREFSTAR_GROUP) {
     N = N1[i];
-    if (stars[N][0].found >= 0) continue;
-
-    catalog[0].average[Nave].R         	   = stars[N][0].R;
-    catalog[0].average[Nave].D         	   = stars[N][0].D;
-    catalog[0].average[Nave].Xp        	   = 0;
+    if (stars[N][0].found > -1) continue;
+
+    catalog[0].average[Nave].R         	   = stars[N][0].average.R;
+    catalog[0].average[Nave].D         	   = stars[N][0].average.D;
+
     catalog[0].average[Nave].Nmeasure      = NREFSTAR_GROUP;
     catalog[0].average[Nave].Nmissing      = 0;
+    catalog[0].average[Nave].Nextend       = 0;
+
     catalog[0].average[Nave].measureOffset = Nmeas;
     catalog[0].average[Nave].missingOffset = -1;
-    catalog[0].average[Nave].code          = 0;
+    catalog[0].average[Nave].extendOffset  = -1;
 
     if (ACCEPT_MOTION) {
-      catalog[0].average[Nave].dR    = stars[N][0].dR;
-      catalog[0].average[Nave].dD    = stars[N][0].dD;
-      catalog[0].average[Nave].uR    = stars[N][0].uR;
-      catalog[0].average[Nave].uD    = stars[N][0].uD;
-      catalog[0].average[Nave].duR   = stars[N][0].duR;
-      catalog[0].average[Nave].duD   = stars[N][0].duD;
-      catalog[0].average[Nave].P     = stars[N][0].P;
-      catalog[0].average[Nave].dP    = stars[N][0].dP;
+      catalog[0].average[Nave].dR    	   = stars[N][0].average.dR;
+      catalog[0].average[Nave].dD    	   = stars[N][0].average.dD;
+      catalog[0].average[Nave].uR    	   = stars[N][0].average.uR;
+      catalog[0].average[Nave].uD    	   = stars[N][0].average.uD;
+      catalog[0].average[Nave].duR   	   = stars[N][0].average.duR;
+      catalog[0].average[Nave].duD   	   = stars[N][0].average.duD;
+      catalog[0].average[Nave].P     	   = stars[N][0].average.P;
+      catalog[0].average[Nave].dP    	   = stars[N][0].average.dP;
     } else {
-      catalog[0].average[Nave].dR    = 0;
-      catalog[0].average[Nave].dD    = 0;
-      catalog[0].average[Nave].uR    = 0;
-      catalog[0].average[Nave].uD    = 0;
-      catalog[0].average[Nave].duR   = 0;
-      catalog[0].average[Nave].duD   = 0;
-      catalog[0].average[Nave].P     = 0;
-      catalog[0].average[Nave].dP    = 0;
-    }
-
-    catalog[0].average[Nave].objID     = objID;
-    catalog[0].average[Nave].catID     = catID;
+      catalog[0].average[Nave].dR    	   = 0;
+      catalog[0].average[Nave].dD    	   = 0;
+      catalog[0].average[Nave].uR    	   = 0;
+      catalog[0].average[Nave].uD    	   = 0;
+      catalog[0].average[Nave].duR   	   = 0;
+      catalog[0].average[Nave].duD   	   = 0;
+      catalog[0].average[Nave].P     	   = 0;
+      catalog[0].average[Nave].dP    	   = 0;
+      catalog[0].average[Nave].Xp    	   = 0;
+    }
+
+    catalog[0].average[Nave].Xp            = 0;
+    catalog[0].average[Nave].ChiSq    	   = 0.0;
+    catalog[0].average[Nave].Npos    	   = 0;
+
+    catalog[0].average[Nave].objID     	   = objID;
+    catalog[0].average[Nave].catID     	   = catID;
+    catalog[0].average[Nave].flags     	   = 0;
+
     objID ++;
 
     for (j = 0; j < Nsecfilt; j++) {
-      catalog[0].secfilt[Nave*Nsecfilt+j].M  = NAN;
-      catalog[0].secfilt[Nave*Nsecfilt+j].dM = NAN;
-      catalog[0].secfilt[Nave*Nsecfilt+j].Xm = NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsecfilt+j].M  	= NAN;
+      catalog[0].secfilt[Nave*Nsecfilt+j].dM 	= NAN;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Xm 	= NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsecfilt+j].M_20 	= NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsecfilt+j].M_80 	= NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Ncode = 0;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Nused = 0;
     }
 
     for (j = 0; j < NREFSTAR_GROUP; j++) {
       N = N1[i + j];
+
+      catalog[0].measure[Nmeas]          = stars[N][0].measure;
+
       catalog[0].measure[Nmeas].dR       = 0.0;
       catalog[0].measure[Nmeas].dD       = 0.0;
-      catalog[0].measure[Nmeas].M        = stars[N][0].M;
-      catalog[0].measure[Nmeas].dM       = stars[N][0].dM;
-      catalog[0].measure[Nmeas].Mcal     = 0;
-      catalog[0].measure[Nmeas].t        = (stars[N][0].t == 0) ? TIMEREF : stars[N][0].t; /** careful : time_t vs e_time **/
+
+      catalog[0].measure[Nmeas].t        = (stars[N][0].measure.t == 0) ? TIMEREF : stars[N][0].measure.t;      /** careful : time_t vs e_time **/
+      catalog[0].measure[Nmeas].t_msec   = (stars[N][0].measure.t == 0) ?       0 : stars[N][0].measure.t_msec; /** careful : time_t vs e_time **/
+      catalog[0].measure[Nmeas].dbFlags  = 0;
       catalog[0].measure[Nmeas].averef   = Nave;
-      catalog[0].measure[Nmeas].photcode = stars[N][0].code;
-      catalog[0].measure[Nmeas].dophot   = 0;
-      catalog[0].measure[Nmeas].dbFlags  = 0;
-      catalog[0].measure[Nmeas].dt       = 0xffff;
-
-      catalog[0].measure[Nmeas].photFlags = stars[N][0].flags; // XXX make sure these are zero'ed as needed
-      catalog[0].measure[Nmeas].qPSF      = 0;
-      catalog[0].measure[Nmeas].psfChisq  = 0;
-      catalog[0].measure[Nmeas].crNsigma  = 0;
-      catalog[0].measure[Nmeas].extNsigma = 0;
-      catalog[0].measure[Nmeas].Sky       = 0;
-      catalog[0].measure[Nmeas].dSky      = 0;
-
-      catalog[0].measure[Nmeas].stargal   = 0; // XXX not yet set
-
-      catalog[0].measure[Nmeas].detID     = 0;
-      catalog[0].measure[Nmeas].imageID   = 0;
-
-      catalog[0].measure[Nmeas].dXccd     = 0;
-      catalog[0].measure[Nmeas].dYccd     = 0;
-
-      catalog[0].measure[Nmeas].Xccd     = stars[N][0].X; // XXX make sure these are zero'ed as needed
-      catalog[0].measure[Nmeas].Yccd     = stars[N][0].Y; // XXX make sure these are zero'ed as needed
-
-      catalog[0].measure[Nmeas].airmass  = 0;
-      catalog[0].measure[Nmeas].Map      = NAN;
-      catalog[0].measure[Nmeas].FWx      = stars[N][0].fx; // XXX make sure these are zero'ed as needed
-      catalog[0].measure[Nmeas].FWy      = stars[N][0].fy; // XXX make sure these are zero'ed as needed
-      catalog[0].measure[Nmeas].theta    = stars[N][0].df; // XXX make sure these are zero'ed as needed
-
-      catalog[0].measure[Nmeas].Xccd     = 0.0;
-      catalog[0].measure[Nmeas].Yccd     = 0.0;
+      catalog[0].measure[Nmeas].objID    = catalog[0].average[Nave].objID;
+      catalog[0].measure[Nmeas].catID    = catalog[0].catID;
 
       stars[N][0].found = Nmeas;
Index: /trunk/Ohana/src/addstar/src/find_subset.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_subset.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/find_subset.c	(revision 21508)
@@ -20,8 +20,8 @@
   /* find stars within ra,dec region */
   for (i = N = 0; i < Nstars; i++) {
-    if (stars[i].R <  RA0)  continue;
-    if (stars[i].R >= RA1)  continue;
-    if (stars[i].D <  DEC0) continue;
-    if (stars[i].D >= DEC1) continue;
+    if (stars[i].average.R <  RA0)  continue;
+    if (stars[i].average.R >= RA1)  continue;
+    if (stars[i].average.D <  DEC0) continue;
+    if (stars[i].average.D >= DEC1) continue;
 
     subset[N] = &stars[i];
Index: /trunk/Ohana/src/addstar/src/get2mass_dr2.c
===================================================================
--- /trunk/Ohana/src/addstar/src/get2mass_dr2.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/get2mass_dr2.c	(revision 21508)
@@ -68,21 +68,21 @@
 	dparse (&J,  1, &buffer[NBYTE*i + 53]);
 	dparse (&dJ, 2, &buffer[NBYTE*i + 53]);
-	stars[Nstars].M    = J;
-	stars[Nstars].dM   = dJ;
-	stars[Nstars].code = TM_J;
+	stars[Nstars].M        = J;
+	stars[Nstars].dM       = dJ;
+	stars[Nstars].photcode = TM_J;
       }
       if (photcode == TM_H) {
 	dparse (&H,  1, &buffer[NBYTE*i + 72]);
 	dparse (&dH, 2, &buffer[NBYTE*i + 72]);
-	stars[Nstars].M    = H;
-	stars[Nstars].dM   = dH;
-	stars[Nstars].code = TM_H;
+	stars[Nstars].M        = H;
+	stars[Nstars].dM       = dH;
+	stars[Nstars].photcode = TM_H;
       }
       if (photcode == TM_K) {
 	dparse (&K,  1, &buffer[NBYTE*i + 91]);
 	dparse (&dK, 2, &buffer[NBYTE*i + 91]);
-	stars[Nstars].M    = K;
-	stars[Nstars].dM   = dK;
-	stars[Nstars].code = TM_K;
+	stars[Nstars].M        = K;
+	stars[Nstars].dM       = dK;
+	stars[Nstars].photcode = TM_K;
       }
       Nstars ++;
Index: /trunk/Ohana/src/addstar/src/get2mass_full.c
===================================================================
--- /trunk/Ohana/src/addstar/src/get2mass_full.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/get2mass_full.c	(revision 21508)
@@ -15,44 +15,44 @@
   ptr = next2MASSfield (ptr); // dec (assumed to be already set)
   ptr = next2MASSfield (ptr); // err_maj
-  star[0][0].fx = strtod (ptr, NULL);
+  star[0][0].measure.FWx = ShortPixels(strtod (ptr, NULL));
   ptr = next2MASSfield (ptr); // err_min
-  star[0][0].fy = strtod (ptr, NULL);
+  star[0][0].measure.FWy = ShortPixels(strtod (ptr, NULL));
   ptr = next2MASSfield (ptr); // err_ang
-  star[0][0].df = strtod (ptr, NULL);
-
-  star[2][0].fx = star[2][0].fx = star[0][0].fx;
-  star[2][0].fy = star[2][0].fy = star[0][0].fy;
-  star[2][0].df = star[2][0].df = star[0][0].df;
+  star[0][0].measure.theta = ShortPixels(strtod (ptr, NULL));
+
+  star[2][0].measure.FWx   = star[2][0].measure.FWx   = star[0][0].measure.FWx;
+  star[2][0].measure.FWy   = star[2][0].measure.FWy   = star[0][0].measure.FWy;
+  star[2][0].measure.theta = star[2][0].measure.theta = star[0][0].measure.theta;
 
   ptr = next2MASSfield (ptr); // designation (skip)
 
   ptr = next2MASSfield (ptr); // j_m
-  star[0][0].M  = strtod (ptr, NULL);
+  star[0][0].measure.M  = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // j_cmsig
-  star[0][0].dM = strtod (ptr, NULL);
+  star[0][0].measure.dM = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // j_msigcom
   dMfull = strtod (ptr, NULL);
-  star[0][0].dMcal = sqrt (SQ(dMfull) - SQ(star[0][0].dM));
+  star[0][0].measure.dMcal = sqrt (SQ(dMfull) - SQ(star[0][0].measure.dM));
   ptr = next2MASSfield (ptr); // j_snr (skip)
 
   ptr = next2MASSfield (ptr); // h_m
-  star[1][0].M  = strtod (ptr, NULL);
+  star[1][0].measure.M  = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // h_cmsig
-  star[1][0].dM = strtod (ptr, NULL);
+  star[1][0].measure.dM = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // h_msigcom
   dMfull = strtod (ptr, NULL);
-  star[1][0].dMcal = sqrt (SQ(dMfull) - SQ(star[1][0].dM));
+  star[1][0].measure.dMcal = sqrt (SQ(dMfull) - SQ(star[1][0].measure.dM));
   ptr = next2MASSfield (ptr); // h_snr (skip)
 
   ptr = next2MASSfield (ptr); // k_m
-  star[2][0].M  = strtod (ptr, NULL);
+  star[2][0].measure.M  = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // k_cmsig
-  star[2][0].dM = strtod (ptr, NULL);
+  star[2][0].measure.dM = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // k_msigcom
   dMfull = strtod (ptr, NULL);
-  star[2][0].dMcal = sqrt (SQ(dMfull) - SQ(star[2][0].dM));
+  star[2][0].measure.dMcal = sqrt (SQ(dMfull) - SQ(star[2][0].measure.dM));
   ptr = next2MASSfield (ptr); // k_snr (skip)
 
-  star[2][0].flags = star[1][0].flags = star[0][0].flags = 0;
+  star[2][0].measure.photFlags = star[1][0].measure.photFlags = star[0][0].measure.photFlags = 0;
 
   ptr = next2MASSfield (ptr); // ph_qual
@@ -99,29 +99,29 @@
 
   ptr = next2MASSfield (ptr); // x_scan
-  star[0][0].X = strtod (ptr, NULL);
-  star[2][0].X = star[1][0].X = star[0][0].X;
+  star[0][0].measure.Xccd = strtod (ptr, NULL);
+  star[2][0].measure.Xccd = star[1][0].measure.Xccd = star[0][0].measure.Xccd;
 
   ptr = next2MASSfield (ptr); // jdate (julian date)
   jd = strtod (ptr, NULL);
-  star[0][0].t = ohana_jd_to_sec (jd);
-  star[2][0].t = star[1][0].t = star[0][0].t;
+  star[0][0].measure.t = ohana_jd_to_sec (jd);
+  star[2][0].measure.t = star[1][0].measure.t = star[0][0].measure.t;
 
   ptr = next2MASSfield (ptr); // j_psfchi
-  star[0][0].psfChisq = strtod (ptr, NULL);
+  star[0][0].measure.psfChisq = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // h_psfchi
-  star[1][0].psfChisq = strtod (ptr, NULL);
+  star[1][0].measure.psfChisq = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // k_psfchi
-  star[2][0].psfChisq = strtod (ptr, NULL);
+  star[2][0].measure.psfChisq = strtod (ptr, NULL);
 
   ptr = next2MASSfield (ptr); // j_m_stdap
-  star[0][0].Map = strtod (ptr, NULL);
+  star[0][0].measure.Map = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // j_msig_stdap (skip?)
 
   ptr = next2MASSfield (ptr); // h_m_stdap
-  star[1][0].Map = strtod (ptr, NULL);
+  star[1][0].measure.Map = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // h_msig_stdap (skip?)
 
   ptr = next2MASSfield (ptr); // k_m_stdap
-  star[2][0].Map = strtod (ptr, NULL);
+  star[2][0].measure.Map = strtod (ptr, NULL);
   ptr = next2MASSfield (ptr); // k_msig_stdap (skip?)
 
@@ -140,18 +140,15 @@
   set2MASS_use_flag (star[2], ptr[0]);
 
-  star[0][0].code  = TM_J;
-  star[0][0].found = -1;
-  star[0][0].detID   = 0;
-  star[0][0].imageID = 0;
-
-  star[1][0].code  = TM_H;
-  star[1][0].found = -1;
-  star[1][0].detID   = 0;
-  star[1][0].imageID = 0;
-
-  star[2][0].code  = TM_K;
-  star[2][0].found = -1;
-  star[2][0].detID   = 0;
-  star[2][0].imageID = 0;
+  star[0][0].measure.photcode  = TM_J;
+  star[0][0].measure.detID   = 0;
+  star[0][0].measure.imageID = 0;
+
+  star[1][0].measure.photcode  = TM_H;
+  star[1][0].measure.detID   = 0;
+  star[1][0].measure.imageID = 0;
+
+  star[2][0].measure.photcode  = TM_K;
+  star[2][0].measure.detID   = 0;
+  star[2][0].measure.imageID = 0;
 
   return TRUE;
@@ -174,12 +171,12 @@
 
   switch (qual) {
-    case 'X': star[0].flags |= 0x0000; break;
-    case 'U': star[0].flags |= 0x0001; break;
-    case 'F': star[0].flags |= 0x0002; break;
-    case 'E': star[0].flags |= 0x0003; break;
-    case 'A': star[0].flags |= 0x0004; break;
-    case 'B': star[0].flags |= 0x0005; break;
-    case 'C': star[0].flags |= 0x0006; break;
-    case 'D': star[0].flags |= 0x0007; break;
+    case 'X': star[0].measure.photFlags |= 0x0000; break;
+    case 'U': star[0].measure.photFlags |= 0x0001; break;
+    case 'F': star[0].measure.photFlags |= 0x0002; break;
+    case 'E': star[0].measure.photFlags |= 0x0003; break;
+    case 'A': star[0].measure.photFlags |= 0x0004; break;
+    case 'B': star[0].measure.photFlags |= 0x0005; break;
+    case 'C': star[0].measure.photFlags |= 0x0006; break;
+    case 'D': star[0].measure.photFlags |= 0x0007; break;
     default: 
       fprintf (stderr, "error!\n");
@@ -192,11 +189,11 @@
 
   switch (qual) {
-    case '0': star[0].flags |= 0x0000; break;
-    case '1': star[0].flags |= 0x0010; break;
-    case '2': star[0].flags |= 0x0020; break;
-    case '3': star[0].flags |= 0x0030; break;
-    case '4': star[0].flags |= 0x0040; break;
-    case '6': star[0].flags |= 0x0050; break;
-    case '9': star[0].flags |= 0x0060; break;
+    case '0': star[0].measure.photFlags |= 0x0000; break;
+    case '1': star[0].measure.photFlags |= 0x0010; break;
+    case '2': star[0].measure.photFlags |= 0x0020; break;
+    case '3': star[0].measure.photFlags |= 0x0030; break;
+    case '4': star[0].measure.photFlags |= 0x0040; break;
+    case '6': star[0].measure.photFlags |= 0x0050; break;
+    case '9': star[0].measure.photFlags |= 0x0060; break;
     default: 
       fprintf (stderr, "error!\n");
@@ -209,10 +206,10 @@
 
   switch (qual) {
-    case 'p': star[0].flags |= 0x0000; break;
-    case 'c': star[0].flags |= 0x0100; break;
-    case 'd': star[0].flags |= 0x0200; break;
-    case 's': star[0].flags |= 0x0300; break;
-    case 'b': star[0].flags |= 0x0400; break;
-    case '0': star[0].flags |= 0x0500; break;
+    case 'p': star[0].measure.photFlags |= 0x0000; break;
+    case 'c': star[0].measure.photFlags |= 0x0100; break;
+    case 'd': star[0].measure.photFlags |= 0x0200; break;
+    case 's': star[0].measure.photFlags |= 0x0300; break;
+    case 'b': star[0].measure.photFlags |= 0x0400; break;
+    case '0': star[0].measure.photFlags |= 0x0500; break;
     default: 
       fprintf (stderr, "error!\n");
@@ -225,7 +222,7 @@
 
   switch (qual) {
-    case '0': star[0].flags &= ~0x0008; break;
-    case '1': star[0].flags &= ~0x0008; break;
-    default:  star[0].flags |=  0x0008; break;
+    case '0': star[0].measure.photFlags &= ~0x0008; break;
+    case '1': star[0].measure.photFlags &= ~0x0008; break;
+    default:  star[0].measure.photFlags |=  0x0008; break;
   }      
   return (TRUE);
@@ -235,9 +232,9 @@
 
   switch (qual) {
-    case '0': star[0].flags &= ~0x0080; break;
-    case '1': star[0].flags &= ~0x0080; break;
+    case '0': star[0].measure.photFlags &= ~0x0080; break;
+    case '1': star[0].measure.photFlags &= ~0x0080; break;
     default:  
-      star[0].flags |= 0x0080; 
-      star[0].extNsigma = 100.0;
+      star[0].measure.photFlags |= 0x0080; 
+      star[0].measure.extNsigma = 100.0;
       break;
   }      
@@ -248,7 +245,7 @@
 
   switch (qual) {
-    case '0': star[0].flags &= ~0x0800; break;
-    case '1': star[0].flags &= ~0x0800; break;
-    default:  star[0].flags |=  0x0800; break;
+    case '0': star[0].measure.photFlags &= ~0x0800; break;
+    case '1': star[0].measure.photFlags &= ~0x0800; break;
+    default:  star[0].measure.photFlags |=  0x0800; break;
   }      
   return (TRUE);
@@ -258,7 +255,7 @@
 
   switch (qual) {
-    case '0': star[0].flags &= ~0x1000; break;
-    case '1': star[0].flags &= ~0x1000; break;
-    default:  star[0].flags |=  0x1000; break;
+    case '0': star[0].measure.photFlags &= ~0x1000; break;
+    case '1': star[0].measure.photFlags &= ~0x1000; break;
+    default:  star[0].measure.photFlags |=  0x1000; break;
   }      
   return (TRUE);
@@ -268,6 +265,6 @@
 
   switch (qual) {
-    case '0': star[0].flags &= ~0x2000; break;
-    case '1': star[0].flags |=  0x2000; break;
+    case '0': star[0].measure.photFlags &= ~0x2000; break;
+    case '1': star[0].measure.photFlags |=  0x2000; break;
     default:  abort();
   }      
Index: /trunk/Ohana/src/addstar/src/get2mass_ops.c
===================================================================
--- /trunk/Ohana/src/addstar/src/get2mass_ops.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/get2mass_ops.c	(revision 21508)
@@ -42,4 +42,6 @@
   double M, dM;
   e_time time;
+
+  InitStar (star);
 
   ptr = skipNbounds (line, '|', FilterSkip, Nmax);
@@ -57,11 +59,10 @@
   }
 
-  star[0].M       = M;
-  star[0].dM      = dM;
-  star[0].code    = Photcode;
-  star[0].t       = time;
-  star[0].found   = -1;
-  star[0].detID   = 0;
-  star[0].imageID = 0;
+  star[0].measure.M        = M;
+  star[0].measure.dM       = dM;
+  star[0].measure.photcode = Photcode;
+  star[0].measure.t        = time;
+  star[0].measure.detID    = 0;
+  star[0].measure.imageID  = 0;
 
   return TRUE;
@@ -107,27 +108,24 @@
   // how many bits are being used for the 2mass flags; can we just set photFlags based on them?
 
-  star[0].M     = J;
-  star[0].dM    = dJ;
-  star[0].code  = TM_J;
-  star[0].t     = time;
-  star[0].found = -1;
-  star[0].detID   = 0;
-  star[0].imageID = 0;
-
-  star[1].M     = H;
-  star[1].dM    = dH;
-  star[1].code  = TM_H;
-  star[1].t     = time;
-  star[1].found = -1;
-  star[1].detID   = 0;
-  star[1].imageID = 0;
-
-  star[2].M     = K;
-  star[2].dM    = dK;
-  star[2].code  = TM_K;
-  star[2].t     = time;
-  star[2].found = -1;
-  star[2].detID   = 0;
-  star[2].imageID = 0;
+  star[0].measure.M         = J;
+  star[0].measure.dM        = dJ;
+  star[0].measure.photcode  = TM_J;
+  star[0].measure.t         = time;
+  star[0].measure.detID     = 0;
+  star[0].measure.imageID   = 0;
+
+  star[1].measure.M         = H;
+  star[1].measure.dM        = dH;
+  star[1].measure.photcode  = TM_H;
+  star[1].measure.t         = time;
+  star[1].measure.detID     = 0;
+  star[1].measure.imageID   = 0;
+
+  star[2].measure.M         = K;
+  star[2].measure.dM        = dK;
+  star[2].measure.photcode  = TM_K;
+  star[2].measure.t         = time;
+  star[2].measure.detID     = 0;
+  star[2].measure.imageID   = 0;
 
   return TRUE;
Index: /trunk/Ohana/src/addstar/src/getgsc.c
===================================================================
--- /trunk/Ohana/src/addstar/src/getgsc.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/getgsc.c	(revision 21508)
@@ -67,26 +67,20 @@
     Nline = nbytes / BYTES_STAR;
     for (i = 0; i < Nline; i++) {
+
+      InitStar (&stars[Nstars]);
+
       Nbyte = i*BYTES_STAR;
-      dparse (&stars[Nstars].R, 1, &buffer[Nbyte]);
-      dparse (&stars[Nstars].D, 2, &buffer[Nbyte]);
-      if (stars[Nstars].R < UserPatch.Rmin) continue;
-      if (stars[Nstars].R > UserPatch.Rmax) continue;
-      if (stars[Nstars].D < UserPatch.Dmin) continue;
-      if (stars[Nstars].D > UserPatch.Dmax) continue;
+      dparse (&stars[Nstars].average.R, 1, &buffer[Nbyte]);
+      dparse (&stars[Nstars].average.D, 2, &buffer[Nbyte]);
 
-      dparse (&stars[Nstars].M, 3, &buffer[Nbyte]);
-      stars[Nstars].dM 	  = NAN;
-      stars[Nstars].t  	  = 0;
-      stars[Nstars].code  = GSC_M;
-      stars[Nstars].found = -1;
+      if (stars[Nstars].average.R < UserPatch.Rmin) continue;
+      if (stars[Nstars].average.R > UserPatch.Rmax) continue;
+      if (stars[Nstars].average.D < UserPatch.Dmin) continue;
+      if (stars[Nstars].average.D > UserPatch.Dmax) continue;
 
-      stars[Nstars].detID   = 0;
-      stars[Nstars].imageID = 0;
-
-      stars[Nstars].X  = 0;
-      stars[Nstars].Y  = 0;
-      stars[Nstars].fx = 0;
-      stars[Nstars].fy = 0;
-      stars[Nstars].df = 0;
+      fparse (&stars[Nstars].measure.M, 3, &buffer[Nbyte]);
+      stars[Nstars].measure.dM 	      = NAN;
+      stars[Nstars].measure.photcode  = GSC_M;
+      stars[Nstars].measure.t  	      = 0;
 
       Nstars ++;
Index: /trunk/Ohana/src/addstar/src/gettycho.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gettycho.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/gettycho.c	(revision 21508)
@@ -69,54 +69,45 @@
 
       for (j = 0; j < nitems; j++) {
+
 	line = &buffer[j*NBYTE];
-	stars[Ntycho].R = atof (&line[15]);
-	stars[Ntycho].D = atof (&line[28]);
 
-	if (stars[Ntycho].R < region[0].Rmin) goto next_section;
-	if (stars[Ntycho].R > region[0].Rmax) goto next_section;
-	if (stars[Ntycho].D < region[0].Dmin) goto next_section;
-	if (stars[Ntycho].D > region[0].Dmax) goto next_section;
+	InitStar (&stars[Ntycho]);
 
-	if (stars[Ntycho].R < UserPatch.Rmin) continue;
-	if (stars[Ntycho].R > UserPatch.Rmax) continue;
-	if (stars[Ntycho].D < UserPatch.Dmin) continue;
-	if (stars[Ntycho].D > UserPatch.Dmax) continue;
+	stars[Ntycho].average.R = atof (&line[15]);
+	stars[Ntycho].average.D = atof (&line[28]);
 
-	stars[Ntycho].dR  = 1000 * atof (&line[57]);
-	stars[Ntycho].dD  = 1000 * atof (&line[64]);
+	if (stars[Ntycho].average.R < region[0].Rmin) goto next_section;
+	if (stars[Ntycho].average.R > region[0].Rmax) goto next_section;
+	if (stars[Ntycho].average.D < region[0].Dmin) goto next_section;
+	if (stars[Ntycho].average.D > region[0].Dmax) goto next_section;
+
+	if (stars[Ntycho].average.R < UserPatch.Rmin) continue;
+	if (stars[Ntycho].average.R > UserPatch.Rmax) continue;
+	if (stars[Ntycho].average.D < UserPatch.Dmin) continue;
+	if (stars[Ntycho].average.D > UserPatch.Dmax) continue;
+
+	stars[Ntycho].average.dR  = 1000 * atof (&line[57]);
+	stars[Ntycho].average.dD  = 1000 * atof (&line[64]);
 
 	/* XXX : we need to apply uR,uD to R,D to advance to 2000.0 */
-	stars[Ntycho].uR  = atof (&line[41]);
-	stars[Ntycho].uD  = atof (&line[49]);
+	stars[Ntycho].average.uR  = atof (&line[41]);
+	stars[Ntycho].average.uD  = atof (&line[49]);
 
-	stars[Ntycho].duR = atof (&line[69]);
-	stars[Ntycho].duD = atof (&line[75]);
-	
-	stars[Ntycho].P   = 0;
-	stars[Ntycho].dP  = 0;
-
-	stars[Ntycho].detID   = 0;
-	stars[Ntycho].imageID = 0;
+	stars[Ntycho].average.duR = atof (&line[69]);
+	stars[Ntycho].average.duD = atof (&line[75]);
 
 	/* Tycho uses J2000 equinox and 1991.25 epoch for coordinates */
 	/* the magnitudes have no temporal information */ 
-	stars[Ntycho].t   = TychoEpoch;
-	stars[Ntycho].found = -1;
-      
-	stars[Ntycho].X  = 0;
-	stars[Ntycho].Y  = 0;
-	stars[Ntycho].fx = 0;
-	stars[Ntycho].fy = 0;
-	stars[Ntycho].df = 0;
+	stars[Ntycho].measure.t   = TychoEpoch;
 
 	/* one pass of addstar does either r or b */
 	if (photcode == TYCHO_B) {
-	  stars[Ntycho].M     = atof (&line[110]);
-	  stars[Ntycho].dM    = atof (&line[117]);
-	  stars[Ntycho].code  = TYCHO_B;
+	  stars[Ntycho].measure.M     	 = atof (&line[110]);
+	  stars[Ntycho].measure.dM    	 = atof (&line[117]);
+	  stars[Ntycho].measure.photcode = TYCHO_B;
 	} else {
-	  stars[Ntycho].M     = atof (&line[123]);
-	  stars[Ntycho].dM    = atof (&line[130]);
-	  stars[Ntycho].code  = TYCHO_V;
+	  stars[Ntycho].measure.M     	 = atof (&line[123]);
+	  stars[Ntycho].measure.dM    	 = atof (&line[130]);
+	  stars[Ntycho].measure.photcode = TYCHO_V;
 	}	
       
Index: /trunk/Ohana/src/addstar/src/getusno.c
===================================================================
--- /trunk/Ohana/src/addstar/src/getusno.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/getusno.c	(revision 21508)
@@ -110,28 +110,18 @@
       if (buf[1] > iDEC1) continue;
 
-      memset (&stars[Nusno], 0, sizeof(Stars));
-      stars[Nusno].R     = buf[0]/360000.0;
-      stars[Nusno].D     = buf[1]/360000.0 - 90.0;
-      stars[Nusno].dM    = NAN;
-      stars[Nusno].t     = 0;
-      stars[Nusno].found = -1;
+      InitStar (&stars[Nusno]);
 
-      stars[Nusno].detID   = 0;
-      stars[Nusno].imageID = 0;
-
-      stars[Nusno].X  = 0;
-      stars[Nusno].Y  = 0;
-      stars[Nusno].fx = 0;
-      stars[Nusno].fy = 0;
-      stars[Nusno].df = 0;
+      stars[Nusno].average.R     = buf[0]/360000.0;
+      stars[Nusno].average.D     = buf[1]/360000.0 - 90.0;
+      stars[Nusno].measure.dM    = NAN;
 
       /* one pass of addstar does either r or b */
       if (photcode == USNO_RED) {
-	stars[Nusno].code  = USNO_RED;
-	stars[Nusno].M     = fabs (0.1*(buf[2] - 1000*((int)(buf[2]/1000))));
+	stars[Nusno].measure.photcode = USNO_RED;
+	stars[Nusno].measure.M        = fabs (0.1*(buf[2] - 1000*((int)(buf[2]/1000))));
       } 
       if (photcode == USNO_BLUE) {	
-	stars[Nusno].code  = USNO_BLUE;
-	stars[Nusno].M     = fabs (0.1*((int)(buf[2] - 1000000*((int)(buf[2]/1000000))) / 1000));
+	stars[Nusno].measure.photcode = USNO_BLUE;
+	stars[Nusno].measure.M        = fabs (0.1*((int)(buf[2] - 1000000*((int)(buf[2]/1000000))) / 1000));
       }
       Nusno ++;
Index: /trunk/Ohana/src/addstar/src/getusnob.c
===================================================================
--- /trunk/Ohana/src/addstar/src/getusnob.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/getusnob.c	(revision 21508)
@@ -125,37 +125,28 @@
       /* USNO coords are reported for J2000 / epoch 2000.0 */
       /* extract the basic stellar data */
-      memset (&stars[Nusno], 0, sizeof(Stars));
-      stars[Nusno].R   = buf[0]/360000.0;
-      stars[Nusno].D   = buf[1]/360000.0 - 90.0;
+      InitStar (&stars[Nusno]);
+
+      stars[Nusno].average.R   = buf[0]/360000.0;
+      stars[Nusno].average.D   = buf[1]/360000.0 - 90.0;
 
       /* XXX uR cos(D) or just uR ??? */
-      stars[Nusno].uR  = 2.0 * ((buf[2]       % 10000) - 5000);
-      stars[Nusno].uD  = 2.0 * ((buf[2]/10000 % 10000) - 5000);
+      stars[Nusno].average.uR  = 2.0 * ((buf[2]       % 10000) - 5000);
+      stars[Nusno].average.uD  = 2.0 * ((buf[2]/10000 % 10000) - 5000);
 
-      stars[Nusno].duR = (buf[3]      % 1000);
-      stars[Nusno].duD = (buf[3]/1000 % 1000);
+      stars[Nusno].average.duR = (buf[3]      % 1000);
+      stars[Nusno].average.duD = (buf[3]/1000 % 1000);
 
-      stars[Nusno].dR  = 0.001 * (buf[4]      % 1000);
-      stars[Nusno].dD  = 0.001 * (buf[4]/1000 % 1000);
+      stars[Nusno].average.dR  = 0.001 * (buf[4]      % 1000);
+      stars[Nusno].average.dD  = 0.001 * (buf[4]/1000 % 1000);
 
-      stars[Nusno].P   = 0;
-      stars[Nusno].dP  = 0;
+      stars[Nusno].average.P   = 0;
+      stars[Nusno].average.dP  = 0;
 
       /* USNO magnitude errors are reported as a fixed 0.3 mag */
-      stars[Nusno].dM    = 0.3;
-      stars[Nusno].found = -1;
-      
-      stars[Nusno].detID   = 0;
-      stars[Nusno].imageID = 0;
-
-      stars[Nusno].X  = 0;
-      stars[Nusno].Y  = 0;
-      stars[Nusno].fx = 0;
-      stars[Nusno].fy = 0;
-      stars[Nusno].df = 0;
+      stars[Nusno].measure.dM  = 0.3;
 
       /* USNO-B uses J2000 equinox and 2000.0 epoch for coordinates */
       /* the magnitudes have no temporal information */ 
-      stars[Nusno].t     = USNOepoch;
+      stars[Nusno].measure.t   = USNOepoch;
 
       /* one pass of addstar does r, b, or n */
@@ -172,5 +163,5 @@
 	  m2 = 0;
       }
-      stars[Nusno].code = photcode;
+      stars[Nusno].measure.photcode = photcode;
 
       /* if two mags are available, get an average */
@@ -180,5 +171,5 @@
 	mag = (m1) ? m1 : m2;
       }
-      stars[Nusno].M = (mag == 0.0) ? 32.0 : mag;
+      stars[Nusno].measure.M = (mag == 0.0) ? 32.0 : mag;
 
       Nusno ++;
Index: /trunk/Ohana/src/addstar/src/greference.c
===================================================================
--- /trunk/Ohana/src/addstar/src/greference.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/greference.c	(revision 21508)
@@ -3,5 +3,5 @@
 # define LOAD_DR2    1
 
-Stars *grefcat (char *Refcat, SkyRegion *region, int photcode, int *nstars) {
+Stars *greference (char *Refcat, SkyRegion *region, int photcode, int *nstars) {
 
   int Nstars;
@@ -36,15 +36,21 @@
   /* get stars from 2MASS for the given region */
   if (!strcasecmp (Refcat, "2MASS")) {
-    stars = get2mass (region, photcode, LOAD_ALLSKY, &Nstars);
+    // stars = get2mass (region, photcode, LOAD_ALLSKY, &Nstars);
+    fprintf (stderr, "2MASS load via addstar is deprecated: use load2mass\n");
+    exit (2);
   }
   
   /* get stars from 2MASS for the given region */
   if (!strcasecmp (Refcat, "2MASS-ALLSKY")) {
-    stars = get2mass (region, photcode, LOAD_ALLSKY, &Nstars);
+    // stars = get2mass (region, photcode, LOAD_ALLSKY, &Nstars);
+    fprintf (stderr, "2MASS load via addstar is deprecated: use load2mass\n");
+    exit (2);
   }
   
   /* get stars from 2MASS for the given region */
   if (!strcasecmp (Refcat, "2MASS-DR2")) {
-    stars = get2mass (region, photcode, LOAD_DR2, &Nstars);
+    // stars = get2mass (region, photcode, LOAD_DR2, &Nstars);
+    fprintf (stderr, "2MASS load via addstar is deprecated: use load2mass\n");
+    exit (2);
   }
   
Index: /trunk/Ohana/src/addstar/src/grefstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/grefstars.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/grefstars.c	(revision 21508)
@@ -21,20 +21,15 @@
     if (line[0] == 0) continue;
     if (line[0] == '#') continue;
-    dparse (&stars[N].R,  1, line);
-    dparse (&stars[N].D,  2, line);
-    dparse (&stars[N].M,  3, line);
-    dparse (&stars[N].dM, 4, line);
-    stars[N].R = ohana_normalize_angle (stars[N].R);
-    stars[N].t = 0;
-    stars[N].code = photcode;
-    stars[N].found = FALSE;
-    stars[N].detID   = 0;
-    stars[N].imageID = 0;
 
-    stars[N].X  = 0;
-    stars[N].Y  = 0;
-    stars[N].fx = 0;
-    stars[N].fy = 0;
-    stars[N].df = 0;
+    InitStar (&stars[N]);
+
+    dparse (&stars[N].average.R,  1, line);
+    dparse (&stars[N].average.D,  2, line);
+    stars[N].average.R = ohana_normalize_angle (stars[N].average.R);
+
+    fparse (&stars[N].measure.M,  3, line);
+    fparse (&stars[N].measure.dM, 4, line);
+
+    stars[N].measure.photcode = photcode;
 
     CHECK_REALLOCATE (stars, Stars, NSTARS, N+1, 100);
@@ -43,7 +38,2 @@
   return (stars);
 }
-
-/* stars.found is set here to FALSE.
-   find_match_refstars uses stars.found to identify the seq number of the
-   star which is found.  it requires an initial value of -1 XXX
-*/
Index: /trunk/Ohana/src/addstar/src/load2mass_as_rawdata.c
===================================================================
--- /trunk/Ohana/src/addstar/src/load2mass_as_rawdata.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/load2mass_as_rawdata.c	(revision 21508)
@@ -140,12 +140,15 @@
 	ALLOCATE (stars[Nstars+2], Stars, 1);
 
-	stars[Nstars+0][0].R = tstars[j].R;
-	stars[Nstars+0][0].D = tstars[j].D;
-	stars[Nstars+1][0].R = tstars[j].R;
-	stars[Nstars+1][0].D = tstars[j].D;
-	stars[Nstars+2][0].R = tstars[j].R;
-	stars[Nstars+2][0].D = tstars[j].D;
+	InitStar (stars[Nstars+0]);
+	InitStar (stars[Nstars+1]);
+	InitStar (stars[Nstars+2]);
+
+	stars[Nstars+0][0].average.R = tstars[j].R;
+	stars[Nstars+0][0].average.D = tstars[j].D;
+	stars[Nstars+1][0].average.R = tstars[j].R;
+	stars[Nstars+1][0].average.D = tstars[j].D;
+	stars[Nstars+2][0].average.R = tstars[j].R;
+	stars[Nstars+2][0].average.D = tstars[j].D;
 	get2mass_3star_full (&stars[Nstars], &buffer[offset], Nbyte - offset);
-	// get2mass_star (&stars[Nstars], &buffer[offset], Nbyte - offset);
 
 	tstars[j].flag = TRUE;
Index: /trunk/Ohana/src/addstar/src/load2mass_catalog.c
===================================================================
--- /trunk/Ohana/src/addstar/src/load2mass_catalog.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/load2mass_catalog.c	(revision 21508)
@@ -20,6 +20,6 @@
     // XXX for now, the output objects will have limited astrometric interpretation...
     // XXX every 3 stars represents 3 measurements and 1 average
-    catalog[0].average[Nave].R     = stars[i].R;
-    catalog[0].average[Nave].D     = stars[i].D;
+    catalog[0].average[Nave].R     = stars[i].average.R;
+    catalog[0].average[Nave].D     = stars[i].average.D;
     catalog[0].average[Nave].dR    = 0;
     catalog[0].average[Nave].dD    = 0;
@@ -37,36 +37,25 @@
     catalog[0].average[Nave].measureOffset = Nmeas;
     catalog[0].average[Nave].missingOffset = -1;
-    catalog[0].average[Nave].code          = 0;
+    catalog[0].average[Nave].flags         = 0;
 
     for (j = 0; j < Nsec; j++) {
-      catalog[0].secfilt[Nave*Nsec+j].M  = NAN;
-      catalog[0].secfilt[Nave*Nsec+j].dM = NAN;
-      catalog[0].secfilt[Nave*Nsec+j].Xm = NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsec+j].M     = NAN;
+      catalog[0].secfilt[Nave*Nsec+j].dM    = NAN;
+      catalog[0].secfilt[Nave*Nsec+j].Xm    = NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsec+j].M_20  = NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsec+j].M_80  = NAN_S_SHORT;
+      catalog[0].secfilt[Nave*Nsec+j].Ncode = 0;
+      catalog[0].secfilt[Nave*Nsec+j].Nused = 0;
     }
 
     // we now have the min chisq row. use this to supply the other filter values....
     for (j = 0; j < 3; j++) {
+      catalog[0].measure[Nmeas]           = stars[i+j].measure;
+
       catalog[0].measure[Nmeas].dR        = 0.0;
       catalog[0].measure[Nmeas].dD        = 0.0;
-      catalog[0].measure[Nmeas].Xccd      = stars[i+j].X;
-      catalog[0].measure[Nmeas].Yccd      = stars[i+j].Y;
-      catalog[0].measure[Nmeas].dXccd     = 0.0;
-      catalog[0].measure[Nmeas].dYccd     = 0.0;
-      catalog[0].measure[Nmeas].M         = stars[i+j].M;
-      catalog[0].measure[Nmeas].dM        = stars[i+j].dM;
-      catalog[0].measure[Nmeas].Mcal      = 0;
-      catalog[0].measure[Nmeas].dMcal     = stars[i+j].dMcal;
-      catalog[0].measure[Nmeas].t         = stars[i+j].t;
-      catalog[0].measure[Nmeas].averef    = Nave;
-      catalog[0].measure[Nmeas].photcode  = stars[i+j].code;
-      catalog[0].measure[Nmeas].dophot    = 0;
-      catalog[0].measure[Nmeas].photFlags = stars[i+j].flags;
-      catalog[0].measure[Nmeas].dbFlags   = 0;
-      catalog[0].measure[Nmeas].dt        = 0xffff;
-					  
-      catalog[0].measure[Nmeas].airmass   = 0;
-      catalog[0].measure[Nmeas].FWx       = stars[i+j].fx;
-      catalog[0].measure[Nmeas].FWy       = stars[i+j].fy;
-      catalog[0].measure[Nmeas].theta     = stars[i+j].df;
+      catalog[0].measure[Nmeas].dt        = NAN_S_SHORT;
+
+      // XXX what about averef?
 
       catalog[0].average[Nave].Nmeasure++;
Index: /trunk/Ohana/src/addstar/src/replace_match.c
===================================================================
--- /trunk/Ohana/src/addstar/src/replace_match.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/replace_match.c	(revision 21508)
@@ -7,9 +7,9 @@
   /* search for entry and replace values M, dM, R, D */
   for (i = 0; i < average[0].Nmeasure; i++) {
-    if (measure[i].photcode != star[0].code) continue;
-    measure[i].dR = 3600.0*(average[0].R - star[0].R);
-    measure[i].dD = 3600.0*(average[0].D - star[0].D);
-    measure[i].M  = star[0].M;
-    measure[i].dM = star[0].dM;
+    if (measure[i].photcode != star[0].measure.photcode) continue;
+    measure[i].dR = 3600.0*(average[0].R - star[0].average.R);
+    measure[i].dD = 3600.0*(average[0].D - star[0].average.D);
+    measure[i].M  = star[0].measure.M;
+    measure[i].dM = star[0].measure.dM;
     star[0].found = average[0].measureOffset + i;
     return (TRUE);
Index: /trunk/Ohana/src/addstar/src/sky_tessalation.c
===================================================================
--- /trunk/Ohana/src/addstar/src/sky_tessalation.c	(revision 21507)
+++ /trunk/Ohana/src/addstar/src/sky_tessalation.c	(revision 21508)
@@ -281,5 +281,5 @@
   image[0].NY = NY;
 
-  image[0].code = 1; // this needs to be set more sensibly
+  image[0].photcode = 1; // this needs to be set more sensibly
 
   image[0].Mx   = xv[0];  image[0].My   = yv[0];
@@ -433,5 +433,5 @@
   rectangle[0].NX = NX;
   rectangle[0].NY = NY;
-  rectangle[0].code = 1; // this needs to be set more sensibly
+  rectangle[0].photcode = 1; // this needs to be set more sensibly
 
   return (TRUE);
@@ -477,5 +477,5 @@
   rectangle[0].NX = NX;
   rectangle[0].NY = NY;
-  rectangle[0].code = 1; // this needs to be set more sensibly
+  rectangle[0].photcode = 1; // this needs to be set more sensibly
 
   return (TRUE);
@@ -511,5 +511,5 @@
       output[N].NX = NX;
       output[N].NY = NY;
-      output[N].code = input[0].code;
+      output[N].photcode = input[0].photcode;
 
       output[N].coords.crpix1 = input[0].coords.crpix1 - i*NX;
Index: /trunk/Ohana/src/getstar/src/ReadImageFiles.c
===================================================================
--- /trunk/Ohana/src/getstar/src/ReadImageFiles.c	(revision 21507)
+++ /trunk/Ohana/src/getstar/src/ReadImageFiles.c	(revision 21508)
@@ -64,5 +64,5 @@
       /* find image rootname */
       name = filebasename (file[i]);
-      snprintf (image[N].name, DVO_IMAGE_NAME_LEN, name);
+      snprintf (image[N].name, DVO_IMAGE_NAME_LEN, "%s", name);
       free (name);
 
Index: /trunk/Ohana/src/getstar/src/ReadImageHeader.c
===================================================================
--- /trunk/Ohana/src/getstar/src/ReadImageHeader.c	(revision 21507)
+++ /trunk/Ohana/src/getstar/src/ReadImageHeader.c	(revision 21508)
@@ -112,5 +112,5 @@
   image[0].Mcal = 0.0;
   image[0].Xm   = NAN_S_SHORT;
-  image[0].code = 0;
+  image[0].flags = 0;
 
   return (TRUE);
Index: /trunk/Ohana/src/getstar/src/write_getstar_ps1_dev_0.c
===================================================================
--- /trunk/Ohana/src/getstar/src/write_getstar_ps1_dev_0.c	(revision 21507)
+++ /trunk/Ohana/src/getstar/src/write_getstar_ps1_dev_0.c	(revision 21508)
@@ -46,5 +46,5 @@
     output[i].D        = average[i].D;
 
-    output[i].code     = average[i].code;
+    output[i].code     = average[i].flags;
     output[i].photcode = code_c0;
 
Index: /trunk/Ohana/src/getstar/src/write_getstar_ps1_dev_1.c
===================================================================
--- /trunk/Ohana/src/getstar/src/write_getstar_ps1_dev_1.c	(revision 21507)
+++ /trunk/Ohana/src/getstar/src/write_getstar_ps1_dev_1.c	(revision 21508)
@@ -49,5 +49,5 @@
     output[i].P        = average[i].P;
 
-    output[i].code     = average[i].code;
+    output[i].code     = average[i].flags;
     output[i].photcode = code_c0;
 
Index: /trunk/Ohana/src/getstar/src/write_getstar_ps1_dev_2.c
===================================================================
--- /trunk/Ohana/src/getstar/src/write_getstar_ps1_dev_2.c	(revision 21507)
+++ /trunk/Ohana/src/getstar/src/write_getstar_ps1_dev_2.c	(revision 21508)
@@ -55,5 +55,5 @@
     output[i].dP       = average[i].dP;
 
-    output[i].code     = average[i].code;
+    output[i].code     = average[i].flags;
     output[i].photcode = code_c0;
 
Index: /trunk/Ohana/src/imregister/imphot/modify.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/modify.c	(revision 21507)
+++ /trunk/Ohana/src/imregister/imphot/modify.c	(revision 21508)
@@ -13,14 +13,14 @@
     if (options.modify) {
       if (!strcasecmp (options.ModifyEntry, "and")) {
-	image[i].code &= atoi (options.ModifyValue);
+	image[i].flags &= atoi (options.ModifyValue);
       }
       if (!strcasecmp (options.ModifyEntry, "or")) {
-	image[i].code |= atoi (options.ModifyValue);
+	image[i].flags |= atoi (options.ModifyValue);
       }
       if (!strcasecmp (options.ModifyEntry, "xor")) {
-	image[i].code ^= atoi (options.ModifyValue);
+	image[i].flags ^= atoi (options.ModifyValue);
       }
       if (!strcasecmp (options.ModifyEntry, "=")) {
-	image[i].code = atoi (options.ModifyValue);
+	image[i].flags = atoi (options.ModifyValue);
       }
     }
Index: /trunk/Ohana/src/imregister/imphot/output.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/output.c	(revision 21507)
+++ /trunk/Ohana/src/imregister/imphot/output.c	(revision 21508)
@@ -39,5 +39,5 @@
       
     fprintf (stdout, "%s %s %s  %7.4f %7.4f  %7.4f %5d %02x\n", image[i].name, photstr, timestr, 
-	     image[i].Mcal, image[i].dMcal, image[i].secz, image[i].nstar, image[i].code); 
+	     image[i].Mcal, image[i].dMcal, image[i].secz, image[i].nstar, image[i].flags); 
     free (timestr);
   }
Index: /trunk/Ohana/src/imregister/imphot/subset.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/subset.c	(revision 21507)
+++ /trunk/Ohana/src/imregister/imphot/subset.c	(revision 21508)
@@ -24,5 +24,5 @@
     if (!status && criteria.Ntimes) continue;
     if (criteria.PhotcodeSelect && (image[i].photcode != criteria.photcode)) continue;
-    if (criteria.CodeSelect     && (image[i].code != criteria.Code)) continue;
+    if (criteria.CodeSelect     && (image[i].flags != criteria.Code)) continue;
     if (criteria.NameSelect     && strncasecmp (image[i].name, criteria.Name, NameSelectLength)) continue;
 
Index: /trunk/Ohana/src/libautocode/Makefile.Targets
===================================================================
--- /trunk/Ohana/src/libautocode/Makefile.Targets	(revision 21507)
+++ /trunk/Ohana/src/libautocode/Makefile.Targets	(revision 21508)
@@ -1,3 +1,2 @@
-
 AOBJS = \
 $(ASRC)/coords.$(ARCH).o \
@@ -9,4 +8,5 @@
 $(ASRC)/average-ps1-dev-1.$(ARCH).o \
 $(ASRC)/average-ps1-dev-2.$(ARCH).o \
+$(ASRC)/average-ps1-v1.$(ARCH).o \
 $(ASRC)/secfilt.$(ARCH).o \
 $(ASRC)/secfilt-loneos.$(ARCH).o \
@@ -16,4 +16,5 @@
 $(ASRC)/secfilt-ps1-dev-1.$(ARCH).o \
 $(ASRC)/secfilt-ps1-dev-2.$(ARCH).o \
+$(ASRC)/secfilt-ps1-v1.$(ARCH).o \
 $(ASRC)/measure.$(ARCH).o \
 $(ASRC)/measure-loneos.$(ARCH).o \
@@ -23,4 +24,5 @@
 $(ASRC)/measure-ps1-dev-1.$(ARCH).o \
 $(ASRC)/measure-ps1-dev-2.$(ARCH).o \
+$(ASRC)/measure-ps1-v1.$(ARCH).o \
 $(ASRC)/missing.$(ARCH).o \
 $(ASRC)/photcode.$(ARCH).o \
@@ -29,4 +31,5 @@
 $(ASRC)/photcode-ps1-dev-2.$(ARCH).o \
 $(ASRC)/photcode-ps1-dev-3.$(ARCH).o \
+$(ASRC)/photcode-ps1-v1.$(ARCH).o \
 $(ASRC)/image.$(ARCH).o \
 $(ASRC)/image-loneos.$(ARCH).o \
@@ -37,11 +40,13 @@
 $(ASRC)/image-ps1-dev-2.$(ARCH).o \
 $(ASRC)/image-ps1-dev-3.$(ARCH).o \
+$(ASRC)/image-ps1-v1.$(ARCH).o \
 $(ASRC)/regimage.$(ARCH).o \
 $(ASRC)/detreg.$(ARCH).o \
 $(ASRC)/photreg.$(ARCH).o \
 $(ASRC)/photreg-old.$(ARCH).o \
-$(ASRC)/ps1_dev_0.$(ARCH).o \
-$(ASRC)/ps1_dev_1.$(ARCH).o \
-$(ASRC)/smpdata.$(ARCH).o \
+$(ASRC)/cmf-ps1-dev-0.$(ARCH).o \
+$(ASRC)/cmf-ps1-dev-1.$(ARCH).o \
+$(ASRC)/cmf-ps1-v1.$(ARCH).o \
+$(ASRC)/cmf-smpdata.$(ARCH).o \
 $(ASRC)/getstar-ps1-dev-0.$(ARCH).o \
 $(ASRC)/getstar-ps1-dev-1.$(ARCH).o \
@@ -49,5 +54,4 @@
 $(ASRC)/spectrum.$(ARCH).o \
 $(ASRC)/spectrum-ascii.$(ARCH).o \
-$(ASRC)/Stars.$(ARCH).o \
 $(ASRC)/GSCRegion.$(ARCH).o \
 $(ASRC)/AddstarClientOptions.$(ARCH).o \
@@ -63,4 +67,5 @@
 $(AINC)/average-ps1-dev-1.h \
 $(AINC)/average-ps1-dev-2.h \
+$(AINC)/average-ps1-v1.h \
 $(AINC)/secfilt.h \
 $(AINC)/secfilt-loneos.h \
@@ -70,4 +75,5 @@
 $(AINC)/secfilt-ps1-dev-1.h \
 $(AINC)/secfilt-ps1-dev-2.h \
+$(AINC)/secfilt-ps1-v1.h \
 $(AINC)/measure.h \
 $(AINC)/measure-loneos.h \
@@ -77,4 +83,5 @@
 $(AINC)/measure-ps1-dev-1.h \
 $(AINC)/measure-ps1-dev-2.h \
+$(AINC)/measure-ps1-v1.h \
 $(AINC)/missing.h \
 $(AINC)/photcode.h \
@@ -83,4 +90,5 @@
 $(AINC)/photcode-ps1-dev-2.h \
 $(AINC)/photcode-ps1-dev-3.h \
+$(AINC)/photcode-ps1-v1.h \
 $(AINC)/image.h \
 $(AINC)/image-loneos.h \
@@ -91,18 +99,23 @@
 $(AINC)/image-ps1-dev-2.h \
 $(AINC)/image-ps1-dev-3.h \
+$(AINC)/image-ps1-v1.h \
 $(AINC)/regimage.h \
 $(AINC)/detreg.h \
 $(AINC)/photreg.h \
 $(AINC)/photreg-old.h \
-$(AINC)/ps1_dev_0.h \
-$(AINC)/ps1_dev_1.h \
+$(AINC)/cmf-ps1-dev-0.h \
+$(AINC)/cmf-ps1-dev-1.h \
+$(AINC)/cmf-ps1-v1.h \
+$(AINC)/cmf-smpdata.h \
 $(AINC)/getstar-ps1-dev-0.h \
 $(AINC)/getstar-ps1-dev-1.h \
 $(AINC)/getstar-ps1-dev-2.h \
-$(AINC)/smpdata.h \
 $(AINC)/spectrum.h \
 $(AINC)/spectrum-ascii.h \
-$(AINC)/Stars.h \
 $(AINC)/GSCRegion.h \
 $(AINC)/AddstarClientOptions.h \
 $(AINC)/SkyRegion.h
+
+# $(ASRC)/Stars.$(ARCH).o 
+# $(AINC)/Stars.h 
+
Index: /trunk/Ohana/src/libautocode/def/Stars.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/Stars.d	(revision 21507)
+++ /trunk/Ohana/src/libautocode/def/Stars.d	(revision 21508)
@@ -2,10 +2,12 @@
 EXTNAME STARS
 TYPE    BINTABLE
-SIZE    248
+SIZE    278
 
-FIELD     X,                X,          double,    x coordinate on image,	     pixels
-FIELD     Y,                Y,          double,    y coordinate on image,	     pixels
-FIELD     dX,               dX,         double,    x coordinate error,	             pixels
-FIELD     dY,               dY,         double,    y coordinate error,  	     pixels
+* FIELD     Xccd,             X,          double,    x coordinate on image,	     pixels
+* FIELD     Yccd,             Y,          double,    y coordinate on image,	     pixels
+* FIELD     dXccd,            dX,         double,    x coordinate error,	             pixels
+* FIELD     dYccd,            dY,         double,    y coordinate error,  	     pixels
+* FIELD     posangle,         POSANGLE,   float
+* FIELD     pltscale,         PLTSCALE,   float
 FIELD     R,                R,          double,    ra coordinate on sky,	     decimal degrees
 FIELD     D,                D,          double,    dec coordinate on sky,	     decimal degrees
@@ -18,34 +20,37 @@
 FIELD     P,                PAR,        double,    parallax,			     milliarcsec
 FIELD     dP,               PAR_ERR,    double,    parallax error,		     milliarcsec
-FIELD     M,                M,          double,    instrumental mag
-FIELD     dM,               DM,         double,    error on mag
-FIELD     dMcal,            DMCAL,      double,    systematic error on mag
-FIELD     sky,              SKY,        double,    local sky counts
-FIELD     dsky,             dSKY,       double,    local sky error counts
-FIELD     fx,               FX,         double,    object FWHM x-dir,		     pixels?
-FIELD     fy,               FY,         double,    object FWHM y-dir,		     pixels?
-FIELD     df,               DF,         double,    object position angle,	     degrees
-# FIELD     Mgal,             MGAL,       double,    alternative (galaxy) magnitude
-FIELD     Map,              MAP,        double,    alternative (aperture) magnitude
+
+* FIELD     M,                M,          double,    instrumental mag
+* FIELD     dM,               DM,         double,    error on mag
+* FIELD     dMcal,            DMCAL,      double,    systematic error on mag
+* FIELD     Sky,              SKY,        double,    local sky counts
+* FIELD     dSky,             dSKY,       double,    local sky error counts
+* FIELD     fx,               FX,         double,    object FWHM x-dir,		     pixels?
+* FIELD     fy,               FY,         double,    object FWHM y-dir,		     pixels?
+* FIELD     df,               DF,         double,    object position angle,	     degrees
+* FIELD     Mcal,             MCAL,       float,     image cal magnitude
+* FIELD     Map,              MAP,        double,    alternative (aperture) magnitude
 FIELD     Mpeak,            MPEAK,      double,    alternative (peak) magnitude
-FIELD     detID,            ID,         int,       detection identifier
-FIELD     imageID,          IMAGE_ID,   int,       image identifier
+* FIELD     detID,            ID,         int,       detection identifier
+* FIELD     imageID,          IMAGE_ID,   int,       image identifier
 FIELD     found,            FOUND,      int,       found in database catalog?
-FIELD     t,                T,          e_time,    date/time of exposure (UNIX)
-FIELD     dt,               EXPTIME,    float,     exposure time,                    2.5*log(exptime)
-FIELD     psfChisq,         PSF_CHISQ,  float
-FIELD     crNsigma,         CR_NSIGMA,  float
-FIELD     extNsigma,        EXT_NSIGMA, float
-FIELD     psfQual,          PSF_QUAL,   float
-FIELD     Mcal,             MCAL,       float,     image cal mag,	             mag
-FIELD     airmass,          AIRMASS,    float,     (airmass - 1),		     airmass
-FIELD     az,     	    AZ,         float,     azimuth
-FIELD     code,             CODE,       short
-FIELD     nFrames,          N_FRAMES,   short
-FIELD     flags,            FLAGS,      short
-FIELD     dophot,           DOPHOT,     char,      dophot type code
-FIELD     dummy,            DUMMY,      char
-
-# XXX I'm going to need azimuth (or load from image header?)
+* FIELD     t,                T,          e_time,    date/time of exposure (UNIX)
+* FIELD     t_msec,           T_MSEC,     short,     milliseconds of exposure
+* FIELD     dt,               EXPTIME,    float,     exposure time,                    2.5*log(exptime)
+* FIELD     psfQual,          PSF_QUAL,   float
+* FIELD     psfChisq,         PSF_CHISQ,  float
+* FIELD     psfNdof,          PSF_NDOF,   int
+* FIELD     psfNpix,          PSF_NPIX,   int
+* FIELD     crNsigma,         CR_NSIGMA,  float
+* FIELD     extNsigma,        EXT_NSIGMA, float
+* FIELD     Mxx,              MOMENTS_XX, float,     second moment
+* FIELD     Mxy,              MOMENTS_XY, float,     second moment
+* FIELD     Myy,              MOMENTS_YY, float,     second moment
+* FIELD     airmass,          AIRMASS,    float,     (airmass - 1),		     airmass
+* FIELD     az,     	    AZ,         float,     azimuth
+* FIELD     photcode,         CODE,       short
+* FIELD     nFrames,          N_FRAMES,   short
+* FIELD     photFlags,        FLAGS,      short
+FIELD     dummy,            DUMMY,      char[2]
 
 # double:   24 * 8 : 192
@@ -58,2 +63,5 @@
 # define down the types to floats where reasonable (all but R,D)?
 # R,D should be in ICRS, J2000, epoch 2000 : precess as needed, apply p-m as needed
+
+# XXX I'd like to merge this with Measure, but need to be careful about addstarClient -> addstarServer
+# XXX extend photFlags to match actual psphot output
Index: /trunk/Ohana/src/libautocode/def/average-ps1-v1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/average-ps1-v1.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/average-ps1-v1.d	(revision 21508)
@@ -0,0 +1,40 @@
+STRUCT       Average_PS1_V1
+EXTNAME      DVO_AVERAGE_PS1_V1
+TYPE         BINTABLE
+SIZE         96
+DESCRIPTION  DVO Average Object Table
+
+# elements of data structure / FITS table
+
+FIELD R,              RA,          double,          RA,                	       	  decimal degrees 
+FIELD D,              DEC,         double,          DEC,               	       	  decimal degrees 
+FIELD dR,             RA_ERR,      float,           RA error                      arcsec
+FIELD dD,             DEC_ERR,     float,           DEC error                     arcsec
+
+FIELD uR,             U_RA,        float,           RA*cos(D) proper-motion,      arcsec/year
+FIELD uD,             U_DEC,       float,           DEC proper-motion,            arcsec/year
+FIELD duR,            V_RA_ERR,    float,           RA*cos(D) p-m error,          arcsec/year
+FIELD duD,            V_DEC_ERR,   float,           DEC p-m error,                arcsec/year
+FIELD P,              PAR,         float,           parallax,			  arcsec
+FIELD dP,             PAR_ERR,     float,           parallax error,               arcsec
+
+FIELD Xp,             SIGMA_POS,   float, 	    position scatter,   	  1/100 arcsec
+FIELD ChiSq,          CHISQ_POS,   float, 	    astrometry analysis chisq
+FIELD Npos,           NUMBER_POS,  unsigned short,  number of detections used for astrometry
+
+# this limits us to a max of 64k measurements per object
+FIELD Nmeasure,       NMEASURE,    unsigned short,  number of psf measurements
+FIELD Nmissing,       NMISSING,    unsigned short,  number of missings
+FIELD Nextend,        NEXTEND,     unsigned short,  number of extended measurements
+
+FIELD measureOffset,  OFF_MEASURE, uint32_t,   	    offset to first psf measurement
+FIELD missingOffset,  OFF_MISSING, uint32_t,   	    offset to first missing obs
+FIELD extendOffset,   OFF_EXTEND,  uint32_t,   	    offset to first extended measurement
+
+# 'flags' was called 'code' prior to 2009.02.07
+FIELD flags,          FLAGS,       uint32_t,        average object flags (star; ghost; etc)
+
+# objID + catID gives a unique ID for all objects in the database
+FIELD objID,          OBJ_ID,      unsigned int,    unique ID for object in table
+FIELD catID,          CAT_ID,      unsigned int,    unique ID for table in which object was first realized
+FIELD extID,          EXT_ID,      uint64_t,        external ID for object (eg PSPS objID)
Index: /trunk/Ohana/src/libautocode/def/average.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/average.d	(revision 21507)
+++ /trunk/Ohana/src/libautocode/def/average.d	(revision 21508)
@@ -2,43 +2,41 @@
 EXTNAME      DVO_AVERAGE
 TYPE         BINTABLE
-SIZE         80
+SIZE         94
 DESCRIPTION  DVO Average Object Table
 
 # elements of data structure / FITS table
 
-FIELD R,              RA,         double,           RA,                	       	  decimal degrees 
-FIELD D,              DEC,        double,           DEC,               	       	  decimal degrees 
-FIELD dR,             RA_ERR,     float,            RA error                      arcsec
-FIELD dD,             DEC_ERR,    float,            DEC error                     arcsec
+FIELD R,              RA,          double,          RA,                           decimal degrees 
+FIELD D,              DEC,         double,          DEC,                          decimal degrees 
+FIELD dR,             RA_ERR,      float,           RA error                      arcsec
+FIELD dD,             DEC_ERR,     float,           DEC error                     arcsec
+				   
+FIELD uR,             U_RA,        float,           RA*cos(D) proper-motion,      arcsec/year
+FIELD uD,             U_DEC,       float,           DEC proper-motion,            arcsec/year
+FIELD duR,            V_RA_ERR,    float,           RA*cos(D) p-m error,          arcsec/year
+FIELD duD,            V_DEC_ERR,   float,           DEC p-m error,                arcsec/year
+FIELD P,              PAR,         float,           parallax,                     arcsec
+FIELD dP,             PAR_ERR,     float,           parallax error,               arcsec
 
-FIELD uR,             U_RA,       float,            RA*cos(D) proper-motion,      arcsec/year
-FIELD uD,             U_DEC,      float,            DEC proper-motion,            arcsec/year
-FIELD duR,            V_RA_ERR,   float,            RA*cos(D) p-m error,          arcsec/year
-FIELD duD,            V_DEC_ERR,  float,            DEC p-m error,                arcsec/year
-FIELD P,              PAR,        float,            parallax,			  arcsec
-FIELD dP,             PAR_ERR,    float,            parallax error,               arcsec
+FIELD Xp,             SIGMA_POS,   short,           position scatter,             1/100 arcsec
+FIELD ChiSq,          CHISQ_POS,   float,           astrometry analysis chisq
+FIELD Npos,           NUMBER_POS,  unsigned short,  number of detections used for astrometry
 
-FIELD Xp,             SIGMA_POS,   short, 	    position scatter,   	  1/100 arcsec
+# this limits us to a max of 64k measurements per object
 FIELD Nmeasure,       NMEASURE,    unsigned short,  number of psf measurements
 FIELD Nmissing,       NMISSING,    unsigned short,  number of missings
 FIELD Nextend,        NEXTEND,     unsigned short,  number of extended measurements
-FIELD measureOffset,  OFF_MEASURE, int,     	    offset to first psf measurement
-FIELD missingOffset,  OFF_MISSING, int,     	    offset to first missing obs
-FIELD extendOffset,   OFF_EXTEND,  int,     	    offset to first extended measurement
 
-FIELD code,           code,       unsigned short,   ID code (star; ghost; etc)
-FIELD dummy,          DUMMY,      char[2],          padding
+FIELD measureOffset,  OFF_MEASURE, int,             offset to first psf measurement
+FIELD missingOffset,  OFF_MISSING, int,             offset to first missing obs
+FIELD extendOffset,   OFF_EXTEND,  int,             offset to first extended measurement
 
-# Pan-STARRS uses a 64-bit detection ID.  keep this in two 32 bit ints
-# for C89 compatibility.  The objID is constructed based on the
-# position of first instatiation.  this is actually quite expensive
-# because we need to include the uniqueness test to construct this,
-# which requires a select for each new object.  Therefore, I will use
-# a table based ID (table ID + object ID), and we will have to
-# re-number the object IDs if we change the table density, OR treat
-# all subdivisions as entries which are from a foreign table.
+# 'flags' was called 'code' prior to 2009.02.07
+FIELD flags,          FLAGS,       uint32_t,        average object flags (star; ghost; etc)
 
-FIELD objID,          OBJ_ID,    unsigned int,   unique ID for object in table
-FIELD catID,          CAT_ID,    unsigned int,   unique ID for table in which object was first realized
+# objID + catID gives a unique ID for all objects in the database
+FIELD objID,          OBJ_ID,      unsigned int,    unique ID for object in table
+FIELD catID,          CAT_ID,      unsigned int,    unique ID for table in which object was first realized
+FIELD extID,          EXT_ID,      uint64_t,        external ID for object (eg PSPS objID)
 
 # this structure should only be used for internal representations
@@ -47,6 +45,3 @@
 # the index for the secfilt table is just Nsecfilt times the index for the average table.
 
-# the DVO object IDs are generated internally and are not equivalent to the PSPS object IDs
-# probably need to add position chisq
-
-# XXX include the number of measurements used to determine the positional information?
+# *** 20090206 : new fields : ChiSq, Npos, flags (was code, uint16_t), extID
Index: /trunk/Ohana/src/libautocode/def/cmf-ps1-dev-0.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/cmf-ps1-dev-0.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/cmf-ps1-dev-0.d	(revision 21508)
@@ -0,0 +1,24 @@
+# name of structure type
+STRUCT  PS1_DEV_0
+EXTNAME PS1_DEV_0
+TYPE    BINTABLE
+SIZE    64
+
+# elements of data structure / FITS table
+FIELD detID,    IPP_IDET,      	  unsigned int, detection ID
+FIELD X,      	X_PSF,    	  float,    x coord,              pixels
+FIELD Y,      	Y_PSF,    	  float,    y coord,              pixels
+FIELD dX,      	X_PSF_SIG,    	  float,    x coord error,        pixels
+FIELD dY,      	Y_PSF_SIG,    	  float,    y coord error,        pixels
+FIELD M,      	PSF_INST_MAG,     float,    inst mags,            mags
+FIELD dM,     	PSF_INST_MAG_SIG, float,    inst mag error,       mags
+FIELD Mpeak,    PEAK_FLUX_AS_MAG, float,    inst mag error,       mags
+FIELD sky,    	SKY,              float,    sky flux,             cnts/sec
+FIELD dSky,    	SKY_SIG,          float,    sky flux errorf       cnts/sec
+FIELD psfChisq, PSF_CHISQ,        float,    psf fit chisq
+FIELD fx,     	PSF_WIDTH_X,      float,    semi-major,           pixels
+FIELD fy,     	PSF_WIDTH_Y,      float,    semi-minor,           pixels
+FIELD df,     	PSF_THETA,        float,    ellipse angle,        degrees
+FIELD psfQual, 	PSF_QF,           float,    quality factor
+FIELD nFrames, 	N_FRAMES,         short,    images overlapping peak
+FIELD dummy,  	DUMMY,            short,    padding
Index: /trunk/Ohana/src/libautocode/def/cmf-ps1-dev-1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/cmf-ps1-dev-1.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/cmf-ps1-dev-1.d	(revision 21508)
@@ -0,0 +1,26 @@
+# name of structure type
+STRUCT  PS1_DEV_1
+EXTNAME PS1_DEV_1
+TYPE    BINTABLE
+SIZE    72
+
+# elements of data structure / FITS table
+FIELD detID,     IPP_IDET,     	   unsigned int, detection ID
+FIELD X,      	 X_PSF,    	   float,    x coord,              pixels
+FIELD Y,      	 Y_PSF,    	   float,    y coord,              pixels
+FIELD dX,      	 X_PSF_SIG,    	   float,    x coord error,        pixels
+FIELD dY,      	 Y_PSF_SIG,    	   float,    y coord error,        pixels
+FIELD M,      	 PSF_INST_MAG,     float,    inst mags,            mags
+FIELD dM,     	 PSF_INST_MAG_SIG, float,    inst mag error,       mags
+FIELD Mpeak,     PEAK_FLUX_AS_MAG, float,    inst mag error,       mags
+FIELD sky,    	 SKY,              float,    sky flux,             cnts/sec
+FIELD dSky,    	 SKY_SIG,          float,    sky flux errorf       cnts/sec
+FIELD psfChisq,  PSF_CHISQ,        float,    psf fit chisq
+FIELD crNsigma,  CR_NSIGMA,        float,    Nsigma deviations from PSF to CF
+FIELD extNsigma, EXT_NSIGMA,       float,    Nsigma deviations from PSF to EXT
+FIELD fx,     	 PSF_WIDTH_X,      float,    semi-major,           pixels
+FIELD fy,     	 PSF_WIDTH_Y,      float,    semi-minor,           pixels
+FIELD df,     	 PSF_THETA,        float,    ellipse angle,        degrees
+FIELD psfQual, 	 PSF_QF,           float,    quality factor
+FIELD nFrames, 	 N_FRAMES,         short,    images overlapping peak
+FIELD flags,  	 FLAGS,            short,    padding
Index: /trunk/Ohana/src/libautocode/def/cmf-ps1-v1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/cmf-ps1-v1.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/cmf-ps1-v1.d	(revision 21508)
@@ -0,0 +1,78 @@
+# name of structure type
+STRUCT  CMF_PS1_V1
+EXTNAME CMF_PS1_V1
+TYPE    BINTABLE
+SIZE    128
+
+# elements of data structure / FITS table
+FIELD detID,     IPP_IDET,     	   unsigned int, detection ID
+FIELD X,      	 X_PSF,    	   float,    x coord,               pixels
+FIELD Y,      	 Y_PSF,    	   float,    y coord,               pixels
+FIELD dX,      	 X_PSF_SIG,    	   float,    x coord error,         pixels
+FIELD dY,      	 Y_PSF_SIG,    	   float,    y coord error,         pixels
+FIELD RA,      	 RA_PSF,    	   float,    PSF RA coord,          degrees
+FIELD DEC,     	 DEC_PSF,    	   float,    PSF DEC coord,         degrees
+FIELD posangle,  POSANGLE,    	   float,    Posangle at source,    degrees
+FIELD pltscale,  PLTSCALE,    	   float,    Plate Scale at source, arcsec/pixel
+FIELD M,      	 PSF_INST_MAG,     float,    inst mags,             mags
+FIELD dM,     	 PSF_INST_MAG_SIG, float,    inst mag error,        mags
+FIELD Map,       AP_MAG_STANDARD,  float,    standard aperture mag, mags
+FIELD apRadius,  AP_MAG_RADIUS,    float,    radius used for fit,   pixels
+FIELD Mpeak,     PEAK_FLUX_AS_MAG, float,    peak flux as a mag,    mags
+FIELD Mcalib,    CAL_PSF_MAG,      float,    calibrated psf mag,    mags
+FIELD dMcal,     CAL_PSF_MAG_SIG,  float,    zero point scatter,    mags
+FIELD sky,    	 SKY,              float,    sky flux,              cnts/sec
+FIELD dSky,    	 SKY_SIG,          float,    sky flux error,        cnts/sec
+FIELD psfChisq,  PSF_CHISQ,        float,    psf fit chisq
+FIELD crNsigma,  CR_NSIGMA,        float,    Nsigma deviations from PSF to CF
+FIELD extNsigma, EXT_NSIGMA,       float,    Nsigma deviations from PSF to EXT
+FIELD fx,     	 PSF_MAJOR,        float,    psf fit major axis,    pixels
+FIELD fy,     	 PSF_MINOR,        float,    psf fit minor axis,    pixels
+FIELD df,     	 PSF_THETA,        float,    ellipse angle,         degrees
+FIELD psfQual, 	 PSF_QF,           float,    quality factor
+FIELD psfNdof, 	 PSF_NDOF,         int,      psf degrees of freedom
+FIELD psfNpix, 	 PSF_NPIX,         int,      psf number of pixels
+FIELD Mxx,     	 MOMENTS_XX,       float,    second moment X,       pixels^2
+FIELD Mxy,     	 MOMENTS_XY,       float,    second moment Y,       pixels^2
+FIELD Myy,     	 MOMENTS_YY,       float,    second moment XY,      pixels^2
+FIELD flags,  	 FLAGS,            int,      analysis flags
+FIELD nFrames, 	 N_FRAMES,         short,    images overlapping peak
+FIELD padding,   PADDING,	   short,    padding for 8byte records
+
+# for an object in an image, we have three triplets that tell us about the shape:
+# second moments: Mxx, Mxy, Myy 
+# model shape parameters: F_major, F_minor, F_theta
+# centroid errors: sigma_X, sigma_Y, sigma_XY
+
+# IPP_IDET         1J  IPP_IDET,     	   unsigned i
+# X_PSF       	 1E  X_PSF,    	   float,        
+# Y_PSF       	 1E  Y_PSF,    	   float,        
+# X_PSF_SIG   	 1E  X_PSF_SIG,    	   float,        
+# Y_PSF_SIG   	 1E  Y_PSF_SIG,    	   float,        
+# RA_PSF      	 1E  RA_PSF,    	   float,        
+# DEC_PSF     	 1E  DEC_PSF,    	   float,        
+# POSANGLE    	 1E  POSANGLE,    	   float,        
+# PLTSCALE    	 1E  PLTSCALE,    	   float,        
+# PSF_INST_MAG	 1E  PSF_INST_MAG,     float,          
+# PSF_INST_MAG_SIG 1E  PSF_INST_MAG_SIG, float,        
+# AP_MAG_STANDARD  1E  AP_MAG_STANDARD,  float,        
+# AP_MAG_RADIUS    1E      
+# PEAK_FLUX_AS_MAG 1E  PEAK_FLUX_AS_MAG, float,        
+# CAL_PSF_MAG      1E  CAL_PSF_MAG,      float,        
+# CAL_PSF_MAG_SIG  1E  CAL_PSF_MAG_SIG,  float,        
+# SKY              1E  SKY,              float,        
+# SKY_SIGMA        1E  SKY_SIG,          float,        
+# PSF_CHISQ        1E  PSF_CHISQ,        float,        
+# CR_NSIGMA        1E  CR_NSIGMA,        float,        
+# EXT_NSIGMA       1E  EXT_NSIGMA,       float,        
+# PSF_MAJOR        1E  PSF_MAJOR,        float,        
+# PSF_MINOR        1E  PSF_MINOR,        float,        
+# PSF_THETA        1E  PSF_THETA,        float,        
+# PSF_QF           1E  PSF_QF,           float,        
+# PSF_NDOF         1J  PSF_NDOF,         int,          
+# PSF_NPIX         1J  PSF_NPIX,         int,          
+# MOMENTS_XX       1E  MOMENTS_XX,       float,        
+# MOMENTS_XY       1E  MOMENTS_XY,       float,        
+# MOMENTS_YY       1E  MOMENTS_YY,       float,        
+# N_FRAMES         1I  N_FRAMES,         short,        
+# FLAGS            1J  FLAGS,            int,          
Index: /trunk/Ohana/src/libautocode/def/cmf-smpdata.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/cmf-smpdata.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/cmf-smpdata.d	(revision 21508)
@@ -0,0 +1,19 @@
+# name of structure type
+STRUCT  SMPData
+EXTNAME SMPFILE
+TYPE    BINTABLE
+SIZE    44
+
+# elements of data structure / FITS table
+FIELD X,      X_PIX,      float,    x coord,              pixels
+FIELD Y,      Y_PIX,      float,    y coord,              pixels
+FIELD M,      MAG_RAW,    float,    inst mags,            mags
+FIELD dM,     MAG_ERR,    float,    inst mag error,       mags
+FIELD Mgal,   MAG_GAL,    float,    galaxy mag,           mags
+FIELD Map,    MAG_AP,     float,    aperture mag,         mags
+FIELD sky,    LOG_SKY,    float,    log-10 of sky,        cnts/sec
+FIELD fx,     FWHM_X,     float,    semi-major,           pixels
+FIELD fy,     FWHM_Y,     float,    semi-minor,           pixels
+FIELD df,     THETA,      float,    ellipse angle,        degrees
+FIELD dophot, DOPHOT,     char,     dophot type,          none
+FIELD dummy,  DUMMY,      char[3],  padding,              none
Index: /trunk/Ohana/src/libautocode/def/getstar-ps1-v1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/getstar-ps1-v1.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/getstar-ps1-v1.d	(revision 21508)
@@ -0,0 +1,17 @@
+STRUCT       Getstar_PS1_V1
+EXTNAME      GETSTAR_PS1_V1
+TYPE         BINTABLE
+SIZE         32
+DESCRIPTION  Getstar output file
+
+# elements of data structure / FITS table
+
+# average R,D are epoch & equinox J2000.0
+FIELD R,              RA,         double,           RA,                	       	  decimal degrees 
+FIELD D,              DEC,        double,           DEC,               	       	  decimal degrees 
+FIELD mag,            MAG,        float,            average magnitude in requested photcode
+FIELD c1,             MAG_C1,     float,            average magnitude in color term 1
+FIELD c2,             MAG_C2,     float,            average magnitude in color term 2
+
+FIELD photcode,       PHOTCODE,   unsigned short,   photcode for this mag
+FIELD code,           CODE,       unsigned short,   ID code (star; ghost; etc)
Index: /trunk/Ohana/src/libautocode/def/image-ps1-v1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/image-ps1-v1.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/image-ps1-v1.d	(revision 21508)
@@ -0,0 +1,72 @@
+STRUCT       Image_PS1_V1
+EXTNAME      DVO_IMAGE_PS1_V1
+TYPE         BINTABLE
+SIZE         344
+DESCRIPTION  DVO Image Table 
+
+# elements of the image structure
+
+SUBSTRUCT coords,           COORDS,               Coords,        astrometric data
+SUBFIELD  crval1,           CRVAL1,               double,   	 coordinate at reference pixel
+SUBFIELD  crval2,           CRVAL2,               double,  	 coordinate at reference pixel
+SUBFIELD  crpix1,           CRPIX1,               float,   	 coordinate of reference pixel
+SUBFIELD  crpix2,           CRPIX2,               float,   	 coordinate of reference pixel
+SUBFIELD  cdelt1,           CDELT1,               float,   	 degrees per pixel
+SUBFIELD  cdelt2,           CDELT2,               float,    	 degrees per pixel
+SUBFIELD  pc1_1,            PC1_1,                float,    	 rotation matrix
+SUBFIELD  pc1_2,            PC1_2,                float,    	 rotation matrix
+SUBFIELD  pc2_1,            PC2_1,                float,    	 rotation matrix
+SUBFIELD  pc2_2,            PC2_2,                float,    	 rotation matrix
+SUBFIELD  polyterms,        POLYTERMS,            float[7][2],	 higher order warping terms
+SUBFIELD  ctype,            CTYPE,                char[15],      coordinate type
+SUBFIELD  Npolyterms,       NPOLYTERMS,           char,     	 order of polynomial
+# 120 bytes
+
+FIELD 	  tzero,            TZERO,                e_time,         readout time (row 0)
+FIELD 	  nstar,            NSTAR,                unsigned int,   number of stars on image
+FIELD 	  secz,             SECZ,                 float,      	  airmass,                   mag
+FIELD 	  NX,               NX,                   unsigned short, image width
+FIELD 	  NY,               NY,                   unsigned short, image height
+FIELD 	  apmifit,          APMIFIT,              float,      	  aperture correction,       mag
+FIELD 	  dapmifit,         DAPMIFIT,             float,      	  apmifit error,             mag
+FIELD 	  Mcal,             MCAL,                 float,      	  calibration mag,           mag
+FIELD 	  dMcal,            DMCAL,                float,      	  error on Mcal,             mag
+FIELD 	  Xm,               XM,                   short,      	  image chisq,               10*log(value)
+FIELD 	  photcode,         PHOTCODE,             short,      	  identifier for CCD,
+FIELD 	  exptime,          EXPTIME,              float,          exposure time,             seconds
+FIELD     sidtime,          ST,			  float,          sidereal time of exposure
+FIELD     latitude,         LAT,		  float,          observatory latitude,      degrees
+# 40 bytes
+
+# should we define the max length of name as a macro?
+FIELD 	  name,             NAME,                 char[121],      name of original image 
+FIELD 	  detection_limit,  DETECTION_LIMIT,      unsigned char,  detection limit,           10*mag
+FIELD 	  saturation_limit, SATURATION_LIMIT,     unsigned char,  saturation limit,          10*mag
+FIELD 	  cerror,           CERROR,               unsigned char,  astrometric error,         50*arcsec
+FIELD 	  fwhm_x,           FWHM_X,               unsigned char,  PSF x width,               25*arcsec
+FIELD 	  fwhm_y,           FWHM_Y,               unsigned char,  PSF y width,               25*arcsec
+FIELD 	  trate,            TRATE,                unsigned char,  scan rate,                 100 usec/pixel
+FIELD 	  ccdnum,           CCDNUM,               unsigned char,  CCD ID number
+FIELD 	  flags,            FLAGS,                unsigned int,   image quality flags
+FIELD 	  imageID,          IMAGE_ID,             unsigned int,   internal image ID
+FIELD 	  parentID,         PARENT_ID,            unsigned int,   associated ref image
+FIELD 	  externID,         EXTERN_ID,            unsigned int,   external image ID
+FIELD 	  sourceID,         SOURCE_ID,            unsigned short, analysis source ID
+# 48 bytes 
+
+FIELD 	  order,            ORDER,                short,      	  Mrel 2D polynomical order 
+FIELD 	  Mx,               MX,                   short,      	  Mrel polyterm
+FIELD 	  My,               MY,                   short,      	  Mrel polyterm
+FIELD 	  Mxx,              MXX,                  short,      	  Mrel polyterm
+FIELD 	  Mxy,              MXY,                  short,      	  Mrel polyterm
+FIELD 	  Myy,              MYY,                  short,      	  Mrel polyterm
+FIELD 	  Mxxx,             MXXX,                 short,      	  Mrel polyterm
+FIELD 	  Mxxy,             MXXY,                 short,      	  Mrel polyterm
+FIELD 	  Mxyy,             MXYY,                 short,      	  Mrel polyterm
+FIELD 	  Myyy,             MYYY,                 short,      	  Mrel polyterm
+FIELD 	  Mxxxx,            MXXXX,                short,      	  Mrel polyterm
+FIELD 	  Mxxxy,            MXXXY,                short,      	  Mrel polyterm
+FIELD 	  Mxxyy,            MXXYY,                short,      	  Mrel polyterm
+FIELD 	  Mxyyy,            MXYYY,                short,      	  Mrel polyterm
+FIELD 	  Myyyy,            MYYYY,                short,      	  Mrel polyterm
+# 40 bytes
Index: /trunk/Ohana/src/libautocode/def/image.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/image.d	(revision 21507)
+++ /trunk/Ohana/src/libautocode/def/image.d	(revision 21508)
@@ -40,5 +40,5 @@
 # 40 bytes
 
-FIELD 	  name,             NAME,                 char[128],      name of original image 
+FIELD 	  name,             NAME,                 char[121],      name of original image 
 FIELD 	  detection_limit,  DETECTION_LIMIT,      unsigned char,  detection limit,           10*mag
 FIELD 	  saturation_limit, SATURATION_LIMIT,     unsigned char,  saturation limit,          10*mag
@@ -47,10 +47,8 @@
 FIELD 	  fwhm_y,           FWHM_Y,               unsigned char,  PSF y width,               25*arcsec
 FIELD 	  trate,            TRATE,                unsigned char,  scan rate,                 100 usec/pixel
-FIELD 	  code,             CODE,                 char,           image quality flag
 FIELD 	  ccdnum,           CCDNUM,               unsigned char,  CCD ID number
+FIELD 	  flags,            FLAGS,                unsigned int,   image quality flags
 FIELD 	  imageID,          IMAGE_ID,             unsigned int,   internal image ID
-
-# XXX do we want to use this to handle mosaics?
-# FIELD 	  parentID,         PARENT_ID,            unsigned int,   associated ref image
+FIELD 	  parentID,         PARENT_ID,            unsigned int,   associated ref image
 FIELD 	  externID,         EXTERN_ID,            unsigned int,   external image ID
 FIELD 	  sourceID,         SOURCE_ID,            unsigned short, analysis source ID
@@ -73,2 +71,4 @@
 FIELD 	  Myyyy,            MYYYY,                short,      	  Mrel polyterm
 # 40 bytes
+
+# *** 20090206 : new fields : parentID, flags (was code char), changed name to 121 bytes.
Index: /trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-0.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-0.d	(revision 21507)
+++ /trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-0.d	(revision 21508)
@@ -46,5 +46,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD qPSF,           PSF_QF,       short,          psf coverage/quality factor
+FIELD psfQual,        PSF_QF,       short,          psf coverage/quality factor
 
 FIELD dophot,         DOPHOT,       char,           dophot type
Index: /trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-1.d	(revision 21507)
+++ /trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-1.d	(revision 21508)
@@ -46,5 +46,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD qPSF,           PSF_QF,       short,          psf coverage/quality factor
+FIELD psfQual,        PSF_QF,       short,          psf coverage/quality factor
 
 FIELD dophot,         DOPHOT,       char,           dophot type
Index: /trunk/Ohana/src/libautocode/def/measure-ps1-dev-1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/measure-ps1-dev-1.d	(revision 21507)
+++ /trunk/Ohana/src/libautocode/def/measure-ps1-dev-1.d	(revision 21508)
@@ -33,5 +33,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD qPSF,           PSF_QF,       float,          psf coverage/quality factor
+FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
 FIELD psfChisq,       PSF_CHISQ,    float,          psf coverage/quality factor
 FIELD crNsigma,       CR_NSIGMA,    float,          psf coverage/quality factor
Index: /trunk/Ohana/src/libautocode/def/measure-ps1-dev-2.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/measure-ps1-dev-2.d	(revision 21507)
+++ /trunk/Ohana/src/libautocode/def/measure-ps1-dev-2.d	(revision 21508)
@@ -34,5 +34,5 @@
 
 # do we need more resolution than a short? should this be a log?
-FIELD qPSF,           PSF_QF,       float,          psf coverage/quality factor
+FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
 FIELD psfChisq,       PSF_CHISQ,    float,          psf fit chisq
 FIELD crNsigma,       CR_NSIGMA,    float,          Nsigma deviation towards CR
Index: /trunk/Ohana/src/libautocode/def/measure-ps1-v1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/measure-ps1-v1.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/measure-ps1-v1.d	(revision 21508)
@@ -0,0 +1,81 @@
+STRUCT       Measure_PS1_V1
+EXTNAME      DVO_MEASURE_PS1_V1
+TYPE         BINTABLE
+SIZE         152
+DESCRIPTION  DVO Detection Measurement Table 
+
+FIELD dR,             D_RA,         float,          RA offset,                	  arcsec
+FIELD dD,             D_DEC,        float,          DEC offset,               	  arcsec
+FIELD M,              MAG,          float,          catalog mag,       	       	  mag
+FIELD Mcal,           M_CAL,        float,          image cal mag,	          mag
+FIELD Map,            M_APER,       float,          aperture mag,		  mag
+FIELD dM,             MAG_ERR,      float,          mag error,                    mag
+FIELD dMcal,          MAG_CAL_ERR,  float,          systematic calibration error, mag
+FIELD dt,             M_TIME,       float,          exposure time,                2.5*log(exptime)
+
+# note that with airmass = 1.0 / cos(90 - alt), we have full alt/az representation
+FIELD airmass,        AIRMASS,      float,          (airmass - 1),		  airmass
+FIELD az,             AZ,           float,          telescope azimuth
+
+# new field elements needed for Pan-STARRS:
+FIELD Xccd,           X_CCD,        float,          X coord on chip,               pixels
+FIELD Yccd,           Y_CCD,        float,          Y coord on chip,               pixels
+
+# could these be packed into fewer bits?
+FIELD Sky,            SKY_FLUX,     float,          local estimate of sky flux,    counts/sec
+FIELD dSky,           SKY_FLUX_ERR, float,          local estimate of sky flux,    counts/sec
+
+FIELD t,              TIME,         int,   	    time in seconds (UNIX)
+FIELD averef,         AVE_REF,      unsigned int,   reference to average entry      
+
+# internally, this is an unsigned int; however, we do NOT convert with TZERO/TSCAL on output
+FIELD detID,          DET_ID,       unsigned int,   detection ID
+FIELD imageID,        IMAGE_ID,     unsigned int,   reference to DVO image ID
+FIELD objID,          OBJ_ID,       unsigned int,   unique ID for object in table
+FIELD catID,          CAT_ID,       unsigned int,   unique ID for table in which object was first realized
+
+# PSPS uses a 64-bit detection ID
+FIELD extID,          EXT_ID,       uint64_t,       external ID (eg PSPS detID)
+
+# do we need more resolution than a short? should this be a log?
+FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
+FIELD psfChisq,       PSF_CHISQ,    float,          psf fit chisq
+FIELD psfNdof,        PSF_NDOF,     int,            psf degrees of freedom
+FIELD psfNpix,        PSF_NPIX,     int,            psf number of pixels
+FIELD crNsigma,       CR_NSIGMA,    float,          Nsigma deviation towards CR
+FIELD extNsigma,      EXT_NSIGMA,   float,          Nsigma deviation towards EXT
+
+# model shape parameters
+FIELD FWx,            FWHM_MAJOR,   short,          object fwhm major axis,         1/100 of pixels
+FIELD FWy,            FWHM_MINOR,   short,          object fwhm minor axis,         1/100 of pixels 
+FIELD theta,          PSF_THETA,    short,          angle wrt ccd X dir,            (0xffff/360) deg
+
+# moments
+FIELD Mxx,            MXX,          short,          second moments in pixel coords, 1/100 of pixels
+FIELD Mxy,            MXY,          short,          second moments in pixel coords, 1/100 of pixels
+FIELD Myy,            MYY,          short,          second moments in pixel coords, 1/100 of pixels
+
+# fractional exposure time
+FIELD t_msec,         TIME_MSEC,    unsigned short, time fraction of second,       milliseconds
+FIELD photcode,       PHOTCODE,     unsigned short, photcode
+
+# position errors
+FIELD dXccd,          X_CCD_ERR,    short,          X coord error on chip,          1/100 of pixels
+FIELD dYccd,          Y_CCD_ERR,    short,          Y coord error on chip,          1/100 of pixels
+
+FIELD pad,            PAD,          char[6],        padding
+
+# local astrometry scales
+FIELD posangle,       POSANGLE,     short,          position angle sky to chip,     (0xffff/360) deg
+FIELD pltscale,       PLTSCALE,     float,          plate scale,                    arcsec/pixel
+
+FIELD dbFlags,        DB_FLAGS,     unsigned int,   flags supplied by analysis in database
+FIELD photFlags,      PHOT_FLAGS,   unsigned int,   flags supplied by photometry program
+
+
+# 19 x float
+# 6 x int or unsigned int
+# 11 x short or unsigned short
+# 3 x uint64_t
+
+# = 
Index: /trunk/Ohana/src/libautocode/def/measure.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/measure.d	(revision 21507)
+++ /trunk/Ohana/src/libautocode/def/measure.d	(revision 21508)
@@ -2,57 +2,72 @@
 EXTNAME      DVO_MEASURE
 TYPE         BINTABLE
-SIZE         112
+SIZE         146
 DESCRIPTION  DVO Detection Measurement Table 
 
-FIELD dR,             D_RA,         float,          RA offset,                	  arcsec
-FIELD dD,             D_DEC,        float,          DEC offset,               	  arcsec
-FIELD M,              MAG,          float,          catalog mag,       	       	  mag
-FIELD Mcal,           M_CAL,        float,          image cal mag,	          mag
-FIELD Map,            M_APER,       float,          aperture mag,		  mag
-FIELD dM,             MAG_ERR,      float,          mag error,                    mag
-FIELD dMcal,          MAG_CAL_ERR,  float,          systematic calibration error, mag
-FIELD dt,             M_TIME,       float,          exposure time,                2.5*log(exptime)
+FIELD dR,             D_RA,         float,          RA offset,                	    arcsec
+FIELD dD,             D_DEC,        float,          DEC offset,               	    arcsec
+FIELD M,              MAG,          float,          catalog mag,       	       	    mag
+FIELD Mcal,           M_CAL,        float,          image cal mag,	            mag
+FIELD Map,            M_APER,       float,          aperture mag,		    mag
+FIELD dM,             MAG_ERR,      float,          mag error,                      mag
+FIELD dMcal,          MAG_CAL_ERR,  float,          systematic calibration error,   mag
+FIELD dt,             M_TIME,       float,          exposure time,                  2.5*log(exptime)
 
 # note that with airmass = 1.0 / cos(90 - alt), we have full alt/az representation
-FIELD airmass,        AIRMASS,      float,          (airmass - 1),		  airmass
+FIELD airmass,        AIRMASS,      float,          (airmass - 1),		    airmass
 FIELD az,             AZ,           float,          telescope azimuth
 
 # new field elements needed for Pan-STARRS:
-FIELD Xccd,           X_CCD,        float,          X coord on chip,               pixels
-FIELD Yccd,           Y_CCD,        float,          Y coord on chip,               pixels
+FIELD Xccd,           X_CCD,        float,          X coord on chip,                pixels
+FIELD Yccd,           Y_CCD,        float,          Y coord on chip,                pixels
 
 # could these be packed into fewer bits?
-FIELD Sky,            SKY_FLUX,     float,          local estimate of sky flux,    counts/sec
-FIELD dSky,           SKY_FLUX_ERR, float,          local estimate of sky flux,    counts/sec
+FIELD Sky,            SKY_FLUX,     float,          local estimate of sky flux,     counts/sec
+FIELD dSky,           SKY_FLUX_ERR, float,          local estimate of sky flux,     counts/sec
 
-FIELD t,              TIME,         unsigned int,   time in seconds (UNIX)
+FIELD t,              TIME,         int,   	    time in seconds (UNIX)
+FIELD t_msec,         TIME_MSEC,    unsigned short, time fraction of second,        milliseconds
 FIELD averef,         AVE_REF,      unsigned int,   reference to average entry      
 
-# Pan-STARRS uses a 64-bit detection ID.  keep this in two 32 bit ints for backwards compatibility?
 FIELD detID,          DET_ID,       unsigned int,   detection ID
 FIELD imageID,        IMAGE_ID,     unsigned int,   reference to DVO image ID
+FIELD objID,          OBJ_ID,       unsigned int,   unique ID for object in table
+FIELD catID,          CAT_ID,       unsigned int,   unique ID for table in which object was first realized
+
+# PSPS uses a 64-bit detection ID
+FIELD extID,          EXT_ID,       uint64_t,       external ID (eg PSPS detID)
 
 # do we need more resolution than a short? should this be a log?
-FIELD qPSF,           PSF_QF,       float,          psf coverage/quality factor
+FIELD psfQual,        PSF_QF,       float,          psf coverage/quality factor
 FIELD psfChisq,       PSF_CHISQ,    float,          psf fit chisq
+FIELD psfNdof,        PSF_NDOF,     int,            psf degrees of freedom
+FIELD psfNpix,        PSF_NPIX,     int,            psf number of pixels
 FIELD crNsigma,       CR_NSIGMA,    float,          Nsigma deviation towards CR
 FIELD extNsigma,      EXT_NSIGMA,   float,          Nsigma deviation towards EXT
 
-FIELD FWx,            FWHM_MAJOR,   short,          object fwhm major axis,       1/100 of arcsec 
-FIELD FWy,            FWHM_MINOR,   short,          object fwhm minor axis,       1/100 of arcsec 
-FIELD theta,          PSF_THETA,    short,          angle wrt ccd X dir,          (0xffff/360) deg
+# model shape parameters
+FIELD FWx,            FWHM_MAJOR,   short,          object fwhm major axis,         1/100 of pixels 
+FIELD FWy,            FWHM_MINOR,   short,          object fwhm minor axis,         1/100 of pixels 
+FIELD theta,          PSF_THETA,    short,          angle wrt ccd X dir,            (0xffff/360) deg
+
+# moments
+FIELD Mxx,            MXX,          short,          second moments in pixel coords, 1/100 of pixels^2
+FIELD Mxy,            MXY,          short,          second moments in pixel coords, 1/100 of pixels^2
+FIELD Myy,            MYY,          short,          second moments in pixel coords, 1/100 of pixels^2
+
+# convert this to error in pixels on load?
+FIELD dXccd,          X_CCD_ERR,    short,          X coord error on chip,          1/100 of pixels
+FIELD dYccd,          Y_CCD_ERR,    short,          Y coord error on chip,          1/100 of pixels
+
+FIELD posangle,       POSANGLE,     short,          position angle sky to chip,     (0xffff/360) deg
+FIELD pltscale,       PLTSCALE,     float,          plate scale,                    arcsec/pixel
+
 FIELD photcode,       PHOTCODE,     unsigned short, photcode
+FIELD dbFlags,        DB_FLAGS,     unsigned int,   flags supplied by analysis in database
+FIELD photFlags,      PHOT_FLAGS,   unsigned int,   flags supplied by photometry program
 
-# convert this to error in arcsec on load?
-FIELD dXccd,          X_CCD_ERR,    short,          X coord error on chip,         pixels
-FIELD dYccd,          Y_CCD_ERR,    short,          Y coord error on chip,         pixels
+# *** 20090206 : new fields : t_msec, extID, Mxx, Mxy, Myy, posangle,
+#                psfNdof, psfNpix, Map_small; deprecated dophot.
+#                Changed dbFlags, photFlags : uint16_t to uint64_t
 
-FIELD dbFlags,        DB_FLAGS,     unsigned short, flags for various uses  
-FIELD photFlags,      PHOT_FLAGS,   unsigned short, flags supplied by photometry program
-
-FIELD stargal,        STAR_GAL,     char,           star-galaxy separator
-
-# absorb these into photFlags?
-FIELD dophot,         DOPHOT,       char,           dophot type
-
-FIELD dummy,          DUMMY,        char[6],        padding
+# XXX unsigned int values are probably not being saved in the FITS file correctly: no BZERO, BSCALE
Index: /trunk/Ohana/src/libautocode/def/photcode-ps1-v1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/photcode-ps1-v1.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/photcode-ps1-v1.d	(revision 21508)
@@ -0,0 +1,33 @@
+STRUCT       PhotCode_PS1_V1
+EXTNAME      DVO_PHOTCODE_PS1_V1
+TYPE         BINTABLE
+SIZE         104
+DESCRIPTION  DVO Photcode Description Table 
+
+# elements of data structure / FITS table
+FIELD  code,         	  CODE,          	 unsigned short, code number (stored in Measure.source) 
+FIELD  name,         	  NAME,          	 char[32],       name for filter combination 
+FIELD  type,         	  TYPE,          	 char,           PRI/SEC/DEP/REF 
+FIELD  dummy,        	  DUMMY,         	 char[3],        padding
+FIELD  C,            	  C_LAM,         	 short,          primary phot calibration terms (millimags) 
+FIELD  dC,           	  C_LAM_ERR,     	 short,          primary phot calibration terms (millimags) 
+FIELD  dX,           	  X_ERR,         	 short,          primary phot calibration terms (millimags) 
+FIELD  K,            	  K,             	 float,          secondary phot calibration terms (millimags) 
+FIELD  c1,           	  C1,            	 int,            color is average.M[c1] - average.M[c2] 
+FIELD  c2,           	  C2,            	 int,            color is average.M[c1] - average.M[c2] 
+FIELD  equiv,        	  EQUIV,         	 int,            this dependent filter is equivalent to equiv PRI/SEC
+FIELD  Nc,           	  NC,            	 int,            number of color terms 
+FIELD  X,            	  X,             	 float[4],       color terms $X[0]*mc + X[1]*mc^2 + X[2]*mc^3$, etc 
+FIELD  astromErrSys,      ASTROM_ERR_SYS,  	 float,          systematic astrometry error (arcsec)
+FIELD  astromErrScale,    ASTROM_ERR_SCALE,  	 float,          astrometric error scale
+FIELD  astromErrMagScale, ASTROM_ERR_MAG_SCALE,  float,          astrometric error / mag error scale
+FIELD  astromPoorMask,    ASTROM_POOR_MASK,      short,          detections matching this mask should only be used in emergencies
+FIELD  astromBadMask,     ASTROM_BAD_MASK,       short,          detections matching this mask should not be used
+FIELD  photomErrSys,   	  PHOTOM_ERR_SYS,  	 float,          systematic photometric error
+FIELD  photomPoorMask, 	  PHOTOM_POOR_MASK,  	 short,          detections matching this mask should only be used in emergencies
+FIELD  photomBadMask,  	  PHOTOM_BAD_MASK,  	 short,          detections matching this mask should not be used
+
+#   dR_total^2 =  dR_sys^2 + AS * dR_obs^2 + MS * dM_obs^2
+#   dR_sys : systematicAstrometryError
+#   AS     : astrometryErrorScale
+#   MS     : astrometryErrorMagScale
Index: unk/Ohana/src/libautocode/def/ps1_dev_0.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/ps1_dev_0.d	(revision 21507)
+++ 	(revision )
@@ -1,24 +1,0 @@
-# name of structure type
-STRUCT  PS1_DEV_0
-EXTNAME PS1_DEV_0
-TYPE    BINTABLE
-SIZE    64
-
-# elements of data structure / FITS table
-FIELD detID,    IPP_IDET,      	  unsigned int, detection ID
-FIELD X,      	X_PSF,    	  float,    x coord,              pixels
-FIELD Y,      	Y_PSF,    	  float,    y coord,              pixels
-FIELD dX,      	X_PSF_SIG,    	  float,    x coord error,        pixels
-FIELD dY,      	Y_PSF_SIG,    	  float,    y coord error,        pixels
-FIELD M,      	PSF_INST_MAG,     float,    inst mags,            mags
-FIELD dM,     	PSF_INST_MAG_SIG, float,    inst mag error,       mags
-FIELD Mpeak,    PEAK_FLUX_AS_MAG, float,    inst mag error,       mags
-FIELD sky,    	SKY,              float,    sky flux,             cnts/sec
-FIELD dSky,    	SKY_SIG,          float,    sky flux errorf       cnts/sec
-FIELD psfChisq, PSF_CHISQ,        float,    psf fit chisq
-FIELD fx,     	PSF_WIDTH_X,      float,    semi-major,           pixels
-FIELD fy,     	PSF_WIDTH_Y,      float,    semi-minor,           pixels
-FIELD df,     	PSF_THETA,        float,    ellipse angle,        degrees
-FIELD psfQual, 	PSF_QF,           float,    quality factor
-FIELD nFrames, 	N_FRAMES,         short,    images overlapping peak
-FIELD dummy,  	DUMMY,            short,    padding
Index: unk/Ohana/src/libautocode/def/ps1_dev_1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/ps1_dev_1.d	(revision 21507)
+++ 	(revision )
@@ -1,26 +1,0 @@
-# name of structure type
-STRUCT  PS1_DEV_1
-EXTNAME PS1_DEV_1
-TYPE    BINTABLE
-SIZE    72
-
-# elements of data structure / FITS table
-FIELD detID,     IPP_IDET,     	   unsigned int, detection ID
-FIELD X,      	 X_PSF,    	   float,    x coord,              pixels
-FIELD Y,      	 Y_PSF,    	   float,    y coord,              pixels
-FIELD dX,      	 X_PSF_SIG,    	   float,    x coord error,        pixels
-FIELD dY,      	 Y_PSF_SIG,    	   float,    y coord error,        pixels
-FIELD M,      	 PSF_INST_MAG,     float,    inst mags,            mags
-FIELD dM,     	 PSF_INST_MAG_SIG, float,    inst mag error,       mags
-FIELD Mpeak,     PEAK_FLUX_AS_MAG, float,    inst mag error,       mags
-FIELD sky,    	 SKY,              float,    sky flux,             cnts/sec
-FIELD dSky,    	 SKY_SIG,          float,    sky flux errorf       cnts/sec
-FIELD psfChisq,  PSF_CHISQ,        float,    psf fit chisq
-FIELD crNsigma,  CR_NSIGMA,        float,    Nsigma deviations from PSF to CF
-FIELD extNsigma, EXT_NSIGMA,       float,    Nsigma deviations from PSF to EXT
-FIELD fx,     	 PSF_WIDTH_X,      float,    semi-major,           pixels
-FIELD fy,     	 PSF_WIDTH_Y,      float,    semi-minor,           pixels
-FIELD df,     	 PSF_THETA,        float,    ellipse angle,        degrees
-FIELD psfQual, 	 PSF_QF,           float,    quality factor
-FIELD nFrames, 	 N_FRAMES,         short,    images overlapping peak
-FIELD flags,  	 FLAGS,            short,    padding
Index: /trunk/Ohana/src/libautocode/def/secfilt-ps1-v1.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/secfilt-ps1-v1.d	(revision 21508)
+++ /trunk/Ohana/src/libautocode/def/secfilt-ps1-v1.d	(revision 21508)
@@ -0,0 +1,14 @@
+STRUCT       SecFilt_PS1_V1
+EXTNAME      DVO_SECFILT_PS1_V1
+TYPE         BINTABLE
+SIZE         20
+DESCRIPTION  DVO SecFilt : Secondary Filter Data 
+
+# elements of data structure / FITS table
+FIELD  M,     MAG,      float,                average mag in this band, mags
+FIELD  dM,    MAG_ERR,  float,                error on average mag,     mags
+FIELD  Xm,    MAG_CHI,  float,                chisq on average mag,     [100*log(value)]
+FIELD  Ncode, NCODE,    short,                number of detections in band
+FIELD  Nused, NUSED,    short,                number of detections used in average
+FIELD  M_20,  MAG_20,   short,                lower 20percent mag,      millimags
+FIELD  M_80,  MAG_80,   short,                upper 20percent mag,      millimags
Index: /trunk/Ohana/src/libautocode/def/secfilt.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/secfilt.d	(revision 21507)
+++ /trunk/Ohana/src/libautocode/def/secfilt.d	(revision 21508)
@@ -2,5 +2,5 @@
 EXTNAME      DVO_SECFILT
 TYPE         BINTABLE
-SIZE         16
+SIZE         18
 DESCRIPTION  DVO SecFilt : Secondary Filter Data 
 
@@ -11,3 +11,6 @@
 FIELD  Ncode, NCODE,    short,                number of detections in band
 FIELD  Nused, NUSED,    short,                number of detections used in average
-FIELD  dummy, JUNK,     short,                place holder
+FIELD  M_20,  MAG_20,   short,                lower 20percent mag,      millimags
+FIELD  M_80,  MAG_80,   short,                upper 20percent mag,      millimags
+
+# *** 20090206 : new fields : M_20, M_80; dropped dummy
Index: unk/Ohana/src/libautocode/def/smpdata.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/smpdata.d	(revision 21507)
+++ 	(revision )
@@ -1,19 +1,0 @@
-# name of structure type
-STRUCT  SMPData
-EXTNAME SMPFILE
-TYPE    BINTABLE
-SIZE    44
-
-# elements of data structure / FITS table
-FIELD X,      X_PIX,      float,    x coord,              pixels
-FIELD Y,      Y_PIX,      float,    y coord,              pixels
-FIELD M,      MAG_RAW,    float,    inst mags,            mags
-FIELD dM,     MAG_ERR,    float,    inst mag error,       mags
-FIELD Mgal,   MAG_GAL,    float,    galaxy mag,           mags
-FIELD Map,    MAG_AP,     float,    aperture mag,         mags
-FIELD sky,    LOG_SKY,    float,    log-10 of sky,        cnts/sec
-FIELD fx,     FWHM_X,     float,    semi-major,           pixels
-FIELD fy,     FWHM_Y,     float,    semi-minor,           pixels
-FIELD df,     THETA,      float,    ellipse angle,        degrees
-FIELD dophot, DOPHOT,     char,     dophot type,          none
-FIELD dummy,  DUMMY,      char[3],  padding,              none
Index: /trunk/Ohana/src/libautocode/doc/autocode.txt
===================================================================
--- /trunk/Ohana/src/libautocode/doc/autocode.txt	(revision 21507)
+++ /trunk/Ohana/src/libautocode/doc/autocode.txt	(revision 21508)
@@ -1,2 +1,12 @@
+
+2009.02.07
+
+The autocode tables currently use type names which are ambiguous in
+terms of their actual sizes in bits.  I've added the intNN_t and
+uintNN_t types -- this requires --use-gnu99 with configure
+
+see libdvo/doc/notes.txt for instructions on adding new dvo catalog formats
+
+2007.00.00
 
 This directory contains a collection of autocoded FITS I/O routines
Index: /trunk/Ohana/src/libautocode/generate
===================================================================
--- /trunk/Ohana/src/libautocode/generate	(revision 21507)
+++ /trunk/Ohana/src/libautocode/generate	(revision 21508)
@@ -114,4 +114,5 @@
 	
 	# rawshort is a short without byteswapping
+	# rawshort is a patch for old photreg tables
 
 	$pt1 = 0;
@@ -124,8 +125,22 @@
 	if ($type eq "int")    	      { $pt1 = "J"; }
 	if ($type eq "unsigned int")  { $pt1 = "J"; }
-	if ($type eq "e_time") 	      { $pt1 = "J"; }
+
+	if ($type eq "int8_t") 	      { $pt1 = "B"; }
+	if ($type eq "uint8_t")       { $pt1 = "B"; }
+	if ($type eq "int16_t")       { $pt1 = "I"; }
+	if ($type eq "uint16_t")      { $pt1 = "I"; }
+	if ($type eq "int32_t")       { $pt1 = "J"; }
+	if ($type eq "uint32_t")      { $pt1 = "J"; }
+
+	# FITS tables do not allow for 64bit integers.  we need to
+	# write these by splitting the value into high and low 32 bit values
+	if ($type eq "int64_t")       { $pt1 = "J"; $Np = 2*$Np; }
+	if ($type eq "uint64_t")      { $pt1 = "J"; $Np = 2*$Np; }
+
 	if ($type eq "float")  	      { $pt1 = "E"; }
 	if ($type eq "double") 	      { $pt1 = "D"; }
 
+	# special 'elixir' types:
+	if ($type eq "e_time") 	      { $pt1 = "J"; }
 	if ($type eq "e_void") 	      { $pt1 = "B"; $Np = 8*$Np; }
 	# e_void is a 64 bit pointer, cast to size_t.  its value is not loaded
@@ -146,5 +161,4 @@
 	printf FILE "%-20s 1.0, 0.0);\n", "\"$unit[$i]\", ";
     }
-
 }
 
@@ -217,8 +231,22 @@
 	if ($type eq "int")    	      { $pt1 = sprintf "I%s", $length; }
 	if ($type eq "unsigned int")  { $pt1 = sprintf "I%s", $length; }
-	if ($type eq "e_time") 	      { $pt1 = sprintf "I%s", $length; }
+
+	if ($type eq "int8_t") 	      { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "uint8_t")       { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "int16_t")       { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "uint16_t")      { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "int32_t")       { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "uint32_t")      { $pt1 = sprintf "I%s", $length; }
+
+	# FITS tables do not allow for 64bit integers.  we need to
+	# write these by splitting the value into high and low 32 bit values
+	if ($type eq "int64_t")       { $pt1 = sprintf "I%s", $length; }
+	if ($type eq "uint64_t")      { $pt1 = sprintf "I%s", $length; }
+
 	if ($type eq "float")  	      { $pt1 = sprintf "F%s", $length; }
 	if ($type eq "double") 	      { $pt1 = sprintf "F%s", $length; }
 
+	# special 'elixir' types:
+	if ($type eq "e_time") 	      { $pt1 = sprintf "I%s", $length; }
 	if ($type eq "e_void") 	      { $pt1 = sprintf "I%s", $length; }
 
@@ -283,12 +311,29 @@
 	if ($type eq "unsigned char") { $N +=   $Np; next; }
 	if ($type eq "rawshort")      { $N += 2*$Np; next; }
+
 	if ($type eq "short")  	      { $T = "BYTE"; $n = 2; }
 	if ($type eq "unsigned short"){ $T = "BYTE"; $n = 2; }
 	if ($type eq "int")    	      { $T = "WORD"; $n = 4; }
 	if ($type eq "unsigned int")  { $T = "WORD"; $n = 4; }
+
+	if ($type eq "int8_t") 	      { $N +=   $Np; next; }
+	if ($type eq "uint8_t")       { $N +=   $Np; next; }
+	if ($type eq "int16_t")       { $T = "BYTE"; $n = 2; }
+	if ($type eq "uint16_t")      { $T = "BYTE"; $n = 2; }
+	if ($type eq "int32_t")       { $T = "WORD"; $n = 4; }
+	if ($type eq "uint32_t")      { $T = "WORD"; $n = 4; }
+
+	# FITS tables do not allow for 64bit integers.  we need to
+	# write these by splitting the value into high and low 32 bit values
+	if ($type eq "int64_t")       { $T = "WORD"; $n = 4; $Np = 2*$Np; }
+	if ($type eq "uint64_t")      { $T = "WORD"; $n = 4; $Np = 2*$Np; }
+
+	if ($type eq "float")  	      { $T = "WORD"; $n = 4; }
+	if ($type eq "double") 	      { $T = "DBLE"; $n = 8; }
+
+	# special 'elixir' types:
 	if ($type eq "e_time") 	      { $T = "WORD"; $n = 4; }
 	if ($type eq "e_void") 	      { $T = "DBLE"; $n = 8; }
-	if ($type eq "float")  	      { $T = "WORD"; $n = 4; }
-	if ($type eq "double") 	      { $T = "DBLE"; $n = 8; }
+
 	if (!$n) { die "unknown type $type"; }
 	for ($j = 0; $j < $Np; $j++) {
@@ -318,8 +363,23 @@
 	if ($type eq "int")    	       { $Nbytes += 4*$Np; $valid = 1; }
 	if ($type eq "unsigned int")   { $Nbytes += 4*$Np; $valid = 1; }
+
+	if ($type eq "int8_t") 	       { $Nbytes += 1*$Np; $valid = 1; }
+	if ($type eq "uint8_t")        { $Nbytes += 1*$Np; $valid = 1; }
+	if ($type eq "int16_t")        { $Nbytes += 2*$Np; $valid = 1; }
+	if ($type eq "uint16_t")       { $Nbytes += 2*$Np; $valid = 1; }
+	if ($type eq "int32_t")        { $Nbytes += 4*$Np; $valid = 1; }
+	if ($type eq "uint32_t")       { $Nbytes += 4*$Np; $valid = 1; }
+
+	# FITS tables do not allow for 64bit integers.  we need to
+	# write these by splitting the value into high and low 32 bit values
+	if ($type eq "int64_t")        { $Nbytes += 8*$Np; $valid = 1; }
+	if ($type eq "uint64_t")       { $Nbytes += 8*$Np; $valid = 1; }
+
+	if ($type eq "float")  	       { $Nbytes += 4*$Np; $valid = 1; }
+	if ($type eq "double") 	       { $Nbytes += 8*$Np; $valid = 1; }
+
+	# special 'elixir' types:
 	if ($type eq "e_time") 	       { $Nbytes += 4*$Np; $valid = 1; }
 	if ($type eq "e_void") 	       { $Nbytes += 8*$Np; $valid = 1; }
-	if ($type eq "float")  	       { $Nbytes += 4*$Np; $valid = 1; }
-	if ($type eq "double") 	       { $Nbytes += 8*$Np; $valid = 1; }
 	if (!$valid) { die "unknown type $type"; }
     }
Index: /trunk/Ohana/src/libdvo/Makefile
===================================================================
--- /trunk/Ohana/src/libdvo/Makefile	(revision 21507)
+++ /trunk/Ohana/src/libdvo/Makefile	(revision 21508)
@@ -31,5 +31,6 @@
 $(DESTINC)/ps1_dev_1_defs.h \
 $(DESTINC)/ps1_dev_2_defs.h \
-$(DESTINC)/ps1_dev_3_defs.h
+$(DESTINC)/ps1_dev_3_defs.h \
+$(DESTINC)/ps1_v1_defs.h
 
 INCS = $(DEFS) $(DESTINC)/dvo.h $(DESTINC)/autocode.h
@@ -65,4 +66,5 @@
 $(SRC)/dvo_convert_PS1_DEV_2.$(ARCH).o \
 $(SRC)/dvo_convert_PS1_DEV_3.$(ARCH).o \
+$(SRC)/dvo_convert_PS1_V1.$(ARCH).o \
 $(SRC)/skyregion_io.$(ARCH).o    \
 $(SRC)/skyregion_gsc.$(ARCH).o    \
Index: /trunk/Ohana/src/libdvo/doc/notes.txt
===================================================================
--- /trunk/Ohana/src/libdvo/doc/notes.txt	(revision 21507)
+++ /trunk/Ohana/src/libdvo/doc/notes.txt	(revision 21508)
@@ -2,9 +2,9 @@
 Adding a new dvo catalog format.  Assume the new format name is 'foo'.
 
-1) create the autocode definition files for average, measure, secfilt, image
-   these files must be a subset of the internal versions of these same
-   tables.  if you intend to add fields which don't exist in the
-   internal tables, you must update the internal tables as well.  the
-   naming convention is: average-foo.d, etc.
+1) create the autocode definition files for average, measure, secfilt,
+   image, and photcode.  these files must be a subset of the internal
+   versions of these same tables.  if you intend to add fields which
+   don't exist in the internal tables, you must update the internal
+   tables as well.  the naming convention is: average-foo.d, etc.
 
 2) add the new definition files to libautocode/Makefile.Targets (both
@@ -22,19 +22,26 @@
  (libdvo/src/dvo_catalog.c).  The name should be "FOO".
 
-6) add entry for structure size in dvo_catalog_load_raw
-   (libdvo/src/dvo_catalog_raw.c), since this is not available from
-   the header.
+6) in libdvo/src/dvo_catalog_raw.c, update the FORMAT_CASE lists to
+   include the new STRUCT name.
 
-7) add entries in ReadRawAverage, WriteRawAverage, ReadRawMeasure,
-   WriteRawMeasure, ReadRawSecFilt, WriteRawSecFilt.  Make sure to use
-   the new STRUCT names. the conversion function gfit_convert_Foo will
-   be automatically generated.
+7) create a new conversion file dvo_convert_foo.c and define the
+   internal to Foo conversions.
 
-8) add entry in WriteRawAverage, making sure to use the new STRUCT
-   name. the conversion function gfit_convert_Foo will be
-   automatically generated.  
+8) add the new format to the list of FORMAT conversion in dvo_convert.c.
 
-9) create a new conversion file dvo_convert_foo.c and define the
-   internal to Foo conversions.
+9) any changes to the internal format need to be reflected in the
+   functions in the dvo_convert_*.c files.  These files are defined so
+   that changed fields are sequential in the functions.  This should
+   make is easy to identify the new changes needed.
+
+10) add the new format to the section at the end of dvo_image.c
+
+11) add the new format to the dvo_image_raw.c
+
+12) add the photcode format conversions to libdvo/src/LoadPhotcodesFITS.c
+
+12) switch the output photcode format conversion ina libdvo/src/SavePhotcodesFITS.c
+
+13) Add the conversion functions to the header file libdvo/include/foo_defs.h
 
 * Note some esoteric format issues:  
@@ -54,2 +61,26 @@
     keywords.
 
+----
+
+20090207 : adding PS1_V1
+
+measure: 
+ new fields : t_msec, extID, Mxx, Mxy, Myy, dTccd; deprecated dophot
+ dbFlags, photFlags : uint16_t to uint64_t
+
+average:
+ new fields : ChiSq, Npos, flags (was code, uint16_t), extID
+
+secfilt:
+ new fields : M_20, M_80; dropped dummy
+
+image:
+ new fields : parentID, flags (was code char), changed name to 121 bytes.
+
+photcode:
+ new fields : NONE
+
+** parentID was added to enable easy lookups from chip->fpa for mosaic
+   astrometry.  In old databases, this conversion was done by making
+   the match via the time and photcode.  This conversion needs to be
+   done on load...
Index: /trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /trunk/Ohana/src/libdvo/include/dvo.h	(revision 21507)
+++ /trunk/Ohana/src/libdvo/include/dvo.h	(revision 21508)
@@ -18,5 +18,6 @@
 	      DVO_FORMAT_PS1_DEV_1,
 	      DVO_FORMAT_PS1_DEV_2,
-	      DVO_FORMAT_PS1_DEV_3
+	      DVO_FORMAT_PS1_DEV_3,
+	      DVO_FORMAT_PS1_V1
 } DVOTableFormat;
 
@@ -407,4 +408,5 @@
 # include "ps1_dev_2_defs.h"
 # include "ps1_dev_3_defs.h"
+# include "ps1_v1_defs.h"
 
 /*** DVO image db I/O Functions ***/
@@ -444,5 +446,4 @@
 void sort_image_subset (Image *image, int *subset, int N);
 void sort_coords_index (double *X, double *Y, int *S, int N);
-void sort_stars_ra (Stars *stars, int N);
 void sort_regions (SkyRegion *region, int N);
 
Index: /trunk/Ohana/src/libdvo/include/ps1_v1_defs.h
===================================================================
--- /trunk/Ohana/src/libdvo/include/ps1_v1_defs.h	(revision 21508)
+++ /trunk/Ohana/src/libdvo/include/ps1_v1_defs.h	(revision 21508)
@@ -0,0 +1,11 @@
+Image 		       	*Image_PS1_V1_ToInternal (Image_PS1_V1 *in, int Nvalues);
+Image_PS1_V1    	*ImageInternalTo_PS1_V1 (Image *in, int Nvalues);
+Average 	       	*Average_PS1_V1_ToInternal (Average_PS1_V1 *in, int Nvalues, SecFilt **primary);
+Average_PS1_V1          *AverageInternalTo_PS1_V1 (Average *in, int Nvalues, SecFilt *primary);
+Measure 	       	*Measure_PS1_V1_ToInternal (Measure_PS1_V1 *in, int Nvalues);
+Measure_PS1_V1          *MeasureInternalTo_PS1_V1 (Measure *in, int Nvalues);
+SecFilt 	       	*SecFilt_PS1_V1_ToInternal (SecFilt_PS1_V1 *in, int Nvalues);
+SecFilt_PS1_V1          *SecFiltInternalTo_PS1_V1 (SecFilt *in, int Nvalues);
+
+PhotCode                *PhotCode_PS1_V1_To_Internal (PhotCode_PS1_V1 *in, int Nvalues);
+PhotCode_PS1_V1         *PhotCode_Internal_To_PS1_V1 (PhotCode *in, int Nvalues);
Index: /trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 21508)
@@ -40,5 +40,11 @@
   gfits_scan (&db.theader, "EXTNAME", "%s", 1, extname);
 
-  if (!strcmp (extname, "DVO_PHOTCODE") || !strcmp (extname, "DVO_PHOTCODE_ELIXIR")) {
+  if (!strcmp (extname, "DVO_PHOTCODE")) {
+    PhotCode_Elixir *photcode_elixir = gfits_table_get_PhotCode_Elixir (&db.ftable, &Ncode, &db.swapped);
+    photcode = PhotCode_Elixir_To_Internal (photcode_elixir, Ncode);
+    free (photcode_elixir);
+  } 
+
+  if (!strcmp (extname, "DVO_PHOTCODE_ELIXIR")) {
     PhotCode_Elixir *photcode_elixir = gfits_table_get_PhotCode_Elixir (&db.ftable, &Ncode, &db.swapped);
     photcode = PhotCode_Elixir_To_Internal (photcode_elixir, Ncode);
@@ -62,4 +68,10 @@
     photcode = PhotCode_PS1_DEV_3_To_Internal (photcode_ps1_dev_3, Ncode);
     free (photcode_ps1_dev_3);
+  } 
+
+  if (!strcmp (extname, "DVO_PHOTCODE_PS1_V1")) {
+    PhotCode_PS1_V1 *photcode_ps1_v1 = gfits_table_get_PhotCode_PS1_V1 (&db.ftable, &Ncode, &db.swapped);
+    photcode = PhotCode_PS1_V1_To_Internal (photcode_ps1_v1, Ncode);
+    free (photcode_ps1_v1);
   } 
 
Index: /trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 21508)
@@ -29,9 +29,9 @@
   // for the moment, we simply support the latest photcode format for output
   // XXX update this as needed as new formats are defined
-  PhotCode_PS1_DEV_3 *photcode_output = PhotCode_Internal_To_PS1_DEV_3 (table[0].code, table[0].Ncode);
+  PhotCode_PS1_V1 *photcode_output = PhotCode_Internal_To_PS1_V1 (table[0].code, table[0].Ncode);
 
   /* convert FITS format data to internal format (byteswaps & EXTNAME) */
   gfits_db_create (&db);
-  gfits_table_set_PhotCode_PS1_DEV_3 (&db.ftable, photcode_output, table[0].Ncode);
+  gfits_table_set_PhotCode_PS1_V1 (&db.ftable, photcode_output, table[0].Ncode);
   gfits_db_save (&db);
   gfits_db_close (&db);
Index: /trunk/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 21508)
@@ -52,4 +52,5 @@
   if (!strcasecmp (catformat, "PS1_DEV_1"))       return (DVO_FORMAT_PS1_DEV_1);
   if (!strcasecmp (catformat, "PS1_DEV_2"))       return (DVO_FORMAT_PS1_DEV_2);
+  if (!strcasecmp (catformat, "PS1_V1"))          return (DVO_FORMAT_PS1_V1);
   return (DVO_FORMAT_UNDEF);
 }
Index: /trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c	(revision 21508)
@@ -86,4 +86,5 @@
       FORMAT_CASE (PS1_DEV_1, PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2, PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,    PS1_V1);
 
     default:
@@ -271,4 +272,5 @@
   if (catalog[0].catformat == DVO_FORMAT_PS1_DEV_1)       gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PS1_DEV_1");
   if (catalog[0].catformat == DVO_FORMAT_PS1_DEV_2)       gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PS1_DEV_2");
+  if (catalog[0].catformat == DVO_FORMAT_PS1_V1)          gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PS1_V1");
 
   /* rewind file pointers and truncate file */
@@ -366,4 +368,5 @@
       FORMAT_CASE (PS1_DEV_1, PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2, PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,    PS1_V1);
 
     default:
@@ -412,4 +415,5 @@
       FORMAT_CASE (PS1_DEV_1, PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2, PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,    PS1_V1);
 
     default:
@@ -462,4 +466,5 @@
       FORMAT_CASE (PS1_DEV_1, PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2, PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,    PS1_V1);
 
     default:
@@ -508,4 +513,5 @@
       FORMAT_CASE (PS1_DEV_1, PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2, PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,    PS1_V1);
 
     default:
@@ -558,4 +564,5 @@
       FORMAT_CASE (PS1_DEV_1, PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2, PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,    PS1_V1);
 
     default:
@@ -604,4 +611,5 @@
       FORMAT_CASE (PS1_DEV_1, PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2, PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,    PS1_V1);
 
     default:
Index: /trunk/Ohana/src/libdvo/src/dvo_convert.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert.c	(revision 21508)
@@ -49,4 +49,5 @@
   CONVERT_FORMAT ("DVO_AVERAGE_PS1_DEV_1",       PS1_DEV_1,       PS1_DEV_1);
   CONVERT_FORMAT ("DVO_AVERAGE_PS1_DEV_2",       PS1_DEV_2,       PS1_DEV_2);
+  CONVERT_FORMAT ("DVO_AVERAGE_PS1_V1",          PS1_V1,          PS1_V1);
 # undef CONVERT_FORMAT
 
@@ -79,4 +80,5 @@
       FORMAT_CASE (PS1_DEV_1,       PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2,       PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,          PS1_V1);
 # undef FORMAT_CASE
 
@@ -122,4 +124,5 @@
   CONVERT_FORMAT ("DVO_MEASURE_PS1_DEV_1",       PS1_DEV_1,       PS1_DEV_1);
   CONVERT_FORMAT ("DVO_MEASURE_PS1_DEV_2",       PS1_DEV_2,       PS1_DEV_2);
+  CONVERT_FORMAT ("DVO_MEASURE_PS1_V1",          PS1_V1,          PS1_V1);
 # undef CONVERT_FORMAT
 
@@ -152,4 +155,5 @@
       FORMAT_CASE (PS1_DEV_1,       PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2,       PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,          PS1_V1);
 # undef FORMAT_CASE
 
@@ -195,4 +199,5 @@
   CONVERT_FORMAT ("DVO_SECFILT_PS1_DEV_1",       PS1_DEV_1,       PS1_DEV_1);
   CONVERT_FORMAT ("DVO_SECFILT_PS1_DEV_2",       PS1_DEV_2,       PS1_DEV_2);
+  CONVERT_FORMAT ("DVO_SECFILT_PS1_V1",          PS1_V1,          PS1_V1);
 # undef CONVERT_FORMAT
 
@@ -225,4 +230,5 @@
       FORMAT_CASE (PS1_DEV_1,       PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2,       PS1_DEV_2);
+      FORMAT_CASE (PS1_V1,          PS1_V1);
 # undef FORMAT_CASE
 
@@ -288,5 +294,9 @@
   CONVERT_FORMAT ("DVO_IMAGE_PS1_DEV_1",       PS1_DEV_1,       PS1_DEV_1);
   CONVERT_FORMAT ("DVO_IMAGE_PS1_DEV_2",       PS1_DEV_2,       PS1_DEV_2);
-  CONVERT_FORMAT ("DVO_IMAGE_PS1_DEV_3",       PS1_DEV_3,       PS1_DEV_3);
+  CONVERT_FORMAT ("DVO_IMAGE_PS1_V1",          PS1_V1,          PS1_V1);
+
+  // XXX Not sure this was ever actually used -- it was not complete...
+  // CONVERT_FORMAT ("DVO_IMAGE_PS1_DEV_3",       PS1_DEV_3,       PS1_DEV_3);
+
 # undef CONVERT_FORMAT
 
@@ -322,5 +332,8 @@
       FORMAT_CASE (PS1_DEV_1,       PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2,       PS1_DEV_2);
-      FORMAT_CASE (PS1_DEV_3,       PS1_DEV_3);
+      FORMAT_CASE (PS1_V1,          PS1_V1);
+
+      // XXX not sure this was actually used: it was incomplete
+      // FORMAT_CASE (PS1_DEV_3,       PS1_DEV_3);
 # undef FORMAT_CASE
 
@@ -372,5 +385,9 @@
       FORMAT_CASE (PS1_DEV_1,       PS1_DEV_1);
       FORMAT_CASE (PS1_DEV_2,       PS1_DEV_2);
-      FORMAT_CASE (PS1_DEV_3,       PS1_DEV_3);
+      FORMAT_CASE (PS1_V1,          PS1_V1);
+
+      // XXX not sure this was used, it was incomplete
+      // FORMAT_CASE (PS1_DEV_3,       PS1_DEV_3);
+
 # undef FORMAT_CASE
 
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c	(revision 21508)
@@ -15,5 +15,4 @@
     out[i].M          = in[i].M;
     out[i].Mcal       = in[i].Mcal;
-    out[i].Map        = in[i].Mgal;
     out[i].dM         = in[i].dM;
     out[i].dt         = in[i].dt;
@@ -28,5 +27,5 @@
     out[i].detID      = in[i].detID;
     out[i].imageID    = in[i].imageID;
-    out[i].qPSF       = in[i].qPSF;
+    out[i].psfQual    = in[i].psfQual;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].crNsigma   = in[i].crNsigma;
@@ -40,6 +39,22 @@
     out[i].dbFlags    = in[i].dbFlags;
     out[i].photFlags  = in[i].photFlags;
-    out[i].stargal    = in[i].stargal;
-    out[i].dophot     = in[i].dophot;
+
+    // changed or added for PS1_DEV_2
+    out[i].Map        = in[i].Mgal;
+    out[i].dMcal      = 0; 
+
+    // changed or added for PS1_V1
+    out[i].photFlags  = in[i].photFlags | (in[i].dophot << 16);
+    out[i].t_msec     = 0;
+    out[i].extID      = 0;
+    out[i].objID      = 0;
+    out[i].catID      = 0;
+    out[i].Mxx 	      = 0.0;
+    out[i].Mxy 	      = 0.0;
+    out[i].Myy        = 0.0;
+    out[i].posangle   = 0;
+    out[i].pltscale   = 0;
+    out[i].psfNdof    = 0;
+    out[i].psfNpix    = 0;
   }
   return (out);
@@ -58,5 +73,4 @@
     out[i].M          = in[i].M;
     out[i].Mcal       = in[i].Mcal;
-    out[i].Mgal       = in[i].Map;
     out[i].dM         = in[i].dM;
     out[i].dt         = in[i].dt;
@@ -71,5 +85,5 @@
     out[i].detID      = in[i].detID;
     out[i].imageID    = in[i].imageID;
-    out[i].qPSF       = in[i].qPSF;
+    out[i].psfQual    = in[i].psfQual;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].crNsigma   = in[i].crNsigma;
@@ -82,7 +96,11 @@
     out[i].dYccd      = in[i].dYccd;
     out[i].dbFlags    = in[i].dbFlags;
-    out[i].photFlags  = in[i].photFlags;
-    out[i].stargal    = in[i].stargal;
-    out[i].dophot     = in[i].dophot;
+
+    // changed or added for PS1_DEV_2
+    out[i].Mgal       = in[i].Map;
+
+    // changed or added for PS1_V1
+    out[i].photFlags  = in[i].photFlags & 0x0000ffff;
+    out[i].dophot     = in[i].photFlags >> 16;
   }
   return (out);
@@ -101,9 +119,4 @@
     out[i].D        	 = in[i].D;      
     out[i].Xp       	 = in[i].Xp;     
-    out[i].Nmeasure      = in[i].Nm;     
-    out[i].Nmissing      = in[i].Nn;     
-    out[i].code     	 = in[i].code;   
-    out[i].measureOffset = in[i].offset; 
-    out[i].missingOffset = in[i].missing;
     out[i].dR       	 = in[i].dR;
     out[i].dD       	 = in[i].dD;
@@ -116,4 +129,18 @@
     out[i].objID 	 = in[i].objID;
     out[i].catID 	 = in[i].catID;
+
+    // changed or added for PS1_DEV_2
+    out[i].Nmeasure      = in[i].Nm;     
+    out[i].Nmissing      = in[i].Nn;     
+    out[i].measureOffset = in[i].offset; 
+    out[i].missingOffset = in[i].missing;
+    out[i].Nextend       = 0;
+    out[i].extendOffset  = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags    	 = in[i].code;   
+    out[i].ChiSq       	 = 0.0;
+    out[i].Npos       	 = 0.0;
+    out[i].extID 	 = 0;
   }
   return (out);
@@ -129,22 +156,26 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].R        = in[i].R;      
-    out[i].D        = in[i].D;      
-    out[i].Xp       = in[i].Xp;     
-    out[i].Nm       = in[i].Nmeasure;     
-    out[i].Nn       = in[i].Nmissing;     
-    out[i].code     = in[i].code;   
-    out[i].offset   = in[i].measureOffset; 
-    out[i].missing  = in[i].missingOffset;
-    out[i].dR       = in[i].dR;
-    out[i].dD       = in[i].dD;
-    out[i].uR       = in[i].uR;
-    out[i].uD       = in[i].uD;
-    out[i].duR      = in[i].duR;
-    out[i].duD      = in[i].duD;
-    out[i].P        = in[i].P;
-    out[i].dP       = in[i].dP;
-    out[i].objID    = in[i].objID;
-    out[i].catID    = in[i].catID;
+    out[i].R        	 = in[i].R;      
+    out[i].D        	 = in[i].D;      
+    out[i].Xp       	 = in[i].Xp;     
+    out[i].dR       	 = in[i].dR;
+    out[i].dD       	 = in[i].dD;
+    out[i].uR       	 = in[i].uR;
+    out[i].uD       	 = in[i].uD;
+    out[i].duR      	 = in[i].duR;
+    out[i].duD      	 = in[i].duD;
+    out[i].P        	 = in[i].P;
+    out[i].dP       	 = in[i].dP;
+    out[i].objID    	 = in[i].objID;
+    out[i].catID    	 = in[i].catID;
+
+    // changed or added for PS1_DEV_2
+    out[i].Nm       	 = in[i].Nmeasure;     
+    out[i].Nn       	 = in[i].Nmissing;     
+    out[i].offset   	 = in[i].measureOffset; 
+    out[i].missing  	 = in[i].missingOffset;
+
+    // changed or added for PS1_V1
+    out[i].code          = in[i].flags;   
   }
   return (out);
@@ -164,4 +195,8 @@
     out[i].Ncode = in[i].Ncode;
     out[i].Nused = in[i].Nused;
+
+    // changed or added for PS1_V1
+    out[i].M_20  = 0;      
+    out[i].M_80  = 0;      
  }
   return (out);
@@ -218,5 +253,4 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
     out[i].imageID	    = in[i].imageID;
@@ -237,4 +271,12 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    // changed or added for PS1_DEV_2
+    out[i].externID	    = 0;
+    out[i].sourceID	    = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags	    = in[i].code;
+    out[i].parentID	    = 0;
   }
   return (out);
@@ -275,5 +317,4 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
     out[i].imageID	    = in[i].imageID;
@@ -294,4 +335,46 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    // changed or added for PS1_V1
+    out[i].code		    = in[i].flags;
+  }
+  return (out);
+}
+
+PhotCode *PhotCode_PS1_DEV_1_To_Internal (PhotCode_PS1_DEV_1 *in, int Nvalues) {
+
+  int i;
+  PhotCode *out;
+
+  ALLOCATE (out, PhotCode, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    strncpy (out[i].name, in[i].name, 31); // out[32], in[32]
+    out[i].name[31] = 0; // force termination
+
+    out[i].code  = in[i].code;         
+    out[i].type  = in[i].type;         
+    out[i].C  	 = in[i].C;            
+    out[i].dC 	 = in[i].dC;           
+    out[i].dX 	 = in[i].dX;           
+    out[i].K  	 = in[i].K;            
+    out[i].c1 	 = in[i].c1;           
+    out[i].c2 	 = in[i].c2;           
+    out[i].equiv = in[i].equiv;        
+    out[i].Nc    = in[i].Nc;           
+    memcpy (out[i].X, in[i].X, 4*sizeof(float));            
+
+    out[i].astromErrMagScale = in[i].astromErrMagScale;
+    out[i].photomErrSys      = in[i].photomErrSys;
+
+    // changed or added for PS1_DEV_2
+    out[i].astromErrSys      = 0.0;
+    out[i].astromErrScale    = 0.0;
+
+    // changed or added for PS1_V1 (also PS1_DEV_3, deprecated)
+    out[i].photomPoorMask      = 0;
+    out[i].photomBadMask       = 0;
+    out[i].astromPoorMask      = 0;
+    out[i].astromBadMask       = 0;
   }
   return (out);
@@ -323,44 +406,5 @@
     out[i].astromErrMagScale = out[i].astromErrMagScale;
     out[i].photomErrSys      = out[i].photomErrSys;
-
-  }
-  return (out);
-}
-
-PhotCode *PhotCode_PS1_DEV_1_To_Internal (PhotCode_PS1_DEV_1 *in, int Nvalues) {
-
-  int i;
-  PhotCode *out;
-
-  ALLOCATE (out, PhotCode, Nvalues);
-
-  for (i = 0; i < Nvalues; i++) {
-    strncpy (out[i].name, in[i].name, 31); // out[32], in[32]
-    out[i].name[31] = 0; // force termination
-
-    out[i].code  = in[i].code;         
-    out[i].type  = in[i].type;         
-    out[i].C  	 = in[i].C;            
-    out[i].dC 	 = in[i].dC;           
-    out[i].dX 	 = in[i].dX;           
-    out[i].K  	 = in[i].K;            
-    out[i].c1 	 = in[i].c1;           
-    out[i].c2 	 = in[i].c2;           
-    out[i].equiv = in[i].equiv;        
-    out[i].Nc    = in[i].Nc;           
-
-    // not defined in PS1_DEV_1
-    out[i].astromErrSys      = 0.0;
-    out[i].astromErrScale    = 0.0;
-    out[i].astromErrMagScale = in[i].astromErrMagScale;
-    out[i].photomErrSys      = in[i].photomErrSys;
-
-    memcpy (out[i].X, in[i].X, 4*sizeof(float));            
-
-    out[i].photomPoorMask      = 0;
-    out[i].photomBadMask       = 0;
-    out[i].astromPoorMask      = 0;
-    out[i].astromBadMask       = 0;
-  }
-  return (out);
-}
+  }
+  return (out);
+}
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_2.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_2.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_2.c	(revision 21508)
@@ -29,5 +29,5 @@
     out[i].detID      = in[i].detID;
     out[i].imageID    = in[i].imageID;
-    out[i].qPSF       = in[i].qPSF;
+    out[i].psfQual    = in[i].psfQual;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].crNsigma   = in[i].crNsigma;
@@ -40,7 +40,18 @@
     out[i].dYccd      = in[i].dYccd;
     out[i].dbFlags    = in[i].dbFlags;
-    out[i].photFlags  = in[i].photFlags;
-    out[i].stargal    = in[i].stargal;
-    out[i].dophot     = in[i].dophot;
+
+    // changed or added for PS1_V1
+    out[i].photFlags  = in[i].photFlags | (in[i].dophot << 16);
+    out[i].t_msec     = 0;
+    out[i].extID      = 0;
+    out[i].objID      = 0;
+    out[i].catID      = 0;
+    out[i].Mxx 	      = 0.0;
+    out[i].Mxy 	      = 0.0;
+    out[i].Myy        = 0.0;
+    out[i].posangle   = 0;
+    out[i].pltscale   = 0;
+    out[i].psfNdof    = 0;
+    out[i].psfNpix    = 0;
   }
   return (out);
@@ -73,5 +84,5 @@
     out[i].detID      = in[i].detID;
     out[i].imageID    = in[i].imageID;
-    out[i].qPSF       = in[i].qPSF;
+    out[i].psfQual    = in[i].psfQual;
     out[i].psfChisq   = in[i].psfChisq;
     out[i].crNsigma   = in[i].crNsigma;
@@ -84,7 +95,8 @@
     out[i].dYccd      = in[i].dYccd;
     out[i].dbFlags    = in[i].dbFlags;
-    out[i].photFlags  = in[i].photFlags;
-    out[i].stargal    = in[i].stargal;
-    out[i].dophot     = in[i].dophot;
+
+    // changed or added for PS1_V1
+    out[i].photFlags  = in[i].photFlags & 0x0000ffff;
+    out[i].dophot     = in[i].photFlags >> 16;
   }
   return (out);
@@ -114,5 +126,4 @@
     out[i].Nmissing      = in[i].Nmissing;     
     out[i].Nextend       = in[i].Nextend;     
-    out[i].code     	 = in[i].code;   
     out[i].measureOffset = in[i].measureOffset; 
     out[i].missingOffset = in[i].missingOffset;
@@ -120,4 +131,10 @@
     out[i].objID 	 = in[i].objID;
     out[i].catID 	 = in[i].catID;
+
+    // changed or added for PS1_V1
+    out[i].flags    	 = in[i].code;   
+    out[i].ChiSq       	 = 0.0;
+    out[i].Npos       	 = 0.0;
+    out[i].extID 	 = 0;
   }
   return (out);
@@ -147,5 +164,4 @@
     out[i].Nmissing      = in[i].Nmissing;     
     out[i].Nextend       = in[i].Nextend;     
-    out[i].code     	 = in[i].code;   
     out[i].measureOffset = in[i].measureOffset; 
     out[i].missingOffset = in[i].missingOffset;
@@ -153,4 +169,7 @@
     out[i].objID 	 = in[i].objID;
     out[i].catID 	 = in[i].catID;
+
+    // changed or added for PS1_V1
+    out[i].code     	 = in[i].flags;   
   }
   return (out);
@@ -170,4 +189,8 @@
     out[i].Ncode = in[i].Ncode;
     out[i].Nused = in[i].Nused;
+
+    // changed or added for PS1_V1
+    out[i].M_20  = 0;      
+    out[i].M_80  = 0;      
  }
   return (out);
@@ -201,5 +224,5 @@
     memcpy (&out[i].coords, &in[i].coords, sizeof(Coords));
 
-    strncpy (out[i].name, in[i].name, 63); // out[128], int[64]
+    strncpy (out[i].name, in[i].name, 63); // out[121], int[64]
     out[i].name[63] = 0; // force termination
 
@@ -224,5 +247,4 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
     out[i].imageID	    = in[i].imageID;
@@ -245,4 +267,8 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    // changed or added for PS1_V1
+    out[i].flags	    = in[i].code;
+    out[i].parentID	    = 0;
   }
   return (out);
@@ -259,5 +285,5 @@
     memcpy (&out[i].coords, &in[i].coords, sizeof(Coords));
 
-    strncpy (out[i].name, in[i].name, 63); // in[128], out[64]
+    strncpy (out[i].name, in[i].name, 63); // in[121], out[64]
     out[i].name[63] = 0; // force termination
 
@@ -282,5 +308,4 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
     out[i].imageID	    = in[i].imageID;
@@ -303,14 +328,17 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
-  }
-  return (out);
-}
-
-PhotCode_PS1_DEV_2 *PhotCode_Internal_To_PS1_DEV_2 (PhotCode *in, int Nvalues) {
-
-  int i;
-  PhotCode_PS1_DEV_2 *out;
-
-  ALLOCATE (out, PhotCode_PS1_DEV_2, Nvalues);
+
+    // changed or added for PS1_V1
+    out[i].code		    = in[i].flags;
+  }
+  return (out);
+}
+
+PhotCode *PhotCode_PS1_DEV_2_To_Internal (PhotCode_PS1_DEV_2 *in, int Nvalues) {
+
+  int i;
+  PhotCode *out;
+
+  ALLOCATE (out, PhotCode, Nvalues);
 
   for (i = 0; i < Nvalues; i++) {
@@ -334,14 +362,20 @@
     out[i].astromErrMagScale = in[i].astromErrMagScale;
     out[i].photomErrSys      = in[i].photomErrSys;
-  }
-  return (out);
-}
-
-PhotCode *PhotCode_PS1_DEV_2_To_Internal (PhotCode_PS1_DEV_2 *in, int Nvalues) {
-
-  int i;
-  PhotCode *out;
-
-  ALLOCATE (out, PhotCode, Nvalues);
+
+    // changed or added for PS1_V1 (also PS1_DEV_3, deprecated)
+    out[i].photomPoorMask      = 0;
+    out[i].photomBadMask       = 0;
+    out[i].astromPoorMask      = 0;
+    out[i].astromBadMask       = 0;
+  }
+  return (out);
+}
+
+PhotCode_PS1_DEV_2 *PhotCode_Internal_To_PS1_DEV_2 (PhotCode *in, int Nvalues) {
+
+  int i;
+  PhotCode_PS1_DEV_2 *out;
+
+  ALLOCATE (out, PhotCode_PS1_DEV_2, Nvalues);
 
   for (i = 0; i < Nvalues; i++) {
@@ -365,10 +399,5 @@
     out[i].astromErrMagScale = in[i].astromErrMagScale;
     out[i].photomErrSys      = in[i].photomErrSys;
-
-    out[i].photomPoorMask      = 0;
-    out[i].photomBadMask       = 0;
-    out[i].astromPoorMask      = 0;
-    out[i].astromBadMask       = 0;
-  }
-  return (out);
-}
+  }
+  return (out);
+}
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_3.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_3.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_3.c	(revision 21508)
@@ -3,5 +3,5 @@
 /* convert PS1_DEV_3 formats to internal formats */
 
-// XXX EAM I am not yet ready to commit to a full PS1_DEV_3 release, but I am providing image and photcode conversion
+// We only provide image and photcode conversion
 
 Image *Image_PS1_DEV_3_ToInternal (Image_PS1_DEV_3 *in, int Nvalues) {
@@ -38,5 +38,4 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
     out[i].imageID	    = in[i].imageID;
@@ -59,4 +58,8 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    // changed or added for PS1_V1
+    out[i].flags	    = in[i].code;
+    out[i].parentID	    = 0;
   }
   return (out);
@@ -96,5 +99,4 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
     out[i].imageID	    = in[i].imageID;
@@ -117,4 +119,43 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    // changed or added for PS1_V1
+    out[i].code		    = in[i].flags;
+  }
+  return (out);
+}
+
+PhotCode *PhotCode_PS1_DEV_3_To_Internal (PhotCode_PS1_DEV_3 *in, int Nvalues) {
+
+  int i;
+  PhotCode *out;
+
+  ALLOCATE (out, PhotCode, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    strncpy (out[i].name, in[i].name, 31); // out[32], in[32]
+    out[i].name[31] = 0; // force termination
+
+    out[i].code  = in[i].code;         
+    out[i].type  = in[i].type;         
+    out[i].C     = in[i].C;            
+    out[i].dC 	 = in[i].dC;           
+    out[i].dX 	 = in[i].dX;           
+    out[i].K  	 = in[i].K;            
+    out[i].c1 	 = in[i].c1;           
+    out[i].c2 	 = in[i].c2;           
+    out[i].equiv = in[i].equiv;        
+    out[i].Nc    = in[i].Nc;           
+    memcpy (out[i].X, in[i].X, 4*sizeof(float));            
+
+    out[i].astromErrSys      = in[i].astromErrSys;
+    out[i].astromErrScale    = in[i].astromErrScale;
+    out[i].astromErrMagScale = in[i].astromErrMagScale;
+    out[i].photomErrSys      = in[i].photomErrSys;
+
+    out[i].photomPoorMask      = in[i].photomPoorMask;
+    out[i].photomBadMask       = in[i].photomBadMask;
+    out[i].astromPoorMask      = in[i].astromPoorMask;
+    out[i].astromBadMask       = in[i].astromBadMask;
   }
   return (out);
@@ -157,37 +198,2 @@
 }
 
-PhotCode *PhotCode_PS1_DEV_3_To_Internal (PhotCode_PS1_DEV_3 *in, int Nvalues) {
-
-  int i;
-  PhotCode *out;
-
-  ALLOCATE (out, PhotCode, Nvalues);
-
-  for (i = 0; i < Nvalues; i++) {
-    strncpy (out[i].name, in[i].name, 31); // out[32], in[32]
-    out[i].name[31] = 0; // force termination
-
-    out[i].code  = in[i].code;         
-    out[i].type  = in[i].type;         
-    out[i].C     = in[i].C;            
-    out[i].dC 	 = in[i].dC;           
-    out[i].dX 	 = in[i].dX;           
-    out[i].K  	 = in[i].K;            
-    out[i].c1 	 = in[i].c1;           
-    out[i].c2 	 = in[i].c2;           
-    out[i].equiv = in[i].equiv;        
-    out[i].Nc    = in[i].Nc;           
-    memcpy (out[i].X, in[i].X, 4*sizeof(float));            
-
-    out[i].astromErrSys      = in[i].astromErrSys;
-    out[i].astromErrScale    = in[i].astromErrScale;
-    out[i].astromErrMagScale = in[i].astromErrMagScale;
-    out[i].photomErrSys      = in[i].photomErrSys;
-
-    out[i].photomPoorMask      = in[i].photomPoorMask;
-    out[i].photomBadMask       = in[i].photomBadMask;
-    out[i].astromPoorMask      = in[i].astromPoorMask;
-    out[i].astromBadMask       = in[i].astromBadMask;
-  }
-  return (out);
-}
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V1.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V1.c	(revision 21508)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_V1.c	(revision 21508)
@@ -0,0 +1,409 @@
+# include <dvo.h>
+
+/* convert PS1_V1 formats to internal formats */
+
+Measure *Measure_PS1_V1_ToInternal (Measure_PS1_V1 *in, int Nvalues) {
+
+  int i;
+  Measure *out;
+
+  ALLOCATE (out, Measure, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].dR         = in[i].dR;
+    out[i].dD         = in[i].dD;
+    out[i].M          = in[i].M;
+    out[i].Mcal       = in[i].Mcal;
+    out[i].Map        = in[i].Map;
+    out[i].dM         = in[i].dM;
+    out[i].dMcal      = in[i].dMcal;
+    out[i].dt         = in[i].dt;
+    out[i].airmass    = in[i].airmass;
+    out[i].az         = in[i].az;
+    out[i].Xccd       = in[i].Xccd;
+    out[i].Yccd       = in[i].Yccd;
+    out[i].Sky        = in[i].Sky;
+    out[i].dSky       = in[i].dSky;
+    out[i].t          = in[i].t;
+    out[i].t_msec     = in[i].t_msec;
+    out[i].averef     = in[i].averef;
+    out[i].detID      = in[i].detID;
+    out[i].imageID    = in[i].imageID;
+    out[i].objID      = in[i].objID;
+    out[i].catID      = in[i].catID;
+    out[i].extID      = in[i].extID;
+    out[i].psfQual    = in[i].psfQual;
+    out[i].psfChisq   = in[i].psfChisq;
+    out[i].psfNdof    = in[i].psfNdof;
+    out[i].psfNpix    = in[i].psfNpix;
+    out[i].crNsigma   = in[i].crNsigma;
+    out[i].extNsigma  = in[i].extNsigma;
+    out[i].FWx 	      = in[i].FWx;
+    out[i].FWy 	      = in[i].FWy;
+    out[i].theta      = in[i].theta;
+    out[i].Mxx 	      = in[i].Mxx;
+    out[i].Mxy 	      = in[i].Mxy;
+    out[i].Myy 	      = in[i].Myy;
+    out[i].dXccd      = in[i].dXccd;
+    out[i].dYccd      = in[i].dYccd;
+    out[i].posangle   = in[i].posangle;
+    out[i].pltscale   = in[i].pltscale;
+    out[i].photcode   = in[i].photcode;
+    out[i].dbFlags    = in[i].dbFlags;
+    out[i].photFlags  = in[i].photFlags;
+  }
+  return (out);
+}
+
+Measure_PS1_V1 *MeasureInternalTo_PS1_V1 (Measure *in, int Nvalues) {
+
+  int i;
+  Measure_PS1_V1 *out;
+
+  ALLOCATE (out, Measure_PS1_V1, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].dR         = in[i].dR;
+    out[i].dD         = in[i].dD;
+    out[i].M          = in[i].M;
+    out[i].Mcal       = in[i].Mcal;
+    out[i].Map        = in[i].Map;
+    out[i].dM         = in[i].dM;
+    out[i].dMcal      = in[i].dMcal;
+    out[i].dt         = in[i].dt;
+    out[i].airmass    = in[i].airmass;
+    out[i].az         = in[i].az;
+    out[i].Xccd       = in[i].Xccd;
+    out[i].Yccd       = in[i].Yccd;
+    out[i].Sky        = in[i].Sky;
+    out[i].dSky       = in[i].dSky;
+    out[i].t          = in[i].t;
+    out[i].t_msec     = in[i].t_msec;
+    out[i].averef     = in[i].averef;
+    out[i].detID      = in[i].detID;
+    out[i].imageID    = in[i].imageID;
+    out[i].objID      = in[i].objID;
+    out[i].catID      = in[i].catID;
+    out[i].extID      = in[i].extID;
+    out[i].psfQual    = in[i].psfQual;
+    out[i].psfChisq   = in[i].psfChisq;
+    out[i].psfNdof    = in[i].psfNdof;
+    out[i].psfNpix    = in[i].psfNpix;
+    out[i].crNsigma   = in[i].crNsigma;
+    out[i].extNsigma  = in[i].extNsigma;
+    out[i].FWx 	      = in[i].FWx;
+    out[i].FWy 	      = in[i].FWy;
+    out[i].theta      = in[i].theta;
+    out[i].Mxx 	      = in[i].Mxx;
+    out[i].Mxy 	      = in[i].Mxy;
+    out[i].Myy 	      = in[i].Myy;
+    out[i].dXccd      = in[i].dXccd;
+    out[i].dYccd      = in[i].dYccd;
+    out[i].posangle   = in[i].posangle;
+    out[i].pltscale   = in[i].pltscale;
+    out[i].photcode   = in[i].photcode;
+    out[i].dbFlags    = in[i].dbFlags;
+    out[i].photFlags  = in[i].photFlags;
+  }
+  return (out);
+}
+
+// 'primary' is needed to conform with the API for Loneos and Elixir, but is not used
+Average *Average_PS1_V1_ToInternal (Average_PS1_V1 *in, int Nvalues, SecFilt **primary) {
+
+  int i;
+  Average *out;
+
+  ALLOCATE (out, Average, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].R        	 = in[i].R;      
+    out[i].D        	 = in[i].D;      
+    out[i].dR       	 = in[i].dR;
+    out[i].dD       	 = in[i].dD;
+    out[i].uR       	 = in[i].uR;
+    out[i].uD       	 = in[i].uD;
+    out[i].duR      	 = in[i].duR;
+    out[i].duD      	 = in[i].duD;
+    out[i].P        	 = in[i].P;
+    out[i].dP       	 = in[i].dP;
+    out[i].Xp       	 = in[i].Xp;     
+    out[i].ChiSq       	 = in[i].ChiSq;     
+    out[i].Npos       	 = in[i].Npos;     
+    out[i].Nmeasure      = in[i].Nmeasure;     
+    out[i].Nmissing      = in[i].Nmissing;     
+    out[i].Nextend       = in[i].Nextend;     
+    out[i].measureOffset = in[i].measureOffset; 
+    out[i].missingOffset = in[i].missingOffset;
+    out[i].extendOffset  = in[i].extendOffset;
+    out[i].flags     	 = in[i].flags;   
+    out[i].objID 	 = in[i].objID;
+    out[i].catID 	 = in[i].catID;
+    out[i].extID 	 = in[i].extID;
+  }
+  return (out);
+}
+
+// 'primary' is needed to conform with the API for Loneos and Elixir, but is not used
+Average_PS1_V1 *AverageInternalTo_PS1_V1 (Average *in, int Nvalues, SecFilt *primary) {
+
+  int i;
+  Average_PS1_V1 *out;
+
+  ALLOCATE (out, Average_PS1_V1, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].R        	 = in[i].R;      
+    out[i].D        	 = in[i].D;      
+    out[i].dR       	 = in[i].dR;
+    out[i].dD       	 = in[i].dD;
+    out[i].uR       	 = in[i].uR;
+    out[i].uD       	 = in[i].uD;
+    out[i].duR      	 = in[i].duR;
+    out[i].duD      	 = in[i].duD;
+    out[i].P        	 = in[i].P;
+    out[i].dP       	 = in[i].dP;
+    out[i].Xp       	 = in[i].Xp;     
+    out[i].ChiSq       	 = in[i].ChiSq;     
+    out[i].Npos       	 = in[i].Npos;     
+    out[i].Nmeasure      = in[i].Nmeasure;     
+    out[i].Nmissing      = in[i].Nmissing;     
+    out[i].Nextend       = in[i].Nextend;     
+    out[i].measureOffset = in[i].measureOffset; 
+    out[i].missingOffset = in[i].missingOffset;
+    out[i].extendOffset  = in[i].extendOffset;
+    out[i].flags     	 = in[i].flags;   
+    out[i].objID 	 = in[i].objID;
+    out[i].catID 	 = in[i].catID;
+    out[i].extID 	 = in[i].extID;
+  }
+  return (out);
+}
+
+SecFilt *SecFilt_PS1_V1_ToInternal (SecFilt_PS1_V1 *in, int Nvalues) {
+
+  int i;
+  SecFilt *out;
+
+  ALLOCATE (out, SecFilt, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].M     = in[i].M;      
+    out[i].dM    = in[i].dM;      
+    out[i].Xm    = in[i].Xm;     
+    out[i].Ncode = in[i].Ncode;
+    out[i].Nused = in[i].Nused;
+    out[i].M_20  = in[i].M_20;      
+    out[i].M_80  = in[i].M_80;      
+ }
+  return (out);
+}
+
+SecFilt_PS1_V1 *SecFiltInternalTo_PS1_V1 (SecFilt *in, int Nvalues) {
+
+  int i;
+  SecFilt_PS1_V1 *out;
+
+  ALLOCATE (out, SecFilt_PS1_V1, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].M     = in[i].M;      
+    out[i].dM    = in[i].dM;      
+    out[i].Xm    = in[i].Xm;     
+    out[i].Ncode = in[i].Ncode;
+    out[i].Nused = in[i].Nused;
+    out[i].M_20  = in[i].M_20;      
+    out[i].M_80  = in[i].M_80;      
+  }
+  return (out);
+}
+
+Image *Image_PS1_V1_ToInternal (Image_PS1_V1 *in, int Nvalues) {
+
+  int i;
+  Image *out;
+
+  ALLOCATE (out, Image, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    memcpy (&out[i].coords, &in[i].coords, sizeof(Coords));
+
+    strncpy (out[i].name, in[i].name, 120); // out[121], in[121]
+    out[i].name[120] = 0; // force termination
+
+    out[i].tzero    	    = in[i].tzero;
+    out[i].nstar    	    = in[i].nstar;
+    out[i].secz	    	    = in[i].secz;
+    out[i].NX	    	    = in[i].NX;
+    out[i].NY	    	    = in[i].NY;
+    out[i].apmifit  	    = in[i].apmifit;
+    out[i].dapmifit 	    = in[i].dapmifit;
+    out[i].Mcal	    	    = in[i].Mcal;
+    out[i].dMcal    	    = in[i].dMcal;
+    out[i].Xm	    	    = in[i].Xm;
+    out[i].photcode   	    = in[i].photcode;
+    out[i].exptime  	    = in[i].exptime;
+    out[i].sidtime  	    = in[i].sidtime;
+    out[i].latitude  	    = in[i].latitude;
+    out[i].detection_limit  = in[i].detection_limit;
+    out[i].saturation_limit = in[i].saturation_limit;
+    out[i].cerror	    = in[i].cerror;
+    out[i].fwhm_x	    = in[i].fwhm_x;
+    out[i].fwhm_y	    = in[i].fwhm_y;
+    out[i].trate	    = in[i].trate;
+    out[i].ccdnum	    = in[i].ccdnum;
+    out[i].flags	    = in[i].flags;
+    out[i].imageID	    = in[i].imageID;
+    out[i].parentID	    = in[i].parentID;
+    out[i].externID	    = in[i].externID;
+    out[i].sourceID	    = in[i].sourceID;
+
+    out[i].order	    = in[i].order;
+    out[i].Mx		    = in[i].Mx;
+    out[i].My		    = in[i].My;
+    out[i].Mxx		    = in[i].Mxx;
+    out[i].Mxy		    = in[i].Mxy;
+    out[i].Myy		    = in[i].Myy;
+    out[i].Mxxx		    = in[i].Mxxx;
+    out[i].Mxxy		    = in[i].Mxxy;
+    out[i].Mxyy		    = in[i].Mxyy;
+    out[i].Myyy		    = in[i].Myyy;
+    out[i].Mxxxx	    = in[i].Mxxxx;
+    out[i].Mxxxy	    = in[i].Mxxxy;
+    out[i].Mxxyy	    = in[i].Mxxyy;
+    out[i].Mxyyy	    = in[i].Mxyyy;
+    out[i].Myyyy	    = in[i].Myyyy;
+  }
+  return (out);
+}
+
+Image_PS1_V1 *ImageInternalTo_PS1_V1 (Image *in, int Nvalues) {
+
+  int i;
+  Image_PS1_V1 *out;
+
+  ALLOCATE (out, Image_PS1_V1, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    memcpy (&out[i].coords, &in[i].coords, sizeof(Coords));
+
+    strncpy (out[i].name, in[i].name, 120); // out[121], in[121]
+    out[i].name[120] = 0; // force termination
+
+    out[i].tzero    	    = in[i].tzero;
+    out[i].nstar    	    = in[i].nstar;
+    out[i].secz	    	    = in[i].secz;
+    out[i].NX	    	    = in[i].NX;
+    out[i].NY	    	    = in[i].NY;
+    out[i].apmifit  	    = in[i].apmifit;
+    out[i].dapmifit 	    = in[i].dapmifit;
+    out[i].Mcal	    	    = in[i].Mcal;
+    out[i].dMcal    	    = in[i].dMcal;
+    out[i].Xm	    	    = in[i].Xm;
+    out[i].photcode   	    = in[i].photcode;
+    out[i].exptime  	    = in[i].exptime;
+    out[i].sidtime  	    = in[i].sidtime;
+    out[i].latitude  	    = in[i].latitude;
+    out[i].detection_limit  = in[i].detection_limit;
+    out[i].saturation_limit = in[i].saturation_limit;
+    out[i].cerror	    = in[i].cerror;
+    out[i].fwhm_x	    = in[i].fwhm_x;
+    out[i].fwhm_y	    = in[i].fwhm_y;
+    out[i].trate	    = in[i].trate;
+    out[i].ccdnum	    = in[i].ccdnum;
+    out[i].flags	    = in[i].flags;
+    out[i].imageID	    = in[i].imageID;
+    out[i].parentID	    = in[i].parentID;
+    out[i].externID	    = in[i].externID;
+    out[i].sourceID	    = in[i].sourceID;
+
+    out[i].order	    = in[i].order;
+    out[i].Mx		    = in[i].Mx;
+    out[i].My		    = in[i].My;
+    out[i].Mxx		    = in[i].Mxx;
+    out[i].Mxy		    = in[i].Mxy;
+    out[i].Myy		    = in[i].Myy;
+    out[i].Mxxx		    = in[i].Mxxx;
+    out[i].Mxxy		    = in[i].Mxxy;
+    out[i].Mxyy		    = in[i].Mxyy;
+    out[i].Myyy		    = in[i].Myyy;
+    out[i].Mxxxx	    = in[i].Mxxxx;
+    out[i].Mxxxy	    = in[i].Mxxxy;
+    out[i].Mxxyy	    = in[i].Mxxyy;
+    out[i].Mxyyy	    = in[i].Mxyyy;
+    out[i].Myyyy	    = in[i].Myyyy;
+  }
+  return (out);
+}
+
+PhotCode *PhotCode_PS1_V1_To_Internal (PhotCode_PS1_V1 *in, int Nvalues) {
+
+  int i;
+  PhotCode *out;
+
+  ALLOCATE (out, PhotCode, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    strncpy (out[i].name, in[i].name, 31); // out[32], in[32]
+    out[i].name[31] = 0; // force termination
+
+    out[i].code  = in[i].code;         
+    out[i].type  = in[i].type;         
+    out[i].C     = in[i].C;            
+    out[i].dC 	 = in[i].dC;           
+    out[i].dX 	 = in[i].dX;           
+    out[i].K  	 = in[i].K;            
+    out[i].c1 	 = in[i].c1;           
+    out[i].c2 	 = in[i].c2;           
+    out[i].equiv = in[i].equiv;        
+    out[i].Nc    = in[i].Nc;           
+    memcpy (out[i].X, in[i].X, 4*sizeof(float));            
+
+    out[i].astromErrSys      = in[i].astromErrSys;
+    out[i].astromErrScale    = in[i].astromErrScale;
+    out[i].astromErrMagScale = in[i].astromErrMagScale;
+    out[i].photomErrSys      = in[i].photomErrSys;
+
+    out[i].photomPoorMask      = in[i].photomPoorMask;
+    out[i].photomBadMask       = in[i].photomBadMask;
+    out[i].astromPoorMask      = in[i].astromPoorMask;
+    out[i].astromBadMask       = in[i].astromBadMask;
+  }
+  return (out);
+}
+
+PhotCode_PS1_V1 *PhotCode_Internal_To_PS1_V1 (PhotCode *in, int Nvalues) {
+
+  int i;
+  PhotCode_PS1_V1 *out;
+
+  ALLOCATE (out, PhotCode_PS1_V1, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    strncpy (out[i].name, in[i].name, 31); // out[32], in[32]
+    out[i].name[31] = 0; // force termination
+
+    out[i].code  = in[i].code;         
+    out[i].type  = in[i].type;         
+    out[i].C     = in[i].C;            
+    out[i].dC 	 = in[i].dC;           
+    out[i].dX 	 = in[i].dX;           
+    out[i].K  	 = in[i].K;            
+    out[i].c1 	 = in[i].c1;           
+    out[i].c2 	 = in[i].c2;           
+    out[i].equiv = in[i].equiv;        
+    out[i].Nc    = in[i].Nc;           
+    memcpy (out[i].X, in[i].X, 4*sizeof(float));            
+
+    out[i].astromErrSys      = in[i].astromErrSys;
+    out[i].astromErrScale    = in[i].astromErrScale;
+    out[i].astromErrMagScale = in[i].astromErrMagScale;
+    out[i].photomErrSys      = in[i].photomErrSys;
+
+    out[i].photomPoorMask      = in[i].photomPoorMask;
+    out[i].photomBadMask       = in[i].photomBadMask;
+    out[i].astromPoorMask      = in[i].astromPoorMask;
+    out[i].astromBadMask       = in[i].astromBadMask;
+  }
+  return (out);
+}
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c	(revision 21508)
@@ -10,4 +10,9 @@
 
   for (i = 0; i < Nvalues; i++) {
+    out[i].FWx 	    = in[i].FWx;
+    out[i].t        = in[i].t;
+    out[i].averef   = in[i].averef;
+
+    // changed for PANSTARRS_DEV_0
     out[i].dR       = (in[i].dR      == NAN_S_SHORT) ? NAN : in[i].dR     * 0.01;
     out[i].dD       = (in[i].dD      == NAN_S_SHORT) ? NAN : in[i].dD     * 0.01;
@@ -16,38 +21,47 @@
     out[i].dt       = (in[i].dt      == NAN_S_SHORT) ? NAN : in[i].dt     * 0.001;
     out[i].Mcal     = (in[i].Mcal    == NAN_S_SHORT) ? NAN : in[i].Mcal   * 0.001;
-
-    // 2008.02.26 : I've renamed Mgal to Map, and intend it to be used per aperture
-    // magnitudes.  Most uses of Mgal in the past were actually aperture (isophotal) mags
     out[i].Map      = (in[i].Mgal    == NAN_S_SHORT) ? NAN : in[i].Mgal   * 0.001;
     out[i].airmass  = (in[i].airmass == NAN_S_SHORT) ? NAN : in[i].airmass* 0.001;
-    out[i].FWx 	    = in[i].FWx;
     out[i].FWy 	    = in[i].fwy * in[i].FWx * 0.01;
     out[i].theta    = in[i].theta*(0x10000 / 0x100);
-    out[i].dophot   = in[i].dophot;
     out[i].photcode = in[i].source;
-    out[i].t        = in[i].t;
-    out[i].averef   = in[i].averef;
-    out[i].dbFlags  = in[i].flags;
     
-    /* these can be determined if needed / desired */
-    out[i].Xccd     = 0;
-    out[i].Yccd     = 0;
-    out[i].dXccd    = 0;
-    out[i].dYccd    = 0;
-
-    /* these do not have a corresponding value */
+    // added for PANSTARRS_DEV_0
+    out[i].Xccd      = 0;  // determine on-the-fly
+    out[i].Yccd      = 0;  // determine on-the-fly
+    out[i].dXccd     = 0;
+    out[i].dYccd     = 0;
     out[i].az        = 0;
-    out[i].stargal   = 0;
     out[i].Sky       = 0;
     out[i].dSky      = 0;
-    out[i].qPSF      = 0;
+    out[i].psfQual   = 0;
     out[i].psfChisq  = 0;
     out[i].crNsigma  = 0;
     out[i].extNsigma = 0;
-    out[i].photFlags = 0;
-
-    /* XXX add these later */
-    out[i].detID = 0;
-    out[i].imageID = 0;
+    out[i].detID     = 0;  // determine on-the-fly
+    out[i].imageID   = 0;  // determine on-the-fly
+
+    // changed or added for PS1_DEV_1 (2008.02.26)
+    out[i].dbFlags    = in[i].flags;
+    out[i].detID      = 0;
+    out[i].imageID    = 0;
+
+    // changed or added for PS1_DEV_2
+    out[i].Map        = in[i].Mgal;
+    out[i].dMcal      = 0; 
+
+    // changed or added for PS1_V1
+    out[i].photFlags  = in[i].dophot << 16;
+    out[i].t_msec     = 0;
+    out[i].extID      = 0;
+    out[i].objID      = 0;
+    out[i].catID      = 0;
+    out[i].Mxx 	      = 0.0;
+    out[i].Mxy 	      = 0.0;
+    out[i].Myy        = 0.0;
+    out[i].posangle   = 0;
+    out[i].pltscale   = 0;
+    out[i].psfNdof    = 0;
+    out[i].psfNpix    = 0;
   }
   return (out);
@@ -63,4 +77,9 @@
 
   for (i = 0; i < Nvalues; i++) {
+    out[i].FWx 	   = in[i].FWx;
+    out[i].t       = in[i].t;
+    out[i].averef  = in[i].averef;
+
+    // changed for PANSTARRS_DEV_0
     out[i].dR       = isnan(in[i].dR     ) ? NAN_S_SHORT : in[i].dR      *  100.0;
     out[i].dD       = isnan(in[i].dD     ) ? NAN_S_SHORT : in[i].dD      *  100.0;
@@ -71,4 +90,7 @@
     out[i].Mgal     = isnan(in[i].Map    ) ? NAN_S_SHORT : in[i].Map     * 1000.0;
     out[i].airmass  = isnan(in[i].airmass) ? NAN_S_SHORT : in[i].airmass * 1000.0;
+    out[i].fwy 	    = in[i].FWy * 100.0 / in[i].FWx;
+    out[i].theta    = in[i].theta*(0x100/ 0x10000);
+    out[i].source   = in[i].photcode;
 
     if (out[i].M < 0) {
@@ -76,12 +98,9 @@
     }
 
-    out[i].FWx 	   = in[i].FWx;
-    out[i].fwy 	   = in[i].FWy * 100.0 / in[i].FWx;
-    out[i].theta   = in[i].theta*(0x100/ 0x10000);
-    out[i].dophot  = in[i].dophot;
-    out[i].source  = in[i].photcode;
-    out[i].t       = in[i].t;
-    out[i].averef  = in[i].averef;
+    // changed or added for PS1_DEV_1 (2008.02.26)
     out[i].flags   = in[i].dbFlags;
+
+    // changed or added for PS1_V1
+    out[i].dophot  = in[i].photFlags >> 16;
   }
   return (out);
@@ -101,11 +120,14 @@
     out[i].D       	 = in[i].D;      
     out[i].Xp      	 = in[i].Xp;     
-    out[i].Nmeasure 	 = in[i].Nm;     
-    out[i].Nmissing 	 = in[i].Nn;     
-    out[i].code     	 = in[i].code;   
-    out[i].measureOffset = in[i].offset; 
-    out[i].missingOffset = in[i].missing;
-
-    /* these don't exist in Elixir */
+
+    // changed for PANSTARRS_DEV_0 (moved from Average to Measure)
+    primary[0][i].M     = (in[i].M  == NAN_S_SHORT) ? NAN : in[i].M  * 0.001;      
+    primary[0][i].dM    = (in[i].dM == NAN_S_SHORT) ? NAN : in[i].dM * 0.001;      
+    primary[0][i].Xm    = in[i].Xm;     
+
+    primary[0][i].Ncode = 0;     
+    primary[0][i].Nused = 0;
+
+    // added for PANSTARRS_DEV_0
     out[i].dR      = 0;
     out[i].dD      = 0;
@@ -116,14 +138,20 @@
     out[i].P       = 0;
     out[i].dP      = 0;
-
-    /* XXX add these later */
-    out[i].objID   = 0;
-    out[i].catID   = 0;
-
-    primary[0][i].M     = (in[i].M  == NAN_S_SHORT) ? NAN : in[i].M  * 0.001;      
-    primary[0][i].dM    = (in[i].dM == NAN_S_SHORT) ? NAN : in[i].dM * 0.001;      
-    primary[0][i].Xm    = in[i].Xm;     
-    primary[0][i].Ncode = 0;     
-    primary[0][i].Nused = 0;
+    out[i].objID   = 0; // determine on-the-fly
+    out[i].catID   = 0; // determine on-the-fly
+
+    // changed or added for PS1_DEV_2
+    out[i].Nmeasure 	 = in[i].Nm;     
+    out[i].Nmissing 	 = in[i].Nn;     
+    out[i].measureOffset = in[i].offset; 
+    out[i].missingOffset = in[i].missing;
+    out[i].Nextend       = 0;
+    out[i].extendOffset  = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags    	 = in[i].code;   
+    out[i].ChiSq       	 = 0.0;
+    out[i].Npos       	 = 0.0;
+    out[i].extID 	 = 0;
   }
   return (out);
@@ -142,13 +170,18 @@
     out[i].D       = in[i].D;      
     out[i].Xp      = in[i].Xp;     
-    out[i].Nm      = in[i].Nmeasure;     
-    out[i].Nn      = in[i].Nmissing;     
-    out[i].code    = in[i].code;   
-    out[i].offset  = in[i].measureOffset; 
-    out[i].missing = in[i].missingOffset;
-
+
+    // changed for PANSTARRS_DEV_0 (moved from Average to Measure)
     out[i].M       = isnan(primary[i].M)  ? NAN_S_SHORT : primary[i].M   * 1000.0;
     out[i].dM      = isnan(primary[i].dM) ? NAN_S_SHORT : primary[i].dM  * 1000.0;
     out[i].Xm      = primary[i].Xm;     
+
+    // changed or added for PS1_DEV_2
+    out[i].Nm      = in[i].Nmeasure;     
+    out[i].Nn      = in[i].Nmissing;     
+    out[i].offset  = in[i].measureOffset; 
+    out[i].missing = in[i].missingOffset;
+
+    // changed or added for PS1_V1
+    out[i].code    = in[i].flags;   
   }
   return (out);
@@ -164,9 +197,15 @@
 
   for (i = 0; i < Nvalues; i++) {
+    out[i].Xm    = in[i].Xm;     
+
+    // added or changed for PANSTARRS_DEV_0
     out[i].M     = (in[i].M  == NAN_S_SHORT) ? NAN : in[i].M   * 0.001;
     out[i].dM    = (in[i].dM == NAN_S_SHORT) ? NAN : in[i].dM  * 0.001;
-    out[i].Xm    = in[i].Xm;     
     out[i].Ncode = 0;
     out[i].Nused = 0;
+
+    // changed or added for PS1_V1
+    out[i].M_20  = 0;      
+    out[i].M_80  = 0;      
   }
   return (out);
@@ -182,7 +221,9 @@
 
   for (i = 0; i < Nvalues; i++) {
+    out[i].Xm    = in[i].Xm;     
+
+    // added or changed for PANSTARRS_DEV_0
     out[i].M     = isnan(in[i].M)  ? NAN_S_SHORT : in[i].M   * 1000.0;
     out[i].dM    = isnan(in[i].dM) ? NAN_S_SHORT : in[i].dM  * 1000.0;
-    out[i].Xm    = in[i].Xm;     
   }
   return (out);
@@ -207,9 +248,4 @@
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-    out[i].secz    	    = (in[i].secz     == NAN_S_SHORT) ? NAN : in[i].secz     * 0.001;
-    out[i].apmifit  	    = (in[i].apmifit  == NAN_S_SHORT) ? NAN : in[i].apmifit  * 0.001;
-    out[i].dapmifit  	    = (in[i].dapmifit == NAN_S_SHORT) ? NAN : in[i].dapmifit * 0.001;
-    out[i].Mcal   	    = (in[i].Mcal     == NAN_S_SHORT) ? NAN : in[i].Mcal     * 0.001;
-    out[i].dMcal   	    = (in[i].dMcal    == NAN_S_SHORT) ? NAN : in[i].dMcal    * 0.001;
     out[i].Xm	    	    = in[i].Xm;
     out[i].photcode   	    = in[i].source;
@@ -221,5 +257,4 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
 
@@ -240,10 +275,21 @@
     out[i].Myyyy	    = in[i].Myyyy;
 
-    /* XXX add these later */
-    out[i].imageID = 0;
-
-    // not available in ELIXIR
+    // added or changed for PANSTARRS_DEV_0
+    out[i].secz    	    = (in[i].secz     == NAN_S_SHORT) ? NAN : in[i].secz     * 0.001;
+    out[i].apmifit  	    = (in[i].apmifit  == NAN_S_SHORT) ? NAN : in[i].apmifit  * 0.001;
+    out[i].dapmifit  	    = (in[i].dapmifit == NAN_S_SHORT) ? NAN : in[i].dapmifit * 0.001;
+    out[i].Mcal   	    = (in[i].Mcal     == NAN_S_SHORT) ? NAN : in[i].Mcal     * 0.001;
+    out[i].dMcal   	    = (in[i].dMcal    == NAN_S_SHORT) ? NAN : in[i].dMcal    * 0.001;
     out[i].sidtime  	    = NAN;
     out[i].latitude  	    = NAN;
+    out[i].imageID          = 0; // determine on-the-fly
+
+    // changed or added for PS1_DEV_2
+    out[i].externID	    = 0;
+    out[i].sourceID	    = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags	    = in[i].code;
+    out[i].parentID	    = 0;
   }
   return (out);
@@ -268,9 +314,5 @@
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-    out[i].secz    	    = isnan(in[i].secz    ) ? NAN_S_SHORT : in[i].secz     * 1000.0;
-    out[i].apmifit    	    = isnan(in[i].apmifit ) ? NAN_S_SHORT : in[i].apmifit  * 1000.0;
-    out[i].dapmifit    	    = isnan(in[i].dapmifit) ? NAN_S_SHORT : in[i].dapmifit * 1000.0;
-    out[i].Mcal    	    = isnan(in[i].Mcal    ) ? NAN_S_SHORT : in[i].Mcal     * 1000.0;
-    out[i].dMcal    	    = isnan(in[i].dMcal   ) ? NAN_S_SHORT : in[i].dMcal    * 1000.0;
+
     out[i].Xm	    	    = in[i].Xm;
     out[i].source   	    = in[i].photcode;
@@ -282,5 +324,4 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
 
@@ -300,14 +341,24 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
-  }
-  return (out);
-}
-
-PhotCode_Elixir *PhotCode_Internal_To_Elixir (PhotCode *in, int Nvalues) {
-
-  int i;
-  PhotCode_Elixir *out;
-
-  ALLOCATE (out, PhotCode_Elixir, Nvalues);
+
+    // added or changed for PANSTARRS_DEV_0
+    out[i].secz    	    = isnan(in[i].secz    ) ? NAN_S_SHORT : in[i].secz     * 1000.0;
+    out[i].apmifit    	    = isnan(in[i].apmifit ) ? NAN_S_SHORT : in[i].apmifit  * 1000.0;
+    out[i].dapmifit    	    = isnan(in[i].dapmifit) ? NAN_S_SHORT : in[i].dapmifit * 1000.0;
+    out[i].Mcal    	    = isnan(in[i].Mcal    ) ? NAN_S_SHORT : in[i].Mcal     * 1000.0;
+    out[i].dMcal    	    = isnan(in[i].dMcal   ) ? NAN_S_SHORT : in[i].dMcal    * 1000.0;
+
+    // changed or added for PS1_V1
+    out[i].code		    = in[i].flags;
+  }
+  return (out);
+}
+
+PhotCode *PhotCode_Elixir_To_Internal (PhotCode_Elixir *in, int Nvalues) {
+
+  int i;
+  PhotCode *out;
+
+  ALLOCATE (out, PhotCode, Nvalues);
 
   for (i = 0; i < Nvalues; i++) {
@@ -326,14 +377,28 @@
     out[i].Nc    = in[i].Nc;           
     memcpy (out[i].X, in[i].X, 4*sizeof(float));            
-  }
-  return (out);
-}
-
-PhotCode *PhotCode_Elixir_To_Internal (PhotCode_Elixir *in, int Nvalues) {
-
-  int i;
-  PhotCode *out;
-
-  ALLOCATE (out, PhotCode, Nvalues);
+
+    // changed or added for PS1_DEV_1
+    out[i].astromErrMagScale = 0.0;
+    out[i].photomErrSys      = 0.0;
+
+    // changed or added for PS1_DEV_2
+    out[i].astromErrSys      = 0.0;
+    out[i].astromErrScale    = 0.0;
+
+    // changed or added for PS1_V1 (also PS1_DEV_3, deprecated)
+    out[i].photomPoorMask      = 0;
+    out[i].photomBadMask       = 0;
+    out[i].astromPoorMask      = 0;
+    out[i].astromBadMask       = 0;
+  }
+  return (out);
+}
+
+PhotCode_Elixir *PhotCode_Internal_To_Elixir (PhotCode *in, int Nvalues) {
+
+  int i;
+  PhotCode_Elixir *out;
+
+  ALLOCATE (out, PhotCode_Elixir, Nvalues);
 
   for (i = 0; i < Nvalues; i++) {
@@ -352,16 +417,5 @@
     out[i].Nc    = in[i].Nc;           
     memcpy (out[i].X, in[i].X, 4*sizeof(float));            
-
-    // not defined in Elixir:
-    out[i].astromErrSys      = 0.0;
-    out[i].astromErrScale    = 0.0;
-    out[i].astromErrMagScale = 0.0;
-    out[i].photomErrSys      = 0.0;
-
-    out[i].photomPoorMask      = 0;
-    out[i].photomBadMask       = 0;
-    out[i].astromPoorMask      = 0;
-    out[i].astromBadMask       = 0;
-  }
-  return (out);
-}
+  }
+  return (out);
+}
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_loneos.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_loneos.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_loneos.c	(revision 21508)
@@ -10,46 +10,60 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].dR        = (in[i].dR      == NAN_S_SHORT) ? NAN : in[i].dR     * 0.01;
-    out[i].dD        = (in[i].dD      == NAN_S_SHORT) ? NAN : in[i].dD     * 0.01;
-    out[i].M         = (in[i].M       == NAN_S_SHORT) ? NAN : in[i].M      * 0.001;
-    out[i].dM        = (in[i].dM      == NAN_U_CHAR)  ? NAN : in[i].dM     * 0.001;
-    out[i].Mcal      = (in[i].Mcal    == NAN_S_SHORT) ? NAN : in[i].Mcal   * 0.001;
-    out[i].dophot    = in[i].dophot;
-    out[i].photcode  = in[i].source;
-    out[i].t         = in[i].t;
-
-    /* flags and averef are now split */
+    out[i].t        = in[i].t;
+
+    // added or changed for ELIXIR
     out[i].averef    =  in[i].averef & 0x00ffffff;
     out[i].dbFlags   = (in[i].averef & 0xff000000) >> 24;
-
-    /* these values don't exist in the Loneos format */
-
-    // 2008.02.26 : I've renamed Mgal to Map, and intend it to be used per aperture
-    // magnitudes.  Most uses of Mgal in the past were actually aperture (isophotal) mags
-    out[i].Map       = out[i].M;
+    out[i].t         = in[i].t;
     out[i].dt        = 0;
     out[i].airmass   = 0;
+    out[i].FWy 	     = 0;
+    out[i].FWx       = 0;
+    out[i].theta     = 0;
+
+    // changed for PANSTARRS_DEV_0
+    out[i].dR       = (in[i].dR      == NAN_S_SHORT) ? NAN : in[i].dR     * 0.01;
+    out[i].dD       = (in[i].dD      == NAN_S_SHORT) ? NAN : in[i].dD     * 0.01;
+    out[i].M        = (in[i].M       == NAN_S_SHORT) ? NAN : in[i].M      * 0.001;
+    out[i].dM       = (in[i].dM      == NAN_U_CHAR)  ? NAN : in[i].dM     * 0.001;
+    out[i].Mcal     = (in[i].Mcal    == NAN_S_SHORT) ? NAN : in[i].Mcal   * 0.001;
+    out[i].Map      = (in[i].M       == NAN_S_SHORT) ? NAN : in[i].M      * 0.001;
+    out[i].photcode = in[i].source;
+    
+    // added for PANSTARRS_DEV_0
+    out[i].Xccd      = 0;  // determine on-the-fly
+    out[i].Yccd      = 0;  // determine on-the-fly
+    out[i].dXccd     = 0;
+    out[i].dYccd     = 0;
     out[i].az        = 0;
-    out[i].FWx 	     = 0;
-    out[i].FWy 	     = 0;
-    out[i].theta     = 0;
-    out[i].stargal   = 0;
     out[i].Sky       = 0;
     out[i].dSky      = 0;
-    out[i].qPSF      = 0;
+    out[i].psfQual   = 0;
     out[i].psfChisq  = 0;
     out[i].crNsigma  = 0;
     out[i].extNsigma = 0;
-    out[i].photFlags = 0;
-
-    /* these can be determined if needed / desired */
-    out[i].Xccd     = 0;
-    out[i].Yccd     = 0;
-    out[i].dXccd    = 0;
-    out[i].dYccd    = 0;
-
-    /* XXX add these later */
-    out[i].detID = 0;
-    out[i].imageID = 0;
+    out[i].detID     = 0;  // determine on-the-fly
+    out[i].imageID   = 0;  // determine on-the-fly
+
+    // changed or added for PS1_DEV_1 (2008.02.26)
+    out[i].detID      = 0;
+    out[i].imageID    = 0;
+
+    // changed or added for PS1_DEV_2
+    out[i].dMcal      = 0; 
+
+    // changed or added for PS1_V1
+    out[i].photFlags  = in[i].dophot << 16;
+    out[i].t_msec     = 0;
+    out[i].extID      = 0;
+    out[i].objID      = 0;
+    out[i].catID      = 0;
+    out[i].Mxx 	      = 0.0;
+    out[i].Mxy 	      = 0.0;
+    out[i].Myy        = 0.0;
+    out[i].posangle   = 0;
+    out[i].pltscale   = 0;
+    out[i].psfNdof    = 0;
+    out[i].psfNpix    = 0;
   }
   return (out);
@@ -65,4 +79,6 @@
 
   for (i = 0; i < Nvalues; i++) {
+
+    // changed for PANSTARRS_DEV_0
     out[i].dR     = isnan(in[i].dR     ) ? NAN_S_SHORT : in[i].dR      *  100.0;
     out[i].dD     = isnan(in[i].dD     ) ? NAN_S_SHORT : in[i].dD      *  100.0;
@@ -70,5 +86,4 @@
     out[i].dM     = isnan(in[i].dM     ) ? NAN_U_CHAR  : in[i].dM      * 1000.0;
     out[i].Mcal   = isnan(in[i].Mcal   ) ? NAN_S_SHORT : in[i].Mcal    * 1000.0;
-    out[i].dophot = in[i].dophot;
     out[i].source = in[i].photcode;
     out[i].t      = in[i].t;
@@ -76,4 +91,7 @@
     /* flags and averef are merged in Loneos */
     out[i].averef =  (in[i].averef & 0x00ffffff) | (in[i].dbFlags << 24);
+
+    // changed or added for PS1_V1
+    out[i].dophot  = in[i].photFlags >> 16;
   }
   return (out);
@@ -93,11 +111,15 @@
     out[i].D       	 = in[i].D;      
     out[i].Xp      	 = in[i].Xp;     
-    out[i].Nmeasure      = in[i].Nm;     
-    out[i].Nmissing      = in[i].Nn;     
-    out[i].code    	 = in[i].code;   
-    out[i].measureOffset = in[i].offset; 
-    out[i].missingOffset = in[i].missing;
-
-    /* these don't exist in Loneos */
+
+    // added for ELIXIR
+    primary[0][i].dM    = NAN;
+
+    // changed for PANSTARRS_DEV_0 (moved from Average to Measure)
+    primary[0][i].M     = (in[i].M  == NAN_S_SHORT) ? NAN : in[i].M  * 0.001;      
+    primary[0][i].Xm    = in[i].Xm;     
+    primary[0][i].Ncode = 0;     
+    primary[0][i].Nused = 0;
+
+    // added for PANSTARRS_DEV_0
     out[i].dR      = 0;
     out[i].dD      = 0;
@@ -108,14 +130,20 @@
     out[i].P       = 0;
     out[i].dP      = 0;
-
-    /* XXX add these later */
-    out[i].objID   = 0;
-    out[i].catID   = 0;
-
-    primary[0][i].M  = (in[i].M  == NAN_S_SHORT) ? NAN : in[i].M  * 0.001;      
-    primary[0][i].Xm = in[i].Xm;     
-    primary[0][i].dM = NAN;
-    primary[0][i].Ncode = 0;     
-    primary[0][i].Nused = 0;
+    out[i].objID   = 0; // determine on-the-fly
+    out[i].catID   = 0; // determine on-the-fly
+
+    // changed or added for PS1_DEV_2
+    out[i].Nmeasure 	 = in[i].Nm;     
+    out[i].Nmissing 	 = in[i].Nn;     
+    out[i].measureOffset = in[i].offset; 
+    out[i].missingOffset = in[i].missing;
+    out[i].Nextend       = 0;
+    out[i].extendOffset  = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags    	 = in[i].code;   
+    out[i].ChiSq       	 = 0.0;
+    out[i].Npos       	 = 0.0;
+    out[i].extID 	 = 0;
   }
   return (out);
@@ -134,12 +162,17 @@
     out[i].D       = in[i].D;      
     out[i].Xp      = in[i].Xp;     
+
+    // changed for PANSTARRS_DEV_0 (moved from Average to Measure)
+    out[i].M       = isnan(primary[i].M)  ? NAN_S_SHORT : primary[i].M   * 1000.0;
+    out[i].Xm      = primary[i].Xm;     
+
+    // changed or added for PS1_DEV_2
     out[i].Nm      = in[i].Nmeasure;     
     out[i].Nn      = in[i].Nmissing;     
-    out[i].code    = in[i].code;   
     out[i].offset  = in[i].measureOffset; 
     out[i].missing = in[i].missingOffset;
 
-    out[i].M       = isnan(primary[i].M)  ? NAN_S_SHORT : primary[i].M   * 1000.0;
-    out[i].Xm      = primary[i].Xm;     
+    // changed or added for PS1_V1
+    out[i].code    = in[i].flags;   
   }
   return (out);
@@ -155,9 +188,17 @@
 
   for (i = 0; i < Nvalues; i++) {
+    out[i].Xm    = in[i].Xm;      
+
+    // added for ELIXIR
+    out[i].dM    = NAN;
+
+    // added or changed for PANSTARRS_DEV_0
     out[i].M     = (in[i].M  == NAN_S_SHORT) ? NAN : in[i].M * 0.001;
-    out[i].Xm    = in[i].Xm;      
-    out[i].dM    = NAN;
     out[i].Ncode = 0;
     out[i].Nused = 0;
+
+    // changed or added for PS1_V1
+    out[i].M_20  = 0;      
+    out[i].M_80  = 0;      
   }
   return (out);
@@ -173,6 +214,8 @@
 
   for (i = 0; i < Nvalues; i++) {
+    out[i].Xm   = in[i].Xm;      
+
+    // added or changed for PANSTARRS_DEV_0
     out[i].M    = isnan(in[i].M)  ? NAN_S_SHORT : in[i].M * 1000.0;
-    out[i].Xm   = in[i].Xm;      
   }
   return (out);
@@ -197,10 +240,4 @@
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-
-    out[i].secz    	    = (in[i].secz     == NAN_S_SHORT) ? NAN : in[i].secz     * 0.001;
-    out[i].apmifit  	    = (in[i].apmifit  == NAN_S_SHORT) ? NAN : in[i].apmifit  * 0.001;
-    out[i].dapmifit  	    = (in[i].dapmifit == NAN_S_SHORT) ? NAN : in[i].dapmifit * 0.001;
-    out[i].Mcal   	    = (in[i].Mcal     == NAN_S_SHORT) ? NAN : in[i].Mcal     * 0.001;
-    out[i].dMcal   	    = (in[i].dMcal    == NAN_S_SHORT) ? NAN : in[i].dMcal    * 0.001;
 
     out[i].Xm	    	    = in[i].Xm;
@@ -213,6 +250,6 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
+
     out[i].order	    = in[i].order;
     out[i].Mx		    = in[i].Mx;
@@ -231,6 +268,21 @@
     out[i].Myyyy	    = in[i].Myyyy;
 
-    /* XXX add these later */
-    out[i].imageID = 0;
+    // added or changed for PANSTARRS_DEV_0
+    out[i].secz    	    = (in[i].secz     == NAN_S_SHORT) ? NAN : in[i].secz     * 0.001;
+    out[i].apmifit  	    = (in[i].apmifit  == NAN_S_SHORT) ? NAN : in[i].apmifit  * 0.001;
+    out[i].dapmifit  	    = (in[i].dapmifit == NAN_S_SHORT) ? NAN : in[i].dapmifit * 0.001;
+    out[i].Mcal   	    = (in[i].Mcal     == NAN_S_SHORT) ? NAN : in[i].Mcal     * 0.001;
+    out[i].dMcal   	    = (in[i].dMcal    == NAN_S_SHORT) ? NAN : in[i].dMcal    * 0.001;
+    out[i].sidtime  	    = NAN;
+    out[i].latitude  	    = NAN;
+    out[i].imageID          = 0; // determine on-the-fly
+
+    // changed or added for PS1_DEV_2
+    out[i].externID	    = 0;
+    out[i].sourceID	    = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags	    = in[i].code;
+    out[i].parentID	    = 0;
   }
   return (out);
@@ -255,10 +307,4 @@
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-
-    out[i].secz    	    = isnan(in[i].secz    ) ? NAN_S_SHORT : in[i].secz     * 1000.0;
-    out[i].apmifit    	    = isnan(in[i].apmifit ) ? NAN_S_SHORT : in[i].apmifit  * 1000.0;
-    out[i].dapmifit    	    = isnan(in[i].dapmifit) ? NAN_S_SHORT : in[i].dapmifit * 1000.0;
-    out[i].Mcal    	    = isnan(in[i].Mcal    ) ? NAN_S_SHORT : in[i].Mcal     * 1000.0;
-    out[i].dMcal    	    = isnan(in[i].dMcal   ) ? NAN_S_SHORT : in[i].dMcal    * 1000.0;
 
     out[i].Xm	    	    = in[i].Xm;
@@ -271,6 +317,6 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
+
     out[i].order	    = in[i].order;
     out[i].Mx		    = in[i].Mx;
@@ -288,5 +334,15 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
-  }
-  return (out);
-}
+
+    // added or changed for PANSTARRS_DEV_0
+    out[i].secz    	    = isnan(in[i].secz    ) ? NAN_S_SHORT : in[i].secz     * 1000.0;
+    out[i].apmifit    	    = isnan(in[i].apmifit ) ? NAN_S_SHORT : in[i].apmifit  * 1000.0;
+    out[i].dapmifit    	    = isnan(in[i].dapmifit) ? NAN_S_SHORT : in[i].dapmifit * 1000.0;
+    out[i].Mcal    	    = isnan(in[i].Mcal    ) ? NAN_S_SHORT : in[i].Mcal     * 1000.0;
+    out[i].dMcal    	    = isnan(in[i].dMcal   ) ? NAN_S_SHORT : in[i].dMcal    * 1000.0;
+
+    // changed or added for PS1_V1
+    out[i].code		    = in[i].flags;
+  }
+  return (out);
+}
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c	(revision 21508)
@@ -16,7 +16,4 @@
     out[i].Mcal       = in[i].Mcal;
 
-    // 2008.02.26 : I've renamed Mgal to Map, and intend it to be used per aperture
-    // magnitudes.  Most uses of Mgal in the past were actually aperture (isophotal) mags
-    out[i].Map        = in[i].Mgal;
     out[i].airmass    = in[i].airmass;
     out[i].az         = in[i].az;
@@ -25,25 +22,43 @@
     out[i].FWy 	      = in[i].FWy;
     out[i].theta      = in[i].theta;
-    out[i].dophot     = in[i].dophot;
     out[i].photcode   = in[i].photcode;
     out[i].t          = in[i].t;
     out[i].averef     = in[i].averef;
-    out[i].dbFlags    = in[i].flags;
     out[i].Xccd       = in[i].Xccd;
     out[i].Yccd       = in[i].Yccd;
     out[i].dXccd      = in[i].dXccd;
     out[i].dYccd      = in[i].dYccd;
-    out[i].stargal    = in[i].stargal;
     out[i].Sky        = in[i].Sky;
     out[i].dSky       = in[i].dSky;
-    out[i].qPSF       = (in[i].qPSF == NAN_S_SHORT) ? NAN : in[i].qPSF;
+
+    // 2008.02.26 : I've renamed Mgal to Map, and intend it to be used per aperture
+    // magnitudes.  Most uses of Mgal in the past were actually aperture (isophotal) mags
+
+    // changed or added for PS1_DEV_1 (2008.02.26)
+    out[i].psfQual    = (in[i].psfQual == NAN_S_SHORT) ? NAN : in[i].psfQual;
+    out[i].dbFlags    = in[i].flags;
     out[i].detID      = in[i].detID_lo;
     out[i].imageID    = in[i].imageID_lo;
-
-    // these don't have a correspondence
-    out[i].psfChisq  = 0;
-    out[i].crNsigma  = 0;
-    out[i].extNsigma = 0;
-    out[i].photFlags = 0;
+    out[i].psfChisq   = 0;
+    out[i].crNsigma   = 0;
+    out[i].extNsigma  = 0;
+
+    // changed or added for PS1_DEV_2
+    out[i].Map        = in[i].Mgal;
+    out[i].dMcal      = 0; 
+
+    // changed or added for PS1_V1
+    out[i].photFlags  = in[i].dophot << 16;
+    out[i].t_msec     = 0;
+    out[i].extID      = 0;
+    out[i].objID      = 0;
+    out[i].catID      = 0;
+    out[i].Mxx 	      = 0.0;
+    out[i].Mxy 	      = 0.0;
+    out[i].Myy        = 0.0;
+    out[i].posangle   = 0;
+    out[i].pltscale   = 0;
+    out[i].psfNdof    = 0;
+    out[i].psfNpix    = 0;
   }
   return (out);
@@ -64,5 +79,4 @@
     out[i].dM         = in[i].dM;
     out[i].Mcal       = in[i].Mcal;
-    out[i].Mgal       = in[i].Map;
     out[i].airmass    = in[i].airmass;
     out[i].az         = in[i].az;
@@ -71,21 +85,28 @@
     out[i].FWy 	      = in[i].FWy;
     out[i].theta      = in[i].theta;
-    out[i].dophot     = in[i].dophot;
     out[i].photcode   = in[i].photcode;
     out[i].t          = in[i].t;
     out[i].averef     = in[i].averef;
-    out[i].flags      = in[i].dbFlags;
     out[i].Xccd       = in[i].Xccd;
     out[i].Yccd       = in[i].Yccd;
     out[i].dXccd      = in[i].dXccd;
     out[i].dYccd      = in[i].dYccd;
-    out[i].stargal    = in[i].stargal;
     out[i].Sky        = in[i].Sky;
     out[i].dSky       = in[i].dSky;
-    out[i].qPSF       = isnan(in[i].qPSF) ? NAN_S_SHORT : in[i].qPSF;
+
+    // changed or added for PS1_DEV_1 (2008.02.26)
+    out[i].flags      = in[i].dbFlags;
+    out[i].psfQual    = isnan(in[i].psfQual) ? NAN_S_SHORT : in[i].psfQual;
     out[i].detID_hi   = 0;
     out[i].detID_lo   = in[i].detID;
     out[i].imageID_hi = 0;
     out[i].imageID_lo = in[i].imageID;
+
+    // changed or added for PS1_DEV_2
+    out[i].Mgal       = in[i].Map;
+
+    // changed or added for PS1_V1
+    // out[i].photFlags  = in[i].photFlags & 0x0000ffff; (only dophot pre PS1_DEV_1)
+    out[i].dophot     = in[i].photFlags >> 16;
   }
   return (out);
@@ -105,9 +126,4 @@
     out[i].D        	 = in[i].D;      
     out[i].Xp       	 = in[i].Xp;     
-    out[i].Nmeasure      = in[i].Nm;     
-    out[i].Nmissing      = in[i].Nn;     
-    out[i].code          = in[i].code;   
-    out[i].measureOffset = in[i].offset; 
-    out[i].missingOffset = in[i].missing;
     out[i].dR       	 = in[i].dR;
     out[i].dD       	 = in[i].dD;
@@ -120,4 +136,18 @@
     out[i].objID 	 = in[i].objID;
     out[i].catID 	 = in[i].catID;
+
+    // changed or added for PS1_DEV_2
+    out[i].Nmeasure      = in[i].Nm;     
+    out[i].Nmissing      = in[i].Nn;     
+    out[i].measureOffset = in[i].offset; 
+    out[i].missingOffset = in[i].missing;
+    out[i].Nextend       = 0;
+    out[i].extendOffset  = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags    	 = in[i].code;   
+    out[i].ChiSq       	 = 0.0;
+    out[i].Npos       	 = 0.0;
+    out[i].extID 	 = 0;
   }
   return (out);
@@ -137,9 +167,4 @@
     out[i].D        = in[i].D;      
     out[i].Xp       = in[i].Xp;     
-    out[i].Nm       = in[i].Nmeasure;     
-    out[i].Nn       = in[i].Nmissing;     
-    out[i].code     = in[i].code;   
-    out[i].offset   = in[i].measureOffset; 
-    out[i].missing  = in[i].missingOffset;
     out[i].dR       = in[i].dR;
     out[i].dD       = in[i].dD;
@@ -152,4 +177,13 @@
     out[i].objID    = in[i].objID;
     out[i].catID    = in[i].catID;
+
+    // changed or added for PS1_DEV_2
+    out[i].Nm       = in[i].Nmeasure;     
+    out[i].Nn       = in[i].Nmissing;     
+    out[i].offset   = in[i].measureOffset; 
+    out[i].missing  = in[i].missingOffset;
+
+    // changed or added for PS1_V1
+    out[i].code          = in[i].flags;   
   }
   return (out);
@@ -170,4 +204,8 @@
     out[i].Ncode = in[i].Ncode;
     out[i].Nused = in[i].Nused;
+
+    // changed or added for PS1_V1
+    out[i].M_20  = 0;      
+    out[i].M_80  = 0;      
  }
   return (out);
@@ -226,7 +264,5 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
-    out[i].imageID	    = in[i].imageID_lo;
 
     out[i].order	    = in[i].order;
@@ -245,4 +281,15 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    // changed or added for PS1_DEV_1
+    out[i].imageID	    = in[i].imageID_lo;
+
+    // changed or added for PS1_DEV_2
+    out[i].externID	    = 0;
+    out[i].sourceID	    = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags	    = in[i].code;
+    out[i].parentID	    = 0;
   }
   return (out);
@@ -283,8 +330,5 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
-    out[i].imageID_hi	    = 0;
-    out[i].imageID_lo	    = in[i].imageID;
 
     out[i].order	    = in[i].order;
@@ -303,5 +347,12 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
-  }
-  return (out);
-}
+
+    // changed or added for PS1_DEV_1
+    out[i].imageID_hi	    = 0;
+    out[i].imageID_lo	    = in[i].imageID;
+
+    // changed or added for PS1_V1
+    out[i].code		    = in[i].flags;
+  }
+  return (out);
+}
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_1.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_1.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_1.c	(revision 21508)
@@ -16,7 +16,4 @@
     out[i].Mcal       = in[i].Mcal;
 
-    // 2008.02.26 : I've renamed Mgal to Map, and intend it to be used per aperture
-    // magnitudes.  Most uses of Mgal in the past were actually aperture (isophotal) mags
-    out[i].Map        = in[i].Mgal;
     out[i].airmass    = in[i].airmass;
     out[i].az         = in[i].az;
@@ -25,25 +22,43 @@
     out[i].FWy 	      = in[i].FWy;
     out[i].theta      = in[i].theta;
-    out[i].dophot     = in[i].dophot;
     out[i].photcode   = in[i].photcode;
     out[i].t          = in[i].t;
     out[i].averef     = in[i].averef;
-    out[i].dbFlags    = in[i].flags;
     out[i].Xccd       = in[i].Xccd;
     out[i].Yccd       = in[i].Yccd;
     out[i].dXccd      = in[i].dXccd;
     out[i].dYccd      = in[i].dYccd;
-    out[i].stargal    = in[i].stargal;
     out[i].Sky        = in[i].Sky;
     out[i].dSky       = in[i].dSky;
-    out[i].qPSF       = (in[i].qPSF == NAN_S_SHORT) ? NAN : in[i].qPSF;
+
+    // 2008.02.26 : I've renamed Mgal to Map, and intend it to be used per aperture
+    // magnitudes.  Most uses of Mgal in the past were actually aperture (isophotal) mags
+
+    // changed or added for PS1_DEV_1 (2008.02.26)
+    out[i].psfQual    = (in[i].psfQual == NAN_S_SHORT) ? NAN : in[i].psfQual;
+    out[i].dbFlags    = in[i].flags;
     out[i].detID      = in[i].detID_lo;
     out[i].imageID    = in[i].imageID_lo;
-
-    // these don't have a correspondence
-    out[i].psfChisq  = 0;
-    out[i].crNsigma  = 0;
-    out[i].extNsigma = 0;
-    out[i].photFlags = 0;
+    out[i].psfChisq   = 0;
+    out[i].crNsigma   = 0;
+    out[i].extNsigma  = 0;
+
+    // changed or added for PS1_DEV_2
+    out[i].Map        = in[i].Mgal;
+    out[i].dMcal      = 0; 
+
+    // changed or added for PS1_V1
+    out[i].photFlags  = in[i].dophot << 16;
+    out[i].t_msec     = 0;
+    out[i].extID      = 0;
+    out[i].objID      = 0;
+    out[i].catID      = 0;
+    out[i].Mxx 	      = 0.0;
+    out[i].Mxy 	      = 0.0;
+    out[i].Myy        = 0.0;
+    out[i].posangle   = 0;
+    out[i].pltscale   = 0;
+    out[i].psfNdof    = 0;
+    out[i].psfNpix    = 0;
   }
   return (out);
@@ -64,5 +79,4 @@
     out[i].dM         = in[i].dM;
     out[i].Mcal       = in[i].Mcal;
-    out[i].Mgal       = in[i].Map;
     out[i].airmass    = in[i].airmass;
     out[i].az         = in[i].az;
@@ -71,21 +85,28 @@
     out[i].FWy 	      = in[i].FWy;
     out[i].theta      = in[i].theta;
-    out[i].dophot     = in[i].dophot;
     out[i].photcode   = in[i].photcode;
     out[i].t          = in[i].t;
     out[i].averef     = in[i].averef;
-    out[i].flags      = in[i].dbFlags;
     out[i].Xccd       = in[i].Xccd;
     out[i].Yccd       = in[i].Yccd;
     out[i].dXccd      = in[i].dXccd;
     out[i].dYccd      = in[i].dYccd;
-    out[i].stargal    = in[i].stargal;
     out[i].Sky        = in[i].Sky;
     out[i].dSky       = in[i].dSky;
-    out[i].qPSF       = isnan(in[i].qPSF) ? NAN_S_SHORT : in[i].qPSF;
+
+    // changed or added for PS1_DEV_1 (2008.02.26)
+    out[i].flags      = in[i].dbFlags;
+    out[i].psfQual    = isnan(in[i].psfQual) ? NAN_S_SHORT : in[i].psfQual;
     out[i].detID_hi   = 0;
     out[i].detID_lo   = in[i].detID;
     out[i].imageID_hi = 0;
     out[i].imageID_lo = in[i].imageID;
+
+    // changed or added for PS1_DEV_2
+    out[i].Mgal       = in[i].Map;
+
+    // changed or added for PS1_V1
+    // out[i].photFlags  = in[i].photFlags & 0x0000ffff; (only dophot pre PS1_DEV_1)
+    out[i].dophot     = in[i].photFlags >> 16;
   }
   return (out);
@@ -105,9 +126,4 @@
     out[i].D        	 = in[i].D;      
     out[i].Xp       	 = in[i].Xp;     
-    out[i].Nmeasure      = in[i].Nm;     
-    out[i].Nmissing      = in[i].Nn;     
-    out[i].code     	 = in[i].code;   
-    out[i].measureOffset = in[i].offset; 
-    out[i].missingOffset = in[i].missing;
     out[i].dR       	 = in[i].dR;
     out[i].dD       	 = in[i].dD;
@@ -120,4 +136,18 @@
     out[i].objID 	 = in[i].objID;
     out[i].catID 	 = in[i].catID;
+
+    // changed or added for PS1_DEV_2
+    out[i].Nmeasure      = in[i].Nm;     
+    out[i].Nmissing      = in[i].Nn;     
+    out[i].measureOffset = in[i].offset; 
+    out[i].missingOffset = in[i].missing;
+    out[i].Nextend       = 0;
+    out[i].extendOffset  = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags    	 = in[i].code;   
+    out[i].ChiSq       	 = 0.0;
+    out[i].Npos       	 = 0.0;
+    out[i].extID 	 = 0;
   }
   return (out);
@@ -137,9 +167,4 @@
     out[i].D        = in[i].D;      
     out[i].Xp       = in[i].Xp;     
-    out[i].Nm       = in[i].Nmeasure;     
-    out[i].Nn       = in[i].Nmissing;     
-    out[i].code     = in[i].code;   
-    out[i].offset   = in[i].measureOffset; 
-    out[i].missing  = in[i].missingOffset;
     out[i].dR       = in[i].dR;
     out[i].dD       = in[i].dD;
@@ -152,4 +177,13 @@
     out[i].objID    = in[i].objID;
     out[i].catID    = in[i].catID;
+
+    // changed or added for PS1_DEV_2
+    out[i].Nm       = in[i].Nmeasure;     
+    out[i].Nn       = in[i].Nmissing;     
+    out[i].offset   = in[i].measureOffset; 
+    out[i].missing  = in[i].missingOffset;
+
+    // changed or added for PS1_V1
+    out[i].code          = in[i].flags;   
   }
   return (out);
@@ -170,4 +204,8 @@
     out[i].Ncode = in[i].Ncode;
     out[i].Nused = in[i].Nused;
+
+    // changed or added for PS1_V1
+    out[i].M_20  = 0;      
+    out[i].M_80  = 0;      
  }
   return (out);
@@ -226,7 +264,5 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
-    out[i].imageID	    = in[i].imageID_lo;
 
     out[i].order	    = in[i].order;
@@ -245,4 +281,15 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    // changed or added for PS1_DEV_1
+    out[i].imageID	    = in[i].imageID_lo;
+
+    // changed or added for PS1_DEV_2
+    out[i].externID	    = 0;
+    out[i].sourceID	    = 0;
+
+    // changed or added for PS1_V1
+    out[i].flags	    = in[i].code;
+    out[i].parentID	    = 0;
   }
   return (out);
@@ -283,8 +330,5 @@
     out[i].fwhm_y	    = in[i].fwhm_y;
     out[i].trate	    = in[i].trate;
-    out[i].code		    = in[i].code;
     out[i].ccdnum	    = in[i].ccdnum;
-    out[i].imageID_hi	    = 0;
-    out[i].imageID_lo	    = in[i].imageID;
 
     out[i].order	    = in[i].order;
@@ -303,5 +347,15 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
-  }
-  return (out);
-}
+
+    // changed or added for PS1_DEV_1
+    out[i].imageID_hi	    = 0;
+    out[i].imageID_lo	    = in[i].imageID;
+
+    // changed or added for PS1_V1
+    out[i].code		    = in[i].flags;
+  }
+  return (out);
+}
+
+// XXX no photcode conversions?  this may be from before we had a photcode FITS table...
+
Index: /trunk/Ohana/src/libdvo/src/dvo_image.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_image.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_image.c	(revision 21508)
@@ -196,4 +196,5 @@
   if (db[0].format == DVO_FORMAT_PS1_DEV_2)       gfits_modify (&db[0].header, "FORMAT", "%s", 1, "PS1_DEV_2");
   if (db[0].format == DVO_FORMAT_PS1_DEV_3)       gfits_modify (&db[0].header, "FORMAT", "%s", 1, "PS1_DEV_3");
+  if (db[0].format == DVO_FORMAT_PS1_V1)          gfits_modify (&db[0].header, "FORMAT", "%s", 1, "PS1_V1");
   
   return;
Index: /trunk/Ohana/src/libdvo/src/dvo_image_raw.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_image_raw.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvo_image_raw.c	(revision 21508)
@@ -54,4 +54,6 @@
   if (db[0].format == DVO_FORMAT_PS1_DEV_1)       ImageSize = sizeof(Image_PS1_DEV_1);
   if (db[0].format == DVO_FORMAT_PS1_DEV_2)       ImageSize = sizeof(Image_PS1_DEV_2);
+  if (db[0].format == DVO_FORMAT_PS1_DEV_3)       ImageSize = sizeof(Image_PS1_DEV_3);
+  if (db[0].format == DVO_FORMAT_PS1_V1)          ImageSize = sizeof(Image_PS1_V1);
 
   /* check that filesize makes sense */
@@ -78,4 +80,6 @@
   if (db[0].format == DVO_FORMAT_PS1_DEV_1)       gfits_table_mkheader_Image_PS1_DEV_1 (&db[0].theader);
   if (db[0].format == DVO_FORMAT_PS1_DEV_2)       gfits_table_mkheader_Image_PS1_DEV_2 (&db[0].theader);
+  if (db[0].format == DVO_FORMAT_PS1_DEV_3)       gfits_table_mkheader_Image_PS1_DEV_3 (&db[0].theader);
+  if (db[0].format == DVO_FORMAT_PS1_V1)          gfits_table_mkheader_Image_PS1_V1 (&db[0].theader);
     
   /* read data from file */
Index: /trunk/Ohana/src/libdvo/src/dvosorts.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvosorts.c	(revision 21507)
+++ /trunk/Ohana/src/libdvo/src/dvosorts.c	(revision 21508)
@@ -46,16 +46,4 @@
 }
 
-void sort_stars_ra (Stars *stars, int N) {
-
-# define SWAPFUNC(A,B){ Stars tmp; tmp = stars[A]; stars[A] = stars[B]; stars[B] = tmp; }
-# define COMPARE(A,B)(stars[A].R < stars[B].R)
-
-  OHANA_SORT (N, COMPARE, SWAPFUNC);
-
-# undef SWAPFUNC
-# undef COMPARE
-
-}
-
 void sort_regions (SkyRegion *region, int N) {
 
@@ -70,2 +58,16 @@
 }
 
+# if (0)
+// deprecated along with Stars as an autocode type
+void sort_stars_ra (Stars *stars, int N) {
+
+# define SWAPFUNC(A,B){ Stars tmp; tmp = stars[A]; stars[A] = stars[B]; stars[B] = tmp; }
+# define COMPARE(A,B)(stars[A].R < stars[B].R)
+
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+
+# undef SWAPFUNC
+# undef COMPARE
+
+}
+# endif
Index: /trunk/Ohana/src/libohana/include/ohana.h
===================================================================
--- /trunk/Ohana/src/libohana/include/ohana.h	(revision 21507)
+++ /trunk/Ohana/src/libohana/include/ohana.h	(revision 21508)
@@ -6,4 +6,5 @@
 # include <time.h>
 # include <stdlib.h>
+# include <stdint.h>
 # include <string.h>
 # include <sys/socket.h>
Index: /trunk/Ohana/src/opihi/cmd.data/Makefile
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/Makefile	(revision 21507)
+++ /trunk/Ohana/src/opihi/cmd.data/Makefile	(revision 21508)
@@ -20,5 +20,5 @@
 $(SRC)/accum.$(ARCH).o		\
 $(SRC)/applyfit2d.$(ARCH).o	\
-$(SRC)/applyfit.$(ARCH).o	\
+$(SRC)/applyfit1d.$(ARCH).o	\
 $(SRC)/box.$(ARCH).o		\
 $(SRC)/book.$(ARCH).o		\
@@ -26,5 +26,5 @@
 $(SRC)/center.$(ARCH).o	\
 $(SRC)/clear.$(ARCH).o		\
-$(SRC)/clip.$(ARCH).o		\
+$(SRC)/imclip.$(ARCH).o		\
 $(SRC)/close.$(ARCH).o		\
 $(SRC)/concat.$(ARCH).o	\
@@ -44,6 +44,6 @@
 $(SRC)/fft1d.$(ARCH).o		\
 $(SRC)/fft2d.$(ARCH).o		\
+$(SRC)/fit1d.$(ARCH).o		\
 $(SRC)/fit2d.$(ARCH).o		\
-$(SRC)/fit.$(ARCH).o		\
 $(SRC)/gaussj.$(ARCH).o	\
 $(SRC)/gaussdeviate.$(ARCH).o	\
@@ -107,5 +107,5 @@
 $(SRC)/spline_apply.$(ARCH).o	\
 $(SRC)/spline_construct.$(ARCH).o \
-$(SRC)/stats.$(ARCH).o		   \
+$(SRC)/imstats.$(ARCH).o	   \
 $(SRC)/style.$(ARCH).o		   \
 $(SRC)/subraster.$(ARCH).o	   \
@@ -131,5 +131,5 @@
 $(SRC)/vroll.$(ARCH).o		   \
 $(SRC)/vsmooth.$(ARCH).o	\
-$(SRC)/vstat.$(ARCH).o		   \
+$(SRC)/vstats.$(ARCH).o		   \
 $(SRC)/wd.$(ARCH).o		   \
 $(SRC)/write_vectors.$(ARCH).o	   \
Index: unk/Ohana/src/opihi/cmd.data/applyfit.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/applyfit.c	(revision 21507)
+++ 	(revision )
@@ -1,66 +1,0 @@
-# include "data.h"
-
-int applyfit (int argc, char **argv) {
-  
-  int i, j, order;
-  char *c, name[64];
-  double *C, X;
-  opihi_flt *y;
-  Vector *xvec, *yvec;
-
-  if (argc != 3) {
-    gprint (GP_ERR, "USAGE: applyfit x y\n");
-    return (FALSE);
-  }
-
-  c = get_variable ("Cn");
-  if (c == NULL) {
-    gprint (GP_ERR, "no fit available\n");
-    return (FALSE);
-  }
-  order = atof (c);
-  free (c);
-
-  if ((xvec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
-  if ((yvec = SelectVector (argv[2], ANYVECTOR, TRUE)) == NULL) return (FALSE);    
-
-  ALLOCATE (C, double, order+1);
-  for (i = 0; i < order + 1; i++) {
-    sprintf (name, "C%d", i);
-    c = get_variable (name);
-    if (c == NULL) {
-      gprint (GP_ERR, "missing fit term %d\n", i);
-      return (FALSE);
-    }
-    C[i] = atof (c);
-    free (c);
-  }
-
-  ResetVector (yvec, OPIHI_FLT, xvec[0].Nelements);
-  bzero (yvec[0].elements.Flt, sizeof(opihi_flt)*yvec[0].Nelements);
-  y = yvec[0].elements.Flt;
-
-  if (xvec[0].type == OPIHI_FLT) {
-    opihi_flt *x = xvec[0].elements.Flt;
-    for (j = 0; j < xvec[0].Nelements; j++, x++, y++) {
-      X = 1;
-      for (i = 0; i < order + 1; i++) {
-	*y += C[i]*X;
-	X = X * (*x);
-      }
-    }
-  } else {
-    opihi_int *x = xvec[0].elements.Int;
-    for (j = 0; j < xvec[0].Nelements; j++, x++, y++) {
-      X = 1;
-      for (i = 0; i < order + 1; i++) {
-	*y += C[i]*X;
-	X = X * (*x);
-      }
-    }
-  }
-
-  free (C);
-  return (TRUE);
-
-}
Index: /trunk/Ohana/src/opihi/cmd.data/applyfit1d.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/applyfit1d.c	(revision 21508)
+++ /trunk/Ohana/src/opihi/cmd.data/applyfit1d.c	(revision 21508)
@@ -0,0 +1,66 @@
+# include "data.h"
+
+int applyfit1d (int argc, char **argv) {
+  
+  int i, j, order;
+  char *c, name[64];
+  double *C, X;
+  opihi_flt *y;
+  Vector *xvec, *yvec;
+
+  if (argc != 3) {
+    gprint (GP_ERR, "USAGE: applyfit x y\n");
+    return (FALSE);
+  }
+
+  c = get_variable ("Cn");
+  if (c == NULL) {
+    gprint (GP_ERR, "no fit available\n");
+    return (FALSE);
+  }
+  order = atof (c);
+  free (c);
+
+  if ((xvec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
+  if ((yvec = SelectVector (argv[2], ANYVECTOR, TRUE)) == NULL) return (FALSE);    
+
+  ALLOCATE (C, double, order+1);
+  for (i = 0; i < order + 1; i++) {
+    sprintf (name, "C%d", i);
+    c = get_variable (name);
+    if (c == NULL) {
+      gprint (GP_ERR, "missing fit term %d\n", i);
+      return (FALSE);
+    }
+    C[i] = atof (c);
+    free (c);
+  }
+
+  ResetVector (yvec, OPIHI_FLT, xvec[0].Nelements);
+  bzero (yvec[0].elements.Flt, sizeof(opihi_flt)*yvec[0].Nelements);
+  y = yvec[0].elements.Flt;
+
+  if (xvec[0].type == OPIHI_FLT) {
+    opihi_flt *x = xvec[0].elements.Flt;
+    for (j = 0; j < xvec[0].Nelements; j++, x++, y++) {
+      X = 1;
+      for (i = 0; i < order + 1; i++) {
+	*y += C[i]*X;
+	X = X * (*x);
+      }
+    }
+  } else {
+    opihi_int *x = xvec[0].elements.Int;
+    for (j = 0; j < xvec[0].Nelements; j++, x++, y++) {
+      X = 1;
+      for (i = 0; i < order + 1; i++) {
+	*y += C[i]*X;
+	X = X * (*x);
+      }
+    }
+  }
+
+  free (C);
+  return (TRUE);
+
+}
Index: unk/Ohana/src/opihi/cmd.data/clip.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/clip.c	(revision 21507)
+++ 	(revision )
@@ -1,73 +1,0 @@
-# include "data.h"
-
-int clip (int argc, char **argv) {
-
-  int i, Npix, DO_NAN, DO_INF, N;
-  double min, Vmin, max, Vmax, nan_val, inf_val;
-  float *in;
-  Buffer *buf;
-
-  inf_val = nan_val = min = Vmin = max = Vmax = 0;
-
-  DO_NAN = FALSE;
-  if ((N = get_argument (argc, argv, "-nan"))) {
-    remove_argument (N, &argc, argv);
-    nan_val  = atof(argv[N]);
-    remove_argument (N, &argc, argv);
-    DO_NAN = TRUE;
-  }
-
-  DO_INF = FALSE;
-  if ((N = get_argument (argc, argv, "-inf"))) {
-    remove_argument (N, &argc, argv);
-    inf_val  = atof(argv[N]);
-    remove_argument (N, &argc, argv);
-    DO_INF = TRUE;
-  }
-
-  if ((argc != 6) && (!(DO_INF || DO_NAN))) {
-    gprint (GP_ERR, "USAGE: clip (buffer) [min Vmin max Vmax] [-inf val] [-nan val]\n");
-    return (FALSE);
-  }
-
-  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
-
-  if (argc == 6) {
-    min = atof (argv[2]);
-    Vmin = atof (argv[3]);
-    max = atof (argv[4]);
-    Vmax = atof (argv[5]);
-  }
-
-  Npix = buf[0].matrix.Naxis[0]*buf[0].matrix.Naxis[1];
-  in = (float *) buf[0].matrix.buffer;
-
-  if (argc == 6) {
-    for (i = 0; i < Npix; i++, in++) {
-      if (*in < min) 
-	*in = Vmin;
-      if (*in > max)
-	*in = Vmax;
-    }
-  }
-  in = (float *) buf[0].matrix.buffer;
-  if (DO_NAN) {
-    for (i = 0; i < Npix; i++, in++) {
-      if (isnan (*in)) {
-	*in = nan_val;
-      }
-    }
-  }
-  in = (float *) buf[0].matrix.buffer;
-  if (DO_INF) {
-    for (i = 0; i < Npix; i++, in++) {
-      if (!finite (*in)) {
-	*in = inf_val;
-      }
-    }
-  }
-  return (TRUE);
-
-}
-
-
Index: unk/Ohana/src/opihi/cmd.data/fit.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/fit.c	(revision 21507)
+++ 	(revision )
@@ -1,234 +1,0 @@
-# include "data.h"
-
-int fit (int argc, char **argv) {
-  
-  double **c, **b, *s, X, Y, dY, dY2;
-  double ClipNSigma, mean, sigma, maxsigma;
-  int i, j, nterm, mterm, order, Npt, Nmask;
-  int N, Weight, Quiet, ClipNiter;
-  Vector *xvec, *yvec, *dyvec;
-  opihi_flt *x, *y, *dy, *yf, *yfit;
-  char name[64], *mask;
-
-  Quiet = FALSE;
-  if ((N = get_argument (argc, argv, "-q"))) {
-    Quiet = TRUE;
-    remove_argument (N, &argc, argv);
-  }
-  if ((N = get_argument (argc, argv, "-quiet"))) {
-    Quiet = TRUE;
-    remove_argument (N, &argc, argv);
-  }
-
-  ClipNSigma = 0;
-  ClipNiter  = 1;
-  if ((N = get_argument (argc, argv, "-clip"))) {
-    remove_argument (N, &argc, argv);
-    ClipNSigma = atof(argv[N]);
-    remove_argument (N, &argc, argv);
-    ClipNiter  = atof(argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-
-  dy = NULL;
-  dyvec = NULL;
-  Weight = FALSE;
-  if ((N = get_argument (argc, argv, "-dy"))) {
-    remove_argument (N, &argc, argv);
-    if ((dyvec = SelectVector (argv[N], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
-    remove_argument (N, &argc, argv);
-    Weight = TRUE;
-  }
-
-  if (argc != 4) {
-    gprint (GP_ERR, "USAGE: fit x y order [-dy wt] [-quiet/-q] [-clip Nsigma Niter]\n");
-    return (FALSE);
-  }
-
-  if ((xvec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
-  if ((yvec = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
-
-  if (xvec[0].Nelements != yvec[0].Nelements) {
-    gprint (GP_ERR, "vectors must have same length\n");
-    return (FALSE);
-  }
-  REQUIRE_VECTOR_FLT (xvec, FALSE); 
-  REQUIRE_VECTOR_FLT (yvec, FALSE); 
-
-  if (Weight) {
-    REQUIRE_VECTOR_FLT (dyvec, FALSE); 
-    if (xvec[0].Nelements != dyvec[0].Nelements) {
-      gprint (GP_ERR, "vectors must have same length\n");
-      return (FALSE);
-    }
-  }
- 
-
-  /* nterm is number of polynomial terms, starting at x^0 */
-  order = atof (argv[3]);
-  nterm = order + 1;
-  mterm = 2*nterm;
-
-  ALLOCATE (yfit, opihi_flt, xvec[0].Nelements);
-  ALLOCATE (mask, char, xvec[0].Nelements);
-  memset (mask, 0, xvec[0].Nelements);
-
-  ALLOCATE (s, double, mterm);
-  ALLOCATE (b, double *, nterm);
-  ALLOCATE (c, double *, nterm);
-  for (i = 0; i < nterm; i++) {
-    ALLOCATE (c[i], double, nterm);
-    ALLOCATE (b[i], double, 1);
-  }
-
-  Nmask = 0;
-  sigma = 0.0;
-
-  for (N = 0; N < ClipNiter; N++) {
-
-    /* init registers for current pass */
-    memset (s, 0, mterm*sizeof(double));
-    for (i = 0; i < nterm; i++) {
-      memset (c[i], 0, nterm*sizeof(double));
-      memset (b[i], 0, sizeof(double));
-    }
-
-    /* perform linear fit */
-    x = xvec[0].elements.Flt;
-    y = yvec[0].elements.Flt;
-    if (Weight) dy = dyvec[0].elements.Flt;
-
-    for (i = 0; i < xvec[0].Nelements; i++, x++, y++) {
-      if (mask[i]) continue;
-      if (!(finite(*x) && finite(*y))) continue;
-      dY = 1.0;
-      if (Weight) { 
-	dY = 1.0 / SQ(*dy);
-	dy ++;
-      }
-      X = 1*dY;
-      Y = *y*dY;
-      for (j = 0; j < nterm; j++) {
-	s[j] += X;
-	b[j][0] += Y;
-	X = X * (*x);
-	Y = Y * (*x);
-      }
-      for (j = nterm; j < mterm; j++) {
-	s[j] += X;
-	X = X * (*x);
-      }
-    }
-    for (i = 0; i < nterm; i++) {
-      for (j = 0; j < nterm; j++) {
-	c[i][j] = s[i + j];
-      }
-    }
-    if (!dgaussjordan (c, b, nterm, 1)) goto escape;
-
-    /* generate fitted values */
-    x = xvec[0].elements.Flt;
-    yf = yfit;
-    for (i = 0; i < xvec[0].Nelements; i++, x++, yf++) {
-      if (!finite(*x)) continue;
-      *yf = 0;
-      X = 1;
-      for (j = 0; j < order + 1; j++) {
-	*yf += b[j][0]*X;
-	X = X * (*x);
-      }
-    }
-
-    /* measure fit residual scatter */
-    x  = xvec[0].elements.Flt;
-    y  = yvec[0].elements.Flt;
-    yf = yfit;
-    dY = dY2 = 0;
-    for (i = Npt = 0; i < xvec[0].Nelements; i++, x++, y++, yf++) {
-      if (mask[i]) continue;
-      if (!finite(*x)) continue;
-      dY  += (*y - *yf);
-      dY2 += SQ(*y - *yf);
-      Npt ++;
-    }
-    mean  = dY / Npt;
-    sigma = sqrt (fabs(dY2/Npt - SQ(mean)));
-    maxsigma = ClipNSigma * sigma;
-
-    /* mask outlier points */
-    x  = xvec[0].elements.Flt;
-    y  = yvec[0].elements.Flt;
-    yf = yfit;
-    Nmask = 0;
-    for (i = 0; ClipNSigma && (i < xvec[0].Nelements); i++, x++, y++, yf++) {
-      dY = (*y - *yf);
-      if (fabs(dY) > maxsigma) {
-	mask[i] = TRUE;
-	Nmask ++;
-      } else {
-	mask[i] = FALSE;
-      }	
-    }
-  }
-      
-  /* print & save basic fit parameters */
-  if (!Quiet) gprint (GP_ERR, "y = ");
-  for (i = 0; i < nterm; i++) {
-    sprintf (name, "C%d", i);
-    set_variable (name, b[i][0]);
-    if (!Quiet) gprint (GP_ERR, "%f x^%d ", b[i][0], i);
-  }
-  sprintf (name, "Cn");
-  set_variable (name, (double) order);
-  
-  /* print & save basic fit parameters */
-  if (!Quiet) gprint (GP_ERR, "\n");
-  if (!Quiet) gprint (GP_ERR, "    ");
-  for (i = 0; i < nterm; i++) {
-    sprintf (name, "dC%d", i);
-    set_variable (name, sqrt(c[i][i]));
-    if (!Quiet) gprint (GP_ERR, "%f     ", sqrt(c[i][i]));
-  }
-  if (!Quiet) gprint (GP_ERR, "\n");
-
-  set_variable ("dC", sigma);
-  set_variable ("Cnv", (xvec[0].Nelements - Nmask));
-
-  /* save mask and yfit for testing? */
-  if (1) { 
-    Vector *fvec, *mvec;
-    if ((fvec = SelectVector ("yfit", ANYVECTOR, TRUE)) == NULL) return (FALSE);    
-    if ((mvec = SelectVector ("mask", ANYVECTOR, TRUE)) == NULL) return (FALSE);    
-    free (fvec[0].elements.Flt);
-    fvec[0].elements.Flt = yfit;
-    fvec[0].Nelements = xvec[0].Nelements;
-    mvec[0].Nelements = xvec[0].Nelements;
-
-    REALLOCATE (mvec[0].elements.Flt, opihi_flt, xvec[0].Nelements);
-    for (i = 0; i < xvec[0].Nelements; i++) {
-      mvec[0].elements.Flt[i] = mask[i];
-    }
-  } else {
-    free (yfit);
-  }
-  free (mask);
-
-  for (i = 0; i < nterm; i++) {
-    free (b[i]);
-    free (c[i]);
-  }
-  free (b);
-  free (c);
-  free (s);
-  return (TRUE);
-
-escape:
-  for (i = 0; i < nterm; i++) {
-    free (b[i]);
-    free (c[i]);
-  }
-  free (b);
-  free (c);
-  free (s);
-  return (FALSE);
-}
Index: /trunk/Ohana/src/opihi/cmd.data/fit1d.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/fit1d.c	(revision 21508)
+++ /trunk/Ohana/src/opihi/cmd.data/fit1d.c	(revision 21508)
@@ -0,0 +1,234 @@
+# include "data.h"
+
+int fit1d (int argc, char **argv) {
+  
+  double **c, **b, *s, X, Y, dY, dY2;
+  double ClipNSigma, mean, sigma, maxsigma;
+  int i, j, nterm, mterm, order, Npt, Nmask;
+  int N, Weight, Quiet, ClipNiter;
+  Vector *xvec, *yvec, *dyvec;
+  opihi_flt *x, *y, *dy, *yf, *yfit;
+  char name[64], *mask;
+
+  Quiet = FALSE;
+  if ((N = get_argument (argc, argv, "-q"))) {
+    Quiet = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-quiet"))) {
+    Quiet = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  ClipNSigma = 0;
+  ClipNiter  = 1;
+  if ((N = get_argument (argc, argv, "-clip"))) {
+    remove_argument (N, &argc, argv);
+    ClipNSigma = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+    ClipNiter  = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  dy = NULL;
+  dyvec = NULL;
+  Weight = FALSE;
+  if ((N = get_argument (argc, argv, "-dy"))) {
+    remove_argument (N, &argc, argv);
+    if ((dyvec = SelectVector (argv[N], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
+    remove_argument (N, &argc, argv);
+    Weight = TRUE;
+  }
+
+  if (argc != 4) {
+    gprint (GP_ERR, "USAGE: fit x y order [-dy wt] [-quiet/-q] [-clip Nsigma Niter]\n");
+    return (FALSE);
+  }
+
+  if ((xvec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
+  if ((yvec = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);    
+
+  if (xvec[0].Nelements != yvec[0].Nelements) {
+    gprint (GP_ERR, "vectors must have same length\n");
+    return (FALSE);
+  }
+  REQUIRE_VECTOR_FLT (xvec, FALSE); 
+  REQUIRE_VECTOR_FLT (yvec, FALSE); 
+
+  if (Weight) {
+    REQUIRE_VECTOR_FLT (dyvec, FALSE); 
+    if (xvec[0].Nelements != dyvec[0].Nelements) {
+      gprint (GP_ERR, "vectors must have same length\n");
+      return (FALSE);
+    }
+  }
+ 
+
+  /* nterm is number of polynomial terms, starting at x^0 */
+  order = atof (argv[3]);
+  nterm = order + 1;
+  mterm = 2*nterm;
+
+  ALLOCATE (yfit, opihi_flt, xvec[0].Nelements);
+  ALLOCATE (mask, char, xvec[0].Nelements);
+  memset (mask, 0, xvec[0].Nelements);
+
+  ALLOCATE (s, double, mterm);
+  ALLOCATE (b, double *, nterm);
+  ALLOCATE (c, double *, nterm);
+  for (i = 0; i < nterm; i++) {
+    ALLOCATE (c[i], double, nterm);
+    ALLOCATE (b[i], double, 1);
+  }
+
+  Nmask = 0;
+  sigma = 0.0;
+
+  for (N = 0; N < ClipNiter; N++) {
+
+    /* init registers for current pass */
+    memset (s, 0, mterm*sizeof(double));
+    for (i = 0; i < nterm; i++) {
+      memset (c[i], 0, nterm*sizeof(double));
+      memset (b[i], 0, sizeof(double));
+    }
+
+    /* perform linear fit */
+    x = xvec[0].elements.Flt;
+    y = yvec[0].elements.Flt;
+    if (Weight) dy = dyvec[0].elements.Flt;
+
+    for (i = 0; i < xvec[0].Nelements; i++, x++, y++) {
+      if (mask[i]) continue;
+      if (!(finite(*x) && finite(*y))) continue;
+      dY = 1.0;
+      if (Weight) { 
+	dY = 1.0 / SQ(*dy);
+	dy ++;
+      }
+      X = 1*dY;
+      Y = *y*dY;
+      for (j = 0; j < nterm; j++) {
+	s[j] += X;
+	b[j][0] += Y;
+	X = X * (*x);
+	Y = Y * (*x);
+      }
+      for (j = nterm; j < mterm; j++) {
+	s[j] += X;
+	X = X * (*x);
+      }
+    }
+    for (i = 0; i < nterm; i++) {
+      for (j = 0; j < nterm; j++) {
+	c[i][j] = s[i + j];
+      }
+    }
+    if (!dgaussjordan (c, b, nterm, 1)) goto escape;
+
+    /* generate fitted values */
+    x = xvec[0].elements.Flt;
+    yf = yfit;
+    for (i = 0; i < xvec[0].Nelements; i++, x++, yf++) {
+      if (!finite(*x)) continue;
+      *yf = 0;
+      X = 1;
+      for (j = 0; j < order + 1; j++) {
+	*yf += b[j][0]*X;
+	X = X * (*x);
+      }
+    }
+
+    /* measure fit residual scatter */
+    x  = xvec[0].elements.Flt;
+    y  = yvec[0].elements.Flt;
+    yf = yfit;
+    dY = dY2 = 0;
+    for (i = Npt = 0; i < xvec[0].Nelements; i++, x++, y++, yf++) {
+      if (mask[i]) continue;
+      if (!finite(*x)) continue;
+      dY  += (*y - *yf);
+      dY2 += SQ(*y - *yf);
+      Npt ++;
+    }
+    mean  = dY / Npt;
+    sigma = sqrt (fabs(dY2/Npt - SQ(mean)));
+    maxsigma = ClipNSigma * sigma;
+
+    /* mask outlier points */
+    x  = xvec[0].elements.Flt;
+    y  = yvec[0].elements.Flt;
+    yf = yfit;
+    Nmask = 0;
+    for (i = 0; ClipNSigma && (i < xvec[0].Nelements); i++, x++, y++, yf++) {
+      dY = (*y - *yf);
+      if (fabs(dY) > maxsigma) {
+	mask[i] = TRUE;
+	Nmask ++;
+      } else {
+	mask[i] = FALSE;
+      }	
+    }
+  }
+      
+  /* print & save basic fit parameters */
+  if (!Quiet) gprint (GP_ERR, "y = ");
+  for (i = 0; i < nterm; i++) {
+    sprintf (name, "C%d", i);
+    set_variable (name, b[i][0]);
+    if (!Quiet) gprint (GP_ERR, "%f x^%d ", b[i][0], i);
+  }
+  sprintf (name, "Cn");
+  set_variable (name, (double) order);
+  
+  /* print & save basic fit parameters */
+  if (!Quiet) gprint (GP_ERR, "\n");
+  if (!Quiet) gprint (GP_ERR, "    ");
+  for (i = 0; i < nterm; i++) {
+    sprintf (name, "dC%d", i);
+    set_variable (name, sqrt(c[i][i]));
+    if (!Quiet) gprint (GP_ERR, "%f     ", sqrt(c[i][i]));
+  }
+  if (!Quiet) gprint (GP_ERR, "\n");
+
+  set_variable ("dC", sigma);
+  set_variable ("Cnv", (xvec[0].Nelements - Nmask));
+
+  /* save mask and yfit for testing? */
+  if (1) { 
+    Vector *fvec, *mvec;
+    if ((fvec = SelectVector ("yfit", ANYVECTOR, TRUE)) == NULL) return (FALSE);    
+    if ((mvec = SelectVector ("mask", ANYVECTOR, TRUE)) == NULL) return (FALSE);    
+    free (fvec[0].elements.Flt);
+    fvec[0].elements.Flt = yfit;
+    fvec[0].Nelements = xvec[0].Nelements;
+    mvec[0].Nelements = xvec[0].Nelements;
+
+    REALLOCATE (mvec[0].elements.Flt, opihi_flt, xvec[0].Nelements);
+    for (i = 0; i < xvec[0].Nelements; i++) {
+      mvec[0].elements.Flt[i] = mask[i];
+    }
+  } else {
+    free (yfit);
+  }
+  free (mask);
+
+  for (i = 0; i < nterm; i++) {
+    free (b[i]);
+    free (c[i]);
+  }
+  free (b);
+  free (c);
+  free (s);
+  return (TRUE);
+
+escape:
+  for (i = 0; i < nterm; i++) {
+    free (b[i]);
+    free (c[i]);
+  }
+  free (b);
+  free (c);
+  free (s);
+  return (FALSE);
+}
Index: /trunk/Ohana/src/opihi/cmd.data/imclip.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/imclip.c	(revision 21508)
+++ /trunk/Ohana/src/opihi/cmd.data/imclip.c	(revision 21508)
@@ -0,0 +1,73 @@
+# include "data.h"
+
+int imclip (int argc, char **argv) {
+
+  int i, Npix, DO_NAN, DO_INF, N;
+  double min, Vmin, max, Vmax, nan_val, inf_val;
+  float *in;
+  Buffer *buf;
+
+  inf_val = nan_val = min = Vmin = max = Vmax = 0;
+
+  DO_NAN = FALSE;
+  if ((N = get_argument (argc, argv, "-nan"))) {
+    remove_argument (N, &argc, argv);
+    nan_val  = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+    DO_NAN = TRUE;
+  }
+
+  DO_INF = FALSE;
+  if ((N = get_argument (argc, argv, "-inf"))) {
+    remove_argument (N, &argc, argv);
+    inf_val  = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+    DO_INF = TRUE;
+  }
+
+  if ((argc != 6) && (!(DO_INF || DO_NAN))) {
+    gprint (GP_ERR, "USAGE: clip (buffer) [min Vmin max Vmax] [-inf val] [-nan val]\n");
+    return (FALSE);
+  }
+
+  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
+
+  if (argc == 6) {
+    min = atof (argv[2]);
+    Vmin = atof (argv[3]);
+    max = atof (argv[4]);
+    Vmax = atof (argv[5]);
+  }
+
+  Npix = buf[0].matrix.Naxis[0]*buf[0].matrix.Naxis[1];
+  in = (float *) buf[0].matrix.buffer;
+
+  if (argc == 6) {
+    for (i = 0; i < Npix; i++, in++) {
+      if (*in < min) 
+	*in = Vmin;
+      if (*in > max)
+	*in = Vmax;
+    }
+  }
+  in = (float *) buf[0].matrix.buffer;
+  if (DO_NAN) {
+    for (i = 0; i < Npix; i++, in++) {
+      if (isnan (*in)) {
+	*in = nan_val;
+      }
+    }
+  }
+  in = (float *) buf[0].matrix.buffer;
+  if (DO_INF) {
+    for (i = 0; i < Npix; i++, in++) {
+      if (!finite (*in)) {
+	*in = inf_val;
+      }
+    }
+  }
+  return (TRUE);
+
+}
+
+
Index: /trunk/Ohana/src/opihi/cmd.data/imstats.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/imstats.c	(revision 21508)
+++ /trunk/Ohana/src/opihi/cmd.data/imstats.c	(revision 21508)
@@ -0,0 +1,125 @@
+# include "data.h"
+
+int imstats (int argc, char **argv) {
+  
+  int i, j, Nmode, Imode;
+  double Npix, N1, N2, max, min, range, median, mode, IgnoreValue;
+  float *V;
+  int sx, sy, nx, ny, *hist, Nhist, bin;
+  int Ignore, Quiet, N;
+  Buffer *buf;
+
+  IgnoreValue = 0;
+  Ignore = FALSE;
+  if ((N = get_argument (argc, argv, "-ignore"))) {
+    Ignore = TRUE;
+    remove_argument (N, &argc, argv);
+    IgnoreValue = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  Quiet = FALSE;
+  if ((N = get_argument (argc, argv, "-q"))) {
+    Quiet = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-quiet"))) {
+    Quiet = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  if ((argc != 2) && (argc != 6)) {
+    gprint (GP_ERR, "USAGE: stats <buffer> sx sy nx ny\n");
+    gprint (GP_ERR, "OR:    stats <buffer>\n");
+    return (FALSE);
+  }
+
+  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
+
+  if (argc == 6) {
+    sx = strcmp (argv[2], "-") ? atof (argv[2]) : 0;
+    sy = strcmp (argv[3], "-") ? atof (argv[3]) : 0;
+    nx = strcmp (argv[4], "-") ? atof (argv[4]) : buf[0].matrix.Naxis[0];
+    ny = strcmp (argv[5], "-") ? atof (argv[5]) : buf[0].matrix.Naxis[1];
+  } else {
+    sx = 0;
+    sy = 0;
+    nx = buf[0].matrix.Naxis[0];
+    ny = buf[0].matrix.Naxis[1];
+  }
+
+  Npix = N1 = N2 = 0;
+  if ((sx < 0) || (sy < 0) || 
+      (sx+nx > buf[0].matrix.Naxis[0]) || 
+      (sy+ny > buf[0].matrix.Naxis[1])) {
+    gprint (GP_ERR, "region out of range\n");
+    return (FALSE);
+  }
+
+  max = -1e32;
+  min = +1e32;
+  for (j = sy; j < sy + ny; j++) {
+    V = (float *)(buf[0].matrix.buffer) + j*buf[0].matrix.Naxis[0] + sx; 
+    for (i = 0; i < nx; i++, V++) {
+      if (Ignore && (fabs (*V - IgnoreValue) < 1e-8)) continue;
+      if (isnan(*V)) continue;
+      if (isinf(*V)) continue;
+      N1 += *V;
+      N2 += (*V)*(*V);
+      Npix += 1.0;
+      max = MAX (max, *V);
+      min = MIN (min, *V);
+    }
+  }
+  N1 = N1 / Npix;
+
+/* calculate mode, median */
+  median = mode = 0.5*(max + min);
+  if ((max - min) != 0) {
+    range = 0xffff / (max - min);
+    ALLOCATE (hist, int, 0x10000);
+    bzero (hist, 0x10000*sizeof(int));
+    for (j = sy; j < sy + ny; j++) {
+      V = (float *)(buf[0].matrix.buffer) + j*buf[0].matrix.Naxis[0] + sx; 
+      for (i = 0; i < nx; i++, V++) {
+	if (Ignore && (fabs (*V - IgnoreValue) < 1e-8)) continue;
+	if (isnan(*V)) continue;
+	if (isinf(*V)) continue;
+	bin = MIN (MAX (0, (*V - min) * range), 0xffff);
+	hist[bin] ++;
+      }
+    }
+    Nhist = 0;
+    for (i = 0; (i < 0xffff) && (Nhist < 0.5*Npix); i++) 
+      Nhist += hist[i];
+    median = i / range + min;
+    Nmode = hist[0];
+    Imode = 0;
+    for (i = 1; i < 0x10000; i++) {
+      if (hist[i] > Nmode) {
+	Nmode = hist[i];
+	Imode = i;
+      }
+    }
+    mode = Imode / range + min;
+    free (hist);
+  }  
+  
+  if (!Quiet) {
+    gprint (GP_LOG, "  mean    stdev    min     max   median   Npix   Total\n");
+    gprint (GP_LOG, "%7.4g %7.4g %7.4g %7.4g %7.4g %7.0f %7.4g\n", N1, sqrt (N2/Npix - N1*N1), 
+	    min, max, median, Npix, Npix*N1);
+  }
+
+  set_variable ("MIN",    min);
+  set_variable ("MAX",    max);
+  set_variable ("MEDIAN", median);
+  set_variable ("MEAN",   N1);
+  set_variable ("MODE",   mode);
+  set_variable ("TOTAL",  N1*Npix);
+  set_variable ("NPIX",   Npix);
+  set_variable ("SIGMA",  sqrt (N2/Npix - N1*N1));
+
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/opihi/cmd.data/init.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/init.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/cmd.data/init.c	(revision 21508)
@@ -3,4 +3,5 @@
 int accum            PROTO((int, char **));
 int applyfit         PROTO((int, char **));
+int applyfit1d       PROTO((int, char **));
 int applyfit2d       PROTO((int, char **));
 int box              PROTO((int, char **));
@@ -11,4 +12,5 @@
 int clear            PROTO((int, char **));
 int clip             PROTO((int, char **));
+int imclip           PROTO((int, char **));
 int close_device     PROTO((int, char **));
 int concat           PROTO((int, char **));
@@ -29,6 +31,7 @@
 int fft1d            PROTO((int, char **));
 int fft2d            PROTO((int, char **));
+int fit              PROTO((int, char **));
+int fit1d            PROTO((int, char **));
 int fit2d            PROTO((int, char **));
-int fit              PROTO((int, char **));
 int gaussjordan      PROTO((int, char **));
 int gaussdeviate     PROTO((int, char **));
@@ -92,4 +95,5 @@
 int spline_construct_cmd PROTO((int, char **));
 int stats            PROTO((int, char **));
+int imstats          PROTO((int, char **));
 int style            PROTO((int, char **));
 int subraster        PROTO((int, char **));
@@ -113,5 +117,5 @@
 int vload            PROTO((int, char **));
 int vzload           PROTO((int, char **));
-int vstat            PROTO((int, char **));
+int vstats           PROTO((int, char **));
 int vroll            PROTO((int, char **));
 int vpop             PROTO((int, char **));
@@ -124,17 +128,20 @@
 static Command cmds[] = {  
   {1, "accum",        accum,            "accumulate vector values in another vector"},
-  {1, "applyfit",     applyfit,         "apply fit to new vector"},
+  {1, "applyfit",     applyfit1d,       "apply 1-d fit to new vector"},
+  {1, "applyfit1d",   applyfit1d,       "apply 1-d fit to new vector"},
   {1, "applyfit2d",   applyfit2d,       "apply 2-d fit to new vector"},
+  {1, "book",         book_command,     "commands to manipulate book/page/word data"},
   {1, "box",          box,              "draw a box on the plot"},
-  {1, "book",         book_command,     "commands to manipulate book/page/word data"},
+  {1, "buffers",      list_buffers,     "list the currently allocated buffers (images)"},
+  {1, "images",       list_buffers,     "list the currently allocated images"},
   {1, "center",       center,           "center image on coords"},
-  {1, "parity",       parity,           "set image parity"},
   {1, "circstats",    circstats,        "circular statistics"},
   {1, "clear",        clear,            "erase plot"},
-  {1, "clip",         clip,             "clip values in a buffer to be within a range"},
+  {1, "clip",         imclip,           "clip values in an image to be within a range"},
   {1, "close",        close_device,     "close the current display device"},
-  {1, "concat",       concat,           "reduce vector dimension"},
+  {1, "concat",       concat,           "append values to the end of a vector"},
   {1, "contour",      contour,          "create contour from image"},
   {1, "create",       create,           "create a new vector"},
+  {1, "vcreate",      create,           "create a new vector"},
   {1, "cumulative",   cumulative,       "build a cumulative histogram from a specific histogram"},
   {1, "cursor",       cursor,           "get coords from cursor"},
@@ -143,81 +150,87 @@
   {1, "dbconnect",    dbconnect,        "setup mysql db connection"},
   {1, "dbselect",     dbselect,         "extract vectors from mysql database table"},
-  {1, "delete",       delete,           "delete vectors or matrices"},
+  {1, "delete",       delete,           "delete vectors or images"},
   {1, "device",       device,           "set / get current graphics device"},
-  {1, "dimendown",    dimendown,        "convert matrix to vector"},
-  {1, "dimenup",      dimenup,          "convert vector to matrix"},
-  {1, "erase",        erase,            "erase objects on an overlay"},
-  {1, "extract",      extract,          "extract a portion of a buffer into another buffer"},
-  {1, "fft1d",        fft1d,            "fft on the pixel-stream in an image"},
+  {1, "dimendown",    dimendown,        "convert image to vector"},
+  {1, "dimenup",      dimenup,          "convert vector to image"},
+  {1, "dot",          dot,              "plot a single point"},
+  {1, "erase",        erase,            "erase objects on an image overlay"},
+  {1, "extract",      extract,          "extract a portion of a image into another image"},
+  {1, "fft1d",        fft1d,            "fft on a vector"},
   {1, "fft2d",        fft2d,            "fft on an image"},
-  {1, "fit",          fit,              "fit polynomial to vector pair"},
+  {1, "fit",          fit1d,            "fit polynomial to vector pair"},
+  {1, "fit1d",        fit1d,            "fit polynomial to vector pair"},
   {1, "fit2d",        fit2d,            "fit 2-d polynomial to vector triplet"},
-  {1, "gaussj",       gaussjordan,      "solve Ax = B (N-D)"},
   {1, "gaussdev",     gaussdeviate,     "generate a gaussian deviate vector"},
   {1, "gaussint",     gaussintegral,    "return the integrated gaussian vector"},
-  {1, "grid",         grid,             "plot cartesian grid"},
-  {1, "gridify",      gridify,          "convert vector triplet to buffer"},
+  {1, "gaussj",       gaussjordan,      "solve Ax = B (N-Dimensional)"},
+  {1, "grid",         grid,             "plot cartesian grid on graph"},
+  {1, "gridify",      gridify,          "convert vector triplet to image (same as vgrid?)"},
   {1, "grow",         grow,             "grow a mask"},
-  {1, "ungridify",    ungridify,        "convert buffer region to vector triplet"},
-  {1, "header",       header,           "print buffer header"},
+  {1, "header",       header,           "print image header"},
   {1, "histogram",    histogram,        "generate histogram from vector"},
+  {1, "imbin",        rebin,            "rebin image data by factor of N"},
+  {1, "imclip",       imclip,           "clip values in an image to be within a range"},
   {1, "imcut",        imcut,            "linear image cut between arbitrary coords"},
-  {1, "imhist",       imhist,           "histogram of an image region"},
+  {1, "imhistogram",  imhist,           "histogram of an image region"},
   {1, "imsmooth",     imsmooth,         "circular gaussian smoothing"},
+  {1, "imstats",      imstats,          "statistics on a portion of an image"},
   {1, "integrate",    integrate,        "integrate a vector"},
   {1, "interpolate",  interpolate,      "interpolate between vector pairs"},
-  {1, "jpeg",         jpeg,             "write text line on graph"},
-  {1, "png",          jpeg,             "write text line on graph"},
-  {1, "ppm",          jpeg,             "write text line on graph"},
+  {1, "jpeg",         jpeg,             "convert display image to JPEG"},
   {1, "kern",         kern,             "convolve with 3x3 kernel"},
-  {1, "keyword",      keyword,          "extract a FITS keyword from buffer header"},
+  {1, "keyword",      keyword,          "extract a FITS keyword from image header"},
   {1, "labels",       labels,           "define labels for plot"},
   {1, "limits",       limits,           "define plot limits"},
-  {1, "line",         line,             "plot line"},
-  {1, "buffers",      list_buffers,     "list the currently allocated buffers"},
-  {1, "vectors",      list_vectors,     "list vectors"},
-  {1, "load",         load,             "load an SAOimage style overlay"},
+  {1, "line",         line,             "plot a line"},
+  {1, "load",         load,             "load an SAOimage/DS9 style overlay"},
   {1, "lookup",       lookup,           "convert vector via lookup table (vector pair)"},
-  {1, "mkrgb",        mkrgb,            "convert 3 images to rgb jpeg"},
-  {1, "mcreate",      mcreate,          "create a matrix"},
+  {1, "mcreate",      mcreate,          "create an image"},
+  {1, "imcreate",     mcreate,          "create an image"},
   {1, "medacc",       medacc,           "accumulate vector values in another vector"},
-  {1, "mget",         mget,             "extract a vector from a matrix"},
+  {1, "mget",         mget,             "extract a vector from an image"},
+  {1, "imget",        mget,             "extract a vector from an image"},
   {1, "minterp",      minterp,          "interpolate image pixels"},
-  {1, "mset",         mset,             "insert a vector in a matrix"},
+  {1, "iminterp",     minterp,          "interpolate image pixels"},
+  {1, "mkrgb",        mkrgb,            "convert 3 images to rgb jpeg (use Kapa for better control)"},
+  {1, "imset",        mset,             "insert a vector in an image"},
+  {1, "parity",       parity,           "set image parity"},
   {1, "peak",         peak,             "find vector peak in range"},
   {1, "periodogram",  periodogram,      "measure periods in unevenly sampled data"},
   {1, "plot",         plot,             "plot a pair of vectors"},
-  {1, "dot",          dot,              "plot a single point"},
-  {1, "point",        point,            "load overlay with single point"},
-  {1, "ps",           ps,               "define labels for plot"},
+  {1, "png",          jpeg,             "convert display graphic to PNG"},
+  {1, "point",        point,            "load image overlay with a single point"},
+  {1, "ppm",          jpeg,             "convert display graphic to PPM"},
+  {1, "ps",           ps,               "convert display to PostScript"},
+  {1, "queuedelete",  queuedelete,      "delete a queue"},
+  {1, "queuedrop",    queuedrop,        "drop values from queue matching a key"},
+  {1, "queueinit",    queueinit,        "create an empty queue"},
+  {1, "queuelist",    queuelist,        "list defined queues"},
+  {1, "queueload",    queueload,        "load queue from command"},
   {1, "queuepop",     queuepop,         "pop value from queue to variable"},
-  {1, "queuedrop",    queuedrop,        "drop values from queue matching a key"},
   {1, "queueprint",   queueprint,       "print the contents of a queue"},
   {1, "queuepush",    queuepush,        "push value onto queue"},
-  {1, "queueinit",    queueinit,        "create an empty queue"},
-  {1, "queuedelete",  queuedelete,      "delete a queue"},
-  {1, "queuelist",    queuelist,        "list defined queues"},
-  {1, "queueload",    queueload,        "load queue from command"},
+  {1, "queuesize",    queuesize,        "show queue size"},
   {1, "queuesubstr",  queuesubstr,      "bulk replace strings in queue"},
-  {1, "queuesize",    queuesize,        "show queue size"},
   {1, "rd",           rd,               "load fits image"},
   {1, "rdseg",        rdseg,            "read a segment of an image from a file"},
   {1, "read",         read_vectors,     "read vectors from datafile"},
-  {1, "rebin",        rebin,            "rebin data by factor of N"},
+  {1, "rebin",        rebin,            "rebin image data by factor of N"},
   {1, "resize",       resize,           "set graphics/image window size"},
   {1, "roll",         roll,             "roll image to new start point"},
   {1, "rotate",       rotate,           "rotate image"},
-  {1, "save",         save,             "save an SAOimage style overlay"},
+  {1, "save",         save,             "save an SAOimage style image overlay"},
   {1, "section",      section,          "define section of graph"},
-  {1, "set",          set,              "vector math"},
+  {1, "select",       vect_select,      "selective vector assignment"},
+  {1, "set",          set,              "image and vector math"},
   {1, "shift",        shift,            "shift data in an image"},
   {1, "sort",         sort_vectors,     "sort list of vectors"},
   {1, "spline.apply", spline_apply_cmd, "apply spline fit to generate an image"},
   {1, "spline.const", spline_construct_cmd, "create spline 2nd deriv. terms"},
-  {1, "stats",        stats,            "give statistics on a portion of a buffer"},
+  {1, "stats",        imstats,          "statistics on a portion of an image"},
   {1, "style",        style,            "set the style for graph plots"},
   {1, "subraster",    subraster,        "subraster of fits image"},
-  {1, "subset",       subset,           "expand vector dimension"},
-  {1, "svd",          svd,              "singular value decomposition of a matrix"},
+  {1, "subset",       subset,           "generate a vector from a portion of another vector"},
+  {1, "svd",          svd,              "singular value decomposition of a matrix (image)"},
   {1, "swapbytes",    swapbytes,        "byte swap thing"},
   {1, "textline",     textline,         "write text line on graph"},
@@ -225,23 +238,25 @@
   {1, "tvchannel",    tvchannel,        "set the current tv channel"},
   {1, "tvcolors",     tvcolors,         "set the tv colormap"},
-  {1, "tvcontour",    tvcontour,        "send contour to overlay"},
-  {1, "tvgrid",       tvgrid,           "wait until return is typed"},
+  {1, "tvcontour",    tvcontour,        "send contour to image overlay"},
+  {1, "tvgrid",       tvgrid,           "RA/DEC grid on an image"},
+  {1, "ungridify",    ungridify,        "convert image region to vector triplet"},
   {1, "uniq",         uniq,             "create a uniq vector subset from a vector"},
   {1, "unsign",       unsign,           "toggle the UNSIGN status"},
-  {1, "vbin",         vbin,             "bin values in a vector to be within a range"},
+  {1, "vbin",         vbin,             "rebin vector data by a fector of N"},
   {1, "vclip",        vclip,            "clip values in a vector to be within a range"},
-  {1, "select",       vect_select,      "selective vector assignment"},
-  {1, "vgauss",       vgauss,           ""},
-  {1, "vmaxwell",     vmaxwell,         ""},
-  {1, "vgrid",        vgrid,            ""},
-  {1, "vload",        vload,            "load vectors on Kii"},
-  {1, "vzload",       vzload,           "load vectors on Kii"},
-  {1, "vstat",        vstat,            "get info from imreg database"},
-  {1, "vsmooth",      vsmooth,          "gaussian smooth of a vector"},
+  {1, "vectors",      list_vectors,     "list vectors"},
+  {1, "vgauss",       vgauss,           "fit a Gaussian to a vector"},
+  {1, "vgrid",        vgrid,            "generate an image from a triplet of vectors"},
+  {1, "vhistogram",   histogram,        "generate histogram from vector"},
+  {1, "vload",        vload,            "load vectors as overlay on image display"},
+  {1, "vmaxwell",     vmaxwell,         "fit a Maxwellian to a vector"},
+  {1, "vpop",         vpop,             "remove first element of a vector"},
   {1, "vroll",        vroll,            "roll vector elements"},
-  {1, "vpop",         vpop,             "remove first element"},
+  {1, "vsmooth",      vsmooth,          "Gaussian smooth of a vector"},
+  {1, "vstats",       vstats,           "statistics on a vector"},
+  {1, "vzload",       vzload,           "load vectors as overlay on image display (scaled points)"},
   {1, "wd",           wd,               "write an image to a file"},
   {1, "write",        write_vectors,    "write vectors to datafile"},
-  {1, "zap",          zap,              "delete pixels"},
+  {1, "zap",          zap,              "assign values to pixel regions"},
   {1, "zplot",        zplot,            "plot x y with size scaled by z"},
 }; 
Index: unk/Ohana/src/opihi/cmd.data/stats.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/stats.c	(revision 21507)
+++ 	(revision )
@@ -1,125 +1,0 @@
-# include "data.h"
-
-int stats (int argc, char **argv) {
-  
-  int i, j, Nmode, Imode;
-  double Npix, N1, N2, max, min, range, median, mode, IgnoreValue;
-  float *V;
-  int sx, sy, nx, ny, *hist, Nhist, bin;
-  int Ignore, Quiet, N;
-  Buffer *buf;
-
-  IgnoreValue = 0;
-  Ignore = FALSE;
-  if ((N = get_argument (argc, argv, "-ignore"))) {
-    Ignore = TRUE;
-    remove_argument (N, &argc, argv);
-    IgnoreValue = atof(argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-
-  Quiet = FALSE;
-  if ((N = get_argument (argc, argv, "-q"))) {
-    Quiet = TRUE;
-    remove_argument (N, &argc, argv);
-  }
-  if ((N = get_argument (argc, argv, "-quiet"))) {
-    Quiet = TRUE;
-    remove_argument (N, &argc, argv);
-  }
-
-  if ((argc != 2) && (argc != 6)) {
-    gprint (GP_ERR, "USAGE: stats <buffer> sx sy nx ny\n");
-    gprint (GP_ERR, "OR:    stats <buffer>\n");
-    return (FALSE);
-  }
-
-  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
-
-  if (argc == 6) {
-    sx = strcmp (argv[2], "-") ? atof (argv[2]) : 0;
-    sy = strcmp (argv[3], "-") ? atof (argv[3]) : 0;
-    nx = strcmp (argv[4], "-") ? atof (argv[4]) : buf[0].matrix.Naxis[0];
-    ny = strcmp (argv[5], "-") ? atof (argv[5]) : buf[0].matrix.Naxis[1];
-  } else {
-    sx = 0;
-    sy = 0;
-    nx = buf[0].matrix.Naxis[0];
-    ny = buf[0].matrix.Naxis[1];
-  }
-
-  Npix = N1 = N2 = 0;
-  if ((sx < 0) || (sy < 0) || 
-      (sx+nx > buf[0].matrix.Naxis[0]) || 
-      (sy+ny > buf[0].matrix.Naxis[1])) {
-    gprint (GP_ERR, "region out of range\n");
-    return (FALSE);
-  }
-
-  max = -1e32;
-  min = +1e32;
-  for (j = sy; j < sy + ny; j++) {
-    V = (float *)(buf[0].matrix.buffer) + j*buf[0].matrix.Naxis[0] + sx; 
-    for (i = 0; i < nx; i++, V++) {
-      if (Ignore && (fabs (*V - IgnoreValue) < 1e-8)) continue;
-      if (isnan(*V)) continue;
-      if (isinf(*V)) continue;
-      N1 += *V;
-      N2 += (*V)*(*V);
-      Npix += 1.0;
-      max = MAX (max, *V);
-      min = MIN (min, *V);
-    }
-  }
-  N1 = N1 / Npix;
-
-/* calculate mode, median */
-  median = mode = 0.5*(max + min);
-  if ((max - min) != 0) {
-    range = 0xffff / (max - min);
-    ALLOCATE (hist, int, 0x10000);
-    bzero (hist, 0x10000*sizeof(int));
-    for (j = sy; j < sy + ny; j++) {
-      V = (float *)(buf[0].matrix.buffer) + j*buf[0].matrix.Naxis[0] + sx; 
-      for (i = 0; i < nx; i++, V++) {
-	if (Ignore && (fabs (*V - IgnoreValue) < 1e-8)) continue;
-	if (isnan(*V)) continue;
-	if (isinf(*V)) continue;
-	bin = MIN (MAX (0, (*V - min) * range), 0xffff);
-	hist[bin] ++;
-      }
-    }
-    Nhist = 0;
-    for (i = 0; (i < 0xffff) && (Nhist < 0.5*Npix); i++) 
-      Nhist += hist[i];
-    median = i / range + min;
-    Nmode = hist[0];
-    Imode = 0;
-    for (i = 1; i < 0x10000; i++) {
-      if (hist[i] > Nmode) {
-	Nmode = hist[i];
-	Imode = i;
-      }
-    }
-    mode = Imode / range + min;
-    free (hist);
-  }  
-  
-  if (!Quiet) {
-    gprint (GP_LOG, "  mean    stdev    min     max   median   Npix   Total\n");
-    gprint (GP_LOG, "%7.4g %7.4g %7.4g %7.4g %7.4g %7.0f %7.4g\n", N1, sqrt (N2/Npix - N1*N1), 
-	    min, max, median, Npix, Npix*N1);
-  }
-
-  set_variable ("MIN",    min);
-  set_variable ("MAX",    max);
-  set_variable ("MEDIAN", median);
-  set_variable ("MEAN",   N1);
-  set_variable ("MODE",   mode);
-  set_variable ("TOTAL",  N1*Npix);
-  set_variable ("NPIX",   Npix);
-  set_variable ("SIGMA",  sqrt (N2/Npix - N1*N1));
-
-  return (TRUE);
-}
-
Index: unk/Ohana/src/opihi/cmd.data/vstat.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/vstat.c	(revision 21507)
+++ 	(revision )
@@ -1,139 +1,0 @@
-# include "data.h"
-
-int vstat (int argc, char **argv) {
-  
-  int i, N;
-  double max, min, sum, var, dvar, mean, stdev;
-  float IgnoreValue;
-  int Ignore, Quiet;
-
-  int *Nval, bin, Nmode, Nmed;
-  double dx, mode, median;
-  Vector *vec;
-
-  IgnoreValue = 0;
-  Ignore = FALSE;
-  if ((N = get_argument (argc, argv, "-ignore"))) {
-    Ignore = TRUE;
-    remove_argument (N, &argc, argv);
-    IgnoreValue = atof(argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-
-  Quiet = FALSE;
-  if ((N = get_argument (argc, argv, "-q"))) {
-    Quiet = TRUE;
-    remove_argument (N, &argc, argv);
-  }
-  if ((N = get_argument (argc, argv, "-quiet"))) {
-    Quiet = TRUE;
-    remove_argument (N, &argc, argv);
-  }
-
-  if (argc != 2) {
-    gprint (GP_ERR, "USAGE: vstat (vector)\n");
-    return (FALSE);
-  }
-
-  if ((vec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
-
-  /* we need two passes, one for max, min, mean, sum, one for median, stdev, etc */
-
-  /* calculate max, min, mean, sum, npix */
-  max = -HUGE_VAL;
-  min = HUGE_VAL;
-  sum = N = 0;
-  if (vec[0].type == OPIHI_FLT) {
-    opihi_flt *X = vec[0].elements.Flt;
-    for (i = 0; i < vec[0].Nelements; i++, X++) {
-      if (!finite (*X)) continue;
-      if (Ignore && (*X == IgnoreValue)) continue;
-      max = MAX (*X, max);
-      min = MIN (*X, min);
-      sum += *X;
-      N++;
-    }      
-  } else {
-    opihi_int *X = vec[0].elements.Int;
-    for (i = 0; i < vec[0].Nelements; i++, X++) {
-      if (!finite (*X)) continue;
-      if (Ignore && (*X == IgnoreValue)) continue;
-      max = MAX (*X, max);
-      min = MIN (*X, min);
-      sum += *X;
-      N++;
-    }      
-  }
-  mean = sum / N;
-
-  /* calculate median and mode with resolution of (max - min) / 1000 */ 
-  dx = (max - min) / 1000;
-  if (dx == 0) {
-    median = mode = min;
-    stdev = 0.0;
-    goto skip;
-  }
-
-  ALLOCATE (Nval, int, 1002);
-  bzero (Nval, 1000*sizeof(int));
-  var = 0;
-  if (vec[0].type == OPIHI_FLT) {
-    opihi_flt *X = vec[0].elements.Flt;
-    for (i = 0; i < vec[0].Nelements; i++, X++) {
-      if (!finite (*X)) continue;
-      if (Ignore && (*X == IgnoreValue)) continue;
-      bin = MAX (0, MIN (1000, (*X - min) / dx));
-      Nval[bin] ++;
-      dvar = (*X - mean);
-      var += dvar*dvar;
-    }      
-  } else {
-    opihi_int *X = vec[0].elements.Int;
-    for (i = 0; i < vec[0].Nelements; i++, X++) {
-      if (!finite (*X)) continue;
-      if (Ignore && (*X == IgnoreValue)) continue;
-      bin = MAX (0, MIN (1000, (*X - min) / dx));
-      Nval[bin] ++;
-      dvar = (*X - mean);
-      var += dvar*dvar;
-    }      
-  }
-  stdev = sqrt (var / N);
-
-  Nmode = 0;
-  mode = Nval[Nmode];
-  median = 0;
-  Nmed = -1;
-  for (i = 0; i < 1001; i++) {
-    if (Nmed == -1) {
-      median += Nval[i];
-      if (median >= N / 2.0) {
-	Nmed = i;
-	median = i * dx + min;
-      }
-    }
-    if (mode < Nval[i]) {
-      Nmode = i;
-      mode = Nval[Nmode];
-    }
-  }
-  mode = Nmode * dx + min;
-  free (Nval);
-
-skip:
-  if (!Quiet) {
-    gprint (GP_ERR, "mean: %g, stdev: %g, min: %g, max: %g, median: %g, mode: %g, Npts: %d\n", 
-	     mean, stdev, min, max, median, mode, N);
-  }
-
-  set_variable ("MIN",      min);
-  set_variable ("MAX",      max);
-  set_variable ("MEDIAN",   median);
-  set_variable ("MEAN",     mean);
-  set_variable ("MODE",     mode);
-  set_variable ("TOTAL",    sum);
-  set_int_variable ("NPIX", N);
-  set_variable ("SIGMA",    stdev);
-  return (TRUE);
-}
-
Index: /trunk/Ohana/src/opihi/cmd.data/vstats.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/vstats.c	(revision 21508)
+++ /trunk/Ohana/src/opihi/cmd.data/vstats.c	(revision 21508)
@@ -0,0 +1,139 @@
+# include "data.h"
+
+int vstats (int argc, char **argv) {
+  
+  int i, N;
+  double max, min, sum, var, dvar, mean, stdev;
+  float IgnoreValue;
+  int Ignore, Quiet;
+
+  int *Nval, bin, Nmode, Nmed;
+  double dx, mode, median;
+  Vector *vec;
+
+  IgnoreValue = 0;
+  Ignore = FALSE;
+  if ((N = get_argument (argc, argv, "-ignore"))) {
+    Ignore = TRUE;
+    remove_argument (N, &argc, argv);
+    IgnoreValue = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  Quiet = FALSE;
+  if ((N = get_argument (argc, argv, "-q"))) {
+    Quiet = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-quiet"))) {
+    Quiet = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+
+  if (argc != 2) {
+    gprint (GP_ERR, "USAGE: vstat (vector)\n");
+    return (FALSE);
+  }
+
+  if ((vec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
+
+  /* we need two passes, one for max, min, mean, sum, one for median, stdev, etc */
+
+  /* calculate max, min, mean, sum, npix */
+  max = -HUGE_VAL;
+  min = HUGE_VAL;
+  sum = N = 0;
+  if (vec[0].type == OPIHI_FLT) {
+    opihi_flt *X = vec[0].elements.Flt;
+    for (i = 0; i < vec[0].Nelements; i++, X++) {
+      if (!finite (*X)) continue;
+      if (Ignore && (*X == IgnoreValue)) continue;
+      max = MAX (*X, max);
+      min = MIN (*X, min);
+      sum += *X;
+      N++;
+    }      
+  } else {
+    opihi_int *X = vec[0].elements.Int;
+    for (i = 0; i < vec[0].Nelements; i++, X++) {
+      if (!finite (*X)) continue;
+      if (Ignore && (*X == IgnoreValue)) continue;
+      max = MAX (*X, max);
+      min = MIN (*X, min);
+      sum += *X;
+      N++;
+    }      
+  }
+  mean = sum / N;
+
+  /* calculate median and mode with resolution of (max - min) / 1000 */ 
+  dx = (max - min) / 1000;
+  if (dx == 0) {
+    median = mode = min;
+    stdev = 0.0;
+    goto skip;
+  }
+
+  ALLOCATE (Nval, int, 1002);
+  bzero (Nval, 1000*sizeof(int));
+  var = 0;
+  if (vec[0].type == OPIHI_FLT) {
+    opihi_flt *X = vec[0].elements.Flt;
+    for (i = 0; i < vec[0].Nelements; i++, X++) {
+      if (!finite (*X)) continue;
+      if (Ignore && (*X == IgnoreValue)) continue;
+      bin = MAX (0, MIN (1000, (*X - min) / dx));
+      Nval[bin] ++;
+      dvar = (*X - mean);
+      var += dvar*dvar;
+    }      
+  } else {
+    opihi_int *X = vec[0].elements.Int;
+    for (i = 0; i < vec[0].Nelements; i++, X++) {
+      if (!finite (*X)) continue;
+      if (Ignore && (*X == IgnoreValue)) continue;
+      bin = MAX (0, MIN (1000, (*X - min) / dx));
+      Nval[bin] ++;
+      dvar = (*X - mean);
+      var += dvar*dvar;
+    }      
+  }
+  stdev = sqrt (var / N);
+
+  Nmode = 0;
+  mode = Nval[Nmode];
+  median = 0;
+  Nmed = -1;
+  for (i = 0; i < 1001; i++) {
+    if (Nmed == -1) {
+      median += Nval[i];
+      if (median >= N / 2.0) {
+	Nmed = i;
+	median = i * dx + min;
+      }
+    }
+    if (mode < Nval[i]) {
+      Nmode = i;
+      mode = Nval[Nmode];
+    }
+  }
+  mode = Nmode * dx + min;
+  free (Nval);
+
+skip:
+  if (!Quiet) {
+    gprint (GP_ERR, "mean: %g, stdev: %g, min: %g, max: %g, median: %g, mode: %g, Npts: %d\n", 
+	     mean, stdev, min, max, median, mode, N);
+  }
+
+  set_variable ("MIN",      min);
+  set_variable ("MAX",      max);
+  set_variable ("MEDIAN",   median);
+  set_variable ("MEAN",     mean);
+  set_variable ("MODE",     mode);
+  set_variable ("TOTAL",    sum);
+  set_int_variable ("NPIX", N);
+  set_variable ("SIGMA",    stdev);
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/opihi/dvo/dbExtractAverages.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dbExtractAverages.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/dvo/dbExtractAverages.c	(revision 21508)
@@ -121,5 +121,5 @@
       break;
     case AVE_FLAG:
-      value.Int = average[0].code;
+      value.Int = average[0].flags;
       break;
     case AVE_OBJID:
Index: /trunk/Ohana/src/opihi/dvo/dbExtractImages.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dbExtractImages.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/dvo/dbExtractImages.c	(revision 21508)
@@ -247,5 +247,5 @@
       break;
     case IMAGE_FLAG:
-      value.Int = image[N].code;
+      value.Int = image[N].flags;
       break;
     case IMAGE_CCDNUM:
Index: /trunk/Ohana/src/opihi/dvo/dbExtractMeasures.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dbExtractMeasures.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/dvo/dbExtractMeasures.c	(revision 21508)
@@ -146,5 +146,5 @@
 	case MAG_NPHOT:
 	  if (equiv == NULL) {
-	    value.Int = NAN;
+	    value.Int = 0;
 	    break;
 	  }
@@ -260,5 +260,5 @@
       break;
     case MEAS_OBJFLAGS: /* OK */
-      value.Int = average[0].code;
+      value.Int = average[0].flags;
       break;
     // note that these represent the ra displacement relative to the average, not 
@@ -312,5 +312,5 @@
       break;
     case MEAS_DOPHOT: /* OK */
-      value.Flt = measure[0].dophot;
+      value.Int = (measure[0].photFlags >> 16);
       break;
     case MEAS_DB_FLAGS: /* ? */
@@ -394,5 +394,5 @@
       break;
     case MEAS_PSF_QF: /* OK */
-      value.Flt = measure[0].qPSF;
+      value.Flt = measure[0].psfQual;
       break;
     case MEAS_PSF_CHISQ: /* OK */
@@ -404,7 +404,4 @@
     case MEAS_EXT_NSIGMA: /* OK */
       value.Flt = measure[0].extNsigma;
-      break;
-    case MEAS_STARGAL: /* OK */
-      value.Flt = measure[0].stargal;
       break;
 
Index: /trunk/Ohana/src/opihi/dvo/dbFields.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dbFields.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/dvo/dbFields.c	(revision 21508)
@@ -170,5 +170,5 @@
   if (!strcasecmp (fieldName, "FWHM_MIN"))   ESCAPE (MEAS_FWHM_MIN,    	  MAG_NONE, OPIHI_FLT);
   if (!strcasecmp (fieldName, "THETA"))      ESCAPE (MEAS_THETA,       	  MAG_NONE, OPIHI_FLT);
-  if (!strcasecmp (fieldName, "DOPHOT"))     ESCAPE (MEAS_DOPHOT,      	  MAG_NONE, OPIHI_FLT);
+  if (!strcasecmp (fieldName, "DOPHOT"))     ESCAPE (MEAS_DOPHOT,      	  MAG_NONE, OPIHI_INT);
   if (!strcasecmp (fieldName, "DB_FLAGS"))   ESCAPE (MEAS_DB_FLAGS,    	  MAG_NONE, OPIHI_INT);
   if (!strcasecmp (fieldName, "PHOT_FLAGS")) ESCAPE (MEAS_PHOT_FLAGS, 	  MAG_NONE, OPIHI_INT);
@@ -193,5 +193,4 @@
   if (!strcasecmp (fieldName, "SKY"))        ESCAPE (MEAS_SKY,     	  MAG_NONE, OPIHI_FLT);
   if (!strcasecmp (fieldName, "SKY_ERR"))    ESCAPE (MEAS_dSKY,     	  MAG_NONE, OPIHI_FLT);
-  if (!strcasecmp (fieldName, "STARGAL"))    ESCAPE (MEAS_STARGAL,     	  MAG_NONE, OPIHI_FLT);
 
   // for words that don't parse, try a photcode
Index: /trunk/Ohana/src/opihi/dvo/gimages.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/gimages.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/dvo/gimages.c	(revision 21508)
@@ -220,9 +220,9 @@
     if (PixelCoords) {
       gprint (GP_LOG, "%3d %s %6.1f %6.1f %20s %5d %2d %4.2f %6.3f %5.3f %5.3f %4x\n", 
-	       Nfound, image[i].name, X, Y, date, image[i].nstar, image[i].photcode, image[i].secz, image[i].Mcal, image[i].dMcal, image[i].exptime, image[i].code);
+	       Nfound, image[i].name, X, Y, date, image[i].nstar, image[i].photcode, image[i].secz, image[i].Mcal, image[i].dMcal, image[i].exptime, image[i].flags);
     } else {
       XY_to_RD (&ra, &dec, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
       gprint (GP_LOG, "%3d %s %8.4f %8.4f %20s %5d %2d %4.2f %6.3f %5.3f %5.3f %4x\n", 
-	       Nfound, image[i].name, ra, dec, date, image[i].nstar, image[i].photcode, image[i].secz, image[i].Mcal, image[i].dMcal, image[i].exptime, image[i].code);
+	       Nfound, image[i].name, ra, dec, date, image[i].nstar, image[i].photcode, image[i].secz, image[i].Mcal, image[i].dMcal, image[i].exptime, image[i].flags);
     }
     sprintf (name, "IMAGEx:%d", Nfound);
Index: /trunk/Ohana/src/opihi/dvo/gstar.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/gstar.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/dvo/gstar.c	(revision 21508)
@@ -158,5 +158,5 @@
 	gprint (GP_LOG, "%3d   ",  catalog.average[k].Nmeasure);
 	gprint (GP_LOG, "%4.1f ",  0.01*catalog.average[k].Xp);
-	gprint (GP_LOG, "%5d",     catalog.average[k].code);
+	gprint (GP_LOG, "%5d",     catalog.average[k].flags);
 
 	if (FULL_OUTPUT) {
@@ -211,5 +211,5 @@
 	    gprint (GP_LOG, "%7.4f ",  catalog.measure[m].dR);
 	    gprint (GP_LOG, "%7.4f",   catalog.measure[m].dD);
-	    gprint (GP_LOG, "%2d ",    catalog.measure[m].dophot);
+	    gprint (GP_LOG, "%4x ",    catalog.measure[m].photFlags);
 	    gprint (GP_LOG, "%3x ",    catalog.measure[m].dbFlags);
 	    gprint (GP_LOG, "%5d ",    catalog.measure[m].photcode);
@@ -233,5 +233,5 @@
 		gprint (GP_LOG, "%d", catalog.measure[m].detID);
 		gprint (GP_LOG, "%d", catalog.measure[m].imageID);
-		gprint (GP_LOG, "%f", catalog.measure[m].qPSF);
+		gprint (GP_LOG, "%f", catalog.measure[m].psfQual);
 		gprint (GP_LOG, "%f", catalog.measure[m].psfChisq);
 		gprint (GP_LOG, "%f", catalog.measure[m].crNsigma);
@@ -240,7 +240,4 @@
 		gprint (GP_LOG, "%f", 0.01*catalog.measure[m].FWy);
 		gprint (GP_LOG, "%f", (360.0/(float)0xffff)*catalog.measure[m].theta);
-
-		gprint (GP_LOG, "%x", catalog.measure[m].photFlags);
-		gprint (GP_LOG, "%d", catalog.measure[m].stargal);
 	    }
 	    gprint (GP_LOG, "\n");
Index: /trunk/Ohana/src/opihi/dvo/paverage.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/paverage.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/dvo/paverage.c	(revision 21508)
@@ -122,5 +122,5 @@
     /* project stars to screen display coords */
     for (i = 0; (i < catalog.Naverage) && !interrupt; i++) {
-      if (IDclip && (average[i].code != IDchoice)) continue;
+      if (IDclip && (average[i].flags != IDchoice)) continue;
       average[i].R = ohana_normalize_angle (average[i].R);
       while (average[i].R < Rmin) average[i].R += 360.0;
Index: /trunk/Ohana/src/opihi/dvo/photometry.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/photometry.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/dvo/photometry.c	(revision 21508)
@@ -2,41 +2,41 @@
 
 /* match code to measure  */
-# define TESTCODE(C,M) \
-  if (C != NULL) { \
-    switch (C[0].type) { \
-    case PHOT_DEP: \
-    case PHOT_REF: \
-      if (C[0].code != M.photcode) continue; \
-      break; \
-    case PHOT_SEC: \
-      if (C[0].code != GetPhotcodeEquivCodebyCode (M.photcode)) continue; \
-      break; \
-    default: \
-      break; \
-  } }
+# define TESTCODE(C,M)							\
+  if (C != NULL) {							\
+    switch (C[0].type) {						\
+      case PHOT_DEP:							\
+      case PHOT_REF:							\
+	if (C[0].code != M.photcode) continue;				\
+	break;								\
+      case PHOT_SEC:							\
+	if (C[0].code != GetPhotcodeEquivCodebyCode (M.photcode)) continue; \
+	break;								\
+      default:								\
+	break;								\
+    } }
 
 /* exclusions based on measure.params  */
-# define TESTMEASURE(M) \
-  if (ApplySelections[SelectionParam]) { \
-    if (TimeSelect && (M.t < tzero)) continue; \
-    if (TimeSelect && (M.t > tend)) continue; \
-    if (ErrSelect  && (M.dM > ErrValue)) continue; \
+# define TESTMEASURE(M)							\
+  if (ApplySelections[SelectionParam]) {				\
+    if (TimeSelect && (M.t < tzero)) continue;				\
+    if (TimeSelect && (M.t > tend)) continue;				\
+    if (ErrSelect  && (M.dM > ErrValue)) continue;			\
+    if (iMagSelect && (PhotInst (&M) < iMagMin)) continue;		\
+    if (FlagSelect && (M.dbFlags != FlagValue)) continue;		\
     if (TypeSelect && (TypeValue != GetMeasureTypeCode (&M))) continue; \
-    if (iMagSelect && (PhotInst (&M) < iMagMin)) continue; \
-    if (FlagSelect && (M.dbFlags != FlagValue)) continue; \
-  }
-
-# define SETMAG(MOUT,MEAS,MODE) \
-  MOUT = NAN; \
-  if (MODE == MAG_INST) MOUT = PhotInst (&MEAS);  \
-  if (MODE == MAG_CAT)  MOUT = PhotCat  (&MEAS); \
-  if (MODE == MAG_SYS)  MOUT = PhotSys  (&MEAS, average, secfilt); \
-  if (MODE == MAG_REL)  MOUT = PhotRel  (&MEAS, average, secfilt); \
+  }
+
+# define SETMAG(MOUT,MEAS,MODE)						\
+  MOUT = NAN;								\
+  if (MODE == MAG_INST) MOUT = PhotInst (&MEAS);			\
+  if (MODE == MAG_CAT)  MOUT = PhotCat  (&MEAS);			\
+  if (MODE == MAG_SYS)  MOUT = PhotSys  (&MEAS, average, secfilt);	\
+  if (MODE == MAG_REL)  MOUT = PhotRel  (&MEAS, average, secfilt);	\
   if (MODE == MAG_CAL)  MOUT = PhotCal  (&MEAS, average, secfilt, measure, GetPhotcodeEquivbyCode (MEAS.photcode)); \
   if (MODE == MAG_AVE)  MOUT = PhotAve  (GetPhotcodeEquivbyCode (MEAS.photcode), average, secfilt); \
   if (MODE == MAG_REF)  MOUT = PhotRef  (GetPhotcodeEquivbyCode (MEAS.photcode), average, secfilt, measure); \
-  if (ApplySelections[SelectionParam]) { \
-    if (MagSelect && (MOUT > MagMax)) continue; \
-    if (MagSelect && (MOUT < MagMin)) continue; \
+  if (ApplySelections[SelectionParam]) {				\
+    if (MagSelect && (MOUT > MagMax)) continue;				\
+    if (MagSelect && (MOUT < MagMin)) continue;				\
   }
 
@@ -182,5 +182,4 @@
   if (!strcasecmp (parname, "dec"))  	 param = MEAS_DEC;
   if (!strcasecmp (parname, "mag")) 	 param = MEAS_MAG;
-  // if (!strcasecmp (parname, "dmag")) 	 param = MEAS_dMAG;
   if (!strcasecmp (parname, "airmass"))  param = MEAS_AIRMASS;
   if (!strcasecmp (parname, "exptime"))  param = MEAS_EXPTIME;
@@ -190,5 +189,4 @@
   if (!strcasecmp (parname, "dD")) 	 param = MEAS_DEC_OFFSET;
   if (!strcasecmp (parname, "fwhm"))   	 param = MEAS_FWHM;
-  // if (!strcasecmp (parname, "dophot")) 	 param = MEAS_DOPHOT;
   if (!strcasecmp (parname, "FLAGS"))    param = MEAS_DB_FLAGS;
   if (!strcasecmp (parname, "XCCD"))   	 param = MEAS_XCCD;
@@ -198,5 +196,5 @@
   if (!strcasecmp (parname, "help")) {
     gprint (GP_ERR, "value may be one of the following:\n");
-    gprint (GP_ERR, " ra dR dec dD mag dmag Mrel Mcal photcode time fwhm dophot xccd yccd xmosaic ymosaic flags\n");
+    gprint (GP_ERR, " ra dR dec dD mag dmag Mrel Mcal photcode time fwhm xccd yccd xmosaic ymosaic flags\n");
     gprint (GP_ERR, "value may also be a valid photcode\n");
     gprint (GP_ERR, "photcodes or 'mag' may have optional magnitude type: mag,[Minst, Mcat, Msys, Mrel, Mcal]\n");
@@ -311,6 +309,6 @@
 
   if (VarConfig ("ZERO_PT", "%lf", &ZERO_POINT) == (char *) NULL) {
-      gprint (GP_ERR, "ZERO_PT undefined in config\n");
-      return (FALSE);
+    gprint (GP_ERR, "ZERO_PT undefined in config\n");
+    return (FALSE);
   }
   SetZeroPoint (ZERO_POINT);
@@ -324,6 +322,6 @@
 
   if (VarConfig ("PHOTCODE_FILE", "%s", MasterPhotcodeFile) == (char *) NULL) {
-      gprint (GP_ERR, "PHOTCODE_FILE undefined in config\n");
-      return (FALSE);
+    gprint (GP_ERR, "PHOTCODE_FILE undefined in config\n");
+    return (FALSE);
   }
   if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
@@ -642,5 +640,5 @@
       break;
     case AVE_FLAG:
-      value = average[0].code;
+      value = average[0].flags;
       break;
     case AVE_MAG:
@@ -659,6 +657,6 @@
 	  value = NAN;
 	  for (i = 0; i < average[0].Nmeasure; i++) {
-	      if (code[0].code != measure[i].photcode) continue;
-	      value = measure[i].M;
+	    if (code[0].code != measure[i].photcode) continue;
+	    value = measure[i].M;
 	  }
 	  break;
@@ -675,5 +673,6 @@
       break;
     case AVE_TYPEFRAC:
-      value = DetermineTypefrac (average, measure, code);
+      // value = DetermineTypefrac (average, measure, code);
+      value = NAN;
       break;
     case AVE_NCODE:
@@ -719,5 +718,5 @@
     if ((code != NULL) && (code[0].code != GetPhotcodeEquivCodebyCode (measure[k].photcode))) continue;
     Nc ++;
-    if (measure[k].dophot != TypefracType) continue;
+    if ((measure[k].photFlags >> 16) != TypefracType) continue;
     Nt ++;
   }
@@ -744,5 +743,5 @@
 
 int GetMeasureTypeCode (Measure *measure) {
-  switch (measure[0].dophot) {
+  switch ((measure[0].photFlags >> 16)) {
     case 0:
     case 1:
@@ -770,5 +769,5 @@
   if (IsDophot) {
     
-    if (measure[0].dophot == 4) return (FALSE);
+    if ((measure[0].photFlags >> 16) == 4) return (FALSE);
     return (TRUE);
   
@@ -866,5 +865,5 @@
   if (ErrSelect) {
     dM = PhotdM (code, average, secfilt);
-    if (dM > ErrValue) return (NAN);
+    if (dM > ErrValue) return (FALSE);
   }
   
@@ -1137,12 +1136,7 @@
       value = average[0].D - measure[0].dD / 3600.0;
       break;
-# if (0)
-    case MEAS_dMAG: /* OK */
-      value = measure[0].dM;
-      break;
     case MEAS_DOPHOT: /* OK */
-      value = measure[0].dophot;
-      break;
-# endif
+      value = (measure[0].photFlags >> 16);
+      break;
     case MEAS_AIRMASS: /* OK */
       value = measure[0].airmass;
Index: /trunk/Ohana/src/opihi/dvo/pmeasure.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/pmeasure.c	(revision 21507)
+++ /trunk/Ohana/src/opihi/dvo/pmeasure.c	(revision 21508)
@@ -192,5 +192,5 @@
     /* project stars to screen display coords */
     for (i = 0; (i < catalog.Naverage) && !interrupt; i++) {
-      if (IDclip && (catalog.average[i].code != IDchoice)) continue;
+      if (IDclip && (catalog.average[i].flags != IDchoice)) continue;
       catalog.average[i].R = ohana_normalize_angle (catalog.average[i].R);
       while (catalog.average[i].R < Rmin) catalog.average[i].R += 360.0;
Index: /trunk/Ohana/src/opihi/include/dvoshell.h
===================================================================
--- /trunk/Ohana/src/opihi/include/dvoshell.h	(revision 21507)
+++ /trunk/Ohana/src/opihi/include/dvoshell.h	(revision 21508)
@@ -106,5 +106,4 @@
       MEAS_CR_NSIGMA, 
       MEAS_EXT_NSIGMA, 
-      MEAS_STARGAL, 
 };
 
Index: /trunk/Ohana/src/relastro/include/relastro.h
===================================================================
--- /trunk/Ohana/src/relastro/include/relastro.h	(revision 21507)
+++ /trunk/Ohana/src/relastro/include/relastro.h	(revision 21508)
@@ -61,5 +61,5 @@
   short Xm;
   float secz;
-  char code;
+  char flags;
   Coords coords;
 } Mosaic; 
Index: /trunk/Ohana/src/relastro/src/MosaicOps.c
===================================================================
--- /trunk/Ohana/src/relastro/src/MosaicOps.c	(revision 21507)
+++ /trunk/Ohana/src/relastro/src/MosaicOps.c	(revision 21508)
@@ -47,5 +47,5 @@
     mosaic[Nmosaic].dMcal = 0.0;
     mosaic[Nmosaic].Xm    = 0.0;
-    mosaic[Nmosaic].code  = image[i].code;
+    mosaic[Nmosaic].flags  = image[i].flags;
     mosaic[Nmosaic].secz  = image[i].secz;
     mosaic[Nmosaic].coords = image[i].coords;
Index: /trunk/Ohana/src/relastro/src/UpdateObjects.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 21507)
+++ /trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 21508)
@@ -169,5 +169,5 @@
       // position, consider including the lower-quality detections
 
-      catalog[i].average[j].code &= ~ID_STAR_FEW;
+      catalog[i].average[j].flags &= ~ID_STAR_FEW;
 
       // XXX add the parallax factor range as a criterion as well
Index: /trunk/Ohana/src/relastro/src/bcatalog.c
===================================================================
--- /trunk/Ohana/src/relastro/src/bcatalog.c	(revision 21507)
+++ /trunk/Ohana/src/relastro/src/bcatalog.c	(revision 21508)
@@ -36,6 +36,6 @@
       // XXX define astrometry vs photometry average.dbFlags
       // XXX put photometry flags in secfilt (ie, per average mag?)
-      subcatalog[0].average[Naverage].code &= ~ID_STAR_FEW;
-      subcatalog[0].average[Naverage].code &= ~ID_STAR_POOR;
+      subcatalog[0].average[Naverage].flags &= ~ID_STAR_FEW;
+      subcatalog[0].average[Naverage].flags &= ~ID_STAR_POOR;
     }
 
@@ -70,5 +70,6 @@
 
       /* select measurements by quality */
-      if (DophotSelect && (catalog[0].measure[offset].dophot != DophotValue)) continue;
+      // XXX FIX THIS!!
+      // if (DophotSelect && (catalog[0].measure[offset].dophot != DophotValue)) continue;
 
       /* select measurements by quality */
Index: /trunk/Ohana/src/relastro/src/load_catalogs.c
===================================================================
--- /trunk/Ohana/src/relastro/src/load_catalogs.c	(revision 21507)
+++ /trunk/Ohana/src/relastro/src/load_catalogs.c	(revision 21508)
@@ -38,5 +38,5 @@
       if (RESET) {
 	for (j = 0; j < catalog[i].Naverage; j++) {
-	  catalog[i].average[j].code = 0;
+	  catalog[i].average[j].flags = 0;
 	  m = catalog[i].average[j].measureOffset;
 	  for (k = 0; k < catalog[i].average[j].Nmeasure; k++) {
Index: /trunk/Ohana/src/relastro/src/relastro_objects.c
===================================================================
--- /trunk/Ohana/src/relastro/src/relastro_objects.c	(revision 21507)
+++ /trunk/Ohana/src/relastro/src/relastro_objects.c	(revision 21508)
@@ -40,5 +40,5 @@
     if (RESET) {
       for (j = 0; j < catalog.Naverage; j++) {
-	catalog.average[j].code = 0;
+	catalog.average[j].flags = 0;
 	m = catalog.average[j].measureOffset;
 	for (k = 0; k < catalog.average[j].Nmeasure; k++) {
Index: /trunk/Ohana/src/relastro/src/select_images.c
===================================================================
--- /trunk/Ohana/src/relastro/src/select_images.c	(revision 21507)
+++ /trunk/Ohana/src/relastro/src/select_images.c	(revision 21508)
@@ -147,6 +147,6 @@
       image[nimage] = timage[i]; 
       /* always allow 'few' images to succeed, if possible */
-      if (image[nimage].code & ID_IMAGE_FEW) { 
-	image[nimage].code &= ~(ID_IMAGE_FEW | ID_IMAGE_POOR);
+      if (image[nimage].flags & ID_IMAGE_FEW) { 
+	image[nimage].flags &= ~(ID_IMAGE_FEW | ID_IMAGE_POOR);
       }
       if (RESET) {
@@ -154,5 +154,5 @@
 	assignMcal (&image[nimage], (double *) NULL, -1);
 	image[nimage].dMcal = NAN;
-	image[nimage].code &= ~ID_IMAGE_POOR;
+	image[nimage].flags &= ~ID_IMAGE_POOR;
       }
       line_number[nimage] = i;
Index: /trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- /trunk/Ohana/src/relphot/include/relphot.h	(revision 21507)
+++ /trunk/Ohana/src/relphot/include/relphot.h	(revision 21508)
@@ -23,5 +23,5 @@
   short Xm;
   float secz;
-  char code;
+  char flags;
   Coords coords;
 } Mosaic; 
Index: /trunk/Ohana/src/relphot/src/GridOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/GridOps.c	(revision 21507)
+++ /trunk/Ohana/src/relphot/src/GridOps.c	(revision 21508)
@@ -369,5 +369,5 @@
 
       // skip stars marked as BAD
-      if (catalog[c].average[n].code & STAR_BAD) {
+      if (catalog[c].average[n].flags & STAR_BAD) {
 	Nrel ++;
 	continue;
Index: /trunk/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 21507)
+++ /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 21508)
@@ -244,5 +244,5 @@
   if (i == -1) return (NAN);
 
-  if (image[i].code & IMAGE_BAD) return (NAN);  
+  if (image[i].flags & IMAGE_BAD) return (NAN);  
   value = image[i].Mcal;
   return (value);
@@ -285,5 +285,5 @@
     /* on PoorImages run, skip good images */
     if (PoorImages) {
-      bad = image[i].code & (ID_IMAGE_FEW | ID_IMAGE_POOR | ID_IMAGE_SKIP);
+      bad = image[i].flags & (ID_IMAGE_FEW | ID_IMAGE_POOR | ID_IMAGE_SKIP);
       if (!bad) continue;
     }      
@@ -331,8 +331,8 @@
       mark = (N < IMAGE_TOOFEW) || (N < IMAGE_GOOD_FRACTION*Nlist[i]);
       if (mark) {
-	image[i].code |= ID_IMAGE_FEW;
+	image[i].flags |= ID_IMAGE_FEW;
 	Nfew ++;
       } else {
-	image[i].code &= ~ID_IMAGE_FEW;
+	image[i].flags &= ~ID_IMAGE_FEW;
       }      
     }
@@ -373,5 +373,5 @@
 
   for (i = N = 0; i < Nimage; i++) {
-    if (image[i].code & IMAGE_BAD) continue;
+    if (image[i].flags & IMAGE_BAD) continue;
     mlist[N] = fabs (image[i].Mcal);
     slist[N] = image[i].dMcal;
@@ -390,11 +390,11 @@
   for (i = 0; i < Nimage; i++) {
     mark = FALSE;
-    image[i].code &= ~ID_IMAGE_POOR;
+    image[i].flags &= ~ID_IMAGE_POOR;
     mark = (image[i].dMcal > MaxScatter) || (fabs(image[i].Mcal - MedOffset) > MaxOffset);
     if (mark) { 
       Nmark ++;
-      image[i].code |= ID_IMAGE_POOR;
+      image[i].flags |= ID_IMAGE_POOR;
     } else {
-      image[i].code &= ~ID_IMAGE_POOR;
+      image[i].flags &= ~ID_IMAGE_POOR;
     }
   }
@@ -470,5 +470,5 @@
   n = 0;
   for (i = 0; i < Nimage; i++) {
-    if (image[i].code & IMAGE_BAD)  continue;
+    if (image[i].flags & IMAGE_BAD)  continue;
 
     N = 0;
@@ -512,5 +512,5 @@
   for (i = 0; i < Nimage; i++) {
 
-    if (image[i].code & IMAGE_BAD)  continue;
+    if (image[i].flags & IMAGE_BAD)  continue;
 
     list[n] = pow (10.0, 0.01*image[i].Xm);
@@ -540,5 +540,5 @@
   for (i = 0; i < Nimage; i++) {
 
-    if (image[i].code & IMAGE_BAD)  continue;
+    if (image[i].flags & IMAGE_BAD)  continue;
 
     list[n] = image[i].Mcal;
@@ -568,5 +568,5 @@
   for (i = 0; i < Nimage; i++) {
 
-    if (image[i].code & IMAGE_BAD)  continue;
+    if (image[i].flags & IMAGE_BAD)  continue;
 
     list[n] = image[i].dMcal;
Index: /trunk/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 21507)
+++ /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 21508)
@@ -68,5 +68,5 @@
     mosaic[Nmosaic].dMcal = 0.0;
     mosaic[Nmosaic].Xm    = 0.0;
-    mosaic[Nmosaic].code  = image[i].code;
+    mosaic[Nmosaic].flags  = image[i].flags;
     mosaic[Nmosaic].secz  = image[i].secz;
 
@@ -183,6 +183,6 @@
       image[im].dMcal = mosaic[i].dMcal;
       image[im].Xm = mosaic[i].Xm;
-      image[im].code |= (mosaic[i].code & ID_IMAGE_FEW);
-      image[im].code |= (mosaic[i].code & ID_IMAGE_POOR);
+      image[im].flags |= (mosaic[i].flags & ID_IMAGE_FEW);
+      image[im].flags |= (mosaic[i].flags & ID_IMAGE_POOR);
     }
   }      
@@ -306,5 +306,5 @@
   if (i == -1) return (NAN);
 
-  if (mosaic[i].code & IMAGE_BAD) return (NAN);  
+  if (mosaic[i].flags & IMAGE_BAD) return (NAN);  
   value = mosaic[i].Mcal;
   return (value);
@@ -343,5 +343,5 @@
     /* on PoorImages run, skip good images */
     if (PoorImages) {
-      bad = mosaic[i].code & (ID_IMAGE_FEW | ID_IMAGE_POOR | ID_IMAGE_SKIP);
+      bad = mosaic[i].flags & (ID_IMAGE_FEW | ID_IMAGE_POOR | ID_IMAGE_SKIP);
       if (!bad) continue;
     }      
@@ -393,8 +393,8 @@
       if (mark) {
 	fprintf (stderr, "marked image %s (%d), (%d < %d) || (%d < %f*%d)\n", image[imlist[i][0]].name, i, N, IMAGE_TOOFEW, N, IMAGE_GOOD_FRACTION, Nlist[i]);
-	mosaic[i].code |= ID_IMAGE_FEW;
+	mosaic[i].flags |= ID_IMAGE_FEW;
 	Nfew ++;
       } else {
-	mosaic[i].code &= ~ID_IMAGE_FEW;
+	mosaic[i].flags &= ~ID_IMAGE_FEW;
       }
     }
@@ -434,5 +434,5 @@
   n = 0;
   for (i = 0; i < Nmosaic; i++) {
-    if (mosaic[i].code & IMAGE_BAD) continue;
+    if (mosaic[i].flags & IMAGE_BAD) continue;
     list[n] = mosaic[i].Mcal;
     dlist[n] = 1;
@@ -462,5 +462,5 @@
   for (i = 0; i < Nmosaic; i++) {
 
-    if (mosaic[i].code & IMAGE_BAD) continue;
+    if (mosaic[i].flags & IMAGE_BAD) continue;
     list[n] = mosaic[i].dMcal;
     dlist[n] = 1;
@@ -490,5 +490,5 @@
   n = 0;
   for (i = 0; i < Nmosaic; i++) {
-    if (mosaic[i].code & IMAGE_BAD)  continue;
+    if (mosaic[i].flags & IMAGE_BAD)  continue;
 
     N = 0;
@@ -534,5 +534,5 @@
   for (i = 0; i < Nmosaic; i++) {
 
-    if (mosaic[i].code & IMAGE_BAD) continue;
+    if (mosaic[i].flags & IMAGE_BAD) continue;
     list[n] = pow(10.0, 0.01*mosaic[i].Xm);
     dlist[n] = 1;
@@ -564,5 +564,5 @@
 
   for (i = N = 0; i < Nmosaic; i++) {
-    if (mosaic[i].code & IMAGE_BAD) continue;
+    if (mosaic[i].flags & IMAGE_BAD) continue;
     mlist[N] = mosaic[i].Mcal;
     slist[N] = mosaic[i].dMcal;
@@ -584,7 +584,7 @@
     if (mark) { 
       Nmark ++;
-      mosaic[i].code |= ID_IMAGE_POOR;
+      mosaic[i].flags |= ID_IMAGE_POOR;
     } else {
-      mosaic[i].code &= ~ID_IMAGE_POOR;
+      mosaic[i].flags &= ~ID_IMAGE_POOR;
     }
   }
Index: /trunk/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/StarOps.c	(revision 21507)
+++ /trunk/Ohana/src/relphot/src/StarOps.c	(revision 21508)
@@ -26,5 +26,5 @@
 
   ave = catalog[cat].measure[meas].averef;
-  if (catalog[cat].average[ave].code & STAR_BAD) return (NAN);  
+  if (catalog[cat].average[ave].flags & STAR_BAD) return (NAN);  
  
   value = catalog[cat].secfilt[PhotNsec*ave+PhotSec].M;
@@ -44,5 +44,5 @@
 
       /* calculate the average value for a single star */
-      if (catalog[i].average[j].code & STAR_BAD) continue;  
+      if (catalog[i].average[j].flags & STAR_BAD) continue;  
       m = catalog[i].average[j].measureOffset;
 
@@ -89,8 +89,8 @@
 
       if (N <= STAR_TOOFEW) { /* too few measurements */
-	catalog[i].average[j].code |= ID_STAR_FEW;
+	catalog[i].average[j].flags |= ID_STAR_FEW;
 	Nfew ++;
       } else {
-	catalog[i].average[j].code &= ~ID_STAR_FEW;
+	catalog[i].average[j].flags &= ~ID_STAR_FEW;
       }	
 
@@ -208,5 +208,5 @@
 
 	  // XXX this is a hack for the 2MASS search; better to save an average value?
-	  if (catalog[i].measure[m].qPSF < 0.85) continue;
+	  if (catalog[i].measure[m].psfQual < 0.85) continue;
 
 	  list[N] = Msys - catalog[i].measure[m].Mcal;
@@ -280,5 +280,5 @@
   for (i = Ntot = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Naverage; j++) {
-      if (catalog[i].average[j].code & STAR_BAD) continue;
+      if (catalog[i].average[j].flags & STAR_BAD) continue;
       Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
       if (Xm == -1) continue;
@@ -306,8 +306,8 @@
       mark = (dM > MaxScatter) || (Xm == NAN_S_SHORT) || (Chisq > MaxChisq);
       if (mark) {
-	catalog[i].average[j].code |= ID_STAR_POOR;
+	catalog[i].average[j].flags |= ID_STAR_POOR;
 	Ndel ++;
       } else {
-	catalog[i].average[j].code &= ~ID_STAR_POOR;
+	catalog[i].average[j].flags &= ~ID_STAR_POOR;
       }
       Nave ++;
@@ -351,5 +351,5 @@
 
       /* skip bad stars to prevent them from becoming good (on inner sample) */
-      if (catalog[i].average[j].code & STAR_BAD) continue;  
+      if (catalog[i].average[j].flags & STAR_BAD) continue;  
 
       /* on final processing, skip stars already measured */
@@ -448,5 +448,5 @@
 
       /* calculate the average value for a single star */
-      if (catalog[i].average[j].code & STAR_BAD) continue;  
+      if (catalog[i].average[j].flags & STAR_BAD) continue;  
       m = catalog[i].average[j].measureOffset;
 
@@ -493,5 +493,5 @@
 
       /* calculate the average value for a single star */
-      if (catalog[i].average[j].code & STAR_BAD) continue;  
+      if (catalog[i].average[j].flags & STAR_BAD) continue;  
 
       Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
@@ -529,5 +529,5 @@
 
       /* calculate the average value for a single star */
-      if (catalog[i].average[j].code & STAR_BAD) continue;  
+      if (catalog[i].average[j].flags & STAR_BAD) continue;  
 
       dM = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
@@ -559,5 +559,5 @@
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Naverage; j++) {
-      if (catalog[i].average[j].code & STAR_BAD) continue;  
+      if (catalog[i].average[j].flags & STAR_BAD) continue;  
       dMrel = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
       bin = dMrel / 0.00025;
@@ -590,5 +590,5 @@
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Naverage; j++) {
-      if (catalog[i].average[j].code & STAR_BAD) continue;
+      if (catalog[i].average[j].flags & STAR_BAD) continue;
       xlist[N] = catalog[i].secfilt[PhotNsec*j+PhotSec].M;
       value    = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
Index: /trunk/Ohana/src/relphot/src/bcatalog.c
===================================================================
--- /trunk/Ohana/src/relphot/src/bcatalog.c	(revision 21507)
+++ /trunk/Ohana/src/relphot/src/bcatalog.c	(revision 21508)
@@ -34,6 +34,6 @@
       subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].M  = NAN;
       subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].dM = NAN;
-      subcatalog[0].average[Naverage].code &= ~ID_STAR_FEW;
-      subcatalog[0].average[Naverage].code &= ~ID_STAR_POOR;
+      subcatalog[0].average[Naverage].flags &= ~ID_STAR_FEW;
+      subcatalog[0].average[Naverage].flags &= ~ID_STAR_POOR;
     }
 
@@ -55,5 +55,6 @@
       /* select measurements by quality */
       // XXX ignore this criterion for REF measurements?
-      if (DophotSelect && (catalog[0].measure[offset].dophot != DophotValue)) continue;
+      // XXX chnage this to select by bitflags
+      if (DophotSelect && ((catalog[0].measure[offset].photFlags >> 16) != DophotValue)) continue;
 
       /* select measurements by mag limit */
Index: /trunk/Ohana/src/relphot/src/plot_scatter.c
===================================================================
--- /trunk/Ohana/src/relphot/src/plot_scatter.c	(revision 21507)
+++ /trunk/Ohana/src/relphot/src/plot_scatter.c	(revision 21508)
@@ -23,5 +23,5 @@
 
       /* calculate the average value for a single star */
-      if (catalog[i].average[j].code & STAR_BAD) continue;  
+      if (catalog[i].average[j].flags & STAR_BAD) continue;  
       m = catalog[i].average[j].measureOffset;
 
Index: /trunk/Ohana/src/relphot/src/relphot_objects.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 21507)
+++ /trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 21508)
@@ -42,12 +42,13 @@
       Nsecfilt = catalog.Nsecfilt;
       for (j = 0; j < catalog.Naverage; j++) {
-	catalog.average[j].code = 0;
+	catalog.average[j].flags = 0;
 	for (k = 0; k < Nsecfilt; k++) {
-	  catalog.secfilt[j*Nsecfilt + k].M  = NAN;
-	  catalog.secfilt[j*Nsecfilt + k].dM = NAN;
-	  catalog.secfilt[j*Nsecfilt + k].Xm = NAN_S_SHORT;
+	  catalog.secfilt[j*Nsecfilt + k].M    	= NAN;
+	  catalog.secfilt[j*Nsecfilt + k].M_20 	= NAN_S_SHORT;
+	  catalog.secfilt[j*Nsecfilt + k].M_80 	= NAN_S_SHORT;
+	  catalog.secfilt[j*Nsecfilt + k].dM   	= NAN;
+	  catalog.secfilt[j*Nsecfilt + k].Xm    = NAN_S_SHORT;
 	  catalog.secfilt[j*Nsecfilt + k].Ncode = 0;
 	  catalog.secfilt[j*Nsecfilt + k].Nused = 0;
-	  catalog.secfilt[j*Nsecfilt + k].dummy = 0;
 	}
 	m = catalog.average[j].measureOffset;
Index: /trunk/Ohana/src/relphot/src/select_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/select_images.c	(revision 21507)
+++ /trunk/Ohana/src/relphot/src/select_images.c	(revision 21508)
@@ -133,11 +133,11 @@
       image[nimage] = timage[i]; 
       /* always allow 'few' images to succeed, if possible */
-      if (image[nimage].code & ID_IMAGE_FEW) { 
-	image[nimage].code &= ~(ID_IMAGE_FEW | ID_IMAGE_POOR);
+      if (image[nimage].flags & ID_IMAGE_FEW) { 
+	image[nimage].flags &= ~(ID_IMAGE_FEW | ID_IMAGE_POOR);
       }
       if (RESET) {
 	assignMcal (&image[nimage], (double *) NULL, -1);
 	image[nimage].dMcal = NAN;
-	image[nimage].code &= ~ID_IMAGE_POOR;
+	image[nimage].flags &= ~ID_IMAGE_POOR;
       }
       line_number[nimage] = i;
Index: /trunk/Ohana/src/relphot/src/setMrelFinal.c
===================================================================
--- /trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 21507)
+++ /trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 21508)
@@ -54,6 +54,6 @@
   /* clear ID_STAR_POOR, ID_STAR_FEW, ID_MEAS_NOCAL values before writing ??? */
   for (i = 0; i < catalog[0].Naverage; i++) {
-    catalog[0].average[i].code &= ~ID_STAR_FEW;
-    catalog[0].average[i].code &= ~ID_STAR_POOR;
+    catalog[0].average[i].flags &= ~ID_STAR_FEW;
+    catalog[0].average[i].flags &= ~ID_STAR_POOR;
     m = catalog[0].average[i].measureOffset;
     for (j = 0; j < catalog[0].average[i].Nmeasure; j++, m++) {
@@ -102,5 +102,6 @@
 
       /* skip measurements with sat. dophot values */
-      if ((pass < 4) && (catalog[0].measure[m].dophot == 10)) goto skip;
+      // XXX we need to fix this: use the photFlags to ID saturation...
+      // if ((pass < 4) && (catalog[0].measure[m].dophot == 10)) goto skip;
 
       /* skip measurements from BAD images and mosaics */
@@ -116,6 +117,8 @@
       }
 
-      d1 = (catalog[0].measure[m].dophot == 1);
-      d2 = (catalog[0].measure[m].dophot == 2);
+      // d1 = (catalog[0].measure[m].dophot == 1);
+      // d2 = (catalog[0].measure[m].dophot == 2);
+      fprintf (stderr, "fix the dophot / type selections!");
+      abort ();
 
       /* skip measurements by measurement error */
Index: /trunk/Ohana/src/uniphot/src/find_image_sgroups.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/find_image_sgroups.c	(revision 21507)
+++ /trunk/Ohana/src/uniphot/src/find_image_sgroups.c	(revision 21508)
@@ -27,5 +27,5 @@
   for (i = 0; i < Nimage; i++) {
     if (imlink[i].sgroup != NULL) continue;
-    if (image[i].code & ID_IMAGE_NOCAL) continue;
+    if (image[i].flags & ID_IMAGE_NOCAL) continue;
 
     /* new sgroup, set ref coords */
@@ -52,5 +52,5 @@
 
     for (j = 0; j < Nimage; j++) {
-      if (image[j].code & ID_IMAGE_NOCAL) continue;
+      if (image[j].flags & ID_IMAGE_NOCAL) continue;
       if (imlink[j].sgroup != NULL) continue;
 
Index: /trunk/Ohana/src/uniphot/src/find_image_tgroups.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/find_image_tgroups.c	(revision 21507)
+++ /trunk/Ohana/src/uniphot/src/find_image_tgroups.c	(revision 21508)
@@ -63,5 +63,5 @@
       if (image[j].tzero < tmin[i]) continue;
       if (image[j].tzero > tmax[i]) continue;
-      if (image[j].code & ID_IMAGE_NOCAL) continue;
+      if (image[j].flags & ID_IMAGE_NOCAL) continue;
       
       group[i].image[Nentry] = &image[j];
Index: /trunk/Ohana/src/uniphot/src/fit_groups.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/fit_groups.c	(revision 21507)
+++ /trunk/Ohana/src/uniphot/src/fit_groups.c	(revision 21508)
@@ -16,5 +16,5 @@
   for (i = 0; i < Ntgroup; i++) {
     for (j = Nlist = 0; j < tgroup[i].Nimage; j++) {
-      if (tgroup[i].image[j][0].code & IMAGE_BAD) continue;
+      if (tgroup[i].image[j][0].flags & IMAGE_BAD) continue;
       sgroup = (Group *) tgroup[i].imlink[j][0].sgroup;
       Mcal = tgroup[i].image[j][0].Mcal;
@@ -54,5 +54,5 @@
   for (i = 0; i < Nsgroup; i++) {
     for (j = Nlist = 0; j < sgroup[i].Nimage; j++) {
-      if (sgroup[i].image[j][0].code & IMAGE_BAD) continue;
+      if (sgroup[i].image[j][0].flags & IMAGE_BAD) continue;
       tgroup = (Group *) sgroup[i].imlink[j][0].tgroup;
       Mcal = sgroup[i].image[j][0].Mcal;
Index: /trunk/Ohana/src/uniphot/src/subset_images.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/subset_images.c	(revision 21507)
+++ /trunk/Ohana/src/uniphot/src/subset_images.c	(revision 21508)
@@ -16,5 +16,5 @@
   for (i = 0; i < Nimage; i++) {
       
-    image[i].code |= ID_IMAGE_NOCAL;
+    image[i].flags |= ID_IMAGE_NOCAL;
 
     /* select images by photcode */
@@ -27,5 +27,5 @@
       if (image[i].tzero > TSTOP) continue;
     }
-    image[i].code &= ~ID_IMAGE_NOCAL;
+    image[i].flags &= ~ID_IMAGE_NOCAL;
     keep[Nkeep] = i;
     Nkeep ++;
Index: /trunk/Ohana/src/uniphot/src/update.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/update.c	(revision 21507)
+++ /trunk/Ohana/src/uniphot/src/update.c	(revision 21508)
@@ -16,5 +16,5 @@
   /* clear the NOCAL flags */
   for (i = 0; i < Nimage; i++) {
-    image[i].code &= ~ID_IMAGE_NOCAL;
+    image[i].flags &= ~ID_IMAGE_NOCAL;
   }
 
