Index: trunk/ippToPsps/jython/stackbatch.py
===================================================================
--- trunk/ippToPsps/jython/stackbatch.py	(revision 40280)
+++ trunk/ippToPsps/jython/stackbatch.py	(revision 40281)
@@ -307,12 +307,13 @@
 
         # zp correction should comes from DVO (unless image is not in DVO)
-        zpImage = self.scratchDb.getImageZeroPoint(stackID)
-
-        if (zpImage == "NULL"):
-            zpImage = zp
+        (zpImagePSF, zpImageAPER) = self.scratchDb.getStackZeroPoint(stackID)
+
+        # zp is the value in the header, zpImage includes the McalPSF value from DVO
+        if (zpImagePSF  == "NULL"): zpImagePSF  = zp
+        if (zpImageAPER == "NULL"): zpImageAPER = zpImagePSF
 
         detectionThreshold = "NULL"
-        if (magref != "NULL") and (zpImage != "NULL") and (expTime != "NULL"):
-            detectionThreshold = magref + zpImage + 2.5 * math.log10(expTime)
+        if (magref != "NULL") and (zpImagePSF != "NULL") and (expTime != "NULL"):
+            detectionThreshold = magref + zpImagePSF + 2.5 * math.log10(expTime)
 
         # insert stack metadata into table
@@ -340,5 +341,6 @@
 #       sqlLine.group("psfFwhm_max",        fwhm_maj_uq)
         sqlLine.group("astroScat",          astroscat)
-        sqlLine.group("photoZero",          zpImage)
+        sqlLine.group("photoZero",          zpImagePSF)
+        sqlLine.group("photoZeroAperture",  zpImageAPER)
         sqlLine.group("photoScat",          zpErr)
         sqlLine.group("nAstroRef",          header['NASTRO'])
@@ -429,5 +431,6 @@
             sqlLine.group("a."+filter+"Epoch",         str(stackEpoch))
 
-            sqlLine.group("a."+filter+"PSFMag",        "b.Mpsf")
+            # the Mag values below are set in dvopsps/insert_detections_dvopsps_catalog.c using either zpPSF (PSFMag) or zpAPER (KronMag, ApMag)
+            sqlLine.group("a."+filter+"PSFMag",        "b.Mpsf") 
             sqlLine.group("a."+filter+"PSFMagErr",     "b.dMpsf")
             sqlLine.group("a."+filter+"KronMag",       "b.Mkron")
@@ -557,14 +560,15 @@
             sqlLine = sqlUtility("UPDATE " + tablename + " AS a, " + self.scratchDb.dvoDetectionTable + " as b SET")
             sqlLine.group("a."+filter+"expTime",       "b.expTime")
-            sqlLine.group("a."+filter+"zp",            "b.zp")
-            sqlLine.group("a."+filter+"PSFFlux",       "a."+filter+"PSFFlux     * b.zpFactor")
-            sqlLine.group("a."+filter+"PSFFluxErr",    "a."+filter+"PSFFluxErr  * b.zpFactor")
-            sqlLine.group("a."+filter+"ApFlux",        "a."+filter+"ApFlux      * b.zpFactor")
-            sqlLine.group("a."+filter+"ApFluxErr",     "a."+filter+"ApFluxErr   * b.zpFactor")
-            sqlLine.group("a."+filter+"KronFlux",      "a."+filter+"KronFlux    * b.zpFactor")
-            sqlLine.group("a."+filter+"KronFluxErr",   "a."+filter+"KronFluxErr * b.zpFactor")
-
-            sqlLine.group("a."+filter+"sky",           "a."+filter+"sky         * b.zpFactor")
-            sqlLine.group("a."+filter+"skyErr",        "a."+filter+"skyErr      * b.zpFactor")
+            sqlLine.group("a."+filter+"zp",            "b.zpPSF")
+            sqlLine.group("a."+filter+"zpAPER",        "b.zpAPER")
+            sqlLine.group("a."+filter+"PSFFlux",       "a."+filter+"PSFFlux     * b.zpFactorPSF")
+            sqlLine.group("a."+filter+"PSFFluxErr",    "a."+filter+"PSFFluxErr  * b.zpFactorPSF")
+            sqlLine.group("a."+filter+"ApFlux",        "a."+filter+"ApFlux      * b.zpFactorAPER")
+            sqlLine.group("a."+filter+"ApFluxErr",     "a."+filter+"ApFluxErr   * b.zpFactorAPER")
+            sqlLine.group("a."+filter+"KronFlux",      "a."+filter+"KronFlux    * b.zpFactorAPER")
+            sqlLine.group("a."+filter+"KronFluxErr",   "a."+filter+"KronFluxErr * b.zpFactorAPER")
+
+            sqlLine.group("a."+filter+"sky",           "a."+filter+"sky         * b.zpFactorAPER")
+            sqlLine.group("a."+filter+"skyErr",        "a."+filter+"skyErr      * b.zpFactorAPER")
 
             # where should these go?
@@ -700,4 +704,7 @@
         imageID = self.imageIDs[filter]
         self.logger.infoPair("selecting imageID", imageID)
+
+        # NOTE: for model magnitudes, we use zpPSF not zpAPER since they are PSF-matched
+        # (maybe that is the wrong solution?)
 
         # model calibrated magnitude = instrumental magnitude + 2.5*log10(exptime) + ZP (added below from dvoDetectionTable)
@@ -765,5 +772,5 @@
         sqlLine = sqlUtility("UPDATE " + tablename + " AS a, " + self.scratchDb.dvoDetectionTable + " as b SET")
 
-        sqlLine.group("a." + filter + model + "Mag",       "a." + filter + model + "Mag       + b.zp")
+        sqlLine.group("a." + filter + model + "Mag",       "a." + filter + model + "Mag       + b.zpPSF")
 
         sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID AND b.imageID = " + str(imageID))
@@ -884,5 +891,4 @@
         # PETRO_MAG is instrumental + 2.5log(exptime) + FPA.ZP
         # we want to apply the DVO zero point, so remove FPA.ZP:
-        ## XXX is this statement true: is FPA.ZP + 2.5log(exptime) applied to PETRO_MAG?
 
         hdrZP   = str(self.zpImage[filter])
@@ -897,5 +903,5 @@
         sqlLine.group("a."+filter+"haveData",         "'1'")
 
-        sqlLine.group("a." + filter + "petMag",       "b.PETRO_MAG - " + hdrZP)  
+        sqlLine.group("a." + filter + "petMag",       "b.PETRO_MAG - " + hdrZP) # note that this is **subtracting** the hdrZP (zpAPER is added in below)
         sqlLine.group("a." + filter + "petMagErr",    "1.0 / b.PETRO_MAG_ERR")  # still inverted? (yes)
 
@@ -913,5 +919,5 @@
         # modify petMag to apply the zero point
         sqlLine = sqlUtility("UPDATE " + tablename + " AS a, " + self.scratchDb.dvoDetectionTable + " as b SET")
-        sqlLine.group("a." + filter + "petMag",       "a." + filter + "petMag    + b.zp")
+        sqlLine.group("a." + filter + "petMag",       "a." + filter + "petMag    + b.zpAPER")
         sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID AND b.imageID = " + str(imageID))
 
@@ -1019,5 +1025,5 @@
         # care of this, but does not?
 
-#chopping this out to see if it still works
+        # chopping this out to see if it still works
 
         #sqlLine = sqlUtility("UPDATE " + tablename + " SET")
@@ -1032,9 +1038,9 @@
 
         #sql = sqlLine.makeEquals("")
-        
-#        try: self.scratchDb.execute(sql)
-#        except:
-#            self.logger.errorPair('failed sql',sql)
-#            raise
+         
+        #        try: self.scratchDb.execute(sql)
+        #        except:
+        #            self.logger.errorPair('failed sql',sql)
+        #            raise
 
         # set the flux values from the cmf file
@@ -1112,7 +1118,7 @@
             pspsNum  = str(int(number) + 2)
             field = filter + prefix + "flxR" + pspsNum
-            sqlLine.group(field,               field         + " * b.zpFactor")
-            sqlLine.group(field + "Err",       field + "err" + " * b.zpFactor")
-            sqlLine.group(field + "Std",       field + "Std" + " * b.zpFactor")
+            sqlLine.group(field,               field         + " * b.zpFactorAPER")
+            sqlLine.group(field + "Err",       field + "err" + " * b.zpFactorAPER")
+            sqlLine.group(field + "Std",       field + "Std" + " * b.zpFactorAPER")
 
         sql = sqlLine.makeEquals("WHERE a.objID = b.objID AND a." + filter + "ippDetectID = b.ippDetectID AND b.imageID = " + str(imageID))
@@ -1122,4 +1128,5 @@
             self.logger.infoPair("failed sql",sql)
             raise
+
     '''
     Populates the StackToImage table
