Index: /trunk/ippToPsps/jython/stackbatch.py
===================================================================
--- /trunk/ippToPsps/jython/stackbatch.py	(revision 36723)
+++ /trunk/ippToPsps/jython/stackbatch.py	(revision 36724)
@@ -643,125 +643,130 @@
               
         
-
-        # insert all the detections
-        sql = "INSERT INTO StackObject(\
-               ippDetectID \
-               ,skyCellID \
-               ,projectionCellID \
-               ,xPos \
-               ,yPos \
-               ,xPosErr \
-               ,yPosErr \
-               ,psfFlux \
-               ,psfFluxErr \
-               ,peakFlux \
-               ,sky \
-               ,skyErr \
-               ,sgSep \
-               ,psfWidMajor \
-               ,psfWidMinor \
-               ,psfTheta \
-               ,psfLikelihood \
-               ,infoFlag \
-               ,psfQf \
-               ,psfQfPerfect \
-               ,psfChiSq \
-               ,momentXX \
-               ,momentXY \
-               ,momentYY \
-               ,momentM3C \
-               ,momentM3S \
-               ,momentM4C \
-               ,momentM4S \
-               ,momentR1 \
-               ,momentRH \
-               ,apFlux \
-               ,apFluxErr \
-               ,kronFlux \
-               ,kronFluxErr \
-               ,kronRad \
-               ,nFrames \
-               ,assocDate \
-               ,historyModNum \
-               ) \
-               SELECT \
-               IPP_IDET \
-               ," + self.skycell + " \
-               ," + self.projectioncell + " \
-               ,X_PSF \
-               ,Y_PSF \
-               ,X_PSF_SIG \
-               ,Y_PSF_SIG \
-               ,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) + " \
-               ,SKY_SIGMA / " + str(self.expTime) + " \
-               ,EXT_NSIGMA \
-               ,PSF_MAJOR \
-               ,PSF_MINOR \
-               ,PSF_THETA \
-               ,psfLikelihood(EXT_NSIGMA) \
-               , FLAGS2 << 32 | FLAGS \
-               ,PSF_QF \
-               ,PSF_QF_PERFECT \
-               ,PSF_CHISQ / PSF_NDOF \
-               ,MOMENTS_XX \
-               ,MOMENTS_XY \
-               ,MOMENTS_YY \
-               ,MOMENTS_M3C \
-               ,MOMENTS_M3S \
-               ,MOMENTS_M4C \
-               ,MOMENTS_M4S \
-               ,MOMENTS_R1 \
-               ,MOMENTS_RH \
-               ,POW(10.0, -0.4*AP_MAG) / " + str(self.expTime) + " \
-               ,NULL \
-               ,KRON_FLUX / " + str(self.expTime) + " \
-               ,KRON_FLUX_ERR / " + str(self.expTime) + " \
-               ,MOMENTS_R1 * 2.5 \
-               ,N_FRAMES \
-               , '" + self.dateStr + "' \
-               ," + self.historyModNum + " \
-               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) + " 
-
-# I need a way to choose the sql above based on the cmf version: V3 (LAP.PV1) does not have AP_FLUX
-#               ,AP_FLUX / " + str(self.expTime) + " 
-
-        # print "sql: ", sql
-        # response = raw_input("ready to insert stack det ")
-
-        self.scratchDb.execute(sql)
-
-        #it is possible to drop some detections from dvo (that are present in the cmf). when that happens we get a 0 for objid
+        
+
+        self.logger.infoPair("inserting filter dependent cmf items into", "StackObject")
+
+        for f in filters:
+          skip = 0
+          if (f == "g"):
+              if self.gstackID > 0:  
+                  stackid = self.gstackID
+                  exptime = self.gheader['EXPTIME']
+              else:
+                  skip = 1
+          if (f == "r"):
+              if self.rstackID > 0:
+                  stackid = self.rstackID
+                  exptime = self.rheader['EXPTIME']
+              else:
+                  skip = 1
+          if (f == "i"):
+              if self.istackID > 0:
+                  stackid = self.istackID
+                  exptime = self.iheader['EXPTIME']
+              else:
+                  skip = 1
+          if (f == "z"):
+              if self.zstackID > 0:
+                  stackid = self.zstackID
+                  exptime = self.zheader['EXPTIME']
+              else:
+                  skip = 1
+          if (f == "y"):
+              if self.ystackID > 0
+                  stackid = self.ystackID
+                  exptime = self.yheader['EXPTIME']:
+              else:
+                  skip = 1
+
+          if skip == 1:
+              self.logger.infoPair("nothing for filter" , f) 
+          else:
+             self.logger.infoPair("filter",f)
+
+
+
+             # insert all the detections
+             sql = "INSERT INTO StackObject(\
+                   "+f+"ippDetectID \
+                   ,"+f+"xPos \
+                   ,"+f+"yPos \
+                   ,"+f+"xPosErr \
+                   ,"+f+"yPosErr \
+                   ,"+f+"PSFFlux \
+                   ,"+f+"PSFFluxErr \
+                   ,"+f+"sky \
+                   ,"+f+"skyErr \
+                   ,"+f+"psfMajorFWHM \
+                   ,"+f+"psfMinorFWHM \
+                   ,"+f+"psfTheta \
+                   ,"+f+"psfLikelihood \
+                   ,"+f+"infoFlag \
+                   ,"+f+"psfQf \
+                   ,"+f+"psfQfPerfect \
+                   ,"+f+"psfChiSq \
+                   ,"+f+"momentXX \
+                   ,"+f+"momentXY \
+                   ,"+f+"momentYY \
+                   ,"+f+"momentR1 \
+                   ,"+f+"momentRH \
+                   ,"+f+"ApFlux \
+                   ,"+f+"ApFluxErr \
+                   ,"+f+"KronFlux \
+                   ,"+f+"KronFluxErr \
+                   ,"+f+"KronRad \
+                   ,"+f+"nFrames \
+                   ) \
+                   SELECT \
+                   IPP_IDET \
+                   ,X_PSF \
+                   ,Y_PSF \
+                   ,X_PSF_SIG \
+                   ,Y_PSF_SIG \
+                   ,PSF_INST_FLUX / " + str(exptime) + " \
+                   ,PSF_INST_FLUX_SIG / " + str(exptime) + " \
+                   ,SKY / " + str(exptime) + " \
+                   ,SKY_SIGMA / " + str(exptime) + " \
+                   ,PSF_MAJOR \
+                   ,PSF_MINOR \
+                   ,PSF_THETA \
+                   ,psfLikelihood(EXT_NSIGMA) \
+                   , FLAGS2 << 32 | FLAGS \
+                   ,PSF_QF \
+                   ,PSF_QF_PERFECT \
+                   ,PSF_CHISQ / PSF_NDOF \
+                   ,MOMENTS_XX \
+                   ,MOMENTS_XY \
+                   ,MOMENTS_YY \
+                   ,MOMENTS_R1 \
+                   ,MOMENTS_RH \
+                   ,POW(10.0, -0.4*AP_MAG) / " + str(exptime) + " \
+                   ,NULL \
+                   ,KRON_FLUX / " + str(exptime) + " \
+                   ,KRON_FLUX_ERR / " + str(exptime) + " \
+                   ,MOMENTS_R1 * 2.5 \
+                   ,N_FRAMES \
+                   FROM " + f + "SkyChip_psf"
+             
+
+             self.scratchDb.execute(sql)
+
+             #it is possible to drop some detections from dvo (that are present in the cmf). when that happens we get a 0 for objid
         #we drop those...
+
         sql="DELETE FROM StackObject where objID = 0"
         self.scratchDb.execute(sql)
         self.logger.infoPair("Deleting", "entries with StackObject.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("StackObject", "surveyID", str(self.surveyID))
         
-        self.scratchDb.updateFilterID("StackObject", self.filter)
+#        self.scratchDb.updateFilterID("StackObject", f+".00000")
         
         self.scratchDb.updateAllRows("StackObject", "dataRelease", str(self.skychunk.dataRelease))
-        #set primary F to 0
-        self.scratchDb.updateAllRows("StackObject", "primaryF", "0")
-        
-        self.scratchDb.updateAllRows("StackObject", "activeFlag", "0")
-        
         self.updateStackMetaID("StackObject")
         
         self.updateStackTypeID("StackObject")
         
-        self.updateDvoIDsAndFlags("StackObject")
+        #self.updateDvoIDsAndFlags("StackObject")
         #this now updates primary F as well
         # response = raw_input("updated dvo ")
@@ -796,16 +801,49 @@
             self.scratchDb.execute(sql)
             # response = raw_input("add psf flux ")
-            
+         
+        #filter dependent
         # 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 StackObject SET psfFlux = 1e20 WHERE psfFlux <= 0.0"
-        self.scratchDb.execute(sql)
-        
-        sql = "UPDATE StackObject SET apFlux = 1e20 WHERE apFlux <= 0.0"
-        self.scratchDb.execute(sql)
-
-        sql = "UPDATE StackObject SET kronFlux = 1e20 WHERE kronFlux <= 0.0"
+        sql = "UPDATE StackObject SET gPSFFlux = 1e20 WHERE gPSFFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        
+        sql = "UPDATE StackObject SET gApFlux = 1e20 WHERE gApFlux <= 0.0"
+        self.scratchDb.execute(sql)
+
+        sql = "UPDATE StackObject SET gKronFlux = 1e20 WHERE gKronFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        sql = "UPDATE StackObject SET rPSFFlux = 1e20 WHERE rPSFFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        
+        sql = "UPDATE StackObject SET rApFlux = 1e20 WHERE rApFlux <= 0.0"
+        self.scratchDb.execute(sql)
+
+        sql = "UPDATE StackObject SET rKronFlux = 1e20 WHERE rKronFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        sql = "UPDATE StackObject SET iPSFFlux = 1e20 WHERE iPSFFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        
+        sql = "UPDATE StackObject SET iApFlux = 1e20 WHERE iApFlux <= 0.0"
+        self.scratchDb.execute(sql)
+
+        sql = "UPDATE StackObject SET iKronFlux = 1e20 WHERE iKronFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        sql = "UPDATE StackObject SET zPSFFlux = 1e20 WHERE zPSFFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        
+        sql = "UPDATE StackObject SET zApFlux = 1e20 WHERE zApFlux <= 0.0"
+        self.scratchDb.execute(sql)
+
+        sql = "UPDATE StackObject SET zKronFlux = 1e20 WHERE zKronFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        sql = "UPDATE StackObject SET yPSFFlux = 1e20 WHERE yPSFFlux <= 0.0"
+        self.scratchDb.execute(sql)
+        
+        sql = "UPDATE StackObject SET yApFlux = 1e20 WHERE yApFlux <= 0.0"
+        self.scratchDb.execute(sql)
+
+        sql = "UPDATE StackObject SET yKronFlux = 1e20 WHERE yKronFlux <= 0.0"
         self.scratchDb.execute(sql)
 
