IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 5, 2012, 5:19:48 PM (14 years ago)
Author:
mhuber
Message:

merging latest r33407 trunk changes to meh_branches/ppstack_test

Location:
branches/meh_branches/ppstack_test
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/meh_branches/ppstack_test

  • branches/meh_branches/ppstack_test/ippToPsps/jython/detectionbatch.py

    r32106 r33415  
    2222'''
    2323DetectionBatch class
     24
     25This class, a sub-class of Batch, processes single exposures in the form of IPP smf files
     26
    2427'''
    2528class DetectionBatch(Batch):
     
    3033    def __init__(self,
    3134                 logger,
    32                  configPath,
    33                  configDoc,
     35                 config,
    3436                 gpc1Db,
    3537                 ippToPspsDb,
    36                  camID):
     38                 scratchDb,
     39                 camID,
     40                 batchID,
     41                 useFullTables):
    3742
    3843       super(DetectionBatch, self).__init__(
    3944               logger,
    40                configPath,
    41                configDoc,
     45               config,
    4246               gpc1Db,
    4347               ippToPspsDb,
     48               scratchDb,
    4449               camID,
     50               batchID,
    4551               "P2",
    46                gpc1Db.getCameraStageSmf(camID))
     52               gpc1Db.getCameraStageSmf(camID),
     53               useFullTables)
    4754
    4855       if not self.everythingOK: return
     
    5057       # get camera meta data
    5158       meta = self.gpc1Db.getCameraStageMeta(self.id)
     59       if not meta:
     60           self.everythingOK = False
     61           return
     62
    5263       self.expID = meta[0];
    5364       self.expName = meta[1];
     
    6475
    6576       # if test mode
    66        if self.testMode:
     77       if self.config.test:
    6778           self.startX = 3
    6879           self.endX = 4
     
    101112
    102113       self.filter = self.header['FILTERID'][0:1]
     114       self.filterID = self.scratchDb.getFilterID(self.filter)
    103115
    104116       # insert what we know about this stack batch into the stack table
     
    209221        self.scratchDb.updateFilterID("FrameMeta", self.filter)
    210222        self.scratchDb.updateAllRows("FrameMeta", "calibModNum", str(self.calibModNum))
    211         self.scratchDb.updateAllRows("FrameMeta", "dataRelease", str(self.dataRelease))
     223        self.scratchDb.updateAllRows("FrameMeta", "dataRelease", str(self.config.dataRelease))
    212224
    213225    '''
     
    312324               ," + self.safeDictionaryAccess(header, 'DAPMIFIT') + " \
    313325               ,'" + self.safeDictionaryAccess(header, 'DETECTOR') + "' \
    314                ," + str(self.scratchDb.getDvoImageFlags(header['SOURCEID'], header['IMAGEID'])) + " \
     326               ," + str(self.scratchDb.getDvoImageFlags(header['IMAGEID'])) + " \
    315327               ,'" + self.safeDictionaryAccess(header, 'DETREND.MASK') + "' \
    316328               ,'" + self.safeDictionaryAccess(header, 'DETREND.DARK') + "' \
     
    356368        self.scratchDb.updateFilterID(tableName, self.filter)
    357369        self.scratchDb.updateAllRows(tableName, "calibModNum", str(self.calibModNum))
    358         self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.dataRelease))
     370        self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.config.dataRelease))
    359371        if 'NASTRO' in header: self.totalNumPhotoRef = self.totalNumPhotoRef + int(header['NASTRO'])
    360372        self.scratchDb.replaceNullsInThisColumn(tableName, "polyOrder", "0")
     
    367379        pspsTableName = "Detection_" + ota
    368380        ippTableName = ota + "_psf"
    369        
     381       
     382        results['ORIGINALTOTAL'] = self.scratchDb.getRowCount(ippTableName)
     383
    370384        # drop then re-create table
    371385        self.scratchDb.dropTable(pspsTableName)
     
    385399        sql = "INSERT IGNORE INTO " + pspsTableName + " ( \
    386400               ippDetectID \
     401               ,filterID \
     402               ,surveyID \
     403               ,obsTime \
    387404               ,xPos \
    388405               ,yPos \
     
    395412               ,psfWidMinor \
    396413               ,psfTheta \
     414               ,psfLikelihood \
    397415               ,psfCf \
    398416               ,momentXX \
     
    400418               ,momentYY \
    401419               ,apMag \
    402                ,kronFlux \
    403                ,kronFluxErr \
    404420               ,infoFlag \
    405421               ,sky \
    406422               ,skyErr \
    407423               ,sgSep \
     424               ,activeFlag \
     425               ,assocDate \
     426               ,historyModNum \
     427               ,dataRelease \
    408428               ) \
    409429               SELECT \
    410430               IPP_IDET \
     431               , " + str(self.filterID) + "\
     432               , " + str(self.surveyID) + " \
     433               ," + str(self.obsTime) + " \
    411434               ,X_PSF \
    412435               ,Y_PSF \
     
    419442               ,PSF_MINOR \
    420443               ,PSF_THETA \
     444               ,psfLikelihood(EXT_NSIGMA) \
    421445               ,PSF_QF \
    422446               ,MOMENTS_XX \
     
    424448               ,MOMENTS_YY \
    425449               ,AP_MAG \
    426                ,KRON_FLUX \
    427                ,KRON_FLUX_ERR \
    428450               ,FLAGS\
    429451               ,SKY \
    430452               ,SKY_SIGMA \
    431453               ,EXT_NSIGMA \
     454               , 0 \
     455               , '" + self.dateStr + "' \
     456               , 0 \
     457               , " + str(self.config.dataRelease) + "\
    432458               FROM " + ippTableName
    433459        self.scratchDb.execute(sql)
    434460
    435         # set obsTime
    436         sql = "UPDATE " + pspsTableName + " SET obsTime = %f, assocDate = '%s', activeFlag = 0" % (self.obsTime, self.dateStr)
    437         self.scratchDb.execute(sql)
    438         self.scratchDb.updateAllRows(pspsTableName, "dataRelease", str(self.dataRelease))
    439         self.scratchDb.updateAllRows(pspsTableName, "historyModNum", "0")
    440 
    441         self.scratchDb.updateAllRows(pspsTableName, "surveyID", str(self.surveyID))
    442         self.scratchDb.updateFilterID(pspsTableName, self.filter)
    443 
    444461        # now delete bad flux and bad chip positions
     462        sql="DELETE FROM " + pspsTableName + " WHERE instFlux < 0.0000001" # TODO clearly a hack = 0 not allowed for instFlux
     463        self.scratchDb.execute(sql)
     464
     465        # update cosmic ray and extended likelihoods
     466        sql="UPDATE " + pspsTableName + " SET extendedLikelihood = 0, crLikelihood = 1.0 - psfLikelihood WHERE sgSep <= 0"
     467        self.scratchDb.execute(sql)
     468        sql="UPDATE " + pspsTableName + " SET crLikelihood = 0, extendedLikelihood = 1.0 - psfLikelihood WHERE sgSep > 0"
     469        self.scratchDb.execute(sql)
     470
     471        # remove detections will NULL inst flux or NULL peak ADU
    445472        results['NULLINSTFLUX'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "instFlux")
    446473        results['NULLPEAKADU'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "peakADU")
     
    472499        self.scratchDb.execute(sql)
    473500
    474         self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.dataRelease))
     501        self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.config.dataRelease))
    475502
    476503    '''
     
    501528
    502529        self.scratchDb.updateAllRows(tableName, "calibModNum", str(self.calibModNum))
    503         self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.dataRelease))
     530        self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.config.dataRelease))
    504531
    505532
     
    514541        self.scratchDb.makeColumnPrimaryKey("Detection", "ippDetectID")
    515542
    516     '''
    517     Applies indexes to the IPP tables
    518     '''
    519     def indexIppTables(self):
    520 
    521         self.logger.infoPair("Creating indexes on", "IPP tables")
    522 
    523         for x in range(self.startX, self.endX):
    524             for y in range(self.startY, self.endY):
    525 
    526                 # dodge the corners
    527                 if x==0 and y==0: continue
    528                 if x==0 and y==7: continue
    529                 if x==7 and y==0: continue
    530                 if x==7 and y==7: continue
    531 
    532                 extension = "XY%d%d_psf" % (x, y)
    533                 self.scratchDb.createIndex(extension, "IPP_IDET")
    534 
    535     '''
    536     Updates provided table with DVO IDs from DVO table
    537     '''
    538     def updateDvoIDs(self, table, sourceID, externID):
    539 
    540         imageID = self.scratchDb.getImageIDFromExternID(sourceID, externID)
    541         self.logger.debug("Updating table '" + table + "' with DVO IDs using imageID = %d" % imageID)
    542         sql = "UPDATE IGNORE " + table + " AS a, " + self.scratchDb.dvoDetectionTable + " AS b SET \
    543                a.ippObjID = b.ippObjID, \
    544                a.detectID = b.detectID, \
    545                a.objID = b.objID, \
    546                a.infoFlag = b.flags << 32 | a.infoFlag \
    547                WHERE a.ippDetectID = b.ippDetectID \
    548                AND b.sourceID = " + str(sourceID) + " \
    549                AND b.imageID = " + str(imageID)
    550 
    551         self.scratchDb.execute(sql)
    552 
    553     '''
    554     Generates psf, cosmic ray and extended source likelihoods
    555     '''
    556     def populateLikelihoods(self, tableName):
    557 
    558         sql = "SELECT ippDetectID, psfLikelihood, crLikelihood, extendedLikelihood, sgSep FROM " + tableName
    559         rs = self.scratchDb.executeUpdatableQuery(sql)
    560 
    561         sqrt2 = Math.sqrt(2)
    562 
    563         while rs.next():
    564 
    565             sgSep = rs.getDouble(5)
    566             psfLikelihood = Erf.erfc(Math.abs(sgSep)/sqrt2)
    567        
    568             if sgSep > 0:
    569                 crLikelihood = 0
    570                 extendedLikelihood = 1.0 - psfLikelihood
    571             else:
    572                 crLikelihood = 1.0 - psfLikelihood
    573                 extendedLikelihood = 0
    574 
    575             # update columns
    576             rs.updateDouble(2, psfLikelihood )
    577             rs.updateDouble(3, crLikelihood )
    578             rs.updateDouble(4, extendedLikelihood )
    579 
    580             # now 'commit' to database
    581             rs.updateRow();
    582 
    583 
    584     '''
    585     Does the processing, i.e. pulling stuff from IPP tables into PSPS tables
    586     '''
    587     def populatePspsTables(self):
    588 
    589543        self.populateFrameMeta()
    590544     
    591         # dictionary objects to hold sourceIDs and imageIDs for later
    592         sourceIDs = {}
    593         imageIDs = {}
     545        # dictionary objects to hold imageIDs for later
     546        self.imageIDs = {}
    594547
    595548        # loop through all OTAs and populate ImageMeta extensions
     
    614567
    615568                # check we have valid sourceID/imageID pair from the header
    616                 if self.safeDictionaryAccess(header, 'SOURCEID') == "NULL":
    617                     continue
    618                 if self.safeDictionaryAccess(header, 'IMAGEID') == "NULL":
    619                     continue
     569                if self.safeDictionaryAccess(header, 'SOURCEID') == "NULL": continue
     570                if self.safeDictionaryAccess(header, 'IMAGEID') == "NULL": continue
    620571
    621572                # store sourceID/imageID combo in Db so DVO can look up later
     
    624575
    625576                # store these for later
    626                 sourceIDs[ota] = header['SOURCEID']
    627                 imageIDs[ota] = header['IMAGEID']
     577                self.imageIDs[ota] = header['IMAGEID']
    628578
    629579                # populate ImageMeta
     
    632582             
    633583        # now run DVO code to get all IDs
    634         if not self.useFullTables: self.getIDsFromDVO()
     584        if not self.useFullTables:
     585            if not self.getIDsFromDVO(): return False
     586
     587        return True
     588
     589
     590    '''
     591    Applies indexes to the IPP tables
     592    '''
     593    def indexIppTables(self):
     594
     595        self.logger.infoPair("Creating indexes on", "IPP tables")
     596
     597        for x in range(self.startX, self.endX):
     598            for y in range(self.startY, self.endY):
     599
     600                # dodge the corners
     601                if x==0 and y==0: continue
     602                if x==0 and y==7: continue
     603                if x==7 and y==0: continue
     604                if x==7 and y==7: continue
     605
     606                extension = "XY%d%d_psf" % (x, y)
     607                self.scratchDb.createIndex(extension, "IPP_IDET")
     608
     609    '''
     610    Updates provided table with DVO IDs from DVO table
     611    '''
     612    def updateDvoIDs(self, table, externID):
     613
     614        imageID = self.scratchDb.getImageIDFromExternID(externID)
     615        self.logger.debug("Updating table '" + table + "' with DVO IDs using imageID = %d" % imageID)
     616        sql = "UPDATE IGNORE " + table + " AS a, " + self.scratchDb.dvoDetectionTable + " AS b SET \
     617               a.ippObjID = b.ippObjID, \
     618               a.detectID = b.detectID, \
     619               a.objID = b.objID, \
     620               a.infoFlag = b.flags << 32 | a.infoFlag \
     621               WHERE a.ippDetectID = b.ippDetectID \
     622               AND b.imageID = " + str(imageID)
     623
     624        self.scratchDb.execute(sql)
     625
     626
     627    '''
     628    Does the processing, i.e. pulling stuff from IPP tables into PSPS tables
     629    '''
     630    def populatePspsTables(self):
    635631
    636632        # loop through all OTAs again to update with DVO IDs
     
    640636        otaCount = 0
    641637        results = {}
    642         self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+")
    643         self.logger.info("|  OTA  |   Sat Det     | NULL instFlux | NULL peak ADU | NULL obj ID   |  Remainder    |")
    644         self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+")
     638        totalOriginal = totalSatDet = totalNulIInstFlux = totalNullPeakFlux = totalNullObjID = totalDetections = 0
     639        self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+---------------+")
     640        self.logger.info("|  OTA  | Initial total |   Sat Det     | NULL instFlux | NULL peak ADU | NULL obj ID   |  Remainder    |")
     641        self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+---------------+")
    645642        for x in range(self.startX, self.endX):
    646643            for y in range(self.startY, self.endY):
     
    653650
    654651                ota = "XY%d%d" % (x, y)
    655                 if ota not in sourceIDs: continue
     652                if ota not in self.imageIDs: continue
    656653
    657654                #self.logger.infoTitle("Processing " + ota)
     655                if not self.scratchDb.astrometricSolutionOK(ota):
     656                    self.logger.info("| %5s |            ------------- Bad astrometric solution : rejecting ------------                    |" % ota)
     657                    continue
    658658
    659659                # populate remainder of tables
     
    661661
    662662                # now add DVO IDs
    663                 self.updateDvoIDs("Detection_" + ota, sourceIDs[ota], imageIDs[ota])
     663                self.updateDvoIDs("Detection_" + ota, self.imageIDs[ota])
    664664                results['NULLOBJID'] = self.scratchDb.reportAndDeleteRowsWithNULLS("Detection_" + ota, "objID")
    665665                self.updateImageID("Detection_" + ota, x, y)
    666                 self.populateLikelihoods("Detection_" + ota)
    667666                rowCount = self.scratchDb.getRowCount("Detection_" + ota)
    668                 self.logger.info("| %5s | %13d | %13d | %13d | %13d | %13d |",
     667                self.logger.info("| %5s | %13d | %13d | %13d | %13d | %13d | %13d |",
    669668                        ota,
     669                        results['ORIGINALTOTAL'],
    670670                        results['SATDET'],
    671671                        results['NULLINSTFLUX'],
     
    674674                        rowCount)
    675675
     676                totalOriginal = totalOriginal + results['ORIGINALTOTAL']
     677                totalSatDet = totalSatDet + results['SATDET']
     678                totalNulIInstFlux = totalNulIInstFlux + results['NULLINSTFLUX']
     679                totalNullPeakFlux = totalNullPeakFlux + results['NULLPEAKADU']
     680                totalNullObjID = totalNullObjID + results['NULLOBJID']
     681                totalDetections = totalDetections + rowCount
     682
    676683                # check we have something in this Detection table TODO add this to table above
    677684                if rowCount < 1:
     
    681688                # update ImageMeta with count of detections for this OTA and photoCodeID
    682689                sql = "UPDATE ImageMeta_" + ota + " \
    683                        SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(sourceIDs[ota], imageIDs[ota]))
     690                       SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(self.imageIDs[ota]))
    684691                self.scratchDb.execute(sql)
    685692
     
    696703                otaCount = otaCount + 1
    697704
    698         # TODO print totals here
    699         self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+")
     705        # print totals
     706        self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+---------------+")
     707        self.logger.info("| Total | %13d | %13d | %13d | %13d | %13d | %13d |",
     708                totalOriginal,
     709                totalSatDet,
     710                totalNulIInstFlux,
     711                totalNullPeakFlux,
     712                totalNullObjID,
     713                totalDetections)
     714        self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+---------------+")
    700715
    701716        # if we only have one table export, i.e. FrameMeta, then get out of here
     
    737752    def importIppTables(self, filter=""):
    738753
    739        if self.testMode: regex = "XY33.psf"
     754       if self.config.test: regex = "XY33.psf"
    740755       else : regex = ".*.psf"
    741 
    742        return super(DetectionBatch, self).importIppTables(regex)
     756 
     757       columns = "IPP_IDET X_PSF Y_PSF X_PSF_SIG Y_PSF_SIG PSF_INST_MAG PSF_INST_MAG_SIG PEAK_FLUX_AS_MAG PSF_MAJOR PSF_MINOR PSF_THETA EXT_NSIGMA PSF_QF MOMENTS_XX MOMENTS_XY MOMENTS_YY AP_MAG FLAGS SKY SKY_SIGMA EXT_NSIGMA"
     758
     759       return super(DetectionBatch, self).importIppTables(columns, regex)
    743760
    744761
Note: See TracChangeset for help on using the changeset viewer.