fioPlotter

The following script displays the contents of a .fio file. Compare the same procedure in matplotlib 9.13.9.

Figure 9.8: pyqtgraph: fioPlotter
\includegraphics[width=22.26cm]{pg_fioPlotter.ps}

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

      $ pg_fioPlotter.py -f fileName.fio

  - ipython and python scripts, non-blocking mode

      import pg_fioPlotter as fp
      a = fp.fioPlotter("<fileName>")
      a.plot()
”'
from optparse import OptionParser
import math, sys
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

app = None
win = None
a = None

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

class fioPlotter:
    def __init__(self, fileName):
        self.comments = []
        self.user_comments = []
        self.parameters = []
        self.dataSets = []
        self.fileName = fileName
        self._read( fileName)
        #
        # /home/p12user/dirName/gen_00001.fio -> gen_00001
        #
        self.scanName = fileName.split("/")[-1].split(".")[0]
        return

    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, flagParameter, flagData = False, False, False
            elif line.find( "%c") == 0:
                flagComment, flagParameter, flagData = True, False, False
                continue
            elif line.find( "%p") == 0:
                flagComment, flagParameter, flagData = False, True, False
                continue
            elif line.find( "%d") == 0:
                flagComment, flagParameter, flagData = False, False, 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( float(lst[0]))
                    self.dataSets[i-1].y.append( float( lst[i]))

    def plot( self):
        ”'
        plots the contents of a fio file
        ”'
        length = len(self.dataSets)
        nCol = int(math.ceil(math.sqrt(length)))
        nRow = int(math.ceil( length/nCol))
        win.clear()
        win.addLabel( self.scanName, row = 1, col = 1, colspan = nCol)
        for i in range( 0, length):
            r = int( math.ceil(i/nCol) + 2)
            c = i % nCol + 1
            plt = win.addPlot( row=r, col=c)
            plt.setContentsMargins( 10, 10, 10, 10)
            plt.showGrid( x = True, y = True)
            plt.setTitle( title = self.dataSets[i].deviceName)
            plt.plot( self.dataSets[i].x, self.dataSets[i].y, pen = (0, 0, 255))

app = QtGui.QApplication.instance()
if app is None:
    pg.setConfigOption( 'background', 'w')
    pg.setConfigOption( 'foreground', 'k')
    app = pg.mkQApp()
    
if win is None:
    win = pg.GraphicsWindow( title="The fioPlotter")
    win.resize( 1000, 800)
win.clear()

if __name__ == '__main__':

    usage = "usage: %prog -f fileName.fio "
    parser = OptionParser(usage=usage)
    parser.add_option( "-f", action="store", type="string", dest="fileName", help="name of the .fio file")
    
    (options, args) = parser.parse_args()

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

    a = fioPlotter( options.fileName)
    a.plot()
    app.exec_()