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

Source Code for Module PyFoam.Basics.GnuplotFile

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