Changeset 31840 for trunk/ippToPsps/jython/batch.py
- Timestamp:
- Jul 8, 2011, 2:48:02 PM (15 years ago)
- File:
-
- 1 edited
-
trunk/ippToPsps/jython/batch.py (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippToPsps/jython/batch.py
r31809 r31840 15 15 from gpc1db import Gpc1Db 16 16 from ipptopspsdb import IppToPspsDb 17 from fits import Fits 17 18 18 19 from java.lang import * … … 39 40 id, 40 41 batchType, 41 inputFitsPath="",42 fits, 42 43 survey=""): 43 44 … … 45 46 self.readHeader = False 46 47 self.doc = doc 48 self.fits = fits 49 self.header = self.fits.getPrimaryHeader() 47 50 48 51 # set up logging 49 52 self.logger = logger 50 self.logger.info ("-------------------------------------------------------------------------------")53 self.logger.infoSeparator() 51 54 self.logger.debug("Batch class constructor") 52 55 … … 57 60 self.batchType = batchType; 58 61 self.pspsVoTableFilePath = "../config/" + batchType + "/tables.vot" 59 self.inputFitsPath = inputFitsPath60 62 self.survey = survey 61 62 if self.alreadyProcessed(): return63 63 64 64 # get dvo info from config … … 66 66 self.dvoLocation = self.doc.find("dvo/location").text 67 67 self.useFullTables = int(self.doc.find("dvo/useFullTables").text) 68 self.scratchDb = ScratchDb(logger, self.useFullTables) 69 70 # do we have an input file? 71 if self.inputFitsPath != "": 72 73 if not self.readPrimaryHeader(): return 68 self.scratchDb = ScratchDb(logger, self.doc, self.useFullTables) 74 69 75 70 # TODO … … 124 119 def printMe(self): 125 120 126 self.logger.info("New batch: ") 127 self.logger.info("Batch type: " + self.batchType) 128 self.logger.info("Survey: " + self.survey) 129 self.logger.info("Survey ID: %d" % self.surveyID) 130 self.logger.info("Test mode? %d" % self.testMode) 131 self.logger.info("DVO location: " + self.dvoLocation) 132 self.logger.info("Use full DVO tables?: %d" % self.useFullTables) 133 self.logger.info("Input FITS file: " + self.inputFitsPath) 134 135 136 ''' 137 Reads the primary header of the FITS file 138 ''' 139 def readPrimaryHeader(self): 140 141 if self.readHeader: return True 142 143 # does it exist? 144 if not os.path.isfile(self.inputFitsPath): 145 146 self.logger.error("Cannot read file at '" + self.inputFitsPath + "'") 147 return False 148 149 file = open(self.inputFitsPath) 150 self.header = self.parseFitsHeader(file) 151 self.logger.info("Read primary header and found " + str(len(self.header)) + " header cards") 152 # TODO close file? 153 154 self.readHeader = True 155 156 return True 157 158 159 ''' 160 Returns the value from this dictinary or else NULL 121 self.logger.infoPair("Batch name", self.batchName) 122 self.logger.infoPair("Survey", self.survey) 123 self.logger.infoPair("Survey ID", "%d" % self.surveyID) 124 self.logger.infoPair("DVO location", self.dvoLocation) 125 self.logger.infoPair("Use full DVO tables?", "%d" % self.useFullTables) 126 self.logger.infoPair("Output path", self.localOutPath) 127 128 ''' 129 Returns the string value from this dictionary or else "NULL" 161 130 ''' 162 131 def safeDictionaryAccess(self, header, key): … … 166 135 167 136 ''' 168 Finds and reads a header extension169 '''170 def findAndReadFITSHeader(self, name, file):171 172 found = False173 174 origIndex = file.tell()175 while True:176 177 index = file.tell()178 179 record = file.read(80)180 if not record: break;181 182 # quit when we reach 'END'183 if record.startswith("XTENSION= 'IMAGE"):184 185 header = self.parseFitsHeader(file)186 if header['EXTNAME'] == name:187 found = True188 file.seek(index + 2880, 0)189 break190 191 file.seek(index + 2880, 0)192 193 if found != True:194 195 file.seek(origIndex, 0)196 self.logger.error("...could not read header in extension '" + name + "'")197 return198 #else: self.logger.info("...read header at '" + name + "' and found " + str(len(header)) + " header cards")199 200 return header201 202 203 '''204 137 Writes the batch manifest file 205 138 ''' … … 208 141 outPath = self.localOutPath + "/BatchManifest.xml" 209 142 tmpPath = "./tmp.xml" 210 self.logger.info ("Creating batch manifest file here: " +outPath)143 self.logger.infoPair("Creating manifest", outPath) 211 144 root = Element('manifest') 212 145 … … 299 232 300 233 ''' 301 Gets PSPS friendly batch type 234 Gets PSPS friendly batch type TODO only use PSPS batch names, P2, ST etc 302 235 ''' 303 236 def getPspsBatchType(self): … … 335 268 336 269 self.ippToPspsDb.updateMinMaxObjID(self.batchID, self.minObjID, self.maxObjID) 337 self.logger.info("Total detections = %ld min objID = %ld max objID = %ld" % (self.totalDetections, self.minObjID, self.maxObjID)) 338 339 340 ''' 341 Reads FITS header and stores all fields in a dictionary object 342 ''' 343 def parseFitsHeader(self, fitsFile): 344 345 header = {} 346 347 while (True): 348 349 record = fitsFile.read(80) 350 351 # quit when we reach 'END' 352 if re.match('END\s+', record): break 353 354 # this regex will get param/value pairs for all header cards, ignoring comments and parsing out 'HIERACH' prefixes 355 match = re.match('^(HIERARCH )*([a-zA-Z0-9-_\.]+)\s*=\s+\'*([a-zA-Z0-9-_\.:\s@#]+)\'*\\/*', record) 356 if match: 357 358 param = match.group(2) 359 value = match.group(3).strip() 360 if value == "NaN": value = "NULL" 361 header[param] = value 362 363 #print param + "|" + value + "|" 364 365 return header 270 self.logger.infoPair("Total detections", "%ld" % self.totalDetections) 271 self.logger.infoPair("Min objID", "%ld" % self.minObjID) 272 self.logger.infoPair("Max objID", "%ld" % self.maxObjID) 366 273 367 274 ''' … … 398 305 def importIppTables(self, filter=""): 399 306 400 self.logger.info ("Attempting to import tables from input FITS file with regex " +filter)401 tables = stilts.treads(self. inputFitsPath)307 self.logger.infoPair("Importing tables with filter", filter) 308 tables = stilts.treads(self.fits.getPath()) 402 309 403 310 count = 0 … … 406 313 match = re.match(filter, table.name) 407 314 if not match: continue 408 self.logger.info ("Reading IPP table " + table.name + " from FITS file")315 self.logger.infoPair("Reading IPP table", table.name) 409 316 table = stilts.tpipe(table, cmd='explodeall') 410 317 … … 425 332 426 333 427 self.logger.info ("Done. Imported%d tables" % count)334 self.logger.infoPair("Done. Imported", "%d tables" % count) 428 335 self.indexIppTables() 429 336 … … 433 340 def exportPspsTablesToFits(self, regex="(.*)"): 434 341 435 self.logger.info("Replacing NULLs with -999, changing tables names using regex: " + regex) 342 self.logger.infoPair("Replacing NULLs with", "-999") 343 self.logger.infoPair("Changing table names with regex", regex) 436 344 _tables = [] 437 345 438 self.logger. info("Selecting database tables for export")346 self.logger.debug("Selecting database tables for export") 439 347 for table in self.tablesToExport: 440 348 … … 460 368 _tables.append(_table) 461 369 462 self.logger.info ("Writing to FITS file '" + self.outputFitsPath + "'...")370 self.logger.infoPair("Writing to FITS", self.outputFitsPath) 463 371 try: 464 372 stilts.twrites(_tables, self.outputFitsPath, fmt='fits') … … 483 391 def replaceAllPspsNulls(self, sub): 484 392 485 self.logger.info ("Replacing all NULL values in PSPS tables with '" + sub + "'...")393 self.logger.infoPair("Replacing all NULL values with", sub) 486 394 for table in self.pspsTables: 487 395 self.scratchDb.replaceNulls(table.name, sub) 488 self.logger.info ("...done")396 self.logger.infoPair("NULL replacement", "done") 489 397 490 398 ''' … … 501 409 # TODO path to DVO prog hardcoded temporarily 502 410 cmd = "../src/dvograbber " + self.dvoLocation 503 self.logger.info ("Running: '" + cmd + "'...")411 self.logger.infoPair("Running DVO", cmd) 504 412 p = Popen(cmd, shell=True, stdout=PIPE) 505 413 p.wait() 506 414 # out = p.stdout.read() 507 self.logger.info ("...done")415 self.logger.infoPair("DVO access", "complete") 508 416 509 417 if self.scratchDb.getRowCount("dvoDetection") < 1: … … 517 425 ''' 518 426 def alreadyProcessed(self): 519 self.logger. info("Not implemented")427 self.logger.error("Not implemented") 520 428 521 429 … … 535 443 if int(self.doc.find("options/reportNulls").text): self.reportNullsInAllPspsTables(False) 536 444 537 self.logger.info ("Finished.")538 if self.testMode: sys.exit()539 540 445 self.logger.infoPair("Batch......", "complete") 446 self.logger.infoSeparator() 447 448
Note:
See TracChangeset
for help on using the changeset viewer.
