Index: trunk/ippToPsps/jython/forcedgalaxybatch.py
===================================================================
--- trunk/ippToPsps/jython/forcedgalaxybatch.py	(revision 38945)
+++ trunk/ippToPsps/jython/forcedgalaxybatch.py	(revision 38945)
@@ -0,0 +1,217 @@
+#!/usr/bin/env jython
+
+import os.path
+import sys
+import glob
+import time
+import stilts
+import re 
+
+from java.lang import *
+from java.sql import *
+
+from java.lang import Math
+from org.apache.commons.math.special import Erf
+
+from xml.etree.ElementTree import ElementTree, Element, tostring
+
+from batch import Batch
+from gpc1db import Gpc1Db
+from ipptopspsdb import IppToPspsDb
+from scratchdb import ScratchDb
+from sqlUtility import sqlUtility
+
+import logging.config
+
+'''
+ForcedGalaxyBatch class
+
+This class, a sub-class of Batch, processes full force summary products.
+
+'''
+class ForcedGalaxyBatch(Batch):
+
+    '''
+    Constructor
+    '''
+    def __init__(self,
+                 logger,
+                 config,
+                 skychunk,
+                 gpc1Db,
+                 ippToPspsDb,
+                 scratchDb,
+                 dvoID,
+                 batchID):
+
+        super(ForcedGalaxyBatch, self).__init__(
+            logger,
+            config,
+            skychunk,
+            gpc1Db,
+            ippToPspsDb,
+            scratchDb,
+            dvoID,
+            batchID,
+            "FG",
+            None)
+
+        try:
+            self.dvoForcedGalaxy = DvoForcedGalaxy(self.logger, self.config, self.skychunk, self.ippToPspsDb, self.scratchDb, self.gpc1Db)
+        except:
+            self.logger.errorPair("Unable to create instance of", "DvoForcedGalaxy")
+            raise
+
+        # create an output filename
+        self.outputFitsFile = "%08d.FITS" % self.id
+        self.outputFitsPath = "%s/%s" & (self.localOutPath, self.outputFitsFile)
+
+        # dump stuff to log
+        self.logger.infoPair("DVO INDEX", "%d" % self.id)
+
+
+    '''
+    Overriden from batch base class to import directly from DVO for forced galaxy shape parameters
+    '''
+    def importIppTables(self, columns="*", filter=""):
+        self.region = self.scratchDb.getRegionNameFromThisDvoIndex(self.id)
+#        self.ippToPspsDb.insertForcedSummaryMeta(self.batchID, self.region)
+        self.dvoForcedGalaxy.nativeInjectRegion(self.region)
+
+        cptTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cpt")
+
+        if not self.scratchDb.tableExists(cptTableName):
+            self.logger.infoPair("can't find in scratch db:", "cpt table")
+            return False
+
+        cpqTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cpq")
+        
+        if not self.scratchDb.tableExists(cpqTableName):
+            self.logger.infoPair("can't find in scratch db:", "cpq table")
+            return False
+            
+        self.logger.infoPair("checking if we have anything in"," cpq/cpt")
+        rowcountcpt = self.scratchDb.getRowCount(cptTableName);
+        rowcountcpq = self.scratchDb.getRowCount(cpqTableName);
+        
+        if (rowcountcpt == 0):
+            self.logger.infoPair("0 rows for ","cpt table")
+            return False
+        if (rowcountcpq == 0):
+            self.logger.infoPair("0 rows for ","cpq table")
+            return False
+        return True
+
+    '''
+    Applies indexes to the PSPS tables
+    '''
+    def alterPspsTables(self):
+        return True
+
+
+    '''
+    Applies indexes to the IPP Tables
+    '''
+    def indexIppTables(self):
+        return True
+
+    '''
+    Updates table and generates pspsuniqueids
+    '''
+    def updatePspsUniqueIDs(self):
+        sql = "UPDATE ForcedGalaxyShape set uniquePspspFGid = (("+str(self.batchID)+"*1000000000 ) + row)"
+        try: self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair('failed sql', sql)
+            raise
+
+    '''
+    Actually inserts the science data
+    '''
+    def updateForcedGalaxyShapeFromCpq(self, cpqTable):
+
+        # Get filter information as in forcedobjectbatch.py
+        interestedFilters = ['g','r','i','z','y']
+        filters = self.scratchDb.getOrderedListOfFiltersFromPhotcodesTable(interestedFilters)
+        filterCount = self.scratchDb.getCountOfFiltersFromPhotcodesTable()
+
+        self.logger.infoPair("Available filters in Photcodes", filters)
+
+        self.logger.infoPair("Adding measurements from", "cpq table")
+
+        for filter in filters:
+            filterID = self.scratchDb.getFilterId(filter[1])
+
+
+            # This is going to need to join elsewhere to get all the fields?
+            # XippDetectID, XforcedSummaryID
+            # Ra, Dec, RaErr, DecErr
+            
+            # Should GalChiSq fold in Npix somehow?  
+            sql = "UPDATE ForcedGalaxyShape JOIN " \
+                + cpqTable + " AS cpq ON (cpq.row = (ForcedGalaxyShape.row* " + str(filterCount) + ")-(" + str(filterCount) + " - " + str(filter[0]) + ")) " \
+                + " SET " + \
+                + " ForcedGalaxyShape." + filter[1] + "ippDetectID                = DET_ID," \
+                + " ForcedGalaxyShape." + filter[1] + "forcedSummaryID            = IMAGE_ID," \
+                + " ForcedGalaxyShape." + filter[1] + "GalModelType               = MODEL_TYPE," \
+                + " ForcedGalaxyShape." + filter[1] + "GalMajor                   = MAJOR_AXIS," \
+                + " ForcedGalaxyShape." + filter[1] + "GalMajorErr                = MAJOR_AXIS_ERR," \
+                + " ForcedGalaxyShape." + filter[1] + "GalMinor                   = MINOR_AXIS," \
+                + " ForcedGalaxyShape." + filter[1] + "GalMinorErr                = MINOR_AXIS_ERR," \
+                + " ForcedGalaxyShape." + filter[1] + "GalMag                     = MAG," \
+                + " ForcedGalaxyShape." + filter[1] + "GalMagErr                  = MAG_ERR," \
+                + " ForcedGalaxyShape." + filter[1] + "GalRa                      = RA," \
+                + " ForcedGalaxyShape." + filter[1] + "GalDec                     = DEC," \
+                + " ForcedGalaxyShape." + filter[1] + "GalRaErr                   = RA_ERR," \
+                + " ForcedGalaxyShape." + filter[1] + "GalDecErr                  = DEC_ERR," \
+                + " ForcedGalaxyShape." + filter[1] + "GalChisq                   = CHISQ "
+
+            try: self.scratchDb.execute(sql)
+            except:
+                self.logger.errorPair("failed update ForcedGalaxyShape", sql)
+                raise
+
+        
+
+
+    '''
+    Populate ForcedGalaxyShape table
+    '''
+    def populateForcedGalaxyShapeTable(self):
+
+        cptTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cpt")
+        cpqTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cpq")
+        
+        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.
+#        sqlLine.group("batchID",    "'" + str(self.batchID) + "'")
+        # surveyID is set where?
+        sqlLine.group("randomForcedGalID",      "RAND("+str(self.batchID)+")")
+        
+        sql = sqlLine.makeRaw(") SELECT ", " FROM " + cptTableName)
+
+        try:
+            self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair("Couldn't populate Forced Galaxy table from cpt", sql)
+            raise
+
+        self.logger.infoPair("Adding 'row' columns to", "Forced Galaxy, cpt tables")
+        self.scratchDb.addRowCountColumn("ForcedGalaxyShape", "row")
+        self.scratchDb.addRowCountColumn(cpsTableName, "row")
+
+        self.logger.infoPair("generating unique ids for ","ForcedGalaxyShape")
+        self.updatePspsUniqueIDs()
+
+        self.logger.infoPair("update ForcedGalaxyShape from ","cpq table")
+        self.updateForcedGalaxyShapeFromCpq(cpqTableName)
+
+        
+        
Index: trunk/ippToPsps/jython/forcedwarpbatch.py
===================================================================
--- trunk/ippToPsps/jython/forcedwarpbatch.py	(revision 38944)
+++ trunk/ippToPsps/jython/forcedwarpbatch.py	(revision 38945)
@@ -638,4 +638,59 @@
 
 
+#     '''
+#     Populate the ForcedSummaryMeta table, which should be fairly easy as it contains very little.
+#     '''
+#     def populateForcedSummaryMeta(self,num):
+#         forcedSummaryID = self.number[num]
+#         self.logger.infoPair("Processing table", "ForcedSummaryMeta_"+forcedSummaryID)
+
+#         sql = "CREATE TABLE ForcedSummaryMeta_"+forcedSummaryID+" LIKE ForcedSummaryMeta"
+#         try: self.scratchDb.execute(sql)
+#         except: pass
+
+#         header = self.header[num]
+#         self.logger.infoPair("found header", "ok")
+
+#         sqlLine = sqlUtility("INSERT INTO ForcedSummaryMeta_"+forcedSummaryID+" (")
+        
+#         sqlLine.group("forcedSummaryID", forcedSummaryID)
+#         sqlLine.group("batchID",         str(self.batchID))
+#         sqlLine.group("surveyID",        str(self.surveyID))
+#         sqlLine.group("filterID",        str(self.filterID))
+#         sqlLine.group("stackMetaID", self.stackID[num])
+#         sqlLine.group("processingVersion", str(self.skychunk.processingVersion))
+
+
+#         sql = sqlLine.make(") VALUES ( ", ")")
+
+#         try: self.scratchDb.execute(sql)
+#         except:
+#             self.logger.errorPair('failed sql: ', sql)
+#             raise
+        
+#     # End populateForcedSummaryMeta?
+
+#     '''
+#     Populate the ForcedSummaryToImage table, which has even less.
+#     '''
+#     def populateForcedSummaryToImage(self,num):
+#         forcedSummaryID = self.number[num]
+#         self.logger.infoPair("Processing table", "ForcedSummaryToImage_"+str(forcedSummaryID))
+
+#         sql = "CREATE TABLE ForcedSummaryToImage_"+str(forcedSummaryID)+" LIKE ForcedSummaryToImage"
+#         try: self.scratchDb.execute(sql)
+#         except: pass
+
+#         if forcedSummaryID > 0:
+#             imageIDs = self.gpc1Db.getImageIDsForThisForcedSummaryID(forcedSummaryID)
+#             for imageID in imageIDs:
+#                 sql = "INSERT INTO ForcedWarpSummaryToImage_"+str(forcedSummaryID)+" (forcedSummaryId, imageID) VALUES (" + str(forcedSummaryID) + ", " + str(imageID) + ")"
+#                 try: self.scratchDb.execute(sql)
+#                 except:
+#                     self.logger.infoPair("can't execute sql", sql)
+#                     raise
+
+#     # End populateForcedSummaryToImage?
+
 
     '''
