Index: /trunk/Ohana/src/libdvo/Makefile
===================================================================
--- /trunk/Ohana/src/libdvo/Makefile	(revision 17189)
+++ /trunk/Ohana/src/libdvo/Makefile	(revision 17190)
@@ -63,4 +63,5 @@
 $(SRC)/dvo_convert_PS1_DEV_1.$(ARCH).o \
 $(SRC)/dvo_convert_PS1_DEV_2.$(ARCH).o \
+$(SRC)/dvo_convert_PS1_DEV_3.$(ARCH).o \
 $(SRC)/skyregion_io.$(ARCH).o    \
 $(SRC)/skyregion_gsc.$(ARCH).o    \
Index: /trunk/Ohana/src/libdvo/doc/dvo-flags.txt
===================================================================
--- /trunk/Ohana/src/libdvo/doc/dvo-flags.txt	(revision 17190)
+++ /trunk/Ohana/src/libdvo/doc/dvo-flags.txt	(revision 17190)
@@ -0,0 +1,39 @@
+
+There are several types of quality flags used within DVO.  There are
+flags associated with the measurements, the average objects, and the
+images.  
+
+measure->photFlags : these flags are supplied by the photometry
+		     analysis program which produced the data.  They
+		     may be directly defined by the program, as in the
+		     case of psphot, or they may be translations of
+		     detection metadata supplied by the data source
+		     (eg, 2MASS or SDSS).  These flags are read only:
+		     the are set by addstar, and not modified by any
+		     of the DVO database manipulation programs.  
+
+measure->dbFlags : these flags are used within DVO to note the ways in
+		   which the detetion is determined to be special by
+		   the database analysis programs.  These flags may be
+		   set by addstar on ingest, or by relphot, relastro,
+		   or equivalent programs.  Here are the possible
+		   values for dbFlags:
+
+NOTE: update these names and use an enum:		   
+# define ID_MEAS_NOCAL        0x0001 : detection ignored for this analysis (photcode, time range) -- internal only
+# define ID_MEAS_POOR_PHOTOM  0x0002 : detection is photometry outlier
+# define ID_MEAS_SKIP_PHOTOM  0x0004 : detection was ignored for photometry measurement
+# define ID_MEAS_AREA         0x0008 : detection near image edge
+# define ID_MEAS_POOR_ASTROM  0x0010 : detection is astrometry outlier
+# define ID_MEAS_SKIP_ASTROM  0x0020 : detection was ignored for astrometry measurement
+# define ID_MEAS_UNDEF_1      0x0040 : unused
+# define ID_MEAS_UNDEF_2      0x0080 : unused
+# define BLEND_IMAGE          0x0100 : detection is within radius of multiple objects
+# define BLEND_CATALOG        0x0200 : multiple detections within radius of object
+# define ID_MEAS_UNDEF_3      0x0400 : unused
+# define ID_MEAS_UNDEF_4      0x0800 : unused
+# define BLEND_IMAGE_NEIGHBOR 0x1000 : detection is within radius of multiple objects across catalogs
+# define ID_ARTIFACT          0x2000 : detection is thought to be non-astronomical
+# define ID_MEAS_UNDEF_5      0x4000 : unused
+# define ID_MEAS_UNDEF_6      0x8000 : unused
+
Index: /trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /trunk/Ohana/src/libdvo/include/dvo.h	(revision 17189)
+++ /trunk/Ohana/src/libdvo/include/dvo.h	(revision 17190)
@@ -98,13 +98,25 @@
 
 /* Measure.flags values */
-# define ID_MEAS_NOCAL        0x0001
-# define ID_MEAS_POOR         0x0002
-# define ID_MEAS_SKIP         0x0004
-# define ID_MEAS_AREA         0x0008
-# define BLEND_IMAGE          0x0100 
-# define BLEND_CATALOG        0x0200
-# define BLEND_IMAGE_NEIGHBOR 0x1000
-# define ID_MEAS_TRAIL        0x2000
-# define ID_MEAS_GHOST        0x4000
+// XXX replace the # defines with typedef enum
+# define ID_MEAS_NOCAL        0x0001  /* detection ignored for this analysis (photcode, time range) -- internal only */
+# define ID_MEAS_POOR_PHOTOM  0x0002  /* detection is photometry outlier					     */	  
+# define ID_MEAS_SKIP_PHOTOM  0x0004  /* detection was ignored for photometry measurement			     */	  
+# define ID_MEAS_AREA         0x0008  /* detection near image edge						     */
+# define ID_MEAS_POOR_ASTROM  0x0010  /* detection is astrometry outlier					     */	  
+# define ID_MEAS_SKIP_ASTROM  0x0020  /* detection was ignored for astrometry measurement			     */	  
+# define ID_MEAS_UNDEF_1      0x0040  /* unused									     */
+# define ID_MEAS_UNDEF_2      0x0080  /* unused									     */
+# define ID_MEAS_BLEND_MEAS   0x0100  /* detection is within radius of multiple objects				     */
+# define ID_MEAS_BLEND_OBJ    0x0200  /* multiple detections within radius of object				     */
+# define ID_MEAS_UNDEF_3      0x0400  /* unused									     */
+# define ID_MEAS_UNDEF_4      0x0800  /* unused									     */
+# define ID_MEAS_BLEND_MEAS_X 0x1000  /* detection is within radius of multiple objects across catalogs		     */
+# define ID_MEAS_ARTIFACT     0x2000  /* detection is thought to be non-astronomical				     */
+# define ID_MEAS_UNDEF_5      0x4000  /* unused									     */
+# define ID_MEAS_UNDEF_6      0x8000  /* unused									     */
+
+// XXX we used these names previously in markstar: replace with ID_MEAS_ARTIFACT
+// # define ID_MEAS_TRAIL        0x2000
+// # define ID_MEAS_GHOST        0x4000
 
 /* some subtle distinctions between the blend flags:
Index: /trunk/Ohana/src/libdvo/include/ps1_dev_3_defs.h
===================================================================
--- /trunk/Ohana/src/libdvo/include/ps1_dev_3_defs.h	(revision 17190)
+++ /trunk/Ohana/src/libdvo/include/ps1_dev_3_defs.h	(revision 17190)
@@ -0,0 +1,2 @@
+PhotCode *PhotCode_PS1_DEV_3_To_Internal (PhotCode_PS1_DEV_3 *in, int Nvalues);
+PhotCode_PS1_DEV_3 *PhotCode_Internal_To_PS1_DEV_3 (PhotCode *in, int Nvalues);
Index: /trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 17189)
+++ /trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 17190)
@@ -56,4 +56,10 @@
     photcode = PhotCode_PS1_DEV_2_To_Internal (photcode_ps1_dev_2, Ncode);
     free (photcode_ps1_dev_2);
+  } 
+
+  if (!strcmp (extname, "DVO_PHOTCODE_PS1_DEV_3")) {
+    PhotCode_PS1_DEV_3 *photcode_ps1_dev_3 = gfits_table_get_PhotCode_PS1_DEV_3 (&db.ftable, &Ncode, &db.swapped);
+    photcode = PhotCode_PS1_DEV_3_To_Internal (photcode_ps1_dev_3, Ncode);
+    free (photcode_ps1_dev_3);
   } 
 
Index: /trunk/Ohana/src/libdvo/src/LoadPhotcodesText.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/LoadPhotcodesText.c	(revision 17189)
+++ /trunk/Ohana/src/libdvo/src/LoadPhotcodesText.c	(revision 17190)
@@ -30,4 +30,5 @@
   char C1[32], C2[32], Slope[32], Color[32], Primary[32];
   char astromErrSys[32], astromErrScale[32], astromErrMagScale[32], photomErrSys[32];
+  char astromPoorMask[32], astromBadMask[32], photomPoorMask[32], photomBadMask[32];
 
   table = GetPhotcodeTable ();
@@ -61,6 +62,6 @@
     for (c = line; isspace (*c); c++);
     if (*c == '#') continue;
-    Nfield = sscanf (c, "%d %s %s %s %s %s %s %s %s %s %s %s %s %s %s", 
-		     &code, name, type, Zero, Airmass, Offset, C1, C2, Slope, Color, Primary, astromErrSys, astromErrScale, astromErrMagScale, photomErrSys);
+    Nfield = sscanf (c, "%d %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s", 
+		     &code, name, type, Zero, Airmass, Offset, C1, C2, Slope, Color, Primary, astromErrSys, astromErrScale, astromErrMagScale, photomErrSys, astromPoorMask, astromBadMask, photomPoorMask, photomBadMask);
 
     switch (Nfield) {
@@ -71,10 +72,28 @@
 	strcpy (astromErrMagScale, "0.0");
 	strcpy (photomErrSys,      "0.0");
+	strcpy (astromPoorMask,    "0");
+	strcpy (astromBadMask,     "0");
+	strcpy (photomPoorMask,    "0");
+	strcpy (photomBadMask,     "0");
 	break;
       case 14: 
 	// allow only astrom elements
 	strcpy (photomErrSys,      "0.0");
+	strcpy (astromPoorMask,    "0");
+	strcpy (astromBadMask,     "0");
+	strcpy (photomPoorMask,    "0");
+	strcpy (photomBadMask,     "0");
 	break;
       case 15: 
+	strcpy (astromPoorMask,    "0");
+	strcpy (astromBadMask,     "0");
+	strcpy (photomPoorMask,    "0");
+	strcpy (photomBadMask,     "0");
+	break;
+      case 17: 
+	strcpy (photomPoorMask,    "0");
+	strcpy (photomBadMask,     "0");
+	break;
+      case 19: 
 	// all fields defined
 	break;
@@ -116,4 +135,9 @@
     photcode[Ncode].astromErrMagScale = atof (astromErrMagScale);
     photcode[Ncode].photomErrSys      = atof (photomErrSys);
+
+    photcode[Ncode].astromPoorMask    = strtol (astromPoorMask, NULL, 0);
+    photcode[Ncode].astromBadMask     = strtol (astromBadMask, NULL, 0);
+    photcode[Ncode].photomPoorMask    = strtol (photomPoorMask, NULL, 0);
+    photcode[Ncode].photomBadMask     = strtol (photomBadMask, NULL, 0);
 
     switch (photcode[Ncode].type) {
Index: /trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 17189)
+++ /trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 17190)
@@ -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_2 *photcode_output = PhotCode_Internal_To_PS1_DEV_2 (table[0].code, table[0].Ncode);
+  PhotCode_PS1_DEV_3 *photcode_output = PhotCode_Internal_To_PS1_DEV_3 (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_2 (&db.ftable, photcode_output, table[0].Ncode);
+  gfits_table_set_PhotCode_PS1_DEV_3 (&db.ftable, photcode_output, table[0].Ncode);
   gfits_db_save (&db);
   gfits_db_close (&db);
Index: /trunk/Ohana/src/libdvo/src/SavePhotcodesText.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/SavePhotcodesText.c	(revision 17189)
+++ /trunk/Ohana/src/libdvo/src/SavePhotcodesText.c	(revision 17190)
@@ -41,4 +41,8 @@
   }
 
+  fprintf (f, "#                                           airmass      color                         astrometry  mag    photom  astrom mask    photom mask\n");
+  fprintf (f, "# code  name                type    zero  slope offset c1    c2   slope   zero  equiv  sys scale   scale  sys     poor   bad     poor   bad\n");
+  //          "  1     g                    sec   0.000  0.000 0.000  1003  1004 0.0160     0  1051   0.000 0.000 0.000  0.000   0xffff 0xffff  0xffff 0xffff
+
   for (i = 0; i < table[0].Ncode; i++) {
     switch (table[0].code[i].type) {
@@ -72,8 +76,21 @@
 
     for (j = 0; j < table[0].code[i].Nc - 1; j++) {
-      fprintf (f, " %f,", table[0].code[i].X[j]);
+      fprintf (f, " %6.4f,", table[0].code[i].X[j]);
     }
-    fprintf (f, "%f %d ", table[0].code[i].X[j], table[0].code[i].dX);
+    fprintf (f, "%6.4f %5d ", table[0].code[i].X[j], table[0].code[i].dX);
     PrintPhotcodeNamebyCode (f, "%5d ", table[0].code[i].equiv);
+
+    fprintf (f, "  %5.3f %5.3f %5.3f  %5.3f", 
+	     table[0].code[i].astromErrSys, 
+	     table[0].code[i].astromErrScale, 
+	     table[0].code[i].astromErrMagScale, 
+	     table[0].code[i].photomErrSys); 
+
+    fprintf (f, "   0x%04x 0x%04x  0x%04x 0x%04x", 
+	     table[0].code[i].astromPoorMask, 
+	     table[0].code[i].astromBadMask, 
+	     table[0].code[i].photomPoorMask, 
+	     table[0].code[i].photomBadMask); 
+
     fprintf (f, "\n");
   }
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c	(revision 17189)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c	(revision 17190)
@@ -354,5 +354,9 @@
     memcpy (out[i].X, in[i].X, 4*sizeof(float));            
 
-  }
-  return (out);
-}
+    out[i].photomPoorMask      = 0;
+    out[i].photomBadMask       = 0;
+    out[i].astromPoorMask      = 0;
+    out[i].astromBadMask       = 0;
+  }
+  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 17189)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_2.c	(revision 17190)
@@ -361,5 +361,10 @@
     out[i].astromErrMagScale = in[i].astromErrMagScale;
     out[i].photomErrSys      = in[i].photomErrSys;
-  }
-  return (out);
-}
+
+    out[i].photomPoorMask      = 0;
+    out[i].photomBadMask       = 0;
+    out[i].astromPoorMask      = 0;
+    out[i].astromBadMask       = 0;
+  }
+  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 17190)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_3.c	(revision 17190)
@@ -0,0 +1,77 @@
+# include <dvo.h>
+
+/* convert PS1_DEV_3 formats to internal formats */
+
+// XXX I am not yet ready to commit to a full PS1_DEV_3 release, but we can add photcode here for now
+
+PhotCode_PS1_DEV_3 *PhotCode_Internal_To_PS1_DEV_3 (PhotCode *in, int Nvalues) {
+
+  int i;
+  PhotCode_PS1_DEV_3 *out;
+
+  ALLOCATE (out, PhotCode_PS1_DEV_3, 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 *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_elixir.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c	(revision 17189)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c	(revision 17190)
@@ -355,5 +355,10 @@
     out[i].astromErrMagScale = 0.0;
     out[i].photomErrSys      = 0.0;
-  }
-  return (out);
-}
+
+    out[i].photomPoorMask      = 0;
+    out[i].photomBadMask       = 0;
+    out[i].astromPoorMask      = 0;
+    out[i].astromBadMask       = 0;
+  }
+  return (out);
+}
Index: /trunk/Ohana/src/libdvo/src/skyregion_gsc.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 17189)
+++ /trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 17190)
@@ -53,4 +53,9 @@
   
   // ohana_memdump (0);
+
+  if (filename == NULL) {
+    if (VERBOSE) fprintf (stderr, "template GSC Region file not defined\n");
+    return (NULL);
+  }
 
   f = fopen (filename, "r");
