[FRPythoneers] Meas data file format reader in Python

Wayde Allen wallen at boulder.nist.gov
Fri Aug 11 12:58:15 MDT 2000


The following is a bit crude, but would be an example of a Python object
class that could be used to read the meas file.

class gammafile:                 # Object Class to import data from ascii file
   def __init__(self):
      self.filename = ''
      self.version = ''
      self.date = ''
      self.datafile = ''
      self.datatype = ''
      self.operator = ''
      self.system = ''
      self.customer = ''
      self.data = {}

   def fileopen (self, filename):

      self.filename = filename
      self.freqcount = 0     # This is the number of frequencies in the data set

      self.rawdata = open(self.filename,'r')

      for line in self.rawdata.readlines():

         if len(line) > 1:                  # Skip blank lines
            words = string.split(line)
            first_word = words[0]

            if first_word == '#VERSION:':
               self.version = string.join(words[1:])

            elif first_word == '#DATE:':
               self.date = string.join(words[1:])

            elif first_word == '#FILENAME:':
               self.datafile = string.join(words[1:])

            elif first_word == '#OPERATOR:':
               self.operator = string.join(words[1:])

            elif first_word == '#SYSTEM:':
               self.system = string.join(words[1:])

            elif first_word == '#CUSTOMER:':
               self.customer = string.join(words[1:])

            elif first_word =='#DATATYPE:':
               self.datatype = string.join(words[1:])

            elif first_word[0] == '#':
               pass

            else:
               freq = string.atof(words[0])    # Get The Frequency 
                                               # Currently assumes no duplicates

               self.freqcount = self.freqcount + 1

               # Next get the complex values

               if self.datatype == "COMPLEX":    # Complex number conversion not necessary

                  short = complex(string.atof(words[1]),string.atof(words[2]))
                  opencirc  = complex(string.atof(words[3]),string.atof(words[4]))
                  load  = complex(string.atof(words[5]),string.atof(words[6]))
                  self.data[freq] = [short, opencirc, load]  # Make data dictionary
   
               elif self.datatype == "MAGPHASE": # Need to convert magphase to complex
                  
                  from math import cos, sin
                  PI = 3.1415926
                  shortmag   = string.atof(words[1])
                  shorttheta = string.atof(words[2]) * PI / 180
                  openmag    = string.atof(words[3])
                  opentheta  = string.atof(words[4]) * PI / 180
                  loadmag    = string.atof(words[5])
                  loadtheta  = string.atof(words[6]) * PI / 180
                  
                  short    = shortmag * complex(cos(shorttheta), sin(shorttheta))
                  opencirc = openmag  * complex(cos(opentheta), sin(opentheta))
                  load     = loadmag  * complex(cos(loadtheta), sin(loadtheta))
                  self.data[freq] = [short, opencirc, load]  # Make data dictionary
               else:

                  print self.datafile, 'Unknown filetype\n'


Once you've got this piece of code located in a file somewhere (let's call
it datafiles) it becomes a loadable module that can be used in any
subsequent program.  All the programer needs, in order to read the data
from the previously defined data file is the following piece of code.

   #!/usr/bin/env python

   from datafiles import gammafile

   mydata = gammafile()    # This creates (instantiates) an object of type
                           # gammafile

   mydata.fileopen(filename)  # Opens the file and reads it into the
                              # object mydata.  In other words you only
                              # need to write the code needed to read the
                              # file once!

Once the mydata object has been instatiated and the data loaded with the
mydata.fileopen method, the individual data elements in the file are
accessible as: 

   mydata.filename
   mydata.version
   mydata.date
   mydata.datafile   - This is the #FILE: keyword info
   mydata.datatype
   mydata.system
   mydata.customer
   mydata.data{}     - This is an array of the data contained in the file
                       (in this case converted to complex numbers)


- Wayde
  (wallen at boulder.nist.gov)






More information about the FRPythoneers mailing list