##// END OF EJS Templates
Merge branch 'isr' into v3.0-devel
jespinoza -
r1380:a03d3bbf5427 merge
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

This diff has been collapsed as it changes many lines, (1156 lines changed) Show them Hide them
@@ -0,0 +1,1156
1
2 import os
3 import datetime
4 import numpy
5 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator #YONG
6
7 from .jroplot_spectra import RTIPlot, NoisePlot
8
9 from schainpy.utils import log
10 from .plotting_codes import *
11
12 from schainpy.model.graphics.jroplot_base import Plot, plt
13
14 import matplotlib.pyplot as plt
15 import matplotlib.colors as colors
16
17 import time
18 import math
19
20
21 from matplotlib.ticker import MultipleLocator
22
23
24
25 class RTIDPPlot(RTIPlot):
26
27 '''
28 Plot for RTI Double Pulse Experiment
29 '''
30
31 CODE = 'RTIDP'
32 colormap = 'jro'
33 plot_name = 'RTI'
34
35 #cb_label = 'Ne Electron Density (1/cm3)'
36
37 def setup(self):
38 self.xaxis = 'time'
39 self.ncols = 1
40 self.nrows = 3
41 self.nplots = self.nrows
42 #self.height=10
43 if self.showSNR:
44 self.nrows += 1
45 self.nplots += 1
46
47 self.ylabel = 'Height [km]'
48 self.xlabel = 'Time (LT)'
49
50 self.cb_label = 'Intensity (dB)'
51
52
53 #self.cb_label = cb_label
54
55 self.titles = ['{} Channel {}'.format(
56 self.plot_name.upper(), '0x1'),'{} Channel {}'.format(
57 self.plot_name.upper(), '0'),'{} Channel {}'.format(
58 self.plot_name.upper(), '1')]
59
60
61 def plot(self):
62
63 self.data.normalize_heights()
64 self.x = self.data.times
65 self.y = self.data.heights[0:self.data.NDP]
66
67 if self.showSNR:
68 self.z = numpy.concatenate(
69 (self.data[self.CODE], self.data['snr'])
70 )
71 else:
72
73 self.z = self.data[self.CODE]
74 #print(numpy.max(self.z[0,0:]))
75
76 self.z = numpy.ma.masked_invalid(self.z)
77
78 if self.decimation is None:
79 x, y, z = self.fill_gaps(self.x, self.y, self.z)
80 else:
81 x, y, z = self.fill_gaps(*self.decimate())
82
83 for n, ax in enumerate(self.axes):
84
85
86 self.zmax = self.zmax if self.zmax is not None else numpy.max(
87 self.z[1][0,12:40])
88 self.zmin = self.zmin if self.zmin is not None else numpy.min(
89 self.z[1][0,12:40])
90
91
92
93 if ax.firsttime:
94
95 if self.zlimits is not None:
96 self.zmin, self.zmax = self.zlimits[n]
97
98
99 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
100 vmin=self.zmin,
101 vmax=self.zmax,
102 cmap=self.cmaps[n]
103 )
104 #plt.tight_layout()
105 else:
106 if self.zlimits is not None:
107 self.zmin, self.zmax = self.zlimits[n]
108 ax.collections.remove(ax.collections[0])
109 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
110 vmin=self.zmin,
111 vmax=self.zmax,
112 cmap=self.cmaps[n]
113 )
114 #plt.tight_layout()
115
116
117 class RTILPPlot(RTIPlot):
118
119 '''
120 Plot for RTI Long Pulse
121 '''
122
123 CODE = 'RTILP'
124 colormap = 'jro'
125 plot_name = 'RTI LP'
126
127 #cb_label = 'Ne Electron Density (1/cm3)'
128
129 def setup(self):
130 self.xaxis = 'time'
131 self.ncols = 1
132 self.nrows = 4
133 self.nplots = self.nrows
134 if self.showSNR:
135 self.nrows += 1
136 self.nplots += 1
137
138 self.ylabel = 'Height [km]'
139 self.xlabel = 'Time (LT)'
140
141 self.cb_label = 'Intensity (dB)'
142
143
144
145 #self.cb_label = cb_label
146
147 self.titles = ['{} Channel {}'.format(
148 self.plot_name.upper(), '0'),'{} Channel {}'.format(
149 self.plot_name.upper(), '1'),'{} Channel {}'.format(
150 self.plot_name.upper(), '2'),'{} Channel {}'.format(
151 self.plot_name.upper(), '3')]
152
153
154 def plot(self):
155
156 self.data.normalize_heights()
157 self.x = self.data.times
158 self.y = self.data.heights[0:self.data.NRANGE]
159
160 if self.showSNR:
161 self.z = numpy.concatenate(
162 (self.data[self.CODE], self.data['snr'])
163 )
164 else:
165
166 self.z = self.data[self.CODE]
167 #print(numpy.max(self.z[0,0:]))
168
169 self.z = numpy.ma.masked_invalid(self.z)
170
171 if self.decimation is None:
172 x, y, z = self.fill_gaps(self.x, self.y, self.z)
173 else:
174 x, y, z = self.fill_gaps(*self.decimate())
175
176 for n, ax in enumerate(self.axes):
177
178
179 self.zmax = self.zmax if self.zmax is not None else numpy.max(
180 self.z[1][0,12:40])
181 self.zmin = self.zmin if self.zmin is not None else numpy.min(
182 self.z[1][0,12:40])
183
184 if ax.firsttime:
185
186 if self.zlimits is not None:
187 self.zmin, self.zmax = self.zlimits[n]
188
189
190 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
191 vmin=self.zmin,
192 vmax=self.zmax,
193 cmap=self.cmaps[n]
194 )
195 #plt.tight_layout()
196 else:
197 if self.zlimits is not None:
198 self.zmin, self.zmax = self.zlimits[n]
199 ax.collections.remove(ax.collections[0])
200 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
201 vmin=self.zmin,
202 vmax=self.zmax,
203 cmap=self.cmaps[n]
204 )
205 #plt.tight_layout()
206
207
208 class DenRTIPlot(RTIPlot):
209
210 '''
211 Plot for Den
212 '''
213
214 CODE = 'denrti'
215 colormap = 'jro'
216 plot_name = 'Electron Density'
217
218 #cb_label = 'Ne Electron Density (1/cm3)'
219
220 def setup(self):
221 self.xaxis = 'time'
222 self.ncols = 1
223 self.nrows = self.data.shape(self.CODE)[0]
224 self.nplots = self.nrows
225 if self.showSNR:
226 self.nrows += 1
227 self.nplots += 1
228
229 self.ylabel = 'Height [km]'
230 self.xlabel = 'Time (LT)'
231
232 self.plots_adjust.update({'wspace': 0.8, 'hspace':0.2, 'left': 0.2, 'right': 0.9, 'bottom': 0.18})
233
234 if self.CODE == 'denrti' or self.CODE=='denrtiLP':
235 self.cb_label = r'$\mathrm{N_e}$ Electron Density ($\mathrm{1/cm^3}$)'
236
237 #self.cb_label = cb_label
238 if not self.titles:
239 self.titles = self.data.parameters \
240 if self.data.parameters else ['{}'.format(self.plot_name)]
241 if self.showSNR:
242 self.titles.append('SNR')
243
244 def plot(self):
245
246 self.data.normalize_heights()
247 self.x = self.data.times
248 self.y = self.data.heights
249
250
251
252 if self.showSNR:
253 self.z = numpy.concatenate(
254 (self.data[self.CODE], self.data['snr'])
255 )
256 else:
257 self.z = self.data[self.CODE]
258
259 self.z = numpy.ma.masked_invalid(self.z)
260
261 if self.decimation is None:
262 x, y, z = self.fill_gaps(self.x, self.y, self.z)
263 else:
264 x, y, z = self.fill_gaps(*self.decimate())
265
266 for n, ax in enumerate(self.axes):
267
268 self.zmax = self.zmax if self.zmax is not None else numpy.max(
269 self.z[n])
270 self.zmin = self.zmin if self.zmin is not None else numpy.min(
271 self.z[n])
272
273 if ax.firsttime:
274
275 if self.zlimits is not None:
276 self.zmin, self.zmax = self.zlimits[n]
277 if numpy.log10(self.zmin)<0:
278 self.zmin=1
279 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
280 vmin=self.zmin,
281 vmax=self.zmax,
282 cmap=self.cmaps[n],
283 norm=colors.LogNorm()
284 )
285 #plt.tight_layout()
286
287 else:
288 if self.zlimits is not None:
289 self.zmin, self.zmax = self.zlimits[n]
290 ax.collections.remove(ax.collections[0])
291 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
292 vmin=self.zmin,
293 vmax=self.zmax,
294 cmap=self.cmaps[n],
295 norm=colors.LogNorm()
296 )
297 #plt.tight_layout()
298
299
300
301 class DenRTILPPlot(DenRTIPlot):
302
303 '''
304 Plot for Electron Temperature
305 '''
306
307 CODE = 'denrtiLP'
308 colormap = 'jro'
309 plot_name = 'Electron Density'
310
311
312 class ETempRTIPlot(RTIPlot):
313
314 '''
315 Plot for Electron Temperature
316 '''
317
318 CODE = 'ETemp'
319 colormap = 'jet'
320 plot_name = 'Electron Temperature'
321
322 #cb_label = 'Ne Electron Density (1/cm3)'
323
324 def setup(self):
325 self.xaxis = 'time'
326 self.ncols = 1
327 self.nrows = self.data.shape(self.CODE)[0]
328 self.nplots = self.nrows
329 if self.showSNR:
330 self.nrows += 1
331 self.nplots += 1
332
333 self.ylabel = 'Height [km]'
334 self.xlabel = 'Time (LT)'
335 self.plots_adjust.update({'wspace': 0.8, 'hspace':0.2, 'left': 0.2, 'right': 0.9, 'bottom': 0.18})
336 if self.CODE == 'ETemp' or self.CODE == 'ETempLP':
337 self.cb_label = 'Electron Temperature (K)'
338 if self.CODE == 'ITemp' or self.CODE == 'ITempLP':
339 self.cb_label = 'Ion Temperature (K)'
340
341
342 if not self.titles:
343 self.titles = self.data.parameters \
344 if self.data.parameters else ['{}'.format(self.plot_name)]
345 if self.showSNR:
346 self.titles.append('SNR')
347
348 def plot(self):
349
350 self.data.normalize_heights()
351 self.x = self.data.times
352 self.y = self.data.heights
353
354 if self.showSNR:
355 self.z = numpy.concatenate(
356 (self.data[self.CODE], self.data['snr'])
357 )
358 else:
359 self.z = self.data[self.CODE]
360
361 self.z = numpy.ma.masked_invalid(self.z)
362
363 if self.decimation is None:
364 x, y, z = self.fill_gaps(self.x, self.y, self.z)
365 else:
366 x, y, z = self.fill_gaps(*self.decimate())
367
368 for n, ax in enumerate(self.axes):
369
370 self.zmax = self.zmax if self.zmax is not None else numpy.max(
371 self.z[n])
372 self.zmin = self.zmin if self.zmin is not None else numpy.min(
373 self.z[n])
374
375 if ax.firsttime:
376
377 if self.zlimits is not None:
378 self.zmin, self.zmax = self.zlimits[n]
379
380 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
381 vmin=self.zmin,
382 vmax=self.zmax,
383 cmap=self.cmaps[n]
384 )
385 #plt.tight_layout()
386
387 else:
388 if self.zlimits is not None:
389 self.zmin, self.zmax = self.zlimits[n]
390 ax.collections.remove(ax.collections[0])
391 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
392 vmin=self.zmin,
393 vmax=self.zmax,
394 cmap=self.cmaps[n]
395 )
396 #plt.tight_layout()
397
398
399
400 class ITempRTIPlot(ETempRTIPlot):
401
402 '''
403 Plot for Ion Temperature
404 '''
405
406 CODE = 'ITemp'
407 colormap = 'jet'
408 plot_name = 'Ion Temperature'
409
410
411 class ElectronTempLPPlot(ETempRTIPlot):
412
413 '''
414 Plot for Electron Temperature LP
415 '''
416
417 CODE = 'ETempLP'
418 colormap = 'jet'
419 plot_name = 'Electron Temperature'
420
421
422 class IonTempLPPlot(ETempRTIPlot):
423
424 '''
425 Plot for Ion Temperature LP
426 '''
427
428 CODE = 'ITempLP'
429 colormap = 'jet'
430 plot_name = 'Ion Temperature'
431
432
433 class HFracRTIPlot(ETempRTIPlot):
434
435 '''
436 Plot for H+ LP
437 '''
438
439 CODE = 'HFracLP'
440 colormap = 'jet'
441 plot_name = 'H+ Frac'
442
443
444 class HeFracRTIPlot(ETempRTIPlot):
445
446 '''
447 Plot for He+ LP
448 '''
449
450 CODE = 'HeFracLP'
451 colormap = 'jet'
452 plot_name = 'He+ Frac'
453
454
455 class TempsDPPlot(Plot):
456 '''
457 Plot for Electron - Ion Temperatures
458 '''
459
460 CODE = 'tempsDP'
461 plot_name = 'Temperatures'
462 plot_type = 'scatterbuffer'
463
464
465 def setup(self):
466
467 self.ncols = 1
468 self.nrows = 1
469 self.nplots = 1
470 self.ylabel = 'Range [km]'
471 self.xlabel = 'Temperature (K)'
472 self.width = 3.5
473 self.height = 5.5
474 self.colorbar = False
475 self.plots_adjust.update({'wspace': 0.8, 'hspace':0.2, 'left': 0.2, 'right': 0.9, 'bottom': 0.18})
476 if not self.titles:
477 self.titles = self.data.parameters \
478 if self.data.parameters else ['{}'.format(self.CODE.upper())]
479
480 def plot(self):
481
482 self.x = self.data['tempsDP'][:,-1]
483 self.y = self.data.heights[0:self.data.NSHTS]
484
485 self.xmin = -100
486 self.xmax = 5000
487 ax = self.axes[0]
488
489 if ax.firsttime:
490
491 ax.errorbar(self.x, self.y, xerr=self.data.ete2, fmt='r^',elinewidth=1.0,color='b',linewidth=2.0, label='Te')
492 ax.errorbar(self.data.ti2, self.y, fmt='k^', xerr=self.data.eti2,elinewidth=1.0,color='b',linewidth=2.0, label='Ti')
493 plt.legend(loc='lower right')
494 self.ystep_given = 50
495 ax.yaxis.set_minor_locator(MultipleLocator(15))
496 ax.grid(which='minor')
497 #plt.tight_layout()
498
499
500 else:
501 self.clear_figures()
502 ax.errorbar(self.x, self.y, xerr=self.data.ete2, fmt='r^',elinewidth=1.0,color='b',linewidth=2.0, label='Te')
503 ax.errorbar(self.data.ti2, self.y, fmt='k^', xerr=self.data.eti2,elinewidth=1.0,color='b',linewidth=2.0, label='Ti')
504 plt.legend(loc='lower right')
505 ax.yaxis.set_minor_locator(MultipleLocator(15))
506 #plt.tight_layout()
507
508
509 class TempsHPPlot(Plot):
510 '''
511 Plot for Temperatures Hybrid Experiment
512 '''
513
514 CODE = 'temps_LP'
515 plot_name = 'Temperatures'
516 plot_type = 'scatterbuffer'
517
518
519 def setup(self):
520
521 self.ncols = 1
522 self.nrows = 1
523 self.nplots = 1
524 self.ylabel = 'Range [km]'
525 self.xlabel = 'Temperature (K)'
526 self.width = 3.5
527 self.height = 6.5
528 self.colorbar = False
529 if not self.titles:
530 self.titles = self.data.parameters \
531 if self.data.parameters else ['{}'.format(self.CODE.upper())]
532
533 def plot(self):
534
535 self.x = self.data['temps_LP'][:,-1]
536 self.y = self.data.heights[0:self.data.NACF]
537 self.xmin = -100
538 self.xmax = 4500
539 ax = self.axes[0]
540
541 if ax.firsttime:
542
543 ax.errorbar(self.x, self.y, xerr=self.data.ete, fmt='r^',elinewidth=1.0,color='b',linewidth=2.0, label='Te')
544 ax.errorbar(self.data.ti, self.y, fmt='k^', xerr=self.data.eti,elinewidth=1.0,color='b',linewidth=2.0, label='Ti')
545 plt.legend(loc='lower right')
546 self.ystep_given = 200
547 ax.yaxis.set_minor_locator(MultipleLocator(15))
548 ax.grid(which='minor')
549 #plt.tight_layout()
550
551
552 else:
553 self.clear_figures()
554 ax.errorbar(self.x, self.y, xerr=self.data.ete, fmt='r^',elinewidth=1.0,color='b',linewidth=2.0, label='Te')
555 ax.errorbar(self.data.ti, self.y, fmt='k^', xerr=self.data.eti,elinewidth=1.0,color='b',linewidth=2.0, label='Ti')
556 plt.legend(loc='lower right')
557 ax.yaxis.set_minor_locator(MultipleLocator(15))
558 #plt.tight_layout()
559
560
561 class FracsHPPlot(Plot):
562 '''
563 Plot for Composition LP
564 '''
565
566 CODE = 'fracs_LP'
567 plot_name = 'Composition'
568 plot_type = 'scatterbuffer'
569
570
571 def setup(self):
572
573 self.ncols = 1
574 self.nrows = 1
575 self.nplots = 1
576 self.ylabel = 'Range [km]'
577 self.xlabel = 'Frac'
578 self.width = 3.5
579 self.height = 6.5
580 self.colorbar = False
581 if not self.titles:
582 self.titles = self.data.parameters \
583 if self.data.parameters else ['{}'.format(self.CODE.upper())]
584
585 def plot(self):
586
587 self.x = self.data['fracs_LP'][:,-1]
588 self.y = self.data.heights[0:self.data.NACF]
589
590 self.xmin = 0
591 self.xmax = 1
592 ax = self.axes[0]
593
594 if ax.firsttime:
595
596 ax.errorbar(self.x, self.y[self.data.cut:], xerr=self.data.eph, fmt='r^',elinewidth=1.0,color='b',linewidth=2.0, label='H+')
597 ax.errorbar(self.data.phe, self.y[self.data.cut:], fmt='k^', xerr=self.data.ephe,elinewidth=1.0,color='b',linewidth=2.0, label='He+')
598 plt.legend(loc='lower right')
599 self.xstep_given = 0.2
600 self.ystep_given = 200
601 ax.yaxis.set_minor_locator(MultipleLocator(15))
602 ax.grid(which='minor')
603 #plt.tight_layout()
604
605
606 else:
607 self.clear_figures()
608 ax.errorbar(self.x, self.y[self.data.cut:], xerr=self.data.eph, fmt='r^',elinewidth=1.0,color='b',linewidth=2.0, label='H+')
609 ax.errorbar(self.data.phe, self.y[self.data.cut:], fmt='k^', xerr=self.data.ephe,elinewidth=1.0,color='b',linewidth=2.0, label='He+')
610 plt.legend(loc='lower right')
611 ax.yaxis.set_minor_locator(MultipleLocator(15))
612 #plt.tight_layout()
613
614
615
616 class EDensityPlot(Plot):
617 '''
618 Plot for electron density
619 '''
620
621 CODE = 'den'
622 plot_name = 'Electron Density'
623 plot_type = 'scatterbuffer'
624
625
626 def setup(self):
627
628 self.ncols = 1
629 self.nrows = 1
630 self.nplots = 1
631 self.ylabel = 'Range [km]'
632 self.xlabel = r'$\mathrm{N_e}$ Electron Density ($\mathrm{1/cm^3}$)'
633 self.width = 4
634 self.height = 6.5
635 self.colorbar = False
636 self.plots_adjust.update({'wspace': 0.8, 'hspace':0.2, 'left': 0.2, 'right': 0.9, 'bottom': 0.18})
637 if not self.titles:
638 self.titles = self.data.parameters \
639 if self.data.parameters else ['{}'.format(self.CODE.upper())]
640
641 def plot(self):
642
643
644 self.x = self.data[self.CODE]
645 self.y = self.data.heights
646 self.xmin = 1000
647 self.xmax = 10000000
648 ax = self.axes[0]
649
650 if ax.firsttime:
651 self.autoxticks=False
652 #if self.CODE=='den':
653 ax.errorbar(self.data.dphi, self.y[:self.data.NSHTS], xerr=1, fmt='h-',elinewidth=1.0,color='g',linewidth=1.0, label='Faraday Profile',markersize=2)
654 #ax.errorbar(self.data.dphi, self.y[:self.data.NSHTS], xerr=self.data.sdn1, fmt='h-',elinewidth=1.0,color='g',linewidth=1.0, label='Faraday Profile',markersize=2)
655
656 ax.errorbar(self.x[:,-1], self.y[:self.data.NSHTS], fmt='k^-', xerr=self.data.sdp2,elinewidth=1.0,color='b',linewidth=1.0, label='Power Profile',markersize=2)
657 #else:
658 #ax.errorbar(self.data.dphi[:self.data.cut], self.y[:self.data.cut], xerr=1, fmt='h-',elinewidth=1.0,color='g',linewidth=1.0, label='Faraday Profile',markersize=2)
659 #ax.errorbar(self.x[:self.data.cut,-1], self.y[:self.data.cut], fmt='k^-', xerr=self.data.sdp2[:self.data.cut],elinewidth=1.0,color='b',linewidth=1.0, label='Power Profile',markersize=2)
660
661 if self.CODE=='denLP':
662 ax.errorbar(self.data.ne[self.data.cut:], self.y[self.data.cut:], xerr=self.data.ene[self.data.cut:], fmt='r^-',elinewidth=1.0,color='r',linewidth=1.0, label='LP Profile',markersize=2)
663
664 plt.legend(loc='upper right')
665 ax.set_xscale("log", nonposx='clip')
666 grid_y_ticks=numpy.arange(numpy.nanmin(self.y),numpy.nanmax(self.y),50)
667 self.ystep_given=100
668 if self.CODE=='denLP':
669 self.ystep_given=200
670 ax.set_yticks(grid_y_ticks,minor=True)
671 ax.grid(which='minor')
672 #plt.tight_layout()
673
674
675
676 else:
677
678 self.clear_figures()
679 #if self.CODE=='den':
680 ax.errorbar(self.data.dphi, self.y[:self.data.NSHTS], xerr=1, fmt='h-',elinewidth=1.0,color='g',linewidth=1.0, label='Faraday Profile',markersize=2)
681 #ax.errorbar(self.data.dphi, self.y[:self.data.NSHTS], xerr=self.data.sdn1, fmt='h-',elinewidth=1.0,color='g',linewidth=1.0, label='Faraday Profile',markersize=2)
682
683 ax.errorbar(self.x[:,-1], self.y[:self.data.NSHTS], fmt='k^-', xerr=self.data.sdp2,elinewidth=1.0,color='b',linewidth=1.0, label='Power Profile',markersize=2)
684 ax.errorbar(self.x[:,-2], self.y[:self.data.NSHTS], elinewidth=1.0,color='r',linewidth=0.5,linestyle="dashed")
685 #else:
686 #ax.errorbar(self.data.dphi[:self.data.cut], self.y[:self.data.cut], xerr=1, fmt='h-',elinewidth=1.0,color='g',linewidth=1.0, label='Faraday Profile',markersize=2)
687 #ax.errorbar(self.x[:self.data.cut,-1], self.y[:self.data.cut], fmt='k^-', xerr=self.data.sdp2[:self.data.cut],elinewidth=1.0,color='b',linewidth=1.0, label='Power Profile',markersize=2)
688 #ax.errorbar(self.x[:self.data.cut,-2], self.y[:self.data.cut], elinewidth=1.0,color='r',linewidth=0.5,linestyle="dashed")
689
690 if self.CODE=='denLP':
691 ax.errorbar(self.data.ne[self.data.cut:], self.y[self.data.cut:], fmt='r^-', xerr=self.data.ene[self.data.cut:],elinewidth=1.0,color='r',linewidth=1.0, label='LP Profile',markersize=2)
692
693 ax.set_xscale("log", nonposx='clip')
694 grid_y_ticks=numpy.arange(numpy.nanmin(self.y),numpy.nanmax(self.y),50)
695 ax.set_yticks(grid_y_ticks,minor=True)
696 ax.grid(which='minor')
697 plt.legend(loc='upper right')
698 #plt.tight_layout()
699
700 class FaradayAnglePlot(Plot):
701 '''
702 Plot for electron density
703 '''
704
705 CODE = 'FaradayAngle'
706 plot_name = 'Faraday Angle'
707 plot_type = 'scatterbuffer'
708
709
710 def setup(self):
711
712 self.ncols = 1
713 self.nrows = 1
714 self.nplots = 1
715 self.ylabel = 'Range [km]'
716 self.xlabel = 'Faraday Angle (º)'
717 self.width = 4
718 self.height = 6.5
719 self.colorbar = False
720 if not self.titles:
721 self.titles = self.data.parameters \
722 if self.data.parameters else ['{}'.format(self.CODE.upper())]
723
724 def plot(self):
725
726
727 self.x = self.data[self.CODE]
728 self.y = self.data.heights
729 self.xmin = -180
730 self.xmax = 180
731 ax = self.axes[0]
732
733 if ax.firsttime:
734 self.autoxticks=False
735 #if self.CODE=='den':
736 ax.plot(self.x, self.y,marker='o',color='g',linewidth=1.0,markersize=2)
737
738 grid_y_ticks=numpy.arange(numpy.nanmin(self.y),numpy.nanmax(self.y),50)
739 self.ystep_given=100
740 if self.CODE=='denLP':
741 self.ystep_given=200
742 ax.set_yticks(grid_y_ticks,minor=True)
743 ax.grid(which='minor')
744 #plt.tight_layout()
745 else:
746
747 self.clear_figures()
748 #if self.CODE=='den':
749 #print(numpy.shape(self.x))
750 ax.plot(self.x[:,-1], self.y, marker='o',color='g',linewidth=1.0, markersize=2)
751
752 grid_y_ticks=numpy.arange(numpy.nanmin(self.y),numpy.nanmax(self.y),50)
753 ax.set_yticks(grid_y_ticks,minor=True)
754 ax.grid(which='minor')
755
756 class EDensityHPPlot(EDensityPlot):
757
758 '''
759 Plot for Electron Density Hybrid Experiment
760 '''
761
762 CODE = 'denLP'
763 plot_name = 'Electron Density'
764 plot_type = 'scatterbuffer'
765
766
767 class ACFsPlot(Plot):
768 '''
769 Plot for ACFs Double Pulse Experiment
770 '''
771
772 CODE = 'acfs'
773 plot_name = 'ACF'
774 plot_type = 'scatterbuffer'
775
776
777 def setup(self):
778 #self.xaxis = 'time'
779 self.ncols = 1
780 self.nrows = 1
781 self.nplots = 1
782 self.ylabel = 'Range [km]'
783 self.xlabel = 'lags (ms)'
784 self.width = 3.5
785 self.height = 6
786 self.colorbar = False
787 self.plots_adjust.update({'wspace': 0.8, 'hspace':0.2, 'left': 0.2, 'right': 0.9, 'bottom': 0.18})
788 if not self.titles:
789 self.titles = self.data.parameters \
790 if self.data.parameters else ['{}'.format(self.CODE.upper())]
791
792 def plot(self):
793
794 self.x = self.data.lags_to_plot
795 self.y = self.data['acfs'][:,-1]
796
797
798 self.xmin = 0.0
799 self.xmax = 2.0
800
801 ax = self.axes[0]
802
803 if ax.firsttime:
804
805 for i in range(self.data.NSHTS):
806 x_aux = numpy.isfinite(self.x[i,:])
807 y_aux = numpy.isfinite(self.y[i,:])
808 yerr_aux = numpy.isfinite(self.data.acfs_error_to_plot[i,:])
809 x_igcej_aux = numpy.isfinite(self.data.x_igcej_to_plot[i,:])
810 y_igcej_aux = numpy.isfinite(self.data.y_igcej_to_plot[i,:])
811 x_ibad_aux = numpy.isfinite(self.data.x_ibad_to_plot[i,:])
812 y_ibad_aux = numpy.isfinite(self.data.y_ibad_to_plot[i,:])
813 if self.x[i,:][~numpy.isnan(self.x[i,:])].shape[0]>2:
814 ax.errorbar(self.x[i,x_aux], self.y[i,y_aux], yerr=self.data.acfs_error_to_plot[i,x_aux],color='b',marker='o',linewidth=1.0,markersize=2)
815 ax.plot(self.data.x_igcej_to_plot[i,x_igcej_aux],self.data.y_igcej_to_plot[i,y_igcej_aux],'x',color='red',markersize=2)
816 ax.plot(self.data.x_ibad_to_plot[i,x_ibad_aux],self.data.y_ibad_to_plot[i,y_ibad_aux],'X',color='red',markersize=2)
817
818 self.xstep_given = (self.xmax-self.xmin)/(self.data.DPL-1)
819 self.ystep_given = 50
820 ax.yaxis.set_minor_locator(MultipleLocator(15))
821 ax.grid(which='minor')
822
823
824
825 else:
826 self.clear_figures()
827
828 for i in range(self.data.NSHTS):
829 x_aux = numpy.isfinite(self.x[i,:])
830 y_aux = numpy.isfinite(self.y[i,:])
831 yerr_aux = numpy.isfinite(self.data.acfs_error_to_plot[i,:])
832 x_igcej_aux = numpy.isfinite(self.data.x_igcej_to_plot[i,:])
833 y_igcej_aux = numpy.isfinite(self.data.y_igcej_to_plot[i,:])
834 x_ibad_aux = numpy.isfinite(self.data.x_ibad_to_plot[i,:])
835 y_ibad_aux = numpy.isfinite(self.data.y_ibad_to_plot[i,:])
836 if self.x[i,:][~numpy.isnan(self.x[i,:])].shape[0]>2:
837 ax.errorbar(self.x[i,x_aux], self.y[i,y_aux], yerr=self.data.acfs_error_to_plot[i,x_aux],linewidth=1.0,markersize=2,color='b',marker='o')
838 ax.plot(self.data.x_igcej_to_plot[i,x_igcej_aux],self.data.y_igcej_to_plot[i,y_igcej_aux],'x',color='red',markersize=2)
839 ax.plot(self.data.x_ibad_to_plot[i,x_ibad_aux],self.data.y_ibad_to_plot[i,y_ibad_aux],'X',color='red',markersize=2)
840 ax.yaxis.set_minor_locator(MultipleLocator(15))
841
842
843
844
845 class ACFsLPPlot(Plot):
846 '''
847 Plot for ACFs Double Pulse Experiment
848 '''
849
850 CODE = 'acfs_LP'
851 plot_name = 'ACF'
852 plot_type = 'scatterbuffer'
853
854
855 def setup(self):
856 #self.xaxis = 'time'
857 self.ncols = 1
858 self.nrows = 1
859 self.nplots = 1
860 self.ylabel = 'Range [km]'
861 self.xlabel = 'lags (ms)'
862 self.width = 3.5
863 self.height = 7
864 self.colorbar = False
865 if not self.titles:
866 self.titles = self.data.parameters \
867 if self.data.parameters else ['{}'.format(self.CODE.upper())]
868
869
870
871 def plot(self):
872
873 self.x = self.data.lags_LP_to_plot
874 self.y = self.data['acfs_LP'][:,-1]
875
876 self.xmin = 0.0
877 self.xmax = 1.5
878
879 ax = self.axes[0]
880
881 if ax.firsttime:
882
883 for i in range(self.data.NACF):
884 x_aux = numpy.isfinite(self.x[i,:])
885 y_aux = numpy.isfinite(self.y[i,:])
886 yerr_aux = numpy.isfinite(self.data.errors[i,:])
887
888 if self.x[i,:][~numpy.isnan(self.x[i,:])].shape[0]>2:
889 ax.errorbar(self.x[i,x_aux], self.y[i,y_aux], yerr=self.data.errors[i,x_aux],color='b',linewidth=1.0,markersize=2,ecolor='r')
890
891 #self.xstep_given = (self.xmax-self.xmin)/(self.data.NLAG-1)
892 self.xstep_given=0.3
893 self.ystep_given = 200
894 ax.yaxis.set_minor_locator(MultipleLocator(15))
895 ax.grid(which='minor')
896
897 else:
898 self.clear_figures()
899
900 for i in range(self.data.NACF):
901 x_aux = numpy.isfinite(self.x[i,:])
902 y_aux = numpy.isfinite(self.y[i,:])
903 yerr_aux = numpy.isfinite(self.data.errors[i,:])
904
905 if self.x[i,:][~numpy.isnan(self.x[i,:])].shape[0]>2:
906 ax.errorbar(self.x[i,x_aux], self.y[i,y_aux], yerr=self.data.errors[i,x_aux],color='b',linewidth=1.0,markersize=2,ecolor='r')
907
908 ax.yaxis.set_minor_locator(MultipleLocator(15))
909
910
911 class CrossProductsPlot(Plot):
912 '''
913 Plot for cross products
914 '''
915
916 CODE = 'crossprod'
917 plot_name = 'Cross Products'
918 plot_type = 'scatterbuffer'
919
920
921 def setup(self):
922
923 self.ncols = 3
924 self.nrows = 1
925 self.nplots = 3
926 self.ylabel = 'Range [km]'
927
928 self.width = 3.5*self.nplots
929 self.height = 5.5
930 self.colorbar = False
931 self.titles = []
932
933 def plot(self):
934
935 self.x = self.data['crossprod'][:,-1,:,:,:,:]
936
937
938
939
940 self.y = self.data.heights[0:self.data.NDP]
941
942
943
944 for n, ax in enumerate(self.axes):
945
946 self.xmin=numpy.min(numpy.concatenate((self.x[n][0,20:30,0,0],self.x[n][1,20:30,0,0],self.x[n][2,20:30,0,0],self.x[n][3,20:30,0,0])))
947 self.xmax=numpy.max(numpy.concatenate((self.x[n][0,20:30,0,0],self.x[n][1,20:30,0,0],self.x[n][2,20:30,0,0],self.x[n][3,20:30,0,0])))
948
949
950 if ax.firsttime:
951
952 self.autoxticks=False
953 if n==0:
954 label1='kax'
955 label2='kay'
956 label3='kbx'
957 label4='kby'
958 self.xlimits=[(self.xmin,self.xmax)]
959 elif n==1:
960 label1='kax2'
961 label2='kay2'
962 label3='kbx2'
963 label4='kby2'
964 self.xlimits.append((self.xmin,self.xmax))
965 elif n==2:
966 label1='kaxay'
967 label2='kbxby'
968 label3='kaxbx'
969 label4='kaxby'
970 self.xlimits.append((self.xmin,self.xmax))
971
972
973 ax.plotline1 = ax.plot(self.x[n][0,:,0,0], self.y, color='r',linewidth=2.0, label=label1)
974 ax.plotline2 = ax.plot(self.x[n][1,:,0,0], self.y, color='k',linewidth=2.0, label=label2)
975 ax.plotline3 = ax.plot(self.x[n][2,:,0,0], self.y, color='b',linewidth=2.0, label=label3)
976 ax.plotline4 = ax.plot(self.x[n][3,:,0,0], self.y, color='m',linewidth=2.0, label=label4)
977 ax.legend(loc='upper right')
978 ax.set_xlim(self.xmin, self.xmax)
979 self.titles.append('{}'.format(self.plot_name.upper()))
980 #plt.tight_layout()
981
982
983 else:
984
985 if n==0:
986 self.xlimits=[(self.xmin,self.xmax)]
987 else:
988 self.xlimits.append((self.xmin,self.xmax))
989
990 ax.set_xlim(self.xmin, self.xmax)
991
992
993 ax.plotline1[0].set_data(self.x[n][0,:,0,0],self.y)
994 ax.plotline2[0].set_data(self.x[n][1,:,0,0],self.y)
995 ax.plotline3[0].set_data(self.x[n][2,:,0,0],self.y)
996 ax.plotline4[0].set_data(self.x[n][3,:,0,0],self.y)
997 self.titles.append('{}'.format(self.plot_name.upper()))
998 #plt.tight_layout()
999
1000
1001
1002 class CrossProductsLPPlot(Plot):
1003 '''
1004 Plot for cross products LP
1005 '''
1006
1007 CODE = 'crossprodlp'
1008 plot_name = 'Cross Products LP'
1009 plot_type = 'scatterbuffer'
1010
1011
1012 def setup(self):
1013
1014 self.ncols = 2
1015 self.nrows = 1
1016 self.nplots = 2
1017 self.ylabel = 'Range [km]'
1018 self.xlabel = 'dB'
1019 self.width = 3.5*self.nplots
1020 self.height = 5.5
1021 self.colorbar = False
1022 self.titles = []
1023 self.plotline_array=numpy.zeros((2,self.data.NLAG),dtype=object)
1024 def plot(self):
1025
1026
1027 self.x = self.data[self.CODE][:,-1,:,:]
1028
1029
1030 self.y = self.data.heights[0:self.data.NRANGE]
1031
1032
1033 label_array=numpy.array(['lag '+ str(x) for x in range(self.data.NLAG)])
1034 color_array=['r','k','g','b','c','m','y','orange','steelblue','purple','peru','darksalmon','grey','limegreen','olive','midnightblue']
1035
1036
1037 for n, ax in enumerate(self.axes):
1038
1039 self.xmin=30
1040 self.xmax=70
1041 #print(self.x[0,12:15,n])
1042 #input()
1043 #self.xmin=numpy.min(numpy.concatenate((self.x[0,:,n],self.x[1,:,n])))
1044 #self.xmax=numpy.max(numpy.concatenate((self.x[0,:,n],self.x[1,:,n])))
1045
1046 #print("before",self.plotline_array)
1047
1048 if ax.firsttime:
1049
1050 self.autoxticks=False
1051
1052
1053 for i in range(self.data.NLAG):
1054 #print(i)
1055 #print(numpy.shape(self.x))
1056 self.plotline_array[n,i], = ax.plot(self.x[i,:,n], self.y, color=color_array[i],linewidth=1.0, label=label_array[i])
1057 #ax.plotline1 = ax.plot(self.x[0,:,n], self.y, color='r',linewidth=2.0, label=label_array[0])
1058 #ax.plotline2 = ax.plot(self.x[n][1,:,0,0], self.y, color='k',linewidth=2.0, label=label2)
1059 #ax.plotline3 = ax.plot(self.x[n][2,:,0,0], self.y, color='b',linewidth=2.0, label=label3)
1060 #ax.plotline4 = ax.plot(self.x[n][3,:,0,0], self.y, color='m',linewidth=2.0, label=label4)
1061
1062
1063 #print(self.plotline_array)
1064
1065
1066
1067 ax.legend(loc='upper right')
1068 ax.set_xlim(self.xmin, self.xmax)
1069 if n==0:
1070 self.titles.append('{} CH0'.format(self.plot_name.upper()))
1071 if n==1:
1072 self.titles.append('{} CH1'.format(self.plot_name.upper()))
1073
1074 #plt.tight_layout()
1075
1076 else:
1077 #print(self.plotline_array)
1078 for i in range(self.data.NLAG):
1079
1080 self.plotline_array[n,i].set_data(self.x[i,:,n],self.y)
1081
1082
1083
1084 #ax.plotline1[0].set_data(self.x[n][0,:,0,0],self.y)
1085 #ax.plotline2[0].set_data(self.x[n][1,:,0,0],self.y)
1086 #ax.plotline3[0].set_data(self.x[n][2,:,0,0],self.y)
1087 #ax.plotline4[0].set_data(self.x[n][3,:,0,0],self.y)
1088
1089 if n==0:
1090 self.titles.append('{} CH0'.format(self.plot_name.upper()))
1091 if n==1:
1092 self.titles.append('{} CH1'.format(self.plot_name.upper()))
1093
1094 #plt.tight_layout()
1095
1096
1097 class NoiseDPPlot(NoisePlot):
1098 '''
1099 Plot for noise Double Pulse
1100 '''
1101
1102 CODE = 'noisedp'
1103 plot_name = 'Noise'
1104 plot_type = 'scatterbuffer'
1105
1106
1107 class XmitWaveformPlot(Plot):
1108 '''
1109 Plot for xmit waveform
1110 '''
1111
1112 CODE = 'xmit'
1113 plot_name = 'Xmit Waveform'
1114 plot_type = 'scatterbuffer'
1115
1116
1117 def setup(self):
1118
1119 self.ncols = 1
1120 self.nrows = 1
1121 self.nplots = 1
1122 self.ylabel = ''
1123 self.xlabel = 'Number of Lag'
1124 self.width = 5.5
1125 self.height = 3.5
1126 self.colorbar = False
1127 if not self.titles:
1128 self.titles = self.data.parameters \
1129 if self.data.parameters else ['{}'.format(self.plot_name.upper())]
1130
1131 def plot(self):
1132
1133 self.x = numpy.arange(0,self.data.NLAG,1,'float32')
1134 self.y = self.data['xmit'][:,-1,:]
1135
1136 self.xmin = 0
1137 self.xmax = self.data.NLAG-1
1138 self.ymin = -1.0
1139 self.ymax = 1.0
1140 ax = self.axes[0]
1141
1142 if ax.firsttime:
1143 ax.plotline0=ax.plot(self.x,self.y[0,:],color='blue')
1144 ax.plotline1=ax.plot(self.x,self.y[1,:],color='red')
1145 secax=ax.secondary_xaxis(location=0.5)
1146 secax.xaxis.tick_bottom()
1147 secax.tick_params( labelleft=False, labeltop=False,
1148 labelright=False, labelbottom=False)
1149
1150 self.xstep_given = 3
1151 self.ystep_given = .25
1152 secax.set_xticks(numpy.linspace(self.xmin, self.xmax, 6)) #only works on matplotlib.version>3.2
1153
1154 else:
1155 ax.plotline0[0].set_data(self.x,self.y[0,:])
1156 ax.plotline1[0].set_data(self.x,self.y[1,:])
This diff has been collapsed as it changes many lines, (683 lines changed) Show them Hide them
@@ -0,0 +1,683
1 '''
2 Created on Jun 9, 2020
3
4 @author: Roberto Flores
5 '''
6
7 import os
8 import sys
9 import time
10
11 import struct
12
13
14 import datetime
15
16 import numpy
17
18
19 import schainpy.admin
20 from schainpy.model.io.jroIO_base import LOCALTIME, Reader
21 from schainpy.model.data.jroheaderIO import BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
22 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
23 from schainpy.model.data.jrodata import Voltage, Parameters
24 from schainpy.utils import log
25
26
27 class DatReader(Reader, ProcessingUnit):
28
29 def __init__(self):
30
31 ProcessingUnit.__init__(self)
32 self.basicHeaderObj = BasicHeader(LOCALTIME)
33 self.systemHeaderObj = SystemHeader()
34 self.radarControllerHeaderObj = RadarControllerHeader()
35 self.processingHeaderObj = ProcessingHeader()
36 self.dataOut = Parameters()
37 #print(self.basicHeaderObj.timezone)
38 #self.counter_block=0
39 self.format='dat'
40 self.flagNoMoreFiles = 0
41 self.filename = None
42 self.intervals = set()
43 #self.datatime = datetime.datetime(1900,1,1)
44
45 self.filefmt = "***%Y%m%d*******"
46
47 self.padding=numpy.zeros(1,'int32')
48 self.hsize=numpy.zeros(1,'int32')
49 self.bufsize=numpy.zeros(1,'int32')
50 self.nr=numpy.zeros(1,'int32')
51 self.ngates=numpy.zeros(1,'int32') ### ### ### 2
52 self.time1=numpy.zeros(1,'uint64') # pos 3
53 self.time2=numpy.zeros(1,'uint64') # pos 4
54 self.lcounter=numpy.zeros(1,'int32')
55 self.groups=numpy.zeros(1,'int32')
56 self.system=numpy.zeros(4,'int8') # pos 7
57 self.h0=numpy.zeros(1,'float32')
58 self.dh=numpy.zeros(1,'float32')
59 self.ipp=numpy.zeros(1,'float32')
60 self.process=numpy.zeros(1,'int32')
61 self.tx=numpy.zeros(1,'int32')
62
63 self.ngates1=numpy.zeros(1,'int32') ### ### ### 13
64 self.time0=numpy.zeros(1,'uint64') # pos 14
65 self.nlags=numpy.zeros(1,'int32')
66 self.nlags1=numpy.zeros(1,'int32')
67 self.txb=numpy.zeros(1,'float32') ### ### ### 17
68 self.time3=numpy.zeros(1,'uint64') # pos 18
69 self.time4=numpy.zeros(1,'uint64') # pos 19
70 self.h0_=numpy.zeros(1,'float32')
71 self.dh_=numpy.zeros(1,'float32')
72 self.ipp_=numpy.zeros(1,'float32')
73 self.txa_=numpy.zeros(1,'float32')
74
75 self.pad=numpy.zeros(100,'int32')
76
77 self.nbytes=numpy.zeros(1,'int32')
78 self.limits=numpy.zeros(1,'int32')
79 self.ngroups=numpy.zeros(1,'int32') ### ### ### 27
80 #Make the header list
81 #header=[hsize,bufsize,nr,ngates,time1,time2,lcounter,groups,system,h0,dh,ipp,process,tx,padding,ngates1,time0,nlags,nlags1,padding,txb,time3,time4,h0_,dh_,ipp_,txa_,pad,nbytes,limits,padding,ngroups]
82 self.header=[self.hsize,self.bufsize,self.nr,self.ngates,self.time1,self.time2,self.lcounter,self.groups,self.system,self.h0,self.dh,self.ipp,self.process,self.tx,self.ngates1,self.padding,self.time0,self.nlags,self.nlags1,self.padding,self.txb,self.time3,self.time4,self.h0_,self.dh_,self.ipp_,self.txa_,self.pad,self.nbytes,self.limits,self.padding,self.ngroups]
83
84
85
86 def setup(self, **kwargs):
87
88 self.set_kwargs(**kwargs)
89
90
91 if self.path is None:
92 raise ValueError('The path is not valid')
93
94 self.open_file = open
95 self.open_mode = 'rb'
96
97
98
99 if self.format is None:
100 raise ValueError('The format is not valid')
101 elif self.format.lower() in ('dat'):
102 self.ext = '.dat'
103 elif self.format.lower() in ('out'):
104 self.ext = '.out'
105
106
107 log.log("Searching files in {}".format(self.path), self.name)
108 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
109 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
110 #print(self.path)
111 #print(self.filenameList)
112 #input()
113
114
115 self.setNextFile()
116
117 def readFirstHeader(self):
118 '''Read header and data'''
119
120 #self.flag_same_file=1
121 self.counter_block=0
122 self.parseHeader()
123 self.parseData()
124 self.blockIndex = 0
125
126 return
127
128 def parseHeader(self):
129 '''
130 '''
131
132 for i in range(len(self.header)):
133 for j in range(len(self.header[i])):
134 #print("len(header[i]) ",len(header[i]))
135 #input()
136 temp=self.fp.read(int(self.header[i].itemsize))
137 if isinstance(self.header[i][0], numpy.int32):
138 #print(struct.unpack('i', temp)[0])
139 self.header[i][0]=struct.unpack('i', temp)[0]
140 if isinstance(self.header[i][0], numpy.uint64):
141 self.header[i][0]=struct.unpack('q', temp)[0]
142 if isinstance(self.header[i][0], numpy.int8):
143 self.header[i][0]=struct.unpack('B', temp)[0]
144 if isinstance(self.header[i][0], numpy.float32):
145 self.header[i][0]=struct.unpack('f', temp)[0]
146
147 self.fp.seek(0,0)
148 if int(self.header[1][0])==int(81864):
149 self.experiment='DP'
150
151 elif int(self.header[1][0])==int(185504):
152 self.experiment='HP'
153
154
155 self.total_blocks=os.stat(self.filename).st_size//self.header[1][0]
156
157
158 def parseData(self):
159 '''
160 '''
161 if self.experiment=='DP':
162 self.header[15][0]=66
163 self.header[18][0]=16
164 self.header[17][0]=11
165 self.header[2][0]=2
166
167
168 self.noise=numpy.zeros(self.header[2][0],'float32') #self.header[2][0]
169 #tmpx=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
170 self.kax=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
171 self.kay=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
172 self.kbx=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
173 self.kby=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
174 self.kax2=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
175 self.kay2=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
176 self.kbx2=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
177 self.kby2=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
178 self.kaxbx=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
179 self.kaxby=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
180 self.kaybx=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
181 self.kayby=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
182 self.kaxay=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
183 self.kbxby=numpy.zeros((self.header[15][0],self.header[17][0],2),'float32')
184 self.output_LP_real=numpy.zeros((self.header[18][0],200,self.header[2][0]),'float32')
185 self.output_LP_imag=numpy.zeros((self.header[18][0],200,self.header[2][0]),'float32')
186 self.final_cross_products=[self.kax,self.kay,self.kbx,self.kby,self.kax2,self.kay2,self.kbx2,self.kby2,self.kaxbx,self.kaxby,self.kaybx,self.kayby,self.kaxay,self.kbxby]
187 #self.final_cross_products=[tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx]
188
189 #print("pos: ",self.fp.tell())
190
191
192 def readNextBlock(self):
193
194 while True:
195 self.flagDiscontinuousBlock = 0
196 #print(os.stat(self.filename).st_size)
197 #print(os.stat(self.filename).st_size//self.header[1][0])
198 #os.stat(self.fp)
199 if self.counter_block == self.total_blocks:
200
201 self.setNextFile()
202
203 self.readBlock()
204 #self.counter_block+=1
205
206 if (self.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or \
207 (self.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
208
209 #print(self.datatime)
210 #print(datetime.datetime.combine(self.startDate, self.startTime))
211 #print(datetime.datetime.combine(self.endDate, self.endTime))
212 #print("warning")
213 log.warning(
214 'Reading Block No. {}/{} -> {} [Skipping]'.format(
215 self.counter_block,
216 self.total_blocks,
217 self.datatime.ctime()),
218 'DATReader')
219 continue
220 break
221
222 log.log(
223 'Reading Block No. {}/{} -> {}'.format(
224 self.counter_block,
225 self.total_blocks,
226 self.datatime.ctime()),
227 'DATReader')
228
229 return 1
230
231 def readBlock(self):
232 '''
233 '''
234
235 self.npos=self.counter_block*self.header[1][0]
236 #print(self.counter_block)
237 self.fp.seek(self.npos, 0)
238 self.counter_block+=1
239 #print("fpos1: ",self.fp.tell())
240
241 self.read_header()
242
243 #put by hand because old files didn't save it in the header
244 if self.experiment=='DP':
245 self.header[15][0]=66
246 self.header[18][0]=16
247 self.header[17][0]=11
248 self.header[2][0]=2
249 #########################################
250
251 if self.experiment=="HP":
252 self.long_pulse_products()
253
254 self.read_cross_products()
255
256
257 self.read_noise()
258
259
260 return
261
262
263
264 def read_header(self):
265
266
267 for i in range(len(self.header)):
268 for j in range(len(self.header[i])):
269 #print("len(header[i]) ",len(header[i]))
270 #input()
271 temp=self.fp.read(int(self.header[i].itemsize))
272 #if(b''==temp):
273 # self.setNextFile()
274 # self.flag_same_file=0
275 if isinstance(self.header[i][0], numpy.int32):
276 #print(struct.unpack('i', temp)[0])
277 self.header[i][0]=struct.unpack('i', temp)[0]
278 if isinstance(self.header[i][0], numpy.uint64):
279 self.header[i][0]=struct.unpack('q', temp)[0]
280 if isinstance(self.header[i][0], numpy.int8):
281 self.header[i][0]=struct.unpack('B', temp)[0]
282 if isinstance(self.header[i][0], numpy.float32):
283 self.header[i][0]=struct.unpack('f', temp)[0]
284 #else:
285 # continue
286 #self.fp.seek(self.npos_aux, 0)
287 # break
288
289 #print("fpos2: ",self.fp.tell())
290 #log.success('Parameters found: {}'.format(self.parameters),
291 # 'DATReader')
292 #print("Success")
293 #self.TimeBlockSeconds_for_dp_power = self.header[4][0]#-((self.dataOut.nint-1)*self.dataOut.NAVG*2)
294 #print(dataOut.TimeBlockSeconds_for_dp_power)
295
296 #self.datatime=datetime.datetime.fromtimestamp(self.header[4][0]).strftime("%Y-%m-%d %H:%M:%S")
297 #print(self.header[4][0])
298 self.datatime=datetime.datetime.fromtimestamp(self.header[4][0])
299 #print(self.header[1][0])
300
301 def long_pulse_products(self):
302 temp=self.fp.read(self.header[18][0]*self.header[2][0]*200*8)
303 ii=0
304
305 for l in range(self.header[18][0]): #lag
306 for r in range(self.header[2][0]): # channels
307 for k in range(200): #RANGE## generalizar
308 self.output_LP_real[l,k,r]=struct.unpack('f', temp[ii:ii+4])[0]
309 ii=ii+4
310 self.output_LP_imag[l,k,r]=struct.unpack('f', temp[ii:ii+4])[0]
311 ii=ii+4
312
313 #print(self.output_LP_real[1,1,1])
314 #print(self.output_LP_imag[1,1,1])
315 def read_cross_products(self):
316
317 for ind in range(len(self.final_cross_products)): #final cross products
318 temp=self.fp.read(self.header[17][0]*2*self.header[15][0]*4) #*4 bytes
319 #if(b''==temp):
320 # self.setNextFile()
321 # self.flag_same_file=0
322 ii=0
323 #print("kabxys.shape ",kabxys.shape)
324 #print(kabxys)
325 #print("fpos3: ",self.fp.tell())
326 for l in range(self.header[17][0]): #lag
327 #print("fpos3: ",self.fp.tell())
328 for fl in range(2): # unflip and flip
329 for k in range(self.header[15][0]): #RANGE
330 #print("fpos3: ",self.fp.tell())
331 self.final_cross_products[ind][k,l,fl]=struct.unpack('f', temp[ii:ii+4])[0]
332 ii=ii+4
333 #print("fpos2: ",self.fp.tell())
334
335
336
337 def read_noise(self):
338
339 temp=self.fp.read(self.header[2][0]*4) #*4 bytes self.header[2][0]
340 for ii in range(self.header[2][0]): #self.header[2][0]
341 self.noise[ii]=struct.unpack('f', temp[ii*4:(ii+1)*4])[0]
342
343 #print("fpos5: ",self.fp.tell())
344
345
346
347 def set_output(self):
348 '''
349 Storing data from buffer to dataOut object
350 '''
351 #print("fpos2: ",self.fp.tell())
352 ##self.dataOut.header = self.header
353 #this is put by hand because it isn't saved in the header
354 if self.experiment=='DP':
355 self.dataOut.NRANGE=0
356 self.dataOut.NSCAN=132
357 self.dataOut.heightList=self.header[10][0]*(numpy.arange(self.header[15][0]))
358 elif self.experiment=='HP':
359 self.dataOut.output_LP=self.output_LP_real+1.j*self.output_LP_imag
360 self.dataOut.NRANGE=200
361 self.dataOut.NSCAN=128
362 self.dataOut.heightList=self.header[10][0]*(numpy.arange(90)) #NEEEDS TO BE GENERALIZED
363 #########################################
364 #print(self.dataOut.output_LP[1,1,1])
365 self.dataOut.MAXNRANGENDT=self.header[3][0]
366 self.dataOut.NDP=self.header[15][0]
367 self.dataOut.DPL=self.header[17][0]
368 self.dataOut.DH=self.header[10][0]
369 self.dataOut.NAVG=self.header[7][0]
370 self.dataOut.H0=self.header[9][0]
371 self.dataOut.NR=self.header[2][0]
372 self.dataOut.NLAG=self.header[18][0]
373 #self.dataOut.tmpx=self.tmpx
374 #self.dataOut.timeZone = 5
375 #self.dataOut.final_cross_products=self.final_cross_products
376 self.dataOut.kax=self.kax
377 #print(self.dataOut.kax[1,1,1])
378 self.dataOut.kay=self.kay
379 self.dataOut.kbx=self.kbx
380 self.dataOut.kby=self.kby
381 self.dataOut.kax2=self.kax2
382 self.dataOut.kay2=self.kay2
383 self.dataOut.kbx2=self.kbx2
384 self.dataOut.kby2=self.kby2
385 self.dataOut.kaxbx=self.kaxbx
386 self.dataOut.kaxby=self.kaxby
387 self.dataOut.kaybx=self.kaybx
388 self.dataOut.kayby=self.kayby
389 self.dataOut.kaxay=self.kaxay
390 self.dataOut.kbxby=self.kbxby
391 self.dataOut.noise_final=self.noise
392 #print("NOISE",self.noise)
393
394
395 self.dataOut.useLocalTime=True
396
397 #self.dataOut.experiment=self.experiment
398 #print(self.datatime)
399 #print(self.dataOut.datatime)
400
401
402 #self.dataOut.utctime = (self.datatime - datetime.datetime(1970, 1, 1)).total_seconds()
403 #self.dataOut.utctimeInit = self.dataOut.utctime
404
405
406
407 self.dataOut.lt=self.datatime.hour
408
409
410 #print(RadarControllerHeader().ippSeconds)
411 #print(RadarControllerHeader().ipp)
412 #self.dataOut.utctime=time.gmtime(self.header[4][0])- datetime.datetime(1970, 1, 1)
413 #self.dataOut.utctime=self.dataOut.utctime.total_seconds()
414 #time1 = self.header[4][0] # header.time1
415 #print("time1: ",time1)
416 #print(self.header[4][0])
417 #date = time.ctime(time1)
418 #print("DADSADA",time.strptime(date))
419 #print("date_before: ",date)
420 #bd_time=time.gmtime(time1)
421 #print(time.mktime(bd_time))
422 #self.dataOut.utctime=time.mktime(bd_time)
423 self.dataOut.utctime = self.header[4][0]
424 #self.dataOut.datatime=a
425 #print(datetime.datetime.utcfromtimestamp(self.dataOut.utctime))
426 #self.dataOut.TimeBlockDate=self.datatime.ctime()
427 self.dataOut.TimeBlockSeconds=time.mktime(time.strptime(self.dataOut.datatime.ctime()))
428
429 #self.dataOut.heightList = self.ranges
430 #self.dataOut.utctime = (self.datatime - datetime.datetime(1970, 1, 1)).total_seconds()
431 #self.dataOut.utctimeInit = self.dataOut.utctime
432 #self.dataOut.paramInterval = min(self.intervals)
433 #self.dataOut.useLocalTime = False
434 self.dataOut.flagNoData = False
435 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
436 #print(self.dataOut.channelIndexList)
437 self.dataOut.channelList=list(range(0,self.header[2][0]))
438 #print(self.dataOut.channelList)
439 #print(self.datatime)
440 #print(self.dataOut.final_cross_products[0])
441
442
443 #self.dataOut.heightList=self.header[10][0]*(numpy.arange(self.header[15][0]))
444
445 #print(numpy.shape(self.dataOut.heightList))
446
447
448 def getData(self):
449 '''
450 Storing data from databuffer to dataOut object
451 '''
452
453 if not self.readNextBlock():
454 self.dataOut.flagNoData = True
455 return 0
456
457 self.set_output()
458
459 return 1
460
461 def run(self, **kwargs):
462
463 if not(self.isConfig):
464 self.setup(**kwargs)
465 self.isConfig = True
466 #print("fpos1: ",self.fp.tell())
467 self.getData()
468
469 return
470
471 @MPDecorator
472 class DatWriter(Operation):
473
474
475 def __init__(self):
476
477 Operation.__init__(self)
478 #self.dataOut = Voltage()
479 self.counter = 0
480 self.path = None
481 self.fp = None
482 return
483 #self.ext= '.dat'
484
485 def run(self, dataOut, path, format='dat', experiment=None, **kwargs):
486 print(dataOut.flagNoData)
487 print(dataOut.datatime.ctime())
488 print(dataOut.TimeBlockDate)
489 input()
490 #if dataOut.flag_save:
491 self.experiment=experiment
492 self.path=path
493 if self.experiment=='DP':
494 dataOut.header[1][0]=81864
495 elif self.experiment=='HP':
496 dataOut.header[1][0]=185504#173216
497 #dataOut.header[1][0]=bufsize
498 self.dataOut = dataOut
499 #print(self.dataOut.nint)
500 #self.bufsize=bufsize
501 if format == 'dat':
502 self.ext = '.dat'
503 if format == 'out':
504 self.ext = '.out'
505 self.putData()
506
507 return
508
509
510
511 def setFile(self):
512 '''
513 Create new out file object
514 '''
515
516 #self.dataOut.TimeBlockSeconds=time.mktime(time.strptime(self.dataOut.TimeBlockDate))
517 date = datetime.datetime.fromtimestamp(self.dataOut.TimeBlockSeconds)
518
519 #print("date",date)
520
521 filename = '{}{}{}'.format('jro',
522 date.strftime('%Y%m%d_%H%M%S'),
523 self.ext)
524 #print(filename)
525 #print(self.path)
526
527 self.fullname = os.path.join(self.path, filename)
528
529 if os.path.isfile(self.fullname) :
530 log.warning(
531 'Destination file {} already exists, previous file deleted.'.format(
532 self.fullname),
533 'DatWriter')
534 os.remove(self.fullname)
535
536 try:
537 log.success(
538 'Creating file: {}'.format(self.fullname),
539 'DatWriter')
540 if not os.path.exists(self.path):
541 os.makedirs(self.path)
542 #self.fp = madrigal.cedar.MadrigalCedarFile(self.fullname, True)
543 self.fp = open(self.fullname,'wb')
544
545 except ValueError as e:
546 log.error(
547 'Impossible to create *.out file',
548 'DatWriter')
549 return
550
551 return 1
552
553 def writeBlock(self):
554
555 #self.dataOut.paramInterval=2
556 #startTime = datetime.datetime.utcfromtimestamp(self.dataOut.utctime)
557 #print(startTime)
558 #endTime = startTime + datetime.timedelta(seconds=self.dataOut.paramInterval)
559
560 self.dataOut.header[0].astype('int32').tofile(self.fp)
561 self.dataOut.header[1].astype('int32').tofile(self.fp)
562 self.dataOut.header[2].astype('int32').tofile(self.fp)
563 self.dataOut.header[3].astype('int32').tofile(self.fp)
564 self.dataOut.header[4].astype('uint64').tofile(self.fp)
565 self.dataOut.header[5].astype('uint64').tofile(self.fp)
566 self.dataOut.header[6].astype('int32').tofile(self.fp)
567 self.dataOut.header[7].astype('int32').tofile(self.fp)
568 #print(dataOut.header[7])
569 self.dataOut.header[8].astype('int8').tofile(self.fp)
570 self.dataOut.header[9].astype('float32').tofile(self.fp)
571 self.dataOut.header[10].astype('float32').tofile(self.fp)
572 self.dataOut.header[11].astype('float32').tofile(self.fp)
573 self.dataOut.header[12].astype('int32').tofile(self.fp)
574 self.dataOut.header[13].astype('int32').tofile(self.fp)
575 self.dataOut.header[14].astype('int32').tofile(self.fp)
576 self.dataOut.header[15].astype('int32').tofile(self.fp)
577 self.dataOut.header[16].astype('uint64').tofile(self.fp)
578 self.dataOut.header[17].astype('int32').tofile(self.fp)
579 self.dataOut.header[18].astype('int32').tofile(self.fp)
580 self.dataOut.header[19].astype('int32').tofile(self.fp)
581 self.dataOut.header[20].astype('float32').tofile(self.fp)
582 self.dataOut.header[21].astype('uint64').tofile(self.fp)
583 self.dataOut.header[22].astype('uint64').tofile(self.fp)
584 self.dataOut.header[23].astype('float32').tofile(self.fp)
585 self.dataOut.header[24].astype('float32').tofile(self.fp)
586 self.dataOut.header[25].astype('float32').tofile(self.fp)
587 self.dataOut.header[26].astype('float32').tofile(self.fp)
588 self.dataOut.header[27].astype('int32').tofile(self.fp)
589 self.dataOut.header[28].astype('int32').tofile(self.fp)
590 self.dataOut.header[29].astype('int32').tofile(self.fp)
591 self.dataOut.header[30].astype('int32').tofile(self.fp)
592 self.dataOut.header[31].astype('int32').tofile(self.fp)
593 #print("tell before 1 ",self.fp.tell())
594 #input()
595
596 if self.experiment=="HP":
597 #print("INSIDE")
598 #tmp=numpy.zeros(1,dtype='complex64')
599 #print("tmp ",tmp)
600 #input()
601 #print(dataOut.NLAG)
602 #print(dataOut.NR)
603 #print(dataOut.NRANGE)
604 for l in range(self.dataOut.NLAG): #lag
605 for r in range(self.dataOut.NR): # unflip and flip
606 for k in range(self.dataOut.NRANGE): #RANGE
607 self.dataOut.output_LP.real[l,k,r].astype('float32').tofile(self.fp)
608 self.dataOut.output_LP.imag[l,k,r].astype('float32').tofile(self.fp)
609
610
611 #print("tell before 2 ",self.outputfile.tell())
612
613
614
615
616
617 #print(self.dataOut.output_LP[1,1,1])
618
619 #print(self.dataOut.kax)
620 final_cross_products=[self.dataOut.kax,self.dataOut.kay,self.dataOut.kbx,self.dataOut.kby,
621 self.dataOut.kax2,self.dataOut.kay2,self.dataOut.kbx2,self.dataOut.kby2,
622 self.dataOut.kaxbx,self.dataOut.kaxby,self.dataOut.kaybx,self.dataOut.kayby,
623 self.dataOut.kaxay,self.dataOut.kbxby]
624
625 #print(self.dataOut.kax)
626 #print("tell before crossp saving ",self.outputfile.tell())
627 for kabxys in final_cross_products:
628
629 for l in range(self.dataOut.DPL): #lag
630 for fl in range(2): # unflip and flip
631 for k in range(self.dataOut.NDT): #RANGE
632 kabxys[k,l,fl].astype('float32').tofile(self.fp)
633
634
635 #print("tell before noise saving ",self.outputfile.tell())
636
637
638 for nch in range(self.dataOut.NR):
639 self.dataOut.noise_final[nch].astype('float32').tofile(self.fp)
640
641 #print("tell before noise saving ",self.fp.tell())
642 #input()
643
644
645
646
647 log.log(
648 'Writing {} blocks'.format(
649 self.counter+1),
650 'DatWriter')
651
652
653
654
655
656
657 def putData(self):
658 #print("flagNoData",self.dataOut.flagNoData)
659 #print("flagDiscontinuousBlock",self.dataOut.flagDiscontinuousBlock)
660 #print(self.dataOut.flagNoData)
661
662 if self.dataOut.flagNoData:
663 return 0
664
665 if self.dataOut.flagDiscontinuousBlock:
666
667 self.counter = 0
668
669 if self.counter == 0:
670 self.setFile()
671 #if self.experiment=="HP":
672 #if self.dataOut.debris_activated==0:
673 #self.writeBlock()
674 #self.counter += 1
675 #else:
676 self.writeBlock()
677 self.counter += 1
678
679 def close(self):
680
681 if self.counter > 0:
682 self.fp.close()
683 log.success('Closing file {}'.format(self.fullname), 'DatWriter')
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,1069 +1,1169
1 # Copyright (c) 2012-2020 Jicamarca Radio Observatory
1 # Copyright (c) 2012-2020 Jicamarca Radio Observatory
2 # All rights reserved.
2 # All rights reserved.
3 #
3 #
4 # Distributed under the terms of the BSD 3-clause license.
4 # Distributed under the terms of the BSD 3-clause license.
5 """Definition of diferent Data objects for different types of data
5 """Definition of diferent Data objects for different types of data
6
6
7 Here you will find the diferent data objects for the different types
7 Here you will find the diferent data objects for the different types
8 of data, this data objects must be used as dataIn or dataOut objects in
8 of data, this data objects must be used as dataIn or dataOut objects in
9 processing units and operations. Currently the supported data objects are:
9 processing units and operations. Currently the supported data objects are:
10 Voltage, Spectra, SpectraHeis, Fits, Correlation and Parameters
10 Voltage, Spectra, SpectraHeis, Fits, Correlation and Parameters
11 """
11 """
12
12
13 import copy
13 import copy
14 import numpy
14 import numpy
15 import datetime
15 import datetime
16 import json
16 import json
17
17
18 import schainpy.admin
18 import schainpy.admin
19 from schainpy.utils import log
19 from schainpy.utils import log
20 from .jroheaderIO import SystemHeader, RadarControllerHeader
20 from .jroheaderIO import SystemHeader, RadarControllerHeader
21 from schainpy.model.data import _noise
21 from schainpy.model.data import _noise
22
22
23
23
24 def getNumpyDtype(dataTypeCode):
24 def getNumpyDtype(dataTypeCode):
25
25
26 if dataTypeCode == 0:
26 if dataTypeCode == 0:
27 numpyDtype = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
27 numpyDtype = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
28 elif dataTypeCode == 1:
28 elif dataTypeCode == 1:
29 numpyDtype = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
29 numpyDtype = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
30 elif dataTypeCode == 2:
30 elif dataTypeCode == 2:
31 numpyDtype = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
31 numpyDtype = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
32 elif dataTypeCode == 3:
32 elif dataTypeCode == 3:
33 numpyDtype = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
33 numpyDtype = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
34 elif dataTypeCode == 4:
34 elif dataTypeCode == 4:
35 numpyDtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
35 numpyDtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
36 elif dataTypeCode == 5:
36 elif dataTypeCode == 5:
37 numpyDtype = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
37 numpyDtype = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
38 else:
38 else:
39 raise ValueError('dataTypeCode was not defined')
39 raise ValueError('dataTypeCode was not defined')
40
40
41 return numpyDtype
41 return numpyDtype
42
42
43
43
44 def getDataTypeCode(numpyDtype):
44 def getDataTypeCode(numpyDtype):
45
45
46 if numpyDtype == numpy.dtype([('real', '<i1'), ('imag', '<i1')]):
46 if numpyDtype == numpy.dtype([('real', '<i1'), ('imag', '<i1')]):
47 datatype = 0
47 datatype = 0
48 elif numpyDtype == numpy.dtype([('real', '<i2'), ('imag', '<i2')]):
48 elif numpyDtype == numpy.dtype([('real', '<i2'), ('imag', '<i2')]):
49 datatype = 1
49 datatype = 1
50 elif numpyDtype == numpy.dtype([('real', '<i4'), ('imag', '<i4')]):
50 elif numpyDtype == numpy.dtype([('real', '<i4'), ('imag', '<i4')]):
51 datatype = 2
51 datatype = 2
52 elif numpyDtype == numpy.dtype([('real', '<i8'), ('imag', '<i8')]):
52 elif numpyDtype == numpy.dtype([('real', '<i8'), ('imag', '<i8')]):
53 datatype = 3
53 datatype = 3
54 elif numpyDtype == numpy.dtype([('real', '<f4'), ('imag', '<f4')]):
54 elif numpyDtype == numpy.dtype([('real', '<f4'), ('imag', '<f4')]):
55 datatype = 4
55 datatype = 4
56 elif numpyDtype == numpy.dtype([('real', '<f8'), ('imag', '<f8')]):
56 elif numpyDtype == numpy.dtype([('real', '<f8'), ('imag', '<f8')]):
57 datatype = 5
57 datatype = 5
58 else:
58 else:
59 datatype = None
59 datatype = None
60
60
61 return datatype
61 return datatype
62
62
63
63
64 def hildebrand_sekhon(data, navg):
64 def hildebrand_sekhon(data, navg):
65 """
65 """
66 This method is for the objective determination of the noise level in Doppler spectra. This
66 This method is for the objective determination of the noise level in Doppler spectra. This
67 implementation technique is based on the fact that the standard deviation of the spectral
67 implementation technique is based on the fact that the standard deviation of the spectral
68 densities is equal to the mean spectral density for white Gaussian noise
68 densities is equal to the mean spectral density for white Gaussian noise
69
69
70 Inputs:
70 Inputs:
71 Data : heights
71 Data : heights
72 navg : numbers of averages
72 navg : numbers of averages
73
73
74 Return:
74 Return:
75 mean : noise's level
75 mean : noise's level
76 """
76 """
77
77
78 sortdata = numpy.sort(data, axis=None)
78 sortdata = numpy.sort(data, axis=None)
79 #print(numpy.shape(data))
80 #exit()
79 '''
81 '''
80 lenOfData = len(sortdata)
82 lenOfData = len(sortdata)
81 nums_min = lenOfData*0.2
83 nums_min = lenOfData*0.2
82
84
83 if nums_min <= 5:
85 if nums_min <= 5:
84
86
85 nums_min = 5
87 nums_min = 5
86
88
87 sump = 0.
89 sump = 0.
88 sumq = 0.
90 sumq = 0.
89
91
90 j = 0
92 j = 0
91 cont = 1
93 cont = 1
92
94
93 while((cont == 1)and(j < lenOfData)):
95 while((cont == 1)and(j < lenOfData)):
94
96
95 sump += sortdata[j]
97 sump += sortdata[j]
96 sumq += sortdata[j]**2
98 sumq += sortdata[j]**2
97
99
98 if j > nums_min:
100 if j > nums_min:
99 rtest = float(j)/(j-1) + 1.0/navg
101 rtest = float(j)/(j-1) + 1.0/navg
100 if ((sumq*j) > (rtest*sump**2)):
102 if ((sumq*j) > (rtest*sump**2)):
101 j = j - 1
103 j = j - 1
102 sump = sump - sortdata[j]
104 sump = sump - sortdata[j]
103 sumq = sumq - sortdata[j]**2
105 sumq = sumq - sortdata[j]**2
104 cont = 0
106 cont = 0
105
107
106 j += 1
108 j += 1
107
109
108 lnoise = sump / j
110 lnoise = sump / j
109 '''
111 '''
110 return _noise.hildebrand_sekhon(sortdata, navg)
112 return _noise.hildebrand_sekhon(sortdata, navg)
111
113
112
114
113 class Beam:
115 class Beam:
114
116
115 def __init__(self):
117 def __init__(self):
116 self.codeList = []
118 self.codeList = []
117 self.azimuthList = []
119 self.azimuthList = []
118 self.zenithList = []
120 self.zenithList = []
119
121
120
122
121 class GenericData(object):
123 class GenericData(object):
122
124
123 flagNoData = True
125 flagNoData = True
124
126
125 def copy(self, inputObj=None):
127 def copy(self, inputObj=None):
126
128
127 if inputObj == None:
129 if inputObj == None:
128 return copy.deepcopy(self)
130 return copy.deepcopy(self)
129
131
130 for key in list(inputObj.__dict__.keys()):
132 for key in list(inputObj.__dict__.keys()):
131
133
132 attribute = inputObj.__dict__[key]
134 attribute = inputObj.__dict__[key]
133
135
134 # If this attribute is a tuple or list
136 # If this attribute is a tuple or list
135 if type(inputObj.__dict__[key]) in (tuple, list):
137 if type(inputObj.__dict__[key]) in (tuple, list):
136 self.__dict__[key] = attribute[:]
138 self.__dict__[key] = attribute[:]
137 continue
139 continue
138
140
139 # If this attribute is another object or instance
141 # If this attribute is another object or instance
140 if hasattr(attribute, '__dict__'):
142 if hasattr(attribute, '__dict__'):
141 self.__dict__[key] = attribute.copy()
143 self.__dict__[key] = attribute.copy()
142 continue
144 continue
143
145
144 self.__dict__[key] = inputObj.__dict__[key]
146 self.__dict__[key] = inputObj.__dict__[key]
145
147
146 def deepcopy(self):
148 def deepcopy(self):
147
149
148 return copy.deepcopy(self)
150 return copy.deepcopy(self)
149
151
150 def isEmpty(self):
152 def isEmpty(self):
151
153
152 return self.flagNoData
154 return self.flagNoData
153
155
154 def isReady(self):
156 def isReady(self):
155
157
156 return not self.flagNoData
158 return not self.flagNoData
157
159
158
160
159 class JROData(GenericData):
161 class JROData(GenericData):
160
162
161 systemHeaderObj = SystemHeader()
163 systemHeaderObj = SystemHeader()
162 radarControllerHeaderObj = RadarControllerHeader()
164 radarControllerHeaderObj = RadarControllerHeader()
163 type = None
165 type = None
164 datatype = None # dtype but in string
166 datatype = None # dtype but in string
165 nProfiles = None
167 nProfiles = None
166 heightList = None
168 heightList = None
167 channelList = None
169 channelList = None
168 flagDiscontinuousBlock = False
170 flagDiscontinuousBlock = False
169 useLocalTime = False
171 useLocalTime = False
170 utctime = None
172 utctime = None
171 timeZone = None
173 timeZone = None
172 dstFlag = None
174 dstFlag = None
173 errorCount = None
175 errorCount = None
174 blocksize = None
176 blocksize = None
175 flagDecodeData = False # asumo q la data no esta decodificada
177 flagDecodeData = False # asumo q la data no esta decodificada
176 flagDeflipData = False # asumo q la data no esta sin flip
178 flagDeflipData = False # asumo q la data no esta sin flip
177 flagShiftFFT = False
179 flagShiftFFT = False
178 nCohInt = None
180 nCohInt = None
179 windowOfFilter = 1
181 windowOfFilter = 1
180 C = 3e8
182 C = 3e8
181 frequency = 49.92e6
183 frequency = 49.92e6
182 realtime = False
184 realtime = False
183 beacon_heiIndexList = None
185 beacon_heiIndexList = None
184 last_block = None
186 last_block = None
185 blocknow = None
187 blocknow = None
186 azimuth = None
188 azimuth = None
187 zenith = None
189 zenith = None
188 beam = Beam()
190 beam = Beam()
189 profileIndex = None
191 profileIndex = None
190 error = None
192 error = None
191 data = None
193 data = None
192 nmodes = None
194 nmodes = None
193 metadata_list = ['heightList', 'timeZone', 'type']
195 metadata_list = ['heightList', 'timeZone', 'type']
194
196
195 def __str__(self):
197 def __str__(self):
196
198
197 return '{} - {}'.format(self.type, self.datatime())
199 return '{} - {}'.format(self.type, self.datatime())
198
200
199 def getNoise(self):
201 def getNoise(self):
200
202
201 raise NotImplementedError
203 raise NotImplementedError
202
204
203 @property
205 @property
204 def nChannels(self):
206 def nChannels(self):
205
207
206 return len(self.channelList)
208 return len(self.channelList)
207
209
208 @property
210 @property
209 def channelIndexList(self):
211 def channelIndexList(self):
210
212
211 return list(range(self.nChannels))
213 return list(range(self.nChannels))
212
214
213 @property
215 @property
214 def nHeights(self):
216 def nHeights(self):
215
217
216 return len(self.heightList)
218 return len(self.heightList)
217
219
218 def getDeltaH(self):
220 def getDeltaH(self):
219
221
220 return self.heightList[1] - self.heightList[0]
222 return self.heightList[1] - self.heightList[0]
221
223
222 @property
224 @property
223 def ltctime(self):
225 def ltctime(self):
224
226
225 if self.useLocalTime:
227 if self.useLocalTime:
226 return self.utctime - self.timeZone * 60
228 return self.utctime - self.timeZone * 60
227
229
228 return self.utctime
230 return self.utctime
229
231
230 @property
232 @property
231 def datatime(self):
233 def datatime(self):
232
234
233 datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime)
235 datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime)
234 return datatimeValue
236 return datatimeValue
235
237
236 def getTimeRange(self):
238 def getTimeRange(self):
237
239
238 datatime = []
240 datatime = []
239
241
240 datatime.append(self.ltctime)
242 datatime.append(self.ltctime)
241 datatime.append(self.ltctime + self.timeInterval + 1)
243 datatime.append(self.ltctime + self.timeInterval + 1)
242
244
243 datatime = numpy.array(datatime)
245 datatime = numpy.array(datatime)
244
246
245 return datatime
247 return datatime
246
248
247 def getFmaxTimeResponse(self):
249 def getFmaxTimeResponse(self):
248
250
249 period = (10**-6) * self.getDeltaH() / (0.15)
251 period = (10**-6) * self.getDeltaH() / (0.15)
250
252
251 PRF = 1. / (period * self.nCohInt)
253 PRF = 1. / (period * self.nCohInt)
252
254
253 fmax = PRF
255 fmax = PRF
254
256
255 return fmax
257 return fmax
256
258
257 def getFmax(self):
259 def getFmax(self):
258 PRF = 1. / (self.ippSeconds * self.nCohInt)
260 PRF = 1. / (self.ippSeconds * self.nCohInt)
259
261
260 fmax = PRF
262 fmax = PRF
261 return fmax
263 return fmax
262
264
263 def getVmax(self):
265 def getVmax(self):
264
266
265 _lambda = self.C / self.frequency
267 _lambda = self.C / self.frequency
266
268
267 vmax = self.getFmax() * _lambda / 2
269 vmax = self.getFmax() * _lambda / 2
268
270
269 return vmax
271 return vmax
270
272
271 @property
273 @property
272 def ippSeconds(self):
274 def ippSeconds(self):
273 '''
275 '''
274 '''
276 '''
275 return self.radarControllerHeaderObj.ippSeconds
277 return self.radarControllerHeaderObj.ippSeconds
276
278
277 @ippSeconds.setter
279 @ippSeconds.setter
278 def ippSeconds(self, ippSeconds):
280 def ippSeconds(self, ippSeconds):
279 '''
281 '''
280 '''
282 '''
281 self.radarControllerHeaderObj.ippSeconds = ippSeconds
283 self.radarControllerHeaderObj.ippSeconds = ippSeconds
282
284
283 @property
285 @property
284 def code(self):
286 def code(self):
285 '''
287 '''
286 '''
288 '''
287 return self.radarControllerHeaderObj.code
289 return self.radarControllerHeaderObj.code
288
290
289 @code.setter
291 @code.setter
290 def code(self, code):
292 def code(self, code):
291 '''
293 '''
292 '''
294 '''
293 self.radarControllerHeaderObj.code = code
295 self.radarControllerHeaderObj.code = code
294
296
295 @property
297 @property
296 def nCode(self):
298 def nCode(self):
297 '''
299 '''
298 '''
300 '''
299 return self.radarControllerHeaderObj.nCode
301 return self.radarControllerHeaderObj.nCode
300
302
301 @nCode.setter
303 @nCode.setter
302 def nCode(self, ncode):
304 def nCode(self, ncode):
303 '''
305 '''
304 '''
306 '''
305 self.radarControllerHeaderObj.nCode = ncode
307 self.radarControllerHeaderObj.nCode = ncode
306
308
307 @property
309 @property
308 def nBaud(self):
310 def nBaud(self):
309 '''
311 '''
310 '''
312 '''
311 return self.radarControllerHeaderObj.nBaud
313 return self.radarControllerHeaderObj.nBaud
312
314
313 @nBaud.setter
315 @nBaud.setter
314 def nBaud(self, nbaud):
316 def nBaud(self, nbaud):
315 '''
317 '''
316 '''
318 '''
317 self.radarControllerHeaderObj.nBaud = nbaud
319 self.radarControllerHeaderObj.nBaud = nbaud
318
320
319 @property
321 @property
320 def ipp(self):
322 def ipp(self):
321 '''
323 '''
322 '''
324 '''
323 return self.radarControllerHeaderObj.ipp
325 return self.radarControllerHeaderObj.ipp
324
326
325 @ipp.setter
327 @ipp.setter
326 def ipp(self, ipp):
328 def ipp(self, ipp):
327 '''
329 '''
328 '''
330 '''
329 self.radarControllerHeaderObj.ipp = ipp
331 self.radarControllerHeaderObj.ipp = ipp
330
332
331 @property
333 @property
332 def metadata(self):
334 def metadata(self):
333 '''
335 '''
334 '''
336 '''
335
337
336 return {attr: getattr(self, attr) for attr in self.metadata_list}
338 return {attr: getattr(self, attr) for attr in self.metadata_list}
337
339
338
340
339 class Voltage(JROData):
341 class Voltage(JROData):
340
342
341 dataPP_POW = None
343 dataPP_POW = None
342 dataPP_DOP = None
344 dataPP_DOP = None
343 dataPP_WIDTH = None
345 dataPP_WIDTH = None
344 dataPP_SNR = None
346 dataPP_SNR = None
345
347
346 def __init__(self):
348 def __init__(self):
347 '''
349 '''
348 Constructor
350 Constructor
349 '''
351 '''
350
352
351 self.useLocalTime = True
353 self.useLocalTime = True
352 self.radarControllerHeaderObj = RadarControllerHeader()
354 self.radarControllerHeaderObj = RadarControllerHeader()
353 self.systemHeaderObj = SystemHeader()
355 self.systemHeaderObj = SystemHeader()
354 self.type = "Voltage"
356 self.type = "Voltage"
355 self.data = None
357 self.data = None
356 self.nProfiles = None
358 self.nProfiles = None
357 self.heightList = None
359 self.heightList = None
358 self.channelList = None
360 self.channelList = None
359 self.flagNoData = True
361 self.flagNoData = True
360 self.flagDiscontinuousBlock = False
362 self.flagDiscontinuousBlock = False
361 self.utctime = None
363 self.utctime = None
362 self.timeZone = 0
364 self.timeZone = 0
363 self.dstFlag = None
365 self.dstFlag = None
364 self.errorCount = None
366 self.errorCount = None
365 self.nCohInt = None
367 self.nCohInt = None
366 self.blocksize = None
368 self.blocksize = None
367 self.flagCohInt = False
369 self.flagCohInt = False
368 self.flagDecodeData = False # asumo q la data no esta decodificada
370 self.flagDecodeData = False # asumo q la data no esta decodificada
369 self.flagDeflipData = False # asumo q la data no esta sin flip
371 self.flagDeflipData = False # asumo q la data no esta sin flip
370 self.flagShiftFFT = False
372 self.flagShiftFFT = False
371 self.flagDataAsBlock = False # Asumo que la data es leida perfil a perfil
373 self.flagDataAsBlock = False # Asumo que la data es leida perfil a perfil
372 self.profileIndex = 0
374 self.profileIndex = 0
373 self.metadata_list = ['type', 'heightList', 'timeZone', 'nProfiles', 'channelList', 'nCohInt',
375 self.metadata_list = ['type', 'heightList', 'timeZone', 'nProfiles', 'channelList', 'nCohInt',
374 'code', 'nCode', 'nBaud', 'ippSeconds', 'ipp']
376 'code', 'nCode', 'nBaud', 'ippSeconds', 'ipp']
375
377
376 def getNoisebyHildebrand(self, channel=None):
378 def getNoisebyHildebrand(self, channel=None):
377 """
379 """
378 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
380 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
379
381
380 Return:
382 Return:
381 noiselevel
383 noiselevel
382 """
384 """
383
385
384 if channel != None:
386 if channel != None:
385 data = self.data[channel]
387 data = self.data[channel]
386 nChannels = 1
388 nChannels = 1
387 else:
389 else:
388 data = self.data
390 data = self.data
389 nChannels = self.nChannels
391 nChannels = self.nChannels
390
392
391 noise = numpy.zeros(nChannels)
393 noise = numpy.zeros(nChannels)
392 power = data * numpy.conjugate(data)
394 power = data * numpy.conjugate(data)
393
395
394 for thisChannel in range(nChannels):
396 for thisChannel in range(nChannels):
395 if nChannels == 1:
397 if nChannels == 1:
396 daux = power[:].real
398 daux = power[:].real
397 else:
399 else:
398 daux = power[thisChannel, :].real
400 daux = power[thisChannel, :].real
399 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
401 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
400
402
401 return noise
403 return noise
402
404
403 def getNoise(self, type=1, channel=None):
405 def getNoise(self, type=1, channel=None):
404
406
405 if type == 1:
407 if type == 1:
406 noise = self.getNoisebyHildebrand(channel)
408 noise = self.getNoisebyHildebrand(channel)
407
409
408 return noise
410 return noise
409
411
410 def getPower(self, channel=None):
412 def getPower(self, channel=None):
411
413
412 if channel != None:
414 if channel != None:
413 data = self.data[channel]
415 data = self.data[channel]
414 else:
416 else:
415 data = self.data
417 data = self.data
416
418
417 power = data * numpy.conjugate(data)
419 power = data * numpy.conjugate(data)
418 powerdB = 10 * numpy.log10(power.real)
420 powerdB = 10 * numpy.log10(power.real)
419 powerdB = numpy.squeeze(powerdB)
421 powerdB = numpy.squeeze(powerdB)
420
422
421 return powerdB
423 return powerdB
422
424
423 @property
425 @property
424 def timeInterval(self):
426 def timeInterval(self):
425
427
426 return self.ippSeconds * self.nCohInt
428 return self.ippSeconds * self.nCohInt
427
429
428 noise = property(getNoise, "I'm the 'nHeights' property.")
430 noise = property(getNoise, "I'm the 'nHeights' property.")
429
431
430
432
433 class CrossProds(JROData):
434
435 # data es un numpy array de 2 dmensiones (canales, alturas)
436 data = None
437
438 def __init__(self):
439 '''
440 Constructor
441 '''
442
443 self.useLocalTime = True
444 '''
445 self.radarControllerHeaderObj = RadarControllerHeader()
446 self.systemHeaderObj = SystemHeader()
447 self.type = "Voltage"
448 self.data = None
449 # self.dtype = None
450 # self.nChannels = 0
451 # self.nHeights = 0
452 self.nProfiles = None
453 self.heightList = None
454 self.channelList = None
455 # self.channelIndexList = None
456 self.flagNoData = True
457 self.flagDiscontinuousBlock = False
458 self.utctime = None
459 self.timeZone = None
460 self.dstFlag = None
461 self.errorCount = None
462 self.nCohInt = None
463 self.blocksize = None
464 self.flagDecodeData = False # asumo q la data no esta decodificada
465 self.flagDeflipData = False # asumo q la data no esta sin flip
466 self.flagShiftFFT = False
467 self.flagDataAsBlock = False # Asumo que la data es leida perfil a perfil
468 self.profileIndex = 0
469
470
471 def getNoisebyHildebrand(self, channel=None):
472
473
474 if channel != None:
475 data = self.data[channel]
476 nChannels = 1
477 else:
478 data = self.data
479 nChannels = self.nChannels
480
481 noise = numpy.zeros(nChannels)
482 power = data * numpy.conjugate(data)
483
484 for thisChannel in range(nChannels):
485 if nChannels == 1:
486 daux = power[:].real
487 else:
488 daux = power[thisChannel, :].real
489 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
490
491 return noise
492
493 def getNoise(self, type=1, channel=None):
494
495 if type == 1:
496 noise = self.getNoisebyHildebrand(channel)
497
498 return noise
499
500 def getPower(self, channel=None):
501
502 if channel != None:
503 data = self.data[channel]
504 else:
505 data = self.data
506
507 power = data * numpy.conjugate(data)
508 powerdB = 10 * numpy.log10(power.real)
509 powerdB = numpy.squeeze(powerdB)
510
511 return powerdB
512
513 def getTimeInterval(self):
514
515 timeInterval = self.ippSeconds * self.nCohInt
516
517 return timeInterval
518
519 noise = property(getNoise, "I'm the 'nHeights' property.")
520 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
521 '''
522 def getTimeInterval(self):
523
524 timeInterval = self.ippSeconds * self.nCohInt
525
526 return timeInterval
527
528
529
431 class Spectra(JROData):
530 class Spectra(JROData):
432
531
433 def __init__(self):
532 def __init__(self):
434 '''
533 '''
435 Constructor
534 Constructor
436 '''
535 '''
437
536
438 self.data_dc = None
537 self.data_dc = None
439 self.data_spc = None
538 self.data_spc = None
440 self.data_cspc = None
539 self.data_cspc = None
441 self.useLocalTime = True
540 self.useLocalTime = True
442 self.radarControllerHeaderObj = RadarControllerHeader()
541 self.radarControllerHeaderObj = RadarControllerHeader()
443 self.systemHeaderObj = SystemHeader()
542 self.systemHeaderObj = SystemHeader()
444 self.type = "Spectra"
543 self.type = "Spectra"
445 self.timeZone = 0
544 self.timeZone = 0
446 self.nProfiles = None
545 self.nProfiles = None
447 self.heightList = None
546 self.heightList = None
448 self.channelList = None
547 self.channelList = None
449 self.pairsList = None
548 self.pairsList = None
450 self.flagNoData = True
549 self.flagNoData = True
451 self.flagDiscontinuousBlock = False
550 self.flagDiscontinuousBlock = False
452 self.utctime = None
551 self.utctime = None
453 self.nCohInt = None
552 self.nCohInt = None
454 self.nIncohInt = None
553 self.nIncohInt = None
455 self.blocksize = None
554 self.blocksize = None
456 self.nFFTPoints = None
555 self.nFFTPoints = None
457 self.wavelength = None
556 self.wavelength = None
458 self.flagDecodeData = False # asumo q la data no esta decodificada
557 self.flagDecodeData = False # asumo q la data no esta decodificada
459 self.flagDeflipData = False # asumo q la data no esta sin flip
558 self.flagDeflipData = False # asumo q la data no esta sin flip
460 self.flagShiftFFT = False
559 self.flagShiftFFT = False
461 self.ippFactor = 1
560 self.ippFactor = 1
462 self.beacon_heiIndexList = []
561 self.beacon_heiIndexList = []
463 self.noise_estimation = None
562 self.noise_estimation = None
464 self.metadata_list = ['type', 'heightList', 'timeZone', 'pairsList', 'channelList', 'nCohInt',
563 self.metadata_list = ['type', 'heightList', 'timeZone', 'pairsList', 'channelList', 'nCohInt',
465 'code', 'nCode', 'nBaud', 'ippSeconds', 'ipp','nIncohInt', 'nFFTPoints', 'nProfiles']
564 'code', 'nCode', 'nBaud', 'ippSeconds', 'ipp','nIncohInt', 'nFFTPoints', 'nProfiles']
466
565
467 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
566 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
468 """
567 """
469 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
568 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
470
569
471 Return:
570 Return:
472 noiselevel
571 noiselevel
473 """
572 """
474
573
475 noise = numpy.zeros(self.nChannels)
574 noise = numpy.zeros(self.nChannels)
476
575
477 for channel in range(self.nChannels):
576 for channel in range(self.nChannels):
478 daux = self.data_spc[channel,
577 daux = self.data_spc[channel,
479 xmin_index:xmax_index, ymin_index:ymax_index]
578 xmin_index:xmax_index, ymin_index:ymax_index]
480 noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
579 noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
481
580
482 return noise
581 return noise
483
582
484 def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
583 def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
485
584
486 if self.noise_estimation is not None:
585 if self.noise_estimation is not None:
487 # this was estimated by getNoise Operation defined in jroproc_spectra.py
586 # this was estimated by getNoise Operation defined in jroproc_spectra.py
488 return self.noise_estimation
587 return self.noise_estimation
489 else:
588 else:
490 noise = self.getNoisebyHildebrand(
589 noise = self.getNoisebyHildebrand(
491 xmin_index, xmax_index, ymin_index, ymax_index)
590 xmin_index, xmax_index, ymin_index, ymax_index)
492 return noise
591 return noise
493
592
494 def getFreqRangeTimeResponse(self, extrapoints=0):
593 def getFreqRangeTimeResponse(self, extrapoints=0):
495
594
496 deltafreq = self.getFmaxTimeResponse() / (self.nFFTPoints * self.ippFactor)
595 deltafreq = self.getFmaxTimeResponse() / (self.nFFTPoints * self.ippFactor)
497 freqrange = deltafreq * (numpy.arange(self.nFFTPoints + extrapoints) - self.nFFTPoints / 2.) - deltafreq / 2
596 freqrange = deltafreq * (numpy.arange(self.nFFTPoints + extrapoints) - self.nFFTPoints / 2.) - deltafreq / 2
498
597
499 return freqrange
598 return freqrange
500
599
501 def getAcfRange(self, extrapoints=0):
600 def getAcfRange(self, extrapoints=0):
502
601
503 deltafreq = 10. / (self.getFmax() / (self.nFFTPoints * self.ippFactor))
602 deltafreq = 10. / (self.getFmax() / (self.nFFTPoints * self.ippFactor))
504 freqrange = deltafreq * (numpy.arange(self.nFFTPoints + extrapoints) -self.nFFTPoints / 2.) - deltafreq / 2
603 freqrange = deltafreq * (numpy.arange(self.nFFTPoints + extrapoints) -self.nFFTPoints / 2.) - deltafreq / 2
505
604
506 return freqrange
605 return freqrange
507
606
508 def getFreqRange(self, extrapoints=0):
607 def getFreqRange(self, extrapoints=0):
509
608
510 deltafreq = self.getFmax() / (self.nFFTPoints * self.ippFactor)
609 deltafreq = self.getFmax() / (self.nFFTPoints * self.ippFactor)
511 freqrange = deltafreq * (numpy.arange(self.nFFTPoints + extrapoints) -self.nFFTPoints / 2.) - deltafreq / 2
610 freqrange = deltafreq * (numpy.arange(self.nFFTPoints + extrapoints) -self.nFFTPoints / 2.) - deltafreq / 2
512
611
513 return freqrange
612 return freqrange
514
613
515 def getVelRange(self, extrapoints=0):
614 def getVelRange(self, extrapoints=0):
516
615
517 deltav = self.getVmax() / (self.nFFTPoints * self.ippFactor)
616 deltav = self.getVmax() / (self.nFFTPoints * self.ippFactor)
518 velrange = deltav * (numpy.arange(self.nFFTPoints + extrapoints) - self.nFFTPoints / 2.)
617 velrange = deltav * (numpy.arange(self.nFFTPoints + extrapoints) - self.nFFTPoints / 2.)
519
618
520 if self.nmodes:
619 if self.nmodes:
521 return velrange/self.nmodes
620 return velrange/self.nmodes
522 else:
621 else:
523 return velrange
622 return velrange
524
623
525 @property
624 @property
526 def nPairs(self):
625 def nPairs(self):
527
626
528 return len(self.pairsList)
627 return len(self.pairsList)
529
628
530 @property
629 @property
531 def pairsIndexList(self):
630 def pairsIndexList(self):
532
631
533 return list(range(self.nPairs))
632 return list(range(self.nPairs))
534
633
535 @property
634 @property
536 def normFactor(self):
635 def normFactor(self):
537
636
538 pwcode = 1
637 pwcode = 1
539
638
540 if self.flagDecodeData:
639 if self.flagDecodeData:
541 pwcode = numpy.sum(self.code[0]**2)
640 pwcode = numpy.sum(self.code[0]**2)
542 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
641 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
543 normFactor = self.nProfiles * self.nIncohInt * self.nCohInt * pwcode * self.windowOfFilter
642 normFactor = self.nProfiles * self.nIncohInt * self.nCohInt * pwcode * self.windowOfFilter
544
643
545 return normFactor
644 return normFactor
546
645
547 @property
646 @property
548 def flag_cspc(self):
647 def flag_cspc(self):
549
648
550 if self.data_cspc is None:
649 if self.data_cspc is None:
551 return True
650 return True
552
651
553 return False
652 return False
554
653
555 @property
654 @property
556 def flag_dc(self):
655 def flag_dc(self):
557
656
558 if self.data_dc is None:
657 if self.data_dc is None:
559 return True
658 return True
560
659
561 return False
660 return False
562
661
563 @property
662 @property
564 def timeInterval(self):
663 def timeInterval(self):
565
664
566 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles * self.ippFactor
665 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles * self.ippFactor
567 if self.nmodes:
666 if self.nmodes:
568 return self.nmodes*timeInterval
667 return self.nmodes*timeInterval
569 else:
668 else:
570 return timeInterval
669 return timeInterval
571
670
572 def getPower(self):
671 def getPower(self):
573
672
574 factor = self.normFactor
673 factor = self.normFactor
575 z = self.data_spc / factor
674 z = self.data_spc / factor
576 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
675 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
577 avg = numpy.average(z, axis=1)
676 avg = numpy.average(z, axis=1)
578
677
579 return 10 * numpy.log10(avg)
678 return 10 * numpy.log10(avg)
580
679
581 def getCoherence(self, pairsList=None, phase=False):
680 def getCoherence(self, pairsList=None, phase=False):
582
681
583 z = []
682 z = []
584 if pairsList is None:
683 if pairsList is None:
585 pairsIndexList = self.pairsIndexList
684 pairsIndexList = self.pairsIndexList
586 else:
685 else:
587 pairsIndexList = []
686 pairsIndexList = []
588 for pair in pairsList:
687 for pair in pairsList:
589 if pair not in self.pairsList:
688 if pair not in self.pairsList:
590 raise ValueError("Pair %s is not in dataOut.pairsList" % (
689 raise ValueError("Pair %s is not in dataOut.pairsList" % (
591 pair))
690 pair))
592 pairsIndexList.append(self.pairsList.index(pair))
691 pairsIndexList.append(self.pairsList.index(pair))
593 for i in range(len(pairsIndexList)):
692 for i in range(len(pairsIndexList)):
594 pair = self.pairsList[pairsIndexList[i]]
693 pair = self.pairsList[pairsIndexList[i]]
595 ccf = numpy.average(self.data_cspc[pairsIndexList[i], :, :], axis=0)
694 ccf = numpy.average(self.data_cspc[pairsIndexList[i], :, :], axis=0)
596 powa = numpy.average(self.data_spc[pair[0], :, :], axis=0)
695 powa = numpy.average(self.data_spc[pair[0], :, :], axis=0)
597 powb = numpy.average(self.data_spc[pair[1], :, :], axis=0)
696 powb = numpy.average(self.data_spc[pair[1], :, :], axis=0)
598 avgcoherenceComplex = ccf / numpy.sqrt(powa * powb)
697 avgcoherenceComplex = ccf / numpy.sqrt(powa * powb)
599 if phase:
698 if phase:
600 data = numpy.arctan2(avgcoherenceComplex.imag,
699 data = numpy.arctan2(avgcoherenceComplex.imag,
601 avgcoherenceComplex.real) * 180 / numpy.pi
700 avgcoherenceComplex.real) * 180 / numpy.pi
602 else:
701 else:
603 data = numpy.abs(avgcoherenceComplex)
702 data = numpy.abs(avgcoherenceComplex)
604
703
605 z.append(data)
704 z.append(data)
606
705
607 return numpy.array(z)
706 return numpy.array(z)
608
707
609 def setValue(self, value):
708 def setValue(self, value):
610
709
611 print("This property should not be initialized")
710 print("This property should not be initialized")
612
711
613 return
712 return
614
713
615 noise = property(getNoise, setValue, "I'm the 'nHeights' property.")
714 noise = property(getNoise, setValue, "I'm the 'nHeights' property.")
616
715
617
716
618 class SpectraHeis(Spectra):
717 class SpectraHeis(Spectra):
619
718
620 def __init__(self):
719 def __init__(self):
621
720
622 self.radarControllerHeaderObj = RadarControllerHeader()
721 self.radarControllerHeaderObj = RadarControllerHeader()
623 self.systemHeaderObj = SystemHeader()
722 self.systemHeaderObj = SystemHeader()
624 self.type = "SpectraHeis"
723 self.type = "SpectraHeis"
625 self.nProfiles = None
724 self.nProfiles = None
626 self.heightList = None
725 self.heightList = None
627 self.channelList = None
726 self.channelList = None
628 self.flagNoData = True
727 self.flagNoData = True
629 self.flagDiscontinuousBlock = False
728 self.flagDiscontinuousBlock = False
630 self.utctime = None
729 self.utctime = None
631 self.blocksize = None
730 self.blocksize = None
632 self.profileIndex = 0
731 self.profileIndex = 0
633 self.nCohInt = 1
732 self.nCohInt = 1
634 self.nIncohInt = 1
733 self.nIncohInt = 1
635
734
636 @property
735 @property
637 def normFactor(self):
736 def normFactor(self):
638 pwcode = 1
737 pwcode = 1
639 if self.flagDecodeData:
738 if self.flagDecodeData:
640 pwcode = numpy.sum(self.code[0]**2)
739 pwcode = numpy.sum(self.code[0]**2)
641
740
642 normFactor = self.nIncohInt * self.nCohInt * pwcode
741 normFactor = self.nIncohInt * self.nCohInt * pwcode
643
742
644 return normFactor
743 return normFactor
645
744
646 @property
745 @property
647 def timeInterval(self):
746 def timeInterval(self):
648
747
649 return self.ippSeconds * self.nCohInt * self.nIncohInt
748 return self.ippSeconds * self.nCohInt * self.nIncohInt
650
749
651
750
652 class Fits(JROData):
751 class Fits(JROData):
653
752
654 def __init__(self):
753 def __init__(self):
655
754
656 self.type = "Fits"
755 self.type = "Fits"
657 self.nProfiles = None
756 self.nProfiles = None
658 self.heightList = None
757 self.heightList = None
659 self.channelList = None
758 self.channelList = None
660 self.flagNoData = True
759 self.flagNoData = True
661 self.utctime = None
760 self.utctime = None
662 self.nCohInt = 1
761 self.nCohInt = 1
663 self.nIncohInt = 1
762 self.nIncohInt = 1
664 self.useLocalTime = True
763 self.useLocalTime = True
665 self.profileIndex = 0
764 self.profileIndex = 0
666 self.timeZone = 0
765 self.timeZone = 0
667
766
668 def getTimeRange(self):
767 def getTimeRange(self):
669
768
670 datatime = []
769 datatime = []
671
770
672 datatime.append(self.ltctime)
771 datatime.append(self.ltctime)
673 datatime.append(self.ltctime + self.timeInterval)
772 datatime.append(self.ltctime + self.timeInterval)
674
773
675 datatime = numpy.array(datatime)
774 datatime = numpy.array(datatime)
676
775
677 return datatime
776 return datatime
678
777
679 def getChannelIndexList(self):
778 def getChannelIndexList(self):
680
779
681 return list(range(self.nChannels))
780 return list(range(self.nChannels))
682
781
683 def getNoise(self, type=1):
782 def getNoise(self, type=1):
684
783
685
784
686 if type == 1:
785 if type == 1:
687 noise = self.getNoisebyHildebrand()
786 noise = self.getNoisebyHildebrand()
688
787
689 if type == 2:
788 if type == 2:
690 noise = self.getNoisebySort()
789 noise = self.getNoisebySort()
691
790
692 if type == 3:
791 if type == 3:
693 noise = self.getNoisebyWindow()
792 noise = self.getNoisebyWindow()
694
793
695 return noise
794 return noise
696
795
697 @property
796 @property
698 def timeInterval(self):
797 def timeInterval(self):
699
798
700 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
799 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
701
800
702 return timeInterval
801 return timeInterval
703
802
704 @property
803 @property
705 def ippSeconds(self):
804 def ippSeconds(self):
706 '''
805 '''
707 '''
806 '''
708 return self.ipp_sec
807 return self.ipp_sec
709
808
710 noise = property(getNoise, "I'm the 'nHeights' property.")
809 noise = property(getNoise, "I'm the 'nHeights' property.")
711
810
712
811
713 class Correlation(JROData):
812 class Correlation(JROData):
714
813
715 def __init__(self):
814 def __init__(self):
716 '''
815 '''
717 Constructor
816 Constructor
718 '''
817 '''
719 self.radarControllerHeaderObj = RadarControllerHeader()
818 self.radarControllerHeaderObj = RadarControllerHeader()
720 self.systemHeaderObj = SystemHeader()
819 self.systemHeaderObj = SystemHeader()
721 self.type = "Correlation"
820 self.type = "Correlation"
722 self.data = None
821 self.data = None
723 self.dtype = None
822 self.dtype = None
724 self.nProfiles = None
823 self.nProfiles = None
725 self.heightList = None
824 self.heightList = None
726 self.channelList = None
825 self.channelList = None
727 self.flagNoData = True
826 self.flagNoData = True
728 self.flagDiscontinuousBlock = False
827 self.flagDiscontinuousBlock = False
729 self.utctime = None
828 self.utctime = None
730 self.timeZone = 0
829 self.timeZone = 0
731 self.dstFlag = None
830 self.dstFlag = None
732 self.errorCount = None
831 self.errorCount = None
733 self.blocksize = None
832 self.blocksize = None
734 self.flagDecodeData = False # asumo q la data no esta decodificada
833 self.flagDecodeData = False # asumo q la data no esta decodificada
735 self.flagDeflipData = False # asumo q la data no esta sin flip
834 self.flagDeflipData = False # asumo q la data no esta sin flip
736 self.pairsList = None
835 self.pairsList = None
737 self.nPoints = None
836 self.nPoints = None
738
837
739 def getPairsList(self):
838 def getPairsList(self):
740
839
741 return self.pairsList
840 return self.pairsList
742
841
743 def getNoise(self, mode=2):
842 def getNoise(self, mode=2):
744
843
745 indR = numpy.where(self.lagR == 0)[0][0]
844 indR = numpy.where(self.lagR == 0)[0][0]
746 indT = numpy.where(self.lagT == 0)[0][0]
845 indT = numpy.where(self.lagT == 0)[0][0]
747
846
748 jspectra0 = self.data_corr[:, :, indR, :]
847 jspectra0 = self.data_corr[:, :, indR, :]
749 jspectra = copy.copy(jspectra0)
848 jspectra = copy.copy(jspectra0)
750
849
751 num_chan = jspectra.shape[0]
850 num_chan = jspectra.shape[0]
752 num_hei = jspectra.shape[2]
851 num_hei = jspectra.shape[2]
753
852
754 freq_dc = jspectra.shape[1] / 2
853 freq_dc = jspectra.shape[1] / 2
755 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
854 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
756
855
757 if ind_vel[0] < 0:
856 if ind_vel[0] < 0:
758 ind_vel[list(range(0, 1))] = ind_vel[list(
857 ind_vel[list(range(0, 1))] = ind_vel[list(
759 range(0, 1))] + self.num_prof
858 range(0, 1))] + self.num_prof
760
859
761 if mode == 1:
860 if mode == 1:
762 jspectra[:, freq_dc, :] = (
861 jspectra[:, freq_dc, :] = (
763 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
862 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
764
863
765 if mode == 2:
864 if mode == 2:
766
865
767 vel = numpy.array([-2, -1, 1, 2])
866 vel = numpy.array([-2, -1, 1, 2])
768 xx = numpy.zeros([4, 4])
867 xx = numpy.zeros([4, 4])
769
868
770 for fil in range(4):
869 for fil in range(4):
771 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
870 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
772
871
773 xx_inv = numpy.linalg.inv(xx)
872 xx_inv = numpy.linalg.inv(xx)
774 xx_aux = xx_inv[0, :]
873 xx_aux = xx_inv[0, :]
775
874
776 for ich in range(num_chan):
875 for ich in range(num_chan):
777 yy = jspectra[ich, ind_vel, :]
876 yy = jspectra[ich, ind_vel, :]
778 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
877 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
779
878
780 junkid = jspectra[ich, freq_dc, :] <= 0
879 junkid = jspectra[ich, freq_dc, :] <= 0
781 cjunkid = sum(junkid)
880 cjunkid = sum(junkid)
782
881
783 if cjunkid.any():
882 if cjunkid.any():
784 jspectra[ich, freq_dc, junkid.nonzero()] = (
883 jspectra[ich, freq_dc, junkid.nonzero()] = (
785 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
884 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
786
885
787 noise = jspectra0[:, freq_dc, :] - jspectra[:, freq_dc, :]
886 noise = jspectra0[:, freq_dc, :] - jspectra[:, freq_dc, :]
788
887
789 return noise
888 return noise
790
889
791 @property
890 @property
792 def timeInterval(self):
891 def timeInterval(self):
793
892
794 return self.ippSeconds * self.nCohInt * self.nProfiles
893 return self.ippSeconds * self.nCohInt * self.nProfiles
795
894
796 def splitFunctions(self):
895 def splitFunctions(self):
797
896
798 pairsList = self.pairsList
897 pairsList = self.pairsList
799 ccf_pairs = []
898 ccf_pairs = []
800 acf_pairs = []
899 acf_pairs = []
801 ccf_ind = []
900 ccf_ind = []
802 acf_ind = []
901 acf_ind = []
803 for l in range(len(pairsList)):
902 for l in range(len(pairsList)):
804 chan0 = pairsList[l][0]
903 chan0 = pairsList[l][0]
805 chan1 = pairsList[l][1]
904 chan1 = pairsList[l][1]
806
905
807 # Obteniendo pares de Autocorrelacion
906 # Obteniendo pares de Autocorrelacion
808 if chan0 == chan1:
907 if chan0 == chan1:
809 acf_pairs.append(chan0)
908 acf_pairs.append(chan0)
810 acf_ind.append(l)
909 acf_ind.append(l)
811 else:
910 else:
812 ccf_pairs.append(pairsList[l])
911 ccf_pairs.append(pairsList[l])
813 ccf_ind.append(l)
912 ccf_ind.append(l)
814
913
815 data_acf = self.data_cf[acf_ind]
914 data_acf = self.data_cf[acf_ind]
816 data_ccf = self.data_cf[ccf_ind]
915 data_ccf = self.data_cf[ccf_ind]
817
916
818 return acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf
917 return acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf
819
918
820 @property
919 @property
821 def normFactor(self):
920 def normFactor(self):
822 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.splitFunctions()
921 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.splitFunctions()
823 acf_pairs = numpy.array(acf_pairs)
922 acf_pairs = numpy.array(acf_pairs)
824 normFactor = numpy.zeros((self.nPairs, self.nHeights))
923 normFactor = numpy.zeros((self.nPairs, self.nHeights))
825
924
826 for p in range(self.nPairs):
925 for p in range(self.nPairs):
827 pair = self.pairsList[p]
926 pair = self.pairsList[p]
828
927
829 ch0 = pair[0]
928 ch0 = pair[0]
830 ch1 = pair[1]
929 ch1 = pair[1]
831
930
832 ch0_max = numpy.max(data_acf[acf_pairs == ch0, :, :], axis=1)
931 ch0_max = numpy.max(data_acf[acf_pairs == ch0, :, :], axis=1)
833 ch1_max = numpy.max(data_acf[acf_pairs == ch1, :, :], axis=1)
932 ch1_max = numpy.max(data_acf[acf_pairs == ch1, :, :], axis=1)
834 normFactor[p, :] = numpy.sqrt(ch0_max * ch1_max)
933 normFactor[p, :] = numpy.sqrt(ch0_max * ch1_max)
835
934
836 return normFactor
935 return normFactor
837
936
838
937
839 class Parameters(Spectra):
938 class Parameters(Spectra):
840
939
841 groupList = None # List of Pairs, Groups, etc
940 groupList = None # List of Pairs, Groups, etc
842 data_param = None # Parameters obtained
941 data_param = None # Parameters obtained
843 data_pre = None # Data Pre Parametrization
942 data_pre = None # Data Pre Parametrization
844 data_SNR = None # Signal to Noise Ratio
943 data_SNR = None # Signal to Noise Ratio
845 abscissaList = None # Abscissa, can be velocities, lags or time
944 abscissaList = None # Abscissa, can be velocities, lags or time
846 utctimeInit = None # Initial UTC time
945 utctimeInit = None # Initial UTC time
847 paramInterval = None # Time interval to calculate Parameters in seconds
946 paramInterval = None # Time interval to calculate Parameters in seconds
848 useLocalTime = True
947 useLocalTime = True
849 # Fitting
948 # Fitting
850 data_error = None # Error of the estimation
949 data_error = None # Error of the estimation
851 constants = None
950 constants = None
852 library = None
951 library = None
853 # Output signal
952 # Output signal
854 outputInterval = None # Time interval to calculate output signal in seconds
953 outputInterval = None # Time interval to calculate output signal in seconds
855 data_output = None # Out signal
954 data_output = None # Out signal
856 nAvg = None
955 nAvg = None
857 noise_estimation = None
956 noise_estimation = None
858 GauSPC = None # Fit gaussian SPC
957 GauSPC = None # Fit gaussian SPC
859
958
860 def __init__(self):
959 def __init__(self):
861 '''
960 '''
862 Constructor
961 Constructor
863 '''
962 '''
864 self.radarControllerHeaderObj = RadarControllerHeader()
963 self.radarControllerHeaderObj = RadarControllerHeader()
865 self.systemHeaderObj = SystemHeader()
964 self.systemHeaderObj = SystemHeader()
866 self.type = "Parameters"
965 self.type = "Parameters"
867 self.timeZone = 0
966 self.timeZone = 0
868
967
869 def getTimeRange1(self, interval):
968 def getTimeRange1(self, interval):
870
969
871 datatime = []
970 datatime = []
872
971
873 if self.useLocalTime:
972 if self.useLocalTime:
874 time1 = self.utctimeInit - self.timeZone * 60
973 time1 = self.utctimeInit - self.timeZone * 60
875 else:
974 else:
876 time1 = self.utctimeInit
975 time1 = self.utctimeInit
877
976
878 datatime.append(time1)
977 datatime.append(time1)
879 datatime.append(time1 + interval)
978 datatime.append(time1 + interval)
880 datatime = numpy.array(datatime)
979 datatime = numpy.array(datatime)
881
980
882 return datatime
981 return datatime
883
982
884 @property
983 @property
885 def timeInterval(self):
984 def timeInterval(self):
886
985
887 if hasattr(self, 'timeInterval1'):
986 if hasattr(self, 'timeInterval1'):
888 return self.timeInterval1
987 return self.timeInterval1
889 else:
988 else:
890 return self.paramInterval
989 return self.paramInterval
891
990
991
892 def setValue(self, value):
992 def setValue(self, value):
893
993
894 print("This property should not be initialized")
994 print("This property should not be initialized")
895
995
896 return
996 return
897
997
898 def getNoise(self):
998 def getNoise(self):
899
999
900 return self.spc_noise
1000 return self.spc_noise
901
1001
902 noise = property(getNoise, setValue, "I'm the 'Noise' property.")
1002 noise = property(getNoise, setValue, "I'm the 'Noise' property.")
903
1003
904
1004
905 class PlotterData(object):
1005 class PlotterData(object):
906 '''
1006 '''
907 Object to hold data to be plotted
1007 Object to hold data to be plotted
908 '''
1008 '''
909
1009
910 MAXNUMX = 200
1010 MAXNUMX = 200
911 MAXNUMY = 200
1011 MAXNUMY = 200
912
1012
913 def __init__(self, code, exp_code, localtime=True):
1013 def __init__(self, code, exp_code, localtime=True):
914
1014
915 self.key = code
1015 self.key = code
916 self.exp_code = exp_code
1016 self.exp_code = exp_code
917 self.ready = False
1017 self.ready = False
918 self.flagNoData = False
1018 self.flagNoData = False
919 self.localtime = localtime
1019 self.localtime = localtime
920 self.data = {}
1020 self.data = {}
921 self.meta = {}
1021 self.meta = {}
922 self.__heights = []
1022 self.__heights = []
923
1023
924 def __str__(self):
1024 def __str__(self):
925 dum = ['{}{}'.format(key, self.shape(key)) for key in self.data]
1025 dum = ['{}{}'.format(key, self.shape(key)) for key in self.data]
926 return 'Data[{}][{}]'.format(';'.join(dum), len(self.times))
1026 return 'Data[{}][{}]'.format(';'.join(dum), len(self.times))
927
1027
928 def __len__(self):
1028 def __len__(self):
929 return len(self.data)
1029 return len(self.data)
930
1030
931 def __getitem__(self, key):
1031 def __getitem__(self, key):
932 if isinstance(key, int):
1032 if isinstance(key, int):
933 return self.data[self.times[key]]
1033 return self.data[self.times[key]]
934 elif isinstance(key, str):
1034 elif isinstance(key, str):
935 ret = numpy.array([self.data[x][key] for x in self.times])
1035 ret = numpy.array([self.data[x][key] for x in self.times])
936 if ret.ndim > 1:
1036 if ret.ndim > 1:
937 ret = numpy.swapaxes(ret, 0, 1)
1037 ret = numpy.swapaxes(ret, 0, 1)
938 return ret
1038 return ret
939
1039
940 def __contains__(self, key):
1040 def __contains__(self, key):
941 return key in self.data[self.min_time]
1041 return key in self.data[self.min_time]
942
1042
943 def setup(self):
1043 def setup(self):
944 '''
1044 '''
945 Configure object
1045 Configure object
946 '''
1046 '''
947 self.type = ''
1047 self.type = ''
948 self.ready = False
1048 self.ready = False
949 del self.data
1049 del self.data
950 self.data = {}
1050 self.data = {}
951 self.__heights = []
1051 self.__heights = []
952 self.__all_heights = set()
1052 self.__all_heights = set()
953
1053
954 def shape(self, key):
1054 def shape(self, key):
955 '''
1055 '''
956 Get the shape of the one-element data for the given key
1056 Get the shape of the one-element data for the given key
957 '''
1057 '''
958
1058
959 if len(self.data[self.min_time][key]):
1059 if len(self.data[self.min_time][key]):
960 return self.data[self.min_time][key].shape
1060 return self.data[self.min_time][key].shape
961 return (0,)
1061 return (0,)
962
1062
963 def update(self, data, tm, meta={}):
1063 def update(self, data, tm, meta={}):
964 '''
1064 '''
965 Update data object with new dataOut
1065 Update data object with new dataOut
966 '''
1066 '''
967
1067
968 self.data[tm] = data
1068 self.data[tm] = data
969
1069
970 for key, value in meta.items():
1070 for key, value in meta.items():
971 setattr(self, key, value)
1071 setattr(self, key, value)
972
1072
973 def normalize_heights(self):
1073 def normalize_heights(self):
974 '''
1074 '''
975 Ensure same-dimension of the data for different heighList
1075 Ensure same-dimension of the data for different heighList
976 '''
1076 '''
977
1077
978 H = numpy.array(list(self.__all_heights))
1078 H = numpy.array(list(self.__all_heights))
979 H.sort()
1079 H.sort()
980 for key in self.data:
1080 for key in self.data:
981 shape = self.shape(key)[:-1] + H.shape
1081 shape = self.shape(key)[:-1] + H.shape
982 for tm, obj in list(self.data[key].items()):
1082 for tm, obj in list(self.data[key].items()):
983 h = self.__heights[self.times.tolist().index(tm)]
1083 h = self.__heights[self.times.tolist().index(tm)]
984 if H.size == h.size:
1084 if H.size == h.size:
985 continue
1085 continue
986 index = numpy.where(numpy.in1d(H, h))[0]
1086 index = numpy.where(numpy.in1d(H, h))[0]
987 dummy = numpy.zeros(shape) + numpy.nan
1087 dummy = numpy.zeros(shape) + numpy.nan
988 if len(shape) == 2:
1088 if len(shape) == 2:
989 dummy[:, index] = obj
1089 dummy[:, index] = obj
990 else:
1090 else:
991 dummy[index] = obj
1091 dummy[index] = obj
992 self.data[key][tm] = dummy
1092 self.data[key][tm] = dummy
993
1093
994 self.__heights = [H for tm in self.times]
1094 self.__heights = [H for tm in self.times]
995
1095
996 def jsonify(self, tm, plot_name, plot_type, decimate=False):
1096 def jsonify(self, tm, plot_name, plot_type, decimate=False):
997 '''
1097 '''
998 Convert data to json
1098 Convert data to json
999 '''
1099 '''
1000
1100
1001 meta = {}
1101 meta = {}
1002 meta['xrange'] = []
1102 meta['xrange'] = []
1003 dy = int(len(self.yrange)/self.MAXNUMY) + 1
1103 dy = int(len(self.yrange)/self.MAXNUMY) + 1
1004 tmp = self.data[tm][self.key]
1104 tmp = self.data[tm][self.key]
1005 shape = tmp.shape
1105 shape = tmp.shape
1006 if len(shape) == 2:
1106 if len(shape) == 2:
1007 data = self.roundFloats(self.data[tm][self.key][::, ::dy].tolist())
1107 data = self.roundFloats(self.data[tm][self.key][::, ::dy].tolist())
1008 elif len(shape) == 3:
1108 elif len(shape) == 3:
1009 dx = int(self.data[tm][self.key].shape[1]/self.MAXNUMX) + 1
1109 dx = int(self.data[tm][self.key].shape[1]/self.MAXNUMX) + 1
1010 data = self.roundFloats(
1110 data = self.roundFloats(
1011 self.data[tm][self.key][::, ::dx, ::dy].tolist())
1111 self.data[tm][self.key][::, ::dx, ::dy].tolist())
1012 meta['xrange'] = self.roundFloats(self.xrange[2][::dx].tolist())
1112 meta['xrange'] = self.roundFloats(self.xrange[2][::dx].tolist())
1013 else:
1113 else:
1014 data = self.roundFloats(self.data[tm][self.key].tolist())
1114 data = self.roundFloats(self.data[tm][self.key].tolist())
1015
1115
1016 ret = {
1116 ret = {
1017 'plot': plot_name,
1117 'plot': plot_name,
1018 'code': self.exp_code,
1118 'code': self.exp_code,
1019 'time': float(tm),
1119 'time': float(tm),
1020 'data': data,
1120 'data': data,
1021 }
1121 }
1022 meta['type'] = plot_type
1122 meta['type'] = plot_type
1023 meta['interval'] = float(self.interval)
1123 meta['interval'] = float(self.interval)
1024 meta['localtime'] = self.localtime
1124 meta['localtime'] = self.localtime
1025 meta['yrange'] = self.roundFloats(self.yrange[::dy].tolist())
1125 meta['yrange'] = self.roundFloats(self.yrange[::dy].tolist())
1026 meta.update(self.meta)
1126 meta.update(self.meta)
1027 ret['metadata'] = meta
1127 ret['metadata'] = meta
1028 return json.dumps(ret)
1128 return json.dumps(ret)
1029
1129
1030 @property
1130 @property
1031 def times(self):
1131 def times(self):
1032 '''
1132 '''
1033 Return the list of times of the current data
1133 Return the list of times of the current data
1034 '''
1134 '''
1035
1135
1036 ret = [t for t in self.data]
1136 ret = [t for t in self.data]
1037 ret.sort()
1137 ret.sort()
1038 return numpy.array(ret)
1138 return numpy.array(ret)
1039
1139
1040 @property
1140 @property
1041 def min_time(self):
1141 def min_time(self):
1042 '''
1142 '''
1043 Return the minimun time value
1143 Return the minimun time value
1044 '''
1144 '''
1045
1145
1046 return self.times[0]
1146 return self.times[0]
1047
1147
1048 @property
1148 @property
1049 def max_time(self):
1149 def max_time(self):
1050 '''
1150 '''
1051 Return the maximun time value
1151 Return the maximun time value
1052 '''
1152 '''
1053
1153
1054 return self.times[-1]
1154 return self.times[-1]
1055
1155
1056 # @property
1156 # @property
1057 # def heights(self):
1157 # def heights(self):
1058 # '''
1158 # '''
1059 # Return the list of heights of the current data
1159 # Return the list of heights of the current data
1060 # '''
1160 # '''
1061
1161
1062 # return numpy.array(self.__heights[-1])
1162 # return numpy.array(self.__heights[-1])
1063
1163
1064 @staticmethod
1164 @staticmethod
1065 def roundFloats(obj):
1165 def roundFloats(obj):
1066 if isinstance(obj, list):
1166 if isinstance(obj, list):
1067 return list(map(PlotterData.roundFloats, obj))
1167 return list(map(PlotterData.roundFloats, obj))
1068 elif isinstance(obj, float):
1168 elif isinstance(obj, float):
1069 return round(obj, 2)
1169 return round(obj, 2)
@@ -1,906 +1,909
1 '''
1 '''
2
2
3 $Author: murco $
3 $Author: murco $
4 $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $
4 $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $
5 '''
5 '''
6 import sys
6 import sys
7 import numpy
7 import numpy
8 import copy
8 import copy
9 import datetime
9 import datetime
10 import inspect
10 import inspect
11 from schainpy.utils import log
11 from schainpy.utils import log
12
12
13 SPEED_OF_LIGHT = 299792458
13 SPEED_OF_LIGHT = 299792458
14 SPEED_OF_LIGHT = 3e8
14 SPEED_OF_LIGHT = 3e8
15
15
16 BASIC_STRUCTURE = numpy.dtype([
16 BASIC_STRUCTURE = numpy.dtype([
17 ('nSize', '<u4'),
17 ('nSize', '<u4'),
18 ('nVersion', '<u2'),
18 ('nVersion', '<u2'),
19 ('nDataBlockId', '<u4'),
19 ('nDataBlockId', '<u4'),
20 ('nUtime', '<u4'),
20 ('nUtime', '<u4'),
21 ('nMilsec', '<u2'),
21 ('nMilsec', '<u2'),
22 ('nTimezone', '<i2'),
22 ('nTimezone', '<i2'),
23 ('nDstflag', '<i2'),
23 ('nDstflag', '<i2'),
24 ('nErrorCount', '<u4')
24 ('nErrorCount', '<u4')
25 ])
25 ])
26
26
27 SYSTEM_STRUCTURE = numpy.dtype([
27 SYSTEM_STRUCTURE = numpy.dtype([
28 ('nSize', '<u4'),
28 ('nSize', '<u4'),
29 ('nNumSamples', '<u4'),
29 ('nNumSamples', '<u4'),
30 ('nNumProfiles', '<u4'),
30 ('nNumProfiles', '<u4'),
31 ('nNumChannels', '<u4'),
31 ('nNumChannels', '<u4'),
32 ('nADCResolution', '<u4'),
32 ('nADCResolution', '<u4'),
33 ('nPCDIOBusWidth', '<u4'),
33 ('nPCDIOBusWidth', '<u4'),
34 ])
34 ])
35
35
36 RADAR_STRUCTURE = numpy.dtype([
36 RADAR_STRUCTURE = numpy.dtype([
37 ('nSize', '<u4'),
37 ('nSize', '<u4'),
38 ('nExpType', '<u4'),
38 ('nExpType', '<u4'),
39 ('nNTx', '<u4'),
39 ('nNTx', '<u4'),
40 ('fIpp', '<f4'),
40 ('fIpp', '<f4'),
41 ('fTxA', '<f4'),
41 ('fTxA', '<f4'),
42 ('fTxB', '<f4'),
42 ('fTxB', '<f4'),
43 ('nNumWindows', '<u4'),
43 ('nNumWindows', '<u4'),
44 ('nNumTaus', '<u4'),
44 ('nNumTaus', '<u4'),
45 ('nCodeType', '<u4'),
45 ('nCodeType', '<u4'),
46 ('nLine6Function', '<u4'),
46 ('nLine6Function', '<u4'),
47 ('nLine5Function', '<u4'),
47 ('nLine5Function', '<u4'),
48 ('fClock', '<f4'),
48 ('fClock', '<f4'),
49 ('nPrePulseBefore', '<u4'),
49 ('nPrePulseBefore', '<u4'),
50 ('nPrePulseAfter', '<u4'),
50 ('nPrePulseAfter', '<u4'),
51 ('sRangeIPP', '<a20'),
51 ('sRangeIPP', '<a20'),
52 ('sRangeTxA', '<a20'),
52 ('sRangeTxA', '<a20'),
53 ('sRangeTxB', '<a20'),
53 ('sRangeTxB', '<a20'),
54 ])
54 ])
55
55
56 SAMPLING_STRUCTURE = numpy.dtype(
56 SAMPLING_STRUCTURE = numpy.dtype(
57 [('h0', '<f4'), ('dh', '<f4'), ('nsa', '<u4')])
57 [('h0', '<f4'), ('dh', '<f4'), ('nsa', '<u4')])
58
58
59
59
60 PROCESSING_STRUCTURE = numpy.dtype([
60 PROCESSING_STRUCTURE = numpy.dtype([
61 ('nSize', '<u4'),
61 ('nSize', '<u4'),
62 ('nDataType', '<u4'),
62 ('nDataType', '<u4'),
63 ('nSizeOfDataBlock', '<u4'),
63 ('nSizeOfDataBlock', '<u4'),
64 ('nProfilesperBlock', '<u4'),
64 ('nProfilesperBlock', '<u4'),
65 ('nDataBlocksperFile', '<u4'),
65 ('nDataBlocksperFile', '<u4'),
66 ('nNumWindows', '<u4'),
66 ('nNumWindows', '<u4'),
67 ('nProcessFlags', '<u4'),
67 ('nProcessFlags', '<u4'),
68 ('nCoherentIntegrations', '<u4'),
68 ('nCoherentIntegrations', '<u4'),
69 ('nIncoherentIntegrations', '<u4'),
69 ('nIncoherentIntegrations', '<u4'),
70 ('nTotalSpectra', '<u4')
70 ('nTotalSpectra', '<u4')
71 ])
71 ])
72
72
73
73
74 class Header(object):
74 class Header(object):
75
75
76 def __init__(self):
76 def __init__(self):
77 raise NotImplementedError
77 raise NotImplementedError
78
78
79 def copy(self):
79 def copy(self):
80 return copy.deepcopy(self)
80 return copy.deepcopy(self)
81
81
82 def read(self):
82 def read(self):
83
83
84 raise NotImplementedError
84 raise NotImplementedError
85
85
86 def write(self):
86 def write(self):
87
87
88 raise NotImplementedError
88 raise NotImplementedError
89
89
90 def getAllowedArgs(self):
90 def getAllowedArgs(self):
91 args = inspect.getargspec(self.__init__).args
91 args = inspect.getargspec(self.__init__).args
92 try:
92 try:
93 args.remove('self')
93 args.remove('self')
94 except:
94 except:
95 pass
95 pass
96 return args
96 return args
97
97
98 def getAsDict(self):
98 def getAsDict(self):
99 args = self.getAllowedArgs()
99 args = self.getAllowedArgs()
100 asDict = {}
100 asDict = {}
101 for x in args:
101 for x in args:
102 asDict[x] = self[x]
102 asDict[x] = self[x]
103 return asDict
103 return asDict
104
104
105 def __getitem__(self, name):
105 def __getitem__(self, name):
106 return getattr(self, name)
106 return getattr(self, name)
107
107
108 def printInfo(self):
108 def printInfo(self):
109
109
110 message = "#" * 50 + "\n"
110 message = "#" * 50 + "\n"
111 message += self.__class__.__name__.upper() + "\n"
111 message += self.__class__.__name__.upper() + "\n"
112 message += "#" * 50 + "\n"
112 message += "#" * 50 + "\n"
113
113
114 keyList = list(self.__dict__.keys())
114 keyList = list(self.__dict__.keys())
115 keyList.sort()
115 keyList.sort()
116
116
117 for key in keyList:
117 for key in keyList:
118 message += "%s = %s" % (key, self.__dict__[key]) + "\n"
118 message += "%s = %s" % (key, self.__dict__[key]) + "\n"
119
119
120 if "size" not in keyList:
120 if "size" not in keyList:
121 attr = getattr(self, "size")
121 attr = getattr(self, "size")
122
122
123 if attr:
123 if attr:
124 message += "%s = %s" % ("size", attr) + "\n"
124 message += "%s = %s" % ("size", attr) + "\n"
125
125
126 print(message)
126 print(message)
127
127
128
128
129 class BasicHeader(Header):
129 class BasicHeader(Header):
130
130
131 size = None
131 size = None
132 version = None
132 version = None
133 dataBlock = None
133 dataBlock = None
134 utc = None
134 utc = None
135 ltc = None
135 ltc = None
136 miliSecond = None
136 miliSecond = None
137 timeZone = None
137 timeZone = None
138 dstFlag = None
138 dstFlag = None
139 errorCount = None
139 errorCount = None
140 datatime = None
140 F = None
141 structure = BASIC_STRUCTURE
141 structure = BASIC_STRUCTURE
142 __LOCALTIME = None
142 __LOCALTIME = None
143
143
144 def __init__(self, useLocalTime=True):
144 def __init__(self, useLocalTime=True):
145
145
146 self.size = 24
146 self.size = 24
147 self.version = 0
147 self.version = 0
148 self.dataBlock = 0
148 self.dataBlock = 0
149 self.utc = 0
149 self.utc = 0
150 self.miliSecond = 0
150 self.miliSecond = 0
151 self.timeZone = 0
151 self.timeZone = 0
152 self.dstFlag = 0
152 self.dstFlag = 0
153 self.errorCount = 0
153 self.errorCount = 0
154
154
155 self.useLocalTime = useLocalTime
155 self.useLocalTime = useLocalTime
156
156
157 def read(self, fp):
157 def read(self, fp):
158
158
159 self.length = 0
159 self.length = 0
160 try:
160 try:
161 if hasattr(fp, 'read'):
161 if hasattr(fp, 'read'):
162 header = numpy.fromfile(fp, BASIC_STRUCTURE, 1)
162 header = numpy.fromfile(fp, BASIC_STRUCTURE, 1)
163 else:
163 else:
164 header = numpy.fromstring(fp, BASIC_STRUCTURE, 1)
164 header = numpy.fromstring(fp, BASIC_STRUCTURE, 1)
165 except Exception as e:
165 except Exception as e:
166 print("BasicHeader: ")
166 print("BasicHeader: ")
167 print(e)
167 print(e)
168 return 0
168 return 0
169
169
170 self.size = int(header['nSize'][0])
170 self.size = int(header['nSize'][0])
171 self.version = int(header['nVersion'][0])
171 self.version = int(header['nVersion'][0])
172 self.dataBlock = int(header['nDataBlockId'][0])
172 self.dataBlock = int(header['nDataBlockId'][0])
173 self.utc = int(header['nUtime'][0])
173 self.utc = int(header['nUtime'][0])
174 self.miliSecond = int(header['nMilsec'][0])
174 self.miliSecond = int(header['nMilsec'][0])
175 self.timeZone = int(header['nTimezone'][0])
175 self.timeZone = int(header['nTimezone'][0])
176 self.dstFlag = int(header['nDstflag'][0])
176 self.dstFlag = int(header['nDstflag'][0])
177 self.errorCount = int(header['nErrorCount'][0])
177 self.errorCount = int(header['nErrorCount'][0])
178
178
179 if self.size < 24:
179 if self.size < 24:
180 return 0
180 return 0
181
181
182 self.length = header.nbytes
182 self.length = header.nbytes
183 return 1
183 return 1
184
184
185 def write(self, fp):
185 def write(self, fp):
186
186
187 headerTuple = (self.size, self.version, self.dataBlock, self.utc,
187 headerTuple = (self.size, self.version, self.dataBlock, self.utc,
188 self.miliSecond, self.timeZone, self.dstFlag, self.errorCount)
188 self.miliSecond, self.timeZone, self.dstFlag, self.errorCount)
189 header = numpy.array(headerTuple, BASIC_STRUCTURE)
189 header = numpy.array(headerTuple, BASIC_STRUCTURE)
190 header.tofile(fp)
190 header.tofile(fp)
191
191
192 return 1
192 return 1
193
193
194 def get_ltc(self):
194 def get_ltc(self):
195
195
196 return self.utc - self.timeZone * 60
196 return self.utc - self.timeZone * 60
197
197
198 def set_ltc(self, value):
198 def set_ltc(self, value):
199
199
200 self.utc = value + self.timeZone * 60
200 self.utc = value + self.timeZone * 60
201
201
202 def get_datatime(self):
202 def get_datatime(self):
203
203
204 return datetime.datetime.utcfromtimestamp(self.ltc)
204 return datetime.datetime.utcfromtimestamp(self.ltc)
205
205
206 ltc = property(get_ltc, set_ltc)
206 ltc = property(get_ltc, set_ltc)
207 datatime = property(get_datatime)
207 datatime = property(get_datatime)
208
208
209
209
210 class SystemHeader(Header):
210 class SystemHeader(Header):
211
211
212 size = None
212 size = None
213 nSamples = None
213 nSamples = None
214 nProfiles = None
214 nProfiles = None
215 nChannels = None
215 nChannels = None
216 adcResolution = None
216 adcResolution = None
217 pciDioBusWidth = None
217 pciDioBusWidth = None
218 structure = SYSTEM_STRUCTURE
218 structure = SYSTEM_STRUCTURE
219
219
220 def __init__(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWidth=0):
220 def __init__(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWidth=0):
221
221
222 self.size = 24
222 self.size = 24
223 self.nSamples = nSamples
223 self.nSamples = nSamples
224 self.nProfiles = nProfiles
224 self.nProfiles = nProfiles
225 self.nChannels = nChannels
225 self.nChannels = nChannels
226 self.adcResolution = adcResolution
226 self.adcResolution = adcResolution
227 self.pciDioBusWidth = pciDioBusWidth
227 self.pciDioBusWidth = pciDioBusWidth
228
228
229 def read(self, fp):
229 def read(self, fp):
230 self.length = 0
230 self.length = 0
231 try:
231 try:
232 startFp = fp.tell()
232 startFp = fp.tell()
233 except Exception as e:
233 except Exception as e:
234 startFp = None
234 startFp = None
235 pass
235 pass
236
236
237 try:
237 try:
238 if hasattr(fp, 'read'):
238 if hasattr(fp, 'read'):
239 header = numpy.fromfile(fp, SYSTEM_STRUCTURE, 1)
239 header = numpy.fromfile(fp, SYSTEM_STRUCTURE, 1)
240 else:
240 else:
241 header = numpy.fromstring(fp, SYSTEM_STRUCTURE, 1)
241 header = numpy.fromstring(fp, SYSTEM_STRUCTURE, 1)
242 except Exception as e:
242 except Exception as e:
243 print("System Header: " + str(e))
243 print("System Header: " + str(e))
244 return 0
244 return 0
245
245
246 self.size = header['nSize'][0]
246 self.size = header['nSize'][0]
247 self.nSamples = header['nNumSamples'][0]
247 self.nSamples = header['nNumSamples'][0]
248 self.nProfiles = header['nNumProfiles'][0]
248 self.nProfiles = header['nNumProfiles'][0]
249 self.nChannels = header['nNumChannels'][0]
249 self.nChannels = header['nNumChannels'][0]
250 self.adcResolution = header['nADCResolution'][0]
250 self.adcResolution = header['nADCResolution'][0]
251 self.pciDioBusWidth = header['nPCDIOBusWidth'][0]
251 self.pciDioBusWidth = header['nPCDIOBusWidth'][0]
252
252
253 if startFp is not None:
253 if startFp is not None:
254 endFp = self.size + startFp
254 endFp = self.size + startFp
255
255
256 if fp.tell() > endFp:
256 if fp.tell() > endFp:
257 sys.stderr.write(
257 sys.stderr.write(
258 "Warning %s: Size value read from System Header is lower than it has to be\n" % fp.name)
258 "Warning %s: Size value read from System Header is lower than it has to be\n" % fp.name)
259 return 0
259 return 0
260
260
261 if fp.tell() < endFp:
261 if fp.tell() < endFp:
262 sys.stderr.write(
262 sys.stderr.write(
263 "Warning %s: Size value read from System Header size is greater than it has to be\n" % fp.name)
263 "Warning %s: Size value read from System Header size is greater than it has to be\n" % fp.name)
264 return 0
264 return 0
265
265
266 self.length = header.nbytes
266 self.length = header.nbytes
267 return 1
267 return 1
268
268
269 def write(self, fp):
269 def write(self, fp):
270
270
271 headerTuple = (self.size, self.nSamples, self.nProfiles,
271 headerTuple = (self.size, self.nSamples, self.nProfiles,
272 self.nChannels, self.adcResolution, self.pciDioBusWidth)
272 self.nChannels, self.adcResolution, self.pciDioBusWidth)
273 header = numpy.array(headerTuple, SYSTEM_STRUCTURE)
273 header = numpy.array(headerTuple, SYSTEM_STRUCTURE)
274 header.tofile(fp)
274 header.tofile(fp)
275
275
276 return 1
276 return 1
277
277
278
278
279 class RadarControllerHeader(Header):
279 class RadarControllerHeader(Header):
280
280
281 expType = None
281 expType = None
282 nTx = None
282 nTx = None
283 ipp = None
283 ipp = None
284 txA = None
284 txA = None
285 txB = None
285 txB = None
286 nWindows = None
286 nWindows = None
287 numTaus = None
287 numTaus = None
288 codeType = None
288 codeType = None
289 line6Function = None
289 line6Function = None
290 line5Function = None
290 line5Function = None
291 fClock = None
291 fClock = None
292 prePulseBefore = None
292 prePulseBefore = None
293 prePulseAfter = None
293 prePulseAfter = None
294 rangeIpp = None
294 rangeIpp = None
295 rangeTxA = None
295 rangeTxA = None
296 rangeTxB = None
296 rangeTxB = None
297 structure = RADAR_STRUCTURE
297 structure = RADAR_STRUCTURE
298 __size = None
298 __size = None
299
299
300 def __init__(self, expType=2, nTx=1,
300 def __init__(self, expType=2, nTx=1,
301 ipp=None, txA=0, txB=0,
301 ipp=None, txA=0, txB=0,
302 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
302 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
303 numTaus=0, line6Function=0, line5Function=0, fClock=None,
303 numTaus=0, line6Function=0, line5Function=0, fClock=None,
304 prePulseBefore=0, prePulseAfter=0,
304 prePulseBefore=0, prePulseAfter=0,
305 codeType=0, nCode=0, nBaud=0, code=[],
305 codeType=0, nCode=0, nBaud=0, code=[],
306 flip1=0, flip2=0):
306 flip1=0, flip2=0):
307
307
308 # self.size = 116
308 # self.size = 116
309 self.expType = expType
309 self.expType = expType
310 self.nTx = nTx
310 self.nTx = nTx
311 self.ipp = ipp
311 self.ipp = ipp
312 self.txA = txA
312 self.txA = txA
313 self.txB = txB
313 self.txB = txB
314 self.rangeIpp = ipp
314 self.rangeIpp = ipp
315 self.rangeTxA = txA
315 self.rangeTxA = txA
316 self.rangeTxB = txB
316 self.rangeTxB = txB
317
317
318 self.nWindows = nWindows
318 self.nWindows = nWindows
319 self.numTaus = numTaus
319 self.numTaus = numTaus
320 self.codeType = codeType
320 self.codeType = codeType
321 self.line6Function = line6Function
321 self.line6Function = line6Function
322 self.line5Function = line5Function
322 self.line5Function = line5Function
323 self.fClock = fClock
323 self.fClock = fClock
324 self.prePulseBefore = prePulseBefore
324 self.prePulseBefore = prePulseBefore
325 self.prePulseAfter = prePulseAfter
325 self.prePulseAfter = prePulseAfter
326
326
327 self.nHeights = nHeights
327 self.nHeights = nHeights
328 self.firstHeight = firstHeight
328 self.firstHeight = firstHeight
329 self.deltaHeight = deltaHeight
329 self.deltaHeight = deltaHeight
330 self.samplesWin = nHeights
330 self.samplesWin = nHeights
331
331
332 self.nCode = nCode
332 self.nCode = nCode
333 self.nBaud = nBaud
333 self.nBaud = nBaud
334 self.code = code
334 self.code = code
335 self.flip1 = flip1
335 self.flip1 = flip1
336 self.flip2 = flip2
336 self.flip2 = flip2
337
337
338 self.code_size = int(numpy.ceil(self.nBaud / 32.)) * self.nCode * 4
338 self.code_size = int(numpy.ceil(self.nBaud / 32.)) * self.nCode * 4
339 # self.dynamic = numpy.array([],numpy.dtype('byte'))
339 # self.dynamic = numpy.array([],numpy.dtype('byte'))
340
340
341 if self.fClock is None and self.deltaHeight is not None:
341 if self.fClock is None and self.deltaHeight is not None:
342 self.fClock = 0.15 / (deltaHeight * 1e-6) # 0.15Km / (height * 1u)
342 self.fClock = 0.15 / (deltaHeight * 1e-6) # 0.15Km / (height * 1u)
343
343
344 def read(self, fp):
344 def read(self, fp):
345 self.length = 0
345 self.length = 0
346 try:
346 try:
347 startFp = fp.tell()
347 startFp = fp.tell()
348 except Exception as e:
348 except Exception as e:
349 startFp = None
349 startFp = None
350 pass
350 pass
351
351
352 try:
352 try:
353 if hasattr(fp, 'read'):
353 if hasattr(fp, 'read'):
354 header = numpy.fromfile(fp, RADAR_STRUCTURE, 1)
354 header = numpy.fromfile(fp, RADAR_STRUCTURE, 1)
355 else:
355 else:
356 header = numpy.fromstring(fp, RADAR_STRUCTURE, 1)
356 header = numpy.fromstring(fp, RADAR_STRUCTURE, 1)
357 self.length += header.nbytes
357 self.length += header.nbytes
358 except Exception as e:
358 except Exception as e:
359 print("RadarControllerHeader: " + str(e))
359 print("RadarControllerHeader: " + str(e))
360 return 0
360 return 0
361
361
362 size = int(header['nSize'][0])
362 size = int(header['nSize'][0])
363 self.expType = int(header['nExpType'][0])
363 self.expType = int(header['nExpType'][0])
364 self.nTx = int(header['nNTx'][0])
364 self.nTx = int(header['nNTx'][0])
365 self.ipp = float(header['fIpp'][0])
365 self.ipp = float(header['fIpp'][0])
366 #print(self.ipp)
366 self.txA = float(header['fTxA'][0])
367 self.txA = float(header['fTxA'][0])
367 self.txB = float(header['fTxB'][0])
368 self.txB = float(header['fTxB'][0])
368 self.nWindows = int(header['nNumWindows'][0])
369 self.nWindows = int(header['nNumWindows'][0])
369 self.numTaus = int(header['nNumTaus'][0])
370 self.numTaus = int(header['nNumTaus'][0])
370 self.codeType = int(header['nCodeType'][0])
371 self.codeType = int(header['nCodeType'][0])
371 self.line6Function = int(header['nLine6Function'][0])
372 self.line6Function = int(header['nLine6Function'][0])
372 self.line5Function = int(header['nLine5Function'][0])
373 self.line5Function = int(header['nLine5Function'][0])
373 self.fClock = float(header['fClock'][0])
374 self.fClock = float(header['fClock'][0])
374 self.prePulseBefore = int(header['nPrePulseBefore'][0])
375 self.prePulseBefore = int(header['nPrePulseBefore'][0])
375 self.prePulseAfter = int(header['nPrePulseAfter'][0])
376 self.prePulseAfter = int(header['nPrePulseAfter'][0])
376 self.rangeIpp = header['sRangeIPP'][0]
377 self.rangeIpp = header['sRangeIPP'][0]
377 self.rangeTxA = header['sRangeTxA'][0]
378 self.rangeTxA = header['sRangeTxA'][0]
378 self.rangeTxB = header['sRangeTxB'][0]
379 self.rangeTxB = header['sRangeTxB'][0]
379
380
380 try:
381 try:
381 if hasattr(fp, 'read'):
382 if hasattr(fp, 'read'):
382 samplingWindow = numpy.fromfile(
383 samplingWindow = numpy.fromfile(
383 fp, SAMPLING_STRUCTURE, self.nWindows)
384 fp, SAMPLING_STRUCTURE, self.nWindows)
384 else:
385 else:
385 samplingWindow = numpy.fromstring(
386 samplingWindow = numpy.fromstring(
386 fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
387 fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
387 self.length += samplingWindow.nbytes
388 self.length += samplingWindow.nbytes
388 except Exception as e:
389 except Exception as e:
389 print("RadarControllerHeader: " + str(e))
390 print("RadarControllerHeader: " + str(e))
390 return 0
391 return 0
391 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
392 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
392 self.firstHeight = samplingWindow['h0']
393 self.firstHeight = samplingWindow['h0']
393 self.deltaHeight = samplingWindow['dh']
394 self.deltaHeight = samplingWindow['dh']
394 self.samplesWin = samplingWindow['nsa']
395 self.samplesWin = samplingWindow['nsa']
395
396
396 try:
397 try:
397 if hasattr(fp, 'read'):
398 if hasattr(fp, 'read'):
398 self.Taus = numpy.fromfile(fp, '<f4', self.numTaus)
399 self.Taus = numpy.fromfile(fp, '<f4', self.numTaus)
399 else:
400 else:
400 self.Taus = numpy.fromstring(
401 self.Taus = numpy.fromstring(
401 fp[self.length:], '<f4', self.numTaus)
402 fp[self.length:], '<f4', self.numTaus)
402 self.length += self.Taus.nbytes
403 self.length += self.Taus.nbytes
403 except Exception as e:
404 except Exception as e:
404 print("RadarControllerHeader: " + str(e))
405 print("RadarControllerHeader: " + str(e))
405 return 0
406 return 0
406
407
407 self.code_size = 0
408 self.code_size = 0
408 if self.codeType != 0:
409 if self.codeType != 0:
409
410
410 try:
411 try:
411 if hasattr(fp, 'read'):
412 if hasattr(fp, 'read'):
412 self.nCode = numpy.fromfile(fp, '<u4', 1)[0]
413 self.nCode = numpy.fromfile(fp, '<u4', 1)[0]
413 self.length += self.nCode.nbytes
414 self.length += self.nCode.nbytes
414 self.nBaud = numpy.fromfile(fp, '<u4', 1)[0]
415 self.nBaud = numpy.fromfile(fp, '<u4', 1)[0]
415 self.length += self.nBaud.nbytes
416 self.length += self.nBaud.nbytes
416 else:
417 else:
417 self.nCode = numpy.fromstring(
418 self.nCode = numpy.fromstring(
418 fp[self.length:], '<u4', 1)[0]
419 fp[self.length:], '<u4', 1)[0]
419 self.length += self.nCode.nbytes
420 self.length += self.nCode.nbytes
420 self.nBaud = numpy.fromstring(
421 self.nBaud = numpy.fromstring(
421 fp[self.length:], '<u4', 1)[0]
422 fp[self.length:], '<u4', 1)[0]
422 self.length += self.nBaud.nbytes
423 self.length += self.nBaud.nbytes
423 except Exception as e:
424 except Exception as e:
424 print("RadarControllerHeader: " + str(e))
425 print("RadarControllerHeader: " + str(e))
425 return 0
426 return 0
426 code = numpy.empty([self.nCode, self.nBaud], dtype='i1')
427 code = numpy.empty([self.nCode, self.nBaud], dtype='i1')
427
428
428 for ic in range(self.nCode):
429 for ic in range(self.nCode):
429 try:
430 try:
430 if hasattr(fp, 'read'):
431 if hasattr(fp, 'read'):
431 temp = numpy.fromfile(fp, 'u4', int(
432 temp = numpy.fromfile(fp, 'u4', int(
432 numpy.ceil(self.nBaud / 32.)))
433 numpy.ceil(self.nBaud / 32.)))
433 else:
434 else:
434 temp = numpy.fromstring(
435 temp = numpy.fromstring(
435 fp, 'u4', int(numpy.ceil(self.nBaud / 32.)))
436 fp, 'u4', int(numpy.ceil(self.nBaud / 32.)))
436 self.length += temp.nbytes
437 self.length += temp.nbytes
437 except Exception as e:
438 except Exception as e:
438 print("RadarControllerHeader: " + str(e))
439 print("RadarControllerHeader: " + str(e))
439 return 0
440 return 0
440
441
441 for ib in range(self.nBaud - 1, -1, -1):
442 for ib in range(self.nBaud - 1, -1, -1):
442 code[ic, ib] = temp[int(ib / 32)] % 2
443 code[ic, ib] = temp[int(ib / 32)] % 2
443 temp[int(ib / 32)] = temp[int(ib / 32)] / 2
444 temp[int(ib / 32)] = temp[int(ib / 32)] / 2
444
445
445 self.code = 2.0 * code - 1.0
446 self.code = 2.0 * code - 1.0
446 self.code_size = int(numpy.ceil(self.nBaud / 32.)) * self.nCode * 4
447 self.code_size = int(numpy.ceil(self.nBaud / 32.)) * self.nCode * 4
447
448
448 # if self.line5Function == RCfunction.FLIP:
449 # if self.line5Function == RCfunction.FLIP:
449 # self.flip1 = numpy.fromfile(fp,'<u4',1)
450 # self.flip1 = numpy.fromfile(fp,'<u4',1)
450 #
451 #
451 # if self.line6Function == RCfunction.FLIP:
452 # if self.line6Function == RCfunction.FLIP:
452 # self.flip2 = numpy.fromfile(fp,'<u4',1)
453 # self.flip2 = numpy.fromfile(fp,'<u4',1)
453 if startFp is not None:
454 if startFp is not None:
454 endFp = size + startFp
455 endFp = size + startFp
455
456
456 if fp.tell() != endFp:
457 if fp.tell() != endFp:
457 # fp.seek(endFp)
458 # fp.seek(endFp)
458 print("%s: Radar Controller Header size is not consistent: from data [%d] != from header field [%d]" % (fp.name, fp.tell() - startFp, size))
459 print("%s: Radar Controller Header size is not consistent: from data [%d] != from header field [%d]" % (fp.name, fp.tell() - startFp, size))
459 # return 0
460 # return 0
460
461
461 if fp.tell() > endFp:
462 if fp.tell() > endFp:
462 sys.stderr.write(
463 sys.stderr.write(
463 "Warning %s: Size value read from Radar Controller header is lower than it has to be\n" % fp.name)
464 "Warning %s: Size value read from Radar Controller header is lower than it has to be\n" % fp.name)
464 # return 0
465 # return 0
465
466
466 if fp.tell() < endFp:
467 if fp.tell() < endFp:
467 sys.stderr.write(
468 sys.stderr.write(
468 "Warning %s: Size value read from Radar Controller header is greater than it has to be\n" % fp.name)
469 "Warning %s: Size value read from Radar Controller header is greater than it has to be\n" % fp.name)
469
470
470 return 1
471 return 1
471
472
472 def write(self, fp):
473 def write(self, fp):
473
474
474 headerTuple = (self.size,
475 headerTuple = (self.size,
475 self.expType,
476 self.expType,
476 self.nTx,
477 self.nTx,
477 self.ipp,
478 self.ipp,
478 self.txA,
479 self.txA,
479 self.txB,
480 self.txB,
480 self.nWindows,
481 self.nWindows,
481 self.numTaus,
482 self.numTaus,
482 self.codeType,
483 self.codeType,
483 self.line6Function,
484 self.line6Function,
484 self.line5Function,
485 self.line5Function,
485 self.fClock,
486 self.fClock,
486 self.prePulseBefore,
487 self.prePulseBefore,
487 self.prePulseAfter,
488 self.prePulseAfter,
488 self.rangeIpp,
489 self.rangeIpp,
489 self.rangeTxA,
490 self.rangeTxA,
490 self.rangeTxB)
491 self.rangeTxB)
491
492
492 header = numpy.array(headerTuple, RADAR_STRUCTURE)
493 header = numpy.array(headerTuple, RADAR_STRUCTURE)
493 header.tofile(fp)
494 header.tofile(fp)
494
495
495 sampleWindowTuple = (
496 sampleWindowTuple = (
496 self.firstHeight, self.deltaHeight, self.samplesWin)
497 self.firstHeight, self.deltaHeight, self.samplesWin)
497 samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE)
498 samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE)
498 samplingWindow.tofile(fp)
499 samplingWindow.tofile(fp)
499
500
500 if self.numTaus > 0:
501 if self.numTaus > 0:
501 self.Taus.tofile(fp)
502 self.Taus.tofile(fp)
502
503
503 if self.codeType != 0:
504 if self.codeType != 0:
504 nCode = numpy.array(self.nCode, '<u4')
505 nCode = numpy.array(self.nCode, '<u4')
505 nCode.tofile(fp)
506 nCode.tofile(fp)
506 nBaud = numpy.array(self.nBaud, '<u4')
507 nBaud = numpy.array(self.nBaud, '<u4')
507 nBaud.tofile(fp)
508 nBaud.tofile(fp)
508 code1 = (self.code + 1.0) / 2.
509 code1 = (self.code + 1.0) / 2.
509
510
510 for ic in range(self.nCode):
511 for ic in range(self.nCode):
511 tempx = numpy.zeros(int(numpy.ceil(self.nBaud / 32.)))
512 tempx = numpy.zeros(int(numpy.ceil(self.nBaud / 32.)))
512 start = 0
513 start = 0
513 end = 32
514 end = 32
514 for i in range(len(tempx)):
515 for i in range(len(tempx)):
515 code_selected = code1[ic, start:end]
516 code_selected = code1[ic, start:end]
516 for j in range(len(code_selected) - 1, -1, -1):
517 for j in range(len(code_selected) - 1, -1, -1):
517 if code_selected[j] == 1:
518 if code_selected[j] == 1:
518 tempx[i] = tempx[i] + \
519 tempx[i] = tempx[i] + \
519 2**(len(code_selected) - 1 - j)
520 2**(len(code_selected) - 1 - j)
520 start = start + 32
521 start = start + 32
521 end = end + 32
522 end = end + 32
522
523
523 tempx = tempx.astype('u4')
524 tempx = tempx.astype('u4')
524 tempx.tofile(fp)
525 tempx.tofile(fp)
525
526
526 # if self.line5Function == RCfunction.FLIP:
527 # if self.line5Function == RCfunction.FLIP:
527 # self.flip1.tofile(fp)
528 # self.flip1.tofile(fp)
528 #
529 #
529 # if self.line6Function == RCfunction.FLIP:
530 # if self.line6Function == RCfunction.FLIP:
530 # self.flip2.tofile(fp)
531 # self.flip2.tofile(fp)
531
532
532 return 1
533 return 1
533
534
534 def get_ippSeconds(self):
535 def get_ippSeconds(self):
535 '''
536 '''
536 '''
537 '''
538
537 ippSeconds = 2.0 * 1000 * self.ipp / SPEED_OF_LIGHT
539 ippSeconds = 2.0 * 1000 * self.ipp / SPEED_OF_LIGHT
538
540
539 return ippSeconds
541 return ippSeconds
540
542
541 def set_ippSeconds(self, ippSeconds):
543 def set_ippSeconds(self, ippSeconds):
542 '''
544 '''
543 '''
545 '''
544
546
545 self.ipp = ippSeconds * SPEED_OF_LIGHT / (2.0 * 1000)
547 self.ipp = ippSeconds * SPEED_OF_LIGHT / (2.0 * 1000)
546
548
547 return
549 return
548
550
549 def get_size(self):
551 def get_size(self):
550
552
551 self.__size = 116 + 12 * self.nWindows + 4 * self.numTaus
553 self.__size = 116 + 12 * self.nWindows + 4 * self.numTaus
552
554
553 if self.codeType != 0:
555 if self.codeType != 0:
554 self.__size += 4 + 4 + 4 * self.nCode * \
556 self.__size += 4 + 4 + 4 * self.nCode * \
555 numpy.ceil(self.nBaud / 32.)
557 numpy.ceil(self.nBaud / 32.)
556
558
557 return self.__size
559 return self.__size
558
560
559 def set_size(self, value):
561 def set_size(self, value):
560
562
561 raise IOError("size is a property and it cannot be set, just read")
563 raise IOError("size is a property and it cannot be set, just read")
562
564
563 return
565 return
564
566
565 ippSeconds = property(get_ippSeconds, set_ippSeconds)
567 ippSeconds = property(get_ippSeconds, set_ippSeconds)
566 size = property(get_size, set_size)
568 size = property(get_size, set_size)
567
569
568
570
569 class ProcessingHeader(Header):
571 class ProcessingHeader(Header):
570
572
571 # size = None
573 # size = None
572 dtype = None
574 dtype = None
573 blockSize = None
575 blockSize = None
574 profilesPerBlock = None
576 profilesPerBlock = None
575 dataBlocksPerFile = None
577 dataBlocksPerFile = None
576 nWindows = None
578 nWindows = None
577 processFlags = None
579 processFlags = None
578 nCohInt = None
580 nCohInt = None
579 nIncohInt = None
581 nIncohInt = None
580 totalSpectra = None
582 totalSpectra = None
581 structure = PROCESSING_STRUCTURE
583 structure = PROCESSING_STRUCTURE
582 flag_dc = None
584 flag_dc = None
583 flag_cspc = None
585 flag_cspc = None
584
586
585 def __init__(self, dtype=0, blockSize=0, profilesPerBlock=0, dataBlocksPerFile=0, nWindows=0, processFlags=0, nCohInt=0,
587 def __init__(self, dtype=0, blockSize=0, profilesPerBlock=0, dataBlocksPerFile=0, nWindows=0, processFlags=0, nCohInt=0,
586 nIncohInt=0, totalSpectra=0, nHeights=0, firstHeight=0, deltaHeight=0, samplesWin=0, spectraComb=0, nCode=0,
588 nIncohInt=0, totalSpectra=0, nHeights=0, firstHeight=0, deltaHeight=0, samplesWin=0, spectraComb=0, nCode=0,
587 code=0, nBaud=None, shif_fft=False, flag_dc=False, flag_cspc=False, flag_decode=False, flag_deflip=False
589 code=0, nBaud=None, shif_fft=False, flag_dc=False, flag_cspc=False, flag_decode=False, flag_deflip=False
588 ):
590 ):
589
591
590 # self.size = 0
592 # self.size = 0
591 self.dtype = dtype
593 self.dtype = dtype
592 self.blockSize = blockSize
594 self.blockSize = blockSize
593 self.profilesPerBlock = 0
595 self.profilesPerBlock = 0
594 self.dataBlocksPerFile = 0
596 self.dataBlocksPerFile = 0
595 self.nWindows = 0
597 self.nWindows = 0
596 self.processFlags = 0
598 self.processFlags = 0
597 self.nCohInt = 0
599 self.nCohInt = 0
598 self.nIncohInt = 0
600 self.nIncohInt = 0
599 self.totalSpectra = 0
601 self.totalSpectra = 0
600
602
601 self.nHeights = 0
603 self.nHeights = 0
602 self.firstHeight = 0
604 self.firstHeight = 0
603 self.deltaHeight = 0
605 self.deltaHeight = 0
604 self.samplesWin = 0
606 self.samplesWin = 0
605 self.spectraComb = 0
607 self.spectraComb = 0
606 self.nCode = None
608 self.nCode = None
607 self.code = None
609 self.code = None
608 self.nBaud = None
610 self.nBaud = None
609
611
610 self.shif_fft = False
612 self.shif_fft = False
611 self.flag_dc = False
613 self.flag_dc = False
612 self.flag_cspc = False
614 self.flag_cspc = False
613 self.flag_decode = False
615 self.flag_decode = False
614 self.flag_deflip = False
616 self.flag_deflip = False
615 self.length = 0
617 self.length = 0
616
618
617 def read(self, fp):
619 def read(self, fp):
618 self.length = 0
620 self.length = 0
619 try:
621 try:
620 startFp = fp.tell()
622 startFp = fp.tell()
621 except Exception as e:
623 except Exception as e:
622 startFp = None
624 startFp = None
623 pass
625 pass
624
626
625 try:
627 try:
626 if hasattr(fp, 'read'):
628 if hasattr(fp, 'read'):
627 header = numpy.fromfile(fp, PROCESSING_STRUCTURE, 1)
629 header = numpy.fromfile(fp, PROCESSING_STRUCTURE, 1)
628 else:
630 else:
629 header = numpy.fromstring(fp, PROCESSING_STRUCTURE, 1)
631 header = numpy.fromstring(fp, PROCESSING_STRUCTURE, 1)
630 self.length += header.nbytes
632 self.length += header.nbytes
631 except Exception as e:
633 except Exception as e:
632 print("ProcessingHeader: " + str(e))
634 print("ProcessingHeader: " + str(e))
633 return 0
635 return 0
634
636
635 size = int(header['nSize'][0])
637 size = int(header['nSize'][0])
636 self.dtype = int(header['nDataType'][0])
638 self.dtype = int(header['nDataType'][0])
637 self.blockSize = int(header['nSizeOfDataBlock'][0])
639 self.blockSize = int(header['nSizeOfDataBlock'][0])
638 self.profilesPerBlock = int(header['nProfilesperBlock'][0])
640 self.profilesPerBlock = int(header['nProfilesperBlock'][0])
639 self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0])
641 self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0])
640 self.nWindows = int(header['nNumWindows'][0])
642 self.nWindows = int(header['nNumWindows'][0])
641 self.processFlags = header['nProcessFlags']
643 self.processFlags = header['nProcessFlags']
642 self.nCohInt = int(header['nCoherentIntegrations'][0])
644 self.nCohInt = int(header['nCoherentIntegrations'][0])
645
643 self.nIncohInt = int(header['nIncoherentIntegrations'][0])
646 self.nIncohInt = int(header['nIncoherentIntegrations'][0])
644 self.totalSpectra = int(header['nTotalSpectra'][0])
647 self.totalSpectra = int(header['nTotalSpectra'][0])
645
648
646 try:
649 try:
647 if hasattr(fp, 'read'):
650 if hasattr(fp, 'read'):
648 samplingWindow = numpy.fromfile(
651 samplingWindow = numpy.fromfile(
649 fp, SAMPLING_STRUCTURE, self.nWindows)
652 fp, SAMPLING_STRUCTURE, self.nWindows)
650 else:
653 else:
651 samplingWindow = numpy.fromstring(
654 samplingWindow = numpy.fromstring(
652 fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
655 fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
653 self.length += samplingWindow.nbytes
656 self.length += samplingWindow.nbytes
654 except Exception as e:
657 except Exception as e:
655 print("ProcessingHeader: " + str(e))
658 print("ProcessingHeader: " + str(e))
656 return 0
659 return 0
657
660
658 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
661 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
659 self.firstHeight = float(samplingWindow['h0'][0])
662 self.firstHeight = float(samplingWindow['h0'][0])
660 self.deltaHeight = float(samplingWindow['dh'][0])
663 self.deltaHeight = float(samplingWindow['dh'][0])
661 self.samplesWin = samplingWindow['nsa'][0]
664 self.samplesWin = samplingWindow['nsa'][0]
662
665
663 try:
666 try:
664 if hasattr(fp, 'read'):
667 if hasattr(fp, 'read'):
665 self.spectraComb = numpy.fromfile(
668 self.spectraComb = numpy.fromfile(
666 fp, 'u1', 2 * self.totalSpectra)
669 fp, 'u1', 2 * self.totalSpectra)
667 else:
670 else:
668 self.spectraComb = numpy.fromstring(
671 self.spectraComb = numpy.fromstring(
669 fp[self.length:], 'u1', 2 * self.totalSpectra)
672 fp[self.length:], 'u1', 2 * self.totalSpectra)
670 self.length += self.spectraComb.nbytes
673 self.length += self.spectraComb.nbytes
671 except Exception as e:
674 except Exception as e:
672 print("ProcessingHeader: " + str(e))
675 print("ProcessingHeader: " + str(e))
673 return 0
676 return 0
674
677
675 if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE):
678 if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE):
676 self.nCode = int(numpy.fromfile(fp, '<u4', 1))
679 self.nCode = int(numpy.fromfile(fp, '<u4', 1))
677 self.nBaud = int(numpy.fromfile(fp, '<u4', 1))
680 self.nBaud = int(numpy.fromfile(fp, '<u4', 1))
678 self.code = numpy.fromfile(
681 self.code = numpy.fromfile(
679 fp, '<f4', self.nCode * self.nBaud).reshape(self.nCode, self.nBaud)
682 fp, '<f4', self.nCode * self.nBaud).reshape(self.nCode, self.nBaud)
680
683
681 if ((self.processFlags & PROCFLAG.EXP_NAME_ESP) == PROCFLAG.EXP_NAME_ESP):
684 if ((self.processFlags & PROCFLAG.EXP_NAME_ESP) == PROCFLAG.EXP_NAME_ESP):
682 exp_name_len = int(numpy.fromfile(fp, '<u4', 1))
685 exp_name_len = int(numpy.fromfile(fp, '<u4', 1))
683 exp_name = numpy.fromfile(fp, 'u1', exp_name_len + 1)
686 exp_name = numpy.fromfile(fp, 'u1', exp_name_len + 1)
684
687
685 if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA):
688 if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA):
686 self.shif_fft = True
689 self.shif_fft = True
687 else:
690 else:
688 self.shif_fft = False
691 self.shif_fft = False
689
692
690 if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC):
693 if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC):
691 self.flag_dc = True
694 self.flag_dc = True
692 else:
695 else:
693 self.flag_dc = False
696 self.flag_dc = False
694
697
695 if ((self.processFlags & PROCFLAG.DECODE_DATA) == PROCFLAG.DECODE_DATA):
698 if ((self.processFlags & PROCFLAG.DECODE_DATA) == PROCFLAG.DECODE_DATA):
696 self.flag_decode = True
699 self.flag_decode = True
697 else:
700 else:
698 self.flag_decode = False
701 self.flag_decode = False
699
702
700 if ((self.processFlags & PROCFLAG.DEFLIP_DATA) == PROCFLAG.DEFLIP_DATA):
703 if ((self.processFlags & PROCFLAG.DEFLIP_DATA) == PROCFLAG.DEFLIP_DATA):
701 self.flag_deflip = True
704 self.flag_deflip = True
702 else:
705 else:
703 self.flag_deflip = False
706 self.flag_deflip = False
704
707
705 nChannels = 0
708 nChannels = 0
706 nPairs = 0
709 nPairs = 0
707 pairList = []
710 pairList = []
708
711
709 for i in range(0, self.totalSpectra * 2, 2):
712 for i in range(0, self.totalSpectra * 2, 2):
710 if self.spectraComb[i] == self.spectraComb[i + 1]:
713 if self.spectraComb[i] == self.spectraComb[i + 1]:
711 nChannels = nChannels + 1 # par de canales iguales
714 nChannels = nChannels + 1 # par de canales iguales
712 else:
715 else:
713 nPairs = nPairs + 1 # par de canales diferentes
716 nPairs = nPairs + 1 # par de canales diferentes
714 pairList.append((self.spectraComb[i], self.spectraComb[i + 1]))
717 pairList.append((self.spectraComb[i], self.spectraComb[i + 1]))
715
718
716 self.flag_cspc = False
719 self.flag_cspc = False
717 if nPairs > 0:
720 if nPairs > 0:
718 self.flag_cspc = True
721 self.flag_cspc = True
719
722
720 if startFp is not None:
723 if startFp is not None:
721 endFp = size + startFp
724 endFp = size + startFp
722 if fp.tell() > endFp:
725 if fp.tell() > endFp:
723 sys.stderr.write(
726 sys.stderr.write(
724 "Warning: Processing header size is lower than it has to be")
727 "Warning: Processing header size is lower than it has to be")
725 return 0
728 return 0
726
729
727 if fp.tell() < endFp:
730 if fp.tell() < endFp:
728 sys.stderr.write(
731 sys.stderr.write(
729 "Warning: Processing header size is greater than it is considered")
732 "Warning: Processing header size is greater than it is considered")
730
733
731 return 1
734 return 1
732
735
733 def write(self, fp):
736 def write(self, fp):
734 # Clear DEFINE_PROCESS_CODE
737 # Clear DEFINE_PROCESS_CODE
735 self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE)
738 self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE)
736
739
737 headerTuple = (self.size,
740 headerTuple = (self.size,
738 self.dtype,
741 self.dtype,
739 self.blockSize,
742 self.blockSize,
740 self.profilesPerBlock,
743 self.profilesPerBlock,
741 self.dataBlocksPerFile,
744 self.dataBlocksPerFile,
742 self.nWindows,
745 self.nWindows,
743 self.processFlags,
746 self.processFlags,
744 self.nCohInt,
747 self.nCohInt,
745 self.nIncohInt,
748 self.nIncohInt,
746 self.totalSpectra)
749 self.totalSpectra)
747
750
748 header = numpy.array(headerTuple, PROCESSING_STRUCTURE)
751 header = numpy.array(headerTuple, PROCESSING_STRUCTURE)
749 header.tofile(fp)
752 header.tofile(fp)
750
753
751 if self.nWindows != 0:
754 if self.nWindows != 0:
752 sampleWindowTuple = (
755 sampleWindowTuple = (
753 self.firstHeight, self.deltaHeight, self.samplesWin)
756 self.firstHeight, self.deltaHeight, self.samplesWin)
754 samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE)
757 samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE)
755 samplingWindow.tofile(fp)
758 samplingWindow.tofile(fp)
756
759
757 if self.totalSpectra != 0:
760 if self.totalSpectra != 0:
758 # spectraComb = numpy.array([],numpy.dtype('u1'))
761 # spectraComb = numpy.array([],numpy.dtype('u1'))
759 spectraComb = self.spectraComb
762 spectraComb = self.spectraComb
760 spectraComb.tofile(fp)
763 spectraComb.tofile(fp)
761
764
762 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
765 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
763 # nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba
766 # nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba
764 # nCode.tofile(fp)
767 # nCode.tofile(fp)
765 #
768 #
766 # nBaud = numpy.array([self.nBaud], numpy.dtype('u4'))
769 # nBaud = numpy.array([self.nBaud], numpy.dtype('u4'))
767 # nBaud.tofile(fp)
770 # nBaud.tofile(fp)
768 #
771 #
769 # code = self.code.reshape(self.nCode*self.nBaud)
772 # code = self.code.reshape(self.nCode*self.nBaud)
770 # code = code.astype(numpy.dtype('<f4'))
773 # code = code.astype(numpy.dtype('<f4'))
771 # code.tofile(fp)
774 # code.tofile(fp)
772
775
773 return 1
776 return 1
774
777
775 def get_size(self):
778 def get_size(self):
776
779
777 self.__size = 40 + 12 * self.nWindows + 2 * self.totalSpectra
780 self.__size = 40 + 12 * self.nWindows + 2 * self.totalSpectra
778
781
779 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
782 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
780 # self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
783 # self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
781 # self.__size += 4 + 4 + 4 * self.nCode * self.nBaud
784 # self.__size += 4 + 4 + 4 * self.nCode * self.nBaud
782
785
783 return self.__size
786 return self.__size
784
787
785 def set_size(self, value):
788 def set_size(self, value):
786
789
787 raise IOError("size is a property and it cannot be set, just read")
790 raise IOError("size is a property and it cannot be set, just read")
788
791
789 return
792 return
790
793
791 size = property(get_size, set_size)
794 size = property(get_size, set_size)
792
795
793
796
794 class RCfunction:
797 class RCfunction:
795 NONE = 0
798 NONE = 0
796 FLIP = 1
799 FLIP = 1
797 CODE = 2
800 CODE = 2
798 SAMPLING = 3
801 SAMPLING = 3
799 LIN6DIV256 = 4
802 LIN6DIV256 = 4
800 SYNCHRO = 5
803 SYNCHRO = 5
801
804
802
805
803 class nCodeType:
806 class nCodeType:
804 NONE = 0
807 NONE = 0
805 USERDEFINE = 1
808 USERDEFINE = 1
806 BARKER2 = 2
809 BARKER2 = 2
807 BARKER3 = 3
810 BARKER3 = 3
808 BARKER4 = 4
811 BARKER4 = 4
809 BARKER5 = 5
812 BARKER5 = 5
810 BARKER7 = 6
813 BARKER7 = 6
811 BARKER11 = 7
814 BARKER11 = 7
812 BARKER13 = 8
815 BARKER13 = 8
813 AC128 = 9
816 AC128 = 9
814 COMPLEMENTARYCODE2 = 10
817 COMPLEMENTARYCODE2 = 10
815 COMPLEMENTARYCODE4 = 11
818 COMPLEMENTARYCODE4 = 11
816 COMPLEMENTARYCODE8 = 12
819 COMPLEMENTARYCODE8 = 12
817 COMPLEMENTARYCODE16 = 13
820 COMPLEMENTARYCODE16 = 13
818 COMPLEMENTARYCODE32 = 14
821 COMPLEMENTARYCODE32 = 14
819 COMPLEMENTARYCODE64 = 15
822 COMPLEMENTARYCODE64 = 15
820 COMPLEMENTARYCODE128 = 16
823 COMPLEMENTARYCODE128 = 16
821 CODE_BINARY28 = 17
824 CODE_BINARY28 = 17
822
825
823
826
824 class PROCFLAG:
827 class PROCFLAG:
825
828
826 COHERENT_INTEGRATION = numpy.uint32(0x00000001)
829 COHERENT_INTEGRATION = numpy.uint32(0x00000001)
827 DECODE_DATA = numpy.uint32(0x00000002)
830 DECODE_DATA = numpy.uint32(0x00000002)
828 SPECTRA_CALC = numpy.uint32(0x00000004)
831 SPECTRA_CALC = numpy.uint32(0x00000004)
829 INCOHERENT_INTEGRATION = numpy.uint32(0x00000008)
832 INCOHERENT_INTEGRATION = numpy.uint32(0x00000008)
830 POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010)
833 POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010)
831 SHIFT_FFT_DATA = numpy.uint32(0x00000020)
834 SHIFT_FFT_DATA = numpy.uint32(0x00000020)
832
835
833 DATATYPE_CHAR = numpy.uint32(0x00000040)
836 DATATYPE_CHAR = numpy.uint32(0x00000040)
834 DATATYPE_SHORT = numpy.uint32(0x00000080)
837 DATATYPE_SHORT = numpy.uint32(0x00000080)
835 DATATYPE_LONG = numpy.uint32(0x00000100)
838 DATATYPE_LONG = numpy.uint32(0x00000100)
836 DATATYPE_INT64 = numpy.uint32(0x00000200)
839 DATATYPE_INT64 = numpy.uint32(0x00000200)
837 DATATYPE_FLOAT = numpy.uint32(0x00000400)
840 DATATYPE_FLOAT = numpy.uint32(0x00000400)
838 DATATYPE_DOUBLE = numpy.uint32(0x00000800)
841 DATATYPE_DOUBLE = numpy.uint32(0x00000800)
839
842
840 DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000)
843 DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000)
841 DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000)
844 DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000)
842 DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000)
845 DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000)
843
846
844 SAVE_CHANNELS_DC = numpy.uint32(0x00008000)
847 SAVE_CHANNELS_DC = numpy.uint32(0x00008000)
845 DEFLIP_DATA = numpy.uint32(0x00010000)
848 DEFLIP_DATA = numpy.uint32(0x00010000)
846 DEFINE_PROCESS_CODE = numpy.uint32(0x00020000)
849 DEFINE_PROCESS_CODE = numpy.uint32(0x00020000)
847
850
848 ACQ_SYS_NATALIA = numpy.uint32(0x00040000)
851 ACQ_SYS_NATALIA = numpy.uint32(0x00040000)
849 ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000)
852 ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000)
850 ACQ_SYS_ADRXD = numpy.uint32(0x000C0000)
853 ACQ_SYS_ADRXD = numpy.uint32(0x000C0000)
851 ACQ_SYS_JULIA = numpy.uint32(0x00100000)
854 ACQ_SYS_JULIA = numpy.uint32(0x00100000)
852 ACQ_SYS_XXXXXX = numpy.uint32(0x00140000)
855 ACQ_SYS_XXXXXX = numpy.uint32(0x00140000)
853
856
854 EXP_NAME_ESP = numpy.uint32(0x00200000)
857 EXP_NAME_ESP = numpy.uint32(0x00200000)
855 CHANNEL_NAMES_ESP = numpy.uint32(0x00400000)
858 CHANNEL_NAMES_ESP = numpy.uint32(0x00400000)
856
859
857 OPERATION_MASK = numpy.uint32(0x0000003F)
860 OPERATION_MASK = numpy.uint32(0x0000003F)
858 DATATYPE_MASK = numpy.uint32(0x00000FC0)
861 DATATYPE_MASK = numpy.uint32(0x00000FC0)
859 DATAARRANGE_MASK = numpy.uint32(0x00007000)
862 DATAARRANGE_MASK = numpy.uint32(0x00007000)
860 ACQ_SYS_MASK = numpy.uint32(0x001C0000)
863 ACQ_SYS_MASK = numpy.uint32(0x001C0000)
861
864
862
865
863 dtype0 = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
866 dtype0 = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
864 dtype1 = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
867 dtype1 = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
865 dtype2 = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
868 dtype2 = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
866 dtype3 = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
869 dtype3 = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
867 dtype4 = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
870 dtype4 = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
868 dtype5 = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
871 dtype5 = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
869
872
870 NUMPY_DTYPE_LIST = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
873 NUMPY_DTYPE_LIST = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
871
874
872 PROCFLAG_DTYPE_LIST = [PROCFLAG.DATATYPE_CHAR,
875 PROCFLAG_DTYPE_LIST = [PROCFLAG.DATATYPE_CHAR,
873 PROCFLAG.DATATYPE_SHORT,
876 PROCFLAG.DATATYPE_SHORT,
874 PROCFLAG.DATATYPE_LONG,
877 PROCFLAG.DATATYPE_LONG,
875 PROCFLAG.DATATYPE_INT64,
878 PROCFLAG.DATATYPE_INT64,
876 PROCFLAG.DATATYPE_FLOAT,
879 PROCFLAG.DATATYPE_FLOAT,
877 PROCFLAG.DATATYPE_DOUBLE]
880 PROCFLAG.DATATYPE_DOUBLE]
878
881
879 DTYPE_WIDTH = [1, 2, 4, 8, 4, 8]
882 DTYPE_WIDTH = [1, 2, 4, 8, 4, 8]
880
883
881
884
882 def get_dtype_index(numpy_dtype):
885 def get_dtype_index(numpy_dtype):
883
886
884 index = None
887 index = None
885
888
886 for i in range(len(NUMPY_DTYPE_LIST)):
889 for i in range(len(NUMPY_DTYPE_LIST)):
887 if numpy_dtype == NUMPY_DTYPE_LIST[i]:
890 if numpy_dtype == NUMPY_DTYPE_LIST[i]:
888 index = i
891 index = i
889 break
892 break
890
893
891 return index
894 return index
892
895
893
896
894 def get_numpy_dtype(index):
897 def get_numpy_dtype(index):
895
898
896 return NUMPY_DTYPE_LIST[index]
899 return NUMPY_DTYPE_LIST[index]
897
900
898
901
899 def get_procflag_dtype(index):
902 def get_procflag_dtype(index):
900
903
901 return PROCFLAG_DTYPE_LIST[index]
904 return PROCFLAG_DTYPE_LIST[index]
902
905
903
906
904 def get_dtype_width(index):
907 def get_dtype_width(index):
905
908
906 return DTYPE_WIDTH[index] No newline at end of file
909 return DTYPE_WIDTH[index]
@@ -1,5 +1,6
1 from .jroplot_voltage import *
1 from .jroplot_voltage import *
2 from .jroplot_spectra import *
2 from .jroplot_spectra import *
3 from .jroplot_heispectra import *
3 from .jroplot_heispectra import *
4 from .jroplot_correlation import *
4 from .jroplot_correlation import *
5 from .jroplot_parameters import *
5 from .jroplot_parameters import *
6 from .jroplot_voltage_lags import *
@@ -1,693 +1,697
1 # Copyright (c) 2012-2020 Jicamarca Radio Observatory
1 # Copyright (c) 2012-2020 Jicamarca Radio Observatory
2 # All rights reserved.
2 # All rights reserved.
3 #
3 #
4 # Distributed under the terms of the BSD 3-clause license.
4 # Distributed under the terms of the BSD 3-clause license.
5 """Base class to create plot operations
5 """Base class to create plot operations
6
6
7 """
7 """
8
8
9 import os
9 import os
10 import sys
10 import sys
11 import zmq
11 import zmq
12 import time
12 import time
13 import numpy
13 import numpy
14 import datetime
14 import datetime
15 from collections import deque
15 from collections import deque
16 from functools import wraps
16 from functools import wraps
17 from threading import Thread
17 from threading import Thread
18 import matplotlib
18 import matplotlib
19
19
20 if 'BACKEND' in os.environ:
20 if 'BACKEND' in os.environ:
21 matplotlib.use(os.environ['BACKEND'])
21 matplotlib.use(os.environ['BACKEND'])
22 elif 'linux' in sys.platform:
22 elif 'linux' in sys.platform:
23 matplotlib.use("TkAgg")
23 matplotlib.use("TkAgg")
24 elif 'darwin' in sys.platform:
24 elif 'darwin' in sys.platform:
25 matplotlib.use('MacOSX')
25 matplotlib.use('MacOSX')
26 else:
26 else:
27 from schainpy.utils import log
27 from schainpy.utils import log
28 log.warning('Using default Backend="Agg"', 'INFO')
28 log.warning('Using default Backend="Agg"', 'INFO')
29 matplotlib.use('Agg')
29 matplotlib.use('Agg')
30
30
31 import matplotlib.pyplot as plt
31 import matplotlib.pyplot as plt
32 from matplotlib.patches import Polygon
32 from matplotlib.patches import Polygon
33 from mpl_toolkits.axes_grid1 import make_axes_locatable
33 from mpl_toolkits.axes_grid1 import make_axes_locatable
34 from matplotlib.ticker import FuncFormatter, LinearLocator, MultipleLocator
34 from matplotlib.ticker import FuncFormatter, LinearLocator, MultipleLocator
35
35
36 from schainpy.model.data.jrodata import PlotterData
36 from schainpy.model.data.jrodata import PlotterData
37 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
37 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
38 from schainpy.utils import log
38 from schainpy.utils import log
39
39
40 jet_values = matplotlib.pyplot.get_cmap('jet', 100)(numpy.arange(100))[10:90]
40 jet_values = matplotlib.pyplot.get_cmap('jet', 100)(numpy.arange(100))[10:90]
41 blu_values = matplotlib.pyplot.get_cmap(
41 blu_values = matplotlib.pyplot.get_cmap(
42 'seismic_r', 20)(numpy.arange(20))[10:15]
42 'seismic_r', 20)(numpy.arange(20))[10:15]
43 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
43 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
44 'jro', numpy.vstack((blu_values, jet_values)))
44 'jro', numpy.vstack((blu_values, jet_values)))
45 matplotlib.pyplot.register_cmap(cmap=ncmap)
45 matplotlib.pyplot.register_cmap(cmap=ncmap)
46
46
47 CMAPS = [plt.get_cmap(s) for s in ('jro', 'jet', 'viridis',
47 CMAPS = [plt.get_cmap(s) for s in ('jro', 'jet', 'viridis',
48 'plasma', 'inferno', 'Greys', 'seismic', 'bwr', 'coolwarm')]
48 'plasma', 'inferno', 'Greys', 'seismic', 'bwr', 'coolwarm')]
49
49
50 EARTH_RADIUS = 6.3710e3
50 EARTH_RADIUS = 6.3710e3
51
51
52 def ll2xy(lat1, lon1, lat2, lon2):
52 def ll2xy(lat1, lon1, lat2, lon2):
53
53
54 p = 0.017453292519943295
54 p = 0.017453292519943295
55 a = 0.5 - numpy.cos((lat2 - lat1) * p)/2 + numpy.cos(lat1 * p) * \
55 a = 0.5 - numpy.cos((lat2 - lat1) * p)/2 + numpy.cos(lat1 * p) * \
56 numpy.cos(lat2 * p) * (1 - numpy.cos((lon2 - lon1) * p)) / 2
56 numpy.cos(lat2 * p) * (1 - numpy.cos((lon2 - lon1) * p)) / 2
57 r = 12742 * numpy.arcsin(numpy.sqrt(a))
57 r = 12742 * numpy.arcsin(numpy.sqrt(a))
58 theta = numpy.arctan2(numpy.sin((lon2-lon1)*p)*numpy.cos(lat2*p), numpy.cos(lat1*p)
58 theta = numpy.arctan2(numpy.sin((lon2-lon1)*p)*numpy.cos(lat2*p), numpy.cos(lat1*p)
59 * numpy.sin(lat2*p)-numpy.sin(lat1*p)*numpy.cos(lat2*p)*numpy.cos((lon2-lon1)*p))
59 * numpy.sin(lat2*p)-numpy.sin(lat1*p)*numpy.cos(lat2*p)*numpy.cos((lon2-lon1)*p))
60 theta = -theta + numpy.pi/2
60 theta = -theta + numpy.pi/2
61 return r*numpy.cos(theta), r*numpy.sin(theta)
61 return r*numpy.cos(theta), r*numpy.sin(theta)
62
62
63
63
64 def km2deg(km):
64 def km2deg(km):
65 '''
65 '''
66 Convert distance in km to degrees
66 Convert distance in km to degrees
67 '''
67 '''
68
68
69 return numpy.rad2deg(km/EARTH_RADIUS)
69 return numpy.rad2deg(km/EARTH_RADIUS)
70
70
71
71
72 def figpause(interval):
72 def figpause(interval):
73 backend = plt.rcParams['backend']
73 backend = plt.rcParams['backend']
74 if backend in matplotlib.rcsetup.interactive_bk:
74 if backend in matplotlib.rcsetup.interactive_bk:
75 figManager = matplotlib._pylab_helpers.Gcf.get_active()
75 figManager = matplotlib._pylab_helpers.Gcf.get_active()
76 if figManager is not None:
76 if figManager is not None:
77 canvas = figManager.canvas
77 canvas = figManager.canvas
78 if canvas.figure.stale:
78 if canvas.figure.stale:
79 canvas.draw()
79 canvas.draw()
80 try:
80 try:
81 canvas.start_event_loop(interval)
81 canvas.start_event_loop(interval)
82 except:
82 except:
83 pass
83 pass
84 return
84 return
85
85
86 def popup(message):
86 def popup(message):
87 '''
87 '''
88 '''
88 '''
89
89
90 fig = plt.figure(figsize=(12, 8), facecolor='r')
90 fig = plt.figure(figsize=(12, 8), facecolor='r')
91 text = '\n'.join([s.strip() for s in message.split(':')])
91 text = '\n'.join([s.strip() for s in message.split(':')])
92 fig.text(0.01, 0.5, text, ha='left', va='center',
92 fig.text(0.01, 0.5, text, ha='left', va='center',
93 size='20', weight='heavy', color='w')
93 size='20', weight='heavy', color='w')
94 fig.show()
94 fig.show()
95 figpause(1000)
95 figpause(1000)
96
96
97
97
98 class Throttle(object):
98 class Throttle(object):
99 '''
99 '''
100 Decorator that prevents a function from being called more than once every
100 Decorator that prevents a function from being called more than once every
101 time period.
101 time period.
102 To create a function that cannot be called more than once a minute, but
102 To create a function that cannot be called more than once a minute, but
103 will sleep until it can be called:
103 will sleep until it can be called:
104 @Throttle(minutes=1)
104 @Throttle(minutes=1)
105 def foo():
105 def foo():
106 pass
106 pass
107
107
108 for i in range(10):
108 for i in range(10):
109 foo()
109 foo()
110 print "This function has run %s times." % i
110 print "This function has run %s times." % i
111 '''
111 '''
112
112
113 def __init__(self, seconds=0, minutes=0, hours=0):
113 def __init__(self, seconds=0, minutes=0, hours=0):
114 self.throttle_period = datetime.timedelta(
114 self.throttle_period = datetime.timedelta(
115 seconds=seconds, minutes=minutes, hours=hours
115 seconds=seconds, minutes=minutes, hours=hours
116 )
116 )
117
117
118 self.time_of_last_call = datetime.datetime.min
118 self.time_of_last_call = datetime.datetime.min
119
119
120 def __call__(self, fn):
120 def __call__(self, fn):
121 @wraps(fn)
121 @wraps(fn)
122 def wrapper(*args, **kwargs):
122 def wrapper(*args, **kwargs):
123 coerce = kwargs.pop('coerce', None)
123 coerce = kwargs.pop('coerce', None)
124 if coerce:
124 if coerce:
125 self.time_of_last_call = datetime.datetime.now()
125 self.time_of_last_call = datetime.datetime.now()
126 return fn(*args, **kwargs)
126 return fn(*args, **kwargs)
127 else:
127 else:
128 now = datetime.datetime.now()
128 now = datetime.datetime.now()
129 time_since_last_call = now - self.time_of_last_call
129 time_since_last_call = now - self.time_of_last_call
130 time_left = self.throttle_period - time_since_last_call
130 time_left = self.throttle_period - time_since_last_call
131
131
132 if time_left > datetime.timedelta(seconds=0):
132 if time_left > datetime.timedelta(seconds=0):
133 return
133 return
134
134
135 self.time_of_last_call = datetime.datetime.now()
135 self.time_of_last_call = datetime.datetime.now()
136 return fn(*args, **kwargs)
136 return fn(*args, **kwargs)
137
137
138 return wrapper
138 return wrapper
139
139
140 def apply_throttle(value):
140 def apply_throttle(value):
141
141
142 @Throttle(seconds=value)
142 @Throttle(seconds=value)
143 def fnThrottled(fn):
143 def fnThrottled(fn):
144 fn()
144 fn()
145
145
146 return fnThrottled
146 return fnThrottled
147
147
148
148
149 @MPDecorator
149 @MPDecorator
150 class Plot(Operation):
150 class Plot(Operation):
151 """Base class for Schain plotting operations
151 """Base class for Schain plotting operations
152
152
153 This class should never be use directtly you must subclass a new operation,
153 This class should never be use directtly you must subclass a new operation,
154 children classes must be defined as follow:
154 children classes must be defined as follow:
155
155
156 ExamplePlot(Plot):
156 ExamplePlot(Plot):
157
157
158 CODE = 'code'
158 CODE = 'code'
159 colormap = 'jet'
159 colormap = 'jet'
160 plot_type = 'pcolor' # options are ('pcolor', 'pcolorbuffer', 'scatter', 'scatterbuffer')
160 plot_type = 'pcolor' # options are ('pcolor', 'pcolorbuffer', 'scatter', 'scatterbuffer')
161
161
162 def setup(self):
162 def setup(self):
163 pass
163 pass
164
164
165 def plot(self):
165 def plot(self):
166 pass
166 pass
167
167
168 """
168 """
169
169
170 CODE = 'Figure'
170 CODE = 'Figure'
171 colormap = 'jet'
171 colormap = 'jet'
172 bgcolor = 'white'
172 bgcolor = 'white'
173 buffering = True
173 buffering = True
174 __missing = 1E30
174 __missing = 1E30
175
175
176 __attrs__ = ['show', 'save', 'ymin', 'ymax', 'zmin', 'zmax', 'title',
176 __attrs__ = ['show', 'save', 'ymin', 'ymax', 'zmin', 'zmax', 'title',
177 'showprofile']
177 'showprofile']
178
178
179 def __init__(self):
179 def __init__(self):
180
180
181 Operation.__init__(self)
181 Operation.__init__(self)
182 self.isConfig = False
182 self.isConfig = False
183 self.isPlotConfig = False
183 self.isPlotConfig = False
184 self.save_time = 0
184 self.save_time = 0
185 self.sender_time = 0
185 self.sender_time = 0
186 self.data = None
186 self.data = None
187 self.firsttime = True
187 self.firsttime = True
188 self.sender_queue = deque(maxlen=10)
188 self.sender_queue = deque(maxlen=10)
189 self.plots_adjust = {'left': 0.125, 'right': 0.9, 'bottom': 0.15, 'top': 0.9, 'wspace': 0.2, 'hspace': 0.2}
189 self.plots_adjust = {'left': 0.125, 'right': 0.9, 'bottom': 0.15, 'top': 0.9, 'wspace': 0.2, 'hspace': 0.2}
190
190
191 def __fmtTime(self, x, pos):
191 def __fmtTime(self, x, pos):
192 '''
192 '''
193 '''
193 '''
194
194
195 return '{}'.format(self.getDateTime(x).strftime('%H:%M'))
195 return '{}'.format(self.getDateTime(x).strftime('%H:%M'))
196
196
197 def __setup(self, **kwargs):
197 def __setup(self, **kwargs):
198 '''
198 '''
199 Initialize variables
199 Initialize variables
200 '''
200 '''
201
201
202 self.figures = []
202 self.figures = []
203 self.axes = []
203 self.axes = []
204 self.cb_axes = []
204 self.cb_axes = []
205 self.localtime = kwargs.pop('localtime', True)
205 self.localtime = kwargs.pop('localtime', True)
206 self.show = kwargs.get('show', True)
206 self.show = kwargs.get('show', True)
207 self.save = kwargs.get('save', False)
207 self.save = kwargs.get('save', False)
208 self.save_period = kwargs.get('save_period', 0)
208 self.save_period = kwargs.get('save_period', 0)
209 self.colormap = kwargs.get('colormap', self.colormap)
209 self.colormap = kwargs.get('colormap', self.colormap)
210 self.colormap_coh = kwargs.get('colormap_coh', 'jet')
210 self.colormap_coh = kwargs.get('colormap_coh', 'jet')
211 self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r')
211 self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r')
212 self.colormaps = kwargs.get('colormaps', None)
212 self.colormaps = kwargs.get('colormaps', None)
213 self.bgcolor = kwargs.get('bgcolor', self.bgcolor)
213 self.bgcolor = kwargs.get('bgcolor', self.bgcolor)
214 self.showprofile = kwargs.get('showprofile', False)
214 self.showprofile = kwargs.get('showprofile', False)
215 self.title = kwargs.get('wintitle', self.CODE.upper())
215 self.title = kwargs.get('wintitle', self.CODE.upper())
216 self.cb_label = kwargs.get('cb_label', None)
216 self.cb_label = kwargs.get('cb_label', None)
217 self.cb_labels = kwargs.get('cb_labels', None)
217 self.cb_labels = kwargs.get('cb_labels', None)
218 self.labels = kwargs.get('labels', None)
218 self.labels = kwargs.get('labels', None)
219 self.xaxis = kwargs.get('xaxis', 'frequency')
219 self.xaxis = kwargs.get('xaxis', 'frequency')
220 self.zmin = kwargs.get('zmin', None)
220 self.zmin = kwargs.get('zmin', None)
221 self.zmax = kwargs.get('zmax', None)
221 self.zmax = kwargs.get('zmax', None)
222 self.zlimits = kwargs.get('zlimits', None)
222 self.zlimits = kwargs.get('zlimits', None)
223 self.xlimits = kwargs.get('xlimits', None)
224 self.xstep_given = kwargs.get('xstep_given', None)
225 self.ystep_given = kwargs.get('ystep_given', None)
226 self.autoxticks = kwargs.get('autoxticks', True)
223 self.xmin = kwargs.get('xmin', None)
227 self.xmin = kwargs.get('xmin', None)
224 self.xmax = kwargs.get('xmax', None)
228 self.xmax = kwargs.get('xmax', None)
225 self.xrange = kwargs.get('xrange', 12)
229 self.xrange = kwargs.get('xrange', 12)
226 self.xscale = kwargs.get('xscale', None)
230 self.xscale = kwargs.get('xscale', None)
227 self.ymin = kwargs.get('ymin', None)
231 self.ymin = kwargs.get('ymin', None)
228 self.ymax = kwargs.get('ymax', None)
232 self.ymax = kwargs.get('ymax', None)
229 self.yscale = kwargs.get('yscale', None)
233 self.yscale = kwargs.get('yscale', None)
230 self.xlabel = kwargs.get('xlabel', None)
234 self.xlabel = kwargs.get('xlabel', None)
231 self.attr_time = kwargs.get('attr_time', 'utctime')
235 self.attr_time = kwargs.get('attr_time', 'utctime')
232 self.attr_data = kwargs.get('attr_data', 'data_param')
236 self.attr_data = kwargs.get('attr_data', 'data_param')
233 self.decimation = kwargs.get('decimation', None)
237 self.decimation = kwargs.get('decimation', None)
234 self.oneFigure = kwargs.get('oneFigure', True)
238 self.oneFigure = kwargs.get('oneFigure', True)
235 self.width = kwargs.get('width', None)
239 self.width = kwargs.get('width', None)
236 self.height = kwargs.get('height', None)
240 self.height = kwargs.get('height', None)
237 self.colorbar = kwargs.get('colorbar', True)
241 self.colorbar = kwargs.get('colorbar', True)
238 self.factors = kwargs.get('factors', [1, 1, 1, 1, 1, 1, 1, 1])
242 self.factors = kwargs.get('factors', [1, 1, 1, 1, 1, 1, 1, 1])
239 self.channels = kwargs.get('channels', None)
243 self.channels = kwargs.get('channels', None)
240 self.titles = kwargs.get('titles', [])
244 self.titles = kwargs.get('titles', [])
241 self.polar = False
245 self.polar = False
242 self.type = kwargs.get('type', 'iq')
246 self.type = kwargs.get('type', 'iq')
243 self.grid = kwargs.get('grid', False)
247 self.grid = kwargs.get('grid', False)
244 self.pause = kwargs.get('pause', False)
248 self.pause = kwargs.get('pause', False)
245 self.save_code = kwargs.get('save_code', self.CODE)
249 self.save_code = kwargs.get('save_code', self.CODE)
246 self.throttle = kwargs.get('throttle', 0)
250 self.throttle = kwargs.get('throttle', 0)
247 self.exp_code = kwargs.get('exp_code', None)
251 self.exp_code = kwargs.get('exp_code', None)
248 self.server = kwargs.get('server', False)
252 self.server = kwargs.get('server', False)
249 self.sender_period = kwargs.get('sender_period', 60)
253 self.sender_period = kwargs.get('sender_period', 60)
250 self.tag = kwargs.get('tag', '')
254 self.tag = kwargs.get('tag', '')
251 self.height_index = kwargs.get('height_index', None)
255 self.height_index = kwargs.get('height_index', None)
252 self.__throttle_plot = apply_throttle(self.throttle)
256 self.__throttle_plot = apply_throttle(self.throttle)
253 code = self.attr_data if self.attr_data else self.CODE
257 code = self.attr_data if self.attr_data else self.CODE
254 self.data = PlotterData(self.CODE, self.exp_code, self.localtime)
258 self.data = PlotterData(self.CODE, self.exp_code, self.localtime)
255
259
256 if self.server:
260 if self.server:
257 if not self.server.startswith('tcp://'):
261 if not self.server.startswith('tcp://'):
258 self.server = 'tcp://{}'.format(self.server)
262 self.server = 'tcp://{}'.format(self.server)
259 log.success(
263 log.success(
260 'Sending to server: {}'.format(self.server),
264 'Sending to server: {}'.format(self.server),
261 self.name
265 self.name
262 )
266 )
263
267
264 if isinstance(self.attr_data, str):
268 if isinstance(self.attr_data, str):
265 self.attr_data = [self.attr_data]
269 self.attr_data = [self.attr_data]
266
270
267 def __setup_plot(self):
271 def __setup_plot(self):
268 '''
272 '''
269 Common setup for all figures, here figures and axes are created
273 Common setup for all figures, here figures and axes are created
270 '''
274 '''
271
275
272 self.setup()
276 self.setup()
273
277
274 self.time_label = 'LT' if self.localtime else 'UTC'
278 self.time_label = 'LT' if self.localtime else 'UTC'
275
279
276 if self.width is None:
280 if self.width is None:
277 self.width = 8
281 self.width = 8
278
282
279 self.figures = []
283 self.figures = []
280 self.axes = []
284 self.axes = []
281 self.cb_axes = []
285 self.cb_axes = []
282 self.pf_axes = []
286 self.pf_axes = []
283 self.cmaps = []
287 self.cmaps = []
284
288
285 size = '15%' if self.ncols == 1 else '30%'
289 size = '15%' if self.ncols == 1 else '30%'
286 pad = '4%' if self.ncols == 1 else '8%'
290 pad = '4%' if self.ncols == 1 else '8%'
287
291
288 if self.oneFigure:
292 if self.oneFigure:
289 if self.height is None:
293 if self.height is None:
290 self.height = 1.4 * self.nrows + 1
294 self.height = 1.4 * self.nrows + 1
291 fig = plt.figure(figsize=(self.width, self.height),
295 fig = plt.figure(figsize=(self.width, self.height),
292 edgecolor='k',
296 edgecolor='k',
293 facecolor='w')
297 facecolor='w')
294 self.figures.append(fig)
298 self.figures.append(fig)
295 for n in range(self.nplots):
299 for n in range(self.nplots):
296 ax = fig.add_subplot(self.nrows, self.ncols,
300 ax = fig.add_subplot(self.nrows, self.ncols,
297 n + 1, polar=self.polar)
301 n + 1, polar=self.polar)
298 ax.tick_params(labelsize=8)
302 ax.tick_params(labelsize=8)
299 ax.firsttime = True
303 ax.firsttime = True
300 ax.index = 0
304 ax.index = 0
301 ax.press = None
305 ax.press = None
302 self.axes.append(ax)
306 self.axes.append(ax)
303 if self.showprofile:
307 if self.showprofile:
304 cax = self.__add_axes(ax, size=size, pad=pad)
308 cax = self.__add_axes(ax, size=size, pad=pad)
305 cax.tick_params(labelsize=8)
309 cax.tick_params(labelsize=8)
306 self.pf_axes.append(cax)
310 self.pf_axes.append(cax)
307 else:
311 else:
308 if self.height is None:
312 if self.height is None:
309 self.height = 3
313 self.height = 3
310 for n in range(self.nplots):
314 for n in range(self.nplots):
311 fig = plt.figure(figsize=(self.width, self.height),
315 fig = plt.figure(figsize=(self.width, self.height),
312 edgecolor='k',
316 edgecolor='k',
313 facecolor='w')
317 facecolor='w')
314 ax = fig.add_subplot(1, 1, 1, polar=self.polar)
318 ax = fig.add_subplot(1, 1, 1, polar=self.polar)
315 ax.tick_params(labelsize=8)
319 ax.tick_params(labelsize=8)
316 ax.firsttime = True
320 ax.firsttime = True
317 ax.index = 0
321 ax.index = 0
318 ax.press = None
322 ax.press = None
319 self.figures.append(fig)
323 self.figures.append(fig)
320 self.axes.append(ax)
324 self.axes.append(ax)
321 if self.showprofile:
325 if self.showprofile:
322 cax = self.__add_axes(ax, size=size, pad=pad)
326 cax = self.__add_axes(ax, size=size, pad=pad)
323 cax.tick_params(labelsize=8)
327 cax.tick_params(labelsize=8)
324 self.pf_axes.append(cax)
328 self.pf_axes.append(cax)
325
329
326 for n in range(self.nrows):
330 for n in range(self.nrows):
327 if self.colormaps is not None:
331 if self.colormaps is not None:
328 cmap = plt.get_cmap(self.colormaps[n])
332 cmap = plt.get_cmap(self.colormaps[n])
329 else:
333 else:
330 cmap = plt.get_cmap(self.colormap)
334 cmap = plt.get_cmap(self.colormap)
331 cmap.set_bad(self.bgcolor, 1.)
335 cmap.set_bad(self.bgcolor, 1.)
332 self.cmaps.append(cmap)
336 self.cmaps.append(cmap)
333
337
334 def __add_axes(self, ax, size='30%', pad='8%'):
338 def __add_axes(self, ax, size='30%', pad='8%'):
335 '''
339 '''
336 Add new axes to the given figure
340 Add new axes to the given figure
337 '''
341 '''
338 divider = make_axes_locatable(ax)
342 divider = make_axes_locatable(ax)
339 nax = divider.new_horizontal(size=size, pad=pad)
343 nax = divider.new_horizontal(size=size, pad=pad)
340 ax.figure.add_axes(nax)
344 ax.figure.add_axes(nax)
341 return nax
345 return nax
342
346
343 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
347 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
344 '''
348 '''
345 Create a masked array for missing data
349 Create a masked array for missing data
346 '''
350 '''
347 if x_buffer.shape[0] < 2:
351 if x_buffer.shape[0] < 2:
348 return x_buffer, y_buffer, z_buffer
352 return x_buffer, y_buffer, z_buffer
349
353
350 deltas = x_buffer[1:] - x_buffer[0:-1]
354 deltas = x_buffer[1:] - x_buffer[0:-1]
351 x_median = numpy.median(deltas)
355 x_median = numpy.median(deltas)
352
356
353 index = numpy.where(deltas > 5 * x_median)
357 index = numpy.where(deltas > 5 * x_median)
354
358
355 if len(index[0]) != 0:
359 if len(index[0]) != 0:
356 z_buffer[::, index[0], ::] = self.__missing
360 z_buffer[::, index[0], ::] = self.__missing
357 z_buffer = numpy.ma.masked_inside(z_buffer,
361 z_buffer = numpy.ma.masked_inside(z_buffer,
358 0.99 * self.__missing,
362 0.99 * self.__missing,
359 1.01 * self.__missing)
363 1.01 * self.__missing)
360
364
361 return x_buffer, y_buffer, z_buffer
365 return x_buffer, y_buffer, z_buffer
362
366
363 def decimate(self):
367 def decimate(self):
364
368
365 # dx = int(len(self.x)/self.__MAXNUMX) + 1
369 # dx = int(len(self.x)/self.__MAXNUMX) + 1
366 dy = int(len(self.y) / self.decimation) + 1
370 dy = int(len(self.y) / self.decimation) + 1
367
371
368 # x = self.x[::dx]
372 # x = self.x[::dx]
369 x = self.x
373 x = self.x
370 y = self.y[::dy]
374 y = self.y[::dy]
371 z = self.z[::, ::, ::dy]
375 z = self.z[::, ::, ::dy]
372
376
373 return x, y, z
377 return x, y, z
374
378
375 def format(self):
379 def format(self):
376 '''
380 '''
377 Set min and max values, labels, ticks and titles
381 Set min and max values, labels, ticks and titles
378 '''
382 '''
379
383
380 for n, ax in enumerate(self.axes):
384 for n, ax in enumerate(self.axes):
381 if ax.firsttime:
385 if ax.firsttime:
382 if self.xaxis != 'time':
386 if self.xaxis != 'time':
383 xmin = self.xmin
387 xmin = self.xmin
384 xmax = self.xmax
388 xmax = self.xmax
385 else:
389 else:
386 xmin = self.tmin
390 xmin = self.tmin
387 xmax = self.tmin + self.xrange*60*60
391 xmax = self.tmin + self.xrange*60*60
388 ax.xaxis.set_major_formatter(FuncFormatter(self.__fmtTime))
392 ax.xaxis.set_major_formatter(FuncFormatter(self.__fmtTime))
389 ax.xaxis.set_major_locator(LinearLocator(9))
393 ax.xaxis.set_major_locator(LinearLocator(9))
390 ymin = self.ymin if self.ymin is not None else numpy.nanmin(self.y[numpy.isfinite(self.y)])
394 ymin = self.ymin if self.ymin is not None else numpy.nanmin(self.y[numpy.isfinite(self.y)])
391 ymax = self.ymax if self.ymax is not None else numpy.nanmax(self.y[numpy.isfinite(self.y)])
395 ymax = self.ymax if self.ymax is not None else numpy.nanmax(self.y[numpy.isfinite(self.y)])
392 ax.set_facecolor(self.bgcolor)
396 ax.set_facecolor(self.bgcolor)
393 if self.xscale:
397 if self.xscale:
394 ax.xaxis.set_major_formatter(FuncFormatter(
398 ax.xaxis.set_major_formatter(FuncFormatter(
395 lambda x, pos: '{0:g}'.format(x*self.xscale)))
399 lambda x, pos: '{0:g}'.format(x*self.xscale)))
396 if self.yscale:
400 if self.yscale:
397 ax.yaxis.set_major_formatter(FuncFormatter(
401 ax.yaxis.set_major_formatter(FuncFormatter(
398 lambda x, pos: '{0:g}'.format(x*self.yscale)))
402 lambda x, pos: '{0:g}'.format(x*self.yscale)))
399 if self.xlabel is not None:
403 if self.xlabel is not None:
400 ax.set_xlabel(self.xlabel)
404 ax.set_xlabel(self.xlabel)
401 if self.ylabel is not None:
405 if self.ylabel is not None:
402 ax.set_ylabel(self.ylabel)
406 ax.set_ylabel(self.ylabel)
403 if self.showprofile:
407 if self.showprofile:
404 self.pf_axes[n].set_ylim(ymin, ymax)
408 self.pf_axes[n].set_ylim(ymin, ymax)
405 self.pf_axes[n].set_xlim(self.zmin, self.zmax)
409 self.pf_axes[n].set_xlim(self.zmin, self.zmax)
406 self.pf_axes[n].set_xlabel('dB')
410 self.pf_axes[n].set_xlabel('dB')
407 self.pf_axes[n].grid(b=True, axis='x')
411 self.pf_axes[n].grid(b=True, axis='x')
408 [tick.set_visible(False)
412 [tick.set_visible(False)
409 for tick in self.pf_axes[n].get_yticklabels()]
413 for tick in self.pf_axes[n].get_yticklabels()]
410 if self.colorbar:
414 if self.colorbar:
411 ax.cbar = plt.colorbar(
415 ax.cbar = plt.colorbar(
412 ax.plt, ax=ax, fraction=0.05, pad=0.02, aspect=10)
416 ax.plt, ax=ax, fraction=0.05, pad=0.02, aspect=10)
413 ax.cbar.ax.tick_params(labelsize=8)
417 ax.cbar.ax.tick_params(labelsize=8)
414 ax.cbar.ax.press = None
418 ax.cbar.ax.press = None
415 if self.cb_label:
419 if self.cb_label:
416 ax.cbar.set_label(self.cb_label, size=8)
420 ax.cbar.set_label(self.cb_label, size=8)
417 elif self.cb_labels:
421 elif self.cb_labels:
418 ax.cbar.set_label(self.cb_labels[n], size=8)
422 ax.cbar.set_label(self.cb_labels[n], size=8)
419 else:
423 else:
420 ax.cbar = None
424 ax.cbar = None
421 ax.set_xlim(xmin, xmax)
425 ax.set_xlim(xmin, xmax)
422 ax.set_ylim(ymin, ymax)
426 ax.set_ylim(ymin, ymax)
423 ax.firsttime = False
427 ax.firsttime = False
424 if self.grid:
428 if self.grid:
425 ax.grid(True)
429 ax.grid(True)
426 if not self.polar:
430 if not self.polar:
427 ax.set_title('{} {} {}'.format(
431 ax.set_title('{} {} {}'.format(
428 self.titles[n],
432 self.titles[n],
429 self.getDateTime(self.data.max_time).strftime(
433 self.getDateTime(self.data.max_time).strftime(
430 '%Y-%m-%d %H:%M:%S'),
434 '%Y-%m-%d %H:%M:%S'),
431 self.time_label),
435 self.time_label),
432 size=8)
436 size=8)
433 else:
437 else:
434 ax.set_title('{}'.format(self.titles[n]), size=8)
438 ax.set_title('{}'.format(self.titles[n]), size=8)
435 ax.set_ylim(0, 90)
439 ax.set_ylim(0, 90)
436 ax.set_yticks(numpy.arange(0, 90, 20))
440 ax.set_yticks(numpy.arange(0, 90, 20))
437 ax.yaxis.labelpad = 40
441 ax.yaxis.labelpad = 40
438
442
439 if self.firsttime:
443 if self.firsttime:
440 for n, fig in enumerate(self.figures):
444 for n, fig in enumerate(self.figures):
441 fig.subplots_adjust(**self.plots_adjust)
445 fig.subplots_adjust(**self.plots_adjust)
442 self.firsttime = False
446 self.firsttime = False
443
447
444 def clear_figures(self):
448 def clear_figures(self):
445 '''
449 '''
446 Reset axes for redraw plots
450 Reset axes for redraw plots
447 '''
451 '''
448
452
449 for ax in self.axes+self.pf_axes+self.cb_axes:
453 for ax in self.axes+self.pf_axes+self.cb_axes:
450 ax.clear()
454 ax.clear()
451 ax.firsttime = True
455 ax.firsttime = True
452 if hasattr(ax, 'cbar') and ax.cbar:
456 if hasattr(ax, 'cbar') and ax.cbar:
453 ax.cbar.remove()
457 ax.cbar.remove()
454
458
455 def __plot(self):
459 def __plot(self):
456 '''
460 '''
457 Main function to plot, format and save figures
461 Main function to plot, format and save figures
458 '''
462 '''
459
463
460 self.plot()
464 self.plot()
461 self.format()
465 self.format()
462
466
463 for n, fig in enumerate(self.figures):
467 for n, fig in enumerate(self.figures):
464 if self.nrows == 0 or self.nplots == 0:
468 if self.nrows == 0 or self.nplots == 0:
465 log.warning('No data', self.name)
469 log.warning('No data', self.name)
466 fig.text(0.5, 0.5, 'No Data', fontsize='large', ha='center')
470 fig.text(0.5, 0.5, 'No Data', fontsize='large', ha='center')
467 fig.canvas.manager.set_window_title(self.CODE)
471 fig.canvas.manager.set_window_title(self.CODE)
468 continue
472 continue
469
473
470 fig.canvas.manager.set_window_title('{} - {}'.format(self.title,
474 fig.canvas.manager.set_window_title('{} - {}'.format(self.title,
471 self.getDateTime(self.data.max_time).strftime('%Y/%m/%d')))
475 self.getDateTime(self.data.max_time).strftime('%Y/%m/%d')))
472 fig.canvas.draw()
476 fig.canvas.draw()
473 if self.show:
477 if self.show:
474 fig.show()
478 fig.show()
475 figpause(0.01)
479 figpause(0.01)
476
480
477 if self.save:
481 if self.save:
478 self.save_figure(n)
482 self.save_figure(n)
479
483
480 if self.server:
484 if self.server:
481 self.send_to_server()
485 self.send_to_server()
482
486
483 def __update(self, dataOut, timestamp):
487 def __update(self, dataOut, timestamp):
484 '''
488 '''
485 '''
489 '''
486
490
487 metadata = {
491 metadata = {
488 'yrange': dataOut.heightList,
492 'yrange': dataOut.heightList,
489 'interval': dataOut.timeInterval,
493 'interval': dataOut.timeInterval,
490 'channels': dataOut.channelList
494 'channels': dataOut.channelList
491 }
495 }
492
496
493 data, meta = self.update(dataOut)
497 data, meta = self.update(dataOut)
494 metadata.update(meta)
498 metadata.update(meta)
495 self.data.update(data, timestamp, metadata)
499 self.data.update(data, timestamp, metadata)
496
500
497 def save_figure(self, n):
501 def save_figure(self, n):
498 '''
502 '''
499 '''
503 '''
500
504
501 if (self.data.max_time - self.save_time) <= self.save_period:
505 if (self.data.max_time - self.save_time) <= self.save_period:
502 return
506 return
503
507
504 self.save_time = self.data.max_time
508 self.save_time = self.data.max_time
505
509
506 fig = self.figures[n]
510 fig = self.figures[n]
507
511
508 if self.throttle == 0:
512 if self.throttle == 0:
509 figname = os.path.join(
513 figname = os.path.join(
510 self.save,
514 self.save,
511 self.save_code,
515 self.save_code,
512 '{}_{}.png'.format(
516 '{}_{}.png'.format(
513 self.save_code,
517 self.save_code,
514 self.getDateTime(self.data.max_time).strftime(
518 self.getDateTime(self.data.max_time).strftime(
515 '%Y%m%d_%H%M%S'
519 '%Y%m%d_%H%M%S'
516 ),
520 ),
517 )
521 )
518 )
522 )
519 log.log('Saving figure: {}'.format(figname), self.name)
523 log.log('Saving figure: {}'.format(figname), self.name)
520 if not os.path.isdir(os.path.dirname(figname)):
524 if not os.path.isdir(os.path.dirname(figname)):
521 os.makedirs(os.path.dirname(figname))
525 os.makedirs(os.path.dirname(figname))
522 fig.savefig(figname)
526 fig.savefig(figname)
523
527
524 figname = os.path.join(
528 figname = os.path.join(
525 self.save,
529 self.save,
530 self.save_code,
526 '{}_{}.png'.format(
531 '{}_{}.png'.format(
527 self.save_code,
532 self.save_code,
528 self.getDateTime(self.data.min_time).strftime(
533 self.getDateTime(self.data.min_time).strftime(
529 '%Y%m%d'
534 '%Y%m%d'
530 ),
535 ),
531 )
536 )
532 )
537 )
533 log.log('Saving figure: {}'.format(figname), self.name)
538 log.log('Saving figure: {}'.format(figname), self.name)
534 if not os.path.isdir(os.path.dirname(figname)):
539 if not os.path.isdir(os.path.dirname(figname)):
535 os.makedirs(os.path.dirname(figname))
540 os.makedirs(os.path.dirname(figname))
536 fig.savefig(figname)
541 fig.savefig(figname)
537
542
538 def send_to_server(self):
543 def send_to_server(self):
539 '''
544 '''
540 '''
545 '''
541
546
542 if self.exp_code == None:
547 if self.exp_code == None:
543 log.warning('Missing `exp_code` skipping sending to server...')
548 log.warning('Missing `exp_code` skipping sending to server...')
544
549
545 last_time = self.data.max_time
550 last_time = self.data.max_time
546 interval = last_time - self.sender_time
551 interval = last_time - self.sender_time
547 if interval < self.sender_period:
552 if interval < self.sender_period:
548 return
553 return
549
554
550 self.sender_time = last_time
555 self.sender_time = last_time
551
556
552 attrs = ['titles', 'zmin', 'zmax', 'tag', 'ymin', 'ymax']
557 attrs = ['titles', 'zmin', 'zmax', 'tag', 'ymin', 'ymax']
553 for attr in attrs:
558 for attr in attrs:
554 value = getattr(self, attr)
559 value = getattr(self, attr)
555 if value:
560 if value:
556 if isinstance(value, (numpy.float32, numpy.float64)):
561 if isinstance(value, (numpy.float32, numpy.float64)):
557 value = round(float(value), 2)
562 value = round(float(value), 2)
558 self.data.meta[attr] = value
563 self.data.meta[attr] = value
559 if self.colormap == 'jet':
564 if self.colormap == 'jet':
560 self.data.meta['colormap'] = 'Jet'
565 self.data.meta['colormap'] = 'Jet'
561 elif 'RdBu' in self.colormap:
566 elif 'RdBu' in self.colormap:
562 self.data.meta['colormap'] = 'RdBu'
567 self.data.meta['colormap'] = 'RdBu'
563 else:
568 else:
564 self.data.meta['colormap'] = 'Viridis'
569 self.data.meta['colormap'] = 'Viridis'
565 self.data.meta['interval'] = int(interval)
570 self.data.meta['interval'] = int(interval)
566
571
567 self.sender_queue.append(last_time)
572 self.sender_queue.append(last_time)
568
573
569 while True:
574 while True:
570 try:
575 try:
571 tm = self.sender_queue.popleft()
576 tm = self.sender_queue.popleft()
572 except IndexError:
577 except IndexError:
573 break
578 break
574 msg = self.data.jsonify(tm, self.save_code, self.plot_type)
579 msg = self.data.jsonify(tm, self.save_code, self.plot_type)
575 self.socket.send_string(msg)
580 self.socket.send_string(msg)
576 socks = dict(self.poll.poll(2000))
581 socks = dict(self.poll.poll(2000))
577 if socks.get(self.socket) == zmq.POLLIN:
582 if socks.get(self.socket) == zmq.POLLIN:
578 reply = self.socket.recv_string()
583 reply = self.socket.recv_string()
579 if reply == 'ok':
584 if reply == 'ok':
580 log.log("Response from server ok", self.name)
585 log.log("Response from server ok", self.name)
581 time.sleep(0.1)
586 time.sleep(0.1)
582 continue
587 continue
583 else:
588 else:
584 log.warning(
589 log.warning(
585 "Malformed reply from server: {}".format(reply), self.name)
590 "Malformed reply from server: {}".format(reply), self.name)
586 else:
591 else:
587 log.warning(
592 log.warning(
588 "No response from server, retrying...", self.name)
593 "No response from server, retrying...", self.name)
589 self.sender_queue.appendleft(tm)
594 self.sender_queue.appendleft(tm)
590 self.socket.setsockopt(zmq.LINGER, 0)
595 self.socket.setsockopt(zmq.LINGER, 0)
591 self.socket.close()
596 self.socket.close()
592 self.poll.unregister(self.socket)
597 self.poll.unregister(self.socket)
593 self.socket = self.context.socket(zmq.REQ)
598 self.socket = self.context.socket(zmq.REQ)
594 self.socket.connect(self.server)
599 self.socket.connect(self.server)
595 self.poll.register(self.socket, zmq.POLLIN)
600 self.poll.register(self.socket, zmq.POLLIN)
596 break
601 break
597
602
598 def setup(self):
603 def setup(self):
599 '''
604 '''
600 This method should be implemented in the child class, the following
605 This method should be implemented in the child class, the following
601 attributes should be set:
606 attributes should be set:
602
607
603 self.nrows: number of rows
608 self.nrows: number of rows
604 self.ncols: number of cols
609 self.ncols: number of cols
605 self.nplots: number of plots (channels or pairs)
610 self.nplots: number of plots (channels or pairs)
606 self.ylabel: label for Y axes
611 self.ylabel: label for Y axes
607 self.titles: list of axes title
612 self.titles: list of axes title
608
613
609 '''
614 '''
610 raise NotImplementedError
615 raise NotImplementedError
611
616
612 def plot(self):
617 def plot(self):
613 '''
618 '''
614 Must be defined in the child class, the actual plotting method
619 Must be defined in the child class, the actual plotting method
615 '''
620 '''
616 raise NotImplementedError
621 raise NotImplementedError
617
622
618 def update(self, dataOut):
623 def update(self, dataOut):
619 '''
624 '''
620 Must be defined in the child class, update self.data with new data
625 Must be defined in the child class, update self.data with new data
621 '''
626 '''
622
627
623 data = {
628 data = {
624 self.CODE: getattr(dataOut, 'data_{}'.format(self.CODE))
629 self.CODE: getattr(dataOut, 'data_{}'.format(self.CODE))
625 }
630 }
626 meta = {}
631 meta = {}
627
632
628 return data, meta
633 return data, meta
629
634
630 def run(self, dataOut, **kwargs):
635 def run(self, dataOut, **kwargs):
631 '''
636 '''
632 Main plotting routine
637 Main plotting routine
633 '''
638 '''
634
639
635 if self.isConfig is False:
640 if self.isConfig is False:
636 self.__setup(**kwargs)
641 self.__setup(**kwargs)
637
642
638 if self.localtime:
643 if self.localtime:
639 self.getDateTime = datetime.datetime.fromtimestamp
644 self.getDateTime = datetime.datetime.fromtimestamp
640 else:
645 else:
641 self.getDateTime = datetime.datetime.utcfromtimestamp
646 self.getDateTime = datetime.datetime.utcfromtimestamp
642
647
643 self.data.setup()
648 self.data.setup()
644 self.isConfig = True
649 self.isConfig = True
645 if self.server:
650 if self.server:
646 self.context = zmq.Context()
651 self.context = zmq.Context()
647 self.socket = self.context.socket(zmq.REQ)
652 self.socket = self.context.socket(zmq.REQ)
648 self.socket.connect(self.server)
653 self.socket.connect(self.server)
649 self.poll = zmq.Poller()
654 self.poll = zmq.Poller()
650 self.poll.register(self.socket, zmq.POLLIN)
655 self.poll.register(self.socket, zmq.POLLIN)
651
656
652 tm = getattr(dataOut, self.attr_time)
657 tm = getattr(dataOut, self.attr_time)
653
658
654 if self.data and 'time' in self.xaxis and (tm - self.tmin) >= self.xrange*60*60:
659 if self.data and 'time' in self.xaxis and (tm - self.tmin) >= self.xrange*60*60:
655 self.save_time = tm
660 self.save_time = tm
656 self.__plot()
661 self.__plot()
657 self.tmin += self.xrange*60*60
662 self.tmin += self.xrange*60*60
658 self.data.setup()
663 self.data.setup()
659 self.clear_figures()
664 self.clear_figures()
660
665
661 self.__update(dataOut, tm)
666 self.__update(dataOut, tm)
662
667
663 if self.isPlotConfig is False:
668 if self.isPlotConfig is False:
664 self.__setup_plot()
669 self.__setup_plot()
665 self.isPlotConfig = True
670 self.isPlotConfig = True
666 if self.xaxis == 'time':
671 if self.xaxis == 'time':
667 dt = self.getDateTime(tm)
672 dt = self.getDateTime(tm)
668 if self.xmin is None:
673 if self.xmin is None:
669 self.tmin = tm
674 self.tmin = tm
670 self.xmin = dt.hour
675 self.xmin = dt.hour
671 minutes = (self.xmin-int(self.xmin)) * 60
676 minutes = (self.xmin-int(self.xmin)) * 60
672 seconds = (minutes - int(minutes)) * 60
677 seconds = (minutes - int(minutes)) * 60
673 self.tmin = (dt.replace(hour=int(self.xmin), minute=int(minutes), second=int(seconds)) -
678 self.tmin = (dt.replace(hour=int(self.xmin), minute=int(minutes), second=int(seconds)) -
674 datetime.datetime(1970, 1, 1)).total_seconds()
679 datetime.datetime(1970, 1, 1)).total_seconds()
675 if self.localtime:
680 if self.localtime:
676 self.tmin += time.timezone
681 self.tmin += time.timezone
677
682
678 if self.xmin is not None and self.xmax is not None:
683 if self.xmin is not None and self.xmax is not None:
679 self.xrange = self.xmax - self.xmin
684 self.xrange = self.xmax - self.xmin
680
685
681 if self.throttle == 0:
686 if self.throttle == 0:
682 self.__plot()
687 self.__plot()
683 else:
688 else:
684 self.__throttle_plot(self.__plot)#, coerce=coerce)
689 self.__throttle_plot(self.__plot)#, coerce=coerce)
685
690
686 def close(self):
691 def close(self):
687
692
688 if self.data and not self.data.flagNoData:
693 if self.data and not self.data.flagNoData:
689 self.save_time = 0
694 self.save_time = 0
690 self.__plot()
695 self.__plot()
691 if self.data and not self.data.flagNoData and self.pause:
696 if self.data and not self.data.flagNoData and self.pause:
692 figpause(10)
697 figpause(10)
693
@@ -1,370 +1,381
1 import os
1 import os
2 import datetime
2 import datetime
3 import numpy
3 import numpy
4
4
5 from schainpy.model.graphics.jroplot_base import Plot, plt
5 from schainpy.model.graphics.jroplot_base import Plot, plt
6 from schainpy.model.graphics.jroplot_spectra import SpectraPlot, RTIPlot, CoherencePlot, SpectraCutPlot
6 from schainpy.model.graphics.jroplot_spectra import SpectraPlot, RTIPlot, CoherencePlot, SpectraCutPlot
7 from schainpy.utils import log
7 from schainpy.utils import log
8
8
9 EARTH_RADIUS = 6.3710e3
9 EARTH_RADIUS = 6.3710e3
10
10
11
11
12 def ll2xy(lat1, lon1, lat2, lon2):
12 def ll2xy(lat1, lon1, lat2, lon2):
13
13
14 p = 0.017453292519943295
14 p = 0.017453292519943295
15 a = 0.5 - numpy.cos((lat2 - lat1) * p)/2 + numpy.cos(lat1 * p) * \
15 a = 0.5 - numpy.cos((lat2 - lat1) * p)/2 + numpy.cos(lat1 * p) * \
16 numpy.cos(lat2 * p) * (1 - numpy.cos((lon2 - lon1) * p)) / 2
16 numpy.cos(lat2 * p) * (1 - numpy.cos((lon2 - lon1) * p)) / 2
17 r = 12742 * numpy.arcsin(numpy.sqrt(a))
17 r = 12742 * numpy.arcsin(numpy.sqrt(a))
18 theta = numpy.arctan2(numpy.sin((lon2-lon1)*p)*numpy.cos(lat2*p), numpy.cos(lat1*p)
18 theta = numpy.arctan2(numpy.sin((lon2-lon1)*p)*numpy.cos(lat2*p), numpy.cos(lat1*p)
19 * numpy.sin(lat2*p)-numpy.sin(lat1*p)*numpy.cos(lat2*p)*numpy.cos((lon2-lon1)*p))
19 * numpy.sin(lat2*p)-numpy.sin(lat1*p)*numpy.cos(lat2*p)*numpy.cos((lon2-lon1)*p))
20 theta = -theta + numpy.pi/2
20 theta = -theta + numpy.pi/2
21 return r*numpy.cos(theta), r*numpy.sin(theta)
21 return r*numpy.cos(theta), r*numpy.sin(theta)
22
22
23
23
24 def km2deg(km):
24 def km2deg(km):
25 '''
25 '''
26 Convert distance in km to degrees
26 Convert distance in km to degrees
27 '''
27 '''
28
28
29 return numpy.rad2deg(km/EARTH_RADIUS)
29 return numpy.rad2deg(km/EARTH_RADIUS)
30
30
31
31
32
32
33 class SpectralMomentsPlot(SpectraPlot):
33 class SpectralMomentsPlot(SpectraPlot):
34 '''
34 '''
35 Plot for Spectral Moments
35 Plot for Spectral Moments
36 '''
36 '''
37 CODE = 'spc_moments'
37 CODE = 'spc_moments'
38 # colormap = 'jet'
38 # colormap = 'jet'
39 # plot_type = 'pcolor'
39 # plot_type = 'pcolor'
40
40
41 class DobleGaussianPlot(SpectraPlot):
41 class DobleGaussianPlot(SpectraPlot):
42 '''
42 '''
43 Plot for Double Gaussian Plot
43 Plot for Double Gaussian Plot
44 '''
44 '''
45 CODE = 'gaussian_fit'
45 CODE = 'gaussian_fit'
46 # colormap = 'jet'
46 # colormap = 'jet'
47 # plot_type = 'pcolor'
47 # plot_type = 'pcolor'
48
48
49
49 class DoubleGaussianSpectraCutPlot(SpectraCutPlot):
50 class DoubleGaussianSpectraCutPlot(SpectraCutPlot):
50 '''
51 '''
51 Plot SpectraCut with Double Gaussian Fit
52 Plot SpectraCut with Double Gaussian Fit
52 '''
53 '''
53 CODE = 'cut_gaussian_fit'
54 CODE = 'cut_gaussian_fit'
54
55
56
57 class SpectralFitObliquePlot(SpectraPlot):
58 '''
59 Plot for Spectral Oblique
60 '''
61 CODE = 'spc_moments'
62 colormap = 'jet'
63 plot_type = 'pcolor'
64
65
66
55 class SnrPlot(RTIPlot):
67 class SnrPlot(RTIPlot):
56 '''
68 '''
57 Plot for SNR Data
69 Plot for SNR Data
58 '''
70 '''
59
71
60 CODE = 'snr'
72 CODE = 'snr'
61 colormap = 'jet'
73 colormap = 'jet'
62
74
63 def update(self, dataOut):
75 def update(self, dataOut):
64
76
65 data = {
77 data = {
66 'snr': 10*numpy.log10(dataOut.data_snr)
78 'snr': 10*numpy.log10(dataOut.data_snr)
67 }
79 }
68
80
69 return data, {}
81 return data, {}
70
82
71 class DopplerPlot(RTIPlot):
83 class DopplerPlot(RTIPlot):
72 '''
84 '''
73 Plot for DOPPLER Data (1st moment)
85 Plot for DOPPLER Data (1st moment)
74 '''
86 '''
75
87
76 CODE = 'dop'
88 CODE = 'dop'
77 colormap = 'jet'
89 colormap = 'jet'
78
90
79 def update(self, dataOut):
91 def update(self, dataOut):
80
92
81 data = {
93 data = {
82 'dop': 10*numpy.log10(dataOut.data_dop)
94 'dop': 10*numpy.log10(dataOut.data_dop)
83 }
95 }
84
96
85 return data, {}
97 return data, {}
86
98
87 class PowerPlot(RTIPlot):
99 class PowerPlot(RTIPlot):
88 '''
100 '''
89 Plot for Power Data (0 moment)
101 Plot for Power Data (0 moment)
90 '''
102 '''
91
103
92 CODE = 'pow'
104 CODE = 'pow'
93 colormap = 'jet'
105 colormap = 'jet'
94
106
95 def update(self, dataOut):
107 def update(self, dataOut):
96
108
97 data = {
109 data = {
98 'pow': 10*numpy.log10(dataOut.data_pow/dataOut.normFactor)
110 'pow': 10*numpy.log10(dataOut.data_pow/dataOut.normFactor)
99 }
111 }
100
112
101 return data, {}
113 return data, {}
102
114
103 class SpectralWidthPlot(RTIPlot):
115 class SpectralWidthPlot(RTIPlot):
104 '''
116 '''
105 Plot for Spectral Width Data (2nd moment)
117 Plot for Spectral Width Data (2nd moment)
106 '''
118 '''
107
119
108 CODE = 'width'
120 CODE = 'width'
109 colormap = 'jet'
121 colormap = 'jet'
110
122
111 def update(self, dataOut):
123 def update(self, dataOut):
112
124
113 data = {
125 data = {
114 'width': dataOut.data_width
126 'width': dataOut.data_width
115 }
127 }
116
128
117 return data, {}
129 return data, {}
118
130
119 class SkyMapPlot(Plot):
131 class SkyMapPlot(Plot):
120 '''
132 '''
121 Plot for meteors detection data
133 Plot for meteors detection data
122 '''
134 '''
123
135
124 CODE = 'param'
136 CODE = 'param'
125
137
126 def setup(self):
138 def setup(self):
127
139
128 self.ncols = 1
140 self.ncols = 1
129 self.nrows = 1
141 self.nrows = 1
130 self.width = 7.2
142 self.width = 7.2
131 self.height = 7.2
143 self.height = 7.2
132 self.nplots = 1
144 self.nplots = 1
133 self.xlabel = 'Zonal Zenith Angle (deg)'
145 self.xlabel = 'Zonal Zenith Angle (deg)'
134 self.ylabel = 'Meridional Zenith Angle (deg)'
146 self.ylabel = 'Meridional Zenith Angle (deg)'
135 self.polar = True
147 self.polar = True
136 self.ymin = -180
148 self.ymin = -180
137 self.ymax = 180
149 self.ymax = 180
138 self.colorbar = False
150 self.colorbar = False
139
151
140 def plot(self):
152 def plot(self):
141
153
142 arrayParameters = numpy.concatenate(self.data['param'])
154 arrayParameters = numpy.concatenate(self.data['param'])
143 error = arrayParameters[:, -1]
155 error = arrayParameters[:, -1]
144 indValid = numpy.where(error == 0)[0]
156 indValid = numpy.where(error == 0)[0]
145 finalMeteor = arrayParameters[indValid, :]
157 finalMeteor = arrayParameters[indValid, :]
146 finalAzimuth = finalMeteor[:, 3]
158 finalAzimuth = finalMeteor[:, 3]
147 finalZenith = finalMeteor[:, 4]
159 finalZenith = finalMeteor[:, 4]
148
160
149 x = finalAzimuth * numpy.pi / 180
161 x = finalAzimuth * numpy.pi / 180
150 y = finalZenith
162 y = finalZenith
151
163
152 ax = self.axes[0]
164 ax = self.axes[0]
153
165
154 if ax.firsttime:
166 if ax.firsttime:
155 ax.plot = ax.plot(x, y, 'bo', markersize=5)[0]
167 ax.plot = ax.plot(x, y, 'bo', markersize=5)[0]
156 else:
168 else:
157 ax.plot.set_data(x, y)
169 ax.plot.set_data(x, y)
158
170
159 dt1 = self.getDateTime(self.data.min_time).strftime('%y/%m/%d %H:%M:%S')
171 dt1 = self.getDateTime(self.data.min_time).strftime('%y/%m/%d %H:%M:%S')
160 dt2 = self.getDateTime(self.data.max_time).strftime('%y/%m/%d %H:%M:%S')
172 dt2 = self.getDateTime(self.data.max_time).strftime('%y/%m/%d %H:%M:%S')
161 title = 'Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n' % (dt1,
173 title = 'Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n' % (dt1,
162 dt2,
174 dt2,
163 len(x))
175 len(x))
164 self.titles[0] = title
176 self.titles[0] = title
165
177
166
178
167 class GenericRTIPlot(Plot):
179 class GenericRTIPlot(Plot):
168 '''
180 '''
169 Plot for data_xxxx object
181 Plot for data_xxxx object
170 '''
182 '''
171
183
172 CODE = 'param'
184 CODE = 'param'
173 colormap = 'viridis'
185 colormap = 'viridis'
174 plot_type = 'pcolorbuffer'
186 plot_type = 'pcolorbuffer'
175
187
176 def setup(self):
188 def setup(self):
177 self.xaxis = 'time'
189 self.xaxis = 'time'
178 self.ncols = 1
190 self.ncols = 1
179 self.nrows = self.data.shape('param')[0]
191 self.nrows = self.data.shape('param')[0]
180 self.nplots = self.nrows
192 self.nplots = self.nrows
181 self.plots_adjust.update({'hspace':0.8, 'left': 0.1, 'bottom': 0.08, 'right':0.95, 'top': 0.95})
193 self.plots_adjust.update({'hspace':0.8, 'left': 0.1, 'bottom': 0.08, 'right':0.95, 'top': 0.95})
182
194
183 if not self.xlabel:
195 if not self.xlabel:
184 self.xlabel = 'Time'
196 self.xlabel = 'Time'
185
197
186 self.ylabel = 'Range [km]'
198 self.ylabel = 'Range [km]'
187 if not self.titles:
199 if not self.titles:
188 self.titles = ['Param {}'.format(x) for x in range(self.nrows)]
200 self.titles = ['Param {}'.format(x) for x in range(self.nrows)]
189
201
190 def update(self, dataOut):
202 def update(self, dataOut):
191
203
192 data = {
204 data = {
193 'param' : numpy.concatenate([getattr(dataOut, attr) for attr in self.attr_data], axis=0)
205 'param' : numpy.concatenate([getattr(dataOut, attr) for attr in self.attr_data], axis=0)
194 }
206 }
195
207
196 meta = {}
208 meta = {}
197
209
198 return data, meta
210 return data, meta
199
211
200 def plot(self):
212 def plot(self):
201 # self.data.normalize_heights()
213 # self.data.normalize_heights()
202 self.x = self.data.times
214 self.x = self.data.times
203 self.y = self.data.yrange
215 self.y = self.data.yrange
204 self.z = self.data['param']
216 self.z = self.data['param']
205
217
206 self.z = numpy.ma.masked_invalid(self.z)
218 self.z = numpy.ma.masked_invalid(self.z)
207
219
208 if self.decimation is None:
220 if self.decimation is None:
209 x, y, z = self.fill_gaps(self.x, self.y, self.z)
221 x, y, z = self.fill_gaps(self.x, self.y, self.z)
210 else:
222 else:
211 x, y, z = self.fill_gaps(*self.decimate())
223 x, y, z = self.fill_gaps(*self.decimate())
212
224
213 for n, ax in enumerate(self.axes):
225 for n, ax in enumerate(self.axes):
214
226
215 self.zmax = self.zmax if self.zmax is not None else numpy.max(
227 self.zmax = self.zmax if self.zmax is not None else numpy.max(
216 self.z[n])
228 self.z[n])
217 self.zmin = self.zmin if self.zmin is not None else numpy.min(
229 self.zmin = self.zmin if self.zmin is not None else numpy.min(
218 self.z[n])
230 self.z[n])
219
231
220 if ax.firsttime:
232 if ax.firsttime:
221 if self.zlimits is not None:
233 if self.zlimits is not None:
222 self.zmin, self.zmax = self.zlimits[n]
234 self.zmin, self.zmax = self.zlimits[n]
223
235
224 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
236 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
225 vmin=self.zmin,
237 vmin=self.zmin,
226 vmax=self.zmax,
238 vmax=self.zmax,
227 cmap=self.cmaps[n]
239 cmap=self.cmaps[n]
228 )
240 )
229 else:
241 else:
230 if self.zlimits is not None:
242 if self.zlimits is not None:
231 self.zmin, self.zmax = self.zlimits[n]
243 self.zmin, self.zmax = self.zlimits[n]
232 ax.collections.remove(ax.collections[0])
244 ax.collections.remove(ax.collections[0])
233 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
245 ax.plt = ax.pcolormesh(x, y, z[n].T * self.factors[n],
234 vmin=self.zmin,
246 vmin=self.zmin,
235 vmax=self.zmax,
247 vmax=self.zmax,
236 cmap=self.cmaps[n]
248 cmap=self.cmaps[n]
237 )
249 )
238
250
239
251
240 class PolarMapPlot(Plot):
252 class PolarMapPlot(Plot):
241 '''
253 '''
242 Plot for weather radar
254 Plot for weather radar
243 '''
255 '''
244
256
245 CODE = 'param'
257 CODE = 'param'
246 colormap = 'seismic'
258 colormap = 'seismic'
247
259
248 def setup(self):
260 def setup(self):
249 self.ncols = 1
261 self.ncols = 1
250 self.nrows = 1
262 self.nrows = 1
251 self.width = 9
263 self.width = 9
252 self.height = 8
264 self.height = 8
253 self.mode = self.data.meta['mode']
265 self.mode = self.data.meta['mode']
254 if self.channels is not None:
266 if self.channels is not None:
255 self.nplots = len(self.channels)
267 self.nplots = len(self.channels)
256 self.nrows = len(self.channels)
268 self.nrows = len(self.channels)
257 else:
269 else:
258 self.nplots = self.data.shape(self.CODE)[0]
270 self.nplots = self.data.shape(self.CODE)[0]
259 self.nrows = self.nplots
271 self.nrows = self.nplots
260 self.channels = list(range(self.nplots))
272 self.channels = list(range(self.nplots))
261 if self.mode == 'E':
273 if self.mode == 'E':
262 self.xlabel = 'Longitude'
274 self.xlabel = 'Longitude'
263 self.ylabel = 'Latitude'
275 self.ylabel = 'Latitude'
264 else:
276 else:
265 self.xlabel = 'Range (km)'
277 self.xlabel = 'Range (km)'
266 self.ylabel = 'Height (km)'
278 self.ylabel = 'Height (km)'
267 self.bgcolor = 'white'
279 self.bgcolor = 'white'
268 self.cb_labels = self.data.meta['units']
280 self.cb_labels = self.data.meta['units']
269 self.lat = self.data.meta['latitude']
281 self.lat = self.data.meta['latitude']
270 self.lon = self.data.meta['longitude']
282 self.lon = self.data.meta['longitude']
271 self.xmin, self.xmax = float(
283 self.xmin, self.xmax = float(
272 km2deg(self.xmin) + self.lon), float(km2deg(self.xmax) + self.lon)
284 km2deg(self.xmin) + self.lon), float(km2deg(self.xmax) + self.lon)
273 self.ymin, self.ymax = float(
285 self.ymin, self.ymax = float(
274 km2deg(self.ymin) + self.lat), float(km2deg(self.ymax) + self.lat)
286 km2deg(self.ymin) + self.lat), float(km2deg(self.ymax) + self.lat)
275 # self.polar = True
287 # self.polar = True
276
288
277 def plot(self):
289 def plot(self):
278
290
279 for n, ax in enumerate(self.axes):
291 for n, ax in enumerate(self.axes):
280 data = self.data['param'][self.channels[n]]
292 data = self.data['param'][self.channels[n]]
281
293
282 zeniths = numpy.linspace(
294 zeniths = numpy.linspace(
283 0, self.data.meta['max_range'], data.shape[1])
295 0, self.data.meta['max_range'], data.shape[1])
284 if self.mode == 'E':
296 if self.mode == 'E':
285 azimuths = -numpy.radians(self.data.yrange)+numpy.pi/2
297 azimuths = -numpy.radians(self.data.yrange)+numpy.pi/2
286 r, theta = numpy.meshgrid(zeniths, azimuths)
298 r, theta = numpy.meshgrid(zeniths, azimuths)
287 x, y = r*numpy.cos(theta)*numpy.cos(numpy.radians(self.data.meta['elevation'])), r*numpy.sin(
299 x, y = r*numpy.cos(theta)*numpy.cos(numpy.radians(self.data.meta['elevation'])), r*numpy.sin(
288 theta)*numpy.cos(numpy.radians(self.data.meta['elevation']))
300 theta)*numpy.cos(numpy.radians(self.data.meta['elevation']))
289 x = km2deg(x) + self.lon
301 x = km2deg(x) + self.lon
290 y = km2deg(y) + self.lat
302 y = km2deg(y) + self.lat
291 else:
303 else:
292 azimuths = numpy.radians(self.data.yrange)
304 azimuths = numpy.radians(self.data.yrange)
293 r, theta = numpy.meshgrid(zeniths, azimuths)
305 r, theta = numpy.meshgrid(zeniths, azimuths)
294 x, y = r*numpy.cos(theta), r*numpy.sin(theta)
306 x, y = r*numpy.cos(theta), r*numpy.sin(theta)
295 self.y = zeniths
307 self.y = zeniths
296
308
297 if ax.firsttime:
309 if ax.firsttime:
298 if self.zlimits is not None:
310 if self.zlimits is not None:
299 self.zmin, self.zmax = self.zlimits[n]
311 self.zmin, self.zmax = self.zlimits[n]
300 ax.plt = ax.pcolormesh( # r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
312 ax.plt = ax.pcolormesh( # r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
301 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
313 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
302 vmin=self.zmin,
314 vmin=self.zmin,
303 vmax=self.zmax,
315 vmax=self.zmax,
304 cmap=self.cmaps[n])
316 cmap=self.cmaps[n])
305 else:
317 else:
306 if self.zlimits is not None:
318 if self.zlimits is not None:
307 self.zmin, self.zmax = self.zlimits[n]
319 self.zmin, self.zmax = self.zlimits[n]
308 ax.collections.remove(ax.collections[0])
320 ax.collections.remove(ax.collections[0])
309 ax.plt = ax.pcolormesh( # r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
321 ax.plt = ax.pcolormesh( # r, theta, numpy.ma.array(data, mask=numpy.isnan(data)),
310 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
322 x, y, numpy.ma.array(data, mask=numpy.isnan(data)),
311 vmin=self.zmin,
323 vmin=self.zmin,
312 vmax=self.zmax,
324 vmax=self.zmax,
313 cmap=self.cmaps[n])
325 cmap=self.cmaps[n])
314
326
315 if self.mode == 'A':
327 if self.mode == 'A':
316 continue
328 continue
317
329
318 # plot district names
330 # plot district names
319 f = open('/data/workspace/schain_scripts/distrito.csv')
331 f = open('/data/workspace/schain_scripts/distrito.csv')
320 for line in f:
332 for line in f:
321 label, lon, lat = [s.strip() for s in line.split(',') if s]
333 label, lon, lat = [s.strip() for s in line.split(',') if s]
322 lat = float(lat)
334 lat = float(lat)
323 lon = float(lon)
335 lon = float(lon)
324 # ax.plot(lon, lat, '.b', ms=2)
336 # ax.plot(lon, lat, '.b', ms=2)
325 ax.text(lon, lat, label.decode('utf8'), ha='center',
337 ax.text(lon, lat, label.decode('utf8'), ha='center',
326 va='bottom', size='8', color='black')
338 va='bottom', size='8', color='black')
327
339
328 # plot limites
340 # plot limites
329 limites = []
341 limites = []
330 tmp = []
342 tmp = []
331 for line in open('/data/workspace/schain_scripts/lima.csv'):
343 for line in open('/data/workspace/schain_scripts/lima.csv'):
332 if '#' in line:
344 if '#' in line:
333 if tmp:
345 if tmp:
334 limites.append(tmp)
346 limites.append(tmp)
335 tmp = []
347 tmp = []
336 continue
348 continue
337 values = line.strip().split(',')
349 values = line.strip().split(',')
338 tmp.append((float(values[0]), float(values[1])))
350 tmp.append((float(values[0]), float(values[1])))
339 for points in limites:
351 for points in limites:
340 ax.add_patch(
352 ax.add_patch(
341 Polygon(points, ec='k', fc='none', ls='--', lw=0.5))
353 Polygon(points, ec='k', fc='none', ls='--', lw=0.5))
342
354
343 # plot Cuencas
355 # plot Cuencas
344 for cuenca in ('rimac', 'lurin', 'mala', 'chillon', 'chilca', 'chancay-huaral'):
356 for cuenca in ('rimac', 'lurin', 'mala', 'chillon', 'chilca', 'chancay-huaral'):
345 f = open('/data/workspace/schain_scripts/{}.csv'.format(cuenca))
357 f = open('/data/workspace/schain_scripts/{}.csv'.format(cuenca))
346 values = [line.strip().split(',') for line in f]
358 values = [line.strip().split(',') for line in f]
347 points = [(float(s[0]), float(s[1])) for s in values]
359 points = [(float(s[0]), float(s[1])) for s in values]
348 ax.add_patch(Polygon(points, ec='b', fc='none'))
360 ax.add_patch(Polygon(points, ec='b', fc='none'))
349
361
350 # plot grid
362 # plot grid
351 for r in (15, 30, 45, 60):
363 for r in (15, 30, 45, 60):
352 ax.add_artist(plt.Circle((self.lon, self.lat),
364 ax.add_artist(plt.Circle((self.lon, self.lat),
353 km2deg(r), color='0.6', fill=False, lw=0.2))
365 km2deg(r), color='0.6', fill=False, lw=0.2))
354 ax.text(
366 ax.text(
355 self.lon + (km2deg(r))*numpy.cos(60*numpy.pi/180),
367 self.lon + (km2deg(r))*numpy.cos(60*numpy.pi/180),
356 self.lat + (km2deg(r))*numpy.sin(60*numpy.pi/180),
368 self.lat + (km2deg(r))*numpy.sin(60*numpy.pi/180),
357 '{}km'.format(r),
369 '{}km'.format(r),
358 ha='center', va='bottom', size='8', color='0.6', weight='heavy')
370 ha='center', va='bottom', size='8', color='0.6', weight='heavy')
359
371
360 if self.mode == 'E':
372 if self.mode == 'E':
361 title = 'El={}$^\circ$'.format(self.data.meta['elevation'])
373 title = 'El={}$^\circ$'.format(self.data.meta['elevation'])
362 label = 'E{:02d}'.format(int(self.data.meta['elevation']))
374 label = 'E{:02d}'.format(int(self.data.meta['elevation']))
363 else:
375 else:
364 title = 'Az={}$^\circ$'.format(self.data.meta['azimuth'])
376 title = 'Az={}$^\circ$'.format(self.data.meta['azimuth'])
365 label = 'A{:02d}'.format(int(self.data.meta['azimuth']))
377 label = 'A{:02d}'.format(int(self.data.meta['azimuth']))
366
378
367 self.save_labels = ['{}-{}'.format(lbl, label) for lbl in self.labels]
379 self.save_labels = ['{}-{}'.format(lbl, label) for lbl in self.labels]
368 self.titles = ['{} {}'.format(
380 self.titles = ['{} {}'.format(
369 self.data.parameters[x], title) for x in self.channels]
381 self.data.parameters[x], title) for x in self.channels]
370
This diff has been collapsed as it changes many lines, (535 lines changed) Show them Hide them
@@ -1,743 +1,1254
1 # Copyright (c) 2012-2021 Jicamarca Radio Observatory
1 # Copyright (c) 2012-2021 Jicamarca Radio Observatory
2 # All rights reserved.
2 # All rights reserved.
3 #
3 #
4 # Distributed under the terms of the BSD 3-clause license.
4 # Distributed under the terms of the BSD 3-clause license.
5 """Classes to plot Spectra data
5 """Classes to plot Spectra data
6
6
7 """
7 """
8
8
9 import os
9 import os
10 import numpy
10 import numpy
11
11
12 from schainpy.model.graphics.jroplot_base import Plot, plt, log
12 from schainpy.model.graphics.jroplot_base import Plot, plt, log
13
13
14
14
15 class SpectraPlot(Plot):
15 class SpectraPlot(Plot):
16 '''
16 '''
17 Plot for Spectra data
17 Plot for Spectra data
18 '''
18 '''
19
19
20 CODE = 'spc'
20 CODE = 'spc'
21 colormap = 'jet'
21 colormap = 'jet'
22 plot_type = 'pcolor'
22 plot_type = 'pcolor'
23 buffering = False
23 buffering = False
24
24
25 def setup(self):
25 def setup(self):
26
26 self.nplots = len(self.data.channels)
27 self.nplots = len(self.data.channels)
27 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
28 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
28 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
29 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
29 self.height = 2.6 * self.nrows
30 self.height = 2.6 * self.nrows
30 self.cb_label = 'dB'
31 self.cb_label = 'dB'
31 if self.showprofile:
32 if self.showprofile:
32 self.width = 4 * self.ncols
33 self.width = 4 * self.ncols
33 else:
34 else:
34 self.width = 3.5 * self.ncols
35 self.width = 3.5 * self.ncols
35 self.plots_adjust.update({'wspace': 0.4, 'hspace':0.4, 'left': 0.1, 'right': 0.9, 'bottom': 0.08})
36 self.plots_adjust.update({'wspace': 0.8, 'hspace':0.2, 'left': 0.2, 'right': 0.9, 'bottom': 0.18})
36 self.ylabel = 'Range [km]'
37 self.ylabel = 'Range [km]'
37
38
38 def update(self, dataOut):
39 def update(self, dataOut):
39
40
40 data = {}
41 data = {}
41 meta = {}
42 meta = {}
42 spc = 10*numpy.log10(dataOut.data_spc/dataOut.normFactor)
43 spc = 10*numpy.log10(dataOut.data_spc/dataOut.normFactor)
43 data['spc'] = spc
44 data['spc'] = spc
44 data['rti'] = dataOut.getPower()
45 data['rti'] = dataOut.getPower()
45 data['noise'] = 10*numpy.log10(dataOut.getNoise()/dataOut.normFactor)
46 data['noise'] = 10*numpy.log10(dataOut.getNoise()/dataOut.normFactor)
46 meta['xrange'] = (dataOut.getFreqRange(1)/1000., dataOut.getAcfRange(1), dataOut.getVelRange(1))
47 meta['xrange'] = (dataOut.getFreqRange(1)/1000., dataOut.getAcfRange(1), dataOut.getVelRange(1))
47
48
48 if self.CODE == 'spc_moments':
49 if self.CODE == 'spc_moments':
49 data['moments'] = dataOut.moments
50 data['moments'] = dataOut.moments
50 # data['spc'] = 10*numpy.log10(dataOut.data_pre[0]/dataOut.normFactor)
51 # data['spc'] = 10*numpy.log10(dataOut.data_pre[0]/dataOut.normFactor)
51 if self.CODE == 'gaussian_fit':
52 if self.CODE == 'gaussian_fit':
52 # data['moments'] = dataOut.moments
53 # data['moments'] = dataOut.moments
53 data['gaussfit'] = dataOut.DGauFitParams
54 data['gaussfit'] = dataOut.DGauFitParams
54 # data['spc'] = 10*numpy.log10(dataOut.data_pre[0]/dataOut.normFactor)
55 # data['spc'] = 10*numpy.log10(dataOut.data_pre[0]/dataOut.normFactor)
55
56
56 return data, meta
57 return data, meta
57
58
58 def plot(self):
59 def plot(self):
60
61 #print(self.xaxis)
62 #exit(1)
59 if self.xaxis == "frequency":
63 if self.xaxis == "frequency":
60 x = self.data.xrange[0]
64 x = self.data.xrange[0]
61 self.xlabel = "Frequency (kHz)"
65 self.xlabel = "Frequency (kHz)"
62 elif self.xaxis == "time":
66 elif self.xaxis == "time":
63 x = self.data.xrange[1]
67 x = self.data.xrange[1]
64 self.xlabel = "Time (ms)"
68 self.xlabel = "Time (ms)"
65 else:
69 else:
66 x = self.data.xrange[2]
70 x = self.data.xrange[2]
67 self.xlabel = "Velocity (m/s)"
71 self.xlabel = "Velocity (m/s)"
68
72
69 if (self.CODE == 'spc_moments') | (self.CODE == 'gaussian_fit'):
73 if (self.CODE == 'spc_moments') | (self.CODE == 'gaussian_fit'):
70 x = self.data.xrange[2]
74 x = self.data.xrange[2]
71 self.xlabel = "Velocity (m/s)"
75 self.xlabel = "Velocity (m/s)"
72
76
73 self.titles = []
77 self.titles = []
74
78
75 y = self.data.yrange
79 y = self.data.yrange
76 self.y = y
80 self.y = y
77
81
78 data = self.data[-1]
82 data = self.data[-1]
79 z = data['spc']
83 z = data['spc']
80
84
85 self.CODE2 = 'spc_oblique'
86
87
81 for n, ax in enumerate(self.axes):
88 for n, ax in enumerate(self.axes):
82 noise = data['noise'][n]
89 noise = data['noise'][n]
83 if self.CODE == 'spc_moments':
90 if self.CODE == 'spc_moments':
84 mean = data['moments'][n, 1]
91 mean = data['moments'][n, 1]
85 if self.CODE == 'gaussian_fit':
92 if self.CODE == 'gaussian_fit':
86 # mean = data['moments'][n, 1]
93 # mean = data['moments'][n, 1]
87 gau0 = data['gaussfit'][n][2,:,0]
94 gau0 = data['gaussfit'][n][2,:,0]
88 gau1 = data['gaussfit'][n][2,:,1]
95 gau1 = data['gaussfit'][n][2,:,1]
89 if ax.firsttime:
96 if ax.firsttime:
90 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
97 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
91 self.xmin = self.xmin if self.xmin else -self.xmax
98 self.xmin = self.xmin if self.xmin else -self.xmax
92 self.zmin = self.zmin if self.zmin else numpy.nanmin(z)
99 self.zmin = self.zmin if self.zmin else numpy.nanmin(z)
93 self.zmax = self.zmax if self.zmax else numpy.nanmax(z)
100 self.zmax = self.zmax if self.zmax else numpy.nanmax(z)
101 #print(numpy.shape(x))
94 ax.plt = ax.pcolormesh(x, y, z[n].T,
102 ax.plt = ax.pcolormesh(x, y, z[n].T,
95 vmin=self.zmin,
103 vmin=self.zmin,
96 vmax=self.zmax,
104 vmax=self.zmax,
97 cmap=plt.get_cmap(self.colormap)
105 cmap=plt.get_cmap(self.colormap)
98 )
106 )
99
107
100 if self.showprofile:
108 if self.showprofile:
101 ax.plt_profile = self.pf_axes[n].plot(
109 ax.plt_profile = self.pf_axes[n].plot(
102 data['rti'][n], y)[0]
110 data['rti'][n], y)[0]
103 ax.plt_noise = self.pf_axes[n].plot(numpy.repeat(noise, len(y)), y,
111 ax.plt_noise = self.pf_axes[n].plot(numpy.repeat(noise, len(y)), y,
104 color="k", linestyle="dashed", lw=1)[0]
112 color="k", linestyle="dashed", lw=1)[0]
105 if self.CODE == 'spc_moments':
113 if self.CODE == 'spc_moments':
106 ax.plt_mean = ax.plot(mean, y, color='k', lw=1)[0]
114 ax.plt_mean = ax.plot(mean, y, color='k', lw=1)[0]
107 if self.CODE == 'gaussian_fit':
115 if self.CODE == 'gaussian_fit':
108 # ax.plt_mean = ax.plot(mean, y, color='k', lw=1)[0]
109 ax.plt_gau0 = ax.plot(gau0, y, color='r', lw=1)[0]
116 ax.plt_gau0 = ax.plot(gau0, y, color='r', lw=1)[0]
110 ax.plt_gau1 = ax.plot(gau1, y, color='y', lw=1)[0]
117 ax.plt_gau1 = ax.plot(gau1, y, color='y', lw=1)[0]
111 else:
118 else:
112 ax.plt.set_array(z[n].T.ravel())
119 ax.plt.set_array(z[n].T.ravel())
113 if self.showprofile:
120 if self.showprofile:
114 ax.plt_profile.set_data(data['rti'][n], y)
121 ax.plt_profile.set_data(data['rti'][n], y)
115 ax.plt_noise.set_data(numpy.repeat(noise, len(y)), y)
122 ax.plt_noise.set_data(numpy.repeat(noise, len(y)), y)
116 if self.CODE == 'spc_moments':
123 if self.CODE == 'spc_moments':
117 ax.plt_mean.set_data(mean, y)
124 ax.plt_mean.set_data(mean, y)
118 if self.CODE == 'gaussian_fit':
125 if self.CODE == 'gaussian_fit':
119 # ax.plt_mean.set_data(mean, y)
120 ax.plt_gau0.set_data(gau0, y)
126 ax.plt_gau0.set_data(gau0, y)
121 ax.plt_gau1.set_data(gau1, y)
127 ax.plt_gau1.set_data(gau1, y)
122 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
128 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
123
129
130 class SpectraObliquePlot(Plot):
131 '''
132 Plot for Spectra data
133 '''
134
135 CODE = 'spc'
136 colormap = 'jet'
137 plot_type = 'pcolor'
138
139 def setup(self):
140 self.xaxis = "oblique"
141 self.nplots = len(self.data.channels)
142 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
143 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
144 self.height = 2.6 * self.nrows
145 self.cb_label = 'dB'
146 if self.showprofile:
147 self.width = 4 * self.ncols
148 else:
149 self.width = 3.5 * self.ncols
150 self.plots_adjust.update({'wspace': 0.8, 'hspace':0.2, 'left': 0.2, 'right': 0.9, 'bottom': 0.18})
151 self.ylabel = 'Range [km]'
152
153 def plot(self):
154
155 #print(self.xaxis)
156 #exit(1)
157 if self.xaxis == "frequency":
158 x = self.data.xrange[0]
159 self.xlabel = "Frequency (kHz)"
160 elif self.xaxis == "time":
161 x = self.data.xrange[1]
162 self.xlabel = "Time (ms)"
163 else:
164 x = self.data.xrange[2]
165 self.xlabel = "Velocity (m/s)"
166
167 if self.CODE == 'spc_moments':
168 x = self.data.xrange[2]
169 self.xlabel = "Velocity (m/s)"
170
171 self.titles = []
172 #self.xlabel = "Velocidad (m/s)"
173 #self.ylabel = 'Rango (km)'
174
175
176 y = self.data.heights
177 self.y = y
178 z = self.data['spc']
179
180 self.CODE2 = 'spc_oblique'
181
182
183 for n, ax in enumerate(self.axes):
184 noise = self.data['noise'][n][-1]
185 if self.CODE == 'spc_moments':
186 mean = self.data['moments'][n, :, 1, :][-1]
187 if self.CODE2 == 'spc_oblique':
188 shift1 = self.data.shift1
189 shift2 = self.data.shift2
190 if ax.firsttime:
191 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
192 self.xmin = self.xmin if self.xmin else -self.xmax
193 self.zmin = self.zmin if self.zmin else numpy.nanmin(z)
194 self.zmax = self.zmax if self.zmax else numpy.nanmax(z)
195 #print(numpy.shape(x))
196 ax.plt = ax.pcolormesh(x, y, z[n].T,
197 vmin=self.zmin,
198 vmax=self.zmax,
199 cmap=plt.get_cmap(self.colormap)
200 )
201
202 if self.showprofile:
203 ax.plt_profile = self.pf_axes[n].plot(
204 self.data['rti'][n][-1], y)[0]
205 ax.plt_noise = self.pf_axes[n].plot(numpy.repeat(noise, len(y)), y,
206 color="k", linestyle="dashed", lw=1)[0]
207 if self.CODE == 'spc_moments':
208 ax.plt_mean = ax.plot(mean, y, color='k')[0]
209
210 if self.CODE2 == 'spc_oblique':
211 #ax.plt_shift1 = ax.plot(shift1, y, color='k', marker='x', linestyle='None', markersize=0.5)[0]
212 #ax.plt_shift2 = ax.plot(shift2, y, color='m', marker='x', linestyle='None', markersize=0.5)[0]
213 self.ploterr1 = ax.errorbar(shift1, y, xerr=self.data.shift1_error,fmt='k^',elinewidth=0.2,marker='x',linestyle='None',markersize=0.5,capsize=0.3,markeredgewidth=0.2)
214 self.ploterr2 = ax.errorbar(shift2, y, xerr=self.data.shift2_error,fmt='m^',elinewidth=0.2,marker='x',linestyle='None',markersize=0.5,capsize=0.3,markeredgewidth=0.2)
215
216 else:
217 self.ploterr1.remove()
218 self.ploterr2.remove()
219 ax.plt.set_array(z[n].T.ravel())
220 if self.showprofile:
221 ax.plt_profile.set_data(self.data['rti'][n][-1], y)
222 ax.plt_noise.set_data(numpy.repeat(noise, len(y)), y)
223 if self.CODE == 'spc_moments':
224 ax.plt_mean.set_data(mean, y)
225 if self.CODE2 == 'spc_oblique':
226 #ax.plt_shift1.set_data(shift1, y)
227 #ax.plt_shift2.set_data(shift2, y)
228 #ax.clf()
229 self.ploterr1 = ax.errorbar(shift1, y, xerr=self.data.shift1_error,fmt='k^',elinewidth=0.2,marker='x',linestyle='None',markersize=0.5,capsize=0.3,markeredgewidth=0.2)
230 self.ploterr2 = ax.errorbar(shift2, y, xerr=self.data.shift2_error,fmt='m^',elinewidth=0.2,marker='x',linestyle='None',markersize=0.5,capsize=0.3,markeredgewidth=0.2)
231
232 self.titles.append('CH {}: {:3.2f}dB'.format(n, noise))
233 #self.titles.append('{}'.format('Velocidad Doppler'))
124
234
125 class CrossSpectraPlot(Plot):
235 class CrossSpectraPlot(Plot):
126
236
127 CODE = 'cspc'
237 CODE = 'cspc'
128 colormap = 'jet'
238 colormap = 'jet'
129 plot_type = 'pcolor'
239 plot_type = 'pcolor'
130 zmin_coh = None
240 zmin_coh = None
131 zmax_coh = None
241 zmax_coh = None
132 zmin_phase = None
242 zmin_phase = None
133 zmax_phase = None
243 zmax_phase = None
134
244
135 def setup(self):
245 def setup(self):
136
246
137 self.ncols = 4
247 self.ncols = 4
138 self.nplots = len(self.data.pairs) * 2
248 self.nplots = len(self.data.pairs) * 2
139 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
249 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
140 self.width = 3.1 * self.ncols
250 self.width = 3.1 * self.ncols
141 self.height = 2.6 * self.nrows
251 self.height = 5 * self.nrows
142 self.ylabel = 'Range [km]'
252 self.ylabel = 'Range [km]'
143 self.showprofile = False
253 self.showprofile = False
144 self.plots_adjust.update({'left': 0.08, 'right': 0.92, 'wspace': 0.5, 'hspace':0.4, 'top':0.95, 'bottom': 0.08})
254 self.plots_adjust.update({'left': 0.08, 'right': 0.92, 'wspace': 0.5, 'hspace':0.4, 'top':0.95, 'bottom': 0.08})
145
255
146 def update(self, dataOut):
256 def update(self, dataOut):
147
257
148 data = {}
258 data = {}
149 meta = {}
259 meta = {}
150
260
151 spc = dataOut.data_spc
261 spc = dataOut.data_spc
152 cspc = dataOut.data_cspc
262 cspc = dataOut.data_cspc
153 meta['xrange'] = (dataOut.getFreqRange(1)/1000., dataOut.getAcfRange(1), dataOut.getVelRange(1))
263 meta['xrange'] = (dataOut.getFreqRange(1)/1000., dataOut.getAcfRange(1), dataOut.getVelRange(1))
154 meta['pairs'] = dataOut.pairsList
264 meta['pairs'] = dataOut.pairsList
155
265
156 tmp = []
266 tmp = []
157
267
158 for n, pair in enumerate(meta['pairs']):
268 for n, pair in enumerate(meta['pairs']):
159 out = cspc[n] / numpy.sqrt(spc[pair[0]] * spc[pair[1]])
269 out = cspc[n] / numpy.sqrt(spc[pair[0]] * spc[pair[1]])
160 coh = numpy.abs(out)
270 coh = numpy.abs(out)
161 phase = numpy.arctan2(out.imag, out.real) * 180 / numpy.pi
271 phase = numpy.arctan2(out.imag, out.real) * 180 / numpy.pi
162 tmp.append(coh)
272 tmp.append(coh)
163 tmp.append(phase)
273 tmp.append(phase)
164
274
165 data['cspc'] = numpy.array(tmp)
275 data['cspc'] = numpy.array(tmp)
166
276
167 return data, meta
277 return data, meta
168
278
169 def plot(self):
279 def plot(self):
170
280
171 if self.xaxis == "frequency":
281 if self.xaxis == "frequency":
172 x = self.data.xrange[0]
282 x = self.data.xrange[0]
173 self.xlabel = "Frequency (kHz)"
283 self.xlabel = "Frequency (kHz)"
174 elif self.xaxis == "time":
284 elif self.xaxis == "time":
175 x = self.data.xrange[1]
285 x = self.data.xrange[1]
176 self.xlabel = "Time (ms)"
286 self.xlabel = "Time (ms)"
177 else:
287 else:
178 x = self.data.xrange[2]
288 x = self.data.xrange[2]
179 self.xlabel = "Velocity (m/s)"
289 self.xlabel = "Velocity (m/s)"
180
290
181 self.titles = []
291 self.titles = []
182
292
183 y = self.data.yrange
293 y = self.data.yrange
184 self.y = y
294 self.y = y
185
295
186 data = self.data[-1]
296 data = self.data[-1]
187 cspc = data['cspc']
297 cspc = data['cspc']
188
298
189 for n in range(len(self.data.pairs)):
299 for n in range(len(self.data.pairs)):
190 pair = self.data.pairs[n]
300 pair = self.data.pairs[n]
191 coh = cspc[n*2]
301 coh = cspc[n*2]
192 phase = cspc[n*2+1]
302 phase = cspc[n*2+1]
193 ax = self.axes[2 * n]
303 ax = self.axes[2 * n]
194 if ax.firsttime:
304 if ax.firsttime:
195 ax.plt = ax.pcolormesh(x, y, coh.T,
305 ax.plt = ax.pcolormesh(x, y, coh.T,
196 vmin=0,
306 vmin=0,
197 vmax=1,
307 vmax=1,
198 cmap=plt.get_cmap(self.colormap_coh)
308 cmap=plt.get_cmap(self.colormap_coh)
199 )
309 )
200 else:
310 else:
201 ax.plt.set_array(coh.T.ravel())
311 ax.plt.set_array(coh.T.ravel())
202 self.titles.append(
312 self.titles.append(
203 'Coherence Ch{} * Ch{}'.format(pair[0], pair[1]))
313 'Coherence Ch{} * Ch{}'.format(pair[0], pair[1]))
204
314
205 ax = self.axes[2 * n + 1]
315 ax = self.axes[2 * n + 1]
206 if ax.firsttime:
316 if ax.firsttime:
207 ax.plt = ax.pcolormesh(x, y, phase.T,
317 ax.plt = ax.pcolormesh(x, y, phase.T,
208 vmin=-180,
318 vmin=-180,
209 vmax=180,
319 vmax=180,
210 cmap=plt.get_cmap(self.colormap_phase)
320 cmap=plt.get_cmap(self.colormap_phase)
211 )
321 )
212 else:
322 else:
213 ax.plt.set_array(phase.T.ravel())
323 ax.plt.set_array(phase.T.ravel())
214 self.titles.append('Phase CH{} * CH{}'.format(pair[0], pair[1]))
324 self.titles.append('Phase CH{} * CH{}'.format(pair[0], pair[1]))
215
325
216
326
327 class CrossSpectra4Plot(Plot):
328
329 CODE = 'cspc'
330 colormap = 'jet'
331 plot_type = 'pcolor'
332 zmin_coh = None
333 zmax_coh = None
334 zmin_phase = None
335 zmax_phase = None
336
337 def setup(self):
338
339 self.ncols = 4
340 self.nrows = len(self.data.pairs)
341 self.nplots = self.nrows * 4
342 self.width = 3.1 * self.ncols
343 self.height = 5 * self.nrows
344 self.ylabel = 'Range [km]'
345 self.showprofile = False
346 self.plots_adjust.update({'left': 0.08, 'right': 0.92, 'wspace': 0.5, 'hspace':0.4, 'top':0.95, 'bottom': 0.08})
347
348 def plot(self):
349
350 if self.xaxis == "frequency":
351 x = self.data.xrange[0]
352 self.xlabel = "Frequency (kHz)"
353 elif self.xaxis == "time":
354 x = self.data.xrange[1]
355 self.xlabel = "Time (ms)"
356 else:
357 x = self.data.xrange[2]
358 self.xlabel = "Velocity (m/s)"
359
360 self.titles = []
361
362
363 y = self.data.heights
364 self.y = y
365 nspc = self.data['spc']
366 #print(numpy.shape(self.data['spc']))
367 spc = self.data['cspc'][0]
368 #print(numpy.shape(nspc))
369 #exit()
370 #nspc[1,:,:] = numpy.flip(nspc[1,:,:],axis=0)
371 #print(numpy.shape(spc))
372 #exit()
373 cspc = self.data['cspc'][1]
374
375 #xflip=numpy.flip(x)
376 #print(numpy.shape(cspc))
377 #exit()
378
379 for n in range(self.nrows):
380 noise = self.data['noise'][:,-1]
381 pair = self.data.pairs[n]
382 #print(pair)
383 #exit()
384 ax = self.axes[4 * n]
385 if ax.firsttime:
386 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
387 self.xmin = self.xmin if self.xmin else -self.xmax
388 self.zmin = self.zmin if self.zmin else numpy.nanmin(nspc)
389 self.zmax = self.zmax if self.zmax else numpy.nanmax(nspc)
390 ax.plt = ax.pcolormesh(x , y , nspc[pair[0]].T,
391 vmin=self.zmin,
392 vmax=self.zmax,
393 cmap=plt.get_cmap(self.colormap)
394 )
395 else:
396 #print(numpy.shape(nspc[pair[0]].T))
397 #exit()
398 ax.plt.set_array(nspc[pair[0]].T.ravel())
399 self.titles.append('CH {}: {:3.2f}dB'.format(pair[0], noise[pair[0]]))
400
401 ax = self.axes[4 * n + 1]
402
403 if ax.firsttime:
404 ax.plt = ax.pcolormesh(x , y, numpy.flip(nspc[pair[1]],axis=0).T,
405 vmin=self.zmin,
406 vmax=self.zmax,
407 cmap=plt.get_cmap(self.colormap)
408 )
409 else:
410
411 ax.plt.set_array(numpy.flip(nspc[pair[1]],axis=0).T.ravel())
412 self.titles.append('CH {}: {:3.2f}dB'.format(pair[1], noise[pair[1]]))
413
414 out = cspc[n] / numpy.sqrt(spc[pair[0]] * spc[pair[1]])
415 coh = numpy.abs(out)
416 phase = numpy.arctan2(out.imag, out.real) * 180 / numpy.pi
417
418 ax = self.axes[4 * n + 2]
419 if ax.firsttime:
420 ax.plt = ax.pcolormesh(x, y, numpy.flip(coh,axis=0).T,
421 vmin=0,
422 vmax=1,
423 cmap=plt.get_cmap(self.colormap_coh)
424 )
425 else:
426 ax.plt.set_array(numpy.flip(coh,axis=0).T.ravel())
427 self.titles.append(
428 'Coherence Ch{} * Ch{}'.format(pair[0], pair[1]))
429
430 ax = self.axes[4 * n + 3]
431 if ax.firsttime:
432 ax.plt = ax.pcolormesh(x, y, numpy.flip(phase,axis=0).T,
433 vmin=-180,
434 vmax=180,
435 cmap=plt.get_cmap(self.colormap_phase)
436 )
437 else:
438 ax.plt.set_array(numpy.flip(phase,axis=0).T.ravel())
439 self.titles.append('Phase CH{} * CH{}'.format(pair[0], pair[1]))
440
441
442 class CrossSpectra2Plot(Plot):
443
444 CODE = 'cspc'
445 colormap = 'jet'
446 plot_type = 'pcolor'
447 zmin_coh = None
448 zmax_coh = None
449 zmin_phase = None
450 zmax_phase = None
451
452 def setup(self):
453
454 self.ncols = 1
455 self.nrows = len(self.data.pairs)
456 self.nplots = self.nrows * 1
457 self.width = 3.1 * self.ncols
458 self.height = 5 * self.nrows
459 self.ylabel = 'Range [km]'
460 self.showprofile = False
461 self.plots_adjust.update({'left': 0.22, 'right': .90, 'wspace': 0.5, 'hspace':0.4, 'top':0.95, 'bottom': 0.08})
462
463 def plot(self):
464
465 if self.xaxis == "frequency":
466 x = self.data.xrange[0]
467 self.xlabel = "Frequency (kHz)"
468 elif self.xaxis == "time":
469 x = self.data.xrange[1]
470 self.xlabel = "Time (ms)"
471 else:
472 x = self.data.xrange[2]
473 self.xlabel = "Velocity (m/s)"
474
475 self.titles = []
476
477
478 y = self.data.heights
479 self.y = y
480 #nspc = self.data['spc']
481 #print(numpy.shape(self.data['spc']))
482 #spc = self.data['cspc'][0]
483 #print(numpy.shape(spc))
484 #exit()
485 cspc = self.data['cspc'][1]
486 #print(numpy.shape(cspc))
487 #exit()
488
489 for n in range(self.nrows):
490 noise = self.data['noise'][:,-1]
491 pair = self.data.pairs[n]
492 #print(pair) #exit()
493
494
495
496 out = cspc[n]# / numpy.sqrt(spc[pair[0]] * spc[pair[1]])
497
498 #print(out[:,53])
499 #exit()
500 cross = numpy.abs(out)
501 z = cross/self.data.nFactor
502 #print("here")
503 #print(dataOut.data_spc[0,0,0])
504 #exit()
505
506 cross = 10*numpy.log10(z)
507 #print(numpy.shape(cross))
508 #print(cross[0,:])
509 #print(self.data.nFactor)
510 #exit()
511 #phase = numpy.arctan2(out.imag, out.real) * 180 / numpy.pi
512
513 ax = self.axes[1 * n]
514 if ax.firsttime:
515 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
516 self.xmin = self.xmin if self.xmin else -self.xmax
517 self.zmin = self.zmin if self.zmin else numpy.nanmin(cross)
518 self.zmax = self.zmax if self.zmax else numpy.nanmax(cross)
519 ax.plt = ax.pcolormesh(x, y, cross.T,
520 vmin=self.zmin,
521 vmax=self.zmax,
522 cmap=plt.get_cmap(self.colormap)
523 )
524 else:
525 ax.plt.set_array(cross.T.ravel())
526 self.titles.append(
527 'Cross Spectra Power Ch{} * Ch{}'.format(pair[0], pair[1]))
528
529
530 class CrossSpectra3Plot(Plot):
531
532 CODE = 'cspc'
533 colormap = 'jet'
534 plot_type = 'pcolor'
535 zmin_coh = None
536 zmax_coh = None
537 zmin_phase = None
538 zmax_phase = None
539
540 def setup(self):
541
542 self.ncols = 3
543 self.nrows = len(self.data.pairs)
544 self.nplots = self.nrows * 3
545 self.width = 3.1 * self.ncols
546 self.height = 5 * self.nrows
547 self.ylabel = 'Range [km]'
548 self.showprofile = False
549 self.plots_adjust.update({'left': 0.22, 'right': .90, 'wspace': 0.5, 'hspace':0.4, 'top':0.95, 'bottom': 0.08})
550
551 def plot(self):
552
553 if self.xaxis == "frequency":
554 x = self.data.xrange[0]
555 self.xlabel = "Frequency (kHz)"
556 elif self.xaxis == "time":
557 x = self.data.xrange[1]
558 self.xlabel = "Time (ms)"
559 else:
560 x = self.data.xrange[2]
561 self.xlabel = "Velocity (m/s)"
562
563 self.titles = []
564
565
566 y = self.data.heights
567 self.y = y
568 #nspc = self.data['spc']
569 #print(numpy.shape(self.data['spc']))
570 #spc = self.data['cspc'][0]
571 #print(numpy.shape(spc))
572 #exit()
573 cspc = self.data['cspc'][1]
574 #print(numpy.shape(cspc))
575 #exit()
576
577 for n in range(self.nrows):
578 noise = self.data['noise'][:,-1]
579 pair = self.data.pairs[n]
580 #print(pair) #exit()
581
582
583
584 out = cspc[n]# / numpy.sqrt(spc[pair[0]] * spc[pair[1]])
585
586 #print(out[:,53])
587 #exit()
588 cross = numpy.abs(out)
589 z = cross/self.data.nFactor
590 cross = 10*numpy.log10(z)
591
592 out_r= out.real/self.data.nFactor
593 #out_r = 10*numpy.log10(out_r)
594
595 out_i= out.imag/self.data.nFactor
596 #out_i = 10*numpy.log10(out_i)
597 #print(numpy.shape(cross))
598 #print(cross[0,:])
599 #print(self.data.nFactor)
600 #exit()
601 #phase = numpy.arctan2(out.imag, out.real) * 180 / numpy.pi
602
603 ax = self.axes[3 * n]
604 if ax.firsttime:
605 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
606 self.xmin = self.xmin if self.xmin else -self.xmax
607 self.zmin = self.zmin if self.zmin else numpy.nanmin(cross)
608 self.zmax = self.zmax if self.zmax else numpy.nanmax(cross)
609 ax.plt = ax.pcolormesh(x, y, cross.T,
610 vmin=self.zmin,
611 vmax=self.zmax,
612 cmap=plt.get_cmap(self.colormap)
613 )
614 else:
615 ax.plt.set_array(cross.T.ravel())
616 self.titles.append(
617 'Cross Spectra Power Ch{} * Ch{}'.format(pair[0], pair[1]))
618
619 ax = self.axes[3 * n + 1]
620 if ax.firsttime:
621 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
622 self.xmin = self.xmin if self.xmin else -self.xmax
623 self.zmin = self.zmin if self.zmin else numpy.nanmin(cross)
624 self.zmax = self.zmax if self.zmax else numpy.nanmax(cross)
625 ax.plt = ax.pcolormesh(x, y, out_r.T,
626 vmin=-1.e6,
627 vmax=0,
628 cmap=plt.get_cmap(self.colormap)
629 )
630 else:
631 ax.plt.set_array(out_r.T.ravel())
632 self.titles.append(
633 'Cross Spectra Real Ch{} * Ch{}'.format(pair[0], pair[1]))
634
635 ax = self.axes[3 * n + 2]
636
637
638 if ax.firsttime:
639 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
640 self.xmin = self.xmin if self.xmin else -self.xmax
641 self.zmin = self.zmin if self.zmin else numpy.nanmin(cross)
642 self.zmax = self.zmax if self.zmax else numpy.nanmax(cross)
643 ax.plt = ax.pcolormesh(x, y, out_i.T,
644 vmin=-1.e6,
645 vmax=1.e6,
646 cmap=plt.get_cmap(self.colormap)
647 )
648 else:
649 ax.plt.set_array(out_i.T.ravel())
650 self.titles.append(
651 'Cross Spectra Imag Ch{} * Ch{}'.format(pair[0], pair[1]))
652
217 class RTIPlot(Plot):
653 class RTIPlot(Plot):
218 '''
654 '''
219 Plot for RTI data
655 Plot for RTI data
220 '''
656 '''
221
657
222 CODE = 'rti'
658 CODE = 'rti'
223 colormap = 'jet'
659 colormap = 'jet'
224 plot_type = 'pcolorbuffer'
660 plot_type = 'pcolorbuffer'
225
661
226 def setup(self):
662 def setup(self):
227 self.xaxis = 'time'
663 self.xaxis = 'time'
228 self.ncols = 1
664 self.ncols = 1
229 self.nrows = len(self.data.channels)
665 self.nrows = len(self.data.channels)
230 self.nplots = len(self.data.channels)
666 self.nplots = len(self.data.channels)
231 self.ylabel = 'Range [km]'
667 self.ylabel = 'Range [km]'
232 self.xlabel = 'Time'
668 self.xlabel = 'Time'
233 self.cb_label = 'dB'
669 self.cb_label = 'dB'
234 self.plots_adjust.update({'hspace':0.8, 'left': 0.1, 'bottom': 0.08, 'right':0.95})
670 self.plots_adjust.update({'hspace':0.8, 'left': 0.1, 'bottom': 0.1, 'right':0.95})
235 self.titles = ['{} Channel {}'.format(
671 self.titles = ['{} Channel {}'.format(
236 self.CODE.upper(), x) for x in range(self.nrows)]
672 self.CODE.upper(), x) for x in range(self.nrows)]
237
673
238 def update(self, dataOut):
674 def update(self, dataOut):
239
675
240 data = {}
676 data = {}
241 meta = {}
677 meta = {}
242 data['rti'] = dataOut.getPower()
678 data['rti'] = dataOut.getPower()
243 data['noise'] = 10*numpy.log10(dataOut.getNoise()/dataOut.normFactor)
679 data['noise'] = 10*numpy.log10(dataOut.getNoise()/dataOut.normFactor)
244
680
245 return data, meta
681 return data, meta
246
682
247 def plot(self):
683 def plot(self):
248 self.x = self.data.times
684 self.x = self.data.times
249 self.y = self.data.yrange
685 self.y = self.data.yrange
250 self.z = self.data[self.CODE]
686 self.z = self.data[self.CODE]
687
688 self.z = numpy.ma.masked_invalid(self.z)
689
690 if self.decimation is None:
691 x, y, z = self.fill_gaps(self.x, self.y, self.z)
692 else:
693 x, y, z = self.fill_gaps(*self.decimate())
694
695 for n, ax in enumerate(self.axes):
696 self.zmin = self.zmin if self.zmin else numpy.min(self.z)
697 self.zmax = self.zmax if self.zmax else numpy.max(self.z)
698 if ax.firsttime:
699 ax.plt = ax.pcolormesh(x, y, z[n].T,
700 vmin=self.zmin,
701 vmax=self.zmax,
702 cmap=plt.get_cmap(self.colormap)
703 )
704 if self.showprofile:
705 ax.plot_profile = self.pf_axes[n].plot(
706 self.data['rti'][n][-1], self.y)[0]
707 ax.plot_noise = self.pf_axes[n].plot(numpy.repeat(self.data['noise'][n][-1], len(self.y)), self.y,
708 color="k", linestyle="dashed", lw=1)[0]
709 else:
710 ax.collections.remove(ax.collections[0])
711 ax.plt = ax.pcolormesh(x, y, z[n].T,
712 vmin=self.zmin,
713 vmax=self.zmax,
714 cmap=plt.get_cmap(self.colormap)
715 )
716 if self.showprofile:
717 ax.plot_profile.set_data(self.data['rti'][n][-1], self.y)
718 ax.plot_noise.set_data(numpy.repeat(
719 self.data['noise'][n][-1], len(self.y)), self.y)
720
721
722 class SpectrogramPlot(Plot):
723 '''
724 Plot for Spectrogram data
725 '''
726
727 CODE = 'spectrogram'
728 colormap = 'binary'
729 plot_type = 'pcolorbuffer'
730
731 def setup(self):
732 self.xaxis = 'time'
733 self.ncols = 1
734 self.nrows = len(self.data.channels)
735 self.nplots = len(self.data.channels)
736 #print(self.dataOut.heightList)
737 #self.ylabel = 'Range [km]'
738 self.xlabel = 'Time'
739 self.cb_label = 'dB'
740 self.plots_adjust.update({'hspace':1.2, 'left': 0.1, 'bottom': 0.12, 'right':0.95})
741 self.titles = ['{} Channel {} \n H = {} km ({} - {})'.format(
742 self.CODE.upper(), x, self.data.heightList[self.data.hei], self.data.heightList[self.data.hei],self.data.heightList[self.data.hei]+(self.data.DH*self.data.nProfiles)) for x in range(self.nrows)]
743
744 def plot(self):
745 self.x = self.data.times
746 #self.y = self.data.heights
747 self.z = self.data[self.CODE]
748 self.y = self.data.xrange[0]
749 #import time
750 #print(time.ctime(self.x))
751
752 '''
753 print(numpy.shape(self.x))
754 print(numpy.shape(self.y))
755 print(numpy.shape(self.z))
756 '''
757 self.ylabel = "Frequency (kHz)"
758
251 self.z = numpy.ma.masked_invalid(self.z)
759 self.z = numpy.ma.masked_invalid(self.z)
252
760
253 if self.decimation is None:
761 if self.decimation is None:
254 x, y, z = self.fill_gaps(self.x, self.y, self.z)
762 x, y, z = self.fill_gaps(self.x, self.y, self.z)
255 else:
763 else:
256 x, y, z = self.fill_gaps(*self.decimate())
764 x, y, z = self.fill_gaps(*self.decimate())
257
765
258 for n, ax in enumerate(self.axes):
766 for n, ax in enumerate(self.axes):
259 self.zmin = self.zmin if self.zmin else numpy.min(self.z)
767 self.zmin = self.zmin if self.zmin else numpy.min(self.z)
260 self.zmax = self.zmax if self.zmax else numpy.max(self.z)
768 self.zmax = self.zmax if self.zmax else numpy.max(self.z)
261 data = self.data[-1]
769 data = self.data[-1]
262 if ax.firsttime:
770 if ax.firsttime:
263 ax.plt = ax.pcolormesh(x, y, z[n].T,
771 ax.plt = ax.pcolormesh(x, y, z[n].T,
264 vmin=self.zmin,
772 vmin=self.zmin,
265 vmax=self.zmax,
773 vmax=self.zmax,
266 cmap=plt.get_cmap(self.colormap)
774 cmap=plt.get_cmap(self.colormap)
267 )
775 )
268 if self.showprofile:
776 if self.showprofile:
269 ax.plot_profile = self.pf_axes[n].plot(
777 ax.plot_profile = self.pf_axes[n].plot(
270 data['rti'][n], self.y)[0]
778 data['rti'][n], self.y)[0]
271 ax.plot_noise = self.pf_axes[n].plot(numpy.repeat(data['noise'][n], len(self.y)), self.y,
779 ax.plot_noise = self.pf_axes[n].plot(numpy.repeat(data['noise'][n], len(self.y)), self.y,
272 color="k", linestyle="dashed", lw=1)[0]
780 color="k", linestyle="dashed", lw=1)[0]
273 else:
781 else:
274 ax.collections.remove(ax.collections[0])
782 ax.collections.remove(ax.collections[0])
275 ax.plt = ax.pcolormesh(x, y, z[n].T,
783 ax.plt = ax.pcolormesh(x, y, z[n].T,
276 vmin=self.zmin,
784 vmin=self.zmin,
277 vmax=self.zmax,
785 vmax=self.zmax,
278 cmap=plt.get_cmap(self.colormap)
786 cmap=plt.get_cmap(self.colormap)
279 )
787 )
280 if self.showprofile:
788 if self.showprofile:
281 ax.plot_profile.set_data(data['rti'][n], self.y)
789 ax.plot_profile.set_data(data['rti'][n], self.y)
282 ax.plot_noise.set_data(numpy.repeat(
790 ax.plot_noise.set_data(numpy.repeat(
283 data['noise'][n], len(self.y)), self.y)
791 data['noise'][n], len(self.y)), self.y)
284
792
285
793
286 class CoherencePlot(RTIPlot):
794 class CoherencePlot(RTIPlot):
287 '''
795 '''
288 Plot for Coherence data
796 Plot for Coherence data
289 '''
797 '''
290
798
291 CODE = 'coh'
799 CODE = 'coh'
292
800
293 def setup(self):
801 def setup(self):
294 self.xaxis = 'time'
802 self.xaxis = 'time'
295 self.ncols = 1
803 self.ncols = 1
296 self.nrows = len(self.data.pairs)
804 self.nrows = len(self.data.pairs)
297 self.nplots = len(self.data.pairs)
805 self.nplots = len(self.data.pairs)
298 self.ylabel = 'Range [km]'
806 self.ylabel = 'Range [km]'
299 self.xlabel = 'Time'
807 self.xlabel = 'Time'
300 self.plots_adjust.update({'hspace':0.6, 'left': 0.1, 'bottom': 0.1,'right':0.95})
808 self.plots_adjust.update({'hspace':0.6, 'left': 0.1, 'bottom': 0.1,'right':0.95})
301 if self.CODE == 'coh':
809 if self.CODE == 'coh':
302 self.cb_label = ''
810 self.cb_label = ''
303 self.titles = [
811 self.titles = [
304 'Coherence Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
812 'Coherence Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
305 else:
813 else:
306 self.cb_label = 'Degrees'
814 self.cb_label = 'Degrees'
307 self.titles = [
815 self.titles = [
308 'Phase Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
816 'Phase Map Ch{} * Ch{}'.format(x[0], x[1]) for x in self.data.pairs]
309
817
310 def update(self, dataOut):
818 def update(self, dataOut):
311
819
312 data = {}
820 data = {}
313 meta = {}
821 meta = {}
314 data['coh'] = dataOut.getCoherence()
822 data['coh'] = dataOut.getCoherence()
315 meta['pairs'] = dataOut.pairsList
823 meta['pairs'] = dataOut.pairsList
316
824
317 return data, meta
825 return data, meta
318
826
319 class PhasePlot(CoherencePlot):
827 class PhasePlot(CoherencePlot):
320 '''
828 '''
321 Plot for Phase map data
829 Plot for Phase map data
322 '''
830 '''
323
831
324 CODE = 'phase'
832 CODE = 'phase'
325 colormap = 'seismic'
833 colormap = 'seismic'
326
834
327 def update(self, dataOut):
835 def update(self, dataOut):
328
836
329 data = {}
837 data = {}
330 meta = {}
838 meta = {}
331 data['phase'] = dataOut.getCoherence(phase=True)
839 data['phase'] = dataOut.getCoherence(phase=True)
332 meta['pairs'] = dataOut.pairsList
840 meta['pairs'] = dataOut.pairsList
333
841
334 return data, meta
842 return data, meta
335
843
336 class NoisePlot(Plot):
844 class NoisePlot(Plot):
337 '''
845 '''
338 Plot for noise
846 Plot for noise
339 '''
847 '''
340
848
341 CODE = 'noise'
849 CODE = 'noise'
342 plot_type = 'scatterbuffer'
850 plot_type = 'scatterbuffer'
343
851
344 def setup(self):
852 def setup(self):
345 self.xaxis = 'time'
853 self.xaxis = 'time'
346 self.ncols = 1
854 self.ncols = 1
347 self.nrows = 1
855 self.nrows = 1
348 self.nplots = 1
856 self.nplots = 1
349 self.ylabel = 'Intensity [dB]'
857 self.ylabel = 'Intensity [dB]'
350 self.xlabel = 'Time'
858 self.xlabel = 'Time'
351 self.titles = ['Noise']
859 self.titles = ['Noise']
352 self.colorbar = False
860 self.colorbar = False
353 self.plots_adjust.update({'right': 0.85 })
861 self.plots_adjust.update({'right': 0.85 })
354
862
355 def update(self, dataOut):
863 def update(self, dataOut):
356
864
357 data = {}
865 data = {}
358 meta = {}
866 meta = {}
359 data['noise'] = 10*numpy.log10(dataOut.getNoise()/dataOut.normFactor).reshape(dataOut.nChannels, 1)
867 data['noise'] = 10*numpy.log10(dataOut.getNoise()/dataOut.normFactor).reshape(dataOut.nChannels, 1)
360 meta['yrange'] = numpy.array([])
868 meta['yrange'] = numpy.array([])
361
869
362 return data, meta
870 return data, meta
363
871
364 def plot(self):
872 def plot(self):
365
873
366 x = self.data.times
874 x = self.data.times
367 xmin = self.data.min_time
875 xmin = self.data.min_time
368 xmax = xmin + self.xrange * 60 * 60
876 xmax = xmin + self.xrange * 60 * 60
369 Y = self.data['noise']
877 Y = self.data['noise']
370
878
371 if self.axes[0].firsttime:
879 if self.axes[0].firsttime:
372 self.ymin = numpy.nanmin(Y) - 5
880 self.ymin = numpy.nanmin(Y) - 5
373 self.ymax = numpy.nanmax(Y) + 5
881 self.ymax = numpy.nanmax(Y) + 5
374 for ch in self.data.channels:
882 for ch in self.data.channels:
375 y = Y[ch]
883 y = Y[ch]
376 self.axes[0].plot(x, y, lw=1, label='Ch{}'.format(ch))
884 self.axes[0].plot(x, y, lw=1, label='Ch{}'.format(ch))
377 plt.legend(bbox_to_anchor=(1.18, 1.0))
885 plt.legend(bbox_to_anchor=(1.18, 1.0))
378 else:
886 else:
379 for ch in self.data.channels:
887 for ch in self.data.channels:
380 y = Y[ch]
888 y = Y[ch]
381 self.axes[0].lines[ch].set_data(x, y)
889 self.axes[0].lines[ch].set_data(x, y)
382
890
383
891 self.ymin = numpy.nanmin(Y) - 5
892 self.ymax = numpy.nanmax(Y) + 10
893
894
384 class PowerProfilePlot(Plot):
895 class PowerProfilePlot(Plot):
385
896
386 CODE = 'pow_profile'
897 CODE = 'pow_profile'
387 plot_type = 'scatter'
898 plot_type = 'scatter'
388
899
389 def setup(self):
900 def setup(self):
390
901
391 self.ncols = 1
902 self.ncols = 1
392 self.nrows = 1
903 self.nrows = 1
393 self.nplots = 1
904 self.nplots = 1
394 self.height = 4
905 self.height = 4
395 self.width = 3
906 self.width = 3
396 self.ylabel = 'Range [km]'
907 self.ylabel = 'Range [km]'
397 self.xlabel = 'Intensity [dB]'
908 self.xlabel = 'Intensity [dB]'
398 self.titles = ['Power Profile']
909 self.titles = ['Power Profile']
399 self.colorbar = False
910 self.colorbar = False
400
911
401 def update(self, dataOut):
912 def update(self, dataOut):
402
913
403 data = {}
914 data = {}
404 meta = {}
915 meta = {}
405 data[self.CODE] = dataOut.getPower()
916 data[self.CODE] = dataOut.getPower()
406
917
407 return data, meta
918 return data, meta
408
919
409 def plot(self):
920 def plot(self):
410
921
411 y = self.data.yrange
922 y = self.data.yrange
412 self.y = y
923 self.y = y
413
924
414 x = self.data[-1][self.CODE]
925 x = self.data[-1][self.CODE]
415
926
416 if self.xmin is None: self.xmin = numpy.nanmin(x)*0.9
927 if self.xmin is None: self.xmin = numpy.nanmin(x)*0.9
417 if self.xmax is None: self.xmax = numpy.nanmax(x)*1.1
928 if self.xmax is None: self.xmax = numpy.nanmax(x)*1.1
418
929
419 if self.axes[0].firsttime:
930 if self.axes[0].firsttime:
420 for ch in self.data.channels:
931 for ch in self.data.channels:
421 self.axes[0].plot(x[ch], y, lw=1, label='Ch{}'.format(ch))
932 self.axes[0].plot(x[ch], y, lw=1, label='Ch{}'.format(ch))
422 plt.legend()
933 plt.legend()
423 else:
934 else:
424 for ch in self.data.channels:
935 for ch in self.data.channels:
425 self.axes[0].lines[ch].set_data(x[ch], y)
936 self.axes[0].lines[ch].set_data(x[ch], y)
426
937
427
938
428 class SpectraCutPlot(Plot):
939 class SpectraCutPlot(Plot):
429
940
430 CODE = 'spc_cut'
941 CODE = 'spc_cut'
431 plot_type = 'scatter'
942 plot_type = 'scatter'
432 buffering = False
943 buffering = False
433
944
434 def setup(self):
945 def setup(self):
435
946
436 self.nplots = len(self.data.channels)
947 self.nplots = len(self.data.channels)
437 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
948 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
438 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
949 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
439 self.width = 3.4 * self.ncols + 1.5
950 self.width = 3.4 * self.ncols + 1.5
440 self.height = 3 * self.nrows
951 self.height = 3 * self.nrows
441 self.ylabel = 'Power [dB]'
952 self.ylabel = 'Power [dB]'
442 self.colorbar = False
953 self.colorbar = False
443 self.plots_adjust.update({'left':0.1, 'hspace':0.3, 'right': 0.75, 'bottom':0.08})
954 self.plots_adjust.update({'left':0.1, 'hspace':0.3, 'right': 0.75, 'bottom':0.08})
444
955
445 def update(self, dataOut):
956 def update(self, dataOut):
446
957
447 data = {}
958 data = {}
448 meta = {}
959 meta = {}
449 spc = 10*numpy.log10(dataOut.data_pre[0]/dataOut.normFactor)
960 spc = 10*numpy.log10(dataOut.data_pre[0]/dataOut.normFactor)
450 data['spc'] = spc
961 data['spc'] = spc
451 meta['xrange'] = (dataOut.getFreqRange(1)/1000., dataOut.getAcfRange(1), dataOut.getVelRange(1))
962 meta['xrange'] = (dataOut.getFreqRange(1)/1000., dataOut.getAcfRange(1), dataOut.getVelRange(1))
452 if self.CODE == 'cut_gaussian_fit':
963 if self.CODE == 'cut_gaussian_fit':
453 data['gauss_fit0'] = 10*numpy.log10(dataOut.GaussFit0/dataOut.normFactor)
964 data['gauss_fit0'] = 10*numpy.log10(dataOut.GaussFit0/dataOut.normFactor)
454 data['gauss_fit1'] = 10*numpy.log10(dataOut.GaussFit1/dataOut.normFactor)
965 data['gauss_fit1'] = 10*numpy.log10(dataOut.GaussFit1/dataOut.normFactor)
455 return data, meta
966 return data, meta
456
967
457 def plot(self):
968 def plot(self):
458 if self.xaxis == "frequency":
969 if self.xaxis == "frequency":
459 x = self.data.xrange[0][1:]
970 x = self.data.xrange[0][1:]
460 self.xlabel = "Frequency (kHz)"
971 self.xlabel = "Frequency (kHz)"
461 elif self.xaxis == "time":
972 elif self.xaxis == "time":
462 x = self.data.xrange[1]
973 x = self.data.xrange[1]
463 self.xlabel = "Time (ms)"
974 self.xlabel = "Time (ms)"
464 else:
975 else:
465 x = self.data.xrange[2][:-1]
976 x = self.data.xrange[2][:-1]
466 self.xlabel = "Velocity (m/s)"
977 self.xlabel = "Velocity (m/s)"
467
978
468 if self.CODE == 'cut_gaussian_fit':
979 if self.CODE == 'cut_gaussian_fit':
469 x = self.data.xrange[2][:-1]
980 x = self.data.xrange[2][:-1]
470 self.xlabel = "Velocity (m/s)"
981 self.xlabel = "Velocity (m/s)"
471
982
472 self.titles = []
983 self.titles = []
473
984
474 y = self.data.yrange
985 y = self.data.yrange
475 data = self.data[-1]
986 data = self.data[-1]
476 z = data['spc']
987 z = data['spc']
477
988
478 if self.height_index:
989 if self.height_index:
479 index = numpy.array(self.height_index)
990 index = numpy.array(self.height_index)
480 else:
991 else:
481 index = numpy.arange(0, len(y), int((len(y))/9))
992 index = numpy.arange(0, len(y), int((len(y))/9))
482
993
483 for n, ax in enumerate(self.axes):
994 for n, ax in enumerate(self.axes):
484 if self.CODE == 'cut_gaussian_fit':
995 if self.CODE == 'cut_gaussian_fit':
485 gau0 = data['gauss_fit0']
996 gau0 = data['gauss_fit0']
486 gau1 = data['gauss_fit1']
997 gau1 = data['gauss_fit1']
487 if ax.firsttime:
998 if ax.firsttime:
488 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
999 self.xmax = self.xmax if self.xmax else numpy.nanmax(x)
489 self.xmin = self.xmin if self.xmin else -self.xmax
1000 self.xmin = self.xmin if self.xmin else -self.xmax
490 self.ymin = self.ymin if self.ymin else numpy.nanmin(z)
1001 self.ymin = self.ymin if self.ymin else numpy.nanmin(z)
491 self.ymax = self.ymax if self.ymax else numpy.nanmax(z)
1002 self.ymax = self.ymax if self.ymax else numpy.nanmax(z)
492 ax.plt = ax.plot(x, z[n, :, index].T, lw=0.25)
1003 ax.plt = ax.plot(x, z[n, :, index].T, lw=0.25)
493 if self.CODE == 'cut_gaussian_fit':
1004 if self.CODE == 'cut_gaussian_fit':
494 ax.plt_gau0 = ax.plot(x, gau0[n, :, index].T, lw=1, linestyle='-.')
1005 ax.plt_gau0 = ax.plot(x, gau0[n, :, index].T, lw=1, linestyle='-.')
495 for i, line in enumerate(ax.plt_gau0):
1006 for i, line in enumerate(ax.plt_gau0):
496 line.set_color(ax.plt[i].get_color())
1007 line.set_color(ax.plt[i].get_color())
497 ax.plt_gau1 = ax.plot(x, gau1[n, :, index].T, lw=1, linestyle='--')
1008 ax.plt_gau1 = ax.plot(x, gau1[n, :, index].T, lw=1, linestyle='--')
498 for i, line in enumerate(ax.plt_gau1):
1009 for i, line in enumerate(ax.plt_gau1):
499 line.set_color(ax.plt[i].get_color())
1010 line.set_color(ax.plt[i].get_color())
500 labels = ['Range = {:2.1f}km'.format(y[i]) for i in index]
1011 labels = ['Range = {:2.1f}km'.format(y[i]) for i in index]
501 self.figures[0].legend(ax.plt, labels, loc='center right')
1012 self.figures[0].legend(ax.plt, labels, loc='center right')
502 else:
1013 else:
503 for i, line in enumerate(ax.plt):
1014 for i, line in enumerate(ax.plt):
504 line.set_data(x, z[n, :, index[i]].T)
1015 line.set_data(x, z[n, :, index[i]].T)
505 for i, line in enumerate(ax.plt_gau0):
1016 for i, line in enumerate(ax.plt_gau0):
506 line.set_data(x, gau0[n, :, index[i]].T)
1017 line.set_data(x, gau0[n, :, index[i]].T)
507 line.set_color(ax.plt[i].get_color())
1018 line.set_color(ax.plt[i].get_color())
508 for i, line in enumerate(ax.plt_gau1):
1019 for i, line in enumerate(ax.plt_gau1):
509 line.set_data(x, gau1[n, :, index[i]].T)
1020 line.set_data(x, gau1[n, :, index[i]].T)
510 line.set_color(ax.plt[i].get_color())
1021 line.set_color(ax.plt[i].get_color())
511 self.titles.append('CH {}'.format(n))
1022 self.titles.append('CH {}'.format(n))
512
1023
513
1024
514 class BeaconPhase(Plot):
1025 class BeaconPhase(Plot):
515
1026
516 __isConfig = None
1027 __isConfig = None
517 __nsubplots = None
1028 __nsubplots = None
518
1029
519 PREFIX = 'beacon_phase'
1030 PREFIX = 'beacon_phase'
520
1031
521 def __init__(self):
1032 def __init__(self):
522 Plot.__init__(self)
1033 Plot.__init__(self)
523 self.timerange = 24*60*60
1034 self.timerange = 24*60*60
524 self.isConfig = False
1035 self.isConfig = False
525 self.__nsubplots = 1
1036 self.__nsubplots = 1
526 self.counter_imagwr = 0
1037 self.counter_imagwr = 0
527 self.WIDTH = 800
1038 self.WIDTH = 800
528 self.HEIGHT = 400
1039 self.HEIGHT = 400
529 self.WIDTHPROF = 120
1040 self.WIDTHPROF = 120
530 self.HEIGHTPROF = 0
1041 self.HEIGHTPROF = 0
531 self.xdata = None
1042 self.xdata = None
532 self.ydata = None
1043 self.ydata = None
533
1044
534 self.PLOT_CODE = BEACON_CODE
1045 self.PLOT_CODE = BEACON_CODE
535
1046
536 self.FTP_WEI = None
1047 self.FTP_WEI = None
537 self.EXP_CODE = None
1048 self.EXP_CODE = None
538 self.SUB_EXP_CODE = None
1049 self.SUB_EXP_CODE = None
539 self.PLOT_POS = None
1050 self.PLOT_POS = None
540
1051
541 self.filename_phase = None
1052 self.filename_phase = None
542
1053
543 self.figfile = None
1054 self.figfile = None
544
1055
545 self.xmin = None
1056 self.xmin = None
546 self.xmax = None
1057 self.xmax = None
547
1058
548 def getSubplots(self):
1059 def getSubplots(self):
549
1060
550 ncol = 1
1061 ncol = 1
551 nrow = 1
1062 nrow = 1
552
1063
553 return nrow, ncol
1064 return nrow, ncol
554
1065
555 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1066 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
556
1067
557 self.__showprofile = showprofile
1068 self.__showprofile = showprofile
558 self.nplots = nplots
1069 self.nplots = nplots
559
1070
560 ncolspan = 7
1071 ncolspan = 7
561 colspan = 6
1072 colspan = 6
562 self.__nsubplots = 2
1073 self.__nsubplots = 2
563
1074
564 self.createFigure(id = id,
1075 self.createFigure(id = id,
565 wintitle = wintitle,
1076 wintitle = wintitle,
566 widthplot = self.WIDTH+self.WIDTHPROF,
1077 widthplot = self.WIDTH+self.WIDTHPROF,
567 heightplot = self.HEIGHT+self.HEIGHTPROF,
1078 heightplot = self.HEIGHT+self.HEIGHTPROF,
568 show=show)
1079 show=show)
569
1080
570 nrow, ncol = self.getSubplots()
1081 nrow, ncol = self.getSubplots()
571
1082
572 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1083 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
573
1084
574 def save_phase(self, filename_phase):
1085 def save_phase(self, filename_phase):
575 f = open(filename_phase,'w+')
1086 f = open(filename_phase,'w+')
576 f.write('\n\n')
1087 f.write('\n\n')
577 f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n')
1088 f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n')
578 f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' )
1089 f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' )
579 f.close()
1090 f.close()
580
1091
581 def save_data(self, filename_phase, data, data_datetime):
1092 def save_data(self, filename_phase, data, data_datetime):
582 f=open(filename_phase,'a')
1093 f=open(filename_phase,'a')
583 timetuple_data = data_datetime.timetuple()
1094 timetuple_data = data_datetime.timetuple()
584 day = str(timetuple_data.tm_mday)
1095 day = str(timetuple_data.tm_mday)
585 month = str(timetuple_data.tm_mon)
1096 month = str(timetuple_data.tm_mon)
586 year = str(timetuple_data.tm_year)
1097 year = str(timetuple_data.tm_year)
587 hour = str(timetuple_data.tm_hour)
1098 hour = str(timetuple_data.tm_hour)
588 minute = str(timetuple_data.tm_min)
1099 minute = str(timetuple_data.tm_min)
589 second = str(timetuple_data.tm_sec)
1100 second = str(timetuple_data.tm_sec)
590 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n')
1101 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n')
591 f.close()
1102 f.close()
592
1103
593 def plot(self):
1104 def plot(self):
594 log.warning('TODO: Not yet implemented...')
1105 log.warning('TODO: Not yet implemented...')
595
1106
596 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1107 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
597 xmin=None, xmax=None, ymin=None, ymax=None, hmin=None, hmax=None,
1108 xmin=None, xmax=None, ymin=None, ymax=None, hmin=None, hmax=None,
598 timerange=None,
1109 timerange=None,
599 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1110 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
600 server=None, folder=None, username=None, password=None,
1111 server=None, folder=None, username=None, password=None,
601 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1112 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
602
1113
603 if dataOut.flagNoData:
1114 if dataOut.flagNoData:
604 return dataOut
1115 return dataOut
605
1116
606 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1117 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
607 return
1118 return
608
1119
609 if pairsList == None:
1120 if pairsList == None:
610 pairsIndexList = dataOut.pairsIndexList[:10]
1121 pairsIndexList = dataOut.pairsIndexList[:10]
611 else:
1122 else:
612 pairsIndexList = []
1123 pairsIndexList = []
613 for pair in pairsList:
1124 for pair in pairsList:
614 if pair not in dataOut.pairsList:
1125 if pair not in dataOut.pairsList:
615 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
1126 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
616 pairsIndexList.append(dataOut.pairsList.index(pair))
1127 pairsIndexList.append(dataOut.pairsList.index(pair))
617
1128
618 if pairsIndexList == []:
1129 if pairsIndexList == []:
619 return
1130 return
620
1131
621 # if len(pairsIndexList) > 4:
1132 # if len(pairsIndexList) > 4:
622 # pairsIndexList = pairsIndexList[0:4]
1133 # pairsIndexList = pairsIndexList[0:4]
623
1134
624 hmin_index = None
1135 hmin_index = None
625 hmax_index = None
1136 hmax_index = None
626
1137
627 if hmin != None and hmax != None:
1138 if hmin != None and hmax != None:
628 indexes = numpy.arange(dataOut.nHeights)
1139 indexes = numpy.arange(dataOut.nHeights)
629 hmin_list = indexes[dataOut.heightList >= hmin]
1140 hmin_list = indexes[dataOut.heightList >= hmin]
630 hmax_list = indexes[dataOut.heightList <= hmax]
1141 hmax_list = indexes[dataOut.heightList <= hmax]
631
1142
632 if hmin_list.any():
1143 if hmin_list.any():
633 hmin_index = hmin_list[0]
1144 hmin_index = hmin_list[0]
634
1145
635 if hmax_list.any():
1146 if hmax_list.any():
636 hmax_index = hmax_list[-1]+1
1147 hmax_index = hmax_list[-1]+1
637
1148
638 x = dataOut.getTimeRange()
1149 x = dataOut.getTimeRange()
639
1150
640 thisDatetime = dataOut.datatime
1151 thisDatetime = dataOut.datatime
641
1152
642 title = wintitle + " Signal Phase" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1153 title = wintitle + " Signal Phase" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
643 xlabel = "Local Time"
1154 xlabel = "Local Time"
644 ylabel = "Phase (degrees)"
1155 ylabel = "Phase (degrees)"
645
1156
646 update_figfile = False
1157 update_figfile = False
647
1158
648 nplots = len(pairsIndexList)
1159 nplots = len(pairsIndexList)
649 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1160 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
650 phase_beacon = numpy.zeros(len(pairsIndexList))
1161 phase_beacon = numpy.zeros(len(pairsIndexList))
651 for i in range(nplots):
1162 for i in range(nplots):
652 pair = dataOut.pairsList[pairsIndexList[i]]
1163 pair = dataOut.pairsList[pairsIndexList[i]]
653 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i], :, hmin_index:hmax_index], axis=0)
1164 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i], :, hmin_index:hmax_index], axis=0)
654 powa = numpy.average(dataOut.data_spc[pair[0], :, hmin_index:hmax_index], axis=0)
1165 powa = numpy.average(dataOut.data_spc[pair[0], :, hmin_index:hmax_index], axis=0)
655 powb = numpy.average(dataOut.data_spc[pair[1], :, hmin_index:hmax_index], axis=0)
1166 powb = numpy.average(dataOut.data_spc[pair[1], :, hmin_index:hmax_index], axis=0)
656 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
1167 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
657 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
1168 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
658
1169
659 if dataOut.beacon_heiIndexList:
1170 if dataOut.beacon_heiIndexList:
660 phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList])
1171 phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList])
661 else:
1172 else:
662 phase_beacon[i] = numpy.average(phase)
1173 phase_beacon[i] = numpy.average(phase)
663
1174
664 if not self.isConfig:
1175 if not self.isConfig:
665
1176
666 nplots = len(pairsIndexList)
1177 nplots = len(pairsIndexList)
667
1178
668 self.setup(id=id,
1179 self.setup(id=id,
669 nplots=nplots,
1180 nplots=nplots,
670 wintitle=wintitle,
1181 wintitle=wintitle,
671 showprofile=showprofile,
1182 showprofile=showprofile,
672 show=show)
1183 show=show)
673
1184
674 if timerange != None:
1185 if timerange != None:
675 self.timerange = timerange
1186 self.timerange = timerange
676
1187
677 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1188 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
678
1189
679 if ymin == None: ymin = 0
1190 if ymin == None: ymin = 0
680 if ymax == None: ymax = 360
1191 if ymax == None: ymax = 360
681
1192
682 self.FTP_WEI = ftp_wei
1193 self.FTP_WEI = ftp_wei
683 self.EXP_CODE = exp_code
1194 self.EXP_CODE = exp_code
684 self.SUB_EXP_CODE = sub_exp_code
1195 self.SUB_EXP_CODE = sub_exp_code
685 self.PLOT_POS = plot_pos
1196 self.PLOT_POS = plot_pos
686
1197
687 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1198 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
688 self.isConfig = True
1199 self.isConfig = True
689 self.figfile = figfile
1200 self.figfile = figfile
690 self.xdata = numpy.array([])
1201 self.xdata = numpy.array([])
691 self.ydata = numpy.array([])
1202 self.ydata = numpy.array([])
692
1203
693 update_figfile = True
1204 update_figfile = True
694
1205
695 #open file beacon phase
1206 #open file beacon phase
696 path = '%s%03d' %(self.PREFIX, self.id)
1207 path = '%s%03d' %(self.PREFIX, self.id)
697 beacon_file = os.path.join(path,'%s.txt'%self.name)
1208 beacon_file = os.path.join(path,'%s.txt'%self.name)
698 self.filename_phase = os.path.join(figpath,beacon_file)
1209 self.filename_phase = os.path.join(figpath,beacon_file)
699 #self.save_phase(self.filename_phase)
1210 #self.save_phase(self.filename_phase)
700
1211
701
1212
702 #store data beacon phase
1213 #store data beacon phase
703 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1214 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
704
1215
705 self.setWinTitle(title)
1216 self.setWinTitle(title)
706
1217
707
1218
708 title = "Phase Plot %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1219 title = "Phase Plot %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
709
1220
710 legendlabels = ["Pair (%d,%d)"%(pair[0], pair[1]) for pair in dataOut.pairsList]
1221 legendlabels = ["Pair (%d,%d)"%(pair[0], pair[1]) for pair in dataOut.pairsList]
711
1222
712 axes = self.axesList[0]
1223 axes = self.axesList[0]
713
1224
714 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1225 self.xdata = numpy.hstack((self.xdata, x[0:1]))
715
1226
716 if len(self.ydata)==0:
1227 if len(self.ydata)==0:
717 self.ydata = phase_beacon.reshape(-1,1)
1228 self.ydata = phase_beacon.reshape(-1,1)
718 else:
1229 else:
719 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1230 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
720
1231
721
1232
722 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1233 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
723 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1234 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
724 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1235 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
725 XAxisAsTime=True, grid='both'
1236 XAxisAsTime=True, grid='both'
726 )
1237 )
727
1238
728 self.draw()
1239 self.draw()
729
1240
730 if dataOut.ltctime >= self.xmax:
1241 if dataOut.ltctime >= self.xmax:
731 self.counter_imagwr = wr_period
1242 self.counter_imagwr = wr_period
732 self.isConfig = False
1243 self.isConfig = False
733 update_figfile = True
1244 update_figfile = True
734
1245
735 self.save(figpath=figpath,
1246 self.save(figpath=figpath,
736 figfile=figfile,
1247 figfile=figfile,
737 save=save,
1248 save=save,
738 ftp=ftp,
1249 ftp=ftp,
739 wr_period=wr_period,
1250 wr_period=wr_period,
740 thisDatetime=thisDatetime,
1251 thisDatetime=thisDatetime,
741 update_figfile=update_figfile)
1252 update_figfile=update_figfile)
742
1253
743 return dataOut No newline at end of file
1254 return dataOut
@@ -1,24 +1,29
1 '''
1 '''
2
2
3 $Author: murco $
3 $Author: murco $
4 $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $
4 $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $
5 '''
5 '''
6
6
7 from .jroIO_voltage import *
7 from .jroIO_voltage import *
8 from .jroIO_spectra import *
8 from .jroIO_spectra import *
9 from .jroIO_heispectra import *
9 from .jroIO_heispectra import *
10 from .jroIO_usrp import *
10 from .jroIO_usrp import *
11 from .jroIO_digitalRF import *
11 from .jroIO_digitalRF import *
12 from .jroIO_kamisr import *
12 from .jroIO_kamisr import *
13 from .jroIO_param import *
13 from .jroIO_param import *
14 from .jroIO_hf import *
14 from .jroIO_hf import *
15
15
16 from .jroIO_madrigal import *
16 from .jroIO_madrigal import *
17
17
18 from .bltrIO_param import *
18 from .bltrIO_param import *
19 from .bltrIO_spectra import *
19 from .bltrIO_spectra import *
20 from .jroIO_mira35c import *
20 from .jroIO_mira35c import *
21 from .julIO_param import *
21 from .julIO_param import *
22
22
23 from .pxIO_param import *
23 from .pxIO_param import *
24 from .jroIO_simulator import * No newline at end of file
24 from .jroIO_simulator import *
25
26 ############DP############
27 from .jroIO_dat import *
28
29 ############DP############
@@ -1,1575 +1,1615
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 os
6 import os
7 import sys
7 import sys
8 import glob
8 import glob
9 import time
9 import time
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import inspect
12 import inspect
13 import time
13 import time
14 import datetime
14 import datetime
15 import zmq
15 import zmq
16
16
17 from schainpy.model.proc.jroproc_base import Operation, MPDecorator
17 from schainpy.model.proc.jroproc_base import Operation, MPDecorator
18 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
18 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
19 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
19 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
20 from schainpy.utils import log
20 from schainpy.utils import log
21 import schainpy.admin
21 import schainpy.admin
22
22
23 LOCALTIME = True
23 LOCALTIME = True
24 DT_DIRECTIVES = {
24 DT_DIRECTIVES = {
25 '%Y': 4,
25 '%Y': 4,
26 '%y': 2,
26 '%y': 2,
27 '%m': 2,
27 '%m': 2,
28 '%d': 2,
28 '%d': 2,
29 '%j': 3,
29 '%j': 3,
30 '%H': 2,
30 '%H': 2,
31 '%M': 2,
31 '%M': 2,
32 '%S': 2,
32 '%S': 2,
33 '%f': 6
33 '%f': 6
34 }
34 }
35
35
36
36
37 def isNumber(cad):
37 def isNumber(cad):
38 """
38 """
39 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
39 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
40
40
41 Excepciones:
41 Excepciones:
42 Si un determinado string no puede ser convertido a numero
42 Si un determinado string no puede ser convertido a numero
43 Input:
43 Input:
44 str, string al cual se le analiza para determinar si convertible a un numero o no
44 str, string al cual se le analiza para determinar si convertible a un numero o no
45
45
46 Return:
46 Return:
47 True : si el string es uno numerico
47 True : si el string es uno numerico
48 False : no es un string numerico
48 False : no es un string numerico
49 """
49 """
50 try:
50 try:
51 float(cad)
51 float(cad)
52 return True
52 return True
53 except:
53 except:
54 return False
54 return False
55
55
56
56
57 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
57 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
58 """
58 """
59 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
59 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
60
60
61 Inputs:
61 Inputs:
62 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
62 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
63
63
64 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
64 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
65 segundos contados desde 01/01/1970.
65 segundos contados desde 01/01/1970.
66 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
66 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
67 segundos contados desde 01/01/1970.
67 segundos contados desde 01/01/1970.
68
68
69 Return:
69 Return:
70 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
70 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
71 fecha especificado, de lo contrario retorna False.
71 fecha especificado, de lo contrario retorna False.
72
72
73 Excepciones:
73 Excepciones:
74 Si el archivo no existe o no puede ser abierto
74 Si el archivo no existe o no puede ser abierto
75 Si la cabecera no puede ser leida.
75 Si la cabecera no puede ser leida.
76
76
77 """
77 """
78 basicHeaderObj = BasicHeader(LOCALTIME)
78 basicHeaderObj = BasicHeader(LOCALTIME)
79
79
80 try:
80 try:
81
81 fp = open(filename, 'rb')
82 fp = open(filename, 'rb')
82 except IOError:
83 except IOError:
83 print("The file %s can't be opened" % (filename))
84 print("The file %s can't be opened" % (filename))
84 return 0
85 return 0
85
86
86 sts = basicHeaderObj.read(fp)
87 sts = basicHeaderObj.read(fp)
87 fp.close()
88 fp.close()
88
89
89 if not(sts):
90 if not(sts):
90 print("Skipping the file %s because it has not a valid header" % (filename))
91 print("Skipping the file %s because it has not a valid header" % (filename))
91 return 0
92 return 0
92
93
93 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
94 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
94 return 0
95 return 0
95
96
96 return 1
97 return 1
97
98
98
99
99 def isTimeInRange(thisTime, startTime, endTime):
100 def isTimeInRange(thisTime, startTime, endTime):
100 if endTime >= startTime:
101 if endTime >= startTime:
101 if (thisTime < startTime) or (thisTime > endTime):
102 if (thisTime < startTime) or (thisTime > endTime):
102 return 0
103 return 0
103 return 1
104 return 1
104 else:
105 else:
105 if (thisTime < startTime) and (thisTime > endTime):
106 if (thisTime < startTime) and (thisTime > endTime):
106 return 0
107 return 0
107 return 1
108 return 1
108
109
109
110
110 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
111 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
111 """
112 """
112 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
113 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
113
114
114 Inputs:
115 Inputs:
115 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
116 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
116
117
117 startDate : fecha inicial del rango seleccionado en formato datetime.date
118 startDate : fecha inicial del rango seleccionado en formato datetime.date
118
119
119 endDate : fecha final del rango seleccionado en formato datetime.date
120 endDate : fecha final del rango seleccionado en formato datetime.date
120
121
121 startTime : tiempo inicial del rango seleccionado en formato datetime.time
122 startTime : tiempo inicial del rango seleccionado en formato datetime.time
122
123
123 endTime : tiempo final del rango seleccionado en formato datetime.time
124 endTime : tiempo final del rango seleccionado en formato datetime.time
124
125
125 Return:
126 Return:
126 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
127 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
127 fecha especificado, de lo contrario retorna False.
128 fecha especificado, de lo contrario retorna False.
128
129
129 Excepciones:
130 Excepciones:
130 Si el archivo no existe o no puede ser abierto
131 Si el archivo no existe o no puede ser abierto
131 Si la cabecera no puede ser leida.
132 Si la cabecera no puede ser leida.
132
133
133 """
134 """
134
135
135 try:
136 try:
136 fp = open(filename, 'rb')
137 fp = open(filename, 'rb')
137 except IOError:
138 except IOError:
138 print("The file %s can't be opened" % (filename))
139 print("The file %s can't be opened" % (filename))
139 return None
140 return None
140
141
141 firstBasicHeaderObj = BasicHeader(LOCALTIME)
142 firstBasicHeaderObj = BasicHeader(LOCALTIME)
142 systemHeaderObj = SystemHeader()
143 systemHeaderObj = SystemHeader()
144
143 radarControllerHeaderObj = RadarControllerHeader()
145 radarControllerHeaderObj = RadarControllerHeader()
144 processingHeaderObj = ProcessingHeader()
146 processingHeaderObj = ProcessingHeader()
145
147
146 lastBasicHeaderObj = BasicHeader(LOCALTIME)
148 lastBasicHeaderObj = BasicHeader(LOCALTIME)
147
149
148 sts = firstBasicHeaderObj.read(fp)
150 sts = firstBasicHeaderObj.read(fp)
149
151
150 if not(sts):
152 if not(sts):
151 print("[Reading] Skipping the file %s because it has not a valid header" % (filename))
153 print("[Reading] Skipping the file %s because it has not a valid header" % (filename))
152 return None
154 return None
153
155
154 if not systemHeaderObj.read(fp):
156 if not systemHeaderObj.read(fp):
155 return None
157 return None
156
158
157 if not radarControllerHeaderObj.read(fp):
159 if not radarControllerHeaderObj.read(fp):
158 return None
160 return None
159
161
160 if not processingHeaderObj.read(fp):
162 if not processingHeaderObj.read(fp):
161 return None
163 return None
162
164
163 filesize = os.path.getsize(filename)
165 filesize = os.path.getsize(filename)
164
166
165 offset = processingHeaderObj.blockSize + 24 # header size
167 offset = processingHeaderObj.blockSize + 24 # header size
166
168
167 if filesize <= offset:
169 if filesize <= offset:
168 print("[Reading] %s: This file has not enough data" % filename)
170 print("[Reading] %s: This file has not enough data" % filename)
169 return None
171 return None
170
172
171 fp.seek(-offset, 2)
173 fp.seek(-offset, 2)
172
174
173 sts = lastBasicHeaderObj.read(fp)
175 sts = lastBasicHeaderObj.read(fp)
174
176
175 fp.close()
177 fp.close()
176
178
177 thisDatetime = lastBasicHeaderObj.datatime
179 thisDatetime = lastBasicHeaderObj.datatime
178 thisTime_last_block = thisDatetime.time()
180 thisTime_last_block = thisDatetime.time()
179
181
180 thisDatetime = firstBasicHeaderObj.datatime
182 thisDatetime = firstBasicHeaderObj.datatime
181 thisDate = thisDatetime.date()
183 thisDate = thisDatetime.date()
182 thisTime_first_block = thisDatetime.time()
184 thisTime_first_block = thisDatetime.time()
183
185
184 # General case
186 # General case
185 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
187 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
186 #-----------o----------------------------o-----------
188 #-----------o----------------------------o-----------
187 # startTime endTime
189 # startTime endTime
188
190
189 if endTime >= startTime:
191 if endTime >= startTime:
190 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
192 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
191 return None
193 return None
192
194
193 return thisDatetime
195 return thisDatetime
194
196
195 # If endTime < startTime then endTime belongs to the next day
197 # If endTime < startTime then endTime belongs to the next day
196
198
197 #<<<<<<<<<<<o o>>>>>>>>>>>
199 #<<<<<<<<<<<o o>>>>>>>>>>>
198 #-----------o----------------------------o-----------
200 #-----------o----------------------------o-----------
199 # endTime startTime
201 # endTime startTime
200
202
201 if (thisDate == startDate) and (thisTime_last_block < startTime):
203 if (thisDate == startDate) and (thisTime_last_block < startTime):
202 return None
204 return None
203
205
204 if (thisDate == endDate) and (thisTime_first_block > endTime):
206 if (thisDate == endDate) and (thisTime_first_block > endTime):
205 return None
207 return None
206
208
207 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
209 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
208 return None
210 return None
209
211
210 return thisDatetime
212 return thisDatetime
211
213
212
214
213 def isFolderInDateRange(folder, startDate=None, endDate=None):
215 def isFolderInDateRange(folder, startDate=None, endDate=None):
214 """
216 """
215 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
217 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
216
218
217 Inputs:
219 Inputs:
218 folder : nombre completo del directorio.
220 folder : nombre completo del directorio.
219 Su formato deberia ser "/path_root/?YYYYDDD"
221 Su formato deberia ser "/path_root/?YYYYDDD"
220
222
221 siendo:
223 siendo:
222 YYYY : Anio (ejemplo 2015)
224 YYYY : Anio (ejemplo 2015)
223 DDD : Dia del anio (ejemplo 305)
225 DDD : Dia del anio (ejemplo 305)
224
226
225 startDate : fecha inicial del rango seleccionado en formato datetime.date
227 startDate : fecha inicial del rango seleccionado en formato datetime.date
226
228
227 endDate : fecha final del rango seleccionado en formato datetime.date
229 endDate : fecha final del rango seleccionado en formato datetime.date
228
230
229 Return:
231 Return:
230 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
232 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
231 fecha especificado, de lo contrario retorna False.
233 fecha especificado, de lo contrario retorna False.
232 Excepciones:
234 Excepciones:
233 Si el directorio no tiene el formato adecuado
235 Si el directorio no tiene el formato adecuado
234 """
236 """
235
237
236 basename = os.path.basename(folder)
238 basename = os.path.basename(folder)
237
239
238 if not isRadarFolder(basename):
240 if not isRadarFolder(basename):
239 print("The folder %s has not the rigth format" % folder)
241 print("The folder %s has not the rigth format" % folder)
240 return 0
242 return 0
241
243
242 if startDate and endDate:
244 if startDate and endDate:
243 thisDate = getDateFromRadarFolder(basename)
245 thisDate = getDateFromRadarFolder(basename)
244
246
245 if thisDate < startDate:
247 if thisDate < startDate:
246 return 0
248 return 0
247
249
248 if thisDate > endDate:
250 if thisDate > endDate:
249 return 0
251 return 0
250
252
251 return 1
253 return 1
252
254
253
255
254 def isFileInDateRange(filename, startDate=None, endDate=None):
256 def isFileInDateRange(filename, startDate=None, endDate=None):
255 """
257 """
256 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
258 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
257
259
258 Inputs:
260 Inputs:
259 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
261 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
260
262
261 Su formato deberia ser "?YYYYDDDsss"
263 Su formato deberia ser "?YYYYDDDsss"
262
264
263 siendo:
265 siendo:
264 YYYY : Anio (ejemplo 2015)
266 YYYY : Anio (ejemplo 2015)
265 DDD : Dia del anio (ejemplo 305)
267 DDD : Dia del anio (ejemplo 305)
266 sss : set
268 sss : set
267
269
268 startDate : fecha inicial del rango seleccionado en formato datetime.date
270 startDate : fecha inicial del rango seleccionado en formato datetime.date
269
271
270 endDate : fecha final del rango seleccionado en formato datetime.date
272 endDate : fecha final del rango seleccionado en formato datetime.date
271
273
272 Return:
274 Return:
273 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
275 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
274 fecha especificado, de lo contrario retorna False.
276 fecha especificado, de lo contrario retorna False.
275 Excepciones:
277 Excepciones:
276 Si el archivo no tiene el formato adecuado
278 Si el archivo no tiene el formato adecuado
277 """
279 """
278
280
279 basename = os.path.basename(filename)
281 basename = os.path.basename(filename)
280
282
281 if not isRadarFile(basename):
283 if not isRadarFile(basename):
282 print("The filename %s has not the rigth format" % filename)
284 print("The filename %s has not the rigth format" % filename)
283 return 0
285 return 0
284
286
285 if startDate and endDate:
287 if startDate and endDate:
286 thisDate = getDateFromRadarFile(basename)
288 thisDate = getDateFromRadarFile(basename)
287
289
288 if thisDate < startDate:
290 if thisDate < startDate:
289 return 0
291 return 0
290
292
291 if thisDate > endDate:
293 if thisDate > endDate:
292 return 0
294 return 0
293
295
294 return 1
296 return 1
295
297
296
298
297 def getFileFromSet(path, ext, set):
299 def getFileFromSet(path, ext, set):
298 validFilelist = []
300 validFilelist = []
299 fileList = os.listdir(path)
301 fileList = os.listdir(path)
300
302
301 # 0 1234 567 89A BCDE
303 # 0 1234 567 89A BCDE
302 # H YYYY DDD SSS .ext
304 # H YYYY DDD SSS .ext
303
305
304 for thisFile in fileList:
306 for thisFile in fileList:
305 try:
307 try:
306 year = int(thisFile[1:5])
308 year = int(thisFile[1:5])
307 doy = int(thisFile[5:8])
309 doy = int(thisFile[5:8])
308 except:
310 except:
309 continue
311 continue
310
312
311 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
313 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
312 continue
314 continue
313
315
314 validFilelist.append(thisFile)
316 validFilelist.append(thisFile)
315
317
316 myfile = fnmatch.filter(
318 myfile = fnmatch.filter(
317 validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set))
319 validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set))
318
320
319 if len(myfile) != 0:
321 if len(myfile) != 0:
320 return myfile[0]
322 return myfile[0]
321 else:
323 else:
322 filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower())
324 filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower())
323 print('the filename %s does not exist' % filename)
325 print('the filename %s does not exist' % filename)
324 print('...going to the last file: ')
326 print('...going to the last file: ')
325
327
326 if validFilelist:
328 if validFilelist:
327 validFilelist = sorted(validFilelist, key=str.lower)
329 validFilelist = sorted(validFilelist, key=str.lower)
328 return validFilelist[-1]
330 return validFilelist[-1]
329
331
330 return None
332 return None
331
333
332
334
333 def getlastFileFromPath(path, ext):
335 def getlastFileFromPath(path, ext):
334 """
336 """
335 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
337 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
336 al final de la depuracion devuelve el ultimo file de la lista que quedo.
338 al final de la depuracion devuelve el ultimo file de la lista que quedo.
337
339
338 Input:
340 Input:
339 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
341 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
340 ext : extension de los files contenidos en una carpeta
342 ext : extension de los files contenidos en una carpeta
341
343
342 Return:
344 Return:
343 El ultimo file de una determinada carpeta, no se considera el path.
345 El ultimo file de una determinada carpeta, no se considera el path.
344 """
346 """
345 validFilelist = []
347 validFilelist = []
346 fileList = os.listdir(path)
348 fileList = os.listdir(path)
347
349
348 # 0 1234 567 89A BCDE
350 # 0 1234 567 89A BCDE
349 # H YYYY DDD SSS .ext
351 # H YYYY DDD SSS .ext
350
352
351 for thisFile in fileList:
353 for thisFile in fileList:
352
354
353 year = thisFile[1:5]
355 year = thisFile[1:5]
354 if not isNumber(year):
356 if not isNumber(year):
355 continue
357 continue
356
358
357 doy = thisFile[5:8]
359 doy = thisFile[5:8]
358 if not isNumber(doy):
360 if not isNumber(doy):
359 continue
361 continue
360
362
361 year = int(year)
363 year = int(year)
362 doy = int(doy)
364 doy = int(doy)
363
365
364 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
366 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
365 continue
367 continue
366
368
367 validFilelist.append(thisFile)
369 validFilelist.append(thisFile)
368
370
369 if validFilelist:
371 if validFilelist:
370 validFilelist = sorted(validFilelist, key=str.lower)
372 validFilelist = sorted(validFilelist, key=str.lower)
371 return validFilelist[-1]
373 return validFilelist[-1]
372
374
373 return None
375 return None
374
376
375
377
376 def isRadarFolder(folder):
378 def isRadarFolder(folder):
377 try:
379 try:
378 year = int(folder[1:5])
380 year = int(folder[1:5])
379 doy = int(folder[5:8])
381 doy = int(folder[5:8])
380 except:
382 except:
381 return 0
383 return 0
382
384
383 return 1
385 return 1
384
386
385
387
386 def isRadarFile(file):
388 def isRadarFile(file):
387 try:
389 try:
388 year = int(file[1:5])
390 year = int(file[1:5])
389 doy = int(file[5:8])
391 doy = int(file[5:8])
390 set = int(file[8:11])
392 set = int(file[8:11])
391 except:
393 except:
392 return 0
394 return 0
393
395
394 return 1
396 return 1
395
397
396
398
397 def getDateFromRadarFile(file):
399 def getDateFromRadarFile(file):
398 try:
400 try:
399 year = int(file[1:5])
401 year = int(file[1:5])
400 doy = int(file[5:8])
402 doy = int(file[5:8])
401 set = int(file[8:11])
403 set = int(file[8:11])
402 except:
404 except:
403 return None
405 return None
404
406
405 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
407 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
406 return thisDate
408 return thisDate
407
409
408
410
409 def getDateFromRadarFolder(folder):
411 def getDateFromRadarFolder(folder):
410 try:
412 try:
411 year = int(folder[1:5])
413 year = int(folder[1:5])
412 doy = int(folder[5:8])
414 doy = int(folder[5:8])
413 except:
415 except:
414 return None
416 return None
415
417
416 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
418 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
417 return thisDate
419 return thisDate
418
420
419 def parse_format(s, fmt):
421 def parse_format(s, fmt):
420
422
421 for i in range(fmt.count('%')):
423 for i in range(fmt.count('%')):
422 x = fmt.index('%')
424 x = fmt.index('%')
423 d = DT_DIRECTIVES[fmt[x:x+2]]
425 d = DT_DIRECTIVES[fmt[x:x+2]]
424 fmt = fmt.replace(fmt[x:x+2], s[x:x+d])
426 fmt = fmt.replace(fmt[x:x+2], s[x:x+d])
425 return fmt
427 return fmt
426
428
427 class Reader(object):
429 class Reader(object):
428
430
429 c = 3E8
431 c = 3E8
430 isConfig = False
432 isConfig = False
431 dtype = None
433 dtype = None
432 pathList = []
434 pathList = []
433 filenameList = []
435 filenameList = []
434 datetimeList = []
436 datetimeList = []
435 filename = None
437 filename = None
436 ext = None
438 ext = None
437 flagIsNewFile = 1
439 flagIsNewFile = 1
438 flagDiscontinuousBlock = 0
440 flagDiscontinuousBlock = 0
439 flagIsNewBlock = 0
441 flagIsNewBlock = 0
440 flagNoMoreFiles = 0
442 flagNoMoreFiles = 0
441 fp = None
443 fp = None
442 firstHeaderSize = 0
444 firstHeaderSize = 0
443 basicHeaderSize = 24
445 basicHeaderSize = 24
444 versionFile = 1103
446 versionFile = 1103
445 fileSize = None
447 fileSize = None
446 fileSizeByHeader = None
448 fileSizeByHeader = None
447 fileIndex = -1
449 fileIndex = -1
448 profileIndex = None
450 profileIndex = None
449 blockIndex = 0
451 blockIndex = 0
450 nTotalBlocks = 0
452 nTotalBlocks = 0
451 maxTimeStep = 30
453 maxTimeStep = 30
452 lastUTTime = None
454 lastUTTime = None
453 datablock = None
455 datablock = None
454 dataOut = None
456 dataOut = None
455 getByBlock = False
457 getByBlock = False
456 path = None
458 path = None
457 startDate = None
459 startDate = None
458 endDate = None
460 endDate = None
459 startTime = datetime.time(0, 0, 0)
461 startTime = datetime.time(0, 0, 0)
460 endTime = datetime.time(23, 59, 59)
462 endTime = datetime.time(23, 59, 59)
461 set = None
463 set = None
462 expLabel = ""
464 expLabel = ""
463 online = False
465 online = False
464 delay = 60
466 delay = 60
465 nTries = 3 # quantity tries
467 nTries = 3 # quantity tries
466 nFiles = 3 # number of files for searching
468 nFiles = 3 # number of files for searching
467 walk = True
469 walk = True
468 getblock = False
470 getblock = False
469 nTxs = 1
471 nTxs = 1
470 realtime = False
472 realtime = False
471 blocksize = 0
473 blocksize = 0
472 blocktime = None
474 blocktime = None
473 warnings = True
475 warnings = True
474 verbose = True
476 verbose = True
475 server = None
477 server = None
476 format = None
478 format = None
477 oneDDict = None
479 oneDDict = None
478 twoDDict = None
480 twoDDict = None
479 independentParam = None
481 independentParam = None
480 filefmt = None
482 filefmt = None
481 folderfmt = None
483 folderfmt = None
482 open_file = open
484 open_file = open
483 open_mode = 'rb'
485 open_mode = 'rb'
484
486
485 def run(self):
487 def run(self):
486
488
487 raise NotImplementedError
489 raise NotImplementedError
488
490
489 def getAllowedArgs(self):
491 def getAllowedArgs(self):
490 if hasattr(self, '__attrs__'):
492 if hasattr(self, '__attrs__'):
491 return self.__attrs__
493 return self.__attrs__
492 else:
494 else:
493 return inspect.getargspec(self.run).args
495 return inspect.getargspec(self.run).args
494
496
495 def set_kwargs(self, **kwargs):
497 def set_kwargs(self, **kwargs):
496
498
497 for key, value in kwargs.items():
499 for key, value in kwargs.items():
498 setattr(self, key, value)
500 setattr(self, key, value)
499
501
500 def find_folders(self, path, startDate, endDate, folderfmt, last=False):
502 def find_folders(self, path, startDate, endDate, folderfmt, last=False):
501
503
502 folders = [x for f in path.split(',')
504 folders = [x for f in path.split(',')
503 for x in os.listdir(f) if os.path.isdir(os.path.join(f, x))]
505 for x in os.listdir(f) if os.path.isdir(os.path.join(f, x))]
504 folders.sort()
506 folders.sort()
505
507
506 if last:
508 if last:
507 folders = [folders[-1]]
509 folders = [folders[-1]]
508
510
509 for folder in folders:
511 for folder in folders:
510 try:
512 try:
511 dt = datetime.datetime.strptime(parse_format(folder, folderfmt), folderfmt).date()
513 dt = datetime.datetime.strptime(parse_format(folder, folderfmt), folderfmt).date()
512 if dt >= startDate and dt <= endDate:
514 if dt >= startDate and dt <= endDate:
513 yield os.path.join(path, folder)
515 yield os.path.join(path, folder)
514 else:
516 else:
515 log.log('Skiping folder {}'.format(folder), self.name)
517 log.log('Skiping folder {}'.format(folder), self.name)
516 except Exception as e:
518 except Exception as e:
517 log.log('Skiping folder {}'.format(folder), self.name)
519 log.log('Skiping folder {}'.format(folder), self.name)
518 continue
520 continue
519 return
521 return
520
522
521 def find_files(self, folders, ext, filefmt, startDate=None, endDate=None,
523 def find_files(self, folders, ext, filefmt, startDate=None, endDate=None,
522 expLabel='', last=False):
524 expLabel='', last=False):
523
525
524 for path in folders:
526 for path in folders:
525 files = glob.glob1(path, '*{}'.format(ext))
527 files = glob.glob1(path, '*{}'.format(ext))
526 files.sort()
528 files.sort()
527 if last:
529 if last:
528 if files:
530 if files:
529 fo = files[-1]
531 fo = files[-1]
530 try:
532 try:
531 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
533 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
532 yield os.path.join(path, expLabel, fo)
534 yield os.path.join(path, expLabel, fo)
533 except Exception as e:
535 except Exception as e:
534 pass
536 pass
535 return
537 return
536 else:
538 else:
537 return
539 return
538
540
539 for fo in files:
541 for fo in files:
540 try:
542 try:
541 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
543 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
544 #print(dt)
545 #print(startDate)
546 #print(endDate)
542 if dt >= startDate and dt <= endDate:
547 if dt >= startDate and dt <= endDate:
548
543 yield os.path.join(path, expLabel, fo)
549 yield os.path.join(path, expLabel, fo)
550
544 else:
551 else:
552
545 log.log('Skiping file {}'.format(fo), self.name)
553 log.log('Skiping file {}'.format(fo), self.name)
546 except Exception as e:
554 except Exception as e:
547 log.log('Skiping file {}'.format(fo), self.name)
555 log.log('Skiping file {}'.format(fo), self.name)
548 continue
556 continue
549
557
550 def searchFilesOffLine(self, path, startDate, endDate,
558 def searchFilesOffLine(self, path, startDate, endDate,
551 expLabel, ext, walk,
559 expLabel, ext, walk,
552 filefmt, folderfmt):
560 filefmt, folderfmt):
553 """Search files in offline mode for the given arguments
561 """Search files in offline mode for the given arguments
554
562
555 Return:
563 Return:
556 Generator of files
564 Generator of files
557 """
565 """
558
566
559 if walk:
567 if walk:
560 folders = self.find_folders(
568 folders = self.find_folders(
561 path, startDate, endDate, folderfmt)
569 path, startDate, endDate, folderfmt)
562 else:
570 else:
563 folders = path.split(',')
571 folders = path.split(',')
564
572
565 return self.find_files(
573 return self.find_files(
566 folders, ext, filefmt, startDate, endDate, expLabel)
574 folders, ext, filefmt, startDate, endDate, expLabel)
567
575
568 def searchFilesOnLine(self, path, startDate, endDate,
576 def searchFilesOnLine(self, path, startDate, endDate,
569 expLabel, ext, walk,
577 expLabel, ext, walk,
570 filefmt, folderfmt):
578 filefmt, folderfmt):
571 """Search for the last file of the last folder
579 """Search for the last file of the last folder
572
580
573 Arguments:
581 Arguments:
574 path : carpeta donde estan contenidos los files que contiene data
582 path : carpeta donde estan contenidos los files que contiene data
575 expLabel : Nombre del subexperimento (subfolder)
583 expLabel : Nombre del subexperimento (subfolder)
576 ext : extension de los files
584 ext : extension de los files
577 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
585 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
578
586
579 Return:
587 Return:
580 generator with the full path of last filename
588 generator with the full path of last filename
581 """
589 """
582
590
583 if walk:
591 if walk:
584 folders = self.find_folders(
592 folders = self.find_folders(
585 path, startDate, endDate, folderfmt, last=True)
593 path, startDate, endDate, folderfmt, last=True)
586 else:
594 else:
587 folders = path.split(',')
595 folders = path.split(',')
588
596
589 return self.find_files(
597 return self.find_files(
590 folders, ext, filefmt, startDate, endDate, expLabel, last=True)
598 folders, ext, filefmt, startDate, endDate, expLabel, last=True)
591
599
592 def setNextFile(self):
600 def setNextFile(self):
593 """Set the next file to be readed open it and parse de file header"""
601 """Set the next file to be readed open it and parse de file header"""
594
602
603 #print("fp: ",self.fp)
595 while True:
604 while True:
605
606 #print(self.fp)
596 if self.fp != None:
607 if self.fp != None:
597 self.fp.close()
608 self.fp.close()
598
609
610 #print("setNextFile")
611 #print("BEFORE OPENING",self.filename)
599 if self.online:
612 if self.online:
600 newFile = self.setNextFileOnline()
613 newFile = self.setNextFileOnline()
614
601 else:
615 else:
616
602 newFile = self.setNextFileOffline()
617 newFile = self.setNextFileOffline()
603
618
619 #print("newFile: ",newFile)
604 if not(newFile):
620 if not(newFile):
621
605 if self.online:
622 if self.online:
606 raise schainpy.admin.SchainError('Time to wait for new files reach')
623 raise schainpy.admin.SchainError('Time to wait for new files reach')
607 else:
624 else:
608 if self.fileIndex == -1:
625 if self.fileIndex == -1:
626 #print("OKK")
609 raise schainpy.admin.SchainWarning('No files found in the given path')
627 raise schainpy.admin.SchainWarning('No files found in the given path')
610 else:
628 else:
629
611 raise schainpy.admin.SchainWarning('No more files to read')
630 raise schainpy.admin.SchainWarning('No more files to read')
612
631
613 if self.verifyFile(self.filename):
632 if self.verifyFile(self.filename):
633
614 break
634 break
615
635
636 ##print("BEFORE OPENING",self.filename)
637
616 log.log('Opening file: %s' % self.filename, self.name)
638 log.log('Opening file: %s' % self.filename, self.name)
617
639
618 self.readFirstHeader()
640 self.readFirstHeader()
619 self.nReadBlocks = 0
641 self.nReadBlocks = 0
620
642
621 def setNextFileOnline(self):
643 def setNextFileOnline(self):
622 """Check for the next file to be readed in online mode.
644 """Check for the next file to be readed in online mode.
623
645
624 Set:
646 Set:
625 self.filename
647 self.filename
626 self.fp
648 self.fp
627 self.filesize
649 self.filesize
628
650
629 Return:
651 Return:
630 boolean
652 boolean
631
653
632 """
654 """
655
633 nextFile = True
656 nextFile = True
634 nextDay = False
657 nextDay = False
635
658
636 for nFiles in range(self.nFiles+1):
659 for nFiles in range(self.nFiles+1):
637 for nTries in range(self.nTries):
660 for nTries in range(self.nTries):
638 fullfilename, filename = self.checkForRealPath(nextFile, nextDay)
661 fullfilename, filename = self.checkForRealPath(nextFile, nextDay)
639 if fullfilename is not None:
662 if fullfilename is not None:
640 break
663 break
641 log.warning(
664 log.warning(
642 "Waiting %0.2f sec for the next file: \"%s\" , try %02d ..." % (self.delay, filename, nTries + 1),
665 "Waiting %0.2f sec for the next file: \"%s\" , try %02d ..." % (self.delay, filename, nTries + 1),
643 self.name)
666 self.name)
644 time.sleep(self.delay)
667 time.sleep(self.delay)
645 nextFile = False
668 nextFile = False
646 continue
669 continue
647
670
648 if fullfilename is not None:
671 if fullfilename is not None:
649 break
672 break
650
673
651 self.nTries = 1
674 #self.nTries = 1
652 nextFile = True
675 nextFile = True
653
676
654 if nFiles == (self.nFiles - 1):
677 if nFiles == (self.nFiles - 1):
655 log.log('Trying with next day...', self.name)
678 log.log('Trying with next day...', self.name)
656 nextDay = True
679 nextDay = True
657 self.nTries = 3
680 self.nTries = 3
658
681
659 if fullfilename:
682 if fullfilename:
660 self.fileSize = os.path.getsize(fullfilename)
683 self.fileSize = os.path.getsize(fullfilename)
661 self.filename = fullfilename
684 self.filename = fullfilename
662 self.flagIsNewFile = 1
685 self.flagIsNewFile = 1
663 if self.fp != None:
686 if self.fp != None:
664 self.fp.close()
687 self.fp.close()
688 #print(fullfilename)
665 self.fp = self.open_file(fullfilename, self.open_mode)
689 self.fp = self.open_file(fullfilename, self.open_mode)
690
666 self.flagNoMoreFiles = 0
691 self.flagNoMoreFiles = 0
667 self.fileIndex += 1
692 self.fileIndex += 1
668 return 1
693 return 1
669 else:
694 else:
670 return 0
695 return 0
671
696
672 def setNextFileOffline(self):
697 def setNextFileOffline(self):
673 """Open the next file to be readed in offline mode"""
698 """Open the next file to be readed in offline mode"""
674
699
675 try:
700 try:
676 filename = next(self.filenameList)
701 filename = next(self.filenameList)
677 self.fileIndex +=1
702 self.fileIndex +=1
678 except StopIteration:
703 except StopIteration:
679 self.flagNoMoreFiles = 1
704 self.flagNoMoreFiles = 1
680 return 0
705 return 0
681
706 #print(self.fileIndex)
707 #print(filename)
682 self.filename = filename
708 self.filename = filename
683 self.fileSize = os.path.getsize(filename)
709 self.fileSize = os.path.getsize(filename)
684 self.fp = self.open_file(filename, self.open_mode)
710 self.fp = self.open_file(filename, self.open_mode)
685 self.flagIsNewFile = 1
711 self.flagIsNewFile = 1
686
712
687 return 1
713 return 1
688
714
689 @staticmethod
715 @staticmethod
690 def isDateTimeInRange(dt, startDate, endDate, startTime, endTime):
716 def isDateTimeInRange(dt, startDate, endDate, startTime, endTime):
691 """Check if the given datetime is in range"""
717 """Check if the given datetime is in range"""
692
718
693 if startDate <= dt.date() <= endDate:
719 if startDate <= dt.date() <= endDate:
694 if startTime <= dt.time() <= endTime:
720 if startTime <= dt.time() <= endTime:
695 return True
721 return True
696 return False
722 return False
697
723
698 def verifyFile(self, filename):
724 def verifyFile(self, filename):
699 """Check for a valid file
725 """Check for a valid file
700
726
701 Arguments:
727 Arguments:
702 filename -- full path filename
728 filename -- full path filename
703
729
704 Return:
730 Return:
705 boolean
731 boolean
706 """
732 """
707
733
708 return True
734 return True
709
735
710 def checkForRealPath(self, nextFile, nextDay):
736 def checkForRealPath(self, nextFile, nextDay):
711 """Check if the next file to be readed exists"""
737 """Check if the next file to be readed exists"""
712
738
713 raise NotImplementedError
739 raise NotImplementedError
714
740
715 def readFirstHeader(self):
741 def readFirstHeader(self):
716 """Parse the file header"""
742 """Parse the file header"""
717
743
744
718 pass
745 pass
719
746
720 def waitDataBlock(self, pointer_location, blocksize=None):
747 def waitDataBlock(self, pointer_location, blocksize=None):
721 """
748 """
722 """
749 """
723
750
724 currentPointer = pointer_location
751 currentPointer = pointer_location
725 if blocksize is None:
752 if blocksize is None:
726 neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize
753 neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize
727 else:
754 else:
728 neededSize = blocksize
755 neededSize = blocksize
729
756
730 for nTries in range(self.nTries):
757 for nTries in range(self.nTries):
731 self.fp.close()
758 self.fp.close()
732 self.fp = open(self.filename, 'rb')
759 self.fp = open(self.filename, 'rb')
733 self.fp.seek(currentPointer)
760 self.fp.seek(currentPointer)
734
761
735 self.fileSize = os.path.getsize(self.filename)
762 self.fileSize = os.path.getsize(self.filename)
736 currentSize = self.fileSize - currentPointer
763 currentSize = self.fileSize - currentPointer
737
764
738 if (currentSize >= neededSize):
765 if (currentSize >= neededSize):
739 return 1
766 return 1
740
767
741 log.warning(
768 log.warning(
742 "Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1),
769 "Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1),
743 self.name
770 self.name
744 )
771 )
745 time.sleep(self.delay)
772 time.sleep(self.delay)
746
773
747 return 0
774 return 0
748
775
749 class JRODataReader(Reader):
776 class JRODataReader(Reader):
750
777
751 utc = 0
778 utc = 0
752 nReadBlocks = 0
779 nReadBlocks = 0
753 foldercounter = 0
780 foldercounter = 0
754 firstHeaderSize = 0
781 firstHeaderSize = 0
755 basicHeaderSize = 24
782 basicHeaderSize = 24
756 __isFirstTimeOnline = 1
783 __isFirstTimeOnline = 1
757 filefmt = "*%Y%j***"
784 filefmt = "*%Y%j***"
758 folderfmt = "*%Y%j"
785 folderfmt = "*%Y%j"
759 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'online', 'delay', 'walk']
786 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'online', 'delay', 'walk']
760
787
761 def getDtypeWidth(self):
788 def getDtypeWidth(self):
762
789
763 dtype_index = get_dtype_index(self.dtype)
790 dtype_index = get_dtype_index(self.dtype)
764 dtype_width = get_dtype_width(dtype_index)
791 dtype_width = get_dtype_width(dtype_index)
765
792
766 return dtype_width
793 return dtype_width
767
794
768 def checkForRealPath(self, nextFile, nextDay):
795 def checkForRealPath(self, nextFile, nextDay):
769 """Check if the next file to be readed exists.
796 """Check if the next file to be readed exists.
770
797
771 Example :
798 Example :
772 nombre correcto del file es .../.../D2009307/P2009307367.ext
799 nombre correcto del file es .../.../D2009307/P2009307367.ext
773
800
774 Entonces la funcion prueba con las siguientes combinaciones
801 Entonces la funcion prueba con las siguientes combinaciones
775 .../.../y2009307367.ext
802 .../.../y2009307367.ext
776 .../.../Y2009307367.ext
803 .../.../Y2009307367.ext
777 .../.../x2009307/y2009307367.ext
804 .../.../x2009307/y2009307367.ext
778 .../.../x2009307/Y2009307367.ext
805 .../.../x2009307/Y2009307367.ext
779 .../.../X2009307/y2009307367.ext
806 .../.../X2009307/y2009307367.ext
780 .../.../X2009307/Y2009307367.ext
807 .../.../X2009307/Y2009307367.ext
781 siendo para este caso, la ultima combinacion de letras, identica al file buscado
808 siendo para este caso, la ultima combinacion de letras, identica al file buscado
782
809
783 Return:
810 Return:
784 str -- fullpath of the file
811 str -- fullpath of the file
785 """
812 """
786
813
787
814
788 if nextFile:
815 if nextFile:
789 self.set += 1
816 self.set += 1
790 if nextDay:
817 if nextDay:
791 self.set = 0
818 self.set = 0
792 self.doy += 1
819 self.doy += 1
793 foldercounter = 0
820 foldercounter = 0
794 prefixDirList = [None, 'd', 'D']
821 prefixDirList = [None, 'd', 'D']
795 if self.ext.lower() == ".r": # voltage
822 if self.ext.lower() == ".r": # voltage
796 prefixFileList = ['d', 'D']
823 prefixFileList = ['d', 'D']
797 elif self.ext.lower() == ".pdata": # spectra
824 elif self.ext.lower() == ".pdata": # spectra
798 prefixFileList = ['p', 'P']
825 prefixFileList = ['p', 'P']
799
826
827 ##############DP##############
828
829 elif self.ext.lower() == ".dat": # dat
830 prefixFileList = ['z', 'Z']
831
832
833
834 ##############DP##############
800 # barrido por las combinaciones posibles
835 # barrido por las combinaciones posibles
801 for prefixDir in prefixDirList:
836 for prefixDir in prefixDirList:
802 thispath = self.path
837 thispath = self.path
803 if prefixDir != None:
838 if prefixDir != None:
804 # formo el nombre del directorio xYYYYDDD (x=d o x=D)
839 # formo el nombre del directorio xYYYYDDD (x=d o x=D)
805 if foldercounter == 0:
840 if foldercounter == 0:
806 thispath = os.path.join(self.path, "%s%04d%03d" %
841 thispath = os.path.join(self.path, "%s%04d%03d" %
807 (prefixDir, self.year, self.doy))
842 (prefixDir, self.year, self.doy))
808 else:
843 else:
809 thispath = os.path.join(self.path, "%s%04d%03d_%02d" % (
844 thispath = os.path.join(self.path, "%s%04d%03d_%02d" % (
810 prefixDir, self.year, self.doy, foldercounter))
845 prefixDir, self.year, self.doy, foldercounter))
811 for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D"
846 for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D"
812 # formo el nombre del file xYYYYDDDSSS.ext
847 # formo el nombre del file xYYYYDDDSSS.ext
813 filename = "%s%04d%03d%03d%s" % (prefixFile, self.year, self.doy, self.set, self.ext)
848 filename = "%s%04d%03d%03d%s" % (prefixFile, self.year, self.doy, self.set, self.ext)
814 fullfilename = os.path.join(
849 fullfilename = os.path.join(
815 thispath, filename)
850 thispath, filename)
816
851
817 if os.path.exists(fullfilename):
852 if os.path.exists(fullfilename):
818 return fullfilename, filename
853 return fullfilename, filename
819
854
820 return None, filename
855 return None, filename
821
856
822 def __waitNewBlock(self):
857 def __waitNewBlock(self):
823 """
858 """
824 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
859 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
825
860
826 Si el modo de lectura es OffLine siempre retorn 0
861 Si el modo de lectura es OffLine siempre retorn 0
827 """
862 """
828 if not self.online:
863 if not self.online:
829 return 0
864 return 0
830
865
831 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
866 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
832 return 0
867 return 0
833
868
834 currentPointer = self.fp.tell()
869 currentPointer = self.fp.tell()
835
870
836 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
871 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
837
872
838 for nTries in range(self.nTries):
873 for nTries in range(self.nTries):
839
874
840 self.fp.close()
875 self.fp.close()
841 self.fp = open(self.filename, 'rb')
876 self.fp = open(self.filename, 'rb')
842 self.fp.seek(currentPointer)
877 self.fp.seek(currentPointer)
843
878
844 self.fileSize = os.path.getsize(self.filename)
879 self.fileSize = os.path.getsize(self.filename)
845 currentSize = self.fileSize - currentPointer
880 currentSize = self.fileSize - currentPointer
846
881
847 if (currentSize >= neededSize):
882 if (currentSize >= neededSize):
848 self.basicHeaderObj.read(self.fp)
883 self.basicHeaderObj.read(self.fp)
849 return 1
884 return 1
850
885
851 if self.fileSize == self.fileSizeByHeader:
886 if self.fileSize == self.fileSizeByHeader:
852 # self.flagEoF = True
887 # self.flagEoF = True
853 return 0
888 return 0
854
889
855 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
890 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
891 #print(self.filename)
856 time.sleep(self.delay)
892 time.sleep(self.delay)
857
893
858 return 0
894 return 0
859
895
860 def __setNewBlock(self):
896 def __setNewBlock(self):
861
897
862 if self.fp == None:
898 if self.fp == None:
863 return 0
899 return 0
864
900
865 if self.flagIsNewFile:
901 if self.flagIsNewFile:
866 self.lastUTTime = self.basicHeaderObj.utc
902 self.lastUTTime = self.basicHeaderObj.utc
867 return 1
903 return 1
868
904
869 if self.realtime:
905 if self.realtime:
870 self.flagDiscontinuousBlock = 1
906 self.flagDiscontinuousBlock = 1
871 if not(self.setNextFile()):
907 if not(self.setNextFile()):
872 return 0
908 return 0
873 else:
909 else:
874 return 1
910 return 1
875
911
876 currentSize = self.fileSize - self.fp.tell()
912 currentSize = self.fileSize - self.fp.tell()
877 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
913 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
878
914
879 if (currentSize >= neededSize):
915 if (currentSize >= neededSize):
880 self.basicHeaderObj.read(self.fp)
916 self.basicHeaderObj.read(self.fp)
881 self.lastUTTime = self.basicHeaderObj.utc
917 self.lastUTTime = self.basicHeaderObj.utc
882 return 1
918 return 1
883
919
884 if self.__waitNewBlock():
920 if self.__waitNewBlock():
885 self.lastUTTime = self.basicHeaderObj.utc
921 self.lastUTTime = self.basicHeaderObj.utc
886 return 1
922 return 1
887
923
888 if not(self.setNextFile()):
924 if not(self.setNextFile()):
889 return 0
925 return 0
890
926
891 deltaTime = self.basicHeaderObj.utc - self.lastUTTime
927 deltaTime = self.basicHeaderObj.utc - self.lastUTTime
892 self.lastUTTime = self.basicHeaderObj.utc
928 self.lastUTTime = self.basicHeaderObj.utc
893
929
894 self.flagDiscontinuousBlock = 0
930 self.flagDiscontinuousBlock = 0
895
931
896 if deltaTime > self.maxTimeStep:
932 if deltaTime > self.maxTimeStep:
897 self.flagDiscontinuousBlock = 1
933 self.flagDiscontinuousBlock = 1
898
934
899 return 1
935 return 1
900
936
901 def readNextBlock(self):
937 def readNextBlock(self):
902
938
903 while True:
939 while True:
904 if not(self.__setNewBlock()):
940 if not(self.__setNewBlock()):
905 continue
941 continue
906
942
907 if not(self.readBlock()):
943 if not(self.readBlock()):
908 return 0
944 return 0
909
945
910 self.getBasicHeader()
946 self.getBasicHeader()
911
947
912 if not self.isDateTimeInRange(self.dataOut.datatime, self.startDate, self.endDate, self.startTime, self.endTime):
948 if not self.isDateTimeInRange(self.dataOut.datatime, self.startDate, self.endDate, self.startTime, self.endTime):
913 print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
949 print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
914 self.processingHeaderObj.dataBlocksPerFile,
950 self.processingHeaderObj.dataBlocksPerFile,
915 self.dataOut.datatime.ctime()))
951 self.dataOut.datatime.ctime()))
916 continue
952 continue
917
953
918 break
954 break
919
955
920 if self.verbose:
956 if self.verbose:
921 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
957 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
922 self.processingHeaderObj.dataBlocksPerFile,
958 self.processingHeaderObj.dataBlocksPerFile,
923 self.dataOut.datatime.ctime()))
959 self.dataOut.datatime.ctime()))
960 #################DP#################
961 self.dataOut.TimeBlockDate=self.dataOut.datatime.ctime()
962 self.dataOut.TimeBlockSeconds=time.mktime(time.strptime(self.dataOut.datatime.ctime()))
963 #################DP#################
924 return 1
964 return 1
925
965
926 def readFirstHeader(self):
966 def readFirstHeader(self):
927
967
928 self.basicHeaderObj.read(self.fp)
968 self.basicHeaderObj.read(self.fp)
929 self.systemHeaderObj.read(self.fp)
969 self.systemHeaderObj.read(self.fp)
930 self.radarControllerHeaderObj.read(self.fp)
970 self.radarControllerHeaderObj.read(self.fp)
931 self.processingHeaderObj.read(self.fp)
971 self.processingHeaderObj.read(self.fp)
932 self.firstHeaderSize = self.basicHeaderObj.size
972 self.firstHeaderSize = self.basicHeaderObj.size
933
973
934 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
974 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
935 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
975 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
936 if datatype == 0:
976 if datatype == 0:
937 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
977 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
938 elif datatype == 1:
978 elif datatype == 1:
939 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
979 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
940 elif datatype == 2:
980 elif datatype == 2:
941 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
981 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
942 elif datatype == 3:
982 elif datatype == 3:
943 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
983 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
944 elif datatype == 4:
984 elif datatype == 4:
945 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
985 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
946 elif datatype == 5:
986 elif datatype == 5:
947 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
987 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
948 else:
988 else:
949 raise ValueError('Data type was not defined')
989 raise ValueError('Data type was not defined')
950
990
951 self.dtype = datatype_str
991 self.dtype = datatype_str
952 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
992 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
953 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
993 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
954 self.firstHeaderSize + self.basicHeaderSize * \
994 self.firstHeaderSize + self.basicHeaderSize * \
955 (self.processingHeaderObj.dataBlocksPerFile - 1)
995 (self.processingHeaderObj.dataBlocksPerFile - 1)
956 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
996 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
957 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
997 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
958 self.getBlockDimension()
998 self.getBlockDimension()
959
999
960 def verifyFile(self, filename):
1000 def verifyFile(self, filename):
961
1001
962 flag = True
1002 flag = True
963
1003
964 try:
1004 try:
965 fp = open(filename, 'rb')
1005 fp = open(filename, 'rb')
966 except IOError:
1006 except IOError:
967 log.error("File {} can't be opened".format(filename), self.name)
1007 log.error("File {} can't be opened".format(filename), self.name)
968 return False
1008 return False
969
1009
970 if self.online and self.waitDataBlock(0):
1010 if self.online and self.waitDataBlock(0):
971 pass
1011 pass
972
1012
973 basicHeaderObj = BasicHeader(LOCALTIME)
1013 basicHeaderObj = BasicHeader(LOCALTIME)
974 systemHeaderObj = SystemHeader()
1014 systemHeaderObj = SystemHeader()
975 radarControllerHeaderObj = RadarControllerHeader()
1015 radarControllerHeaderObj = RadarControllerHeader()
976 processingHeaderObj = ProcessingHeader()
1016 processingHeaderObj = ProcessingHeader()
977
1017
978 if not(basicHeaderObj.read(fp)):
1018 if not(basicHeaderObj.read(fp)):
979 flag = False
1019 flag = False
980 if not(systemHeaderObj.read(fp)):
1020 if not(systemHeaderObj.read(fp)):
981 flag = False
1021 flag = False
982 if not(radarControllerHeaderObj.read(fp)):
1022 if not(radarControllerHeaderObj.read(fp)):
983 flag = False
1023 flag = False
984 if not(processingHeaderObj.read(fp)):
1024 if not(processingHeaderObj.read(fp)):
985 flag = False
1025 flag = False
986 if not self.online:
1026 if not self.online:
987 dt1 = basicHeaderObj.datatime
1027 dt1 = basicHeaderObj.datatime
988 pos = self.fileSize-processingHeaderObj.blockSize-24
1028 pos = self.fileSize-processingHeaderObj.blockSize-24
989 if pos<0:
1029 if pos<0:
990 flag = False
1030 flag = False
991 log.error('Invalid size for file: {}'.format(self.filename), self.name)
1031 log.error('Invalid size for file: {}'.format(self.filename), self.name)
992 else:
1032 else:
993 fp.seek(pos)
1033 fp.seek(pos)
994 if not(basicHeaderObj.read(fp)):
1034 if not(basicHeaderObj.read(fp)):
995 flag = False
1035 flag = False
996 dt2 = basicHeaderObj.datatime
1036 dt2 = basicHeaderObj.datatime
997 if not self.isDateTimeInRange(dt1, self.startDate, self.endDate, self.startTime, self.endTime) and not \
1037 if not self.isDateTimeInRange(dt1, self.startDate, self.endDate, self.startTime, self.endTime) and not \
998 self.isDateTimeInRange(dt2, self.startDate, self.endDate, self.startTime, self.endTime):
1038 self.isDateTimeInRange(dt2, self.startDate, self.endDate, self.startTime, self.endTime):
999 flag = False
1039 flag = False
1000
1040
1001 fp.close()
1041 fp.close()
1002 return flag
1042 return flag
1003
1043
1004 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1044 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1005
1045
1006 path_empty = True
1046 path_empty = True
1007
1047
1008 dateList = []
1048 dateList = []
1009 pathList = []
1049 pathList = []
1010
1050
1011 multi_path = path.split(',')
1051 multi_path = path.split(',')
1012
1052
1013 if not walk:
1053 if not walk:
1014
1054
1015 for single_path in multi_path:
1055 for single_path in multi_path:
1016
1056
1017 if not os.path.isdir(single_path):
1057 if not os.path.isdir(single_path):
1018 continue
1058 continue
1019
1059
1020 fileList = glob.glob1(single_path, "*" + ext)
1060 fileList = glob.glob1(single_path, "*" + ext)
1021
1061
1022 if not fileList:
1062 if not fileList:
1023 continue
1063 continue
1024
1064
1025 path_empty = False
1065 path_empty = False
1026
1066
1027 fileList.sort()
1067 fileList.sort()
1028
1068
1029 for thisFile in fileList:
1069 for thisFile in fileList:
1030
1070
1031 if not os.path.isfile(os.path.join(single_path, thisFile)):
1071 if not os.path.isfile(os.path.join(single_path, thisFile)):
1032 continue
1072 continue
1033
1073
1034 if not isRadarFile(thisFile):
1074 if not isRadarFile(thisFile):
1035 continue
1075 continue
1036
1076
1037 if not isFileInDateRange(thisFile, startDate, endDate):
1077 if not isFileInDateRange(thisFile, startDate, endDate):
1038 continue
1078 continue
1039
1079
1040 thisDate = getDateFromRadarFile(thisFile)
1080 thisDate = getDateFromRadarFile(thisFile)
1041
1081
1042 if thisDate in dateList or single_path in pathList:
1082 if thisDate in dateList or single_path in pathList:
1043 continue
1083 continue
1044
1084
1045 dateList.append(thisDate)
1085 dateList.append(thisDate)
1046 pathList.append(single_path)
1086 pathList.append(single_path)
1047
1087
1048 else:
1088 else:
1049 for single_path in multi_path:
1089 for single_path in multi_path:
1050
1090
1051 if not os.path.isdir(single_path):
1091 if not os.path.isdir(single_path):
1052 continue
1092 continue
1053
1093
1054 dirList = []
1094 dirList = []
1055
1095
1056 for thisPath in os.listdir(single_path):
1096 for thisPath in os.listdir(single_path):
1057
1097
1058 if not os.path.isdir(os.path.join(single_path, thisPath)):
1098 if not os.path.isdir(os.path.join(single_path, thisPath)):
1059 continue
1099 continue
1060
1100
1061 if not isRadarFolder(thisPath):
1101 if not isRadarFolder(thisPath):
1062 continue
1102 continue
1063
1103
1064 if not isFolderInDateRange(thisPath, startDate, endDate):
1104 if not isFolderInDateRange(thisPath, startDate, endDate):
1065 continue
1105 continue
1066
1106
1067 dirList.append(thisPath)
1107 dirList.append(thisPath)
1068
1108
1069 if not dirList:
1109 if not dirList:
1070 continue
1110 continue
1071
1111
1072 dirList.sort()
1112 dirList.sort()
1073
1113
1074 for thisDir in dirList:
1114 for thisDir in dirList:
1075
1115
1076 datapath = os.path.join(single_path, thisDir, expLabel)
1116 datapath = os.path.join(single_path, thisDir, expLabel)
1077 fileList = glob.glob1(datapath, "*" + ext)
1117 fileList = glob.glob1(datapath, "*" + ext)
1078
1118
1079 if not fileList:
1119 if not fileList:
1080 continue
1120 continue
1081
1121
1082 path_empty = False
1122 path_empty = False
1083
1123
1084 thisDate = getDateFromRadarFolder(thisDir)
1124 thisDate = getDateFromRadarFolder(thisDir)
1085
1125
1086 pathList.append(datapath)
1126 pathList.append(datapath)
1087 dateList.append(thisDate)
1127 dateList.append(thisDate)
1088
1128
1089 dateList.sort()
1129 dateList.sort()
1090
1130
1091 if walk:
1131 if walk:
1092 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1132 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1093 else:
1133 else:
1094 pattern_path = multi_path[0]
1134 pattern_path = multi_path[0]
1095
1135
1096 if path_empty:
1136 if path_empty:
1097 raise schainpy.admin.SchainError("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate))
1137 raise schainpy.admin.SchainError("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate))
1098 else:
1138 else:
1099 if not dateList:
1139 if not dateList:
1100 raise schainpy.admin.SchainError("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path))
1140 raise schainpy.admin.SchainError("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path))
1101
1141
1102 if include_path:
1142 if include_path:
1103 return dateList, pathList
1143 return dateList, pathList
1104
1144
1105 return dateList
1145 return dateList
1106
1146
1107 def setup(self, **kwargs):
1147 def setup(self, **kwargs):
1108
1148
1109 self.set_kwargs(**kwargs)
1149 self.set_kwargs(**kwargs)
1110 if not self.ext.startswith('.'):
1150 if not self.ext.startswith('.'):
1111 self.ext = '.{}'.format(self.ext)
1151 self.ext = '.{}'.format(self.ext)
1112
1152
1113 if self.server is not None:
1153 if self.server is not None:
1114 if 'tcp://' in self.server:
1154 if 'tcp://' in self.server:
1115 address = server
1155 address = server
1116 else:
1156 else:
1117 address = 'ipc:///tmp/%s' % self.server
1157 address = 'ipc:///tmp/%s' % self.server
1118 self.server = address
1158 self.server = address
1119 self.context = zmq.Context()
1159 self.context = zmq.Context()
1120 self.receiver = self.context.socket(zmq.PULL)
1160 self.receiver = self.context.socket(zmq.PULL)
1121 self.receiver.connect(self.server)
1161 self.receiver.connect(self.server)
1122 time.sleep(0.5)
1162 time.sleep(0.5)
1123 print('[Starting] ReceiverData from {}'.format(self.server))
1163 print('[Starting] ReceiverData from {}'.format(self.server))
1124 else:
1164 else:
1125 self.server = None
1165 self.server = None
1126 if self.path == None:
1166 if self.path == None:
1127 raise ValueError("[Reading] The path is not valid")
1167 raise ValueError("[Reading] The path is not valid")
1128
1168
1129 if self.online:
1169 if self.online:
1130 log.log("[Reading] Searching files in online mode...", self.name)
1170 log.log("[Reading] Searching files in online mode...", self.name)
1131
1171
1132 for nTries in range(self.nTries):
1172 for nTries in range(self.nTries):
1133 fullpath = self.searchFilesOnLine(self.path, self.startDate,
1173 fullpath = self.searchFilesOnLine(self.path, self.startDate,
1134 self.endDate, self.expLabel, self.ext, self.walk,
1174 self.endDate, self.expLabel, self.ext, self.walk,
1135 self.filefmt, self.folderfmt)
1175 self.filefmt, self.folderfmt)
1136
1176
1137 try:
1177 try:
1138 fullpath = next(fullpath)
1178 fullpath = next(fullpath)
1139 except:
1179 except:
1140 fullpath = None
1180 fullpath = None
1141
1181
1142 if fullpath:
1182 if fullpath:
1143 break
1183 break
1144
1184
1145 log.warning(
1185 log.warning(
1146 'Waiting {} sec for a valid file in {}: try {} ...'.format(
1186 'Waiting {} sec for a valid file in {}: try {} ...'.format(
1147 self.delay, self.path, nTries + 1),
1187 self.delay, self.path, nTries + 1),
1148 self.name)
1188 self.name)
1149 time.sleep(self.delay)
1189 time.sleep(self.delay)
1150
1190
1151 if not(fullpath):
1191 if not(fullpath):
1152 raise schainpy.admin.SchainError(
1192 raise schainpy.admin.SchainError(
1153 'There isn\'t any valid file in {}'.format(self.path))
1193 'There isn\'t any valid file in {}'.format(self.path))
1154
1194
1155 pathname, filename = os.path.split(fullpath)
1195 pathname, filename = os.path.split(fullpath)
1156 self.year = int(filename[1:5])
1196 self.year = int(filename[1:5])
1157 self.doy = int(filename[5:8])
1197 self.doy = int(filename[5:8])
1158 self.set = int(filename[8:11]) - 1
1198 self.set = int(filename[8:11]) - 1
1159 else:
1199 else:
1160 log.log("Searching files in {}".format(self.path), self.name)
1200 log.log("Searching files in {}".format(self.path), self.name)
1161 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
1201 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
1162 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
1202 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
1163
1203
1164 self.setNextFile()
1204 self.setNextFile()
1165
1205
1166 return
1206 return
1167
1207
1168 def getBasicHeader(self):
1208 def getBasicHeader(self):
1169
1209
1170 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
1210 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
1171 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1211 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1172
1212
1173 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1213 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1174
1214
1175 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1215 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1176
1216
1177 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1217 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1178
1218
1179 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1219 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1180
1220
1181 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1221 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1182
1222
1183 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1223 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1184
1224
1185 def getFirstHeader(self):
1225 def getFirstHeader(self):
1186
1226
1187 raise NotImplementedError
1227 raise NotImplementedError
1188
1228
1189 def getData(self):
1229 def getData(self):
1190
1230
1191 raise NotImplementedError
1231 raise NotImplementedError
1192
1232
1193 def hasNotDataInBuffer(self):
1233 def hasNotDataInBuffer(self):
1194
1234
1195 raise NotImplementedError
1235 raise NotImplementedError
1196
1236
1197 def readBlock(self):
1237 def readBlock(self):
1198
1238
1199 raise NotImplementedError
1239 raise NotImplementedError
1200
1240
1201 def isEndProcess(self):
1241 def isEndProcess(self):
1202
1242
1203 return self.flagNoMoreFiles
1243 return self.flagNoMoreFiles
1204
1244
1205 def printReadBlocks(self):
1245 def printReadBlocks(self):
1206
1246
1207 print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks)
1247 print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks)
1208
1248
1209 def printTotalBlocks(self):
1249 def printTotalBlocks(self):
1210
1250
1211 print("[Reading] Number of read blocks %04d" % self.nTotalBlocks)
1251 print("[Reading] Number of read blocks %04d" % self.nTotalBlocks)
1212
1252
1213 def run(self, **kwargs):
1253 def run(self, **kwargs):
1214 """
1254 """
1215
1255
1216 Arguments:
1256 Arguments:
1217 path :
1257 path :
1218 startDate :
1258 startDate :
1219 endDate :
1259 endDate :
1220 startTime :
1260 startTime :
1221 endTime :
1261 endTime :
1222 set :
1262 set :
1223 expLabel :
1263 expLabel :
1224 ext :
1264 ext :
1225 online :
1265 online :
1226 delay :
1266 delay :
1227 walk :
1267 walk :
1228 getblock :
1268 getblock :
1229 nTxs :
1269 nTxs :
1230 realtime :
1270 realtime :
1231 blocksize :
1271 blocksize :
1232 blocktime :
1272 blocktime :
1233 skip :
1273 skip :
1234 cursor :
1274 cursor :
1235 warnings :
1275 warnings :
1236 server :
1276 server :
1237 verbose :
1277 verbose :
1238 format :
1278 format :
1239 oneDDict :
1279 oneDDict :
1240 twoDDict :
1280 twoDDict :
1241 independentParam :
1281 independentParam :
1242 """
1282 """
1243
1283
1244 if not(self.isConfig):
1284 if not(self.isConfig):
1245 self.setup(**kwargs)
1285 self.setup(**kwargs)
1246 self.isConfig = True
1286 self.isConfig = True
1247 if self.server is None:
1287 if self.server is None:
1248 self.getData()
1288 self.getData()
1249 else:
1289 else:
1250 self.getFromServer()
1290 self.getFromServer()
1251
1291
1252
1292
1253 class JRODataWriter(Reader):
1293 class JRODataWriter(Reader):
1254
1294
1255 """
1295 """
1256 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1296 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1257 de los datos siempre se realiza por bloques.
1297 de los datos siempre se realiza por bloques.
1258 """
1298 """
1259
1299
1260 setFile = None
1300 setFile = None
1261 profilesPerBlock = None
1301 profilesPerBlock = None
1262 blocksPerFile = None
1302 blocksPerFile = None
1263 nWriteBlocks = 0
1303 nWriteBlocks = 0
1264 fileDate = None
1304 fileDate = None
1265
1305
1266 def __init__(self, dataOut=None):
1306 def __init__(self, dataOut=None):
1267 raise NotImplementedError
1307 raise NotImplementedError
1268
1308
1269 def hasAllDataInBuffer(self):
1309 def hasAllDataInBuffer(self):
1270 raise NotImplementedError
1310 raise NotImplementedError
1271
1311
1272 def setBlockDimension(self):
1312 def setBlockDimension(self):
1273 raise NotImplementedError
1313 raise NotImplementedError
1274
1314
1275 def writeBlock(self):
1315 def writeBlock(self):
1276 raise NotImplementedError
1316 raise NotImplementedError
1277
1317
1278 def putData(self):
1318 def putData(self):
1279 raise NotImplementedError
1319 raise NotImplementedError
1280
1320
1281 def getDtypeWidth(self):
1321 def getDtypeWidth(self):
1282
1322
1283 dtype_index = get_dtype_index(self.dtype)
1323 dtype_index = get_dtype_index(self.dtype)
1284 dtype_width = get_dtype_width(dtype_index)
1324 dtype_width = get_dtype_width(dtype_index)
1285
1325
1286 return dtype_width
1326 return dtype_width
1287
1327
1288 def getProcessFlags(self):
1328 def getProcessFlags(self):
1289
1329
1290 processFlags = 0
1330 processFlags = 0
1291
1331
1292 dtype_index = get_dtype_index(self.dtype)
1332 dtype_index = get_dtype_index(self.dtype)
1293 procflag_dtype = get_procflag_dtype(dtype_index)
1333 procflag_dtype = get_procflag_dtype(dtype_index)
1294
1334
1295 processFlags += procflag_dtype
1335 processFlags += procflag_dtype
1296
1336
1297 if self.dataOut.flagDecodeData:
1337 if self.dataOut.flagDecodeData:
1298 processFlags += PROCFLAG.DECODE_DATA
1338 processFlags += PROCFLAG.DECODE_DATA
1299
1339
1300 if self.dataOut.flagDeflipData:
1340 if self.dataOut.flagDeflipData:
1301 processFlags += PROCFLAG.DEFLIP_DATA
1341 processFlags += PROCFLAG.DEFLIP_DATA
1302
1342
1303 if self.dataOut.code is not None:
1343 if self.dataOut.code is not None:
1304 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1344 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1305
1345
1306 if self.dataOut.nCohInt > 1:
1346 if self.dataOut.nCohInt > 1:
1307 processFlags += PROCFLAG.COHERENT_INTEGRATION
1347 processFlags += PROCFLAG.COHERENT_INTEGRATION
1308
1348
1309 if self.dataOut.type == "Spectra":
1349 if self.dataOut.type == "Spectra":
1310 if self.dataOut.nIncohInt > 1:
1350 if self.dataOut.nIncohInt > 1:
1311 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1351 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1312
1352
1313 if self.dataOut.data_dc is not None:
1353 if self.dataOut.data_dc is not None:
1314 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1354 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1315
1355
1316 if self.dataOut.flagShiftFFT:
1356 if self.dataOut.flagShiftFFT:
1317 processFlags += PROCFLAG.SHIFT_FFT_DATA
1357 processFlags += PROCFLAG.SHIFT_FFT_DATA
1318
1358
1319 return processFlags
1359 return processFlags
1320
1360
1321 def setBasicHeader(self):
1361 def setBasicHeader(self):
1322
1362
1323 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1363 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1324 self.basicHeaderObj.version = self.versionFile
1364 self.basicHeaderObj.version = self.versionFile
1325 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1365 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1326 utc = numpy.floor(self.dataOut.utctime)
1366 utc = numpy.floor(self.dataOut.utctime)
1327 milisecond = (self.dataOut.utctime - utc) * 1000.0
1367 milisecond = (self.dataOut.utctime - utc) * 1000.0
1328 self.basicHeaderObj.utc = utc
1368 self.basicHeaderObj.utc = utc
1329 self.basicHeaderObj.miliSecond = milisecond
1369 self.basicHeaderObj.miliSecond = milisecond
1330 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1370 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1331 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1371 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1332 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1372 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1333
1373
1334 def setFirstHeader(self):
1374 def setFirstHeader(self):
1335 """
1375 """
1336 Obtiene una copia del First Header
1376 Obtiene una copia del First Header
1337
1377
1338 Affected:
1378 Affected:
1339
1379
1340 self.basicHeaderObj
1380 self.basicHeaderObj
1341 self.systemHeaderObj
1381 self.systemHeaderObj
1342 self.radarControllerHeaderObj
1382 self.radarControllerHeaderObj
1343 self.processingHeaderObj self.
1383 self.processingHeaderObj self.
1344
1384
1345 Return:
1385 Return:
1346 None
1386 None
1347 """
1387 """
1348
1388
1349 raise NotImplementedError
1389 raise NotImplementedError
1350
1390
1351 def __writeFirstHeader(self):
1391 def __writeFirstHeader(self):
1352 """
1392 """
1353 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1393 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1354
1394
1355 Affected:
1395 Affected:
1356 __dataType
1396 __dataType
1357
1397
1358 Return:
1398 Return:
1359 None
1399 None
1360 """
1400 """
1361
1401
1362 # CALCULAR PARAMETROS
1402 # CALCULAR PARAMETROS
1363
1403
1364 sizeLongHeader = self.systemHeaderObj.size + \
1404 sizeLongHeader = self.systemHeaderObj.size + \
1365 self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1405 self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1366 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1406 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1367
1407
1368 self.basicHeaderObj.write(self.fp)
1408 self.basicHeaderObj.write(self.fp)
1369 self.systemHeaderObj.write(self.fp)
1409 self.systemHeaderObj.write(self.fp)
1370 self.radarControllerHeaderObj.write(self.fp)
1410 self.radarControllerHeaderObj.write(self.fp)
1371 self.processingHeaderObj.write(self.fp)
1411 self.processingHeaderObj.write(self.fp)
1372
1412
1373 def __setNewBlock(self):
1413 def __setNewBlock(self):
1374 """
1414 """
1375 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1415 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1376
1416
1377 Return:
1417 Return:
1378 0 : si no pudo escribir nada
1418 0 : si no pudo escribir nada
1379 1 : Si escribio el Basic el First Header
1419 1 : Si escribio el Basic el First Header
1380 """
1420 """
1381 if self.fp == None:
1421 if self.fp == None:
1382 self.setNextFile()
1422 self.setNextFile()
1383
1423
1384 if self.flagIsNewFile:
1424 if self.flagIsNewFile:
1385 return 1
1425 return 1
1386
1426
1387 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1427 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1388 self.basicHeaderObj.write(self.fp)
1428 self.basicHeaderObj.write(self.fp)
1389 return 1
1429 return 1
1390
1430
1391 if not(self.setNextFile()):
1431 if not(self.setNextFile()):
1392 return 0
1432 return 0
1393
1433
1394 return 1
1434 return 1
1395
1435
1396 def writeNextBlock(self):
1436 def writeNextBlock(self):
1397 """
1437 """
1398 Selecciona el bloque siguiente de datos y los escribe en un file
1438 Selecciona el bloque siguiente de datos y los escribe en un file
1399
1439
1400 Return:
1440 Return:
1401 0 : Si no hizo pudo escribir el bloque de datos
1441 0 : Si no hizo pudo escribir el bloque de datos
1402 1 : Si no pudo escribir el bloque de datos
1442 1 : Si no pudo escribir el bloque de datos
1403 """
1443 """
1404 if not(self.__setNewBlock()):
1444 if not(self.__setNewBlock()):
1405 return 0
1445 return 0
1406
1446
1407 self.writeBlock()
1447 self.writeBlock()
1408
1448
1409 print("[Writing] Block No. %d/%d" % (self.blockIndex,
1449 print("[Writing] Block No. %d/%d" % (self.blockIndex,
1410 self.processingHeaderObj.dataBlocksPerFile))
1450 self.processingHeaderObj.dataBlocksPerFile))
1411
1451
1412 return 1
1452 return 1
1413
1453
1414 def setNextFile(self):
1454 def setNextFile(self):
1415 """Determina el siguiente file que sera escrito
1455 """Determina el siguiente file que sera escrito
1416
1456
1417 Affected:
1457 Affected:
1418 self.filename
1458 self.filename
1419 self.subfolder
1459 self.subfolder
1420 self.fp
1460 self.fp
1421 self.setFile
1461 self.setFile
1422 self.flagIsNewFile
1462 self.flagIsNewFile
1423
1463
1424 Return:
1464 Return:
1425 0 : Si el archivo no puede ser escrito
1465 0 : Si el archivo no puede ser escrito
1426 1 : Si el archivo esta listo para ser escrito
1466 1 : Si el archivo esta listo para ser escrito
1427 """
1467 """
1428 ext = self.ext
1468 ext = self.ext
1429 path = self.path
1469 path = self.path
1430
1470
1431 if self.fp != None:
1471 if self.fp != None:
1432 self.fp.close()
1472 self.fp.close()
1433
1473
1434 if not os.path.exists(path):
1474 if not os.path.exists(path):
1435 os.mkdir(path)
1475 os.mkdir(path)
1436
1476
1437 timeTuple = time.localtime(self.dataOut.utctime)
1477 timeTuple = time.localtime(self.dataOut.utctime)
1438 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
1478 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
1439
1479
1440 fullpath = os.path.join(path, subfolder)
1480 fullpath = os.path.join(path, subfolder)
1441 setFile = self.setFile
1481 setFile = self.setFile
1442
1482
1443 if not(os.path.exists(fullpath)):
1483 if not(os.path.exists(fullpath)):
1444 os.mkdir(fullpath)
1484 os.mkdir(fullpath)
1445 setFile = -1 # inicializo mi contador de seteo
1485 setFile = -1 # inicializo mi contador de seteo
1446 else:
1486 else:
1447 filesList = os.listdir(fullpath)
1487 filesList = os.listdir(fullpath)
1448 if len(filesList) > 0:
1488 if len(filesList) > 0:
1449 filesList = sorted(filesList, key=str.lower)
1489 filesList = sorted(filesList, key=str.lower)
1450 filen = filesList[-1]
1490 filen = filesList[-1]
1451 # el filename debera tener el siguiente formato
1491 # el filename debera tener el siguiente formato
1452 # 0 1234 567 89A BCDE (hex)
1492 # 0 1234 567 89A BCDE (hex)
1453 # x YYYY DDD SSS .ext
1493 # x YYYY DDD SSS .ext
1454 if isNumber(filen[8:11]):
1494 if isNumber(filen[8:11]):
1455 # inicializo mi contador de seteo al seteo del ultimo file
1495 # inicializo mi contador de seteo al seteo del ultimo file
1456 setFile = int(filen[8:11])
1496 setFile = int(filen[8:11])
1457 else:
1497 else:
1458 setFile = -1
1498 setFile = -1
1459 else:
1499 else:
1460 setFile = -1 # inicializo mi contador de seteo
1500 setFile = -1 # inicializo mi contador de seteo
1461
1501
1462 setFile += 1
1502 setFile += 1
1463
1503
1464 # If this is a new day it resets some values
1504 # If this is a new day it resets some values
1465 if self.dataOut.datatime.date() > self.fileDate:
1505 if self.dataOut.datatime.date() > self.fileDate:
1466 setFile = 0
1506 setFile = 0
1467 self.nTotalBlocks = 0
1507 self.nTotalBlocks = 0
1468
1508
1469 filen = '{}{:04d}{:03d}{:03d}{}'.format(
1509 filen = '{}{:04d}{:03d}{:03d}{}'.format(
1470 self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext)
1510 self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext)
1471
1511
1472 filename = os.path.join(path, subfolder, filen)
1512 filename = os.path.join(path, subfolder, filen)
1473
1513
1474 fp = open(filename, 'wb')
1514 fp = open(filename, 'wb')
1475
1515
1476 self.blockIndex = 0
1516 self.blockIndex = 0
1477 self.filename = filename
1517 self.filename = filename
1478 self.subfolder = subfolder
1518 self.subfolder = subfolder
1479 self.fp = fp
1519 self.fp = fp
1480 self.setFile = setFile
1520 self.setFile = setFile
1481 self.flagIsNewFile = 1
1521 self.flagIsNewFile = 1
1482 self.fileDate = self.dataOut.datatime.date()
1522 self.fileDate = self.dataOut.datatime.date()
1483 self.setFirstHeader()
1523 self.setFirstHeader()
1484
1524
1485 print('[Writing] Opening file: %s' % self.filename)
1525 print('[Writing] Opening file: %s' % self.filename)
1486
1526
1487 self.__writeFirstHeader()
1527 self.__writeFirstHeader()
1488
1528
1489 return 1
1529 return 1
1490
1530
1491 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1531 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1492 """
1532 """
1493 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1533 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1494
1534
1495 Inputs:
1535 Inputs:
1496 path : directory where data will be saved
1536 path : directory where data will be saved
1497 profilesPerBlock : number of profiles per block
1537 profilesPerBlock : number of profiles per block
1498 set : initial file set
1538 set : initial file set
1499 datatype : An integer number that defines data type:
1539 datatype : An integer number that defines data type:
1500 0 : int8 (1 byte)
1540 0 : int8 (1 byte)
1501 1 : int16 (2 bytes)
1541 1 : int16 (2 bytes)
1502 2 : int32 (4 bytes)
1542 2 : int32 (4 bytes)
1503 3 : int64 (8 bytes)
1543 3 : int64 (8 bytes)
1504 4 : float32 (4 bytes)
1544 4 : float32 (4 bytes)
1505 5 : double64 (8 bytes)
1545 5 : double64 (8 bytes)
1506
1546
1507 Return:
1547 Return:
1508 0 : Si no realizo un buen seteo
1548 0 : Si no realizo un buen seteo
1509 1 : Si realizo un buen seteo
1549 1 : Si realizo un buen seteo
1510 """
1550 """
1511
1551
1512 if ext == None:
1552 if ext == None:
1513 ext = self.ext
1553 ext = self.ext
1514
1554
1515 self.ext = ext.lower()
1555 self.ext = ext.lower()
1516
1556
1517 self.path = path
1557 self.path = path
1518
1558
1519 if set is None:
1559 if set is None:
1520 self.setFile = -1
1560 self.setFile = -1
1521 else:
1561 else:
1522 self.setFile = set - 1
1562 self.setFile = set - 1
1523
1563
1524 self.blocksPerFile = blocksPerFile
1564 self.blocksPerFile = blocksPerFile
1525 self.profilesPerBlock = profilesPerBlock
1565 self.profilesPerBlock = profilesPerBlock
1526 self.dataOut = dataOut
1566 self.dataOut = dataOut
1527 self.fileDate = self.dataOut.datatime.date()
1567 self.fileDate = self.dataOut.datatime.date()
1528 self.dtype = self.dataOut.dtype
1568 self.dtype = self.dataOut.dtype
1529
1569
1530 if datatype is not None:
1570 if datatype is not None:
1531 self.dtype = get_numpy_dtype(datatype)
1571 self.dtype = get_numpy_dtype(datatype)
1532
1572
1533 if not(self.setNextFile()):
1573 if not(self.setNextFile()):
1534 print("[Writing] There isn't a next file")
1574 print("[Writing] There isn't a next file")
1535 return 0
1575 return 0
1536
1576
1537 self.setBlockDimension()
1577 self.setBlockDimension()
1538
1578
1539 return 1
1579 return 1
1540
1580
1541 def run(self, dataOut, path, blocksPerFile=100, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1581 def run(self, dataOut, path, blocksPerFile=100, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1542
1582
1543 if not(self.isConfig):
1583 if not(self.isConfig):
1544
1584
1545 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock,
1585 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock,
1546 set=set, ext=ext, datatype=datatype, **kwargs)
1586 set=set, ext=ext, datatype=datatype, **kwargs)
1547 self.isConfig = True
1587 self.isConfig = True
1548
1588
1549 self.dataOut = dataOut
1589 self.dataOut = dataOut
1550 self.putData()
1590 self.putData()
1551 return self.dataOut
1591 return self.dataOut
1552
1592
1553 @MPDecorator
1593 @MPDecorator
1554 class printInfo(Operation):
1594 class printInfo(Operation):
1555
1595
1556 def __init__(self):
1596 def __init__(self):
1557
1597
1558 Operation.__init__(self)
1598 Operation.__init__(self)
1559 self.__printInfo = True
1599 self.__printInfo = True
1560
1600
1561 def run(self, dataOut, headers = ['systemHeaderObj', 'radarControllerHeaderObj', 'processingHeaderObj']):
1601 def run(self, dataOut, headers = ['systemHeaderObj', 'radarControllerHeaderObj', 'processingHeaderObj']):
1562 if self.__printInfo == False:
1602 if self.__printInfo == False:
1563 return
1603 return
1564
1604
1565 for header in headers:
1605 for header in headers:
1566 if hasattr(dataOut, header):
1606 if hasattr(dataOut, header):
1567 obj = getattr(dataOut, header)
1607 obj = getattr(dataOut, header)
1568 if hasattr(obj, 'printInfo'):
1608 if hasattr(obj, 'printInfo'):
1569 obj.printInfo()
1609 obj.printInfo()
1570 else:
1610 else:
1571 print(obj)
1611 print(obj)
1572 else:
1612 else:
1573 log.warning('Header {} Not found in object'.format(header))
1613 log.warning('Header {} Not found in object'.format(header))
1574
1614
1575 self.__printInfo = False
1615 self.__printInfo = False
@@ -1,595 +1,629
1 '''
1 '''
2 Created on Aug 1, 2017
2 Created on Aug 1, 2017
3
3
4 @author: Juan C. Espinoza
4 @author: Juan C. Espinoza
5 '''
5 '''
6
6
7 import os
7 import os
8 import sys
8 import sys
9 import time
9 import time
10 import json
10 import json
11 import glob
11 import glob
12 import datetime
12 import datetime
13
13
14 import numpy
14 import numpy
15 import h5py
15 import h5py
16
16
17 import schainpy.admin
17 import schainpy.admin
18 from schainpy.model.io.jroIO_base import LOCALTIME, Reader
18 from schainpy.model.io.jroIO_base import LOCALTIME, Reader
19 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
19 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
20 from schainpy.model.data.jrodata import Parameters
20 from schainpy.model.data.jrodata import Parameters
21 from schainpy.utils import log
21 from schainpy.utils import log
22
22
23 try:
23 try:
24 import madrigal.cedar
24 import madrigal.cedar
25 except:
25 except:
26 pass
26 pass
27
27
28 try:
28 try:
29 basestring
29 basestring
30 except:
30 except:
31 basestring = str
31 basestring = str
32
32
33 DEF_CATALOG = {
33 DEF_CATALOG = {
34 'principleInvestigator': 'Marco Milla',
34 'principleInvestigator': 'Marco Milla',
35 'expPurpose': '',
35 'expPurpose': '',
36 'cycleTime': '',
36 'cycleTime': '',
37 'correlativeExp': '',
37 'correlativeExp': '',
38 'sciRemarks': '',
38 'sciRemarks': '',
39 'instRemarks': ''
39 'instRemarks': ''
40 }
40 }
41
41
42 DEF_HEADER = {
42 DEF_HEADER = {
43 'kindatDesc': '',
43 'kindatDesc': '',
44 'analyst': 'Jicamarca User',
44 'analyst': 'Jicamarca User',
45 'comments': '',
45 'comments': '',
46 'history': ''
46 'history': ''
47 }
47 }
48
48
49 MNEMONICS = {
49 MNEMONICS = {
50 10: 'jro',
50 10: 'jro',
51 11: 'jbr',
51 11: 'jbr',
52 840: 'jul',
52 840: 'jul',
53 13: 'jas',
53 13: 'jas',
54 1000: 'pbr',
54 1000: 'pbr',
55 1001: 'hbr',
55 1001: 'hbr',
56 1002: 'obr',
56 1002: 'obr',
57 400: 'clr'
57 400: 'clr'
58
58
59 }
59 }
60
60
61 UT1970 = datetime.datetime(1970, 1, 1) - datetime.timedelta(seconds=time.timezone)
61 UT1970 = datetime.datetime(1970, 1, 1) - datetime.timedelta(seconds=time.timezone)
62
62
63 def load_json(obj):
63 def load_json(obj):
64 '''
64 '''
65 Parse json as string instead of unicode
65 Parse json as string instead of unicode
66 '''
66 '''
67
67
68 if isinstance(obj, str):
68 if isinstance(obj, str):
69 iterable = json.loads(obj)
69 iterable = json.loads(obj)
70 else:
70 else:
71 iterable = obj
71 iterable = obj
72
72
73 if isinstance(iterable, dict):
73 if isinstance(iterable, dict):
74 return {str(k): load_json(v) if isinstance(v, dict) else str(v) if isinstance(v, basestring) else v
74 return {str(k): load_json(v) if isinstance(v, dict) else str(v) if isinstance(v, basestring) else v
75 for k, v in list(iterable.items())}
75 for k, v in list(iterable.items())}
76 elif isinstance(iterable, (list, tuple)):
76 elif isinstance(iterable, (list, tuple)):
77 return [str(v) if isinstance(v, basestring) else v for v in iterable]
77 return [str(v) if isinstance(v, basestring) else v for v in iterable]
78
78
79 return iterable
79 return iterable
80
80
81
81
82 class MADReader(Reader, ProcessingUnit):
82 class MADReader(Reader, ProcessingUnit):
83
83
84 def __init__(self):
84 def __init__(self):
85
85
86 ProcessingUnit.__init__(self)
86 ProcessingUnit.__init__(self)
87
87
88 self.dataOut = Parameters()
88 self.dataOut = Parameters()
89 self.counter_records = 0
89 self.counter_records = 0
90 self.nrecords = None
90 self.nrecords = None
91 self.flagNoMoreFiles = 0
91 self.flagNoMoreFiles = 0
92 self.filename = None
92 self.filename = None
93 self.intervals = set()
93 self.intervals = set()
94 self.datatime = datetime.datetime(1900,1,1)
94 self.datatime = datetime.datetime(1900,1,1)
95 self.format = None
95 self.format = None
96 self.filefmt = "***%Y%m%d*******"
96 self.filefmt = "***%Y%m%d*******"
97
97
98 def setup(self, **kwargs):
98 def setup(self, **kwargs):
99
99
100 self.set_kwargs(**kwargs)
100 self.set_kwargs(**kwargs)
101 self.oneDDict = load_json(self.oneDDict)
101 self.oneDDict = load_json(self.oneDDict)
102 self.twoDDict = load_json(self.twoDDict)
102 self.twoDDict = load_json(self.twoDDict)
103 self.ind2DList = load_json(self.ind2DList)
103 self.ind2DList = load_json(self.ind2DList)
104 self.independentParam = self.ind2DList[0]
104 self.independentParam = self.ind2DList[0]
105
105
106 if self.path is None:
106 if self.path is None:
107 raise ValueError('The path is not valid')
107 raise ValueError('The path is not valid')
108
108
109 self.open_file = open
109 self.open_file = open
110 self.open_mode = 'rb'
110 self.open_mode = 'rb'
111
111
112 if self.format is None:
112 if self.format is None:
113 raise ValueError('The format is not valid choose simple or hdf5')
113 raise ValueError('The format is not valid choose simple or hdf5')
114 elif self.format.lower() in ('simple', 'txt'):
114 elif self.format.lower() in ('simple', 'txt'):
115 self.ext = '.txt'
115 self.ext = '.txt'
116 elif self.format.lower() in ('cedar',):
116 elif self.format.lower() in ('cedar',):
117 self.ext = '.001'
117 self.ext = '.001'
118 else:
118 else:
119 self.ext = '.hdf5'
119 self.ext = '.hdf5'
120 self.open_file = h5py.File
120 self.open_file = h5py.File
121 self.open_mode = 'r'
121 self.open_mode = 'r'
122
122
123 if self.online:
123 if self.online:
124 log.log("Searching files in online mode...", self.name)
124 log.log("Searching files in online mode...", self.name)
125
125
126 for nTries in range(self.nTries):
126 for nTries in range(self.nTries):
127 fullpath = self.searchFilesOnLine(self.path, self.startDate,
127 fullpath = self.searchFilesOnLine(self.path, self.startDate,
128 self.endDate, self.expLabel, self.ext, self.walk,
128 self.endDate, self.expLabel, self.ext, self.walk,
129 self.filefmt, self.folderfmt)
129 self.filefmt, self.folderfmt)
130
130
131 try:
131 try:
132 fullpath = next(fullpath)
132 fullpath = next(fullpath)
133 except:
133 except:
134 fullpath = None
134 fullpath = None
135
135
136 if fullpath:
136 if fullpath:
137 break
137 break
138
138
139 log.warning(
139 log.warning(
140 'Waiting {} sec for a valid file in {}: try {} ...'.format(
140 'Waiting {} sec for a valid file in {}: try {} ...'.format(
141 self.delay, self.path, nTries + 1),
141 self.delay, self.path, nTries + 1),
142 self.name)
142 self.name)
143 time.sleep(self.delay)
143 time.sleep(self.delay)
144
144
145 if not(fullpath):
145 if not(fullpath):
146 raise schainpy.admin.SchainError(
146 raise schainpy.admin.SchainError(
147 'There isn\'t any valid file in {}'.format(self.path))
147 'There isn\'t any valid file in {}'.format(self.path))
148
148
149 else:
149 else:
150 log.log("Searching files in {}".format(self.path), self.name)
150 log.log("Searching files in {}".format(self.path), self.name)
151 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
151 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
152 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
152 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
153
153
154 self.setNextFile()
154 self.setNextFile()
155
155
156 def readFirstHeader(self):
156 def readFirstHeader(self):
157 '''Read header and data'''
157 '''Read header and data'''
158
158
159 self.parseHeader()
159 self.parseHeader()
160 self.parseData()
160 self.parseData()
161 self.blockIndex = 0
161 self.blockIndex = 0
162
162
163 return
163 return
164
164
165 def parseHeader(self):
165 def parseHeader(self):
166 '''
166 '''
167 '''
167 '''
168
168
169 self.output = {}
169 self.output = {}
170 self.version = '2'
170 self.version = '2'
171 s_parameters = None
171 s_parameters = None
172 if self.ext == '.txt':
172 if self.ext == '.txt':
173 self.parameters = [s.strip().lower() for s in self.fp.readline().decode().strip().split(' ') if s]
173 self.parameters = [s.strip().lower() for s in self.fp.readline().decode().strip().split(' ') if s]
174 elif self.ext == '.hdf5':
174 elif self.ext == '.hdf5':
175 self.metadata = self.fp['Metadata']
175 self.metadata = self.fp['Metadata']
176 if '_record_layout' in self.metadata:
176 if '_record_layout' in self.metadata:
177 s_parameters = [s[0].lower().decode() for s in self.metadata['Independent Spatial Parameters']]
177 s_parameters = [s[0].lower().decode() for s in self.metadata['Independent Spatial Parameters']]
178 self.version = '3'
178 self.version = '3'
179 self.parameters = [s[0].lower().decode() for s in self.metadata['Data Parameters']]
179 self.parameters = [s[0].lower().decode() for s in self.metadata['Data Parameters']]
180
180
181 log.success('Parameters found: {}'.format(self.parameters),
181 log.success('Parameters found: {}'.format(self.parameters),
182 'MADReader')
182 'MADReader')
183 if s_parameters:
183 if s_parameters:
184 log.success('Spatial parameters found: {}'.format(s_parameters),
184 log.success('Spatial parameters found: {}'.format(s_parameters),
185 'MADReader')
185 'MADReader')
186
186
187 for param in list(self.oneDDict.keys()):
187 for param in list(self.oneDDict.keys()):
188 if param.lower() not in self.parameters:
188 if param.lower() not in self.parameters:
189 log.warning(
189 log.warning(
190 'Parameter {} not found will be ignored'.format(
190 'Parameter {} not found will be ignored'.format(
191 param),
191 param),
192 'MADReader')
192 'MADReader')
193 self.oneDDict.pop(param, None)
193 self.oneDDict.pop(param, None)
194
194
195 for param, value in list(self.twoDDict.items()):
195 for param, value in list(self.twoDDict.items()):
196 if param.lower() not in self.parameters:
196 if param.lower() not in self.parameters:
197 log.warning(
197 log.warning(
198 'Parameter {} not found, it will be ignored'.format(
198 'Parameter {} not found, it will be ignored'.format(
199 param),
199 param),
200 'MADReader')
200 'MADReader')
201 self.twoDDict.pop(param, None)
201 self.twoDDict.pop(param, None)
202 continue
202 continue
203 if isinstance(value, list):
203 if isinstance(value, list):
204 if value[0] not in self.output:
204 if value[0] not in self.output:
205 self.output[value[0]] = []
205 self.output[value[0]] = []
206 self.output[value[0]].append([])
206 self.output[value[0]].append([])
207
207
208 def parseData(self):
208 def parseData(self):
209 '''
209 '''
210 '''
210 '''
211
211
212 if self.ext == '.txt':
212 if self.ext == '.txt':
213 self.data = numpy.genfromtxt(self.fp, missing_values=('missing'))
213 self.data = numpy.genfromtxt(self.fp, missing_values=('missing'))
214 self.nrecords = self.data.shape[0]
214 self.nrecords = self.data.shape[0]
215 self.ranges = numpy.unique(self.data[:,self.parameters.index(self.independentParam.lower())])
215 self.ranges = numpy.unique(self.data[:,self.parameters.index(self.independentParam.lower())])
216 self.counter_records = 0
216 self.counter_records = 0
217 elif self.ext == '.hdf5':
217 elif self.ext == '.hdf5':
218 self.data = self.fp['Data']
218 self.data = self.fp['Data']
219 self.ranges = numpy.unique(self.data['Table Layout'][self.independentParam.lower()])
219 self.ranges = numpy.unique(self.data['Table Layout'][self.independentParam.lower()])
220 self.times = numpy.unique(self.data['Table Layout']['ut1_unix'])
220 self.times = numpy.unique(self.data['Table Layout']['ut1_unix'])
221 self.counter_records = int(self.data['Table Layout']['recno'][0])
221 self.counter_records = int(self.data['Table Layout']['recno'][0])
222 self.nrecords = int(self.data['Table Layout']['recno'][-1])
222 self.nrecords = int(self.data['Table Layout']['recno'][-1])
223
223
224 def readNextBlock(self):
224 def readNextBlock(self):
225
225
226 while True:
226 while True:
227 self.flagDiscontinuousBlock = 0
227 self.flagDiscontinuousBlock = 0
228 if self.counter_records == self.nrecords:
228 if self.counter_records == self.nrecords:
229 self.setNextFile()
229 self.setNextFile()
230
230
231 self.readBlock()
231 self.readBlock()
232
232
233 if (self.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or \
233 if (self.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or \
234 (self.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
234 (self.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
235 log.warning(
235 log.warning(
236 'Reading Record No. {}/{} -> {} [Skipping]'.format(
236 'Reading Record No. {}/{} -> {} [Skipping]'.format(
237 self.counter_records,
237 self.counter_records,
238 self.nrecords,
238 self.nrecords,
239 self.datatime.ctime()),
239 self.datatime.ctime()),
240 'MADReader')
240 'MADReader')
241 continue
241 continue
242 break
242 break
243
243
244 log.log(
244 log.log(
245 'Reading Record No. {}/{} -> {}'.format(
245 'Reading Record No. {}/{} -> {}'.format(
246 self.counter_records,
246 self.counter_records,
247 self.nrecords,
247 self.nrecords,
248 self.datatime.ctime()),
248 self.datatime.ctime()),
249 'MADReader')
249 'MADReader')
250
250
251 return 1
251 return 1
252
252
253 def readBlock(self):
253 def readBlock(self):
254 '''
254 '''
255 '''
255 '''
256 dum = []
256 dum = []
257 if self.ext == '.txt':
257 if self.ext == '.txt':
258 dt = self.data[self.counter_records][:6].astype(int)
258 dt = self.data[self.counter_records][:6].astype(int)
259 if datetime.datetime(dt[0], dt[1], dt[2], dt[3], dt[4], dt[5]).date() > self.datatime.date():
259 if datetime.datetime(dt[0], dt[1], dt[2], dt[3], dt[4], dt[5]).date() > self.datatime.date():
260 self.flagDiscontinuousBlock = 1
260 self.flagDiscontinuousBlock = 1
261 self.datatime = datetime.datetime(dt[0], dt[1], dt[2], dt[3], dt[4], dt[5])
261 self.datatime = datetime.datetime(dt[0], dt[1], dt[2], dt[3], dt[4], dt[5])
262 while True:
262 while True:
263 dt = self.data[self.counter_records][:6].astype(int)
263 dt = self.data[self.counter_records][:6].astype(int)
264 datatime = datetime.datetime(dt[0], dt[1], dt[2], dt[3], dt[4], dt[5])
264 datatime = datetime.datetime(dt[0], dt[1], dt[2], dt[3], dt[4], dt[5])
265 if datatime == self.datatime:
265 if datatime == self.datatime:
266 dum.append(self.data[self.counter_records])
266 dum.append(self.data[self.counter_records])
267 self.counter_records += 1
267 self.counter_records += 1
268 if self.counter_records == self.nrecords:
268 if self.counter_records == self.nrecords:
269 break
269 break
270 continue
270 continue
271 self.intervals.add((datatime-self.datatime).seconds)
271 self.intervals.add((datatime-self.datatime).seconds)
272 break
272 break
273 elif self.ext == '.hdf5':
273 elif self.ext == '.hdf5':
274 datatime = datetime.datetime.utcfromtimestamp(
274 datatime = datetime.datetime.utcfromtimestamp(
275 self.times[self.counter_records])
275 self.times[self.counter_records])
276 dum = self.data['Table Layout'][self.data['Table Layout']['recno']==self.counter_records]
276 dum = self.data['Table Layout'][self.data['Table Layout']['recno']==self.counter_records]
277 self.intervals.add((datatime-self.datatime).seconds)
277 self.intervals.add((datatime-self.datatime).seconds)
278 if datatime.date()>self.datatime.date():
278 if datatime.date()>self.datatime.date():
279 self.flagDiscontinuousBlock = 1
279 self.flagDiscontinuousBlock = 1
280 self.datatime = datatime
280 self.datatime = datatime
281 self.counter_records += 1
281 self.counter_records += 1
282
282
283 self.buffer = numpy.array(dum)
283 self.buffer = numpy.array(dum)
284 return
284 return
285
285
286 def set_output(self):
286 def set_output(self):
287 '''
287 '''
288 Storing data from buffer to dataOut object
288 Storing data from buffer to dataOut object
289 '''
289 '''
290
290
291 parameters = [None for __ in self.parameters]
291 parameters = [None for __ in self.parameters]
292
292
293 for param, attr in list(self.oneDDict.items()):
293 for param, attr in list(self.oneDDict.items()):
294 x = self.parameters.index(param.lower())
294 x = self.parameters.index(param.lower())
295 setattr(self.dataOut, attr, self.buffer[0][x])
295 setattr(self.dataOut, attr, self.buffer[0][x])
296
296
297 for param, value in list(self.twoDDict.items()):
297 for param, value in list(self.twoDDict.items()):
298 dummy = numpy.zeros(self.ranges.shape) + numpy.nan
298 dummy = numpy.zeros(self.ranges.shape) + numpy.nan
299 if self.ext == '.txt':
299 if self.ext == '.txt':
300 x = self.parameters.index(param.lower())
300 x = self.parameters.index(param.lower())
301 y = self.parameters.index(self.independentParam.lower())
301 y = self.parameters.index(self.independentParam.lower())
302 ranges = self.buffer[:,y]
302 ranges = self.buffer[:,y]
303 #if self.ranges.size == ranges.size:
303 #if self.ranges.size == ranges.size:
304 # continue
304 # continue
305 index = numpy.where(numpy.in1d(self.ranges, ranges))[0]
305 index = numpy.where(numpy.in1d(self.ranges, ranges))[0]
306 dummy[index] = self.buffer[:,x]
306 dummy[index] = self.buffer[:,x]
307 else:
307 else:
308 ranges = self.buffer[self.independentParam.lower()]
308 ranges = self.buffer[self.independentParam.lower()]
309 index = numpy.where(numpy.in1d(self.ranges, ranges))[0]
309 index = numpy.where(numpy.in1d(self.ranges, ranges))[0]
310 dummy[index] = self.buffer[param.lower()]
310 dummy[index] = self.buffer[param.lower()]
311
311
312 if isinstance(value, str):
312 if isinstance(value, str):
313 if value not in self.independentParam:
313 if value not in self.independentParam:
314 setattr(self.dataOut, value, dummy.reshape(1,-1))
314 setattr(self.dataOut, value, dummy.reshape(1,-1))
315 elif isinstance(value, list):
315 elif isinstance(value, list):
316 self.output[value[0]][value[1]] = dummy
316 self.output[value[0]][value[1]] = dummy
317 parameters[value[1]] = param
317 parameters[value[1]] = param
318 for key, value in list(self.output.items()):
318 for key, value in list(self.output.items()):
319 setattr(self.dataOut, key, numpy.array(value))
319 setattr(self.dataOut, key, numpy.array(value))
320
320
321 self.dataOut.parameters = [s for s in parameters if s]
321 self.dataOut.parameters = [s for s in parameters if s]
322 self.dataOut.heightList = self.ranges
322 self.dataOut.heightList = self.ranges
323 self.dataOut.utctime = (self.datatime - datetime.datetime(1970, 1, 1)).total_seconds()
323 self.dataOut.utctime = (self.datatime - datetime.datetime(1970, 1, 1)).total_seconds()
324 self.dataOut.utctimeInit = self.dataOut.utctime
324 self.dataOut.utctimeInit = self.dataOut.utctime
325 self.dataOut.paramInterval = min(self.intervals)
325 self.dataOut.paramInterval = min(self.intervals)
326 self.dataOut.useLocalTime = False
326 self.dataOut.useLocalTime = False
327 self.dataOut.flagNoData = False
327 self.dataOut.flagNoData = False
328 self.dataOut.nrecords = self.nrecords
328 self.dataOut.nrecords = self.nrecords
329 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
329 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
330
330
331 def getData(self):
331 def getData(self):
332 '''
332 '''
333 Storing data from databuffer to dataOut object
333 Storing data from databuffer to dataOut object
334 '''
334 '''
335
335
336 if not self.readNextBlock():
336 if not self.readNextBlock():
337 self.dataOut.flagNoData = True
337 self.dataOut.flagNoData = True
338 return 0
338 return 0
339
339
340 self.set_output()
340 self.set_output()
341
341
342 return 1
342 return 1
343
343
344 def run(self, **kwargs):
344 def run(self, **kwargs):
345
345
346 if not(self.isConfig):
346 if not(self.isConfig):
347 self.setup(**kwargs)
347 self.setup(**kwargs)
348 self.isConfig = True
348 self.isConfig = True
349
349
350 self.getData()
350 self.getData()
351
351
352 return
352 return
353
353
354 @MPDecorator
354 @MPDecorator
355 class MADWriter(Operation):
355 class MADWriter(Operation):
356 '''Writing module for Madrigal files
356 '''Writing module for Madrigal files
357
357
358 type: external
358 type: external
359
359
360 Inputs:
360 Inputs:
361 path path where files will be created
361 path path where files will be created
362 oneDDict json of one-dimensional parameters in record where keys
362 oneDDict json of one-dimensional parameters in record where keys
363 are Madrigal codes (integers or mnemonics) and values the corresponding
363 are Madrigal codes (integers or mnemonics) and values the corresponding
364 dataOut attribute e.g: {
364 dataOut attribute e.g: {
365 'gdlatr': 'lat',
365 'gdlatr': 'lat',
366 'gdlonr': 'lon',
366 'gdlonr': 'lon',
367 'gdlat2':'lat',
367 'gdlat2':'lat',
368 'glon2':'lon'}
368 'glon2':'lon'}
369 ind2DList list of independent spatial two-dimensional parameters e.g:
369 ind2DList list of independent spatial two-dimensional parameters e.g:
370 ['heigthList']
370 ['heigthList']
371 twoDDict json of two-dimensional parameters in record where keys
371 twoDDict json of two-dimensional parameters in record where keys
372 are Madrigal codes (integers or mnemonics) and values the corresponding
372 are Madrigal codes (integers or mnemonics) and values the corresponding
373 dataOut attribute if multidimensional array specify as tupple
373 dataOut attribute if multidimensional array specify as tupple
374 ('attr', pos) e.g: {
374 ('attr', pos) e.g: {
375 'gdalt': 'heightList',
375 'gdalt': 'heightList',
376 'vn1p2': ('data_output', 0),
376 'vn1p2': ('data_output', 0),
377 'vn2p2': ('data_output', 1),
377 'vn2p2': ('data_output', 1),
378 'vn3': ('data_output', 2),
378 'vn3': ('data_output', 2),
379 'snl': ('data_SNR', 'db')
379 'snl': ('data_SNR', 'db')
380 }
380 }
381 metadata json of madrigal metadata (kinst, kindat, catalog and header)
381 metadata json of madrigal metadata (kinst, kindat, catalog and header)
382 format hdf5, cedar
382 format hdf5, cedar
383 blocks number of blocks per file'''
383 blocks number of blocks per file'''
384
384
385 __attrs__ = ['path', 'oneDDict', 'ind2DList', 'twoDDict','metadata', 'format', 'blocks']
385 __attrs__ = ['path', 'oneDDict', 'ind2DList', 'twoDDict','metadata', 'format', 'blocks']
386 missing = -32767
386 missing = -32767
387
387
388 def __init__(self):
388 def __init__(self):
389
389
390 Operation.__init__(self)
390 Operation.__init__(self)
391 self.dataOut = Parameters()
391 self.dataOut = Parameters()
392 self.counter = 0
392 self.counter = 0
393 self.path = None
393 self.path = None
394 self.fp = None
394 self.fp = None
395
395
396 def run(self, dataOut, path, oneDDict, ind2DList='[]', twoDDict='{}',
396 def run(self, dataOut, path, oneDDict, ind2DList='[]', twoDDict='{}',
397 metadata='{}', format='cedar', **kwargs):
397 metadata='{}', format='cedar', **kwargs):
398
398
399
400 #if dataOut.AUX==1: #Modified
401
399 if not self.isConfig:
402 if not self.isConfig:
400 self.setup(path, oneDDict, ind2DList, twoDDict, metadata, format, **kwargs)
403 self.setup(path, oneDDict, ind2DList, twoDDict, metadata, format, **kwargs)
401 self.isConfig = True
404 self.isConfig = True
402
405
403 self.dataOut = dataOut
406 self.dataOut = dataOut
404 self.putData()
407 self.putData()
408
405 return 1
409 return 1
406
410
407 def setup(self, path, oneDDict, ind2DList, twoDDict, metadata, format, **kwargs):
411 def setup(self, path, oneDDict, ind2DList, twoDDict, metadata, format, **kwargs):
408 '''
412 '''
409 Configure Operation
413 Configure Operation
410 '''
414 '''
411
415
412 self.path = path
416 self.path = path
413 self.blocks = kwargs.get('blocks', None)
417 self.blocks = kwargs.get('blocks', None)
414 self.counter = 0
418 self.counter = 0
415 self.oneDDict = load_json(oneDDict)
419 self.oneDDict = load_json(oneDDict)
416 self.twoDDict = load_json(twoDDict)
420 self.twoDDict = load_json(twoDDict)
417 self.ind2DList = load_json(ind2DList)
421 self.ind2DList = load_json(ind2DList)
418 meta = load_json(metadata)
422 meta = load_json(metadata)
419 self.kinst = meta.get('kinst')
423 self.kinst = meta.get('kinst')
420 self.kindat = meta.get('kindat')
424 self.kindat = meta.get('kindat')
421 self.catalog = meta.get('catalog', DEF_CATALOG)
425 self.catalog = meta.get('catalog', DEF_CATALOG)
422 self.header = meta.get('header', DEF_HEADER)
426 self.header = meta.get('header', DEF_HEADER)
423 if format == 'cedar':
427 if format == 'cedar':
424 self.ext = '.dat'
428 self.ext = '.dat'
425 self.extra_args = {}
429 self.extra_args = {}
426 elif format == 'hdf5':
430 elif format == 'hdf5':
427 self.ext = '.hdf5'
431 self.ext = '.hdf5'
428 self.extra_args = {'ind2DList': self.ind2DList}
432 self.extra_args = {'ind2DList': self.ind2DList}
429
433
430 self.keys = [k.lower() for k in self.twoDDict]
434 self.keys = [k.lower() for k in self.twoDDict]
431 if 'range' in self.keys:
435 if 'range' in self.keys:
432 self.keys.remove('range')
436 self.keys.remove('range')
433 if 'gdalt' in self.keys:
437 if 'gdalt' in self.keys:
434 self.keys.remove('gdalt')
438 self.keys.remove('gdalt')
435
439
436 def setFile(self):
440 def setFile(self):
437 '''
441 '''
438 Create new cedar file object
442 Create new cedar file object
439 '''
443 '''
440
444
441 self.mnemonic = MNEMONICS[self.kinst] #TODO get mnemonic from madrigal
445 self.mnemonic = MNEMONICS[self.kinst] #TODO get mnemonic from madrigal
442 date = datetime.datetime.utcfromtimestamp(self.dataOut.utctime)
446 date = datetime.datetime.utcfromtimestamp(self.dataOut.utctime)
447 #if self.dataOut.input_dat_type:
448 #date=datetime.datetime.fromtimestamp(self.dataOut.TimeBlockSeconds_for_dp_power)
449 #print("date",date)
443
450
444 filename = '{}{}{}'.format(self.mnemonic,
451 filename = '{}{}{}'.format(self.mnemonic,
445 date.strftime('%Y%m%d_%H%M%S'),
452 date.strftime('%Y%m%d_%H%M%S'),
446 self.ext)
453 self.ext)
447
454
448 self.fullname = os.path.join(self.path, filename)
455 self.fullname = os.path.join(self.path, filename)
449
456
450 if os.path.isfile(self.fullname) :
457 if os.path.isfile(self.fullname) :
451 log.warning(
458 log.warning(
452 'Destination file {} already exists, previous file deleted.'.format(
459 'Destination file {} already exists, previous file deleted.'.format(
453 self.fullname),
460 self.fullname),
454 'MADWriter')
461 'MADWriter')
455 os.remove(self.fullname)
462 os.remove(self.fullname)
456
463
457 try:
464 try:
458 log.success(
465 log.success(
459 'Creating file: {}'.format(self.fullname),
466 'Creating file: {}'.format(self.fullname),
460 'MADWriter')
467 'MADWriter')
461 if not os.path.exists(self.path):
468 if not os.path.exists(self.path):
462 os.makedirs(self.path)
469 os.makedirs(self.path)
463 self.fp = madrigal.cedar.MadrigalCedarFile(self.fullname, True)
470 self.fp = madrigal.cedar.MadrigalCedarFile(self.fullname, True)
471
472
464 except ValueError as e:
473 except ValueError as e:
465 log.error(
474 log.error(
466 'Impossible to create a cedar object with "madrigal.cedar.MadrigalCedarFile"',
475 'Impossible to create a cedar object with "madrigal.cedar.MadrigalCedarFile"',
467 'MADWriter')
476 'MADWriter')
468 return
477 return
469
478
470 return 1
479 return 1
471
480
472 def writeBlock(self):
481 def writeBlock(self):
473 '''
482 '''
474 Add data records to cedar file taking data from oneDDict and twoDDict
483 Add data records to cedar file taking data from oneDDict and twoDDict
475 attributes.
484 attributes.
476 Allowed parameters in: parcodes.tab
485 Allowed parameters in: parcodes.tab
477 '''
486 '''
478
487 #self.dataOut.paramInterval=2
479 startTime = datetime.datetime.utcfromtimestamp(self.dataOut.utctime)
488 startTime = datetime.datetime.utcfromtimestamp(self.dataOut.utctime)
489
480 endTime = startTime + datetime.timedelta(seconds=self.dataOut.paramInterval)
490 endTime = startTime + datetime.timedelta(seconds=self.dataOut.paramInterval)
491
492 #if self.dataOut.input_dat_type:
493 #if self.dataOut.experiment=="DP":
494 #startTime=datetime.datetime.fromtimestamp(self.dataOut.TimeBlockSeconds_for_dp_power)
495 #endTime = startTime + datetime.timedelta(seconds=self.dataOut.paramInterval)
496
497
498 #print("2: ",startTime)
499 #print(endTime)
481 heights = self.dataOut.heightList
500 heights = self.dataOut.heightList
482
501
502 #print(self.blocks)
503 #print(startTime)
504 #print(endTime)
505 #print(heights)
506 #input()
483 if self.ext == '.dat':
507 if self.ext == '.dat':
484 for key, value in list(self.twoDDict.items()):
508 for key, value in list(self.twoDDict.items()):
485 if isinstance(value, str):
509 if isinstance(value, str):
486 data = getattr(self.dataOut, value)
510 data = getattr(self.dataOut, value)
487 invalid = numpy.isnan(data)
511 invalid = numpy.isnan(data)
488 data[invalid] = self.missing
512 data[invalid] = self.missing
489 elif isinstance(value, (tuple, list)):
513 elif isinstance(value, (tuple, list)):
490 attr, key = value
514 attr, key = value
491 data = getattr(self.dataOut, attr)
515 data = getattr(self.dataOut, attr)
492 invalid = numpy.isnan(data)
516 invalid = numpy.isnan(data)
493 data[invalid] = self.missing
517 data[invalid] = self.missing
494
518
495 out = {}
519 out = {}
496 for key, value in list(self.twoDDict.items()):
520 for key, value in list(self.twoDDict.items()):
497 key = key.lower()
521 key = key.lower()
498 if isinstance(value, str):
522 if isinstance(value, str):
499 if 'db' in value.lower():
523 if 'db' in value.lower():
500 tmp = getattr(self.dataOut, value.replace('_db', ''))
524 tmp = getattr(self.dataOut, value.replace('_db', ''))
501 SNRavg = numpy.average(tmp, axis=0)
525 SNRavg = numpy.average(tmp, axis=0)
502 tmp = 10*numpy.log10(SNRavg)
526 tmp = 10*numpy.log10(SNRavg)
503 else:
527 else:
504 tmp = getattr(self.dataOut, value)
528 tmp = getattr(self.dataOut, value)
505 out[key] = tmp.flatten()[:len(heights)]
529 out[key] = tmp.flatten()[:len(heights)]
506 elif isinstance(value, (tuple, list)):
530 elif isinstance(value, (tuple, list)):
507 attr, x = value
531 attr, x = value
508 data = getattr(self.dataOut, attr)
532 data = getattr(self.dataOut, attr)
533 #print(x)
534 #print(len(heights))
535 #print(data[int(x)][:len(heights)])
536 #print(numpy.shape(out))
537 #print(numpy.shape(data))
538
509 out[key] = data[int(x)][:len(heights)]
539 out[key] = data[int(x)][:len(heights)]
510
540
511 a = numpy.array([out[k] for k in self.keys])
541 a = numpy.array([out[k] for k in self.keys])
542 #print(a)
512 nrows = numpy.array([numpy.isnan(a[:, x]).all() for x in range(len(heights))])
543 nrows = numpy.array([numpy.isnan(a[:, x]).all() for x in range(len(heights))])
513 index = numpy.where(nrows == False)[0]
544 index = numpy.where(nrows == False)[0]
514
545
546 #print(startTime.minute)
515 rec = madrigal.cedar.MadrigalDataRecord(
547 rec = madrigal.cedar.MadrigalDataRecord(
516 self.kinst,
548 self.kinst,
517 self.kindat,
549 self.kindat,
518 startTime.year,
550 startTime.year,
519 startTime.month,
551 startTime.month,
520 startTime.day,
552 startTime.day,
521 startTime.hour,
553 startTime.hour,
522 startTime.minute,
554 startTime.minute,
523 startTime.second,
555 startTime.second,
524 startTime.microsecond/10000,
556 startTime.microsecond/10000,
525 endTime.year,
557 endTime.year,
526 endTime.month,
558 endTime.month,
527 endTime.day,
559 endTime.day,
528 endTime.hour,
560 endTime.hour,
529 endTime.minute,
561 endTime.minute,
530 endTime.second,
562 endTime.second,
531 endTime.microsecond/10000,
563 endTime.microsecond/10000,
532 list(self.oneDDict.keys()),
564 list(self.oneDDict.keys()),
533 list(self.twoDDict.keys()),
565 list(self.twoDDict.keys()),
534 len(index),
566 len(index),
535 **self.extra_args
567 **self.extra_args
536 )
568 )
537
569 #print("rec",rec)
538 # Setting 1d values
570 # Setting 1d values
539 for key in self.oneDDict:
571 for key in self.oneDDict:
540 rec.set1D(key, getattr(self.dataOut, self.oneDDict[key]))
572 rec.set1D(key, getattr(self.dataOut, self.oneDDict[key]))
541
573
542 # Setting 2d values
574 # Setting 2d values
543 nrec = 0
575 nrec = 0
544 for n in index:
576 for n in index:
545 for key in out:
577 for key in out:
546 rec.set2D(key, nrec, out[key][n])
578 rec.set2D(key, nrec, out[key][n])
547 nrec += 1
579 nrec += 1
548
580
549 self.fp.append(rec)
581 self.fp.append(rec)
550 if self.ext == '.hdf5' and self.counter % 500 == 0 and self.counter > 0:
582 if self.ext == '.hdf5' and self.counter %2 == 0 and self.counter > 0:
583 #print("here")
551 self.fp.dump()
584 self.fp.dump()
552 if self.counter % 20 == 0 and self.counter > 0:
585 if self.counter % 20 == 0 and self.counter > 0:
586 #self.fp.write()
553 log.log(
587 log.log(
554 'Writing {} records'.format(
588 'Writing {} records'.format(
555 self.counter),
589 self.counter),
556 'MADWriter')
590 'MADWriter')
557
591
558 def setHeader(self):
592 def setHeader(self):
559 '''
593 '''
560 Create an add catalog and header to cedar file
594 Create an add catalog and header to cedar file
561 '''
595 '''
562
596
563 log.success('Closing file {}'.format(self.fullname), 'MADWriter')
597 log.success('Closing file {}'.format(self.fullname), 'MADWriter')
564
598
565 if self.ext == '.dat':
599 if self.ext == '.dat':
566 self.fp.write()
600 self.fp.write()
567 else:
601 else:
568 self.fp.dump()
602 self.fp.dump()
569 self.fp.close()
603 self.fp.close()
570
604
571 header = madrigal.cedar.CatalogHeaderCreator(self.fullname)
605 header = madrigal.cedar.CatalogHeaderCreator(self.fullname)
572 header.createCatalog(**self.catalog)
606 header.createCatalog(**self.catalog)
573 header.createHeader(**self.header)
607 header.createHeader(**self.header)
574 header.write()
608 header.write()
575
609
576 def putData(self):
610 def putData(self):
577
611
578 if self.dataOut.flagNoData:
612 if self.dataOut.flagNoData:
579 return 0
613 return 0
580
614
581 if self.dataOut.flagDiscontinuousBlock or self.counter == self.blocks:
615 if self.dataOut.flagDiscontinuousBlock or self.counter == self.blocks:
582 if self.counter > 0:
616 if self.counter > 0:
583 self.setHeader()
617 self.setHeader()
584 self.counter = 0
618 self.counter = 0
585
619
586 if self.counter == 0:
620 if self.counter == 0:
587 self.setFile()
621 self.setFile()
588
622
589 self.writeBlock()
623 self.writeBlock()
590 self.counter += 1
624 self.counter += 1
591
625
592 def close(self):
626 def close(self):
593
627
594 if self.counter > 0:
628 if self.counter > 0:
595 self.setHeader() No newline at end of file
629 self.setHeader()
@@ -1,626 +1,626
1 import os
1 import os
2 import time
2 import time
3 import datetime
3 import datetime
4
4
5 import numpy
5 import numpy
6 import h5py
6 import h5py
7
7
8 import schainpy.admin
8 import schainpy.admin
9 from schainpy.model.data.jrodata import *
9 from schainpy.model.data.jrodata import *
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
11 from schainpy.model.io.jroIO_base import *
11 from schainpy.model.io.jroIO_base import *
12 from schainpy.utils import log
12 from schainpy.utils import log
13
13
14
14
15 class HDFReader(Reader, ProcessingUnit):
15 class HDFReader(Reader, ProcessingUnit):
16 """Processing unit to read HDF5 format files
16 """Processing unit to read HDF5 format files
17
17
18 This unit reads HDF5 files created with `HDFWriter` operation contains
18 This unit reads HDF5 files created with `HDFWriter` operation contains
19 by default two groups Data and Metadata all variables would be saved as `dataOut`
19 by default two groups Data and Metadata all variables would be saved as `dataOut`
20 attributes.
20 attributes.
21 It is possible to read any HDF5 file by given the structure in the `description`
21 It is possible to read any HDF5 file by given the structure in the `description`
22 parameter, also you can add extra values to metadata with the parameter `extras`.
22 parameter, also you can add extra values to metadata with the parameter `extras`.
23
23
24 Parameters:
24 Parameters:
25 -----------
25 -----------
26 path : str
26 path : str
27 Path where files are located.
27 Path where files are located.
28 startDate : date
28 startDate : date
29 Start date of the files
29 Start date of the files
30 endDate : list
30 endDate : list
31 End date of the files
31 End date of the files
32 startTime : time
32 startTime : time
33 Start time of the files
33 Start time of the files
34 endTime : time
34 endTime : time
35 End time of the files
35 End time of the files
36 description : dict, optional
36 description : dict, optional
37 Dictionary with the description of the HDF5 file
37 Dictionary with the description of the HDF5 file
38 extras : dict, optional
38 extras : dict, optional
39 Dictionary with extra metadata to be be added to `dataOut`
39 Dictionary with extra metadata to be be added to `dataOut`
40
40
41 Examples
41 Examples
42 --------
42 --------
43
43
44 desc = {
44 desc = {
45 'Data': {
45 'Data': {
46 'data_output': ['u', 'v', 'w'],
46 'data_output': ['u', 'v', 'w'],
47 'utctime': 'timestamps',
47 'utctime': 'timestamps',
48 } ,
48 } ,
49 'Metadata': {
49 'Metadata': {
50 'heightList': 'heights'
50 'heightList': 'heights'
51 }
51 }
52 }
52 }
53
53
54 desc = {
54 desc = {
55 'Data': {
55 'Data': {
56 'data_output': 'winds',
56 'data_output': 'winds',
57 'utctime': 'timestamps'
57 'utctime': 'timestamps'
58 },
58 },
59 'Metadata': {
59 'Metadata': {
60 'heightList': 'heights'
60 'heightList': 'heights'
61 }
61 }
62 }
62 }
63
63
64 extras = {
64 extras = {
65 'timeZone': 300
65 'timeZone': 300
66 }
66 }
67
67
68 reader = project.addReadUnit(
68 reader = project.addReadUnit(
69 name='HDFReader',
69 name='HDFReader',
70 path='/path/to/files',
70 path='/path/to/files',
71 startDate='2019/01/01',
71 startDate='2019/01/01',
72 endDate='2019/01/31',
72 endDate='2019/01/31',
73 startTime='00:00:00',
73 startTime='00:00:00',
74 endTime='23:59:59',
74 endTime='23:59:59',
75 # description=json.dumps(desc),
75 # description=json.dumps(desc),
76 # extras=json.dumps(extras),
76 # extras=json.dumps(extras),
77 )
77 )
78
78
79 """
79 """
80
80
81 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'description', 'extras']
81 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'description', 'extras']
82
82
83 def __init__(self):
83 def __init__(self):
84 ProcessingUnit.__init__(self)
84 ProcessingUnit.__init__(self)
85 self.dataOut = Parameters()
85 self.dataOut = Parameters()
86 self.ext = ".hdf5"
86 self.ext = ".hdf5"
87 self.optchar = "D"
87 self.optchar = "D"
88 self.meta = {}
88 self.meta = {}
89 self.data = {}
89 self.data = {}
90 self.open_file = h5py.File
90 self.open_file = h5py.File
91 self.open_mode = 'r'
91 self.open_mode = 'r'
92 self.description = {}
92 self.description = {}
93 self.extras = {}
93 self.extras = {}
94 self.filefmt = "*%Y%j***"
94 self.filefmt = "*%Y%j***"
95 self.folderfmt = "*%Y%j"
95 self.folderfmt = "*%Y%j"
96 self.utcoffset = 0
96 self.utcoffset = 0
97
97
98 def setup(self, **kwargs):
98 def setup(self, **kwargs):
99
99
100 self.set_kwargs(**kwargs)
100 self.set_kwargs(**kwargs)
101 if not self.ext.startswith('.'):
101 if not self.ext.startswith('.'):
102 self.ext = '.{}'.format(self.ext)
102 self.ext = '.{}'.format(self.ext)
103
103
104 if self.online:
104 if self.online:
105 log.log("Searching files in online mode...", self.name)
105 log.log("Searching files in online mode...", self.name)
106
106
107 for nTries in range(self.nTries):
107 for nTries in range(self.nTries):
108 fullpath = self.searchFilesOnLine(self.path, self.startDate,
108 fullpath = self.searchFilesOnLine(self.path, self.startDate,
109 self.endDate, self.expLabel, self.ext, self.walk,
109 self.endDate, self.expLabel, self.ext, self.walk,
110 self.filefmt, self.folderfmt)
110 self.filefmt, self.folderfmt)
111 try:
111 try:
112 fullpath = next(fullpath)
112 fullpath = next(fullpath)
113 except:
113 except:
114 fullpath = None
114 fullpath = None
115
115
116 if fullpath:
116 if fullpath:
117 break
117 break
118
118
119 log.warning(
119 log.warning(
120 'Waiting {} sec for a valid file in {}: try {} ...'.format(
120 'Waiting {} sec for a valid file in {}: try {} ...'.format(
121 self.delay, self.path, nTries + 1),
121 self.delay, self.path, nTries + 1),
122 self.name)
122 self.name)
123 time.sleep(self.delay)
123 time.sleep(self.delay)
124
124
125 if not(fullpath):
125 if not(fullpath):
126 raise schainpy.admin.SchainError(
126 raise schainpy.admin.SchainError(
127 'There isn\'t any valid file in {}'.format(self.path))
127 'There isn\'t any valid file in {}'.format(self.path))
128
128
129 pathname, filename = os.path.split(fullpath)
129 pathname, filename = os.path.split(fullpath)
130 self.year = int(filename[1:5])
130 self.year = int(filename[1:5])
131 self.doy = int(filename[5:8])
131 self.doy = int(filename[5:8])
132 self.set = int(filename[8:11]) - 1
132 self.set = int(filename[8:11]) - 1
133 else:
133 else:
134 log.log("Searching files in {}".format(self.path), self.name)
134 log.log("Searching files in {}".format(self.path), self.name)
135 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
135 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
136 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
136 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
137
137
138 self.setNextFile()
138 self.setNextFile()
139
139
140 return
140 return
141
141
142 def readFirstHeader(self):
142 def readFirstHeader(self):
143 '''Read metadata and data'''
143 '''Read metadata and data'''
144
144
145 self.__readMetadata()
145 self.__readMetadata()
146 self.__readData()
146 self.__readData()
147 self.__setBlockList()
147 self.__setBlockList()
148
148
149 if 'type' in self.meta:
149 if 'type' in self.meta:
150 self.dataOut = eval(self.meta['type'])()
150 self.dataOut = eval(self.meta['type'])()
151
151
152 for attr in self.meta:
152 for attr in self.meta:
153 setattr(self.dataOut, attr, self.meta[attr])
153 setattr(self.dataOut, attr, self.meta[attr])
154
154
155 self.blockIndex = 0
155 self.blockIndex = 0
156
156
157 return
157 return
158
158
159 def __setBlockList(self):
159 def __setBlockList(self):
160 '''
160 '''
161 Selects the data within the times defined
161 Selects the data within the times defined
162
162
163 self.fp
163 self.fp
164 self.startTime
164 self.startTime
165 self.endTime
165 self.endTime
166 self.blockList
166 self.blockList
167 self.blocksPerFile
167 self.blocksPerFile
168
168
169 '''
169 '''
170
170
171 startTime = self.startTime
171 startTime = self.startTime
172 endTime = self.endTime
172 endTime = self.endTime
173 thisUtcTime = self.data['utctime'] + self.utcoffset
173 thisUtcTime = self.data['utctime'] + self.utcoffset
174 self.interval = numpy.min(thisUtcTime[1:] - thisUtcTime[:-1])
174 self.interval = numpy.min(thisUtcTime[1:] - thisUtcTime[:-1])
175 thisDatetime = datetime.datetime.utcfromtimestamp(thisUtcTime[0])
175 thisDatetime = datetime.datetime.utcfromtimestamp(thisUtcTime[0])
176
176
177 thisDate = thisDatetime.date()
177 thisDate = thisDatetime.date()
178 thisTime = thisDatetime.time()
178 thisTime = thisDatetime.time()
179
179
180 startUtcTime = (datetime.datetime.combine(thisDate, startTime) - datetime.datetime(1970, 1, 1)).total_seconds()
180 startUtcTime = (datetime.datetime.combine(thisDate, startTime) - datetime.datetime(1970, 1, 1)).total_seconds()
181 endUtcTime = (datetime.datetime.combine(thisDate, endTime) - datetime.datetime(1970, 1, 1)).total_seconds()
181 endUtcTime = (datetime.datetime.combine(thisDate, endTime) - datetime.datetime(1970, 1, 1)).total_seconds()
182
182
183 ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0]
183 ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0]
184
184
185 self.blockList = ind
185 self.blockList = ind
186 self.blocksPerFile = len(ind)
186 self.blocksPerFile = len(ind)
187 return
187 return
188
188
189 def __readMetadata(self):
189 def __readMetadata(self):
190 '''
190 '''
191 Reads Metadata
191 Reads Metadata
192 '''
192 '''
193
193
194 meta = {}
194 meta = {}
195
195
196 if self.description:
196 if self.description:
197 for key, value in self.description['Metadata'].items():
197 for key, value in self.description['Metadata'].items():
198 meta[key] = self.fp[value][()]
198 meta[key] = self.fp[value][()]
199 else:
199 else:
200 grp = self.fp['Metadata']
200 grp = self.fp['Metadata']
201 for name in grp:
201 for name in grp:
202 meta[name] = grp[name][()]
202 meta[name] = grp[name][()]
203
203
204 if self.extras:
204 if self.extras:
205 for key, value in self.extras.items():
205 for key, value in self.extras.items():
206 meta[key] = value
206 meta[key] = value
207 self.meta = meta
207 self.meta = meta
208
208
209 return
209 return
210
210
211 def __readData(self):
211 def __readData(self):
212
212
213 data = {}
213 data = {}
214
214
215 if self.description:
215 if self.description:
216 for key, value in self.description['Data'].items():
216 for key, value in self.description['Data'].items():
217 if isinstance(value, str):
217 if isinstance(value, str):
218 if isinstance(self.fp[value], h5py.Dataset):
218 if isinstance(self.fp[value], h5py.Dataset):
219 data[key] = self.fp[value][()]
219 data[key] = self.fp[value][()]
220 elif isinstance(self.fp[value], h5py.Group):
220 elif isinstance(self.fp[value], h5py.Group):
221 array = []
221 array = []
222 for ch in self.fp[value]:
222 for ch in self.fp[value]:
223 array.append(self.fp[value][ch][()])
223 array.append(self.fp[value][ch][()])
224 data[key] = numpy.array(array)
224 data[key] = numpy.array(array)
225 elif isinstance(value, list):
225 elif isinstance(value, list):
226 array = []
226 array = []
227 for ch in value:
227 for ch in value:
228 array.append(self.fp[ch][()])
228 array.append(self.fp[ch][()])
229 data[key] = numpy.array(array)
229 data[key] = numpy.array(array)
230 else:
230 else:
231 grp = self.fp['Data']
231 grp = self.fp['Data']
232 for name in grp:
232 for name in grp:
233 if isinstance(grp[name], h5py.Dataset):
233 if isinstance(grp[name], h5py.Dataset):
234 array = grp[name][()]
234 array = grp[name][()]
235 elif isinstance(grp[name], h5py.Group):
235 elif isinstance(grp[name], h5py.Group):
236 array = []
236 array = []
237 for ch in grp[name]:
237 for ch in grp[name]:
238 array.append(grp[name][ch][()])
238 array.append(grp[name][ch][()])
239 array = numpy.array(array)
239 array = numpy.array(array)
240 else:
240 else:
241 log.warning('Unknown type: {}'.format(name))
241 log.warning('Unknown type: {}'.format(name))
242
242
243 if name in self.description:
243 if name in self.description:
244 key = self.description[name]
244 key = self.description[name]
245 else:
245 else:
246 key = name
246 key = name
247 data[key] = array
247 data[key] = array
248
248
249 self.data = data
249 self.data = data
250 return
250 return
251
251
252 def getData(self):
252 def getData(self):
253
253
254 for attr in self.data:
254 for attr in self.data:
255 if self.data[attr].ndim == 1:
255 if self.data[attr].ndim == 1:
256 setattr(self.dataOut, attr, self.data[attr][self.blockIndex])
256 setattr(self.dataOut, attr, self.data[attr][self.blockIndex])
257 else:
257 else:
258 setattr(self.dataOut, attr, self.data[attr][:, self.blockIndex])
258 setattr(self.dataOut, attr, self.data[attr][:, self.blockIndex])
259
259
260 self.dataOut.flagNoData = False
260 self.dataOut.flagNoData = False
261 self.blockIndex += 1
261 self.blockIndex += 1
262
262
263 log.log("Block No. {}/{} -> {}".format(
263 log.log("Block No. {}/{} -> {}".format(
264 self.blockIndex,
264 self.blockIndex,
265 self.blocksPerFile,
265 self.blocksPerFile,
266 self.dataOut.datatime.ctime()), self.name)
266 self.dataOut.datatime.ctime()), self.name)
267
267
268 return
268 return
269
269
270 def run(self, **kwargs):
270 def run(self, **kwargs):
271
271
272 if not(self.isConfig):
272 if not(self.isConfig):
273 self.setup(**kwargs)
273 self.setup(**kwargs)
274 self.isConfig = True
274 self.isConfig = True
275
275
276 if self.blockIndex == self.blocksPerFile:
276 if self.blockIndex == self.blocksPerFile:
277 self.setNextFile()
277 self.setNextFile()
278
278
279 self.getData()
279 self.getData()
280
280
281 return
281 return
282
282
283 @MPDecorator
283 @MPDecorator
284 class HDFWriter(Operation):
284 class HDFWriter(Operation):
285 """Operation to write HDF5 files.
285 """Operation to write HDF5 files.
286
286
287 The HDF5 file contains by default two groups Data and Metadata where
287 The HDF5 file contains by default two groups Data and Metadata where
288 you can save any `dataOut` attribute specified by `dataList` and `metadataList`
288 you can save any `dataOut` attribute specified by `dataList` and `metadataList`
289 parameters, data attributes are normaly time dependent where the metadata
289 parameters, data attributes are normaly time dependent where the metadata
290 are not.
290 are not.
291 It is possible to customize the structure of the HDF5 file with the
291 It is possible to customize the structure of the HDF5 file with the
292 optional description parameter see the examples.
292 optional description parameter see the examples.
293
293
294 Parameters:
294 Parameters:
295 -----------
295 -----------
296 path : str
296 path : str
297 Path where files will be saved.
297 Path where files will be saved.
298 blocksPerFile : int
298 blocksPerFile : int
299 Number of blocks per file
299 Number of blocks per file
300 metadataList : list
300 metadataList : list
301 List of the dataOut attributes that will be saved as metadata
301 List of the dataOut attributes that will be saved as metadata
302 dataList : int
302 dataList : int
303 List of the dataOut attributes that will be saved as data
303 List of the dataOut attributes that will be saved as data
304 setType : bool
304 setType : bool
305 If True the name of the files corresponds to the timestamp of the data
305 If True the name of the files corresponds to the timestamp of the data
306 description : dict, optional
306 description : dict, optional
307 Dictionary with the desired description of the HDF5 file
307 Dictionary with the desired description of the HDF5 file
308
308
309 Examples
309 Examples
310 --------
310 --------
311
311
312 desc = {
312 desc = {
313 'data_output': {'winds': ['z', 'w', 'v']},
313 'data_output': {'winds': ['z', 'w', 'v']},
314 'utctime': 'timestamps',
314 'utctime': 'timestamps',
315 'heightList': 'heights'
315 'heightList': 'heights'
316 }
316 }
317 desc = {
317 desc = {
318 'data_output': ['z', 'w', 'v'],
318 'data_output': ['z', 'w', 'v'],
319 'utctime': 'timestamps',
319 'utctime': 'timestamps',
320 'heightList': 'heights'
320 'heightList': 'heights'
321 }
321 }
322 desc = {
322 desc = {
323 'Data': {
323 'Data': {
324 'data_output': 'winds',
324 'data_output': 'winds',
325 'utctime': 'timestamps'
325 'utctime': 'timestamps'
326 },
326 },
327 'Metadata': {
327 'Metadata': {
328 'heightList': 'heights'
328 'heightList': 'heights'
329 }
329 }
330 }
330 }
331
331
332 writer = proc_unit.addOperation(name='HDFWriter')
332 writer = proc_unit.addOperation(name='HDFWriter')
333 writer.addParameter(name='path', value='/path/to/file')
333 writer.addParameter(name='path', value='/path/to/file')
334 writer.addParameter(name='blocksPerFile', value='32')
334 writer.addParameter(name='blocksPerFile', value='32')
335 writer.addParameter(name='metadataList', value='heightList,timeZone')
335 writer.addParameter(name='metadataList', value='heightList,timeZone')
336 writer.addParameter(name='dataList',value='data_output,utctime')
336 writer.addParameter(name='dataList',value='data_output,utctime')
337 # writer.addParameter(name='description',value=json.dumps(desc))
337 # writer.addParameter(name='description',value=json.dumps(desc))
338
338
339 """
339 """
340
340
341 ext = ".hdf5"
341 ext = ".hdf5"
342 optchar = "D"
342 optchar = "D"
343 filename = None
343 filename = None
344 path = None
344 path = None
345 setFile = None
345 setFile = None
346 fp = None
346 fp = None
347 firsttime = True
347 firsttime = True
348 #Configurations
348 #Configurations
349 blocksPerFile = None
349 blocksPerFile = None
350 blockIndex = None
350 blockIndex = None
351 dataOut = None
351 dataOut = None
352 #Data Arrays
352 #Data Arrays
353 dataList = None
353 dataList = None
354 metadataList = None
354 metadataList = None
355 currentDay = None
355 currentDay = None
356 lastTime = None
356 lastTime = None
357
357
358 def __init__(self):
358 def __init__(self):
359
359
360 Operation.__init__(self)
360 Operation.__init__(self)
361 return
361 return
362
362
363 def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None, description=None):
363 def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None, description=None):
364 self.path = path
364 self.path = path
365 self.blocksPerFile = blocksPerFile
365 self.blocksPerFile = blocksPerFile
366 self.metadataList = metadataList
366 self.metadataList = metadataList
367 self.dataList = [s.strip() for s in dataList]
367 self.dataList = [s.strip() for s in dataList]
368 self.setType = setType
368 self.setType = setType
369 self.description = description
369 self.description = description
370
370
371 if self.metadataList is None:
371 if self.metadataList is None:
372 self.metadataList = self.dataOut.metadata_list
372 self.metadataList = self.dataOut.metadata_list
373
373
374 tableList = []
374 tableList = []
375 dsList = []
375 dsList = []
376
376
377 for i in range(len(self.dataList)):
377 for i in range(len(self.dataList)):
378 dsDict = {}
378 dsDict = {}
379 if hasattr(self.dataOut, self.dataList[i]):
379 if hasattr(self.dataOut, self.dataList[i]):
380 dataAux = getattr(self.dataOut, self.dataList[i])
380 dataAux = getattr(self.dataOut, self.dataList[i])
381 dsDict['variable'] = self.dataList[i]
381 dsDict['variable'] = self.dataList[i]
382 else:
382 else:
383 log.warning('Attribute {} not found in dataOut', self.name)
383 log.warning('Attribute {} not found in dataOut', self.name)
384 continue
384 continue
385
385
386 if dataAux is None:
386 if dataAux is None:
387 continue
387 continue
388 elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)):
388 elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)):
389 dsDict['nDim'] = 0
389 dsDict['nDim'] = 0
390 else:
390 else:
391 dsDict['nDim'] = len(dataAux.shape)
391 dsDict['nDim'] = len(dataAux.shape)
392 dsDict['shape'] = dataAux.shape
392 dsDict['shape'] = dataAux.shape
393 dsDict['dsNumber'] = dataAux.shape[0]
393 dsDict['dsNumber'] = dataAux.shape[0]
394 dsDict['dtype'] = dataAux.dtype
394 dsDict['dtype'] = dataAux.dtype
395
395
396 dsList.append(dsDict)
396 dsList.append(dsDict)
397
397
398 self.dsList = dsList
398 self.dsList = dsList
399 self.currentDay = self.dataOut.datatime.date()
399 self.currentDay = self.dataOut.datatime.date()
400
400
401 def timeFlag(self):
401 def timeFlag(self):
402 currentTime = self.dataOut.utctime
402 currentTime = self.dataOut.utctime
403 timeTuple = time.localtime(currentTime)
403 timeTuple = time.localtime(currentTime)
404 dataDay = timeTuple.tm_yday
404 dataDay = timeTuple.tm_yday
405
405
406 if self.lastTime is None:
406 if self.lastTime is None:
407 self.lastTime = currentTime
407 self.lastTime = currentTime
408 self.currentDay = dataDay
408 self.currentDay = dataDay
409 return False
409 return False
410
410
411 timeDiff = currentTime - self.lastTime
411 timeDiff = currentTime - self.lastTime
412
412
413 #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
413 #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
414 if dataDay != self.currentDay:
414 if dataDay != self.currentDay:
415 self.currentDay = dataDay
415 self.currentDay = dataDay
416 return True
416 return True
417 elif timeDiff > 3*60*60:
417 elif timeDiff > 3*60*60:
418 self.lastTime = currentTime
418 self.lastTime = currentTime
419 return True
419 return True
420 else:
420 else:
421 self.lastTime = currentTime
421 self.lastTime = currentTime
422 return False
422 return False
423
423
424 def run(self, dataOut, path, blocksPerFile=10, metadataList=None,
424 def run(self, dataOut, path, blocksPerFile=10, metadataList=None,
425 dataList=[], setType=None, description={}):
425 dataList=[], setType=None, description={}):
426
426
427 self.dataOut = dataOut
427 self.dataOut = dataOut
428 if not(self.isConfig):
428 if not(self.isConfig):
429 self.setup(path=path, blocksPerFile=blocksPerFile,
429 self.setup(path=path, blocksPerFile=blocksPerFile,
430 metadataList=metadataList, dataList=dataList,
430 metadataList=metadataList, dataList=dataList,
431 setType=setType, description=description)
431 setType=setType, description=description)
432
432
433 self.isConfig = True
433 self.isConfig = True
434 self.setNextFile()
434 self.setNextFile()
435
435
436 self.putData()
436 self.putData()
437 return
437 return
438
438
439 def setNextFile(self):
439 def setNextFile(self):
440
440
441 ext = self.ext
441 ext = self.ext
442 path = self.path
442 path = self.path
443 setFile = self.setFile
443 setFile = self.setFile
444
444
445 timeTuple = time.localtime(self.dataOut.utctime)
445 timeTuple = time.localtime(self.dataOut.utctime)
446 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
446 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
447 fullpath = os.path.join(path, subfolder)
447 fullpath = os.path.join(path, subfolder)
448
448
449 if os.path.exists(fullpath):
449 if os.path.exists(fullpath):
450 filesList = os.listdir(fullpath)
450 filesList = os.listdir(fullpath)
451 filesList = [k for k in filesList if k.startswith(self.optchar)]
451 filesList = [k for k in filesList if k.startswith(self.optchar)]
452 if len( filesList ) > 0:
452 if len( filesList ) > 0:
453 filesList = sorted(filesList, key=str.lower)
453 filesList = sorted(filesList, key=str.lower)
454 filen = filesList[-1]
454 filen = filesList[-1]
455 # el filename debera tener el siguiente formato
455 # el filename debera tener el siguiente formato
456 # 0 1234 567 89A BCDE (hex)
456 # 0 1234 567 89A BCDE (hex)
457 # x YYYY DDD SSS .ext
457 # x YYYY DDD SSS .ext
458 if isNumber(filen[8:11]):
458 if isNumber(filen[8:11]):
459 setFile = int(filen[8:11]) #inicializo mi contador de seteo al seteo del ultimo file
459 setFile = int(filen[8:11]) #inicializo mi contador de seteo al seteo del ultimo file
460 else:
460 else:
461 setFile = -1
461 setFile = -1
462 else:
462 else:
463 setFile = -1 #inicializo mi contador de seteo
463 setFile = -1 #inicializo mi contador de seteo
464 else:
464 else:
465 os.makedirs(fullpath)
465 os.makedirs(fullpath)
466 setFile = -1 #inicializo mi contador de seteo
466 setFile = -1 #inicializo mi contador de seteo
467
467
468 if self.setType is None:
468 if self.setType is None:
469 setFile += 1
469 setFile += 1
470 file = '%s%4.4d%3.3d%03d%s' % (self.optchar,
470 file = '%s%4.4d%3.3d%03d%s' % (self.optchar,
471 timeTuple.tm_year,
471 timeTuple.tm_year,
472 timeTuple.tm_yday,
472 timeTuple.tm_yday,
473 setFile,
473 setFile,
474 ext )
474 ext )
475 else:
475 else:
476 setFile = timeTuple.tm_hour*60+timeTuple.tm_min
476 setFile = timeTuple.tm_hour*60+timeTuple.tm_min
477 file = '%s%4.4d%3.3d%04d%s' % (self.optchar,
477 file = '%s%4.4d%3.3d%04d%s' % (self.optchar,
478 timeTuple.tm_year,
478 timeTuple.tm_year,
479 timeTuple.tm_yday,
479 timeTuple.tm_yday,
480 setFile,
480 setFile,
481 ext )
481 ext )
482
482
483 self.filename = os.path.join( path, subfolder, file )
483 self.filename = os.path.join( path, subfolder, file )
484
484
485 #Setting HDF5 File
485 #Setting HDF5 File
486 self.fp = h5py.File(self.filename, 'w')
486 self.fp = h5py.File(self.filename, 'w')
487 #write metadata
487 #write metadata
488 self.writeMetadata(self.fp)
488 self.writeMetadata(self.fp)
489 #Write data
489 #Write data
490 self.writeData(self.fp)
490 self.writeData(self.fp)
491
491
492 def getLabel(self, name, x=None):
492 def getLabel(self, name, x=None):
493
493
494 if x is None:
494 if x is None:
495 if 'Data' in self.description:
495 if 'Data' in self.description:
496 data = self.description['Data']
496 data = self.description['Data']
497 if 'Metadata' in self.description:
497 if 'Metadata' in self.description:
498 data.update(self.description['Metadata'])
498 data.update(self.description['Metadata'])
499 else:
499 else:
500 data = self.description
500 data = self.description
501 if name in data:
501 if name in data:
502 if isinstance(data[name], str):
502 if isinstance(data[name], str):
503 return data[name]
503 return data[name]
504 elif isinstance(data[name], list):
504 elif isinstance(data[name], list):
505 return None
505 return None
506 elif isinstance(data[name], dict):
506 elif isinstance(data[name], dict):
507 for key, value in data[name].items():
507 for key, value in data[name].items():
508 return key
508 return key
509 return name
509 return name
510 else:
510 else:
511 if 'Metadata' in self.description:
511 if 'Metadata' in self.description:
512 meta = self.description['Metadata']
512 meta = self.description['Metadata']
513 else:
513 else:
514 meta = self.description
514 meta = self.description
515 if name in meta:
515 if name in meta:
516 if isinstance(meta[name], list):
516 if isinstance(meta[name], list):
517 return meta[name][x]
517 return meta[name][x]
518 elif isinstance(meta[name], dict):
518 elif isinstance(meta[name], dict):
519 for key, value in meta[name].items():
519 for key, value in meta[name].items():
520 return value[x]
520 return value[x]
521 if 'cspc' in name:
521 if 'cspc' in name:
522 return 'pair{:02d}'.format(x)
522 return 'pair{:02d}'.format(x)
523 else:
523 else:
524 return 'channel{:02d}'.format(x)
524 return 'channel{:02d}'.format(x)
525
525
526 def writeMetadata(self, fp):
526 def writeMetadata(self, fp):
527
527
528 if self.description:
528 if self.description:
529 if 'Metadata' in self.description:
529 if 'Metadata' in self.description:
530 grp = fp.create_group('Metadata')
530 grp = fp.create_group('Metadata')
531 else:
531 else:
532 grp = fp
532 grp = fp
533 else:
533 else:
534 grp = fp.create_group('Metadata')
534 grp = fp.create_group('Metadata')
535
535
536 for i in range(len(self.metadataList)):
536 for i in range(len(self.metadataList)):
537 if not hasattr(self.dataOut, self.metadataList[i]):
537 if not hasattr(self.dataOut, self.metadataList[i]):
538 log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name)
538 log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name)
539 continue
539 continue
540 value = getattr(self.dataOut, self.metadataList[i])
540 value = getattr(self.dataOut, self.metadataList[i])
541 if isinstance(value, bool):
541 if isinstance(value, bool):
542 if value is True:
542 if value is True:
543 value = 1
543 value = 1
544 else:
544 else:
545 value = 0
545 value = 0
546 grp.create_dataset(self.getLabel(self.metadataList[i]), data=value)
546 grp.create_dataset(self.getLabel(self.metadataList[i]), data=value)
547 return
547 return
548
548
549 def writeData(self, fp):
549 def writeData(self, fp):
550
550
551 if self.description:
551 if self.description:
552 if 'Data' in self.description:
552 if 'Data' in self.description:
553 grp = fp.create_group('Data')
553 grp = fp.create_group('Data')
554 else:
554 else:
555 grp = fp
555 grp = fp
556 else:
556 else:
557 grp = fp.create_group('Data')
557 grp = fp.create_group('Data')
558
558
559 dtsets = []
559 dtsets = []
560 data = []
560 data = []
561
561
562 for dsInfo in self.dsList:
562 for dsInfo in self.dsList:
563 if dsInfo['nDim'] == 0:
563 if dsInfo['nDim'] == 0:
564 ds = grp.create_dataset(
564 ds = grp.create_dataset(
565 self.getLabel(dsInfo['variable']),
565 self.getLabel(dsInfo['variable']),
566 (self.blocksPerFile, ),
566 (self.blocksPerFile, ),
567 chunks=True,
567 chunks=True,
568 dtype=numpy.float64)
568 dtype=numpy.float64)
569 dtsets.append(ds)
569 dtsets.append(ds)
570 data.append((dsInfo['variable'], -1))
570 data.append((dsInfo['variable'], -1))
571 else:
571 else:
572 label = self.getLabel(dsInfo['variable'])
572 label = self.getLabel(dsInfo['variable'])
573 if label is not None:
573 if label is not None:
574 sgrp = grp.create_group(label)
574 sgrp = grp.create_group(label)
575 else:
575 else:
576 sgrp = grp
576 sgrp = grp
577 for i in range(dsInfo['dsNumber']):
577 for i in range(dsInfo['dsNumber']):
578 ds = sgrp.create_dataset(
578 ds = sgrp.create_dataset(
579 self.getLabel(dsInfo['variable'], i),
579 self.getLabel(dsInfo['variable'], i),
580 (self.blocksPerFile, ) + dsInfo['shape'][1:],
580 (self.blocksPerFile, ) + dsInfo['shape'][1:],
581 chunks=True,
581 chunks=True,
582 dtype=dsInfo['dtype'])
582 dtype=dsInfo['dtype'])
583 dtsets.append(ds)
583 dtsets.append(ds)
584 data.append((dsInfo['variable'], i))
584 data.append((dsInfo['variable'], i))
585 fp.flush()
585 fp.flush()
586
586
587 log.log('Creating file: {}'.format(fp.filename), self.name)
587 log.log('Creating file: {}'.format(fp.filename), self.name)
588
588
589 self.ds = dtsets
589 self.ds = dtsets
590 self.data = data
590 self.data = data
591 self.firsttime = True
591 self.firsttime = True
592 self.blockIndex = 0
592 self.blockIndex = 0
593 return
593 return
594
594
595 def putData(self):
595 def putData(self):
596
596
597 if (self.blockIndex == self.blocksPerFile) or self.timeFlag():
597 if (self.blockIndex == self.blocksPerFile) or self.timeFlag():
598 self.closeFile()
598 self.closeFile()
599 self.setNextFile()
599 self.setNextFile()
600
600
601 for i, ds in enumerate(self.ds):
601 for i, ds in enumerate(self.ds):
602 attr, ch = self.data[i]
602 attr, ch = self.data[i]
603 if ch == -1:
603 if ch == -1:
604 ds[self.blockIndex] = getattr(self.dataOut, attr)
604 ds[self.blockIndex] = getattr(self.dataOut, attr)
605 else:
605 else:
606 ds[self.blockIndex] = getattr(self.dataOut, attr)[ch]
606 ds[self.blockIndex] = getattr(self.dataOut, attr)[ch]
607
607
608 self.fp.flush()
608 self.fp.flush()
609 self.blockIndex += 1
609 self.blockIndex += 1
610 log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name)
610 log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name)
611
611
612 return
612 return
613
613
614 def closeFile(self):
614 def closeFile(self):
615
615
616 if self.blockIndex != self.blocksPerFile:
616 if self.blockIndex != self.blocksPerFile:
617 for ds in self.ds:
617 for ds in self.ds:
618 ds.resize(self.blockIndex, axis=0)
618 ds.resize(self.blockIndex, axis=0)
619
619
620 if self.fp:
620 if self.fp:
621 self.fp.flush()
621 self.fp.flush()
622 self.fp.close()
622 self.fp.close()
623
623
624 def close(self):
624 def close(self):
625
625
626 self.closeFile()
626 self.closeFile()
@@ -1,676 +1,692
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
6
7 import numpy
7 import numpy
8
8
9 from .jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
9 from .jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
11 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
11 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
12 from schainpy.model.data.jrodata import Voltage
12 from schainpy.model.data.jrodata import Voltage
13
13
14
14
15 class VoltageReader(JRODataReader, ProcessingUnit):
15 class VoltageReader(JRODataReader, ProcessingUnit):
16 """
16 """
17 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
17 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
18 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
18 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
19 perfiles*alturas*canales) son almacenados en la variable "buffer".
19 perfiles*alturas*canales) son almacenados en la variable "buffer".
20
20
21 perfiles * alturas * canales
21 perfiles * alturas * canales
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 Voltage. Los tres primeros se usan para almacenar informacion de la
24 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
25 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
25 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil 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
29
30 dpath = "/home/myuser/data"
30 dpath = "/home/myuser/data"
31
31
32 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
32 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
33
33
34 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
34 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
35
35
36 readerObj = VoltageReader()
36 readerObj = VoltageReader()
37
37
38 readerObj.setup(dpath, startTime, endTime)
38 readerObj.setup(dpath, startTime, endTime)
39
39
40 while(True):
40 while(True):
41
41
42 #to get one profile
42 #to get one profile
43 profile = readerObj.getData()
43 profile = readerObj.getData()
44
44
45 #print the profile
45 #print the profile
46 print profile
46 print profile
47
47
48 #If you want to see all datablock
48 #If you want to see all datablock
49 print readerObj.datablock
49 print readerObj.datablock
50
50
51 if readerObj.flagNoMoreFiles:
51 if readerObj.flagNoMoreFiles:
52 break
52 break
53
53
54 """
54 """
55
55
56 def __init__(self):
56 def __init__(self):
57 """
57 """
58 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
58 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
59
59
60 Input:
60 Input:
61 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
61 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
62 almacenar un perfil de datos cada vez que se haga un requerimiento
62 almacenar un perfil de datos cada vez que se haga un requerimiento
63 (getData). El perfil sera obtenido a partir del buffer de datos,
63 (getData). El perfil sera obtenido a partir del buffer de datos,
64 si el buffer esta vacio se hara un nuevo proceso de lectura de un
64 si el buffer esta vacio se hara un nuevo proceso de lectura de un
65 bloque de datos.
65 bloque de datos.
66 Si este parametro no es pasado se creara uno internamente.
66 Si este parametro no es pasado se creara uno internamente.
67
67
68 Variables afectadas:
68 Variables afectadas:
69 self.dataOut
69 self.dataOut
70
70
71 Return:
71 Return:
72 None
72 None
73 """
73 """
74
74
75 ProcessingUnit.__init__(self)
75 ProcessingUnit.__init__(self)
76
76
77 self.ext = ".r"
77 self.ext = ".r"
78 self.optchar = "D"
78 self.optchar = "D"
79 self.basicHeaderObj = BasicHeader(LOCALTIME)
79 self.basicHeaderObj = BasicHeader(LOCALTIME)
80 self.systemHeaderObj = SystemHeader()
80 self.systemHeaderObj = SystemHeader()
81 self.radarControllerHeaderObj = RadarControllerHeader()
81 self.radarControllerHeaderObj = RadarControllerHeader()
82
82 self.processingHeaderObj = ProcessingHeader()
83 self.processingHeaderObj = ProcessingHeader()
83 self.lastUTTime = 0
84 self.lastUTTime = 0
84 self.profileIndex = 2**32 - 1
85 self.profileIndex = 2**32 - 1
85 self.dataOut = Voltage()
86 self.dataOut = Voltage()
86 self.selBlocksize = None
87 self.selBlocksize = None
87 self.selBlocktime = None
88 self.selBlocktime = None
88
89 ##print("1--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
89 def createObjByDefault(self):
90 def createObjByDefault(self):
90
91 ##print("2--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
91 dataObj = Voltage()
92 dataObj = Voltage()
92
93
93 return dataObj
94 return dataObj
94
95
95 def __hasNotDataInBuffer(self):
96 def __hasNotDataInBuffer(self):
96
97 ##print("3--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
97 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock * self.nTxs:
98 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock * self.nTxs:
98 return 1
99 return 1
99
100
100 return 0
101 return 0
101
102
102 def getBlockDimension(self):
103 def getBlockDimension(self):
103 """
104 """
104 Obtiene la cantidad de puntos a leer por cada bloque de datos
105 Obtiene la cantidad de puntos a leer por cada bloque de datos
105
106
106 Affected:
107 Affected:
107 self.blocksize
108 self.blocksize
108
109
109 Return:
110 Return:
110 None
111 None
111 """
112 """
113 ##print("4--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
112 pts2read = self.processingHeaderObj.profilesPerBlock * \
114 pts2read = self.processingHeaderObj.profilesPerBlock * \
113 self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
115 self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
114 self.blocksize = pts2read
116 self.blocksize = pts2read
115
117
116 def readBlock(self):
118 def readBlock(self):
119
117 """
120 """
118 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
121 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
119 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
122 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
120 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
123 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
121 es seteado a 0
124 es seteado a 0
122
125
123 Inputs:
126 Inputs:
124 None
127 None
125
128
126 Return:
129 Return:
127 None
130 None
128
131
129 Affected:
132 Affected:
130 self.profileIndex
133 self.profileIndex
131 self.datablock
134 self.datablock
132 self.flagIsNewFile
135 self.flagIsNewFile
133 self.flagIsNewBlock
136 self.flagIsNewBlock
134 self.nTotalBlocks
137 self.nTotalBlocks
135
138
136 Exceptions:
139 Exceptions:
137 Si un bloque leido no es un bloque valido
140 Si un bloque leido no es un bloque valido
138 """
141 """
139
142 ##print("5--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
140 # if self.server is not None:
143 # if self.server is not None:
141 # self.zBlock = self.receiver.recv()
144 # self.zBlock = self.receiver.recv()
142 # self.zHeader = self.zBlock[:24]
145 # self.zHeader = self.zBlock[:24]
143 # self.zDataBlock = self.zBlock[24:]
146 # self.zDataBlock = self.zBlock[24:]
144 # junk = numpy.fromstring(self.zDataBlock, numpy.dtype([('real','<i4'),('imag','<i4')]))
147 # junk = numpy.fromstring(self.zDataBlock, numpy.dtype([('real','<i4'),('imag','<i4')]))
145 # self.processingHeaderObj.profilesPerBlock = 240
148 # self.processingHeaderObj.profilesPerBlock = 240
146 # self.processingHeaderObj.nHeights = 248
149 # self.processingHeaderObj.nHeights = 248
147 # self.systemHeaderObj.nChannels
150 # self.systemHeaderObj.nChannels
148 # else:
151 # else:
149 current_pointer_location = self.fp.tell()
152 current_pointer_location = self.fp.tell()
150 junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)
153 junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)
151
154
152 try:
155 try:
153 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
156 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
154 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
157 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
155 except:
158 except:
156 # print "The read block (%3d) has not enough data" %self.nReadBlocks
159 # print "The read block (%3d) has not enough data" %self.nReadBlocks
157
160
158 if self.waitDataBlock(pointer_location=current_pointer_location):
161 if self.waitDataBlock(pointer_location=current_pointer_location):
159 junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)
162 junk = numpy.fromfile(self.fp, self.dtype, self.blocksize)
160 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
163 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
161 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
164 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
162 # return 0
165 # return 0
163
166
164 # Dimensions : nChannels, nProfiles, nSamples
167 # Dimensions : nChannels, nProfiles, nSamples
165
168
166 junk = numpy.transpose(junk, (2, 0, 1))
169 junk = numpy.transpose(junk, (2, 0, 1))
167 self.datablock = junk['real'] + junk['imag'] * 1j
170 self.datablock = junk['real'] + junk['imag'] * 1j
168
171
169 self.profileIndex = 0
172 self.profileIndex = 0
170
173
171 self.flagIsNewFile = 0
174 self.flagIsNewFile = 0
172 self.flagIsNewBlock = 1
175 self.flagIsNewBlock = 1
173
176
174 self.nTotalBlocks += 1
177 self.nTotalBlocks += 1
175 self.nReadBlocks += 1
178 self.nReadBlocks += 1
176
179
177 return 1
180 return 1
178
181
179 def getFirstHeader(self):
182 def getFirstHeader(self):
183 ##print("6--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
180
184
181 self.getBasicHeader()
185 self.getBasicHeader()
182
186
183 self.dataOut.processingHeaderObj = self.processingHeaderObj.copy()
187 self.dataOut.processingHeaderObj = self.processingHeaderObj.copy()
184
188
185 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
189 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
186
190
187 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
191 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
188
192
193 #self.dataOut.ippSeconds_general=self.radarControllerHeaderObj.ippSeconds
194 #print(self.nTxs)
189 if self.nTxs > 1:
195 if self.nTxs > 1:
196 #print(self.radarControllerHeaderObj.ippSeconds)
190 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
197 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
198 #print(self.radarControllerHeaderObj.ippSeconds)
191 # Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj.
199 # Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj.
192
200
193 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt
201 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt
194 #
202 #
195 # if self.radarControllerHeaderObj.code is not None:
203 # if self.radarControllerHeaderObj.code is not None:
196 #
204 #
197 # self.dataOut.nCode = self.radarControllerHeaderObj.nCode
205 # self.dataOut.nCode = self.radarControllerHeaderObj.nCode
198 #
206 #
199 # self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
207 # self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
200 #
208 #
201 # self.dataOut.code = self.radarControllerHeaderObj.code
209 # self.dataOut.code = self.radarControllerHeaderObj.code
202
210
203 self.dataOut.dtype = self.dtype
211 self.dataOut.dtype = self.dtype
204
212
205 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
213 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
206
214
207 self.dataOut.heightList = numpy.arange(
215 self.dataOut.heightList = numpy.arange(
208 self.processingHeaderObj.nHeights) * self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
216 self.processingHeaderObj.nHeights) * self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
209
217
210 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
218 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
211
219
212 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
220 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
213
221
214 # asumo q la data no esta decodificada
222 # asumo q la data no esta decodificada
215 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode
223 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode
216
224
217 # asumo q la data no esta sin flip
225 # asumo q la data no esta sin flip
218 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip
226 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip
219
227
220 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
228 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
221
229
222 def reshapeData(self):
230 def reshapeData(self):
223
231 ##print("7--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
224 if self.nTxs < 0:
232 if self.nTxs < 0:
225 return
233 return
226
234
227 if self.nTxs == 1:
235 if self.nTxs == 1:
228 return
236 return
229
237
230 if self.nTxs < 1 and self.processingHeaderObj.profilesPerBlock % (1. / self.nTxs) != 0:
238 if self.nTxs < 1 and self.processingHeaderObj.profilesPerBlock % (1. / self.nTxs) != 0:
231 raise ValueError("1./nTxs (=%f), should be a multiple of nProfiles (=%d)" % (
239 raise ValueError("1./nTxs (=%f), should be a multiple of nProfiles (=%d)" % (
232 1. / self.nTxs, self.processingHeaderObj.profilesPerBlock))
240 1. / self.nTxs, self.processingHeaderObj.profilesPerBlock))
233
241
234 if self.nTxs > 1 and self.processingHeaderObj.nHeights % self.nTxs != 0:
242 if self.nTxs > 1 and self.processingHeaderObj.nHeights % self.nTxs != 0:
235 raise ValueError("nTxs (=%d), should be a multiple of nHeights (=%d)" % (
243 raise ValueError("nTxs (=%d), should be a multiple of nHeights (=%d)" % (
236 self.nTxs, self.processingHeaderObj.nHeights))
244 self.nTxs, self.processingHeaderObj.nHeights))
237
245
238 self.datablock = self.datablock.reshape(
246 self.datablock = self.datablock.reshape(
239 (self.systemHeaderObj.nChannels, self.processingHeaderObj.profilesPerBlock * self.nTxs, int(self.processingHeaderObj.nHeights / self.nTxs)))
247 (self.systemHeaderObj.nChannels, self.processingHeaderObj.profilesPerBlock * self.nTxs, int(self.processingHeaderObj.nHeights / self.nTxs)))
240
248
241 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock * self.nTxs
249 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock * self.nTxs
242 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights / self.nTxs) * \
250 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights / self.nTxs) * \
243 self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
251 self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
244 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
252 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
245
253
246 return
254 return
247
255
248 def readFirstHeaderFromServer(self):
256 def readFirstHeaderFromServer(self):
249
257
258 ##print("8--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
250 self.getFirstHeader()
259 self.getFirstHeader()
251
260
252 self.firstHeaderSize = self.basicHeaderObj.size
261 self.firstHeaderSize = self.basicHeaderObj.size
253
262
254 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
263 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
255 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
264 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
256 if datatype == 0:
265 if datatype == 0:
257 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
266 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
258 elif datatype == 1:
267 elif datatype == 1:
259 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
268 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
260 elif datatype == 2:
269 elif datatype == 2:
261 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
270 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
262 elif datatype == 3:
271 elif datatype == 3:
263 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
272 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
264 elif datatype == 4:
273 elif datatype == 4:
265 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
274 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
266 elif datatype == 5:
275 elif datatype == 5:
267 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
276 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
268 else:
277 else:
269 raise ValueError('Data type was not defined')
278 raise ValueError('Data type was not defined')
270
279
271 self.dtype = datatype_str
280 self.dtype = datatype_str
272 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
281 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
273 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
282 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
274 self.firstHeaderSize + self.basicHeaderSize * \
283 self.firstHeaderSize + self.basicHeaderSize * \
275 (self.processingHeaderObj.dataBlocksPerFile - 1)
284 (self.processingHeaderObj.dataBlocksPerFile - 1)
276 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
285 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
277 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
286 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
278 self.getBlockDimension()
287 self.getBlockDimension()
279
288
280 def getFromServer(self):
289 def getFromServer(self):
290 ##print("9--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
281 self.flagDiscontinuousBlock = 0
291 self.flagDiscontinuousBlock = 0
282 self.profileIndex = 0
292 self.profileIndex = 0
283 self.flagIsNewBlock = 1
293 self.flagIsNewBlock = 1
284 self.dataOut.flagNoData = False
294 self.dataOut.flagNoData = False
285 self.nTotalBlocks += 1
295 self.nTotalBlocks += 1
286 self.nReadBlocks += 1
296 self.nReadBlocks += 1
287 self.blockPointer = 0
297 self.blockPointer = 0
288
298
289 block = self.receiver.recv()
299 block = self.receiver.recv()
290
300
291 self.basicHeaderObj.read(block[self.blockPointer:])
301 self.basicHeaderObj.read(block[self.blockPointer:])
292 self.blockPointer += self.basicHeaderObj.length
302 self.blockPointer += self.basicHeaderObj.length
293 self.systemHeaderObj.read(block[self.blockPointer:])
303 self.systemHeaderObj.read(block[self.blockPointer:])
294 self.blockPointer += self.systemHeaderObj.length
304 self.blockPointer += self.systemHeaderObj.length
295 self.radarControllerHeaderObj.read(block[self.blockPointer:])
305 self.radarControllerHeaderObj.read(block[self.blockPointer:])
296 self.blockPointer += self.radarControllerHeaderObj.length
306 self.blockPointer += self.radarControllerHeaderObj.length
297 self.processingHeaderObj.read(block[self.blockPointer:])
307 self.processingHeaderObj.read(block[self.blockPointer:])
298 self.blockPointer += self.processingHeaderObj.length
308 self.blockPointer += self.processingHeaderObj.length
299 self.readFirstHeaderFromServer()
309 self.readFirstHeaderFromServer()
300
310
301 timestamp = self.basicHeaderObj.get_datatime()
311 timestamp = self.basicHeaderObj.get_datatime()
302 print('[Reading] - Block {} - {}'.format(self.nTotalBlocks, timestamp))
312 print('[Reading] - Block {} - {}'.format(self.nTotalBlocks, timestamp))
303 current_pointer_location = self.blockPointer
313 current_pointer_location = self.blockPointer
304 junk = numpy.fromstring(
314 junk = numpy.fromstring(
305 block[self.blockPointer:], self.dtype, self.blocksize)
315 block[self.blockPointer:], self.dtype, self.blocksize)
306
316
307 try:
317 try:
308 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
318 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
309 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
319 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
310 except:
320 except:
311 # print "The read block (%3d) has not enough data" %self.nReadBlocks
321 # print "The read block (%3d) has not enough data" %self.nReadBlocks
312 if self.waitDataBlock(pointer_location=current_pointer_location):
322 if self.waitDataBlock(pointer_location=current_pointer_location):
313 junk = numpy.fromstring(
323 junk = numpy.fromstring(
314 block[self.blockPointer:], self.dtype, self.blocksize)
324 block[self.blockPointer:], self.dtype, self.blocksize)
315 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
325 junk = junk.reshape((self.processingHeaderObj.profilesPerBlock,
316 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
326 self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels))
317 # return 0
327 # return 0
318
328
319 # Dimensions : nChannels, nProfiles, nSamples
329 # Dimensions : nChannels, nProfiles, nSamples
320
330
321 junk = numpy.transpose(junk, (2, 0, 1))
331 junk = numpy.transpose(junk, (2, 0, 1))
322 self.datablock = junk['real'] + junk['imag'] * 1j
332 self.datablock = junk['real'] + junk['imag'] * 1j
323 self.profileIndex = 0
333 self.profileIndex = 0
324 if self.selBlocksize == None:
334 if self.selBlocksize == None:
325 self.selBlocksize = self.dataOut.nProfiles
335 self.selBlocksize = self.dataOut.nProfiles
326 if self.selBlocktime != None:
336 if self.selBlocktime != None:
327 if self.dataOut.nCohInt is not None:
337 if self.dataOut.nCohInt is not None:
328 nCohInt = self.dataOut.nCohInt
338 nCohInt = self.dataOut.nCohInt
329 else:
339 else:
330 nCohInt = 1
340 nCohInt = 1
331 self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
341 self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
332 nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))
342 nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))
333 self.dataOut.data = self.datablock[:,
343 self.dataOut.data = self.datablock[:,
334 self.profileIndex:self.profileIndex + self.selBlocksize, :]
344 self.profileIndex:self.profileIndex + self.selBlocksize, :]
335 datasize = self.dataOut.data.shape[1]
345 datasize = self.dataOut.data.shape[1]
336 if datasize < self.selBlocksize:
346 if datasize < self.selBlocksize:
337 buffer = numpy.zeros(
347 buffer = numpy.zeros(
338 (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
348 (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
339 buffer[:, :datasize, :] = self.dataOut.data
349 buffer[:, :datasize, :] = self.dataOut.data
340 self.dataOut.data = buffer
350 self.dataOut.data = buffer
341 self.profileIndex = blockIndex
351 self.profileIndex = blockIndex
342
352
343 self.dataOut.flagDataAsBlock = True
353 self.dataOut.flagDataAsBlock = True
344 self.flagIsNewBlock = 1
354 self.flagIsNewBlock = 1
345 self.dataOut.realtime = self.online
355 self.dataOut.realtime = self.online
346
356
347 return self.dataOut.data
357 return self.dataOut.data
348
358
349 def getData(self):
359 def getData(self):
350 """
360 """
351 getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut
361 getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut
352 del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos
362 del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos
353 en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando
363 en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando
354 "readNextBlock"
364 "readNextBlock"
355
365
356 Ademas incrementa el contador del buffer "self.profileIndex" en 1.
366 Ademas incrementa el contador del buffer "self.profileIndex" en 1.
357
367
358 Return:
368 Return:
359
369
360 Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex
370 Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex
361 es igual al total de perfiles leidos desde el archivo.
371 es igual al total de perfiles leidos desde el archivo.
362
372
363 Si self.getByBlock == False:
373 Si self.getByBlock == False:
364
374
365 self.dataOut.data = buffer[:, thisProfile, :]
375 self.dataOut.data = buffer[:, thisProfile, :]
366
376
367 shape = [nChannels, nHeis]
377 shape = [nChannels, nHeis]
368
378
369 Si self.getByBlock == True:
379 Si self.getByBlock == True:
370
380
371 self.dataOut.data = buffer[:, :, :]
381 self.dataOut.data = buffer[:, :, :]
372
382
373 shape = [nChannels, nProfiles, nHeis]
383 shape = [nChannels, nProfiles, nHeis]
374
384
375 Variables afectadas:
385 Variables afectadas:
376 self.dataOut
386 self.dataOut
377 self.profileIndex
387 self.profileIndex
378
388
379 Affected:
389 Affected:
380 self.dataOut
390 self.dataOut
381 self.profileIndex
391 self.profileIndex
382 self.flagDiscontinuousBlock
392 self.flagDiscontinuousBlock
383 self.flagIsNewBlock
393 self.flagIsNewBlock
384 """
394 """
395
396 ##print("10--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
385 if self.flagNoMoreFiles:
397 if self.flagNoMoreFiles:
386 self.dataOut.flagNoData = True
398 self.dataOut.flagNoData = True
387 return 0
399 return 0
388 self.flagDiscontinuousBlock = 0
400 self.flagDiscontinuousBlock = 0
389 self.flagIsNewBlock = 0
401 self.flagIsNewBlock = 0
390 if self.__hasNotDataInBuffer():
402 if self.__hasNotDataInBuffer():
391 if not(self.readNextBlock()):
403 if not(self.readNextBlock()):
392 return 0
404 return 0
393
405
394 self.getFirstHeader()
406 self.getFirstHeader()
395
407
396 self.reshapeData()
408 self.reshapeData()
397 if self.datablock is None:
409 if self.datablock is None:
398 self.dataOut.flagNoData = True
410 self.dataOut.flagNoData = True
399 return 0
411 return 0
400
412
401 if not self.getByBlock:
413 if not self.getByBlock:
402
414
403 """
415 """
404 Return profile by profile
416 Return profile by profile
405
417
406 If nTxs > 1 then one profile is divided by nTxs and number of total
418 If nTxs > 1 then one profile is divided by nTxs and number of total
407 blocks is increased by nTxs (nProfiles *= nTxs)
419 blocks is increased by nTxs (nProfiles *= nTxs)
408 """
420 """
409 self.dataOut.flagDataAsBlock = False
421 self.dataOut.flagDataAsBlock = False
410 self.dataOut.data = self.datablock[:, self.profileIndex, :]
422 self.dataOut.data = self.datablock[:, self.profileIndex, :]
411 self.dataOut.profileIndex = self.profileIndex
423 self.dataOut.profileIndex = self.profileIndex
412
424
425
413 self.profileIndex += 1
426 self.profileIndex += 1
414
427
415 else:
428 else:
416 """
429 """
417 Return a block
430 Return a block
418 """
431 """
419 if self.selBlocksize == None:
432 if self.selBlocksize == None:
420 self.selBlocksize = self.dataOut.nProfiles
433 self.selBlocksize = self.dataOut.nProfiles
421 if self.selBlocktime != None:
434 if self.selBlocktime != None:
422 if self.dataOut.nCohInt is not None:
435 if self.dataOut.nCohInt is not None:
423 nCohInt = self.dataOut.nCohInt
436 nCohInt = self.dataOut.nCohInt
424 else:
437 else:
425 nCohInt = 1
438 nCohInt = 1
426 self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
439 self.selBlocksize = int(self.dataOut.nProfiles * round(self.selBlocktime / (
427 nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))
440 nCohInt * self.dataOut.ippSeconds * self.dataOut.nProfiles)))
428
441
429 self.dataOut.data = self.datablock[:,
442 self.dataOut.data = self.datablock[:,
430 self.profileIndex:self.profileIndex + self.selBlocksize, :]
443 self.profileIndex:self.profileIndex + self.selBlocksize, :]
431 self.profileIndex += self.selBlocksize
444 self.profileIndex += self.selBlocksize
432 datasize = self.dataOut.data.shape[1]
445 datasize = self.dataOut.data.shape[1]
433
446
434 if datasize < self.selBlocksize:
447 if datasize < self.selBlocksize:
435 buffer = numpy.zeros(
448 buffer = numpy.zeros(
436 (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
449 (self.dataOut.data.shape[0], self.selBlocksize, self.dataOut.data.shape[2]), dtype='complex')
437 buffer[:, :datasize, :] = self.dataOut.data
450 buffer[:, :datasize, :] = self.dataOut.data
438
451
439 while datasize < self.selBlocksize: # Not enough profiles to fill the block
452 while datasize < self.selBlocksize: # Not enough profiles to fill the block
440 if not(self.readNextBlock()):
453 if not(self.readNextBlock()):
441 return 0
454 return 0
442 self.getFirstHeader()
455 self.getFirstHeader()
443 self.reshapeData()
456 self.reshapeData()
444 if self.datablock is None:
457 if self.datablock is None:
445 self.dataOut.flagNoData = True
458 self.dataOut.flagNoData = True
446 return 0
459 return 0
447 # stack data
460 # stack data
448 blockIndex = self.selBlocksize - datasize
461 blockIndex = self.selBlocksize - datasize
449 datablock1 = self.datablock[:, :blockIndex, :]
462 datablock1 = self.datablock[:, :blockIndex, :]
450
463
451 buffer[:, datasize:datasize +
464 buffer[:, datasize:datasize +
452 datablock1.shape[1], :] = datablock1
465 datablock1.shape[1], :] = datablock1
453 datasize += datablock1.shape[1]
466 datasize += datablock1.shape[1]
454
467
455 self.dataOut.data = buffer
468 self.dataOut.data = buffer
456 self.profileIndex = blockIndex
469 self.profileIndex = blockIndex
457
470
458 self.dataOut.flagDataAsBlock = True
471 self.dataOut.flagDataAsBlock = True
459 self.dataOut.nProfiles = self.dataOut.data.shape[1]
472 self.dataOut.nProfiles = self.dataOut.data.shape[1]
460
473
474 #######################DP#######################
475 self.dataOut.CurrentBlock=self.nReadBlocks
476 self.dataOut.LastBlock=self.processingHeaderObj.dataBlocksPerFile
477 #######################DP#######################
461 self.dataOut.flagNoData = False
478 self.dataOut.flagNoData = False
462
479
463 self.getBasicHeader()
480 #self.getBasicHeader()
464
481
465 self.dataOut.realtime = self.online
482 self.dataOut.realtime = self.online
466
483
467 return self.dataOut.data
484 return self.dataOut.data
468
485
469
486
470 @MPDecorator
487 @MPDecorator
471 class VoltageWriter(JRODataWriter, Operation):
488 class VoltageWriter(JRODataWriter, Operation):
472 """
489 """
473 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
490 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
474 de los datos siempre se realiza por bloques.
491 de los datos siempre se realiza por bloques.
475 """
492 """
476
493
477 ext = ".r"
494 ext = ".r"
478
495
479 optchar = "D"
496 optchar = "D"
480
497
481 shapeBuffer = None
498 shapeBuffer = None
482
499
483 def __init__(self):#, **kwargs):
500 def __init__(self):#, **kwargs):
484 """
501 """
485 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
502 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
486
503
487 Affected:
504 Affected:
488 self.dataOut
505 self.dataOut
489
506
490 Return: None
507 Return: None
491 """
508 """
492 Operation.__init__(self)#, **kwargs)
509 Operation.__init__(self)#, **kwargs)
493
510
494 self.nTotalBlocks = 0
511 self.nTotalBlocks = 0
495
512
496 self.profileIndex = 0
513 self.profileIndex = 0
497
514
498 self.isConfig = False
515 self.isConfig = False
499
516
500 self.fp = None
517 self.fp = None
501
518
502 self.flagIsNewFile = 1
519 self.flagIsNewFile = 1
503
520
504 self.blockIndex = 0
521 self.blockIndex = 0
505
522
506 self.flagIsNewBlock = 0
523 self.flagIsNewBlock = 0
507
524
508 self.setFile = None
525 self.setFile = None
509
526
510 self.dtype = None
527 self.dtype = None
511
528
512 self.path = None
529 self.path = None
513
530
514 self.filename = None
531 self.filename = None
515
532
516 self.basicHeaderObj = BasicHeader(LOCALTIME)
533 self.basicHeaderObj = BasicHeader(LOCALTIME)
517
534
518 self.systemHeaderObj = SystemHeader()
535 self.systemHeaderObj = SystemHeader()
519
536
520 self.radarControllerHeaderObj = RadarControllerHeader()
537 self.radarControllerHeaderObj = RadarControllerHeader()
521
538
522 self.processingHeaderObj = ProcessingHeader()
539 self.processingHeaderObj = ProcessingHeader()
523
540
524 def hasAllDataInBuffer(self):
541 def hasAllDataInBuffer(self):
525 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
542 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
526 return 1
543 return 1
527 return 0
544 return 0
528
545
529 def setBlockDimension(self):
546 def setBlockDimension(self):
530 """
547 """
531 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
548 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
532
549
533 Affected:
550 Affected:
534 self.shape_spc_Buffer
551 self.shape_spc_Buffer
535 self.shape_cspc_Buffer
552 self.shape_cspc_Buffer
536 self.shape_dc_Buffer
553 self.shape_dc_Buffer
537
554
538 Return: None
555 Return: None
539 """
556 """
540 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
557 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
541 self.processingHeaderObj.nHeights,
558 self.processingHeaderObj.nHeights,
542 self.systemHeaderObj.nChannels)
559 self.systemHeaderObj.nChannels)
543
560
544 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
561 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
545 self.processingHeaderObj.profilesPerBlock,
562 self.processingHeaderObj.profilesPerBlock,
546 self.processingHeaderObj.nHeights),
563 self.processingHeaderObj.nHeights),
547 dtype=numpy.dtype('complex64'))
564 dtype=numpy.dtype('complex64'))
548
565
549 def writeBlock(self):
566 def writeBlock(self):
550 """
567 """
551 Escribe el buffer en el file designado
568 Escribe el buffer en el file designado
552
569
553 Affected:
570 Affected:
554 self.profileIndex
571 self.profileIndex
555 self.flagIsNewFile
572 self.flagIsNewFile
556 self.flagIsNewBlock
573 self.flagIsNewBlock
557 self.nTotalBlocks
574 self.nTotalBlocks
558 self.blockIndex
575 self.blockIndex
559
576
560 Return: None
577 Return: None
561 """
578 """
562 data = numpy.zeros(self.shapeBuffer, self.dtype)
579 data = numpy.zeros(self.shapeBuffer, self.dtype)
563
580
564 junk = numpy.transpose(self.datablock, (1, 2, 0))
581 junk = numpy.transpose(self.datablock, (1, 2, 0))
565
582
566 data['real'] = junk.real
583 data['real'] = junk.real
567 data['imag'] = junk.imag
584 data['imag'] = junk.imag
568
585
569 data = data.reshape((-1))
586 data = data.reshape((-1))
570
587
571 data.tofile(self.fp)
588 data.tofile(self.fp)
572
589
573 self.datablock.fill(0)
590 self.datablock.fill(0)
574
591
575 self.profileIndex = 0
592 self.profileIndex = 0
576 self.flagIsNewFile = 0
593 self.flagIsNewFile = 0
577 self.flagIsNewBlock = 1
594 self.flagIsNewBlock = 1
578
595
579 self.blockIndex += 1
596 self.blockIndex += 1
580 self.nTotalBlocks += 1
597 self.nTotalBlocks += 1
581
598
582 # print "[Writing] Block = %04d" %self.blockIndex
599 # print "[Writing] Block = %04d" %self.blockIndex
583
600
584 def putData(self):
601 def putData(self):
585 """
602 """
586 Setea un bloque de datos y luego los escribe en un file
603 Setea un bloque de datos y luego los escribe en un file
587
604
588 Affected:
605 Affected:
589 self.flagIsNewBlock
606 self.flagIsNewBlock
590 self.profileIndex
607 self.profileIndex
591
608
592 Return:
609 Return:
593 0 : Si no hay data o no hay mas files que puedan escribirse
610 0 : Si no hay data o no hay mas files que puedan escribirse
594 1 : Si se escribio la data de un bloque en un file
611 1 : Si se escribio la data de un bloque en un file
595 """
612 """
596 if self.dataOut.flagNoData:
613 if self.dataOut.flagNoData:
597 return 0
614 return 0
598
615
599 self.flagIsNewBlock = 0
616 self.flagIsNewBlock = 0
600
617
601 if self.dataOut.flagDiscontinuousBlock:
618 if self.dataOut.flagDiscontinuousBlock:
602 self.datablock.fill(0)
619 self.datablock.fill(0)
603 self.profileIndex = 0
620 self.profileIndex = 0
604 self.setNextFile()
621 self.setNextFile()
605
622
606 if self.profileIndex == 0:
623 if self.profileIndex == 0:
607 self.setBasicHeader()
624 self.setBasicHeader()
608
625
609 self.datablock[:, self.profileIndex, :] = self.dataOut.data
626 self.datablock[:, self.profileIndex, :] = self.dataOut.data
610
627
611 self.profileIndex += 1
628 self.profileIndex += 1
612
629
613 if self.hasAllDataInBuffer():
630 if self.hasAllDataInBuffer():
614 # if self.flagIsNewFile:
631 # if self.flagIsNewFile:
615 self.writeNextBlock()
632 self.writeNextBlock()
616 # self.setFirstHeader()
633 # self.setFirstHeader()
617
634
618 return 1
635 return 1
619
636
620 def __getBlockSize(self):
637 def __getBlockSize(self):
621 '''
638 '''
622 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
639 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
623 '''
640 '''
624
641
625 dtype_width = self.getDtypeWidth()
642 dtype_width = self.getDtypeWidth()
626
643
627 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels *
644 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels *
628 self.profilesPerBlock * dtype_width * 2)
645 self.profilesPerBlock * dtype_width * 2)
629
646
630 return blocksize
647 return blocksize
631
648
632 def setFirstHeader(self):
649 def setFirstHeader(self):
633 """
650 """
634 Obtiene una copia del First Header
651 Obtiene una copia del First Header
635
652
636 Affected:
653 Affected:
637 self.systemHeaderObj
654 self.systemHeaderObj
638 self.radarControllerHeaderObj
655 self.radarControllerHeaderObj
639 self.dtype
656 self.dtype
640
657
641 Return:
658 Return:
642 None
659 None
643 """
660 """
644
661
645 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
662 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
646 self.systemHeaderObj.nChannels = self.dataOut.nChannels
663 self.systemHeaderObj.nChannels = self.dataOut.nChannels
647 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
664 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
648
665
649 self.processingHeaderObj.dtype = 0 # Voltage
666 self.processingHeaderObj.dtype = 0 # Voltage
650 self.processingHeaderObj.blockSize = self.__getBlockSize()
667 self.processingHeaderObj.blockSize = self.__getBlockSize()
651 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
668 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
652 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
669 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
653 # podria ser 1 o self.dataOut.processingHeaderObj.nWindows
670 # podria ser 1 o self.dataOut.processingHeaderObj.nWindows
654 self.processingHeaderObj.nWindows = 1
671 self.processingHeaderObj.nWindows = 1
655 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
672 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
656 # Cuando la data de origen es de tipo Voltage
673 # Cuando la data de origen es de tipo Voltage
657 self.processingHeaderObj.nIncohInt = 1
674 self.processingHeaderObj.nIncohInt = 1
658 # Cuando la data de origen es de tipo Voltage
675 # Cuando la data de origen es de tipo Voltage
659 self.processingHeaderObj.totalSpectra = 0
676 self.processingHeaderObj.totalSpectra = 0
660
677
661 if self.dataOut.code is not None:
678 if self.dataOut.code is not None:
662 self.processingHeaderObj.code = self.dataOut.code
679 self.processingHeaderObj.code = self.dataOut.code
663 self.processingHeaderObj.nCode = self.dataOut.nCode
680 self.processingHeaderObj.nCode = self.dataOut.nCode
664 self.processingHeaderObj.nBaud = self.dataOut.nBaud
681 self.processingHeaderObj.nBaud = self.dataOut.nBaud
665
682
666 if self.processingHeaderObj.nWindows != 0:
683 if self.processingHeaderObj.nWindows != 0:
667 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
684 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
668 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - \
685 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - \
669 self.dataOut.heightList[0]
686 self.dataOut.heightList[0]
670 self.processingHeaderObj.nHeights = self.dataOut.nHeights
687 self.processingHeaderObj.nHeights = self.dataOut.nHeights
671 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
688 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
672
689
673 self.processingHeaderObj.processFlags = self.getProcessFlags()
690 self.processingHeaderObj.processFlags = self.getProcessFlags()
674
691
675 self.setBasicHeader()
692 self.setBasicHeader()
676 No newline at end of file
@@ -1,16 +1,22
1 '''
1 '''
2
2
3 $Author: murco $
3 $Author: murco $
4 $Id: Processor.py 1 2012-11-12 18:56:07Z murco $
4 $Id: Processor.py 1 2012-11-12 18:56:07Z murco $
5 '''
5 '''
6
6
7 from .jroproc_voltage import *
7 from .jroproc_voltage import *
8 from .jroproc_spectra import *
8 from .jroproc_spectra import *
9 from .jroproc_heispectra import *
9 from .jroproc_heispectra import *
10 from .jroproc_amisr import *
10 from .jroproc_amisr import *
11 from .jroproc_correlation import *
11 from .jroproc_correlation import *
12 from .jroproc_parameters import *
12 from .jroproc_parameters import *
13 from .jroproc_spectra_lags import *
13 from .jroproc_spectra_lags import *
14 from .jroproc_spectra_acf import *
14 from .jroproc_spectra_acf import *
15 from .bltrproc_parameters import *
15 from .bltrproc_parameters import *
16 from .pxproc_parameters import *
16 from .pxproc_parameters import *
17
18
19 ###########DP###########
20 from .jroproc_voltage_lags import *
21 ###########DP###########
22 from .jroproc_spectra_lags_faraday import *
@@ -1,206 +1,206
1 '''
1 '''
2 Base clases to create Processing units and operations, the MPDecorator
2 Base clases to create Processing units and operations, the MPDecorator
3 must be used in plotting and writing operations to allow to run as an
3 must be used in plotting and writing operations to allow to run as an
4 external process.
4 external process.
5 '''
5 '''
6
6
7 import os
7 import os
8 import inspect
8 import inspect
9 import zmq
9 import zmq
10 import time
10 import time
11 import pickle
11 import pickle
12 import traceback
12 import traceback
13 from threading import Thread
13 from threading import Thread
14 from multiprocessing import Process, Queue
14 from multiprocessing import Process, Queue
15 from schainpy.utils import log
15 from schainpy.utils import log
16
16
17 QUEUE_SIZE = int(os.environ.get('QUEUE_MAX_SIZE', '10'))
17 QUEUE_SIZE = int(os.environ.get('QUEUE_MAX_SIZE', '10'))
18
18
19 class ProcessingUnit(object):
19 class ProcessingUnit(object):
20 '''
20 '''
21 Base class to create Signal Chain Units
21 Base class to create Signal Chain Units
22 '''
22 '''
23
23
24 proc_type = 'processing'
24 proc_type = 'processing'
25
25
26 def __init__(self):
26 def __init__(self):
27
27
28 self.dataIn = None
28 self.dataIn = None
29 self.dataOut = None
29 self.dataOut = None
30 self.isConfig = False
30 self.isConfig = False
31 self.operations = []
31 self.operations = []
32
32
33 def setInput(self, unit):
33 def setInput(self, unit):
34
34
35 self.dataIn = unit.dataOut
35 self.dataIn = unit.dataOut
36
36
37 def getAllowedArgs(self):
37 def getAllowedArgs(self):
38 if hasattr(self, '__attrs__'):
38 if hasattr(self, '__attrs__'):
39 return self.__attrs__
39 return self.__attrs__
40 else:
40 else:
41 return inspect.getargspec(self.run).args
41 return inspect.getargspec(self.run).args
42
42
43 def addOperation(self, conf, operation):
43 def addOperation(self, conf, operation):
44 '''
44 '''
45 '''
45 '''
46
46
47 self.operations.append((operation, conf.type, conf.getKwargs()))
47 self.operations.append((operation, conf.type, conf.getKwargs()))
48
48
49 def getOperationObj(self, objId):
49 def getOperationObj(self, objId):
50
50
51 if objId not in list(self.operations.keys()):
51 if objId not in list(self.operations.keys()):
52 return None
52 return None
53
53
54 return self.operations[objId]
54 return self.operations[objId]
55
55
56 def call(self, **kwargs):
56 def call(self, **kwargs):
57 '''
57 '''
58 '''
58 '''
59
59
60 try:
60 try:
61 if self.dataIn is not None and self.dataIn.flagNoData and not self.dataIn.error:
61 if self.dataIn is not None and self.dataIn.flagNoData and not self.dataIn.error:
62 return self.dataIn.isReady()
62 return self.dataIn.isReady()
63 elif self.dataIn is None or not self.dataIn.error:
63 elif self.dataIn is None or not self.dataIn.error:
64 self.run(**kwargs)
64 self.run(**kwargs)
65 elif self.dataIn.error:
65 elif self.dataIn.error:
66 self.dataOut.error = self.dataIn.error
66 self.dataOut.error = self.dataIn.error
67 self.dataOut.flagNoData = True
67 self.dataOut.flagNoData = True
68 except:
68 except:
69 err = traceback.format_exc()
69 err = traceback.format_exc()
70 if 'SchainWarning' in err:
70 if 'SchainWarning' in err:
71 log.warning(err.split('SchainWarning:')[-1].split('\n')[0].strip(), self.name)
71 log.warning(err.split('SchainWarning:')[-1].split('\n')[0].strip(), self.name)
72 elif 'SchainError' in err:
72 elif 'SchainError' in err:
73 log.error(err.split('SchainError:')[-1].split('\n')[0].strip(), self.name)
73 log.error(err.split('SchainError:')[-1].split('\n')[0].strip(), self.name)
74 else:
74 else:
75 log.error(err, self.name)
75 log.error(err, self.name)
76 self.dataOut.error = True
76 self.dataOut.error = True
77
77
78 for op, optype, opkwargs in self.operations:
78 for op, optype, opkwargs in self.operations:
79 aux = self.dataOut.copy()
79 aux = self.dataOut.copy()
80 if optype == 'other' and not self.dataOut.flagNoData:
80 if optype == 'other' and not self.dataOut.flagNoData:
81 self.dataOut = op.run(self.dataOut, **opkwargs)
81 self.dataOut = op.run(self.dataOut, **opkwargs)
82 elif optype == 'external' and not self.dataOut.flagNoData:
82 elif optype == 'external' and not self.dataOut.flagNoData:
83 op.queue.put(aux)
83 op.queue.put(aux)
84 elif optype == 'external' and self.dataOut.error:
84 elif optype == 'external' and self.dataOut.error:
85 op.queue.put(aux)
85 op.queue.put(aux)
86
86
87 return 'Error' if self.dataOut.error else self.dataOut.isReady()
87 return 'Error' if self.dataOut.error else self.dataOut.isReady()
88
88
89 def setup(self):
89 def setup(self):
90
90
91 raise NotImplementedError
91 raise NotImplementedError
92
92
93 def run(self):
93 def run(self):
94
94
95 raise NotImplementedError
95 raise NotImplementedError
96
96
97 def close(self):
97 def close(self):
98
98
99 return
99 return
100
100
101
101
102 class Operation(object):
102 class Operation(object):
103
103
104 '''
104 '''
105 '''
105 '''
106
106
107 proc_type = 'operation'
107 proc_type = 'operation'
108
108
109 def __init__(self):
109 def __init__(self):
110
110
111 self.id = None
111 self.id = None
112 self.isConfig = False
112 self.isConfig = False
113
113
114 if not hasattr(self, 'name'):
114 if not hasattr(self, 'name'):
115 self.name = self.__class__.__name__
115 self.name = self.__class__.__name__
116
116
117 def getAllowedArgs(self):
117 def getAllowedArgs(self):
118 if hasattr(self, '__attrs__'):
118 if hasattr(self, '__attrs__'):
119 return self.__attrs__
119 return self.__attrs__
120 else:
120 else:
121 return inspect.getargspec(self.run).args
121 return inspect.getargspec(self.run).args
122
122
123 def setup(self):
123 def setup(self):
124
124
125 self.isConfig = True
125 self.isConfig = True
126
126
127 raise NotImplementedError
127 raise NotImplementedError
128
128
129 def run(self, dataIn, **kwargs):
129 def run(self, dataIn, **kwargs):
130 """
130 """
131 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los
131 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los
132 atributos del objeto dataIn.
132 atributos del objeto dataIn.
133
133
134 Input:
134 Input:
135
135
136 dataIn : objeto del tipo JROData
136 dataIn : objeto del tipo JROData
137
137
138 Return:
138 Return:
139
139
140 None
140 None
141
141
142 Affected:
142 Affected:
143 __buffer : buffer de recepcion de datos.
143 __buffer : buffer de recepcion de datos.
144
144
145 """
145 """
146 if not self.isConfig:
146 if not self.isConfig:
147 self.setup(**kwargs)
147 self.setup(**kwargs)
148
148
149 raise NotImplementedError
149 raise NotImplementedError
150
150
151 def close(self):
151 def close(self):
152
152
153 return
153 return
154
154
155
155
156 def MPDecorator(BaseClass):
156 def MPDecorator(BaseClass):
157 """
157 """
158 Multiprocessing class decorator
158 Multiprocessing class decorator
159
159
160 This function add multiprocessing features to a BaseClass.
160 This function add multiprocessing features to a BaseClass.
161 """
161 """
162
162
163 class MPClass(BaseClass, Process):
163 class MPClass(BaseClass, Process):
164
164
165 def __init__(self, *args, **kwargs):
165 def __init__(self, *args, **kwargs):
166 super(MPClass, self).__init__()
166 super(MPClass, self).__init__()
167 Process.__init__(self)
167 Process.__init__(self)
168
168
169 self.args = args
169 self.args = args
170 self.kwargs = kwargs
170 self.kwargs = kwargs
171 self.t = time.time()
171 self.t = time.time()
172 self.op_type = 'external'
172 self.op_type = 'external'
173 self.name = BaseClass.__name__
173 self.name = BaseClass.__name__
174 self.__doc__ = BaseClass.__doc__
174 self.__doc__ = BaseClass.__doc__
175
175
176 if 'plot' in self.name.lower() and not self.name.endswith('_'):
176 if 'plot' in self.name.lower() and not self.name.endswith('_'):
177 self.name = '{}{}'.format(self.CODE.upper(), 'Plot')
177 self.name = '{}{}'.format(self.CODE.upper(), 'Plot')
178
178
179 self.start_time = time.time()
179 self.start_time = time.time()
180 self.err_queue = args[3]
180 self.err_queue = args[3]
181 self.queue = Queue(maxsize=QUEUE_SIZE)
181 self.queue = Queue(maxsize=QUEUE_SIZE)
182 self.myrun = BaseClass.run
182 self.myrun = BaseClass.run
183
183
184 def run(self):
184 def run(self):
185
185
186 while True:
186 while True:
187
187
188 dataOut = self.queue.get()
188 dataOut = self.queue.get()
189
189
190 if not dataOut.error:
190 if not dataOut.error:
191 try:
191 try:
192 BaseClass.run(self, dataOut, **self.kwargs)
192 BaseClass.run(self, dataOut, **self.kwargs)
193 except:
193 except:
194 err = traceback.format_exc()
194 err = traceback.format_exc()
195 log.error(err, self.name)
195 log.error(err, self.name)
196 else:
196 else:
197 break
197 break
198
198
199 self.close()
199 self.close()
200
200
201 def close(self):
201 def close(self):
202
202
203 BaseClass.close(self)
203 BaseClass.close(self)
204 log.success('Done...(Time:{:4.2f} secs)'.format(time.time()-self.start_time), self.name)
204 log.success('Done...(Time:{:4.2f} secs)'.format(time.time()-self.start_time), self.name)
205
205
206 return MPClass
206 return MPClass
1 NO CONTENT: modified file chmod 100755 => 100644
NO CONTENT: modified file chmod 100755 => 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now