Index: trunk/ippToPsps/jython/batch.py
===================================================================
--- trunk/ippToPsps/jython/batch.py	(revision 31286)
+++ trunk/ippToPsps/jython/batch.py	(revision 31291)
@@ -102,5 +102,8 @@
         self.dateStr = now.strftime("%Y-%m-%d")
 
-        if self.inputFitsPath != "": self.parseFitsHeader()
+        if self.inputFitsPath != "": 
+            file = open(self.inputFitsPath)
+            self.header = self.parseFitsHeader(file)
+            # TODO close file?
 
         # create DVO table
@@ -115,4 +118,41 @@
         self.localStmt.close()
         self.localCon.close()
+
+    '''
+    Finds and reads a header extension
+    '''
+    def findAndReadFITSHeader(self, name):
+
+        self.logger.info("Searching for header extension: '" + name + "'...")
+
+        file = open(self.inputFitsPath, 'r')
+
+        index = 0
+        found = False
+        while True:
+            
+            file.seek(index, 0)
+
+            record = file.read(80)
+            if not record: break;
+
+            header = {}
+
+            # quit when we reach 'END'
+            if record.startswith("XTENSION= 'IMAGE"):
+
+                header = self.parseFitsHeader(file)
+                if header['EXTNAME'] == name:
+                    found = True
+                    break 
+            
+            index = index + 2880
+
+        if found != True: self.logger.error("...could not find extension '" + name + "'")
+        else: self.logger.info("...read header at '" + name + "' and found " + str(len(header)) + " header cards") 
+
+        # TODO close file?
+        return header
+
 
     '''
@@ -265,40 +305,26 @@
     Reads FITS header and stores all fields in a dictionary object
     '''
-    def parseFitsHeader(self):
-
-        fitsFile = open(self.inputFitsPath)
-
-        self.header = {}
+    def parseFitsHeader(self, fitsFile):
+
+        header = {}
 
         while (True):
+
            record = fitsFile.read(80)
 
            # quit when we reach 'END'
-           if record.startswith("END"): break
-
-           # ignore comments
-           if record.startswith("COMMENT"): continue
-           match = re.match('(.*)=(.*)', record)
+           if re.match('END\s+', record): break
+
+           # this regex will get param/value pairs for all header cards, ignoring comments and parsing out 'HIERACH' prefixes
+           match = re.match('^(HIERARCH )*([a-zA-Z0-9-_\.]+)\s*=\s+\'*([a-zA-Z0-9-_\.:\s@#]+)\'*\\/*', record)
            if match:
 
-               # remove HIERARCH prefix
-               param = match.group(1).replace("HIERARCH", "")
-               param = param.strip()
-
-               value = match.group(2)
-               # remove trailing comment after / char, if there is one
-               index = value.find("/")
-               if index != -1: value = value[0:index]
-
-               # remove ' chars around content
-               value = value.replace("'", "")
-
-               # remove leading and trailing whitespace
-               value = value.strip()
-
-               # store in out dictionary object
-               self.header[param] = value
+               param = match.group(2)
+               value = match.group(3).strip()
+               header[param] = value
 
                #print param + "|" + value + "|"
+
+        return header
 
     '''
@@ -363,4 +389,6 @@
           table = stilts.tpipe(table, cmd='replaceval -Infinity null *')
           table = stilts.tpipe(table, cmd='replaceval Infinity null *')
+          #params = table.parameters()
+          #print params
 
           try:
