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