IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 11, 2012, 11:19:21 PM (15 years ago)
Author:
mhuber
Message:

merged latest r33093 trunk changes to meh_branches/ppsub_test

Location:
branches/meh_branches/ppsub_test
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/meh_branches/ppsub_test

  • branches/meh_branches/ppsub_test/ippToPsps/jython/batch.py

    r32118 r33098  
    3939                 ippToPspsDb,
    4040                 id,
     41                 batchID,
    4142                 batchType,
    4243                 fits):
     
    4748        self.doc = doc
    4849        self.fits = fits
    49         if self.fits: self.header = self.fits.getPrimaryHeader()
    5050
    5151        # set up logging
     
    5656        # set up class variables
    5757        self.id = id
     58        self.batchID = batchID
    5859        self.gpc1Db = gpc1Db
    5960        self.ippToPspsDb = ippToPspsDb
     
    6162        self.pspsVoTableFilePath = "../config/" + batchType + "/tables.vot"
    6263
     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
    6379        # get info from config
    6480        self.survey = self.doc.find("options/survey").text
    6581        self.pspsSurvey = self.doc.find("options/pspsSurvey").text
    66         dvoGpc1Label = self.doc.find("dvo/gpc1Label").text
     82        self.dvoGpc1Label = self.doc.find("dvo/gpc1Label").text
    6783        self.dvoLocation = self.doc.find("dvo/location").text
    6884        self.useFullTables = int(self.doc.find("dvo/useFullTables").text)
     
    7894        else:
    7995            self.surveyID = -1;
    80 
    8196       
    8297        # get some options from the config
     
    85100
    86101        # 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)
    96103
    97104        # get local storage location from config
    98105        self.batchName = Batch.getNameFromID(self.batchID)
    99         basePath = self.doc.find("localOutPath").text
     106        self.basePath = self.doc.find("localOutPath").text
    100107        self.subDir = Batch.getSubDir(
    101                 basePath,
     108                self.basePath,
    102109                self.batchType,
    103                 dvoGpc1Label)
     110                self.dvoGpc1Label)
    104111
    105112        self.localOutPath = Batch.getOutputPath(
    106                 basePath,
     113                self.basePath,
    107114                self.batchType,
    108                 dvoGpc1Label,
     115                self.dvoGpc1Label,
    109116                self.batchID)
    110117
     
    157164
    158165    '''
     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    '''
    159216    Destructor
    160217    '''
     
    194251
    195252        outPath = self.localOutPath + "/BatchManifest.xml"
    196         tmpPath = "./tmp.xml"
     253        tmpPath = self.localOutPath + "/tmp.xml"
    197254        self.logger.infoPair("Creating manifest", outPath)
    198255        root = Element('manifest')
     
    210267        else: root.attrib['maxObjId'] = str(self.maxObjID)
    211268
    212         # get md5sum
    213         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 size
    219         fileSize = os.path.getsize(self.outputFitsPath)
    220 
    221269        # file information
    222270        child = Element('file')
    223271        root.append(child)
    224272        child.attrib['name'] = self.outputFitsFile
    225         child.attrib['bytes'] = str(fileSize)
    226         child.attrib['md5'] = md5sum
    227273
    228274        # now create doc and write to file
     
    240286    Publishes this batch to the datastore
    241287    '''
    242     def publishToDatastore(self):
     288    def tarAndZip(self):
    243289     
    244290        # 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)
    250295
    251296        # 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)
    255300        p.wait()
    256301
     302        if p.returncode != 0:
     303            self.logger.errorPair("tar command", "failed")
     304            return False
     305
    257306        # 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)
    259310        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)
    260318
    261319        # delete tar file and original directory
     
    263321        shutil.rmtree(self.localOutPath)
    264322
    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")
    268331
    269332    '''
     
    293356            rs.close()
    294357
    295         self.ippToPspsDb.updateMinMaxObjID(self.batchID, self.minObjID, self.maxObjID)
     358        self.ippToPspsDb.updateDetectionStats(self.batchID, self.minObjID, self.maxObjID, self.totalDetections)
    296359        self.logger.infoPair("Total detections", "%ld" % self.totalDetections)
    297360        self.logger.infoPair("Min objID", "%ld" % self.minObjID)
     
    309372             self.tablesToExport.append(table.name)
    310373
    311          self.alterPspsTables();
     374         return self.alterPspsTables();
    312375
    313376    '''
     
    329392    Accepts a regular expression filter so not all tables need to be imported
    330393    '''
    331     def importIppTables(self, filter=""):
     394    def importIppTables(self, columns="*", filter=""):
    332395
    333396      self.logger.infoPair("Importing tables with filter", filter)
     
    347410          # IPP FITS files are littered with infinities, so remove these
    348411          self.logger.debug("Removing Infinity values from all columns")
     412          table = stilts.tpipe(table, cmd='keepcols "' + columns + '"')
    349413          table = stilts.tpipe(table, cmd='replaceval -Infinity null *')
    350414          table = stilts.tpipe(table, cmd='replaceval Infinity null *')
     
    396460        try:
    397461            stilts.twrites(_tables, self.outputFitsPath, fmt='fits')
    398             self.ippToPspsDb.updateProcessed(self.batchID, 1)
    399462        except:
    400463            self.logger.exception("Could not write to FITS")
     464            self.ippToPspsDb.updateProcessed(self.batchID, -1)
    401465            return False
    402466
     
    441505        p = Popen(cmd, shell=True, stdout=PIPE)
    442506        p.wait()
    443         #        out = p.stdout.read()
    444507
    445508        rowCount = self.scratchDb.getRowCount("dvoDetection")
     
    453516
    454517    '''
    455     Checks whether this batch has already been processed and published. To be implemented by all subclasses
    456     '''
    457     def alreadyProcessed(self):
    458         self.logger.warn("alreadyProcessed not implemented")
    459 
    460 
    461     '''
    462518    Creates and publishes a batch
     519    TODO all methods call below should throw exceptions on failure
    463520    '''
    464521    def run(self):
     
    466523        if not self.everythingOK:
    467524            self.logger.error("Aborting this batch due to (hopefully) previously reported errors")
     525            self.ippToPspsDb.updateProcessed(self.batchID, -1)
    468526            return
    469527
    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
    471533        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:
    474541                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
    476549                if int(self.doc.find("options/reportNulls").text): self.reportNullsInAllPspsTables(False)
    477550
    478         self.logger.infoPair("Batch......", "complete")
    479 
    480551from datastore import Datastore
    481552
Note: See TracChangeset for help on using the changeset viewer.