Package PyFoam :: Package Applications :: Module RedoPlot
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.RedoPlot

  1  #! /usr/bin/env python 
  2  """A test utility that ghets all the information necessary for plotting from a remote machine and writes some plots<<""" 
  3   
  4  from PyFoam.Applications.PyFoamApplication import PyFoamApplication 
  5   
  6  import xmlrpclib,socket 
  7  import sys 
  8  from optparse import OptionGroup 
  9  import cPickle as pickle 
 10  from time import sleep 
 11   
 12  from PyFoam.Basics.TimeLineCollection import TimeLineCollection,TimeLinesRegistry 
 13  from PyFoam.Basics.PlotTimelinesFactory import createPlotTimelines 
 14  from PyFoam.Basics.GeneralPlotTimelines import PlotLinesRegistry 
 15  from PyFoam.Basics.CustomPlotInfo import CustomPlotInfo 
 16  from PyFoam.Error import error,warning 
 17   
18 -class RedoPlot(PyFoamApplication):
19 - def __init__(self):
20 description="""\ 21 Either connects to a running pyFoam-Server and gets all the 22 information for plotting or reads the relevant data from a pickle file 23 and either displays the plot or writes the plots to file 24 """ 25 PyFoamApplication.__init__(self, 26 description=description, 27 usage="%prog [options] (<host> <port>|<pickleFile>)", 28 interspersed=True, 29 nr=1, 30 exactNr=False)
31 - def addOptions(self):
32 mode=OptionGroup(self.parser, 33 "Input mode", 34 "How we get the data") 35 mode.add_option("--server", 36 dest="server", 37 action="store_true", 38 default=False, 39 help="Get the data from a FoamServer") 40 mode.add_option("--pickle-file", 41 dest="pickle", 42 action="store_true", 43 default=False, 44 help="Get the data from a pickle-file") 45 46 self.parser.add_option_group(mode) 47 48 output=OptionGroup(self.parser, 49 "Output", 50 "Output of the data") 51 output.add_option("--csv-files", 52 dest="csvFiles", 53 action="store_true", 54 default=False, 55 help="Write CSV-files instead of plotting") 56 output.add_option("--file-prefix", 57 dest="filePrefix", 58 default="", 59 help="Prefix to add to the names of the data files") 60 output.add_option("--raw-lines", 61 dest="rawLines", 62 action="store_true", 63 default=False, 64 help="Write the raw line data (not the way it is plotted)") 65 self.parser.add_option_group(output) 66 67 plot=OptionGroup(self.parser, 68 "Plot mode", 69 "How the data should be plotted") 70 71 plot.add_option("--implementation", 72 default="matplotlib", 73 dest="implementation", 74 help="The implementation that should be used for plotting") 75 plot.add_option("--show-window", 76 dest="showWindow", 77 action="store_true", 78 default=False, 79 help="Show the window with the plot") 80 plot.add_option("--no-write-pictures", 81 dest="writePictures", 82 action="store_false", 83 default=True, 84 help="Do not write picture files") 85 plot.add_option("--picture-prefix", 86 dest="prefix", 87 default="", 88 help="Prefix to add to the names of the picture files") 89 plot.add_option("--sleep-time", 90 dest="sleepTime", 91 action="store", 92 default=0.1, 93 type="float", 94 help="How long to wait to allow gnuplot to finish. Default: %default") 95 plot.add_option("--insert-titles", 96 dest="insertTitles", 97 action="store_true", 98 default=False, 99 help="Add the title to the plots") 100 101 self.parser.add_option_group(plot)
102
103 - def run(self):
104 if not self.opts.server and not self.opts.pickle: 105 error("No mode selected") 106 if self.opts.server and self.opts.pickle: 107 error("Both modes selected") 108 109 if self.opts.server: 110 if len(self.parser.getArgs())!=2: 111 error("Need a server and a port to be specified") 112 113 host=self.parser.getArgs()[0] 114 port=int(self.parser.getArgs()[1]) 115 116 try: 117 self.server=xmlrpclib.ServerProxy("http://%s:%d" % (host,port)) 118 methods=self.server.system.listMethods() 119 except socket.error,reason: 120 self.error("Socket error while connecting:",reason) 121 except xmlrpclib.ProtocolError,reason: 122 self.error("XMLRPC-problem",reason) 123 124 plotInfo=self.executeCommand("getPlots()") 125 lineInfo=self.executeCommand("getPlotData()") 126 else: 127 if len(self.parser.getArgs()[0])!=1: 128 warning("Only the first parameter is used") 129 130 fName=self.parser.getArgs()[0] 131 unpick=pickle.Unpickler(open(fName)) 132 133 lineInfo=unpick.load() 134 plotInfo=unpick.load() 135 136 print "Found",len(plotInfo),"plots and",len(lineInfo),"data sets" 137 138 registry=TimeLinesRegistry() 139 for nr,line in lineInfo.iteritems(): 140 print "Adding line",nr 141 TimeLineCollection(preloadData=line,registry=registry) 142 143 registry.resolveSlaves() 144 145 if self.opts.csvFiles and self.opts.rawLines: 146 for k,l in registry.lines.iteritems(): 147 name=str(k) 148 if type(k)==int: 149 name="Line%d" % k 150 name=self.opts.filePrefix+name+".csv" 151 print "Writing",k,"to",name 152 l.getData().writeCSV(name) 153 return 154 155 pRegistry=PlotLinesRegistry() 156 157 for i,p in plotInfo.iteritems(): 158 theId=p["id"] 159 print "Plotting",i,":",theId, 160 spec=CustomPlotInfo(raw=p["spec"]) 161 if len(registry.get(p["data"]).getTimes())>0 and registry.get(p["data"]).getValueNames()>0: 162 if self.opts.csvFiles: 163 registry.get(p["data"]).getData().writeCSV(self.opts.filePrefix+theId+".csv") 164 else: 165 mp=createPlotTimelines(registry.get(p["data"]), 166 spec, 167 implementation=self.opts.implementation, 168 showWindow=self.opts.showWindow, 169 registry=pRegistry) 170 if self.opts.insertTitles: 171 mp.actualSetTitle(p["spec"]["theTitle"]) 172 if self.opts.writePictures: 173 if mp.hasData(): 174 mp.doHardcopy(self.opts.prefix+theId,"png") 175 else: 176 print "has no data", 177 print 178 else: 179 print "No data - skipping" 180 181 sleep(self.opts.sleepTime) # there seems to be a timing issue with Gnuplot
182 183
184 - def executeCommand(self,cmd):
185 result=None 186 try: 187 result=eval("self.server."+cmd) 188 if result==None: # this needed to catch the unmarschalled-None-exception 189 return None 190 except xmlrpclib.Fault,reason: 191 print "XMLRPC-problem:",reason.faultString 192 except socket.error,reason: 193 print "Problem with socket (server propably dead):",reason 194 except TypeError,reason: 195 print "Type error: ",reason 196 result=None 197 except SyntaxError,reason: 198 print "Syntax Error in:",cmd 199 200 return result
201