- Timestamp:
- Mar 5, 2012, 5:19:48 PM (14 years ago)
- Location:
- branches/meh_branches/ppstack_test
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
ippToPsps/jython/detectionbatch.py (modified) (27 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/meh_branches/ppstack_test
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/ippToPsps/jython/detectionbatch.py
r32106 r33415 22 22 ''' 23 23 DetectionBatch class 24 25 This class, a sub-class of Batch, processes single exposures in the form of IPP smf files 26 24 27 ''' 25 28 class DetectionBatch(Batch): … … 30 33 def __init__(self, 31 34 logger, 32 configPath, 33 configDoc, 35 config, 34 36 gpc1Db, 35 37 ippToPspsDb, 36 camID): 38 scratchDb, 39 camID, 40 batchID, 41 useFullTables): 37 42 38 43 super(DetectionBatch, self).__init__( 39 44 logger, 40 configPath, 41 configDoc, 45 config, 42 46 gpc1Db, 43 47 ippToPspsDb, 48 scratchDb, 44 49 camID, 50 batchID, 45 51 "P2", 46 gpc1Db.getCameraStageSmf(camID)) 52 gpc1Db.getCameraStageSmf(camID), 53 useFullTables) 47 54 48 55 if not self.everythingOK: return … … 50 57 # get camera meta data 51 58 meta = self.gpc1Db.getCameraStageMeta(self.id) 59 if not meta: 60 self.everythingOK = False 61 return 62 52 63 self.expID = meta[0]; 53 64 self.expName = meta[1]; … … 64 75 65 76 # if test mode 66 if self. testMode:77 if self.config.test: 67 78 self.startX = 3 68 79 self.endX = 4 … … 101 112 102 113 self.filter = self.header['FILTERID'][0:1] 114 self.filterID = self.scratchDb.getFilterID(self.filter) 103 115 104 116 # insert what we know about this stack batch into the stack table … … 209 221 self.scratchDb.updateFilterID("FrameMeta", self.filter) 210 222 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)) 212 224 213 225 ''' … … 312 324 ," + self.safeDictionaryAccess(header, 'DAPMIFIT') + " \ 313 325 ,'" + self.safeDictionaryAccess(header, 'DETECTOR') + "' \ 314 ," + str(self.scratchDb.getDvoImageFlags(header[' SOURCEID'], header['IMAGEID'])) + " \326 ," + str(self.scratchDb.getDvoImageFlags(header['IMAGEID'])) + " \ 315 327 ,'" + self.safeDictionaryAccess(header, 'DETREND.MASK') + "' \ 316 328 ,'" + self.safeDictionaryAccess(header, 'DETREND.DARK') + "' \ … … 356 368 self.scratchDb.updateFilterID(tableName, self.filter) 357 369 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)) 359 371 if 'NASTRO' in header: self.totalNumPhotoRef = self.totalNumPhotoRef + int(header['NASTRO']) 360 372 self.scratchDb.replaceNullsInThisColumn(tableName, "polyOrder", "0") … … 367 379 pspsTableName = "Detection_" + ota 368 380 ippTableName = ota + "_psf" 369 381 382 results['ORIGINALTOTAL'] = self.scratchDb.getRowCount(ippTableName) 383 370 384 # drop then re-create table 371 385 self.scratchDb.dropTable(pspsTableName) … … 385 399 sql = "INSERT IGNORE INTO " + pspsTableName + " ( \ 386 400 ippDetectID \ 401 ,filterID \ 402 ,surveyID \ 403 ,obsTime \ 387 404 ,xPos \ 388 405 ,yPos \ … … 395 412 ,psfWidMinor \ 396 413 ,psfTheta \ 414 ,psfLikelihood \ 397 415 ,psfCf \ 398 416 ,momentXX \ … … 400 418 ,momentYY \ 401 419 ,apMag \ 402 ,kronFlux \403 ,kronFluxErr \404 420 ,infoFlag \ 405 421 ,sky \ 406 422 ,skyErr \ 407 423 ,sgSep \ 424 ,activeFlag \ 425 ,assocDate \ 426 ,historyModNum \ 427 ,dataRelease \ 408 428 ) \ 409 429 SELECT \ 410 430 IPP_IDET \ 431 , " + str(self.filterID) + "\ 432 , " + str(self.surveyID) + " \ 433 ," + str(self.obsTime) + " \ 411 434 ,X_PSF \ 412 435 ,Y_PSF \ … … 419 442 ,PSF_MINOR \ 420 443 ,PSF_THETA \ 444 ,psfLikelihood(EXT_NSIGMA) \ 421 445 ,PSF_QF \ 422 446 ,MOMENTS_XX \ … … 424 448 ,MOMENTS_YY \ 425 449 ,AP_MAG \ 426 ,KRON_FLUX \427 ,KRON_FLUX_ERR \428 450 ,FLAGS\ 429 451 ,SKY \ 430 452 ,SKY_SIGMA \ 431 453 ,EXT_NSIGMA \ 454 , 0 \ 455 , '" + self.dateStr + "' \ 456 , 0 \ 457 , " + str(self.config.dataRelease) + "\ 432 458 FROM " + ippTableName 433 459 self.scratchDb.execute(sql) 434 460 435 # set obsTime436 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 444 461 # 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 445 472 results['NULLINSTFLUX'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "instFlux") 446 473 results['NULLPEAKADU'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "peakADU") … … 472 499 self.scratchDb.execute(sql) 473 500 474 self.scratchDb.updateAllRows(tableName, "dataRelease", str(self. dataRelease))501 self.scratchDb.updateAllRows(tableName, "dataRelease", str(self.config.dataRelease)) 475 502 476 503 ''' … … 501 528 502 529 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)) 504 531 505 532 … … 514 541 self.scratchDb.makeColumnPrimaryKey("Detection", "ippDetectID") 515 542 516 '''517 Applies indexes to the IPP tables518 '''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 corners527 if x==0 and y==0: continue528 if x==0 and y==7: continue529 if x==7 and y==0: continue530 if x==7 and y==7: continue531 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 table537 '''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 likelihoods555 '''556 def populateLikelihoods(self, tableName):557 558 sql = "SELECT ippDetectID, psfLikelihood, crLikelihood, extendedLikelihood, sgSep FROM " + tableName559 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 = 0570 extendedLikelihood = 1.0 - psfLikelihood571 else:572 crLikelihood = 1.0 - psfLikelihood573 extendedLikelihood = 0574 575 # update columns576 rs.updateDouble(2, psfLikelihood )577 rs.updateDouble(3, crLikelihood )578 rs.updateDouble(4, extendedLikelihood )579 580 # now 'commit' to database581 rs.updateRow();582 583 584 '''585 Does the processing, i.e. pulling stuff from IPP tables into PSPS tables586 '''587 def populatePspsTables(self):588 589 543 self.populateFrameMeta() 590 544 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 = {} 594 547 595 548 # loop through all OTAs and populate ImageMeta extensions … … 614 567 615 568 # 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 620 571 621 572 # store sourceID/imageID combo in Db so DVO can look up later … … 624 575 625 576 # store these for later 626 sourceIDs[ota] = header['SOURCEID'] 627 imageIDs[ota] = header['IMAGEID'] 577 self.imageIDs[ota] = header['IMAGEID'] 628 578 629 579 # populate ImageMeta … … 632 582 633 583 # 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): 635 631 636 632 # loop through all OTAs again to update with DVO IDs … … 640 636 otaCount = 0 641 637 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("+-------+---------------+---------------+---------------+---------------+---------------+---------------+") 645 642 for x in range(self.startX, self.endX): 646 643 for y in range(self.startY, self.endY): … … 653 650 654 651 ota = "XY%d%d" % (x, y) 655 if ota not in s ourceIDs: continue652 if ota not in self.imageIDs: continue 656 653 657 654 #self.logger.infoTitle("Processing " + ota) 655 if not self.scratchDb.astrometricSolutionOK(ota): 656 self.logger.info("| %5s | ------------- Bad astrometric solution : rejecting ------------ |" % ota) 657 continue 658 658 659 659 # populate remainder of tables … … 661 661 662 662 # now add DVO IDs 663 self.updateDvoIDs("Detection_" + ota, s ourceIDs[ota],imageIDs[ota])663 self.updateDvoIDs("Detection_" + ota, self.imageIDs[ota]) 664 664 results['NULLOBJID'] = self.scratchDb.reportAndDeleteRowsWithNULLS("Detection_" + ota, "objID") 665 665 self.updateImageID("Detection_" + ota, x, y) 666 self.populateLikelihoods("Detection_" + ota)667 666 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 |", 669 668 ota, 669 results['ORIGINALTOTAL'], 670 670 results['SATDET'], 671 671 results['NULLINSTFLUX'], … … 674 674 rowCount) 675 675 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 676 683 # check we have something in this Detection table TODO add this to table above 677 684 if rowCount < 1: … … 681 688 # update ImageMeta with count of detections for this OTA and photoCodeID 682 689 sql = "UPDATE ImageMeta_" + ota + " \ 683 SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(s ourceIDs[ota],imageIDs[ota]))690 SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(self.imageIDs[ota])) 684 691 self.scratchDb.execute(sql) 685 692 … … 696 703 otaCount = otaCount + 1 697 704 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("+-------+---------------+---------------+---------------+---------------+---------------+---------------+") 700 715 701 716 # if we only have one table export, i.e. FrameMeta, then get out of here … … 737 752 def importIppTables(self, filter=""): 738 753 739 if self. testMode: regex = "XY33.psf"754 if self.config.test: regex = "XY33.psf" 740 755 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) 743 760 744 761
Note:
See TracChangeset
for help on using the changeset viewer.
