IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 15, 2011, 8:53:08 AM (15 years ago)
Author:
rhenders
Message:

New generic FITS header reading method, and methid to search for a particular header

File:
1 edited

Legend:

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

    r31286 r31291  
    102102        self.dateStr = now.strftime("%Y-%m-%d")
    103103
    104         if self.inputFitsPath != "": self.parseFitsHeader()
     104        if self.inputFitsPath != "":
     105            file = open(self.inputFitsPath)
     106            self.header = self.parseFitsHeader(file)
     107            # TODO close file?
    105108
    106109        # create DVO table
     
    115118        self.localStmt.close()
    116119        self.localCon.close()
     120
     121    '''
     122    Finds and reads a header extension
     123    '''
     124    def findAndReadFITSHeader(self, name):
     125
     126        self.logger.info("Searching for header extension: '" + name + "'...")
     127
     128        file = open(self.inputFitsPath, 'r')
     129
     130        index = 0
     131        found = False
     132        while True:
     133           
     134            file.seek(index, 0)
     135
     136            record = file.read(80)
     137            if not record: break;
     138
     139            header = {}
     140
     141            # quit when we reach 'END'
     142            if record.startswith("XTENSION= 'IMAGE"):
     143
     144                header = self.parseFitsHeader(file)
     145                if header['EXTNAME'] == name:
     146                    found = True
     147                    break
     148           
     149            index = index + 2880
     150
     151        if found != True: self.logger.error("...could not find extension '" + name + "'")
     152        else: self.logger.info("...read header at '" + name + "' and found " + str(len(header)) + " header cards")
     153
     154        # TODO close file?
     155        return header
     156
    117157
    118158    '''
     
    265305    Reads FITS header and stores all fields in a dictionary object
    266306    '''
    267     def parseFitsHeader(self):
    268 
    269         fitsFile = open(self.inputFitsPath)
    270 
    271         self.header = {}
     307    def parseFitsHeader(self, fitsFile):
     308
     309        header = {}
    272310
    273311        while (True):
     312
    274313           record = fitsFile.read(80)
    275314
    276315           # quit when we reach 'END'
    277            if record.startswith("END"): break
    278 
    279            # ignore comments
    280            if record.startswith("COMMENT"): continue
    281            match = re.match('(.*)=(.*)', record)
     316           if re.match('END\s+', record): break
     317
     318           # this regex will get param/value pairs for all header cards, ignoring comments and parsing out 'HIERACH' prefixes
     319           match = re.match('^(HIERARCH )*([a-zA-Z0-9-_\.]+)\s*=\s+\'*([a-zA-Z0-9-_\.:\s@#]+)\'*\\/*', record)
    282320           if match:
    283321
    284                # remove HIERARCH prefix
    285                param = match.group(1).replace("HIERARCH", "")
    286                param = param.strip()
    287 
    288                value = match.group(2)
    289                # remove trailing comment after / char, if there is one
    290                index = value.find("/")
    291                if index != -1: value = value[0:index]
    292 
    293                # remove ' chars around content
    294                value = value.replace("'", "")
    295 
    296                # remove leading and trailing whitespace
    297                value = value.strip()
    298 
    299                # store in out dictionary object
    300                self.header[param] = value
     322               param = match.group(2)
     323               value = match.group(3).strip()
     324               header[param] = value
    301325
    302326               #print param + "|" + value + "|"
     327
     328        return header
    303329
    304330    '''
     
    363389          table = stilts.tpipe(table, cmd='replaceval -Infinity null *')
    364390          table = stilts.tpipe(table, cmd='replaceval Infinity null *')
     391          #params = table.parameters()
     392          #print params
    365393
    366394          try:
Note: See TracChangeset for help on using the changeset viewer.