##// END OF EJS Templates
Udating from v2.3
George Yong -
r1174:0a82420b353b merge
parent child
Show More
@@ -1,1148 +1,1154
1
1
2 import os
2 import os
3 import time
3 import time
4 import glob
4 import glob
5 import datetime
5 import datetime
6 from multiprocessing import Process
6 from multiprocessing import Process
7
7
8 import zmq
8 import zmq
9 import numpy
9 import numpy
10 import matplotlib
10 import matplotlib
11 import matplotlib.pyplot as plt
11 import matplotlib.pyplot as plt
12 from matplotlib.patches import Polygon
12 from matplotlib.patches import Polygon
13 from mpl_toolkits.axes_grid1 import make_axes_locatable
13 from mpl_toolkits.axes_grid1 import make_axes_locatable
14 from matplotlib.ticker import FuncFormatter, LinearLocator, MultipleLocator
14 from matplotlib.ticker import FuncFormatter, LinearLocator, MultipleLocator
15
15
16 from schainpy.model.proc.jroproc_base import Operation
16 from schainpy.model.proc.jroproc_base import Operation
17 from schainpy.utils import log
17 from schainpy.utils import log
18
18
19 jet_values = matplotlib.pyplot.get_cmap('jet', 100)(numpy.arange(100))[10:90]
19 jet_values = matplotlib.pyplot.get_cmap('jet', 100)(numpy.arange(100))[10:90]
20 blu_values = matplotlib.pyplot.get_cmap(
20 blu_values = matplotlib.pyplot.get_cmap(
21 'seismic_r', 20)(numpy.arange(20))[10:15]
21 'seismic_r', 20)(numpy.arange(20))[10:15]
22 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
22 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
23 'jro', numpy.vstack((blu_values, jet_values)))
23 'jro', numpy.vstack((blu_values, jet_values)))
24 matplotlib.pyplot.register_cmap(cmap=ncmap)
24 matplotlib.pyplot.register_cmap(cmap=ncmap)
25
25
26 CMAPS = [plt.get_cmap(s) for s in ('jro', 'jet', 'viridis', 'plasma', 'inferno', 'Greys', 'seismic', 'bwr', 'coolwarm')]
26 CMAPS = [plt.get_cmap(s) for s in ('jro', 'jet', 'viridis', 'plasma', 'inferno', 'Greys', 'seismic', 'bwr', 'coolwarm')]
27
27
28 EARTH_RADIUS = 6.3710e3
28 EARTH_RADIUS = 6.3710e3
29
29
30 def ll2xy(lat1, lon1, lat2, lon2):
30 def ll2xy(lat1, lon1, lat2, lon2):
31
31
32 p = 0.017453292519943295
32 p = 0.017453292519943295
33 a = 0.5 - numpy.cos((lat2 - lat1) * p)/2 + numpy.cos(lat1 * p) * numpy.cos(lat2 * p) * (1 - numpy.cos((lon2 - lon1) * p)) / 2
33 a = 0.5 - numpy.cos((lat2 - lat1) * p)/2 + numpy.cos(lat1 * p) * numpy.cos(lat2 * p) * (1 - numpy.cos((lon2 - lon1) * p)) / 2
34 r = 12742 * numpy.arcsin(numpy.sqrt(a))
34 r = 12742 * numpy.arcsin(numpy.sqrt(a))
35 theta = numpy.arctan2(numpy.sin((lon2-lon1)*p)*numpy.cos(lat2*p), numpy.cos(lat1*p)*numpy.sin(lat2*p)-numpy.sin(lat1*p)*numpy.cos(lat2*p)*numpy.cos((lon2-lon1)*p))
35 theta = numpy.arctan2(numpy.sin((lon2-lon1)*p)*numpy.cos(lat2*p), numpy.cos(lat1*p)*numpy.sin(lat2*p)-numpy.sin(lat1*p)*numpy.cos(lat2*p)*numpy.cos((lon2-lon1)*p))
36 theta = -theta + numpy.pi/2
36 theta = -theta + numpy.pi/2
37 return r*numpy.cos(theta), r*numpy.sin(theta)
37 return r*numpy.cos(theta), r*numpy.sin(theta)
38
38
39 def km2deg(km):
39 def km2deg(km):
40 '''
40 '''
41 Convert distance in km to degrees
41 Convert distance in km to degrees
42 '''
42 '''
43
43
44 return numpy.rad2deg(km/EARTH_RADIUS)
44 return numpy.rad2deg(km/EARTH_RADIUS)
45
45
46 def figpause(interval):
46 def figpause(interval):
47 backend = plt.rcParams['backend']
47 backend = plt.rcParams['backend']
48 if backend in matplotlib.rcsetup.interactive_bk:
48 if backend in matplotlib.rcsetup.interactive_bk:
49 figManager = matplotlib._pylab_helpers.Gcf.get_active()
49 figManager = matplotlib._pylab_helpers.Gcf.get_active()
50 if figManager is not None:
50 if figManager is not None:
51 canvas = figManager.canvas
51 canvas = figManager.canvas
52 if canvas.figure.stale:
52 if canvas.figure.stale:
53 canvas.draw()
53 canvas.draw()
54 try:
54 try:
55 canvas.start_event_loop(interval)
55 canvas.start_event_loop(interval)
56 except:
56 except:
57 pass
57 pass
58 return
58 return
59
59
60 def popup(message):
60 def popup(message):
61 '''
61 '''
62 '''
62 '''
63
63
64 fig = plt.figure(figsize=(12, 8), facecolor='r')
64 fig = plt.figure(figsize=(12, 8), facecolor='r')
65 text = '\n'.join([s.strip() for s in message.split(':')])
65 text = '\n'.join([s.strip() for s in message.split(':')])
66 fig.text(0.01, 0.5, text, ha='left', va='center', size='20', weight='heavy', color='w')
66 fig.text(0.01, 0.5, text, ha='left', va='center', size='20', weight='heavy', color='w')
67 fig.show()
67 fig.show()
68 figpause(1000)
68 figpause(1000)
69
69
70
70
71 class PlotData(Operation, Process):
71 class PlotData(Operation, Process):
72 '''
72 '''
73 Base class for Schain plotting operations
73 Base class for Schain plotting operations
74 '''
74 '''
75
75
76 CODE = 'Figure'
76 CODE = 'Figure'
77 colormap = 'jro'
77 colormap = 'jro'
78 bgcolor = 'white'
78 bgcolor = 'white'
79 CONFLATE = False
79 CONFLATE = False
80 __missing = 1E30
80 __missing = 1E30
81
81
82 __attrs__ = ['show', 'save', 'xmin', 'xmax', 'ymin', 'ymax', 'zmin', 'zmax',
82 __attrs__ = ['show', 'save', 'xmin', 'xmax', 'ymin', 'ymax', 'zmin', 'zmax',
83 'zlimits', 'xlabel', 'ylabel', 'xaxis','cb_label', 'title',
83 'zlimits', 'xlabel', 'ylabel', 'xaxis','cb_label', 'title',
84 'colorbar', 'bgcolor', 'width', 'height', 'localtime', 'oneFigure',
84 'colorbar', 'bgcolor', 'width', 'height', 'localtime', 'oneFigure',
85 'showprofile', 'decimation', 'ftp']
85 'showprofile', 'decimation', 'ftp']
86
86
87 def __init__(self, **kwargs):
87 def __init__(self, **kwargs):
88
88
89 Operation.__init__(self, plot=True, **kwargs)
89 Operation.__init__(self, plot=True, **kwargs)
90 Process.__init__(self)
90 Process.__init__(self)
91
91
92 self.kwargs['code'] = self.CODE
92 self.kwargs['code'] = self.CODE
93 self.mp = False
93 self.mp = False
94 self.data = None
94 self.data = None
95 self.isConfig = False
95 self.isConfig = False
96 self.figures = []
96 self.figures = []
97 self.axes = []
97 self.axes = []
98 self.cb_axes = []
98 self.cb_axes = []
99 self.localtime = kwargs.pop('localtime', True)
99 self.localtime = kwargs.pop('localtime', True)
100 self.show = kwargs.get('show', True)
100 self.show = kwargs.get('show', True)
101 self.save = kwargs.get('save', False)
101 self.save = kwargs.get('save', False)
102 self.ftp = kwargs.get('ftp', False)
102 self.ftp = kwargs.get('ftp', False)
103 self.colormap = kwargs.get('colormap', self.colormap)
103 self.colormap = kwargs.get('colormap', self.colormap)
104 self.colormap_coh = kwargs.get('colormap_coh', 'jet')
104 self.colormap_coh = kwargs.get('colormap_coh', 'jet')
105 self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r')
105 self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r')
106 self.colormaps = kwargs.get('colormaps', None)
106 self.colormaps = kwargs.get('colormaps', None)
107 self.bgcolor = kwargs.get('bgcolor', self.bgcolor)
107 self.bgcolor = kwargs.get('bgcolor', self.bgcolor)
108 self.showprofile = kwargs.get('showprofile', False)
108 self.showprofile = kwargs.get('showprofile', False)
109 self.title = kwargs.get('wintitle', self.CODE.upper())
109 self.title = kwargs.get('wintitle', self.CODE.upper())
110 self.cb_label = kwargs.get('cb_label', None)
110 self.cb_label = kwargs.get('cb_label', None)
111 self.cb_labels = kwargs.get('cb_labels', None)
111 self.cb_labels = kwargs.get('cb_labels', None)
112 self.labels = kwargs.get('labels', None)
112 self.labels = kwargs.get('labels', None)
113 self.xaxis = kwargs.get('xaxis', 'frequency')
113 self.xaxis = kwargs.get('xaxis', 'frequency')
114 self.zmin = kwargs.get('zmin', None)
114 self.zmin = kwargs.get('zmin', None)
115 self.zmax = kwargs.get('zmax', None)
115 self.zmax = kwargs.get('zmax', None)
116 self.zlimits = kwargs.get('zlimits', None)
116 self.zlimits = kwargs.get('zlimits', None)
117 self.xmin = kwargs.get('xmin', None)
117 self.xmin = kwargs.get('xmin', None)
118 self.xmax = kwargs.get('xmax', None)
118 self.xmax = kwargs.get('xmax', None)
119 self.xrange = kwargs.get('xrange', 24)
119 self.xrange = kwargs.get('xrange', 24)
120 self.xscale = kwargs.get('xscale', None)
120 self.xscale = kwargs.get('xscale', None)
121 self.ymin = kwargs.get('ymin', None)
121 self.ymin = kwargs.get('ymin', None)
122 self.ymax = kwargs.get('ymax', None)
122 self.ymax = kwargs.get('ymax', None)
123 self.yscale = kwargs.get('yscale', None)
123 self.yscale = kwargs.get('yscale', None)
124 self.xlabel = kwargs.get('xlabel', None)
124 self.xlabel = kwargs.get('xlabel', None)
125 self.decimation = kwargs.get('decimation', None)
125 self.decimation = kwargs.get('decimation', None)
126 self.showSNR = kwargs.get('showSNR', False)
126 self.showSNR = kwargs.get('showSNR', False)
127 self.oneFigure = kwargs.get('oneFigure', True)
127 self.oneFigure = kwargs.get('oneFigure', True)
128 self.width = kwargs.get('width', None)
128 self.width = kwargs.get('width', None)
129 self.height = kwargs.get('height', None)
129 self.height = kwargs.get('height', None)
130 self.colorbar = kwargs.get('colorbar', True)
130 self.colorbar = kwargs.get('colorbar', True)
131 self.factors = kwargs.get('factors', [1, 1, 1, 1, 1, 1, 1, 1])
131 self.factors = kwargs.get('factors', [1, 1, 1, 1, 1, 1, 1, 1])
132 self.channels = kwargs.get('channels', None)
132 self.channels = kwargs.get('channels', None)
133 self.titles = kwargs.get('titles', [])
133 self.titles = kwargs.get('titles', [])
134 self.polar = False
134 self.polar = False
135 self.grid = kwargs.get('grid', False)
135 self.grid = kwargs.get('grid', False)
136
136
137 def __fmtTime(self, x, pos):
137 def __fmtTime(self, x, pos):
138 '''
138 '''
139 '''
139 '''
140
140
141 return '{}'.format(self.getDateTime(x).strftime('%H:%M'))
141 return '{}'.format(self.getDateTime(x).strftime('%H:%M'))
142
142
143 def __setup(self):
143 def __setup(self):
144 '''
144 '''
145 Common setup for all figures, here figures and axes are created
145 Common setup for all figures, here figures and axes are created
146 '''
146 '''
147
147
148 if self.CODE not in self.data:
148 if self.CODE not in self.data:
149 raise ValueError(log.error('Missing data for {}'.format(self.CODE),
149 raise ValueError(log.error('Missing data for {}'.format(self.CODE),
150 self.name))
150 self.name))
151
151
152 self.setup()
152 self.setup()
153
153
154 self.time_label = 'LT' if self.localtime else 'UTC'
154 self.time_label = 'LT' if self.localtime else 'UTC'
155 if self.data.localtime:
155 if self.data.localtime:
156 self.getDateTime = datetime.datetime.fromtimestamp
156 self.getDateTime = datetime.datetime.fromtimestamp
157 else:
157 else:
158 self.getDateTime = datetime.datetime.utcfromtimestamp
158 self.getDateTime = datetime.datetime.utcfromtimestamp
159
159
160 if self.width is None:
160 if self.width is None:
161 self.width = 8
161 self.width = 8
162
162
163 self.figures = []
163 self.figures = []
164 self.axes = []
164 self.axes = []
165 self.cb_axes = []
165 self.cb_axes = []
166 self.pf_axes = []
166 self.pf_axes = []
167 self.cmaps = []
167 self.cmaps = []
168
168
169 size = '15%' if self.ncols == 1 else '30%'
169 size = '15%' if self.ncols == 1 else '30%'
170 pad = '4%' if self.ncols == 1 else '8%'
170 pad = '4%' if self.ncols == 1 else '8%'
171
171
172 if self.oneFigure:
172 if self.oneFigure:
173 if self.height is None:
173 if self.height is None:
174 self.height = 1.4 * self.nrows + 1
174 self.height = 1.4 * self.nrows + 1
175 fig = plt.figure(figsize=(self.width, self.height),
175 fig = plt.figure(figsize=(self.width, self.height),
176 edgecolor='k',
176 edgecolor='k',
177 facecolor='w')
177 facecolor='w')
178 self.figures.append(fig)
178 self.figures.append(fig)
179 for n in range(self.nplots):
179 for n in range(self.nplots):
180 ax = fig.add_subplot(self.nrows, self.ncols,
180 ax = fig.add_subplot(self.nrows, self.ncols,
181 n + 1, polar=self.polar)
181 n + 1, polar=self.polar)
182 ax.tick_params(labelsize=8)
182 ax.tick_params(labelsize=8)
183 ax.firsttime = True
183 ax.firsttime = True
184 ax.index = 0
184 ax.index = 0
185 ax.press = None
185 ax.press = None
186 self.axes.append(ax)
186 self.axes.append(ax)
187 if self.showprofile:
187 if self.showprofile:
188 cax = self.__add_axes(ax, size=size, pad=pad)
188 cax = self.__add_axes(ax, size=size, pad=pad)
189 cax.tick_params(labelsize=8)
189 cax.tick_params(labelsize=8)
190 self.pf_axes.append(cax)
190 self.pf_axes.append(cax)
191 else:
191 else:
192 if self.height is None:
192 if self.height is None:
193 self.height = 3
193 self.height = 3
194 for n in range(self.nplots):
194 for n in range(self.nplots):
195 fig = plt.figure(figsize=(self.width, self.height),
195 fig = plt.figure(figsize=(self.width, self.height),
196 edgecolor='k',
196 edgecolor='k',
197 facecolor='w')
197 facecolor='w')
198 ax = fig.add_subplot(1, 1, 1, polar=self.polar)
198 ax = fig.add_subplot(1, 1, 1, polar=self.polar)
199 ax.tick_params(labelsize=8)
199 ax.tick_params(labelsize=8)
200 ax.firsttime = True
200 ax.firsttime = True
201 ax.index = 0
201 ax.index = 0
202 ax.press = None
202 ax.press = None
203 self.figures.append(fig)
203 self.figures.append(fig)
204 self.axes.append(ax)
204 self.axes.append(ax)
205 if self.showprofile:
205 if self.showprofile:
206 cax = self.__add_axes(ax, size=size, pad=pad)
206 cax = self.__add_axes(ax, size=size, pad=pad)
207 cax.tick_params(labelsize=8)
207 cax.tick_params(labelsize=8)
208 self.pf_axes.append(cax)
208 self.pf_axes.append(cax)
209
209
210 for n in range(self.nrows):
210 for n in range(self.nrows):
211 if self.colormaps is not None:
211 if self.colormaps is not None:
212 cmap = plt.get_cmap(self.colormaps[n])
212 cmap = plt.get_cmap(self.colormaps[n])
213 else:
213 else:
214 cmap = plt.get_cmap(self.colormap)
214 cmap = plt.get_cmap(self.colormap)
215 cmap.set_bad(self.bgcolor, 1.)
215 cmap.set_bad(self.bgcolor, 1.)
216 self.cmaps.append(cmap)
216 self.cmaps.append(cmap)
217
217
218 for fig in self.figures:
218 for fig in self.figures:
219 fig.canvas.mpl_connect('key_press_event', self.OnKeyPress)
219 fig.canvas.mpl_connect('key_press_event', self.OnKeyPress)
220 fig.canvas.mpl_connect('scroll_event', self.OnBtnScroll)
220 fig.canvas.mpl_connect('scroll_event', self.OnBtnScroll)
221 fig.canvas.mpl_connect('button_press_event', self.onBtnPress)
221 fig.canvas.mpl_connect('button_press_event', self.onBtnPress)
222 fig.canvas.mpl_connect('motion_notify_event', self.onMotion)
222 fig.canvas.mpl_connect('motion_notify_event', self.onMotion)
223 fig.canvas.mpl_connect('button_release_event', self.onBtnRelease)
223 fig.canvas.mpl_connect('button_release_event', self.onBtnRelease)
224 if self.show:
224 if self.show:
225 fig.show()
225 fig.show()
226
226
227 def OnKeyPress(self, event):
227 def OnKeyPress(self, event):
228 '''
228 '''
229 Event for pressing keys (up, down) change colormap
229 Event for pressing keys (up, down) change colormap
230 '''
230 '''
231 ax = event.inaxes
231 ax = event.inaxes
232 if ax in self.axes:
232 if ax in self.axes:
233 if event.key == 'down':
233 if event.key == 'down':
234 ax.index += 1
234 ax.index += 1
235 elif event.key == 'up':
235 elif event.key == 'up':
236 ax.index -= 1
236 ax.index -= 1
237 if ax.index < 0:
237 if ax.index < 0:
238 ax.index = len(CMAPS) - 1
238 ax.index = len(CMAPS) - 1
239 elif ax.index == len(CMAPS):
239 elif ax.index == len(CMAPS):
240 ax.index = 0
240 ax.index = 0
241 cmap = CMAPS[ax.index]
241 cmap = CMAPS[ax.index]
242 ax.cbar.set_cmap(cmap)
242 ax.cbar.set_cmap(cmap)
243 ax.cbar.draw_all()
243 ax.cbar.draw_all()
244 ax.plt.set_cmap(cmap)
244 ax.plt.set_cmap(cmap)
245 ax.cbar.patch.figure.canvas.draw()
245 ax.cbar.patch.figure.canvas.draw()
246 self.colormap = cmap.name
246 self.colormap = cmap.name
247
247
248 def OnBtnScroll(self, event):
248 def OnBtnScroll(self, event):
249 '''
249 '''
250 Event for scrolling, scale figure
250 Event for scrolling, scale figure
251 '''
251 '''
252 cb_ax = event.inaxes
252 cb_ax = event.inaxes
253 if cb_ax in [ax.cbar.ax for ax in self.axes if ax.cbar]:
253 if cb_ax in [ax.cbar.ax for ax in self.axes if ax.cbar]:
254 ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0]
254 ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0]
255 pt = ax.cbar.ax.bbox.get_points()[:, 1]
255 pt = ax.cbar.ax.bbox.get_points()[:, 1]
256 nrm = ax.cbar.norm
256 nrm = ax.cbar.norm
257 vmin, vmax, p0, p1, pS = (
257 vmin, vmax, p0, p1, pS = (
258 nrm.vmin, nrm.vmax, pt[0], pt[1], event.y)
258 nrm.vmin, nrm.vmax, pt[0], pt[1], event.y)
259 scale = 2 if event.step == 1 else 0.5
259 scale = 2 if event.step == 1 else 0.5
260 point = vmin + (vmax - vmin) / (p1 - p0) * (pS - p0)
260 point = vmin + (vmax - vmin) / (p1 - p0) * (pS - p0)
261 ax.cbar.norm.vmin = point - scale * (point - vmin)
261 ax.cbar.norm.vmin = point - scale * (point - vmin)
262 ax.cbar.norm.vmax = point - scale * (point - vmax)
262 ax.cbar.norm.vmax = point - scale * (point - vmax)
263 ax.plt.set_norm(ax.cbar.norm)
263 ax.plt.set_norm(ax.cbar.norm)
264 ax.cbar.draw_all()
264 ax.cbar.draw_all()
265 ax.cbar.patch.figure.canvas.draw()
265 ax.cbar.patch.figure.canvas.draw()
266
266
267 def onBtnPress(self, event):
267 def onBtnPress(self, event):
268 '''
268 '''
269 Event for mouse button press
269 Event for mouse button press
270 '''
270 '''
271 cb_ax = event.inaxes
271 cb_ax = event.inaxes
272 if cb_ax is None:
272 if cb_ax is None:
273 return
273 return
274
274
275 if cb_ax in [ax.cbar.ax for ax in self.axes if ax.cbar]:
275 if cb_ax in [ax.cbar.ax for ax in self.axes if ax.cbar]:
276 cb_ax.press = event.x, event.y
276 cb_ax.press = event.x, event.y
277 else:
277 else:
278 cb_ax.press = None
278 cb_ax.press = None
279
279
280 def onMotion(self, event):
280 def onMotion(self, event):
281 '''
281 '''
282 Event for move inside colorbar
282 Event for move inside colorbar
283 '''
283 '''
284 cb_ax = event.inaxes
284 cb_ax = event.inaxes
285 if cb_ax is None:
285 if cb_ax is None:
286 return
286 return
287 if cb_ax not in [ax.cbar.ax for ax in self.axes if ax.cbar]:
287 if cb_ax not in [ax.cbar.ax for ax in self.axes if ax.cbar]:
288 return
288 return
289 if cb_ax.press is None:
289 if cb_ax.press is None:
290 return
290 return
291
291
292 ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0]
292 ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0]
293 xprev, yprev = cb_ax.press
293 xprev, yprev = cb_ax.press
294 dx = event.x - xprev
294 dx = event.x - xprev
295 dy = event.y - yprev
295 dy = event.y - yprev
296 cb_ax.press = event.x, event.y
296 cb_ax.press = event.x, event.y
297 scale = ax.cbar.norm.vmax - ax.cbar.norm.vmin
297 scale = ax.cbar.norm.vmax - ax.cbar.norm.vmin
298 perc = 0.03
298 perc = 0.03
299
299
300 if event.button == 1:
300 if event.button == 1:
301 ax.cbar.norm.vmin -= (perc * scale) * numpy.sign(dy)
301 ax.cbar.norm.vmin -= (perc * scale) * numpy.sign(dy)
302 ax.cbar.norm.vmax -= (perc * scale) * numpy.sign(dy)
302 ax.cbar.norm.vmax -= (perc * scale) * numpy.sign(dy)
303 elif event.button == 3:
303 elif event.button == 3:
304 ax.cbar.norm.vmin -= (perc * scale) * numpy.sign(dy)
304 ax.cbar.norm.vmin -= (perc * scale) * numpy.sign(dy)
305 ax.cbar.norm.vmax += (perc * scale) * numpy.sign(dy)
305 ax.cbar.norm.vmax += (perc * scale) * numpy.sign(dy)
306
306
307 ax.cbar.draw_all()
307 ax.cbar.draw_all()
308 ax.plt.set_norm(ax.cbar.norm)
308 ax.plt.set_norm(ax.cbar.norm)
309 ax.cbar.patch.figure.canvas.draw()
309 ax.cbar.patch.figure.canvas.draw()
310
310
311 def onBtnRelease(self, event):
311 def onBtnRelease(self, event):
312 '''
312 '''
313 Event for mouse button release
313 Event for mouse button release
314 '''
314 '''
315 cb_ax = event.inaxes
315 cb_ax = event.inaxes
316 if cb_ax is not None:
316 if cb_ax is not None:
317 cb_ax.press = None
317 cb_ax.press = None
318
318
319 def __add_axes(self, ax, size='30%', pad='8%'):
319 def __add_axes(self, ax, size='30%', pad='8%'):
320 '''
320 '''
321 Add new axes to the given figure
321 Add new axes to the given figure
322 '''
322 '''
323 divider = make_axes_locatable(ax)
323 divider = make_axes_locatable(ax)
324 nax = divider.new_horizontal(size=size, pad=pad)
324 nax = divider.new_horizontal(size=size, pad=pad)
325 ax.figure.add_axes(nax)
325 ax.figure.add_axes(nax)
326 return nax
326 return nax
327
327
328 self.setup()
328 self.setup()
329
329
330 def setup(self):
330 def setup(self):
331 '''
331 '''
332 This method should be implemented in the child class, the following
332 This method should be implemented in the child class, the following
333 attributes should be set:
333 attributes should be set:
334
334
335 self.nrows: number of rows
335 self.nrows: number of rows
336 self.ncols: number of cols
336 self.ncols: number of cols
337 self.nplots: number of plots (channels or pairs)
337 self.nplots: number of plots (channels or pairs)
338 self.ylabel: label for Y axes
338 self.ylabel: label for Y axes
339 self.titles: list of axes title
339 self.titles: list of axes title
340
340
341 '''
341 '''
342 raise NotImplementedError
342 raise NotImplementedError
343
343
344 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
344 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
345 '''
345 '''
346 Create a masked array for missing data
346 Create a masked array for missing data
347 '''
347 '''
348 if x_buffer.shape[0] < 2:
348 if x_buffer.shape[0] < 2:
349 return x_buffer, y_buffer, z_buffer
349 return x_buffer, y_buffer, z_buffer
350
350
351 deltas = x_buffer[1:] - x_buffer[0:-1]
351 deltas = x_buffer[1:] - x_buffer[0:-1]
352 x_median = numpy.median(deltas)
352 x_median = numpy.median(deltas)
353
353
354 index = numpy.where(deltas > 5 * x_median)
354 index = numpy.where(deltas > 5 * x_median)
355
355
356 if len(index[0]) != 0:
356 if len(index[0]) != 0:
357 z_buffer[::, index[0], ::] = self.__missing
357 z_buffer[::, index[0], ::] = self.__missing
358 z_buffer = numpy.ma.masked_inside(z_buffer,
358 z_buffer = numpy.ma.masked_inside(z_buffer,
359 0.99 * self.__missing,
359 0.99 * self.__missing,
360 1.01 * self.__missing)
360 1.01 * self.__missing)
361
361
362 return x_buffer, y_buffer, z_buffer
362 return x_buffer, y_buffer, z_buffer
363
363
364 def decimate(self):
364 def decimate(self):
365
365
366 # dx = int(len(self.x)/self.__MAXNUMX) + 1
366 # dx = int(len(self.x)/self.__MAXNUMX) + 1
367 dy = int(len(self.y) / self.decimation) + 1
367 dy = int(len(self.y) / self.decimation) + 1
368
368
369 # x = self.x[::dx]
369 # x = self.x[::dx]
370 x = self.x
370 x = self.x
371 y = self.y[::dy]
371 y = self.y[::dy]
372 z = self.z[::, ::, ::dy]
372 z = self.z[::, ::, ::dy]
373
373
374 return x, y, z
374 return x, y, z
375
375
376 def format(self):
376 def format(self):
377 '''
377 '''
378 Set min and max values, labels, ticks and titles
378 Set min and max values, labels, ticks and titles
379 '''
379 '''
380
380
381 if self.xmin is None:
381 if self.xmin is None:
382 xmin = self.min_time
382 xmin = self.min_time
383 else:
383 else:
384 if self.xaxis is 'time':
384 if self.xaxis is 'time':
385 dt = self.getDateTime(self.min_time)
385 dt = self.getDateTime(self.min_time)
386 xmin = (dt.replace(hour=int(self.xmin), minute=0, second=0) -
386 xmin = (dt.replace(hour=int(self.xmin), minute=0, second=0) -
387 datetime.datetime(1970, 1, 1)).total_seconds()
387 datetime.datetime(1970, 1, 1)).total_seconds()
388 if self.data.localtime:
388 if self.data.localtime:
389 xmin += time.timezone
389 xmin += time.timezone
390 else:
390 else:
391 xmin = self.xmin
391 xmin = self.xmin
392
392
393 if self.xmax is None:
393 if self.xmax is None:
394 xmax = xmin + self.xrange * 60 * 60
394 xmax = xmin + self.xrange * 60 * 60
395 else:
395 else:
396 if self.xaxis is 'time':
396 if self.xaxis is 'time':
397 dt = self.getDateTime(self.max_time)
397 dt = self.getDateTime(self.max_time)
398 xmax = (dt.replace(hour=int(self.xmax), minute=59, second=59) -
398 xmax = (dt.replace(hour=int(self.xmax), minute=59, second=59) -
399 datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=1)).total_seconds()
399 datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=1)).total_seconds()
400 if self.data.localtime:
400 if self.data.localtime:
401 xmax += time.timezone
401 xmax += time.timezone
402 else:
402 else:
403 xmax = self.xmax
403 xmax = self.xmax
404
404
405 ymin = self.ymin if self.ymin else numpy.nanmin(self.y)
405 ymin = self.ymin if self.ymin else numpy.nanmin(self.y)
406 ymax = self.ymax if self.ymax else numpy.nanmax(self.y)
406 ymax = self.ymax if self.ymax else numpy.nanmax(self.y)
407
407
408 Y = numpy.array([1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000])
408 Y = numpy.array([1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000])
409 i = 1 if numpy.where(abs(ymax-ymin) <= Y)[0][0] < 0 else numpy.where(abs(ymax-ymin) <= Y)[0][0]
409 i = 1 if numpy.where(abs(ymax-ymin) <= Y)[0][0] < 0 else numpy.where(abs(ymax-ymin) <= Y)[0][0]
410 ystep = Y[i] / 10.
410 ystep = Y[i] / 10.
411
411
412 if self.xaxis is not 'time':
413 X = numpy.array([1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000])/2.
414 i = 1 if numpy.where(abs(xmax-xmin) <= X)[0][0] < 0 else numpy.where(abs(xmax-xmin) <= X)[0][0]
415 xstep = X[i] / 10.
416
412 for n, ax in enumerate(self.axes):
417 for n, ax in enumerate(self.axes):
413 if ax.firsttime:
418 if ax.firsttime:
414 ax.set_facecolor(self.bgcolor)
419 ax.set_facecolor(self.bgcolor)
415 ax.yaxis.set_major_locator(MultipleLocator(ystep))
420 ax.yaxis.set_major_locator(MultipleLocator(ystep))
416 ax.xaxis.set_major_locator(MultipleLocator(ystep))
417 if self.xscale:
421 if self.xscale:
418 ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{0:g}'.format(x*self.xscale)))
422 ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{0:g}'.format(x*self.xscale)))
419 if self.xscale:
423 if self.xscale:
420 ax.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{0:g}'.format(x*self.yscale)))
424 ax.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{0:g}'.format(x*self.yscale)))
421 if self.xaxis is 'time':
425 if self.xaxis is 'time':
422 ax.xaxis.set_major_formatter(FuncFormatter(self.__fmtTime))
426 ax.xaxis.set_major_formatter(FuncFormatter(self.__fmtTime))
423 ax.xaxis.set_major_locator(LinearLocator(9))
427 ax.xaxis.set_major_locator(LinearLocator(9))
428 else:
429 ax.xaxis.set_major_locator(MultipleLocator(xstep))
424 if self.xlabel is not None:
430 if self.xlabel is not None:
425 ax.set_xlabel(self.xlabel)
431 ax.set_xlabel(self.xlabel)
426 ax.set_ylabel(self.ylabel)
432 ax.set_ylabel(self.ylabel)
427 ax.firsttime = False
433 ax.firsttime = False
428 if self.showprofile:
434 if self.showprofile:
429 self.pf_axes[n].set_ylim(ymin, ymax)
435 self.pf_axes[n].set_ylim(ymin, ymax)
430 self.pf_axes[n].set_xlim(self.zmin, self.zmax)
436 self.pf_axes[n].set_xlim(self.zmin, self.zmax)
431 self.pf_axes[n].set_xlabel('dB')
437 self.pf_axes[n].set_xlabel('dB')
432 self.pf_axes[n].grid(b=True, axis='x')
438 self.pf_axes[n].grid(b=True, axis='x')
433 [tick.set_visible(False)
439 [tick.set_visible(False)
434 for tick in self.pf_axes[n].get_yticklabels()]
440 for tick in self.pf_axes[n].get_yticklabels()]
435 if self.colorbar:
441 if self.colorbar:
436 ax.cbar = plt.colorbar(
442 ax.cbar = plt.colorbar(
437 ax.plt, ax=ax, fraction=0.05, pad=0.02, aspect=10)
443 ax.plt, ax=ax, fraction=0.05, pad=0.02, aspect=10)
438 ax.cbar.ax.tick_params(labelsize=8)
444 ax.cbar.ax.tick_params(labelsize=8)
439 ax.cbar.ax.press = None
445 ax.cbar.ax.press = None
440 if self.cb_label:
446 if self.cb_label:
441 ax.cbar.set_label(self.cb_label, size=8)
447 ax.cbar.set_label(self.cb_label, size=8)
442 elif self.cb_labels:
448 elif self.cb_labels:
443 ax.cbar.set_label(self.cb_labels[n], size=8)
449 ax.cbar.set_label(self.cb_labels[n], size=8)
444 else:
450 else:
445 ax.cbar = None
451 ax.cbar = None
446 if self.grid:
452 if self.grid:
447 ax.grid(True)
453 ax.grid(True)
448
454
449 if not self.polar:
455 if not self.polar:
450 ax.set_xlim(xmin, xmax)
456 ax.set_xlim(xmin, xmax)
451 ax.set_ylim(ymin, ymax)
457 ax.set_ylim(ymin, ymax)
452 ax.set_title('{} {} {}'.format(
458 ax.set_title('{} {} {}'.format(
453 self.titles[n],
459 self.titles[n],
454 self.getDateTime(self.max_time).strftime('%Y-%m-%dT%H:%M:%S'),
460 self.getDateTime(self.max_time).strftime('%Y-%m-%dT%H:%M:%S'),
455 self.time_label),
461 self.time_label),
456 size=8)
462 size=8)
457 else:
463 else:
458 ax.set_title('{}'.format(self.titles[n]), size=8)
464 ax.set_title('{}'.format(self.titles[n]), size=8)
459 ax.set_ylim(0, 90)
465 ax.set_ylim(0, 90)
460 ax.set_yticks(numpy.arange(0, 90, 20))
466 ax.set_yticks(numpy.arange(0, 90, 20))
461 ax.yaxis.labelpad = 40
467 ax.yaxis.labelpad = 40
462
468
463 def __plot(self):
469 def __plot(self):
464 '''
470 '''
465 '''
471 '''
466 log.log('Plotting', self.name)
472 log.log('Plotting', self.name)
467
473
468 try:
474 try:
469 self.plot()
475 self.plot()
470 self.format()
476 self.format()
471 except Exception as e:
477 except Exception as e:
472 log.warning('{} Plot could not be updated... check data'.format(self.CODE), self.name)
478 log.warning('{} Plot could not be updated... check data'.format(self.CODE), self.name)
473 log.error(str(e), '')
479 log.error(str(e), '')
474 return
480 return
475
481
476 for n, fig in enumerate(self.figures):
482 for n, fig in enumerate(self.figures):
477 if self.nrows == 0 or self.nplots == 0:
483 if self.nrows == 0 or self.nplots == 0:
478 log.warning('No data', self.name)
484 log.warning('No data', self.name)
479 fig.text(0.5, 0.5, 'No Data', fontsize='large', ha='center')
485 fig.text(0.5, 0.5, 'No Data', fontsize='large', ha='center')
480 fig.canvas.manager.set_window_title(self.CODE)
486 fig.canvas.manager.set_window_title(self.CODE)
481 continue
487 continue
482
488
483 fig.tight_layout()
489 fig.tight_layout()
484 fig.canvas.manager.set_window_title('{} - {}'.format(self.title,
490 fig.canvas.manager.set_window_title('{} - {}'.format(self.title,
485 self.getDateTime(self.max_time).strftime('%Y/%m/%d')))
491 self.getDateTime(self.max_time).strftime('%Y/%m/%d')))
486 fig.canvas.draw()
492 fig.canvas.draw()
487
493
488 if self.save and (self.data.ended or not self.data.buffering):
494 if self.save and (self.data.ended or not self.data.buffering):
489
495
490 if self.save_labels:
496 if self.save_labels:
491 labels = self.save_labels
497 labels = self.save_labels
492 else:
498 else:
493 labels = list(range(self.nrows))
499 labels = list(range(self.nrows))
494
500
495 if self.oneFigure:
501 if self.oneFigure:
496 label = ''
502 label = ''
497 else:
503 else:
498 label = '-{}'.format(labels[n])
504 label = '-{}'.format(labels[n])
499 figname = os.path.join(
505 figname = os.path.join(
500 self.save,
506 self.save,
501 self.CODE,
507 self.CODE,
502 '{}{}_{}.png'.format(
508 '{}{}_{}.png'.format(
503 self.CODE,
509 self.CODE,
504 label,
510 label,
505 self.getDateTime(self.saveTime).strftime(
511 self.getDateTime(self.saveTime).strftime(
506 '%Y%m%d_%H%M%S'),
512 '%Y%m%d_%H%M%S'),
507 )
513 )
508 )
514 )
509 log.log('Saving figure: {}'.format(figname), self.name)
515 log.log('Saving figure: {}'.format(figname), self.name)
510 if not os.path.isdir(os.path.dirname(figname)):
516 if not os.path.isdir(os.path.dirname(figname)):
511 os.makedirs(os.path.dirname(figname))
517 os.makedirs(os.path.dirname(figname))
512 fig.savefig(figname)
518 fig.savefig(figname)
513
519
514 def plot(self):
520 def plot(self):
515 '''
521 '''
516 '''
522 '''
517 raise NotImplementedError
523 raise NotImplementedError
518
524
519 def run(self):
525 def run(self):
520
526
521 log.log('Starting', self.name)
527 log.log('Starting', self.name)
522
528
523 context = zmq.Context()
529 context = zmq.Context()
524 receiver = context.socket(zmq.SUB)
530 receiver = context.socket(zmq.SUB)
525 receiver.setsockopt(zmq.SUBSCRIBE, '')
531 receiver.setsockopt(zmq.SUBSCRIBE, '')
526 receiver.setsockopt(zmq.CONFLATE, self.CONFLATE)
532 receiver.setsockopt(zmq.CONFLATE, self.CONFLATE)
527
533
528 if 'server' in self.kwargs['parent']:
534 if 'server' in self.kwargs['parent']:
529 receiver.connect(
535 receiver.connect(
530 'ipc:///tmp/{}.plots'.format(self.kwargs['parent']['server']))
536 'ipc:///tmp/{}.plots'.format(self.kwargs['parent']['server']))
531 else:
537 else:
532 receiver.connect("ipc:///tmp/zmq.plots")
538 receiver.connect("ipc:///tmp/zmq.plots")
533
539
534 while True:
540 while True:
535 try:
541 try:
536 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)
542 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)
537 if self.data.localtime and self.localtime:
543 if self.data.localtime and self.localtime:
538 self.times = self.data.times
544 self.times = self.data.times
539 elif self.data.localtime and not self.localtime:
545 elif self.data.localtime and not self.localtime:
540 self.times = self.data.times + time.timezone
546 self.times = self.data.times + time.timezone
541 elif not self.data.localtime and self.localtime:
547 elif not self.data.localtime and self.localtime:
542 self.times = self.data.times - time.timezone
548 self.times = self.data.times - time.timezone
543 else:
549 else:
544 self.times = self.data.times
550 self.times = self.data.times
545
551
546 self.min_time = self.times[0]
552 self.min_time = self.times[0]
547 self.max_time = self.times[-1]
553 self.max_time = self.times[-1]
548
554
549 if self.isConfig is False:
555 if self.isConfig is False:
550 self.__setup()
556 self.__setup()
551 self.isConfig = True
557 self.isConfig = True
552
558
553 self.__plot()
559 self.__plot()
554
560
555 except zmq.Again as e:
561 except zmq.Again as e:
556 if self.data and self.data.ended:
562 if self.data and self.data.ended:
557 break
563 break
558 log.log('Waiting for data...')
564 log.log('Waiting for data...')
559 if self.data:
565 if self.data:
560 figpause(self.data.throttle)
566 figpause(self.data.throttle)
561 else:
567 else:
562 time.sleep(2)
568 time.sleep(2)
563
569
564 def close(self):
570 def close(self):
565 if self.data:
571 if self.data:
566 self.__plot()
572 self.__plot()
567
573
568
574
569 class PlotSpectraData(PlotData):
575 class PlotSpectraData(PlotData):
570 '''
576 '''
571 Plot for Spectra data
577 Plot for Spectra data
572 '''
578 '''
573
579
574 CODE = 'spc'
580 CODE = 'spc'
575 colormap = 'jro'
581 colormap = 'jro'
576
582
577 def setup(self):
583 def setup(self):
578 self.nplots = len(self.data.channels)
584 self.nplots = len(self.data.channels)
579 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
585 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
580 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
586 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
581 self.width = 3.4 * self.ncols
587 self.width = 3.4 * self.ncols
582 self.height = 3 * self.nrows
588 self.height = 3 * self.nrows
583 self.cb_label = 'dB'
589 self.cb_label = 'dB'
584 if self.showprofile:
590 if self.showprofile:
585 self.width += 0.8 * self.ncols
591 self.width += 0.8 * self.ncols
586
592
587 self.ylabel = 'Range [km]'
593 self.ylabel = 'Range [km]'
588
594
589 def plot(self):
595 def plot(self):
590 if self.xaxis == "frequency":
596 if self.xaxis == "frequency":
591 x = self.data.xrange[0]
597 x = self.data.xrange[0]
592 self.xlabel = "Frequency (kHz)"
598 self.xlabel = "Frequency (kHz)"
593 elif self.xaxis == "time":
599 elif self.xaxis == "time":
594 x = self.data.xrange[1]
600 x = self.data.xrange[1]
595 self.xlabel = "Time (ms)"
601 self.xlabel = "Time (ms)"
596 else:
602 else:
597 x = self.data.xrange[2]
603 x = self.data.xrange[2]
598 self.xlabel = "Velocity (m/s)"
604 self.xlabel = "Velocity (m/s)"
599
605
600 if self.CODE == 'spc_mean':
606 if self.CODE == 'spc_mean':
601 x = self.data.xrange[2]
607 x = self.data.xrange[2]
602 self.xlabel = "Velocity (m/s)"
608 self.xlabel = "Velocity (m/s)"
603
609
604 self.titles = []
610 self.titles = []
605
611
606 y = self.data.heights
612 y = self.data.heights
607 self.y = y
613 self.y = y
608 z = self.data['spc']
614 z = self.data['spc']
609
615
610 for n, ax in enumerate(self.axes):
616 for n, ax in enumerate(self.axes):
611 noise = self.data['noise'][n][-1]
617 noise = self.data['noise'][n][-1]
612 if self.CODE == 'spc_mean':
618 if self.CODE == 'spc_mean':
613 mean = self.data['mean'][n][-1]
619 mean = self.data['mean'][n][-1]
614 if ax.firsttime:
620 if ax.firsttime:
615 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
621 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
616 self.xmin = self.xmin if self.xmin else -self.xmax
622 self.xmin = self.xmin if self.xmin else -self.xmax
617 self.zmin = self.zmin if self.zmin else numpy.nanmin(z)
623 self.zmin = self.zmin if self.zmin else numpy.nanmin(z)
618 self.zmax = self.zmax if self.zmax else numpy.nanmax(z)
624 self.zmax = self.zmax if self.zmax else numpy.nanmax(z)
619 ax.plt = ax.pcolormesh(x, y, z[n].T,
625 ax.plt = ax.pcolormesh(x, y, z[n].T,
620 vmin=self.zmin,
626 vmin=self.zmin,
621 vmax=self.zmax,
627 vmax=self.zmax,
622 cmap=plt.get_cmap(self.colormap)
628 cmap=plt.get_cmap(self.colormap)
623 )
629 )
624
630
625 if self.showprofile:
631 if self.showprofile:
626 ax.plt_profile = self.pf_axes[n].plot(
632 ax.plt_profile = self.pf_axes[n].plot(
627 self.data['rti'][n][-1], y)[0]
633 self.data['rti'][n][-1], y)[0]
628 ax.plt_noise = self.pf_axes[n].plot(numpy.repeat(noise, len(y)), y,
634 ax.plt_noise = self.pf_axes[n].plot(numpy.repeat(noise, len(y)), y,
629 color="k", linestyle="dashed", lw=1)[0]
635 color="k", linestyle="dashed", lw=1)[0]
630 if self.CODE == 'spc_mean':
636 if self.CODE == 'spc_mean':
631 ax.plt_mean = ax.plot(mean, y, color='k')[0]
637 ax.plt_mean = ax.plot(mean, y, color='k')[0]
632 else:
638 else:
633 ax.plt.set_array(z[n].T.ravel())
639 ax.plt.set_array(z[n].T.ravel())
634 if self.showprofile:
640 if self.showprofile:
635 ax.plt_profile.set_data(self.data['rti'][n][-1], y)
641 ax.plt_profile.set_data(self.data['rti'][n][-1], y)
636 ax.plt_noise.set_data(numpy.repeat(noise, len(y)), y)
642 ax.plt_noise.set_data(numpy.repeat(noise, len(y)), y)
637 if self.CODE == 'spc_mean':
643 if self.CODE == 'spc_mean':
638 ax.plt_mean.set_data(mean, y)
644 ax.plt_mean.set_data(mean, y)
639
645
640 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
646 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
641 self.saveTime = self.max_time
647 self.saveTime = self.max_time
642
648
643
649
644 class PlotCrossSpectraData(PlotData):
650 class PlotCrossSpectraData(PlotData):
645
651
646 CODE = 'cspc'
652 CODE = 'cspc'
647 zmin_coh = None
653 zmin_coh = None
648 zmax_coh = None
654 zmax_coh = None
649 zmin_phase = None
655 zmin_phase = None
650 zmax_phase = None
656 zmax_phase = None
651
657
652 def setup(self):
658 def setup(self):
653
659
654 self.ncols = 4
660 self.ncols = 4
655 self.nrows = len(self.data.pairs)
661 self.nrows = len(self.data.pairs)
656 self.nplots = self.nrows * 4
662 self.nplots = self.nrows * 4
657 self.width = 3.4 * self.ncols
663 self.width = 3.4 * self.ncols
658 self.height = 3 * self.nrows
664 self.height = 3 * self.nrows
659 self.ylabel = 'Range [km]'
665 self.ylabel = 'Range [km]'
660 self.showprofile = False
666 self.showprofile = False
661
667
662 def plot(self):
668 def plot(self):
663
669
664 if self.xaxis == "frequency":
670 if self.xaxis == "frequency":
665 x = self.data.xrange[0]
671 x = self.data.xrange[0]
666 self.xlabel = "Frequency (kHz)"
672 self.xlabel = "Frequency (kHz)"
667 elif self.xaxis == "time":
673 elif self.xaxis == "time":
668 x = self.data.xrange[1]
674 x = self.data.xrange[1]
669 self.xlabel = "Time (ms)"
675 self.xlabel = "Time (ms)"
670 else:
676 else:
671 x = self.data.xrange[2]
677 x = self.data.xrange[2]
672 self.xlabel = "Velocity (m/s)"
678 self.xlabel = "Velocity (m/s)"
673
679
674 self.titles = []
680 self.titles = []
675
681
676 y = self.data.heights
682 y = self.data.heights
677 self.y = y
683 self.y = y
678 spc = self.data['spc']
684 spc = self.data['spc']
679 cspc = self.data['cspc']
685 cspc = self.data['cspc']
680
686
681 for n in range(self.nrows):
687 for n in range(self.nrows):
682 noise = self.data['noise'][n][-1]
688 noise = self.data['noise'][n][-1]
683 pair = self.data.pairs[n]
689 pair = self.data.pairs[n]
684 ax = self.axes[4 * n]
690 ax = self.axes[4 * n]
685 ax3 = self.axes[4 * n + 3]
691 ax3 = self.axes[4 * n + 3]
686 if ax.firsttime:
692 if ax.firsttime:
687 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
693 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
688 self.xmin = self.xmin if self.xmin else -self.xmax
694 self.xmin = self.xmin if self.xmin else -self.xmax
689 self.zmin = self.zmin if self.zmin else numpy.nanmin(spc)
695 self.zmin = self.zmin if self.zmin else numpy.nanmin(spc)
690 self.zmax = self.zmax if self.zmax else numpy.nanmax(spc)
696 self.zmax = self.zmax if self.zmax else numpy.nanmax(spc)
691 ax.plt = ax.pcolormesh(x, y, spc[pair[0]].T,
697 ax.plt = ax.pcolormesh(x, y, spc[pair[0]].T,
692 vmin=self.zmin,
698 vmin=self.zmin,
693 vmax=self.zmax,
699 vmax=self.zmax,
694 cmap=plt.get_cmap(self.colormap)
700 cmap=plt.get_cmap(self.colormap)
695 )
701 )
696 else:
702 else:
697 ax.plt.set_array(spc[pair[0]].T.ravel())
703 ax.plt.set_array(spc[pair[0]].T.ravel())
698 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
704 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
699
705
700 ax = self.axes[4 * n + 1]
706 ax = self.axes[4 * n + 1]
701 if ax.firsttime:
707 if ax.firsttime:
702 ax.plt = ax.pcolormesh(x, y, spc[pair[1]].T,
708 ax.plt = ax.pcolormesh(x, y, spc[pair[1]].T,
703 vmin=self.zmin,
709 vmin=self.zmin,
704 vmax=self.zmax,
710 vmax=self.zmax,
705 cmap=plt.get_cmap(self.colormap)
711 cmap=plt.get_cmap(self.colormap)
706 )
712 )
707 else:
713 else:
708 ax.plt.set_array(spc[pair[1]].T.ravel())
714 ax.plt.set_array(spc[pair[1]].T.ravel())
709 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
715 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
710
716
711 out = cspc[n] / numpy.sqrt(spc[pair[0]] * spc[pair[1]])
717 out = cspc[n] / numpy.sqrt(spc[pair[0]] * spc[pair[1]])
712 coh = numpy.abs(out)
718 coh = numpy.abs(out)
713 phase = numpy.arctan2(out.imag, out.real) * 180 / numpy.pi
719 phase = numpy.arctan2(out.imag, out.real) * 180 / numpy.pi
714
720
715 ax = self.axes[4 * n + 2]
721 ax = self.axes[4 * n + 2]
716 if ax.firsttime:
722 if ax.firsttime:
717 ax.plt = ax.pcolormesh(x, y, coh.T,
723 ax.plt = ax.pcolormesh(x, y, coh.T,
718 vmin=0,
724 vmin=0,
719 vmax=1,
725 vmax=1,
720 cmap=plt.get_cmap(self.colormap_coh)
726 cmap=plt.get_cmap(self.colormap_coh)
721 )
727 )
722 else:
728 else:
723 ax.plt.set_array(coh.T.ravel())
729 ax.plt.set_array(coh.T.ravel())
724 self.titles.append(
730 self.titles.append(
725 'Coherence Ch{} * Ch{}'.format(pair[0], pair[1]))
731 'Coherence Ch{} * Ch{}'.format(pair[0], pair[1]))
726
732
727 ax = self.axes[4 * n + 3]
733 ax = self.axes[4 * n + 3]
728 if ax.firsttime:
734 if ax.firsttime:
729 ax.plt = ax.pcolormesh(x, y, phase.T,
735 ax.plt = ax.pcolormesh(x, y, phase.T,
730 vmin=-180,
736 vmin=-180,
731 vmax=180,
737 vmax=180,
732 cmap=plt.get_cmap(self.colormap_phase)
738 cmap=plt.get_cmap(self.colormap_phase)
733 )
739 )
734 else:
740 else:
735 ax.plt.set_array(phase.T.ravel())
741 ax.plt.set_array(phase.T.ravel())
736 self.titles.append('Phase CH{} * CH{}'.format(pair[0], pair[1]))
742 self.titles.append('Phase CH{} * CH{}'.format(pair[0], pair[1]))
737
743
738 self.saveTime = self.max_time
744 self.saveTime = self.max_time
739
745
740
746
741 class PlotSpectraMeanData(PlotSpectraData):
747 class PlotSpectraMeanData(PlotSpectraData):
742 '''
748 '''
743 Plot for Spectra and Mean
749 Plot for Spectra and Mean
744 '''
750 '''
745 CODE = 'spc_mean'
751 CODE = 'spc_mean'
746 colormap = 'jro'
752 colormap = 'jro'
747
753
748
754
749 class PlotRTIData(PlotData):
755 class PlotRTIData(PlotData):
750 '''
756 '''
751 Plot for RTI data
757 Plot for RTI data
752 '''
758 '''
753
759
754 CODE = 'rti'
760 CODE = 'rti'
755 colormap = 'jro'
761 colormap = 'jro'
756
762
757 def setup(self):
763 def setup(self):
758 self.xaxis = 'time'
764 self.xaxis = 'time'
759 self.ncols = 1
765 self.ncols = 1
760 self.nrows = len(self.data.channels)
766 self.nrows = len(self.data.channels)
761 self.nplots = len(self.data.channels)
767 self.nplots = len(self.data.channels)
762 self.ylabel = 'Range [km]'
768 self.ylabel = 'Range [km]'
763 self.cb_label = 'dB'
769 self.cb_label = 'dB'
764 self.titles = ['{} Channel {}'.format(
770 self.titles = ['{} Channel {}'.format(
765 self.CODE.upper(), x) for x in range(self.nrows)]
771 self.CODE.upper(), x) for x in range(self.nrows)]
766
772
767 def plot(self):
773 def plot(self):
768 self.x = self.times
774 self.x = self.times
769 self.y = self.data.heights
775 self.y = self.data.heights
770 self.z = self.data[self.CODE]
776 self.z = self.data[self.CODE]
771 self.z = numpy.ma.masked_invalid(self.z)
777 self.z = numpy.ma.masked_invalid(self.z)
772
778
773 if self.decimation is None:
779 if self.decimation is None:
774 x, y, z = self.fill_gaps(self.x, self.y, self.z)
780 x, y, z = self.fill_gaps(self.x, self.y, self.z)
775 else:
781 else:
776 x, y, z = self.fill_gaps(*self.decimate())
782 x, y, z = self.fill_gaps(*self.decimate())
777
783
778 for n, ax in enumerate(self.axes):
784 for n, ax in enumerate(self.axes):
779 self.zmin = self.zmin if self.zmin else numpy.min(self.z)
785 self.zmin = self.zmin if self.zmin else numpy.min(self.z)
780 self.zmax = self.zmax if self.zmax else numpy.max(self.z)
786 self.zmax = self.zmax if self.zmax else numpy.max(self.z)
781 if ax.firsttime:
787 if ax.firsttime:
782 ax.plt = ax.pcolormesh(x, y, z[n].T,
788 ax.plt = ax.pcolormesh(x, y, z[n].T,
783 vmin=self.zmin,
789 vmin=self.zmin,
784 vmax=self.zmax,
790 vmax=self.zmax,
785 cmap=plt.get_cmap(self.colormap)
791 cmap=plt.get_cmap(self.colormap)
786 )
792 )
787 if self.showprofile:
793 if self.showprofile:
788 ax.plot_profile = self.pf_axes[n].plot(
794 ax.plot_profile = self.pf_axes[n].plot(
789 self.data['rti'][n][-1], self.y)[0]
795 self.data['rti'][n][-1], self.y)[0]
790 ax.plot_noise = self.pf_axes[n].plot(numpy.repeat(self.data['noise'][n][-1], len(self.y)), self.y,
796 ax.plot_noise = self.pf_axes[n].plot(numpy.repeat(self.data['noise'][n][-1], len(self.y)), self.y,
791 color="k", linestyle="dashed", lw=1)[0]
797 color="k", linestyle="dashed", lw=1)[0]
792 else:
798 else:
793 ax.collections.remove(ax.collections[0])
799 ax.collections.remove(ax.collections[0])
794 ax.plt = ax.pcolormesh(x, y, z[n].T,
800 ax.plt = ax.pcolormesh(x, y, z[n].T,
795 vmin=self.zmin,
801 vmin=self.zmin,
796 vmax=self.zmax,
802 vmax=self.zmax,
797 cmap=plt.get_cmap(self.colormap)
803 cmap=plt.get_cmap(self.colormap)
798 )
804 )
799 if self.showprofile:
805 if self.showprofile:
800 ax.plot_profile.set_data(self.data['rti'][n][-1], self.y)
806 ax.plot_profile.set_data(self.data['rti'][n][-1], self.y)
801 ax.plot_noise.set_data(numpy.repeat(
807 ax.plot_noise.set_data(numpy.repeat(
802 self.data['noise'][n][-1], len(self.y)), self.y)
808 self.data['noise'][n][-1], len(self.y)), self.y)
803
809
804 self.saveTime = self.min_time
810 self.saveTime = self.min_time
805
811
806
812
807 class PlotCOHData(PlotRTIData):
813 class PlotCOHData(PlotRTIData):
808 '''
814 '''
809 Plot for Coherence data
815 Plot for Coherence data
810 '''
816 '''
811
817
812 CODE = 'coh'
818 CODE = 'coh'
813
819
814 def setup(self):
820 def setup(self):
815 self.xaxis = 'time'
821 self.xaxis = 'time'
816 self.ncols = 1
822 self.ncols = 1
817 self.nrows = len(self.data.pairs)
823 self.nrows = len(self.data.pairs)
818 self.nplots = len(self.data.pairs)
824 self.nplots = len(self.data.pairs)
819 self.ylabel = 'Range [km]'
825 self.ylabel = 'Range [km]'
820 if self.CODE == 'coh':
826 if self.CODE == 'coh':
821 self.cb_label = ''
827 self.cb_label = ''
822 self.titles = [
828 self.titles = [
823 'Coherence Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
829 'Coherence Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
824 else:
830 else:
825 self.cb_label = 'Degrees'
831 self.cb_label = 'Degrees'
826 self.titles = [
832 self.titles = [
827 'Phase Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
833 'Phase Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
828
834
829
835
830 class PlotPHASEData(PlotCOHData):
836 class PlotPHASEData(PlotCOHData):
831 '''
837 '''
832 Plot for Phase map data
838 Plot for Phase map data
833 '''
839 '''
834
840
835 CODE = 'phase'
841 CODE = 'phase'
836 colormap = 'seismic'
842 colormap = 'seismic'
837
843
838
844
839 class PlotNoiseData(PlotData):
845 class PlotNoiseData(PlotData):
840 '''
846 '''
841 Plot for noise
847 Plot for noise
842 '''
848 '''
843
849
844 CODE = 'noise'
850 CODE = 'noise'
845
851
846 def setup(self):
852 def setup(self):
847 self.xaxis = 'time'
853 self.xaxis = 'time'
848 self.ncols = 1
854 self.ncols = 1
849 self.nrows = 1
855 self.nrows = 1
850 self.nplots = 1
856 self.nplots = 1
851 self.ylabel = 'Intensity [dB]'
857 self.ylabel = 'Intensity [dB]'
852 self.titles = ['Noise']
858 self.titles = ['Noise']
853 self.colorbar = False
859 self.colorbar = False
854
860
855 def plot(self):
861 def plot(self):
856
862
857 x = self.times
863 x = self.times
858 xmin = self.min_time
864 xmin = self.min_time
859 xmax = xmin + self.xrange * 60 * 60
865 xmax = xmin + self.xrange * 60 * 60
860 Y = self.data[self.CODE]
866 Y = self.data[self.CODE]
861
867
862 if self.axes[0].firsttime:
868 if self.axes[0].firsttime:
863 for ch in self.data.channels:
869 for ch in self.data.channels:
864 y = Y[ch]
870 y = Y[ch]
865 self.axes[0].plot(x, y, lw=1, label='Ch{}'.format(ch))
871 self.axes[0].plot(x, y, lw=1, label='Ch{}'.format(ch))
866 plt.legend()
872 plt.legend()
867 else:
873 else:
868 for ch in self.data.channels:
874 for ch in self.data.channels:
869 y = Y[ch]
875 y = Y[ch]
870 self.axes[0].lines[ch].set_data(x, y)
876 self.axes[0].lines[ch].set_data(x, y)
871
877
872 self.ymin = numpy.nanmin(Y) - 5
878 self.ymin = numpy.nanmin(Y) - 5
873 self.ymax = numpy.nanmax(Y) + 5
879 self.ymax = numpy.nanmax(Y) + 5
874 self.saveTime = self.min_time
880 self.saveTime = self.min_time
875
881
876
882
877 class PlotSNRData(PlotRTIData):
883 class PlotSNRData(PlotRTIData):
878 '''
884 '''
879 Plot for SNR Data
885 Plot for SNR Data
880 '''
886 '''
881
887
882 CODE = 'snr'
888 CODE = 'snr'
883 colormap = 'jet'
889 colormap = 'jet'
884
890
885
891
886 class PlotDOPData(PlotRTIData):
892 class PlotDOPData(PlotRTIData):
887 '''
893 '''
888 Plot for DOPPLER Data
894 Plot for DOPPLER Data
889 '''
895 '''
890
896
891 CODE = 'dop'
897 CODE = 'dop'
892 colormap = 'jet'
898 colormap = 'jet'
893
899
894
900
895 class PlotSkyMapData(PlotData):
901 class PlotSkyMapData(PlotData):
896 '''
902 '''
897 Plot for meteors detection data
903 Plot for meteors detection data
898 '''
904 '''
899
905
900 CODE = 'param'
906 CODE = 'param'
901
907
902 def setup(self):
908 def setup(self):
903
909
904 self.ncols = 1
910 self.ncols = 1
905 self.nrows = 1
911 self.nrows = 1
906 self.width = 7.2
912 self.width = 7.2
907 self.height = 7.2
913 self.height = 7.2
908 self.nplots = 1
914 self.nplots = 1
909 self.xlabel = 'Zonal Zenith Angle (deg)'
915 self.xlabel = 'Zonal Zenith Angle (deg)'
910 self.ylabel = 'Meridional Zenith Angle (deg)'
916 self.ylabel = 'Meridional Zenith Angle (deg)'
911 self.polar = True
917 self.polar = True
912 self.ymin = -180
918 self.ymin = -180
913 self.ymax = 180
919 self.ymax = 180
914 self.colorbar = False
920 self.colorbar = False
915
921
916 def plot(self):
922 def plot(self):
917
923
918 arrayParameters = numpy.concatenate(self.data['param'])
924 arrayParameters = numpy.concatenate(self.data['param'])
919 error = arrayParameters[:, -1]
925 error = arrayParameters[:, -1]
920 indValid = numpy.where(error == 0)[0]
926 indValid = numpy.where(error == 0)[0]
921 finalMeteor = arrayParameters[indValid, :]
927 finalMeteor = arrayParameters[indValid, :]
922 finalAzimuth = finalMeteor[:, 3]
928 finalAzimuth = finalMeteor[:, 3]
923 finalZenith = finalMeteor[:, 4]
929 finalZenith = finalMeteor[:, 4]
924
930
925 x = finalAzimuth * numpy.pi / 180
931 x = finalAzimuth * numpy.pi / 180
926 y = finalZenith
932 y = finalZenith
927
933
928 ax = self.axes[0]
934 ax = self.axes[0]
929
935
930 if ax.firsttime:
936 if ax.firsttime:
931 ax.plot = ax.plot(x, y, 'bo', markersize=5)[0]
937 ax.plot = ax.plot(x, y, 'bo', markersize=5)[0]
932 else:
938 else:
933 ax.plot.set_data(x, y)
939 ax.plot.set_data(x, y)
934
940
935 dt1 = self.getDateTime(self.min_time).strftime('%y/%m/%d %H:%M:%S')
941 dt1 = self.getDateTime(self.min_time).strftime('%y/%m/%d %H:%M:%S')
936 dt2 = self.getDateTime(self.max_time).strftime('%y/%m/%d %H:%M:%S')
942 dt2 = self.getDateTime(self.max_time).strftime('%y/%m/%d %H:%M:%S')
937 title = 'Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n' % (dt1,
943 title = 'Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n' % (dt1,
938 dt2,
944 dt2,
939 len(x))
945 len(x))
940 self.titles[0] = title
946 self.titles[0] = title
941 self.saveTime = self.max_time
947 self.saveTime = self.max_time
942
948
943
949
944 class PlotParamData(PlotRTIData):
950 class PlotParamData(PlotRTIData):
945 '''
951 '''
946 Plot for data_param object
952 Plot for data_param object
947 '''
953 '''
948
954
949 CODE = 'param'
955 CODE = 'param'
950 colormap = 'seismic'
956 colormap = 'seismic'
951
957
952 def setup(self):
958 def setup(self):
953 self.xaxis = 'time'
959 self.xaxis = 'time'
954 self.ncols = 1
960 self.ncols = 1
955 self.nrows = self.data.shape(self.CODE)[0]
961 self.nrows = self.data.shape(self.CODE)[0]
956 self.nplots = self.nrows
962 self.nplots = self.nrows
957 if self.showSNR:
963 if self.showSNR:
958 self.nrows += 1
964 self.nrows += 1
959 self.nplots += 1
965 self.nplots += 1
960
966
961 self.ylabel = 'Height [km]'
967 self.ylabel = 'Height [km]'
962 if not self.titles:
968 if not self.titles:
963 self.titles = self.data.parameters \
969 self.titles = self.data.parameters \
964 if self.data.parameters else ['Param {}'.format(x) for x in range(self.nrows)]
970 if self.data.parameters else ['Param {}'.format(x) for x in range(self.nrows)]
965 if self.showSNR:
971 if self.showSNR:
966 self.titles.append('SNR')
972 self.titles.append('SNR')
967
973
968 def plot(self):
974 def plot(self):
969 self.data.normalize_heights()
975 self.data.normalize_heights()
970 self.x = self.times
976 self.x = self.times
971 self.y = self.data.heights
977 self.y = self.data.heights
972 if self.showSNR:
978 if self.showSNR:
973 self.z = numpy.concatenate(
979 self.z = numpy.concatenate(
974 (self.data[self.CODE], self.data['snr'])
980 (self.data[self.CODE], self.data['snr'])
975 )
981 )
976 else:
982 else:
977 self.z = self.data[self.CODE]
983 self.z = self.data[self.CODE]
978
984
979 self.z = numpy.ma.masked_invalid(self.z)
985 self.z = numpy.ma.masked_invalid(self.z)
980
986
981 if self.decimation is None:
987 if self.decimation is None:
982 x, y, z = self.fill_gaps(self.x, self.y, self.z)
988 x, y, z = self.fill_gaps(self.x, self.y, self.z)
983 else:
989 else:
984 x, y, z = self.fill_gaps(*self.decimate())
990 x, y, z = self.fill_gaps(*self.decimate())
985
991
986 for n, ax in enumerate(self.axes):
992 for n, ax in enumerate(self.axes):
987
993
988 self.zmax = self.zmax if self.zmax is not None else numpy.max(
994 self.zmax = self.zmax if self.zmax is not None else numpy.max(
989 self.z[n])
995 self.z[n])
990 self.zmin = self.zmin if self.zmin is not None else numpy.min(
996 self.zmin = self.zmin if self.zmin is not None else numpy.min(
991 self.z[n])
997 self.z[n])
992
998
993 if ax.firsttime:
999 if ax.firsttime:
994 if self.zlimits is not None:
1000 if self.zlimits is not None:
995 self.zmin, self.zmax = self.zlimits[n]
1001 self.zmin, self.zmax = self.zlimits[n]
996
1002
997 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
1003 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
998 vmin=self.zmin,
1004 vmin=self.zmin,
999 vmax=self.zmax,
1005 vmax=self.zmax,
1000 cmap=self.cmaps[n]
1006 cmap=self.cmaps[n]
1001 )
1007 )
1002 else:
1008 else:
1003 if self.zlimits is not None:
1009 if self.zlimits is not None:
1004 self.zmin, self.zmax = self.zlimits[n]
1010 self.zmin, self.zmax = self.zlimits[n]
1005 ax.collections.remove(ax.collections[0])
1011 ax.collections.remove(ax.collections[0])
1006 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
1012 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
1007 vmin=self.zmin,
1013 vmin=self.zmin,
1008 vmax=self.zmax,
1014 vmax=self.zmax,
1009 cmap=self.cmaps[n]
1015 cmap=self.cmaps[n]
1010 )
1016 )
1011
1017
1012 self.saveTime = self.min_time
1018 self.saveTime = self.min_time
1013
1019
1014
1020
1015 class PlotOutputData(PlotParamData):
1021 class PlotOutputData(PlotParamData):
1016 '''
1022 '''
1017 Plot data_output object
1023 Plot data_output object
1018 '''
1024 '''
1019
1025
1020 CODE = 'output'
1026 CODE = 'output'
1021 colormap = 'seismic'
1027 colormap = 'seismic'
1022
1028
1023
1029
1024 class PlotPolarMapData(PlotData):
1030 class PlotPolarMapData(PlotData):
1025 '''
1031 '''
1026 Plot for meteors detection data
1032 Plot for meteors detection data
1027 '''
1033 '''
1028
1034
1029 CODE = 'param'
1035 CODE = 'param'
1030 colormap = 'seismic'
1036 colormap = 'seismic'
1031
1037
1032 def setup(self):
1038 def setup(self):
1033 self.ncols = 1
1039 self.ncols = 1
1034 self.nrows = 1
1040 self.nrows = 1
1035 self.width = 9
1041 self.width = 9
1036 self.height = 8
1042 self.height = 8
1037 self.mode = self.data.meta['mode']
1043 self.mode = self.data.meta['mode']
1038 if self.channels is not None:
1044 if self.channels is not None:
1039 self.nplots = len(self.channels)
1045 self.nplots = len(self.channels)
1040 self.nrows = len(self.channels)
1046 self.nrows = len(self.channels)
1041 else:
1047 else:
1042 self.nplots = self.data.shape(self.CODE)[0]
1048 self.nplots = self.data.shape(self.CODE)[0]
1043 self.nrows = self.nplots
1049 self.nrows = self.nplots
1044 self.channels = list(range(self.nplots))
1050 self.channels = list(range(self.nplots))
1045 if self.mode == 'E':
1051 if self.mode == 'E':
1046 self.xlabel = 'Longitude'
1052 self.xlabel = 'Longitude'
1047 self.ylabel = 'Latitude'
1053 self.ylabel = 'Latitude'
1048 else:
1054 else:
1049 self.xlabel = 'Range (km)'
1055 self.xlabel = 'Range (km)'
1050 self.ylabel = 'Height (km)'
1056 self.ylabel = 'Height (km)'
1051 self.bgcolor = 'white'
1057 self.bgcolor = 'white'
1052 self.cb_labels = self.data.meta['units']
1058 self.cb_labels = self.data.meta['units']
1053 self.lat = self.data.meta['latitude']
1059 self.lat = self.data.meta['latitude']
1054 self.lon = self.data.meta['longitude']
1060 self.lon = self.data.meta['longitude']
1055 self.xmin, self.xmax = float(km2deg(self.xmin) + self.lon), float(km2deg(self.xmax) + self.lon)
1061 self.xmin, self.xmax = float(km2deg(self.xmin) + self.lon), float(km2deg(self.xmax) + self.lon)
1056 self.ymin, self.ymax = float(km2deg(self.ymin) + self.lat), float(km2deg(self.ymax) + self.lat)
1062 self.ymin, self.ymax = float(km2deg(self.ymin) + self.lat), float(km2deg(self.ymax) + self.lat)
1057 # self.polar = True
1063 # self.polar = True
1058
1064
1059 def plot(self):
1065 def plot(self):
1060
1066
1061 for n, ax in enumerate(self.axes):
1067 for n, ax in enumerate(self.axes):
1062 data = self.data['param'][self.channels[n]]
1068 data = self.data['param'][self.channels[n]]
1063
1069
1064 zeniths = numpy.linspace(0, self.data.meta['max_range'], data.shape[1])
1070 zeniths = numpy.linspace(0, self.data.meta['max_range'], data.shape[1])
1065 if self.mode == 'E':
1071 if self.mode == 'E':
1066 azimuths = -numpy.radians(self.data.heights)+numpy.pi/2
1072 azimuths = -numpy.radians(self.data.heights)+numpy.pi/2
1067 r, theta = numpy.meshgrid(zeniths, azimuths)
1073 r, theta = numpy.meshgrid(zeniths, azimuths)
1068 x, y = r*numpy.cos(theta)*numpy.cos(numpy.radians(self.data.meta['elevation'])), r*numpy.sin(theta)*numpy.cos(numpy.radians(self.data.meta['elevation']))
1074 x, y = r*numpy.cos(theta)*numpy.cos(numpy.radians(self.data.meta['elevation'])), r*numpy.sin(theta)*numpy.cos(numpy.radians(self.data.meta['elevation']))
1069 x = km2deg(x) + self.lon
1075 x = km2deg(x) + self.lon
1070 y = km2deg(y) + self.lat
1076 y = km2deg(y) + self.lat
1071 else:
1077 else:
1072 azimuths = numpy.radians(self.data.heights)
1078 azimuths = numpy.radians(self.data.heights)
1073 r, theta = numpy.meshgrid(zeniths, azimuths)
1079 r, theta = numpy.meshgrid(zeniths, azimuths)
1074 x, y = r*numpy.cos(theta), r*numpy.sin(theta)
1080 x, y = r*numpy.cos(theta), r*numpy.sin(theta)
1075 self.y = zeniths
1081 self.y = zeniths
1076
1082
1077 if ax.firsttime:
1083 if ax.firsttime:
1078 if self.zlimits is not None:
1084 if self.zlimits is not None:
1079 self.zmin, self.zmax = self.zlimits[n]
1085 self.zmin, self.zmax = self.zlimits[n]
1080 ax.plt = ax.pcolormesh(#r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
1086 ax.plt = ax.pcolormesh(#r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
1081 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
1087 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
1082 vmin=self.zmin,
1088 vmin=self.zmin,
1083 vmax=self.zmax,
1089 vmax=self.zmax,
1084 cmap=self.cmaps[n])
1090 cmap=self.cmaps[n])
1085 else:
1091 else:
1086 if self.zlimits is not None:
1092 if self.zlimits is not None:
1087 self.zmin, self.zmax = self.zlimits[n]
1093 self.zmin, self.zmax = self.zlimits[n]
1088 ax.collections.remove(ax.collections[0])
1094 ax.collections.remove(ax.collections[0])
1089 ax.plt = ax.pcolormesh(# r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
1095 ax.plt = ax.pcolormesh(# r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
1090 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
1096 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
1091 vmin=self.zmin,
1097 vmin=self.zmin,
1092 vmax=self.zmax,
1098 vmax=self.zmax,
1093 cmap=self.cmaps[n])
1099 cmap=self.cmaps[n])
1094
1100
1095 if self.mode == 'A':
1101 if self.mode == 'A':
1096 continue
1102 continue
1097
1103
1098 # plot district names
1104 # plot district names
1099 f = open('/data/workspace/schain_scripts/distrito.csv')
1105 f = open('/data/workspace/schain_scripts/distrito.csv')
1100 for line in f:
1106 for line in f:
1101 label, lon, lat = [s.strip() for s in line.split(',') if s]
1107 label, lon, lat = [s.strip() for s in line.split(',') if s]
1102 lat = float(lat)
1108 lat = float(lat)
1103 lon = float(lon)
1109 lon = float(lon)
1104 # ax.plot(lon, lat, '.b', ms=2)
1110 # ax.plot(lon, lat, '.b', ms=2)
1105 ax.text(lon, lat, label.decode('utf8'), ha='center', va='bottom', size='8', color='black')
1111 ax.text(lon, lat, label.decode('utf8'), ha='center', va='bottom', size='8', color='black')
1106
1112
1107 # plot limites
1113 # plot limites
1108 limites =[]
1114 limites =[]
1109 tmp = []
1115 tmp = []
1110 for line in open('/data/workspace/schain_scripts/lima.csv'):
1116 for line in open('/data/workspace/schain_scripts/lima.csv'):
1111 if '#' in line:
1117 if '#' in line:
1112 if tmp:
1118 if tmp:
1113 limites.append(tmp)
1119 limites.append(tmp)
1114 tmp = []
1120 tmp = []
1115 continue
1121 continue
1116 values = line.strip().split(',')
1122 values = line.strip().split(',')
1117 tmp.append((float(values[0]), float(values[1])))
1123 tmp.append((float(values[0]), float(values[1])))
1118 for points in limites:
1124 for points in limites:
1119 ax.add_patch(Polygon(points, ec='k', fc='none', ls='--', lw=0.5))
1125 ax.add_patch(Polygon(points, ec='k', fc='none', ls='--', lw=0.5))
1120
1126
1121 # plot Cuencas
1127 # plot Cuencas
1122 for cuenca in ('rimac', 'lurin', 'mala', 'chillon', 'chilca', 'chancay-huaral'):
1128 for cuenca in ('rimac', 'lurin', 'mala', 'chillon', 'chilca', 'chancay-huaral'):
1123 f = open('/data/workspace/schain_scripts/{}.csv'.format(cuenca))
1129 f = open('/data/workspace/schain_scripts/{}.csv'.format(cuenca))
1124 values = [line.strip().split(',') for line in f]
1130 values = [line.strip().split(',') for line in f]
1125 points = [(float(s[0]), float(s[1])) for s in values]
1131 points = [(float(s[0]), float(s[1])) for s in values]
1126 ax.add_patch(Polygon(points, ec='b', fc='none'))
1132 ax.add_patch(Polygon(points, ec='b', fc='none'))
1127
1133
1128 # plot grid
1134 # plot grid
1129 for r in (15, 30, 45, 60):
1135 for r in (15, 30, 45, 60):
1130 ax.add_artist(plt.Circle((self.lon, self.lat), km2deg(r), color='0.6', fill=False, lw=0.2))
1136 ax.add_artist(plt.Circle((self.lon, self.lat), km2deg(r), color='0.6', fill=False, lw=0.2))
1131 ax.text(
1137 ax.text(
1132 self.lon + (km2deg(r))*numpy.cos(60*numpy.pi/180),
1138 self.lon + (km2deg(r))*numpy.cos(60*numpy.pi/180),
1133 self.lat + (km2deg(r))*numpy.sin(60*numpy.pi/180),
1139 self.lat + (km2deg(r))*numpy.sin(60*numpy.pi/180),
1134 '{}km'.format(r),
1140 '{}km'.format(r),
1135 ha='center', va='bottom', size='8', color='0.6', weight='heavy')
1141 ha='center', va='bottom', size='8', color='0.6', weight='heavy')
1136
1142
1137 if self.mode == 'E':
1143 if self.mode == 'E':
1138 title = 'El={}$^\circ$'.format(self.data.meta['elevation'])
1144 title = 'El={}$^\circ$'.format(self.data.meta['elevation'])
1139 label = 'E{:02d}'.format(int(self.data.meta['elevation']))
1145 label = 'E{:02d}'.format(int(self.data.meta['elevation']))
1140 else:
1146 else:
1141 title = 'Az={}$^\circ$'.format(self.data.meta['azimuth'])
1147 title = 'Az={}$^\circ$'.format(self.data.meta['azimuth'])
1142 label = 'A{:02d}'.format(int(self.data.meta['azimuth']))
1148 label = 'A{:02d}'.format(int(self.data.meta['azimuth']))
1143
1149
1144 self.save_labels = ['{}-{}'.format(lbl, label) for lbl in self.labels]
1150 self.save_labels = ['{}-{}'.format(lbl, label) for lbl in self.labels]
1145 self.titles = ['{} {}'.format(self.data.parameters[x], title) for x in self.channels]
1151 self.titles = ['{} {}'.format(self.data.parameters[x], title) for x in self.channels]
1146 self.saveTime = self.max_time
1152 self.saveTime = self.max_time
1147
1153
1148 No newline at end of file
1154
@@ -1,680 +1,680
1 '''
1 '''
2 Created on Jul 2, 2014
2 Created on Jul 2, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import numpy
6 import numpy
7
7
8 from schainpy.model.io.jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
8 from schainpy.model.io.jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
10 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
10 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
11 from schainpy.model.data.jrodata import Spectra
11 from schainpy.model.data.jrodata import Spectra
12
12
13 @MPDecorator
13 @MPDecorator
14 class SpectraReader(JRODataReader, ProcessingUnit):
14 class SpectraReader(JRODataReader, ProcessingUnit):
15 """
15 """
16 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
16 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
17 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
17 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
18 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
18 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
19
19
20 paresCanalesIguales * alturas * perfiles (Self Spectra)
20 paresCanalesIguales * alturas * perfiles (Self Spectra)
21 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
21 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
22 canales * alturas (DC Channels)
22 canales * alturas (DC Channels)
23
23
24 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
24 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
25 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
25 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
26 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
26 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
27 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
27 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
28
28
29 Example:
29 Example:
30 dpath = "/home/myuser/data"
30 dpath = "/home/myuser/data"
31
31
32 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
32 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
33
33
34 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
34 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
35
35
36 readerObj = SpectraReader()
36 readerObj = SpectraReader()
37
37
38 readerObj.setup(dpath, startTime, endTime)
38 readerObj.setup(dpath, startTime, endTime)
39
39
40 while(True):
40 while(True):
41
41
42 readerObj.getData()
42 readerObj.getData()
43
43
44 print readerObj.data_spc
44 print readerObj.data_spc
45
45
46 print readerObj.data_cspc
46 print readerObj.data_cspc
47
47
48 print readerObj.data_dc
48 print readerObj.data_dc
49
49
50 if readerObj.flagNoMoreFiles:
50 if readerObj.flagNoMoreFiles:
51 break
51 break
52
52
53 """
53 """
54
54
55 pts2read_SelfSpectra = 0
55 pts2read_SelfSpectra = 0
56
56
57 pts2read_CrossSpectra = 0
57 pts2read_CrossSpectra = 0
58
58
59 pts2read_DCchannels = 0
59 pts2read_DCchannels = 0
60
60
61 ext = ".pdata"
61 ext = ".pdata"
62
62
63 optchar = "P"
63 optchar = "P"
64
64
65 dataOut = None
65 dataOut = None
66
66
67 nRdChannels = None
67 nRdChannels = None
68
68
69 nRdPairs = None
69 nRdPairs = None
70
70
71 rdPairList = []
71 rdPairList = []
72
72
73 def __init__(self):#, **kwargs):
73 def __init__(self):#, **kwargs):
74 """
74 """
75 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
75 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
76
76
77 Inputs:
77 Inputs:
78 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
78 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
79 almacenar un perfil de datos cada vez que se haga un requerimiento
79 almacenar un perfil de datos cada vez que se haga un requerimiento
80 (getData). El perfil sera obtenido a partir del buffer de datos,
80 (getData). El perfil sera obtenido a partir del buffer de datos,
81 si el buffer esta vacio se hara un nuevo proceso de lectura de un
81 si el buffer esta vacio se hara un nuevo proceso de lectura de un
82 bloque de datos.
82 bloque de datos.
83 Si este parametro no es pasado se creara uno internamente.
83 Si este parametro no es pasado se creara uno internamente.
84
84
85 Affected:
85 Affected:
86 self.dataOut
86 self.dataOut
87
87
88 Return : None
88 Return : None
89 """
89 """
90
90
91 #Eliminar de la base la herencia
91 #Eliminar de la base la herencia
92 ProcessingUnit.__init__(self)#, **kwargs)
92 ProcessingUnit.__init__(self)#, **kwargs)
93
93
94 # self.isConfig = False
94 # self.isConfig = False
95
95
96 self.pts2read_SelfSpectra = 0
96 self.pts2read_SelfSpectra = 0
97
97
98 self.pts2read_CrossSpectra = 0
98 self.pts2read_CrossSpectra = 0
99
99
100 self.pts2read_DCchannels = 0
100 self.pts2read_DCchannels = 0
101
101
102 self.datablock = None
102 self.datablock = None
103
103
104 self.utc = None
104 self.utc = None
105
105
106 self.ext = ".pdata"
106 self.ext = ".pdata"
107
107
108 self.optchar = "P"
108 self.optchar = "P"
109
109
110 self.basicHeaderObj = BasicHeader(LOCALTIME)
110 self.basicHeaderObj = BasicHeader(LOCALTIME)
111
111
112 self.systemHeaderObj = SystemHeader()
112 self.systemHeaderObj = SystemHeader()
113
113
114 self.radarControllerHeaderObj = RadarControllerHeader()
114 self.radarControllerHeaderObj = RadarControllerHeader()
115
115
116 self.processingHeaderObj = ProcessingHeader()
116 self.processingHeaderObj = ProcessingHeader()
117
117
118 self.online = 0
118 self.online = 0
119
119
120 self.fp = None
120 self.fp = None
121
121
122 self.idFile = None
122 self.idFile = None
123
123
124 self.dtype = None
124 self.dtype = None
125
125
126 self.fileSizeByHeader = None
126 self.fileSizeByHeader = None
127
127
128 self.filenameList = []
128 self.filenameList = []
129
129
130 self.filename = None
130 self.filename = None
131
131
132 self.fileSize = None
132 self.fileSize = None
133
133
134 self.firstHeaderSize = 0
134 self.firstHeaderSize = 0
135
135
136 self.basicHeaderSize = 24
136 self.basicHeaderSize = 24
137
137
138 self.pathList = []
138 self.pathList = []
139
139
140 self.lastUTTime = 0
140 self.lastUTTime = 0
141
141
142 self.maxTimeStep = 30
142 self.maxTimeStep = 30
143
143
144 self.flagNoMoreFiles = 0
144 self.flagNoMoreFiles = 0
145
145
146 self.set = 0
146 self.set = 0
147
147
148 self.path = None
148 self.path = None
149
149
150 self.delay = 60 #seconds
150 self.delay = 60 #seconds
151
151
152 self.nTries = 3 #quantity tries
152 self.nTries = 3 #quantity tries
153
153
154 self.nFiles = 3 #number of files for searching
154 self.nFiles = 3 #number of files for searching
155
155
156 self.nReadBlocks = 0
156 self.nReadBlocks = 0
157
157
158 self.flagIsNewFile = 1
158 self.flagIsNewFile = 1
159
159
160 self.__isFirstTimeOnline = 1
160 self.__isFirstTimeOnline = 1
161
161
162 # self.ippSeconds = 0
162 # self.ippSeconds = 0
163
163
164 self.flagDiscontinuousBlock = 0
164 self.flagDiscontinuousBlock = 0
165
165
166 self.flagIsNewBlock = 0
166 self.flagIsNewBlock = 0
167
167
168 self.nTotalBlocks = 0
168 self.nTotalBlocks = 0
169
169
170 self.blocksize = 0
170 self.blocksize = 0
171
171
172 self.dataOut = self.createObjByDefault()
172 self.dataOut = self.createObjByDefault()
173
173
174 self.profileIndex = 1 #Always
174 self.profileIndex = 1 #Always
175
175
176
176
177 def createObjByDefault(self):
177 def createObjByDefault(self):
178
178
179 dataObj = Spectra()
179 dataObj = Spectra()
180
180
181 return dataObj
181 return dataObj
182
182
183 def __hasNotDataInBuffer(self):
183 def __hasNotDataInBuffer(self):
184 return 1
184 return 1
185
185
186
186
187 def getBlockDimension(self):
187 def getBlockDimension(self):
188 """
188 """
189 Obtiene la cantidad de puntos a leer por cada bloque de datos
189 Obtiene la cantidad de puntos a leer por cada bloque de datos
190
190
191 Affected:
191 Affected:
192 self.nRdChannels
192 self.nRdChannels
193 self.nRdPairs
193 self.nRdPairs
194 self.pts2read_SelfSpectra
194 self.pts2read_SelfSpectra
195 self.pts2read_CrossSpectra
195 self.pts2read_CrossSpectra
196 self.pts2read_DCchannels
196 self.pts2read_DCchannels
197 self.blocksize
197 self.blocksize
198 self.dataOut.nChannels
198 self.dataOut.nChannels
199 self.dataOut.nPairs
199 self.dataOut.nPairs
200
200
201 Return:
201 Return:
202 None
202 None
203 """
203 """
204 self.nRdChannels = 0
204 self.nRdChannels = 0
205 self.nRdPairs = 0
205 self.nRdPairs = 0
206 self.rdPairList = []
206 self.rdPairList = []
207
207
208 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
208 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
209 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
209 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
210 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
210 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
211 else:
211 else:
212 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
212 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
213 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
213 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
214
214
215 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
215 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
216
216
217 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
217 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
218 self.blocksize = self.pts2read_SelfSpectra
218 self.blocksize = self.pts2read_SelfSpectra
219
219
220 if self.processingHeaderObj.flag_cspc:
220 if self.processingHeaderObj.flag_cspc:
221 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
221 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
222 self.blocksize += self.pts2read_CrossSpectra
222 self.blocksize += self.pts2read_CrossSpectra
223
223
224 if self.processingHeaderObj.flag_dc:
224 if self.processingHeaderObj.flag_dc:
225 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
225 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
226 self.blocksize += self.pts2read_DCchannels
226 self.blocksize += self.pts2read_DCchannels
227
227
228 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
228 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
229
229
230
230
231 def readBlock(self):
231 def readBlock(self):
232 """
232 """
233 Lee el bloque de datos desde la posicion actual del puntero del archivo
233 Lee el bloque de datos desde la posicion actual del puntero del archivo
234 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
234 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
235 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
235 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
236 es seteado a 0
236 es seteado a 0
237
237
238 Return: None
238 Return: None
239
239
240 Variables afectadas:
240 Variables afectadas:
241
241
242 self.flagIsNewFile
242 self.flagIsNewFile
243 self.flagIsNewBlock
243 self.flagIsNewBlock
244 self.nTotalBlocks
244 self.nTotalBlocks
245 self.data_spc
245 self.data_spc
246 self.data_cspc
246 self.data_cspc
247 self.data_dc
247 self.data_dc
248
248
249 Exceptions:
249 Exceptions:
250 Si un bloque leido no es un bloque valido
250 Si un bloque leido no es un bloque valido
251 """
251 """
252 blockOk_flag = False
252 blockOk_flag = False
253 fpointer = self.fp.tell()
253 fpointer = self.fp.tell()
254
254
255 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
255 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
256 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
256 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
257
257
258 if self.processingHeaderObj.flag_cspc:
258 if self.processingHeaderObj.flag_cspc:
259 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
259 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
260 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
260 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
261
261
262 if self.processingHeaderObj.flag_dc:
262 if self.processingHeaderObj.flag_dc:
263 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
263 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
264 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
264 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
265
265
266
266
267 if self.processingHeaderObj.shif_fft:
267 if not self.processingHeaderObj.shif_fft:
268 #desplaza a la derecha en el eje 2 determinadas posiciones
268 #desplaza a la derecha en el eje 2 determinadas posiciones
269 shift = int(self.processingHeaderObj.profilesPerBlock/2)
269 shift = int(self.processingHeaderObj.profilesPerBlock/2)
270 spc = numpy.roll( spc, shift , axis=2 )
270 spc = numpy.roll( spc, shift , axis=2 )
271
271
272 if self.processingHeaderObj.flag_cspc:
272 if self.processingHeaderObj.flag_cspc:
273 #desplaza a la derecha en el eje 2 determinadas posiciones
273 #desplaza a la derecha en el eje 2 determinadas posiciones
274 cspc = numpy.roll( cspc, shift, axis=2 )
274 cspc = numpy.roll( cspc, shift, axis=2 )
275
275
276 #Dimensions : nChannels, nProfiles, nSamples
276 #Dimensions : nChannels, nProfiles, nSamples
277 spc = numpy.transpose( spc, (0,2,1) )
277 spc = numpy.transpose( spc, (0,2,1) )
278 self.data_spc = spc
278 self.data_spc = spc
279
279
280 if self.processingHeaderObj.flag_cspc:
280 if self.processingHeaderObj.flag_cspc:
281 cspc = numpy.transpose( cspc, (0,2,1) )
281 cspc = numpy.transpose( cspc, (0,2,1) )
282 self.data_cspc = cspc['real'] + cspc['imag']*1j
282 self.data_cspc = cspc['real'] + cspc['imag']*1j
283 else:
283 else:
284 self.data_cspc = None
284 self.data_cspc = None
285
285
286 if self.processingHeaderObj.flag_dc:
286 if self.processingHeaderObj.flag_dc:
287 self.data_dc = dc['real'] + dc['imag']*1j
287 self.data_dc = dc['real'] + dc['imag']*1j
288 else:
288 else:
289 self.data_dc = None
289 self.data_dc = None
290
290
291 self.flagIsNewFile = 0
291 self.flagIsNewFile = 0
292 self.flagIsNewBlock = 1
292 self.flagIsNewBlock = 1
293
293
294 self.nTotalBlocks += 1
294 self.nTotalBlocks += 1
295 self.nReadBlocks += 1
295 self.nReadBlocks += 1
296
296
297 return 1
297 return 1
298
298
299 def getFirstHeader(self):
299 def getFirstHeader(self):
300
300
301 self.getBasicHeader()
301 self.getBasicHeader()
302
302
303 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
303 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
304
304
305 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
305 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
306
306
307 # self.dataOut.ippSeconds = self.ippSeconds
307 # self.dataOut.ippSeconds = self.ippSeconds
308
308
309 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock
309 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock
310
310
311 self.dataOut.dtype = self.dtype
311 self.dataOut.dtype = self.dtype
312
312
313 # self.dataOut.nPairs = self.nPairs
313 # self.dataOut.nPairs = self.nPairs
314
314
315 self.dataOut.pairsList = self.rdPairList
315 self.dataOut.pairsList = self.rdPairList
316
316
317 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
317 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
318
318
319 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
319 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
320
320
321 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
321 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
322
322
323 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
323 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
324
324
325 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
325 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
326
326
327 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
327 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
328
328
329 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
329 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
330
330
331 self.dataOut.flagShiftFFT = True #Data is always shifted
331 self.dataOut.flagShiftFFT = True #Data is always shifted
332
332
333 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
333 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
334
334
335 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data esta sin flip
335 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data esta sin flip
336
336
337 def getData(self):
337 def getData(self):
338 """
338 """
339 First method to execute before "RUN" is called.
339 First method to execute before "RUN" is called.
340
340
341 Copia el buffer de lectura a la clase "Spectra",
341 Copia el buffer de lectura a la clase "Spectra",
342 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
342 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
343 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
343 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
344
344
345 Return:
345 Return:
346 0 : Si no hay mas archivos disponibles
346 0 : Si no hay mas archivos disponibles
347 1 : Si hizo una buena copia del buffer
347 1 : Si hizo una buena copia del buffer
348
348
349 Affected:
349 Affected:
350 self.dataOut
350 self.dataOut
351
351
352 self.flagDiscontinuousBlock
352 self.flagDiscontinuousBlock
353 self.flagIsNewBlock
353 self.flagIsNewBlock
354 """
354 """
355
355
356 if self.flagNoMoreFiles:
356 if self.flagNoMoreFiles:
357 self.dataOut.flagNoData = True
357 self.dataOut.flagNoData = True
358 print('Process finished')
358 print('Process finished')
359 return 0
359 return 0
360
360
361 self.flagDiscontinuousBlock = 0
361 self.flagDiscontinuousBlock = 0
362 self.flagIsNewBlock = 0
362 self.flagIsNewBlock = 0
363
363
364 if self.__hasNotDataInBuffer():
364 if self.__hasNotDataInBuffer():
365
365
366 if not( self.readNextBlock() ):
366 if not( self.readNextBlock() ):
367 self.dataOut.flagNoData = True
367 self.dataOut.flagNoData = True
368 return 0
368 return 0
369
369
370 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
370 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
371
371
372 if self.data_spc is None:
372 if self.data_spc is None:
373 self.dataOut.flagNoData = True
373 self.dataOut.flagNoData = True
374 return 0
374 return 0
375
375
376 self.getBasicHeader()
376 self.getBasicHeader()
377
377
378 self.getFirstHeader()
378 self.getFirstHeader()
379
379
380 self.dataOut.data_spc = self.data_spc
380 self.dataOut.data_spc = self.data_spc
381
381
382 self.dataOut.data_cspc = self.data_cspc
382 self.dataOut.data_cspc = self.data_cspc
383
383
384 self.dataOut.data_dc = self.data_dc
384 self.dataOut.data_dc = self.data_dc
385
385
386 self.dataOut.flagNoData = False
386 self.dataOut.flagNoData = False
387
387
388 self.dataOut.realtime = self.online
388 self.dataOut.realtime = self.online
389
389
390 return self.dataOut.data_spc
390 return self.dataOut.data_spc
391
391
392 class SpectraWriter(JRODataWriter, Operation):
392 class SpectraWriter(JRODataWriter, Operation):
393
393
394 """
394 """
395 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
395 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
396 de los datos siempre se realiza por bloques.
396 de los datos siempre se realiza por bloques.
397 """
397 """
398
398
399 ext = ".pdata"
399 ext = ".pdata"
400
400
401 optchar = "P"
401 optchar = "P"
402
402
403 shape_spc_Buffer = None
403 shape_spc_Buffer = None
404
404
405 shape_cspc_Buffer = None
405 shape_cspc_Buffer = None
406
406
407 shape_dc_Buffer = None
407 shape_dc_Buffer = None
408
408
409 data_spc = None
409 data_spc = None
410
410
411 data_cspc = None
411 data_cspc = None
412
412
413 data_dc = None
413 data_dc = None
414
414
415 # dataOut = None
415 # dataOut = None
416
416
417 def __init__(self, **kwargs):
417 def __init__(self, **kwargs):
418 """
418 """
419 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
419 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
420
420
421 Affected:
421 Affected:
422 self.dataOut
422 self.dataOut
423 self.basicHeaderObj
423 self.basicHeaderObj
424 self.systemHeaderObj
424 self.systemHeaderObj
425 self.radarControllerHeaderObj
425 self.radarControllerHeaderObj
426 self.processingHeaderObj
426 self.processingHeaderObj
427
427
428 Return: None
428 Return: None
429 """
429 """
430
430
431 Operation.__init__(self, **kwargs)
431 Operation.__init__(self, **kwargs)
432
432
433 self.isConfig = False
433 self.isConfig = False
434
434
435 self.nTotalBlocks = 0
435 self.nTotalBlocks = 0
436
436
437 self.data_spc = None
437 self.data_spc = None
438
438
439 self.data_cspc = None
439 self.data_cspc = None
440
440
441 self.data_dc = None
441 self.data_dc = None
442
442
443 self.fp = None
443 self.fp = None
444
444
445 self.flagIsNewFile = 1
445 self.flagIsNewFile = 1
446
446
447 self.nTotalBlocks = 0
447 self.nTotalBlocks = 0
448
448
449 self.flagIsNewBlock = 0
449 self.flagIsNewBlock = 0
450
450
451 self.setFile = None
451 self.setFile = None
452
452
453 self.dtype = None
453 self.dtype = None
454
454
455 self.path = None
455 self.path = None
456
456
457 self.noMoreFiles = 0
457 self.noMoreFiles = 0
458
458
459 self.filename = None
459 self.filename = None
460
460
461 self.basicHeaderObj = BasicHeader(LOCALTIME)
461 self.basicHeaderObj = BasicHeader(LOCALTIME)
462
462
463 self.systemHeaderObj = SystemHeader()
463 self.systemHeaderObj = SystemHeader()
464
464
465 self.radarControllerHeaderObj = RadarControllerHeader()
465 self.radarControllerHeaderObj = RadarControllerHeader()
466
466
467 self.processingHeaderObj = ProcessingHeader()
467 self.processingHeaderObj = ProcessingHeader()
468
468
469
469
470 def hasAllDataInBuffer(self):
470 def hasAllDataInBuffer(self):
471 return 1
471 return 1
472
472
473
473
474 def setBlockDimension(self):
474 def setBlockDimension(self):
475 """
475 """
476 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
476 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
477
477
478 Affected:
478 Affected:
479 self.shape_spc_Buffer
479 self.shape_spc_Buffer
480 self.shape_cspc_Buffer
480 self.shape_cspc_Buffer
481 self.shape_dc_Buffer
481 self.shape_dc_Buffer
482
482
483 Return: None
483 Return: None
484 """
484 """
485 self.shape_spc_Buffer = (self.dataOut.nChannels,
485 self.shape_spc_Buffer = (self.dataOut.nChannels,
486 self.processingHeaderObj.nHeights,
486 self.processingHeaderObj.nHeights,
487 self.processingHeaderObj.profilesPerBlock)
487 self.processingHeaderObj.profilesPerBlock)
488
488
489 self.shape_cspc_Buffer = (self.dataOut.nPairs,
489 self.shape_cspc_Buffer = (self.dataOut.nPairs,
490 self.processingHeaderObj.nHeights,
490 self.processingHeaderObj.nHeights,
491 self.processingHeaderObj.profilesPerBlock)
491 self.processingHeaderObj.profilesPerBlock)
492
492
493 self.shape_dc_Buffer = (self.dataOut.nChannels,
493 self.shape_dc_Buffer = (self.dataOut.nChannels,
494 self.processingHeaderObj.nHeights)
494 self.processingHeaderObj.nHeights)
495
495
496
496
497 def writeBlock(self):
497 def writeBlock(self):
498 """
498 """
499 Escribe el buffer en el file designado
499 Escribe el buffer en el file designado
500
500
501 Affected:
501 Affected:
502 self.data_spc
502 self.data_spc
503 self.data_cspc
503 self.data_cspc
504 self.data_dc
504 self.data_dc
505 self.flagIsNewFile
505 self.flagIsNewFile
506 self.flagIsNewBlock
506 self.flagIsNewBlock
507 self.nTotalBlocks
507 self.nTotalBlocks
508 self.nWriteBlocks
508 self.nWriteBlocks
509
509
510 Return: None
510 Return: None
511 """
511 """
512
512
513 spc = numpy.transpose( self.data_spc, (0,2,1) )
513 spc = numpy.transpose( self.data_spc, (0,2,1) )
514 if not( self.processingHeaderObj.shif_fft ):
514 if not self.processingHeaderObj.shif_fft:
515 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
515 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
516 data = spc.reshape((-1))
516 data = spc.reshape((-1))
517 data = data.astype(self.dtype[0])
517 data = data.astype(self.dtype[0])
518 data.tofile(self.fp)
518 data.tofile(self.fp)
519
519
520 if self.data_cspc is not None:
520 if self.data_cspc is not None:
521 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
521 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
522 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
522 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
523 if not( self.processingHeaderObj.shif_fft ):
523 if not self.processingHeaderObj.shif_fft:
524 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
524 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
525 data['real'] = cspc.real
525 data['real'] = cspc.real
526 data['imag'] = cspc.imag
526 data['imag'] = cspc.imag
527 data = data.reshape((-1))
527 data = data.reshape((-1))
528 data.tofile(self.fp)
528 data.tofile(self.fp)
529
529
530 if self.data_dc is not None:
530 if self.data_dc is not None:
531 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
531 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
532 dc = self.data_dc
532 dc = self.data_dc
533 data['real'] = dc.real
533 data['real'] = dc.real
534 data['imag'] = dc.imag
534 data['imag'] = dc.imag
535 data = data.reshape((-1))
535 data = data.reshape((-1))
536 data.tofile(self.fp)
536 data.tofile(self.fp)
537
537
538 # self.data_spc.fill(0)
538 # self.data_spc.fill(0)
539 #
539 #
540 # if self.data_dc is not None:
540 # if self.data_dc is not None:
541 # self.data_dc.fill(0)
541 # self.data_dc.fill(0)
542 #
542 #
543 # if self.data_cspc is not None:
543 # if self.data_cspc is not None:
544 # self.data_cspc.fill(0)
544 # self.data_cspc.fill(0)
545
545
546 self.flagIsNewFile = 0
546 self.flagIsNewFile = 0
547 self.flagIsNewBlock = 1
547 self.flagIsNewBlock = 1
548 self.nTotalBlocks += 1
548 self.nTotalBlocks += 1
549 self.nWriteBlocks += 1
549 self.nWriteBlocks += 1
550 self.blockIndex += 1
550 self.blockIndex += 1
551
551
552 # print "[Writing] Block = %d04" %self.blockIndex
552 # print "[Writing] Block = %d04" %self.blockIndex
553
553
554 def putData(self):
554 def putData(self):
555 """
555 """
556 Setea un bloque de datos y luego los escribe en un file
556 Setea un bloque de datos y luego los escribe en un file
557
557
558 Affected:
558 Affected:
559 self.data_spc
559 self.data_spc
560 self.data_cspc
560 self.data_cspc
561 self.data_dc
561 self.data_dc
562
562
563 Return:
563 Return:
564 0 : Si no hay data o no hay mas files que puedan escribirse
564 0 : Si no hay data o no hay mas files que puedan escribirse
565 1 : Si se escribio la data de un bloque en un file
565 1 : Si se escribio la data de un bloque en un file
566 """
566 """
567
567
568 if self.dataOut.flagNoData:
568 if self.dataOut.flagNoData:
569 return 0
569 return 0
570
570
571 self.flagIsNewBlock = 0
571 self.flagIsNewBlock = 0
572
572
573 if self.dataOut.flagDiscontinuousBlock:
573 if self.dataOut.flagDiscontinuousBlock:
574 self.data_spc.fill(0)
574 self.data_spc.fill(0)
575 if self.dataOut.data_cspc is not None:
575 if self.dataOut.data_cspc is not None:
576 self.data_cspc.fill(0)
576 self.data_cspc.fill(0)
577 if self.dataOut.data_dc is not None:
577 if self.dataOut.data_dc is not None:
578 self.data_dc.fill(0)
578 self.data_dc.fill(0)
579 self.setNextFile()
579 self.setNextFile()
580
580
581 if self.flagIsNewFile == 0:
581 if self.flagIsNewFile == 0:
582 self.setBasicHeader()
582 self.setBasicHeader()
583
583
584 self.data_spc = self.dataOut.data_spc.copy()
584 self.data_spc = self.dataOut.data_spc.copy()
585
585
586 if self.dataOut.data_cspc is not None:
586 if self.dataOut.data_cspc is not None:
587 self.data_cspc = self.dataOut.data_cspc.copy()
587 self.data_cspc = self.dataOut.data_cspc.copy()
588
588
589 if self.dataOut.data_dc is not None:
589 if self.dataOut.data_dc is not None:
590 self.data_dc = self.dataOut.data_dc.copy()
590 self.data_dc = self.dataOut.data_dc.copy()
591
591
592 # #self.processingHeaderObj.dataBlocksPerFile)
592 # #self.processingHeaderObj.dataBlocksPerFile)
593 if self.hasAllDataInBuffer():
593 if self.hasAllDataInBuffer():
594 # self.setFirstHeader()
594 # self.setFirstHeader()
595 self.writeNextBlock()
595 self.writeNextBlock()
596
596
597 return 1
597 return 1
598
598
599 def __getBlockSize(self):
599 def __getBlockSize(self):
600 '''
600 '''
601 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
601 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
602 '''
602 '''
603
603
604 dtype_width = self.getDtypeWidth()
604 dtype_width = self.getDtypeWidth()
605
605
606 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
606 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
607
607
608 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
608 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
609 blocksize = (pts2write_SelfSpectra*dtype_width)
609 blocksize = (pts2write_SelfSpectra*dtype_width)
610
610
611 if self.dataOut.data_cspc is not None:
611 if self.dataOut.data_cspc is not None:
612 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
612 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
613 blocksize += (pts2write_CrossSpectra*dtype_width*2)
613 blocksize += (pts2write_CrossSpectra*dtype_width*2)
614
614
615 if self.dataOut.data_dc is not None:
615 if self.dataOut.data_dc is not None:
616 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
616 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
617 blocksize += (pts2write_DCchannels*dtype_width*2)
617 blocksize += (pts2write_DCchannels*dtype_width*2)
618
618
619 # blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
619 # blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
620
620
621 return blocksize
621 return blocksize
622
622
623 def setFirstHeader(self):
623 def setFirstHeader(self):
624
624
625 """
625 """
626 Obtiene una copia del First Header
626 Obtiene una copia del First Header
627
627
628 Affected:
628 Affected:
629 self.systemHeaderObj
629 self.systemHeaderObj
630 self.radarControllerHeaderObj
630 self.radarControllerHeaderObj
631 self.dtype
631 self.dtype
632
632
633 Return:
633 Return:
634 None
634 None
635 """
635 """
636
636
637 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
637 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
638 self.systemHeaderObj.nChannels = self.dataOut.nChannels
638 self.systemHeaderObj.nChannels = self.dataOut.nChannels
639 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
639 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
640
640
641 self.processingHeaderObj.dtype = 1 # Spectra
641 self.processingHeaderObj.dtype = 1 # Spectra
642 self.processingHeaderObj.blockSize = self.__getBlockSize()
642 self.processingHeaderObj.blockSize = self.__getBlockSize()
643 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
643 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
644 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
644 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
645 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
645 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
646 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
646 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
647 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
647 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
648 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
648 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
649 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
649 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
650
650
651 if self.processingHeaderObj.totalSpectra > 0:
651 if self.processingHeaderObj.totalSpectra > 0:
652 channelList = []
652 channelList = []
653 for channel in range(self.dataOut.nChannels):
653 for channel in range(self.dataOut.nChannels):
654 channelList.append(channel)
654 channelList.append(channel)
655 channelList.append(channel)
655 channelList.append(channel)
656
656
657 pairsList = []
657 pairsList = []
658 if self.dataOut.nPairs > 0:
658 if self.dataOut.nPairs > 0:
659 for pair in self.dataOut.pairsList:
659 for pair in self.dataOut.pairsList:
660 pairsList.append(pair[0])
660 pairsList.append(pair[0])
661 pairsList.append(pair[1])
661 pairsList.append(pair[1])
662
662
663 spectraComb = channelList + pairsList
663 spectraComb = channelList + pairsList
664 spectraComb = numpy.array(spectraComb, dtype="u1")
664 spectraComb = numpy.array(spectraComb, dtype="u1")
665 self.processingHeaderObj.spectraComb = spectraComb
665 self.processingHeaderObj.spectraComb = spectraComb
666
666
667 if self.dataOut.code is not None:
667 if self.dataOut.code is not None:
668 self.processingHeaderObj.code = self.dataOut.code
668 self.processingHeaderObj.code = self.dataOut.code
669 self.processingHeaderObj.nCode = self.dataOut.nCode
669 self.processingHeaderObj.nCode = self.dataOut.nCode
670 self.processingHeaderObj.nBaud = self.dataOut.nBaud
670 self.processingHeaderObj.nBaud = self.dataOut.nBaud
671
671
672 if self.processingHeaderObj.nWindows != 0:
672 if self.processingHeaderObj.nWindows != 0:
673 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
673 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
674 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
674 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
675 self.processingHeaderObj.nHeights = self.dataOut.nHeights
675 self.processingHeaderObj.nHeights = self.dataOut.nHeights
676 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
676 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
677
677
678 self.processingHeaderObj.processFlags = self.getProcessFlags()
678 self.processingHeaderObj.processFlags = self.getProcessFlags()
679
679
680 self.setBasicHeader() No newline at end of file
680 self.setBasicHeader()
@@ -1,139 +1,139
1 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
1 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
2 #define NUM_CPY_THREADS 8
2 #define NUM_CPY_THREADS 8
3 #include <Python.h>
3 #include <Python.h>
4 #include <numpy/arrayobject.h>
4 #include <numpy/arrayobject.h>
5 #include <math.h>
5 #include <math.h>
6 #include <complex.h>
6 #include <complex.h>
7 #include <time.h>
7 #include <time.h>
8
8
9 // void printArr(int *array);
9 // void printArr(int *array);
10 static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args);
10 static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args);
11 static PyObject *correlateByBlock(PyObject *self, PyObject *args);
11 static PyObject *correlateByBlock(PyObject *self, PyObject *args);
12 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
12 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
13 #define PyMODINIT_FUNC void
13 #define PyMODINIT_FUNC void
14 #endif
14 #endif
15
15
16 static PyMethodDef extensionsMethods[] = {
16 static PyMethodDef extensionsMethods[] = {
17 { "correlateByBlock", (PyCFunction)correlateByBlock, METH_VARARGS, "get correlation by block" },
17 { "correlateByBlock", (PyCFunction)correlateByBlock, METH_VARARGS, "get correlation by block" },
18 { "hildebrand_sekhon", (PyCFunction)hildebrand_sekhon, METH_VARARGS, "get noise with hildebrand_sekhon" },
18 { "hildebrand_sekhon", (PyCFunction)hildebrand_sekhon, METH_VARARGS, "get noise with hildebrand_sekhon" },
19 { NULL, NULL, 0, NULL }
19 { NULL, NULL, 0, NULL }
20 };
20 };
21
21
22 PyMODINIT_FUNC initcSchain() {
22 PyMODINIT_FUNC initcSchain() {
23 Py_InitModule("cSchain", extensionsMethods);
23 Py_InitModule("cSchain", extensionsMethods);
24 import_array();
24 import_array();
25 }
25 }
26
26
27 static PyObject *correlateByBlock(PyObject *self, PyObject *args) {
27 static PyObject *correlateByBlock(PyObject *self, PyObject *args) {
28
28
29 // int *x = (int*) malloc(4000000 * 216 * sizeof(int));;
29 // int *x = (int*) malloc(4000000 * 216 * sizeof(int));;
30 // int a = 5;
30 // int a = 5;
31 // x = &a;
31 // x = &a;
32 // int b = 6;
32 // int b = 6;
33 // x = &b;
33 // x = &b;
34 // printf("Antes de imprimir x \n");
34 // printf("Antes de imprimir x \n");
35 // printf("%d \n", x[0]);
35 // printf("%d \n", x[0]);
36
36
37 PyObject *data_obj1, *data_obj2;
37 PyObject *data_obj1, *data_obj2;
38 PyArrayObject *data_array1, *data_array2, *correlateRow, *out, *dataRow, *codeRow; //, ,
38 PyArrayObject *data_array1, *data_array2, *correlateRow, *out, *dataRow, *codeRow; //, ,
39 int mode;
39 int mode;
40
40
41 if (!PyArg_ParseTuple(args, "OOi", &data_obj1, &data_obj2, &mode)) return NULL;
41 if (!PyArg_ParseTuple(args, "OOi", &data_obj1, &data_obj2, &mode)) return NULL;
42
42
43 data_array1 = (PyArrayObject *) PyArray_FROM_OTF(data_obj1, NPY_COMPLEX128, NPY_ARRAY_DEFAULT);
43 data_array1 = (PyArrayObject *) PyArray_FROM_OTF(data_obj1, NPY_COMPLEX128, NPY_ARRAY_IN_ARRAY);
44 data_array2 = (PyArrayObject *) PyArray_FROM_OTF(data_obj2, NPY_FLOAT64, NPY_ARRAY_DEFAULT);
44 data_array2 = (PyArrayObject *) PyArray_FROM_OTF(data_obj2, NPY_FLOAT64, NPY_ARRAY_IN_ARRAY);
45
45
46 npy_intp dims[1];
46 npy_intp dims[1];
47 dims[0] = 200;
47 dims[0] = 200;
48 npy_intp dims_code[1];
48 npy_intp dims_code[1];
49 dims_code[0] = 16;
49 dims_code[0] = 16;
50
50
51 double complex * dataRaw;
51 double complex * dataRaw;
52 double * codeRaw;
52 double * codeRaw;
53 dataRaw = (double complex*) PyArray_DATA(data_array1);
53 dataRaw = (double complex*) PyArray_DATA(data_array1);
54 codeRaw = (double *) PyArray_DATA(data_array2);
54 codeRaw = (double *) PyArray_DATA(data_array2);
55 double complex ** outC = malloc(40000*200*sizeof(double complex));
55 double complex ** outC = malloc(40000*200*sizeof(double complex));
56 int i;
56 int i;
57
57
58 clock_t start = clock();
58 clock_t start = clock();
59 for(i=0; i<40000; i++){
59 for(i=0; i<40000; i++){
60 // codeRow = PyArray_SimpleNewFromData(1, dims_code, NPY_FLOAT64, codeRaw + 16 * i);
60 // codeRow = PyArray_SimpleNewFromData(1, dims_code, NPY_FLOAT64, codeRaw + 16 * i);
61 // dataRow = PyArray_SimpleNewFromData(1, dims, NPY_COMPLEX128, dataRaw + 200 * i);
61 // dataRow = PyArray_SimpleNewFromData(1, dims, NPY_COMPLEX128, dataRaw + 200 * i);
62 // Py_INCREF(codeRow);
62 // Py_INCREF(codeRow);
63 // Py_INCREF(dataRow);
63 // Py_INCREF(dataRow);
64 // PyArray_ENABLEFLAGS(codeRow, NPY_ARRAY_OWNDATA);
64 // PyArray_ENABLEFLAGS(codeRow, NPY_ARRAY_OWNDATA);
65 // PyArray_ENABLEFLAGS(dataRow, NPY_ARRAY_OWNDATA);
65 // PyArray_ENABLEFLAGS(dataRow, NPY_ARRAY_OWNDATA);
66 correlateRow = (PyArrayObject *) PyArray_Correlate2(PyArray_SimpleNewFromData(1, dims_code, NPY_FLOAT64, codeRaw + 16 * i), PyArray_SimpleNewFromData(1, dims, NPY_COMPLEX128, dataRaw + 200 * i), (npy_intp) 2);
66 correlateRow = (PyArrayObject *) PyArray_Correlate2(PyArray_SimpleNewFromData(1, dims_code, NPY_FLOAT64, codeRaw + 16 * i), PyArray_SimpleNewFromData(1, dims, NPY_COMPLEX128, dataRaw + 200 * i), (npy_intp) 2);
67 //Py_INCREF(correlateRow);
67 //Py_INCREF(correlateRow);
68 // PyArray_ENABLEFLAGS(correlateRow, NPY_ARRAY_OWNDATA);
68 // PyArray_ENABLEFLAGS(correlateRow, NPY_ARRAY_OWNDATA);
69 memcpy(outC + 200*i, (double complex*) PyArray_DATA(correlateRow), 200 * sizeof(double complex));
69 memcpy(outC + 200*i, (double complex*) PyArray_DATA(correlateRow), 200 * sizeof(double complex));
70
70
71 Py_DECREF(correlateRow);
71 Py_DECREF(correlateRow);
72 // Py_DECREF(codeRow);
72 // Py_DECREF(codeRow);
73 // Py_DECREF(dataRow);
73 // Py_DECREF(dataRow);
74 }
74 }
75 clock_t end = clock();
75 clock_t end = clock();
76 float seconds = (float)(end - start) / CLOCKS_PER_SEC;
76 float seconds = (float)(end - start) / CLOCKS_PER_SEC;
77 printf("%f", seconds);
77 printf("%f", seconds);
78 //
78 //
79 npy_intp dimsret[2];
79 npy_intp dimsret[2];
80 dimsret[0] = 40000;
80 dimsret[0] = 40000;
81 dimsret[1] = 200;
81 dimsret[1] = 200;
82 out = PyArray_SimpleNewFromData(2, dimsret, NPY_COMPLEX128, outC);
82 out = PyArray_SimpleNewFromData(2, dimsret, NPY_COMPLEX128, outC);
83 PyArray_ENABLEFLAGS(out, NPY_ARRAY_OWNDATA);
83 PyArray_ENABLEFLAGS(out, NPY_ARRAY_OWNDATA);
84 //Py_INCREF(out);
84 //Py_INCREF(out);
85 Py_DECREF(data_array1);
85 Py_DECREF(data_array1);
86 Py_DECREF(data_array2);
86 Py_DECREF(data_array2);
87 // PyArray_DebugPrint(out);
87 // PyArray_DebugPrint(out);
88 // Py_DECREF(data_obj2);
88 // Py_DECREF(data_obj2);
89 // Py_DECREF(data_obj1);
89 // Py_DECREF(data_obj1);
90 // Py_DECREF(codeRow);
90 // Py_DECREF(codeRow);
91 // Py_DECREF(dataRow);
91 // Py_DECREF(dataRow);
92 // free(dataRaw);
92 // free(dataRaw);
93 // free(codeRaw);
93 // free(codeRaw);
94
94
95 return PyArray_Return(out);
95 return PyArray_Return(out);
96 }
96 }
97
97
98 static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args) {
98 static PyObject *hildebrand_sekhon(PyObject *self, PyObject *args) {
99 double navg;
99 double navg;
100 PyObject *data_obj, *data_array;
100 PyObject *data_obj, *data_array;
101
101
102 if (!PyArg_ParseTuple(args, "Od", &data_obj, &navg)) return NULL;
102 if (!PyArg_ParseTuple(args, "Od", &data_obj, &navg)) return NULL;
103 data_array = PyArray_FROM_OTF(data_obj, NPY_FLOAT64, NPY_ARRAY_DEFAULT);
103 data_array = PyArray_FROM_OTF(data_obj, NPY_FLOAT64, NPY_ARRAY_IN_ARRAY);
104 if (data_array == NULL) {
104 if (data_array == NULL) {
105 Py_XDECREF(data_array);
105 Py_XDECREF(data_array);
106 Py_XDECREF(data_obj);
106 Py_XDECREF(data_obj);
107 return NULL;
107 return NULL;
108 }
108 }
109 double *sortdata = (double*)PyArray_DATA(data_array);
109 double *sortdata = (double*)PyArray_DATA(data_array);
110 int lenOfData = (int)PyArray_SIZE(data_array) ;
110 int lenOfData = (int)PyArray_SIZE(data_array) ;
111 double nums_min = lenOfData*0.2;
111 double nums_min = lenOfData*0.2;
112 if (nums_min <= 5) nums_min = 5;
112 if (nums_min <= 5) nums_min = 5;
113 double sump = 0;
113 double sump = 0;
114 double sumq = 0;
114 double sumq = 0;
115 int j = 0;
115 int j = 0;
116 int cont = 1;
116 int cont = 1;
117 double rtest = 0;
117 double rtest = 0;
118 while ((cont == 1) && (j < lenOfData)) {
118 while ((cont == 1) && (j < lenOfData)) {
119 sump = sump + sortdata[j];
119 sump = sump + sortdata[j];
120 sumq = sumq + pow(sortdata[j], 2);
120 sumq = sumq + pow(sortdata[j], 2);
121 if (j > nums_min) {
121 if (j > nums_min) {
122 rtest = (double)j/(j-1) + 1/navg;
122 rtest = (double)j/(j-1) + 1/navg;
123 if ((sumq*j) > (rtest*pow(sump, 2))) {
123 if ((sumq*j) > (rtest*pow(sump, 2))) {
124 j = j - 1;
124 j = j - 1;
125 sump = sump - sortdata[j];
125 sump = sump - sortdata[j];
126 sumq = sumq - pow(sortdata[j],2);
126 sumq = sumq - pow(sortdata[j],2);
127 cont = 0;
127 cont = 0;
128 }
128 }
129 }
129 }
130 j = j + 1;
130 j = j + 1;
131 }
131 }
132
132
133 double lnoise = sump / j;
133 double lnoise = sump / j;
134
134
135 Py_DECREF(data_array);
135 Py_DECREF(data_array);
136
136
137 return Py_BuildValue("d", lnoise);
137 return Py_BuildValue("d", lnoise);
138 }
138 }
139
139
General Comments 0
You need to be logged in to leave comments. Login now