##// END OF EJS Templates
Merge branch 'master' into v3.0-devel
Juan C. Espinoza -
r1172:07f1c959305f 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,679 +1,679
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
9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
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 class SpectraReader(JRODataReader, ProcessingUnit):
13 class SpectraReader(JRODataReader, ProcessingUnit):
14 """
14 """
15 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
15 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
16 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
16 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
17 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
17 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
18
18
19 paresCanalesIguales * alturas * perfiles (Self Spectra)
19 paresCanalesIguales * alturas * perfiles (Self Spectra)
20 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
20 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
21 canales * alturas (DC Channels)
21 canales * alturas (DC Channels)
22
22
23 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
23 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
24 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
24 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
25 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
25 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
26 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
26 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
27
27
28 Example:
28 Example:
29 dpath = "/home/myuser/data"
29 dpath = "/home/myuser/data"
30
30
31 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
31 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
32
32
33 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
33 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
34
34
35 readerObj = SpectraReader()
35 readerObj = SpectraReader()
36
36
37 readerObj.setup(dpath, startTime, endTime)
37 readerObj.setup(dpath, startTime, endTime)
38
38
39 while(True):
39 while(True):
40
40
41 readerObj.getData()
41 readerObj.getData()
42
42
43 print readerObj.data_spc
43 print readerObj.data_spc
44
44
45 print readerObj.data_cspc
45 print readerObj.data_cspc
46
46
47 print readerObj.data_dc
47 print readerObj.data_dc
48
48
49 if readerObj.flagNoMoreFiles:
49 if readerObj.flagNoMoreFiles:
50 break
50 break
51
51
52 """
52 """
53
53
54 pts2read_SelfSpectra = 0
54 pts2read_SelfSpectra = 0
55
55
56 pts2read_CrossSpectra = 0
56 pts2read_CrossSpectra = 0
57
57
58 pts2read_DCchannels = 0
58 pts2read_DCchannels = 0
59
59
60 ext = ".pdata"
60 ext = ".pdata"
61
61
62 optchar = "P"
62 optchar = "P"
63
63
64 dataOut = None
64 dataOut = None
65
65
66 nRdChannels = None
66 nRdChannels = None
67
67
68 nRdPairs = None
68 nRdPairs = None
69
69
70 rdPairList = []
70 rdPairList = []
71
71
72 def __init__(self, **kwargs):
72 def __init__(self, **kwargs):
73 """
73 """
74 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
74 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
75
75
76 Inputs:
76 Inputs:
77 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
77 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
78 almacenar un perfil de datos cada vez que se haga un requerimiento
78 almacenar un perfil de datos cada vez que se haga un requerimiento
79 (getData). El perfil sera obtenido a partir del buffer de datos,
79 (getData). El perfil sera obtenido a partir del buffer de datos,
80 si el buffer esta vacio se hara un nuevo proceso de lectura de un
80 si el buffer esta vacio se hara un nuevo proceso de lectura de un
81 bloque de datos.
81 bloque de datos.
82 Si este parametro no es pasado se creara uno internamente.
82 Si este parametro no es pasado se creara uno internamente.
83
83
84 Affected:
84 Affected:
85 self.dataOut
85 self.dataOut
86
86
87 Return : None
87 Return : None
88 """
88 """
89
89
90 #Eliminar de la base la herencia
90 #Eliminar de la base la herencia
91 ProcessingUnit.__init__(self, **kwargs)
91 ProcessingUnit.__init__(self, **kwargs)
92
92
93 # self.isConfig = False
93 # self.isConfig = False
94
94
95 self.pts2read_SelfSpectra = 0
95 self.pts2read_SelfSpectra = 0
96
96
97 self.pts2read_CrossSpectra = 0
97 self.pts2read_CrossSpectra = 0
98
98
99 self.pts2read_DCchannels = 0
99 self.pts2read_DCchannels = 0
100
100
101 self.datablock = None
101 self.datablock = None
102
102
103 self.utc = None
103 self.utc = None
104
104
105 self.ext = ".pdata"
105 self.ext = ".pdata"
106
106
107 self.optchar = "P"
107 self.optchar = "P"
108
108
109 self.basicHeaderObj = BasicHeader(LOCALTIME)
109 self.basicHeaderObj = BasicHeader(LOCALTIME)
110
110
111 self.systemHeaderObj = SystemHeader()
111 self.systemHeaderObj = SystemHeader()
112
112
113 self.radarControllerHeaderObj = RadarControllerHeader()
113 self.radarControllerHeaderObj = RadarControllerHeader()
114
114
115 self.processingHeaderObj = ProcessingHeader()
115 self.processingHeaderObj = ProcessingHeader()
116
116
117 self.online = 0
117 self.online = 0
118
118
119 self.fp = None
119 self.fp = None
120
120
121 self.idFile = None
121 self.idFile = None
122
122
123 self.dtype = None
123 self.dtype = None
124
124
125 self.fileSizeByHeader = None
125 self.fileSizeByHeader = None
126
126
127 self.filenameList = []
127 self.filenameList = []
128
128
129 self.filename = None
129 self.filename = None
130
130
131 self.fileSize = None
131 self.fileSize = None
132
132
133 self.firstHeaderSize = 0
133 self.firstHeaderSize = 0
134
134
135 self.basicHeaderSize = 24
135 self.basicHeaderSize = 24
136
136
137 self.pathList = []
137 self.pathList = []
138
138
139 self.lastUTTime = 0
139 self.lastUTTime = 0
140
140
141 self.maxTimeStep = 30
141 self.maxTimeStep = 30
142
142
143 self.flagNoMoreFiles = 0
143 self.flagNoMoreFiles = 0
144
144
145 self.set = 0
145 self.set = 0
146
146
147 self.path = None
147 self.path = None
148
148
149 self.delay = 60 #seconds
149 self.delay = 60 #seconds
150
150
151 self.nTries = 3 #quantity tries
151 self.nTries = 3 #quantity tries
152
152
153 self.nFiles = 3 #number of files for searching
153 self.nFiles = 3 #number of files for searching
154
154
155 self.nReadBlocks = 0
155 self.nReadBlocks = 0
156
156
157 self.flagIsNewFile = 1
157 self.flagIsNewFile = 1
158
158
159 self.__isFirstTimeOnline = 1
159 self.__isFirstTimeOnline = 1
160
160
161 # self.ippSeconds = 0
161 # self.ippSeconds = 0
162
162
163 self.flagDiscontinuousBlock = 0
163 self.flagDiscontinuousBlock = 0
164
164
165 self.flagIsNewBlock = 0
165 self.flagIsNewBlock = 0
166
166
167 self.nTotalBlocks = 0
167 self.nTotalBlocks = 0
168
168
169 self.blocksize = 0
169 self.blocksize = 0
170
170
171 self.dataOut = self.createObjByDefault()
171 self.dataOut = self.createObjByDefault()
172
172
173 self.profileIndex = 1 #Always
173 self.profileIndex = 1 #Always
174
174
175
175
176 def createObjByDefault(self):
176 def createObjByDefault(self):
177
177
178 dataObj = Spectra()
178 dataObj = Spectra()
179
179
180 return dataObj
180 return dataObj
181
181
182 def __hasNotDataInBuffer(self):
182 def __hasNotDataInBuffer(self):
183 return 1
183 return 1
184
184
185
185
186 def getBlockDimension(self):
186 def getBlockDimension(self):
187 """
187 """
188 Obtiene la cantidad de puntos a leer por cada bloque de datos
188 Obtiene la cantidad de puntos a leer por cada bloque de datos
189
189
190 Affected:
190 Affected:
191 self.nRdChannels
191 self.nRdChannels
192 self.nRdPairs
192 self.nRdPairs
193 self.pts2read_SelfSpectra
193 self.pts2read_SelfSpectra
194 self.pts2read_CrossSpectra
194 self.pts2read_CrossSpectra
195 self.pts2read_DCchannels
195 self.pts2read_DCchannels
196 self.blocksize
196 self.blocksize
197 self.dataOut.nChannels
197 self.dataOut.nChannels
198 self.dataOut.nPairs
198 self.dataOut.nPairs
199
199
200 Return:
200 Return:
201 None
201 None
202 """
202 """
203 self.nRdChannels = 0
203 self.nRdChannels = 0
204 self.nRdPairs = 0
204 self.nRdPairs = 0
205 self.rdPairList = []
205 self.rdPairList = []
206
206
207 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
207 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
208 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
208 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
209 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
209 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
210 else:
210 else:
211 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
211 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
212 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
212 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
213
213
214 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
214 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
215
215
216 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
216 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
217 self.blocksize = self.pts2read_SelfSpectra
217 self.blocksize = self.pts2read_SelfSpectra
218
218
219 if self.processingHeaderObj.flag_cspc:
219 if self.processingHeaderObj.flag_cspc:
220 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
220 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
221 self.blocksize += self.pts2read_CrossSpectra
221 self.blocksize += self.pts2read_CrossSpectra
222
222
223 if self.processingHeaderObj.flag_dc:
223 if self.processingHeaderObj.flag_dc:
224 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
224 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
225 self.blocksize += self.pts2read_DCchannels
225 self.blocksize += self.pts2read_DCchannels
226
226
227 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
227 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
228
228
229
229
230 def readBlock(self):
230 def readBlock(self):
231 """
231 """
232 Lee el bloque de datos desde la posicion actual del puntero del archivo
232 Lee el bloque de datos desde la posicion actual del puntero del archivo
233 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
233 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
234 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
234 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
235 es seteado a 0
235 es seteado a 0
236
236
237 Return: None
237 Return: None
238
238
239 Variables afectadas:
239 Variables afectadas:
240
240
241 self.flagIsNewFile
241 self.flagIsNewFile
242 self.flagIsNewBlock
242 self.flagIsNewBlock
243 self.nTotalBlocks
243 self.nTotalBlocks
244 self.data_spc
244 self.data_spc
245 self.data_cspc
245 self.data_cspc
246 self.data_dc
246 self.data_dc
247
247
248 Exceptions:
248 Exceptions:
249 Si un bloque leido no es un bloque valido
249 Si un bloque leido no es un bloque valido
250 """
250 """
251 blockOk_flag = False
251 blockOk_flag = False
252 fpointer = self.fp.tell()
252 fpointer = self.fp.tell()
253
253
254 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
254 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
255 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
255 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
256
256
257 if self.processingHeaderObj.flag_cspc:
257 if self.processingHeaderObj.flag_cspc:
258 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
258 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
259 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
259 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
260
260
261 if self.processingHeaderObj.flag_dc:
261 if self.processingHeaderObj.flag_dc:
262 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
262 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
263 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
263 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
264
264
265
265
266 if self.processingHeaderObj.shif_fft:
266 if not self.processingHeaderObj.shif_fft:
267 #desplaza a la derecha en el eje 2 determinadas posiciones
267 #desplaza a la derecha en el eje 2 determinadas posiciones
268 shift = int(self.processingHeaderObj.profilesPerBlock/2)
268 shift = int(self.processingHeaderObj.profilesPerBlock/2)
269 spc = numpy.roll( spc, shift , axis=2 )
269 spc = numpy.roll( spc, shift , axis=2 )
270
270
271 if self.processingHeaderObj.flag_cspc:
271 if self.processingHeaderObj.flag_cspc:
272 #desplaza a la derecha en el eje 2 determinadas posiciones
272 #desplaza a la derecha en el eje 2 determinadas posiciones
273 cspc = numpy.roll( cspc, shift, axis=2 )
273 cspc = numpy.roll( cspc, shift, axis=2 )
274
274
275 #Dimensions : nChannels, nProfiles, nSamples
275 #Dimensions : nChannels, nProfiles, nSamples
276 spc = numpy.transpose( spc, (0,2,1) )
276 spc = numpy.transpose( spc, (0,2,1) )
277 self.data_spc = spc
277 self.data_spc = spc
278
278
279 if self.processingHeaderObj.flag_cspc:
279 if self.processingHeaderObj.flag_cspc:
280 cspc = numpy.transpose( cspc, (0,2,1) )
280 cspc = numpy.transpose( cspc, (0,2,1) )
281 self.data_cspc = cspc['real'] + cspc['imag']*1j
281 self.data_cspc = cspc['real'] + cspc['imag']*1j
282 else:
282 else:
283 self.data_cspc = None
283 self.data_cspc = None
284
284
285 if self.processingHeaderObj.flag_dc:
285 if self.processingHeaderObj.flag_dc:
286 self.data_dc = dc['real'] + dc['imag']*1j
286 self.data_dc = dc['real'] + dc['imag']*1j
287 else:
287 else:
288 self.data_dc = None
288 self.data_dc = None
289
289
290 self.flagIsNewFile = 0
290 self.flagIsNewFile = 0
291 self.flagIsNewBlock = 1
291 self.flagIsNewBlock = 1
292
292
293 self.nTotalBlocks += 1
293 self.nTotalBlocks += 1
294 self.nReadBlocks += 1
294 self.nReadBlocks += 1
295
295
296 return 1
296 return 1
297
297
298 def getFirstHeader(self):
298 def getFirstHeader(self):
299
299
300 self.getBasicHeader()
300 self.getBasicHeader()
301
301
302 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
302 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
303
303
304 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
304 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
305
305
306 # self.dataOut.ippSeconds = self.ippSeconds
306 # self.dataOut.ippSeconds = self.ippSeconds
307
307
308 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock
308 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock
309
309
310 self.dataOut.dtype = self.dtype
310 self.dataOut.dtype = self.dtype
311
311
312 # self.dataOut.nPairs = self.nPairs
312 # self.dataOut.nPairs = self.nPairs
313
313
314 self.dataOut.pairsList = self.rdPairList
314 self.dataOut.pairsList = self.rdPairList
315
315
316 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
316 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
317
317
318 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
318 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
319
319
320 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
320 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
321
321
322 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
322 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
323
323
324 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
324 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
325
325
326 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
326 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
327
327
328 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
328 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
329
329
330 self.dataOut.flagShiftFFT = True #Data is always shifted
330 self.dataOut.flagShiftFFT = True #Data is always shifted
331
331
332 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
332 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
333
333
334 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data esta sin flip
334 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data esta sin flip
335
335
336 def getData(self):
336 def getData(self):
337 """
337 """
338 First method to execute before "RUN" is called.
338 First method to execute before "RUN" is called.
339
339
340 Copia el buffer de lectura a la clase "Spectra",
340 Copia el buffer de lectura a la clase "Spectra",
341 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
341 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
342 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
342 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
343
343
344 Return:
344 Return:
345 0 : Si no hay mas archivos disponibles
345 0 : Si no hay mas archivos disponibles
346 1 : Si hizo una buena copia del buffer
346 1 : Si hizo una buena copia del buffer
347
347
348 Affected:
348 Affected:
349 self.dataOut
349 self.dataOut
350
350
351 self.flagDiscontinuousBlock
351 self.flagDiscontinuousBlock
352 self.flagIsNewBlock
352 self.flagIsNewBlock
353 """
353 """
354
354
355 if self.flagNoMoreFiles:
355 if self.flagNoMoreFiles:
356 self.dataOut.flagNoData = True
356 self.dataOut.flagNoData = True
357 print('Process finished')
357 print('Process finished')
358 return 0
358 return 0
359
359
360 self.flagDiscontinuousBlock = 0
360 self.flagDiscontinuousBlock = 0
361 self.flagIsNewBlock = 0
361 self.flagIsNewBlock = 0
362
362
363 if self.__hasNotDataInBuffer():
363 if self.__hasNotDataInBuffer():
364
364
365 if not( self.readNextBlock() ):
365 if not( self.readNextBlock() ):
366 self.dataOut.flagNoData = True
366 self.dataOut.flagNoData = True
367 return 0
367 return 0
368
368
369 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
369 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
370
370
371 if self.data_spc is None:
371 if self.data_spc is None:
372 self.dataOut.flagNoData = True
372 self.dataOut.flagNoData = True
373 return 0
373 return 0
374
374
375 self.getBasicHeader()
375 self.getBasicHeader()
376
376
377 self.getFirstHeader()
377 self.getFirstHeader()
378
378
379 self.dataOut.data_spc = self.data_spc
379 self.dataOut.data_spc = self.data_spc
380
380
381 self.dataOut.data_cspc = self.data_cspc
381 self.dataOut.data_cspc = self.data_cspc
382
382
383 self.dataOut.data_dc = self.data_dc
383 self.dataOut.data_dc = self.data_dc
384
384
385 self.dataOut.flagNoData = False
385 self.dataOut.flagNoData = False
386
386
387 self.dataOut.realtime = self.online
387 self.dataOut.realtime = self.online
388
388
389 return self.dataOut.data_spc
389 return self.dataOut.data_spc
390
390
391 class SpectraWriter(JRODataWriter, Operation):
391 class SpectraWriter(JRODataWriter, Operation):
392
392
393 """
393 """
394 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
394 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
395 de los datos siempre se realiza por bloques.
395 de los datos siempre se realiza por bloques.
396 """
396 """
397
397
398 ext = ".pdata"
398 ext = ".pdata"
399
399
400 optchar = "P"
400 optchar = "P"
401
401
402 shape_spc_Buffer = None
402 shape_spc_Buffer = None
403
403
404 shape_cspc_Buffer = None
404 shape_cspc_Buffer = None
405
405
406 shape_dc_Buffer = None
406 shape_dc_Buffer = None
407
407
408 data_spc = None
408 data_spc = None
409
409
410 data_cspc = None
410 data_cspc = None
411
411
412 data_dc = None
412 data_dc = None
413
413
414 # dataOut = None
414 # dataOut = None
415
415
416 def __init__(self, **kwargs):
416 def __init__(self, **kwargs):
417 """
417 """
418 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
418 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
419
419
420 Affected:
420 Affected:
421 self.dataOut
421 self.dataOut
422 self.basicHeaderObj
422 self.basicHeaderObj
423 self.systemHeaderObj
423 self.systemHeaderObj
424 self.radarControllerHeaderObj
424 self.radarControllerHeaderObj
425 self.processingHeaderObj
425 self.processingHeaderObj
426
426
427 Return: None
427 Return: None
428 """
428 """
429
429
430 Operation.__init__(self, **kwargs)
430 Operation.__init__(self, **kwargs)
431
431
432 self.isConfig = False
432 self.isConfig = False
433
433
434 self.nTotalBlocks = 0
434 self.nTotalBlocks = 0
435
435
436 self.data_spc = None
436 self.data_spc = None
437
437
438 self.data_cspc = None
438 self.data_cspc = None
439
439
440 self.data_dc = None
440 self.data_dc = None
441
441
442 self.fp = None
442 self.fp = None
443
443
444 self.flagIsNewFile = 1
444 self.flagIsNewFile = 1
445
445
446 self.nTotalBlocks = 0
446 self.nTotalBlocks = 0
447
447
448 self.flagIsNewBlock = 0
448 self.flagIsNewBlock = 0
449
449
450 self.setFile = None
450 self.setFile = None
451
451
452 self.dtype = None
452 self.dtype = None
453
453
454 self.path = None
454 self.path = None
455
455
456 self.noMoreFiles = 0
456 self.noMoreFiles = 0
457
457
458 self.filename = None
458 self.filename = None
459
459
460 self.basicHeaderObj = BasicHeader(LOCALTIME)
460 self.basicHeaderObj = BasicHeader(LOCALTIME)
461
461
462 self.systemHeaderObj = SystemHeader()
462 self.systemHeaderObj = SystemHeader()
463
463
464 self.radarControllerHeaderObj = RadarControllerHeader()
464 self.radarControllerHeaderObj = RadarControllerHeader()
465
465
466 self.processingHeaderObj = ProcessingHeader()
466 self.processingHeaderObj = ProcessingHeader()
467
467
468
468
469 def hasAllDataInBuffer(self):
469 def hasAllDataInBuffer(self):
470 return 1
470 return 1
471
471
472
472
473 def setBlockDimension(self):
473 def setBlockDimension(self):
474 """
474 """
475 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
475 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
476
476
477 Affected:
477 Affected:
478 self.shape_spc_Buffer
478 self.shape_spc_Buffer
479 self.shape_cspc_Buffer
479 self.shape_cspc_Buffer
480 self.shape_dc_Buffer
480 self.shape_dc_Buffer
481
481
482 Return: None
482 Return: None
483 """
483 """
484 self.shape_spc_Buffer = (self.dataOut.nChannels,
484 self.shape_spc_Buffer = (self.dataOut.nChannels,
485 self.processingHeaderObj.nHeights,
485 self.processingHeaderObj.nHeights,
486 self.processingHeaderObj.profilesPerBlock)
486 self.processingHeaderObj.profilesPerBlock)
487
487
488 self.shape_cspc_Buffer = (self.dataOut.nPairs,
488 self.shape_cspc_Buffer = (self.dataOut.nPairs,
489 self.processingHeaderObj.nHeights,
489 self.processingHeaderObj.nHeights,
490 self.processingHeaderObj.profilesPerBlock)
490 self.processingHeaderObj.profilesPerBlock)
491
491
492 self.shape_dc_Buffer = (self.dataOut.nChannels,
492 self.shape_dc_Buffer = (self.dataOut.nChannels,
493 self.processingHeaderObj.nHeights)
493 self.processingHeaderObj.nHeights)
494
494
495
495
496 def writeBlock(self):
496 def writeBlock(self):
497 """
497 """
498 Escribe el buffer en el file designado
498 Escribe el buffer en el file designado
499
499
500 Affected:
500 Affected:
501 self.data_spc
501 self.data_spc
502 self.data_cspc
502 self.data_cspc
503 self.data_dc
503 self.data_dc
504 self.flagIsNewFile
504 self.flagIsNewFile
505 self.flagIsNewBlock
505 self.flagIsNewBlock
506 self.nTotalBlocks
506 self.nTotalBlocks
507 self.nWriteBlocks
507 self.nWriteBlocks
508
508
509 Return: None
509 Return: None
510 """
510 """
511
511
512 spc = numpy.transpose( self.data_spc, (0,2,1) )
512 spc = numpy.transpose( self.data_spc, (0,2,1) )
513 if self.processingHeaderObj.shif_fft:
513 if not self.processingHeaderObj.shif_fft:
514 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
514 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
515 data = spc.reshape((-1))
515 data = spc.reshape((-1))
516 data = data.astype(self.dtype[0])
516 data = data.astype(self.dtype[0])
517 data.tofile(self.fp)
517 data.tofile(self.fp)
518
518
519 if self.data_cspc is not None:
519 if self.data_cspc is not None:
520 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
520 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
521 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
521 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
522 if self.processingHeaderObj.shif_fft:
522 if not self.processingHeaderObj.shif_fft:
523 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
523 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
524 data['real'] = cspc.real
524 data['real'] = cspc.real
525 data['imag'] = cspc.imag
525 data['imag'] = cspc.imag
526 data = data.reshape((-1))
526 data = data.reshape((-1))
527 data.tofile(self.fp)
527 data.tofile(self.fp)
528
528
529 if self.data_dc is not None:
529 if self.data_dc is not None:
530 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
530 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
531 dc = self.data_dc
531 dc = self.data_dc
532 data['real'] = dc.real
532 data['real'] = dc.real
533 data['imag'] = dc.imag
533 data['imag'] = dc.imag
534 data = data.reshape((-1))
534 data = data.reshape((-1))
535 data.tofile(self.fp)
535 data.tofile(self.fp)
536
536
537 # self.data_spc.fill(0)
537 # self.data_spc.fill(0)
538 #
538 #
539 # if self.data_dc is not None:
539 # if self.data_dc is not None:
540 # self.data_dc.fill(0)
540 # self.data_dc.fill(0)
541 #
541 #
542 # if self.data_cspc is not None:
542 # if self.data_cspc is not None:
543 # self.data_cspc.fill(0)
543 # self.data_cspc.fill(0)
544
544
545 self.flagIsNewFile = 0
545 self.flagIsNewFile = 0
546 self.flagIsNewBlock = 1
546 self.flagIsNewBlock = 1
547 self.nTotalBlocks += 1
547 self.nTotalBlocks += 1
548 self.nWriteBlocks += 1
548 self.nWriteBlocks += 1
549 self.blockIndex += 1
549 self.blockIndex += 1
550
550
551 # print "[Writing] Block = %d04" %self.blockIndex
551 # print "[Writing] Block = %d04" %self.blockIndex
552
552
553 def putData(self):
553 def putData(self):
554 """
554 """
555 Setea un bloque de datos y luego los escribe en un file
555 Setea un bloque de datos y luego los escribe en un file
556
556
557 Affected:
557 Affected:
558 self.data_spc
558 self.data_spc
559 self.data_cspc
559 self.data_cspc
560 self.data_dc
560 self.data_dc
561
561
562 Return:
562 Return:
563 0 : Si no hay data o no hay mas files que puedan escribirse
563 0 : Si no hay data o no hay mas files que puedan escribirse
564 1 : Si se escribio la data de un bloque en un file
564 1 : Si se escribio la data de un bloque en un file
565 """
565 """
566
566
567 if self.dataOut.flagNoData:
567 if self.dataOut.flagNoData:
568 return 0
568 return 0
569
569
570 self.flagIsNewBlock = 0
570 self.flagIsNewBlock = 0
571
571
572 if self.dataOut.flagDiscontinuousBlock:
572 if self.dataOut.flagDiscontinuousBlock:
573 self.data_spc.fill(0)
573 self.data_spc.fill(0)
574 if self.dataOut.data_cspc is not None:
574 if self.dataOut.data_cspc is not None:
575 self.data_cspc.fill(0)
575 self.data_cspc.fill(0)
576 if self.dataOut.data_dc is not None:
576 if self.dataOut.data_dc is not None:
577 self.data_dc.fill(0)
577 self.data_dc.fill(0)
578 self.setNextFile()
578 self.setNextFile()
579
579
580 if self.flagIsNewFile == 0:
580 if self.flagIsNewFile == 0:
581 self.setBasicHeader()
581 self.setBasicHeader()
582
582
583 self.data_spc = self.dataOut.data_spc.copy()
583 self.data_spc = self.dataOut.data_spc.copy()
584
584
585 if self.dataOut.data_cspc is not None:
585 if self.dataOut.data_cspc is not None:
586 self.data_cspc = self.dataOut.data_cspc.copy()
586 self.data_cspc = self.dataOut.data_cspc.copy()
587
587
588 if self.dataOut.data_dc is not None:
588 if self.dataOut.data_dc is not None:
589 self.data_dc = self.dataOut.data_dc.copy()
589 self.data_dc = self.dataOut.data_dc.copy()
590
590
591 # #self.processingHeaderObj.dataBlocksPerFile)
591 # #self.processingHeaderObj.dataBlocksPerFile)
592 if self.hasAllDataInBuffer():
592 if self.hasAllDataInBuffer():
593 # self.setFirstHeader()
593 # self.setFirstHeader()
594 self.writeNextBlock()
594 self.writeNextBlock()
595
595
596 return 1
596 return 1
597
597
598 def __getBlockSize(self):
598 def __getBlockSize(self):
599 '''
599 '''
600 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
600 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
601 '''
601 '''
602
602
603 dtype_width = self.getDtypeWidth()
603 dtype_width = self.getDtypeWidth()
604
604
605 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
605 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
606
606
607 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
607 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
608 blocksize = (pts2write_SelfSpectra*dtype_width)
608 blocksize = (pts2write_SelfSpectra*dtype_width)
609
609
610 if self.dataOut.data_cspc is not None:
610 if self.dataOut.data_cspc is not None:
611 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
611 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
612 blocksize += (pts2write_CrossSpectra*dtype_width*2)
612 blocksize += (pts2write_CrossSpectra*dtype_width*2)
613
613
614 if self.dataOut.data_dc is not None:
614 if self.dataOut.data_dc is not None:
615 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
615 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
616 blocksize += (pts2write_DCchannels*dtype_width*2)
616 blocksize += (pts2write_DCchannels*dtype_width*2)
617
617
618 # blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
618 # blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
619
619
620 return blocksize
620 return blocksize
621
621
622 def setFirstHeader(self):
622 def setFirstHeader(self):
623
623
624 """
624 """
625 Obtiene una copia del First Header
625 Obtiene una copia del First Header
626
626
627 Affected:
627 Affected:
628 self.systemHeaderObj
628 self.systemHeaderObj
629 self.radarControllerHeaderObj
629 self.radarControllerHeaderObj
630 self.dtype
630 self.dtype
631
631
632 Return:
632 Return:
633 None
633 None
634 """
634 """
635
635
636 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
636 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
637 self.systemHeaderObj.nChannels = self.dataOut.nChannels
637 self.systemHeaderObj.nChannels = self.dataOut.nChannels
638 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
638 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
639
639
640 self.processingHeaderObj.dtype = 1 # Spectra
640 self.processingHeaderObj.dtype = 1 # Spectra
641 self.processingHeaderObj.blockSize = self.__getBlockSize()
641 self.processingHeaderObj.blockSize = self.__getBlockSize()
642 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
642 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
643 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
643 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
644 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
644 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
645 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
645 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
646 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
646 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
647 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
647 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
648 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
648 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
649
649
650 if self.processingHeaderObj.totalSpectra > 0:
650 if self.processingHeaderObj.totalSpectra > 0:
651 channelList = []
651 channelList = []
652 for channel in range(self.dataOut.nChannels):
652 for channel in range(self.dataOut.nChannels):
653 channelList.append(channel)
653 channelList.append(channel)
654 channelList.append(channel)
654 channelList.append(channel)
655
655
656 pairsList = []
656 pairsList = []
657 if self.dataOut.nPairs > 0:
657 if self.dataOut.nPairs > 0:
658 for pair in self.dataOut.pairsList:
658 for pair in self.dataOut.pairsList:
659 pairsList.append(pair[0])
659 pairsList.append(pair[0])
660 pairsList.append(pair[1])
660 pairsList.append(pair[1])
661
661
662 spectraComb = channelList + pairsList
662 spectraComb = channelList + pairsList
663 spectraComb = numpy.array(spectraComb, dtype="u1")
663 spectraComb = numpy.array(spectraComb, dtype="u1")
664 self.processingHeaderObj.spectraComb = spectraComb
664 self.processingHeaderObj.spectraComb = spectraComb
665
665
666 if self.dataOut.code is not None:
666 if self.dataOut.code is not None:
667 self.processingHeaderObj.code = self.dataOut.code
667 self.processingHeaderObj.code = self.dataOut.code
668 self.processingHeaderObj.nCode = self.dataOut.nCode
668 self.processingHeaderObj.nCode = self.dataOut.nCode
669 self.processingHeaderObj.nBaud = self.dataOut.nBaud
669 self.processingHeaderObj.nBaud = self.dataOut.nBaud
670
670
671 if self.processingHeaderObj.nWindows != 0:
671 if self.processingHeaderObj.nWindows != 0:
672 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
672 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
673 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
673 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
674 self.processingHeaderObj.nHeights = self.dataOut.nHeights
674 self.processingHeaderObj.nHeights = self.dataOut.nHeights
675 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
675 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
676
676
677 self.processingHeaderObj.processFlags = self.getProcessFlags()
677 self.processingHeaderObj.processFlags = self.getProcessFlags()
678
678
679 self.setBasicHeader() No newline at end of file
679 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