- Timestamp:
- Jan 11, 2012, 11:19:21 PM (15 years ago)
- Location:
- branches/meh_branches/ppsub_test
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
ippToPsps/jython/detectionbatch.py (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/meh_branches/ppsub_test
- Property svn:mergeinfo changed
-
branches/meh_branches/ppsub_test/ippToPsps/jython/detectionbatch.py
r32106 r33098 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): … … 34 37 gpc1Db, 35 38 ippToPspsDb, 36 camID): 39 camID, 40 batchID): 37 41 38 42 super(DetectionBatch, self).__init__( … … 43 47 ippToPspsDb, 44 48 camID, 49 batchID, 45 50 "P2", 46 51 gpc1Db.getCameraStageSmf(camID)) … … 50 55 # get camera meta data 51 56 meta = self.gpc1Db.getCameraStageMeta(self.id) 57 if not meta: 58 self.everythingOK = False 59 return 60 52 61 self.expID = meta[0]; 53 62 self.expName = meta[1]; … … 101 110 102 111 self.filter = self.header['FILTERID'][0:1] 112 self.filterID = self.scratchDb.getFilterID(self.filter) 103 113 104 114 # insert what we know about this stack batch into the stack table … … 367 377 pspsTableName = "Detection_" + ota 368 378 ippTableName = ota + "_psf" 369 379 380 results['ORIGINALTOTAL'] = self.scratchDb.getRowCount(ippTableName) 381 370 382 # drop then re-create table 371 383 self.scratchDb.dropTable(pspsTableName) … … 385 397 sql = "INSERT IGNORE INTO " + pspsTableName + " ( \ 386 398 ippDetectID \ 399 ,filterID \ 400 ,surveyID \ 401 ,obsTime \ 387 402 ,xPos \ 388 403 ,yPos \ … … 395 410 ,psfWidMinor \ 396 411 ,psfTheta \ 412 ,psfLikelihood \ 397 413 ,psfCf \ 398 414 ,momentXX \ … … 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 \ … … 430 454 ,SKY_SIGMA \ 431 455 ,EXT_NSIGMA \ 456 , 0 \ 457 , '" + self.dateStr + "' \ 458 , 0 \ 459 , " + str(self.dataRelease) + "\ 432 460 FROM " + ippTableName 433 461 self.scratchDb.execute(sql) 434 462 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 463 # 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 445 474 results['NULLINSTFLUX'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "instFlux") 446 475 results['NULLPEAKADU'] = self.scratchDb.reportAndDeleteRowsWithNULLS(pspsTableName, "peakADU") … … 514 543 self.scratchDb.makeColumnPrimaryKey("Detection", "ippDetectID") 515 544 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 545 self.populateFrameMeta() 590 546 591 547 # dictionary objects to hold sourceIDs and imageIDs for later 592 s ourceIDs = {}593 imageIDs = {}548 self.sourceIDs = {} 549 self.imageIDs = {} 594 550 595 551 # loop through all OTAs and populate ImageMeta extensions … … 614 570 615 571 # 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 620 574 621 575 # store sourceID/imageID combo in Db so DVO can look up later … … 624 578 625 579 # store these for later 626 s ourceIDs[ota] = header['SOURCEID']627 imageIDs[ota] = header['IMAGEID']580 self.sourceIDs[ota] = header['SOURCEID'] 581 self.imageIDs[ota] = header['IMAGEID'] 628 582 629 583 # populate ImageMeta … … 632 586 633 587 # 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): 635 636 636 637 # loop through all OTAs again to update with DVO IDs … … 640 641 otaCount = 0 641 642 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("+-------+---------------+---------------+---------------+---------------+---------------+---------------+") 645 647 for x in range(self.startX, self.endX): 646 648 for y in range(self.startY, self.endY): … … 653 655 654 656 ota = "XY%d%d" % (x, y) 655 if ota not in s ourceIDs: continue657 if ota not in self.sourceIDs: continue 656 658 657 659 #self.logger.infoTitle("Processing " + ota) 660 if not self.scratchDb.astrometricSolutionOK(ota): 661 self.logger.info("| %5s | ------------- Bad astrometric solution : rejecting ------------ |" % ota) 662 continue 658 663 659 664 # populate remainder of tables … … 661 666 662 667 # now add DVO IDs 663 self.updateDvoIDs("Detection_" + ota, s ourceIDs[ota],imageIDs[ota])668 self.updateDvoIDs("Detection_" + ota, self.sourceIDs[ota], self.imageIDs[ota]) 664 669 results['NULLOBJID'] = self.scratchDb.reportAndDeleteRowsWithNULLS("Detection_" + ota, "objID") 665 670 self.updateImageID("Detection_" + ota, x, y) 666 self.populateLikelihoods("Detection_" + ota)667 671 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 |", 669 673 ota, 674 results['ORIGINALTOTAL'], 670 675 results['SATDET'], 671 676 results['NULLINSTFLUX'], … … 674 679 rowCount) 675 680 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 676 688 # check we have something in this Detection table TODO add this to table above 677 689 if rowCount < 1: … … 681 693 # update ImageMeta with count of detections for this OTA and photoCodeID 682 694 sql = "UPDATE ImageMeta_" + ota + " \ 683 SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(s ourceIDs[ota],imageIDs[ota]))695 SET nDetect = %d, photoCalID = %d" % (self.scratchDb.getRowCount("Detection_" + ota), self.scratchDb.getPhotoCalID(self.sourceIDs[ota], self.imageIDs[ota])) 684 696 self.scratchDb.execute(sql) 685 697 … … 696 708 otaCount = otaCount + 1 697 709 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("+-------+---------------+---------------+---------------+---------------+---------------+---------------+") 700 720 701 721 # if we only have one table export, i.e. FrameMeta, then get out of here … … 739 759 if self.testMode: regex = "XY33.psf" 740 760 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) 743 765 744 766
Note:
See TracChangeset
for help on using the changeset viewer.
