Changeset 31469
- Timestamp:
- May 6, 2011, 12:42:21 PM (15 years ago)
- File:
-
- 1 edited
-
trunk/ippToPsps/jython/batch.py (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippToPsps/jython/batch.py
r31405 r31469 32 32 "../config/2/tables.vot" 33 33 ''' 34 def __init__(self, logger, batchType, inputFitsPath="", survey="", useFullTables=False): 34 def __init__(self, 35 logger, 36 gpc1Db, 37 ippToPspsDb, 38 scratchDb, 39 id, 40 batchType, 41 inputFitsPath="", 42 survey="", 43 useFullTables=False): 44 45 self.everythingOK = False 46 self.readHeader = False 35 47 36 48 # set up logging … … 40 52 41 53 # set up class variables 54 self.id = id 55 self.gpc1Db = gpc1Db 56 self.ippToPspsDb = ippToPspsDb 57 self.scratchDb = scratchDb 42 58 self.batchType = batchType; 43 59 self.pspsVoTableFilePath = "../config/" + batchType + "/tables.vot" … … 46 62 self.useFullTables = useFullTables 47 63 64 if self.alreadyProcessed(): return 65 66 # do we have an input file? 67 if self.inputFitsPath != "": 68 69 if not self.readPrimaryHeader(): return 70 48 71 # TODO 49 72 self.tablesToExport = [] … … 51 74 # open config 52 75 doc = ElementTree(file="config.xml") 53 54 # create Gpc1Db object55 self.gpc1Db = Gpc1Db(self.logger)56 self.ippToPspsDb = IppToPspsDb(logger)57 self.scratchDb = ScratchDb(logger, self.useFullTables)58 76 59 77 if self.survey != "": … … 88 106 self.dateStr = now.strftime("%Y-%m-%d") 89 107 90 if self.inputFitsPath != "":91 file = open(self.inputFitsPath)92 self.header = self.parseFitsHeader(file)93 self.logger.info("Read primary and found " + str(len(self.header)) + " header cards")94 # TODO close file?95 96 108 # create DVO tables if accessing DVO directly 97 109 if not self.useFullTables: self.scratchDb.createDvoTables() 98 110 111 self.everythingOK = True 112 99 113 ''' 100 114 Destructor … … 103 117 104 118 self.logger.debug("Batch destructor") 119 120 121 ''' 122 Reads the primary header of the FITS file 123 ''' 124 def readPrimaryHeader(self): 125 126 if self.readHeader: return True 127 128 # does it exist? 129 if not os.path.isfile(self.inputFitsPath): 130 131 self.logger.error("Cannot read file at '" + self.inputFitsPath + "'") 132 return False 133 134 file = open(self.inputFitsPath) 135 self.header = self.parseFitsHeader(file) 136 self.logger.info("Read primary header and found " + str(len(self.header)) + " header cards") 137 # TODO close file? 138 139 self.readHeader = True 140 141 return True 105 142 106 143 … … 138 175 file.seek(index + 2880, 0) 139 176 140 if found != True: self.logger.error("...could not find extension '" + name + "'") 141 else: self.logger.info("...read header at '" + name + "' and found " + str(len(header)) + " header cards") 177 if found != True: 178 self.logger.error("...could not read header in extension '" + name + "'") 179 return 180 #else: self.logger.info("...read header at '" + name + "' and found " + str(len(header)) + " header cards") 142 181 143 182 return header … … 262 301 263 302 first = True 303 304 self.totalDetections = 0 264 305 for table in tables: 265 306 266 sql = "SELECT MIN(objID), MAX(objID) FROM " + table267 rs = self.scratchDb. stmt.executeQuery(sql)307 sql = "SELECT MIN(objID), MAX(objID), COUNT(objID) FROM " + table 308 rs = self.scratchDb.executeQuery(sql) 268 309 rs.first() 310 311 self.totalDetections = self.totalDetections + rs.getLong(3) 269 312 270 313 if first: … … 276 319 277 320 first = False 321 rs.close() 278 322 279 323 self.ippToPspsDb.updateMinMaxObjID(self.batchID, self.minObjID, self.maxObjID) 324 self.logger.info("Total detections = %ld min objID = %ld max objID = %ld" % (self.totalDetections, self.minObjID, self.maxObjID)) 325 280 326 281 327 ''' … … 313 359 self.pspsTables = stilts.treads(self.pspsVoTableFilePath) 314 360 for table in self.pspsTables: 315 self.logger. info("Creating PSPS table: " + table.name)361 self.logger.debug("Creating PSPS table: " + table.name) 316 362 table.write(self.scratchDb.url + '#' + table.name) 317 363 self.tablesToExport.append(table.name) … … 337 383 Accepts a regular expression filter so not all tables need to be imported 338 384 ''' 339 def importIppTables(self, filter ):385 def importIppTables(self, filter=""): 340 386 341 387 self.logger.info("Attempting to import tables from input FITS file") … … 347 393 match = re.match(filter, table.name) 348 394 if not match: continue 349 self.logger.info(" Reading IPP table " + table.name + " from FITS file")395 self.logger.info("Reading IPP table " + table.name + " from FITS file") 350 396 table = stilts.tpipe(table, cmd='explodeall') 351 397 352 398 # drop any previous tables before import 353 self.scratchDb.dropTable(table.name)399 #self.scratchDb.dropTable(table.name) 354 400 355 401 # IPP FITS files are littered with infinities, so remove these 356 self.logger. info("Removing Infinity values from all columns")402 self.logger.debug("Removing Infinity values from all columns") 357 403 table = stilts.tpipe(table, cmd='replaceval -Infinity null *') 358 404 table = stilts.tpipe(table, cmd='replaceval Infinity null *') … … 360 406 try: 361 407 table.write(self.scratchDb.url + '#' + table.name) 408 self.scratchDb.killLastConnectionID() 409 count = count + 1 362 410 except: 363 self.logger.exception(" Problem writing table '" + table.name + "' to the database")364 count = count + 1 411 self.logger.exception("Problem writing table '" + table.name + "' to the database") 412 365 413 366 414 self.logger.info("Done. Imported %d tables" % count) 367 368 415 self.indexIppTables() 369 416 … … 373 420 def exportPspsTablesToFits(self, regex="(.*)"): 374 421 375 self.logger.info("Replacing NULLs with -999 then exporting all PSPS tables to FITS")422 self.logger.info("Replacing NULLs with -999, changing tables names using regex: " + regex) 376 423 _tables = [] 377 424 378 self.logger.info(" Selecting database tables")425 self.logger.info("Selecting database tables") 379 426 for table in self.tablesToExport: 380 427 … … 383 430 384 431 # get everything from table 385 _table = stilts.tread(self.scratchDb.url + '#SELECT * FROM ' + table) 386 432 try: 433 _table = stilts.tread(self.scratchDb.url + '#SELECT * FROM ' + table) 434 self.scratchDb.killLastConnectionID() 435 except: 436 self.logger.exception("Could not read from DB table: " + table) 437 return False 438 387 439 # replace nulls and empty fields with weird PSPS -999 pseudo-null 388 440 _table = stilts.tpipe(_table, cmd='replaceval "" -999 *') … … 395 447 _tables.append(_table) 396 448 397 self.logger.info(" Writing to FITS file '" + self.outputFitsPath + "'...") 398 stilts.twrites(_tables, self.outputFitsPath, fmt='fits') 399 self.logger.info(" ...done") 400 self.ippToPspsDb.updateProcessed(self.batchID, 1) 449 self.logger.info("Writing to FITS file '" + self.outputFitsPath + "'...") 450 try: 451 stilts.twrites(_tables, self.outputFitsPath, fmt='fits') 452 self.ippToPspsDb.updateProcessed(self.batchID, 1) 453 except: 454 self.logger.exception("Could not write to FITS") 455 return False 456 457 return True 401 458 402 459 ''' … … 447 504 ''' 448 505 def alreadyProcessed(self): 449 self.logger.info("Not implemented") 450 451 452 506 self.logger.info("Not implemented") 507 508 509 ''' 510 Creates and publishes a batch 511 ''' 512 def run(self): 513 514 if not self.everythingOK: return 515 516 self.createEmptyPspsTables() 517 self.importIppTables() 518 if self.populatePspsTables(): 519 if self.exportPspsTablesToFits(): 520 self.writeBatchManifest() 521 self.createTarball() 522 self.publishToDatastore() 523 #self.reportNullsInAllPspsTables(False) 524 #sys.exit() 525 self.logger.info("Finished.") 526 527
Note:
See TracChangeset
for help on using the changeset viewer.
