IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 31840


Ignore:
Timestamp:
Jul 8, 2011, 2:48:02 PM (15 years ago)
Author:
rhenders
Message:

removed FITS specific code and moved into new Fits class; config XML document now passed-in to all database constructors; using new logging class, PSLogger

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/jython/batch.py

    r31809 r31840  
    1515from gpc1db import Gpc1Db
    1616from ipptopspsdb import IppToPspsDb
     17from fits import Fits
    1718
    1819from java.lang import *
     
    3940                 id,
    4041                 batchType,
    41                  inputFitsPath="",
     42                 fits,
    4243                 survey=""):
    4344
     
    4546        self.readHeader = False
    4647        self.doc = doc
     48        self.fits = fits
     49        self.header = self.fits.getPrimaryHeader()
    4750
    4851        # set up logging
    4952        self.logger = logger
    50         self.logger.info("-------------------------------------------------------------------------------")
     53        self.logger.infoSeparator()
    5154        self.logger.debug("Batch class constructor")
    5255
     
    5760        self.batchType = batchType;
    5861        self.pspsVoTableFilePath = "../config/" + batchType + "/tables.vot"
    59         self.inputFitsPath = inputFitsPath
    6062        self.survey = survey
    61 
    62         if self.alreadyProcessed(): return
    6363
    6464        # get dvo info from config
     
    6666        self.dvoLocation = self.doc.find("dvo/location").text
    6767        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)
    7469
    7570        # TODO
     
    124119    def printMe(self):
    125120
    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"
    161130    '''
    162131    def safeDictionaryAccess(self, header, key):
     
    166135
    167136    '''
    168     Finds and reads a header extension
    169     '''
    170     def findAndReadFITSHeader(self, name, file):
    171 
    172         found = False
    173        
    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 = True
    188                     file.seek(index + 2880, 0)
    189                     break
    190 
    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             return
    198         #else: self.logger.info("...read header at '" + name + "' and found " + str(len(header)) + " header cards")
    199 
    200         return header
    201 
    202 
    203     '''
    204137    Writes the batch manifest file
    205138    '''
     
    208141        outPath = self.localOutPath + "/BatchManifest.xml"
    209142        tmpPath = "./tmp.xml"
    210         self.logger.info("Creating batch manifest file here: " + outPath)
     143        self.logger.infoPair("Creating manifest", outPath)
    211144        root = Element('manifest')
    212145
     
    299232
    300233    '''
    301     Gets PSPS friendly batch type
     234    Gets PSPS friendly batch type TODO only use PSPS batch names, P2, ST etc
    302235    '''
    303236    def getPspsBatchType(self):
     
    335268
    336269        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)
    366273
    367274    '''
     
    398305    def importIppTables(self, filter=""):
    399306
    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())
    402309
    403310      count = 0
     
    406313          match = re.match(filter, table.name)
    407314          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)
    409316          table = stilts.tpipe(table, cmd='explodeall')
    410317
     
    425332
    426333
    427       self.logger.info("Done. Imported %d tables" % count)
     334      self.logger.infoPair("Done. Imported", "%d tables" % count)
    428335      self.indexIppTables()
    429336
     
    433340    def exportPspsTablesToFits(self, regex="(.*)"):
    434341
    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)
    436344        _tables = []
    437345
    438         self.logger.info("Selecting database tables for export")
     346        self.logger.debug("Selecting database tables for export")
    439347        for table in self.tablesToExport:
    440348
     
    460368           _tables.append(_table)
    461369
    462         self.logger.info("Writing to FITS file '" + self.outputFitsPath + "'...")
     370        self.logger.infoPair("Writing to FITS", self.outputFitsPath)
    463371        try:
    464372            stilts.twrites(_tables, self.outputFitsPath, fmt='fits')
     
    483391    def replaceAllPspsNulls(self, sub):
    484392
    485         self.logger.info("Replacing all NULL values in PSPS tables with '" + sub + "'...")
     393        self.logger.infoPair("Replacing all NULL values with", sub)
    486394        for table in self.pspsTables:
    487395            self.scratchDb.replaceNulls(table.name, sub)
    488         self.logger.info("...done")
     396        self.logger.infoPair("NULL replacement", "done")
    489397
    490398    '''
     
    501409        # TODO path to DVO prog hardcoded temporarily
    502410        cmd = "../src/dvograbber " + self.dvoLocation
    503         self.logger.info("Running: '" + cmd + "'...")
     411        self.logger.infoPair("Running DVO", cmd)
    504412        p = Popen(cmd, shell=True, stdout=PIPE)
    505413        p.wait()
    506414        #        out = p.stdout.read()
    507         self.logger.info("...done")
     415        self.logger.infoPair("DVO access", "complete")
    508416
    509417        if self.scratchDb.getRowCount("dvoDetection") < 1:
     
    517425    '''
    518426    def alreadyProcessed(self):
    519         self.logger.info("Not implemented")
     427        self.logger.error("Not implemented")
    520428
    521429
     
    535443                if int(self.doc.find("options/reportNulls").text): self.reportNullsInAllPspsTables(False)
    536444
    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.