Index: trunk/ippToPsps/jython/forcedgalaxybatch.py
===================================================================
--- trunk/ippToPsps/jython/forcedgalaxybatch.py	(revision 39075)
+++ trunk/ippToPsps/jython/forcedgalaxybatch.py	(revision 39079)
@@ -138,5 +138,5 @@
         for filter in filters:
             filterID = self.scratchDb.getFilterID(filter[1])
-
+            
             photcodeName = "NONE"
             if self.config.camera == "gpc1":
@@ -144,11 +144,11 @@
             if self.config.camera == "simtest":
                 photcodeName = "SIMTEST." + filter[1] + ".ForcedWarp"
-
+            
             photcode = self.scratchDb.getPhotcodeByName(photcodeName)
-
+            
             # This is going to need to join elsewhere to get all the fields?
             # XforcedSummaryID, Ra, Dec, RaErr, DecErr
             # Should GalChiSq fold in Npix somehow? 
-
+            
             sqlLine = sqlUtility("UPDATE ForcedGalaxyShape AS a, " + cpqTable + " AS b SET")
             sqlLine.group("a." + filter[1] + "ippDetectID",     "b.DET_ID")
@@ -162,4 +162,8 @@
             sqlLine.group("a." + filter[1] + "GalChisq",        "b.CHISQ")
 
+            # we insert data for each of the possible model types, counting the number of filter/model values
+            # selected for each type.  later we will delete rows for which nFilter is 0
+            sqlLine.group("a.nFilter", "a.nFilter + 1")
+            
             # These fields don't exist.
             # sqlLine.group("a." + filter[1] + "GalRa",           "b.RA")
@@ -167,9 +171,9 @@
             # sqlLine.group("a." + filter[1] + "GalRaErr",        "b.RA_ERR")
             # sqlLine.group("a." + filter[1] + "GalDecErr",       "b.DEC_ERR")
-
+            
             sql = sqlLine.makeEquals("WHERE a.ippObjID = (b.OBJ_ID + (b.CAT_ID << 32)) AND a.galModelType = b.MODEL_TYPE AND b.Photcode = " + str(photcode))
-
+            
             self.logger.debugPair("cpq sql: ", sql)
-
+            
             self.scratchDb.execute(sql)
 
@@ -183,32 +187,46 @@
         
         self.logger.info("Populating ForcedGalaxyShape")
-        self.logger.info("Inserting from cpq file")
-
-        sqlLine = sqlUtility("INSERT INTO ForcedGalaxyShape (")
-
-        sqlLine.group("objID",      "EXT_ID")
-        # uniquePspsFGid is set elsewhere
-        sqlLine.group("ippObjID",   "OBJ_ID + (CAT_ID << 32)")
-        # batchID isn't in the schema, should it be?
-        # sqlLine.group("batchID",    "'" + str(self.batchID) + "'")
-        # surveyID is set where?
-        sqlLine.group("randomForcedGalID",      "RAND("+str(self.batchID)+")")
-        
-        sql = sqlLine.makeRaw(") SELECT ", " FROM " + cptTableName)
-
-        print "--- sql: " + sql
-
+
+        # the cpt file has the basic object data (IDs).  insert a row for each of the
+        # model types of interest, listed in the array below
+        self.logger.info("Inserting from cpt file")
+        for modelName in ["PS_MODEL_EXP", "PS_MODEL_DEV", "PS_MODEL_SERSIC"]:
+            modelID = self.scratchDb.getFitModelID(modelName)
+            ## assert on modelID > 0
+
+            sqlLine = sqlUtility("INSERT INTO ForcedGalaxyShape (")
+
+            # uniquePspsFGid is set elsewhere
+            # batchID isn't in the schema, should it be?
+
+            sqlLine.group("objID",             "EXT_ID")
+            sqlLine.group("ippObjID",          "OBJ_ID + (CAT_ID << 32)")
+            sqlLine.group("surveyID",          str(self.surveyID))
+            sqlLine.group("randomForcedGalID", "RAND("+str(self.batchID)+")")
+            sqlLine.group("nFilter",           "0")
+            sqlLine.group("galModelType",      str(modelID))
+
+            sql = sqlLine.makeRaw(") SELECT ", " FROM " + cptTableName)
+
+            print "--- sql: " + sql
+
+            self.scratchDb.execute(sql)
+
+        # add in the actual galaxy photometry & shape measurements
+        self.logger.infoPair("update ForcedGalaxyShape from ","cpq table")
+        self.updateForcedGalaxyShapeFromCpq(cpqTableName)
+
+        # drop the rows with no galaxy photometry
+        self.logger.infoPair("dropping rows with no data", "ForcedGalaxyShape")
+        sql = "DELETE FROM ForcedGalaxyShape WHERE nFilter = 0"
         self.scratchDb.execute(sql)
 
-        self.logger.infoPair("Adding 'row' columns to", "Forced Galaxy, cpt tables")
+        # now we need to generate a unique psps ID:
+        self.logger.infoPair("Adding 'row' columns to", "ForcedGalaxyShape table")
         self.scratchDb.addRowCountColumn("ForcedGalaxyShape", "row")
-        self.scratchDb.addRowCountColumn(cpqTableName, "row")
 
         self.logger.infoPair("generating unique ids for ","ForcedGalaxyShape")
         self.updatePspsUniqueIDs()
 
-        self.logger.infoPair("update ForcedGalaxyShape from ","cpq table")
-        self.updateForcedGalaxyShapeFromCpq(cpqTableName)
-        
         self.logger.infoPair("Dropping row column from ", "ForcedGalaxyShape table")
         self.scratchDb.dropColumn("ForcedGalaxyShape", "row")
@@ -216,6 +234,6 @@
         self.logger.infoPair("setting minmaxobjid for ", "ForcedGalaxyShape table")
         self.setMinMaxObjID(["ForcedGalaxyShape"])
+
         self.logger.infoPair("checking if we have detections", "ForcedGalaxyShape table")
-
         rowcount = self.scratchDb.getRowCount("ForcedGalaxyShape")
 
