Index: trunk/ippToPsps/jython/detectionbatch.py
===================================================================
--- trunk/ippToPsps/jython/detectionbatch.py	(revision 35451)
+++ trunk/ippToPsps/jython/detectionbatch.py	(revision 35452)
@@ -439,6 +439,6 @@
                ,X_PSF_SIG \
                ,Y_PSF_SIG \
-               ,POW(10.0, (-0.4*PSF_INST_MAG)) / " + self.header['EXPTIME'] + " \
-               ,ABS((PSF_INST_MAG_SIG*(POW(10.0, (-0.4*PSF_INST_MAG)) / " + self.header['EXPTIME'] + ")) / 1.085736) \
+               ,PSF_INST_FLUX / " + self.header['EXPTIME'] + " \
+               ,PSF_INST_FLUX_SIG / " + self.header['EXPTIME'] + " \
                ,POW(10.0, (-0.4*PEAK_FLUX_AS_MAG)) / " + self.header['EXPTIME'] + " \
                ,PSF_MAJOR \
@@ -450,5 +450,5 @@
                ,MOMENTS_XY \
                ,MOMENTS_YY \
-               ,POW(10.0, -0.4 *AP_MAG) / " + self.header['EXPTIME'] + " \
+               ,AP_FLUX / " + self.header['EXPTIME'] + " \
                ,KRON_FLUX / " + self.header['EXPTIME'] + " \
                ,KRON_FLUX_ERR / " + self.header['EXPTIME'] + " \
@@ -466,11 +466,30 @@
         # self.logger.info(sql)
 
-        self.scratchDb.execute(sql)
-
+# these were used above (left over from before PSF_INST_FLUX, PSF_INST_FLUX_SIG were available?
+#               ,POW(10.0, (-0.4*PSF_INST_MAG)) / " + self.header['EXPTIME'] + " 
+#               ,ABS((PSF_INST_MAG_SIG*(POW(10.0, (-0.4*PSF_INST_MAG)) / " + self.header['EXPTIME'] + ")) / 1.085736) 
+#               ,POW(10.0, -0.4 *AP_MAG) / " + self.header['EXPTIME'] + " 
+
+        self.scratchDb.execute(sql)
+
+        # XXX EAM : I removed this old fix for invalid fluxes
         # add a instFlux = 0.0 -> 0.000001
-        # XXX EAM : why is this done?
-        sql="UPDATE " + pspsTableName + " SET psfFlux = 0.0000001 WHERE psfFlux =  0"
-        self.scratchDb.execute(sql)
+        # XXX EAM : why is this done? 
+        # sql="UPDATE " + pspsTableName + " SET psfFlux = 0.0000001 WHERE psfFlux =  0"
+        # self.scratchDb.execute(sql)
       
+        # NOTE : Flux limits : in the current PSPS schema, negative fluxes 
+        # cause problems for sql queries which work in mags as SQL cannot do
+        # something like (f < 0.0) ? -999 : -2.5*log10(f)
+        # as a result, the negative fluxes here result in floating point errors
+        sql = "UPDATE " + pspsTableName " SET psfFlux = 1e20 WHERE psfFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        
+        sql = "UPDATE " + pspsTableName " SET apFlux = 1e20 WHERE apFlux <= 0.0"
+        self.scratchDb.execute(sql)
+
+        sql = "UPDATE " + pspsTableName " SET kronFlux = 1e20 WHERE kronFlux <= 0.0"
+        self.scratchDb.execute(sql)
+
         # we don't delete these anymore
         results['NULLINSTFLUX'] = 0;
Index: trunk/ippToPsps/jython/stackbatch.py
===================================================================
--- trunk/ippToPsps/jython/stackbatch.py	(revision 35451)
+++ trunk/ippToPsps/jython/stackbatch.py	(revision 35452)
@@ -482,6 +482,6 @@
                ,X_PSF_SIG \
                ,Y_PSF_SIG \
-               ,POW(10.0, (-0.4*PSF_INST_MAG)) / " + str(self.expTime) + " \
-               ,ABS((PSF_INST_MAG_SIG*(POW(10.0, (-0.4*PSF_INST_MAG)) / " + str(self.expTime) + ")) / 1.085736) \
+               ,PSF_INST_FLUX / " + str(self.expTime) + " \
+               ,PSF_INST_FLUX_SIG / " + str(self.expTime) + " \
                ,POW(10.0, (-0.4*PEAK_FLUX_AS_MAG)) / " + str(self.expTime) + " \
                ,SKY / " + str(self.expTime) + " \
@@ -504,5 +504,5 @@
                ,MOMENTS_R1 \
                ,MOMENTS_RH \
-               ,POW(10.0, -0.4*AP_MAG) / " + str(self.expTime) + " \
+               ,AP_FLUX / " + str(self.expTime) + " \
                ,NULL \
                ,KRON_FLUX / " + str(self.expTime) + " \
@@ -515,4 +515,9 @@
                FROM SkyChip_psf"
 
+# these were used above (left over from before PSF_INST_FLUX, PSF_INST_FLUX_SIG were available?
+#               ,POW(10.0, (-0.4*PSF_INST_MAG)) / " + str(self.expTime) + " 
+#               ,ABS((PSF_INST_MAG_SIG*(POW(10.0, (-0.4*PSF_INST_MAG)) / " + str(self.expTime) + ")) / 1.085736) 
+#               ,POW(10.0, -0.4*AP_MAG) / " + str(self.expTime) + " 
+
         # print "sql: ", sql
         # response = raw_input("ready to insert stack det ")
@@ -526,4 +531,8 @@
         self.logger.infoPair("Deleting", "entries with StackDetection.objID = 0")
         
+        # XXX EAM : this seems quite inefficient : these commands use updates to set 
+        # fields which are constant across all rows.  Why not just include these in 
+        # the original insert above?
+
         self.scratchDb.updateAllRows("StackDetection", "surveyID", str(self.surveyID))
         
@@ -560,5 +569,6 @@
         # response = raw_input("add primary key? ")
   
-        if self.stackType == "DEEP_STACK": 
+        ### XXX this should not be a DEEP_STACK only case...
+        if false and self.stackType == "DEEP_STACK": 
 
             #if deep stack and instFlux = null and err not null
@@ -572,4 +582,17 @@
             # response = raw_input("add psf flux ")
             
+        # NOTE : Flux limits : in the current PSPS schema, negative fluxes 
+        # cause problems for sql queries which work in mags as SQL cannot do
+        # something like (f < 0.0) ? -999 : -2.5*log10(f)
+        # as a result, the negative fluxes here result in floating point errors
+        sql = "UPDATE StackDetection SET psfFlux = 1e20 WHERE psfFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        
+        sql = "UPDATE StackDetection SET apFlux = 1e20 WHERE apFlux <= 0.0"
+        self.scratchDb.execute(sql)
+
+        sql = "UPDATE StackDetection SET kronFlux = 1e20 WHERE kronFlux <= 0.0"
+        self.scratchDb.execute(sql)
+
         #leave null instflux in
         #self.scratchDb.reportAndDeleteRowsWithNULLS("StackDetection", "instFlux")
