Changeset 31222
- Timestamp:
- Apr 6, 2011, 10:25:10 PM (15 years ago)
- File:
-
- 1 edited
-
trunk/ippToPsps/jython/batch.py (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippToPsps/jython/batch.py
r31201 r31222 7 7 import os 8 8 import md5 9 9 import shutil 10 import logging 11 from subprocess import call, PIPE, Popen 12 13 from datastore import Datastore 10 14 from gpc1db import Gpc1Db 11 15 from ipptopspsdb import IppToPspsDb … … 14 18 from java.sql import * 15 19 from xml.etree.ElementTree import ElementTree, Element, tostring 16 from xml.dom import minidom17 20 18 21 ''' … … 30 33 "../config/2/tables.vot" 31 34 ''' 32 def __init__(self, batchType, inputFitsPath="", survey=""): 35 def __init__(self, logger, batchType, inputFitsPath="", survey=""): 36 37 # set up logging 38 self.logger = logger 39 self.logger.debug("Batch class constructor") 33 40 34 41 # set up class variables … … 51 58 52 59 # create Gpc1Db object 53 self.gpc1Db = Gpc1Db( )60 self.gpc1Db = Gpc1Db(self.logger) 54 61 55 62 # get survey ID from init table … … 60 67 self.surveyID = rs.getInt(1) 61 68 except: 62 self.log ("No survey ID found for this survey: '" + survey + "'")69 self.logger.exception("No survey ID found for this survey: '" + survey + "'") 63 70 self.surveyID = -1; 64 71 65 72 # get dvo info from config 66 dvoName = doc.find(survey+"dvo/name").text 67 dvoLocation = doc.find(survey+"dvo/location").text 73 if survey != "": 74 dvoName = doc.find(survey+"dvo/name").text 75 dvoLocation = doc.find(survey+"dvo/location").text 76 else: 77 dvoName = "" 78 dvoLocation = "" 68 79 69 80 # get datastore info from config 70 datastoreProduct = doc.find("datastore/product").text81 self.datastore = Datastore(self.logger) 71 82 72 83 # create IppToPspsDb object and create a new batch 73 self.ippToPspsDb = IppToPspsDb() 74 self.batchID = self.ippToPspsDb.createNewBatch(66, survey, self.getPspsBatchType(), dvoName, datastoreProduct) 84 self.ippToPspsDb = IppToPspsDb(logger) 85 self.batchID = self.ippToPspsDb.createNewBatch(66, 86 survey, 87 self.getPspsBatchType(), 88 dvoName, 89 self.datastore.product) 75 90 76 91 # get local storage location from config 77 92 self.batchName = "B%08d" % self.batchID 78 self.localOutPath = doc.find("localOutPath").text + "/" + self.getPspsBatchType() + "/" + dvoName + "/%s" % self.batchName 93 self.subDir = doc.find("localOutPath").text + "/" + self.getPspsBatchType() + "/" + dvoName 94 self.localOutPath = self.subDir + "/" + self.batchName 79 95 if not os.path.exists(self.localOutPath): os.makedirs(self.localOutPath) 80 96 … … 94 110 def __del__(self): 95 111 96 self.log ("Batch destructor")112 self.logger.debug("Batch destructor") 97 113 self.localStmt.close() 98 114 self.localCon.close() … … 105 121 outPath = self.localOutPath + "/BatchManifest.xml" 106 122 tmpPath = "./tmp.xml" 107 self.log ("Creating batch manifest file here: " + outPath)123 self.logger.info("Creating batch manifest file here: " + outPath) 108 124 root = Element('manifest') 109 125 … … 117 133 118 134 # get md5sum 119 fits=os.popen("md5sum " + self.outputFitsPath) 120 for f in fits.readlines(): md5sum = f[0:f.rfind(" ")] 135 p = Popen("md5sum " + self.outputFitsPath, shell=True, stdout=PIPE) 136 p.wait() 137 out = p.stdout.read() 138 md5sum = out[0:out.rfind(" ")] 121 139 122 140 # get file size … … 136 154 137 155 # clunky way to prettify XML 138 os.popen("xmllint --format " + tmpPath + " > " + outPath) 139 # os.remove(tmpPath) 140 156 p = Popen("xmllint --format " + tmpPath + " > " + outPath, shell=True, stdout=PIPE) 157 p.wait() 158 os.remove(tmpPath) 159 160 161 ''' 162 tar and zips batch directory 163 ''' 164 def createTarball(self): 165 166 # set up filenams and paths 167 tarFile = self.batchName + ".tar" 168 tarPath = self.subDir + "/" + tarFile 169 170 self.tarballFile = tarFile + ".gz" 171 tarballPath = self.subDir + "/" + self.tarballFile 172 173 # tar directory 174 p = Popen("tar -cvf " + tarPath + "\ 175 -C " + self.subDir + " \ 176 " + self.batchName, shell=True, stdout=PIPE) 177 p.wait() 178 179 # zip tar archive 180 p = Popen("gzip -c " + tarPath + " > " + tarballPath, shell=True, stdout=PIPE) 181 p.wait() 182 183 # delete tar file and original directory 184 os.remove(tarPath) 185 shutil.rmtree(self.localOutPath) 186 187 ''' 188 Publishes this batch to the datastore 189 ''' 190 def publishToDatastore(self): 191 192 self.datastore.publish(self.batchName, self.subDir, self.tarballFile, "tgz") 193 # TODO update ippToPsps Db here 141 194 142 195 ''' … … 147 200 if self.survey == "ThreePi": return "3PI" 148 201 elif self.survey == "MD04": return "MD04" 149 else: self.log ("ERROR:Don't know this survey: " + self.survey)202 else: self.logger.error("Don't know this survey: " + self.survey) 150 203 151 204 ''' … … 157 210 elif self.batchType == "detection": return "P2" 158 211 elif self.batchType == "stack": return "ST" 159 else: self.log ("ERROR:Don't know this batch type: " + self.survey)212 else: self.logger.error("Don't know this batch type: " + self.survey) 160 213 161 214 … … 244 297 self.pspsTables = stilts.treads(self.pspsVoTableFilePath) 245 298 for table in self.pspsTables: 246 self.log ("Creating PSPS table: " + table.name)299 self.logger.info("Creating PSPS table: " + table.name) 247 300 table.write(self.localUrl + '#' + table.name) 248 301 … … 253 306 ''' 254 307 def indexIppTables(self): 255 self.log ("indexIppTables not implemented")308 self.logger.warn("indexIppTables not implemented") 256 309 257 310 … … 261 314 def createIndex(self, table, column): 262 315 263 self.log ("Creating index on column '"+column+"' for table '"+table+"'")316 self.logger.info("Creating index on column '"+column+"' for table '"+table+"'") 264 317 265 318 sql = "CREATE INDEX "+table+"_index ON "+table+" ("+column+")" … … 267 320 self.localStmt.execute(sql) 268 321 except: 269 self.log ("Index already in place on '" + column + "' for table '" + table + "'")322 self.logger.warn("Index already in place on '" + column + "' for table '" + table + "'") 270 323 271 324 … … 274 327 ''' 275 328 def indexPspsTables(self): 276 self.log ("indexPspsTables not implemented")329 self.logger.warn("indexPspsTables not implemented") 277 330 278 331 ''' … … 290 343 match = re.match(filter, table.name) 291 344 if not match: continue 292 self.log ("Creating IPP table " + table.name)345 self.logger.info("Creating IPP table " + table.name) 293 346 table = stilts.tpipe(table, cmd='explodeall') 294 347 try: 295 348 table.write(self.localUrl + '#' + table.name) 296 349 except: 297 self.log ("ERROR problem writing table '" + table.name + "' to the database")350 self.logger.exception("Problem writing table '" + table.name + "' to the database") 298 351 299 352 count = count + 1 300 353 301 self.log ("Imported %d tables from '%s' " % (count, self.inputFitsPath))354 self.logger.info("Imported %d tables from '%s' " % (count, self.inputFitsPath)) 302 355 303 356 self.indexIppTables() … … 308 361 def exportPspsTablesToFits(self): 309 362 310 self.log ("Exporting all PSPS tables to FITS")363 self.logger.info("Exporting all PSPS tables to FITS") 311 364 _tables = [] 312 365 313 self.log (" Selecting database tables")366 self.logger.info(" Selecting database tables") 314 367 for table in self.pspsTables: 315 368 _table = stilts.tread(self.localUrl + '#SELECT * FROM ' + table.name) … … 317 370 _tables.append(_table) 318 371 319 self.log (" Writing to FITS file '" + self.outputFitsPath + "'...")372 self.logger.info(" Writing to FITS file '" + self.outputFitsPath + "'...") 320 373 stilts.twrites(_tables, self.outputFitsPath, fmt='fits') 321 self.log (" ...done")374 self.logger.info(" ...done") 322 375 323 376 ''' … … 394 447 def replaceAllPspsNulls(self, sub): 395 448 396 self.log ("Replacing all NULL values in PSPS tables with '" + sub + "'...")449 self.logger.info("Replacing all NULL values in PSPS tables with '" + sub + "'...") 397 450 for table in self.pspsTables: 398 451 self.replaceNulls(table.name, sub) 399 self.log ("...done")452 self.logger.info("...done") 400 453 401 454 ''' … … 403 456 ''' 404 457 def populatePspsTables(self): 405 self.log ("Not implemented yet")458 self.logger.warn("Not implemented yet") 406 459 407 460 ''' … … 410 463 def updateDvoIDs(self, table): 411 464 412 self.log ("Updating table '" + table + "' with DVO IDs...")465 self.logger.info("Updating table '" + table + "' with DVO IDs...") 413 466 sql = "UPDATE " + table + " AS a, dvo AS b SET \ 414 467 a.ippObjID = b.ippObjID, \ … … 416 469 WHERE a.ippDetectID = b.ippDetectID" 417 470 self.localStmt.execute(sql) 418 self.log ("...done")471 self.logger.info("...done") 419 472 420 473 … … 424 477 def createDvoTable(self): 425 478 426 self.log ("Creating DVO table for ID matching")479 self.logger.info("Creating DVO table for ID matching") 427 480 sql = "DROP TABLE dvo" 428 481 self.localStmt.execute(sql)
Note:
See TracChangeset
for help on using the changeset viewer.
