- 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/batch.py (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/meh_branches/ppsub_test
- Property svn:mergeinfo changed
-
branches/meh_branches/ppsub_test/ippToPsps/jython/batch.py
r32118 r33098 39 39 ippToPspsDb, 40 40 id, 41 batchID, 41 42 batchType, 42 43 fits): … … 47 48 self.doc = doc 48 49 self.fits = fits 49 if self.fits: self.header = self.fits.getPrimaryHeader()50 50 51 51 # set up logging … … 56 56 # set up class variables 57 57 self.id = id 58 self.batchID = batchID 58 59 self.gpc1Db = gpc1Db 59 60 self.ippToPspsDb = ippToPspsDb … … 61 62 self.pspsVoTableFilePath = "../config/" + batchType + "/tables.vot" 62 63 64 # check FITS file is ok 65 if self.fits: 66 self.header = self.fits.getPrimaryHeader() 67 68 # now check that the fits header is readable 69 if not self.header: 70 logger.errorPair("Could not read FITS for id", "%d" % id) 71 return 72 73 # if no fits file, and this is not an IN batch, then get out of here 74 else: 75 if batchType != "IN": 76 logger.errorPair("Could not read FITS for id", "%d" % id) 77 return 78 63 79 # get info from config 64 80 self.survey = self.doc.find("options/survey").text 65 81 self.pspsSurvey = self.doc.find("options/pspsSurvey").text 66 dvoGpc1Label = self.doc.find("dvo/gpc1Label").text82 self.dvoGpc1Label = self.doc.find("dvo/gpc1Label").text 67 83 self.dvoLocation = self.doc.find("dvo/location").text 68 84 self.useFullTables = int(self.doc.find("dvo/useFullTables").text) … … 78 94 else: 79 95 self.surveyID = -1; 80 81 96 82 97 # get some options from the config … … 85 100 86 101 # create datastore object 87 self.datastore = Datastore(self.logger, self.doc) 88 89 # create a new batch 90 self.batchID = self.ippToPspsDb.createNewBatch( 91 self.batchType, 92 self.id, 93 self.survey, 94 dvoGpc1Label, 95 self.datastore.product) 102 self.datastore = Datastore(self.logger, self.doc, self.ippToPspsDb) 96 103 97 104 # get local storage location from config 98 105 self.batchName = Batch.getNameFromID(self.batchID) 99 basePath = self.doc.find("localOutPath").text106 self.basePath = self.doc.find("localOutPath").text 100 107 self.subDir = Batch.getSubDir( 101 basePath,108 self.basePath, 102 109 self.batchType, 103 dvoGpc1Label)110 self.dvoGpc1Label) 104 111 105 112 self.localOutPath = Batch.getOutputPath( 106 basePath,113 self.basePath, 107 114 self.batchType, 108 dvoGpc1Label,115 self.dvoGpc1Label, 109 116 self.batchID) 110 117 … … 157 164 158 165 ''' 166 Static method to get name of tar file 167 ''' 168 @staticmethod 169 def getTarFile(id): 170 return Batch.getNameFromID(id) + ".tar" 171 172 ''' 173 Static method to get path of tar file 174 ''' 175 @staticmethod 176 def getTarPath(basePath, batchType, dvoLabel, id): 177 return Batch.getSubDir(basePath, batchType, dvoLabel) + "/" + Batch.getTarFile(id) 178 179 ''' 180 Static method to get name of tarball file 181 ''' 182 @staticmethod 183 def getTarballFile(id): 184 return Batch.getTarFile(id) + ".gz" 185 186 ''' 187 Static method to get path of tarball file 188 ''' 189 @staticmethod 190 def getTarballPath(basePath, batchType, dvoLabel, id): 191 return Batch.getSubDir(basePath, batchType, dvoLabel) + "/" + Batch.getTarballFile(id) 192 193 ''' 194 Static method to delete this batch from local disk 195 ''' 196 @staticmethod 197 def deleteFromDisk(logger, basePath, batchType, dvoLabel, id): 198 199 tarballPath = Batch.getTarballPath(basePath, batchType, dvoLabel, id) 200 dirPath = Batch.getOutputPath(basePath, batchType, dvoLabel, id) 201 202 if not os.path.exists(tarballPath): 203 logger.errorPair("Could not find", tarballPath) 204 return 1 205 206 try: 207 os.remove(tarballPath) 208 logger.infoPair("Deleted", tarballPath) 209 except: 210 logger.errorPair("Could not delete", tarballPath) 211 return 0 212 213 return 1 214 215 ''' 159 216 Destructor 160 217 ''' … … 194 251 195 252 outPath = self.localOutPath + "/BatchManifest.xml" 196 tmpPath = "./tmp.xml"253 tmpPath = self.localOutPath + "/tmp.xml" 197 254 self.logger.infoPair("Creating manifest", outPath) 198 255 root = Element('manifest') … … 210 267 else: root.attrib['maxObjId'] = str(self.maxObjID) 211 268 212 # get md5sum213 p = Popen("md5sum " + self.outputFitsPath, shell=True, stdout=PIPE)214 p.wait()215 out = p.stdout.read()216 md5sum = out[0:out.rfind(" ")]217 218 # get file size219 fileSize = os.path.getsize(self.outputFitsPath)220 221 269 # file information 222 270 child = Element('file') 223 271 root.append(child) 224 272 child.attrib['name'] = self.outputFitsFile 225 child.attrib['bytes'] = str(fileSize)226 child.attrib['md5'] = md5sum227 273 228 274 # now create doc and write to file … … 240 286 Publishes this batch to the datastore 241 287 ''' 242 def publishToDatastore(self):288 def tarAndZip(self): 243 289 244 290 # set up filenams and paths 245 tarFile = self.batchName + ".tar" 246 tarPath = self.subDir + "/" + tarFile 247 248 tarballFile = tarFile + ".gz" 249 tarballPath = self.subDir + "/" + tarballFile 291 tarFile = Batch.getTarFile(self.batchID) 292 tarPath = Batch.getTarPath(self.basePath, self.batchType, self.dvoGpc1Label, self.batchID) 293 tarballFile = Batch.getTarballFile(self.batchID) 294 tarballPath = Batch.getTarballPath(self.basePath, self.batchType, self.dvoGpc1Label, self.batchID) 250 295 251 296 # tar directory 252 p = Popen("tar -cvf " + tarPath + "\253 -C " + self.subDir + " \254 " + self.batchName, shell=True, stdout=PIPE)297 cmd = "tar -cvf " + tarPath + " -C " + self.subDir + " " + self.batchName 298 self.logger.infoPair("Creating tar archive", cmd) 299 p = Popen(cmd, shell=True, stdout=PIPE) 255 300 p.wait() 256 301 302 if p.returncode != 0: 303 self.logger.errorPair("tar command", "failed") 304 return False 305 257 306 # zip tar archive 258 p = Popen("gzip -c " + tarPath + " > " + tarballPath, shell=True, stdout=PIPE) 307 cmd = "gzip -c " + tarPath + " > " + tarballPath 308 self.logger.infoPair("Compressing tar archive", cmd) 309 p = Popen(cmd, shell=True, stdout=PIPE) 259 310 p.wait() 311 312 if p.returncode != 0: 313 self.logger.errorPair("gzip command", "failed") 314 return False 315 316 # only now can we report that the batch has successfully processed 317 self.ippToPspsDb.updateProcessed(self.batchID, 1) 260 318 261 319 # delete tar file and original directory … … 263 321 shutil.rmtree(self.localOutPath) 264 322 265 # now publish to the datastore 266 if self.datastore.publish(self.batchName, self.subDir, tarballFile, "tgz"): 267 self.ippToPspsDb.updateLoadedToDatastore(self.batchID, 1) 323 return True 324 325 ''' 326 Static method to publish a batch to the datastore 327 ''' 328 @staticmethod 329 def publishToDatastore(datastore, batchID, batchName, subDir, tarballFile): 330 datastore.publish(batchName, subDir, tarballFile, "tgz") 268 331 269 332 ''' … … 293 356 rs.close() 294 357 295 self.ippToPspsDb.update MinMaxObjID(self.batchID, self.minObjID, self.maxObjID)358 self.ippToPspsDb.updateDetectionStats(self.batchID, self.minObjID, self.maxObjID, self.totalDetections) 296 359 self.logger.infoPair("Total detections", "%ld" % self.totalDetections) 297 360 self.logger.infoPair("Min objID", "%ld" % self.minObjID) … … 309 372 self.tablesToExport.append(table.name) 310 373 311 self.alterPspsTables();374 return self.alterPspsTables(); 312 375 313 376 ''' … … 329 392 Accepts a regular expression filter so not all tables need to be imported 330 393 ''' 331 def importIppTables(self, filter=""):394 def importIppTables(self, columns="*", filter=""): 332 395 333 396 self.logger.infoPair("Importing tables with filter", filter) … … 347 410 # IPP FITS files are littered with infinities, so remove these 348 411 self.logger.debug("Removing Infinity values from all columns") 412 table = stilts.tpipe(table, cmd='keepcols "' + columns + '"') 349 413 table = stilts.tpipe(table, cmd='replaceval -Infinity null *') 350 414 table = stilts.tpipe(table, cmd='replaceval Infinity null *') … … 396 460 try: 397 461 stilts.twrites(_tables, self.outputFitsPath, fmt='fits') 398 self.ippToPspsDb.updateProcessed(self.batchID, 1)399 462 except: 400 463 self.logger.exception("Could not write to FITS") 464 self.ippToPspsDb.updateProcessed(self.batchID, -1) 401 465 return False 402 466 … … 441 505 p = Popen(cmd, shell=True, stdout=PIPE) 442 506 p.wait() 443 # out = p.stdout.read()444 507 445 508 rowCount = self.scratchDb.getRowCount("dvoDetection") … … 453 516 454 517 ''' 455 Checks whether this batch has already been processed and published. To be implemented by all subclasses456 '''457 def alreadyProcessed(self):458 self.logger.warn("alreadyProcessed not implemented")459 460 461 '''462 518 Creates and publishes a batch 519 TODO all methods call below should throw exceptions on failure 463 520 ''' 464 521 def run(self): … … 466 523 if not self.everythingOK: 467 524 self.logger.error("Aborting this batch due to (hopefully) previously reported errors") 525 self.ippToPspsDb.updateProcessed(self.batchID, -1) 468 526 return 469 527 470 self.createEmptyPspsTables() 528 if not self.createEmptyPspsTables(): 529 self.logger.error("Aborting this batch due to (hopefully) previously reported errors") 530 self.ippToPspsDb.updateProcessed(self.batchID, -1) 531 return 532 471 533 self.importIppTables() 472 if self.populatePspsTables(): 473 if self.exportPspsTablesToFits(): 534 if not self.populatePspsTables(): 535 self.logger.errorPair("Aborting this batch", "unable to populate PSPS tables") 536 self.ippToPspsDb.updateProcessed(self.batchID, -1) 537 else: 538 if not self.exportPspsTablesToFits(): 539 self.logger.errorPair("Aborting this batch", "unable to export tables to FITS file") 540 else: 474 541 self.writeBatchManifest() 475 if int(self.doc.find("options/publishToDatastore").text): self.publishToDatastore() 542 if int(self.doc.find("options/publishToDatastore").text): 543 # tar and zip ready for publication to datastore 544 if self.tarAndZip(): 545 # now publish to the datastore 546 tarballFile = Batch.getTarballFile(self.batchID) 547 Batch.publishToDatastore(self.datastore, self.batchID, self.batchName, self.subDir, tarballFile) 548 476 549 if int(self.doc.find("options/reportNulls").text): self.reportNullsInAllPspsTables(False) 477 550 478 self.logger.infoPair("Batch......", "complete")479 480 551 from datastore import Datastore 481 552
Note:
See TracChangeset
for help on using the changeset viewer.
