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

Source Code for Module PyFoam.Basics.GnuplotFile

 1  #  ICE Revision: $Id$ 
 2  """Analyze a file with GNUPLOT-Data""" 
 3   
 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 23 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 #). 27 In this case the rest of the line 28 is analyzed and used as names 29 """ 30 fh=open(self.fname) 31 line=fh.readline() 32 fh.close() 33 line=line.strip() 34 35 if line[0]=='#': 36 line=line[1:] 37 exp=re.compile("^\s*((\"[^\"]+\")|(\S+))(.*)$") 38 while exp.match(line)!=None: 39 m=exp.match(line) 40 fnd=m.group(1) 41 line=m.group(4).strip() 42 if fnd[0]=='\"': 43 fnd=fnd[1:-1] 44 self.titles.append(fnd) 45 else: 46 self.titles=["time"] 47 els=line.split() 48 for i in range(1,len(els)): 49 self.titles.append("value%02d" % i )
50
51 - def writePlotFile(self,name):
52 """ 53 Writes a file that can be used by Gnuplot 54 55 @param name: name of the file 56 """ 57 fh=open(name,'w') 58 59 fh.write("plot ") 60 first=True 61 62 for i in range(1,len(self.titles)): 63 if first: 64 first=False 65 else: 66 fh.write(" , ") 67 68 fh.write(" \"%s\" using 1:%d title \"%s\" with lines " % (self.fname,i+1,self.titles[i])) 69 70 fh.write("\n") 71 fh.close()
72