fioPlotter

The following piece of code parses .fio files. The graphics is produced in a widget (9.6) or a png file.

Figure 9.6: fioPlotter output
\includegraphics[width=20.67cm]{fioPlotter.ps}

#!/usr/bin/env python
”'
this module reads and plots fio files. It can be invoked from 
  - the command line, blocking mode 

      $ fioPlotter.py -f fileName.fio

  - the command line, to create png files

      $ fioPlotter.py -p -f fileName.fio

  - ipython and python scripts, non-blocking mode

      import fioPlotter as fp
      a = fp.fioPlotter("<fileName>")
      a.plot()

”'
from optparse import OptionParser
import math, sys
import matplotlib
flagStandalone = False

class fioPlotter:
    def __init__(self, fileName, flagPng):
        self.comments = []
        self.user_comments = []
        self.parameters = []
        self.dataSets = []
        self.fileName = fileName
        self.flagPng = flagPng
        self._read( fileName)
        #
        # /home/p12user/dirName/gen_00001.fio -> gen_00001
        #
        self.scanName = fileName.split("/")[-1].split(".")[0]
        #
        # enable the desired display mode: avoid show() 
        # but update the plot only at the end
        #
        if not flagStandalone:
            plt.ion()
            self.fig = plt.figure( 1, figsize = (11.6, 8.2))
            self.fig.canvas.flush_events()
            #plt.pause( 1.0)
            #plt.draw()
        else:
            self.fig = plt.figure( 1, figsize = (11.6, 8.2))

        return

    def clear( self):
        ”'
        clears the figure widget
        ”'
        self.fig.clear()
        self.fig.canvas.flush_events()
        #plt.pause( 1.0)
        #plt.draw()

    def plot( self):
        ”'
        plots the contents of a fio file
        ”'
        length = len(self.dataSets)
        col = math.ceil(math.sqrt(length))
        row = math.ceil( length/col)
        plt.figtext( 0.5, 0.95, self.scanName, va='center', ha = 'center')
        for i in range( 0, length):
            plt.subplot( col, row, i + 1)
            plt.title( self.dataSets[i].deviceName, fontsize = 10,
                       x = 0.95, y = 0.85, ha='right', va='top')
            plt.plot( self.dataSets[i].x, self.dataSets[i].y, "b", linewidth = 1)
        #
        # we want the blocking mode, if the script is called from the command line
        # 
        if self.flagPng:
            self.fig.savefig( self.scanName + '.png')
        elif flagStandalone:
            plt.show()
        else:
            self.fig.canvas.flush_events()
            #plt.pause( 1.0)
            #plt.draw()
            
    def _read( self, name):
        ”'
        !
        ! user comments
        !
        %c
        comments
        %p
        parameterName = parameterValue
        %d
        Col 1 AU_ALO_14_0001  FLOAT 
        Col 2 AU_ALO_14_0001  FLOAT 
        Col 3 AU_ALO_14_0001_RING  FLOAT 
        data data data etc.
        ”'
        print( "reading", name)
        try:
            inp = open( name, 'r')
        except IOError as e:
            print( "Failed to open {0}, {1}".format( name, e.strerror) )
            sys.exit(255)
        lines = inp.readlines()
        inp.close()
        flagComment = 0
        flagParameter = 0
        flagData = 0
        for line in lines:
            line = line.strip()
            if line.find( "!") == 0:
                self.user_comments.append( line)
                flagComment = False
                flagParameter = False
                flagData = False
            elif line.find( "%c") == 0:
                flagComment = True
                flagParameter = False
                flagData = False
                continue
            elif line.find( "%p") == 0:
                flagComment = False
                flagParameter = True
                flagData = False
                continue
            elif line.find( "%d") == 0:
                flagComment = False
                flagParameter = False
                flagData = True
                continue
            #
            if flagComment:
                self.comments.append( line)
            #
            # parName = parValue
            #
            if flagParameter:
                lst = line.split( "=")
                self.parameters.append( {lst[0]: lst[1]})
            if not flagData:
                continue
            lst = line.split()
            if lst[0] == "Col":
                #
                # the 'Col 1 ...' description does not create a
                # new FIO_dataset because it contains the x-axis for all
                #
                if lst[1] == "1":
                    pass
                else:
                    self.dataSets.append( fioDataset( lst[2]))
            else:
                for i in range(1, len( self.dataSets)):
                    self.dataSets[i-1].x.append( lst[0])
                    self.dataSets[i-1].y.append( lst[i])

class fioDataset:
    ”'
    the class represents a column of a FIO file. The first column is the
    x-axis which is used by all columns, name_in, e.g. test_00001_C1
    ”'
    def __init__(self, name_in):
        self.name = name_in
        lst = self.name.split('_')
        if len(lst) > 1:
            self.deviceName = lst[-1]
            if self.deviceName.find( "0") == 0:
                self.deviceName = "ScanName"
        else:
            self.deviceName = "n.n."
        self.x = []
        self.y = []
        return

if __name__ == '__main__':
    flagStandalone = True

    usage = "usage: %prog <-p> fileName.fio "
    parser = OptionParser(usage=usage)
    parser.add_option( "-f", action="store", type="string", 
                       dest="fileName", help="name of the .fio file")
    parser.add_option( "-p", action="store_true", dest="flagPng", 
                       default = False, help="create png file")
    
    (options, args) = parser.parse_args()

    if options.fileName is None:
        parser.print_help()
        sys.exit(255)

    if options.flagPng:
        matplotlib.use( 'TkAgg')        
    import matplotlib.pyplot as plt

    a = fioPlotter( options.fileName, options.flagPng)
    a.plot()
else:
    import matplotlib.pyplot as plt



Subsections