#!/usr/bin/env jython

import stilts
from java.lang import *
from java.sql import *
from batch import Batch

class DetectionBatch(Batch):

    '''
    Constructor
    '''
    def __init__(self):
       super(DetectionBatch, self).__init__(
               "detection", 
               "detdemo.fits", 
               "detection.fits",
               "localhost",
               "ipptopsps_scratch",
               "ipp",
               "ipp",
               "3PI") # TODO



    '''
    Updates a table with stackMetaID
    '''
    def updateStackMetaID(self, table):

        sql = "UPDATE " + table + "  SET stackMetaID=" + self.header['STK_ID']
        self.stmt.execute(sql)

    '''
    Updates a table with stackTypeID grabbed from FitModel init table
    '''
    def updateStackTypeID(self, table):

        sql = "UPDATE "+table+" AS a, StackType AS b SET a.stackTypeID=b.stackTypeID WHERE b.name = '"+self.header['STK_TYPE']+"'"
        self.stmt.execute(sql)

    '''
    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 \
         ) VALUES ( \
        " + self.header['STK_ID'] + " \
        ," + self.skycell + " \
        ," + self.header['FPA.ZP'] + " \
        ," + self.header['NINPUTS'] + " \
        ,'" + self.header['CTYPE1'] + "' \
        ,'" + self.header['CTYPE2'] + "' \
        ," + self.header['CRVAL1'] + " \
        ," + self.header['CRVAL2'] + " \
        ," + self.header['CRPIX1'] + " \
        ," + self.header['CRPIX2'] + " \
        ," + self.header['CDELT1'] + " \
        ," + self.header['CDELT2'] + " \
        ," + self.header['PC001001'] + " \
        ," + self.header['PC001002'] + " \
        ," + self.header['PC002001'] + " \
        ," + self.header['PC002002'] + " \
        )"
        self.stmt.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.stmt.execute(sql)


    '''
    Applies indexes to the PSPS tables
    '''
    def indexPspsTables(self):

        self.log("Creating indexes on PSPS tables")
#        self.createIndex("StackDetection", "ippDetectID")
 #       self.createIndex("StackApFlx", "ippDetectID")
  #      self.createIndex("StackModelFit", "ippDetectID")

    '''
    Applies indexes to the IPP tables
    '''
    def indexIppTables(self):

        self.log("Creating indexes on IPP tables")

        for x in range(0,7):
           for y in range(0,7):

               extension = "XY%d%d_psf" % (x, y)
               self.createIndex(extension, "IPP_IDET")

    '''
    Does the processing, i.e. pulling stuff from IPP tables into PSPS tables
    '''
    def populatePspsTables(self):

        # get filterID using init table
        self.filter = self.header['FILTERID']
        self.filter = self.filter[0:1]
        print "filter = ", self.filter

        #self.populateStackMeta()
        self.populateDetectionTables()
        #self.populateStackModelFit()
        #self.populateStackApFlx()

    
detectionBatch = DetectionBatch()
detectionBatch.createEmptyPspsTables()
detectionBatch.importIppTables("XY33\.psf")
detectionBatch.populatePspsTables()
detectionBatch.exportPspsTablesToFits()
