Index: trunk/ippToPsps/jython/detectionbatch.py
===================================================================
--- trunk/ippToPsps/jython/detectionbatch.py	(revision 31299)
+++ trunk/ippToPsps/jython/detectionbatch.py	(revision 31303)
@@ -21,8 +21,9 @@
                "detection", 
                inputFile, 
-               "ThreePi") # TODO
+               "3PI") # TODO
 
        self.logger.info("DetectionBatch constructor. Creating batch from: '" + inputFile + "'")
 
+       self.expID = expID
        # create an output filename, which is {expID}.FITS
        self.outputFitsFile = "%08d.FITS" % expID
@@ -33,29 +34,50 @@
     Populates the FrameMeta table, mainly from dictionary values found in IPP FITS header
     '''
-    def populateImageMeta(self):
-        self.log("Procesing ImageMeta table")
-        '''
-        sql = "INSERT INTO ImageMeta (\
-        stackMetaID \
-        ,skycellID \
-        ,photoZero \
-        ,nP2Images \
-        ,ctype1 \
-        ,ctype2 \
-        ,crval1 \
-        ,crval2 \
-        ,crpix1 \
-        ,crpix2 \
-        ,cdelt1 \
-        ,cdelt2 \
-        ,pc001001 \
-        ,pc001002 \
-        ,pc002001 \
-        ,pc002002 \
+    def populateFrameMeta(self):
+        self.log("Procesing FrameMeta table")
+
+        sql = "INSERT INTO FrameMeta (\
+        frameID \
+         ,photoScat \
+         ,expStart \
+         ,expTime \
+         ,airmass \
+         ,raBore \
+         ,decBore \
+         ,ctype1 \
+         ,ctype2 \
+         ,crval1 \
+         ,crval2 \
+         ,crpix1 \
+         ,crpix2 \
+         ,cdelt1 \
+         ,cdelt2 \
+         ,pc001001 \
+         ,pc001002 \
+         ,pc002001 \
+         ,pc002002 \
+         ,polyOrder \
+         ,pca1x3y0 \
+         ,pca1x2y1 \
+         ,pca1x1y2 \
+         ,pca1x0y3 \
+         ,pca1x2y0 \
+         ,pca1x1y1 \
+         ,pca1x0y2 \
+         ,pca2x3y0 \
+         ,pca2x2y1 \
+         ,pca2x1y2 \
+         ,pca2x0y3 \
+         ,pca2x2y0 \
+         ,pca2x1y1 \
+         ,pca2x0y2 \
          ) VALUES ( \
-        " + self.header['STK_ID'] + " \
-        ," + self.skycell + " \
-        ," + self.header['FPA.ZP'] + " \
-        ," + self.header['NINPUTS'] + " \
+        " + str(self.expID) + " \
+        ," + self.header['ZPT_ERR'] + " \
+        ," + self.header['MJD-OBS'] + " \
+        ," + self.header['EXPREQ'] + " \
+        ," + self.header['AIRMASS'] + " \
+        ," + self.header['RA'] + " \
+        ," + self.header['DEC'] + " \
         ,'" + self.header['CTYPE1'] + "' \
         ,'" + self.header['CTYPE2'] + "' \
@@ -70,31 +92,253 @@
         ," + self.header['PC002001'] + " \
         ," + self.header['PC002002'] + " \
+        ," + self.header['NPLYTERM'] + " \
+        ," + self.header['PCA1X3Y0'] + " \
+        ," + self.header['PCA1X2Y1'] + " \
+        ," + self.header['PCA1X1Y2'] + " \
+        ," + self.header['PCA1X0Y3'] + " \
+        ," + self.header['PCA1X2Y0'] + " \
+        ," + self.header['PCA1X1Y1'] + " \
+        ," + self.header['PCA1X0Y2'] + " \
+        ," + self.header['PCA2X3Y0'] + " \
+        ," + self.header['PCA2X2Y1'] + " \
+        ," + self.header['PCA2X1Y2'] + " \
+        ," + self.header['PCA2X0Y3'] + " \
+        ," + self.header['PCA2X2Y0'] + " \
+        ," + self.header['PCA2X1Y1'] + " \
+        ," + self.header['PCA2X0Y2'] + " \
         )"
         self.localStmt.execute(sql)
 
-        self.updateSurveyID("ImageMeta")
-        self.updateFilterID("ImageMeta")
-        self.updateStackTypeID("ImageMeta")
-        '''
-
-    '''
-    Populates the Detection tables
-    '''
-    def populateDetectionTables(self):
-
-        self.log("Procesing Detection tables")
-        for x in range(3,4):
-           for y in range(3,4):
-
-               self.log("Populating detections for OTA %d%d" % (x, y))
-               
-               sql = "INSERT INTO Detection ( \
-                      ippDetectID \
-                      ) \
-                      SELECT \
-                      IPP_IDET \
-                      FROM XY%d%d_psf" % (x, y)
-
-               self.localStmt.execute(sql)
+        self.updateSurveyID("FrameMeta")
+        self.updateFilterID("FrameMeta")
+
+    '''
+    Populates the ImageMeta table for this OTA
+    '''
+    def populateImageMetaTable(self, ota, header):
+
+        tableName = "ImageMeta_" + ota
+        self.log("   Dealing with table " + tableName)
+        
+        # drop then re-create table
+        self.dropTable(tableName)
+        sql = "CREATE TABLE " + tableName + " LIKE ImageMeta"
+        try: self.localStmt.execute(sql)
+        except: pass
+
+        # insert all detections into table
+        sql = "INSERT INTO " + tableName + " ( \
+               frameID \
+               ,sky \
+               ,skyScat \
+               ,magSat \
+               ,completMag \
+               ,astroScat \
+               ,numAstroRef \
+               ,numPhotoRef \
+               ,nx \
+               ,ny \
+               ,psfWidMajor \
+               ,psfWidMinor \
+               ,psfTheta \
+               ,momentWidMajor \
+               ,momentWidMinor \
+               ,apResid \
+               ,dapResid \
+               ,detectorID \
+               ,detrend1 \
+               ,detrend2 \
+               ,detrend3 \
+               ,ctype1 \
+               ,ctype2 \
+               ,crval1 \
+               ,crval2 \
+               ,crpix1 \
+               ,crpix2 \
+               ,cdelt1 \
+               ,cdelt2 \
+               ,pc001001 \
+               ,pc001002 \
+               ,pc002001 \
+               ,pc002002 \
+               ,polyOrder \
+               ,pca1x3y0 \
+               ,pca1x2y1 \
+               ,pca1x1y2 \
+               ,pca1x0y3 \
+               ,pca1x2y0 \
+               ,pca1x1y1 \
+               ,pca1x0y2 \
+               ,pca2x3y0 \
+               ,pca2x2y1 \
+               ,pca2x1y2 \
+               ,pca2x0y3 \
+               ,pca2x2y0 \
+               ,pca2x1y1 \
+               ,pca2x0y2 \
+               ) VALUES ( \
+               " + str(self.expID) + " \
+               ," + header['MSKY_MN'] + " \
+               ," + header['MSKY_SIG'] + " \
+               ," + header['FSATUR'] + " \
+               ," + header['FLIMIT'] + " \
+               ," + header['CERROR'] + " \
+               ," + header['NASTRO'] + " \
+               ," + header['NASTRO'] + " \
+               ," + header['CNAXIS1'] + " \
+               ," + header['CNAXIS2'] + " \
+               ," + header['FWHM_MAJ'] + " \
+               ," + header['FWHM_MIN'] + " \
+               ," + header['ANGLE'] + " \
+               ," + header['IQ_FW1'] + " \
+               ," + header['IQ_FW2'] + " \
+               ," + header['APMIFIT'] + " \
+               ," + header['DAPMIFIT'] + " \
+               ,'" + header['DETECTOR'] + "' \
+               ,'" + header['DETREND.MASK'] + "' \
+               ,'" + header['DETREND.DARK'] + "' \
+               ,'" + header['DETREND.FLAT'] + "' \
+               ,'" + header['CTYPE1'] + "' \
+               ,'" + header['CTYPE2'] + "' \
+               ," + header['CRVAL1'] + " \
+               ," + header['CRVAL2'] + " \
+               ," + header['CRPIX1'] + " \
+               ," + header['CRPIX2'] + " \
+               ," + header['CDELT1'] + " \
+               ," + header['CDELT2'] + " \
+               ," + header['PC001001'] + " \
+               ," + header['PC001002'] + " \
+               ," + header['PC002001'] + " \
+               ," + header['PC002002'] + " \
+               ," + header['NPLYTERM'] + " \
+               ," + header['PCA1X3Y0'] + " \
+               ," + header['PCA1X2Y1'] + " \
+               ," + header['PCA1X1Y2'] + " \
+               ," + header['PCA1X0Y3'] + " \
+               ," + header['PCA1X2Y0'] + " \
+               ," + header['PCA1X1Y1'] + " \
+               ," + header['PCA1X0Y2'] + " \
+               ," + header['PCA2X3Y0'] + " \
+               ," + header['PCA2X2Y1'] + " \
+               ," + header['PCA2X1Y2'] + " \
+               ," + header['PCA2X0Y3'] + " \
+               ," + header['PCA2X2Y0'] + " \
+               ," + header['PCA2X1Y1'] + " \
+               ," + header['PCA2X0Y2'] + " \
+               )"
+        self.localStmt.execute(sql)
+        self.updateFilterID(tableName)
+
+    '''
+    Populates the Detection table for this OTA
+    '''
+    def populateDetectionTable(self, ota):
+
+        tableName = "Detection_" + ota
+        self.log("   Dealing with table " + tableName)
+        
+        # drop then re-create table
+        self.dropTable(tableName)
+        sql = "CREATE TABLE " + tableName + " LIKE Detection"
+        try: self.localStmt.execute(sql)
+        except: pass
+
+        # insert all detections into table
+        sql = "INSERT INTO " + tableName + " ( \
+               ippDetectID \
+               ,xPos \
+               ,yPos \
+               ,xPosErr \
+               ,yPosErr \
+               ,psfWidMajor \
+               ,psfWidMinor \
+               ,psfTheta \
+               ,psfCf \
+               ,momentXX \
+               ,momentXY \
+               ,momentYY \
+               ,sky \
+               ,skyErr \
+               ,sgSep \
+               ) \
+               SELECT \
+               IPP_IDET \
+               ,X_PSF \
+               ,Y_PSF \
+               ,X_PSF_SIG \
+               ,Y_PSF_SIG \
+               ,PSF_MAJOR \
+               ,PSF_MINOR \
+               ,PSF_THETA \
+               ,PSF_QF \
+               ,MOMENTS_XX \
+               ,MOMENTS_XY \
+               ,MOMENTS_YY \
+               ,SKY \
+               ,SKY_SIGMA \
+               ,EXT_NSIGMA \
+               FROM " + ota + "_psf"
+
+        self.localStmt.execute(sql)
+
+        self.updateSurveyID(tableName)
+        self.updateFilterID(tableName)
+
+        # now add DVO IDs
+        self.updateDvoIDs(tableName)
+
+    '''
+    Populates the SkinnyObject table for this OTA
+    '''
+    def populateSkinnyObjectTable(self, ota):
+
+        tableName = "SkinnyObject_" + ota
+        self.log("   Dealing with table " + tableName)
+        
+        # drop then re-create table
+        self.dropTable(tableName)
+        sql = "CREATE TABLE " + tableName + " LIKE SkinnyObject"
+        try: self.localStmt.execute(sql)
+        except: pass
+
+        # insert all detections into table
+        sql = "INSERT INTO " + tableName + " ( \
+               objID \
+               ,ippObjID \
+               ,surveyID \
+               ) \
+               SELECT \
+               objID \
+               ,ippObjID \
+               ,surveyID \
+               FROM Detection_" + ota
+        self.localStmt.execute(sql)
+
+    '''
+    Populates the Detection table for this OTA
+    '''
+    def populateObjectCalColorTable(self, ota):
+
+        tableName = "ObjectCalColor_" + ota
+        self.log("   Dealing with table " + tableName)
+        
+        # drop then re-create table
+        self.dropTable(tableName)
+        sql = "CREATE TABLE " + tableName + " LIKE ObjectCalColor"
+        try: self.localStmt.execute(sql)
+        except: pass
+
+        # insert all detections into table
+        sql = "INSERT INTO " + tableName + " ( \
+               objID \
+               ,ippObjID \
+               ,filterID \
+               ) \
+               SELECT \
+               objID \
+               ,ippObjID \
+               ,filterID \
+               FROM Detection_" + ota
+        self.localStmt.execute(sql)
 
 
@@ -123,4 +367,5 @@
 
     '''
+    Overriden from Batch class
     Updates provided table with DVO IDs from DVO table
     '''
@@ -142,17 +387,37 @@
     def populatePspsTables(self):
 
+
         # get filterID using init table
-        self.filter = self.header['FILTERID']
-        self.filter = self.filter[0:1]
-
-        #self.populateStackMeta()
-        self.populateDetectionTables()
-        #self.populateStackModelFit()
-        #self.populateStackApFlx()
-
-        self.getIDsFromDVO("33", "8345290") # TODO need to get sourceID and imageID from chip header
-        self.updateDvoIDs("Detection")
-        self.setMinMaxObjID("Detection")
-
+        self.filter = self.header['FILTERID'][0:1]
+
+        self.populateFrameMeta()
+
+        for x in range(3, 4):
+            for y in range(3, 4):
+                
+                ota = "XY%d%d" % (x, y)
+                
+                # load corresponding header into memory
+                header = self.findAndReadFITSHeader(ota + ".hdr")
+                self.getIDsFromDVO(header['SOURCEID'], header['IMAGEID'])
+ 
+                self.populateImageMetaTable(ota, header)
+                self.populateDetectionTable(ota)
+                self.populateSkinnyObjectTable(ota)
+                self.populateObjectCalColorTable(ota)
+
+
+        #self.setMinMaxObjID(tableName) # TODO set based on all tables
+
+
+    '''
+    Overriding this method
+    '''
+    def reportNullsInAllPspsTables(self, showPartials):
+
+        # loops round all imported tables, but we want to check one OTA's worth
+        for table in self.pspsTables:
+            if table.name == "FrameMeta": self.reportNulls(table.name, showPartials)
+            else: self.reportNulls(table.name + "_XY33", showPartials)
 
 
@@ -168,5 +433,4 @@
 detectionBatch.populatePspsTables()
 detectionBatch.exportPspsTablesToFits()
-detectionBatch.exportPspsTablesToFits()
 detectionBatch.writeBatchManifest()
 detectionBatch.reportNullsInAllPspsTables(False)
