Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvopsps/include/dvopsps.h
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvopsps/include/dvopsps.h	(revision 37197)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvopsps/include/dvopsps.h	(revision 37198)
@@ -14,4 +14,5 @@
   unsigned int ippDetectID;
   int imageID;
+  int catID;
   double ra;
   double dec;
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvopsps/src/DetectionOps.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvopsps/src/DetectionOps.c	(revision 37197)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvopsps/src/DetectionOps.c	(revision 37198)
@@ -51,4 +51,5 @@
   GET_COLUMN(ippDetectID , "ippDetectID", int);
   GET_COLUMN(imageID     , "imageID",     int);
+  GET_COLUMN(catID       , "catID",       int);
   GET_COLUMN(ra          , "ra",          double); // XXX signed vs unsigned?
   GET_COLUMN(dec         , "dec",         double);
@@ -76,4 +77,5 @@
     detections[i].ippDetectID  = ippDetectID[i];
     detections[i].imageID      = imageID[i];    
+    detections[i].catID        = catID[i];    
     detections[i].ra           = ra[i];         
     detections[i].dec          = dec[i];        
@@ -99,4 +101,5 @@
   free (ippDetectID);
   free (imageID    );
+  free (catID      );
   free (ra         );
   free (dec        );
@@ -166,4 +169,5 @@
   gfits_define_bintable_column (&theader, "J", "ippDetectID", NULL, NULL, 1.0, FT_BZERO_INT32);
   gfits_define_bintable_column (&theader, "J", "imageID",     NULL, NULL, 1.0, FT_BZERO_INT32);
+  gfits_define_bintable_column (&theader, "J", "catID",       NULL, NULL, 1.0, FT_BZERO_INT32);
   gfits_define_bintable_column (&theader, "D", "ra",          NULL, NULL, 1.0, 0.0);
   gfits_define_bintable_column (&theader, "D", "dec",         NULL, NULL, 1.0, 0.0);
@@ -191,4 +195,5 @@
   uint32_t   *ippDetectID ; ALLOCATE (ippDetectID ,  uint32_t, Ndetections);
    int32_t   *imageID     ; ALLOCATE (imageID     ,   int32_t, Ndetections);
+   int32_t   *catID       ; ALLOCATE (catID       ,   int32_t, Ndetections);
   double     *ra          ; ALLOCATE (ra          ,  double,   Ndetections);
   double     *dec         ; ALLOCATE (dec         ,  double,   Ndetections);
@@ -214,4 +219,5 @@
     ippDetectID[i] = detections[i].ippDetectID ;
     imageID[i]     = detections[i].imageID     ;
+    catID[i]       = detections[i].catID       ;
     ra[i]          = detections[i].ra          ;
     dec[i]         = detections[i].dec         ;
@@ -237,4 +243,5 @@
   gfits_set_bintable_column (&theader, &ftable, "ippDetectID", ippDetectID , Ndetections);
   gfits_set_bintable_column (&theader, &ftable, "imageID",     imageID     , Ndetections);
+  gfits_set_bintable_column (&theader, &ftable, "catID",       catID       , Ndetections);
   gfits_set_bintable_column (&theader, &ftable, "ra",          ra          , Ndetections);
   gfits_set_bintable_column (&theader, &ftable, "dec",         dec         , Ndetections);
@@ -258,4 +265,5 @@
   free (ippDetectID );
   free (imageID     );
+  free (catID       );
   free (ra          );
   free (dec         );
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvopsps/src/insert_detections_dvopsps_catalog.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvopsps/src/insert_detections_dvopsps_catalog.c	(revision 37197)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvopsps/src/insert_detections_dvopsps_catalog.c	(revision 37198)
@@ -6,4 +6,6 @@
 Detections *detections = NULL;
 
+static float ZeroPoint = 25.0;
+
 int init_detections () {
   NDETECTIONS = 1000;
@@ -15,4 +17,6 @@
 
   off_t i, j;
+
+  ZeroPoint = GetZeroPoint();
 
   int missingID = 0;
@@ -91,4 +95,6 @@
 
   off_t found = 0;
+
+  ZeroPoint = GetZeroPoint();
 
   INITTIME;
@@ -179,6 +185,7 @@
   }
 
-  PrintIOBuffer (buffer, "INSERT INTO dvoDetectionFull (objID, detectID, ippObjID, ippDetectID, imageID, ");
-  PrintIOBuffer (buffer, "ra, dec_, raErr, decErr, zp, telluricExt, airmass, expTime, Mpsf, dMpsf, Mkron, dMkron, Map, dMap, flags) VALUES \n");
+  PrintIOBuffer (buffer, "INSERT INTO dvoDetectionFull (objID, detectID, ippObjID, ippDetectID, imageID, catID, ");
+  PrintIOBuffer (buffer, "ra, dec_, raErr, decErr, zp, telluricExt, airmass, expTime, ");
+  PrintIOBuffer (buffer, "Mpsf, dMpsf, Mkron, dMkron, Map, dMap, flags) VALUES \n");
 
   return TRUE;
@@ -231,4 +238,5 @@
   PrintIOBuffer (buffer,  "%u,  ", detection->ippDetectID); // ippDetectID
   PrintIOBuffer (buffer,  "%d,  ", detection->imageID);	    // imageID
+  PrintIOBuffer (buffer,  "%d,  ", detection->catID);	    // DVO Catalog ID
 
   PRINT_FLOAT(buffer, detection->ra,          "%.8f, ");
@@ -252,4 +260,35 @@
 }
 
+float getMagFromValueOrFlux (float flux, float mag, float zp) {
+
+  // first, if mag is finite, use mag:
+  if (isfinite(mag)) {
+    return (zp + mag - ZeroPoint);
+  }
+
+  if (isfinite(flux) && (flux > 0.0)) {
+    fprintf (stderr, "how did we get here?\n");
+    return (zp - 2.5*log10(flux));
+  }
+
+  if (isfinite(flux) && (flux <= 0.0)) {
+    return (NAN);
+  }
+  return (NAN);
+}
+
+float getdMagFromValueOrFlux (float flux, float dflux, float dmag) {
+
+  // first, if mag is finite, use mag:
+  if (isfinite(dmag)) {
+    return (dmag);
+  }
+
+  if (isfinite(flux) && isfinite(dflux)) {
+    return (dflux / fabs(flux));
+  }
+  return (NAN);
+}
+
 int assign_detection_values (Detections *detection, Measure *measure, Average *average) {
 
@@ -266,7 +305,8 @@
   detection->objID	  = average->extID;   // objID
   detection->detectID     = measure->extID;   // detectID
-  detection->ippObjID     = ippObjID;	       // ippObID
+  detection->ippObjID     = ippObjID;	      // ippObjID
   detection->ippDetectID  = measure->detID;   // ippDetectID
   detection->imageID      = measure->imageID; // imageID
+  detection->catID        = measure->catID;   // catID
 
   detection->ra 	  = measure->R;    // ra
@@ -281,11 +321,12 @@
 
   // XXX clean this up with dvo_photcode_ops calls:
-  detection->Mpsf   = (measure->FluxPSF  > 0.0) ? zp - 2.5*log10(measure->FluxPSF)  : NAN;
-  detection->Mkron  = (measure->FluxKron > 0.0) ? zp - 2.5*log10(measure->FluxKron) : NAN;
-  detection->Map    = (measure->FluxAp   > 0.0) ? zp - 2.5*log10(measure->FluxAp)   : NAN;
-
-  detection->dMpsf  = (measure->FluxPSF  > 0.0) ? measure->dFluxPSF / measure->FluxPSF : NAN;
-  detection->dMkron = (measure->FluxKron > 0.0) ? measure->dFluxKron/ measure->FluxKron: NAN;
-  detection->dMap   = (measure->FluxAp   > 0.0) ? measure->dFluxAp  / measure->FluxAp  : NAN;
+  // if (isfinite(measure->FluxPSF) && (measure->FluxPSF < 0.0)) 
+  detection->Mpsf  = getMagFromValueOrFlux (measure->FluxPSF,  measure->M,     zp);
+  detection->Mkron = getMagFromValueOrFlux (measure->FluxKron, measure->Mkron, zp);
+  detection->Map   = getMagFromValueOrFlux (measure->FluxAp,   measure->Map,   zp);
+
+  detection->dMpsf  = getdMagFromValueOrFlux (measure->FluxPSF,  measure->dFluxPSF,  measure->dM);
+  detection->dMkron = getdMagFromValueOrFlux (measure->FluxKron, measure->dFluxKron, measure->dMkron);
+  detection->dMap   = getdMagFromValueOrFlux (measure->FluxAp,   measure->dFluxAp,   measure->dMap);
 
   detection->flags  = measure->dbFlags;  // flags
