##// END OF EJS Templates
nadaaa
José Chávez -
r914:9d82fa5b564c merge
parent child
Show More
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
@@ -0,0 +1,1
1 =: ERROR: cannot open `=' (No such file or directory)
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
@@ -1,428 +1,426
1
1
2 import os
2 import os
3 import zmq
3 import zmq
4 import time
4 import time
5 import numpy
5 import numpy
6 import datetime
6 import datetime
7 import numpy as np
7 import numpy as np
8 import matplotlib.pyplot as plt
8 import matplotlib.pyplot as plt
9 from mpl_toolkits.axes_grid1 import make_axes_locatable
9 from mpl_toolkits.axes_grid1 import make_axes_locatable
10 from matplotlib.ticker import FuncFormatter, LinearLocator
10 from matplotlib.ticker import FuncFormatter, LinearLocator
11 from multiprocessing import Process
11 from multiprocessing import Process
12
12
13 from schainpy.model.proc.jroproc_base import Operation
13 from schainpy.model.proc.jroproc_base import Operation
14
14
15 #plt.ion()
15 #plt.ion()
16
16
17 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime('%H:%M'))
17 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime('%H:%M'))
18
18
19 d1970 = datetime.datetime(1970,1,1)
19 d1970 = datetime.datetime(1970,1,1)
20
20
21 class PlotData(Operation, Process):
21 class PlotData(Operation, Process):
22
22
23 CODE = 'Figure'
23 CODE = 'Figure'
24 colormap = 'jet'
24 colormap = 'jet'
25 CONFLATE = True
25 CONFLATE = True
26 __MAXNUMX = 80
26 __MAXNUMX = 80
27 __MAXNUMY = 80
27 __MAXNUMY = 80
28 __missing = 1E30
28 __missing = 1E30
29
29
30 def __init__(self, **kwargs):
30 def __init__(self, **kwargs):
31
31
32 Operation.__init__(self, plot=True, **kwargs)
32 Operation.__init__(self, plot=True, **kwargs)
33 Process.__init__(self)
33 Process.__init__(self)
34 self.kwargs['code'] = self.CODE
34 self.kwargs['code'] = self.CODE
35 self.mp = False
35 self.mp = False
36 self.dataOut = None
36 self.dataOut = None
37 self.isConfig = False
37 self.isConfig = False
38 self.figure = None
38 self.figure = None
39 self.axes = []
39 self.axes = []
40 self.localtime = kwargs.pop('localtime', True)
40 self.localtime = kwargs.pop('localtime', True)
41 self.show = kwargs.get('show', True)
41 self.show = kwargs.get('show', True)
42 self.save = kwargs.get('save', False)
42 self.save = kwargs.get('save', False)
43 self.colormap = kwargs.get('colormap', self.colormap)
43 self.colormap = kwargs.get('colormap', self.colormap)
44 self.showprofile = kwargs.get('showprofile', True)
44 self.showprofile = kwargs.get('showprofile', True)
45 self.title = kwargs.get('wintitle', '')
45 self.title = kwargs.get('wintitle', '')
46 self.xaxis = kwargs.get('xaxis', 'time')
46 self.xaxis = kwargs.get('xaxis', 'time')
47 self.zmin = kwargs.get('zmin', None)
47 self.zmin = kwargs.get('zmin', None)
48 self.zmax = kwargs.get('zmax', None)
48 self.zmax = kwargs.get('zmax', None)
49 self.xmin = kwargs.get('xmin', None)
49 self.xmin = kwargs.get('xmin', None)
50 self.xmax = kwargs.get('xmax', None)
50 self.xmax = kwargs.get('xmax', None)
51 self.xrange = kwargs.get('xrange', 24)
51 self.xrange = kwargs.get('xrange', 24)
52 self.ymin = kwargs.get('ymin', None)
52 self.ymin = kwargs.get('ymin', None)
53 self.ymax = kwargs.get('ymax', None)
53 self.ymax = kwargs.get('ymax', None)
54 self.throttle_value = 1
54 self.throttle_value = 1
55 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
55 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
56
56
57 if x_buffer.shape[0] < 2:
57 if x_buffer.shape[0] < 2:
58 return x_buffer, y_buffer, z_buffer
58 return x_buffer, y_buffer, z_buffer
59
59
60 deltas = x_buffer[1:] - x_buffer[0:-1]
60 deltas = x_buffer[1:] - x_buffer[0:-1]
61 x_median = np.median(deltas)
61 x_median = np.median(deltas)
62
62
63 index = np.where(deltas > 5*x_median)
63 index = np.where(deltas > 5*x_median)
64
64
65 if len(index[0]) != 0:
65 if len(index[0]) != 0:
66 z_buffer[::, index[0], ::] = self.__missing
66 z_buffer[::, index[0], ::] = self.__missing
67 z_buffer = np.ma.masked_inside(z_buffer,
67 z_buffer = np.ma.masked_inside(z_buffer,
68 0.99*self.__missing,
68 0.99*self.__missing,
69 1.01*self.__missing)
69 1.01*self.__missing)
70
70
71 return x_buffer, y_buffer, z_buffer
71 return x_buffer, y_buffer, z_buffer
72
72
73 def decimate(self):
73 def decimate(self):
74
74
75 # dx = int(len(self.x)/self.__MAXNUMX) + 1
75 # dx = int(len(self.x)/self.__MAXNUMX) + 1
76 dy = int(len(self.y)/self.__MAXNUMY) + 1
76 dy = int(len(self.y)/self.__MAXNUMY) + 1
77
77
78 # x = self.x[::dx]
78 # x = self.x[::dx]
79 x = self.x
79 x = self.x
80 y = self.y[::dy]
80 y = self.y[::dy]
81 z = self.z[::, ::, ::dy]
81 z = self.z[::, ::, ::dy]
82
82
83 return x, y, z
83 return x, y, z
84
84
85 def __plot(self):
85 def __plot(self):
86
86
87 print 'plotting...{}'.format(self.CODE)
87 print 'plotting...{}'.format(self.CODE)
88
88
89 if self.show:
90 self.figure.show()
91
89 self.plot()
92 self.plot()
90 self.figure.suptitle('{} {} - Date:{}'.format(self.title, self.CODE.upper(),
93 self.figure.suptitle('{} {} - Date:{}'.format(self.title, self.CODE.upper(),
91 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')))
94 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')))
92
95
93 if self.save:
96 if self.save:
94 figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE,
97 figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE,
95 datetime.datetime.utcfromtimestamp(self.times[0]).strftime('%y%m%d_%H%M%S')))
98 datetime.datetime.utcfromtimestamp(self.times[0]).strftime('%y%m%d_%H%M%S')))
96 print 'Saving figure: {}'.format(figname)
99 print 'Saving figure: {}'.format(figname)
97 self.figure.savefig(figname)
100 self.figure.savefig(figname)
98
101
99 self.figure.canvas.draw()
102 self.figure.canvas.draw()
100
103
101 def plot(self):
104 def plot(self):
102
105
103 print 'plotting...{}'.format(self.CODE.upper())
106 print 'plotting...{}'.format(self.CODE.upper())
104 return
107 return
105
108
106 def run(self):
109 def run(self):
107
110
108 print '[Starting] {}'.format(self.name)
111 print '[Starting] {}'.format(self.name)
109 context = zmq.Context()
112 context = zmq.Context()
110 receiver = context.socket(zmq.SUB)
113 receiver = context.socket(zmq.SUB)
111 receiver.setsockopt(zmq.SUBSCRIBE, '')
114 receiver.setsockopt(zmq.SUBSCRIBE, '')
112 receiver.setsockopt(zmq.CONFLATE, self.CONFLATE)
115 receiver.setsockopt(zmq.CONFLATE, self.CONFLATE)
113 receiver.connect("ipc:///tmp/zmq.plots")
116 receiver.connect("ipc:///tmp/zmq.plots")
114
117
115 while True:
118 while True:
116 try:
119 try:
117 #if True:
120 #if True:
118 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)
121 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)
119 self.dataOut = self.data['dataOut']
122 self.dataOut = self.data['dataOut']
120 self.times = self.data['times']
123 self.times = self.data['times']
121 self.times.sort()
124 self.times.sort()
122 self.throttle_value = self.data['throttle']
125 self.throttle_value = self.data['throttle']
123 self.min_time = self.times[0]
126 self.min_time = self.times[0]
124 self.max_time = self.times[-1]
127 self.max_time = self.times[-1]
125
128
126 if self.isConfig is False:
129 if self.isConfig is False:
127 self.setup()
130 self.setup()
128 self.isConfig = True
131 self.isConfig = True
129 self.__plot()
132 self.__plot()
130
133
131 if self.data['ENDED'] is True:
134 if self.data['ENDED'] is True:
132 # self.__plot()
135 # self.__plot()
133 self.isConfig = False
136 self.isConfig = False
134
137
135 except zmq.Again as e:
138 except zmq.Again as e:
136 print 'Waiting for data...'
139 print 'Waiting for data...'
137 plt.pause(self.throttle_value)
140 plt.pause(self.throttle_value)
138 # time.sleep(3)
141 # time.sleep(3)
139
142
140 def close(self):
143 def close(self):
141 if self.dataOut:
144 if self.dataOut:
142 self._plot()
145 self._plot()
143
146
144
147
145 class PlotSpectraData(PlotData):
148 class PlotSpectraData(PlotData):
146
149
147 CODE = 'spc'
150 CODE = 'spc'
148 colormap = 'jro'
151 colormap = 'jro'
149 CONFLATE = False
152 CONFLATE = False
150 def setup(self):
153 def setup(self):
151
154
152 ncolspan = 1
155 ncolspan = 1
153 colspan = 1
156 colspan = 1
154 self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9)
157 self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9)
155 self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9)
158 self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9)
156 self.width = 3.6*self.ncols
159 self.width = 3.6*self.ncols
157 self.height = 3.2*self.nrows
160 self.height = 3.2*self.nrows
158 if self.showprofile:
161 if self.showprofile:
159 ncolspan = 3
162 ncolspan = 3
160 colspan = 2
163 colspan = 2
161 self.width += 1.2*self.ncols
164 self.width += 1.2*self.ncols
162
165
163 self.ylabel = 'Range [Km]'
166 self.ylabel = 'Range [Km]'
164 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
167 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
165
168
166 if self.figure is None:
169 if self.figure is None:
167 self.figure = plt.figure(figsize=(self.width, self.height),
170 self.figure = plt.figure(figsize=(self.width, self.height),
168 edgecolor='k',
171 edgecolor='k',
169 facecolor='w')
172 facecolor='w')
170 else:
173 else:
171 self.figure.clf()
174 self.figure.clf()
172
175
173 n = 0
176 n = 0
174 for y in range(self.nrows):
177 for y in range(self.nrows):
175 for x in range(self.ncols):
178 for x in range(self.ncols):
176 if n >= self.dataOut.nChannels:
179 if n >= self.dataOut.nChannels:
177 break
180 break
178 ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan)
181 ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan)
179 if self.showprofile:
182 if self.showprofile:
180 ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1)
183 ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1)
181
184
182 ax.firsttime = True
185 ax.firsttime = True
183 self.axes.append(ax)
186 self.axes.append(ax)
184 n += 1
187 n += 1
185
188
186 self.figure.subplots_adjust(left=0.1, right=0.95, bottom=0.15, top=0.85, wspace=0.9, hspace=0.5)
189 self.figure.subplots_adjust(left=0.1, right=0.95, bottom=0.15, top=0.85, wspace=0.9, hspace=0.5)
187 self.figure.show()
188
190
189 def plot(self):
191 def plot(self):
190
192
191 if self.xaxis == "frequency":
193 if self.xaxis == "frequency":
192 x = self.dataOut.getFreqRange(1)/1000.
194 x = self.dataOut.getFreqRange(1)/1000.
193 xlabel = "Frequency (kHz)"
195 xlabel = "Frequency (kHz)"
194 elif self.xaxis == "time":
196 elif self.xaxis == "time":
195 x = self.dataOut.getAcfRange(1)
197 x = self.dataOut.getAcfRange(1)
196 xlabel = "Time (ms)"
198 xlabel = "Time (ms)"
197 else:
199 else:
198 x = self.dataOut.getVelRange(1)
200 x = self.dataOut.getVelRange(1)
199 xlabel = "Velocity (m/s)"
201 xlabel = "Velocity (m/s)"
200
202
201 y = self.dataOut.getHeiRange()
203 y = self.dataOut.getHeiRange()
202 z = self.data[self.CODE]
204 z = self.data[self.CODE]
203
205
204 for n, ax in enumerate(self.axes):
206 for n, ax in enumerate(self.axes):
205
207
206 if ax.firsttime:
208 if ax.firsttime:
207 self.xmax = self.xmax if self.xmax else np.nanmax(x)
209 self.xmax = self.xmax if self.xmax else np.nanmax(x)
208 self.xmin = self.xmin if self.xmin else -self.xmax
210 self.xmin = self.xmin if self.xmin else -self.xmax
209 self.ymin = self.ymin if self.ymin else np.nanmin(y)
211 self.ymin = self.ymin if self.ymin else np.nanmin(y)
210 self.ymax = self.ymax if self.ymax else np.nanmax(y)
212 self.ymax = self.ymax if self.ymax else np.nanmax(y)
211 self.zmin = self.zmin if self.zmin else np.nanmin(z)
213 self.zmin = self.zmin if self.zmin else np.nanmin(z)
212 self.zmax = self.zmax if self.zmax else np.nanmax(z)
214 self.zmax = self.zmax if self.zmax else np.nanmax(z)
213 ax.plot = ax.pcolormesh(x, y, z[n].T,
215 ax.plot = ax.pcolormesh(x, y, z[n].T,
214 vmin=self.zmin,
216 vmin=self.zmin,
215 vmax=self.zmax,
217 vmax=self.zmax,
216 cmap=plt.get_cmap(self.colormap)
218 cmap=plt.get_cmap(self.colormap)
217 )
219 )
218 divider = make_axes_locatable(ax)
220 divider = make_axes_locatable(ax)
219 cax = divider.new_horizontal(size='3%', pad=0.05)
221 cax = divider.new_horizontal(size='3%', pad=0.05)
220 self.figure.add_axes(cax)
222 self.figure.add_axes(cax)
221 plt.colorbar(ax.plot, cax)
223 plt.colorbar(ax.plot, cax)
222
224
223 ax.set_xlim(self.xmin, self.xmax)
225 ax.set_xlim(self.xmin, self.xmax)
224 ax.set_ylim(self.ymin, self.ymax)
226 ax.set_ylim(self.ymin, self.ymax)
225
227
226 ax.xaxis.set_major_locator(LinearLocator(5))
228 ax.xaxis.set_major_locator(LinearLocator(5))
227 #ax.yaxis.set_major_locator(LinearLocator(4))
229 #ax.yaxis.set_major_locator(LinearLocator(4))
228
230
229 ax.set_ylabel(self.ylabel)
231 ax.set_ylabel(self.ylabel)
230 ax.set_xlabel(xlabel)
232 ax.set_xlabel(xlabel)
231
233
232 ax.firsttime = False
234 ax.firsttime = False
233
235
234 if self.showprofile:
236 if self.showprofile:
235 ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0]
237 ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0]
236 ax.ax_profile.set_xlim(self.zmin, self.zmax)
238 ax.ax_profile.set_xlim(self.zmin, self.zmax)
237 ax.ax_profile.set_ylim(self.ymin, self.ymax)
239 ax.ax_profile.set_ylim(self.ymin, self.ymax)
238 ax.ax_profile.set_xlabel('dB')
240 ax.ax_profile.set_xlabel('dB')
239 ax.ax_profile.grid(b=True, axis='x')
241 ax.ax_profile.grid(b=True, axis='x')
240 ax.plot_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y,
242 ax.plot_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y,
241 color="k", linestyle="dashed", lw=2)[0]
243 color="k", linestyle="dashed", lw=2)[0]
242 [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()]
244 [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()]
243 else:
245 else:
244 ax.plot.set_array(z[n].T.ravel())
246 ax.plot.set_array(z[n].T.ravel())
245 if self.showprofile:
247 if self.showprofile:
246 ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y)
248 ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y)
247 ax.plot_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y)
249 ax.plot_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y)
248
250
249 ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]),
251 ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]),
250 size=8)
252 size=8)
251
253
252 class PlotRTIData(PlotData):
254 class PlotRTIData(PlotData):
253
255
254 CODE = 'rti'
256 CODE = 'rti'
255 colormap = 'jro'
257 colormap = 'jro'
256
258
257 def setup(self):
259 def setup(self):
258 self.ncols = 1
260 self.ncols = 1
259 self.nrows = self.dataOut.nChannels
261 self.nrows = self.dataOut.nChannels
260 self.width = 10
262 self.width = 10
261 self.height = 2.2*self.nrows
263 self.height = 2.2*self.nrows
262 if self.nrows==1:
264 if self.nrows==1:
263 self.height += 1
265 self.height += 1
264 self.ylabel = 'Range [Km]'
266 self.ylabel = 'Range [Km]'
265 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
267 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
266
268
267 if self.figure is None:
269 if self.figure is None:
268 self.figure = plt.figure(figsize=(self.width, self.height),
270 self.figure = plt.figure(figsize=(self.width, self.height),
269 edgecolor='k',
271 edgecolor='k',
270 facecolor='w')
272 facecolor='w')
271 else:
273 else:
272 self.figure.clf()
274 self.figure.clf()
273 self.axes = []
275 self.axes = []
274
276
275 for n in range(self.nrows):
277 for n in range(self.nrows):
276 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
278 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
277 ax.firsttime = True
279 ax.firsttime = True
278 self.axes.append(ax)
280 self.axes.append(ax)
279 self.figure.subplots_adjust(hspace=0.5)
281 self.figure.subplots_adjust(hspace=0.5)
280 self.figure.show()
281
282
282 def plot(self):
283 def plot(self):
283
284
284 self.x = np.array(self.times)
285 self.x = np.array(self.times)
285 self.y = self.dataOut.getHeiRange()
286 self.y = self.dataOut.getHeiRange()
286 self.z = []
287 self.z = []
287
288
288 for ch in range(self.nrows):
289 for ch in range(self.nrows):
289 self.z.append([self.data[self.CODE][t][ch] for t in self.times])
290 self.z.append([self.data[self.CODE][t][ch] for t in self.times])
290
291
291 self.z = np.array(self.z)
292 self.z = np.array(self.z)
292 for n, ax in enumerate(self.axes):
293 for n, ax in enumerate(self.axes):
293
294
294 x, y, z = self.fill_gaps(*self.decimate())
295 x, y, z = self.fill_gaps(*self.decimate())
295 xmin = self.min_time
296 xmin = self.min_time
296 xmax = xmin+self.xrange*60*60
297 xmax = xmin+self.xrange*60*60
297 if ax.firsttime:
298 if ax.firsttime:
298 self.ymin = self.ymin if self.ymin else np.nanmin(self.y)
299 self.ymin = self.ymin if self.ymin else np.nanmin(self.y)
299 self.ymax = self.ymax if self.ymax else np.nanmax(self.y)
300 self.ymax = self.ymax if self.ymax else np.nanmax(self.y)
300 self.zmin = self.zmin if self.zmin else np.nanmin(self.z)
301 self.zmin = self.zmin if self.zmin else np.nanmin(self.z)
301 self.zmax = self.zmax if self.zmax else np.nanmax(self.z)
302 self.zmax = self.zmax if self.zmax else np.nanmax(self.z)
302 plot = ax.pcolormesh(x, y, z[n].T,
303 plot = ax.pcolormesh(x, y, z[n].T,
303 vmin=self.zmin,
304 vmin=self.zmin,
304 vmax=self.zmax,
305 vmax=self.zmax,
305 cmap=plt.get_cmap(self.colormap)
306 cmap=plt.get_cmap(self.colormap)
306 )
307 )
307 divider = make_axes_locatable(ax)
308 divider = make_axes_locatable(ax)
308 cax = divider.new_horizontal(size='2%', pad=0.05)
309 cax = divider.new_horizontal(size='2%', pad=0.05)
309 self.figure.add_axes(cax)
310 self.figure.add_axes(cax)
310 plt.colorbar(plot, cax)
311 plt.colorbar(plot, cax)
311 ax.set_ylim(self.ymin, self.ymax)
312 ax.set_ylim(self.ymin, self.ymax)
312 if self.xaxis == 'time':
313 if self.xaxis == 'time':
313 ax.xaxis.set_major_formatter(FuncFormatter(func))
314 ax.xaxis.set_major_formatter(FuncFormatter(func))
314 ax.xaxis.set_major_locator(LinearLocator(6))
315 ax.xaxis.set_major_locator(LinearLocator(6))
315
316
316 # ax.yaxis.set_major_locator(LinearLocator(4))
317 # ax.yaxis.set_major_locator(LinearLocator(4))
317
318
318 ax.set_ylabel(self.ylabel)
319 ax.set_ylabel(self.ylabel)
319
320
320 # if self.xmin is None:
321 # if self.xmin is None:
321 # xmin = self.min_time
322 # xmin = self.min_time
322 # else:
323 # else:
323 # xmin = (datetime.datetime.combine(self.dataOut.datatime.date(),
324 # xmin = (datetime.datetime.combine(self.dataOut.datatime.date(),
324 # datetime.time(self.xmin, 0, 0))-d1970).total_seconds()
325 # datetime.time(self.xmin, 0, 0))-d1970).total_seconds()
325
326
326 ax.set_xlim(xmin, xmax)
327 ax.set_xlim(xmin, xmax)
327 ax.firsttime = False
328 ax.firsttime = False
328 else:
329 else:
329 ax.collections.remove(ax.collections[0])
330 ax.collections.remove(ax.collections[0])
330 ax.set_xlim(xmin, xmax)
331 ax.set_xlim(xmin, xmax)
331 plot = ax.pcolormesh(x, y, z[n].T,
332 plot = ax.pcolormesh(x, y, z[n].T,
332 vmin=self.zmin,
333 vmin=self.zmin,
333 vmax=self.zmax,
334 vmax=self.zmax,
334 cmap=plt.get_cmap(self.colormap)
335 cmap=plt.get_cmap(self.colormap)
335 )
336 )
336 ax.set_title('{} {}'.format(self.titles[n],
337 ax.set_title('{} {}'.format(self.titles[n],
337 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
338 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
338 size=8)
339 size=8)
339
340
340
341
341 class PlotCOHData(PlotRTIData):
342 class PlotCOHData(PlotRTIData):
342
343
343 CODE = 'coh'
344 CODE = 'coh'
344
345
345 def setup(self):
346 def setup(self):
346
347
347 self.ncols = 1
348 self.ncols = 1
348 self.nrows = self.dataOut.nPairs
349 self.nrows = self.dataOut.nPairs
349 self.width = 10
350 self.width = 10
350 self.height = 2.2*self.nrows
351 self.height = 2.2*self.nrows
351 if self.nrows==1:
352 if self.nrows==1:
352 self.height += 1
353 self.height += 1
353 self.ylabel = 'Range [Km]'
354 self.ylabel = 'Range [Km]'
354 self.titles = ['Channels {}'.format(x) for x in self.dataOut.pairsList]
355 self.titles = ['Channels {}'.format(x) for x in self.dataOut.pairsList]
355
356
356 if self.figure is None:
357 if self.figure is None:
357 self.figure = plt.figure(figsize=(self.width, self.height),
358 self.figure = plt.figure(figsize=(self.width, self.height),
358 edgecolor='k',
359 edgecolor='k',
359 facecolor='w')
360 facecolor='w')
360 else:
361 else:
361 self.figure.clf()
362 self.figure.clf()
362 self.axes = []
363 self.axes = []
363
364
364 for n in range(self.nrows):
365 for n in range(self.nrows):
365 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
366 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
366 ax.firsttime = True
367 ax.firsttime = True
367 self.axes.append(ax)
368 self.axes.append(ax)
368
369
369 self.figure.subplots_adjust(hspace=0.5)
370 self.figure.subplots_adjust(hspace=0.5)
370 self.figure.show()
371
371
372 class PlotNoiseData(PlotData):
372 class PlotNoiseData(PlotData):
373 CODE = 'noise'
373 CODE = 'noise'
374
374
375 def setup(self):
375 def setup(self):
376
376
377 self.ncols = 1
377 self.ncols = 1
378 self.nrows = 1
378 self.nrows = 1
379 self.width = 10
379 self.width = 10
380 self.height = 3.2
380 self.height = 3.2
381 self.ylabel = 'Intensity [dB]'
381 self.ylabel = 'Intensity [dB]'
382 self.titles = ['Noise']
382 self.titles = ['Noise']
383
383
384 if self.figure is None:
384 if self.figure is None:
385 self.figure = plt.figure(figsize=(self.width, self.height),
385 self.figure = plt.figure(figsize=(self.width, self.height),
386 edgecolor='k',
386 edgecolor='k',
387 facecolor='w')
387 facecolor='w')
388 else:
388 else:
389 self.figure.clf()
389 self.figure.clf()
390 self.axes = []
390 self.axes = []
391
391
392 self.ax = self.figure.add_subplot(self.nrows, self.ncols, 1)
392 self.ax = self.figure.add_subplot(self.nrows, self.ncols, 1)
393 self.ax.firsttime = True
393 self.ax.firsttime = True
394
394
395 self.figure.show()
396
397 def plot(self):
395 def plot(self):
398
396
399 x = self.times
397 x = self.times
400 xmin = self.min_time
398 xmin = self.min_time
401 xmax = xmin+self.xrange*60*60
399 xmax = xmin+self.xrange*60*60
402 if self.ax.firsttime:
400 if self.ax.firsttime:
403 for ch in self.dataOut.channelList:
401 for ch in self.dataOut.channelList:
404 y = [self.data[self.CODE][t][ch] for t in self.times]
402 y = [self.data[self.CODE][t][ch] for t in self.times]
405 self.ax.plot(x, y, lw=1, label='Ch{}'.format(ch))
403 self.ax.plot(x, y, lw=1, label='Ch{}'.format(ch))
406 self.ax.firsttime = False
404 self.ax.firsttime = False
407 self.ax.xaxis.set_major_formatter(FuncFormatter(func))
405 self.ax.xaxis.set_major_formatter(FuncFormatter(func))
408 self.ax.xaxis.set_major_locator(LinearLocator(6))
406 self.ax.xaxis.set_major_locator(LinearLocator(6))
409 self.ax.set_ylabel(self.ylabel)
407 self.ax.set_ylabel(self.ylabel)
410 plt.legend()
408 plt.legend()
411 else:
409 else:
412 for ch in self.dataOut.channelList:
410 for ch in self.dataOut.channelList:
413 y = [self.data[self.CODE][t][ch] for t in self.times]
411 y = [self.data[self.CODE][t][ch] for t in self.times]
414 self.ax.lines[ch].set_data(x, y)
412 self.ax.lines[ch].set_data(x, y)
415
413
416 self.ax.set_xlim(xmin, xmax)
414 self.ax.set_xlim(xmin, xmax)
417 self.ax.set_ylim(min(y)-5, max(y)+5)
415 self.ax.set_ylim(min(y)-5, max(y)+5)
418
416
419 class PlotSNRData(PlotRTIData):
417 class PlotSNRData(PlotRTIData):
420 CODE = 'snr'
418 CODE = 'snr'
421
419
422 class PlotDOPData(PlotRTIData):
420 class PlotDOPData(PlotRTIData):
423 CODE = 'dop'
421 CODE = 'dop'
424 colormap = 'jet'
422 colormap = 'jet'
425
423
426 class PlotPHASEData(PlotCOHData):
424 class PlotPHASEData(PlotCOHData):
427 CODE = 'phase'
425 CODE = 'phase'
428 colormap = 'seismic'
426 colormap = 'seismic'
@@ -1,420 +1,432
1 '''
1 '''
2 @author: Juan C. Espinoza
2 @author: Juan C. Espinoza
3 '''
3 '''
4
4
5 import time
5 import time
6 import json
6 import json
7 import numpy
7 import numpy
8 import paho.mqtt.client as mqtt
8 import paho.mqtt.client as mqtt
9 import zmq
9 import zmq
10 import cPickle as pickle
10 import cPickle as pickle
11 import datetime
11 import datetime
12 from zmq.utils.monitor import recv_monitor_message
12 from zmq.utils.monitor import recv_monitor_message
13 from functools import wraps
13 from functools import wraps
14 from threading import Thread
14 from threading import Thread
15 from multiprocessing import Process
15 from multiprocessing import Process
16
16
17 from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit
17 from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit
18
18
19 MAXNUMX = 100
19 MAXNUMX = 100
20 MAXNUMY = 100
20 MAXNUMY = 100
21
21
22 class PrettyFloat(float):
22 class PrettyFloat(float):
23 def __repr__(self):
23 def __repr__(self):
24 return '%.2f' % self
24 return '%.2f' % self
25
25
26 def roundFloats(obj):
26 def roundFloats(obj):
27 if isinstance(obj, list):
27 if isinstance(obj, list):
28 return map(roundFloats, obj)
28 return map(roundFloats, obj)
29 elif isinstance(obj, float):
29 elif isinstance(obj, float):
30 return round(obj, 2)
30 return round(obj, 2)
31
31
32 def decimate(z):
32 def decimate(z):
33 # dx = int(len(self.x)/self.__MAXNUMX) + 1
33 # dx = int(len(self.x)/self.__MAXNUMX) + 1
34
34 dy = int(len(z[0])/MAXNUMY) + 1
35 dy = int(len(z[0])/MAXNUMY) + 1
36
35 return z[::, ::dy]
37 return z[::, ::dy]
36
38
37 class throttle(object):
39 class throttle(object):
38 """Decorator that prevents a function from being called more than once every
40 """Decorator that prevents a function from being called more than once every
39 time period.
41 time period.
40 To create a function that cannot be called more than once a minute, but
42 To create a function that cannot be called more than once a minute, but
41 will sleep until it can be called:
43 will sleep until it can be called:
42 @throttle(minutes=1)
44 @throttle(minutes=1)
43 def foo():
45 def foo():
44 pass
46 pass
45
47
46 for i in range(10):
48 for i in range(10):
47 foo()
49 foo()
48 print "This function has run %s times." % i
50 print "This function has run %s times." % i
49 """
51 """
50
52
51 def __init__(self, seconds=0, minutes=0, hours=0):
53 def __init__(self, seconds=0, minutes=0, hours=0):
52 self.throttle_period = datetime.timedelta(
54 self.throttle_period = datetime.timedelta(
53 seconds=seconds, minutes=minutes, hours=hours
55 seconds=seconds, minutes=minutes, hours=hours
54 )
56 )
55
57
56 self.time_of_last_call = datetime.datetime.min
58 self.time_of_last_call = datetime.datetime.min
57
59
58 def __call__(self, fn):
60 def __call__(self, fn):
59 @wraps(fn)
61 @wraps(fn)
60 def wrapper(*args, **kwargs):
62 def wrapper(*args, **kwargs):
61 now = datetime.datetime.now()
63 now = datetime.datetime.now()
62 time_since_last_call = now - self.time_of_last_call
64 time_since_last_call = now - self.time_of_last_call
63 time_left = self.throttle_period - time_since_last_call
65 time_left = self.throttle_period - time_since_last_call
64
66
65 if time_left > datetime.timedelta(seconds=0):
67 if time_left > datetime.timedelta(seconds=0):
66 return
68 return
67
69
68 self.time_of_last_call = datetime.datetime.now()
70 self.time_of_last_call = datetime.datetime.now()
69 return fn(*args, **kwargs)
71 return fn(*args, **kwargs)
70
72
71 return wrapper
73 return wrapper
72
74
73
75
74 class PublishData(Operation):
76 class PublishData(Operation):
75 """Clase publish."""
77 """Clase publish."""
76
78
77 def __init__(self, **kwargs):
79 def __init__(self, **kwargs):
78 """Inicio."""
80 """Inicio."""
79 Operation.__init__(self, **kwargs)
81 Operation.__init__(self, **kwargs)
80 self.isConfig = False
82 self.isConfig = False
81 self.client = None
83 self.client = None
82 self.zeromq = None
84 self.zeromq = None
83 self.mqtt = None
85 self.mqtt = None
84
86
85 def on_disconnect(self, client, userdata, rc):
87 def on_disconnect(self, client, userdata, rc):
86 if rc != 0:
88 if rc != 0:
87 print("Unexpected disconnection.")
89 print("Unexpected disconnection.")
88 self.connect()
90 self.connect()
89
91
90 def connect(self):
92 def connect(self):
91 print 'trying to connect'
93 print 'trying to connect'
92 try:
94 try:
93 self.client.connect(
95 self.client.connect(
94 host=self.host,
96 host=self.host,
95 port=self.port,
97 port=self.port,
96 keepalive=60*10,
98 keepalive=60*10,
97 bind_address='')
99 bind_address='')
98 self.client.loop_start()
100 self.client.loop_start()
99 # self.client.publish(
101 # self.client.publish(
100 # self.topic + 'SETUP',
102 # self.topic + 'SETUP',
101 # json.dumps(setup),
103 # json.dumps(setup),
102 # retain=True
104 # retain=True
103 # )
105 # )
104 except:
106 except:
105 print "MQTT Conection error."
107 print "MQTT Conection error."
106 self.client = False
108 self.client = False
107
109
108 def setup(self, port=1883, username=None, password=None, clientId="user", zeromq=1, **kwargs):
110 def setup(self, port=1883, username=None, password=None, clientId="user", zeromq=1, **kwargs):
109 self.counter = 0
111 self.counter = 0
110 self.topic = kwargs.get('topic', 'schain')
112 self.topic = kwargs.get('topic', 'schain')
111 self.delay = kwargs.get('delay', 0)
113 self.delay = kwargs.get('delay', 0)
112 self.plottype = kwargs.get('plottype', 'spectra')
114 self.plottype = kwargs.get('plottype', 'spectra')
113 self.host = kwargs.get('host', "10.10.10.82")
115 self.host = kwargs.get('host', "10.10.10.82")
114 self.port = kwargs.get('port', 3000)
116 self.port = kwargs.get('port', 3000)
115 self.clientId = clientId
117 self.clientId = clientId
116 self.cnt = 0
118 self.cnt = 0
117 self.zeromq = zeromq
119 self.zeromq = zeromq
118 self.mqtt = kwargs.get('plottype', 0)
120 self.mqtt = kwargs.get('plottype', 0)
119 self.client = None
121 self.client = None
120 setup = []
122 setup = []
121 if mqtt is 1:
123 if mqtt is 1:
122 self.client = mqtt.Client(
124 self.client = mqtt.Client(
123 client_id=self.clientId + self.topic + 'SCHAIN',
125 client_id=self.clientId + self.topic + 'SCHAIN',
124 clean_session=True)
126 clean_session=True)
125 self.client.on_disconnect = self.on_disconnect
127 self.client.on_disconnect = self.on_disconnect
126 self.connect()
128 self.connect()
127 for plot in self.plottype:
129 for plot in self.plottype:
128 setup.append({
130 setup.append({
129 'plot': plot,
131 'plot': plot,
130 'topic': self.topic + plot,
132 'topic': self.topic + plot,
131 'title': getattr(self, plot + '_' + 'title', False),
133 'title': getattr(self, plot + '_' + 'title', False),
132 'xlabel': getattr(self, plot + '_' + 'xlabel', False),
134 'xlabel': getattr(self, plot + '_' + 'xlabel', False),
133 'ylabel': getattr(self, plot + '_' + 'ylabel', False),
135 'ylabel': getattr(self, plot + '_' + 'ylabel', False),
134 'xrange': getattr(self, plot + '_' + 'xrange', False),
136 'xrange': getattr(self, plot + '_' + 'xrange', False),
135 'yrange': getattr(self, plot + '_' + 'yrange', False),
137 'yrange': getattr(self, plot + '_' + 'yrange', False),
136 'zrange': getattr(self, plot + '_' + 'zrange', False),
138 'zrange': getattr(self, plot + '_' + 'zrange', False),
137 })
139 })
138 if zeromq is 1:
140 if zeromq is 1:
139 context = zmq.Context()
141 context = zmq.Context()
140 self.zmq_socket = context.socket(zmq.PUSH)
142 self.zmq_socket = context.socket(zmq.PUSH)
141 server = kwargs.get('server', 'zmq.pipe')
143 server = kwargs.get('server', 'zmq.pipe')
142
144
143 if 'tcp://' in server:
145 if 'tcp://' in server:
144 address = server
146 address = server
145 else:
147 else:
146 address = 'ipc:///tmp/%s' % server
148 address = 'ipc:///tmp/%s' % server
147
149
148 self.zmq_socket.connect(address)
150 self.zmq_socket.connect(address)
149 time.sleep(1)
151 time.sleep(1)
150
152
151
152
153 def publish_data(self):
153 def publish_data(self):
154 self.dataOut.finished = False
154 self.dataOut.finished = False
155 if self.mqtt is 1:
155 if self.mqtt is 1:
156 yData = self.dataOut.heightList[:2].tolist()
156 yData = self.dataOut.heightList[:2].tolist()
157 if self.plottype == 'spectra':
157 if self.plottype == 'spectra':
158 data = getattr(self.dataOut, 'data_spc')
158 data = getattr(self.dataOut, 'data_spc')
159 z = data/self.dataOut.normFactor
159 z = data/self.dataOut.normFactor
160 zdB = 10*numpy.log10(z)
160 zdB = 10*numpy.log10(z)
161 xlen, ylen = zdB[0].shape
161 xlen, ylen = zdB[0].shape
162 dx = int(xlen/MAXNUMX) + 1
162 dx = int(xlen/MAXNUMX) + 1
163 dy = int(ylen/MAXNUMY) + 1
163 dy = int(ylen/MAXNUMY) + 1
164 Z = [0 for i in self.dataOut.channelList]
164 Z = [0 for i in self.dataOut.channelList]
165 for i in self.dataOut.channelList:
165 for i in self.dataOut.channelList:
166 Z[i] = zdB[i][::dx, ::dy].tolist()
166 Z[i] = zdB[i][::dx, ::dy].tolist()
167 payload = {
167 payload = {
168 'timestamp': self.dataOut.utctime,
168 'timestamp': self.dataOut.utctime,
169 'data': roundFloats(Z),
169 'data': roundFloats(Z),
170 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
170 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
171 'interval': self.dataOut.getTimeInterval(),
171 'interval': self.dataOut.getTimeInterval(),
172 'type': self.plottype,
172 'type': self.plottype,
173 'yData': yData
173 'yData': yData
174 }
174 }
175 # print payload
175 # print payload
176
176
177 elif self.plottype in ('rti', 'power'):
177 elif self.plottype in ('rti', 'power'):
178 data = getattr(self.dataOut, 'data_spc')
178 data = getattr(self.dataOut, 'data_spc')
179 z = data/self.dataOut.normFactor
179 z = data/self.dataOut.normFactor
180 avg = numpy.average(z, axis=1)
180 avg = numpy.average(z, axis=1)
181 avgdB = 10*numpy.log10(avg)
181 avgdB = 10*numpy.log10(avg)
182 xlen, ylen = z[0].shape
182 xlen, ylen = z[0].shape
183 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
183 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
184 AVG = [0 for i in self.dataOut.channelList]
184 AVG = [0 for i in self.dataOut.channelList]
185 for i in self.dataOut.channelList:
185 for i in self.dataOut.channelList:
186 AVG[i] = avgdB[i][::dy].tolist()
186 AVG[i] = avgdB[i][::dy].tolist()
187 payload = {
187 payload = {
188 'timestamp': self.dataOut.utctime,
188 'timestamp': self.dataOut.utctime,
189 'data': roundFloats(AVG),
189 'data': roundFloats(AVG),
190 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
190 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
191 'interval': self.dataOut.getTimeInterval(),
191 'interval': self.dataOut.getTimeInterval(),
192 'type': self.plottype,
192 'type': self.plottype,
193 'yData': yData
193 'yData': yData
194 }
194 }
195 elif self.plottype == 'noise':
195 elif self.plottype == 'noise':
196 noise = self.dataOut.getNoise()/self.dataOut.normFactor
196 noise = self.dataOut.getNoise()/self.dataOut.normFactor
197 noisedB = 10*numpy.log10(noise)
197 noisedB = 10*numpy.log10(noise)
198 payload = {
198 payload = {
199 'timestamp': self.dataOut.utctime,
199 'timestamp': self.dataOut.utctime,
200 'data': roundFloats(noisedB.reshape(-1, 1).tolist()),
200 'data': roundFloats(noisedB.reshape(-1, 1).tolist()),
201 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
201 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
202 'interval': self.dataOut.getTimeInterval(),
202 'interval': self.dataOut.getTimeInterval(),
203 'type': self.plottype,
203 'type': self.plottype,
204 'yData': yData
204 'yData': yData
205 }
205 }
206 elif self.plottype == 'snr':
206 elif self.plottype == 'snr':
207 data = getattr(self.dataOut, 'data_SNR')
207 data = getattr(self.dataOut, 'data_SNR')
208 avgdB = 10*numpy.log10(data)
208 avgdB = 10*numpy.log10(data)
209
209
210 ylen = data[0].size
210 ylen = data[0].size
211 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
211 dy = numpy.floor(ylen/self.__MAXNUMY) + 1
212 AVG = [0 for i in self.dataOut.channelList]
212 AVG = [0 for i in self.dataOut.channelList]
213 for i in self.dataOut.channelList:
213 for i in self.dataOut.channelList:
214 AVG[i] = avgdB[i][::dy].tolist()
214 AVG[i] = avgdB[i][::dy].tolist()
215 payload = {
215 payload = {
216 'timestamp': self.dataOut.utctime,
216 'timestamp': self.dataOut.utctime,
217 'data': roundFloats(AVG),
217 'data': roundFloats(AVG),
218 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
218 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList],
219 'type': self.plottype,
219 'type': self.plottype,
220 'yData': yData
220 'yData': yData
221 }
221 }
222 else:
222 else:
223 print "Tipo de grafico invalido"
223 print "Tipo de grafico invalido"
224 payload = {
224 payload = {
225 'data': 'None',
225 'data': 'None',
226 'timestamp': 'None',
226 'timestamp': 'None',
227 'type': None
227 'type': None
228 }
228 }
229 # print 'Publishing data to {}'.format(self.host)
229 # print 'Publishing data to {}'.format(self.host)
230 self.client.publish(self.topic + self.plottype, json.dumps(payload), qos=0)
230 self.client.publish(self.topic + self.plottype, json.dumps(payload), qos=0)
231
231
232 if self.zeromq is 1:
232 if self.zeromq is 1:
233 print '[Sending] {} - {}'.format(self.dataOut.type, self.dataOut.datatime)
233 print '[Sending] {} - {}'.format(self.dataOut.type, self.dataOut.datatime)
234 self.zmq_socket.send_pyobj(self.dataOut)
234 self.zmq_socket.send_pyobj(self.dataOut)
235
235
236 def run(self, dataOut, **kwargs):
236 def run(self, dataOut, **kwargs):
237 self.dataOut = dataOut
237 self.dataOut = dataOut
238 if not self.isConfig:
238 if not self.isConfig:
239 self.setup(**kwargs)
239 self.setup(**kwargs)
240 self.isConfig = True
240 self.isConfig = True
241
241
242 self.publish_data()
242 self.publish_data()
243 time.sleep(self.delay)
243 time.sleep(self.delay)
244
244
245 def close(self):
245 def close(self):
246 if self.zeromq is 1:
246 if self.zeromq is 1:
247 self.dataOut.finished = True
247 self.dataOut.finished = True
248 self.zmq_socket.send_pyobj(self.dataOut)
248 self.zmq_socket.send_pyobj(self.dataOut)
249
249
250 if self.client:
250 if self.client:
251 self.client.loop_stop()
251 self.client.loop_stop()
252 self.client.disconnect()
252 self.client.disconnect()
253
253
254
254
255 class ReceiverData(ProcessingUnit, Process):
255 class ReceiverData(ProcessingUnit, Process):
256
256
257 throttle_value = 5
257 throttle_value = 5
258
258
259 def __init__(self, **kwargs):
259 def __init__(self, **kwargs):
260
260
261 ProcessingUnit.__init__(self, **kwargs)
261 ProcessingUnit.__init__(self, **kwargs)
262 Process.__init__(self)
262 Process.__init__(self)
263 self.mp = False
263 self.mp = False
264 self.isConfig = False
264 self.isConfig = False
265 self.isWebConfig = False
265 self.isWebConfig = False
266 self.plottypes =[]
266 self.plottypes =[]
267 self.connections = 0
267 self.connections = 0
268 server = kwargs.get('server', 'zmq.pipe')
268 server = kwargs.get('server', 'zmq.pipe')
269 plot_server = kwargs.get('plot_server', 'zmq.web')
269 plot_server = kwargs.get('plot_server', 'zmq.web')
270 if 'tcp://' in server:
270 if 'tcp://' in server:
271 address = server
271 address = server
272 else:
272 else:
273 address = 'ipc:///tmp/%s' % server
273 address = 'ipc:///tmp/%s' % server
274
274
275 if 'tcp://' in plot_server:
275 if 'tcp://' in plot_server:
276 plot_address = plot_server
276 plot_address = plot_server
277 else:
277 else:
278 plot_address = 'ipc:///tmp/%s' % plot_server
278 plot_address = 'ipc:///tmp/%s' % plot_server
279
279
280 self.address = address
280 self.address = address
281 self.plot_address = plot_address
281 self.plot_address = plot_address
282 self.plottypes = [s.strip() for s in kwargs.get('plottypes', 'rti').split(',')]
282 self.plottypes = [s.strip() for s in kwargs.get('plottypes', 'rti').split(',')]
283 self.realtime = kwargs.get('realtime', False)
283 self.realtime = kwargs.get('realtime', False)
284 self.throttle_value = kwargs.get('throttle', 10)
284 self.throttle_value = kwargs.get('throttle', 10)
285 self.sendData = self.initThrottle(self.throttle_value)
285 self.sendData = self.initThrottle(self.throttle_value)
286 self.setup()
286 self.setup()
287
287
288 def setup(self):
288 def setup(self):
289
289
290 self.data = {}
290 self.data = {}
291 self.data['times'] = []
291 self.data['times'] = []
292 for plottype in self.plottypes:
292 for plottype in self.plottypes:
293 self.data[plottype] = {}
293 self.data[plottype] = {}
294 self.data['noise'] = {}
294 self.data['noise'] = {}
295 self.data['throttle'] = self.throttle_value
295 self.data['throttle'] = self.throttle_value
296 self.data['ENDED'] = False
296 self.data['ENDED'] = False
297 self.isConfig = True
297 self.isConfig = True
298 self.data_web = {}
298 self.data_web = {}
299
299
300 def event_monitor(self, monitor):
300 def event_monitor(self, monitor):
301
301
302 events = {}
302 events = {}
303
303
304 for name in dir(zmq):
304 for name in dir(zmq):
305 if name.startswith('EVENT_'):
305 if name.startswith('EVENT_'):
306 value = getattr(zmq, name)
306 value = getattr(zmq, name)
307 events[value] = name
307 events[value] = name
308
308
309 while monitor.poll():
309 while monitor.poll():
310 evt = recv_monitor_message(monitor)
310 evt = recv_monitor_message(monitor)
311 if evt['event'] == 32:
311 if evt['event'] == 32:
312 self.connections += 1
312 self.connections += 1
313 if evt['event'] == 512:
313 if evt['event'] == 512:
314 pass
314 pass
315 if self.connections == 0 and self.started is True:
315 if self.connections == 0 and self.started is True:
316 self.ended = True
316 self.ended = True
317 # send('ENDED')
317 # send('ENDED')
318 evt.update({'description': events[evt['event']]})
318 evt.update({'description': events[evt['event']]})
319
319
320 if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
320 if evt['event'] == zmq.EVENT_MONITOR_STOPPED:
321 break
321 break
322 monitor.close()
322 monitor.close()
323 print("event monitor thread done!")
323 print("event monitor thread done!")
324
324
325 def initThrottle(self, throttle_value):
325 def initThrottle(self, throttle_value):
326
326
327 @throttle(seconds=throttle_value)
327 @throttle(seconds=throttle_value)
328 def sendDataThrottled(fn_sender, data):
328 def sendDataThrottled(fn_sender, data):
329 fn_sender(data)
329 fn_sender(data)
330
330
331 return sendDataThrottled
331 return sendDataThrottled
332
332
333 def send(self, data):
333 def send(self, data):
334 # print '[sending] data=%s size=%s' % (data.keys(), len(data['times']))
334 # print '[sending] data=%s size=%s' % (data.keys(), len(data['times']))
335 self.sender.send_pyobj(data)
335 self.sender.send_pyobj(data)
336
336
337 def update(self):
337 def update(self):
338 t = self.dataOut.ltctime
338 t = self.dataOut.ltctime
339 self.data['times'].append(t)
339 self.data['times'].append(t)
340 self.data['dataOut'] = self.dataOut
340 self.data['dataOut'] = self.dataOut
341 for plottype in self.plottypes:
341 for plottype in self.plottypes:
342 if plottype == 'spc':
342 if plottype == 'spc':
343 z = self.dataOut.data_spc/self.dataOut.normFactor
343 z = self.dataOut.data_spc/self.dataOut.normFactor
344 self.data[plottype] = 10*numpy.log10(z)
344 self.data[plottype] = 10*numpy.log10(z)
345 self.data['noise'][t] = 10*numpy.log10(self.dataOut.getNoise()/self.dataOut.normFactor)
345 self.data['noise'][t] = 10*numpy.log10(self.dataOut.getNoise()/self.dataOut.normFactor)
346 if plottype == 'rti':
346 if plottype == 'rti':
347 self.data[plottype][t] = self.dataOut.getPower()
347 self.data[plottype][t] = self.dataOut.getPower()
348 if plottype == 'snr':
348 if plottype == 'snr':
349 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_SNR)
349 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_SNR)
350 if plottype == 'dop':
350 if plottype == 'dop':
351 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_DOP)
351 self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_DOP)
352 if plottype == 'coh':
352 if plottype == 'coh':
353 self.data[plottype][t] = self.dataOut.getCoherence()
353 self.data[plottype][t] = self.dataOut.getCoherence()
354 if plottype == 'phase':
354 if plottype == 'phase':
355 self.data[plottype][t] = self.dataOut.getCoherence(phase=True)
355 self.data[plottype][t] = self.dataOut.getCoherence(phase=True)
356 if self.realtime:
356 if self.realtime:
357 <<<<<<< HEAD
357 self.data_web[plottype] = roundFloats(decimate(self.data[plottype][t]).tolist())
358 self.data_web[plottype] = roundFloats(decimate(self.data[plottype][t]).tolist())
358 self.data_web['timestamp'] = t
359 self.data_web['timestamp'] = t
360 =======
361 if plottype == 'spc':
362 self.data_web[plottype] = roundFloats(decimate(self.data[plottype]).tolist())
363 else:
364 self.data_web[plottype] = roundFloats(decimate(self.data[plottype][t]).tolist())
365 self.data_web['time'] = t
366 >>>>>>> f65929d2cf32d4dddb2d5fa2a72f3970d4d51812
359 self.data_web['interval'] = self.dataOut.getTimeInterval()
367 self.data_web['interval'] = self.dataOut.getTimeInterval()
360 self.data_web['type'] = plottype
368 self.data_web['type'] = plottype
369
361 def run(self):
370 def run(self):
362
371
363 print '[Starting] {} from {}'.format(self.name, self.address)
372 print '[Starting] {} from {}'.format(self.name, self.address)
364
373
365 self.context = zmq.Context()
374 self.context = zmq.Context()
366 self.receiver = self.context.socket(zmq.PULL)
375 self.receiver = self.context.socket(zmq.PULL)
367 self.receiver.bind(self.address)
376 self.receiver.bind(self.address)
368 monitor = self.receiver.get_monitor_socket()
377 monitor = self.receiver.get_monitor_socket()
369 self.sender = self.context.socket(zmq.PUB)
378 self.sender = self.context.socket(zmq.PUB)
370 if self.realtime:
379 if self.realtime:
371 self.sender_web = self.context.socket(zmq.PUB)
380 self.sender_web = self.context.socket(zmq.PUB)
372 self.sender_web.bind(self.plot_address)
381 self.sender_web.bind(self.plot_address)
373 self.sender.bind("ipc:///tmp/zmq.plots")
382 self.sender.bind("ipc:///tmp/zmq.plots")
374
383
375 t = Thread(target=self.event_monitor, args=(monitor,))
384 t = Thread(target=self.event_monitor, args=(monitor,))
376 t.start()
385 t.start()
377
386
378 while True:
387 while True:
379 self.dataOut = self.receiver.recv_pyobj()
388 self.dataOut = self.receiver.recv_pyobj()
380 # print '[Receiving] {} - {}'.format(self.dataOut.type,
389 # print '[Receiving] {} - {}'.format(self.dataOut.type,
381 # self.dataOut.datatime.ctime())
390 # self.dataOut.datatime.ctime())
382
391
383 self.update()
392 self.update()
384
393
385 if self.dataOut.finished is True:
394 if self.dataOut.finished is True:
386 self.send(self.data)
395 self.send(self.data)
387 self.connections -= 1
396 self.connections -= 1
388 if self.connections == 0 and self.started:
397 if self.connections == 0 and self.started:
389 self.ended = True
398 self.ended = True
390 self.data['ENDED'] = True
399 self.data['ENDED'] = True
391 self.send(self.data)
400 self.send(self.data)
392 self.setup()
401 self.setup()
393 else:
402 else:
394 if self.realtime:
403 if self.realtime:
395 self.send(self.data)
404 self.send(self.data)
396 self.sender_web.send_string(json.dumps(self.data_web))
405 self.sender_web.send_string(json.dumps(self.data_web))
397 else:
406 else:
398 self.sendData(self.send, self.data)
407 self.sendData(self.send, self.data)
399 self.started = True
408 self.started = True
400
409
401 return
410 return
402
411
403 def sendToWeb(self):
412 def sendToWeb(self):
404
413
405 if not self.isWebConfig:
414 if not self.isWebConfig:
406 context = zmq.Context()
415 context = zmq.Context()
407 sender_web_config = context.socket(zmq.PUB)
416 sender_web_config = context.socket(zmq.PUB)
408 if 'tcp://' in self.plot_address:
417 if 'tcp://' in self.plot_address:
409 print self.plot_address
410 dum, address, port = self.plot_address.split(':')
418 dum, address, port = self.plot_address.split(':')
411 conf_address = '{}:{}:{}'.format(dum, address, int(port)+1)
419 conf_address = '{}:{}:{}'.format(dum, address, int(port)+1)
412 else:
420 else:
413 conf_address = self.plot_address + '.config'
421 conf_address = self.plot_address + '.config'
414 sender_web_config.bind(conf_address)
422 sender_web_config.bind(conf_address)
423 <<<<<<< HEAD
415
424
425 =======
426 time.sleep(1)
427 >>>>>>> f65929d2cf32d4dddb2d5fa2a72f3970d4d51812
416 for kwargs in self.operationKwargs.values():
428 for kwargs in self.operationKwargs.values():
417 if 'plot' in kwargs:
429 if 'plot' in kwargs:
430 print '[Sending] Config data to web for {}'.format(kwargs['code'].upper())
418 sender_web_config.send_string(json.dumps(kwargs))
431 sender_web_config.send_string(json.dumps(kwargs))
419 print kwargs
420 self.isWebConfig = True
432 self.isWebConfig = True
General Comments 0
You need to be logged in to leave comments. Login now