1
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
20 description="""
21 Either connects to a running pyFoam-Server and gets all the information for
22 plotting or reads the relevant data from a pickle file and either displays
23 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)
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 self.parser.add_option_group(mode)
46 plot=OptionGroup(self.parser,
47 "Plot mode",
48 "How the data should be plotted")
49 plot.add_option("--implementation",
50 default="matplotlib",
51 dest="implementation",
52 help="The implementation that should be used for plotting")
53 plot.add_option("--show-window",
54 dest="showWindow",
55 action="store_true",
56 default=False,
57 help="Show the window with the plot")
58 plot.add_option("--no-write-pictures",
59 dest="writePictures",
60 action="store_false",
61 default=True,
62 help="Do not write picture files")
63 plot.add_option("--picture-prefix",
64 dest="prefix",
65 default="",
66 help="Prefix to add to the names of the picture files")
67 plot.add_option("--sleep-time",
68 dest="sleepTime",
69 action="store",
70 default=0.1,
71 type="float",
72 help="How long to wait to allow gnuplot to finish. Default: %default")
73 plot.add_option("--insert-titles",
74 dest="insertTitles",
75 action="store_true",
76 default=False,
77 help="Add the title to the plots")
78
79 self.parser.add_option_group(plot)
80
82 if not self.opts.server and not self.opts.pickle:
83 error("No mode selected")
84 if self.opts.server and self.opts.pickle:
85 error("Both modes selected")
86
87 if self.opts.server:
88 if len(self.parser.getArgs()[0])!=2:
89 error("Need a server and a port to be specified")
90
91 host=self.parser.getArgs()[0]
92 port=int(self.parser.getArgs()[1])
93
94 try:
95 self.server=xmlrpclib.ServerProxy("http://%s:%d" % (host,port))
96 methods=self.server.system.listMethods()
97 except socket.error,reason:
98 print "Socket error while connecting:",reason
99 sys.exit(1)
100 except xmlrpclib.ProtocolError,reason:
101 print "XMLRPC-problem",reason
102 sys.exit(1)
103
104 plotInfo=self.executeCommand("getPlots()")
105 lineInfo=self.executeCommand("getPlotData()")
106 else:
107 if len(self.parser.getArgs()[0])!=1:
108 warning("Only the first parameter is used")
109
110 fName=self.parser.getArgs()[0]
111 unpick=pickle.Unpickler(open(fName))
112
113 lineInfo=unpick.load()
114 plotInfo=unpick.load()
115
116 print "Found",len(plotInfo),"plots and",len(lineInfo),"data sets"
117
118 registry=TimeLinesRegistry()
119 for nr,line in lineInfo.iteritems():
120 print "Adding line",nr
121 TimeLineCollection(preloadData=line,registry=registry)
122
123 registry.resolveSlaves()
124
125 pRegistry=PlotLinesRegistry()
126
127 for i,p in plotInfo.iteritems():
128 theId=p["id"]
129 print "Plotting",i,":",theId,
130 spec=CustomPlotInfo(raw=p["spec"])
131 if len(registry.get(p["data"]).getTimes())>0 and registry.get(p["data"]).getValueNames()>0:
132 mp=createPlotTimelines(registry.get(p["data"]),
133 spec,
134 implementation=self.opts.implementation,
135 showWindow=self.opts.showWindow,
136 registry=pRegistry)
137 if self.opts.insertTitles:
138 mp.actualSetTitle(p["spec"]["theTitle"])
139 if self.opts.writePictures:
140 if mp.hasData():
141 mp.doHardcopy(self.opts.prefix+theId,"png")
142 else:
143 print "has no data",
144 print
145 else:
146 print "No data - skipping"
147
148 sleep(self.opts.sleepTime)
149
150
152 result=None
153 try:
154 result=eval("self.server."+cmd)
155 if result==None:
156 return None
157 except xmlrpclib.Fault,reason:
158 print "XMLRPC-problem:",reason.faultString
159 except socket.error,reason:
160 print "Problem with socket (server propably dead):",reason
161 except TypeError,reason:
162 print "Type error: ",reason
163 result=None
164 except SyntaxError,reason:
165 print "Syntax Error in:",cmd
166
167 return result
168