IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 5, 2011, 10:05:10 AM (15 years ago)
Author:
eugene
Message:

merging updates from trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20110404/ippToPsps/jython/detectionbatch.py

    r31116 r31439  
    11#!/usr/bin/env jython
    22
     3import os.path
     4import sys
    35import stilts
    46from java.lang import *
    57from java.sql import *
    68from batch import Batch
    7 
     9from gpc1db import Gpc1Db
     10
     11import logging.config
     12
     13'''
     14DetectionBatch class
     15'''
    816class DetectionBatch(Batch):
    917
     
    1119    Constructor
    1220    '''
    13     def __init__(self):
     21    def __init__(self, logger, camID, inputFile, test=False, useFullTables=False):
    1422       super(DetectionBatch, self).__init__(
     23               logger,
    1524               "detection",
    16                "detdemo.fits",
    17                "detection.fits",
    18                "localhost",
    19                "ipptopsps_scratch",
    20                "ipp",
    21                "ipp",
    22                "3PI") # TODO
    23 
    24 
    25 
    26     '''
    27     Updates a table with stackMetaID
    28     '''
    29     def updateStackMetaID(self, table):
    30 
    31         sql = "UPDATE " + table + "  SET stackMetaID=" + self.header['STK_ID']
    32         self.stmt.execute(sql)
    33 
    34     '''
    35     Updates a table with stackTypeID grabbed from FitModel init table
    36     '''
    37     def updateStackTypeID(self, table):
    38 
    39         sql = "UPDATE "+table+" AS a, StackType AS b SET a.stackTypeID=b.stackTypeID WHERE b.name = '"+self.header['STK_TYPE']+"'"
    40         self.stmt.execute(sql)
     25               inputFile,
     26               "MD04",
     27               useFullTables) # TODO
     28               #"3PI") # TODO
     29
     30       self.logger.info("DetectionBatch constructor. Creating batch from: '" + inputFile + "'")
     31
     32       meta = self.gpc1Db.getCameraStageMeta(camID)
     33     
     34       self.expID = meta[0];
     35       self.expName = meta[1];
     36       self.distGroup = meta[2];
     37
     38       self.logger.info("Processing exposure with ID: %d, name: %s and distribution group: %s" % (self.expID, self.expName, self.distGroup))
     39
     40       # create an output filename, which is {expID}.FITS
     41       self.outputFitsFile = "%08d.FITS" % self.expID
     42       self.outputFitsPath = "%s/%s" % (self.localOutPath, self.outputFitsFile)
     43
     44       # if test mode
     45       if test:
     46           self.startX = 3
     47           self.endX = 4
     48           self.startY = 3
     49           self.endY = 4
     50       else:
     51           self.startX = 0
     52           self.endX = 8
     53           self.startY = 0
     54           self.endY = 8
     55
     56       #self.startX = 1
     57       #self.endX = 2
     58       #self.startY = 7
     59       #self.endY = 8
     60
     61       self.obsTime = float(self.header['MJD-OBS']) + (float(self.header['EXPTIME']) / 172800.0)
     62     
     63       # set up some defauts
     64       self.calibModNum = 0
     65       self.dataRelease = 0
     66
     67       self.totalNumPhotoRef = 0
     68
     69       # get filterID using init table
     70       self.filter = self.header['FILTERID'][0:1]
     71
     72       # insert what we know about this stack batch into the stack table
     73       self.ippToPspsDb.insertDetectionMeta(self.batchID, self.expID, self.filter)
     74
    4175
    4276    '''
    4377    Populates the FrameMeta table, mainly from dictionary values found in IPP FITS header
    4478    '''
    45     def populateImageMeta(self):
    46         self.log("Procesing ImageMeta table")
    47         '''
    48         sql = "INSERT INTO ImageMeta (\
    49         stackMetaID \
    50         ,skycellID \
    51         ,photoZero \
    52         ,nP2Images \
    53         ,ctype1 \
    54         ,ctype2 \
    55         ,crval1 \
    56         ,crval2 \
    57         ,crpix1 \
    58         ,crpix2 \
    59         ,cdelt1 \
    60         ,cdelt2 \
    61         ,pc001001 \
    62         ,pc001002 \
    63         ,pc002001 \
    64         ,pc002002 \
     79    def populateFrameMeta(self):
     80        self.logger.info("Procesing FrameMeta table")
     81
     82        sql = "INSERT INTO FrameMeta (\
     83          frameID \
     84         ,frameName \
     85         ,cameraID \
     86         ,cameraConfigID \
     87         ,telescopeID \
     88         ,analysisVer \
     89         ,p1Recip \
     90         ,p2Recip \
     91         ,p3Recip \
     92         ,photoScat \
     93         ,expStart \
     94         ,expTime \
     95         ,airmass \
     96         ,raBore \
     97         ,decBore \
     98         ,ctype1 \
     99         ,ctype2 \
     100         ,crval1 \
     101         ,crval2 \
     102         ,crpix1 \
     103         ,crpix2 \
     104         ,cdelt1 \
     105         ,cdelt2 \
     106         ,pc001001 \
     107         ,pc001002 \
     108         ,pc002001 \
     109         ,pc002002 \
     110         ,polyOrder \
     111         ,pca1x3y0 \
     112         ,pca1x2y1 \
     113         ,pca1x1y2 \
     114         ,pca1x0y3 \
     115         ,pca1x2y0 \
     116         ,pca1x1y1 \
     117         ,pca1x0y2 \
     118         ,pca2x3y0 \
     119         ,pca2x2y1 \
     120         ,pca2x1y2 \
     121         ,pca2x0y3 \
     122         ,pca2x2y0 \
     123         ,pca2x1y1 \
     124         ,pca2x0y2 \
    65125         ) VALUES ( \
    66         " + self.header['STK_ID'] + " \
    67         ," + self.skycell + " \
    68         ," + self.header['FPA.ZP'] + " \
    69         ," + self.header['NINPUTS'] + " \
     126        " + str(self.expID) + " \
     127        ,'" + self.expName + "' \
     128        ,1  \
     129        ,1  \
     130        ,1  \
     131        ,' ' \
     132        ,' ' \
     133        ,' ' \
     134        ,' ' \
     135        ," + self.header['ZPT_ERR'] + " \
     136        ," + self.header['MJD-OBS'] + " \
     137        ," + self.header['EXPREQ'] + " \
     138        ," + self.header['AIRMASS'] + " \
     139        ," + self.header['RA'] + " \
     140        ," + self.header['DEC'] + " \
    70141        ,'" + self.header['CTYPE1'] + "' \
    71142        ,'" + self.header['CTYPE2'] + "' \
     
    80151        ," + self.header['PC002001'] + " \
    81152        ," + self.header['PC002002'] + " \
     153        ," + self.header['NPLYTERM'] + " \
     154        ," + self.header['PCA1X3Y0'] + " \
     155        ," + self.header['PCA1X2Y1'] + " \
     156        ," + self.header['PCA1X1Y2'] + " \
     157        ," + self.header['PCA1X0Y3'] + " \
     158        ," + self.header['PCA1X2Y0'] + " \
     159        ," + self.header['PCA1X1Y1'] + " \
     160        ," + self.header['PCA1X0Y2'] + " \
     161        ," + self.header['PCA2X3Y0'] + " \
     162        ," + self.header['PCA2X2Y1'] + " \
     163        ," + self.header['PCA2X1Y2'] + " \
     164        ," + self.header['PCA2X0Y3'] + " \
     165        ," + self.header['PCA2X2Y0'] + " \
     166        ," + self.header['PCA2X1Y1'] + " \
     167        ," + self.header['PCA2X0Y2'] + " \
    82168        )"
    83         self.stmt.execute(sql)
    84 
    85         self.updateSurveyID("ImageMeta")
    86         self.updateFilterID("ImageMeta")
    87         self.updateStackTypeID("ImageMeta")
    88         '''
    89 
    90     '''
    91     Populates the Detection tables
    92     '''
    93     def populateDetectionTables(self):
    94 
    95         self.log("Procesing Detection tables")
    96         for x in range(3,4):
    97            for y in range(3,4):
    98 
    99                self.log("Populating detections for OTA %d%d" % (x, y))
     169        self.scratchDb.stmt.execute(sql)
     170
     171        self.scratchDb.updateAllRows("FrameMeta", "surveyID", str(self.surveyID))
     172        self.scratchDb.updateFilterID("FrameMeta", self.filter)
     173        self.scratchDb.updateAllRows("FrameMeta", "calibModNum", str(self.calibModNum))
     174        self.scratchDb.updateAllRows("FrameMeta", "dataRelease", str(self.dataRelease))
     175
     176    '''
     177    Populates the ImageMeta table for this OTA
     178    '''
     179    def populateImageMetaTable(self, ota, header):
     180
     181        tableName = "ImageMeta_" + ota
     182       
     183        # drop then re-create table
     184        self.scratchDb.dropTable(tableName)
     185        sql = "CREATE TABLE " + tableName + " LIKE ImageMeta"
     186        try: self.scratchDb.stmt.execute(sql)
     187        except: pass
     188
     189        # insert all detections into table
     190        sql = "INSERT INTO " + tableName + " ( \
     191               frameID \
     192               ,ccdID \
     193               ,sky \
     194               ,skyScat \
     195               ,magSat \
     196               ,completMag \
     197               ,astroScat \
     198               ,photoScat \
     199               ,numAstroRef \
     200               ,numPhotoRef \
     201               ,nx \
     202               ,ny \
     203               ,psfFwhm \
     204               ,psfWidMajor \
     205               ,psfWidMinor \
     206               ,psfTheta \
     207               ,momentFwhm \
     208               ,momentWidMajor \
     209               ,momentWidMinor \
     210               ,apResid \
     211               ,dapResid \
     212               ,detectorID \
     213               ,qaFlags \
     214               ,detrend1 \
     215               ,detrend2 \
     216               ,detrend3 \
     217               ,detrend4 \
     218               ,detrend5 \
     219               ,detrend6 \
     220               ,detrend7 \
     221               ,detrend8 \
     222               ,photoZero \
     223               ,ctype1 \
     224               ,ctype2 \
     225               ,crval1 \
     226               ,crval2 \
     227               ,crpix1 \
     228               ,crpix2 \
     229               ,cdelt1 \
     230               ,cdelt2 \
     231               ,pc001001 \
     232               ,pc001002 \
     233               ,pc002001 \
     234               ,pc002002 \
     235               ,polyOrder \
     236               ,pca1x3y0 \
     237               ,pca1x2y1 \
     238               ,pca1x1y2 \
     239               ,pca1x0y3 \
     240               ,pca1x2y0 \
     241               ,pca1x1y1 \
     242               ,pca1x0y2 \
     243               ,pca2x3y0 \
     244               ,pca2x2y1 \
     245               ,pca2x1y2 \
     246               ,pca2x0y3 \
     247               ,pca2x2y0 \
     248               ,pca2x1y1 \
     249               ,pca2x0y2 \
     250               ) VALUES ( \
     251               " + str(self.expID) + " \
     252               ," + ota[2:4] + " \
     253               ," + self.safeDictionaryAccess(header, 'MSKY_MN') + " \
     254               ," + self.safeDictionaryAccess(header, 'MSKY_SIG') + " \
     255               ," + self.safeDictionaryAccess(header, 'FSATUR') + " \
     256               ," + self.safeDictionaryAccess(header, 'FLIMIT') + " \
     257               ," + self.safeDictionaryAccess(header, 'CERROR') + " \
     258               ," + self.safeDictionaryAccess(self.header, 'ZPT_OBS') + " \
     259               ," + self.safeDictionaryAccess(header, 'NASTRO') + " \
     260               ," + self.safeDictionaryAccess(header, 'NASTRO') + " \
     261               ," + self.safeDictionaryAccess(header, 'CNAXIS1') + " \
     262               ," + self.safeDictionaryAccess(header, 'CNAXIS2') + " \
     263               ," + str((float(self.safeDictionaryAccess(header, 'FWHM_MAJ')) + float(self.safeDictionaryAccess(header, 'FWHM_MIN')))/2.0) + " \
     264               ," + self.safeDictionaryAccess(header, 'FWHM_MAJ') + " \
     265               ," + self.safeDictionaryAccess(header, 'FWHM_MIN') + " \
     266               ," + self.safeDictionaryAccess(header, 'ANGLE') + " \
     267               ," + str((float(self.safeDictionaryAccess(header, 'IQ_FW1')) + float(self.safeDictionaryAccess(header, 'IQ_FW2')))/2.0) + " \
     268               ," + self.safeDictionaryAccess(header, 'IQ_FW1') + " \
     269               ," + self.safeDictionaryAccess(header, 'IQ_FW2') + " \
     270               ," + self.safeDictionaryAccess(header, 'APMIFIT') + " \
     271               ," + self.safeDictionaryAccess(header, 'DAPMIFIT') + " \
     272               ,'" + self.safeDictionaryAccess(header, 'DETECTOR') + "' \
     273               ," + str(self.scratchDb.getDvoImageFlags(header['SOURCEID'], header['IMAGEID'])) + " \
     274               ,'" + self.safeDictionaryAccess(header, 'DETREND.MASK') + "' \
     275               ,'" + self.safeDictionaryAccess(header, 'DETREND.DARK') + "' \
     276               ,'" + self.safeDictionaryAccess(header, 'DETREND.FLAT') + "' \
     277               ,' ' \
     278               ,' ' \
     279               ,' ' \
     280               ,' ' \
     281               ,' ' \
     282               ," + self.safeDictionaryAccess(self.header, 'ZPT_OBS') + " \
     283               ,'" + self.safeDictionaryAccess(header, 'CTYPE1') + "' \
     284               ,'" + self.safeDictionaryAccess(header, 'CTYPE2') + "' \
     285               ," + self.safeDictionaryAccess(header, 'CRVAL1') + " \
     286               ," + self.safeDictionaryAccess(header, 'CRVAL2') + " \
     287               ," + self.safeDictionaryAccess(header, 'CRPIX1') + " \
     288               ," + self.safeDictionaryAccess(header, 'CRPIX2') + " \
     289               ," + self.safeDictionaryAccess(header, 'CDELT1') + " \
     290               ," + self.safeDictionaryAccess(header, 'CDELT2') + " \
     291               ," + self.safeDictionaryAccess(header, 'PC001001') + " \
     292               ," + self.safeDictionaryAccess(header, 'PC001002') + " \
     293               ," + self.safeDictionaryAccess(header, 'PC002001') + " \
     294               ," + self.safeDictionaryAccess(header, 'PC002002') + " \
     295               ," + self.safeDictionaryAccess(header, 'NPLYTERM') + " \
     296               ," + self.safeDictionaryAccess(header, 'PCA1X3Y0') + " \
     297               ," + self.safeDictionaryAccess(header, 'PCA1X2Y1') + " \
     298               ," + self.safeDictionaryAccess(header, 'PCA1X1Y2') + " \
     299               ," + self.safeDictionaryAccess(header, 'PCA1X0Y3') + " \
     300               ," + self.safeDictionaryAccess(header, 'PCA1X2Y0') + " \
     301               ," + self.safeDictionaryAccess(header, 'PCA1X1Y1') + " \
     302               ," + self.safeDictionaryAccess(header, 'PCA1X0Y2') + " \
     303               ," + self.safeDictionaryAccess(header, 'PCA2X3Y0') + " \
     304               ," + self.safeDictionaryAccess(header, 'PCA2X2Y1') + " \
     305               ," + self.safeDictionaryAccess(header, 'PCA2X1Y2') + " \
     306               ," + self.safeDictionaryAccess(header, 'PCA2X0Y3') + " \
     307               ," + self.safeDictionaryAccess(header, 'PCA2X2Y0') + " \
     308               ," + self.safeDictionaryAccess(header, 'PCA2X1Y1') + " \
     309               ," + self.safeDictionaryAccess(header, 'PCA2X0Y2') + " \
     310               )"
     311
     312        self.scratchDb.stmt.execute(sql)
     313        self.scratchDb.updateFilterID(tableName, self.filter)
     314        self.scratchDb.updateAllRows(tableName, "calibModNum", str(self.calibModNum))
     315        self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.dataRelease))
     316        self.totalNumPhotoRef = self.totalNumPhotoRef + int(header['NASTRO'])
     317        self.scratchDb.replaceNullsInThisColumn(tableName, "polyOrder", "0")
     318
     319    '''
     320    Populates the Detection table for this OTA
     321    '''
     322    def populateDetectionTable(self, ota):
     323
     324        tableName = "Detection_" + ota
     325       
     326        # drop then re-create table
     327        self.scratchDb.dropTable(tableName)
     328        sql = "CREATE TABLE " + tableName + " LIKE Detection"
     329        try: self.scratchDb.stmt.execute(sql)
     330        except: pass
     331
     332        # insert all detections into table
     333        sql = "INSERT INTO " + tableName + " ( \
     334               ippDetectID \
     335               ,xPos \
     336               ,yPos \
     337               ,xPosErr \
     338               ,yPosErr \
     339               ,instFlux \
     340               ,instFluxErr \
     341               ,peakADU \
     342               ,psfWidMajor \
     343               ,psfWidMinor \
     344               ,psfTheta \
     345               ,psfCf \
     346               ,momentXX \
     347               ,momentXY \
     348               ,momentYY \
     349               ,apMag \
     350               ,infoFlag \
     351               ,sky \
     352               ,skyErr \
     353               ,sgSep \
     354               ) \
     355               SELECT \
     356               IPP_IDET \
     357               ,X_PSF \
     358               ,Y_PSF \
     359               ,X_PSF_SIG \
     360               ,Y_PSF_SIG \
     361               ,POW(10.0, (-0.4*PSF_INST_MAG)) / "+self.header['EXPTIME']+" \
     362               ,ABS((PSF_INST_MAG_SIG*(POW(10.0, (-0.4*PSF_INST_MAG)) / "+self.header['EXPTIME']+")) / 1.085736) \
     363               ,POW(10.0, (-0.4*PEAK_FLUX_AS_MAG)) / "+self.header['EXPTIME']+" \
     364               ,PSF_MAJOR \
     365               ,PSF_MINOR \
     366               ,PSF_THETA \
     367               ,PSF_QF \
     368               ,MOMENTS_XX \
     369               ,MOMENTS_XY \
     370               ,MOMENTS_YY \
     371               ,AP_MAG \
     372               ,FLAGS\
     373               ,SKY \
     374               ,SKY_SIGMA \
     375               ,EXT_NSIGMA \
     376               FROM " + ota + "_psf"
     377
     378        self.scratchDb.stmt.execute(sql)
     379
     380        # set obsTime
     381        sql = "UPDATE " + tableName + " SET obsTime = %f, assocDate = '%s', activeFlag = 0" % (self.obsTime, self.dateStr)
     382        self.scratchDb.stmt.execute(sql)
     383        self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.dataRelease))
     384        self.scratchDb.updateAllRows(tableName, "historyModNum", "0")
     385
     386        self.scratchDb.updateAllRows(tableName, "surveyID", str(self.surveyID))
     387        self.scratchDb.updateFilterID(tableName, self.filter)
     388
     389        # now delete bad flux
     390        self.scratchDb.reportAndDeleteRowsWithNULLS(tableName, "instFlux")
     391        self.scratchDb.reportAndDeleteRowsWithNULLS(tableName, "peakADU")
     392
     393    '''
     394    Populates the SkinnyObject table for this OTA
     395    '''
     396    def populateSkinnyObjectTable(self, ota):
     397
     398        tableName = "SkinnyObject_" + ota
     399       
     400        # drop then re-create table
     401        self.scratchDb.dropTable(tableName)
     402        sql = "CREATE TABLE " + tableName + " LIKE SkinnyObject"
     403        try: self.scratchDb.stmt.execute(sql)
     404        except: pass
     405
     406        # insert all detections into table
     407        sql = "INSERT INTO " + tableName + " ( \
     408               objID \
     409               ,ippObjID \
     410               ,surveyID \
     411               ) \
     412               SELECT \
     413               objID \
     414               ,ippObjID \
     415               ,surveyID \
     416               FROM Detection_" + ota
     417        self.scratchDb.stmt.execute(sql)
     418
     419        self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.dataRelease))
     420
     421    '''
     422    Populates the Detection table for this OTA
     423    '''
     424    def populateObjectCalColorTable(self, ota):
     425
     426        tableName = "ObjectCalColor_" + ota
     427       
     428        # drop then re-create table
     429        self.scratchDb.dropTable(tableName)
     430        sql = "CREATE TABLE " + tableName + " LIKE ObjectCalColor"
     431        try: self.scratchDb.stmt.execute(sql)
     432        except: pass
     433
     434        # insert all detections into table
     435        sql = "INSERT INTO " + tableName + " ( \
     436               objID \
     437               ,ippObjID \
     438               ,filterID \
     439               ) \
     440               SELECT \
     441               objID \
     442               ,ippObjID \
     443               ,filterID \
     444               FROM Detection_" + ota
     445        self.scratchDb.stmt.execute(sql)
     446
     447        self.scratchDb.updateAllRows(tableName, "calibModNum", str(self.calibModNum))
     448        self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.dataRelease))
     449
     450
     451    '''
     452    Applies indexes and other constraints to the PSPS tables
     453    '''
     454    def alterPspsTables(self):
     455
     456        self.logger.info("Altering PSPS tables")
     457        self.logger.info("Creating indexes on PSPS tables")
     458        self.scratchDb.makeColumnUnique("Detection", "objID")
     459        self.scratchDb.createIndex("Detection", "ippDetectID")
     460
     461    '''
     462    Applies indexes to the IPP tables
     463    '''
     464    def indexIppTables(self):
     465
     466        self.logger.info("Creating indexes on IPP tables")
     467
     468        for x in range(self.startX, self.endX):
     469            for y in range(self.startY, self.endY):
     470
     471                # dodge the corners
     472                if x==0 and y==0: continue
     473                if x==0 and y==7: continue
     474                if x==7 and y==0: continue
     475                if x==7 and y==7: continue
     476
     477                extension = "XY%d%d_psf" % (x, y)
     478                self.scratchDb.createIndex(extension, "IPP_IDET")
     479
     480    '''
     481    Updates provided table with DVO IDs from DVO table
     482    '''
     483    def updateDvoIDs(self, table, sourceID, externID):
     484
     485        imageID = self.scratchDb.getImageIDFromExternID(sourceID, externID)
     486        self.logger.info("Updating table '" + table + "' with DVO IDs using imageID = %d" % imageID)
     487        sql = "UPDATE IGNORE " + table + " AS a, " + self.scratchDb.dvoDetection + " AS b SET \
     488               a.ippObjID = b.ippObjID, \
     489               a.detectID = b.detectID, \
     490               a.objID = b.objID, \
     491               a.infoFlag = b.flags << 32 | a.infoFlag \
     492               WHERE a.ippDetectID = b.ippDetectID \
     493               AND b.sourceID = " + str(sourceID) + " \
     494               AND b.imageID = " + str(imageID)
     495
     496        self.scratchDb.stmt.execute(sql)
     497
     498
     499    '''
     500    Does the processing, i.e. pulling stuff from IPP tables into PSPS tables
     501    '''
     502    def populatePspsTables(self):
     503
     504        self.populateFrameMeta()
     505     
     506        # dictionary objects to hold sourceIDs and imageIDs for later
     507        sourceIDs = {}
     508        imageIDs = {}
     509
     510        file = open(self.inputFitsPath, 'r')
     511
     512        # loop through all OTAs and populate ImageMeta extensions
     513        for x in range(self.startX, self.endX):
     514            for y in range(self.startY, self.endY):
    100515               
    101                sql = "INSERT INTO Detection ( \
    102                       ippDetectID \
    103                       ) \
    104                       SELECT \
    105                       IPP_IDET \
    106                       FROM XY%d%d_psf" % (x, y)
    107 
    108                self.stmt.execute(sql)
    109 
    110 
    111     '''
    112     Applies indexes to the PSPS tables
    113     '''
    114     def indexPspsTables(self):
    115 
    116         self.log("Creating indexes on PSPS tables")
    117 #        self.createIndex("StackDetection", "ippDetectID")
    118  #       self.createIndex("StackApFlx", "ippDetectID")
    119   #      self.createIndex("StackModelFit", "ippDetectID")
    120 
    121     '''
    122     Applies indexes to the IPP tables
    123     '''
    124     def indexIppTables(self):
    125 
    126         self.log("Creating indexes on IPP tables")
    127 
    128         for x in range(0,7):
    129            for y in range(0,7):
    130 
    131                extension = "XY%d%d_psf" % (x, y)
    132                self.createIndex(extension, "IPP_IDET")
    133 
    134     '''
    135     Does the processing, i.e. pulling stuff from IPP tables into PSPS tables
    136     '''
    137     def populatePspsTables(self):
    138 
    139         # get filterID using init table
    140         self.filter = self.header['FILTERID']
    141         self.filter = self.filter[0:1]
    142         print "filter = ", self.filter
    143 
    144         #self.populateStackMeta()
    145         self.populateDetectionTables()
    146         #self.populateStackModelFit()
    147         #self.populateStackApFlx()
    148 
     516                # dodge the corners
     517                if x==0 and y==0: continue
     518                if x==0 and y==7: continue
     519                if x==7 and y==0: continue
     520                if x==7 and y==7: continue
     521
     522                ota = "XY%d%d" % (x, y)
     523               
     524                # load corresponding header into memory
     525                header = self.findAndReadFITSHeader(ota + ".hdr", file)
     526
     527                # store sourceID/imageID combo in Db so DVO can look up later
     528                if not self.useFullTables:
     529                    self.scratchDb.insertNewDvoImage(header['SOURCEID'], header['IMAGEID'])
     530
     531                # check we have valid sourceID/imageID pair from the header
     532                if 'SOURCEID' not in header or 'IMAGEID' not in header:
     533                    self.logger.error("Can't read SOURCEID/IMAGEID pair from " + ota + " header")
     534                    continue
     535
     536                # store these for later
     537                sourceIDs[ota] = header['SOURCEID']
     538                imageIDs[ota] = header['IMAGEID']
     539
     540                # populate ImageMeta
     541                self.populateImageMetaTable(ota, header)
     542                self.updateImageID("ImageMeta_" + ota, x, y)
     543             
     544        # now run DVO code to get all IDs
     545        if not self.useFullTables: self.getIDsFromDVO()
     546
     547        # loop through all OTAs again to update with DVO IDs
     548        self.tablesToExport = []   
     549        self.tablesToExport.append("FrameMeta")
     550        tables = []   
     551        otaCount = 0
     552        for x in range(self.startX, self.endX):
     553            for y in range(self.startY, self.endY):
     554               
     555                # dodge the corners
     556                if x==0 and y==0: continue
     557                if x==0 and y==7: continue
     558                if x==7 and y==0: continue
     559                if x==7 and y==7: continue
     560
     561                ota = "XY%d%d" % (x, y)
     562                if ota not in sourceIDs: continue
     563
     564                self.logger.info("******************* Dealing with OTA " + ota + " *******************")
     565
     566                # populate remainder of tables
     567                self.populateDetectionTable(ota)
     568
     569                # now add DVO IDs
     570                self.updateDvoIDs("Detection_" + ota, sourceIDs[ota], imageIDs[ota])
     571                self.scratchDb.reportAndDeleteRowsWithNULLS("Detection_" + ota, "objID")
     572                self.updateImageID("Detection_" + ota, x, y)
     573               
     574                # check we have something in this Detection table
     575                if self.scratchDb.getRowCount("Detection_" + ota) < 1:
     576                    self.logger.info("Empty tables for ota " + ota + ". Skipping.")
     577                    continue;
     578
     579                # update ImageMeta with count of detections for this OTA and photoCodeID
     580                sql = "UPDATE ImageMeta_" + ota + " SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(sourceIDs[ota], imageIDs[ota]))
     581                self.scratchDb.stmt.execute(sql)
     582
     583                self.populateSkinnyObjectTable(ota)
     584                self.populateObjectCalColorTable(ota)
     585
     586                # add these to list of tables to export later
     587                self.tablesToExport.append("ImageMeta_" + ota)
     588                self.tablesToExport.append("Detection_" + ota)
     589                self.tablesToExport.append("SkinnyObject_" + ota)
     590                self.tablesToExport.append("ObjectCalColor_" + ota)
     591                tables.append("Detection_" + ota)
     592           
     593                otaCount = otaCount + 1
     594
     595        # if we only have one table export, i.e. FrameMeta, then get out of here
     596        if len(self.tablesToExport) == 1:
     597           
     598            self.logger.error("No tables to export")
     599            return False
     600
     601        self.setMinMaxObjID(tables)
     602
     603        # update FrameMeta with count OTAs in this file and total number of photometric reference sources
     604        sql = "UPDATE FrameMeta SET nOTA = %d, numPhotoRef = %d" % (otaCount, self.totalNumPhotoRef)
     605        self.scratchDb.stmt.execute(sql)
     606       
     607        return True
     608
     609    '''
     610    Updates imageID {EXP_ID}{OTA} in the provided table
     611    '''
     612    def updateImageID(self, tableName, x, y):
     613
     614        sql = "UPDATE " + tableName + " SET imageID = %d%d%d" % (self.expID, x, y)
     615        self.scratchDb.stmt.execute(sql)
     616
     617    '''
     618    Checks whether this batch has already been processed and published
     619    '''
     620    def alreadyProcessed(self):
     621
     622        return self.ippToPspsDb.alreadyProcessed("detection", "exp_id", self.expID)
     623
     624
     625    '''
     626    Overriding this method
     627    '''
     628    def reportNullsInAllPspsTables(self, showPartials):
     629
     630        # loops round all imported tables, but we want to check one OTA's worth
     631        for table in self.pspsTables:
     632            if table.name == "FrameMeta": self.scratchDb.reportNulls(table.name, showPartials)
     633            else: self.scratchDb.reportNulls(table.name + "_XY33", showPartials)
     634
     635
     636logging.config.fileConfig("logging.conf")
     637logger = logging.getLogger("detectionbatch")
     638logger.info("Starting")
     639
     640gpc1Db = Gpc1Db(logger)
     641camIDs = gpc1Db.getIDsInThisDVODbForThisStage("MD04.V2", "cam")
     642logger.info("Found %d exposures" % len(camIDs))
     643
     644i = 0
     645for camID in camIDs:
     646
     647    logger.info("-------------------------------------------------- cam ID: %d" % camID)
     648
     649    file = gpc1Db.getCameraStageSmf(camID)
     650    if not os.path.isfile(file):
     651        logger.error("Cannot read file at '" + file)
     652        continue
     653
     654    detectionBatch = DetectionBatch(logger, camID, file, False, True)
     655
     656    if not detectionBatch.alreadyProcessed():
     657
     658        detectionBatch.createEmptyPspsTables()
     659        detectionBatch.importIppTables(".*.psf")
     660        if detectionBatch.populatePspsTables():
     661            detectionBatch.exportPspsTablesToFits("([a-zA-Z]+)")
     662            detectionBatch.writeBatchManifest()
     663            #detectionBatch.reportNullsInAllPspsTables(False)
     664            #detectionBatch.createTarball()
     665            #detectionBatch.publishToDatastore()
    149666   
    150 detectionBatch = DetectionBatch()
    151 detectionBatch.createEmptyPspsTables()
    152 detectionBatch.importIppTables("XY33\.psf")
    153 detectionBatch.populatePspsTables()
    154 detectionBatch.exportPspsTablesToFits()
     667            i = i+1
     668           # if i > 0: sys.exit()
     669
Note: See TracChangeset for help on using the changeset viewer.