Index: /trunk/ippToPsps/jython/objectbatch.py
===================================================================
--- /trunk/ippToPsps/jython/objectbatch.py	(revision 33687)
+++ /trunk/ippToPsps/jython/objectbatch.py	(revision 33687)
@@ -0,0 +1,195 @@
+#!/usr/bin/env jython
+
+import os.path
+import sys
+
+import stilts
+from java.lang import *
+from java.sql import *
+
+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 dvoobjects import DvoObjects
+
+import logging.config
+
+'''
+ObjectBatch class
+
+This class, a sub-class of Batch, processes single stack skycells in the form of IPP cmf files
+
+'''
+class ObjectBatch(Batch):
+
+    '''
+    Constructor
+    '''
+    def __init__(self, 
+                 logger, 
+                 config,
+                 gpc1Db,
+                 ippToPspsDb,
+                 scratchDb,
+                 dvoID,
+                 batchID,
+                 useFullTables):
+
+       super(ObjectBatch, self).__init__(
+               logger,
+               config,
+               gpc1Db,
+               ippToPspsDb,
+               scratchDb,
+               dvoID,
+               batchID,
+               "OB", 
+               None,
+               1)
+
+       if not self.everythingOK: return
+
+       self.dvoObjects = DvoObjects(self.logger, self.config, self.scratchDb.dbName)
+
+
+       # create an output filename, which is {dvoINDEX}.FITS
+       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. We import from DVO directly for objects
+    '''
+    def importIppTables(self, columns="*", filter=""):
+
+        self.region = self.scratchDb.getRegionNameFromThisDvoIndex(self.id)
+        self.ippToPspsDb.insertObjectMeta(self.batchID, self.region)
+        self.dvoObjects.ingestRegion(self.region)
+
+        return True
+
+    '''
+    Applies indexes to the PSPS tables
+    '''
+    def alterPspsTables(self):
+
+        self.logger.debug("Altering PSPS tables")
+        return True
+
+    '''
+    Applies indexes to the IPP tables
+    '''
+    def indexIppTables(self):
+
+        self.logger.infoPair("Creating indexes on", "IPP tables")
+
+        return True
+
+    '''
+    Inserts stuff for all mags
+    '''
+    def insertMags(self, cpsTable):
+
+        # do this properly with photcode table
+        filters = ['g', 'r', 'i', 'z', 'y']
+        
+        for filter in filters:
+
+            if filter == "g": index = 4
+            elif filter == "r": index = 3
+            elif filter == "i": index = 2
+            elif filter == "z": index = 1
+            elif filter == "y": index = 0
+
+            sql = "UPDATE Object \
+                   JOIN " + cpsTable + " AS cps ON (cps.row = (Object.row*5)-" + str(index) + ") \
+                   SET \
+                   " + filter + "MeanMag = MAG, \
+                   " + filter + "MeanMagErr = MAG_ERR"
+
+            self.scratchDb.execute(sql)
+
+    '''
+    Inserts colors
+    '''
+    def updateColors(self):
+
+        sql = "UPDATE Object \
+               SET \
+               grMeanColor = (gMeanMag - rMeanMag) \
+               ,riMeanColor = (rMeanMag - iMeanMag) \
+               ,izMeanColor = (iMeanMag - zMeanMag) \
+               ,zyMeanColor = (zMeanMag - yMeanMag) \
+               ,grMeanColorErr = (gMeanMagErr - rMeanMagErr) \
+               ,riMeanColorErr = (rMeanMagErr - iMeanMagErr) \
+               ,izMeanColorErr = (iMeanMagErr - zMeanMagErr) \
+               ,zyMeanColorErr = (zMeanMagErr - yMeanMagErr) \
+                   "
+        try:
+            self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair("Couldn't calculate colors", sql)
+
+
+    '''
+    Does the processing, i.e. pulling stuff from IPP tables into PSPS tables
+    '''
+    def populatePspsTables(self):
+
+        cptTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cpt")
+        cpsTableName = self.scratchDb.getDbFriendlyTableName(self.region + ".cps")
+
+        self.logger.infoPair("Inserting objects from", "cpt")
+        sql = "INSERT INTO Object (\
+               objID \
+               ,ippObjID \
+               ,objInfoFlag \
+               ,surveyID \
+               ,ra \
+               ,dec_ \
+               ,raErr \
+               ,decErr \
+               ,nDetections \
+               ) \
+               SELECT \
+               EXT_ID \
+               ,CAT_ID*1000000000 + OBJ_ID \
+               ,FLAGS \
+               ," + str(self.surveyID) + " \
+               ,RA \
+               ,DEC_ \
+               ,RA_ERR \
+               ,DEC_ERR \
+               ,NMEASURE \
+               FROM " + cptTableName
+
+        try:
+            self.scratchDb.execute(sql)
+        except:
+            self.logger.errorPair("Couldn't populate Object table", sql)
+
+        # add row count columns so we can perform joins to get colors
+        self.logger.infoPair("Adding 'row' columns to", "Object and cps tables")
+        self.scratchDb.addRowCountColumn("Object", "row")
+        self.scratchDb.addRowCountColumn(cpsTableName, "row")
+
+        self.logger.infoPair("Adding magnitudes from", "cps table")
+        self.insertMags(cpsTableName)
+
+        self.logger.infoPair("Calculating", "colors")
+        self.updateColors()
+
+        self.logger.infoPair("Dropping row column from", "Object table")
+        self.scratchDb.dropColumn("Object", "row")
+        self.logger.infoPair("Purging from scratch Db", self.region + " region")
+        self.dvoObjects.purgeRegion(self.region)
+
+        self.setMinMaxObjID(["Object"])
+
+        return True
+
