IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 11, 2012, 11:19:21 PM (15 years ago)
Author:
mhuber
Message:

merged latest r33093 trunk changes to meh_branches/ppsub_test

Location:
branches/meh_branches/ppsub_test
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/meh_branches/ppsub_test

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

    r32106 r33098  
    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):
     
    3437                 gpc1Db,
    3538                 ippToPspsDb,
    36                  camID):
     39                 camID,
     40                 batchID):
    3741
    3842       super(DetectionBatch, self).__init__(
     
    4347               ippToPspsDb,
    4448               camID,
     49               batchID,
    4550               "P2",
    4651               gpc1Db.getCameraStageSmf(camID))
     
    5055       # get camera meta data
    5156       meta = self.gpc1Db.getCameraStageMeta(self.id)
     57       if not meta:
     58           self.everythingOK = False
     59           return
     60
    5261       self.expID = meta[0];
    5362       self.expName = meta[1];
     
    101110
    102111       self.filter = self.header['FILTERID'][0:1]
     112       self.filterID = self.scratchDb.getFilterID(self.filter)
    103113
    104114       # insert what we know about this stack batch into the stack table
     
    367377        pspsTableName = "Detection_" + ota
    368378        ippTableName = ota + "_psf"
    369        
     379       
     380        results['ORIGINALTOTAL'] = self.scratchDb.getRowCount(ippTableName)
     381
    370382        # drop then re-create table
    371383        self.scratchDb.dropTable(pspsTableName)
     
    385397        sql = "INSERT IGNORE INTO " + pspsTableName + " ( \
    386398               ippDetectID \
     399               ,filterID \
     400               ,surveyID \
     401               ,obsTime \
    387402               ,xPos \
    388403               ,yPos \
     
    395410               ,psfWidMinor \
    396411               ,psfTheta \
     412               ,psfLikelihood \
    397413               ,psfCf \
    398414               ,momentXX \
     
    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 \
     
    430454               ,SKY_SIGMA \
    431455               ,EXT_NSIGMA \
     456               , 0 \
     457               , '" + self.dateStr + "' \
     458               , 0 \
     459               , " + str(self.dataRelease) + "\
    432460               FROM " + ippTableName
    433461        self.scratchDb.execute(sql)
    434462
    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 
    444463        # now delete bad flux and bad chip positions
     464        sql="DELETE FROM " + pspsTableName + " WHERE instFlux < 0.0000001" # TODO clearly a hack = 0 not allowed for instFlux
     465        self.scratchDb.execute(sql)
     466
     467        # update cosmic ray and extended likelihoods
     468        sql="UPDATE " + pspsTableName + " SET extendedLikelihood = 0, crLikelihood = 1.0 - psfLikelihood WHERE sgSep <= 0"
     469        self.scratchDb.execute(sql)
     470        sql="UPDATE " + pspsTableName + " SET crLikelihood = 0, extendedLikelihood = 1.0 - psfLikelihood WHERE sgSep > 0"
     471        self.scratchDb.execute(sql)
     472
     473        # remove detections will NULL inst flux or NULL peak ADU
    445474        results['NULLINSTFLUX'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "instFlux")
    446475        results['NULLPEAKADU'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "peakADU")
     
    514543        self.scratchDb.makeColumnPrimaryKey("Detection", "ippDetectID")
    515544
    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 
    589545        self.populateFrameMeta()
    590546     
    591547        # dictionary objects to hold sourceIDs and imageIDs for later
    592         sourceIDs = {}
    593         imageIDs = {}
     548        self.sourceIDs = {}
     549        self.imageIDs = {}
    594550
    595551        # loop through all OTAs and populate ImageMeta extensions
     
    614570
    615571                # 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
     572                if self.safeDictionaryAccess(header, 'SOURCEID') == "NULL": continue
     573                if self.safeDictionaryAccess(header, 'IMAGEID') == "NULL": continue
    620574
    621575                # store sourceID/imageID combo in Db so DVO can look up later
     
    624578
    625579                # store these for later
    626                 sourceIDs[ota] = header['SOURCEID']
    627                 imageIDs[ota] = header['IMAGEID']
     580                self.sourceIDs[ota] = header['SOURCEID']
     581                self.imageIDs[ota] = header['IMAGEID']
    628582
    629583                # populate ImageMeta
     
    632586             
    633587        # now run DVO code to get all IDs
    634         if not self.useFullTables: self.getIDsFromDVO()
     588        if not self.useFullTables:
     589            if not self.getIDsFromDVO(): return False
     590
     591        return True
     592
     593
     594    '''
     595    Applies indexes to the IPP tables
     596    '''
     597    def indexIppTables(self):
     598
     599        self.logger.infoPair("Creating indexes on", "IPP tables")
     600
     601        for x in range(self.startX, self.endX):
     602            for y in range(self.startY, self.endY):
     603
     604                # dodge the corners
     605                if x==0 and y==0: continue
     606                if x==0 and y==7: continue
     607                if x==7 and y==0: continue
     608                if x==7 and y==7: continue
     609
     610                extension = "XY%d%d_psf" % (x, y)
     611                self.scratchDb.createIndex(extension, "IPP_IDET")
     612
     613    '''
     614    Updates provided table with DVO IDs from DVO table
     615    '''
     616    def updateDvoIDs(self, table, sourceID, externID):
     617
     618        imageID = self.scratchDb.getImageIDFromExternID(sourceID, externID)
     619        self.logger.debug("Updating table '" + table + "' with DVO IDs using imageID = %d" % imageID)
     620        sql = "UPDATE IGNORE " + table + " AS a, " + self.scratchDb.dvoDetectionTable + " AS b SET \
     621               a.ippObjID = b.ippObjID, \
     622               a.detectID = b.detectID, \
     623               a.objID = b.objID, \
     624               a.infoFlag = b.flags << 32 | a.infoFlag \
     625               WHERE a.ippDetectID = b.ippDetectID \
     626               AND b.sourceID = " + str(sourceID) + " \
     627               AND b.imageID = " + str(imageID)
     628
     629        self.scratchDb.execute(sql)
     630
     631
     632    '''
     633    Does the processing, i.e. pulling stuff from IPP tables into PSPS tables
     634    '''
     635    def populatePspsTables(self):
    635636
    636637        # loop through all OTAs again to update with DVO IDs
     
    640641        otaCount = 0
    641642        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("+-------+---------------+---------------+---------------+---------------+---------------+")
     643        totalOriginal = totalSatDet = totalNulIInstFlux = totalNullPeakFlux = totalNullObjID = totalDetections = 0
     644        self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+---------------+")
     645        self.logger.info("|  OTA  | Initial total |   Sat Det     | NULL instFlux | NULL peak ADU | NULL obj ID   |  Remainder    |")
     646        self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+---------------+")
    645647        for x in range(self.startX, self.endX):
    646648            for y in range(self.startY, self.endY):
     
    653655
    654656                ota = "XY%d%d" % (x, y)
    655                 if ota not in sourceIDs: continue
     657                if ota not in self.sourceIDs: continue
    656658
    657659                #self.logger.infoTitle("Processing " + ota)
     660                if not self.scratchDb.astrometricSolutionOK(ota):
     661                    self.logger.info("| %5s |            ------------- Bad astrometric solution : rejecting ------------                    |" % ota)
     662                    continue
    658663
    659664                # populate remainder of tables
     
    661666
    662667                # now add DVO IDs
    663                 self.updateDvoIDs("Detection_" + ota, sourceIDs[ota], imageIDs[ota])
     668                self.updateDvoIDs("Detection_" + ota, self.sourceIDs[ota], self.imageIDs[ota])
    664669                results['NULLOBJID'] = self.scratchDb.reportAndDeleteRowsWithNULLS("Detection_" + ota, "objID")
    665670                self.updateImageID("Detection_" + ota, x, y)
    666                 self.populateLikelihoods("Detection_" + ota)
    667671                rowCount = self.scratchDb.getRowCount("Detection_" + ota)
    668                 self.logger.info("| %5s | %13d | %13d | %13d | %13d | %13d |",
     672                self.logger.info("| %5s | %13d | %13d | %13d | %13d | %13d | %13d |",
    669673                        ota,
     674                        results['ORIGINALTOTAL'],
    670675                        results['SATDET'],
    671676                        results['NULLINSTFLUX'],
     
    674679                        rowCount)
    675680
     681                totalOriginal = totalOriginal + results['ORIGINALTOTAL']
     682                totalSatDet = totalSatDet + results['SATDET']
     683                totalNulIInstFlux = totalNulIInstFlux + results['NULLINSTFLUX']
     684                totalNullPeakFlux = totalNullPeakFlux + results['NULLPEAKADU']
     685                totalNullObjID = totalNullObjID + results['NULLOBJID']
     686                totalDetections = totalDetections + rowCount
     687
    676688                # check we have something in this Detection table TODO add this to table above
    677689                if rowCount < 1:
     
    681693                # update ImageMeta with count of detections for this OTA and photoCodeID
    682694                sql = "UPDATE ImageMeta_" + ota + " \
    683                        SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(sourceIDs[ota], imageIDs[ota]))
     695                       SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(self.sourceIDs[ota], self.imageIDs[ota]))
    684696                self.scratchDb.execute(sql)
    685697
     
    696708                otaCount = otaCount + 1
    697709
    698         # TODO print totals here
    699         self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+")
     710        # print totals
     711        self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+---------------+")
     712        self.logger.info("| Total | %13d | %13d | %13d | %13d | %13d | %13d |",
     713                totalOriginal,
     714                totalSatDet,
     715                totalNulIInstFlux,
     716                totalNullPeakFlux,
     717                totalNullObjID,
     718                totalDetections)
     719        self.logger.info("+-------+---------------+---------------+---------------+---------------+---------------+---------------+")
    700720
    701721        # if we only have one table export, i.e. FrameMeta, then get out of here
     
    739759       if self.testMode: regex = "XY33.psf"
    740760       else : regex = ".*.psf"
    741 
    742        return super(DetectionBatch, self).importIppTables(regex)
     761 
     762       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 KRON_FLUX KRON_FLUX_ERR FLAGS SKY SKY_SIGMA EXT_NSIGMA"
     763
     764       return super(DetectionBatch, self).importIppTables(columns, regex)
    743765
    744766
Note: See TracChangeset for help on using the changeset viewer.