Package PyFoam :: Package Basics :: Module GnuplotFile
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.GnuplotFile

 1  #  ICE Revision: $Id: GnuplotFile.py 7581 2007-06-27 15:29:14Z bgschaid $  
 2  """Analyze a file with GNUPLOT-Data""" 
 3   
 4  from string import strip,split 
 5  import re 
 6   
7 -class GnuplotFile(object):
8 - def __init__(self,fname):
9 """ 10 @param fname: The filename 11 """ 12 self.fname=fname 13 14 self.titles=[] 15 self.analyze()
16
17 - def analyze(self):
18 """ 19 Find out how many columns there are and what their names are 20 21 There are two cases: 22 23 1. the first line is not a comment. In this case the column names are 'time' and 'value0x' 24 depending on how many values are in the first line 25 26 2. the first line is a comment line (it starts with a #). In this case the rest of the line 27 is analyzed and used as names 28 """ 29 fh=open(self.fname) 30 line=fh.readline() 31 fh.close() 32 line=strip(line) 33 34 if line[0]=='#': 35 line=line[1:] 36 exp=re.compile("^\s*((\"[^\"]+\")|(\S+))(.*)$") 37 while exp.match(line)!=None: 38 m=exp.match(line) 39 fnd=m.group(1) 40 line=strip(m.group(4)) 41 if fnd[0]=='\"': 42 fnd=fnd[1:-1] 43 self.titles.append(fnd) 44 else: 45 self.titles=["time"] 46 els=split(line) 47 for i in range(1,len(els)): 48 self.titles.append("value%02d" % i )
49
50 - def writePlotFile(self,name):
51 """ 52 Writes a file that can be used by Gnuplot 53 54 @param name: name of the file 55 """ 56 fh=open(name,'w') 57 58 fh.write("plot ") 59 first=True 60 61 for i in range(1,len(self.titles)): 62 if first: 63 first=False 64 else: 65 fh.write(" , ") 66 67 fh.write(" \"%s\" using 1:%d title \"%s\" with lines " % (self.fname,i+1,self.titles[i])) 68 69 fh.write("\n") 70 fh.close()
71