Index: trunk/ippToPsps/jython/detectionbatch.py
===================================================================
--- trunk/ippToPsps/jython/detectionbatch.py	(revision 34441)
+++ trunk/ippToPsps/jython/detectionbatch.py	(revision 34615)
@@ -388,8 +388,11 @@
         # delete all detections with PSF_INST_MAG < 17.5, as decreed by Gene
         BEFORE = self.scratchDb.getRowCount(ippTableName)
-        sql = "DELETE FROM " + ippTableName + " WHERE PSF_INST_MAG < -17.5"
-        self.scratchDb.execute(sql)
+        #don't do this anymore
+        #sql = "DELETE FROM " + ippTableName + " WHERE PSF_INST_MAG < -17.5"
+        #self.scratchDb.execute(sql)
         #DELETED = BEFORE - self.scratchDb.getRowCount(ippTableName)
-        results['SATDET'] = BEFORE - self.scratchDb.getRowCount(ippTableName)
+        #results['SATDET'] = BEFORE - self.scratchDb.getRowCount(ippTableName)
+        #we aren't deletng these now
+        results['SATDET'] = 0 
         #self.logger.infoPair("Saturated detections", "%d deleted" % DELETED)
 
@@ -404,6 +407,6 @@
                ,xPosErr \
                ,yPosErr \
-               ,instFlux \
-               ,instFluxErr \
+               ,psfFlux \
+               ,psfFluxErr \
                ,peakADU \
                ,psfWidMajor \
@@ -411,11 +414,13 @@
                ,psfTheta \
                ,psfLikelihood \
-               ,psfCf \
+               ,psfQf \
                ,momentXX \
                ,momentXY \
                ,momentYY \
-               ,apMag \
+               ,apFlux \
                ,kronFlux \
                ,kronFluxErr \
+               ,psfQfPerfect \
+               ,psfChiSq \
                ,infoFlag \
                ,sky \
@@ -450,5 +455,7 @@
                ,KRON_FLUX / " + self.header['EXPTIME'] + " \
                ,KRON_FLUX_ERR / " + self.header['EXPTIME'] + " \
-               ,FLAGS\
+               ,PSF_QF_PERFECT \
+               ,PSF_CHISQ \
+               ,FLAGS2 << 32 | FLAGS\
                ,SKY \
                ,SKY_SIGMA \
@@ -459,19 +466,36 @@
                , " + str(self.config.dataRelease) + "\
                FROM " + ippTableName
+        # SA10 todo
+        # need to correct formulas for psfFlux/psfFluxErr
+        # need to correct apMag apFlux
+        # need to correct unites kronFlux/Err to correct units
+        # need to change psfqfperfect -> psfqfperfect PSF_QF_PERFECT?
+        # need to change psfchisq  PSF_CHI_SQ?
+        # need to change unites of sky/skyErr to adu/s/pixel
+        # need formula for sgsep/psflikelhood
+        # self.logger.info(sql)
         self.scratchDb.execute(sql)
 
         # now delete bad flux and bad chip positions
-        sql="DELETE FROM " + pspsTableName + " WHERE instFlux < 0.0000001" # TODO clearly a hack = 0 not allowed for instFlux
-        self.scratchDb.execute(sql)
-
+        #sql="DELETE FROM " + pspsTableName + " WHERE instFlux < 0.0000001" # TODO clearly a hack = 0 not allowed for instFlux
+        #self.scratchDb.execute(sql)
+
+        # add a instFlux = 0.0 -> 0.000001
+        sql="UPDATE " + pspsTableName + " SET psfFlux = 0.0000001 WHERE psfFlux =  0"
+        self.scratchDb.execute(sql)
+      
         # update cosmic ray and extended likelihoods
-        sql="UPDATE " + pspsTableName + " SET extendedLikelihood = 0, crLikelihood = 1.0 - psfLikelihood WHERE sgSep <= 0"
-        self.scratchDb.execute(sql)
-        sql="UPDATE " + pspsTableName + " SET crLikelihood = 0, extendedLikelihood = 1.0 - psfLikelihood WHERE sgSep > 0"
-        self.scratchDb.execute(sql)
+        #sql="UPDATE " + pspsTableName + " SET extendedLikelihood = 0, crLikelihood = 1.0 - psfLikelihood WHERE sgSep <= 0"
+        #self.scratchDb.execute(sql)
+        #sql="UPDATE " + pspsTableName + " SET crLikelihood = 0, extendedLikelihood = 1.0 - psfLikelihood WHERE sgSep > 0"
+        #self.scratchDb.execute(sql)
 
         # remove detections will NULL inst flux or NULL peak ADU
-        results['NULLINSTFLUX'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "instFlux")
-        results['NULLPEAKADU'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "peakADU")
+        # leave them in at this stage- don't delete them.
+        #results['NULLINSTFLUX'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "instFlux")
+        #results['NULLPEAKADU'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "peakADU")
+        #we don't delete thos anymore
+        results['NULLINSTFLUX'] = 0;
+        results['NULLPEAKADU'] = 0;
 
     '''
@@ -504,4 +528,41 @@
 
     '''
+    Populates the DetectionCalib table for this OTA
+    '''
+    def populateDetectionCalibTable(self, ota):
+
+        tableName = "DetectionCalib_" + ota
+        # drop then re-create table
+        self.scratchDb.dropTable(tableName)
+        sql = "CREATE TABLE " + tableName + " LIKE DetectionCalib"
+        try: self.scratchDb.execute(sql)
+        except: pass
+
+        # insert all detections into table
+        sql = "INSERT INTO " + tableName + " ( \
+               objID \
+               ,detectID \
+               ,ippObjID \
+               ,ippDetectID \
+               ,filterID \
+               ,surveyID \
+               ) \
+               SELECT \
+               objID \
+               ,detectID \
+               ,ippObjID \
+               ,ippDetectID \
+               ,filterID \
+               ,surveyID \
+               FROM Detection_" + ota
+        self.scratchDb.execute(sql)
+        #SA10 TODO
+        # insert ra/dec/calstuff into Table
+        # heather discovered dec is called 'dec_'
+        sql = "UPDATE " + tableName + " SET ra = -999, dec_ =-999, raErr = -999, decErr = -999, zp = -999, zpErr = -999, expTime = -999, airMass = -999"
+        self.scratchDb.execute(sql)
+        self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.config.dataRelease))
+        
+    '''
     Populates the Detection table for this OTA
     '''
@@ -608,5 +669,5 @@
                 extension = "XY%d%d_psf" % (x, y)
                 self.scratchDb.createIndex(extension, "IPP_IDET")
-
+        self.logger.infoPair("created indexes on", "IPP tables")      
     '''
     Updates provided table with DVO IDs from DVO table
@@ -620,5 +681,5 @@
                a.detectID = b.detectID, \
                a.objID = b.objID, \
-               a.infoFlag = b.flags << 32 | a.infoFlag \
+               a.infoFlag = b.flags << 46 | a.infoFlag \
                WHERE a.ippDetectID = b.ippDetectID \
                AND b.imageID = " + str(imageID)
@@ -658,13 +719,17 @@
                     self.logger.info("| %5s |            ------------- Bad astrometric solution : rejecting ------------                    |" % ota)
                     continue
-
+               # self.logger.info("populate stuff ");
                 # populate remainder of tables
                 self.populateDetectionTable(ota, results)
-
+               # self.logger.info("successful populate ");
                 # now add DVO IDs
                 self.updateDvoIDs("Detection_" + ota, self.imageIDs[ota])
+               # self.logger.info("updated dvoids")
                 results['NULLOBJID'] = self.scratchDb.reportAndDeleteRowsWithNULLS("Detection_" + ota, "objID")
-                self.updateImageID("Detection_" + ota, x, y) 
+                #self.logger.info("deleted nulls")
+                self.updateImageID("Detection_" + ota, x, y)
+               # self.logger.info("updateImageId")
                 rowCount = self.scratchDb.getRowCount("Detection_" + ota) 
+              #  self.logger.info("got row count")
                 self.logger.info("| %5s | %13d | %13d | %13d | %13d | %13d | %13d |", 
                         ota, 
@@ -675,5 +740,4 @@
                         results['NULLOBJID'],
                         rowCount)
-
                 totalOriginal = totalOriginal + results['ORIGINALTOTAL']
                 totalSatDet = totalSatDet + results['SATDET']
@@ -682,5 +746,5 @@
                 totalNullObjID = totalNullObjID + results['NULLOBJID']
                 totalDetections = totalDetections + rowCount
-
+                #self.logger.info("updated totals")
                 # check we have something in this Detection table TODO add this to table above
                 if rowCount < 1:
@@ -692,8 +756,13 @@
                        SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(self.imageIDs[ota]))
                 self.scratchDb.execute(sql)
-
+                
+                #self.logger.info("updated imagedata")
                 self.populateSkinnyObjectTable(ota)
+                #self.logger.info("updated skinnyobject")
                 self.populateObjectCalColorTable(ota)
-
+                #self.logger.info("updated objectcalcolor")
+                self.populateDetectionCalibTable(ota)
+                #self.logger.info("updated detectioncalibtable")
+                
                 # add these to list of tables to export later
                 self.tablesToExport.append("ImageMeta_" + ota)
@@ -701,8 +770,9 @@
                 self.tablesToExport.append("SkinnyObject_" + ota)
                 self.tablesToExport.append("ObjectCalColor_" + ota)
+                self.tablesToExport.append("DetectionCalib_" + ota)
                 tables.append("Detection_" + ota)
            
                 otaCount = otaCount + 1
-
+                #self.logger.info("end of ota") 
         # print totals
         self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+---------------+")
@@ -757,5 +827,5 @@
        else : regex = ".*.psf"
   
-       columns = "IPP_IDET X_PSF Y_PSF X_PSF_SIG Y_PSF_SIG PSF_INST_MAG PSF_INST_MAG_SIG PEAK_FLUX_AS_MAG PSF_MAJOR PSF_MINOR PSF_THETA EXT_NSIGMA PSF_QF MOMENTS_XX MOMENTS_XY MOMENTS_YY AP_MAG KRON_FLUX KRON_FLUX_ERR FLAGS SKY SKY_SIGMA EXT_NSIGMA"
+       columns = "IPP_IDET X_PSF Y_PSF X_PSF_SIG Y_PSF_SIG PSF_INST_MAG PSF_INST_MAG_SIG PEAK_FLUX_AS_MAG PSF_MAJOR PSF_MINOR PSF_THETA EXT_NSIGMA PSF_QF MOMENTS_XX MOMENTS_XY MOMENTS_YY AP_MAG KRON_FLUX KRON_FLUX_ERR FLAGS FLAGS2 SKY SKY_SIGMA EXT_NSIGMA PSF_QF_PERFECT PSF_CHISQ"
 
        return super(DetectionBatch, self).importIppTables(columns, regex)
Index: trunk/ippToPsps/jython/objectbatch.py
===================================================================
--- trunk/ippToPsps/jython/objectbatch.py	(revision 34441)
+++ trunk/ippToPsps/jython/objectbatch.py	(revision 34615)
@@ -114,6 +114,6 @@
         for filter in filters:
 
-            sql = "UPDATE Object \
-                   JOIN " + cpsTable + " AS cps ON (cps.row = (Object.row*" + str(filterCount) + ")-(" + str(filterCount) + " - " + str(filter[0]) + ")) \
+            sql = "UPDATE Object JOIN \
+                   " + cpsTable + " AS cps ON (cps.row = (Object.row* " + str(filterCount) + ")-(" + str(filterCount) + " - " + str(filter[0]) + ")) \
                    SET \
                    n" + filter[1] + " = NCODE \
@@ -122,14 +122,28 @@
                    ," + filter[1] + "MeanKronMag = MAG_KRON \
                    ," + filter[1] + "MeanKronMagErr = MAG_KRON_ERR \
-                   ," + filter[1] + "StackPSFFlux = FLUX_PSF \
-                   ," + filter[1] + "StackPSFFluxErr = FLUX_PSF_ERR \
+                   ," + filter[1] + "StackPSFMag = -2.5 * log10(FLUX_PSF) + 8.9 \
+                   ," + filter[1] + "StackPSFMagErr = FLUX_PSF_ERR / FLUX_PSF \
                    ," + filter[1] + "StackKronMag = -2.5 * log10(FLUX_KRON) + 8.9 \
                    ," + filter[1] + "StackKronMagErr = FLUX_KRON_ERR / FLUX_KRON \
                    ," + filter[1] + "20pct = MAG_20/1000 \
                    ," + filter[1] + "80pct = MAG_80/1000 \
-                   ," + filter[1] + "Flags = FLAGS "  
-                   
+                   ," + filter[1] + "Flags = FLAGS  \
+                   ," + filter[1] + "StackDetectID = STACK_ID"
+
+            #stackDetectID needs to be filled still for SA10 - currently at STACK_ID for test purposes
+            # everthing should be correctly calculated as mags for SA10       
+            #self.logger.info(sql)
+            self.scratchDb.execute(sql)
+
+        self.logger.infoPair("Calculating nDetections from", "n[filters]")
+        for filter in filters:
+            # now do a sum of n[filters], but do not include the ones with -999
+            sql = "UPDATE Object \
+                   SET nDetections = nDetections + n" + filter[1] + " \
+                   WHERE n" + filter[1] + " != -999"
+            self.scratchDb.execute(sql)
             
-            self.scratchDb.execute(sql)
+
+
 
         # now set to null all MeanMagErr values > 0.5 (cut set by Gene, 2012-04-12)
@@ -204,5 +218,5 @@
                ,decErr \
                ,nDetections \
-               ,extra \
+               ,qfPerfect \
                ,sgSep \
                ,dataRelease \
Index: trunk/ippToPsps/jython/stackbatch.py
===================================================================
--- trunk/ippToPsps/jython/stackbatch.py	(revision 34441)
+++ trunk/ippToPsps/jython/stackbatch.py	(revision 34615)
@@ -89,4 +89,5 @@
        self.scratchDb.dropTable("SkinnyObject")
        self.scratchDb.dropTable("ObjectCalColor")
+       self.scratchDb.dropTable("StackDetectionCalib")
 
        # delete IPP tables
@@ -140,43 +141,43 @@
 
         sql = "UPDATE StackApFlx AS a, SkyChip_xrad AS b SET \
-        "+prefix+"flxR1=b.APER_FLUX_1, \
-        "+prefix+"flxR1Err=b.APER_FLUX_ERR_1, \
-        "+prefix+"flxR1Std=b.APER_FLUX_STDEV_1, \
+        "+prefix+"flxR1=b.APER_FLUX_1 / " + str(self.expTime) + ", \
+        "+prefix+"flxR1Err=b.APER_FLUX_ERR_1 / " + str(self.expTime) + ", \
+        "+prefix+"flxR1Std=b.APER_FLUX_STDEV_1 / " + str(self.expTime) + ", \
         "+prefix+"flxR1Fill=b.APER_FILL_1, \
-        "+prefix+"flxR2=b.APER_FLUX_2, \
-        "+prefix+"flxR2Err=b.APER_FLUX_ERR_2, \
-        "+prefix+"flxR2Std=b.APER_FLUX_STDEV_2, \
+        "+prefix+"flxR2=b.APER_FLUX_2 / " + str(self.expTime) + ", \
+        "+prefix+"flxR2Err=b.APER_FLUX_ERR_2 / " + str(self.expTime) + ", \
+        "+prefix+"flxR2Std=b.APER_FLUX_STDEV_2 / " + str(self.expTime) + ", \
         "+prefix+"flxR2Fill=b.APER_FILL_2, \
-        "+prefix+"flxR3=b.APER_FLUX_3, \
-        "+prefix+"flxR3Err=b.APER_FLUX_ERR_3, \
-        "+prefix+"flxR3Std=b.APER_FLUX_STDEV_3, \
+        "+prefix+"flxR3=b.APER_FLUX_3 / " + str(self.expTime) + ", \
+        "+prefix+"flxR3Err=b.APER_FLUX_ERR_3 / " + str(self.expTime) + ", \
+        "+prefix+"flxR3Std=b.APER_FLUX_STDEV_3 / " + str(self.expTime) + ", \
         "+prefix+"flxR3Fill=b.APER_FILL_3, \
-        "+prefix+"flxR4=b.APER_FLUX_4, \
-        "+prefix+"flxR4Err=b.APER_FLUX_ERR_4, \
-        "+prefix+"flxR4Std=b.APER_FLUX_STDEV_4, \
+        "+prefix+"flxR4=b.APER_FLUX_4 / " + str(self.expTime) + ", \
+        "+prefix+"flxR4Err=b.APER_FLUX_ERR_4 / " + str(self.expTime) + ", \
+        "+prefix+"flxR4Std=b.APER_FLUX_STDEV_4 / " + str(self.expTime) + ", \
         "+prefix+"flxR4Fill=b.APER_FILL_4, \
-        "+prefix+"flxR5=b.APER_FLUX_5, \
-        "+prefix+"flxR5Err=b.APER_FLUX_ERR_5, \
-        "+prefix+"flxR5Std=b.APER_FLUX_STDEV_5, \
+        "+prefix+"flxR5=b.APER_FLUX_5 / " + str(self.expTime) + ", \
+        "+prefix+"flxR5Err=b.APER_FLUX_ERR_5 / " + str(self.expTime) + ", \
+        "+prefix+"flxR5Std=b.APER_FLUX_STDEV_5 / " + str(self.expTime) + ", \
         "+prefix+"flxR5Fill=b.APER_FILL_5, \
-        "+prefix+"flxR6=b.APER_FLUX_6, \
-        "+prefix+"flxR6Err=b.APER_FLUX_ERR_6, \
-        "+prefix+"flxR6Std=b.APER_FLUX_STDEV_6, \
+        "+prefix+"flxR6=b.APER_FLUX_6 / " + str(self.expTime) + ", \
+        "+prefix+"flxR6Err=b.APER_FLUX_ERR_6 / " + str(self.expTime) + ", \
+        "+prefix+"flxR6Std=b.APER_FLUX_STDEV_6 / " + str(self.expTime) + ", \
         "+prefix+"flxR6Fill=b.APER_FILL_6, \
-        "+prefix+"flxR7=b.APER_FLUX_7, \
-        "+prefix+"flxR7Err=b.APER_FLUX_ERR_7, \
-        "+prefix+"flxR7Std=b.APER_FLUX_STDEV_7, \
+        "+prefix+"flxR7=b.APER_FLUX_7 / " + str(self.expTime) + ", \
+        "+prefix+"flxR7Err=b.APER_FLUX_ERR_7 / " + str(self.expTime) + ", \
+        "+prefix+"flxR7Std=b.APER_FLUX_STDEV_7 / " + str(self.expTime) + ", \
         "+prefix+"flxR7Fill=b.APER_FILL_7, \
-        "+prefix+"flxR8=b.APER_FLUX_8, \
-        "+prefix+"flxR8Err=b.APER_FLUX_ERR_8, \
-        "+prefix+"flxR8Std=b.APER_FLUX_STDEV_8, \
+        "+prefix+"flxR8=b.APER_FLUX_8 / " + str(self.expTime) + ", \
+        "+prefix+"flxR8Err=b.APER_FLUX_ERR_8 / " + str(self.expTime) + ", \
+        "+prefix+"flxR8Std=b.APER_FLUX_STDEV_8 / " + str(self.expTime) + ", \
         "+prefix+"flxR8Fill=b.APER_FILL_8, \
-        "+prefix+"flxR9=b.APER_FLUX_9, \
-        "+prefix+"flxR9Err=b.APER_FLUX_ERR_9, \
-        "+prefix+"flxR9Std=b.APER_FLUX_STDEV_9, \
+        "+prefix+"flxR9=b.APER_FLUX_9 / " + str(self.expTime) + ", \
+        "+prefix+"flxR9Err=b.APER_FLUX_ERR_9 / " + str(self.expTime) + ", \
+        "+prefix+"flxR9Std=b.APER_FLUX_STDEV_9 / " + str(self.expTime) + ", \
         "+prefix+"flxR9Fill=b.APER_FILL_9, \
-        "+prefix+"flxR10=b.APER_FLUX_10, \
-        "+prefix+"flxR10Err=b.APER_FLUX_ERR_10, \
-        "+prefix+"flxR10Std=b.APER_FLUX_STDEV_10, \
+        "+prefix+"flxR10=b.APER_FLUX_10 / " + str(self.expTime) + ", \
+        "+prefix+"flxR10Err=b.APER_FLUX_ERR_10 / " + str(self.expTime) + ", \
+        "+prefix+"flxR10Std=b.APER_FLUX_STDEV_10 / " + str(self.expTime) + ", \
         "+prefix+"flxR10Fill=b.APER_FILL_10 \
         WHERE a.ippDetectID=b.IPP_IDET AND b.PSF_FWHM "+psfCondition
@@ -196,6 +197,6 @@
         sql = "UPDATE StackModelFit AS a, SkyChip_xfit AS b SET \
         "+model+"Radius=b.EXT_WIDTH_MAJ,  \
-        "+model+"Mag=b.EXT_INST_MAG,  \
-        "+model+"MagErr=b.EXT_INST_MAG_SIG,  \
+        "+model+"Flux=b.EXT_INST_MAG,  \
+        "+model+"FluxErr=b.EXT_INST_MAG_SIG,  \
         "+model+"Ab=b.EXT_WIDTH_MAJ/b.EXT_WIDTH_MIN, \
         "+model+"Phi=b.EXT_THETA,  \
@@ -229,4 +230,6 @@
         "+modelLong+"Covar77=b.EXT_COVAR_06_06   \
         WHERE a.ippDetectID=b.IPP_IDET AND b.MODEL_TYPE = '"+ippModelType+"'"
+
+        #SA10 convert mag to flux in calculation above
 
         self.scratchDb.execute(sql)
@@ -319,11 +322,10 @@
                ,skyCellID \
                ,projectionCellID \
-               ,obsTime \
                ,xPos \
                ,yPos \
                ,xPosErr \
                ,yPosErr \
-               ,instFlux \
-               ,instFluxErr \
+               ,psfFlux \
+               ,psfFluxErr \
                ,peakFlux \
                ,sky \
@@ -335,5 +337,6 @@
                ,psfLikelihood \
                ,infoFlag \
-               ,psfCf \
+               ,psfQf \
+               ,psfQfPerfect \
                ,momentXX \
                ,momentXY \
@@ -345,10 +348,10 @@
                ,momentR1 \
                ,momentRH \
-               ,apMag \
-               ,apMagErr \
+               ,apFlux \
+               ,apFluxErr \
                ,kronFlux \
                ,kronFluxErr \
                ,kronRad \
-               ,kronRadErr \
+               ,psfChiSq \
                ,nFrames \
                ,assocDate \
@@ -359,5 +362,4 @@
                ," + self.skycell + " \
                ," + self.projectioncell + " \
-               ," + self.header['MJD-OBS'] + " \
                ,X_PSF \
                ,Y_PSF \
@@ -374,6 +376,7 @@
                ,PSF_THETA \
                ,psfLikelihood(EXT_NSIGMA) \
-               ,FLAGS2 << 32 | FLAGS \
+               , FLAGS2 << 32 | FLAGS \
                ,PSF_QF \
+               ,PSF_QF_PERFECT \
                ,MOMENTS_XX \
                ,MOMENTS_XY \
@@ -389,10 +392,14 @@
                ,KRON_FLUX / " + str(self.expTime) + " \
                ,KRON_FLUX_ERR / " + str(self.expTime) + " \
-               , MOMENTS_R1 * 2.5 \
-               , NULL \
+               ,MOMENTS_R1 * 2.5 \
+               ,PSF_CHISQ \
                ,N_FRAMES \
                , '" + self.dateStr + "' \
                ," + self.historyModNum + " \
                FROM SkyChip_psf"
+
+               #SA10 to do
+               # put in correct formula for apFlux, psfFlux, psfLikelihood, sgsep
+               # need correct units of anything in Flux
         self.scratchDb.execute(sql)
         
@@ -417,5 +424,5 @@
         self.updateStackTypeID("StackDetection")
         
-        self.updateDvoIDs("StackDetection")
+        self.updateDvoIDsAndFlags("StackDetection")
         
         sql = "ALTER IGNORE TABLE StackDetection ADD PRIMARY KEY (objID)"
@@ -427,6 +434,6 @@
             #if deep stack and instFlux = null and err not null
             sql = "UPDATE StackDetection AS a, SkyChip_psf AS b \
-                   SET instFlux = 2*b.PSF_INST_FLUX_SIG / " + str(self.expTime) + " \
-                   WHERE instFlux IS NULL \
+                   SET psfFlux = 2*b.PSF_INST_FLUX_SIG / " + str(self.expTime) + " \
+                   WHERE psfFlux IS NULL \
                    AND a.ippDetectID = b.IPP_IDET  \
                    AND b.PSF_INST_FLUX_SIG IS NOT NULL"
@@ -434,6 +441,6 @@
             self.scratchDb.execute(sql)
             
-        
-        self.scratchDb.reportAndDeleteRowsWithNULLS("StackDetection", "instFlux")
+        #leave null instflux in
+        #self.scratchDb.reportAndDeleteRowsWithNULLS("StackDetection", "instFlux")
         
         self.scratchDb.reportAndDeleteRowsWithNULLS("StackDetection", "objID")
@@ -474,6 +481,6 @@
         petRadius=b.PETRO_RADIUS \
         ,petRadiusErr=b.PETRO_RADIUS_ERR \
-        ,petMag=b.PETRO_MAG \
-        ,petMagErr=b.PETRO_MAG_ERR \
+        ,petFlux=b.PETRO_MAG \
+        ,petFluxErr=b.PETRO_MAG_ERR \
         ,petR50=b.PETRO_RADIUS_50 \
         ,petR50Err=b.PETRO_RADIUS_50_ERR \
@@ -482,4 +489,5 @@
         ,petCf=b.PETRO_FILL \
         WHERE a.ippDetectID=b.IPP_IDET"
+        #SA10 Put correct calculation for PetFlux
         self.scratchDb.execute(sql)
 
@@ -586,4 +594,40 @@
         self.scratchDb.updateAllRows("SkinnyObject", "surveyID", str(self.surveyID))
         self.scratchDb.updateAllRows("SkinnyObject", "dataRelease", str(self.config.dataRelease))
+
+    '''    
+    Populates the StackDetectionCalib table
+    '''
+    def populateStackDetectionCalib(self):
+        self.logger.infoPair("Processing table", "StackDetectionCalib")
+        tableName = "StackDetectionCalib"
+        # insert all detections into table
+        sql = "INSERT INTO " + tableName + " ( \
+               objID \
+               ,stackDetectID \
+               ,ippObjID \
+               ,ippDetectID \
+               ,filterID \
+               ,surveyID \
+               ) \
+               SELECT \
+               objID \
+               ,stackDetectID \
+               ,ippObjID \
+               ,ippDetectID \
+               ,filterID \
+               ,surveyID \
+               FROM StackDetection"
+        self.logger.info(sql)
+        self.scratchDb.execute(sql)
+        #SA10 TODO
+        # insert ra/dec/calstuff into Table
+        # heather discovered dec is called 'dec_'
+        sql = "UPDATE " + tableName + " SET ra = -999, dec_ =-999, raErr = -999, decErr = -999, zp = -999, zpErr = -999, expTime = -999, airMass = -999"
+        self.logger.info(sql)
+        self.scratchDb.execute(sql)
+        self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.config.dataRelease))
+        
+    
+
 
     '''
@@ -649,7 +693,24 @@
                WHERE a.ippDetectID = b.ippDetectID \
                AND b.imageID = " + str(imageID)
-        
-        self.scratchDb.execute(sql)
-        
+        self.scratchDb.execute(sql)
+    
+    '''
+    Updates Flags
+    '''
+    def updateDvoIDsAndFlags(self, table):
+        self.logger.infoPair("getting","imageID")
+        imageID = self.scratchDb.getImageIDFromExternID(self.header['IMAGEID'])
+        self.logger.infoPair("obtained","imageID")
+        self.logger.debug("Updating table '" + table + "' with DVO IDs...")
+        sql = "UPDATE " + table + " AS a, " + self.scratchDb.dvoDetectionTable + " AS b SET \
+               a.ippObjID = b.ippObjID, \
+               a.stackDetectID = 10 * b.detectID + " + str(self.filterID) + ", \
+               a.objID = b.objID, \
+               a.infoFlag = b.flags << 46 | a.infoFlag \
+               WHERE a.ippDetectID = b.ippDetectID \
+               AND b.imageID = " + str(imageID)
+        self.scratchDb.execute(sql)
+    
+    
     '''
     Does the processing, i.e. pulling stuff from IPP tables into PSPS tables
@@ -660,15 +721,22 @@
             if not self.getIDsFromDVO():
                 return False
-
+        #self.logger.infoPair("populating","StackMeta")    
         self.populateStackMeta()
+        #self.logger.infoPair("populating","StackDetection") 
         self.populateStackDetection()
-
+        
         if self.stackType != "NIGHTLY_STACK": 
+            #self.logger.infoPair("populating","StackModelFit") 
             self.populateStackModelFit()
+            #self.logger.infoPair("populating","StackApFlx") 
             self.populateStackApFlx()
-   
+        #self.logger.infoPair("populating","StackToImage") 
         self.populateStackToImage()
+        #self.logger.infoPair("populating","SkinnyObject") 
         self.populateSkinnyObject()
+        #self.logger.infoPair("populating","ObjectCalColor") 
         self.populateObjectCalColor()
+        #self.logger.infoPair("populating","StackDetectionCalib") 
+        self.populateStackDetectionCalib()
 
         self.setMinMaxObjID(["StackDetection"])
