##// END OF EJS Templates
Merge branch 'isr' into v3.0-devel
jespinoza -
r1380:a03d3bbf5427 merge
parent child
Show More
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')
This diff has been collapsed as it changes many lines, (2561 lines changed) Show them Hide them
@@ -0,0 +1,2561
1
2 import matplotlib.pyplot as plt
3
4
5
6 import numpy
7 import time
8 import math
9
10 from datetime import datetime
11
12 from schainpy.utils import log
13
14 import struct
15 import os
16
17 import sys
18
19 from ctypes import *
20
21 from schainpy.model.io.jroIO_voltage import VoltageReader,JRODataReader
22 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
23 from schainpy.model.data.jrodata import Voltage
24
25
26
27
28 @MPDecorator
29 class VoltageLagsProc(ProcessingUnit):
30
31 def __init__(self):
32
33 ProcessingUnit.__init__(self)
34
35 self.dataOut = Voltage()
36 self.bcounter=0
37 self.dataOut.kax=None
38 self.dataOut.kay=None
39 self.dataOut.kbx=None
40 self.dataOut.kby=None
41 self.dataOut.kax2=None
42 self.dataOut.kay2=None
43 self.dataOut.kbx2=None
44 self.dataOut.kby2=None
45 self.dataOut.kaxbx=None
46 self.dataOut.kaxby=None
47 self.dataOut.kaybx=None
48 self.dataOut.kayby=None
49 self.dataOut.kaxay=None
50 self.dataOut.kbxby=None
51 self.aux=1
52
53 self.LP_products_aux=0
54 self.lag_products_LP_median_estimates_aux=0
55
56 #self.dataOut.input_dat_type=0 #06/04/2020
57
58 def get_products_cabxys(self):
59
60
61 if self.aux==1:
62
63
64
65 self.dataOut.read_samples=int(self.dataOut.systemHeaderObj.nSamples/self.dataOut.OSAMP)
66 if self.dataOut.experiment=="DP":
67 self.dataOut.nptsfft1=132 #30/03/2020
68 self.dataOut.nptsfft2=140 #30/03/2020
69 if self.dataOut.experiment=="HP":
70 self.dataOut.nptsfft1=128 #30/03/2020
71 self.dataOut.nptsfft2=150 #30/03/2020
72
73
74 #self.dataOut.noise_final_list=[] #30/03/2020
75
76 padding=numpy.zeros(1,'int32')
77
78 hsize=numpy.zeros(1,'int32')
79 bufsize=numpy.zeros(1,'int32')
80 nr=numpy.zeros(1,'int32')
81 ngates=numpy.zeros(1,'int32') ### ### ### 2
82 time1=numpy.zeros(1,'uint64') # pos 3
83 time2=numpy.zeros(1,'uint64') # pos 4
84 lcounter=numpy.zeros(1,'int32')
85 groups=numpy.zeros(1,'int32')
86 system=numpy.zeros(4,'int8') # pos 7
87 h0=numpy.zeros(1,'float32')
88 dh=numpy.zeros(1,'float32')
89 ipp=numpy.zeros(1,'float32')
90 process=numpy.zeros(1,'int32')
91 tx=numpy.zeros(1,'int32')
92
93 ngates1=numpy.zeros(1,'int32') ### ### ### 13
94 time0=numpy.zeros(1,'uint64') # pos 14
95 nlags=numpy.zeros(1,'int32')
96 nlags1=numpy.zeros(1,'int32')
97 txb=numpy.zeros(1,'float32') ### ### ### 17
98 time3=numpy.zeros(1,'uint64') # pos 18
99 time4=numpy.zeros(1,'uint64') # pos 19
100 h0_=numpy.zeros(1,'float32')
101 dh_=numpy.zeros(1,'float32')
102 ipp_=numpy.zeros(1,'float32')
103 txa_=numpy.zeros(1,'float32')
104
105 pad=numpy.zeros(100,'int32')
106
107 nbytes=numpy.zeros(1,'int32')
108 limits=numpy.zeros(1,'int32')
109 ngroups=numpy.zeros(1,'int32') ### ### ### 27
110
111
112 self.dataOut.header=[hsize,bufsize,nr,ngates,time1,time2,
113 lcounter,groups,system,h0,dh,ipp,
114 process,tx,ngates1,padding,time0,nlags,
115 nlags1,padding,txb,time3,time4,h0_,dh_,
116 ipp_,txa_,pad,nbytes,limits,padding,ngroups]
117
118 if self.dataOut.experiment == "DP":
119 self.dataOut.header[1][0]=81864
120 if self.dataOut.experiment == "HP":
121 self.dataOut.header[1][0]=173216
122
123 self.dataOut.header[3][0]=max(self.dataOut.NRANGE,self.dataOut.NDT)
124 self.dataOut.header[7][0]=self.dataOut.NAVG
125 self.dataOut.header[9][0]=int(self.dataOut.heightList[0])
126 self.dataOut.header[10][0]=self.dataOut.DH
127 self.dataOut.header[17][0]=self.dataOut.DPL
128 self.dataOut.header[18][0]=self.dataOut.NLAG
129 #self.header[5][0]=0
130 self.dataOut.header[15][0]=self.dataOut.NDP
131 self.dataOut.header[2][0]=self.dataOut.NR
132 #time.mktime(time.strptime()
133
134
135
136
137 self.aux=0
138
139
140
141
142
143
144
145
146
147 if self.dataOut.experiment=="DP":
148
149
150 self.dataOut.lags_array=[x / self.dataOut.DH for x in self.dataOut.flags_array]
151 self.cax=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
152 self.cay=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
153 self.cbx=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
154 self.cby=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
155 self.cax2=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
156 self.cay2=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
157 self.cbx2=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
158 self.cby2=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
159 self.caxbx=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
160 self.caxby=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
161 self.caybx=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
162 self.cayby=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
163 self.caxay=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
164 self.cbxby=numpy.zeros((self.dataOut.NDP,self.dataOut.nlags_array,2))
165
166 for i in range(2):
167 for j in range(self.dataOut.NDP):
168 for k in range(int(self.dataOut.NSCAN/2)):
169 n=k%self.dataOut.nlags_array
170 ax=self.dataOut.data[0,2*k+i,j].real
171 ay=self.dataOut.data[0,2*k+i,j].imag
172 if j+self.dataOut.lags_array[n]<self.dataOut.NDP:
173 bx=self.dataOut.data[1,2*k+i,j+int(self.dataOut.lags_array[n])].real
174 by=self.dataOut.data[1,2*k+i,j+int(self.dataOut.lags_array[n])].imag
175 else:
176 if k+1<int(self.dataOut.NSCAN/2):
177 bx=self.dataOut.data[1,2*(k+1)+i,(self.dataOut.NRANGE+self.dataOut.NCAL+j+int(self.dataOut.lags_array[n]))%self.dataOut.NDP].real
178 by=self.dataOut.data[1,2*(k+1)+i,(self.dataOut.NRANGE+self.dataOut.NCAL+j+int(self.dataOut.lags_array[n]))%self.dataOut.NDP].imag
179
180 if k+1==int(self.dataOut.NSCAN/2):
181 bx=self.dataOut.data[1,2*k+i,(self.dataOut.NRANGE+self.dataOut.NCAL+j+int(self.dataOut.lags_array[n]))%self.dataOut.NDP].real
182 by=self.dataOut.data[1,2*k+i,(self.dataOut.NRANGE+self.dataOut.NCAL+j+int(self.dataOut.lags_array[n]))%self.dataOut.NDP].imag
183
184 if(k<self.dataOut.nlags_array):
185 self.cax[j][n][i]=ax
186 self.cay[j][n][i]=ay
187 self.cbx[j][n][i]=bx
188 self.cby[j][n][i]=by
189 self.cax2[j][n][i]=ax*ax
190 self.cay2[j][n][i]=ay*ay
191 self.cbx2[j][n][i]=bx*bx
192 self.cby2[j][n][i]=by*by
193 self.caxbx[j][n][i]=ax*bx
194 self.caxby[j][n][i]=ax*by
195 self.caybx[j][n][i]=ay*bx
196 self.cayby[j][n][i]=ay*by
197 self.caxay[j][n][i]=ax*ay
198 self.cbxby[j][n][i]=bx*by
199 else:
200 self.cax[j][n][i]+=ax
201 self.cay[j][n][i]+=ay
202 self.cbx[j][n][i]+=bx
203 self.cby[j][n][i]+=by
204 self.cax2[j][n][i]+=ax*ax
205 self.cay2[j][n][i]+=ay*ay
206 self.cbx2[j][n][i]+=bx*bx
207 self.cby2[j][n][i]+=by*by
208 self.caxbx[j][n][i]+=ax*bx
209 self.caxby[j][n][i]+=ax*by
210 self.caybx[j][n][i]+=ay*bx
211 self.cayby[j][n][i]+=ay*by
212 self.caxay[j][n][i]+=ax*ay
213 self.cbxby[j][n][i]+=bx*by
214
215
216
217 #return self.cax,self.cay,self.cbx,self.cby,self.cax2,self.cay2,self.cbx2,self.cby2,self.caxbx,self.caxby,self.caybx,self.cayby,self.caxay,self.cbxby
218
219 if self.dataOut.experiment=="HP":
220
221 #lagind=[0,1,2,3,4,5,6,7,0,3,4,5,6,8,9,10]
222 #lagfirst=[1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1]
223
224 self.cax=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))# hp:67x11x2 dp: 66x11x2
225 self.cay=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
226 self.cbx=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
227 self.cby=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
228 self.cax2=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
229 self.cay2=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
230 self.cbx2=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
231 self.cby2=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
232 self.caxbx=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
233 self.caxby=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
234 self.caybx=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
235 self.cayby=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
236 self.caxay=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
237 self.cbxby=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2))
238 for i in range(2): # flipped and unflipped
239 for j in range(self.dataOut.NDP): # loop over true ranges # 67
240 for k in range(int(self.dataOut.NSCAN)): # 128
241 #print("flip ",i," NDP ",j, " NSCAN ",k)
242 #print("cdata ",cdata[i:NSCAN:2][k][:,0])
243 n=self.dataOut.lagind[k%self.dataOut.nlags_array] # 128=16x8
244 #print("n ",n)
245 #ind1=nrx*(j+ngates_2*i+ngates_2*2*k)# scan has flip or unflip
246 #ind2=ind1+(1)+nrx*lags_array[n]#jump each lagged
247 #ax=cdata[i:NSCAN:2][k][:,0][NRANGE+NCAL+j].real #cdata[ind1].r
248 #ay=cdata[i:NSCAN:2][k][:,0][NRANGE+NCAL+j].imag #cdata[ind1].i
249 #input()
250 ##ax=cdata[int(i*NSCAN):int((i+1)*NSCAN)][k][:,0][NRANGE+NCAL+j].real #cdata[ind1].r
251 ##ay=cdata[int(i*NSCAN):int((i+1)*NSCAN)][k][:,0][NRANGE+NCAL+j].imag #cdata[ind1].i
252
253 ax=self.dataOut.data[0,k,self.dataOut.NRANGE+self.dataOut.NCAL+j+i*self.dataOut.NDT].real
254 ay=self.dataOut.data[0,k,self.dataOut.NRANGE+self.dataOut.NCAL+j+i*self.dataOut.NDT].imag
255
256 #print("ax ",ax," ay",ay)
257 if self.dataOut.NRANGE+self.dataOut.NCAL+j+i*self.dataOut.NDT+2*n<self.dataOut.read_samples:
258 #bx=cdata[i:NSCAN:2][k][:,1][NRANGE+NCAL+j+n].real #cdata[ind2].r
259 #by=cdata[i:NSCAN:2][k][:,1][NRANGE+NCAL+j+n].imag #cdata[ind2].i
260 ##bx=cdata[int(i*NSCAN):int((i+1)*NSCAN)][k][:,1][NRANGE+NCAL+j+2*n].real #cdata[ind2].r
261 ##by=cdata[int(i*NSCAN):int((i+1)*NSCAN)][k][:,1][NRANGE+NCAL+j+2*n].imag #cdata[ind2].i
262
263 bx=self.dataOut.data[1,k,self.dataOut.NRANGE+self.dataOut.NCAL+j+i*self.dataOut.NDT+2*n].real
264 by=self.dataOut.data[1,k,self.dataOut.NRANGE+self.dataOut.NCAL+j+i*self.dataOut.NDT+2*n].imag
265
266 #bx=self.dataOut.data[0:NSCAN][k][:,1][NRANGE+NCAL+j+i*NDT+2*n].real #cdata[ind2].r
267 #by=self.dataOut.data[0:NSCAN][k][:,1][NRANGE+NCAL+j+i*NDT+2*n].imag #cdata[ind2].i
268 #print("bx ",bx, " by ",by)
269 #input()
270 else:
271 #print("n ",n," k ",k," j ",j," i ",i, " n ",n)
272 #input()
273 if k+1<int(self.dataOut.NSCAN):
274 #print("k+1 ",k+1)
275 #print("int(NSCAN/2) ",int(NSCAN/2))
276 #bx=cdata[i:NSCAN:2][k+1][:,1][(NRANGE+NCAL+j+n)%NDP].real#np.nan
277 #by=cdata[i:NSCAN:2][k+1][:,1][(NRANGE+NCAL+j+n)%NDP].imag#np.nan
278 ##bx=cdata[int(i*NSCAN):int((i+1)*NSCAN)][k+1][:,1][(NRANGE+NCAL+j+2*n)%NDP].real#np.nan
279 ##by=cdata[int(i*NSCAN):int((i+1)*NSCAN)][k+1][:,1][(NRANGE+NCAL+j+2*n)%NDP].imag#np.nan
280 #bx=self.dataOut.data[0:NSCAN][k+1][:,1][(NRANGE+NCAL+j+i*NDT+2*n)%NDP].real#np.nan
281 #by=self.dataOut.data[0:NSCAN][k+1][:,1][(NRANGE+NCAL+j+i*NDT+2*n)%NDP].imag#np.nan
282 bx=self.dataOut.data[1,k+1,(self.dataOut.NRANGE+self.dataOut.NCAL+j+i*self.dataOut.NDT+2*n)%self.dataOut.NDP].real
283 by=self.dataOut.data[1,k+1,(self.dataOut.NRANGE+self.dataOut.NCAL+j+i*self.dataOut.NDT+2*n)%self.dataOut.NDP].imag
284
285 #print("n ",n," k ",k," j ",j," i ",i, " lags_array[n] ",lags_array[n])
286 #print("bx ",bx, " by ",by)
287 #input()
288 if k+1==int(self.dataOut.NSCAN):## ESTO ES UN PARCHE PUES NO SE TIENE EL SIGUIENTE BLOQUE
289 #bx=cdata[i:NSCAN:2][k][:,1][(NRANGE+NCAL+j+n)%NDP].real#np.nan
290 #by=cdata[i:NSCAN:2][k][:,1][(NRANGE+NCAL+j+n)%NDP].imag#np.nan
291 ##bx=cdata[int(i*NSCAN):int((i+1)*NSCAN)][k][:,1][(NRANGE+NCAL+j+2*n)%NDP].real#np.nan
292 ##by=cdata[int(i*NSCAN):int((i+1)*NSCAN)][k][:,1][(NRANGE+NCAL+j+2*n)%NDP].imag#np.nan
293 #print("****n ",n," k ",k," j ",j," i ",i, " lags_array[n] ",lags_array[n])
294 #bx=self.dataOut.data[0:NSCAN][k][:,1][(NRANGE+NCAL+j+i*NDT+2*n)%NDP].real#np.nan
295 #by=self.dataOut.data[0:NSCAN][k][:,1][(NRANGE+NCAL+j+i*NDT+2*n)%NDP].imag#np.nan
296 bx=self.dataOut.data[1,k,(self.dataOut.NRANGE+self.dataOut.NCAL+j+i*self.dataOut.NDT+2*n)%self.dataOut.NDP].real
297 by=self.dataOut.data[1,k,(self.dataOut.NRANGE+self.dataOut.NCAL+j+i*self.dataOut.NDT+2*n)%self.dataOut.NDP].imag
298
299 #print("bx ",bx, " by ",by)
300 #input()
301
302 #print("i ",i," j ",j," k ",k," n ",n," ax ",ax)
303 #input()
304 #ip1=j+NDP*(i+2*n)
305 #ip2=ip1*navg+iavg
306 ##if(k<11): # PREVIOUS
307 if(k<self.dataOut.nlags_array and self.dataOut.lagfirst[k%self.dataOut.nlags_array]==1):# if(k<16 && lagfirst[k%16]==1)
308 self.cax[j][n][i]=ax#[int(k/nlags_array)*nlags_array+n]
309 self.cay[j][n][i]=ay#[int(k/nlags_array)*nlags_array+n]
310 self.cbx[j][n][i]=bx#[int(k/nlags_array)*nlags_array+n]
311 self.cby[j][n][i]=by#[int(k/nlags_array)*nlags_array+n]
312 self.cax2[j][n][i]=ax*ax#np.multiply(ax,ax)[int(k/nlags_array)*nlags_array+n]
313 self.cay2[j][n][i]=ay*ay#np.multiply(ay,ay)[int(k/nlags_array)*nlags_array+n]
314 self.cbx2[j][n][i]=bx*bx#np.multiply(bx,bx)[int(k/nlags_array)*nlags_array+n]
315 self.cby2[j][n][i]=by*by#np.multiply(by,by)[int(k/nlags_array)*nlags_array+n]
316 self.caxbx[j][n][i]=ax*bx#np.multiply(ax,bx)[int(k/nlags_array)*nlags_array+n]
317 self.caxby[j][n][i]=ax*by#np.multiply(ax,by)[int(k/nlags_array)*nlags_array+n]
318 self.caybx[j][n][i]=ay*bx#np.multiply(ay,bx)[int(k/nlags_array)*nlags_array+n]
319 self.cayby[j][n][i]=ay*by#np.multiply(ay,by)[int(k/nlags_array)*nlags_array+n]
320 self.caxay[j][n][i]=ax*ay#np.multiply(ax,ay)[int(k/nlags_array)*nlags_array+n]
321 self.cbxby[j][n][i]=bx*by#np.multiply(bx,by)[int(k/nlags_array)*nlags_array+n]
322 else:
323 self.cax[j][n][i]+=ax#[int(k/nlags_array)*nlags_array+n]
324 self.cay[j][n][i]+=ay#[int(k/nlags_array)*nlags_array+n]
325 self.cbx[j][n][i]+=bx#[int(k/nlags_array)*nlags_array+n]
326 self.cby[j][n][i]+=by#[int(k/nlags_array)*nlags_array+n]
327 self.cax2[j][n][i]+=ax*ax#np.multiply(ax,ax)[int(k/nlags_array)*nlags_array+n]
328 self.cay2[j][n][i]+=ay*ay#np.multiply(ay,ay)[int(k/nlags_array)*nlags_array+n]
329 self.cbx2[j][n][i]+=bx*bx#np.multiply(bx,bx)[int(k/nlags_array)*nlags_array+n]
330 self.cby2[j][n][i]+=by*by#np.multiply(by,by)[int(k/nlags_array)*nlags_array+n]
331 self.caxbx[j][n][i]+=ax*bx#np.multiply(ax,bx)[int(k/nlags_array)*nlags_array+n]
332 self.caxby[j][n][i]+=ax*by#np.multiply(ax,by)[int(k/nlags_array)*nlags_array+n]
333 self.caybx[j][n][i]+=ay*bx#np.multiply(ay,bx)[int(k/nlags_array)*nlags_array+n]
334 self.cayby[j][n][i]+=ay*by#np.multiply(ay,by)[int(k/nlags_array)*nlags_array+n]
335 self.caxay[j][n][i]+=ax*ay#np.multiply(ax,ay)[int(k/nlags_array)*nlags_array+n]
336 self.cbxby[j][n][i]+=bx*by#np.multiply(bx,by)[int(k/nlags_array)*nlags_array+n]
337
338
339
340
341
342
343
344
345
346 def medi(self,data_navg):
347 sorts=sorted(data_navg)
348 rsorts=numpy.arange(self.dataOut.NAVG)
349 result=0.0
350 for k in range(self.dataOut.NAVG):
351 if k>=self.dataOut.nkill/2 and k<self.dataOut.NAVG-self.dataOut.nkill/2:
352 result+=sorts[k]*float(self.dataOut.NAVG)/(float)(self.dataOut.NAVG-self.dataOut.nkill)
353 return result
354
355
356
357 '''
358 def range(self):
359 Range=numpy.arange(0,990,self.DH)
360 return Range
361 '''
362
363
364
365
366
367
368 def cabxys_navg(self):
369
370 #print("blocknow",self.dataOut.CurrentBlock)
371 #bcounter=0
372 #print("self.bcounter",self.bcounter)
373 self.get_products_cabxys()
374
375 self.dataOut.header[5][0]=time.mktime(time.strptime(self.dataOut.TimeBlockDate))
376
377 #if salf.dataOut.CurrentBlock<NAVG:
378 if self.bcounter==0:
379
380 self.dataOut.header[4][0]=self.dataOut.header[5][0]
381 if self.dataOut.CurrentBlock==1:
382 self.dataOut.header[16][0]=self.dataOut.header[5][0]
383
384 self.cax_navg=[]
385 self.cay_navg=[]
386 self.cbx_navg=[]
387 self.cby_navg=[]
388 self.cax2_navg=[]
389 self.cay2_navg=[]
390 self.cbx2_navg=[]
391 self.cby2_navg=[]
392 self.caxbx_navg=[]
393 self.caxby_navg=[]
394 self.caybx_navg=[]
395 self.cayby_navg=[]
396 self.caxay_navg=[]
397 self.cbxby_navg=[]
398 self.dataOut.kax=None
399 self.dataOut.kay=None
400 self.dataOut.kbx=None
401 self.dataOut.kby=None
402 self.dataOut.kax2=None
403 self.dataOut.kay2=None
404 self.dataOut.kbx2=None
405 self.dataOut.kby2=None
406 self.dataOut.kaxbx=None
407 self.dataOut.kaxby=None
408 self.dataOut.kaybx=None
409 self.dataOut.kayby=None
410 self.dataOut.kaxay=None
411 self.dataOut.kbxby=None
412
413 self.dataOut.noisevector=numpy.zeros((self.dataOut.read_samples,self.dataOut.NR,self.dataOut.NAVG),'float32') #30/03/2020
414 self.dataOut.noisevector_=numpy.zeros((self.dataOut.read_samples,self.dataOut.NR,self.dataOut.NAVG),'float32')
415 self.dataOut.dc=numpy.zeros(self.dataOut.NR,dtype=numpy.complex_) #30/03/2020
416 #self.dataOut.noisevector=numpy.zeros((self.dataOut.read_samples,2,self.dataOut.NAVG),'float32') #31/03/2020
417 #self.dataOut.noisevector_=numpy.zeros((self.dataOut.read_samples,2,self.dataOut.NAVG),'float32') #31/03/2020
418
419 #self.dataOut.dc=numpy.zeros(2,dtype=numpy.complex_) #31/03/2020
420 #self.dataOut.processingHeaderObj.profilesPerBlock
421 if self.dataOut.experiment=="DP":
422 self.noisevectorizer(self.dataOut.nptsfft1,self.dataOut.nptsfft2) #30/03/2020
423 if self.dataOut.experiment=="HP":
424 self.noisevectorizer(self.dataOut.nptsfft1,self.dataOut.nptsfftx1) #31/03/2020
425 #print(self.dataOut.noisevector[:,:,:])
426 #print("·················································")
427 #print("CAX: ",self.cax)
428 self.cax_navg.append(self.cax)
429 self.cay_navg.append(self.cay)
430 self.cbx_navg.append(self.cbx)
431 self.cby_navg.append(self.cby)
432 self.cax2_navg.append(self.cax2)
433 self.cay2_navg.append(self.cay2)
434 self.cbx2_navg.append(self.cbx2)
435 self.cby2_navg.append(self.cby2)
436 self.caxbx_navg.append(self.caxbx)
437 self.caxby_navg.append(self.caxby)
438 self.caybx_navg.append(self.caybx)
439 self.cayby_navg.append(self.cayby)
440 self.caxay_navg.append(self.caxay)
441 self.cbxby_navg.append(self.cbxby)
442 self.bcounter+=1
443
444 #self.dataOut.data=None
445 #print("bcounter",bcounter)
446 #/#/#/#if self.bcounter==NAVG:
447
448 #/#/#/#print("cax_navg: ",self.cax_navg)
449 #/#/#/#self.bcounter=0
450 #print("blocknow",self.dataOut.current)
451
452
453
454 def kabxys(self,NAVG,nkill):#,NRANGE,NCAL,NDT):
455
456 self.dataOut.NAVG=NAVG
457 self.dataOut.nkill=nkill
458 #print("bcounter_before: ",self.bcounter)
459 #print("kabxys")
460
461 #if self.dataOut.input_dat_type==0:
462 #self.dataOut.NDP=NDP
463 #self.dataOut.nlags_array=nlags_array
464 #self.dataOut.NSCAN=NSCAN
465 #self.dataOut.DH=float(DH)
466 #self.dataOut.flags_array=flags_array
467
468 #self.dataOut.DPL=DPL
469 #self.dataOut.NRANGE=NRANGE
470 #self.dataOut.NCAL=NCAL
471
472 #self.dataOut.NDT=NDT
473 #self.lag_products_LP()
474 ####self.cabxys_navg(NDP,nlags_array,NSCAN,flags_array)
475 self.cabxys_navg()
476 #self.dataOut.kshape=numpy.zeros((numpy.shape(self.cax_navg[0])[0],numpy.shape(self.cax_navg[0])[1],numpy.shape(self.cax_navg[0])[2]))
477 #print("Shape cavg",numpy.shape(self.cax_navg[0])[0])
478 self.dataOut.flag_save=0
479 #self.dataOut.flagNoData = True # new 1
480
481
482 if self.bcounter==self.dataOut.NAVG:
483
484 #self.dataOut.flagNoData = False # new 2
485 self.dataOut.flag_save=1
486 #self.dataOut.kax=None
487
488
489 self.dataOut.noise_final=numpy.zeros(self.dataOut.NR,'float32') #30/03/2020
490 #self.dataOut.noise_final=numpy.zeros(2,'float32') #31/03/2020
491
492 #print("self.dataOut.nChannels: ",self.dataOut.systemHeaderObj.nChannels)
493 self.kax=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
494 self.kay=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
495 self.kbx=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
496 self.kby=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
497 self.kax2=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
498 self.kay2=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
499 self.kbx2=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
500 self.kby2=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
501 self.kaxbx=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
502 self.kaxby=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
503 self.kaybx=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
504 self.kayby=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
505 self.kaxay=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
506 self.kbxby=numpy.zeros((self.dataOut.NDP,self.dataOut.DPL,2),'float32')
507 #print("Shape K",numpy.shape(self.kax))
508 for i in range(self.cax_navg[0].shape[0]):
509 for j in range(self.cax_navg[0].shape[1]):
510 for k in range(self.cax_navg[0].shape[2]):
511 data_navg=[item[i,j,k] for item in self.cax_navg]
512 self.kax[i,j,k]=self.medi(data_navg)
513 data_navg=[item[i,j,k] for item in self.cay_navg]
514 self.kay[i,j,k]=self.medi(data_navg)
515 data_navg=[item[i,j,k] for item in self.cbx_navg]
516 self.kbx[i,j,k]=self.medi(data_navg)
517 data_navg=[item[i,j,k] for item in self.cby_navg]
518 self.kby[i,j,k]=self.medi(data_navg)
519 data_navg=[item[i,j,k] for item in self.cax2_navg]
520 self.kax2[i,j,k]=self.medi(data_navg)
521 data_navg=[item[i,j,k] for item in self.cay2_navg]
522 self.kay2[i,j,k]=self.medi(data_navg)
523 data_navg=[item[i,j,k] for item in self.cbx2_navg]
524 self.kbx2[i,j,k]=self.medi(data_navg)
525 data_navg=[item[i,j,k] for item in self.cby2_navg]
526 self.kby2[i,j,k]=self.medi(data_navg)
527 data_navg=[item[i,j,k] for item in self.caxbx_navg]
528 self.kaxbx[i,j,k]=self.medi(data_navg)
529 data_navg=[item[i,j,k] for item in self.caxby_navg]
530 self.kaxby[i,j,k]=self.medi(data_navg)
531 data_navg=[item[i,j,k] for item in self.caybx_navg]
532 self.kaybx[i,j,k]=self.medi(data_navg)
533 data_navg=[item[i,j,k] for item in self.cayby_navg]
534 self.kayby[i,j,k]=self.medi(data_navg)
535 data_navg=[item[i,j,k] for item in self.caxay_navg]
536 self.kaxay[i,j,k]=self.medi(data_navg)
537 data_navg=[item[i,j,k] for item in self.cbxby_navg]
538 self.kbxby[i,j,k]=self.medi(data_navg)
539 #self.bcounter=0
540 #print("KAX",self.kax)
541 #self.__buffer=self.kax
542 #print("CurrentBlock: ", self.dataOut.CurrentBlock)
543
544 self.dataOut.kax=self.kax
545 self.dataOut.kay=self.kay
546 self.dataOut.kbx=self.kbx
547 self.dataOut.kby=self.kby
548 self.dataOut.kax2=self.kax2
549 self.dataOut.kay2=self.kay2
550 self.dataOut.kbx2=self.kbx2
551 self.dataOut.kby2=self.kby2
552 self.dataOut.kaxbx=self.kaxbx
553 self.dataOut.kaxby=self.kaxby
554 self.dataOut.kaybx=self.kaybx
555 self.dataOut.kayby=self.kayby
556 self.dataOut.kaxay=self.kaxay
557 self.dataOut.kbxby=self.kbxby
558 self.bcounter=0
559
560 #print("before: ",self.dataOut.noise_final)
561
562 self.noise_estimation4x() #30/03/2020
563
564 #print("after: ", self.dataOut.noise_final)
565 #print(numpy.shape(self.dataOut.data))
566 #input()
567 #self.dataOut.noise_final_list.append(self.dataOut.noise_final[0]) #30/03/2020
568
569
570 '''
571 print("hsize[0] ",self.dataOut.header[0])
572 print("bufsize[1] ",self.dataOut.header[1])
573 print("nr[2] ",self.dataOut.header[2])
574 print("ngates[3] ",self.dataOut.header[3])
575 print("time1[4] ",self.dataOut.header[4])
576 print("time2[5] ",self.dataOut.header[5])
577 print("lcounter[6] ",self.dataOut.header[6])
578 print("groups[7] ",self.dataOut.header[7])
579 print("system[8] ",self.dataOut.header[8])
580 print("h0[9] ",self.dataOut.header[9])
581 print("dh[10] ",self.dataOut.header[10])
582 print("ipp[11] ",self.dataOut.header[11])
583 print("process[12] ",self.dataOut.header[12])
584 print("tx[13] ",self.dataOut.header[13])
585 print("padding[14] ",self.dataOut.header[14])
586 print("ngates1[15] ",self.dataOut.header[15])
587 print("header[16] ",self.dataOut.header[16])
588 print("header[17] ",self.dataOut.header[17])
589 print("header[18] ",self.dataOut.header[18])
590 print("header[19] ",self.dataOut.header[19])
591 print("header[20] ",self.dataOut.header[20])
592 print("header[21] ",self.dataOut.header[21])
593 print("header[22] ",self.dataOut.header[22])
594 print("header[23] ",self.dataOut.header[23])
595 print("header[24] ",self.dataOut.header[24])
596 print("header[25] ",self.dataOut.header[25])
597 print("header[26] ",self.dataOut.header[26])
598 print("header[27] ",self.dataOut.header[27])
599 print("header[28] ",self.dataOut.header[28])
600 print("header[29] ",self.dataOut.header[29])
601 print("header[30] ",self.dataOut.header[30])
602 print("header[31] ",self.dataOut.header[31])
603 '''
604
605
606
607 #print("CurrentBlock: ",self.dataOut.CurrentBlock)
608 ##print("KAX: ",self.dataOut.kax)
609
610
611 '''
612 plt.plot(self.kaxby[:,0,0],self.range(),'m',linewidth=2.0)
613 plt.xlim(min(self.kaxby[12::,0,0]), max(self.kaxby[12::,0,0]))
614 plt.show()
615 '''
616
617
618
619
620
621 #/#/#/#print("CurrentBlock: ",self.dataOut.CurrentBlock)
622 ####self.newdataOut=self.kax
623 #print("shapedataout",numpy.shape(self.dataOut.data))
624 #print("kax",numpy.shape(self.kax))
625 ## return 1
626
627
628 ####def NewData(self):
629 ####print("NewData",self.dataOut.kaxby)
630 ####print("CurrentBlock: ",self.dataOut.CurrentBlock)
631
632
633 '''
634 def PlotVoltageLag(self):
635
636 plt.plot(self.dataOut.data[:,0,0],self.range(),'m',linewidth=2.0)
637 plt.xlim(min(self.dataOut.data[12::,0,0]), max(self.dataOut.data[12::,0,0]))
638 plt.show()
639
640
641 if self.bcounter==self.NAVG:
642 #print("shapedataout",self.dataOut.data)
643 print("CurrentBlock: ",self.dataOut.CurrentBlock)
644 self.bcounter=0
645 '''
646
647 #print("Newdataout",self.dataOut.data)
648 ##
649
650
651 #30/03/2020:
652 def noisevectorizer(self,nptsfft1,nptsfft2):
653
654 rnormalizer= 1./float(nptsfft2 - nptsfft1)
655 for i in range(self.dataOut.NR):
656 for j in range(self.dataOut.read_samples):
657 for k in range(nptsfft1,nptsfft2):
658 #TODO:integrate just 2nd quartile gates
659 if k==nptsfft1:
660 self.dataOut.noisevector[j][i][self.bcounter]=(abs(self.dataOut.data[i][k][j]-self.dataOut.dc[i])**2)*rnormalizer
661 ##noisevector[j][i][iavg]=(abs(cdata[k][j][i])**2)*rnormalizer
662 else:
663 self.dataOut.noisevector[j][i][self.bcounter]+=(abs(self.dataOut.data[i][k][j]-self.dataOut.dc[i])**2)*rnormalizer
664
665 #30/03/2020:
666 def noise_estimation4x(self):
667 snoise=numpy.zeros((self.dataOut.NR,self.dataOut.NAVG),'float32')
668 nvector1=numpy.zeros((self.dataOut.NR,self.dataOut.NAVG,self.dataOut.read_samples),'float32')
669 for i in range(self.dataOut.NR):
670 self.dataOut.noise_final[i]=0.0
671 for k in range(self.dataOut.NAVG):
672 snoise[i][k]=0.0
673 for j in range(self.dataOut.read_samples):
674 nvector1[i][k][j]= self.dataOut.noisevector[j][i][k];
675 snoise[i][k]=self.noise_hs4x(self.dataOut.read_samples, nvector1[i][k])
676 self.dataOut.noise_final[i]=self.noise_hs4x(self.dataOut.NAVG, snoise[i])
677
678
679 #30/03/2020:
680 def noise_hs4x(self, ndatax, datax):
681 #print("datax ",datax)
682 divider=10#divider was originally 10
683 noise=0.0
684 data=numpy.zeros(ndatax,'float32')
685 ndata1=int(ndatax/4)
686 ndata2=int(2.5*(ndatax/4.))
687 ndata=int(ndata2-ndata1)
688 sorts=sorted(datax)
689 for k in range(ndata2): # select just second quartile
690 data[k]=sorts[k+ndata1]
691 nums_min= int(ndata/divider)
692 if(int(ndata/divider)> 2):
693 nums_min= int(ndata/divider)
694 else:
695 nums_min=2
696 sump=0.0
697 sumq=0.0
698 j=0
699 cont=1
700 while ( (cont==1) and (j<ndata)):
701 sump+=data[j]
702 sumq+= data[j]*data[j]
703 j=j+1
704 if (j> nums_min):
705 rtest= float(j/(j-1)) +1.0/ndata
706 if( (sumq*j) > (rtest*sump*sump ) ):
707 j=j-1
708 sump-= data[j]
709 sumq-=data[j]*data[j]
710 cont= 0
711 noise= (sump/j)
712
713 return noise
714
715
716
717 def test(self):
718
719 #print("LP_init")
720 #self.dataOut.flagNoData=1
721 buffer=self.dataOut.data
722 #self.dataOut.flagNoData=0
723 if self.LP_products_aux==0:
724
725 #self.dataOut.nptsfft2=150
726 self.cnorm=float((self.dataOut.nptsfft2LP-self.dataOut.NSCAN)/self.dataOut.NSCAN)
727
728
729 #print("self.bcounter",self.bcounter)
730 self.lagp0=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
731 self.lagp1=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
732 self.lagp2=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
733 self.lagp3=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
734 self.lagp4=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
735 self.LP_products_aux=1
736
737 #print(self.dataOut.data[0,0,0])
738 #self.dataOut.flagNoData =False
739 for i in range(self.dataOut.NR-1):
740 #print("inside i",i)
741 buffer_dc=self.dataOut.dc[i]
742 for j in range(self.dataOut.NRANGE):
743 #print("inside j",j)
744 #print(self.dataOut.read_samples)
745 #input()
746 range_for_n=numpy.min((self.dataOut.NRANGE-j,self.dataOut.NLAG))
747 for k in range(self.dataOut.nptsfft2LP):
748 #print(self.dataOut.data[i][k][j])
749 #input()
750 #print(self.dataOut.dc)
751 #input()
752 #aux_ac=0
753 buffer_aux=numpy.conj(buffer[i][k][j]-buffer_dc)
754 #self.dataOut.flagNoData=0
755 for n in range(range_for_n):
756
757
758 #for n in range(numpy.min((self.dataOut.NRANGE-j,self.dataOut.NLAG))):
759 #print(numpy.shape(self.dataOut.data))
760 #input()
761 #pass
762 #self.dataOut.flagNoData=1
763 #c=2*buffer_aux
764 #c=(self.dataOut.data[i][k][j]-self.dataOut.dc[i])*(numpy.conj(self.dataOut.data[i][k][j+n]-self.dataOut.dc[i]))
765 #c=(buffer[i][k][j]-buffer_dc)*(numpy.conj(buffer[i][k][j+n])-buffer_dc)
766
767 c=(buffer_aux)*(buffer[i][k][j+n]-buffer_dc)
768 #c=(buffer[i][k][j])*(buffer[i][k][j+n])
769 #print("first: ",self.dataOut.data[i][k][j]-self.dataOut.dc[i])
770 #print("second: ",numpy.conj(self.dataOut.data[i][k][j+n]-self.dataOut.dc[i]))
771
772 #print("c: ",c)
773 #input()
774 #print("n: ",n)
775 #print("aux_ac",aux_ac)
776 #print("data1:",self.dataOut.data[i][k][j])
777 #print("data2:",self.dataOut.data[i][k][j+n])
778 #print("dc: ",self.dataOut.dc[i])
779 #if aux_ac==2:
780 #input()
781 #aux_ac+=1
782 #print("GG")
783 #print("inside n",n)
784 #pass
785
786 if k<self.dataOut.NSCAN:
787 if k==0:
788
789 while True:
790 if i==0:
791 self.lagp0[n][j][self.bcounter-1]=c
792 break
793 elif i==1:
794 self.lagp1[n][j][self.bcounter-1]=c
795 break
796 elif i==2:
797 self.lagp2[n][j][self.bcounter-1]=c
798 break
799 else:
800 break
801
802 else:
803
804 while True:
805 if i==0:
806 self.lagp0[n][j][self.bcounter-1]=c+self.lagp0[n][j][self.bcounter-1]
807 break
808 elif i==1:
809 self.lagp1[n][j][self.bcounter-1]=c+self.lagp1[n][j][self.bcounter-1]
810 break
811 elif i==2:
812 self.lagp2[n][j][self.bcounter-1]=c+self.lagp2[n][j][self.bcounter-1]
813 break
814 else:
815 break
816
817 else:
818 #c=c/self.cnorm
819 if i==0:
820 c=c/self.cnorm
821 if k==self.dataOut.NSCAN:
822 #if i==0:
823 self.lagp3[n][j][self.bcounter-1]=c
824 #print("n: ",n,"j: ",j,"iavg: ",self.bcounter-1)
825 #print("lagp3_inside: ",self.lagp3[n][j][self.bcounter-1])
826 else:
827 #if i==0:
828 self.lagp3[n][j][self.bcounter-1]=c+self.lagp3[n][j][self.bcounter-1]
829
830
831
832
833 #print("lagp2: ",self.lagp2[:,0,0])
834 self.lagp0[:,:,self.bcounter-1]=numpy.conj(self.lagp0[:,:,self.bcounter-1])
835 self.lagp1[:,:,self.bcounter-1]=numpy.conj(self.lagp1[:,:,self.bcounter-1])
836 self.lagp2[:,:,self.bcounter-1]=numpy.conj(self.lagp2[:,:,self.bcounter-1])
837 self.lagp3[:,:,self.bcounter-1]=numpy.conj(self.lagp3[:,:,self.bcounter-1])
838 #self.dataOut.flagNoData=0
839 #print(self.bcounter-1)
840 #print("lagp2_conj: ",self.lagp2[:,0,self.bcounter-1])
841 #input()
842 #self.dataOut.lagp3=self.lagp3
843 print("TEST")
844
845
846
847 def lag_products_LP(self):
848
849 #print("LP_init")
850 #self.dataOut.flagNoData=1
851 buffer=self.dataOut.data
852 #self.dataOut.flagNoData=0
853 if self.LP_products_aux==0:
854
855 #self.dataOut.nptsfft2=150
856 self.cnorm=float((self.dataOut.nptsfft2LP-self.dataOut.NSCAN)/self.dataOut.NSCAN)
857
858
859 #print("self.bcounter",self.bcounter)
860 self.lagp0=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
861 self.lagp1=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
862 self.lagp2=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
863 self.lagp3=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
864 self.lagp4=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
865 self.LP_products_aux=1
866
867 #print(self.dataOut.data[0,0,0])
868 #self.dataOut.flagNoData =False
869 for i in range(self.dataOut.NR):
870 #print("inside i",i)
871 buffer_dc=self.dataOut.dc[i]
872 for j in range(self.dataOut.NRANGE):
873 #print("inside j",j)
874 #print(self.dataOut.read_samples)
875 #input()
876 range_for_n=numpy.min((self.dataOut.NRANGE-j,self.dataOut.NLAG))
877 for k in range(self.dataOut.nptsfft2LP):
878 #print(self.dataOut.data[i][k][j])
879 #input()
880 #print(self.dataOut.dc)
881 #input()
882 #aux_ac=0
883 buffer_aux=numpy.conj(buffer[i][k][j]-buffer_dc)
884 #self.dataOut.flagNoData=0
885 for n in range(range_for_n):
886 #for n in range(numpy.min((self.dataOut.NRANGE-j,self.dataOut.NLAG))):
887 #print(numpy.shape(self.dataOut.data))
888 #input()
889 #pass
890 #self.dataOut.flagNoData=1
891 #c=2*buffer_aux
892 #c=(self.dataOut.data[i][k][j]-self.dataOut.dc[i])*(numpy.conj(self.dataOut.data[i][k][j+n]-self.dataOut.dc[i]))
893 #c=(buffer[i][k][j]-buffer_dc)*(numpy.conj(buffer[i][k][j+n])-buffer_dc)
894
895 c=(buffer_aux)*(buffer[i][k][j+n]-buffer_dc)
896 #c=(buffer[i][k][j])*(buffer[i][k][j+n])
897 #print("first: ",self.dataOut.data[i][k][j]-self.dataOut.dc[i])
898 #print("second: ",numpy.conj(self.dataOut.data[i][k][j+n]-self.dataOut.dc[i]))
899
900 #print("c: ",c)
901 #input()
902 #print("n: ",n)
903 #print("aux_ac",aux_ac)
904 #print("data1:",self.dataOut.data[i][k][j])
905 #print("data2:",self.dataOut.data[i][k][j+n])
906 #print("dc: ",self.dataOut.dc[i])
907 #if aux_ac==2:
908 #input()
909 #aux_ac+=1
910 #print("GG")
911 #print("inside n",n)
912 #pass
913
914 if k<self.dataOut.NSCAN:
915 if k==0:
916 if i==0:
917 self.lagp0[n][j][self.bcounter-1]=c
918 elif i==1:
919 self.lagp1[n][j][self.bcounter-1]=c
920 elif i==2:
921 self.lagp2[n][j][self.bcounter-1]=c
922 else:
923 if i==0:
924 self.lagp0[n][j][self.bcounter-1]=c+self.lagp0[n][j][self.bcounter-1]
925 elif i==1:
926 self.lagp1[n][j][self.bcounter-1]=c+self.lagp1[n][j][self.bcounter-1]
927 elif i==2:
928 self.lagp2[n][j][self.bcounter-1]=c+self.lagp2[n][j][self.bcounter-1]
929
930 else:
931 c=c/self.cnorm
932 if k==self.dataOut.NSCAN:
933 if i==0:
934 self.lagp3[n][j][self.bcounter-1]=c
935 #print("n: ",n,"j: ",j,"iavg: ",self.bcounter-1)
936 #print("lagp3_inside: ",self.lagp3[n][j][self.bcounter-1])
937 else:
938 if i==0:
939 self.lagp3[n][j][self.bcounter-1]=c+self.lagp3[n][j][self.bcounter-1]
940
941
942
943 #print("lagp2: ",self.lagp2[:,0,0])
944 self.lagp0[:,:,self.bcounter-1]=numpy.conj(self.lagp0[:,:,self.bcounter-1])
945 self.lagp1[:,:,self.bcounter-1]=numpy.conj(self.lagp1[:,:,self.bcounter-1])
946 self.lagp2[:,:,self.bcounter-1]=numpy.conj(self.lagp2[:,:,self.bcounter-1])
947 self.lagp3[:,:,self.bcounter-1]=numpy.conj(self.lagp3[:,:,self.bcounter-1])
948 #self.dataOut.flagNoData=0
949 #print(self.bcounter-1)
950 #print("lagp2_conj: ",self.lagp2[:,0,self.bcounter-1])
951 #input()
952 #self.dataOut.lagp3=self.lagp3
953 print("LP")
954
955
956 def test_2(self):
957
958 #print("LP_init")
959 #self.dataOut.flagNoData=1
960
961 #self.dataOut.flagNoData=0
962 if self.LP_products_aux==0:
963
964 #self.dataOut.nptsfft2=150
965 self.cnorm=float((self.dataOut.nptsfft2LP-self.dataOut.NSCAN)/self.dataOut.NSCAN)
966
967
968 #print("self.bcounter",self.bcounter)
969 self.lagp0=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
970 self.lagp1=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
971 self.lagp2=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
972 self.lagp3=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
973 self.lagp4=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NAVG),'complex64')
974 self.LP_products_aux=1
975
976 #print(self.dataOut.data[0,0,0])
977 #self.dataOut.flagNoData =False
978 for i in range(self.dataOut.NR):
979 #print("inside i",i)
980
981 for j in range(self.dataOut.NRANGE):
982 #print("inside j",j)
983 #print(self.dataOut.read_samples)
984 #input()
985
986 for k in range(self.dataOut.nptsfft2LP):
987 #print(self.dataOut.data[i][k][j])
988 #input()
989 #print(self.dataOut.dc)
990 #input()
991 #aux_ac=0
992
993 #self.dataOut.flagNoData=0
994
995 for n in range(numpy.min((self.dataOut.NRANGE-j,self.dataOut.NLAG))):
996 #print(numpy.shape(self.dataOut.data))
997 #input()
998 #pass
999 #self.dataOut.flagNoData=1
1000 #c=2*buffer_aux
1001 c=(self.dataOut.data[i][k][j]-self.dataOut.dc[i])*(numpy.conj(self.dataOut.data[i][k][j+n]-self.dataOut.dc[i]))
1002 #c=(buffer[i][k][j]-buffer_dc)*(numpy.conj(buffer[i][k][j+n])-buffer_dc)
1003
1004
1005 #c=(buffer[i][k][j])*(buffer[i][k][j+n])
1006 #print("first: ",self.dataOut.data[i][k][j]-self.dataOut.dc[i])
1007 #print("second: ",numpy.conj(self.dataOut.data[i][k][j+n]-self.dataOut.dc[i]))
1008
1009 #print("c: ",c)
1010 #input()
1011 #print("n: ",n)
1012 #print("aux_ac",aux_ac)
1013 #print("data1:",self.dataOut.data[i][k][j])
1014 #print("data2:",self.dataOut.data[i][k][j+n])
1015 #print("dc: ",self.dataOut.dc[i])
1016 #if aux_ac==2:
1017 #input()
1018 #aux_ac+=1
1019 #print("GG")
1020 #print("inside n",n)
1021 #pass
1022
1023 if k<self.dataOut.NSCAN:
1024 if k==0:
1025 if i==0:
1026 self.lagp0[n][j][self.bcounter-1]=c
1027 elif i==1:
1028 self.lagp1[n][j][self.bcounter-1]=c
1029 elif i==2:
1030 self.lagp2[n][j][self.bcounter-1]=c
1031 else:
1032 if i==0:
1033 self.lagp0[n][j][self.bcounter-1]=c+self.lagp0[n][j][self.bcounter-1]
1034 elif i==1:
1035 self.lagp1[n][j][self.bcounter-1]=c+self.lagp1[n][j][self.bcounter-1]
1036 elif i==2:
1037 self.lagp2[n][j][self.bcounter-1]=c+self.lagp2[n][j][self.bcounter-1]
1038
1039 else:
1040 c=c/self.cnorm
1041 if k==self.dataOut.NSCAN:
1042 if i==0:
1043 self.lagp3[n][j][self.bcounter-1]=c
1044 #print("n: ",n,"j: ",j,"iavg: ",self.bcounter-1)
1045 #print("lagp3_inside: ",self.lagp3[n][j][self.bcounter-1])
1046 else:
1047 if i==0:
1048 self.lagp3[n][j][self.bcounter-1]=c+self.lagp3[n][j][self.bcounter-1]
1049
1050
1051
1052 #print("lagp2: ",self.lagp2[:,0,0])
1053
1054 #self.dataOut.flagNoData=0
1055 #print(self.bcounter-1)
1056 #print("lagp2_conj: ",self.lagp2[:,0,self.bcounter-1])
1057 #input()
1058 #self.dataOut.lagp3=self.lagp3
1059 print("LP")
1060
1061
1062 def LP_median_estimates(self):
1063 #print("lagp3: ",self.lagp3[:,0,0])
1064 #print("self.bcounter: ",self.bcounter)
1065 if self.dataOut.flag_save==1:
1066
1067 #print("lagp1: ",self.lagp1[0,0,:])
1068 #input()
1069
1070 if self.lag_products_LP_median_estimates_aux==0:
1071 self.output=numpy.zeros((self.dataOut.NLAG,self.dataOut.NRANGE,self.dataOut.NR),'complex64')
1072 #sorts=numpy.zeros(128,'float32')
1073 #self.dataOut.output_LP=None
1074 self.lag_products_LP_median_estimates_aux=1
1075
1076
1077 for i in range(self.dataOut.NLAG):
1078 for j in range(self.dataOut.NRANGE):
1079 for l in range(4): #four outputs
1080 '''
1081 for k in range(self.dataOut.NAVG):
1082 #rsorts[k]=float(k)
1083 if l==0:
1084 #sorts[k]=self.lagp0[i,j,k].real
1085 self.lagp0[i,j,k].real=sorted(self.lagp0[i,j,k].real)
1086 if l==1:
1087 #sorts[k]=self.lagp1[i,j,k].real
1088 self.lagp1[i,j,k].real=sorted(self.lagp1[i,j,k].real)
1089 if l==2:
1090 #sorts[k]=self.lagp2[i,j,k].real
1091 self.lagp2[i,j,k].real=sorted(self.lagp2[i,j,k].real)
1092 if l==3:
1093 #sorts[k]=self.lagp3[i,j,k].real
1094 self.lagp3[i,j,k].real=sorted(self.lagp3[i,j,k].real)
1095 '''
1096
1097 #sorts=sorted(sorts)
1098 #self.lagp0[i,j,k].real=sorted(self.lagp0[i,j,k].real)
1099 #self.lagp1[i,j,k].real=sorted(self.lagp1[i,j,k].real)
1100 #self.lagp2[i,j,k].real=sorted(self.lagp2[i,j,k].real)
1101 #self.lagp3[i,j,k].real=sorted(self.lagp3[i,j,k].real)
1102
1103 for k in range(self.dataOut.NAVG):
1104
1105
1106
1107 if k==0:
1108 self.output[i,j,l]=0.0+0.j
1109
1110 if l==0:
1111 self.lagp0[i,j,:]=sorted(self.lagp0[i,j,:], key=lambda x: x.real) #sorted(self.lagp0[i,j,:].real)
1112
1113 if l==1:
1114 self.lagp1[i,j,:]=sorted(self.lagp1[i,j,:], key=lambda x: x.real) #sorted(self.lagp1[i,j,:].real)
1115 if l==2:
1116 self.lagp2[i,j,:]=sorted(self.lagp2[i,j,:], key=lambda x: x.real) #sorted(self.lagp2[i,j,:].real)
1117 if l==3:
1118 self.lagp3[i,j,:]=sorted(self.lagp3[i,j,:], key=lambda x: x.real) #sorted(self.lagp3[i,j,:].real)
1119
1120
1121 if k>=self.dataOut.nkill/2 and k<self.dataOut.NAVG-self.dataOut.nkill/2:
1122 if l==0:
1123
1124 self.output[i,j,l]=self.output[i,j,l]+((float(self.dataOut.NAVG)/(float)(self.dataOut.NAVG-self.dataOut.nkill))*self.lagp0[i,j,k])
1125 if l==1:
1126 #print("lagp1: ",self.lagp1[0,0,:])
1127 #input()
1128 self.output[i,j,l]=self.output[i,j,l]+((float(self.dataOut.NAVG)/(float)(self.dataOut.NAVG-self.dataOut.nkill))*self.lagp1[i,j,k])
1129 #print("self.lagp1[i,j,k]: ",self.lagp1[i,j,k])
1130 #input()
1131 if l==2:
1132 self.output[i,j,l]=self.output[i,j,l]+((float(self.dataOut.NAVG)/(float)(self.dataOut.NAVG-self.dataOut.nkill))*self.lagp2[i,j,k])
1133 if l==3:
1134 #print(numpy.shape(output))
1135 #print(numpy.shape(self.lagp3))
1136 #print("i: ",i,"j: ",j,"k: ",k)
1137
1138 #a=((float(self.dataOut.NAVG)/(float)(self.dataOut.NAVG-self.dataOut.nkill))*self.lagp3[i,j,k])
1139 #print("self.lagp3[i,j,k]: ",self.lagp3[i,j,k])
1140 #input()
1141 self.output[i,j,l]=self.output[i,j,l]+((float(self.dataOut.NAVG)/(float)(self.dataOut.NAVG-self.dataOut.nkill))*self.lagp3[i,j,k])
1142 #print(a)
1143 #print("output[i,j,l]: ",output[i,j,l])
1144 #input()
1145
1146
1147 self.dataOut.output_LP=self.output
1148 #print(numpy.shape(sefl.dataOut.output_LP))
1149 #input()
1150 #print("output: ",self.dataOut.output_LP[:,0,0])
1151 #input()
1152
1153
1154 def remove_debris_LP(self):
1155
1156 if self.dataOut.flag_save==1:
1157 debris=numpy.zeros(self.dataOut.NRANGE,'float32')
1158 #self.dataOut.debris_activated=0
1159 for j in range(0,3):
1160 for i in range(self.dataOut.NRANGE):
1161 if j==0:
1162 debris[i]=10*numpy.log10(numpy.abs(self.dataOut.output_LP[j,i,0]))
1163 else:
1164 debris[i]+=10*numpy.log10(numpy.abs(self.dataOut.output_LP[j,i,0]))
1165
1166 '''
1167 debris=10*numpy.log10(numpy.abs(self.dataOut.output_LP[0,:,0]))
1168
1169 for j in range(1,3):
1170 for i in range(self.dataOut.NRANGE):
1171 debris[i]+=debris[i]
1172 '''
1173
1174 thresh=8.0+4+4+4
1175 for i in range(47,100):
1176 if ((debris[i-2]+debris[i-1]+debris[i]+debris[i+1])>
1177 ((debris[i-12]+debris[i-11]+debris[i-10]+debris[i-9]+
1178 debris[i+12]+debris[i+11]+debris[i+10]+debris[i+9])/2.0+
1179 thresh)):
1180
1181 self.dataOut.debris_activated=1
1182 #print("LP debris",i)
1183
1184
1185 #print("self.debris",debris)
1186
1187
1188 def remove_debris_DP(self):
1189
1190 if self.dataOut.flag_save==1:
1191 debris=numpy.zeros(self.dataOut.NDP,dtype='float32')
1192 Range=numpy.arange(0,3000,15)
1193 for k in range(2): #flip
1194 for i in range(self.dataOut.NDP): #
1195 debris[i]+=numpy.sqrt((self.dataOut.kaxbx[i,0,k]+self.dataOut.kayby[i,0,k])**2+(self.dataOut.kaybx[i,0,k]-self.dataOut.kaxby[i,0,k])**2)
1196
1197 #print("debris: ",debris)
1198
1199 if time.gmtime(self.dataOut.utctime).tm_hour > 11:
1200 for i in range(2,self.dataOut.NDP-2):
1201 if (debris[i]>3.0*debris[i-2] and
1202 debris[i]>3.0*debris[i+2] and
1203 Range[i]>200.0 and Range[i]<=540.0):
1204
1205 self.dataOut.debris_activated=1
1206 #print("DP debris")
1207
1208
1209
1210
1211
1212
1213 def run(self, experiment="", nlags_array=None, NLAG=None, NR=None, NRANGE=None, NCAL=None, DPL=None,
1214 NDN=None, NDT=None, NDP=None, NLP=None, NSCAN=None, HDR_SIZE=None, DH=15, H0=None, LPMASK=None,
1215 flags_array=None,
1216 NPROFILE1=None, NPROFILE2=None, NPROFILES=None, NPROFILE=None,
1217 lagind=None, lagfirst=None,
1218 nptsfftx1=None):
1219
1220 #self.dataOut.input_dat_type=input_dat_type
1221
1222 self.dataOut.experiment=experiment
1223
1224 #print(self.dataOut.experiment)
1225 self.dataOut.nlags_array=nlags_array
1226 self.dataOut.NLAG=NLAG
1227 self.dataOut.NR=NR
1228 self.dataOut.NRANGE=NRANGE
1229 #print(self.dataOut.NRANGE)
1230 self.dataOut.NCAL=NCAL
1231 self.dataOut.DPL=DPL
1232 self.dataOut.NDN=NDN
1233 self.dataOut.NDT=NDT
1234 self.dataOut.NDP=NDP
1235 self.dataOut.NLP=NLP
1236 self.dataOut.NSCAN=NSCAN
1237 self.dataOut.HDR_SIZE=HDR_SIZE
1238 self.dataOut.DH=float(DH)
1239 self.dataOut.H0=H0
1240 self.dataOut.LPMASK=LPMASK
1241 self.dataOut.flags_array=flags_array
1242
1243 self.dataOut.NPROFILE1=NPROFILE1
1244 self.dataOut.NPROFILE2=NPROFILE2
1245 self.dataOut.NPROFILES=NPROFILES
1246 self.dataOut.NPROFILE=NPROFILE
1247 self.dataOut.lagind=lagind
1248 self.dataOut.lagfirst=lagfirst
1249 self.dataOut.nptsfftx1=nptsfftx1
1250
1251
1252 self.dataOut.copy(self.dataIn)
1253 #print(self.dataOut.datatime)
1254 #print(self.dataOut.ippSeconds_general)
1255 #print("Data: ",numpy.shape(self.dataOut.data))
1256 #print("Data_after: ",self.dataOut.data[0,0,1])
1257 ## (4, 150, 334)
1258 #print(self.dataOut.channelIndexList)
1259
1260 #print(self.dataOut.timeInterval)
1261
1262 ###NEWWWWWWW
1263 self.dataOut.lat=-11.95
1264 self.dataOut.lon=-7687
1265 self.dataOut.debris_activated=0
1266
1267 #print(time.gmtime(self.dataOut.utctime).tm_hour)
1268 #print(numpy.shape(self.dataOut.heightList))
1269
1270
1271
1272 class NewData(Operation):
1273 def __init__(self, **kwargs):
1274
1275 Operation.__init__(self, **kwargs)
1276
1277
1278
1279
1280
1281 def run(self,dataOut):
1282
1283 #print("SHAPE",numpy.shape(dataOut.kaxby))
1284 print("CurrentBlock",dataOut.CurrentBlock)
1285 #print("DATAOUT",dataOut.kaxby)
1286 #print("TRUE OR FALSE",numpy.shape(dataOut.kaxby)==())
1287 #print("SHAPE",numpy.shape(dataOut.kaxby))
1288 if numpy.shape(dataOutF.kax)!=(): ############VER SI SE PUEDE TRABAJAR CON dataOut.kaxby==None ##Puede ser cualquier k...
1289
1290 print("NEWDATA",dataOut.kaxby)
1291
1292
1293
1294
1295
1296 return dataOut
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308 '''
1309
1310 class PlotVoltageLag(Operation):
1311 def __init__(self, **kwargs):
1312
1313 Operation.__init__(self, **kwargs)
1314
1315
1316
1317 self.kax=numpy.zeros((self.NDP,self.nlags_array,2),'float32')
1318 def range(self,DH):
1319 Range=numpy.arange(0,990,DH)
1320 return Range
1321
1322
1323
1324 def run(self,dataOut):
1325
1326
1327
1328 #plt.subplot(1, 4, 1)
1329 plt.plot(kax[:,0,0],Range,'r',linewidth=2.0)
1330 plt.xlim(min(limit_min_plot1[12::,0,0]), max(limit_max_plot1[12::,0,0]))
1331 plt.show()
1332
1333 self.kax=numpy.zeros((self.NDP,self.nlags_array,2),'float32')
1334
1335 return dataOut
1336 '''
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366 class Integration(Operation):
1367 def __init__(self, **kwargs):
1368
1369 Operation.__init__(self, **kwargs)
1370
1371
1372
1373 self.counter=0
1374 self.aux=0
1375 self.aux2=1
1376
1377 def run(self,dataOut,nint=None):
1378
1379 dataOut.nint=nint
1380 dataOut.AUX=0
1381 dataOut.paramInterval=dataOut.nint*dataOut.header[7][0]*2 #GENERALIZAR EL 2
1382 #print("CurrentBlock: ",dataOut.CurrentBlock)
1383 #print("date: ",dataOut.datatime)
1384 #print("self.aux: ",self.aux)
1385 #print("CurrentBlockAAAAAA: ",dataOut.CurrentBlock)
1386 #print(dataOut.input_dat_type)
1387 #print(dataOut.heightList)
1388
1389 #print(dataOut.blocktime.ctime())
1390 '''
1391 if dataOut.input_dat_type: #when .dat data is read
1392 #print(dataOut.realtime)
1393 #print("OKODOKO")
1394 #dataOut.flagNoData = False
1395 #print(dataOut.flagNoData)
1396 if self.aux2:
1397
1398 self.noise=numpy.zeros(dataOut.NR,'float32')
1399
1400
1401 padding=numpy.zeros(1,'int32')
1402
1403 hsize=numpy.zeros(1,'int32')
1404 bufsize=numpy.zeros(1,'int32')
1405 nr=numpy.zeros(1,'int32')
1406 ngates=numpy.zeros(1,'int32') ### ### ### 2
1407 time1=numpy.zeros(1,'uint64') # pos 3
1408 time2=numpy.zeros(1,'uint64') # pos 4
1409 lcounter=numpy.zeros(1,'int32')
1410 groups=numpy.zeros(1,'int32')
1411 system=numpy.zeros(4,'int8') # pos 7
1412 h0=numpy.zeros(1,'float32')
1413 dh=numpy.zeros(1,'float32')
1414 ipp=numpy.zeros(1,'float32')
1415 process=numpy.zeros(1,'int32')
1416 tx=numpy.zeros(1,'int32')
1417
1418 ngates1=numpy.zeros(1,'int32') ### ### ### 13
1419 time0=numpy.zeros(1,'uint64') # pos 14
1420 nlags=numpy.zeros(1,'int32')
1421 nlags1=numpy.zeros(1,'int32')
1422 txb=numpy.zeros(1,'float32') ### ### ### 17
1423 time3=numpy.zeros(1,'uint64') # pos 18
1424 time4=numpy.zeros(1,'uint64') # pos 19
1425 h0_=numpy.zeros(1,'float32')
1426 dh_=numpy.zeros(1,'float32')
1427 ipp_=numpy.zeros(1,'float32')
1428 txa_=numpy.zeros(1,'float32')
1429
1430 pad=numpy.zeros(100,'int32')
1431
1432 nbytes=numpy.zeros(1,'int32')
1433 limits=numpy.zeros(1,'int32')
1434 ngroups=numpy.zeros(1,'int32') ### ### ### 27
1435 #Make the header list
1436 #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]
1437 dataOut.header=[hsize,bufsize,nr,ngates,time1,time2,lcounter,groups,system,h0,dh,ipp,process,tx,ngates1,padding,time0,nlags,nlags1,padding,txb,time3,time4,h0_,dh_,ipp_,txa_,pad,nbytes,limits,padding,ngroups]
1438
1439
1440
1441 dataOut.kax=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1442 dataOut.kay=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1443 dataOut.kbx=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1444 dataOut.kby=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1445 dataOut.kax2=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1446 dataOut.kay2=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1447 dataOut.kbx2=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1448 dataOut.kby2=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1449 dataOut.kaxbx=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1450 dataOut.kaxby=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1451 dataOut.kaybx=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1452 dataOut.kayby=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1453 dataOut.kaxay=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1454 dataOut.kbxby=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1455
1456 self.dataOut.final_cross_products=[dataOut.kax,dataOut.kay,dataOut.kbx,dataOut.kby,dataOut.kax2,dataOut.kay2,dataOut.kbx2,dataOut.kby2,dataOut.kaxbx,dataOut.kaxby,dataOut.kaybx,dataOut.kayby,dataOut.kaxay,dataOut.kbxby]
1457
1458 self.inputfile_DP = open(dataOut.fname,"rb")
1459
1460 ## read header the header first time
1461 for i in range(len(dataOut.header)):
1462 for j in range(len(dataOut.header[i])):
1463 #print("len(header[i]) ",len(header[i]))
1464 #input()
1465 temp=self.inputfile_DP.read(int(dataOut.header[i].itemsize))
1466 if isinstance(dataOut.header[i][0], numpy.int32):
1467 #print(struct.unpack('i', temp)[0])
1468 dataOut.header[i][0]=struct.unpack('i', temp)[0]
1469 if isinstance(dataOut.header[i][0], numpy.uint64):
1470 dataOut.header[i][0]=struct.unpack('q', temp)[0]
1471 if isinstance(dataOut.header[i][0], numpy.int8):
1472 dataOut.header[i][0]=struct.unpack('B', temp)[0]
1473 if isinstance(dataOut.header[i][0], numpy.float32):
1474 dataOut.header[i][0]=struct.unpack('f', temp)[0]
1475
1476
1477
1478
1479 self.activator_No_Data=1
1480
1481 self.inputfile_DP.seek(0,0)
1482
1483 #print("Repositioning to",self.npos," bytes, bufsize ", self.header[1][0])
1484 #self.inputfile.seek(self.npos, 0)
1485 #print("inputfile.tell() ",self.inputfile.tell() ," npos : ", self.npos)
1486
1487 self.npos=0
1488
1489 #if dataOut.nint < 0:
1490 # dataOut.nint=-dataOut.nint
1491 # sfile=os.stat(dataOut.fname)
1492 # if (os.path.exists(dataOut.fname)==0):
1493 # print("ERROR on STAT file: %s\n", dataOut.fname)
1494 # self.npos=sfile.st_size - dataOut.nint*dataOut.header[1][0]# sfile.st_size - nint*header.bufsize
1495
1496 self.start_another_day=False
1497 if dataOut.new_time_date!=" ":
1498 self.start_another_day=True
1499
1500
1501 if self.start_another_day:
1502 #print("Starting_at_another_day")
1503 #new_time_date = "16/08/2013 09:51:43"
1504 #new_time_seconds=time.mktime(time.strptime(new_time_date))
1505 #dataOut.new_time_date = "04/12/2019 09:21:21"
1506 d = datetime.strptime(dataOut.new_time_date, "%d/%m/%Y %H:%M:%S")
1507 new_time_seconds=time.mktime(d.timetuple())
1508
1509 d_2 = datetime.strptime(dataOut.new_ending_time, "%d/%m/%Y %H:%M:%S")
1510 self.new_ending_time_seconds=time.mktime(d_2.timetuple())
1511 #print("new_time_seconds: ",new_time_seconds)
1512 #input()
1513 jumper=0
1514
1515 #if jumper>0 and nint>0:
1516 while True:
1517 sfile=os.stat(dataOut.fname)
1518
1519 if (os.path.exists(dataOut.fname)==0):
1520 print("ERROR on STAT file: %s\n",dataOut.fname)
1521 self.npos=jumper*dataOut.nint*dataOut.header[1][0] #jump_blocks*header,bufsize
1522 self.npos_next=(jumper+1)*dataOut.nint*dataOut.header[1][0]
1523 self.inputfile_DP.seek(self.npos, 0)
1524 jumper+=1
1525 for i in range(len(dataOut.header)):
1526 for j in range(len(dataOut.header[i])):
1527 #print("len(header[i]) ",len(header[i]))
1528 #input()
1529 temp=self.inputfile_DP.read(int(dataOut.header[i].itemsize))
1530 if isinstance(dataOut.header[i][0], numpy.int32):
1531 #print(struct.unpack('i', temp)[0])
1532 dataOut.header[i][0]=struct.unpack('i', temp)[0]
1533 if isinstance(dataOut.header[i][0], numpy.uint64):
1534 dataOut.header[i][0]=struct.unpack('q', temp)[0]
1535 if isinstance(dataOut.header[i][0], numpy.int8):
1536 dataOut.header[i][0]=struct.unpack('B', temp)[0]
1537 if isinstance(dataOut.header[i][0], numpy.float32):
1538 dataOut.header[i][0]=struct.unpack('f', temp)[0]
1539
1540 if self.npos==0:
1541 if new_time_seconds<dataOut.header[4][0]:
1542 break
1543 #dataOut.flagNoData=1
1544 #return dataOut.flagNoData
1545
1546 self.npos_aux=sfile.st_size - dataOut.nint*dataOut.header[1][0]
1547 self.inputfile_DP.seek(self.npos_aux, 0)
1548
1549 for i in range(len(dataOut.header)):
1550 for j in range(len(dataOut.header[i])):
1551 #print("len(header[i]) ",len(header[i]))
1552 #input()
1553 temp=self.inputfile_DP.read(int(dataOut.header[i].itemsize))
1554 if isinstance(dataOut.header[i][0], numpy.int32):
1555 #print(struct.unpack('i', temp)[0])
1556 dataOut.header[i][0]=struct.unpack('i', temp)[0]
1557 if isinstance(dataOut.header[i][0], numpy.uint64):
1558 dataOut.header[i][0]=struct.unpack('q', temp)[0]
1559 if isinstance(dataOut.header[i][0], numpy.int8):
1560 dataOut.header[i][0]=struct.unpack('B', temp)[0]
1561 if isinstance(dataOut.header[i][0], numpy.float32):
1562 dataOut.header[i][0]=struct.unpack('f', temp)[0]
1563
1564 if new_time_seconds>dataOut.header[4][0]:
1565 print("No Data")
1566 self.inputfile_DP.close()
1567 sys.exit(1)
1568
1569 self.inputfile_DP.seek(self.npos, 0)
1570
1571
1572
1573
1574 if new_time_seconds==dataOut.header[4][0]:
1575 #print("EQUALS")
1576 break
1577
1578 self.inputfile_DP.seek(self.npos_next, 0)
1579
1580 for i in range(len(dataOut.header)):
1581 for j in range(len(dataOut.header[i])):
1582 #print("len(header[i]) ",len(header[i]))
1583 #input()
1584 temp=self.inputfile_DP.read(int(dataOut.header[i].itemsize))
1585 if isinstance(dataOut.header[i][0], numpy.int32):
1586 #print(struct.unpack('i', temp)[0])
1587 dataOut.header[i][0]=struct.unpack('i', temp)[0]
1588 if isinstance(dataOut.header[i][0], numpy.uint64):
1589 dataOut.header[i][0]=struct.unpack('q', temp)[0]
1590 if isinstance(dataOut.header[i][0], numpy.int8):
1591 dataOut.header[i][0]=struct.unpack('B', temp)[0]
1592 if isinstance(dataOut.header[i][0], numpy.float32):
1593 dataOut.header[i][0]=struct.unpack('f', temp)[0]
1594
1595
1596 if new_time_seconds<dataOut.header[4][0]:
1597 break
1598
1599
1600
1601
1602
1603
1604
1605
1606 #print("Repositioning to",self.npos," bytes, bufsize ", dataOut.header[1][0])
1607 self.inputfile_DP.seek(self.npos, 0)
1608 #print("inputfile.tell() ",self.inputfile_DP.tell() ," npos : ", self.npos)
1609
1610 self.aux2=0
1611
1612
1613 for ii in range(len(dataOut.header)):
1614 for j in range(len(dataOut.header[ii])):
1615 temp=self.inputfile_DP.read(int(dataOut.header[ii].itemsize))
1616
1617 if(b''==temp):# sizeof(header)
1618 dataOut.flagDiscontinuousBlock=1
1619 #print("EOF \n\n\n\n")
1620 #log.success("")
1621 #self.inputfile_DP.close()
1622 dataOut.error = True
1623 #dataOut.flagNoData = True
1624 #dataOut.stop=True
1625 #return dataOut
1626 #dataOut.
1627 return dataOut
1628
1629 #return dataOut.flagNoData
1630 #writedb_head()
1631 #outputfile.close()
1632 #sys.exit(0)
1633 #THE PROGRAM SHOULD END HERE
1634
1635 if isinstance(dataOut.header[ii][0], numpy.int32):
1636 #print(struct.unpack('i', temp)[0])
1637 dataOut.header[ii][0]=struct.unpack('i', temp)[0]
1638 if isinstance(dataOut.header[ii][0], numpy.uint64):
1639 dataOut.header[ii][0]=struct.unpack('q', temp)[0]
1640 if isinstance(dataOut.header[ii][0], numpy.int8):
1641 dataOut.header[ii][0]=struct.unpack('B', temp)[0]
1642 if isinstance(dataOut.header[ii][0], numpy.float32):
1643 dataOut.header[ii][0]=struct.unpack('f', temp)[0]
1644
1645
1646 if self.start_another_day:
1647
1648 if dataOut.header[4][0]>self.new_ending_time_seconds:
1649 print("EOF \n")
1650 if self.activator_No_Data:
1651 print("No Data")
1652 self.inputfile_DP.close()
1653 #sys.exit(0)
1654 dataOut.error = True
1655 return dataOut
1656 #print(self.activator_No_Data)
1657 self.activator_No_Data=0
1658 #dataOut.TimeBlockDate_for_dp_power=dataOut.TimeBlockDate
1659 #dataOut.TimeBlockSeconds_for_dp_power=time.mktime(time.strptime(dataOut.TimeBlockDate_for_dp_power))
1660 dataOut.TimeBlockSeconds_for_dp_power = dataOut.header[4][0]-((dataOut.nint-1)*dataOut.NAVG*2)
1661 #print(dataOut.TimeBlockSeconds_for_dp_power)
1662 dataOut.TimeBlockDate_for_dp_power=datetime.fromtimestamp(dataOut.TimeBlockSeconds_for_dp_power).strftime("%a %b %-d %H:%M:%S %Y")
1663 #print("Date: ",dataOut.TimeBlockDate_for_dp_power)
1664 #print("Seconds: ",dataOut.TimeBlockSeconds_for_dp_power)
1665 dataOut.bd_time=time.gmtime(dataOut.TimeBlockSeconds_for_dp_power)
1666 dataOut.year=dataOut.bd_time.tm_year+(dataOut.bd_time.tm_yday-1)/364.0
1667 dataOut.ut=dataOut.bd_time.tm_hour+dataOut.bd_time.tm_min/60.0+dataOut.bd_time.tm_sec/3600.0
1668
1669
1670 if dataOut.experiment=="HP": # NRANGE*NLAG*NR # np.zeros([total_samples*nprofiles],dtype='complex64')
1671 temp=self.inputfile_DP.read(dataOut.NLAG*dataOut.NR*176*8)
1672 ii=0
1673 for l in range(dataOut.NLAG): #lag
1674 for r in range(dataOut.NR): # unflip and flip
1675 for k in range(176): #RANGE## generalizar
1676 struct.unpack('q', temp[ii:ii+8])[0]
1677 ii=ii+8
1678
1679
1680
1681 #print("A: ",dataOut.kax)
1682 for ind in range(len(self.dataOut.final_cross_products)): #final cross products
1683 temp=self.inputfile_DP.read(dataOut.DPL*2*dataOut.NDT*4) #*4 bytes
1684 ii=0
1685 #print("kabxys.shape ",kabxys.shape)
1686 #print(kabxys)
1687 for l in range(dataOut.DPL): #lag
1688 for fl in range(2): # unflip and flip
1689 for k in range(dataOut.NDT): #RANGE
1690 self.dataOut.final_cross_products[ind][k,l,fl]=struct.unpack('f', temp[ii:ii+4])[0]
1691 ii=ii+4
1692 #print("DPL*2*NDT*4 es: ", DPL*2*NDT*4)
1693 #print("B: ",dataOut.kax)
1694 ## read noise
1695 temp=self.inputfile_DP.read(dataOut.NR*4) #*4 bytes
1696 for ii in range(dataOut.NR):
1697 self.noise[ii]=struct.unpack('f', temp[ii*4:(ii+1)*4])[0]
1698 #print("NR*4 es: ", NR*4)
1699
1700
1701 ################################END input_dat_type################################
1702 '''
1703
1704 #if dataOut.input_dat_type==0:
1705
1706 if self.aux==1:
1707 #print("CurrentBlockBBBBB: ",dataOut.CurrentBlock)
1708 #print(dataOut.datatime)
1709 dataOut.TimeBlockDate_for_dp_power=dataOut.TimeBlockDate
1710
1711 #print("Date: ",dataOut.TimeBlockDate_for_dp_power)
1712 dataOut.TimeBlockSeconds_for_dp_power=time.mktime(time.strptime(dataOut.TimeBlockDate_for_dp_power))
1713 #print("Seconds: ",dataOut.TimeBlockSeconds_for_dp_power)
1714 dataOut.bd_time=time.gmtime(dataOut.TimeBlockSeconds_for_dp_power)
1715 dataOut.year=dataOut.bd_time.tm_year+(dataOut.bd_time.tm_yday-1)/364.0
1716 dataOut.ut=dataOut.bd_time.tm_hour+dataOut.bd_time.tm_min/60.0+dataOut.bd_time.tm_sec/3600.0
1717 #print("date: ", dataOut.TimeBlockDate)
1718 self.aux=0
1719
1720 if numpy.shape(dataOut.kax)!=():
1721 #print("SELFCOUNTER",self.counter)
1722 #dataOut.flagNoData =True
1723 if self.counter==0:
1724 '''
1725 dataOut.kax_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1726 dataOut.kay_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1727 dataOut.kax2_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1728 dataOut.kay2_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1729 dataOut.kbx_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1730 dataOut.kby_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1731 dataOut.kbx2_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1732 dataOut.kby2_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1733 dataOut.kaxbx_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1734 dataOut.kaxby_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1735 dataOut.kaybx_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1736 dataOut.kayby_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1737 dataOut.kaxay_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1738 dataOut.kbxby_integrated=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1739 '''
1740
1741 tmpx=numpy.zeros((dataOut.header[15][0],dataOut.header[17][0],2),'float32')
1742 dataOut.kabxys_integrated=[tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx]
1743 #self.final_cross_products=[dataOut.kax,dataOut.kay,dataOut.kbx,dataOut.kby,dataOut.kax2,dataOut.kay2,dataOut.kbx2,dataOut.kby2,dataOut.kaxbx,dataOut.kaxby,dataOut.kaybx,dataOut.kayby,dataOut.kaxay,dataOut.kbxby]
1744
1745 #print(numpy.shape(tmpx))
1746 if self.counter < dataOut.nint:
1747 #if dataOut.input_dat_type==0:
1748 dataOut.final_cross_products=[dataOut.kax,dataOut.kay,dataOut.kbx,dataOut.kby,dataOut.kax2,dataOut.kay2,dataOut.kbx2,dataOut.kby2,dataOut.kaxbx,dataOut.kaxby,dataOut.kaybx,dataOut.kayby,dataOut.kaxay,dataOut.kbxby]
1749
1750 '''
1751 dataOut.kax_integrated=dataOut.kax_integrated+dataOut.kax
1752 dataOut.kay_integrated=dataOut.kay_integrated+dataOut.kay
1753 dataOut.kax2_integrated=dataOut.kax2_integrated+dataOut.kax2
1754 dataOut.kay2_integrated=dataOut.kay2_integrated+dataOut.kay2
1755 dataOut.kbx_integrated=dataOut.kbx_integrated+dataOut.kbx
1756 dataOut.kby_integrated=dataOut.kby_integrated+dataOut.kby
1757 dataOut.kbx2_integrated=dataOut.kbx2_integrated+dataOut.kbx2
1758 dataOut.kby2_integrated=dataOut.kby2_integrated+dataOut.kby2
1759 dataOut.kaxbx_integrated=dataOut.kaxbx_integrated+dataOut.kaxbx
1760 dataOut.kaxby_integrated=dataOut.kaxby_integrated+dataOut.kaxby
1761 dataOut.kaybx_integrated=dataOut.kaybx_integrated+dataOut.kaybx
1762 dataOut.kayby_integrated=dataOut.kayby_integrated+dataOut.kayby
1763 dataOut.kaxay_integrated=dataOut.kaxay_integrated+dataOut.kaxbx
1764 dataOut.kbxby_integrated=dataOut.kbxby_integrated+dataOut.kbxby
1765 #print("KAX_BEFORE: ",self.kax_integrated)
1766 '''
1767 #print("self.final_cross_products[0]: ",self.final_cross_products[0])
1768
1769 for ind in range(len(dataOut.kabxys_integrated)): #final cross products
1770 dataOut.kabxys_integrated[ind]=dataOut.kabxys_integrated[ind]+dataOut.final_cross_products[ind]
1771 #print("ataOut.kabxys_integrated[0]: ",dataOut.kabxys_integrated[0])
1772
1773 self.counter+=1
1774 if self.counter==dataOut.nint-1:
1775 self.aux=1
1776 #dataOut.TimeBlockDate_for_dp_power=dataOut.TimeBlockDate
1777 if self.counter==dataOut.nint:
1778
1779 #dataOut.flagNoData =False
1780
1781 self.counter=0
1782 dataOut.AUX=1
1783 #self.aux=1
1784 #print("KAXBY_INTEGRATED: ",dataOut.kaxby_integrated)
1785
1786 '''
1787 else :
1788 #dataOut.kax_integrated=self.kax_integrated
1789 self.counter=0
1790
1791
1792 #print("CurrentBlock: ", dataOut.CurrentBlock)
1793 print("KAX_INTEGRATED: ",self.kax_integrated)
1794 #print("nint: ",nint)
1795 '''
1796
1797 ##print("CurrentBlock: ", dataOut.CurrentBlock)
1798 ##print("KAX_INTEGRATED: ",dataOut.kax_integrated)
1799
1800
1801 return dataOut
1802
1803
1804
1805
1806
1807
1808
1809
1810 class SumLagProducts_Old(Operation):
1811 def __init__(self, **kwargs):
1812
1813 Operation.__init__(self, **kwargs)
1814 #dataOut.rnint2=numpy.zeros(dataOut.nlags_array,'float32')
1815
1816
1817 def run(self,dataOut):
1818
1819 if dataOut.AUX: #Solo cuando ya hizo la intregacion se ejecuta
1820
1821
1822 dataOut.rnint2=numpy.zeros(dataOut.header[17][0],'float32')
1823 #print(dataOut.experiment)
1824 if dataOut.experiment=="DP":
1825 for l in range(dataOut.header[17][0]):
1826 dataOut.rnint2[l]=1.0/(dataOut.nint*dataOut.header[7][0]*12.0)
1827
1828
1829 if dataOut.experiment=="HP":
1830 for l in range(dataOut.header[17][0]):
1831 if(l==0 or (l>=3 and l <=6)):
1832 dataOut.rnint2[l]=0.5/float(dataOut.nint*dataOut.header[7][0]*16.0)
1833 else:
1834 dataOut.rnint2[l]=0.5/float(dataOut.nint*dataOut.header[7][0]*8.0)
1835 #print(dataOut.rnint2)
1836 for l in range(dataOut.header[17][0]):
1837
1838 dataOut.kabxys_integrated[4][:,l,0]=(dataOut.kabxys_integrated[4][:,l,0]+dataOut.kabxys_integrated[4][:,l,1])*dataOut.rnint2[l]
1839 dataOut.kabxys_integrated[5][:,l,0]=(dataOut.kabxys_integrated[5][:,l,0]+dataOut.kabxys_integrated[5][:,l,1])*dataOut.rnint2[l]
1840 dataOut.kabxys_integrated[6][:,l,0]=(dataOut.kabxys_integrated[6][:,l,0]+dataOut.kabxys_integrated[6][:,l,1])*dataOut.rnint2[l]
1841 dataOut.kabxys_integrated[7][:,l,0]=(dataOut.kabxys_integrated[7][:,l,0]+dataOut.kabxys_integrated[7][:,l,1])*dataOut.rnint2[l]
1842
1843 dataOut.kabxys_integrated[8][:,l,0]=(dataOut.kabxys_integrated[8][:,l,0]-dataOut.kabxys_integrated[8][:,l,1])*dataOut.rnint2[l]
1844 dataOut.kabxys_integrated[9][:,l,0]=(dataOut.kabxys_integrated[9][:,l,0]-dataOut.kabxys_integrated[9][:,l,1])*dataOut.rnint2[l]
1845 dataOut.kabxys_integrated[10][:,l,0]=(dataOut.kabxys_integrated[10][:,l,0]-dataOut.kabxys_integrated[10][:,l,1])*dataOut.rnint2[l]
1846 dataOut.kabxys_integrated[11][:,l,0]=(dataOut.kabxys_integrated[11][:,l,0]-dataOut.kabxys_integrated[11][:,l,1])*dataOut.rnint2[l]
1847
1848
1849 #print("Final Integration: ",dataOut.kabxys_integrated[4][:,l,0])
1850
1851
1852
1853
1854
1855
1856 return dataOut
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866 class BadHeights_Old(Operation):
1867 def __init__(self, **kwargs):
1868
1869 Operation.__init__(self, **kwargs)
1870
1871
1872
1873 def run(self,dataOut):
1874
1875
1876 if dataOut.AUX==1:
1877 dataOut.ibad=numpy.zeros((dataOut.header[15][0],dataOut.header[17][0]),'int32')
1878
1879 for j in range(dataOut.header[15][0]):
1880 for l in range(dataOut.header[17][0]):
1881 ip1=j+dataOut.header[15][0]*(0+2*l)
1882 if( (dataOut.kabxys_integrated[5][j,l,0] <= 0.) or (dataOut.kabxys_integrated[4][j,l,0] <= 0.) or (dataOut.kabxys_integrated[7][j,l,0] <= 0.) or (dataOut.kabxys_integrated[6][j,l,0] <= 0.)):
1883 dataOut.ibad[j][l]=1
1884 else:
1885 dataOut.ibad[j][l]=0
1886 #print("ibad: ",dataOut.ibad)
1887
1888
1889
1890 return dataOut
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907 class NoisePower_old(Operation):
1908 def __init__(self, **kwargs):
1909
1910 Operation.__init__(self, **kwargs)
1911
1912 def hildebrand(self,dataOut,data):
1913 #print("data ",data )
1914 divider=10 # divider was originally 10
1915 noise=0.0
1916 n1=0
1917 n2=int(dataOut.header[15][0]/2)
1918 sorts= sorted(data)
1919
1920 nums_min= dataOut.header[15][0]/divider
1921 if((dataOut.header[15][0]/divider)> 2):
1922 nums_min= int(dataOut.header[15][0]/divider)
1923 else:
1924 nums_min=2
1925 sump=0.0
1926 sumq=0.0
1927 j=0
1928 cont=1
1929 while( (cont==1) and (j<n2)):
1930 sump+=sorts[j+n1]
1931 sumq+= sorts[j+n1]*sorts[j+n1]
1932 t3= sump/(j+1)
1933 j=j+1
1934 if(j> nums_min):
1935 rtest= float(j/(j-1)) +1.0/dataOut.header[7][0]
1936 t1= (sumq*j)
1937 t2=(rtest*sump*sump)
1938 if( (t1/t2) > 0.990):
1939 j=j-1
1940 sump-= sorts[j+n1]
1941 sumq-=sorts[j+n1]*sorts[j+n1]
1942 cont= 0
1943
1944 noise= sump/j
1945 stdv=numpy.sqrt((sumq- noise*noise)/(j-1))
1946 return noise
1947
1948 def run(self,dataOut):
1949
1950 if dataOut.AUX==1:
1951
1952 #print("ax2 shape ",ax2.shape)
1953 p=numpy.zeros((dataOut.header[2][0],dataOut.header[15][0],dataOut.header[17][0]),'float32')
1954 av=numpy.zeros(dataOut.header[15][0],'float32')
1955 dataOut.pnoise=numpy.zeros(dataOut.header[2][0],'float32')
1956
1957 p[0,:,:]=dataOut.kabxys_integrated[4][:,:,0]+dataOut.kabxys_integrated[5][:,:,0] #total power for channel 0, just pulse with non-flip
1958 p[1,:,:]=dataOut.kabxys_integrated[6][:,:,0]+dataOut.kabxys_integrated[7][:,:,0] #total power for channel 1
1959
1960 #print("p[0,:,:] ",p[0,:,:])
1961 #print("p[1,:,:] ",p[1,:,:])
1962
1963 for i in range(dataOut.header[2][0]):
1964 dataOut.pnoise[i]=0.0
1965 for k in range(dataOut.header[17][0]):
1966 dataOut.pnoise[i]+= self.hildebrand(dataOut,p[i,:,k])
1967 #print("dpl ",k, "pnoise[",i,"] ",pnoise[i] )
1968 dataOut.pnoise[i]=dataOut.pnoise[i]/dataOut.header[17][0]
1969
1970
1971 #print("POWERNOISE: ",dataOut.pnoise)
1972 dataOut.pan=1.0*dataOut.pnoise[0] # weights could change
1973 dataOut.pbn=1.0*dataOut.pnoise[1] # weights could change
1974 #print("dataOut.pan ",dataOut.pan, " dataOut.pbn ",dataOut.pbn)
1975 #print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa")
1976
1977 #print("POWERNOISE: ",dataOut.pnoise)
1978
1979
1980 return dataOut
1981
1982
1983
1984
1985
1986
1987
1988
1989 class double_pulse_ACFs(Operation):
1990 def __init__(self, **kwargs):
1991
1992 Operation.__init__(self, **kwargs)
1993 self.aux=1
1994
1995 def run(self,dataOut):
1996 dataOut.pairsList=None
1997 if dataOut.AUX==1:
1998 dataOut.igcej=numpy.zeros((dataOut.header[15][0],dataOut.header[17][0]),'int32')
1999
2000 if self.aux==1:
2001 dataOut.rhor=numpy.zeros((dataOut.header[15][0],dataOut.header[17][0]), dtype=float)
2002 dataOut.rhoi=numpy.zeros((dataOut.header[15][0],dataOut.header[17][0]), dtype=float)
2003 dataOut.sdp=numpy.zeros((dataOut.header[15][0],dataOut.header[17][0]), dtype=float)
2004 dataOut.sd=numpy.zeros((dataOut.header[15][0],dataOut.header[17][0]), dtype=float)
2005 #dataOut.igcej=numpy.zeros((dataOut.NDP,dataOut.nlags_array),'int32')
2006 dataOut.p=numpy.zeros((dataOut.header[15][0],dataOut.header[17][0]), dtype=float)
2007 dataOut.alag=numpy.zeros(dataOut.header[15][0],'float32')
2008 for l in range(dataOut.header[17][0]):
2009 dataOut.alag[l]=l*dataOut.header[10][0]*2.0/150.0
2010 self.aux=0
2011 sn4=dataOut.pan*dataOut.pbn
2012 rhorn=0
2013 rhoin=0
2014 #p=np.zeros((ndt,dpl), dtype=float)
2015 panrm=numpy.zeros((dataOut.header[15][0],dataOut.header[17][0]), dtype=float)
2016
2017
2018 for i in range(dataOut.header[15][0]):
2019 for j in range(dataOut.header[17][0]):
2020 ################# Total power
2021 pa=numpy.abs(dataOut.kabxys_integrated[4][i,j,0]+dataOut.kabxys_integrated[5][i,j,0])
2022 pb=numpy.abs(dataOut.kabxys_integrated[6][i,j,0]+dataOut.kabxys_integrated[7][i,j,0])
2023 #print("PA",pb)
2024 st4=pa*pb
2025 dataOut.p[i,j]=pa+pb-(dataOut.pan+dataOut.pbn)
2026 dataOut.sdp[i,j]=2*dataOut.rnint2[j]*((pa+pb)*(pa+pb))
2027 ## ACF
2028 rhorp=dataOut.kabxys_integrated[8][i,j,0]+dataOut.kabxys_integrated[11][i,j,0]
2029 rhoip=dataOut.kabxys_integrated[10][i,j,0]-dataOut.kabxys_integrated[9][i,j,0]
2030 if ((pa>dataOut.pan)&(pb>dataOut.pbn)):
2031 #print("dataOut.pnoise[0]: ",dataOut.pnoise[0])
2032 #print("dataOut.pnoise[1]: ",dataOut.pnoise[1])
2033 #print("OKKKKKKKKKKKKKKK")
2034 ss4=numpy.abs((pa-dataOut.pan)*(pb-dataOut.pbn))
2035 #print("ss4: ",ss4)
2036 #print("OKKKKKKKKKKKKKKK")
2037 panrm[i,j]=math.sqrt(ss4)
2038 rnorm=1/panrm[i,j]
2039 #print("rnorm: ",rnorm)get_number_density
2040 #print("OKKKKKKKKKKKKKKK")
2041
2042 ## ACF
2043 dataOut.rhor[i,j]=rhorp*rnorm
2044 dataOut.rhoi[i,j]=rhoip*rnorm
2045 #print("rhoi: ",dataOut.rhoi)
2046 #print("OKKKKKKKKKKKKKKK")
2047 ############# Compute standard error for ACF
2048 stoss4=st4/ss4
2049 snoss4=sn4/ss4
2050 rp2=((rhorp*rhorp)+(rhoip*rhoip))/st4
2051 rn2=((rhorn*rhorn)+(rhoin*rhoin))/sn4
2052 rs2=(dataOut.rhor[i,j]*dataOut.rhor[i,j])+(dataOut.rhoi[i,j]*dataOut.rhoi[i,j])
2053 st=1.0+rs2*(stoss4-(2*math.sqrt(stoss4*snoss4)))
2054 stn=1.0+rs2*(snoss4-(2*math.sqrt(stoss4*snoss4)))
2055 dataOut.sd[i,j]=((stoss4*((1.0+rp2)*st+(2.0*rp2*rs2*snoss4)-4.0*math.sqrt(rs2*rp2)))+(0.25*snoss4*((1.0+rn2)*stn+(2.0*rn2*rs2*stoss4)-4.0*math.sqrt(rs2*rn2))))*dataOut.rnint2[j]
2056 dataOut.sd[i,j]=numpy.abs(dataOut.sd[i,j])
2057 #print("sd: ",dataOut.sd)
2058 #print("OKKKKKKKKKKKKKKK")
2059 else: #default values for bad points
2060 rnorm=1/math.sqrt(st4)
2061 dataOut.sd[i,j]=1.e30
2062 dataOut.ibad[i,j]=4
2063 dataOut.rhor[i,j]=rhorp*rnorm
2064 dataOut.rhoi[i,j]=rhoip*rnorm
2065 if ((pa/dataOut.pan-1.0)>2.25*(pb/dataOut.pbn-1.0)):
2066 dataOut.igcej[i,j]=1
2067
2068 #print("sdp",dataOut.sdp)
2069
2070 return dataOut
2071
2072
2073
2074
2075
2076
2077
2078 class faraday_angle_and_power_double_pulse(Operation):
2079 def __init__(self, **kwargs):
2080
2081 Operation.__init__(self, **kwargs)
2082 self.aux=1
2083
2084 def run(self,dataOut):
2085 #dataOut.NRANGE=NRANGE
2086 #dataOut.H0=H0
2087 ######### H0 Y NRANGE SON PARAMETROS?
2088
2089 if dataOut.AUX==1:
2090 if self.aux==1:
2091 dataOut.h2=numpy.zeros(dataOut.header[15][0],'float32')
2092 dataOut.range1=numpy.zeros(dataOut.header[15][0],order='F',dtype='float32')
2093 dataOut.sdn2=numpy.zeros(dataOut.header[15][0],'float32')
2094 dataOut.ph2=numpy.zeros(dataOut.header[15][0],'float32')
2095 dataOut.sdp2=numpy.zeros(dataOut.header[15][0],'float32')
2096 dataOut.ibd=numpy.zeros(dataOut.header[15][0],'float32')
2097 dataOut.phi=numpy.zeros(dataOut.header[15][0],'float32')
2098 self.aux=0
2099 #print("p: ",dataOut.p)
2100
2101
2102 for i in range(dataOut.header[15][0]):
2103 dataOut.range1[i]=dataOut.header[9][0] + i*dataOut.header[10][0] # (float) header.h0 + (float)i * header.dh
2104 dataOut.h2[i]=dataOut.range1[i]**2
2105
2106 #print("sd: ",dataOut.sd)
2107 #print("OIKKKKKKKKKKKKKKK")
2108 #print("ibad: ",dataOut.ibad)
2109 #print("igcej: ",dataOut.igcej)
2110 for j in range(dataOut.header[15][0]):
2111 dataOut.ph2[j]=0.
2112 dataOut.sdp2[j]=0.
2113 ri=dataOut.rhoi[j][0]/dataOut.sd[j][0]
2114 rr=dataOut.rhor[j][0]/dataOut.sd[j][0]
2115 dataOut.sdn2[j]=1./dataOut.sd[j][0]
2116 #print("sdn2: ",dataOut.sdn2)
2117 #print("OIKKKKKKKKKKKKKKK")
2118 pt=0.# // total power
2119 st=0.# // total signal
2120 ibt=0# // bad lags
2121 ns=0# // no. good lags
2122 for l in range(dataOut.header[17][0]):
2123 #add in other lags if outside of e-jet contamination
2124 if( (dataOut.igcej[j][l] == 0) and (dataOut.ibad[j][l] == 0) ):
2125 #print("dataOut.p[j][l]: ",dataOut.p[j][l])
2126 dataOut.ph2[j]+=dataOut.p[j][l]/dataOut.sdp[j][l]
2127 dataOut.sdp2[j]=dataOut.sdp2[j]+1./dataOut.sdp[j][l]
2128 ns+=1
2129
2130 pt+=dataOut.p[j][l]/dataOut.sdp[j][l]
2131 st+=1./dataOut.sdp[j][l]
2132 ibt|=dataOut.ibad[j][l];
2133 #print("pt: ",pt)
2134 #print("st: ",st)
2135 if(ns!= 0):
2136 dataOut.ibd[j]=0
2137 dataOut.ph2[j]=dataOut.ph2[j]/dataOut.sdp2[j]
2138 dataOut.sdp2[j]=1./dataOut.sdp2[j]
2139 else:
2140 dataOut.ibd[j]=ibt
2141 dataOut.ph2[j]=pt/st
2142 #print("ph2: ",dataOut.ph2)
2143 dataOut.sdp2[j]=1./st
2144 #print("ph2: ",dataOut.ph2)
2145 dataOut.ph2[j]=dataOut.ph2[j]*dataOut.h2[j]
2146 dataOut.sdp2[j]=numpy.sqrt(dataOut.sdp2[j])*dataOut.h2[j]
2147 rr=rr/dataOut.sdn2[j]
2148 ri=ri/dataOut.sdn2[j]
2149 #rm[j]=np.sqrt(rr*rr + ri*ri) it is not used in c program
2150 dataOut.sdn2[j]=1./(dataOut.sdn2[j]*(rr*rr + ri*ri))
2151 if( (ri == 0.) and (rr == 0.) ):
2152 dataOut.phi[j]=0.
2153 else:
2154 dataOut.phi[j]=math.atan2( ri , rr )
2155
2156 #print("ph2: ",dataOut.ph2)
2157 #print("sdp2: ",dataOut.sdp2)
2158 #print("sdn2",dataOut.sdn2)
2159
2160
2161 return dataOut
2162
2163
2164
2165
2166
2167
2168 class get_number_density(Operation):
2169 def __init__(self, **kwargs):
2170
2171 Operation.__init__(self, **kwargs)
2172 self.aux=1
2173
2174 def run(self,dataOut,NSHTS=None,RATE=None):
2175 dataOut.NSHTS=NSHTS
2176 dataOut.RATE=RATE
2177 if dataOut.AUX==1:
2178 #dataOut.TimeBlockSeconds=time.mktime(time.strptime(dataOut.TimeBlockDate))
2179 #dataOut.bd_time=time.gmtime(dataOut.TimeBlockSeconds)
2180 #dataOut.ut=dataOut.bd_time.tm_hour+dataOut.bd_time.tm_min/60.0+dataOut.bd_time.tm_sec/3600.0
2181 if self.aux==1:
2182 dataOut.dphi=numpy.zeros(dataOut.header[15][0],'float32')
2183 dataOut.sdn1=numpy.zeros(dataOut.header[15][0],'float32')
2184 self.aux=0
2185 theta=numpy.zeros(dataOut.header[15][0],dtype=numpy.complex_)
2186 thetai=numpy.zeros(dataOut.header[15][0],dtype=numpy.complex_)
2187 # use complex numbers for phase
2188 for i in range(dataOut.NSHTS):
2189 theta[i]=math.cos(dataOut.phi[i])+math.sin(dataOut.phi[i])*1j
2190 thetai[i]=-math.sin(dataOut.phi[i])+math.cos(dataOut.phi[i])*1j
2191
2192 # differentiate and convert to number density
2193 ndphi=dataOut.NSHTS-4
2194 #print("dataOut.dphiBEFORE: ",dataOut.dphi)
2195 for i in range(2,dataOut.NSHTS-2):
2196 fact=(-0.5/(dataOut.RATE*dataOut.header[10][0]))*dataOut.bki[i]
2197 #four-point derivative, no phase unwrapping necessary
2198 dataOut.dphi[i]=((((theta[i+1]-theta[i-1])+(2.0*(theta[i+2]-theta[i-2])))/thetai[i])).real/10.0
2199 #print("dataOut.dphi[i]AFTER: ",dataOut.dphi[i])
2200 dataOut.dphi[i]=abs(dataOut.dphi[i]*fact)
2201 dataOut.sdn1[i]=(4.*(dataOut.sdn2[i-2]+dataOut.sdn2[i+2])+dataOut.sdn2[i-1]+dataOut.sdn2[i+1])
2202 dataOut.sdn1[i]=numpy.sqrt(dataOut.sdn1[i])*fact
2203 '''
2204 #print("date: ",dataOut.TimeBlockDate)
2205 #print("CurrentBlock: ", dataOut.CurrentBlock)
2206 #print("NSHTS: ",dataOut.NSHTS)
2207 print("phi: ",dataOut.phi)
2208 #print("header[10][0]: ",dataOut.DH)
2209 print("bkibki: ",dataOut.bki)
2210 #print("RATE: ",dataOut.RATE)
2211 print("sdn2: ",dataOut.sdn2)
2212 print("dphi: ",dataOut.dphi)
2213 print("sdn1: ",dataOut.sdn1)
2214 print("ph2: ",dataOut.ph2)
2215 print("sdp2: ",dataOut.sdp2)
2216 print("sdn1: ",dataOut.sdn1)
2217 '''
2218
2219 '''
2220 Al finallllllllllllllllllllllllllllllllllllllllllllllllllllllllll
2221 for i in range(dataOut.NSHTS):
2222 dataOut.ph2[i]=(max(1.0, dataOut.ph2[i]))
2223 dataOut.dphi[i]=(max(1.0, dataOut.dphi[i]))
2224 #print("dphi ",dphi)
2225 # threshold - values less than 10⁴
2226 for i in range(dataOut.NSHTS):
2227 if dataOut.ph2[i]<10000:
2228 dataOut.ph2[i]=10000
2229
2230 # threshold values more than 10⁷
2231 for i in range(dataOut.NSHTS):
2232 if dataOut.ph2[i]>10000000:#
2233 dataOut.ph2[i]=10000000
2234
2235 ## filter for errors
2236 for i in range(dataOut.NSHTS):
2237 if dataOut.sdp2[i]>100000:#
2238 dataOut.ph2[i]=10000
2239 '''
2240
2241
2242
2243
2244 return dataOut
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256 class normalize_dp_power2(Operation):
2257 def __init__(self, **kwargs):
2258
2259 Operation.__init__(self, **kwargs)
2260 self.aux=1
2261
2262 def normal(self,a,b,n,m):
2263 chmin=1.0e30
2264 chisq=numpy.zeros(150,'float32')
2265 temp=numpy.zeros(150,'float32')
2266
2267 for i in range(2*m-1):
2268 an=al=be=chisq[i]=0.0
2269 for j in range(int(n/m)):
2270 k=int(j+i*n/(2*m))
2271 if(a[k]>0.0 and b[k]>0.0):
2272 al+=a[k]*b[k]
2273 be+=b[k]*b[k]
2274
2275 if(be>0.0):
2276 temp[i]=al/be
2277 else:
2278 temp[i]=1.0
2279
2280 for j in range(int(n/m)):
2281 k=int(j+i*n/(2*m))
2282 if(a[k]>0.0 and b[k]>0.0):
2283 chisq[i]+=(numpy.log10(b[k]*temp[i]/a[k]))**2
2284 an=an+1
2285
2286 if(chisq[i]>0.0):
2287 chisq[i]/=an
2288
2289
2290 for i in range(int(2*m-1)):
2291 if(chisq[i]<chmin and chisq[i]>1.0e-6):
2292 chmin=chisq[i]
2293 cf=temp[i]
2294 return cf
2295
2296
2297
2298 def run(self,dataOut,cut0=None,cut1=None):
2299 dataOut.cut0=float(cut0)
2300 dataOut.cut1=float(cut1)
2301 if dataOut.AUX==1:
2302 #print("dateBefore: ",dataOut.TimeBlockDate_for_dp_power)
2303 #print("dateNow: ",dataOut.TimeBlockDate)
2304 if self.aux==1:
2305 dataOut.cf=numpy.zeros(1,'float32')
2306 dataOut.cflast=numpy.zeros(1,'float32')
2307 self.aux=0
2308
2309 night_first=300.0
2310 night_first1= 310.0
2311 night_end= 450.0
2312 day_first=250.0
2313 day_end=400.0
2314 day_first_sunrise=190.0
2315 day_end_sunrise=280.0
2316
2317 if(dataOut.ut>4.0 and dataOut.ut<11.0): #early
2318 i2=(night_end-dataOut.range1[0])/dataOut.header[10][0]
2319 i1=(night_first -dataOut.range1[0])/dataOut.header[10][0]
2320 elif (dataOut.ut>0.0 and dataOut.ut<4.0): #night
2321 i2=(night_end-dataOut.range1[0])/dataOut.header[10][0]
2322 i1=(night_first1 -dataOut.range1[0])/dataOut.header[10][0]
2323 elif (dataOut.ut>=11.0 and dataOut.ut<13.5): #sunrise
2324 i2=( day_end_sunrise-dataOut.range1[0])/dataOut.header[10][0]
2325 i1=(day_first_sunrise - dataOut.range1[0])/dataOut.header[10][0]
2326 else:
2327 i2=(day_end-dataOut.range1[0])/dataOut.header[10][0]
2328 i1=(day_first -dataOut.range1[0])/dataOut.header[10][0]
2329
2330 i1=int(i1)
2331 i2=int(i2)
2332 #print("ph2: ",dataOut.ph2)
2333 dataOut.cf=self.normal(dataOut.dphi[i1::], dataOut.ph2[i1::], i2-i1, 1)
2334
2335 #print("n in:",i1,"(",dataOut.range1[i1],"), i2=",i2,"(",dataOut.range1[i2],"), ut=",dataOut.ut,", cf=",dataOut.cf,", cf_last=",
2336 #dataOut.cflast)
2337 # in case of spread F, normalize much higher
2338 if(dataOut.cf<dataOut.cflast[0]/10.0):
2339 i1=(night_first1+100.-dataOut.range1[0])/dataOut.header[10][0]
2340 i2=(night_end+100.0-dataOut.range1[0])/dataOut.header[10][0]
2341 i1=int(i1)
2342 i2=int(i2)
2343 #print("normal over: ",i1,"(",dataOut.range1[i1],") ",i2,"(",dataOut.range1[i2],") => cf: ",dataOut.cf," cflast: ", dataOut.cflast)
2344 dataOut.cf=self.normal(dataOut.dphi[int(i1)::], dataOut.ph2[int(i1)::], int(i2-i1), 1)
2345 dataOut.cf=dataOut.cflast[0]
2346
2347 #print(">>>i1=",i1,"(",dataOut.range1[i1],"), i2=",i2,"(",dataOut.range1[i2],"), ut=",dataOut.ut,", cf=",dataOut.cf,", cf_last=",
2348 # dataOut.cflast," (",dataOut.cf/dataOut.cflast,"), cut=",dataOut.cut0," ",dataOut.cut1)
2349 dataOut.cflast[0]=dataOut.cf
2350
2351 ## normalize double pulse power and error bars to Faraday
2352 for i in range(dataOut.NSHTS):
2353 dataOut.ph2[i]*=dataOut.cf
2354 dataOut.sdp2[i]*=dataOut.cf
2355 #print("******* correction factor: ",dataOut.cf)
2356
2357 #print(dataOut.ph2)
2358
2359 for i in range(dataOut.NSHTS):
2360 dataOut.ph2[i]=(max(1.0, dataOut.ph2[i]))
2361 dataOut.dphi[i]=(max(1.0, dataOut.dphi[i]))
2362 #print("dphi ",dphi)
2363 # threshold - values less than 10⁴
2364
2365 '''
2366 for i in range(dataOut.NSHTS):
2367 if dataOut.ph2[i]<10000:
2368 dataOut.ph2[i]=10000
2369
2370 # threshold values more than 10⁷
2371 for i in range(dataOut.NSHTS):
2372 if dataOut.ph2[i]>10000000:#
2373 dataOut.ph2[i]=10000000
2374
2375 ## filter for errors
2376 for i in range(dataOut.NSHTS):
2377 if dataOut.sdp2[i]>100000:#
2378 dataOut.ph2[i]=10000
2379 '''
2380
2381
2382
2383
2384
2385 '''
2386 #print("date: ",dataOut.TimeBlockDate)
2387 #print("CurrentBlock: ", dataOut.CurrentBlock)
2388 #print("NSHTS: ",dataOut.NSHTS)
2389 print("phi: ",dataOut.phi)
2390 #print("header[10][0]: ",dataOut.DH)
2391 print("bkibki: ",dataOut.bki)
2392 #print("RATE: ",dataOut.RATE)
2393 print("sdn2: ",dataOut.sdn2)
2394 print("dphi: ",dataOut.dphi)
2395 print("sdn1: ",dataOut.sdn1)
2396 print("ph2: ",dataOut.ph2)
2397 print("sdp2: ",dataOut.sdp2)
2398 print("sdn1: ",dataOut.sdn1)
2399 '''
2400
2401
2402
2403
2404
2405
2406
2407 return dataOut
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423 '''
2424 from ctypes import *
2425 class IDATE(Structure):
2426 _fields_ = [
2427 ("year", c_int),
2428 ("moda", c_int),
2429 ("hrmn", c_int),
2430 ("sec", c_int),
2431 ("secs", c_int),
2432 ]
2433 #typedef struct IDATE {int year,moda,hrmn,sec,secs;} idate;
2434 '''
2435
2436
2437
2438
2439 '''
2440 class get_number_density(Operation):
2441 def __init__(self, **kwargs):
2442
2443 Operation.__init__(self, **kwargs)
2444
2445 #self.aux=1
2446 '''
2447
2448 '''
2449 def IDATE(Structure):
2450
2451 _fields_ = [
2452 ("year", c_int),
2453 ("moda", c_int),
2454 ("hrmn", c_int),
2455 ("sec", c_int),
2456 ("secs", c_int),
2457 ]
2458
2459 '''
2460
2461
2462
2463
2464 '''
2465 def run(self,dataOut):
2466 '''
2467 '''
2468 if dataOut.CurrentBlock==1 and self.aux==1:
2469
2470 #print("CurrentBlock: ",dataOut.CurrentBlock)
2471
2472 dataOut.TimeBlockSeconds=time.mktime(time.strptime(dataOut.TimeBlockDate))
2473 #print("time1: ",dataOut.TimeBlockSeconds)
2474
2475 #print("date: ",dataOut.TimeBlockDate)
2476 dataOut.bd_time=time.gmtime(dataOut.TimeBlockSeconds)
2477 #print("bd_time: ",dataOut.bd_time)
2478 dataOut.year=dataOut.bd_time.tm_year+(dataOut.bd_time.tm_yday-1)/364.0
2479 #print("year: ",dataOut.year)
2480 dataOut.ut=dataOut.bd_time.tm_hour+dataOut.bd_time.tm_min/60.0+dataOut.bd_time.tm_sec/3600.0
2481 #print("ut: ",dataOut.ut)
2482 self.aux=0
2483
2484
2485
2486
2487 '''
2488 #print("CurrentBlock: ",dataOut.CurrentBlock)
2489 #print("date: ",dataOut.firsttime)
2490 #print("bd_time: ",time.strptime(dataOut.datatime.ctime()))
2491 #mkfact_short.mkfact(year,h,bfm,thb,bki,dataOut.NDP)
2492 #print("CurrentBlock: ",dataOut.CurrentBlock)
2493 '''
2494 if dataOut.AUX==1:
2495 '''
2496 '''
2497 #begin=IDATE()
2498 #begin.year=dataOut.bd_time.tm_year
2499 #begin.moda=100*(dataOut.bd_time.tm_mon)+dataOut.bd_time.tm_mday
2500 #begin.hrmn=100*dataOut.bd_time.tm_hour+dataOut.bd_time.tm_min
2501 #begin.sec=dataOut.bd_time.tm_sec
2502 #begin.secs=dataOut.bd_time.tm_sec+60*(dataOut.bd_time.tm_min+60*(dataOut.bd_time.tm_hour+24*(dataOut.bd_time.tm_yday-1)))
2503 h=numpy.arange(0.0,15.0*dataOut.NDP,15.0,dtype='float32')
2504 bfm=numpy.zeros(dataOut.NDP,dtype='float32')
2505 bfm=numpy.array(bfm,order='F')
2506 thb=numpy.zeros(dataOut.NDP,dtype='float32')
2507 thb=numpy.array(thb,order='F')
2508 bki=numpy.zeros(dataOut.NDP,dtype='float32')
2509 bki=numpy.array(thb,order='F')
2510 #yearmanually=2019.9285714285713
2511 #print("year manually: ",yearmanually)
2512 #print("year: ",dataOut.year)
2513 mkfact_short.mkfact(dataOut.year,h,bfm,thb,bki,dataOut.NDP)
2514 #print("tm ",tm)
2515 '''
2516 '''
2517 print("year ",dataOut.year)
2518 print("h ", dataOut.h)
2519 print("bfm ", dataOut.bfm)
2520 print("thb ", dataOut.thb)
2521 print("bki ", dataOut.bki)
2522 '''
2523
2524
2525
2526
2527 '''
2528 print("CurrentBlock: ",dataOut.CurrentBlock)
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539 return dataOut
2540 '''
2541
2542
2543
2544
2545
2546
2547 class test(Operation):
2548 def __init__(self, **kwargs):
2549
2550 Operation.__init__(self, **kwargs)
2551
2552
2553
2554
2555 def run(self,dataOut,tt=10):
2556
2557 print("tt: ",tt)
2558
2559
2560
2561 return dataOut
@@ -76,6 +76,8 def hildebrand_sekhon(data, navg):
76 76 """
77 77
78 78 sortdata = numpy.sort(data, axis=None)
79 #print(numpy.shape(data))
80 #exit()
79 81 '''
80 82 lenOfData = len(sortdata)
81 83 nums_min = lenOfData*0.2
@@ -273,13 +275,13 class JROData(GenericData):
273 275 '''
274 276 '''
275 277 return self.radarControllerHeaderObj.ippSeconds
276
278
277 279 @ippSeconds.setter
278 280 def ippSeconds(self, ippSeconds):
279 281 '''
280 282 '''
281 283 self.radarControllerHeaderObj.ippSeconds = ippSeconds
282
284
283 285 @property
284 286 def code(self):
285 287 '''
@@ -370,7 +372,7 class Voltage(JROData):
370 372 self.flagShiftFFT = False
371 373 self.flagDataAsBlock = False # Asumo que la data es leida perfil a perfil
372 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 376 'code', 'nCode', 'nBaud', 'ippSeconds', 'ipp']
375 377
376 378 def getNoisebyHildebrand(self, channel=None):
@@ -428,6 +430,103 class Voltage(JROData):
428 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 530 class Spectra(JROData):
432 531
433 532 def __init__(self):
@@ -461,7 +560,7 class Spectra(JROData):
461 560 self.ippFactor = 1
462 561 self.beacon_heiIndexList = []
463 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 564 'code', 'nCode', 'nBaud', 'ippSeconds', 'ipp','nIncohInt', 'nFFTPoints', 'nProfiles']
466 565
467 566 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
@@ -611,7 +710,7 class Spectra(JROData):
611 710 print("This property should not be initialized")
612 711
613 712 return
614
713
615 714 noise = property(getNoise, setValue, "I'm the 'nHeights' property.")
616 715
617 716
@@ -708,7 +807,7 class Fits(JROData):
708 807 return self.ipp_sec
709 808
710 809 noise = property(getNoise, "I'm the 'nHeights' property.")
711
810
712 811
713 812 class Correlation(JROData):
714 813
@@ -889,6 +988,7 class Parameters(Spectra):
889 988 else:
890 989 return self.paramInterval
891 990
991
892 992 def setValue(self, value):
893 993
894 994 print("This property should not be initialized")
@@ -137,7 +137,7 class BasicHeader(Header):
137 137 timeZone = None
138 138 dstFlag = None
139 139 errorCount = None
140 datatime = None
140 F = None
141 141 structure = BASIC_STRUCTURE
142 142 __LOCALTIME = None
143 143
@@ -363,6 +363,7 class RadarControllerHeader(Header):
363 363 self.expType = int(header['nExpType'][0])
364 364 self.nTx = int(header['nNTx'][0])
365 365 self.ipp = float(header['fIpp'][0])
366 #print(self.ipp)
366 367 self.txA = float(header['fTxA'][0])
367 368 self.txB = float(header['fTxB'][0])
368 369 self.nWindows = int(header['nNumWindows'][0])
@@ -534,6 +535,7 class RadarControllerHeader(Header):
534 535 def get_ippSeconds(self):
535 536 '''
536 537 '''
538
537 539 ippSeconds = 2.0 * 1000 * self.ipp / SPEED_OF_LIGHT
538 540
539 541 return ippSeconds
@@ -640,6 +642,7 class ProcessingHeader(Header):
640 642 self.nWindows = int(header['nNumWindows'][0])
641 643 self.processFlags = header['nProcessFlags']
642 644 self.nCohInt = int(header['nCoherentIntegrations'][0])
645
643 646 self.nIncohInt = int(header['nIncoherentIntegrations'][0])
644 647 self.totalSpectra = int(header['nTotalSpectra'][0])
645 648
@@ -903,4 +906,4 def get_procflag_dtype(index):
903 906
904 907 def get_dtype_width(index):
905 908
906 return DTYPE_WIDTH[index] No newline at end of file
909 return DTYPE_WIDTH[index]
@@ -3,3 +3,4 from .jroplot_spectra import *
3 3 from .jroplot_heispectra import *
4 4 from .jroplot_correlation import *
5 5 from .jroplot_parameters import *
6 from .jroplot_voltage_lags import *
@@ -220,6 +220,10 class Plot(Operation):
220 220 self.zmin = kwargs.get('zmin', None)
221 221 self.zmax = kwargs.get('zmax', None)
222 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 227 self.xmin = kwargs.get('xmin', None)
224 228 self.xmax = kwargs.get('xmax', None)
225 229 self.xrange = kwargs.get('xrange', 12)
@@ -271,7 +275,7 class Plot(Operation):
271 275
272 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 280 if self.width is None:
277 281 self.width = 8
@@ -376,7 +380,7 class Plot(Operation):
376 380 '''
377 381 Set min and max values, labels, ticks and titles
378 382 '''
379
383
380 384 for n, ax in enumerate(self.axes):
381 385 if ax.firsttime:
382 386 if self.xaxis != 'time':
@@ -459,14 +463,14 class Plot(Operation):
459 463
460 464 self.plot()
461 465 self.format()
462
466
463 467 for n, fig in enumerate(self.figures):
464 468 if self.nrows == 0 or self.nplots == 0:
465 469 log.warning('No data', self.name)
466 470 fig.text(0.5, 0.5, 'No Data', fontsize='large', ha='center')
467 471 fig.canvas.manager.set_window_title(self.CODE)
468 472 continue
469
473
470 474 fig.canvas.manager.set_window_title('{} - {}'.format(self.title,
471 475 self.getDateTime(self.data.max_time).strftime('%Y/%m/%d')))
472 476 fig.canvas.draw()
@@ -476,7 +480,7 class Plot(Operation):
476 480
477 481 if self.save:
478 482 self.save_figure(n)
479
483
480 484 if self.server:
481 485 self.send_to_server()
482 486
@@ -523,6 +527,7 class Plot(Operation):
523 527
524 528 figname = os.path.join(
525 529 self.save,
530 self.save_code,
526 531 '{}_{}.png'.format(
527 532 self.save_code,
528 533 self.getDateTime(self.data.min_time).strftime(
@@ -604,7 +609,7 class Plot(Operation):
604 609 self.ncols: number of cols
605 610 self.nplots: number of plots (channels or pairs)
606 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 615 raise NotImplementedError
@@ -631,7 +636,7 class Plot(Operation):
631 636 '''
632 637 Main plotting routine
633 638 '''
634
639
635 640 if self.isConfig is False:
636 641 self.__setup(**kwargs)
637 642
@@ -667,7 +672,7 class Plot(Operation):
667 672 dt = self.getDateTime(tm)
668 673 if self.xmin is None:
669 674 self.tmin = tm
670 self.xmin = dt.hour
675 self.xmin = dt.hour
671 676 minutes = (self.xmin-int(self.xmin)) * 60
672 677 seconds = (minutes - int(minutes)) * 60
673 678 self.tmin = (dt.replace(hour=int(self.xmin), minute=int(minutes), second=int(seconds)) -
@@ -690,4 +695,3 class Plot(Operation):
690 695 self.__plot()
691 696 if self.data and not self.data.flagNoData and self.pause:
692 697 figpause(10)
693
@@ -46,12 +46,24 class DobleGaussianPlot(SpectraPlot):
46 46 # colormap = 'jet'
47 47 # plot_type = 'pcolor'
48 48
49
49 50 class DoubleGaussianSpectraCutPlot(SpectraCutPlot):
50 51 '''
51 52 Plot SpectraCut with Double Gaussian Fit
52 53 '''
53 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 67 class SnrPlot(RTIPlot):
56 68 '''
57 69 Plot for SNR Data
@@ -179,7 +191,7 class GenericRTIPlot(Plot):
179 191 self.nrows = self.data.shape('param')[0]
180 192 self.nplots = self.nrows
181 193 self.plots_adjust.update({'hspace':0.8, 'left': 0.1, 'bottom': 0.08, 'right':0.95, 'top': 0.95})
182
194
183 195 if not self.xlabel:
184 196 self.xlabel = 'Time'
185 197
@@ -367,4 +379,3 class PolarMapPlot(Plot):
367 379 self.save_labels = ['{}-{}'.format(lbl, label) for lbl in self.labels]
368 380 self.titles = ['{} {}'.format(
369 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
@@ -23,6 +23,7 class SpectraPlot(Plot):
23 23 buffering = False
24 24
25 25 def setup(self):
26
26 27 self.nplots = len(self.data.channels)
27 28 self.ncols = int(numpy.sqrt(self.nplots) + 0.9)
28 29 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
@@ -32,7 +33,7 class SpectraPlot(Plot):
32 33 self.width = 4 * self.ncols
33 34 else:
34 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 37 self.ylabel = 'Range [km]'
37 38
38 39 def update(self, dataOut):
@@ -56,6 +57,9 class SpectraPlot(Plot):
56 57 return data, meta
57 58
58 59 def plot(self):
60
61 #print(self.xaxis)
62 #exit(1)
59 63 if self.xaxis == "frequency":
60 64 x = self.data.xrange[0]
61 65 self.xlabel = "Frequency (kHz)"
@@ -78,6 +82,9 class SpectraPlot(Plot):
78 82 data = self.data[-1]
79 83 z = data['spc']
80 84
85 self.CODE2 = 'spc_oblique'
86
87
81 88 for n, ax in enumerate(self.axes):
82 89 noise = data['noise'][n]
83 90 if self.CODE == 'spc_moments':
@@ -91,6 +98,7 class SpectraPlot(Plot):
91 98 self.xmin = self.xmin if self.xmin else -self.xmax
92 99 self.zmin = self.zmin if self.zmin else numpy.nanmin(z)
93 100 self.zmax = self.zmax if self.zmax else numpy.nanmax(z)
101 #print(numpy.shape(x))
94 102 ax.plt = ax.pcolormesh(x, y, z[n].T,
95 103 vmin=self.zmin,
96 104 vmax=self.zmax,
@@ -105,7 +113,6 class SpectraPlot(Plot):
105 113 if self.CODE == 'spc_moments':
106 114 ax.plt_mean = ax.plot(mean, y, color='k', lw=1)[0]
107 115 if self.CODE == 'gaussian_fit':
108 # ax.plt_mean = ax.plot(mean, y, color='k', lw=1)[0]
109 116 ax.plt_gau0 = ax.plot(gau0, y, color='r', lw=1)[0]
110 117 ax.plt_gau1 = ax.plot(gau1, y, color='y', lw=1)[0]
111 118 else:
@@ -116,11 +123,114 class SpectraPlot(Plot):
116 123 if self.CODE == 'spc_moments':
117 124 ax.plt_mean.set_data(mean, y)
118 125 if self.CODE == 'gaussian_fit':
119 # ax.plt_mean.set_data(mean, y)
120 126 ax.plt_gau0.set_data(gau0, y)
121 127 ax.plt_gau1.set_data(gau1, y)
122 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 235 class CrossSpectraPlot(Plot):
126 236
@@ -138,7 +248,7 class CrossSpectraPlot(Plot):
138 248 self.nplots = len(self.data.pairs) * 2
139 249 self.nrows = int((1.0 * self.nplots / self.ncols) + 0.9)
140 250 self.width = 3.1 * self.ncols
141 self.height = 2.6 * self.nrows
251 self.height = 5 * self.nrows
142 252 self.ylabel = 'Range [km]'
143 253 self.showprofile = False
144 254 self.plots_adjust.update({'left': 0.08, 'right': 0.92, 'wspace': 0.5, 'hspace':0.4, 'top':0.95, 'bottom': 0.08})
@@ -177,7 +287,7 class CrossSpectraPlot(Plot):
177 287 else:
178 288 x = self.data.xrange[2]
179 289 self.xlabel = "Velocity (m/s)"
180
290
181 291 self.titles = []
182 292
183 293 y = self.data.yrange
@@ -207,13 +317,339 class CrossSpectraPlot(Plot):
207 317 ax.plt = ax.pcolormesh(x, y, phase.T,
208 318 vmin=-180,
209 319 vmax=180,
210 cmap=plt.get_cmap(self.colormap_phase)
320 cmap=plt.get_cmap(self.colormap_phase)
211 321 )
212 322 else:
213 323 ax.plt.set_array(phase.T.ravel())
214 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 653 class RTIPlot(Plot):
218 654 '''
219 655 Plot for RTI data
@@ -231,7 +667,7 class RTIPlot(Plot):
231 667 self.ylabel = 'Range [km]'
232 668 self.xlabel = 'Time'
233 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 671 self.titles = ['{} Channel {}'.format(
236 672 self.CODE.upper(), x) for x in range(self.nrows)]
237 673
@@ -248,6 +684,78 class RTIPlot(Plot):
248 684 self.x = self.data.times
249 685 self.y = self.data.yrange
250 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 759 self.z = numpy.ma.masked_invalid(self.z)
252 760
253 761 if self.decimation is None:
@@ -335,7 +843,7 class PhasePlot(CoherencePlot):
335 843
336 844 class NoisePlot(Plot):
337 845 '''
338 Plot for noise
846 Plot for noise
339 847 '''
340 848
341 849 CODE = 'noise'
@@ -380,7 +888,10 class NoisePlot(Plot):
380 888 y = Y[ch]
381 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 895 class PowerProfilePlot(Plot):
385 896
386 897 CODE = 'pow_profile'
@@ -415,7 +926,7 class PowerProfilePlot(Plot):
415 926
416 927 if self.xmin is None: self.xmin = numpy.nanmin(x)*0.9
417 928 if self.xmax is None: self.xmax = numpy.nanmax(x)*1.1
418
929
419 930 if self.axes[0].firsttime:
420 931 for ch in self.data.channels:
421 932 self.axes[0].plot(x[ch], y, lw=1, label='Ch{}'.format(ch))
@@ -600,7 +1111,7 class BeaconPhase(Plot):
600 1111 server=None, folder=None, username=None, password=None,
601 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 1115 return dataOut
605 1116
606 1117 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
@@ -740,4 +1251,4 class BeaconPhase(Plot):
740 1251 thisDatetime=thisDatetime,
741 1252 update_figfile=update_figfile)
742 1253
743 return dataOut No newline at end of file
1254 return dataOut
@@ -21,4 +21,9 from .jroIO_mira35c import *
21 21 from .julIO_param import *
22 22
23 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############
@@ -78,6 +78,7 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
78 78 basicHeaderObj = BasicHeader(LOCALTIME)
79 79
80 80 try:
81
81 82 fp = open(filename, 'rb')
82 83 except IOError:
83 84 print("The file %s can't be opened" % (filename))
@@ -140,6 +141,7 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
140 141
141 142 firstBasicHeaderObj = BasicHeader(LOCALTIME)
142 143 systemHeaderObj = SystemHeader()
144
143 145 radarControllerHeaderObj = RadarControllerHeader()
144 146 processingHeaderObj = ProcessingHeader()
145 147
@@ -384,7 +386,7 def isRadarFolder(folder):
384 386
385 387
386 388 def isRadarFile(file):
387 try:
389 try:
388 390 year = int(file[1:5])
389 391 doy = int(file[5:8])
390 392 set = int(file[8:11])
@@ -395,10 +397,10 def isRadarFile(file):
395 397
396 398
397 399 def getDateFromRadarFile(file):
398 try:
400 try:
399 401 year = int(file[1:5])
400 402 doy = int(file[5:8])
401 set = int(file[8:11])
403 set = int(file[8:11])
402 404 except:
403 405 return None
404 406
@@ -417,7 +419,7 def getDateFromRadarFolder(folder):
417 419 return thisDate
418 420
419 421 def parse_format(s, fmt):
420
422
421 423 for i in range(fmt.count('%')):
422 424 x = fmt.index('%')
423 425 d = DT_DIRECTIVES[fmt[x:x+2]]
@@ -484,7 +486,7 class Reader(object):
484 486
485 487 def run(self):
486 488
487 raise NotImplementedError
489 raise NotImplementedError
488 490
489 491 def getAllowedArgs(self):
490 492 if hasattr(self, '__attrs__'):
@@ -496,19 +498,19 class Reader(object):
496 498
497 499 for key, value in kwargs.items():
498 500 setattr(self, key, value)
499
501
500 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 505 for x in os.listdir(f) if os.path.isdir(os.path.join(f, x))]
504 506 folders.sort()
505 507
506 508 if last:
507 509 folders = [folders[-1]]
508 510
509 for folder in folders:
510 try:
511 dt = datetime.datetime.strptime(parse_format(folder, folderfmt), folderfmt).date()
511 for folder in folders:
512 try:
513 dt = datetime.datetime.strptime(parse_format(folder, folderfmt), folderfmt).date()
512 514 if dt >= startDate and dt <= endDate:
513 515 yield os.path.join(path, folder)
514 516 else:
@@ -517,38 +519,44 class Reader(object):
517 519 log.log('Skiping folder {}'.format(folder), self.name)
518 520 continue
519 521 return
520
521 def find_files(self, folders, ext, filefmt, startDate=None, endDate=None,
522
523 def find_files(self, folders, ext, filefmt, startDate=None, endDate=None,
522 524 expLabel='', last=False):
523
524 for path in folders:
525
526 for path in folders:
525 527 files = glob.glob1(path, '*{}'.format(ext))
526 528 files.sort()
527 529 if last:
528 if files:
530 if files:
529 531 fo = files[-1]
530 try:
532 try:
531 533 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
532 yield os.path.join(path, expLabel, fo)
533 except Exception as e:
534 yield os.path.join(path, expLabel, fo)
535 except Exception as e:
534 536 pass
535 537 return
536 538 else:
537 539 return
538 540
539 541 for fo in files:
540 try:
541 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
542 try:
543 dt = datetime.datetime.strptime(parse_format(fo, filefmt), filefmt).date()
544 #print(dt)
545 #print(startDate)
546 #print(endDate)
542 547 if dt >= startDate and dt <= endDate:
548
543 549 yield os.path.join(path, expLabel, fo)
550
544 551 else:
552
545 553 log.log('Skiping file {}'.format(fo), self.name)
546 554 except Exception as e:
547 555 log.log('Skiping file {}'.format(fo), self.name)
548 continue
556 continue
549 557
550 558 def searchFilesOffLine(self, path, startDate, endDate,
551 expLabel, ext, walk,
559 expLabel, ext, walk,
552 560 filefmt, folderfmt):
553 561 """Search files in offline mode for the given arguments
554 562
@@ -561,12 +569,12 class Reader(object):
561 569 path, startDate, endDate, folderfmt)
562 570 else:
563 571 folders = path.split(',')
564
572
565 573 return self.find_files(
566 folders, ext, filefmt, startDate, endDate, expLabel)
574 folders, ext, filefmt, startDate, endDate, expLabel)
567 575
568 576 def searchFilesOnLine(self, path, startDate, endDate,
569 expLabel, ext, walk,
577 expLabel, ext, walk,
570 578 filefmt, folderfmt):
571 579 """Search for the last file of the last folder
572 580
@@ -579,40 +587,54 class Reader(object):
579 587 Return:
580 588 generator with the full path of last filename
581 589 """
582
590
583 591 if walk:
584 592 folders = self.find_folders(
585 593 path, startDate, endDate, folderfmt, last=True)
586 594 else:
587 595 folders = path.split(',')
588
596
589 597 return self.find_files(
590 598 folders, ext, filefmt, startDate, endDate, expLabel, last=True)
591 599
592 600 def setNextFile(self):
593 601 """Set the next file to be readed open it and parse de file header"""
594 602
603 #print("fp: ",self.fp)
595 604 while True:
605
606 #print(self.fp)
596 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 612 if self.online:
600 613 newFile = self.setNextFileOnline()
614
601 615 else:
616
602 617 newFile = self.setNextFileOffline()
603
618
619 #print("newFile: ",newFile)
604 620 if not(newFile):
621
605 622 if self.online:
606 623 raise schainpy.admin.SchainError('Time to wait for new files reach')
607 624 else:
608 625 if self.fileIndex == -1:
626 #print("OKK")
609 627 raise schainpy.admin.SchainWarning('No files found in the given path')
610 628 else:
629
611 630 raise schainpy.admin.SchainWarning('No more files to read')
612
631
613 632 if self.verifyFile(self.filename):
633
614 634 break
615
635
636 ##print("BEFORE OPENING",self.filename)
637
616 638 log.log('Opening file: %s' % self.filename, self.name)
617 639
618 640 self.readFirstHeader()
@@ -625,15 +647,16 class Reader(object):
625 647 self.filename
626 648 self.fp
627 649 self.filesize
628
650
629 651 Return:
630 652 boolean
631 653
632 654 """
655
633 656 nextFile = True
634 657 nextDay = False
635 658
636 for nFiles in range(self.nFiles+1):
659 for nFiles in range(self.nFiles+1):
637 660 for nTries in range(self.nTries):
638 661 fullfilename, filename = self.checkForRealPath(nextFile, nextDay)
639 662 if fullfilename is not None:
@@ -643,18 +666,18 class Reader(object):
643 666 self.name)
644 667 time.sleep(self.delay)
645 668 nextFile = False
646 continue
647
669 continue
670
648 671 if fullfilename is not None:
649 672 break
650
651 self.nTries = 1
652 nextFile = True
673
674 #self.nTries = 1
675 nextFile = True
653 676
654 677 if nFiles == (self.nFiles - 1):
655 678 log.log('Trying with next day...', self.name)
656 679 nextDay = True
657 self.nTries = 3
680 self.nTries = 3
658 681
659 682 if fullfilename:
660 683 self.fileSize = os.path.getsize(fullfilename)
@@ -662,45 +685,48 class Reader(object):
662 685 self.flagIsNewFile = 1
663 686 if self.fp != None:
664 687 self.fp.close()
688 #print(fullfilename)
665 689 self.fp = self.open_file(fullfilename, self.open_mode)
690
666 691 self.flagNoMoreFiles = 0
667 692 self.fileIndex += 1
668 693 return 1
669 else:
694 else:
670 695 return 0
671
696
672 697 def setNextFileOffline(self):
673 698 """Open the next file to be readed in offline mode"""
674
699
675 700 try:
676 701 filename = next(self.filenameList)
677 702 self.fileIndex +=1
678 703 except StopIteration:
679 704 self.flagNoMoreFiles = 1
680 return 0
681
705 return 0
706 #print(self.fileIndex)
707 #print(filename)
682 708 self.filename = filename
683 709 self.fileSize = os.path.getsize(filename)
684 710 self.fp = self.open_file(filename, self.open_mode)
685 711 self.flagIsNewFile = 1
686 712
687 713 return 1
688
714
689 715 @staticmethod
690 716 def isDateTimeInRange(dt, startDate, endDate, startTime, endTime):
691 717 """Check if the given datetime is in range"""
692
718
693 719 if startDate <= dt.date() <= endDate:
694 720 if startTime <= dt.time() <= endTime:
695 721 return True
696 722 return False
697
723
698 724 def verifyFile(self, filename):
699 725 """Check for a valid file
700
726
701 727 Arguments:
702 728 filename -- full path filename
703
729
704 730 Return:
705 731 boolean
706 732 """
@@ -711,10 +737,11 class Reader(object):
711 737 """Check if the next file to be readed exists"""
712 738
713 739 raise NotImplementedError
714
740
715 741 def readFirstHeader(self):
716 742 """Parse the file header"""
717 743
744
718 745 pass
719 746
720 747 def waitDataBlock(self, pointer_location, blocksize=None):
@@ -783,8 +810,8 class JRODataReader(Reader):
783 810 Return:
784 811 str -- fullpath of the file
785 812 """
786
787
813
814
788 815 if nextFile:
789 816 self.set += 1
790 817 if nextDay:
@@ -796,7 +823,15 class JRODataReader(Reader):
796 823 prefixFileList = ['d', 'D']
797 824 elif self.ext.lower() == ".pdata": # spectra
798 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 835 # barrido por las combinaciones posibles
801 836 for prefixDir in prefixDirList:
802 837 thispath = self.path
@@ -816,9 +851,9 class JRODataReader(Reader):
816 851
817 852 if os.path.exists(fullfilename):
818 853 return fullfilename, filename
819
820 return None, filename
821
854
855 return None, filename
856
822 857 def __waitNewBlock(self):
823 858 """
824 859 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
@@ -853,6 +888,7 class JRODataReader(Reader):
853 888 return 0
854 889
855 890 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
891 #print(self.filename)
856 892 time.sleep(self.delay)
857 893
858 894 return 0
@@ -860,9 +896,9 class JRODataReader(Reader):
860 896 def __setNewBlock(self):
861 897
862 898 if self.fp == None:
863 return 0
864
865 if self.flagIsNewFile:
899 return 0
900
901 if self.flagIsNewFile:
866 902 self.lastUTTime = self.basicHeaderObj.utc
867 903 return 1
868 904
@@ -875,12 +911,12 class JRODataReader(Reader):
875 911
876 912 currentSize = self.fileSize - self.fp.tell()
877 913 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
878
914
879 915 if (currentSize >= neededSize):
880 916 self.basicHeaderObj.read(self.fp)
881 917 self.lastUTTime = self.basicHeaderObj.utc
882 918 return 1
883
919
884 920 if self.__waitNewBlock():
885 921 self.lastUTTime = self.basicHeaderObj.utc
886 922 return 1
@@ -921,6 +957,10 class JRODataReader(Reader):
921 957 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
922 958 self.processingHeaderObj.dataBlocksPerFile,
923 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 964 return 1
925 965
926 966 def readFirstHeader(self):
@@ -966,10 +1006,10 class JRODataReader(Reader):
966 1006 except IOError:
967 1007 log.error("File {} can't be opened".format(filename), self.name)
968 1008 return False
969
1009
970 1010 if self.online and self.waitDataBlock(0):
971 1011 pass
972
1012
973 1013 basicHeaderObj = BasicHeader(LOCALTIME)
974 1014 systemHeaderObj = SystemHeader()
975 1015 radarControllerHeaderObj = RadarControllerHeader()
@@ -996,7 +1036,7 class JRODataReader(Reader):
996 1036 dt2 = basicHeaderObj.datatime
997 1037 if not self.isDateTimeInRange(dt1, self.startDate, self.endDate, self.startTime, self.endTime) and not \
998 1038 self.isDateTimeInRange(dt2, self.startDate, self.endDate, self.startTime, self.endTime):
999 flag = False
1039 flag = False
1000 1040
1001 1041 fp.close()
1002 1042 return flag
@@ -1105,11 +1145,11 class JRODataReader(Reader):
1105 1145 return dateList
1106 1146
1107 1147 def setup(self, **kwargs):
1108
1148
1109 1149 self.set_kwargs(**kwargs)
1110 1150 if not self.ext.startswith('.'):
1111 1151 self.ext = '.{}'.format(self.ext)
1112
1152
1113 1153 if self.server is not None:
1114 1154 if 'tcp://' in self.server:
1115 1155 address = server
@@ -1131,36 +1171,36 class JRODataReader(Reader):
1131 1171
1132 1172 for nTries in range(self.nTries):
1133 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 1175 self.filefmt, self.folderfmt)
1136 1176
1137 1177 try:
1138 1178 fullpath = next(fullpath)
1139 1179 except:
1140 1180 fullpath = None
1141
1181
1142 1182 if fullpath:
1143 1183 break
1144 1184
1145 1185 log.warning(
1146 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 1188 self.name)
1149 1189 time.sleep(self.delay)
1150 1190
1151 1191 if not(fullpath):
1152 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 1195 pathname, filename = os.path.split(fullpath)
1156 1196 self.year = int(filename[1:5])
1157 1197 self.doy = int(filename[5:8])
1158 self.set = int(filename[8:11]) - 1
1198 self.set = int(filename[8:11]) - 1
1159 1199 else:
1160 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 1202 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
1163
1203
1164 1204 self.setNextFile()
1165 1205
1166 1206 return
@@ -1181,7 +1221,7 class JRODataReader(Reader):
1181 1221 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1182 1222
1183 1223 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1184
1224
1185 1225 def getFirstHeader(self):
1186 1226
1187 1227 raise NotImplementedError
@@ -1214,8 +1254,8 class JRODataReader(Reader):
1214 1254 """
1215 1255
1216 1256 Arguments:
1217 path :
1218 startDate :
1257 path :
1258 startDate :
1219 1259 endDate :
1220 1260 startTime :
1221 1261 endTime :
@@ -1284,7 +1324,7 class JRODataWriter(Reader):
1284 1324 dtype_width = get_dtype_width(dtype_index)
1285 1325
1286 1326 return dtype_width
1287
1327
1288 1328 def getProcessFlags(self):
1289 1329
1290 1330 processFlags = 0
@@ -1322,9 +1362,9 class JRODataWriter(Reader):
1322 1362
1323 1363 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1324 1364 self.basicHeaderObj.version = self.versionFile
1325 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1365 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1326 1366 utc = numpy.floor(self.dataOut.utctime)
1327 milisecond = (self.dataOut.utctime - utc) * 1000.0
1367 milisecond = (self.dataOut.utctime - utc) * 1000.0
1328 1368 self.basicHeaderObj.utc = utc
1329 1369 self.basicHeaderObj.miliSecond = milisecond
1330 1370 self.basicHeaderObj.timeZone = self.dataOut.timeZone
@@ -1465,9 +1505,9 class JRODataWriter(Reader):
1465 1505 if self.dataOut.datatime.date() > self.fileDate:
1466 1506 setFile = 0
1467 1507 self.nTotalBlocks = 0
1468
1508
1469 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 1512 filename = os.path.join(path, subfolder, filen)
1473 1513
@@ -1515,11 +1555,11 class JRODataWriter(Reader):
1515 1555 self.ext = ext.lower()
1516 1556
1517 1557 self.path = path
1518
1558
1519 1559 if set is None:
1520 1560 self.setFile = -1
1521 1561 else:
1522 self.setFile = set - 1
1562 self.setFile = set - 1
1523 1563
1524 1564 self.blocksPerFile = blocksPerFile
1525 1565 self.profilesPerBlock = profilesPerBlock
@@ -38,7 +38,7 DEF_CATALOG = {
38 38 'sciRemarks': '',
39 39 'instRemarks': ''
40 40 }
41
41
42 42 DEF_HEADER = {
43 43 'kindatDesc': '',
44 44 'analyst': 'Jicamarca User',
@@ -75,7 +75,7 def load_json(obj):
75 75 for k, v in list(iterable.items())}
76 76 elif isinstance(iterable, (list, tuple)):
77 77 return [str(v) if isinstance(v, basestring) else v for v in iterable]
78
78
79 79 return iterable
80 80
81 81
@@ -85,18 +85,18 class MADReader(Reader, ProcessingUnit):
85 85
86 86 ProcessingUnit.__init__(self)
87 87
88 self.dataOut = Parameters()
88 self.dataOut = Parameters()
89 89 self.counter_records = 0
90 90 self.nrecords = None
91 91 self.flagNoMoreFiles = 0
92 self.filename = None
92 self.filename = None
93 93 self.intervals = set()
94 94 self.datatime = datetime.datetime(1900,1,1)
95 95 self.format = None
96 96 self.filefmt = "***%Y%m%d*******"
97
97
98 98 def setup(self, **kwargs):
99
99
100 100 self.set_kwargs(**kwargs)
101 101 self.oneDDict = load_json(self.oneDDict)
102 102 self.twoDDict = load_json(self.twoDDict)
@@ -125,32 +125,32 class MADReader(Reader, ProcessingUnit):
125 125
126 126 for nTries in range(self.nTries):
127 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 129 self.filefmt, self.folderfmt)
130 130
131 131 try:
132 132 fullpath = next(fullpath)
133 133 except:
134 134 fullpath = None
135
135
136 136 if fullpath:
137 137 break
138 138
139 139 log.warning(
140 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 142 self.name)
143 143 time.sleep(self.delay)
144 144
145 145 if not(fullpath):
146 146 raise schainpy.admin.SchainError(
147 'There isn\'t any valid file in {}'.format(self.path))
148
147 'There isn\'t any valid file in {}'.format(self.path))
148
149 149 else:
150 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 152 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
153
153
154 154 self.setNextFile()
155 155
156 156 def readFirstHeader(self):
@@ -159,8 +159,8 class MADReader(Reader, ProcessingUnit):
159 159 self.parseHeader()
160 160 self.parseData()
161 161 self.blockIndex = 0
162
163 return
162
163 return
164 164
165 165 def parseHeader(self):
166 166 '''
@@ -183,7 +183,7 class MADReader(Reader, ProcessingUnit):
183 183 if s_parameters:
184 184 log.success('Spatial parameters found: {}'.format(s_parameters),
185 185 'MADReader')
186
186
187 187 for param in list(self.oneDDict.keys()):
188 188 if param.lower() not in self.parameters:
189 189 log.warning(
@@ -191,7 +191,7 class MADReader(Reader, ProcessingUnit):
191 191 param),
192 192 'MADReader')
193 193 self.oneDDict.pop(param, None)
194
194
195 195 for param, value in list(self.twoDDict.items()):
196 196 if param.lower() not in self.parameters:
197 197 log.warning(
@@ -226,10 +226,10 class MADReader(Reader, ProcessingUnit):
226 226 while True:
227 227 self.flagDiscontinuousBlock = 0
228 228 if self.counter_records == self.nrecords:
229 self.setNextFile()
229 self.setNextFile()
230 230
231 231 self.readBlock()
232
232
233 233 if (self.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or \
234 234 (self.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
235 235 log.warning(
@@ -268,7 +268,7 class MADReader(Reader, ProcessingUnit):
268 268 if self.counter_records == self.nrecords:
269 269 break
270 270 continue
271 self.intervals.add((datatime-self.datatime).seconds)
271 self.intervals.add((datatime-self.datatime).seconds)
272 272 break
273 273 elif self.ext == '.hdf5':
274 274 datatime = datetime.datetime.utcfromtimestamp(
@@ -278,27 +278,27 class MADReader(Reader, ProcessingUnit):
278 278 if datatime.date()>self.datatime.date():
279 279 self.flagDiscontinuousBlock = 1
280 280 self.datatime = datatime
281 self.counter_records += 1
282
281 self.counter_records += 1
282
283 283 self.buffer = numpy.array(dum)
284 284 return
285 285
286 286 def set_output(self):
287 287 '''
288 288 Storing data from buffer to dataOut object
289 '''
289 '''
290 290
291 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 294 x = self.parameters.index(param.lower())
295 295 setattr(self.dataOut, attr, self.buffer[0][x])
296 296
297 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 299 if self.ext == '.txt':
300 300 x = self.parameters.index(param.lower())
301 y = self.parameters.index(self.independentParam.lower())
301 y = self.parameters.index(self.independentParam.lower())
302 302 ranges = self.buffer[:,y]
303 303 #if self.ranges.size == ranges.size:
304 304 # continue
@@ -308,23 +308,23 class MADReader(Reader, ProcessingUnit):
308 308 ranges = self.buffer[self.independentParam.lower()]
309 309 index = numpy.where(numpy.in1d(self.ranges, ranges))[0]
310 310 dummy[index] = self.buffer[param.lower()]
311
311
312 312 if isinstance(value, str):
313 if value not in self.independentParam:
313 if value not in self.independentParam:
314 314 setattr(self.dataOut, value, dummy.reshape(1,-1))
315 elif isinstance(value, list):
315 elif isinstance(value, list):
316 316 self.output[value[0]][value[1]] = dummy
317 317 parameters[value[1]] = param
318 318 for key, value in list(self.output.items()):
319 319 setattr(self.dataOut, key, numpy.array(value))
320
320
321 321 self.dataOut.parameters = [s for s in parameters if s]
322 322 self.dataOut.heightList = self.ranges
323 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 325 self.dataOut.paramInterval = min(self.intervals)
326 self.dataOut.useLocalTime = False
327 self.dataOut.flagNoData = False
326 self.dataOut.useLocalTime = False
327 self.dataOut.flagNoData = False
328 328 self.dataOut.nrecords = self.nrecords
329 329 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
330 330
@@ -354,7 +354,7 class MADReader(Reader, ProcessingUnit):
354 354 @MPDecorator
355 355 class MADWriter(Operation):
356 356 '''Writing module for Madrigal files
357
357
358 358 type: external
359 359
360 360 Inputs:
@@ -384,7 +384,7 Inputs:
384 384
385 385 __attrs__ = ['path', 'oneDDict', 'ind2DList', 'twoDDict','metadata', 'format', 'blocks']
386 386 missing = -32767
387
387
388 388 def __init__(self):
389 389
390 390 Operation.__init__(self)
@@ -395,27 +395,31 Inputs:
395 395
396 396 def run(self, dataOut, path, oneDDict, ind2DList='[]', twoDDict='{}',
397 397 metadata='{}', format='cedar', **kwargs):
398
398
399
400 #if dataOut.AUX==1: #Modified
401
399 402 if not self.isConfig:
400 403 self.setup(path, oneDDict, ind2DList, twoDDict, metadata, format, **kwargs)
401 404 self.isConfig = True
402
403 self.dataOut = dataOut
404 self.putData()
405
406 self.dataOut = dataOut
407 self.putData()
408
405 409 return 1
406
410
407 411 def setup(self, path, oneDDict, ind2DList, twoDDict, metadata, format, **kwargs):
408 412 '''
409 Configure Operation
413 Configure Operation
410 414 '''
411
415
412 416 self.path = path
413 417 self.blocks = kwargs.get('blocks', None)
414 418 self.counter = 0
415 419 self.oneDDict = load_json(oneDDict)
416 420 self.twoDDict = load_json(twoDDict)
417 421 self.ind2DList = load_json(ind2DList)
418 meta = load_json(metadata)
422 meta = load_json(metadata)
419 423 self.kinst = meta.get('kinst')
420 424 self.kindat = meta.get('kindat')
421 425 self.catalog = meta.get('catalog', DEF_CATALOG)
@@ -426,8 +430,8 Inputs:
426 430 elif format == 'hdf5':
427 431 self.ext = '.hdf5'
428 432 self.extra_args = {'ind2DList': self.ind2DList}
429
430 self.keys = [k.lower() for k in self.twoDDict]
433
434 self.keys = [k.lower() for k in self.twoDDict]
431 435 if 'range' in self.keys:
432 436 self.keys.remove('range')
433 437 if 'gdalt' in self.keys:
@@ -440,20 +444,23 Inputs:
440 444
441 445 self.mnemonic = MNEMONICS[self.kinst] #TODO get mnemonic from madrigal
442 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 451 filename = '{}{}{}'.format(self.mnemonic,
445 452 date.strftime('%Y%m%d_%H%M%S'),
446 453 self.ext)
447
454
448 455 self.fullname = os.path.join(self.path, filename)
449
450 if os.path.isfile(self.fullname) :
456
457 if os.path.isfile(self.fullname) :
451 458 log.warning(
452 459 'Destination file {} already exists, previous file deleted.'.format(
453 460 self.fullname),
454 461 'MADWriter')
455 462 os.remove(self.fullname)
456
463
457 464 try:
458 465 log.success(
459 466 'Creating file: {}'.format(self.fullname),
@@ -461,6 +468,8 Inputs:
461 468 if not os.path.exists(self.path):
462 469 os.makedirs(self.path)
463 470 self.fp = madrigal.cedar.MadrigalCedarFile(self.fullname, True)
471
472
464 473 except ValueError as e:
465 474 log.error(
466 475 'Impossible to create a cedar object with "madrigal.cedar.MadrigalCedarFile"',
@@ -475,11 +484,26 Inputs:
475 484 attributes.
476 485 Allowed parameters in: parcodes.tab
477 486 '''
478
487 #self.dataOut.paramInterval=2
479 488 startTime = datetime.datetime.utcfromtimestamp(self.dataOut.utctime)
489
480 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 500 heights = self.dataOut.heightList
482 501
502 #print(self.blocks)
503 #print(startTime)
504 #print(endTime)
505 #print(heights)
506 #input()
483 507 if self.ext == '.dat':
484 508 for key, value in list(self.twoDDict.items()):
485 509 if isinstance(value, str):
@@ -505,13 +529,21 Inputs:
505 529 out[key] = tmp.flatten()[:len(heights)]
506 530 elif isinstance(value, (tuple, list)):
507 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 539 out[key] = data[int(x)][:len(heights)]
510
540
511 541 a = numpy.array([out[k] for k in self.keys])
542 #print(a)
512 543 nrows = numpy.array([numpy.isnan(a[:, x]).all() for x in range(len(heights))])
513 544 index = numpy.where(nrows == False)[0]
514 545
546 #print(startTime.minute)
515 547 rec = madrigal.cedar.MadrigalDataRecord(
516 548 self.kinst,
517 549 self.kindat,
@@ -534,22 +566,24 Inputs:
534 566 len(index),
535 567 **self.extra_args
536 568 )
537
538 # Setting 1d values
569 #print("rec",rec)
570 # Setting 1d values
539 571 for key in self.oneDDict:
540 572 rec.set1D(key, getattr(self.dataOut, self.oneDDict[key]))
541 573
542 574 # Setting 2d values
543 575 nrec = 0
544 for n in index:
576 for n in index:
545 577 for key in out:
546 578 rec.set2D(key, nrec, out[key][n])
547 nrec += 1
579 nrec += 1
548 580
549 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 584 self.fp.dump()
552 585 if self.counter % 20 == 0 and self.counter > 0:
586 #self.fp.write()
553 587 log.log(
554 588 'Writing {} records'.format(
555 589 self.counter),
@@ -558,8 +592,8 Inputs:
558 592 def setHeader(self):
559 593 '''
560 594 Create an add catalog and header to cedar file
561 '''
562
595 '''
596
563 597 log.success('Closing file {}'.format(self.fullname), 'MADWriter')
564 598
565 599 if self.ext == '.dat':
@@ -567,17 +601,17 Inputs:
567 601 else:
568 602 self.fp.dump()
569 603 self.fp.close()
570
571 header = madrigal.cedar.CatalogHeaderCreator(self.fullname)
604
605 header = madrigal.cedar.CatalogHeaderCreator(self.fullname)
572 606 header.createCatalog(**self.catalog)
573 607 header.createHeader(**self.header)
574 608 header.write()
575
609
576 610 def putData(self):
577 611
578 612 if self.dataOut.flagNoData:
579 return 0
580
613 return 0
614
581 615 if self.dataOut.flagDiscontinuousBlock or self.counter == self.blocks:
582 616 if self.counter > 0:
583 617 self.setHeader()
@@ -585,11 +619,11 Inputs:
585 619
586 620 if self.counter == 0:
587 621 self.setFile()
588
622
589 623 self.writeBlock()
590 self.counter += 1
591
624 self.counter += 1
625
592 626 def close(self):
593
594 if self.counter > 0:
595 self.setHeader() No newline at end of file
627
628 if self.counter > 0:
629 self.setHeader()
@@ -17,7 +17,7 class HDFReader(Reader, ProcessingUnit):
17 17
18 18 This unit reads HDF5 files created with `HDFWriter` operation contains
19 19 by default two groups Data and Metadata all variables would be saved as `dataOut`
20 attributes.
20 attributes.
21 21 It is possible to read any HDF5 file by given the structure in the `description`
22 22 parameter, also you can add extra values to metadata with the parameter `extras`.
23 23
@@ -37,10 +37,10 class HDFReader(Reader, ProcessingUnit):
37 37 Dictionary with the description of the HDF5 file
38 38 extras : dict, optional
39 39 Dictionary with extra metadata to be be added to `dataOut`
40
40
41 41 Examples
42 42 --------
43
43
44 44 desc = {
45 45 'Data': {
46 46 'data_output': ['u', 'v', 'w'],
@@ -64,7 +64,7 class HDFReader(Reader, ProcessingUnit):
64 64 extras = {
65 65 'timeZone': 300
66 66 }
67
67
68 68 reader = project.addReadUnit(
69 69 name='HDFReader',
70 70 path='/path/to/files',
@@ -99,42 +99,42 class HDFReader(Reader, ProcessingUnit):
99 99
100 100 self.set_kwargs(**kwargs)
101 101 if not self.ext.startswith('.'):
102 self.ext = '.{}'.format(self.ext)
102 self.ext = '.{}'.format(self.ext)
103 103
104 104 if self.online:
105 105 log.log("Searching files in online mode...", self.name)
106 106
107 107 for nTries in range(self.nTries):
108 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 110 self.filefmt, self.folderfmt)
111 111 try:
112 112 fullpath = next(fullpath)
113 113 except:
114 114 fullpath = None
115
115
116 116 if fullpath:
117 117 break
118 118
119 119 log.warning(
120 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 122 self.name)
123 123 time.sleep(self.delay)
124 124
125 125 if not(fullpath):
126 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 129 pathname, filename = os.path.split(fullpath)
130 130 self.year = int(filename[1:5])
131 131 self.doy = int(filename[5:8])
132 self.set = int(filename[8:11]) - 1
132 self.set = int(filename[8:11]) - 1
133 133 else:
134 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 136 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
137
137
138 138 self.setNextFile()
139 139
140 140 return
@@ -142,18 +142,18 class HDFReader(Reader, ProcessingUnit):
142 142 def readFirstHeader(self):
143 143 '''Read metadata and data'''
144 144
145 self.__readMetadata()
145 self.__readMetadata()
146 146 self.__readData()
147 147 self.__setBlockList()
148
148
149 149 if 'type' in self.meta:
150 150 self.dataOut = eval(self.meta['type'])()
151
151
152 152 for attr in self.meta:
153 153 setattr(self.dataOut, attr, self.meta[attr])
154
154
155 155 self.blockIndex = 0
156
156
157 157 return
158 158
159 159 def __setBlockList(self):
@@ -211,7 +211,7 class HDFReader(Reader, ProcessingUnit):
211 211 def __readData(self):
212 212
213 213 data = {}
214
214
215 215 if self.description:
216 216 for key, value in self.description['Data'].items():
217 217 if isinstance(value, str):
@@ -239,7 +239,7 class HDFReader(Reader, ProcessingUnit):
239 239 array = numpy.array(array)
240 240 else:
241 241 log.warning('Unknown type: {}'.format(name))
242
242
243 243 if name in self.description:
244 244 key = self.description[name]
245 245 else:
@@ -248,7 +248,7 class HDFReader(Reader, ProcessingUnit):
248 248
249 249 self.data = data
250 250 return
251
251
252 252 def getData(self):
253 253
254 254 for attr in self.data:
@@ -287,8 +287,8 class HDFWriter(Operation):
287 287 The HDF5 file contains by default two groups Data and Metadata where
288 288 you can save any `dataOut` attribute specified by `dataList` and `metadataList`
289 289 parameters, data attributes are normaly time dependent where the metadata
290 are not.
291 It is possible to customize the structure of the HDF5 file with the
290 are not.
291 It is possible to customize the structure of the HDF5 file with the
292 292 optional description parameter see the examples.
293 293
294 294 Parameters:
@@ -305,10 +305,10 class HDFWriter(Operation):
305 305 If True the name of the files corresponds to the timestamp of the data
306 306 description : dict, optional
307 307 Dictionary with the desired description of the HDF5 file
308
308
309 309 Examples
310 310 --------
311
311
312 312 desc = {
313 313 'data_output': {'winds': ['z', 'w', 'v']},
314 314 'utctime': 'timestamps',
@@ -328,7 +328,7 class HDFWriter(Operation):
328 328 'heightList': 'heights'
329 329 }
330 330 }
331
331
332 332 writer = proc_unit.addOperation(name='HDFWriter')
333 333 writer.addParameter(name='path', value='/path/to/file')
334 334 writer.addParameter(name='blocksPerFile', value='32')
@@ -356,7 +356,7 class HDFWriter(Operation):
356 356 lastTime = None
357 357
358 358 def __init__(self):
359
359
360 360 Operation.__init__(self)
361 361 return
362 362
@@ -392,7 +392,7 class HDFWriter(Operation):
392 392 dsDict['shape'] = dataAux.shape
393 393 dsDict['dsNumber'] = dataAux.shape[0]
394 394 dsDict['dtype'] = dataAux.dtype
395
395
396 396 dsList.append(dsDict)
397 397
398 398 self.dsList = dsList
@@ -407,7 +407,7 class HDFWriter(Operation):
407 407 self.lastTime = currentTime
408 408 self.currentDay = dataDay
409 409 return False
410
410
411 411 timeDiff = currentTime - self.lastTime
412 412
413 413 #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
@@ -426,7 +426,7 class HDFWriter(Operation):
426 426
427 427 self.dataOut = dataOut
428 428 if not(self.isConfig):
429 self.setup(path=path, blocksPerFile=blocksPerFile,
429 self.setup(path=path, blocksPerFile=blocksPerFile,
430 430 metadataList=metadataList, dataList=dataList,
431 431 setType=setType, description=description)
432 432
@@ -435,9 +435,9 class HDFWriter(Operation):
435 435
436 436 self.putData()
437 437 return
438
438
439 439 def setNextFile(self):
440
440
441 441 ext = self.ext
442 442 path = self.path
443 443 setFile = self.setFile
@@ -522,7 +522,7 class HDFWriter(Operation):
522 522 return 'pair{:02d}'.format(x)
523 523 else:
524 524 return 'channel{:02d}'.format(x)
525
525
526 526 def writeMetadata(self, fp):
527 527
528 528 if self.description:
@@ -547,7 +547,7 class HDFWriter(Operation):
547 547 return
548 548
549 549 def writeData(self, fp):
550
550
551 551 if self.description:
552 552 if 'Data' in self.description:
553 553 grp = fp.create_group('Data')
@@ -558,13 +558,13 class HDFWriter(Operation):
558 558
559 559 dtsets = []
560 560 data = []
561
561
562 562 for dsInfo in self.dsList:
563 563 if dsInfo['nDim'] == 0:
564 564 ds = grp.create_dataset(
565 self.getLabel(dsInfo['variable']),
565 self.getLabel(dsInfo['variable']),
566 566 (self.blocksPerFile, ),
567 chunks=True,
567 chunks=True,
568 568 dtype=numpy.float64)
569 569 dtsets.append(ds)
570 570 data.append((dsInfo['variable'], -1))
@@ -576,7 +576,7 class HDFWriter(Operation):
576 576 sgrp = grp
577 577 for i in range(dsInfo['dsNumber']):
578 578 ds = sgrp.create_dataset(
579 self.getLabel(dsInfo['variable'], i),
579 self.getLabel(dsInfo['variable'], i),
580 580 (self.blocksPerFile, ) + dsInfo['shape'][1:],
581 581 chunks=True,
582 582 dtype=dsInfo['dtype'])
@@ -585,7 +585,7 class HDFWriter(Operation):
585 585 fp.flush()
586 586
587 587 log.log('Creating file: {}'.format(fp.filename), self.name)
588
588
589 589 self.ds = dtsets
590 590 self.data = data
591 591 self.firsttime = True
@@ -73,27 +73,28 class VoltageReader(JRODataReader, ProcessingUnit):
73 73 """
74 74
75 75 ProcessingUnit.__init__(self)
76
76
77 77 self.ext = ".r"
78 78 self.optchar = "D"
79 79 self.basicHeaderObj = BasicHeader(LOCALTIME)
80 80 self.systemHeaderObj = SystemHeader()
81 81 self.radarControllerHeaderObj = RadarControllerHeader()
82
82 83 self.processingHeaderObj = ProcessingHeader()
83 84 self.lastUTTime = 0
84 self.profileIndex = 2**32 - 1
85 self.profileIndex = 2**32 - 1
85 86 self.dataOut = Voltage()
86 87 self.selBlocksize = None
87 88 self.selBlocktime = None
88
89 ##print("1--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
89 90 def createObjByDefault(self):
90
91 ##print("2--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
91 92 dataObj = Voltage()
92 93
93 94 return dataObj
94 95
95 96 def __hasNotDataInBuffer(self):
96
97 ##print("3--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
97 98 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock * self.nTxs:
98 99 return 1
99 100
@@ -109,11 +110,13 class VoltageReader(JRODataReader, ProcessingUnit):
109 110 Return:
110 111 None
111 112 """
113 ##print("4--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
112 114 pts2read = self.processingHeaderObj.profilesPerBlock * \
113 115 self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
114 116 self.blocksize = pts2read
115 117
116 118 def readBlock(self):
119
117 120 """
118 121 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
119 122 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
@@ -133,10 +136,10 class VoltageReader(JRODataReader, ProcessingUnit):
133 136 self.flagIsNewBlock
134 137 self.nTotalBlocks
135 138
136 Exceptions:
139 Exceptions:
137 140 Si un bloque leido no es un bloque valido
138 141 """
139
142 ##print("5--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
140 143 # if self.server is not None:
141 144 # self.zBlock = self.receiver.recv()
142 145 # self.zHeader = self.zBlock[:24]
@@ -177,6 +180,7 class VoltageReader(JRODataReader, ProcessingUnit):
177 180 return 1
178 181
179 182 def getFirstHeader(self):
183 ##print("6--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
180 184
181 185 self.getBasicHeader()
182 186
@@ -186,8 +190,12 class VoltageReader(JRODataReader, ProcessingUnit):
186 190
187 191 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
188 192
193 #self.dataOut.ippSeconds_general=self.radarControllerHeaderObj.ippSeconds
194 #print(self.nTxs)
189 195 if self.nTxs > 1:
196 #print(self.radarControllerHeaderObj.ippSeconds)
190 197 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
198 #print(self.radarControllerHeaderObj.ippSeconds)
191 199 # Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj.
192 200
193 201 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt
@@ -220,7 +228,7 class VoltageReader(JRODataReader, ProcessingUnit):
220 228 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
221 229
222 230 def reshapeData(self):
223
231 ##print("7--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
224 232 if self.nTxs < 0:
225 233 return
226 234
@@ -247,6 +255,7 class VoltageReader(JRODataReader, ProcessingUnit):
247 255
248 256 def readFirstHeaderFromServer(self):
249 257
258 ##print("8--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
250 259 self.getFirstHeader()
251 260
252 261 self.firstHeaderSize = self.basicHeaderObj.size
@@ -278,6 +287,7 class VoltageReader(JRODataReader, ProcessingUnit):
278 287 self.getBlockDimension()
279 288
280 289 def getFromServer(self):
290 ##print("9--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
281 291 self.flagDiscontinuousBlock = 0
282 292 self.profileIndex = 0
283 293 self.flagIsNewBlock = 1
@@ -382,6 +392,8 class VoltageReader(JRODataReader, ProcessingUnit):
382 392 self.flagDiscontinuousBlock
383 393 self.flagIsNewBlock
384 394 """
395
396 ##print("10--OKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK")
385 397 if self.flagNoMoreFiles:
386 398 self.dataOut.flagNoData = True
387 399 return 0
@@ -410,6 +422,7 class VoltageReader(JRODataReader, ProcessingUnit):
410 422 self.dataOut.data = self.datablock[:, self.profileIndex, :]
411 423 self.dataOut.profileIndex = self.profileIndex
412 424
425
413 426 self.profileIndex += 1
414 427
415 428 else:
@@ -458,9 +471,13 class VoltageReader(JRODataReader, ProcessingUnit):
458 471 self.dataOut.flagDataAsBlock = True
459 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 478 self.dataOut.flagNoData = False
462 479
463 self.getBasicHeader()
480 #self.getBasicHeader()
464 481
465 482 self.dataOut.realtime = self.online
466 483
@@ -673,4 +690,3 class VoltageWriter(JRODataWriter, Operation):
673 690 self.processingHeaderObj.processFlags = self.getProcessFlags()
674 691
675 692 self.setBasicHeader()
676 No newline at end of file
@@ -14,3 +14,9 from .jroproc_spectra_lags import *
14 14 from .jroproc_spectra_acf import *
15 15 from .bltrproc_parameters import *
16 16 from .pxproc_parameters import *
17
18
19 ###########DP###########
20 from .jroproc_voltage_lags import *
21 ###########DP###########
22 from .jroproc_spectra_lags_faraday import *
@@ -172,7 +172,7 def MPDecorator(BaseClass):
172 172 self.op_type = 'external'
173 173 self.name = BaseClass.__name__
174 174 self.__doc__ = BaseClass.__doc__
175
175
176 176 if 'plot' in self.name.lower() and not self.name.endswith('_'):
177 177 self.name = '{}{}'.format(self.CODE.upper(), 'Plot')
178 178
@@ -319,6 +319,12 class SpectralFilters(Operation):
319 319 dataOut.data_pre[0] = self.spc
320 320 return dataOut
321 321
322
323 from scipy.optimize import fmin
324 import itertools
325 from scipy.optimize import curve_fit
326
327
322 328 class GaussianFit(Operation):
323 329
324 330 '''
@@ -378,18 +384,17 class GaussianFit(Operation):
378 384 v1 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][2,:,1]] * self.Num_Bin))
379 385 s0 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][3,:,0]] * self.Num_Bin))
380 386 s1 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][3,:,1]] * self.Num_Bin))
381 if method == 'genealized':
387 if method == 'generalized':
382 388 p0 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][4,:,0]] * self.Num_Bin))
383 389 p1 = numpy.transpose(numpy.transpose([dataOut.DGauFitParams[iCh][4,:,1]] * self.Num_Bin))
384 390 elif method == 'squared':
385 391 p0 = 2.
386 p1 = 2.
392 p1 = 2.
387 393 gau0[iCh] = A0*numpy.exp(-0.5*numpy.abs((x_mtr-v0)/s0)**p0)+N0
388 394 gau1[iCh] = A1*numpy.exp(-0.5*numpy.abs((x_mtr-v1)/s1)**p1)+N1
389 395 dataOut.GaussFit0 = gau0
390 396 dataOut.GaussFit1 = gau1
391
392 print('Leaving ',method ,' double Gaussian fit')
397
393 398 return dataOut
394 399
395 400 def FitGau(self, X):
@@ -493,7 +498,7 class GaussianFit(Operation):
493 498 if powerwidth <= 1:
494 499 # print('powerwidth <= 1')
495 500 continue
496
501
497 502 # print ('stop 6')
498 503 firstpeak = powerlo + powerwidth/10.# first gaussian energy location
499 504 secondpeak = powerhi - powerwidth/10. #second gaussian energy location
@@ -531,7 +536,6 class GaussianFit(Operation):
531 536 noise=lsq1[0][4]
532 537 #return (numpy.array([shift0,width0,Amplitude0,p0]),
533 538 # numpy.array([shift1,width1,Amplitude1,p1]),noise,snrdB,chiSq1,6.,sigmas1,[None,]*9,choice)
534
535 539 # print ('stop 9')
536 540 ''' two Gaussians '''
537 541 #shift0=numpy.mod(firstpeak+minx,64); shift1=numpy.mod(secondpeak+minx,64)
@@ -628,7 +632,7 class GaussianFit(Operation):
628 632 if Amplitude1<0.05:
629 633 shift1,width1,Amplitude1,p1 = 4*[numpy.NaN]
630 634
631 # print ('stop 16 ')
635 # print ('stop 16 ')
632 636 # SPC_ch1[:,ht] = noise + Amplitude0*numpy.exp(-0.5*(abs(x-shift0)/width0)**p0)
633 637 # SPC_ch2[:,ht] = noise + Amplitude1*numpy.exp(-0.5*(abs(x-shift1)/width1)**p1)
634 638 # SPCparam = (SPC_ch1,SPC_ch2)
@@ -644,8 +648,6 class GaussianFit(Operation):
644 648 DGauFitParam[4,ht,0] = p0
645 649 DGauFitParam[4,ht,1] = p1
646 650
647 # print (DGauFitParam.shape)
648 # print ('Leaving FitGau')
649 651 return DGauFitParam
650 652 # return SPCparam
651 653 # return GauSPC
@@ -662,7 +664,7 class GaussianFit(Operation):
662 664 model0 = amplitude0*numpy.exp(-0.5*abs((x-shift0)/width0)**power0)
663 665 model0u = amplitude0*numpy.exp(-0.5*abs((x - shift0 - self.Num_Bin)/width0)**power0)
664 666 model0d = amplitude0*numpy.exp(-0.5*abs((x - shift0 + self.Num_Bin)/width0)**power0)
665
667
666 668 model1 = amplitude1*numpy.exp(-0.5*abs((x - shift1)/width1)**power1)
667 669 model1u = amplitude1*numpy.exp(-0.5*abs((x - shift1 - self.Num_Bin)/width1)**power1)
668 670 model1d = amplitude1*numpy.exp(-0.5*abs((x - shift1 + self.Num_Bin)/width1)**power1)
@@ -676,6 +678,206 class GaussianFit(Operation):
676 678 return num_intg*sum((numpy.log(y_data)-numpy.log(self.y_model2(x,state)))**2)#/(64-9.)
677 679
678 680
681 class Oblique_Gauss_Fit(Operation):
682
683 def __init__(self):
684 Operation.__init__(self)
685
686 def Gauss_fit(self,spc,x,nGauss):
687
688
689 def gaussian(x, a, b, c, d):
690 val = a * numpy.exp(-(x - b)**2 / (2*c**2)) + d
691 return val
692
693 if nGauss == 'first':
694 spc_1_aux = numpy.copy(spc[:numpy.argmax(spc)+1])
695 spc_2_aux = numpy.flip(spc_1_aux)
696 spc_3_aux = numpy.concatenate((spc_1_aux,spc_2_aux[1:]))
697
698 len_dif = len(x)-len(spc_3_aux)
699
700 spc_zeros = numpy.ones(len_dif)*spc_1_aux[0]
701
702 spc_new = numpy.concatenate((spc_3_aux,spc_zeros))
703
704 y = spc_new
705
706 elif nGauss == 'second':
707 y = spc
708
709
710 # estimate starting values from the data
711 a = y.max()
712 b = x[numpy.argmax(y)]
713 if nGauss == 'first':
714 c = 1.#b#b#numpy.std(spc)
715 elif nGauss == 'second':
716 c = b
717 else:
718 print("ERROR")
719
720 d = numpy.mean(y[-100:])
721
722 # define a least squares function to optimize
723 def minfunc(params):
724 return sum((y-gaussian(x,params[0],params[1],params[2],params[3]))**2)
725
726 # fit
727 popt = fmin(minfunc,[a,b,c,d],disp=False)
728 #popt,fopt,niter,funcalls = fmin(minfunc,[a,b,c,d])
729
730
731 return gaussian(x, popt[0], popt[1], popt[2], popt[3]), popt[0], popt[1], popt[2], popt[3]
732
733
734 def Gauss_fit_2(self,spc,x,nGauss):
735
736
737 def gaussian(x, a, b, c, d):
738 val = a * numpy.exp(-(x - b)**2 / (2*c**2)) + d
739 return val
740
741 if nGauss == 'first':
742 spc_1_aux = numpy.copy(spc[:numpy.argmax(spc)+1])
743 spc_2_aux = numpy.flip(spc_1_aux)
744 spc_3_aux = numpy.concatenate((spc_1_aux,spc_2_aux[1:]))
745
746 len_dif = len(x)-len(spc_3_aux)
747
748 spc_zeros = numpy.ones(len_dif)*spc_1_aux[0]
749
750 spc_new = numpy.concatenate((spc_3_aux,spc_zeros))
751
752 y = spc_new
753
754 elif nGauss == 'second':
755 y = spc
756
757
758 # estimate starting values from the data
759 a = y.max()
760 b = x[numpy.argmax(y)]
761 if nGauss == 'first':
762 c = 1.#b#b#numpy.std(spc)
763 elif nGauss == 'second':
764 c = b
765 else:
766 print("ERROR")
767
768 d = numpy.mean(y[-100:])
769
770 # define a least squares function to optimize
771 popt,pcov = curve_fit(gaussian,x,y,p0=[a,b,c,d])
772 #popt,fopt,niter,funcalls = fmin(minfunc,[a,b,c,d])
773
774
775 #return gaussian(x, popt[0], popt[1], popt[2], popt[3]), popt[0], popt[1], popt[2], popt[3]
776 return gaussian(x, popt[0], popt[1], popt[2], popt[3]),popt[0], popt[1], popt[2], popt[3]
777
778 def Double_Gauss_fit(self,spc,x,A1,B1,C1,A2,B2,C2,D):
779
780 def double_gaussian(x, a1, b1, c1, a2, b2, c2, d):
781 val = a1 * numpy.exp(-(x - b1)**2 / (2*c1**2)) + a2 * numpy.exp(-(x - b2)**2 / (2*c2**2)) + d
782 return val
783
784
785 y = spc
786
787 # estimate starting values from the data
788 a1 = A1
789 b1 = B1
790 c1 = C1#numpy.std(spc)
791
792 a2 = A2#y.max()
793 b2 = B2#x[numpy.argmax(y)]
794 c2 = C2#numpy.std(spc)
795 d = D
796
797 # define a least squares function to optimize
798 def minfunc(params):
799 return sum((y-double_gaussian(x,params[0],params[1],params[2],params[3],params[4],params[5],params[6]))**2)
800
801 # fit
802 popt = fmin(minfunc,[a1,b1,c1,a2,b2,c2,d],disp=False)
803
804 return double_gaussian(x, popt[0], popt[1], popt[2], popt[3], popt[4], popt[5], popt[6]), popt[0], popt[1], popt[2], popt[3], popt[4], popt[5], popt[6]
805
806 def Double_Gauss_fit_2(self,spc,x,A1,B1,C1,A2,B2,C2,D):
807
808 def double_gaussian(x, a1, b1, c1, a2, b2, c2, d):
809 val = a1 * numpy.exp(-(x - b1)**2 / (2*c1**2)) + a2 * numpy.exp(-(x - b2)**2 / (2*c2**2)) + d
810 return val
811
812
813 y = spc
814
815 # estimate starting values from the data
816 a1 = A1
817 b1 = B1
818 c1 = C1#numpy.std(spc)
819
820 a2 = A2#y.max()
821 b2 = B2#x[numpy.argmax(y)]
822 c2 = C2#numpy.std(spc)
823 d = D
824
825 # fit
826
827 popt,pcov = curve_fit(double_gaussian,x,y,p0=[a1,b1,c1,a2,b2,c2,d])
828
829 error = numpy.sqrt(numpy.diag(pcov))
830
831 return popt[0], popt[1], popt[2], popt[3], popt[4], popt[5], popt[6], error[0], error[1], error[2], error[3], error[4], error[5], error[6]
832
833 def run(self, dataOut):
834
835 pwcode = 1
836
837 if dataOut.flagDecodeData:
838 pwcode = numpy.sum(dataOut.code[0]**2)
839 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
840 normFactor = dataOut.nProfiles * dataOut.nIncohInt * dataOut.nCohInt * pwcode * dataOut.windowOfFilter
841 factor = normFactor
842 z = dataOut.data_spc / factor
843 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
844 dataOut.power = numpy.average(z, axis=1)
845 dataOut.powerdB = 10 * numpy.log10(dataOut.power)
846
847
848 x = dataOut.getVelRange(0)
849
850 dataOut.Oblique_params = numpy.ones((1,7,dataOut.nHeights))*numpy.NAN
851 dataOut.Oblique_param_errors = numpy.ones((1,7,dataOut.nHeights))*numpy.NAN
852
853 dataOut.VelRange = x
854
855
856 l1=range(22,36)
857 l2=range(58,99)
858
859 for hei in itertools.chain(l1, l2):
860
861 try:
862 spc = dataOut.data_spc[0,:,hei]
863
864 spc_fit, A1, B1, C1, D1 = self.Gauss_fit_2(spc,x,'first')
865
866 spc_diff = spc - spc_fit
867 spc_diff[spc_diff < 0] = 0
868
869 spc_fit_diff, A2, B2, C2, D2 = self.Gauss_fit_2(spc_diff,x,'second')
870
871 D = (D1+D2)
872
873 dataOut.Oblique_params[0,0,hei],dataOut.Oblique_params[0,1,hei],dataOut.Oblique_params[0,2,hei],dataOut.Oblique_params[0,3,hei],dataOut.Oblique_params[0,4,hei],dataOut.Oblique_params[0,5,hei],dataOut.Oblique_params[0,6,hei],dataOut.Oblique_param_errors[0,0,hei],dataOut.Oblique_param_errors[0,1,hei],dataOut.Oblique_param_errors[0,2,hei],dataOut.Oblique_param_errors[0,3,hei],dataOut.Oblique_param_errors[0,4,hei],dataOut.Oblique_param_errors[0,5,hei],dataOut.Oblique_param_errors[0,6,hei] = self.Double_Gauss_fit_2(spc,x,A1,B1,C1,A2,B2,C2,D)
874 #spc_double_fit,dataOut.Oblique_params = self.Double_Gauss_fit(spc,x,A1,B1,C1,A2,B2,C2,D)
875
876 except:
877 ###dataOut.Oblique_params[0,:,hei] = dataOut.Oblique_params[0,:,hei]*numpy.NAN
878 pass
879
880 return dataOut
679 881
680 882 class PrecipitationProc(Operation):
681 883
@@ -3884,3 +4086,55 class SMOperations():
3884 4086 # error[indInvalid1] = 13
3885 4087 #
3886 4088 # return heights, error
4089
4090
4091
4092 class IGRFModel(Operation):
4093 """Operation to calculate Geomagnetic parameters.
4094
4095 Parameters:
4096 -----------
4097 None
4098
4099 Example
4100 --------
4101
4102 op = proc_unit.addOperation(name='IGRFModel', optype='other')
4103
4104 """
4105
4106 def __init__(self, **kwargs):
4107
4108 Operation.__init__(self, **kwargs)
4109
4110 self.aux=1
4111
4112 def run(self,dataOut):
4113
4114 try:
4115 from schainpy.model.proc import mkfact_short_2020
4116 except:
4117 log.warning('You should install "mkfact_short_2020" module to process IGRF Model')
4118
4119 if self.aux==1:
4120
4121 #dataOut.TimeBlockSeconds_First_Time=time.mktime(time.strptime(dataOut.TimeBlockDate))
4122 #### we do not use dataOut.datatime.ctime() because it's the time of the second (next) block
4123 dataOut.TimeBlockSeconds_First_Time=dataOut.TimeBlockSeconds
4124 dataOut.bd_time=time.gmtime(dataOut.TimeBlockSeconds_First_Time)
4125 dataOut.year=dataOut.bd_time.tm_year+(dataOut.bd_time.tm_yday-1)/364.0
4126 dataOut.ut=dataOut.bd_time.tm_hour+dataOut.bd_time.tm_min/60.0+dataOut.bd_time.tm_sec/3600.0
4127
4128 self.aux=0
4129
4130 dataOut.h=numpy.arange(0.0,15.0*dataOut.MAXNRANGENDT,15.0,dtype='float32')
4131 dataOut.bfm=numpy.zeros(dataOut.MAXNRANGENDT,dtype='float32')
4132 dataOut.bfm=numpy.array(dataOut.bfm,order='F')
4133 dataOut.thb=numpy.zeros(dataOut.MAXNRANGENDT,dtype='float32')
4134 dataOut.thb=numpy.array(dataOut.thb,order='F')
4135 dataOut.bki=numpy.zeros(dataOut.MAXNRANGENDT,dtype='float32')
4136 dataOut.bki=numpy.array(dataOut.bki,order='F')
4137
4138 mkfact_short_2020.mkfact(dataOut.year,dataOut.h,dataOut.bfm,dataOut.thb,dataOut.bki,dataOut.MAXNRANGENDT)
4139
4140 return dataOut
@@ -895,4 +895,4 class dopplerFlip(Operation):
895 895 # canal modificado es re-escrito en el arreglo de canales
896 896 self.dataOut.data_spc[2] = jspectra_tmp
897 897
898 return self.dataOut No newline at end of file
898 return self.dataOut
@@ -736,4 +736,4 class SpectraLagsProc(ProcessingUnit):
736 736
737 737 self.dataOut.noise_estimation = noise.copy()
738 738
739 return 1 No newline at end of file
739 return 1
This diff has been collapsed as it changes many lines, (5769 lines changed) Show them Hide them
@@ -4,8 +4,8 from scipy import interpolate
4 4 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
5 5 from schainpy.model.data.jrodata import Voltage,hildebrand_sekhon
6 6 from schainpy.utils import log
7 from time import time
8
7 from time import time, mktime, strptime, gmtime, ctime
8 import os
9 9
10 10
11 11 class VoltageProc(ProcessingUnit):
@@ -17,8 +17,12 class VoltageProc(ProcessingUnit):
17 17 self.dataOut = Voltage()
18 18 self.flip = 1
19 19 self.setupReq = False
20 #self.dataOut.test=1
21
20 22
21 23 def run(self):
24 #import time
25 #time.sleep(3)
22 26
23 27 if self.dataIn.type == 'AMISR':
24 28 self.__updateObjFromAmisrInput()
@@ -26,6 +30,31 class VoltageProc(ProcessingUnit):
26 30 if self.dataIn.type == 'Voltage':
27 31 self.dataOut.copy(self.dataIn)
28 32
33
34 #self.dataOut.flagNoData=True
35 #print(self.dataOut.data[-1,:])
36 #print(ctime(self.dataOut.utctime))
37 #print(self.dataOut.heightList)
38 #print(self.dataOut.nHeights)
39 #exit(1)
40 #print(self.dataOut.data[6,:32])
41 #print(self.dataOut.data[0,320-5:320+5-5])
42 ##print(self.dataOut.heightList[-20:])
43 #print(numpy.shape(self.dataOut.data))
44 #print(self.dataOut.code)
45 #print(numpy.shape(self.dataOut.code))
46 #exit(1)
47 #print(self.dataOut.CurrentBlock)
48 #print(self.dataOut.data[0,:,0])
49
50 #print(numpy.shape(self.dataOut.data))
51 #print(self.dataOut.data[0,:,1666:1666+320])
52 #exit(1)
53
54 #print(self.dataOut.utctime)
55 #self.dataOut.test+=1
56
57
29 58 def __updateObjFromAmisrInput(self):
30 59
31 60 self.dataOut.timeZone = self.dataIn.timeZone
@@ -52,11 +81,13 class VoltageProc(ProcessingUnit):
52 81 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
53 82 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
54 83
55
56 84 class selectChannels(Operation):
57 85
58 86 def run(self, dataOut, channelList):
59 87
88
89
90
60 91 channelIndexList = []
61 92 self.dataOut = dataOut
62 93 for channel in channelList:
@@ -66,8 +97,10 class selectChannels(Operation):
66 97 index = self.dataOut.channelList.index(channel)
67 98 channelIndexList.append(index)
68 99 self.selectChannelsByIndex(channelIndexList)
100
69 101 return self.dataOut
70 102
103
71 104 def selectChannelsByIndex(self, channelIndexList):
72 105 """
73 106 Selecciona un bloque de datos en base a canales segun el channelIndexList
@@ -190,6 +223,8 class selectHeights(Operation):
190 223 maxIndex = len(heights)
191 224
192 225 self.selectHeightsByIndex(minIndex, maxIndex)
226 #print(self.dataOut.nHeights)
227
193 228
194 229 return self.dataOut
195 230
@@ -314,11 +349,21 class setH0(Operation):
314 349
315 350
316 351 class deFlip(Operation):
352 def __init__(self):
353
354 self.flip = 1
317 355
318 356 def run(self, dataOut, channelList = []):
319 357
320 358 data = dataOut.data.copy()
359 #print(dataOut.channelList)
360 #exit()
361
362 if channelList==1: #PARCHE
363 channelList=[1]
321 364
365
366 dataOut.FlipChannels=channelList
322 367 if dataOut.flagDataAsBlock:
323 368 flip = self.flip
324 369 profileList = list(range(dataOut.nProfiles))
@@ -338,10 +383,15 class deFlip(Operation):
338 383
339 384 self.flip = flip
340 385
386
387
388
341 389 else:
342 390 if not channelList:
343 391 data[:,:] = data[:,:]*self.flip
344 392 else:
393 channelList=[1]
394 #print(self.flip)
345 395 for thisChannel in channelList:
346 396 if thisChannel not in dataOut.channelList:
347 397 continue
@@ -350,6 +400,7 class deFlip(Operation):
350 400
351 401 self.flip *= -1.
352 402
403
353 404 dataOut.data = data
354 405
355 406 return dataOut
@@ -414,879 +465,5459 class interpolateHeights(Operation):
414 465 return dataOut
415 466
416 467
417 class CohInt(Operation):
468 class LagsReshape(Operation):
469 """Operation to reshape input data into (Channels,Profiles(with same lag),Heights,Lags) and heights reconstruction.
470
471 Parameters:
472 -----------
473
474
475 Example
476 --------
477
478 op = proc_unit.addOperation(name='LagsReshape')
418 479
419 isConfig = False
420 __profIndex = 0
421 __byTime = False
422 __initime = None
423 __lastdatatime = None
424 __integrationtime = None
425 __buffer = None
426 __bufferStride = []
427 __dataReady = False
428 __profIndexStride = 0
429 __dataToPutStride = False
430 n = None
480
481 """
431 482
432 483 def __init__(self, **kwargs):
433 484
434 485 Operation.__init__(self, **kwargs)
435 486
436 def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False):
437 """
438 Set the parameters of the integration class.
487 self.buffer=None
488 self.buffer_HR=None
489 self.buffer_HRonelag=None
439 490
440 Inputs:
491 def LagDistribution(self,dataOut):
441 492
442 n : Number of coherent integrations
443 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
444 overlapping :
445 """
493 dataOut.datapure=numpy.copy(dataOut.data[:,0:dataOut.NSCAN,:])
494 self.buffer = numpy.zeros((dataOut.nChannels,
495 int(dataOut.NSCAN/dataOut.DPL),
496 dataOut.nHeights,dataOut.DPL),
497 dtype='complex')
446 498
447 self.__initime = None
448 self.__lastdatatime = 0
449 self.__buffer = None
450 self.__dataReady = False
451 self.byblock = byblock
452 self.stride = stride
499 for j in range(int(self.buffer.shape[1]/2)):
500 for i in range(dataOut.DPL):
501 if j+1==int(self.buffer.shape[1]/2) and i+1==dataOut.DPL:
502 self.buffer[:,2*j:,:,i]=dataOut.datapure[:,2*i+int(2*j*dataOut.DPL):,:]
503 else:
504 self.buffer[:,2*j:2*(j+1),:,i]=dataOut.datapure[:,2*i+int(2*j*dataOut.DPL):2*(i+1)+int(2*j*dataOut.DPL),:]
453 505
454 if n == None and timeInterval == None:
455 raise ValueError("n or timeInterval should be specified ...")
506 return self.buffer
456 507
457 if n != None:
458 self.n = n
459 self.__byTime = False
460 else:
461 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
462 self.n = 9999
463 self.__byTime = True
508 def HeightReconstruction(self,dataOut):
464 509
465 if overlapping:
466 self.__withOverlapping = True
467 self.__buffer = None
468 else:
469 self.__withOverlapping = False
470 self.__buffer = 0
510 self.buffer_HR = numpy.zeros((int(dataOut.NSCAN/dataOut.DPL),
511 dataOut.nHeights,dataOut.DPL),
512 dtype='complex')
471 513
472 self.__profIndex = 0
514 #self.buffer_HR[0,:,:,:]=dataOut.datalags[0,:,:,:] #No Lags
473 515
474 def putData(self, data):
516 for i in range(int(dataOut.DPL)): #Only channel B
517 if i==0:
518 self.buffer_HR[:,:,i]=dataOut.datalags[1,:,:,i]
519 else:
520 self.buffer_HR[:,:,i]=self.HRonelag(dataOut,i)
475 521
476 """
477 Add a profile to the __buffer and increase in one the __profileIndex
522 return self.buffer_HR
478 523
479 """
480 524
481 if not self.__withOverlapping:
482 self.__buffer += data.copy()
483 self.__profIndex += 1
484 return
525 def HRonelag(self,dataOut,whichlag):
526 self.buffer_HRonelag = numpy.zeros((int(dataOut.NSCAN/dataOut.DPL),
527 dataOut.nHeights),
528 dtype='complex')
485 529
486 #Overlapping data
487 nChannels, nHeis = data.shape
488 data = numpy.reshape(data, (1, nChannels, nHeis))
530 for i in range(self.buffer_HRonelag.shape[0]):
531 for j in range(dataOut.nHeights):
532 if j+int(2*whichlag)<dataOut.nHeights:
533 self.buffer_HRonelag[i,j]=dataOut.datalags[1,i,j+2*whichlag,whichlag]
534 else:
535 if whichlag!=10:
536 self.buffer_HRonelag[i,j]=dataOut.datalags[1,i,(j+2*whichlag)%dataOut.nHeights,whichlag+1]
537 else:
538 if i+2<self.buffer_HRonelag.shape[0]:
539 self.buffer_HRonelag[i,j]=dataOut.datalags[1,i+2,(j+2*whichlag)%dataOut.nHeights,0]
540 else: #i+1==self.buffer_HRonelag.shape[0]:
541 self.buffer_HRonelag[i,j]=dataOut.datalags[1,i,(j+2*whichlag)%dataOut.nHeights,whichlag]
489 542
490 #If the buffer is empty then it takes the data value
491 if self.__buffer is None:
492 self.__buffer = data
493 self.__profIndex += 1
494 return
543 return self.buffer_HRonelag
495 544
496 #If the buffer length is lower than n then stakcing the data value
497 if self.__profIndex < self.n:
498 self.__buffer = numpy.vstack((self.__buffer, data))
499 self.__profIndex += 1
500 return
501 545
502 #If the buffer length is equal to n then replacing the last buffer value with the data value
503 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
504 self.__buffer[self.n-1] = data
505 self.__profIndex = self.n
506 return
507 546
547 def run(self,dataOut,DPL=11,NSCAN=132):
508 548
509 def pushData(self):
510 """
511 Return the sum of the last profiles and the profiles used in the sum.
549 dataOut.DPL=DPL
550 dataOut.NSCAN=NSCAN
551 dataOut.paramInterval=0#int(dataOut.nint*dataOut.header[7][0]*2 )
552 dataOut.lat=-11.95
553 dataOut.lon=-76.87
554 dataOut.datalags=None
555 #print(dataOut.NSCAN)
512 556
513 Affected:
557 dataOut.datalags=numpy.copy(self.LagDistribution(dataOut))
558 dataOut.datalags[1,:,:,:]=self.HeightReconstruction(dataOut)
514 559
515 self.__profileIndex
560 return dataOut
516 561
517 """
518 562
519 if not self.__withOverlapping:
520 data = self.__buffer
521 n = self.__profIndex
522 563
523 self.__buffer = 0
524 self.__profIndex = 0
525 564
526 return data, n
565 class CrossProdDP(Operation):
566 """Operation to calculate cross products of the Double Pulse Experiment.
567
568 Parameters:
569 -----------
570 NLAG : int
571 Number of lags Long Pulse.
572 NRANGE : int
573 Number of samples for Long Pulse.
574 NCAL : int
575 .*
576 DPL : int
577 Number of lags Double Pulse.
578 NDN : int
579 .*
580 NDT : int
581 Number of heights for Double Pulse.*
582 NDP : int
583 Number of heights for Double Pulse.*
584 NSCAN : int
585 Number of profiles when the transmitter is on.
586 flags_array : intlist
587 .*
588 NAVG : int
589 Number of blocks to be "averaged".
590 nkill : int
591 Number of blocks not to be considered when averaging.
592
593 Example
594 --------
595
596 op = proc_unit.addOperation(name='CrossProdDP', optype='other')
597 op.addParameter(name='NLAG', value='16', format='int')
598 op.addParameter(name='NRANGE', value='0', format='int')
599 op.addParameter(name='NCAL', value='0', format='int')
600 op.addParameter(name='DPL', value='11', format='int')
601 op.addParameter(name='NDN', value='0', format='int')
602 op.addParameter(name='NDT', value='66', format='int')
603 op.addParameter(name='NDP', value='66', format='int')
604 op.addParameter(name='NSCAN', value='132', format='int')
605 op.addParameter(name='flags_array', value='(0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300)', format='intlist')
606 op.addParameter(name='NAVG', value='16', format='int')
607 op.addParameter(name='nkill', value='6', format='int')
608
609 """
527 610
528 #Integration with Overlapping
529 data = numpy.sum(self.__buffer, axis=0)
530 # print data
531 # raise
532 n = self.__profIndex
611 def __init__(self, **kwargs):
533 612
534 return data, n
613 Operation.__init__(self, **kwargs)
614 self.bcounter=0
615 self.aux=1
616 self.lag_products_LP_median_estimates_aux=0
617
618 def set_header_output(self,dataOut):
619
620 dataOut.read_samples=len(dataOut.heightList)#int(dataOut.systemHeaderObj.nSamples/dataOut.windowOfFilter)
621 padding=numpy.zeros(1,'int32')
622 hsize=numpy.zeros(1,'int32')
623 bufsize=numpy.zeros(1,'int32')
624 nr=numpy.zeros(1,'int32')
625 ngates=numpy.zeros(1,'int32') ### ### ### 2
626 time1=numpy.zeros(1,'uint64') # pos 3
627 time2=numpy.zeros(1,'uint64') # pos 4
628 lcounter=numpy.zeros(1,'int32')
629 groups=numpy.zeros(1,'int32')
630 system=numpy.zeros(4,'int8') # pos 7
631 h0=numpy.zeros(1,'float32')
632 dh=numpy.zeros(1,'float32')
633 ipp=numpy.zeros(1,'float32')
634 process=numpy.zeros(1,'int32')
635 tx=numpy.zeros(1,'int32')
636 ngates1=numpy.zeros(1,'int32') ### ### ### 13
637 time0=numpy.zeros(1,'uint64') # pos 14
638 nlags=numpy.zeros(1,'int32')
639 nlags1=numpy.zeros(1,'int32')
640 txb=numpy.zeros(1,'float32') ### ### ### 17
641 time3=numpy.zeros(1,'uint64') # pos 18
642 time4=numpy.zeros(1,'uint64') # pos 19
643 h0_=numpy.zeros(1,'float32')
644 dh_=numpy.zeros(1,'float32')
645 ipp_=numpy.zeros(1,'float32')
646 txa_=numpy.zeros(1,'float32')
647 pad=numpy.zeros(100,'int32')
648 nbytes=numpy.zeros(1,'int32')
649 limits=numpy.zeros(1,'int32')
650 ngroups=numpy.zeros(1,'int32') ### ### ### 27
651
652 dataOut.header=[hsize,bufsize,nr,ngates,time1,time2,
653 lcounter,groups,system,h0,dh,ipp,
654 process,tx,ngates1,padding,time0,nlags,
655 nlags1,padding,txb,time3,time4,h0_,dh_,
656 ipp_,txa_,pad,nbytes,limits,padding,ngroups]
657
658
659 #dataOut.header[1][0]=81864
660 dataOut.FirstHeight=int(dataOut.heightList[0])
661 dataOut.MAXNRANGENDT=max(dataOut.NRANGE,dataOut.NDT)
662 dataOut.header[3][0]=max(dataOut.NRANGE,dataOut.NDT)
663 dataOut.header[7][0]=dataOut.NAVG
664 dataOut.header[9][0]=int(dataOut.heightList[0])
665 dataOut.header[10][0]=dataOut.DH
666 dataOut.header[17][0]=dataOut.DPL
667 dataOut.header[18][0]=dataOut.NLAG
668 #self.header[5][0]=0
669 dataOut.header[15][0]=dataOut.NDP
670 dataOut.header[2][0]=dataOut.NR
671
672
673 def get_products_cabxys(self,dataOut):
674
675 if self.aux==1:
676 self.set_header_output(dataOut)
677 self.aux=0
678
679
680 dataOut.lags_array=[x / dataOut.DH for x in dataOut.flags_array]
681 self.cax=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
682 self.cay=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
683 self.cbx=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
684 self.cby=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
685 self.cax2=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
686 self.cay2=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
687 self.cbx2=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
688 self.cby2=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
689 self.caxbx=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
690 self.caxby=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
691 self.caybx=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
692 self.cayby=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
693 self.caxay=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
694 self.cbxby=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
695
696 for i in range(2):
697 for j in range(dataOut.NDP):
698 for k in range(int(dataOut.NSCAN/2)):
699 n=k%dataOut.DPL
700 ax=dataOut.data[0,2*k+i,j].real
701 ay=dataOut.data[0,2*k+i,j].imag
702 if j+dataOut.lags_array[n]<dataOut.NDP:
703 bx=dataOut.data[1,2*k+i,j+int(dataOut.lags_array[n])].real
704 by=dataOut.data[1,2*k+i,j+int(dataOut.lags_array[n])].imag
705 else:
706 if k+1<int(dataOut.NSCAN/2):
707 bx=dataOut.data[1,2*(k+1)+i,(dataOut.NRANGE+dataOut.NCAL+j+int(dataOut.lags_array[n]))%dataOut.NDP].real
708 by=dataOut.data[1,2*(k+1)+i,(dataOut.NRANGE+dataOut.NCAL+j+int(dataOut.lags_array[n]))%dataOut.NDP].imag
709
710 if k+1==int(dataOut.NSCAN/2):
711 bx=dataOut.data[1,2*k+i,(dataOut.NRANGE+dataOut.NCAL+j+int(dataOut.lags_array[n]))%dataOut.NDP].real
712 by=dataOut.data[1,2*k+i,(dataOut.NRANGE+dataOut.NCAL+j+int(dataOut.lags_array[n]))%dataOut.NDP].imag
713
714 if(k<dataOut.DPL):
715 self.cax[j][n][i]=ax
716 self.cay[j][n][i]=ay
717 self.cbx[j][n][i]=bx
718 self.cby[j][n][i]=by
719 self.cax2[j][n][i]=ax*ax
720 self.cay2[j][n][i]=ay*ay
721 self.cbx2[j][n][i]=bx*bx
722 self.cby2[j][n][i]=by*by
723 self.caxbx[j][n][i]=ax*bx
724 self.caxby[j][n][i]=ax*by
725 self.caybx[j][n][i]=ay*bx
726 self.cayby[j][n][i]=ay*by
727 self.caxay[j][n][i]=ax*ay
728 self.cbxby[j][n][i]=bx*by
729 else:
730 self.cax[j][n][i]+=ax
731 self.cay[j][n][i]+=ay
732 self.cbx[j][n][i]+=bx
733 self.cby[j][n][i]+=by
734 self.cax2[j][n][i]+=ax*ax
735 self.cay2[j][n][i]+=ay*ay
736 self.cbx2[j][n][i]+=bx*bx
737 self.cby2[j][n][i]+=by*by
738 self.caxbx[j][n][i]+=ax*bx
739 self.caxby[j][n][i]+=ax*by
740 self.caybx[j][n][i]+=ay*bx
741 self.cayby[j][n][i]+=ay*by
742 self.caxay[j][n][i]+=ax*ay
743 self.cbxby[j][n][i]+=bx*by
744
745
746 def medi(self,data_navg,NAVG,nkill):
747 sorts=sorted(data_navg)
748 rsorts=numpy.arange(NAVG)
749 result=0.0
750 for k in range(NAVG):
751 if k>=nkill/2 and k<NAVG-nkill/2:
752 result+=sorts[k]*float(NAVG)/(float)(NAVG-nkill)
753 return result
754
755
756 def get_dc(self,dataOut):
757 if self.bcounter==0:
758 dataOut.dc=numpy.zeros(dataOut.NR,dtype='complex64')
759 def cabxys_navg(self,dataOut):
760
761
762 #dataOut.header[5][0]=mktime(strptime(dataOut.TimeBlockDate))
763 dataOut.header[5][0]=dataOut.TimeBlockSeconds
764 #print(dataOut.TimeBlockDate)
765 #print(dataOut.utctime)
766 #print(dataOut.datatime)
767 #print(mktime(strptime(dataOut.TimeBlockDate)))
768 #print(dataOut.header[5][0])
769
770 #dataOut.LastAVGDate=mktime(strptime(dataOut.TimeBlockDate))
771 dataOut.LastAVGDate=dataOut.TimeBlockSeconds
772 #print(dataOut.TimeBlockDate)
773 #print(TimeBlockSeconds)
774 #input()
775 if self.bcounter==0:
776 #dataOut.FirstAVGDate=mktime(strptime(dataOut.TimeBlockDate))
777 dataOut.FirstAVGDate=dataOut.TimeBlockSeconds
778 dataOut.header[4][0]=dataOut.header[5][0]#firsttimeofNAVG
779 if dataOut.CurrentBlock==1:
780 #dataOut.FirstBlockDate=mktime(strptime(dataOut.TimeBlockDate))
781 dataOut.FirstBlockDate=dataOut.TimeBlockSeconds
782 dataOut.header[16][0]=dataOut.header[5][0]#FirsTimeOfTotalBlocks
783
784 self.cax_navg=[]
785 self.cay_navg=[]
786 self.cbx_navg=[]
787 self.cby_navg=[]
788 self.cax2_navg=[]
789 self.cay2_navg=[]
790 self.cbx2_navg=[]
791 self.cby2_navg=[]
792 self.caxbx_navg=[]
793 self.caxby_navg=[]
794 self.caybx_navg=[]
795 self.cayby_navg=[]
796 self.caxay_navg=[]
797 self.cbxby_navg=[]
798
799 dataOut.noisevector=numpy.zeros((dataOut.MAXNRANGENDT,dataOut.NR,dataOut.NAVG),'float32') #30/03/2020
800
801 dataOut.noisevector_=numpy.zeros((dataOut.read_samples,dataOut.NR,dataOut.NAVG),'float32')
802 #dataOut.dc=numpy.zeros(dataOut.NR,dtype=numpy.complex_) #30/03/2020
803 #self.dataOut.noisevector=numpy.zeros((self.dataOut.read_samples,2,self.dataOut.NAVG),'float32') #31/03/2020
804 #self.dataOut.noisevector_=numpy.zeros((self.dataOut.read_samples,2,self.dataOut.NAVG),'float32') #31/03/2020
805 #dataOut.dc=numpy.zeros(dataOut.NR,dtype='complex64')
806 #self.dataOut.dc=numpy.zeros(2,dtype=numpy.complex_) #31/03/2020
807 #self.dataOut.processingHeaderObj.profilesPerBlock
808
809 self.noisevectorizer(dataOut.NSCAN,dataOut.nProfiles,dataOut.NR,dataOut.MAXNRANGENDT,dataOut.noisevector,dataOut.data,dataOut.dc) #30/03/2020
810
811 #print(self.dataOut.noisevector[:,:,:])
812
813 self.cax_navg.append(self.cax)
814 self.cay_navg.append(self.cay)
815 self.cbx_navg.append(self.cbx)
816 self.cby_navg.append(self.cby)
817 self.cax2_navg.append(self.cax2)
818 self.cay2_navg.append(self.cay2)
819 self.cbx2_navg.append(self.cbx2)
820 self.cby2_navg.append(self.cby2)
821 self.caxbx_navg.append(self.caxbx)
822 self.caxby_navg.append(self.caxby)
823 self.caybx_navg.append(self.caybx)
824 self.cayby_navg.append(self.cayby)
825 self.caxay_navg.append(self.caxay)
826 self.cbxby_navg.append(self.cbxby)
827 self.bcounter+=1
828
829 def noise_estimation4x_DP(self,dataOut):
830 if self.bcounter==dataOut.NAVG:
831 dataOut.noise_final=numpy.zeros(dataOut.NR,'float32')
832 snoise=numpy.zeros((dataOut.NR,dataOut.NAVG),'float32')
833 nvector1=numpy.zeros((dataOut.NR,dataOut.NAVG,dataOut.MAXNRANGENDT),'float32')
834 for i in range(dataOut.NR):
835 dataOut.noise_final[i]=0.0
836 for k in range(dataOut.NAVG):
837 snoise[i][k]=0.0
838 for j in range(dataOut.MAXNRANGENDT):
839 nvector1[i][k][j]= dataOut.noisevector[j][i][k];
840 snoise[i][k]=self.noise_hs4x(dataOut.MAXNRANGENDT, nvector1[i][k])
841 #print("snoise",snoise[3,k])
842 dataOut.noise_final[i]=self.noise_hs4x(dataOut.NAVG, snoise[i])
843
844
845
846
847
848
849
850
851 def kabxys(self,dataOut):
852
853
854 #self.cabxys_navg(dataOut)
855
856
857 if self.bcounter==dataOut.NAVG:
858
859 dataOut.flagNoData = False
860
861
862 #dataOut.noise_final=numpy.zeros(dataOut.NR,'float32') #30/03/2020
863 #self.dataOut.noise_final=numpy.zeros(2,'float32') #31/03/2020
864
865
866 self.kax=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
867 self.kay=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
868 self.kbx=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
869 self.kby=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
870 self.kax2=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
871 self.kay2=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
872 self.kbx2=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
873 self.kby2=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
874 self.kaxbx=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
875 self.kaxby=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
876 self.kaybx=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
877 self.kayby=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
878 self.kaxay=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
879 self.kbxby=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
880
881 for i in range(self.cax_navg[0].shape[0]):
882 for j in range(self.cax_navg[0].shape[1]):
883 for k in range(self.cax_navg[0].shape[2]):
884 data_navg=[item[i,j,k] for item in self.cax_navg]
885 self.kax[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
886 data_navg=[item[i,j,k] for item in self.cay_navg]
887 self.kay[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
888 data_navg=[item[i,j,k] for item in self.cbx_navg]
889 self.kbx[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
890 data_navg=[item[i,j,k] for item in self.cby_navg]
891 self.kby[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
892 data_navg=[item[i,j,k] for item in self.cax2_navg]
893 self.kax2[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
894 data_navg=[item[i,j,k] for item in self.cay2_navg]
895 self.kay2[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
896 data_navg=[item[i,j,k] for item in self.cbx2_navg]
897 self.kbx2[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
898 data_navg=[item[i,j,k] for item in self.cby2_navg]
899 self.kby2[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
900 data_navg=[item[i,j,k] for item in self.caxbx_navg]
901 self.kaxbx[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
902 data_navg=[item[i,j,k] for item in self.caxby_navg]
903 self.kaxby[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
904 data_navg=[item[i,j,k] for item in self.caybx_navg]
905 self.kaybx[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
906 data_navg=[item[i,j,k] for item in self.cayby_navg]
907 self.kayby[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
908 data_navg=[item[i,j,k] for item in self.caxay_navg]
909 self.kaxay[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
910 data_navg=[item[i,j,k] for item in self.cbxby_navg]
911 self.kbxby[i,j,k]=self.medi(data_navg,dataOut.NAVG,dataOut.nkill)
912
913
914 dataOut.kax=self.kax
915 dataOut.kay=self.kay
916 dataOut.kbx=self.kbx
917 dataOut.kby=self.kby
918 dataOut.kax2=self.kax2
919 dataOut.kay2=self.kay2
920 dataOut.kbx2=self.kbx2
921 dataOut.kby2=self.kby2
922 dataOut.kaxbx=self.kaxbx
923 dataOut.kaxby=self.kaxby
924 dataOut.kaybx=self.kaybx
925 dataOut.kayby=self.kayby
926 dataOut.kaxay=self.kaxay
927 dataOut.kbxby=self.kbxby
928
929 self.bcounter=0
930
931 dataOut.crossprods=numpy.zeros((3,4,numpy.shape(dataOut.kax)[0],numpy.shape(dataOut.kax)[1],numpy.shape(dataOut.kax)[2]))
932
933 dataOut.crossprods[0]=[dataOut.kax,dataOut.kay,dataOut.kbx,dataOut.kby]
934 dataOut.crossprods[1]=[dataOut.kax2,dataOut.kay2,dataOut.kbx2,dataOut.kby2]
935 dataOut.crossprods[2]=[dataOut.kaxay,dataOut.kbxby,dataOut.kaxbx,dataOut.kaxby]
936 #print("before: ",self.dataOut.noise_final)
937 dataOut.data_for_RTI_DP=numpy.zeros((3,dataOut.NDP))
938 dataOut.data_for_RTI_DP[0],dataOut.data_for_RTI_DP[1],dataOut.data_for_RTI_DP[2]=self.RTI_COLUMN(dataOut.kax2,dataOut.kay2,dataOut.kbx2,dataOut.kby2,dataOut.kaxbx,dataOut.kayby,dataOut.kaybx,dataOut.kaxby, dataOut.NDP)
535 939
536 def byProfiles(self, data):
537 940
538 self.__dataReady = False
539 avgdata = None
540 # n = None
541 # print data
542 # raise
543 self.putData(data)
544 941
545 if self.__profIndex == self.n:
546 avgdata, n = self.pushData()
547 self.__dataReady = True
942 def RTI_COLUMN(self,kax2,kay2,kbx2,kby2,kaxbx,kayby,kaybx,kaxby, NDP):
943 x00=numpy.zeros(NDP,dtype='float32')
944 x01=numpy.zeros(NDP,dtype='float32')
945 x02=numpy.zeros(NDP,dtype='float32')
946 for j in range(2):# first couple lags
947 for k in range(2): #flip
948 for i in range(NDP): #
949 fx=numpy.sqrt((kaxbx[i,j,k]+kayby[i,j,k])**2+(kaybx[i,j,k]-kaxby[i,j,k])**2)
950 x00[i]=x00[i]+(kax2[i,j,k]+kay2[i,j,k])
951 x01[i]=x01[i]+(kbx2[i,j,k]+kby2[i,j,k])
952 x02[i]=x02[i]+fx
953
954 x00[i]=10.0*numpy.log10(x00[i]/4.)
955 x01[i]=10.0*numpy.log10(x01[i]/4.)
956 x02[i]=10.0*numpy.log10(x02[i])
957 return x02,x00,x01
958
959
960
961
962
963
964 #30/03/2020:
965 def noisevectorizer(self,NSCAN,nProfiles,NR,MAXNRANGENDT,noisevector,data,dc):
966
967 rnormalizer= 1./(float(nProfiles - NSCAN))
968 #rnormalizer= float(NSCAN)/((float(nProfiles - NSCAN))*float(MAXNRANGENDT))
969 for i in range(NR):
970 for j in range(MAXNRANGENDT):
971 for k in range(NSCAN,nProfiles):
972 #TODO:integrate just 2nd quartile gates
973 if k==NSCAN:
974 noisevector[j][i][self.bcounter]=(abs(data[i][k][j]-dc[i])**2)*rnormalizer
975 else:
976 noisevector[j][i][self.bcounter]+=(abs(data[i][k][j]-dc[i])**2)*rnormalizer
977
978
979
980
981 def noise_hs4x(self, ndatax, datax):
982 divider=10#divider was originally 10
983 noise=0.0
984 data=numpy.zeros(ndatax,'float32')
985 ndata1=int(ndatax/4)
986 ndata2=int(2.5*(ndatax/4.))
987 ndata=int(ndata2-ndata1)
988 sorts=sorted(datax)
989
990 for k in range(ndata2): # select just second quartile
991 data[k]=sorts[k+ndata1]
992 nums_min= int(ndata/divider)
993 if(int(ndata/divider)> 2):
994 nums_min= int(ndata/divider)
995 else:
996 nums_min=2
997 sump=0.0
998 sumq=0.0
999 j=0
1000 cont=1
1001 while ( (cont==1) and (j<ndata)):
1002 sump+=data[j]
1003 sumq+= data[j]*data[j]
1004 j=j+1
1005 if (j> nums_min):
1006 rtest= float(j/(j-1)) +1.0/ndata
1007 if( (sumq*j) > (rtest*sump*sump ) ):
1008 j=j-1
1009 sump-= data[j]
1010 sumq-=data[j]*data[j]
1011 cont= 0
1012 noise= (sump/j)
1013
1014 return noise
1015
1016
1017
1018 def run(self, dataOut, NLAG=16, NRANGE=0, NCAL=0, DPL=11,
1019 NDN=0, NDT=66, NDP=66, NSCAN=132,
1020 flags_array=(0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300), NAVG=16, nkill=6, **kwargs):
1021
1022 dataOut.NLAG=NLAG
1023 dataOut.NR=len(dataOut.channelList)
1024 dataOut.NRANGE=NRANGE
1025 dataOut.NCAL=NCAL
1026 dataOut.DPL=DPL
1027 dataOut.NDN=NDN
1028 dataOut.NDT=NDT
1029 dataOut.NDP=NDP
1030 dataOut.NSCAN=NSCAN
1031 dataOut.DH=dataOut.heightList[1]-dataOut.heightList[0]
1032 dataOut.H0=int(dataOut.heightList[0])
1033 dataOut.flags_array=flags_array
1034 dataOut.NAVG=NAVG
1035 dataOut.nkill=nkill
1036 dataOut.flagNoData = True
1037
1038 self.get_dc(dataOut)
1039 self.get_products_cabxys(dataOut)
1040 self.cabxys_navg(dataOut)
1041 self.noise_estimation4x_DP(dataOut)
1042 self.kabxys(dataOut)
548 1043
549 return avgdata
1044 return dataOut
550 1045
551 def byTime(self, data, datatime):
552 1046
553 self.__dataReady = False
554 avgdata = None
555 n = None
556 1047
557 self.putData(data)
1048 class IntegrationDP(Operation):
1049 """Operation to integrate the Double Pulse data.
558 1050
559 if (datatime - self.__initime) >= self.__integrationtime:
560 avgdata, n = self.pushData()
561 self.n = n
562 self.__dataReady = True
1051 Parameters:
1052 -----------
1053 nint : int
1054 Number of integrations.
563 1055
564 return avgdata
1056 Example
1057 --------
565 1058
566 def integrateByStride(self, data, datatime):
567 # print data
568 if self.__profIndex == 0:
569 self.__buffer = [[data.copy(), datatime]]
570 else:
571 self.__buffer.append([data.copy(),datatime])
572 self.__profIndex += 1
573 self.__dataReady = False
1059 op = proc_unit.addOperation(name='IntegrationDP', optype='other')
1060 op.addParameter(name='nint', value='30', format='int')
574 1061
575 if self.__profIndex == self.n * self.stride :
576 self.__dataToPutStride = True
577 self.__profIndexStride = 0
578 self.__profIndex = 0
579 self.__bufferStride = []
580 for i in range(self.stride):
581 current = self.__buffer[i::self.stride]
582 data = numpy.sum([t[0] for t in current], axis=0)
583 avgdatatime = numpy.average([t[1] for t in current])
584 # print data
585 self.__bufferStride.append((data, avgdatatime))
1062 """
586 1063
587 if self.__dataToPutStride:
588 self.__dataReady = True
589 self.__profIndexStride += 1
590 if self.__profIndexStride == self.stride:
591 self.__dataToPutStride = False
592 # print self.__bufferStride[self.__profIndexStride - 1]
593 # raise
594 return self.__bufferStride[self.__profIndexStride - 1]
1064 def __init__(self, **kwargs):
595 1065
1066 Operation.__init__(self, **kwargs)
596 1067
597 return None, None
1068 self.counter=0
1069 self.aux=0
1070 self.init_time=None
598 1071
599 def integrate(self, data, datatime=None):
1072 def integration_for_double_pulse(self,dataOut):
1073 #print("inside")
1074 #print(self.aux)
1075 if self.aux==1:
1076 #print("CurrentBlockBBBBB: ",dataOut.CurrentBlock)
1077 #print(dataOut.datatime)
600 1078
601 if self.__initime == None:
602 self.__initime = datatime
1079 #dataOut.TimeBlockDate_for_dp_power=dataOut.TimeBlockDate
1080 ########dataOut.TimeBlockSeconds_for_dp_power=dataOut.LastAVGDate
1081 #print("Date: ",dataOut.TimeBlockDate_for_dp_power)
603 1082
604 if self.__byTime:
605 avgdata = self.byTime(data, datatime)
606 else:
607 avgdata = self.byProfiles(data)
1083 #dataOut.TimeBlockSeconds_for_dp_power=mktime(strptime(dataOut.TimeBlockDate_for_dp_power))
1084 dataOut.TimeBlockSeconds_for_dp_power=dataOut.utctime#dataOut.TimeBlockSeconds-18000
1085 #dataOut.TimeBlockSeconds_for_dp_power=dataOut.LastAVGDate
1086 #print("Seconds: ",dataOut.TimeBlockSeconds_for_dp_power)
1087 dataOut.bd_time=gmtime(dataOut.TimeBlockSeconds_for_dp_power)
1088 #print(dataOut.bd_time)
1089 #exit()
1090 dataOut.year=dataOut.bd_time.tm_year+(dataOut.bd_time.tm_yday-1)/364.0
1091 dataOut.ut_Faraday=dataOut.bd_time.tm_hour+dataOut.bd_time.tm_min/60.0+dataOut.bd_time.tm_sec/3600.0
1092 #print("date: ", dataOut.TimeBlockDate)
608 1093
609 1094
610 self.__lastdatatime = datatime
1095 self.aux=0
611 1096
612 if avgdata is None:
613 return None, None
1097 #print("after")
614 1098
615 avgdatatime = self.__initime
1099 if self.counter==0:
616 1100
617 deltatime = datatime - self.__lastdatatime
1101 tmpx=numpy.zeros((dataOut.NDP,dataOut.DPL,2),'float32')
1102 dataOut.kabxys_integrated=[tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx,tmpx]
1103 self.init_time=dataOut.utctime
618 1104
619 if not self.__withOverlapping:
620 self.__initime = datatime
621 else:
622 self.__initime += deltatime
1105 if self.counter < dataOut.nint:
1106 #print("HERE")
623 1107
624 return avgdata, avgdatatime
1108 dataOut.final_cross_products=[dataOut.kax,dataOut.kay,dataOut.kbx,dataOut.kby,dataOut.kax2,dataOut.kay2,dataOut.kbx2,dataOut.kby2,dataOut.kaxbx,dataOut.kaxby,dataOut.kaybx,dataOut.kayby,dataOut.kaxay,dataOut.kbxby]
625 1109
626 def integrateByBlock(self, dataOut):
1110 for ind in range(len(dataOut.kabxys_integrated)): #final cross products
1111 dataOut.kabxys_integrated[ind]=dataOut.kabxys_integrated[ind]+dataOut.final_cross_products[ind]
627 1112
628 times = int(dataOut.data.shape[1]/self.n)
629 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
1113 self.counter+=1
630 1114
631 id_min = 0
632 id_max = self.n
1115 if self.counter==dataOut.nint-1:
1116 self.aux=1
1117 #dataOut.TimeBlockDate_for_dp_power=dataOut.TimeBlockDate
1118 if self.counter==dataOut.nint:
633 1119
634 for i in range(times):
635 junk = dataOut.data[:,id_min:id_max,:]
636 avgdata[:,i,:] = junk.sum(axis=1)
637 id_min += self.n
638 id_max += self.n
1120 dataOut.flagNoData=False
1121 dataOut.utctime=self.init_time
1122 self.counter=0
639 1123
640 timeInterval = dataOut.ippSeconds*self.n
641 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
642 self.__dataReady = True
643 return avgdata, avgdatatime
644 1124
645 def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs):
1125 def run(self,dataOut,nint=20):
646 1126
647 if not self.isConfig:
648 self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
649 self.isConfig = True
1127 dataOut.flagNoData=True
1128 dataOut.nint=nint
1129 dataOut.paramInterval=0#int(dataOut.nint*dataOut.header[7][0]*2 )
1130 dataOut.lat=-11.95
1131 dataOut.lon=-76.87
650 1132
651 if dataOut.flagDataAsBlock:
652 """
653 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
654 """
655 avgdata, avgdatatime = self.integrateByBlock(dataOut)
656 dataOut.nProfiles /= self.n
657 else:
658 if stride is None:
659 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
660 else:
661 avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime)
1133 self.integration_for_double_pulse(dataOut)
662 1134
1135 return dataOut
663 1136
664 # dataOut.timeInterval *= n
665 dataOut.flagNoData = True
666 1137
667 if self.__dataReady:
668 dataOut.data = avgdata
669 if not dataOut.flagCohInt:
670 dataOut.nCohInt *= self.n
671 dataOut.flagCohInt = True
672 dataOut.utctime = avgdatatime
673 # print avgdata, avgdatatime
674 # raise
675 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
676 dataOut.flagNoData = False
677 return dataOut
1138 class SumFlips(Operation):
1139 """Operation to sum the flip and unflip part of certain cross products of the Double Pulse.
678 1140
679 class Decoder(Operation):
1141 Parameters:
1142 -----------
1143 None
680 1144
681 isConfig = False
682 __profIndex = 0
1145 Example
1146 --------
683 1147
684 code = None
1148 op = proc_unit.addOperation(name='SumFlips', optype='other')
685 1149
686 nCode = None
687 nBaud = None
1150 """
688 1151
689 1152 def __init__(self, **kwargs):
690 1153
691 1154 Operation.__init__(self, **kwargs)
692 1155
693 self.times = None
694 self.osamp = None
695 # self.__setValues = False
696 self.isConfig = False
697 self.setupReq = False
698 def setup(self, code, osamp, dataOut):
699 1156
700 self.__profIndex = 0
1157 def rint2DP(self,dataOut):
701 1158
702 self.code = code
1159 dataOut.rnint2=numpy.zeros(dataOut.DPL,'float32')
703 1160
704 self.nCode = len(code)
705 self.nBaud = len(code[0])
1161 for l in range(dataOut.DPL):
706 1162
707 if (osamp != None) and (osamp >1):
708 self.osamp = osamp
709 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
710 self.nBaud = self.nBaud*self.osamp
1163 dataOut.rnint2[l]=1.0/(dataOut.nint*dataOut.NAVG*12.0)
711 1164
712 self.__nChannels = dataOut.nChannels
713 self.__nProfiles = dataOut.nProfiles
714 self.__nHeis = dataOut.nHeights
715 1165
716 if self.__nHeis < self.nBaud:
717 raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud))
1166 def SumLags(self,dataOut):
718 1167
719 #Frequency
720 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
1168 for l in range(dataOut.DPL):
721 1169
722 __codeBuffer[:,0:self.nBaud] = self.code
1170 dataOut.kabxys_integrated[4][:,l,0]=(dataOut.kabxys_integrated[4][:,l,0]+dataOut.kabxys_integrated[4][:,l,1])*dataOut.rnint2[l]
1171 dataOut.kabxys_integrated[5][:,l,0]=(dataOut.kabxys_integrated[5][:,l,0]+dataOut.kabxys_integrated[5][:,l,1])*dataOut.rnint2[l]
1172 dataOut.kabxys_integrated[6][:,l,0]=(dataOut.kabxys_integrated[6][:,l,0]+dataOut.kabxys_integrated[6][:,l,1])*dataOut.rnint2[l]
1173 dataOut.kabxys_integrated[7][:,l,0]=(dataOut.kabxys_integrated[7][:,l,0]+dataOut.kabxys_integrated[7][:,l,1])*dataOut.rnint2[l]
723 1174
724 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
1175 dataOut.kabxys_integrated[8][:,l,0]=(dataOut.kabxys_integrated[8][:,l,0]-dataOut.kabxys_integrated[8][:,l,1])*dataOut.rnint2[l]
1176 dataOut.kabxys_integrated[9][:,l,0]=(dataOut.kabxys_integrated[9][:,l,0]-dataOut.kabxys_integrated[9][:,l,1])*dataOut.rnint2[l]
1177 dataOut.kabxys_integrated[10][:,l,0]=(dataOut.kabxys_integrated[10][:,l,0]-dataOut.kabxys_integrated[10][:,l,1])*dataOut.rnint2[l]
1178 dataOut.kabxys_integrated[11][:,l,0]=(dataOut.kabxys_integrated[11][:,l,0]-dataOut.kabxys_integrated[11][:,l,1])*dataOut.rnint2[l]
725 1179
726 if dataOut.flagDataAsBlock:
727 1180
728 self.ndatadec = self.__nHeis #- self.nBaud + 1
1181 def run(self,dataOut):
729 1182
730 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
1183 self.rint2DP(dataOut)
1184 self.SumLags(dataOut)
731 1185
732 else:
1186 return dataOut
733 1187
734 #Time
735 self.ndatadec = self.__nHeis #- self.nBaud + 1
736 1188
737 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
1189 class FlagBadHeights(Operation):
1190 """Operation to flag bad heights (bad data) of the Double Pulse.
738 1191
739 def __convolutionInFreq(self, data):
1192 Parameters:
1193 -----------
1194 None
740 1195
741 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
1196 Example
1197 --------
742 1198
743 fft_data = numpy.fft.fft(data, axis=1)
1199 op = proc_unit.addOperation(name='FlagBadHeights', optype='other')
744 1200
745 conv = fft_data*fft_code
1201 """
746 1202
747 data = numpy.fft.ifft(conv,axis=1)
1203 def __init__(self, **kwargs):
748 1204
749 return data
1205 Operation.__init__(self, **kwargs)
750 1206
751 def __convolutionInFreqOpt(self, data):
1207 def run(self,dataOut):
752 1208
753 raise NotImplementedError
1209 dataOut.ibad=numpy.zeros((dataOut.NDP,dataOut.DPL),'int32')
754 1210
755 def __convolutionInTime(self, data):
1211 for j in range(dataOut.NDP):
1212 for l in range(dataOut.DPL):
1213 ip1=j+dataOut.NDP*(0+2*l)
756 1214
757 code = self.code[self.__profIndex]
758 for i in range(self.__nChannels):
759 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
1215 if( (dataOut.kabxys_integrated[5][j,l,0] <= 0.) or (dataOut.kabxys_integrated[4][j,l,0] <= 0.) or (dataOut.kabxys_integrated[7][j,l,0] <= 0.) or (dataOut.kabxys_integrated[6][j,l,0] <= 0.)):
1216 dataOut.ibad[j][l]=1
1217 else:
1218 dataOut.ibad[j][l]=0
760 1219
761 return self.datadecTime
1220 return dataOut
762 1221
763 def __convolutionByBlockInTime(self, data):
1222 class FlagBadHeightsSpectra(Operation):
1223 """Operation to flag bad heights (bad data) of the Double Pulse.
764 1224
765 repetitions = int(self.__nProfiles / self.nCode)
766 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
767 junk = junk.flatten()
768 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
769 profilesList = range(self.__nProfiles)
1225 Parameters:
1226 -----------
1227 None
770 1228
771 for i in range(self.__nChannels):
772 for j in profilesList:
773 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
774 return self.datadecTime
1229 Example
1230 --------
775 1231
776 def __convolutionByBlockInFreq(self, data):
1232 op = proc_unit.addOperation(name='FlagBadHeightsSpectra', optype='other')
777 1233
778 raise NotImplementedError("Decoder by frequency fro Blocks not implemented")
1234 """
779 1235
1236 def __init__(self, **kwargs):
780 1237
781 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
1238 Operation.__init__(self, **kwargs)
782 1239
783 fft_data = numpy.fft.fft(data, axis=2)
1240 def run(self,dataOut):
784 1241
785 conv = fft_data*fft_code
1242 dataOut.ibad=numpy.zeros((dataOut.NDP,dataOut.DPL),'int32')
786 1243
787 data = numpy.fft.ifft(conv,axis=2)
1244 for j in range(dataOut.NDP):
1245 for l in range(dataOut.DPL):
1246 ip1=j+dataOut.NDP*(0+2*l)
788 1247
789 return data
1248 if( (dataOut.kabxys_integrated[4][j,l,0] <= 0.) or (dataOut.kabxys_integrated[6][j,l,0] <= 0.)):
1249 dataOut.ibad[j][l]=1
1250 else:
1251 dataOut.ibad[j][l]=0
790 1252
1253 return dataOut
791 1254
792 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
1255 class NoisePower(Operation):
1256 """Operation to get noise power from the integrated data of the Double Pulse.
793 1257
794 if dataOut.flagDecodeData:
795 print("This data is already decoded, recoding again ...")
1258 Parameters:
1259 -----------
1260 None
796 1261
797 if not self.isConfig:
1262 Example
1263 --------
798 1264
799 if code is None:
800 if dataOut.code is None:
801 raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type)
1265 op = proc_unit.addOperation(name='NoisePower', optype='other')
802 1266
803 code = dataOut.code
1267 """
1268
1269 def __init__(self, **kwargs):
1270
1271 Operation.__init__(self, **kwargs)
1272
1273 def hildebrand(self,dataOut,data):
1274
1275 divider=10 # divider was originally 10
1276 noise=0.0
1277 n1=0
1278 n2=int(dataOut.NDP/2)
1279 sorts= sorted(data)
1280 nums_min= dataOut.NDP/divider
1281 if((dataOut.NDP/divider)> 2):
1282 nums_min= int(dataOut.NDP/divider)
1283
1284 else:
1285 nums_min=2
1286 sump=0.0
1287 sumq=0.0
1288 j=0
1289 cont=1
1290 while( (cont==1) and (j<n2)):
1291 sump+=sorts[j+n1]
1292 sumq+= sorts[j+n1]*sorts[j+n1]
1293 t3= sump/(j+1)
1294 j=j+1
1295 if(j> nums_min):
1296 rtest= float(j/(j-1)) +1.0/dataOut.NAVG
1297 t1= (sumq*j)
1298 t2=(rtest*sump*sump)
1299 if( (t1/t2) > 0.990):
1300 j=j-1
1301 sump-= sorts[j+n1]
1302 sumq-=sorts[j+n1]*sorts[j+n1]
1303 cont= 0
1304
1305 noise= sump/j
1306 stdv=numpy.sqrt((sumq- noise*noise)/(j-1))
1307 return noise
1308
1309 def run(self,dataOut):
1310
1311 p=numpy.zeros((dataOut.NR,dataOut.NDP,dataOut.DPL),'float32')
1312 av=numpy.zeros(dataOut.NDP,'float32')
1313 dataOut.pnoise=numpy.zeros(dataOut.NR,'float32')
1314
1315 p[0,:,:]=dataOut.kabxys_integrated[4][:,:,0]+dataOut.kabxys_integrated[5][:,:,0] #total power for channel 0, just pulse with non-flip
1316 p[1,:,:]=dataOut.kabxys_integrated[6][:,:,0]+dataOut.kabxys_integrated[7][:,:,0] #total power for channel 1
1317
1318 for i in range(dataOut.NR):
1319 dataOut.pnoise[i]=0.0
1320 for k in range(dataOut.DPL):
1321 dataOut.pnoise[i]+= self.hildebrand(dataOut,p[i,:,k])
1322
1323 dataOut.pnoise[i]=dataOut.pnoise[i]/dataOut.DPL
1324
1325
1326 dataOut.pan=1.0*dataOut.pnoise[0] # weights could change
1327 dataOut.pbn=1.0*dataOut.pnoise[1] # weights could change
1328
1329 return dataOut
1330
1331
1332 class DoublePulseACFs(Operation):
1333 """Operation to get the ACFs of the Double Pulse.
1334
1335 Parameters:
1336 -----------
1337 None
1338
1339 Example
1340 --------
1341
1342 op = proc_unit.addOperation(name='DoublePulseACFs', optype='other')
1343
1344 """
1345
1346 def __init__(self, **kwargs):
1347
1348 Operation.__init__(self, **kwargs)
1349 self.aux=1
1350
1351 def run(self,dataOut):
1352
1353 dataOut.igcej=numpy.zeros((dataOut.NDP,dataOut.DPL),'int32')
1354
1355 if self.aux==1:
1356 dataOut.rhor=numpy.zeros((dataOut.NDP,dataOut.DPL), dtype=float)
1357 dataOut.rhoi=numpy.zeros((dataOut.NDP,dataOut.DPL), dtype=float)
1358 dataOut.sdp=numpy.zeros((dataOut.NDP,dataOut.DPL), dtype=float)
1359 dataOut.sd=numpy.zeros((dataOut.NDP,dataOut.DPL), dtype=float)
1360 dataOut.p=numpy.zeros((dataOut.NDP,dataOut.DPL), dtype=float)
1361 dataOut.alag=numpy.zeros(dataOut.NDP,'float32')
1362 for l in range(dataOut.DPL):
1363 dataOut.alag[l]=l*dataOut.DH*2.0/150.0
1364 self.aux=0
1365 sn4=dataOut.pan*dataOut.pbn
1366 rhorn=0
1367 rhoin=0
1368 panrm=numpy.zeros((dataOut.NDP,dataOut.DPL), dtype=float)
1369
1370 for i in range(dataOut.NDP):
1371 for j in range(dataOut.DPL):
1372 ################# Total power
1373 pa=numpy.abs(dataOut.kabxys_integrated[4][i,j,0]+dataOut.kabxys_integrated[5][i,j,0])
1374 pb=numpy.abs(dataOut.kabxys_integrated[6][i,j,0]+dataOut.kabxys_integrated[7][i,j,0])
1375 st4=pa*pb
1376 dataOut.p[i,j]=pa+pb-(dataOut.pan+dataOut.pbn)
1377 dataOut.sdp[i,j]=2*dataOut.rnint2[j]*((pa+pb)*(pa+pb))
1378 ## ACF
1379 rhorp=dataOut.kabxys_integrated[8][i,j,0]+dataOut.kabxys_integrated[11][i,j,0]
1380 rhoip=dataOut.kabxys_integrated[10][i,j,0]-dataOut.kabxys_integrated[9][i,j,0]
1381 if ((pa>dataOut.pan)&(pb>dataOut.pbn)):
1382
1383 ss4=numpy.abs((pa-dataOut.pan)*(pb-dataOut.pbn))
1384 panrm[i,j]=math.sqrt(ss4)
1385 rnorm=1/panrm[i,j]
1386 ## ACF
1387 dataOut.rhor[i,j]=rhorp*rnorm
1388 dataOut.rhoi[i,j]=rhoip*rnorm
1389 ############# Compute standard error for ACF
1390 stoss4=st4/ss4
1391 snoss4=sn4/ss4
1392 rp2=((rhorp*rhorp)+(rhoip*rhoip))/st4
1393 rn2=((rhorn*rhorn)+(rhoin*rhoin))/sn4
1394 rs2=(dataOut.rhor[i,j]*dataOut.rhor[i,j])+(dataOut.rhoi[i,j]*dataOut.rhoi[i,j])
1395 st=1.0+rs2*(stoss4-(2*math.sqrt(stoss4*snoss4)))
1396 stn=1.0+rs2*(snoss4-(2*math.sqrt(stoss4*snoss4)))
1397 dataOut.sd[i,j]=((stoss4*((1.0+rp2)*st+(2.0*rp2*rs2*snoss4)-4.0*math.sqrt(rs2*rp2)))+(0.25*snoss4*((1.0+rn2)*stn+(2.0*rn2*rs2*stoss4)-4.0*math.sqrt(rs2*rn2))))*dataOut.rnint2[j]
1398 dataOut.sd[i,j]=numpy.abs(dataOut.sd[i,j])
1399
1400 else: #default values for bad points
1401 rnorm=1/math.sqrt(st4)
1402 dataOut.sd[i,j]=1.e30
1403 dataOut.ibad[i,j]=4
1404 dataOut.rhor[i,j]=rhorp*rnorm
1405 dataOut.rhoi[i,j]=rhoip*rnorm
1406
1407 if ((pa/dataOut.pan-1.0)>2.25*(pb/dataOut.pbn-1.0)):
1408 dataOut.igcej[i,j]=1
1409
1410 return dataOut
1411
1412
1413 class FaradayAngleAndDPPower(Operation):
1414 """Operation to calculate Faraday angle and Double Pulse power.
1415
1416 Parameters:
1417 -----------
1418 None
1419
1420 Example
1421 --------
1422
1423 op = proc_unit.addOperation(name='FaradayAngleAndDPPower', optype='other')
1424
1425 """
1426
1427 def __init__(self, **kwargs):
1428
1429 Operation.__init__(self, **kwargs)
1430 self.aux=1
1431
1432 def run(self,dataOut):
1433
1434 if self.aux==1:
1435 dataOut.h2=numpy.zeros(dataOut.MAXNRANGENDT,'float32')
1436 dataOut.range1=numpy.zeros(dataOut.MAXNRANGENDT,order='F',dtype='float32')
1437 dataOut.sdn2=numpy.zeros(dataOut.NDP,'float32')
1438 dataOut.ph2=numpy.zeros(dataOut.NDP,'float32')
1439 dataOut.sdp2=numpy.zeros(dataOut.NDP,'float32')
1440 dataOut.ibd=numpy.zeros(dataOut.NDP,'float32')
1441 dataOut.phi=numpy.zeros(dataOut.NDP,'float32')
1442
1443 self.aux=0
1444
1445 for i in range(dataOut.MAXNRANGENDT):
1446 dataOut.range1[i]=dataOut.H0 + i*dataOut.DH
1447 dataOut.h2[i]=dataOut.range1[i]**2
1448
1449 for j in range(dataOut.NDP):
1450 dataOut.ph2[j]=0.
1451 dataOut.sdp2[j]=0.
1452 ri=dataOut.rhoi[j][0]/dataOut.sd[j][0]
1453 rr=dataOut.rhor[j][0]/dataOut.sd[j][0]
1454 dataOut.sdn2[j]=1./dataOut.sd[j][0]
1455
1456 pt=0.# // total power
1457 st=0.# // total signal
1458 ibt=0# // bad lags
1459 ns=0# // no. good lags
1460 for l in range(dataOut.DPL):
1461 #add in other lags if outside of e-jet contamination
1462 if( (dataOut.igcej[j][l] == 0) and (dataOut.ibad[j][l] == 0) ):
1463
1464 dataOut.ph2[j]+=dataOut.p[j][l]/dataOut.sdp[j][l]
1465 dataOut.sdp2[j]=dataOut.sdp2[j]+1./dataOut.sdp[j][l]
1466 ns+=1
1467
1468
1469 pt+=dataOut.p[j][l]/dataOut.sdp[j][l]
1470 st+=1./dataOut.sdp[j][l]
1471 ibt|=dataOut.ibad[j][l];
1472 if(ns!= 0):
1473 dataOut.ibd[j]=0
1474 dataOut.ph2[j]=dataOut.ph2[j]/dataOut.sdp2[j]
1475 dataOut.sdp2[j]=1./dataOut.sdp2[j]
804 1476 else:
805 code = numpy.array(code).reshape(nCode,nBaud)
806 self.setup(code, osamp, dataOut)
1477 dataOut.ibd[j]=ibt
1478 dataOut.ph2[j]=pt/st
1479 dataOut.sdp2[j]=1./st
1480
1481 dataOut.ph2[j]=dataOut.ph2[j]*dataOut.h2[j]
1482 dataOut.sdp2[j]=numpy.sqrt(dataOut.sdp2[j])*dataOut.h2[j]
1483 rr=rr/dataOut.sdn2[j]
1484 ri=ri/dataOut.sdn2[j]
1485 #rm[j]=np.sqrt(rr*rr + ri*ri) it is not used in c program
1486 dataOut.sdn2[j]=1./(dataOut.sdn2[j]*(rr*rr + ri*ri))
1487 if( (ri == 0.) and (rr == 0.) ):
1488 dataOut.phi[j]=0.
1489 else:
1490 dataOut.phi[j]=math.atan2( ri , rr )
807 1491
808 self.isConfig = True
1492 return dataOut
809 1493
810 if mode == 3:
811 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
812 1494
813 if times != None:
814 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
1495 class ElectronDensityFaraday(Operation):
1496 """Operation to calculate electron density from Faraday angle.
815 1497
816 if self.code is None:
817 print("Fail decoding: Code is not defined.")
818 return
1498 Parameters:
1499 -----------
1500 NSHTS : int
1501 .*
1502 RATE : float
1503 .*
819 1504
820 self.__nProfiles = dataOut.nProfiles
821 datadec = None
1505 Example
1506 --------
822 1507
823 if mode == 3:
824 mode = 0
1508 op = proc_unit.addOperation(name='ElectronDensityFaraday', optype='other')
1509 op.addParameter(name='NSHTS', value='50', format='int')
1510 op.addParameter(name='RATE', value='1.8978873e-6', format='float')
825 1511
826 if dataOut.flagDataAsBlock:
827 """
828 Decoding when data have been read as block,
829 """
1512 """
830 1513
831 if mode == 0:
832 datadec = self.__convolutionByBlockInTime(dataOut.data)
833 if mode == 1:
834 datadec = self.__convolutionByBlockInFreq(dataOut.data)
835 else:
836 """
837 Decoding when data have been read profile by profile
838 """
839 if mode == 0:
840 datadec = self.__convolutionInTime(dataOut.data)
1514 def __init__(self, **kwargs):
841 1515
842 if mode == 1:
843 datadec = self.__convolutionInFreq(dataOut.data)
1516 Operation.__init__(self, **kwargs)
1517 self.aux=1
1518
1519 def run(self,dataOut,NSHTS=50,RATE=1.8978873e-6):
1520
1521 #print(ctime(dataOut.utctime))
1522 #3print("Faraday Angle",dataOut.phi)
1523
1524 dataOut.NSHTS=NSHTS
1525 dataOut.RATE=RATE
1526
1527 if self.aux==1:
1528 dataOut.dphi=numpy.zeros(dataOut.NDP,'float32')
1529 dataOut.sdn1=numpy.zeros(dataOut.NDP,'float32')
1530 self.aux=0
1531 theta=numpy.zeros(dataOut.NDP,dtype=numpy.complex_)
1532 thetai=numpy.zeros(dataOut.NDP,dtype=numpy.complex_)
1533 # use complex numbers for phase
1534 for i in range(dataOut.NSHTS):
1535 theta[i]=math.cos(dataOut.phi[i])+math.sin(dataOut.phi[i])*1j
1536 thetai[i]=-math.sin(dataOut.phi[i])+math.cos(dataOut.phi[i])*1j
1537
1538 # differentiate and convert to number density
1539 ndphi=dataOut.NSHTS-4
1540 for i in range(2,dataOut.NSHTS-2):
1541 fact=(-0.5/(dataOut.RATE*dataOut.DH))*dataOut.bki[i]
1542 #four-point derivative, no phase unwrapping necessary
1543 ####dataOut.dphi[i]=((((theta[i+1]-theta[i-1])+(2.0*(theta[i+2]-theta[i-2])))/thetai[i])).real/10.0
1544 dataOut.dphi[i]=((((theta[i-2]-theta[i+2])+(8.0*(theta[i+1]-theta[i-1])))/thetai[i])).real/12.0
1545
1546 dataOut.dphi[i]=abs(dataOut.dphi[i]*fact)
1547 dataOut.sdn1[i]=(4.*(dataOut.sdn2[i-2]+dataOut.sdn2[i+2])+dataOut.sdn2[i-1]+dataOut.sdn2[i+1])
1548 dataOut.sdn1[i]=numpy.sqrt(dataOut.sdn1[i])*fact
844 1549
845 if mode == 2:
846 datadec = self.__convolutionInFreqOpt(dataOut.data)
1550 return dataOut
847 1551
848 if datadec is None:
849 raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode)
1552 class ElectronDensityRobertoTestFaraday(Operation):
1553 """Operation to calculate electron density from Faraday angle.
850 1554
851 dataOut.code = self.code
852 dataOut.nCode = self.nCode
853 dataOut.nBaud = self.nBaud
1555 Parameters:
1556 -----------
1557 NSHTS : int
1558 .*
1559 RATE : float
1560 .*
854 1561
855 dataOut.data = datadec
1562 Example
1563 --------
856 1564
857 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
1565 op = proc_unit.addOperation(name='ElectronDensityFaraday', optype='other')
1566 op.addParameter(name='NSHTS', value='50', format='int')
1567 op.addParameter(name='RATE', value='1.8978873e-6', format='float')
858 1568
859 dataOut.flagDecodeData = True #asumo q la data esta decodificada
1569 """
860 1570
861 if self.__profIndex == self.nCode-1:
862 self.__profIndex = 0
863 return dataOut
1571 def __init__(self, **kwargs):
864 1572
865 self.__profIndex += 1
1573 Operation.__init__(self, **kwargs)
1574 self.aux=1
1575
1576 def run(self,dataOut,NSHTS=50,RATE=1.8978873e-6):
1577
1578 #print(ctime(dataOut.utctime))
1579 #print("Faraday Angle",dataOut.phi)
1580
1581 dataOut.NSHTS=NSHTS
1582 dataOut.RATE=RATE
1583
1584 if self.aux==1:
1585 dataOut.dphi=numpy.zeros(dataOut.NDP,'float32')
1586 dataOut.sdn1=numpy.zeros(dataOut.NDP,'float32')
1587 self.aux=0
1588 theta=numpy.zeros(dataOut.NDP,dtype=numpy.complex_)
1589 thetai=numpy.zeros(dataOut.NDP,dtype=numpy.complex_)
1590 # use complex numbers for phase
1591 '''
1592 for i in range(dataOut.NSHTS):
1593 theta[i]=math.cos(dataOut.phi[i])+math.sin(dataOut.phi[i])*1j
1594 thetai[i]=-math.sin(dataOut.phi[i])+math.cos(dataOut.phi[i])*1j
1595 '''
1596
1597 # differentiate and convert to number density
1598 ndphi=dataOut.NSHTS-4
1599
1600 dataOut.phi=numpy.unwrap(dataOut.phi)
1601
1602 for i in range(2,dataOut.NSHTS-2):
1603 fact=(-0.5/(dataOut.RATE*dataOut.DH))*dataOut.bki[i]
1604 #four-point derivative, no phase unwrapping necessary
1605 ####dataOut.dphi[i]=((((theta[i+1]-theta[i-1])+(2.0*(theta[i+2]-theta[i-2])))/thetai[i])).real/10.0
1606 ##dataOut.dphi[i]=((((theta[i-2]-theta[i+2])+(8.0*(theta[i+1]-theta[i-1])))/thetai[i])).real/12.0
1607 dataOut.dphi[i]=((dataOut.phi[i+1]-dataOut.phi[i-1])+(2.0*(dataOut.phi[i+2]-dataOut.phi[i-2])))/10.0
1608
1609 dataOut.dphi[i]=abs(dataOut.dphi[i]*fact)
1610 dataOut.sdn1[i]=(4.*(dataOut.sdn2[i-2]+dataOut.sdn2[i+2])+dataOut.sdn2[i-1]+dataOut.sdn2[i+1])
1611 dataOut.sdn1[i]=numpy.sqrt(dataOut.sdn1[i])*fact
866 1612
867 1613 return dataOut
868 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
869 1614
1615 class ElectronDensityRobertoTest2Faraday(Operation):
1616 """Operation to calculate electron density from Faraday angle.
870 1617
871 class ProfileConcat(Operation):
1618 Parameters:
1619 -----------
1620 NSHTS : int
1621 .*
1622 RATE : float
1623 .*
872 1624
873 isConfig = False
874 buffer = None
1625 Example
1626 --------
1627
1628 op = proc_unit.addOperation(name='ElectronDensityFaraday', optype='other')
1629 op.addParameter(name='NSHTS', value='50', format='int')
1630 op.addParameter(name='RATE', value='1.8978873e-6', format='float')
1631
1632 """
875 1633
876 1634 def __init__(self, **kwargs):
877 1635
878 1636 Operation.__init__(self, **kwargs)
879 self.profileIndex = 0
1637 self.aux=1
880 1638
881 def reset(self):
882 self.buffer = numpy.zeros_like(self.buffer)
883 self.start_index = 0
884 self.times = 1
1639 def run(self,dataOut,NSHTS=50,RATE=1.8978873e-6):
885 1640
886 def setup(self, data, m, n=1):
887 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
888 self.nHeights = data.shape[1]#.nHeights
889 self.start_index = 0
890 self.times = 1
1641 #print(ctime(dataOut.utctime))
1642 #print("Faraday Angle",dataOut.phi)
891 1643
892 def concat(self, data):
1644 dataOut.NSHTS=NSHTS
1645 dataOut.RATE=RATE
893 1646
894 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
895 self.start_index = self.start_index + self.nHeights
1647 if self.aux==1:
1648 dataOut.dphi=numpy.zeros(dataOut.NDP,'float32')
1649 dataOut.sdn1=numpy.zeros(dataOut.NDP,'float32')
1650 self.aux=0
1651 theta=numpy.zeros(dataOut.NDP,dtype=numpy.complex_)
1652 thetai=numpy.zeros(dataOut.NDP,dtype=numpy.complex_)
1653 # use complex numbers for phase
1654 '''
1655 for i in range(dataOut.NSHTS):
1656 theta[i]=math.cos(dataOut.phi[i])+math.sin(dataOut.phi[i])*1j
1657 thetai[i]=-math.sin(dataOut.phi[i])+math.cos(dataOut.phi[i])*1j
1658 '''
896 1659
897 def run(self, dataOut, m):
898 dataOut.flagNoData = True
1660 # differentiate and convert to number density
1661 ndphi=dataOut.NSHTS-4
899 1662
900 if not self.isConfig:
901 self.setup(dataOut.data, m, 1)
902 self.isConfig = True
1663 #dataOut.phi=numpy.unwrap(dataOut.phi)
1664 f1=numpy.exp((dataOut.phi*1.j)/10)
1665 f2=numpy.exp((dataOut.phi*2.j)/10)
903 1666
904 if dataOut.flagDataAsBlock:
905 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
1667 for i in range(2,dataOut.NSHTS-2):
1668 fact=(-0.5/(dataOut.RATE*dataOut.DH))*dataOut.bki[i]
1669 #four-point derivative, no phase unwrapping necessary
1670 ####dataOut.dphi[i]=((((theta[i+1]-theta[i-1])+(2.0*(theta[i+2]-theta[i-2])))/thetai[i])).real/10.0
1671 ##dataOut.dphi[i]=((((theta[i-2]-theta[i+2])+(8.0*(theta[i+1]-theta[i-1])))/thetai[i])).real/12.0
1672 ##dataOut.dphi[i]=((dataOut.phi[i+1]-dataOut.phi[i-1])+(2.0*(dataOut.phi[i+2]-dataOut.phi[i-2])))/10.0
906 1673
1674 dataOut.dphi[i]=numpy.angle(f1[i+1]*numpy.conjugate(f1[i-1])*f2[i+2]*numpy.conjugate(f2[i-2]))
1675
1676
1677 dataOut.dphi[i]=abs(dataOut.dphi[i]*fact)
1678 dataOut.sdn1[i]=(4.*(dataOut.sdn2[i-2]+dataOut.sdn2[i+2])+dataOut.sdn2[i-1]+dataOut.sdn2[i+1])
1679 dataOut.sdn1[i]=numpy.sqrt(dataOut.sdn1[i])*fact
1680
1681 return dataOut
1682
1683 class NormalizeDPPower(Operation):
1684 """Operation to normalize relative electron density from power with total electron density from Farday angle.
1685
1686 Parameters:
1687 -----------
1688 None
1689
1690 Example
1691 --------
1692
1693 op = proc_unit.addOperation(name='NormalizeDPPower', optype='other')
1694
1695 """
1696
1697 def __init__(self, **kwargs):
1698
1699 Operation.__init__(self, **kwargs)
1700 self.aux=1
1701
1702 def normal(self,a,b,n,m):
1703 chmin=1.0e30
1704 chisq=numpy.zeros(150,'float32')
1705 temp=numpy.zeros(150,'float32')
1706
1707 for i in range(2*m-1):
1708 an=al=be=chisq[i]=0.0
1709 for j in range(int(n/m)):
1710 k=int(j+i*n/(2*m))
1711 if(a[k]>0.0 and b[k]>0.0):
1712 al+=a[k]*b[k]
1713 be+=b[k]*b[k]
1714
1715 if(be>0.0):
1716 temp[i]=al/be
1717 else:
1718 temp[i]=1.0
1719
1720 for j in range(int(n/m)):
1721 k=int(j+i*n/(2*m))
1722 if(a[k]>0.0 and b[k]>0.0):
1723 chisq[i]+=(numpy.log10(b[k]*temp[i]/a[k]))**2
1724 an=an+1
1725
1726 if(chisq[i]>0.0):
1727 chisq[i]/=an
1728
1729 for i in range(int(2*m-1)):
1730 if(chisq[i]<chmin and chisq[i]>1.0e-6):
1731 chmin=chisq[i]
1732 cf=temp[i]
1733 return cf
1734
1735 def normalize(self,dataOut):
1736
1737 if self.aux==1:
1738 dataOut.cf=numpy.zeros(1,'float32')
1739 dataOut.cflast=numpy.zeros(1,'float32')
1740 self.aux=0
1741
1742 night_first=300.0
1743 night_first1= 310.0
1744 night_end= 450.0
1745 day_first=250.0
1746 day_end=400.0
1747 day_first_sunrise=190.0
1748 day_end_sunrise=280.0
1749
1750 print(dataOut.ut_Faraday)
1751 if(dataOut.ut_Faraday>4.0 and dataOut.ut_Faraday<11.0): #early
1752 print("EARLY")
1753 i2=(night_end-dataOut.range1[0])/dataOut.DH
1754 i1=(night_first -dataOut.range1[0])/dataOut.DH
1755 elif (dataOut.ut_Faraday>0.0 and dataOut.ut_Faraday<4.0): #night
1756 print("NIGHT")
1757 i2=(night_end-dataOut.range1[0])/dataOut.DH
1758 i1=(night_first1 -dataOut.range1[0])/dataOut.DH
1759 elif (dataOut.ut_Faraday>=11.0 and dataOut.ut_Faraday<13.5): #sunrise
1760 print("SUNRISE")
1761 i2=( day_end_sunrise-dataOut.range1[0])/dataOut.DH
1762 i1=(day_first_sunrise - dataOut.range1[0])/dataOut.DH
907 1763 else:
908 self.concat(dataOut.data)
909 self.times += 1
910 if self.times > m:
911 dataOut.data = self.buffer
912 self.reset()
913 dataOut.flagNoData = False
914 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
915 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
916 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
917 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
918 dataOut.ippSeconds *= m
1764 print("ELSE")
1765 i2=(day_end-dataOut.range1[0])/dataOut.DH
1766 i1=(day_first -dataOut.range1[0])/dataOut.DH
1767 #print(i1*dataOut.DH)
1768 #print(i2*dataOut.DH)
1769
1770 i1=int(i1)
1771 i2=int(i2)
1772
1773 try:
1774 dataOut.cf=self.normal(dataOut.dphi[i1::], dataOut.ph2[i1::], i2-i1, 1)
1775 except:
1776 pass
1777
1778 #print(dataOut.ph2)
1779 #input()
1780 # in case of spread F, normalize much higher
1781 if(dataOut.cf<dataOut.cflast[0]/10.0):
1782 i1=(night_first1+100.-dataOut.range1[0])/dataOut.DH
1783 i2=(night_end+100.0-dataOut.range1[0])/dataOut.DH
1784 i1=int(i1)
1785 i2=int(i2)
1786 try:
1787 dataOut.cf=self.normal(dataOut.dphi[int(i1)::], dataOut.ph2[int(i1)::], int(i2-i1), 1)
1788 except:
1789 pass
1790
1791 dataOut.cflast[0]=dataOut.cf
1792
1793 ## normalize double pulse power and error bars to Faraday
1794 for i in range(dataOut.NSHTS):
1795 dataOut.ph2[i]*=dataOut.cf
1796 dataOut.sdp2[i]*=dataOut.cf
1797 #print(dataOut.ph2)
1798 #input()
1799
1800 for i in range(dataOut.NSHTS):
1801 dataOut.ph2[i]=(max(1.0, dataOut.ph2[i]))
1802 dataOut.dphi[i]=(max(1.0, dataOut.dphi[i]))
1803
1804
1805 def run(self,dataOut):
1806
1807 self.normalize(dataOut)
1808 #print(dataOut.ph2)
1809 #print(dataOut.sdp2)
1810 #input()
1811
1812
1813 return dataOut
1814
1815 class NormalizeDPPowerRoberto(Operation):
1816 """Operation to normalize relative electron density from power with total electron density from Farday angle.
1817
1818 Parameters:
1819 -----------
1820 None
1821
1822 Example
1823 --------
1824
1825 op = proc_unit.addOperation(name='NormalizeDPPower', optype='other')
1826
1827 """
1828
1829 def __init__(self, **kwargs):
1830
1831 Operation.__init__(self, **kwargs)
1832 self.aux=1
1833
1834 def normal(self,a,b,n,m):
1835 chmin=1.0e30
1836 chisq=numpy.zeros(150,'float32')
1837 temp=numpy.zeros(150,'float32')
1838
1839 for i in range(2*m-1):
1840 an=al=be=chisq[i]=0.0
1841 for j in range(int(n/m)):
1842 k=int(j+i*n/(2*m))
1843 if(a[k]>0.0 and b[k]>0.0):
1844 al+=a[k]*b[k]
1845 be+=b[k]*b[k]
1846
1847 if(be>0.0):
1848 temp[i]=al/be
1849 else:
1850 temp[i]=1.0
1851
1852 for j in range(int(n/m)):
1853 k=int(j+i*n/(2*m))
1854 if(a[k]>0.0 and b[k]>0.0):
1855 chisq[i]+=(numpy.log10(b[k]*temp[i]/a[k]))**2
1856 an=an+1
1857
1858 if(chisq[i]>0.0):
1859 chisq[i]/=an
1860
1861 for i in range(int(2*m-1)):
1862 if(chisq[i]<chmin and chisq[i]>1.0e-6):
1863 chmin=chisq[i]
1864 cf=temp[i]
1865 return cf
1866
1867 def normalize(self,dataOut):
1868
1869 if self.aux==1:
1870 dataOut.cf=numpy.zeros(1,'float32')
1871 dataOut.cflast=numpy.zeros(1,'float32')
1872 self.aux=0
1873
1874 night_first=300.0
1875 night_first1= 310.0
1876 night_end= 450.0
1877 day_first=250.0
1878 day_end=400.0
1879 day_first_sunrise=190.0
1880 day_end_sunrise=350.0
1881
1882 print(dataOut.ut_Faraday)
1883 '''
1884 if(dataOut.ut_Faraday>4.0 and dataOut.ut_Faraday<11.0): #early
1885 print("EARLY")
1886 i2=(night_end-dataOut.range1[0])/dataOut.DH
1887 i1=(night_first -dataOut.range1[0])/dataOut.DH
1888 elif (dataOut.ut_Faraday>0.0 and dataOut.ut_Faraday<4.0): #night
1889 print("NIGHT")
1890 i2=(night_end-dataOut.range1[0])/dataOut.DH
1891 i1=(night_first1 -dataOut.range1[0])/dataOut.DH
1892 elif (dataOut.ut_Faraday>=11.0 and dataOut.ut_Faraday<13.5): #sunrise
1893 print("SUNRISE")
1894 i2=( day_end_sunrise-dataOut.range1[0])/dataOut.DH
1895 i1=(day_first_sunrise - dataOut.range1[0])/dataOut.DH
1896 else:
1897 print("ELSE")
1898 i2=(day_end-dataOut.range1[0])/dataOut.DH
1899 i1=(day_first -dataOut.range1[0])/dataOut.DH
1900 '''
1901 i2=(420-dataOut.range1[0])/dataOut.DH
1902 i1=(200 -dataOut.range1[0])/dataOut.DH
1903 print(i1*dataOut.DH)
1904 print(i2*dataOut.DH)
1905
1906 i1=int(i1)
1907 i2=int(i2)
1908
1909 try:
1910 dataOut.cf=self.normal(dataOut.dphi[i1::], dataOut.ph2[i1::], i2-i1, 1)
1911 except:
1912 pass
1913
1914 #print(dataOut.ph2)
1915 #input()
1916 # in case of spread F, normalize much higher
1917 if(dataOut.cf<dataOut.cflast[0]/10.0):
1918 i1=(night_first1+100.-dataOut.range1[0])/dataOut.DH
1919 i2=(night_end+100.0-dataOut.range1[0])/dataOut.DH
1920 i1=int(i1)
1921 i2=int(i2)
1922 try:
1923 dataOut.cf=self.normal(dataOut.dphi[int(i1)::], dataOut.ph2[int(i1)::], int(i2-i1), 1)
1924 except:
1925 pass
1926
1927 dataOut.cflast[0]=dataOut.cf
1928
1929 ## normalize double pulse power and error bars to Faraday
1930 for i in range(dataOut.NSHTS):
1931 dataOut.ph2[i]*=dataOut.cf
1932 dataOut.sdp2[i]*=dataOut.cf
1933 #print(dataOut.ph2)
1934 #input()
1935
1936 for i in range(dataOut.NSHTS):
1937 dataOut.ph2[i]=(max(1.0, dataOut.ph2[i]))
1938 dataOut.dphi[i]=(max(1.0, dataOut.dphi[i]))
1939
1940
1941 def run(self,dataOut):
1942
1943 self.normalize(dataOut)
1944 #print(dataOut.ph2)
1945 #print(dataOut.sdp2)
1946 #input()
1947
1948
1949 return dataOut
1950
1951
1952 class suppress_stdout_stderr(object):
1953 '''
1954 A context manager for doing a "deep suppression" of stdout and stderr in
1955 Python, i.e. will suppress all print, even if the print originates in a
1956 compiled C/Fortran sub-function.
1957 This will not suppress raised exceptions, since exceptions are printed
1958 to stderr just before a script exits, and after the context manager has
1959 exited (at least, I think that is why it lets exceptions through).
1960
1961 '''
1962 def __init__(self):
1963 # Open a pair of null files
1964 self.null_fds = [os.open(os.devnull,os.O_RDWR) for x in range(2)]
1965 # Save the actual stdout (1) and stderr (2) file descriptors.
1966 self.save_fds = [os.dup(1), os.dup(2)]
1967
1968 def __enter__(self):
1969 # Assign the null pointers to stdout and stderr.
1970 os.dup2(self.null_fds[0],1)
1971 os.dup2(self.null_fds[1],2)
1972
1973 def __exit__(self, *_):
1974 # Re-assign the real stdout/stderr back to (1) and (2)
1975 os.dup2(self.save_fds[0],1)
1976 os.dup2(self.save_fds[1],2)
1977 # Close all file descriptors
1978 for fd in self.null_fds + self.save_fds:
1979 os.close(fd)
1980
1981
1982 from schainpy.model.proc import fitacf_guess
1983 from schainpy.model.proc import fitacf_fit_short
1984 class DPTemperaturesEstimation(Operation):
1985 """Operation to estimate temperatures for Double Pulse data.
1986
1987 Parameters:
1988 -----------
1989 IBITS : int
1990 .*
1991
1992 Example
1993 --------
1994
1995 op = proc_unit.addOperation(name='DPTemperaturesEstimation', optype='other')
1996 op.addParameter(name='IBITS', value='16', format='int')
1997
1998 """
1999
2000 def __init__(self, **kwargs):
2001
2002 Operation.__init__(self, **kwargs)
2003
2004 self.aux=1
2005
2006 def Estimation(self,dataOut):
2007 with suppress_stdout_stderr():
2008 #if True:
2009
2010 if self.aux==1:
2011 dataOut.ifit=numpy.zeros(5,order='F',dtype='int32')
2012 dataOut.m=numpy.zeros(1,order='F',dtype='int32')
2013 dataOut.te2=numpy.zeros(dataOut.NSHTS,order='F',dtype='float32')
2014 dataOut.ti2=numpy.zeros(dataOut.NSHTS,order='F',dtype='float32')
2015 dataOut.ete2=numpy.zeros(dataOut.NSHTS,order='F',dtype='float32')
2016 dataOut.eti2=numpy.zeros(dataOut.NSHTS,order='F',dtype='float32')
2017
2018 self.aux=0
2019
2020 dataOut.phy2=numpy.zeros(dataOut.NSHTS,order='F',dtype='float32')
2021 dataOut.ephy2=numpy.zeros(dataOut.NSHTS,order='F',dtype='float32')
2022 dataOut.info2=numpy.zeros(dataOut.NDP,order='F',dtype='float32')
2023 dataOut.params=numpy.zeros(10,order='F',dtype='float32')
2024 dataOut.cov=numpy.zeros(dataOut.IBITS*dataOut.IBITS,order='F',dtype='float32')
2025 dataOut.covinv=numpy.zeros(dataOut.IBITS*dataOut.IBITS,order='F',dtype='float32')
2026
2027 #null_fd = os.open(os.devnull, os.O_RDWR)
2028 #os.dup2(null_fd, 1)
2029
2030 for i in range(10,dataOut.NSHTS): #no point below 150 km
2031
2032 #some definitions
2033 iflag=0 # inicializado a cero?
2034 wl = 3.0
2035 x=numpy.zeros(dataOut.DPL+dataOut.IBITS,order='F',dtype='float32')
2036 y=numpy.zeros(dataOut.DPL+dataOut.IBITS,order='F',dtype='float32')
2037 e=numpy.zeros(dataOut.DPL+dataOut.IBITS,order='F',dtype='float32')
2038 eb=numpy.zeros(5,order='F',dtype='float32')
2039 zero=numpy.zeros(1,order='F',dtype='float32')
2040 depth=numpy.zeros(1,order='F',dtype='float32')
2041 t1=numpy.zeros(1,order='F',dtype='float32')
2042 t2=numpy.zeros(1,order='F',dtype='float32')
2043
2044 if i>10 and l1>=0:
2045 if l1==0:
2046 l1=1
2047
2048 dataOut.cov=numpy.reshape(dataOut.cov,l1*l1)
2049 dataOut.cov=numpy.resize(dataOut.cov,dataOut.DPL*dataOut.DPL)
2050 dataOut.covinv=numpy.reshape(dataOut.covinv,l1*l1)
2051 dataOut.covinv=numpy.resize(dataOut.covinv,dataOut.DPL*dataOut.DPL)
2052
2053 for l in range(dataOut.DPL*dataOut.DPL):
2054 dataOut.cov[l]=0.0
2055 acfm= (dataOut.rhor[i][0])**2 + (dataOut.rhoi[i][0])**2
2056 if acfm> 0.0:
2057 cc=dataOut.rhor[i][0]/acfm
2058 ss=dataOut.rhoi[i][0]/acfm
2059 else:
2060 cc=1.
2061 ss=0.
2062 # keep only uncontaminated data, don't pass zero lag to fitter
2063 l1=0
2064 for l in range(0+1,dataOut.DPL):
2065 if dataOut.igcej[i][l]==0 and dataOut.ibad[i][l]==0:
2066 y[l1]=dataOut.rhor[i][l]*cc + dataOut.rhoi[i][l]*ss
2067 x[l1]=dataOut.alag[l]*1.0e-3
2068 dataOut.sd[i][l]=dataOut.sd[i][l]/((acfm)**2)# important
2069 e[l1]=dataOut.sd[i][l] #this is the variance, not the st. dev.
2070 l1=l1+1
2071
2072 for l in range(l1*(l1+1)):
2073 dataOut.cov[l]=0.0
2074 for l in range(l1):
2075 dataOut.cov[l*(1+l1)]=e[l]
2076 angle=dataOut.thb[i]*0.01745
2077 bm=dataOut.bfm[i]
2078 dataOut.params[0]=1.0 #norm
2079 dataOut.params[1]=1000.0 #te
2080 dataOut.params[2]=800.0 #ti
2081 dataOut.params[3]=0.00 #ph
2082 dataOut.params[4]=0.00 #phe
2083
2084 if l1!=0:
2085 x=numpy.resize(x,l1)
2086 y=numpy.resize(y,l1)
2087 else:
2088 x=numpy.resize(x,1)
2089 y=numpy.resize(y,1)
2090
2091 if True: #len(y)!=0:
2092
2093 fitacf_guess.guess(y,x,zero,depth,t1,t2,len(y))
2094 t2=t1/t2
2095
2096
2097
2098
2099 if (t1<5000.0 and t1> 600.0):
2100 dataOut.params[1]=t1
2101 dataOut.params[2]=min(t2,t1)
2102 dataOut.ifit[1]=dataOut.ifit[2]=1
2103 dataOut.ifit[0]=dataOut.ifit[3]=dataOut.ifit[4]=0
2104 #print(dataOut.ut_Faraday)
2105 if dataOut.ut_Faraday<10.0 and dataOut.ut_Faraday>=0.5:
2106 dataOut.ifit[2]=0
2107
2108 den=dataOut.ph2[i]
2109
2110 if l1!=0:
2111 dataOut.covinv=dataOut.covinv[0:l1*l1].reshape((l1,l1))
2112 dataOut.cov=dataOut.cov[0:l1*l1].reshape((l1,l1))
2113 e=numpy.resize(e,l1)
2114 else:
2115 dataOut.covinv=numpy.resize(dataOut.covinv,1)
2116 dataOut.cov=numpy.resize(dataOut.cov,1)
2117 e=numpy.resize(e,1)
2118
2119 eb=numpy.resize(eb,10)
2120 dataOut.ifit=numpy.resize(dataOut.ifit,10)
2121
2122
2123
2124 dataOut.covinv,e,dataOut.params,eb,dataOut.m=fitacf_fit_short.fit(wl,x,y,dataOut.cov,dataOut.covinv,e,dataOut.params,bm,angle,den,dataOut.range1[i],dataOut.year,dataOut.ifit,dataOut.m,l1) #
2125
2126 #exit()
2127
2128 if dataOut.params[2]>dataOut.params[1]*1.05:
2129 dataOut.ifit[2]=0
2130 dataOut.params[1]=dataOut.params[2]=t1
2131 dataOut.covinv,e,dataOut.params,eb,dataOut.m=fitacf_fit_short.fit(wl,x,y,dataOut.cov,dataOut.covinv,e,dataOut.params,bm,angle,den,dataOut.range1[i],dataOut.year,dataOut.ifit,dataOut.m,l1) #
2132
2133 if (dataOut.ifit[2]==0):
2134 dataOut.params[2]=dataOut.params[1]
2135 if (dataOut.ifit[3]==0 and iflag==0):
2136 dataOut.params[3]=0.0
2137 if (dataOut.ifit[4]==0):
2138 dataOut.params[4]=0.0
2139 dataOut.te2[i]=dataOut.params[1]
2140 dataOut.ti2[i]=dataOut.params[2]
2141 dataOut.ete2[i]=eb[1]
2142 dataOut.eti2[i]=eb[2]
2143
2144 if dataOut.eti2[i]==0:
2145 dataOut.eti2[i]=dataOut.ete2[i]
2146
2147 dataOut.phy2[i]=dataOut.params[3]
2148 dataOut.ephy2[i]=eb[3]
2149 if(iflag==1):
2150 dataOut.ephy2[i]=0.0
2151
2152 if (dataOut.m<=3 and dataOut.m!= 0 and dataOut.te2[i]>400.0):
2153 dataOut.info2[i]=1
2154 else:
2155 dataOut.info2[i]=0
2156
2157 def run(self,dataOut,IBITS=16):
2158
2159 dataOut.IBITS = IBITS
2160
2161 self.Estimation(dataOut)
2162
2163
919 2164 return dataOut
920 2165
921 class ProfileSelector(Operation):
2166 class NeTeTiRecal(NormalizeDPPower,DPTemperaturesEstimation):
2167
2168 def __init__(self, **kwargs):
2169
2170 Operation.__init__(self, **kwargs)
2171 self.aux=0
2172
2173 def run(self,dataOut):
2174
2175 for i in range(dataOut.NSHTS):
2176 print("H: ",i*15)
2177 print(1+(dataOut.te2[i]/dataOut.ti2[i]))
2178 dataOut.ph2[i]*=1+(dataOut.te2[i]/dataOut.ti2[i])
2179
2180 self.normalize(dataOut)
2181 self.Estimation(dataOut)
2182
2183
2184 return dataOut
2185
2186
2187 from schainpy.model.proc import fitacf_acf2
2188 class DenCorrection(Operation):
2189
2190 def __init__(self, **kwargs):
2191
2192 Operation.__init__(self, **kwargs)
2193
2194
2195 def run(self,dataOut):
2196
2197 y=numpy.zeros(dataOut.DPL,order='F',dtype='float32')
2198 #y_aux = numpy.zeros(1,,dtype='float32')
2199 for i in range(dataOut.NSHTS):
2200 y[0]=y[1]=dataOut.range1[i]
2201
2202 y = y.astype(dtype='float64',order='F')
2203 three=int(3)
2204 wl = 3.0
2205 tion=numpy.zeros(three,order='F',dtype='float32')
2206 fion=numpy.zeros(three,order='F',dtype='float32')
2207 nui=numpy.zeros(three,order='F',dtype='float32')
2208 wion=numpy.zeros(three,order='F',dtype='int32')
2209 bline=0.0
2210 #bline=numpy.zeros(1,order='F',dtype='float32')
2211
2212
2213 #print("**** ACF2 WRAPPER ***** ",fitacf_acf2.acf2.__doc__ )
2214 print("BEFORE",dataOut.ph2[10:35])
2215 for i in range(dataOut.NSHTS):
2216 if dataOut.info2[i]==1:
2217 angle=dataOut.thb[i]*0.01745
2218 nue=nui[0]=nui[1]=nui[2]=0.0#nui[3]=0.0
2219 wion[0]=16
2220 wion[1]=1
2221 wion[2]=4
2222 tion[0]=tion[1]=tion[2]=dataOut.ti2[i]
2223 fion[0]=1.0-dataOut.phy2[i]
2224 fion[1]=dataOut.phy2[i]
2225 fion[2]=0.0
2226 for j in range(dataOut.DPL):
2227 tau=dataOut.alag[j]*1.0e-3
2228
2229 '''
2230 print("**** input from acf2 ***** ")
2231 print("wl ",wl)
2232 print("tau ",tau)
2233 print("te2[i] ",dataOut.te2[i])
2234 print("tion ",tion)
2235 print("fion ",fion)
2236 print("nue ",nue)
2237 print("nui ",nui)
2238 print("wion ",wion)
2239 print("angle ",angle)
2240 print("ph2[i] ",dataOut.ph2[i])
2241 print("bfm[i] ",dataOut.bfm[i])
2242 print("y[j] ",y[j])
2243 '''
2244 print("Before y[j] ",y[j])
2245 #with suppress_stdout_stderr():
2246 y[j]=fitacf_acf2.acf2(wl,tau,dataOut.te2[i],tion,fion,nue,nui,wion,angle,dataOut.ph2[i],dataOut.bfm[i],y[j],three)
2247 #print("l",l)
2248 print("After y[j] ",y[j])
2249 '''
2250 print("**** output from acf2 ***** ")
2251 print("wl ",wl)
2252 print("tau ",tau)
2253 print("te2[i] ",dataOut.te2[i])
2254 print("tion ",tion)
2255 print("fion ",fion)
2256 print("nue ",nue)
2257 print("nui ",nui)
2258 print("wion ",wion)
2259 print("angle ",angle)
2260 print("ph2[i] ",dataOut.ph2[i])
2261 print("bfm[i] ",dataOut.bfm[i])
2262 print("y[j] ",y[j])
2263 print("i ",i , " j ",j , "y[j] ",y[j])
2264 '''
2265
2266
2267 #exit(1)
2268 if dataOut.ut_Faraday>11.0 and dataOut.range1[i]>150.0 and dataOut.range1[i]<400.0:
2269 tau=0.0
2270 #with suppress_stdout_stderr():
2271 bline=fitacf_acf2.acf2(wl,tau,tion,tion,fion,nue,nui,wion,angle,dataOut.ph2[i],dataOut.bfm[i],bline,three)
2272 cf=min(1.2,max(1.0,bline/y[0]))
2273 print("bline: ",bline)
2274 if cf != 1.0:
2275 print("bline: ",bline)
2276 print("cf: ",cf)
2277 #exit(1)
2278 #print("cf: ",cf)
2279 dataOut.ph2[i]=cf*dataOut.ph2[i]
2280 dataOut.sdp2[i]=cf*dataOut.sdp2[i]
2281 for j in range(1,dataOut.DPL):
2282 y[j]=(y[j]/y[0])*dataOut.DH+dataOut.range1[i]
2283 y[0]=dataOut.range1[i]+dataOut.DH
2284 #exit(1)
2285
2286
2287 #exit(1)
2288 print("AFTER",dataOut.ph2[10:35])
2289 #exit(1)
2290
2291
2292
2293
2294
2295 return dataOut
2296
2297 class DataPlotCleaner(Operation):
2298 def __init__(self, **kwargs):
2299
2300 Operation.__init__(self, **kwargs)
2301
2302 def run(self,dataOut):
2303
2304
2305 THRESH_MIN_POW=10000
2306 THRESH_MAX_POW=10000000
2307 THRESH_MIN_TEMP=500
2308 THRESH_MAX_TEMP=4000
2309 dataOut.DensityClean=numpy.zeros((1,dataOut.NDP))
2310 dataOut.EDensityClean=numpy.zeros((1,dataOut.NDP))
2311 dataOut.ElecTempClean=numpy.zeros((1,dataOut.NDP))
2312 dataOut.EElecTempClean=numpy.zeros((1,dataOut.NDP))
2313 dataOut.IonTempClean=numpy.zeros((1,dataOut.NDP))
2314 dataOut.EIonTempClean=numpy.zeros((1,dataOut.NDP))
2315
2316 dataOut.DensityClean[0]=numpy.copy(dataOut.ph2)
2317 dataOut.EDensityClean[0]=numpy.copy(dataOut.sdp2)
2318 dataOut.ElecTempClean[0,:dataOut.NSHTS]=numpy.copy(dataOut.te2)
2319 dataOut.EElecTempClean[0,:dataOut.NSHTS]=numpy.copy(dataOut.ete2)
2320 dataOut.IonTempClean[0,:dataOut.NSHTS]=numpy.copy(dataOut.ti2)
2321 dataOut.EIonTempClean[0,:dataOut.NSHTS]=numpy.copy(dataOut.eti2)
2322
2323 for i in range(dataOut.NDP):
2324 if dataOut.DensityClean[0,i]<THRESH_MIN_POW:
2325 dataOut.DensityClean[0,i]=THRESH_MIN_POW
2326
2327 for i in range(dataOut.NDP):
2328 if dataOut.DensityClean[0,i]>THRESH_MAX_POW:
2329 dataOut.DensityClean[0,i]=THRESH_MAX_POW
2330
2331 for i in range(dataOut.NSHTS):
2332 dataOut.ElecTempClean[0,i]=(max(1.0, dataOut.ElecTempClean[0,i]))
2333 dataOut.IonTempClean[0,i]=(max(1.0, dataOut.IonTempClean[0,i]))
2334 for i in range(dataOut.NSHTS):
2335 if dataOut.ElecTempClean[0,i]<THRESH_MIN_TEMP:
2336 dataOut.ElecTempClean[0,i]=THRESH_MIN_TEMP
2337 if dataOut.IonTempClean[0,i]<THRESH_MIN_TEMP:
2338 dataOut.IonTempClean[0,i]=THRESH_MIN_TEMP
2339 for i in range(dataOut.NSHTS):
2340 if dataOut.ElecTempClean[0,i]>THRESH_MAX_TEMP:
2341 dataOut.ElecTempClean[0,i]=THRESH_MAX_TEMP
2342 if dataOut.IonTempClean[0,i]>THRESH_MAX_TEMP:
2343 dataOut.IonTempClean[0,i]=THRESH_MAX_TEMP
2344 for i in range(dataOut.NSHTS):
2345 if dataOut.EElecTempClean[0,i]>500:#
2346 dataOut.ElecTempClean[0,i]=500
2347 if dataOut.EIonTempClean[0,i]>500:#
2348 dataOut.IonTempClean[0,i]=500
2349
2350 missing=numpy.nan
2351
2352 for i in range(dataOut.NSHTS,dataOut.NDP):
2353
2354 dataOut.ElecTempClean[0,i]=missing
2355 dataOut.EElecTempClean[0,i]=missing
2356 dataOut.IonTempClean[0,i]=missing
2357 dataOut.EIonTempClean[0,i]=missing
2358
2359 return dataOut
2360
2361
2362 class DataSaveCleaner(Operation):
2363 def __init__(self, **kwargs):
2364
2365 Operation.__init__(self, **kwargs)
2366
2367
2368 def run(self,dataOut):
2369
2370 dataOut.DensityFinal=numpy.zeros((1,dataOut.NDP))
2371 dataOut.EDensityFinal=numpy.zeros((1,dataOut.NDP))
2372 dataOut.ElecTempFinal=numpy.zeros((1,dataOut.NDP))
2373 dataOut.EElecTempFinal=numpy.zeros((1,dataOut.NDP))
2374 dataOut.IonTempFinal=numpy.zeros((1,dataOut.NDP))
2375 dataOut.EIonTempFinal=numpy.zeros((1,dataOut.NDP))
2376 dataOut.PhyFinal=numpy.zeros((1,dataOut.NDP))
2377 dataOut.EPhyFinal=numpy.zeros((1,dataOut.NDP))
2378
2379 dataOut.DensityFinal[0]=numpy.copy(dataOut.ph2)
2380 dataOut.EDensityFinal[0]=numpy.copy(dataOut.sdp2)
2381 dataOut.ElecTempFinal[0,:dataOut.NSHTS]=numpy.copy(dataOut.te2)
2382 dataOut.EElecTempFinal[0,:dataOut.NSHTS]=numpy.copy(dataOut.ete2)
2383 dataOut.IonTempFinal[0,:dataOut.NSHTS]=numpy.copy(dataOut.ti2)
2384 dataOut.EIonTempFinal[0,:dataOut.NSHTS]=numpy.copy(dataOut.eti2)
2385 dataOut.PhyFinal[0,:dataOut.NSHTS]=numpy.copy(dataOut.phy2)
2386 dataOut.EPhyFinal[0,:dataOut.NSHTS]=numpy.copy(dataOut.ephy2)
2387
2388 missing=numpy.nan
2389
2390 temp_min=100.0
2391 temp_max=3000.0#6000.0e
2392
2393 for i in range(dataOut.NSHTS):
2394
2395 if dataOut.info2[i]!=1:
2396 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2397
2398 if dataOut.ElecTempFinal[0,i]<=temp_min or dataOut.ElecTempFinal[0,i]>temp_max or dataOut.EElecTempFinal[0,i]>temp_max:
2399
2400 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=missing
2401
2402
2403 if dataOut.IonTempFinal[0,i]<=temp_min or dataOut.IonTempFinal[0,i]>temp_max or dataOut.EIonTempFinal[0,i]>temp_max:
2404 dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2405
2406 if dataOut.lags_to_plot[i,:][~numpy.isnan(dataOut.lags_to_plot[i,:])].shape[0]<6:
2407 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2408
2409 if dataOut.ut_Faraday>4 and dataOut.ut_Faraday<11:
2410 if numpy.nanmax(dataOut.acfs_error_to_plot[i,:])>=10:
2411 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2412
2413 if dataOut.EPhyFinal[0,i]<0.0 or dataOut.EPhyFinal[0,i]>1.0:
2414 dataOut.PhyFinal[0,i]=dataOut.EPhyFinal[0,i]=missing
2415 if dataOut.EDensityFinal[0,i]>0.0 and dataOut.DensityFinal[0,i]>0.0 and dataOut.DensityFinal[0,i]<9.9e6:
2416 dataOut.EDensityFinal[0,i]=max(dataOut.EDensityFinal[0,i],1000.0)
2417 else:
2418 dataOut.DensityFinal[0,i]=dataOut.EDensityFinal[0,i]=missing
2419 if dataOut.PhyFinal[0,i]==0 or dataOut.PhyFinal[0,i]>0.4:
2420 dataOut.PhyFinal[0,i]=dataOut.EPhyFinal[0,i]=missing
2421 if dataOut.ElecTempFinal[0,i]==dataOut.IonTempFinal[0,i]:
2422 dataOut.EElecTempFinal[0,i]=dataOut.EIonTempFinal[0,i]
2423 if numpy.isnan(dataOut.ElecTempFinal[0,i]):
2424 dataOut.EElecTempFinal[0,i]=missing
2425 if numpy.isnan(dataOut.IonTempFinal[0,i]):
2426 dataOut.EIonTempFinal[0,i]=missing
2427 if numpy.isnan(dataOut.ElecTempFinal[0,i]) or numpy.isnan(dataOut.EElecTempFinal[0,i]):
2428 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2429
2430 for i in range(12,dataOut.NSHTS-1):
2431
2432 if numpy.isnan(dataOut.ElecTempFinal[0,i-1]) and numpy.isnan(dataOut.ElecTempFinal[0,i+1]):
2433 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=missing
2434
2435 if numpy.isnan(dataOut.IonTempFinal[0,i-1]) and numpy.isnan(dataOut.IonTempFinal[0,i+1]):
2436 dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2437
2438 if dataOut.ut_Faraday>4 and dataOut.ut_Faraday<11:
2439
2440 if numpy.isnan(dataOut.ElecTempFinal[0,i-1]) and numpy.isnan(dataOut.ElecTempFinal[0,i-2]) and numpy.isnan(dataOut.ElecTempFinal[0,i+2]) and numpy.isnan(dataOut.ElecTempFinal[0,i+3]): #and numpy.isnan(dataOut.ElecTempFinal[0,i-5]):
2441
2442 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=missing
2443 if numpy.isnan(dataOut.IonTempFinal[0,i-1]) and numpy.isnan(dataOut.IonTempFinal[0,i-2]) and numpy.isnan(dataOut.IonTempFinal[0,i+2]) and numpy.isnan(dataOut.IonTempFinal[0,i+3]): #and numpy.isnan(dataOut.IonTempFinal[0,i-5]):
2444
2445 dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2446
2447
2448
2449 if i>25:
2450 if numpy.isnan(dataOut.ElecTempFinal[0,i-1]) and numpy.isnan(dataOut.ElecTempFinal[0,i-2]) and numpy.isnan(dataOut.ElecTempFinal[0,i-3]) and numpy.isnan(dataOut.ElecTempFinal[0,i-4]): #and numpy.isnan(dataOut.ElecTempFinal[0,i-5]):
2451 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=missing
2452 if numpy.isnan(dataOut.IonTempFinal[0,i-1]) and numpy.isnan(dataOut.IonTempFinal[0,i-2]) and numpy.isnan(dataOut.IonTempFinal[0,i-3]) and numpy.isnan(dataOut.IonTempFinal[0,i-4]): #and numpy.isnan(dataOut.IonTempFinal[0,i-5]):
2453
2454 dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2455
2456 if numpy.isnan(dataOut.ElecTempFinal[0,i]) or numpy.isnan(dataOut.EElecTempFinal[0,i]):
2457
2458 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2459
2460 for i in range(12,dataOut.NSHTS-1):
2461
2462 if numpy.isnan(dataOut.ElecTempFinal[0,i-1]) and numpy.isnan(dataOut.ElecTempFinal[0,i+1]):
2463 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=missing
2464
2465 if numpy.isnan(dataOut.IonTempFinal[0,i-1]) and numpy.isnan(dataOut.IonTempFinal[0,i+1]):
2466 dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2467
2468
2469 if numpy.isnan(dataOut.ElecTempFinal[0,i]) or numpy.isnan(dataOut.EElecTempFinal[0,i]):
2470
2471 dataOut.ElecTempFinal[0,i]=dataOut.EElecTempFinal[0,i]=dataOut.IonTempFinal[0,i]=dataOut.EIonTempFinal[0,i]=missing
2472
2473 if numpy.count_nonzero(~numpy.isnan(dataOut.ElecTempFinal[0,12:50]))<5:
2474 dataOut.ElecTempFinal[0,:]=dataOut.EElecTempFinal[0,:]=missing
2475 if numpy.count_nonzero(~numpy.isnan(dataOut.IonTempFinal[0,12:50]))<5:
2476 dataOut.IonTempFinal[0,:]=dataOut.EIonTempFinal[0,:]=missing
2477
2478 for i in range(dataOut.NSHTS,dataOut.NDP):
2479
2480 dataOut.ElecTempFinal[0,i]=missing
2481 dataOut.EElecTempFinal[0,i]=missing
2482 dataOut.IonTempFinal[0,i]=missing
2483 dataOut.EIonTempFinal[0,i]=missing
2484 dataOut.PhyFinal[0,i]=missing
2485 dataOut.EPhyFinal[0,i]=missing
2486
2487 return dataOut
2488
2489
2490 class DataSaveCleanerHP(Operation):
2491 def __init__(self, **kwargs):
2492
2493 Operation.__init__(self, **kwargs)
2494
2495 def run(self,dataOut):
2496
2497 dataOut.Density_DP=numpy.zeros(dataOut.cut)
2498 dataOut.EDensity_DP=numpy.zeros(dataOut.cut)
2499 dataOut.ElecTemp_DP=numpy.zeros(dataOut.cut)
2500 dataOut.EElecTemp_DP=numpy.zeros(dataOut.cut)
2501 dataOut.IonTemp_DP=numpy.zeros(dataOut.cut)
2502 dataOut.EIonTemp_DP=numpy.zeros(dataOut.cut)
2503 dataOut.Phy_DP=numpy.zeros(dataOut.cut)
2504 dataOut.EPhy_DP=numpy.zeros(dataOut.cut)
2505 dataOut.Phe_DP=numpy.empty(dataOut.cut)
2506 dataOut.EPhe_DP=numpy.empty(dataOut.cut)
2507
2508 dataOut.Density_DP[:]=numpy.copy(dataOut.ph2[:dataOut.cut])
2509 dataOut.EDensity_DP[:]=numpy.copy(dataOut.sdp2[:dataOut.cut])
2510 dataOut.ElecTemp_DP[:]=numpy.copy(dataOut.te2[:dataOut.cut])
2511 dataOut.EElecTemp_DP[:]=numpy.copy(dataOut.ete2[:dataOut.cut])
2512 dataOut.IonTemp_DP[:]=numpy.copy(dataOut.ti2[:dataOut.cut])
2513 dataOut.EIonTemp_DP[:]=numpy.copy(dataOut.eti2[:dataOut.cut])
2514 dataOut.Phy_DP[:]=numpy.copy(dataOut.phy2[:dataOut.cut])
2515 dataOut.EPhy_DP[:]=numpy.copy(dataOut.ephy2[:dataOut.cut])
2516 dataOut.Phe_DP[:]=numpy.nan
2517 dataOut.EPhe_DP[:]=numpy.nan
2518
2519 missing=numpy.nan
2520 temp_min=100.0
2521 temp_max_dp=3000.0
2522
2523 for i in range(dataOut.cut):
2524 if dataOut.info2[i]!=1:
2525 dataOut.ElecTemp_DP[i]=dataOut.EElecTemp_DP[i]=dataOut.IonTemp_DP[i]=dataOut.EIonTemp_DP[i]=missing
2526
2527 if dataOut.ElecTemp_DP[i]<=temp_min or dataOut.ElecTemp_DP[i]>temp_max_dp or dataOut.EElecTemp_DP[i]>temp_max_dp:
2528
2529 dataOut.ElecTemp_DP[i]=dataOut.EElecTemp_DP[i]=missing
2530
2531 if dataOut.IonTemp_DP[i]<=temp_min or dataOut.IonTemp_DP[i]>temp_max_dp or dataOut.EIonTemp_DP[i]>temp_max_dp:
2532 dataOut.IonTemp_DP[i]=dataOut.EIonTemp_DP[i]=missing
2533
2534 ####################################################################################### CHECK THIS
2535 if dataOut.lags_to_plot[i,:][~numpy.isnan(dataOut.lags_to_plot[i,:])].shape[0]<6:
2536 dataOut.ElecTemp_DP[i]=dataOut.EElecTemp_DP[i]=dataOut.IonTemp_DP[i]=dataOut.EIonTemp_DP[i]=missing
2537
2538 if dataOut.ut_Faraday>4 and dataOut.ut_Faraday<11:
2539 if numpy.nanmax(dataOut.acfs_error_to_plot[i,:])>=10:
2540 dataOut.ElecTemp_DP[i]=dataOut.EElecTemp_DP[i]=dataOut.IonTemp_DP[i]=dataOut.EIonTemp_DP[i]=missing
2541 #######################################################################################
2542
2543 if dataOut.EPhy_DP[i]<0.0 or dataOut.EPhy_DP[i]>1.0:
2544 dataOut.Phy_DP[i]=dataOut.EPhy_DP[i]=missing
2545 if dataOut.EDensity_DP[i]>0.0 and dataOut.Density_DP[i]>0.0 and dataOut.Density_DP[i]<9.9e6:
2546 dataOut.EDensity_DP[i]=max(dataOut.EDensity_DP[i],1000.0)
2547 else:
2548 dataOut.Density_DP[i]=dataOut.EDensity_DP[i]=missing
2549 if dataOut.Phy_DP[i]==0 or dataOut.Phy_DP[i]>0.4:
2550 dataOut.Phy_DP[i]=dataOut.EPhy_DP[i]=missing
2551 if dataOut.ElecTemp_DP[i]==dataOut.IonTemp_DP[i]:
2552 dataOut.EElecTemp_DP[i]=dataOut.EIonTemp_DP[i]
2553 if numpy.isnan(dataOut.ElecTemp_DP[i]):
2554 dataOut.EElecTemp_DP[i]=missing
2555 if numpy.isnan(dataOut.IonTemp_DP[i]):
2556 dataOut.EIonTemp_DP[i]=missing
2557 if numpy.isnan(dataOut.ElecTemp_DP[i]) or numpy.isnan(dataOut.EElecTemp_DP[i]):
2558 dataOut.ElecTemp_DP[i]=dataOut.EElecTemp_DP[i]=dataOut.IonTemp_DP[i]=dataOut.EIonTemp_DP[i]=missing
2559
2560
2561
2562 dataOut.Density_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2563 dataOut.EDensity_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2564 dataOut.ElecTemp_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2565 dataOut.EElecTemp_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2566 dataOut.IonTemp_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2567 dataOut.EIonTemp_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2568 dataOut.Phy_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2569 dataOut.EPhy_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2570 dataOut.Phe_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2571 dataOut.EPhe_LP=numpy.zeros(dataOut.NACF-dataOut.cut)
2572
2573 dataOut.Density_LP[:]=numpy.copy(dataOut.ne[dataOut.cut:dataOut.NACF])
2574 dataOut.EDensity_LP[:]=numpy.copy(dataOut.ene[dataOut.cut:dataOut.NACF])
2575 dataOut.ElecTemp_LP[:]=numpy.copy(dataOut.te[dataOut.cut:dataOut.NACF])
2576 dataOut.EElecTemp_LP[:]=numpy.copy(dataOut.ete[dataOut.cut:dataOut.NACF])
2577 dataOut.IonTemp_LP[:]=numpy.copy(dataOut.ti[dataOut.cut:dataOut.NACF])
2578 dataOut.EIonTemp_LP[:]=numpy.copy(dataOut.eti[dataOut.cut:dataOut.NACF])
2579 dataOut.Phy_LP[:]=numpy.copy(dataOut.ph[dataOut.cut:dataOut.NACF])
2580 dataOut.EPhy_LP[:]=numpy.copy(dataOut.eph[dataOut.cut:dataOut.NACF])
2581 dataOut.Phe_LP[:]=numpy.copy(dataOut.phe[dataOut.cut:dataOut.NACF])
2582 dataOut.EPhe_LP[:]=numpy.copy(dataOut.ephe[dataOut.cut:dataOut.NACF])
2583
2584 temp_max_lp=6000.0
2585
2586 for i in range(dataOut.NACF-dataOut.cut):
2587
2588 if dataOut.ElecTemp_LP[i]<=temp_min or dataOut.ElecTemp_LP[i]>temp_max_lp or dataOut.EElecTemp_LP[i]>temp_max_lp:
2589
2590 dataOut.ElecTemp_LP[i]=dataOut.EElecTemp_LP[i]=missing
2591
2592 if dataOut.IonTemp_LP[i]<=temp_min or dataOut.IonTemp_LP[i]>temp_max_lp or dataOut.EIonTemp_LP[i]>temp_max_lp:
2593 dataOut.IonTemp_LP[i]=dataOut.EIonTemp_LP[i]=missing
2594 if dataOut.EPhy_LP[i]<0.0 or dataOut.EPhy_LP[i]>1.0:
2595 dataOut.Phy_LP[i]=dataOut.EPhy_LP[i]=missing
2596
2597 if dataOut.EPhe_LP[i]<0.0 or dataOut.EPhe_LP[i]>1.0:
2598 dataOut.Phe_LP[i]=dataOut.EPhe_LP[i]=missing
2599 if dataOut.EDensity_LP[i]>0.0 and dataOut.Density_LP[i]>0.0 and dataOut.Density_LP[i]<9.9e6 and dataOut.EDensity_LP[i]*dataOut.Density_LP[i]<9.9e6:
2600 dataOut.EDensity_LP[i]=max(dataOut.EDensity_LP[i],1000.0/dataOut.Density_LP[i])
2601 else:
2602 dataOut.Density_LP[i]=missing
2603 dataOut.EDensity_LP[i]=1.0
2604
2605 if numpy.isnan(dataOut.Phy_LP[i]):
2606 dataOut.EPhy_LP[i]=missing
2607
2608 if numpy.isnan(dataOut.Phe_LP[i]):
2609 dataOut.EPhe_LP[i]=missing
2610
2611
2612 if dataOut.ElecTemp_LP[i]==dataOut.IonTemp_LP[i]:
2613 dataOut.EElecTemp_LP[i]=dataOut.EIonTemp_LP[i]
2614 if numpy.isnan(dataOut.ElecTemp_LP[i]):
2615 dataOut.EElecTemp_LP[i]=missing
2616 if numpy.isnan(dataOut.IonTemp_LP[i]):
2617 dataOut.EIonTemp_LP[i]=missing
2618 if numpy.isnan(dataOut.ElecTemp_LP[i]) or numpy.isnan(dataOut.EElecTemp_LP[i]):
2619 dataOut.ElecTemp_LP[i]=dataOut.EElecTemp_LP[i]=dataOut.IonTemp_LP[i]=dataOut.EIonTemp_LP[i]=missing
2620
2621
2622 dataOut.DensityFinal=numpy.reshape(numpy.concatenate((dataOut.Density_DP,dataOut.Density_LP)),(1,-1))
2623 dataOut.EDensityFinal=numpy.reshape(numpy.concatenate((dataOut.EDensity_DP,dataOut.EDensity_LP)),(1,-1))
2624 dataOut.ElecTempFinal=numpy.reshape(numpy.concatenate((dataOut.ElecTemp_DP,dataOut.ElecTemp_LP)),(1,-1))
2625 dataOut.EElecTempFinal=numpy.reshape(numpy.concatenate((dataOut.EElecTemp_DP,dataOut.EElecTemp_LP)),(1,-1))
2626 dataOut.IonTempFinal=numpy.reshape(numpy.concatenate((dataOut.IonTemp_DP,dataOut.IonTemp_LP)),(1,-1))
2627 dataOut.EIonTempFinal=numpy.reshape(numpy.concatenate((dataOut.EIonTemp_DP,dataOut.EIonTemp_LP)),(1,-1))
2628 dataOut.PhyFinal=numpy.reshape(numpy.concatenate((dataOut.Phy_DP,dataOut.Phy_LP)),(1,-1))
2629 dataOut.EPhyFinal=numpy.reshape(numpy.concatenate((dataOut.EPhy_DP,dataOut.EPhy_LP)),(1,-1))
2630 dataOut.PheFinal=numpy.reshape(numpy.concatenate((dataOut.Phe_DP,dataOut.Phe_LP)),(1,-1))
2631 dataOut.EPheFinal=numpy.reshape(numpy.concatenate((dataOut.EPhe_DP,dataOut.EPhe_LP)),(1,-1))
2632
2633 nan_array_2=numpy.empty(dataOut.NACF-dataOut.NDP)
2634 nan_array_2[:]=numpy.nan
2635
2636 dataOut.acfs_DP=numpy.zeros((dataOut.NACF,dataOut.DPL),'float32')
2637 dataOut.acfs_error_DP=numpy.zeros((dataOut.NACF,dataOut.DPL),'float32')
2638 acfs_dp_aux=dataOut.acfs_to_save.transpose()
2639 acfs_error_dp_aux=dataOut.acfs_error_to_save.transpose()
2640 for i in range(dataOut.DPL):
2641 dataOut.acfs_DP[:,i]=numpy.concatenate((acfs_dp_aux[:,i],nan_array_2))
2642 dataOut.acfs_error_DP[:,i]=numpy.concatenate((acfs_error_dp_aux[:,i],nan_array_2))
2643 dataOut.acfs_DP=dataOut.acfs_DP.transpose()
2644 dataOut.acfs_error_DP=dataOut.acfs_error_DP.transpose()
2645
2646 dataOut.acfs_LP=numpy.zeros((dataOut.NACF,dataOut.IBITS),'float32')
2647 dataOut.acfs_error_LP=numpy.zeros((dataOut.NACF,dataOut.IBITS),'float32')
2648
2649 for i in range(dataOut.NACF):
2650 for j in range(dataOut.IBITS):
2651 if numpy.abs(dataOut.errors[j,i]/dataOut.output_LP_integrated.real[0,i,0])<1.0:
2652 dataOut.acfs_LP[i,j]=dataOut.output_LP_integrated.real[j,i,0]/dataOut.output_LP_integrated.real[0,i,0]
2653 dataOut.acfs_LP[i,j]=max(min(dataOut.acfs_LP[i,j],1.0),-1.0)
2654
2655 dataOut.acfs_error_LP[i,j]=dataOut.errors[j,i]/dataOut.output_LP_integrated.real[0,i,0]
2656 else:
2657 dataOut.acfs_LP[i,j]=numpy.nan
2658
2659 dataOut.acfs_error_LP[i,j]=numpy.nan
2660
2661 dataOut.acfs_LP=dataOut.acfs_LP.transpose()
2662 dataOut.acfs_error_LP=dataOut.acfs_error_LP.transpose()
2663
2664 return dataOut
2665
2666
2667 class ACFs(Operation):
2668 def __init__(self, **kwargs):
2669
2670 Operation.__init__(self, **kwargs)
2671
2672 self.aux=1
2673
2674 def run(self,dataOut):
2675
2676 if self.aux:
2677 self.taup=numpy.zeros(dataOut.DPL,'float32')
2678 self.pacf=numpy.zeros(dataOut.DPL,'float32')
2679 self.sacf=numpy.zeros(dataOut.DPL,'float32')
2680
2681 self.taup_full=numpy.zeros(dataOut.DPL,'float32')
2682 self.pacf_full=numpy.zeros(dataOut.DPL,'float32')
2683 self.sacf_full=numpy.zeros(dataOut.DPL,'float32')
2684 self.x_igcej=numpy.zeros(dataOut.DPL,'float32')
2685 self.y_igcej=numpy.zeros(dataOut.DPL,'float32')
2686 self.x_ibad=numpy.zeros(dataOut.DPL,'float32')
2687 self.y_ibad=numpy.zeros(dataOut.DPL,'float32')
2688 self.aux=0
2689
2690 dataOut.acfs_to_plot=numpy.zeros((dataOut.NDP,dataOut.DPL),'float32')
2691 dataOut.acfs_to_save=numpy.zeros((dataOut.NDP,dataOut.DPL),'float32')
2692 dataOut.acfs_error_to_plot=numpy.zeros((dataOut.NDP,dataOut.DPL),'float32')
2693 dataOut.acfs_error_to_save=numpy.zeros((dataOut.NDP,dataOut.DPL),'float32')
2694 dataOut.lags_to_plot=numpy.zeros((dataOut.NDP,dataOut.DPL),'float32')
2695 dataOut.x_igcej_to_plot=numpy.zeros((dataOut.NDP,dataOut.DPL),'float32')
2696 dataOut.x_ibad_to_plot=numpy.zeros((dataOut.NDP,dataOut.DPL),'float32')
2697 dataOut.y_igcej_to_plot=numpy.zeros((dataOut.NDP,dataOut.DPL),'float32')
2698 dataOut.y_ibad_to_plot=numpy.zeros((dataOut.NDP,dataOut.DPL),'float32')
2699
2700 for i in range(dataOut.NSHTS):
2701
2702 acfm=dataOut.rhor[i][0]**2+dataOut.rhoi[i][0]**2
2703
2704 if acfm>0:
2705 cc=dataOut.rhor[i][0]/acfm
2706 ss=dataOut.rhoi[i][0]/acfm
2707 else:
2708 cc=1.
2709 ss=0.
2710
2711 # keep only uncontaminated data
2712 for l in range(dataOut.DPL):
2713 fact=dataOut.DH
2714 if (dataOut.igcej[i][l]==0 and dataOut.ibad[i][l]==0):
2715
2716 self.pacf_full[l]=min(1.0,max(-1.0,(dataOut.rhor[i][l]*cc + dataOut.rhoi[i][l]*ss)))*fact+dataOut.range1[i]
2717 self.sacf_full[l]=min(1.0,numpy.sqrt(dataOut.sd[i][l]))*fact
2718 self.taup_full[l]=dataOut.alag[l]
2719 self.x_igcej[l]=numpy.nan
2720 self.y_igcej[l]=numpy.nan
2721 self.x_ibad[l]=numpy.nan
2722 self.y_ibad[l]=numpy.nan
2723
2724 else:
2725 self.pacf_full[l]=numpy.nan
2726 self.sacf_full[l]=numpy.nan
2727 self.taup_full[l]=numpy.nan
2728
2729 if dataOut.igcej[i][l]:
2730 self.x_igcej[l]=dataOut.alag[l]
2731 self.y_igcej[l]=dataOut.range1[i]
2732 self.x_ibad[l]=numpy.nan
2733 self.y_ibad[l]=numpy.nan
2734
2735 if dataOut.ibad[i][l]:
2736 self.x_igcej[l]=numpy.nan
2737 self.y_igcej[l]=numpy.nan
2738 self.x_ibad[l]=dataOut.alag[l]
2739 self.y_ibad[l]=dataOut.range1[i]
2740
2741 pacf_new=numpy.copy((self.pacf_full-dataOut.range1[i])/dataOut.DH)
2742 sacf_new=numpy.copy(self.sacf_full/dataOut.DH)
2743 dataOut.acfs_to_save[i,:]=numpy.copy(pacf_new)
2744 dataOut.acfs_error_to_save[i,:]=numpy.copy(sacf_new)
2745 dataOut.acfs_to_plot[i,:]=numpy.copy(self.pacf_full)
2746 dataOut.acfs_error_to_plot[i,:]=numpy.copy(self.sacf_full)
2747 dataOut.lags_to_plot[i,:]=numpy.copy(self.taup_full)
2748 dataOut.x_igcej_to_plot[i,:]=numpy.copy(self.x_igcej)
2749 dataOut.x_ibad_to_plot[i,:]=numpy.copy(self.x_ibad)
2750 dataOut.y_igcej_to_plot[i,:]=numpy.copy(self.y_igcej)
2751 dataOut.y_ibad_to_plot[i,:]=numpy.copy(self.y_ibad)
2752
2753 missing=numpy.nan#-32767
2754
2755 for i in range(dataOut.NSHTS,dataOut.NDP):
2756 for j in range(dataOut.DPL):
2757 dataOut.acfs_to_save[i,j]=missing
2758 dataOut.acfs_error_to_save[i,j]=missing
2759 dataOut.acfs_to_plot[i,j]=missing
2760 dataOut.acfs_error_to_plot[i,j]=missing
2761 dataOut.lags_to_plot[i,j]=missing
2762 dataOut.x_igcej_to_plot[i,j]=missing
2763 dataOut.x_ibad_to_plot[i,j]=missing
2764 dataOut.y_igcej_to_plot[i,j]=missing
2765 dataOut.y_ibad_to_plot[i,j]=missing
2766
2767 dataOut.acfs_to_save=dataOut.acfs_to_save.transpose()
2768 dataOut.acfs_error_to_save=dataOut.acfs_error_to_save.transpose()
2769
2770 return dataOut
2771
2772
2773 class CohInt(Operation):
2774
2775 isConfig = False
2776 __profIndex = 0
2777 __byTime = False
2778 __initime = None
2779 __lastdatatime = None
2780 __integrationtime = None
2781 __buffer = None
2782 __bufferStride = []
2783 __dataReady = False
2784 __profIndexStride = 0
2785 __dataToPutStride = False
2786 n = None
2787
2788 def __init__(self, **kwargs):
2789
2790 Operation.__init__(self, **kwargs)
2791
2792 # self.isConfig = False
2793
2794 def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False):
2795 """
2796 Set the parameters of the integration class.
2797
2798 Inputs:
2799
2800 n : Number of coherent integrations
2801 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
2802 overlapping :
2803 """
2804
2805 self.__initime = None
2806 self.__lastdatatime = 0
2807 self.__buffer = None
2808 self.__dataReady = False
2809 self.byblock = byblock
2810 self.stride = stride
2811
2812 if n == None and timeInterval == None:
2813 raise ValueError("n or timeInterval should be specified ...")
2814
2815 if n != None:
2816 self.n = n
2817 self.__byTime = False
2818 else:
2819 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
2820 self.n = 9999
2821 self.__byTime = True
2822
2823 if overlapping:
2824 self.__withOverlapping = True
2825 self.__buffer = None
2826 else:
2827 self.__withOverlapping = False
2828 self.__buffer = 0
2829
2830 self.__profIndex = 0
2831
2832 def putData(self, data):
2833
2834 """
2835 Add a profile to the __buffer and increase in one the __profileIndex
2836
2837 """
2838
2839 if not self.__withOverlapping:
2840 self.__buffer += data.copy()
2841 self.__profIndex += 1
2842 return
2843
2844 #Overlapping data
2845 nChannels, nHeis = data.shape
2846 data = numpy.reshape(data, (1, nChannels, nHeis))
2847
2848 #If the buffer is empty then it takes the data value
2849 if self.__buffer is None:
2850 self.__buffer = data
2851 self.__profIndex += 1
2852 return
2853
2854 #If the buffer length is lower than n then stakcing the data value
2855 if self.__profIndex < self.n:
2856 self.__buffer = numpy.vstack((self.__buffer, data))
2857 self.__profIndex += 1
2858 return
2859
2860 #If the buffer length is equal to n then replacing the last buffer value with the data value
2861 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
2862 self.__buffer[self.n-1] = data
2863 self.__profIndex = self.n
2864 return
2865
2866
2867 def pushData(self):
2868 """
2869 Return the sum of the last profiles and the profiles used in the sum.
2870
2871 Affected:
2872
2873 self.__profileIndex
2874
2875 """
2876
2877 if not self.__withOverlapping:
2878 data = self.__buffer
2879 n = self.__profIndex
2880
2881 self.__buffer = 0
2882 self.__profIndex = 0
2883
2884 return data, n
2885
2886 #Integration with Overlapping
2887 data = numpy.sum(self.__buffer, axis=0)
2888 # print data
2889 # raise
2890 n = self.__profIndex
2891
2892 return data, n
2893
2894 def byProfiles(self, data):
2895
2896 self.__dataReady = False
2897 avgdata = None
2898 # n = None
2899 # print data
2900 # raise
2901 self.putData(data)
2902
2903 if self.__profIndex == self.n:
2904 avgdata, n = self.pushData()
2905 self.__dataReady = True
2906
2907 return avgdata
2908
2909 def byTime(self, data, datatime):
2910
2911 self.__dataReady = False
2912 avgdata = None
2913 n = None
2914
2915 self.putData(data)
2916
2917 if (datatime - self.__initime) >= self.__integrationtime:
2918 avgdata, n = self.pushData()
2919 self.n = n
2920 self.__dataReady = True
2921
2922 return avgdata
2923
2924 def integrateByStride(self, data, datatime):
2925 # print data
2926 if self.__profIndex == 0:
2927 self.__buffer = [[data.copy(), datatime]]
2928 else:
2929 self.__buffer.append([data.copy(),datatime])
2930 self.__profIndex += 1
2931 self.__dataReady = False
2932
2933 if self.__profIndex == self.n * self.stride :
2934 self.__dataToPutStride = True
2935 self.__profIndexStride = 0
2936 self.__profIndex = 0
2937 self.__bufferStride = []
2938 for i in range(self.stride):
2939 current = self.__buffer[i::self.stride]
2940 data = numpy.sum([t[0] for t in current], axis=0)
2941 avgdatatime = numpy.average([t[1] for t in current])
2942 # print data
2943 self.__bufferStride.append((data, avgdatatime))
2944
2945 if self.__dataToPutStride:
2946 self.__dataReady = True
2947 self.__profIndexStride += 1
2948 if self.__profIndexStride == self.stride:
2949 self.__dataToPutStride = False
2950 # print self.__bufferStride[self.__profIndexStride - 1]
2951 # raise
2952 return self.__bufferStride[self.__profIndexStride - 1]
2953
2954
2955 return None, None
2956
2957 def integrate(self, data, datatime=None):
2958
2959 if self.__initime == None:
2960 self.__initime = datatime
2961
2962 if self.__byTime:
2963 avgdata = self.byTime(data, datatime)
2964 else:
2965 avgdata = self.byProfiles(data)
2966
2967
2968 self.__lastdatatime = datatime
2969
2970 if avgdata is None:
2971 return None, None
2972
2973 avgdatatime = self.__initime
2974
2975 deltatime = datatime - self.__lastdatatime
2976
2977 if not self.__withOverlapping:
2978 self.__initime = datatime
2979 else:
2980 self.__initime += deltatime
2981
2982 return avgdata, avgdatatime
2983
2984 def integrateByBlock(self, dataOut):
2985
2986 times = int(dataOut.data.shape[1]/self.n)
2987 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
2988
2989 id_min = 0
2990 id_max = self.n
2991
2992 for i in range(times):
2993 junk = dataOut.data[:,id_min:id_max,:]
2994 avgdata[:,i,:] = junk.sum(axis=1)
2995 id_min += self.n
2996 id_max += self.n
2997
2998 timeInterval = dataOut.ippSeconds*self.n
2999 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
3000 self.__dataReady = True
3001 return avgdata, avgdatatime
3002
3003 def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs):
3004
3005 if not self.isConfig:
3006 self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
3007 self.isConfig = True
3008 print("inside")
3009 if dataOut.flagDataAsBlock:
3010 """
3011 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
3012 """
3013
3014 avgdata, avgdatatime = self.integrateByBlock(dataOut)
3015 dataOut.nProfiles /= self.n
3016 else:
3017 if stride is None:
3018 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
3019 else:
3020 avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime)
3021
3022
3023 # dataOut.timeInterval *= n
3024 dataOut.flagNoData = True
3025
3026 if self.__dataReady:
3027 dataOut.data = avgdata
3028 if not dataOut.flagCohInt:
3029 dataOut.nCohInt *= self.n
3030 dataOut.flagCohInt = True
3031 dataOut.utctime = avgdatatime
3032 # print avgdata, avgdatatime
3033 # raise
3034 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
3035 dataOut.flagNoData = False
3036 return dataOut
3037
3038 class TimesCode(Operation):
3039 """
3040
3041 """
3042
3043 def __init__(self, **kwargs):
3044
3045 Operation.__init__(self, **kwargs)
3046
3047
3048
3049 def run(self,dataOut,code):
3050
3051 #code = numpy.repeat(code, repeats=osamp, axis=1)
3052 nCodes = numpy.shape(code)[1]
3053 #nprofcode = dataOut.nProfiles//nCodes
3054 code = numpy.array(code)
3055 #print("nHeights",dataOut.nHeights)
3056 #print("nheicode",nheicode)
3057 #print("Code.Shape",numpy.shape(code))
3058 #print("Code",code[0,:])
3059 nheicode = dataOut.nHeights//nCodes
3060 res = dataOut.nHeights%nCodes
3061 '''
3062 buffer = numpy.zeros((dataOut.nChannels,
3063 nprofcode,
3064 nCodes,
3065 ndataOut.nHeights),
3066 dtype='complex')
3067 '''
3068 #exit(1)
3069 #for ipr in range(dataOut.nProfiles):
3070 #print(dataOut.nHeights)
3071 #print(dataOut.data[0,384-2:])
3072 #print(dataOut.profileIndex)
3073 #print(dataOut.data[0,:2])
3074 #print(dataOut.data[0,0:64])
3075 #print(dataOut.data[0,64:64+64])
3076 #exit(1)
3077 for ich in range(dataOut.nChannels):
3078 for ihe in range(nheicode):
3079 #print(ihe*nCodes)
3080 #print((ihe+1)*nCodes)
3081 #dataOut.data[ich,ipr,ihe*nCodes:nCodes*(ihe+1)]
3082 #code[ipr,:]
3083 #print("before",dataOut.data[ich,ipr,ihe*nCodes:nCodes*(ihe+1)])
3084 #dataOut.data[ich,ipr,ihe*nCodes:nCodes*(ihe+1)] = numpy.prod([dataOut.data[ich,ipr,ihe*nCodes:nCodes*(ihe+1)],code[ipr,:]],axis=0)
3085 dataOut.data[ich,ihe*nCodes:nCodes*(ihe+1)] = numpy.prod([dataOut.data[ich,ihe*nCodes:nCodes*(ihe+1)],code[dataOut.profileIndex,:]],axis=0)
3086
3087 #print("after",dataOut.data[ich,ipr,ihe*nCodes:nCodes*(ihe+1)])
3088 #exit(1)
3089 #print(dataOut.data[0,:2])
3090 #exit(1)
3091 #print(nheicode)
3092 #print((nheicode)*nCodes)
3093 #print(((nheicode)*nCodes)+res)
3094 if res != 0:
3095 for ich in range(dataOut.nChannels):
3096 dataOut.data[ich,nheicode*nCodes:] = numpy.prod([dataOut.data[ich,nheicode*nCodes:],code[dataOut.profileIndex,:res]],axis=0)
3097
3098 #pass
3099 #print(dataOut.data[0,384-2:])
3100 #exit(1)
3101 #dataOut.data = numpy.mean(buffer,axis=1)
3102 #print(numpy.shape(dataOut.data))
3103 #print(dataOut.nHeights)
3104 #dataOut.heightList = dataOut.heightList[0:nheicode]
3105 #print(dataOut.nHeights)
3106 #dataOut.nHeights = numpy.shape(dataOut.data)[2]
3107 #print(numpy.shape(dataOut.data))
3108 #exit(1)
3109
3110 return dataOut
3111
3112 '''
3113 class Spectrogram(Operation):
3114 """
3115
3116 """
3117
3118 def __init__(self, **kwargs):
3119
3120 Operation.__init__(self, **kwargs)
3121
3122
3123
3124 def run(self,dataOut):
3125
3126 import scipy
3127
3128
3129
3130 fs = 3200*1e-6
3131 fs = fs/64
3132 fs = 1/fs
3133
3134 nperseg=64
3135 noverlap=48
3136
3137 f, t, Sxx = signal.spectrogram(x, fs, return_onesided=False, nperseg=nperseg, noverlap=noverlap, mode='complex')
3138
3139
3140 for ich in range(dataOut.nChannels):
3141 for ihe in range(nheicode):
3142
3143
3144 return dataOut
3145 '''
3146
3147
3148 class RemoveDcHae(Operation):
3149
3150 def __init__(self, **kwargs):
3151
3152 Operation.__init__(self, **kwargs)
3153 self.DcCounter = 0
3154
3155 def run(self, dataOut):
3156
3157 if self.DcCounter == 0:
3158 dataOut.DcHae = numpy.zeros((dataOut.data.shape[0],320),dtype='complex')
3159 #dataOut.DcHae = []
3160 self.DcCounter = 1
3161
3162 dataOut.dataaux = numpy.copy(dataOut.data)
3163
3164 #dataOut.DcHae += dataOut.dataaux[:,1666:1666+320]
3165 dataOut.DcHae += dataOut.dataaux[:,0:0+320]
3166 hei = 1666
3167 hei = 2000
3168 hei = 1000
3169 hei = 0
3170 #dataOut.DcHae = numpy.concatenate([dataOut.DcHae,dataOut.dataaux[0,hei]],axis = None)
3171
3172
3173
3174 return dataOut
3175
3176
3177 class SSheightProfiles(Operation):
3178
3179 step = None
3180 nsamples = None
3181 bufferShape = None
3182 profileShape = None
3183 sshProfiles = None
3184 profileIndex = None
3185
3186 def __init__(self, **kwargs):
3187
3188 Operation.__init__(self, **kwargs)
3189 self.isConfig = False
3190
3191 def setup(self,dataOut ,step = None , nsamples = None):
3192
3193 if step == None and nsamples == None:
3194 #pass
3195 raise ValueError("step or nheights should be specified ...")
3196
3197 self.step = step
3198 self.nsamples = nsamples
3199 self.__nChannels = dataOut.nChannels
3200 self.__nProfiles = dataOut.nProfiles
3201 self.__nHeis = dataOut.nHeights
3202 shape = dataOut.data.shape #nchannels, nprofiles, nsamples
3203 '''
3204 print "input nChannels",self.__nChannels
3205 print "input nProfiles",self.__nProfiles
3206 print "input nHeis",self.__nHeis
3207 print "input Shape",shape
3208 '''
3209
3210
3211 residue = (shape[1] - self.nsamples) % self.step
3212 if residue != 0:
3213 print("The residue is %d, step=%d should be multiple of %d to avoid loss of %d samples"%(residue,step,shape[1] - self.nsamples,residue))
3214
3215 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
3216 numberProfile = self.nsamples
3217 numberSamples = (shape[1] - self.nsamples)/self.step
3218 '''
3219 print "new numberProfile",numberProfile
3220 print "new numberSamples",numberSamples
3221
3222 print "New number of profile: %d, number of height: %d, Resolution %f Km"%(numberProfile,numberSamples,deltaHeight*self.step)
3223 '''
3224 self.bufferShape = int(shape[0]), int(numberSamples), int(numberProfile) # nchannels, nsamples , nprofiles
3225 self.profileShape = int(shape[0]), int(numberProfile), int(numberSamples) # nchannels, nprofiles, nsamples
3226
3227 self.buffer = numpy.zeros(self.bufferShape , dtype=numpy.complex)
3228 self.sshProfiles = numpy.zeros(self.profileShape, dtype=numpy.complex)
3229
3230 def run(self, dataOut, step, nsamples, code = None, repeat = None):
3231 #print(dataOut.profileIndex)
3232 dataOut.flagNoData = True
3233 dataOut.flagDataAsBlock = False
3234 profileIndex = None
3235
3236 #code = numpy.array(code)
3237 #print(dataOut.data[0,:])
3238 #exit(1)
3239
3240
3241 if not self.isConfig:
3242 #print("STEP",step)
3243 self.setup(dataOut, step=step , nsamples=nsamples)
3244 self.isConfig = True
3245 #print(code[dataOut.profileIndex,:])
3246
3247 #DC_Hae = numpy.array([0.398+0.588j, -0.926+0.306j, -0.536-0.682j, -0.072+0.53j, 0.368-0.356j, 0.996+0.362j])
3248 DC_Hae = numpy.array([ 0.001025 +0.0516375j, 0.03485 +0.20923125j, -0.168 -0.02720625j,
3249 -0.1105375 +0.0707125j, -0.20309375-0.09670625j, 0.189775 +0.02716875j])*(-3.5)
3250
3251 DC_Hae = numpy.array([ -32.26 +8.66j, -32.26 +8.66j])
3252
3253 DC_Hae = numpy.array([-2.78500000e-01 -1.39175j, -6.63237294e+02+210.4268625j])
3254
3255
3256
3257
3258
3259
3260 #print(dataOut.data[0,13:15])
3261 dataOut.data = dataOut.data - DC_Hae[:,None]
3262 #print(dataOut.data[0,13:15])
3263 #exit(1)
3264
3265
3266
3267 code = numpy.array(code)
3268 roll = 0
3269 code = numpy.roll(code,roll,axis=0)
3270 code = numpy.reshape(code,(5,100,64))
3271 block = dataOut.CurrentBlock%5
3272 #print(block)
3273
3274 #code_block = code[block-1-2,:,:]
3275 day_dif = 1 #day_12
3276 code_block = code[block-1-0,:,:]
3277
3278 if repeat is not None:
3279 code_block = numpy.repeat(code_block, repeats=repeat, axis=1)
3280
3281
3282
3283 #print(dataOut.data[0:2,13])
3284 for i in range(self.buffer.shape[1]):
3285 #self.buffer[:,i] = numpy.flip(dataOut.data[:,i*self.step:i*self.step + self.nsamples])
3286 '''
3287 print(dataOut.profileIndex)
3288 print(code[dataOut.profileIndex,:])
3289 print("before",dataOut.data[:,i*self.step:i*self.step + self.nsamples])
3290 print("after",dataOut.data[:,i*self.step:i*self.step + self.nsamples]*code[dataOut.profileIndex,:])
3291 exit(1)
3292 '''
3293
3294 #dif = numpy.copy(code)
3295 if code is not None:
3296 '''
3297 code = numpy.array(code)
3298 #print(code[0,:])
3299
3300 #print("There is Code")
3301 #exit(1)
3302 #code = dataOut.code
3303 #print(code[0,:])
3304 #exit(1)
3305
3306 roll = 0
3307 code = numpy.roll(code,roll,axis=0)
3308 code = numpy.reshape(code,(5,100,64))
3309 block = dataOut.CurrentBlock%5
3310 #print(block)
3311
3312 #code_block = code[block-1-2,:,:]
3313 day_dif = 1 #day_12
3314 code_block = code[block-1-0,:,:]
3315
3316
3317
3318 if repeat is not None:
3319 code_block = numpy.repeat(code_block, repeats=repeat, axis=1)
3320 '''
3321
3322
3323
3324 #code_block = code[0,:,:]
3325
3326 #print(code_block[2,:])
3327 #for l in range(dataOut.data.shape[1]):
3328 #dataOut.data[:,l] = dataOut.data[:,l] - numpy.array([0.398+0.588j, -0.926+0.306j, -0.536-0.682j, -0.072+0.53j, 0.368-0.356j, 0.996+0.362j])
3329
3330 ##DC_Hae = numpy.array([0.398+0.588j, -0.926+0.306j, -0.536-0.682j, -0.072+0.53j, 0.368-0.356j, 0.996+0.362j])
3331
3332 #print(dataOut.data[0:2,13])
3333 ##dataOut.data = dataOut.data - DC_Hae[:,None]
3334 #print(dataOut.data[0:2,13])
3335 #exit(1)
3336 #print(dataOut.data[0,i*self.step:i*self.step + self.nsamples])
3337 #print(dataOut.data[1,i*self.step:i*self.step + self.nsamples])
3338 #print(code_block[dataOut.profileIndex,:])
3339 #print(numpy.shape(code_block[dataOut.profileIndex,:]))
3340 #exit(1)
3341 ###aux = numpy.mean(dataOut.data[:,i*self.step:i*self.step + self.nsamples],axis=1)
3342 ###self.buffer[:,i] = (dataOut.data[:,i*self.step:i*self.step + self.nsamples]-aux[:,None])*code_block[dataOut.profileIndex,:]
3343 '''
3344 if i == 18:
3345 buffer = dataOut.data[0,i*self.step:i*self.step + self.nsamples]
3346 import matplotlib.pyplot as plt
3347 fig, axes = plt.subplots(figsize=(14, 10))
3348 x = numpy.linspace(0,20,numpy.shape(buffer)[0])
3349 x = numpy.fft.fftfreq(numpy.shape(buffer)[0],0.00005)
3350 x = numpy.fft.fftshift(x)
3351
3352 plt.plot(x,buffer)
3353 plt.show()
3354 import time
3355 time.sleep(50)
3356 '''
3357 #for k in range(dataOut.nChannels):
3358 self.buffer[:,i] = dataOut.data[:,i*self.step:i*self.step + self.nsamples]*code_block[dataOut.profileIndex,:]
3359 #print(dataOut.data[0,:])
3360 #print(code_block[0,:])
3361 #print(self.buffer[1,i])
3362 #exit(1)
3363 else:
3364 #print("There is no Code")
3365 #exit(1)
3366 self.buffer[:,i] = dataOut.data[:,i*self.step:i*self.step + self.nsamples]#*code[dataOut.profileIndex,:]
3367
3368 #self.buffer[:,j,self.__nHeis-j*self.step - self.nheights:self.__nHeis-j*self.step] = numpy.flip(dataOut.data[:,j*self.step:j*self.step + self.nheights])
3369
3370 for j in range(self.buffer.shape[0]):
3371 self.sshProfiles[j] = numpy.transpose(self.buffer[j])
3372
3373 profileIndex = self.nsamples
3374 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
3375 ippSeconds = (deltaHeight*1.0e-6)/(0.15)
3376 #print "ippSeconds",ippSeconds
3377 try:
3378 if dataOut.concat_m is not None:
3379 ippSeconds= ippSeconds/float(dataOut.concat_m)
3380 #print "Profile concat %d"%dataOut.concat_m
3381 except:
3382 pass
3383
3384
3385
3386 dataOut.data = self.sshProfiles
3387 dataOut.flagNoData = False
3388 dataOut.heightList = numpy.arange(self.buffer.shape[1]) *self.step*deltaHeight + dataOut.heightList[0]
3389 dataOut.nProfiles = int(dataOut.nProfiles*self.nsamples)
3390
3391 '''
3392 print(dataOut.profileIndex)
3393 if dataOut.profileIndex == 0:
3394 dataOut.data = dataOut.data*1.e5
3395
3396 buffer_prom =
3397 '''
3398 #dataOut.utctime = dataOut.utctime - dataOut.profileIndex
3399 #print(dataOut.profileIndex)
3400 #print(dataOut.data[0,0,0])
3401 '''
3402 if dataOut.profileIndex == 0:
3403 self.buffer_prom = numpy.copy(dataOut.data)
3404
3405 else:
3406 self.buffer_prom = dataOut.data+self.buffer_prom
3407 if dataOut.profileIndex == 99:
3408 dataOut.data = self.buffer_prom/100
3409 '''
3410
3411 #print(dataOut.data[0,0,0])
3412 #print(dataOut.profileIndex)
3413 dataOut.profileIndex = profileIndex
3414 dataOut.flagDataAsBlock = True
3415 dataOut.ippSeconds = ippSeconds
3416 dataOut.step = self.step
3417 #print(dataOut.profileIndex)
3418 #print(dataOut.heightList)
3419 #exit(1)
3420
3421 #print(dataOut.times)
3422
3423 return dataOut
3424
3425 class Decoder(Operation):
3426
3427 isConfig = False
3428 __profIndex = 0
3429
3430 code = None
3431
3432 nCode = None
3433 nBaud = None
3434
3435 def __init__(self, **kwargs):
3436
3437 Operation.__init__(self, **kwargs)
3438
3439 self.times = None
3440 self.osamp = None
3441 # self.__setValues = False
3442 self.isConfig = False
3443 self.setupReq = False
3444 def setup(self, code, osamp, dataOut):
3445
3446 self.__profIndex = 0
3447
3448 self.code = code
3449
3450 self.nCode = len(code)
3451 self.nBaud = len(code[0])
3452
3453 if (osamp != None) and (osamp >1):
3454 self.osamp = osamp
3455 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
3456 self.nBaud = self.nBaud*self.osamp
3457
3458 self.__nChannels = dataOut.nChannels
3459 self.__nProfiles = dataOut.nProfiles
3460 self.__nHeis = dataOut.nHeights
3461
3462 if self.__nHeis < self.nBaud:
3463 raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud))
3464
3465 #Frequency
3466 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
3467
3468 __codeBuffer[:,0:self.nBaud] = self.code
3469
3470 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
3471
3472 if dataOut.flagDataAsBlock:
3473
3474 self.ndatadec = self.__nHeis #- self.nBaud + 1
3475
3476 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
3477
3478 else:
3479
3480 #Time
3481 self.ndatadec = self.__nHeis #- self.nBaud + 1
3482
3483
3484 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
3485
3486 def __convolutionInFreq(self, data):
3487
3488 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
3489
3490 fft_data = numpy.fft.fft(data, axis=1)
3491
3492 conv = fft_data*fft_code
3493
3494 data = numpy.fft.ifft(conv,axis=1)
3495
3496 return data
3497
3498 def __convolutionInFreqOpt(self, data):
3499
3500 raise NotImplementedError
3501
3502 def __convolutionInTime(self, data):
3503
3504 code = self.code[self.__profIndex]
3505 for i in range(self.__nChannels):
3506 #aux=numpy.correlate(data[i,:], code, mode='full')
3507 #print(numpy.shape(aux))
3508 #print(numpy.shape(data[i,:]))
3509 #print(numpy.shape(code))
3510 #exit(1)
3511 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
3512
3513 return self.datadecTime
3514
3515 def __convolutionByBlockInTime(self, data):
3516
3517 repetitions = int(self.__nProfiles / self.nCode)
3518 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
3519 junk = junk.flatten()
3520 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
3521 profilesList = range(self.__nProfiles)
3522 #print(numpy.shape(self.datadecTime))
3523 #print(numpy.shape(data))
3524 for i in range(self.__nChannels):
3525 for j in profilesList:
3526 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
3527 return self.datadecTime
3528
3529 def __convolutionByBlockInFreq(self, data):
3530
3531 raise NotImplementedError("Decoder by frequency fro Blocks not implemented")
3532
3533
3534 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
3535
3536 fft_data = numpy.fft.fft(data, axis=2)
3537
3538 conv = fft_data*fft_code
3539
3540 data = numpy.fft.ifft(conv,axis=2)
3541
3542 return data
3543
3544
3545 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
3546
3547 if dataOut.flagDecodeData:
3548 print("This data is already decoded, recoding again ...")
3549
3550 if not self.isConfig:
3551
3552 if code is None:
3553 if dataOut.code is None:
3554 raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type)
3555
3556 code = dataOut.code
3557 else:
3558 code = numpy.array(code).reshape(nCode,nBaud)
3559 self.setup(code, osamp, dataOut)
3560
3561 self.isConfig = True
3562
3563 if mode == 3:
3564 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
3565
3566 if times != None:
3567 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
3568
3569 if self.code is None:
3570 print("Fail decoding: Code is not defined.")
3571 return
3572
3573 self.__nProfiles = dataOut.nProfiles
3574 datadec = None
3575
3576 if mode == 3:
3577 mode = 0
3578
3579 if dataOut.flagDataAsBlock:
3580 """
3581 Decoding when data have been read as block,
3582 """
3583
3584 if mode == 0:
3585 datadec = self.__convolutionByBlockInTime(dataOut.data)
3586 if mode == 1:
3587 datadec = self.__convolutionByBlockInFreq(dataOut.data)
3588 else:
3589 """
3590 Decoding when data have been read profile by profile
3591 """
3592 if mode == 0:
3593 datadec = self.__convolutionInTime(dataOut.data)
3594
3595 if mode == 1:
3596 datadec = self.__convolutionInFreq(dataOut.data)
3597
3598 if mode == 2:
3599 datadec = self.__convolutionInFreqOpt(dataOut.data)
3600
3601 if datadec is None:
3602 raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode)
3603
3604 dataOut.code = self.code
3605 dataOut.nCode = self.nCode
3606 dataOut.nBaud = self.nBaud
3607
3608 dataOut.data = datadec
3609 #print("before",dataOut.heightList)
3610 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
3611 #print("after",dataOut.heightList)
3612
3613 dataOut.flagDecodeData = True #asumo q la data esta decodificada
3614
3615 if self.__profIndex == self.nCode-1:
3616 self.__profIndex = 0
3617 return dataOut
3618
3619 self.__profIndex += 1
3620
3621 #print("SHAPE",numpy.shape(dataOut.data))
3622
3623 return dataOut
3624 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
3625
3626 class DecoderRoll(Operation):
3627
3628 isConfig = False
3629 __profIndex = 0
3630
3631 code = None
3632
3633 nCode = None
3634 nBaud = None
3635
3636 def __init__(self, **kwargs):
3637
3638 Operation.__init__(self, **kwargs)
3639
3640 self.times = None
3641 self.osamp = None
3642 # self.__setValues = False
3643 self.isConfig = False
3644 self.setupReq = False
3645 def setup(self, code, osamp, dataOut):
3646
3647 self.__profIndex = 0
3648
3649
3650 self.code = code
3651
3652 self.nCode = len(code)
3653 self.nBaud = len(code[0])
3654
3655 if (osamp != None) and (osamp >1):
3656 self.osamp = osamp
3657 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
3658 self.nBaud = self.nBaud*self.osamp
3659
3660 self.__nChannels = dataOut.nChannels
3661 self.__nProfiles = dataOut.nProfiles
3662 self.__nHeis = dataOut.nHeights
3663
3664 if self.__nHeis < self.nBaud:
3665 raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud))
3666
3667 #Frequency
3668 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
3669
3670 __codeBuffer[:,0:self.nBaud] = self.code
3671
3672 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
3673
3674 if dataOut.flagDataAsBlock:
3675
3676 self.ndatadec = self.__nHeis #- self.nBaud + 1
3677
3678 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
3679
3680 else:
3681
3682 #Time
3683 self.ndatadec = self.__nHeis #- self.nBaud + 1
3684
3685
3686 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
3687
3688 def __convolutionInFreq(self, data):
3689
3690 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
3691
3692 fft_data = numpy.fft.fft(data, axis=1)
3693
3694 conv = fft_data*fft_code
3695
3696 data = numpy.fft.ifft(conv,axis=1)
3697
3698 return data
3699
3700 def __convolutionInFreqOpt(self, data):
3701
3702 raise NotImplementedError
3703
3704 def __convolutionInTime(self, data):
3705
3706 code = self.code[self.__profIndex]
3707 #print("code",code[0,0])
3708 for i in range(self.__nChannels):
3709 #aux=numpy.correlate(data[i,:], code, mode='full')
3710 #print(numpy.shape(aux))
3711 #print(numpy.shape(data[i,:]))
3712 #print(numpy.shape(code))
3713 #exit(1)
3714 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
3715
3716 return self.datadecTime
3717
3718 def __convolutionByBlockInTime(self, data):
3719
3720 repetitions = int(self.__nProfiles / self.nCode)
3721 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
3722 junk = junk.flatten()
3723 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
3724 profilesList = range(self.__nProfiles)
3725 #print(numpy.shape(self.datadecTime))
3726 #print(numpy.shape(data))
3727 for i in range(self.__nChannels):
3728 for j in profilesList:
3729 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
3730 return self.datadecTime
3731
3732 def __convolutionByBlockInFreq(self, data):
3733
3734 raise NotImplementedError("Decoder by frequency fro Blocks not implemented")
3735
3736
3737 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
3738
3739 fft_data = numpy.fft.fft(data, axis=2)
3740
3741 conv = fft_data*fft_code
3742
3743 data = numpy.fft.ifft(conv,axis=2)
3744
3745 return data
3746
3747
3748 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
3749
3750 if dataOut.flagDecodeData:
3751 print("This data is already decoded, recoding again ...")
3752
3753
3754
3755 #print(dataOut.ippSeconds)
3756 #exit(1)
3757 roll = 0
3758
3759 if self.isConfig:
3760 code = numpy.array(code)
3761
3762 #roll = 29
3763 code = numpy.roll(code,roll,axis=0)
3764 code = numpy.reshape(code,(5,100,64))
3765 block = dataOut.CurrentBlock%5
3766 #code = code[block-1,:,:] #NormalizeDPPower
3767 code = code[block-1-1,:,:] #Next Day
3768 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
3769
3770
3771 if not self.isConfig:
3772
3773 if code is None:
3774 if dataOut.code is None:
3775 raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type)
3776
3777 code = dataOut.code
3778 else:
3779 code = numpy.array(code)
3780
3781 #roll = 29
3782 code = numpy.roll(code,roll,axis=0)
3783 code = numpy.reshape(code,(5,100,64))
3784 block = dataOut.CurrentBlock%5
3785 code = code[block-1-1,:,:]
3786 #print(code.shape())
3787 #exit(1)
3788
3789 code = numpy.array(code).reshape(nCode,nBaud)
3790 self.setup(code, osamp, dataOut)
3791
3792 self.isConfig = True
3793
3794 if mode == 3:
3795 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
3796
3797 if times != None:
3798 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
3799
3800 if self.code is None:
3801 print("Fail decoding: Code is not defined.")
3802 return
3803
3804 self.__nProfiles = dataOut.nProfiles
3805 datadec = None
3806
3807 if mode == 3:
3808 mode = 0
3809
3810 if dataOut.flagDataAsBlock:
3811 """
3812 Decoding when data have been read as block,
3813 """
3814
3815 if mode == 0:
3816 datadec = self.__convolutionByBlockInTime(dataOut.data)
3817 if mode == 1:
3818 datadec = self.__convolutionByBlockInFreq(dataOut.data)
3819 else:
3820 """
3821 Decoding when data have been read profile by profile
3822 """
3823 if mode == 0:
3824 datadec = self.__convolutionInTime(dataOut.data)
3825
3826 if mode == 1:
3827 datadec = self.__convolutionInFreq(dataOut.data)
3828
3829 if mode == 2:
3830 datadec = self.__convolutionInFreqOpt(dataOut.data)
3831
3832 if datadec is None:
3833 raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode)
3834
3835 dataOut.code = self.code
3836 dataOut.nCode = self.nCode
3837 dataOut.nBaud = self.nBaud
3838
3839 dataOut.data = datadec
3840 #print("before",dataOut.heightList)
3841 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
3842 #print("after",dataOut.heightList)
3843
3844 dataOut.flagDecodeData = True #asumo q la data esta decodificada
3845
3846 if self.__profIndex == self.nCode-1:
3847 self.__profIndex = 0
3848 return dataOut
3849
3850 self.__profIndex += 1
3851
3852 #print("SHAPE",numpy.shape(dataOut.data))
3853
3854 return dataOut
3855
3856
3857
3858
3859
3860
3861 class ProfileConcat(Operation):
3862
3863 isConfig = False
3864 buffer = None
3865
3866 def __init__(self, **kwargs):
3867
3868 Operation.__init__(self, **kwargs)
3869 self.profileIndex = 0
3870
3871 def reset(self):
3872 self.buffer = numpy.zeros_like(self.buffer)
3873 self.start_index = 0
3874 self.times = 1
3875
3876 def setup(self, data, m, n=1):
3877 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
3878 self.nHeights = data.shape[1]#.nHeights
3879 self.start_index = 0
3880 self.times = 1
3881
3882 def concat(self, data):
3883
3884 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
3885 self.start_index = self.start_index + self.nHeights
3886
3887 def run(self, dataOut, m):
3888 dataOut.flagNoData = True
3889
3890 if not self.isConfig:
3891 self.setup(dataOut.data, m, 1)
3892 self.isConfig = True
3893
3894 if dataOut.flagDataAsBlock:
3895 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
3896
3897 else:
3898 self.concat(dataOut.data)
3899 self.times += 1
3900 if self.times > m:
3901 dataOut.data = self.buffer
3902 self.reset()
3903 dataOut.flagNoData = False
3904 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
3905 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
3906 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
3907 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
3908 dataOut.ippSeconds *= m
3909 return dataOut
3910
3911 class ProfileSelector(Operation):
3912
3913 profileIndex = None
3914 # Tamanho total de los perfiles
3915 nProfiles = None
3916
3917 def __init__(self, **kwargs):
3918
3919 Operation.__init__(self, **kwargs)
3920 self.profileIndex = 0
3921
3922 def incProfileIndex(self):
3923
3924 self.profileIndex += 1
3925
3926 if self.profileIndex >= self.nProfiles:
3927 self.profileIndex = 0
3928
3929 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
3930
3931 if profileIndex < minIndex:
3932 return False
3933
3934 if profileIndex > maxIndex:
3935 return False
3936
3937 return True
3938
3939 def isThisProfileInList(self, profileIndex, profileList):
3940
3941 if profileIndex not in profileList:
3942 return False
3943
3944 return True
3945
3946 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
3947
3948 """
3949 ProfileSelector:
3950
3951 Inputs:
3952 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
3953
3954 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
3955
3956 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
3957
3958 """
3959
3960 if rangeList is not None:
3961 if type(rangeList[0]) not in (tuple, list):
3962 rangeList = [rangeList]
3963
3964 dataOut.flagNoData = True
3965
3966 if dataOut.flagDataAsBlock:
3967 """
3968 data dimension = [nChannels, nProfiles, nHeis]
3969 """
3970 if profileList != None:
3971 dataOut.data = dataOut.data[:,profileList,:]
3972
3973 if profileRangeList != None:
3974 minIndex = profileRangeList[0]
3975 maxIndex = profileRangeList[1]
3976 profileList = list(range(minIndex, maxIndex+1))
3977
3978 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
3979
3980 if rangeList != None:
3981
3982 profileList = []
3983
3984 for thisRange in rangeList:
3985 minIndex = thisRange[0]
3986 maxIndex = thisRange[1]
3987
3988 profileList.extend(list(range(minIndex, maxIndex+1)))
3989
3990 dataOut.data = dataOut.data[:,profileList,:]
3991
3992 dataOut.nProfiles = len(profileList)
3993 dataOut.profileIndex = dataOut.nProfiles - 1
3994 dataOut.flagNoData = False
3995
3996 return dataOut
3997
3998 """
3999 data dimension = [nChannels, nHeis]
4000 """
4001
4002 if profileList != None:
4003
4004 if self.isThisProfileInList(dataOut.profileIndex, profileList):
4005
4006 self.nProfiles = len(profileList)
4007 dataOut.nProfiles = self.nProfiles
4008 dataOut.profileIndex = self.profileIndex
4009 dataOut.flagNoData = False
4010
4011 self.incProfileIndex()
4012 return dataOut
4013
4014 if profileRangeList != None:
4015
4016 minIndex = profileRangeList[0]
4017 maxIndex = profileRangeList[1]
4018
4019 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
4020
4021 self.nProfiles = maxIndex - minIndex + 1
4022 dataOut.nProfiles = self.nProfiles
4023 dataOut.profileIndex = self.profileIndex
4024 dataOut.flagNoData = False
4025
4026 self.incProfileIndex()
4027 return dataOut
4028
4029 if rangeList != None:
4030
4031 nProfiles = 0
4032
4033 for thisRange in rangeList:
4034 minIndex = thisRange[0]
4035 maxIndex = thisRange[1]
4036
4037 nProfiles += maxIndex - minIndex + 1
4038
4039 for thisRange in rangeList:
4040
4041 minIndex = thisRange[0]
4042 maxIndex = thisRange[1]
4043
4044 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
4045
4046 self.nProfiles = nProfiles
4047 dataOut.nProfiles = self.nProfiles
4048 dataOut.profileIndex = self.profileIndex
4049 dataOut.flagNoData = False
4050
4051 self.incProfileIndex()
4052
4053 break
4054
4055 return dataOut
4056
4057
4058 if beam != None: #beam is only for AMISR data
4059 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
4060 dataOut.flagNoData = False
4061 dataOut.profileIndex = self.profileIndex
4062
4063 self.incProfileIndex()
4064
4065 return dataOut
4066
4067 raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter")
4068
4069 #return False
4070 return dataOut
4071
4072 class Reshaper(Operation):
4073
4074 def __init__(self, **kwargs):
4075
4076 Operation.__init__(self, **kwargs)
4077
4078 self.__buffer = None
4079 self.__nitems = 0
4080
4081 def __appendProfile(self, dataOut, nTxs):
4082
4083 if self.__buffer is None:
4084 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
4085 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
4086
4087 ini = dataOut.nHeights * self.__nitems
4088 end = ini + dataOut.nHeights
4089
4090 self.__buffer[:, ini:end] = dataOut.data
4091
4092 self.__nitems += 1
4093
4094 return int(self.__nitems*nTxs)
4095
4096 def __getBuffer(self):
4097
4098 if self.__nitems == int(1./self.__nTxs):
4099
4100 self.__nitems = 0
4101
4102 return self.__buffer.copy()
4103
4104 return None
4105
4106 def __checkInputs(self, dataOut, shape, nTxs):
4107
4108 if shape is None and nTxs is None:
4109 raise ValueError("Reshaper: shape of factor should be defined")
4110
4111 if nTxs:
4112 if nTxs < 0:
4113 raise ValueError("nTxs should be greater than 0")
4114
4115 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
4116 raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs)))
4117
4118 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
4119
4120 return shape, nTxs
4121
4122 if len(shape) != 2 and len(shape) != 3:
4123 raise ValueError("shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights))
4124
4125 if len(shape) == 2:
4126 shape_tuple = [dataOut.nChannels]
4127 shape_tuple.extend(shape)
4128 else:
4129 shape_tuple = list(shape)
4130
4131 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
4132
4133 return shape_tuple, nTxs
4134
4135 def run(self, dataOut, shape=None, nTxs=None):
4136
4137 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
4138
4139 dataOut.flagNoData = True
4140 profileIndex = None
4141
4142 if dataOut.flagDataAsBlock:
4143
4144 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
4145 dataOut.flagNoData = False
4146
4147 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
4148
4149 else:
4150
4151
4152 if self.__nTxs < 1:
4153
4154 self.__appendProfile(dataOut, self.__nTxs)
4155 new_data = self.__getBuffer()
4156
4157 if new_data is not None:
4158 dataOut.data = new_data
4159 dataOut.flagNoData = False
4160
4161 profileIndex = dataOut.profileIndex*nTxs
4162
4163 else:
4164 raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)")
4165
4166 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
4167
4168 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
4169
4170 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
4171
4172 dataOut.profileIndex = profileIndex
4173
4174 dataOut.ippSeconds /= self.__nTxs
4175
4176 return dataOut
4177
4178 class SplitProfiles(Operation):
4179
4180 def __init__(self, **kwargs):
4181
4182 Operation.__init__(self, **kwargs)
4183
4184 def run(self, dataOut, n):
4185
4186 dataOut.flagNoData = True
4187 profileIndex = None
4188
4189 if dataOut.flagDataAsBlock:
4190
4191 #nchannels, nprofiles, nsamples
4192 shape = dataOut.data.shape
4193
4194 if shape[2] % n != 0:
4195 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2]))
4196
4197 new_shape = shape[0], shape[1]*n, int(shape[2]/n)
4198
4199 dataOut.data = numpy.reshape(dataOut.data, new_shape)
4200 dataOut.flagNoData = False
4201
4202 profileIndex = int(dataOut.nProfiles/n) - 1
4203
4204 else:
4205
4206 raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)")
4207
4208 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
4209
4210 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
4211
4212 dataOut.nProfiles = int(dataOut.nProfiles*n)
4213
4214 dataOut.profileIndex = profileIndex
4215
4216 dataOut.ippSeconds /= n
4217
4218 return dataOut
4219
4220 class CombineProfiles(Operation):
4221 def __init__(self, **kwargs):
4222
4223 Operation.__init__(self, **kwargs)
4224
4225 self.__remData = None
4226 self.__profileIndex = 0
4227
4228 def run(self, dataOut, n):
4229
4230 dataOut.flagNoData = True
4231 profileIndex = None
4232
4233 if dataOut.flagDataAsBlock:
4234
4235 #nchannels, nprofiles, nsamples
4236 shape = dataOut.data.shape
4237 new_shape = shape[0], shape[1]/n, shape[2]*n
4238
4239 if shape[1] % n != 0:
4240 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1]))
4241
4242 dataOut.data = numpy.reshape(dataOut.data, new_shape)
4243 dataOut.flagNoData = False
4244
4245 profileIndex = int(dataOut.nProfiles*n) - 1
4246
4247 else:
4248
4249 #nchannels, nsamples
4250 if self.__remData is None:
4251 newData = dataOut.data
4252 else:
4253 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
4254
4255 self.__profileIndex += 1
4256
4257 if self.__profileIndex < n:
4258 self.__remData = newData
4259 #continue
4260 return
4261
4262 self.__profileIndex = 0
4263 self.__remData = None
4264
4265 dataOut.data = newData
4266 dataOut.flagNoData = False
4267
4268 profileIndex = dataOut.profileIndex/n
4269
4270
4271 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
4272
4273 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
4274
4275 dataOut.nProfiles = int(dataOut.nProfiles/n)
4276
4277 dataOut.profileIndex = profileIndex
4278
4279 dataOut.ippSeconds *= n
4280
4281 return dataOut
4282 # import collections
4283 # from scipy.stats import mode
4284 #
4285 # class Synchronize(Operation):
4286 #
4287 # isConfig = False
4288 # __profIndex = 0
4289 #
4290 # def __init__(self, **kwargs):
4291 #
4292 # Operation.__init__(self, **kwargs)
4293 # # self.isConfig = False
4294 # self.__powBuffer = None
4295 # self.__startIndex = 0
4296 # self.__pulseFound = False
4297 #
4298 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
4299 #
4300 # #Read data
4301 #
4302 # powerdB = dataOut.getPower(channel = channel)
4303 # noisedB = dataOut.getNoise(channel = channel)[0]
4304 #
4305 # self.__powBuffer.extend(powerdB.flatten())
4306 #
4307 # dataArray = numpy.array(self.__powBuffer)
4308 #
4309 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
4310 #
4311 # maxValue = numpy.nanmax(filteredPower)
4312 #
4313 # if maxValue < noisedB + 10:
4314 # #No se encuentra ningun pulso de transmision
4315 # return None
4316 #
4317 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
4318 #
4319 # if len(maxValuesIndex) < 2:
4320 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
4321 # return None
4322 #
4323 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
4324 #
4325 # #Seleccionar solo valores con un espaciamiento de nSamples
4326 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
4327 #
4328 # if len(pulseIndex) < 2:
4329 # #Solo se encontro un pulso de transmision con ancho mayor a 1
4330 # return None
4331 #
4332 # spacing = pulseIndex[1:] - pulseIndex[:-1]
4333 #
4334 # #remover senales que se distancien menos de 10 unidades o muestras
4335 # #(No deberian existir IPP menor a 10 unidades)
4336 #
4337 # realIndex = numpy.where(spacing > 10 )[0]
4338 #
4339 # if len(realIndex) < 2:
4340 # #Solo se encontro un pulso de transmision con ancho mayor a 1
4341 # return None
4342 #
4343 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
4344 # realPulseIndex = pulseIndex[realIndex]
4345 #
4346 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
4347 #
4348 # print "IPP = %d samples" %period
4349 #
4350 # self.__newNSamples = dataOut.nHeights #int(period)
4351 # self.__startIndex = int(realPulseIndex[0])
4352 #
4353 # return 1
4354 #
4355 #
4356 # def setup(self, nSamples, nChannels, buffer_size = 4):
4357 #
4358 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
4359 # maxlen = buffer_size*nSamples)
4360 #
4361 # bufferList = []
4362 #
4363 # for i in range(nChannels):
4364 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
4365 # maxlen = buffer_size*nSamples)
4366 #
4367 # bufferList.append(bufferByChannel)
4368 #
4369 # self.__nSamples = nSamples
4370 # self.__nChannels = nChannels
4371 # self.__bufferList = bufferList
4372 #
4373 # def run(self, dataOut, channel = 0):
4374 #
4375 # if not self.isConfig:
4376 # nSamples = dataOut.nHeights
4377 # nChannels = dataOut.nChannels
4378 # self.setup(nSamples, nChannels)
4379 # self.isConfig = True
4380 #
4381 # #Append new data to internal buffer
4382 # for thisChannel in range(self.__nChannels):
4383 # bufferByChannel = self.__bufferList[thisChannel]
4384 # bufferByChannel.extend(dataOut.data[thisChannel])
4385 #
4386 # if self.__pulseFound:
4387 # self.__startIndex -= self.__nSamples
4388 #
4389 # #Finding Tx Pulse
4390 # if not self.__pulseFound:
4391 # indexFound = self.__findTxPulse(dataOut, channel)
4392 #
4393 # if indexFound == None:
4394 # dataOut.flagNoData = True
4395 # return
4396 #
4397 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
4398 # self.__pulseFound = True
4399 # self.__startIndex = indexFound
4400 #
4401 # #If pulse was found ...
4402 # for thisChannel in range(self.__nChannels):
4403 # bufferByChannel = self.__bufferList[thisChannel]
4404 # #print self.__startIndex
4405 # x = numpy.array(bufferByChannel)
4406 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
4407 #
4408 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
4409 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
4410 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
4411 #
4412 # dataOut.data = self.__arrayBuffer
4413 #
4414 # self.__startIndex += self.__newNSamples
4415 #
4416 # return
4417
4418
4419
4420
4421
4422
4423
4424 ##############################LONG PULSE##############################
4425
4426
4427
4428 class CrossProdHybrid(CrossProdDP):
4429 """Operation to calculate cross products of the Hybrid Experiment.
4430
4431 Parameters:
4432 -----------
4433 NLAG : int
4434 Number of lags for Long Pulse.
4435 NRANGE : int
4436 Number of samples (heights) for Long Pulse.
4437 NCAL : int
4438 .*
4439 DPL : int
4440 Number of lags for Double Pulse.
4441 NDN : int
4442 .*
4443 NDT : int
4444 Number of heights for Double Pulse.*
4445 NDP : int
4446 Number of heights for Double Pulse.*
4447 NSCAN : int
4448 Number of profiles when the transmitter is on.
4449 lagind : intlist
4450 .*
4451 lagfirst : intlist
4452 .*
4453 NAVG : int
4454 Number of blocks to be "averaged".
4455 nkill : int
4456 Number of blocks not to be considered when averaging.
4457
4458 Example
4459 --------
4460
4461 op = proc_unit.addOperation(name='CrossProdHybrid', optype='other')
4462 op.addParameter(name='NLAG', value='16', format='int')
4463 op.addParameter(name='NRANGE', value='200', format='int')
4464 op.addParameter(name='NCAL', value='0', format='int')
4465 op.addParameter(name='DPL', value='11', format='int')
4466 op.addParameter(name='NDN', value='0', format='int')
4467 op.addParameter(name='NDT', value='67', format='int')
4468 op.addParameter(name='NDP', value='67', format='int')
4469 op.addParameter(name='NSCAN', value='128', format='int')
4470 op.addParameter(name='lagind', value='(0,1,2,3,4,5,6,7,0,3,4,5,6,8,9,10)', format='intlist')
4471 op.addParameter(name='lagfirst', value='(1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1)', format='intlist')
4472 op.addParameter(name='NAVG', value='16', format='int')
4473 op.addParameter(name='nkill', value='6', format='int')
4474
4475 """
4476
4477 def __init__(self, **kwargs):
4478
4479 Operation.__init__(self, **kwargs)
4480 self.bcounter=0
4481 self.aux=1
4482 self.aux_cross_lp=1
4483 self.lag_products_LP_median_estimates_aux=1
4484
4485 def get_products_cabxys_HP(self,dataOut):
4486
4487 if self.aux==1:
4488 self.set_header_output(dataOut)
4489 self.aux=0
4490
4491 self.cax=numpy.zeros((dataOut.NDP,dataOut.DPL,2))# hp:67x11x2 dp: 66x11x2
4492 self.cay=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4493 self.cbx=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4494 self.cby=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4495 self.cax2=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4496 self.cay2=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4497 self.cbx2=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4498 self.cby2=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4499 self.caxbx=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4500 self.caxby=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4501 self.caybx=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4502 self.cayby=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4503 self.caxay=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4504 self.cbxby=numpy.zeros((dataOut.NDP,dataOut.DPL,2))
4505 for i in range(2): # flipped and unflipped
4506 for j in range(dataOut.NDP): # loop over true ranges # 67
4507 for k in range(int(dataOut.NSCAN)): # 128
4508
4509 n=dataOut.lagind[k%dataOut.NLAG] # 128=16x8
4510
4511 ax=dataOut.data[0,k,dataOut.NRANGE+dataOut.NCAL+j+i*dataOut.NDT].real-dataOut.dc.real[0]
4512 ay=dataOut.data[0,k,dataOut.NRANGE+dataOut.NCAL+j+i*dataOut.NDT].imag-dataOut.dc.imag[0]
4513
4514 if dataOut.NRANGE+dataOut.NCAL+j+i*dataOut.NDT+2*n<dataOut.read_samples:
4515
4516 bx=dataOut.data[1,k,dataOut.NRANGE+dataOut.NCAL+j+i*dataOut.NDT+2*n].real-dataOut.dc.real[1]
4517 by=dataOut.data[1,k,dataOut.NRANGE+dataOut.NCAL+j+i*dataOut.NDT+2*n].imag-dataOut.dc.imag[1]
4518
4519 else:
4520
4521 if k+1<int(dataOut.NSCAN):
4522 bx=dataOut.data[1,k+1,(dataOut.NRANGE+dataOut.NCAL+j+i*dataOut.NDT+2*n)%dataOut.NDP].real
4523 by=dataOut.data[1,k+1,(dataOut.NRANGE+dataOut.NCAL+j+i*dataOut.NDT+2*n)%dataOut.NDP].imag
4524
4525 if k+1==int(dataOut.NSCAN):## ESTO ES UN PARCHE PUES NO SE TIENE EL SIGUIENTE BLOQUE
4526 bx=dataOut.data[1,k,(dataOut.NRANGE+dataOut.NCAL+j+i*dataOut.NDT+2*n)%dataOut.NDP].real
4527 by=dataOut.data[1,k,(dataOut.NRANGE+dataOut.NCAL+j+i*dataOut.NDT+2*n)%dataOut.NDP].imag
4528
4529 if(k<dataOut.NLAG and dataOut.lagfirst[k%dataOut.NLAG]==1):# if(k<16 && lagfirst[k%16]==1)
4530 self.cax[j][n][i]=ax
4531 self.cay[j][n][i]=ay
4532 self.cbx[j][n][i]=bx
4533 self.cby[j][n][i]=by
4534 self.cax2[j][n][i]=ax*ax
4535 self.cay2[j][n][i]=ay*ay
4536 self.cbx2[j][n][i]=bx*bx
4537 self.cby2[j][n][i]=by*by
4538 self.caxbx[j][n][i]=ax*bx
4539 self.caxby[j][n][i]=ax*by
4540 self.caybx[j][n][i]=ay*bx
4541 self.cayby[j][n][i]=ay*by
4542 self.caxay[j][n][i]=ax*ay
4543 self.cbxby[j][n][i]=bx*by
4544 else:
4545 self.cax[j][n][i]+=ax
4546 self.cay[j][n][i]+=ay
4547 self.cbx[j][n][i]+=bx
4548 self.cby[j][n][i]+=by
4549 self.cax2[j][n][i]+=ax*ax
4550 self.cay2[j][n][i]+=ay*ay
4551 self.cbx2[j][n][i]+=bx*bx
4552 self.cby2[j][n][i]+=by*by
4553 self.caxbx[j][n][i]+=ax*bx
4554 self.caxby[j][n][i]+=ax*by
4555 self.caybx[j][n][i]+=ay*bx
4556 self.cayby[j][n][i]+=ay*by
4557 self.caxay[j][n][i]+=ax*ay
4558 self.cbxby[j][n][i]+=bx*by
4559
4560
4561 #print(self.cax2[2,0,1])
4562 #input()
4563
4564
4565 def lag_products_LP(self,dataOut):
4566
4567
4568 buffer=dataOut.data
4569 if self.aux_cross_lp==1:
4570
4571 #self.dataOut.nptsfft2=150
4572 self.cnorm=float((dataOut.nProfiles-dataOut.NSCAN)/dataOut.NSCAN)
4573 self.lagp0=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4574 self.lagp1=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4575 self.lagp2=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4576 self.lagp3=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4577
4578 #self.lagp4=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4579 self.aux_cross_lp=0
4580
4581 #print(self.dataOut.data[0,0,0])
4582
4583 for i in range(dataOut.NR):
4584 #print("inside i",i)
4585 buffer_dc=dataOut.dc[i]
4586 for j in range(dataOut.NRANGE):
4587
4588 range_for_n=numpy.min((dataOut.NRANGE-j,dataOut.NLAG))
4589
4590 buffer_aux=numpy.conj(buffer[i,:dataOut.nProfiles,j]-buffer_dc)
4591 for n in range(range_for_n):
4592
4593 c=(buffer_aux)*(buffer[i,:dataOut.nProfiles,j+n]-buffer_dc)
4594
4595 if i==0:
4596 self.lagp0[n][j][self.bcounter-1]=numpy.sum(c[:dataOut.NSCAN])
4597 self.lagp3[n][j][self.bcounter-1]=numpy.sum(c[dataOut.NSCAN:]/self.cnorm)
4598 elif i==1:
4599 self.lagp1[n][j][self.bcounter-1]=numpy.sum(c[:dataOut.NSCAN])
4600 elif i==2:
4601 self.lagp2[n][j][self.bcounter-1]=numpy.sum(c[:dataOut.NSCAN])
4602
4603
4604 self.lagp0[:,:,self.bcounter-1]=numpy.conj(self.lagp0[:,:,self.bcounter-1])
4605 self.lagp1[:,:,self.bcounter-1]=numpy.conj(self.lagp1[:,:,self.bcounter-1])
4606 self.lagp2[:,:,self.bcounter-1]=numpy.conj(self.lagp2[:,:,self.bcounter-1])
4607 self.lagp3[:,:,self.bcounter-1]=numpy.conj(self.lagp3[:,:,self.bcounter-1])
4608
4609
4610 def LP_median_estimates(self,dataOut):
4611
4612 if self.bcounter==dataOut.NAVG:
4613
4614 if self.lag_products_LP_median_estimates_aux==1:
4615 self.output=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NR),'complex64')
4616 self.lag_products_LP_median_estimates_aux=0
4617
4618
4619 for i in range(dataOut.NLAG):
4620 for j in range(dataOut.NRANGE):
4621 for l in range(4): #four outputs
4622
4623 for k in range(dataOut.NAVG):
4624
4625
4626 if k==0:
4627 self.output[i,j,l]=0.0+0.j
4628
4629 if l==0:
4630 self.lagp0[i,j,:]=sorted(self.lagp0[i,j,:], key=lambda x: x.real) #sorted(self.lagp0[i,j,:].real)
4631
4632 if l==1:
4633 self.lagp1[i,j,:]=sorted(self.lagp1[i,j,:], key=lambda x: x.real) #sorted(self.lagp1[i,j,:].real)
4634
4635 if l==2:
4636 self.lagp2[i,j,:]=sorted(self.lagp2[i,j,:], key=lambda x: x.real) #sorted(self.lagp2[i,j,:].real)
4637
4638 if l==3:
4639 self.lagp3[i,j,:]=sorted(self.lagp3[i,j,:], key=lambda x: x.real) #sorted(self.lagp3[i,j,:].real)
4640
4641
4642
4643 if k>=dataOut.nkill/2 and k<dataOut.NAVG-dataOut.nkill/2:
4644 if l==0:
4645
4646 self.output[i,j,l]=self.output[i,j,l]+((float(dataOut.NAVG)/(float)(dataOut.NAVG-dataOut.nkill))*self.lagp0[i,j,k])
4647 if l==1:
4648 #print("lagp1: ",self.lagp1[0,0,:])
4649 #input()
4650 self.output[i,j,l]=self.output[i,j,l]+((float(dataOut.NAVG)/(float)(dataOut.NAVG-dataOut.nkill))*self.lagp1[i,j,k])
4651 #print("self.lagp1[i,j,k]: ",self.lagp1[i,j,k])
4652 #input()
4653 if l==2:
4654 self.output[i,j,l]=self.output[i,j,l]+((float(dataOut.NAVG)/(float)(dataOut.NAVG-dataOut.nkill))*self.lagp2[i,j,k])
4655 if l==3:
4656
4657 self.output[i,j,l]=self.output[i,j,l]+((float(dataOut.NAVG)/(float)(dataOut.NAVG-dataOut.nkill))*self.lagp3[i,j,k])
4658
4659
4660 dataOut.output_LP=self.output
4661 dataOut.data_for_RTI_LP=numpy.zeros((4,dataOut.NRANGE))
4662 dataOut.data_for_RTI_LP[0],dataOut.data_for_RTI_LP[1],dataOut.data_for_RTI_LP[2],dataOut.data_for_RTI_LP[3]=self.RTI_LP(dataOut.output_LP,dataOut.NRANGE)
4663
4664
4665 def get_dc(self,dataOut):
4666
4667 if self.bcounter==0:
4668 dataOut.dc=numpy.zeros(dataOut.NR,dtype='complex64')
4669
4670 #print(numpy.shape(dataOut.data))
4671 #input()
4672
4673 dataOut.dc+=numpy.sum(dataOut.data[:,:,2*dataOut.NLAG:dataOut.NRANGE],axis=(1,2))
4674
4675 dataOut.dc=dataOut.dc/float(dataOut.nProfiles*(dataOut.NRANGE-2*dataOut.NLAG))
4676
4677
4678 #print("dc:",dataOut.dc[0])
4679
4680 def get_dc_new(self,dataOut):
4681
4682 if self.bcounter==0:
4683 dataOut.dc_dp=numpy.zeros(dataOut.NR,dtype='complex64')
4684 dataOut.dc_lp=numpy.zeros(dataOut.NR,dtype='complex64')
4685
4686 #print(numpy.shape(dataOut.data))
4687 #input()
4688
4689 dataOut.dc+=numpy.sum(dataOut.data[:,:,2*dataOut.NLAG:dataOut.NRANGE],axis=(1,2))
4690
4691 dataOut.dc=dataOut.dc/float(dataOut.nProfiles*(dataOut.NRANGE-2*dataOut.NLAG))
4692
4693
4694 #print("dc:",dataOut.dc[0])
4695
4696
4697 def noise_estimation4x_HP(self,dataOut):
4698 if self.bcounter==dataOut.NAVG:
4699 dataOut.noise_final=numpy.zeros(dataOut.NR,'float32')
4700 #snoise=numpy.zeros((NR,NAVG),'float32')
4701 #nvector1=numpy.zeros((NR,NAVG,MAXNRANGENDT),'float32')
4702 sorted_data=numpy.zeros((dataOut.MAXNRANGENDT,dataOut.NR,dataOut.NAVG),'float32')
4703 for i in range(dataOut.NR):
4704 dataOut.noise_final[i]=0.0
4705 for j in range(dataOut.MAXNRANGENDT):
4706 sorted_data[j,i,:]=numpy.copy(sorted(dataOut.noisevector[j,i,:]))
4707 #print(sorted(noisevector[j,i,:]))
4708 #input()
4709 l=dataOut.MAXNRANGENDT-2
4710 for k in range(dataOut.NAVG):
4711 if k>=dataOut.nkill/2 and k<dataOut.NAVG-dataOut.nkill/2:
4712 #print(k)
4713 #print(sorted_data[min(j,l),i,k])
4714 dataOut.noise_final[i]+=sorted_data[min(j,l),i,k]*float(dataOut.NAVG)/float(dataOut.NAVG-dataOut.nkill)
4715 #print(dataOut.noise_final[i])
4716 #input()
4717 #print(dataOut.noise_final)
4718 #input()
4719
4720 def noisevectorizer(self,NSCAN,nProfiles,NR,MAXNRANGENDT,noisevector,data,dc):
4721
4722 #rnormalizer= 1./(float(nProfiles - NSCAN))
4723 rnormalizer= float(NSCAN)/((float(nProfiles - NSCAN))*float(MAXNRANGENDT))
4724 for i in range(NR):
4725 for j in range(MAXNRANGENDT):
4726 for k in range(NSCAN,nProfiles):
4727 #TODO:integrate just 2nd quartile gates
4728 if k==NSCAN:
4729 noisevector[j][i][self.bcounter]=(abs(data[i][k][j]-dc[i])**2)*rnormalizer
4730 ##noisevector[j][i][iavg]=(abs(cdata[k][j][i])**2)*rnormalizer
4731 else:
4732 noisevector[j][i][self.bcounter]+=(abs(data[i][k][j]-dc[i])**2)*rnormalizer
4733
4734
4735 def RTI_LP(self,output,NRANGE):
4736 x00=numpy.zeros(NRANGE,dtype='float32')
4737 x01=numpy.zeros(NRANGE,dtype='float32')
4738 x02=numpy.zeros(NRANGE,dtype='float32')
4739 x03=numpy.zeros(NRANGE,dtype='float32')
4740
4741 for i in range(2): #first couple of lags
4742 for j in range(NRANGE): #
4743 #fx=numpy.sqrt((kaxbx[i,j,k]+kayby[i,j,k])**2+(kaybx[i,j,k]-kaxby[i,j,k])**2)
4744 x00[j]+=numpy.abs(output[i,j,0]) #Ch0
4745 x01[j]+=numpy.abs(output[i,j,1]) #Ch1
4746 x02[j]+=numpy.abs(output[i,j,2]) #Ch2
4747 x03[j]+=numpy.abs(output[i,j,3]) #Ch3
4748 #x02[i]=x02[i]+fx
4749
4750 x00[j]=10.0*numpy.log10(x00[j]/4.)
4751 x01[j]=10.0*numpy.log10(x01[j]/4.)
4752 x02[j]=10.0*numpy.log10(x02[j]/4.)
4753 x03[j]=10.0*numpy.log10(x03[j]/4.)
4754 #x02[i]=10.0*numpy.log10(x02[i])
4755 return x00,x01,x02,x03
4756
4757 def run(self, dataOut, NLAG=None, NRANGE=None, NCAL=None, DPL=None,
4758 NDN=None, NDT=None, NDP=None, NSCAN=None,
4759 lagind=None, lagfirst=None,
4760 NAVG=None, nkill=None):
4761
4762 dataOut.NLAG=NLAG
4763 dataOut.NR=len(dataOut.channelList)
4764 dataOut.NRANGE=NRANGE
4765 dataOut.NCAL=NCAL
4766 dataOut.DPL=DPL
4767 dataOut.NDN=NDN
4768 dataOut.NDT=NDT
4769 dataOut.NDP=NDP
4770 dataOut.NSCAN=NSCAN
4771 dataOut.DH=dataOut.heightList[1]-dataOut.heightList[0]
4772 dataOut.H0=int(dataOut.heightList[0])
4773 dataOut.lagind=lagind
4774 dataOut.lagfirst=lagfirst
4775 dataOut.NAVG=NAVG
4776 dataOut.nkill=nkill
4777
4778 dataOut.flagNoData = True
4779
4780 self.get_dc(dataOut)
4781 self.get_products_cabxys_HP(dataOut)
4782 self.cabxys_navg(dataOut)
4783 self.lag_products_LP(dataOut)
4784 self.LP_median_estimates(dataOut)
4785 self.noise_estimation4x_HP(dataOut)
4786 self.kabxys(dataOut)
4787
4788 return dataOut
4789
4790
4791 class CrossProdLP(CrossProdDP):
4792 """Operation to calculate cross products of the Hybrid Experiment.
4793
4794 Parameters:
4795 -----------
4796 NLAG : int
4797 Number of lags for Long Pulse.
4798 NRANGE : int
4799 Number of samples (heights) for Long Pulse.
4800 NCAL : int
4801 .*
4802 DPL : int
4803 Number of lags for Double Pulse.
4804 NDN : int
4805 .*
4806 NDT : int
4807 Number of heights for Double Pulse.*
4808 NDP : int
4809 Number of heights for Double Pulse.*
4810 NSCAN : int
4811 Number of profiles when the transmitter is on.
4812 lagind : intlist
4813 .*
4814 lagfirst : intlist
4815 .*
4816 NAVG : int
4817 Number of blocks to be "averaged".
4818 nkill : int
4819 Number of blocks not to be considered when averaging.
4820
4821 Example
4822 --------
4823
4824 op = proc_unit.addOperation(name='CrossProdHybrid', optype='other')
4825 op.addParameter(name='NLAG', value='16', format='int')
4826 op.addParameter(name='NRANGE', value='200', format='int')
4827 op.addParameter(name='NCAL', value='0', format='int')
4828 op.addParameter(name='DPL', value='11', format='int')
4829 op.addParameter(name='NDN', value='0', format='int')
4830 op.addParameter(name='NDT', value='67', format='int')
4831 op.addParameter(name='NDP', value='67', format='int')
4832 op.addParameter(name='NSCAN', value='128', format='int')
4833 op.addParameter(name='lagind', value='(0,1,2,3,4,5,6,7,0,3,4,5,6,8,9,10)', format='intlist')
4834 op.addParameter(name='lagfirst', value='(1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1)', format='intlist')
4835 op.addParameter(name='NAVG', value='16', format='int')
4836 op.addParameter(name='nkill', value='6', format='int')
4837
4838 """
4839
4840 def __init__(self, **kwargs):
4841
4842 Operation.__init__(self, **kwargs)
4843 self.bcounter=0
4844 self.aux=1
4845 self.aux_cross_lp=1
4846 self.lag_products_LP_median_estimates_aux=1
4847
4848
4849
4850 #print(self.cax2[2,0,1])
4851 #input()
4852
4853
4854 def lag_products_LP(self,dataOut):
4855
4856
4857 buffer=dataOut.data
4858 if self.aux_cross_lp==1:
4859
4860 #self.dataOut.nptsfft2=150
4861 self.cnorm=float((dataOut.nProfiles-dataOut.NSCAN)/dataOut.NSCAN)
4862 self.lagp0=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4863 self.lagp1=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4864 self.lagp2=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4865 self.lagp3=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4866 self.lagp4=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4867 self.lagp5=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4868
4869 #self.lagp4=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NAVG),'complex64')
4870 self.aux_cross_lp=0
4871
4872 dataOut.noisevector=numpy.zeros((dataOut.MAXNRANGENDT,dataOut.NR,dataOut.NAVG),'float32')
4873
4874 #print(self.dataOut.data[0,0,0])
4875 self.noisevectorizer(dataOut.NSCAN,dataOut.nProfiles,dataOut.NR,dataOut.MAXNRANGENDT,dataOut.noisevector,dataOut.data,dataOut.dc) #30/03/2020
4876
4877
4878 for i in range(dataOut.NR):
4879 #print("inside i",i)
4880 buffer_dc=dataOut.dc[i]
4881 for j in range(dataOut.NRANGE):
4882
4883 range_for_n=numpy.min((dataOut.NRANGE-j,dataOut.NLAG))
4884
4885 buffer_aux=numpy.conj(buffer[i,:dataOut.nProfiles,j]-buffer_dc)
4886 for n in range(range_for_n):
4887
4888 c=(buffer_aux)*(buffer[i,:dataOut.nProfiles,j+n]-buffer_dc)
4889
4890 if i==0:
4891 self.lagp0[n][j][self.bcounter]=numpy.sum(c[:dataOut.NSCAN])
4892 #self.lagp3[n][j][self.bcounter-1]=numpy.sum(c[dataOut.NSCAN:]/self.cnorm)
4893 elif i==1:
4894 self.lagp1[n][j][self.bcounter]=numpy.sum(c[:dataOut.NSCAN])
4895 elif i==2:
4896 self.lagp2[n][j][self.bcounter]=numpy.sum(c[:dataOut.NSCAN])
4897 elif i==3:
4898 self.lagp3[n][j][self.bcounter]=numpy.sum(c[:dataOut.NSCAN])
4899 elif i==4:
4900 self.lagp4[n][j][self.bcounter]=numpy.sum(c[:dataOut.NSCAN])
4901 elif i==5:
4902 self.lagp5[n][j][self.bcounter]=numpy.sum(c[:dataOut.NSCAN])
4903
4904
4905 self.lagp0[:,:,self.bcounter]=numpy.conj(self.lagp0[:,:,self.bcounter])
4906 self.lagp1[:,:,self.bcounter]=numpy.conj(self.lagp1[:,:,self.bcounter])
4907 self.lagp2[:,:,self.bcounter]=numpy.conj(self.lagp2[:,:,self.bcounter])
4908 self.lagp3[:,:,self.bcounter]=numpy.conj(self.lagp3[:,:,self.bcounter])
4909
4910 self.bcounter += 1
4911
4912
4913 def LP_median_estimates(self,dataOut):
4914
4915 if self.bcounter==dataOut.NAVG:
4916 dataOut.flagNoData = False
4917
4918 if self.lag_products_LP_median_estimates_aux==1:
4919 self.output=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NR),'complex64')
4920 self.lag_products_LP_median_estimates_aux=0
4921
4922
4923 for i in range(dataOut.NLAG):
4924 for j in range(dataOut.NRANGE):
4925 for l in range(4): #four outputs
4926
4927 for k in range(dataOut.NAVG):
4928
4929
4930 if k==0:
4931 self.output[i,j,l]=0.0+0.j
4932
4933 if l==0:
4934 self.lagp0[i,j,:]=sorted(self.lagp0[i,j,:], key=lambda x: x.real) #sorted(self.lagp0[i,j,:].real)
4935
4936 if l==1:
4937 self.lagp1[i,j,:]=sorted(self.lagp1[i,j,:], key=lambda x: x.real) #sorted(self.lagp1[i,j,:].real)
4938
4939 if l==2:
4940 self.lagp2[i,j,:]=sorted(self.lagp2[i,j,:], key=lambda x: x.real) #sorted(self.lagp2[i,j,:].real)
4941
4942 if l==3:
4943 self.lagp3[i,j,:]=sorted(self.lagp3[i,j,:], key=lambda x: x.real) #sorted(self.lagp3[i,j,:].real)
4944
4945
4946
4947 if k>=dataOut.nkill/2 and k<dataOut.NAVG-dataOut.nkill/2:
4948 if l==0:
4949
4950 self.output[i,j,l]=self.output[i,j,l]+((float(dataOut.NAVG)/(float)(dataOut.NAVG-dataOut.nkill))*self.lagp0[i,j,k])
4951 if l==1:
4952 #print("lagp1: ",self.lagp1[0,0,:])
4953 #input()
4954 self.output[i,j,l]=self.output[i,j,l]+((float(dataOut.NAVG)/(float)(dataOut.NAVG-dataOut.nkill))*self.lagp1[i,j,k])
4955 #print("self.lagp1[i,j,k]: ",self.lagp1[i,j,k])
4956 #input()
4957 if l==2:
4958 self.output[i,j,l]=self.output[i,j,l]+((float(dataOut.NAVG)/(float)(dataOut.NAVG-dataOut.nkill))*self.lagp2[i,j,k])
4959 if l==3:
4960
4961 self.output[i,j,l]=self.output[i,j,l]+((float(dataOut.NAVG)/(float)(dataOut.NAVG-dataOut.nkill))*self.lagp3[i,j,k])
4962
4963
4964 dataOut.output_LP=self.output
4965 dataOut.data_for_RTI_LP=numpy.zeros((4,dataOut.NRANGE))
4966 dataOut.data_for_RTI_LP[0],dataOut.data_for_RTI_LP[1],dataOut.data_for_RTI_LP[2],dataOut.data_for_RTI_LP[3]=self.RTI_LP(dataOut.output_LP,dataOut.NRANGE)
4967
4968 self.bcounter = 0
4969
4970 def get_dc(self,dataOut):
4971
4972 if self.bcounter==0:
4973 dataOut.dc=numpy.zeros(dataOut.NR,dtype='complex64')
4974
4975 #print(numpy.shape(dataOut.data))
4976 #input()
4977
4978 dataOut.dc+=numpy.sum(dataOut.data[:,:,2*dataOut.NLAG:dataOut.NRANGE],axis=(1,2))
4979
4980 dataOut.dc=dataOut.dc/float(dataOut.nProfiles*(dataOut.NRANGE-2*dataOut.NLAG))
4981
4982
4983 #print("dc:",dataOut.dc[0])
4984
4985
4986
4987
4988 def noise_estimation4x_HP(self,dataOut):
4989 if self.bcounter==dataOut.NAVG:
4990 dataOut.noise_final=numpy.zeros(dataOut.NR,'float32')
4991 #snoise=numpy.zeros((NR,NAVG),'float32')
4992 #nvector1=numpy.zeros((NR,NAVG,MAXNRANGENDT),'float32')
4993 sorted_data=numpy.zeros((dataOut.MAXNRANGENDT,dataOut.NR,dataOut.NAVG),'float32')
4994 for i in range(dataOut.NR):
4995 dataOut.noise_final[i]=0.0
4996 for j in range(dataOut.MAXNRANGENDT):
4997 sorted_data[j,i,:]=numpy.copy(sorted(dataOut.noisevector[j,i,:]))
4998 #print(sorted(noisevector[j,i,:]))
4999 #input()
5000 l=dataOut.MAXNRANGENDT-2
5001 for k in range(dataOut.NAVG):
5002 if k>=dataOut.nkill/2 and k<dataOut.NAVG-dataOut.nkill/2:
5003 #print(k)
5004 #print(sorted_data[min(j,l),i,k])
5005 dataOut.noise_final[i]+=sorted_data[min(j,l),i,k]*float(dataOut.NAVG)/float(dataOut.NAVG-dataOut.nkill)
5006 #print(dataOut.noise_final[i])
5007 #input()
5008 #print(dataOut.noise_final)
5009 #input()
5010
5011 def noisevectorizer(self,NSCAN,nProfiles,NR,MAXNRANGENDT,noisevector,data,dc):
5012
5013 #rnormalizer= 1./(float(nProfiles - NSCAN))
5014 #rnormalizer= float(NSCAN)/((float(nProfiles - NSCAN))*float(MAXNRANGENDT))
5015 rnormalizer= float(NSCAN)/((float(1))*float(MAXNRANGENDT))
5016 for i in range(NR):
5017 for j in range(MAXNRANGENDT):
5018 for k in range(NSCAN,nProfiles):
5019 #TODO:integrate just 2nd quartile gates
5020 if k==NSCAN:
5021 noisevector[j][i][self.bcounter]=(abs(data[i][k][j]-dc[i])**2)*rnormalizer
5022 ##noisevector[j][i][iavg]=(abs(cdata[k][j][i])**2)*rnormalizer
5023 else:
5024 noisevector[j][i][self.bcounter]+=(abs(data[i][k][j]-dc[i])**2)*rnormalizer
5025
5026
5027 def RTI_LP(self,output,NRANGE):
5028 x00=numpy.zeros(NRANGE,dtype='float32')
5029 x01=numpy.zeros(NRANGE,dtype='float32')
5030 x02=numpy.zeros(NRANGE,dtype='float32')
5031 x03=numpy.zeros(NRANGE,dtype='float32')
5032
5033 for i in range(1): #first couple of lags
5034 for j in range(NRANGE): #
5035 #fx=numpy.sqrt((kaxbx[i,j,k]+kayby[i,j,k])**2+(kaybx[i,j,k]-kaxby[i,j,k])**2)
5036 x00[j]+=numpy.abs(output[i,j,0]) #Ch0
5037 x01[j]+=numpy.abs(output[i,j,1]) #Ch1
5038 x02[j]+=numpy.abs(output[i,j,2]) #Ch2
5039 x03[j]+=numpy.abs(output[i,j,3]) #Ch3
5040 #x02[i]=x02[i]+fx
5041
5042 x00[j]=10.0*numpy.log10(x00[j]/4.)
5043 x01[j]=10.0*numpy.log10(x01[j]/4.)
5044 x02[j]=10.0*numpy.log10(x02[j]/4.)
5045 x03[j]=10.0*numpy.log10(x03[j]/4.)
5046 #x02[i]=10.0*numpy.log10(x02[i])
5047 return x00,x01,x02,x03
5048
5049 def run(self, dataOut, NLAG=None, NRANGE=None, NCAL=None, DPL=None,
5050 NDN=None, NDT=None, NDP=None, NSCAN=None,
5051 lagind=None, lagfirst=None,
5052 NAVG=None, nkill=None):
5053
5054 dataOut.NLAG=NLAG
5055 dataOut.NR=len(dataOut.channelList)
5056 #dataOut.NRANGE=NRANGE
5057 dataOut.NRANGE=dataOut.nHeights
5058 dataOut.NCAL=NCAL
5059 dataOut.DPL=DPL
5060 dataOut.NDN=NDN
5061 dataOut.NDT=NDT
5062 dataOut.NDP=NDP
5063 dataOut.NSCAN=NSCAN
5064 dataOut.DH=dataOut.heightList[1]-dataOut.heightList[0]
5065 dataOut.H0=int(dataOut.heightList[0])
5066 dataOut.lagind=lagind
5067 dataOut.lagfirst=lagfirst
5068 dataOut.NAVG=NAVG
5069 dataOut.nkill=nkill
5070
5071 dataOut.MAXNRANGENDT = dataOut.NRANGE
5072
5073 dataOut.flagNoData = True
5074
5075 print(self.bcounter)
5076
5077 self.get_dc(dataOut)
5078 self.lag_products_LP(dataOut)
5079 self.noise_estimation4x_HP(dataOut)
5080 self.LP_median_estimates(dataOut)
5081
5082 print("******************DONE******************")
5083
5084
5085
5086 return dataOut
5087
5088
5089 class RemoveDebris(Operation):
5090 """Operation to remove blocks where an outlier is found for Double (Long) Pulse.
5091
5092 Parameters:
5093 -----------
5094 None
5095
5096 Example
5097 --------
5098
5099 op = proc_unit.addOperation(name='RemoveDebris', optype='other')
5100
5101 """
5102
5103 def __init__(self, **kwargs):
5104
5105 Operation.__init__(self, **kwargs)
5106
5107 def run(self,dataOut):
5108 print("init_debris",dataOut.flagNoData)
5109 #dataOut.debris_activated=0
5110 debris=numpy.zeros(dataOut.NRANGE,'float32')
5111
5112 for j in range(0,3):
5113 for i in range(dataOut.NRANGE):
5114 if j==0:
5115 debris[i]=10*numpy.log10(numpy.abs(dataOut.output_LP[j,i,0]))
5116 else:
5117 debris[i]+=10*numpy.log10(numpy.abs(dataOut.output_LP[j,i,0]))
5118
5119 thresh=8.0+4+4+4
5120 for i in range(47,100):
5121 if ((debris[i-2]+debris[i-1]+debris[i]+debris[i+1])>
5122 ((debris[i-12]+debris[i-11]+debris[i-10]+debris[i-9]+
5123 debris[i+12]+debris[i+11]+debris[i+10]+debris[i+9])/2.0+
5124 thresh)):
5125
5126 dataOut.flagNoData=True
5127 print("LP Debris detected at",i*15,"km")
5128
5129 debris=numpy.zeros(dataOut.NDP,dtype='float32')
5130 Range=numpy.arange(0,3000,15)
5131 for k in range(2): #flip
5132 for i in range(dataOut.NDP): #
5133 debris[i]+=numpy.sqrt((dataOut.kaxbx[i,0,k]+dataOut.kayby[i,0,k])**2+(dataOut.kaybx[i,0,k]-dataOut.kaxby[i,0,k])**2)
5134
5135 if gmtime(dataOut.utctime).tm_hour > 11:
5136 for i in range(2,dataOut.NDP-2):
5137 if (debris[i]>3.0*debris[i-2] and
5138 debris[i]>3.0*debris[i+2] and
5139 Range[i]>200.0 and Range[i]<=540.0):
5140 dataOut.flagNoData=True
5141 print("DP Debris detected at",i*15,"km")
5142
5143 print("inside debris",dataOut.flagNoData)
5144 return dataOut
5145
5146
5147 class IntegrationHP(IntegrationDP):
5148 """Operation to integrate Double Pulse and Long Pulse data.
5149
5150 Parameters:
5151 -----------
5152 nint : int
5153 Number of integrations.
5154
5155 Example
5156 --------
5157
5158 op = proc_unit.addOperation(name='IntegrationHP', optype='other')
5159 op.addParameter(name='nint', value='30', format='int')
5160
5161 """
5162
5163 def __init__(self, **kwargs):
5164
5165 Operation.__init__(self, **kwargs)
5166
5167 self.counter = 0
5168 self.aux = 0
5169
5170 def integration_noise(self,dataOut):
5171
5172 if self.counter == 0:
5173 dataOut.tnoise=numpy.zeros((dataOut.NR),dtype='float32')
5174
5175 dataOut.tnoise+=dataOut.noise_final
922 5176
923 profileIndex = None
924 # Tamanho total de los perfiles
925 nProfiles = None
5177 def integration_for_long_pulse(self,dataOut):
926 5178
927 def __init__(self, **kwargs):
5179 if self.counter == 0:
5180 dataOut.output_LP_integrated=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NR),order='F',dtype='complex64')
928 5181
929 Operation.__init__(self, **kwargs)
930 self.profileIndex = 0
5182 dataOut.output_LP_integrated+=dataOut.output_LP
931 5183
932 def incProfileIndex(self):
5184 def run(self,dataOut,nint=None):
933 5185
934 self.profileIndex += 1
5186 dataOut.flagNoData=True
935 5187
936 if self.profileIndex >= self.nProfiles:
937 self.profileIndex = 0
5188 #print("flag_inside",dataOut.flagNoData)
5189 dataOut.nint=nint
5190 dataOut.paramInterval=0#int(dataOut.nint*dataOut.header[7][0]*2 )
5191 dataOut.lat=-11.95
5192 dataOut.lon=-76.87
938 5193
939 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
5194 self.integration_for_long_pulse(dataOut)
940 5195
941 if profileIndex < minIndex:
942 return False
5196 self.integration_noise(dataOut)
943 5197
944 if profileIndex > maxIndex:
945 return False
946 5198
947 return True
5199 if self.counter==dataOut.nint-1:
948 5200
949 def isThisProfileInList(self, profileIndex, profileList):
5201 dataOut.tnoise[0]*=0.995
5202 dataOut.tnoise[1]*=0.995
5203 dataOut.pan=dataOut.tnoise[0]/float(dataOut.NSCAN*dataOut.nint*dataOut.NAVG)
5204 dataOut.pbn=dataOut.tnoise[1]/float(dataOut.NSCAN*dataOut.nint*dataOut.NAVG)
950 5205
951 if profileIndex not in profileList:
952 return False
5206 self.integration_for_double_pulse(dataOut)
953 5207
954 return True
955 5208
956 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
957 5209
958 """
959 ProfileSelector:
5210 return dataOut
960 5211
961 Inputs:
962 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
963 5212
964 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
5213 class IntegrationLP(Operation):
5214 """Operation to integrate Double Pulse and Long Pulse data.
965 5215
966 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
5216 Parameters:
5217 -----------
5218 nint : int
5219 Number of integrations.
967 5220
968 """
5221 Example
5222 --------
969 5223
970 if rangeList is not None:
971 if type(rangeList[0]) not in (tuple, list):
972 rangeList = [rangeList]
5224 op = proc_unit.addOperation(name='IntegrationHP', optype='other')
5225 op.addParameter(name='nint', value='30', format='int')
973 5226
974 dataOut.flagNoData = True
5227 """
975 5228
976 if dataOut.flagDataAsBlock:
977 """
978 data dimension = [nChannels, nProfiles, nHeis]
979 """
980 if profileList != None:
981 dataOut.data = dataOut.data[:,profileList,:]
5229 def __init__(self, **kwargs):
982 5230
983 if profileRangeList != None:
984 minIndex = profileRangeList[0]
985 maxIndex = profileRangeList[1]
986 profileList = list(range(minIndex, maxIndex+1))
5231 Operation.__init__(self, **kwargs)
987 5232
988 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
5233 self.counter = 0
5234 self.aux = 0
989 5235
990 if rangeList != None:
5236 def integration_noise(self,dataOut):
991 5237
992 profileList = []
5238 if self.counter == 0:
5239 dataOut.tnoise=numpy.zeros((dataOut.NR),dtype='float32')
993 5240
994 for thisRange in rangeList:
995 minIndex = thisRange[0]
996 maxIndex = thisRange[1]
5241 dataOut.tnoise+=dataOut.noise_final
5242 '''
5243 def integration_for_long_pulse(self,dataOut):
997 5244
998 profileList.extend(list(range(minIndex, maxIndex+1)))
5245 if self.counter == 0:
5246 dataOut.output_LP_integrated=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NR),order='F',dtype='complex64')
999 5247
1000 dataOut.data = dataOut.data[:,profileList,:]
5248 dataOut.output_LP_integrated+=dataOut.output_LP
5249 '''
5250 def integration_for_long_pulse(self,dataOut):
5251 #print("inside")
5252 #print(self.aux)
1001 5253
1002 dataOut.nProfiles = len(profileList)
1003 dataOut.profileIndex = dataOut.nProfiles - 1
1004 dataOut.flagNoData = False
5254 if self.counter == 0:
5255 dataOut.output_LP_integrated=numpy.zeros((dataOut.NLAG,dataOut.NRANGE,dataOut.NR),order='F',dtype='complex64')
1005 5256
1006 return dataOut
5257 dataOut.output_LP_integrated+=dataOut.output_LP
1007 5258
1008 """
1009 data dimension = [nChannels, nHeis]
1010 """
5259 if self.aux==1:
5260 #print("CurrentBlockBBBBB: ",dataOut.CurrentBlock)
5261 #print(dataOut.datatime)
1011 5262
1012 if profileList != None:
5263 #dataOut.TimeBlockDate_for_dp_power=dataOut.TimeBlockDate
5264 ########dataOut.TimeBlockSeconds_for_dp_power=dataOut.LastAVGDate
5265 #print("Date: ",dataOut.TimeBlockDate_for_dp_power)
1013 5266
1014 if self.isThisProfileInList(dataOut.profileIndex, profileList):
5267 #dataOut.TimeBlockSeconds_for_dp_power=mktime(strptime(dataOut.TimeBlockDate_for_dp_power))
5268 dataOut.TimeBlockSeconds_for_dp_power=dataOut.utctime#dataOut.TimeBlockSeconds-18000
5269 #dataOut.TimeBlockSeconds_for_dp_power=dataOut.LastAVGDate
5270 #print("Seconds: ",dataOut.TimeBlockSeconds_for_dp_power)
5271 dataOut.bd_time=gmtime(dataOut.TimeBlockSeconds_for_dp_power)
5272 #print(dataOut.bd_time)
5273 #exit()
5274 dataOut.year=dataOut.bd_time.tm_year+(dataOut.bd_time.tm_yday-1)/364.0
5275 dataOut.ut_Faraday=dataOut.bd_time.tm_hour+dataOut.bd_time.tm_min/60.0+dataOut.bd_time.tm_sec/3600.0
5276 #print("date: ", dataOut.TimeBlockDate)
1015 5277
1016 self.nProfiles = len(profileList)
1017 dataOut.nProfiles = self.nProfiles
1018 dataOut.profileIndex = self.profileIndex
1019 dataOut.flagNoData = False
1020 5278
1021 self.incProfileIndex()
1022 return dataOut
5279 self.aux=0
1023 5280
1024 if profileRangeList != None:
5281 #print("after")
1025 5282
1026 minIndex = profileRangeList[0]
1027 maxIndex = profileRangeList[1]
5283 self.integration_noise(dataOut)
1028 5284
1029 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
5285 if self.counter==0:
1030 5286
1031 self.nProfiles = maxIndex - minIndex + 1
1032 dataOut.nProfiles = self.nProfiles
1033 dataOut.profileIndex = self.profileIndex
1034 dataOut.flagNoData = False
5287 self.init_time=dataOut.utctime
1035 5288
1036 self.incProfileIndex()
1037 return dataOut
5289 if self.counter < dataOut.nint:
5290 #print("HERE")
1038 5291
1039 if rangeList != None:
1040 5292
1041 nProfiles = 0
1042 5293
1043 for thisRange in rangeList:
1044 minIndex = thisRange[0]
1045 maxIndex = thisRange[1]
5294 self.counter+=1
1046 5295
1047 nProfiles += maxIndex - minIndex + 1
5296 if self.counter==dataOut.nint-1:
5297 self.aux=1
5298 #dataOut.TimeBlockDate_for_dp_power=dataOut.TimeBlockDate
5299 if self.counter==dataOut.nint:
1048 5300
1049 for thisRange in rangeList:
5301 dataOut.flagNoData=False
5302 dataOut.utctime=self.init_time
5303 self.counter=0
1050 5304
1051 minIndex = thisRange[0]
1052 maxIndex = thisRange[1]
5305 def run(self,dataOut,nint=None):
1053 5306
1054 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
5307 dataOut.flagNoData=True
1055 5308
1056 self.nProfiles = nProfiles
1057 dataOut.nProfiles = self.nProfiles
1058 dataOut.profileIndex = self.profileIndex
1059 dataOut.flagNoData = False
5309 #print("flag_inside",dataOut.flagNoData)
5310 dataOut.nint=nint
5311 dataOut.paramInterval=0#int(dataOut.nint*dataOut.header[7][0]*2 )
5312 dataOut.lat=-11.95
5313 dataOut.lon=-76.87
1060 5314
1061 self.incProfileIndex()
5315 self.integration_for_long_pulse(dataOut)
1062 5316
1063 break
1064 5317
1065 return dataOut
5318 if self.counter==dataOut.nint:
1066 5319
5320 dataOut.tnoise[0]*=0.995
5321 dataOut.tnoise[1]*=0.995
5322 dataOut.pan=dataOut.tnoise[0]/float(dataOut.NSCAN*dataOut.nint*dataOut.NAVG)
5323 dataOut.pbn=dataOut.tnoise[1]/float(dataOut.NSCAN*dataOut.nint*dataOut.NAVG)
1067 5324
1068 if beam != None: #beam is only for AMISR data
1069 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
1070 dataOut.flagNoData = False
1071 dataOut.profileIndex = self.profileIndex
5325 #self.integration_for_double_pulse(dataOut)
5326 print("HERE2")
1072 5327
1073 self.incProfileIndex()
1074 5328
1075 return dataOut
1076 5329
1077 raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter")
5330 return dataOut
1078 5331
1079 5332
1080 class Reshaper(Operation):
5333 class SumFlipsHP(SumFlips):
5334 """Operation to sum the flip and unflip part of certain cross products of the Double Pulse.
5335
5336 Parameters:
5337 -----------
5338 None
5339
5340 Example
5341 --------
5342
5343 op = proc_unit.addOperation(name='SumFlipsHP', optype='other')
5344
5345 """
1081 5346
1082 5347 def __init__(self, **kwargs):
1083 5348
1084 5349 Operation.__init__(self, **kwargs)
1085 5350
1086 self.__buffer = None
1087 self.__nitems = 0
5351 def rint2HP(self,dataOut):
1088 5352
1089 def __appendProfile(self, dataOut, nTxs):
5353 dataOut.rnint2=numpy.zeros(dataOut.DPL,'float32')
1090 5354
1091 if self.__buffer is None:
1092 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
1093 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
5355 for l in range(dataOut.DPL):
5356 if(l==0 or (l>=3 and l <=6)):
5357 dataOut.rnint2[l]=0.5/float(dataOut.nint*dataOut.NAVG*16.0)
5358 else:
5359 dataOut.rnint2[l]=0.5/float(dataOut.nint*dataOut.NAVG*8.0)
1094 5360
1095 ini = dataOut.nHeights * self.__nitems
1096 end = ini + dataOut.nHeights
5361 def run(self,dataOut):
1097 5362
1098 self.__buffer[:, ini:end] = dataOut.data
5363 self.rint2HP(dataOut)
5364 self.SumLags(dataOut)
1099 5365
1100 self.__nitems += 1
5366 return dataOut
1101 5367
1102 return int(self.__nitems*nTxs)
1103 5368
1104 def __getBuffer(self):
5369 from schainpy.model.proc import full_profile_profile
5370 from scipy.optimize import nnls
5371 class LongPulseAnalysis(Operation):
5372 """Operation to estimate ACFs, temperatures, total electron density and Hydrogen/Helium fractions from the Long Pulse data.
1105 5373
1106 if self.__nitems == int(1./self.__nTxs):
5374 Parameters:
5375 -----------
5376 NACF : int
5377 .*
1107 5378
1108 self.__nitems = 0
5379 Example
5380 --------
1109 5381
1110 return self.__buffer.copy()
5382 op = proc_unit.addOperation(name='LongPulseAnalysis', optype='other')
5383 op.addParameter(name='NACF', value='16', format='int')
1111 5384
1112 return None
5385 """
1113 5386
1114 def __checkInputs(self, dataOut, shape, nTxs):
5387 def __init__(self, **kwargs):
1115 5388
1116 if shape is None and nTxs is None:
1117 raise ValueError("Reshaper: shape of factor should be defined")
5389 Operation.__init__(self, **kwargs)
5390 self.aux=1
5391
5392 def run(self,dataOut,NACF):
5393
5394 dataOut.NACF=NACF
5395 dataOut.heightList=dataOut.DH*(numpy.arange(dataOut.NACF))
5396 anoise0=dataOut.tnoise[0]
5397 anoise1=anoise0*0.0 #seems to be noise in 1st lag 0.015 before '14
5398
5399 if self.aux:
5400 #dataOut.cut=31#26#height=31*15=465
5401 self.cal=numpy.zeros((dataOut.NLAG),'float32')
5402 self.drift=numpy.zeros((200),'float32')
5403 self.rdrift=numpy.zeros((200),'float32')
5404 self.ddrift=numpy.zeros((200),'float32')
5405 self.sigma=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5406 self.powera=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5407 self.powerb=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5408 self.perror=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5409 dataOut.ene=numpy.zeros((dataOut.NRANGE),'float32')
5410 self.dpulse=numpy.zeros((dataOut.NACF),'float32')
5411 self.lpulse=numpy.zeros((dataOut.NACF),'float32')
5412 dataOut.lags_LP=numpy.zeros((dataOut.IBITS),order='F',dtype='float32')
5413 self.lagp=numpy.zeros((dataOut.NACF),'float32')
5414 self.u=numpy.zeros((2*dataOut.NACF,2*dataOut.NACF),'float32')
5415 dataOut.ne=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5416 dataOut.te=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5417 dataOut.ete=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5418 dataOut.ti=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5419 dataOut.eti=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5420 dataOut.ph=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5421 dataOut.eph=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5422 dataOut.phe=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5423 dataOut.ephe=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5424 dataOut.errors=numpy.zeros((dataOut.IBITS,max(dataOut.NRANGE,dataOut.NSHTS)),order='F',dtype='float32')
5425 dataOut.fit_array_real=numpy.zeros((max(dataOut.NRANGE,dataOut.NSHTS),dataOut.NLAG),order='F',dtype='float32')
5426 dataOut.status=numpy.zeros(1,'float32')
5427 dataOut.tx=240.0 #debería provenir del header #hybrid
5428
5429 for i in range(dataOut.IBITS):
5430 dataOut.lags_LP[i]=float(i)*(dataOut.tx/150.0)/float(dataOut.IBITS) # (float)i*(header.tx/150.0)/(float)IBITS;
5431
5432 self.aux=0
5433
5434 dataOut.cut=30
5435 for i in range(30,15,-1):
5436 if numpy.nanmax(dataOut.acfs_error_to_plot[i,:])>=10 or dataOut.info2[i]==0:
5437 dataOut.cut=i-1
5438 #print(dataOut.cut)
5439 #print(dataOut.info2[:])
5440 #print(dataOut.te2[:])
5441 #print(dataOut.ti2[:])
5442 for i in range(dataOut.NLAG):
5443 self.cal[i]=sum(dataOut.output_LP_integrated[i,:,3].real)
5444
5445
5446 self.cal/=float(dataOut.NRANGE)
5447
5448 for j in range(dataOut.NACF+2*dataOut.IBITS+2):
5449
5450 dataOut.output_LP_integrated.real[0,j,0]-=anoise0 #lag0 ch0
5451 dataOut.output_LP_integrated.real[1,j,0]-=anoise1 #lag1 ch0
5452
5453 for i in range(1,dataOut.NLAG): #remove cal data from certain lags
5454 dataOut.output_LP_integrated.real[i,j,0]-=self.cal[i]
5455 k=max(j,26) #constant power below range 26
5456 self.powera[j]=dataOut.output_LP_integrated.real[0,k,0]
5457
5458 ## examine drifts here - based on 60 'indep.' estimates
5459
5460 nis=dataOut.NSCAN*dataOut.NAVG*dataOut.nint*10
5461 alpha=beta=delta=0.0
5462 nest=0
5463 gamma=3.0/(2.0*numpy.pi*dataOut.lags_LP[1]*1.0e-3)
5464 beta=gamma*(math.atan2(dataOut.output_LP_integrated.imag[14,0,2],dataOut.output_LP_integrated.real[14,0,2])-math.atan2(dataOut.output_LP_integrated.imag[1,0,2],dataOut.output_LP_integrated.real[1,0,2]))/13.0
5465 for i in range(1,3):
5466 gamma=3.0/(2.0*numpy.pi*dataOut.lags_LP[i]*1.0e-3)
5467 for j in range(34,44):
5468 rho2=numpy.abs(dataOut.output_LP_integrated[i,j,0])/numpy.abs(dataOut.output_LP_integrated[0,j,0])
5469 dataOut.dphi2=(1.0/rho2-1.0)/(float(2*nis))
5470 dataOut.dphi2*=gamma**2
5471 pest=gamma*math.atan(dataOut.output_LP_integrated.imag[i,j,0]/dataOut.output_LP_integrated.real[i,j,0])
5472
5473 self.drift[nest]=pest
5474 self.ddrift[nest]=dataOut.dphi2
5475 self.rdrift[nest]=float(nest)
5476 nest+=1
5477
5478 sorted(self.drift[:nest])
5479
5480 for j in range(int(nest/4),int(3*nest/4)):
5481 #i=int(self.rdrift[j])
5482 alpha+=self.drift[j]/self.ddrift[j]
5483 delta+=1.0/self.ddrift[j]
5484
5485 alpha/=delta
5486 delta=1./numpy.sqrt(delta)
5487 vdrift=alpha-beta
5488 dvdrift=delta
5489
5490 #need to develop estimate of complete density profile using all
5491 #available data
5492
5493 #estimate sample variances for long-pulse power profile
5494
5495 nis=dataOut.NSCAN*dataOut.NAVG*dataOut.nint
5496
5497 self.sigma[:dataOut.NACF+2*dataOut.IBITS+2]=((anoise0+self.powera[:dataOut.NACF+2*dataOut.IBITS+2])**2)/float(nis)
5498
5499 ioff=1
5500
5501 #deconvolve rectangular pulse shape from profile ==> powerb, perror
5502
5503
5504 ############# START nnlswrap#############
5505
5506 if dataOut.ut_Faraday>14.0:
5507 alpha_nnlswrap=20.0
5508 else:
5509 alpha_nnlswrap=30.0
1118 5510
1119 if nTxs:
1120 if nTxs < 0:
1121 raise ValueError("nTxs should be greater than 0")
5511 range1_nnls=dataOut.NACF
5512 range2_nnls=dataOut.NACF+dataOut.IBITS-1
1122 5513
1123 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
1124 raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs)))
5514 g_nnlswrap=numpy.zeros((range1_nnls,range2_nnls),'float32')
5515 a_nnlswrap=numpy.zeros((range2_nnls,range2_nnls),'float64')
1125 5516
1126 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
5517 for i in range(range1_nnls):
5518 for j in range(range2_nnls):
5519 if j>=i and j<i+dataOut.IBITS:
5520 g_nnlswrap[i,j]=1.0
5521 else:
5522 g_nnlswrap[i,j]=0.0
1127 5523
1128 return shape, nTxs
5524 a_nnlswrap[:]=numpy.matmul(numpy.transpose(g_nnlswrap),g_nnlswrap)
1129 5525
1130 if len(shape) != 2 and len(shape) != 3:
1131 raise ValueError("shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights))
5526 numpy.fill_diagonal(a_nnlswrap,a_nnlswrap.diagonal()+alpha_nnlswrap**2)
1132 5527
1133 if len(shape) == 2:
1134 shape_tuple = [dataOut.nChannels]
1135 shape_tuple.extend(shape)
1136 else:
1137 shape_tuple = list(shape)
5528 #ERROR ANALYSIS#
1138 5529
1139 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
5530 self.perror[:range2_nnls]=0.0
5531 self.perror[:range2_nnls]=numpy.matmul(1./(self.sigma[dataOut.IBITS+ioff:range1_nnls+dataOut.IBITS+ioff]),g_nnlswrap**2)
5532 self.perror[:range1_nnls]+=(alpha_nnlswrap**2)/(self.sigma[dataOut.IBITS+ioff:range1_nnls+dataOut.IBITS+ioff])
5533 self.perror[:range2_nnls]=1.00/self.perror[:range2_nnls]
1140 5534
1141 return shape_tuple, nTxs
5535 b_nnlswrap=numpy.zeros(range2_nnls,'float64')
5536 b_nnlswrap[:]=numpy.matmul(self.powera[dataOut.IBITS+ioff:range1_nnls+dataOut.IBITS+ioff],g_nnlswrap)
1142 5537
1143 def run(self, dataOut, shape=None, nTxs=None):
5538 x_nnlswrap=numpy.zeros(range2_nnls,'float64')
5539 x_nnlswrap[:]=nnls(a_nnlswrap,b_nnlswrap)[0]
1144 5540
1145 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
5541 self.powerb[:range2_nnls]=x_nnlswrap
1146 5542
1147 dataOut.flagNoData = True
1148 profileIndex = None
5543 #############END nnlswrap#############
1149 5544
1150 if dataOut.flagDataAsBlock:
5545 #estimate relative error for deconvolved profile (scaling irrelevant)
1151 5546
1152 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
1153 dataOut.flagNoData = False
5547 dataOut.ene[0:dataOut.NACF]=numpy.sqrt(self.perror[0:dataOut.NACF])/self.powerb[0:dataOut.NACF]
1154 5548
1155 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
5549 aux=0
1156 5550
1157 else:
5551 for i in range(dataOut.IBITS,dataOut.NACF):
5552 self.dpulse[i]=self.lpulse[i]=0.0
5553 for j in range(dataOut.IBITS):
5554 k=int(i-j)
5555 if k<36-aux and k>16:
5556 self.dpulse[i]+=dataOut.ph2[k]/dataOut.h2[k]
5557 elif k>=36-aux:
5558 self.lpulse[i]+=self.powerb[k]
5559 self.lagp[i]=self.powera[i]
1158 5560
1159 if self.__nTxs < 1:
5561 #find scale factor that best merges profiles
1160 5562
1161 self.__appendProfile(dataOut, self.__nTxs)
1162 new_data = self.__getBuffer()
5563 qi=sum(self.dpulse[32:dataOut.NACF]**2/(self.lagp[32:dataOut.NACF]+anoise0)**2)
5564 ri=sum((self.dpulse[32:dataOut.NACF]*self.lpulse[32:dataOut.NACF])/(self.lagp[32:dataOut.NACF]+anoise0)**2)
5565 si=sum((self.dpulse[32:dataOut.NACF]*self.lagp[32:dataOut.NACF])/(self.lagp[32:dataOut.NACF]+anoise0)**2)
5566 ui=sum(self.lpulse[32:dataOut.NACF]**2/(self.lagp[32:dataOut.NACF]+anoise0)**2)
5567 vi=sum((self.lpulse[32:dataOut.NACF]*self.lagp[32:dataOut.NACF])/(self.lagp[32:dataOut.NACF]+anoise0)**2)
1163 5568
1164 if new_data is not None:
1165 dataOut.data = new_data
1166 dataOut.flagNoData = False
5569 alpha=(si*ui-vi*ri)/(qi*ui-ri*ri)
5570 beta=(qi*vi-ri*si)/(qi*ui-ri*ri)
1167 5571
1168 profileIndex = dataOut.profileIndex*nTxs
5572 #form density profile estimate, merging rescaled power profiles
1169 5573
1170 else:
1171 raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)")
5574 self.powerb[16:36-aux]=alpha*dataOut.ph2[16:36-aux]/dataOut.h2[16:36-aux]
5575 self.powerb[36-aux:dataOut.NACF]*=beta
1172 5576
1173 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
5577 #form Ne estimate, fill in error estimate at low altitudes
1174 5578
1175 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
5579 dataOut.ene[0:36-aux]=dataOut.sdp2[0:36-aux]/dataOut.ph2[0:36-aux]
5580 dataOut.ne[:dataOut.NACF]=self.powerb[:dataOut.NACF]*dataOut.h2[:dataOut.NACF]/alpha
1176 5581
1177 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
5582 #now do error propagation: store zero lag error covariance in u
1178 5583
1179 dataOut.profileIndex = profileIndex
5584 nis=dataOut.NSCAN*dataOut.NAVG*dataOut.nint/1 # DLH serious debris removal
1180 5585
1181 dataOut.ippSeconds /= self.__nTxs
5586 for i in range(dataOut.NACF):
5587 for j in range(i,dataOut.NACF):
5588 if j-i>=dataOut.IBITS:
5589 self.u[i,j]=0.0
5590 else:
5591 self.u[i,j]=dataOut.output_LP_integrated.real[j-i,i,0]**2/float(nis)
5592 self.u[i,j]*=(anoise0+dataOut.output_LP_integrated.real[0,i,0])/dataOut.output_LP_integrated.real[0,i,0]
5593 self.u[i,j]*=(anoise0+dataOut.output_LP_integrated.real[0,j,0])/dataOut.output_LP_integrated.real[0,j,0]
5594
5595 self.u[j,i]=self.u[i,j]
5596
5597 #now error analyis for lag product matrix (diag), place in acf_err
5598
5599 for i in range(dataOut.NACF):
5600 for j in range(dataOut.IBITS):
5601 if j==0:
5602 dataOut.errors[0,i]=numpy.sqrt(self.u[i,i])
5603 else:
5604 dataOut.errors[j,i]=numpy.sqrt(((dataOut.output_LP_integrated.real[0,i,0]+anoise0)*(dataOut.output_LP_integrated.real[0,i+j,0]+anoise0)+dataOut.output_LP_integrated.real[j,i,0]**2)/float(2*nis))
5605
5606 #with suppress_stdout_stderr():
5607 #full_profile_profile.profile(numpy.transpose(dataOut.output_LP_integrated,(2,1,0)),numpy.transpose(dataOut.errors),self.powerb,dataOut.ne,dataOut.lags_LP,dataOut.thb,dataOut.bfm,dataOut.te,dataOut.ete,dataOut.ti,dataOut.eti,dataOut.ph,dataOut.eph,dataOut.phe,dataOut.ephe,dataOut.range1,dataOut.ut,dataOut.NACF,dataOut.fit_array_real,dataOut.status,dataOut.NRANGE,dataOut.IBITS)
5608
5609 if dataOut.status>=3.5:
5610 dataOut.te[:]=numpy.nan
5611 dataOut.ete[:]=numpy.nan
5612 dataOut.ti[:]=numpy.nan
5613 dataOut.eti[:]=numpy.nan
5614 dataOut.ph[:]=numpy.nan
5615 dataOut.eph[:]=numpy.nan
5616 dataOut.phe[:]=numpy.nan
5617 dataOut.ephe[:]=numpy.nan
1182 5618
1183 5619 return dataOut
1184 5620
1185 class SplitProfiles(Operation):
5621
5622 class LongPulseAnalysisLP(Operation):
5623 """Operation to estimate ACFs, temperatures, total electron density and Hydrogen/Helium fractions from the Long Pulse data.
5624
5625 Parameters:
5626 -----------
5627 NACF : int
5628 .*
5629
5630 Example
5631 --------
5632
5633 op = proc_unit.addOperation(name='LongPulseAnalysis', optype='other')
5634 op.addParameter(name='NACF', value='16', format='int')
5635
5636 """
1186 5637
1187 5638 def __init__(self, **kwargs):
1188 5639
1189 5640 Operation.__init__(self, **kwargs)
5641 self.aux=1
5642
5643 def run(self,dataOut,NACF=None):
5644
5645
5646 dataOut.IBITS = 64
5647 dataOut.NACF = dataOut.nHeights# - (2*dataOut.IBITS+5)
5648 #print(dataOut.heightList[int(dataOut.NACF)])
5649 #exit(1)
5650
5651 #dataOut.heightList=dataOut.DH*(numpy.arange(dataOut.NACF))
5652 anoise0=dataOut.tnoise[0]
5653 anoise1=anoise0*0.0 #seems to be noise in 1st lag 0.015 before '14
5654
5655 if self.aux:
5656 #dataOut.cut=31#26#height=31*15=465
5657 self.cal=numpy.zeros((dataOut.NLAG),'float32')
5658 self.drift=numpy.zeros((200),'float32')
5659 self.rdrift=numpy.zeros((200),'float32')
5660 self.ddrift=numpy.zeros((200),'float32')
5661 self.sigma=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5662 self.powera=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5663 self.powerb=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5664 self.perror=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5665 dataOut.ene=numpy.zeros((dataOut.NRANGE),'float32')
5666 self.dpulse=numpy.zeros((dataOut.NACF),'float32')
5667 self.lpulse=numpy.zeros((dataOut.NACF),'float32')
5668 dataOut.lags_LP=numpy.zeros((dataOut.IBITS),order='F',dtype='float32')
5669 self.lagp=numpy.zeros((dataOut.NACF),'float32')
5670 self.u=numpy.zeros((2*dataOut.NACF,2*dataOut.NACF),'float32')
5671 dataOut.ne=numpy.zeros((dataOut.NRANGE),order='F',dtype='float32')
5672 dataOut.te=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5673 dataOut.ete=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5674 dataOut.ti=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5675 dataOut.eti=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5676 dataOut.ph=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5677 dataOut.eph=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5678 dataOut.phe=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5679 dataOut.ephe=numpy.zeros((dataOut.NACF),order='F',dtype='float32')
5680 dataOut.errors=numpy.zeros((dataOut.IBITS,max(dataOut.NRANGE,1)),order='F',dtype='float32')
5681 dataOut.fit_array_real=numpy.zeros((max(dataOut.NRANGE,1),dataOut.NLAG),order='F',dtype='float32')
5682 dataOut.status=numpy.zeros(1,'float32')
5683 dataOut.tx=480.0 #debería provenir del header #HAE
5684
5685 dataOut.h2=numpy.zeros(dataOut.MAXNRANGENDT,'float32')
5686 dataOut.range1=numpy.zeros(dataOut.MAXNRANGENDT,order='F',dtype='float32')
5687
5688
5689
5690 for i in range(dataOut.IBITS):
5691 dataOut.lags_LP[i]=float(i)*(dataOut.tx/150.0)/float(dataOut.IBITS) # (float)i*(header.tx/150.0)/(float)IBITS;
5692
5693 self.aux=0
5694
5695
5696
5697 for i in range(dataOut.MAXNRANGENDT):
5698 dataOut.range1[i]=dataOut.H0 + i*dataOut.DH
5699 dataOut.h2[i]=dataOut.range1[i]**2
5700
5701 dataOut.cut=30
5702 #for i in range(30,15,-1):
5703 # if numpy.nanmax(dataOut.acfs_error_to_plot[i,:])>=10 or dataOut.info2[i]==0:
5704 # dataOut.cut=i-1
5705 #print(dataOut.cut)
5706 #print(dataOut.info2[:])
5707 #print(dataOut.te2[:])
5708 #print(dataOut.ti2[:])
5709 #for i in range(dataOut.NLAG):
5710 # self.cal[i]=sum(dataOut.output_LP_integrated[i,:,3].real)
5711
5712
5713 #self.cal/=float(dataOut.NRANGE)
5714
5715 for j in range(dataOut.NACF):#+2*dataOut.IBITS+2):
5716
5717 self.powera[j]=dataOut.output_LP_integrated.real[0,j,0]
5718
5719
5720 print(dataOut.heightList[:dataOut.NACF])
5721 import matplotlib.pyplot as plt
5722 fig, axes = plt.subplots(figsize=(14, 10))
5723 axes.plot(self.powera[:dataOut.NACF]*dataOut.h2[:dataOut.NACF],dataOut.heightList[:dataOut.NACF])
5724 axes.set_xscale("log", nonposx='clip')
5725 #axes.set_xlim(1e18,2e19)
5726 axes.set_ylim(180,470)
5727 import time
5728
5729 plt.title(time.ctime(dataOut.utctime))
5730 plt.show()
5731 time.sleep(50)
5732 exit(1)
5733 '''
5734 for j in range(dataOut.NACF+2*dataOut.IBITS+2):
1190 5735
1191 def run(self, dataOut, n):
5736 dataOut.output_LP_integrated.real[0,j,0]-=anoise0 #lag0 ch0
5737 dataOut.output_LP_integrated.real[1,j,0]-=anoise1 #lag1 ch0
1192 5738
1193 dataOut.flagNoData = True
1194 profileIndex = None
5739 #for i in range(1,dataOut.NLAG): #remove cal data from certain lags
5740 # dataOut.output_LP_integrated.real[i,j,0]-=self.cal[i]
5741 k=max(j,26) #constant power below range 26
5742 self.powera[j]=dataOut.output_LP_integrated.real[0,k,0]
1195 5743
1196 if dataOut.flagDataAsBlock:
5744 ## examine drifts here - based on 60 'indep.' estimates
1197 5745
1198 #nchannels, nprofiles, nsamples
1199 shape = dataOut.data.shape
5746 nis=dataOut.NSCAN*dataOut.NAVG*dataOut.nint*10
5747 alpha=beta=delta=0.0
5748 nest=0
5749 gamma=3.0/(2.0*numpy.pi*dataOut.lags_LP[1]*1.0e-3)
5750 beta=gamma*(math.atan2(dataOut.output_LP_integrated.imag[14,0,2],dataOut.output_LP_integrated.real[14,0,2])-math.atan2(dataOut.output_LP_integrated.imag[1,0,2],dataOut.output_LP_integrated.real[1,0,2]))/13.0
5751 for i in range(1,3):
5752 gamma=3.0/(2.0*numpy.pi*dataOut.lags_LP[i]*1.0e-3)
5753 for j in range(34,44):
5754 rho2=numpy.abs(dataOut.output_LP_integrated[i,j,0])/numpy.abs(dataOut.output_LP_integrated[0,j,0])
5755 dataOut.dphi2=(1.0/rho2-1.0)/(float(2*nis))
5756 dataOut.dphi2*=gamma**2
5757 pest=gamma*math.atan(dataOut.output_LP_integrated.imag[i,j,0]/dataOut.output_LP_integrated.real[i,j,0])
1200 5758
1201 if shape[2] % n != 0:
1202 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2]))
5759 self.drift[nest]=pest
5760 self.ddrift[nest]=dataOut.dphi2
5761 self.rdrift[nest]=float(nest)
5762 nest+=1
1203 5763
1204 new_shape = shape[0], shape[1]*n, int(shape[2]/n)
5764 sorted(self.drift[:nest])
1205 5765
1206 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1207 dataOut.flagNoData = False
5766 for j in range(int(nest/4),int(3*nest/4)):
5767 #i=int(self.rdrift[j])
5768 alpha+=self.drift[j]/self.ddrift[j]
5769 delta+=1.0/self.ddrift[j]
1208 5770
1209 profileIndex = int(dataOut.nProfiles/n) - 1
5771 alpha/=delta
5772 delta=1./numpy.sqrt(delta)
5773 vdrift=alpha-beta
5774 dvdrift=delta
1210 5775
1211 else:
5776 #need to develop estimate of complete density profile using all
5777 #available data
1212 5778
1213 raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)")
5779 #estimate sample variances for long-pulse power profile
1214 5780
1215 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
5781 nis=dataOut.NSCAN*dataOut.NAVG*dataOut.nint
1216 5782
1217 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
5783 self.sigma[:dataOut.NACF+2*dataOut.IBITS+2]=((anoise0+self.powera[:dataOut.NACF+2*dataOut.IBITS+2])**2)/float(nis)
5784 '''
5785 ioff=1
1218 5786
1219 dataOut.nProfiles = int(dataOut.nProfiles*n)
5787 #deconvolve rectangular pulse shape from profile ==> powerb, perror
1220 5788
1221 dataOut.profileIndex = profileIndex
1222 5789
1223 dataOut.ippSeconds /= n
5790 ############# START nnlswrap#############
1224 5791
1225 return dataOut
5792 if dataOut.ut_Faraday>14.0:
5793 alpha_nnlswrap=20.0
5794 else:
5795 alpha_nnlswrap=30.0
1226 5796
1227 class CombineProfiles(Operation):
1228 def __init__(self, **kwargs):
5797 range1_nnls=dataOut.NACF
5798 range2_nnls=dataOut.NACF+dataOut.IBITS-1
1229 5799
1230 Operation.__init__(self, **kwargs)
5800 g_nnlswrap=numpy.zeros((range1_nnls,range2_nnls),'float32')
5801 a_nnlswrap=numpy.zeros((range2_nnls,range2_nnls),'float64')
1231 5802
1232 self.__remData = None
1233 self.__profileIndex = 0
5803 for i in range(range1_nnls):
5804 for j in range(range2_nnls):
5805 if j>=i and j<i+dataOut.IBITS:
5806 g_nnlswrap[i,j]=1.0
5807 else:
5808 g_nnlswrap[i,j]=0.0
1234 5809
1235 def run(self, dataOut, n):
5810 a_nnlswrap[:]=numpy.matmul(numpy.transpose(g_nnlswrap),g_nnlswrap)
1236 5811
1237 dataOut.flagNoData = True
1238 profileIndex = None
5812 numpy.fill_diagonal(a_nnlswrap,a_nnlswrap.diagonal()+alpha_nnlswrap**2)
1239 5813
1240 if dataOut.flagDataAsBlock:
5814 #ERROR ANALYSIS#
5815 '''
5816 self.perror[:range2_nnls]=0.0
5817 self.perror[:range2_nnls]=numpy.matmul(1./(self.sigma[dataOut.IBITS+ioff:range1_nnls+dataOut.IBITS+ioff]),g_nnlswrap**2)
5818 self.perror[:range1_nnls]+=(alpha_nnlswrap**2)/(self.sigma[dataOut.IBITS+ioff:range1_nnls+dataOut.IBITS+ioff])
5819 self.perror[:range2_nnls]=1.00/self.perror[:range2_nnls]
5820 '''
5821 b_nnlswrap=numpy.zeros(range2_nnls,'float64')
5822 b_nnlswrap[:]=numpy.matmul(self.powera[dataOut.IBITS+ioff:range1_nnls+dataOut.IBITS+ioff],g_nnlswrap)
1241 5823
1242 #nchannels, nprofiles, nsamples
1243 shape = dataOut.data.shape
1244 new_shape = shape[0], shape[1]/n, shape[2]*n
5824 x_nnlswrap=numpy.zeros(range2_nnls,'float64')
5825 x_nnlswrap[:]=nnls(a_nnlswrap,b_nnlswrap)[0]
1245 5826
1246 if shape[1] % n != 0:
1247 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1]))
5827 self.powerb[:range2_nnls]=x_nnlswrap
1248 5828
1249 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1250 dataOut.flagNoData = False
1251 5829
1252 profileIndex = int(dataOut.nProfiles*n) - 1
5830 import matplotlib.pyplot as plt
5831 fig, axes = plt.subplots(figsize=(14, 10))
5832 axes.plot(self.powerb[:dataOut.NACF]*dataOut.h2[:dataOut.NACF],dataOut.heightList[:dataOut.NACF])
5833 axes.set_xscale("log", nonposx='clip')
5834 #axes.set_xlim(1e10,8e12)
5835 axes.set_ylim(0,300)
5836 plt.show()
5837 import time
5838 time.sleep(60)
5839 exit(1)
1253 5840
1254 else:
1255 5841
1256 #nchannels, nsamples
1257 if self.__remData is None:
1258 newData = dataOut.data
1259 else:
1260 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
5842 #############END nnlswrap#############
1261 5843
1262 self.__profileIndex += 1
5844 #estimate relative error for deconvolved profile (scaling irrelevant)
1263 5845
1264 if self.__profileIndex < n:
1265 self.__remData = newData
1266 #continue
1267 return
5846 dataOut.ene[0:dataOut.NACF]=numpy.sqrt(self.perror[0:dataOut.NACF])/self.powerb[0:dataOut.NACF]
1268 5847
1269 self.__profileIndex = 0
1270 self.__remData = None
5848 aux=0
1271 5849
1272 dataOut.data = newData
1273 dataOut.flagNoData = False
5850 for i in range(dataOut.IBITS,dataOut.NACF):
5851 self.dpulse[i]=self.lpulse[i]=0.0
5852 for j in range(dataOut.IBITS):
5853 k=int(i-j)
5854 if k<36-aux and k>16:
5855 self.dpulse[i]+=dataOut.ph2[k]/dataOut.h2[k]
5856 elif k>=36-aux:
5857 self.lpulse[i]+=self.powerb[k]
5858 self.lagp[i]=self.powera[i]
1274 5859
1275 profileIndex = dataOut.profileIndex/n
5860 #find scale factor that best merges profiles
1276 5861
5862 qi=sum(self.dpulse[32:dataOut.NACF]**2/(self.lagp[32:dataOut.NACF]+anoise0)**2)
5863 ri=sum((self.dpulse[32:dataOut.NACF]*self.lpulse[32:dataOut.NACF])/(self.lagp[32:dataOut.NACF]+anoise0)**2)
5864 si=sum((self.dpulse[32:dataOut.NACF]*self.lagp[32:dataOut.NACF])/(self.lagp[32:dataOut.NACF]+anoise0)**2)
5865 ui=sum(self.lpulse[32:dataOut.NACF]**2/(self.lagp[32:dataOut.NACF]+anoise0)**2)
5866 vi=sum((self.lpulse[32:dataOut.NACF]*self.lagp[32:dataOut.NACF])/(self.lagp[32:dataOut.NACF]+anoise0)**2)
1277 5867
1278 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
5868 alpha=(si*ui-vi*ri)/(qi*ui-ri*ri)
5869 beta=(qi*vi-ri*si)/(qi*ui-ri*ri)
1279 5870
1280 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
5871 #form density profile estimate, merging rescaled power profiles
1281 5872
1282 dataOut.nProfiles = int(dataOut.nProfiles/n)
5873 self.powerb[16:36-aux]=alpha*dataOut.ph2[16:36-aux]/dataOut.h2[16:36-aux]
5874 self.powerb[36-aux:dataOut.NACF]*=beta
1283 5875
1284 dataOut.profileIndex = profileIndex
5876 #form Ne estimate, fill in error estimate at low altitudes
1285 5877
1286 dataOut.ippSeconds *= n
5878 dataOut.ene[0:36-aux]=dataOut.sdp2[0:36-aux]/dataOut.ph2[0:36-aux]
5879 dataOut.ne[:dataOut.NACF]=self.powerb[:dataOut.NACF]*dataOut.h2[:dataOut.NACF]/alpha
1287 5880
5881 #now do error propagation: store zero lag error covariance in u
5882
5883 nis=dataOut.NSCAN*dataOut.NAVG*dataOut.nint/1 # DLH serious debris removal
5884
5885 for i in range(dataOut.NACF):
5886 for j in range(i,dataOut.NACF):
5887 if j-i>=dataOut.IBITS:
5888 self.u[i,j]=0.0
5889 else:
5890 self.u[i,j]=dataOut.output_LP_integrated.real[j-i,i,0]**2/float(nis)
5891 self.u[i,j]*=(anoise0+dataOut.output_LP_integrated.real[0,i,0])/dataOut.output_LP_integrated.real[0,i,0]
5892 self.u[i,j]*=(anoise0+dataOut.output_LP_integrated.real[0,j,0])/dataOut.output_LP_integrated.real[0,j,0]
5893
5894 self.u[j,i]=self.u[i,j]
5895
5896 #now error analyis for lag product matrix (diag), place in acf_err
5897
5898 for i in range(dataOut.NACF):
5899 for j in range(dataOut.IBITS):
5900 if j==0:
5901 dataOut.errors[0,i]=numpy.sqrt(self.u[i,i])
5902 else:
5903 dataOut.errors[j,i]=numpy.sqrt(((dataOut.output_LP_integrated.real[0,i,0]+anoise0)*(dataOut.output_LP_integrated.real[0,i+j,0]+anoise0)+dataOut.output_LP_integrated.real[j,i,0]**2)/float(2*nis))
5904
5905 #with suppress_stdout_stderr():
5906 #full_profile_profile.profile(numpy.transpose(dataOut.output_LP_integrated,(2,1,0)),numpy.transpose(dataOut.errors),self.powerb,dataOut.ne,dataOut.lags_LP,dataOut.thb,dataOut.bfm,dataOut.te,dataOut.ete,dataOut.ti,dataOut.eti,dataOut.ph,dataOut.eph,dataOut.phe,dataOut.ephe,dataOut.range1,dataOut.ut,dataOut.NACF,dataOut.fit_array_real,dataOut.status,dataOut.NRANGE,dataOut.IBITS)
5907 '''
5908 if dataOut.status>=3.5:
5909 dataOut.te[:]=numpy.nan
5910 dataOut.ete[:]=numpy.nan
5911 dataOut.ti[:]=numpy.nan
5912 dataOut.eti[:]=numpy.nan
5913 dataOut.ph[:]=numpy.nan
5914 dataOut.eph[:]=numpy.nan
5915 dataOut.phe[:]=numpy.nan
5916 dataOut.ephe[:]=numpy.nan
5917 '''
1288 5918 return dataOut
1289 5919
5920
1290 5921 class PulsePairVoltage(Operation):
1291 5922 '''
1292 5923 Function PulsePair(Signal Power, Velocity)
@@ -61,4 +61,4 class PXParametersProc(ProcessingUnit):
61 61 meta[attr] = getattr(self.dataOut, attr)
62 62
63 63 meta['mode'] = mode
64 self.dataOut.meta = meta No newline at end of file
64 self.dataOut.meta = meta
@@ -1,6 +1,6
1 1 import argparse
2 2
3 from schainpy.controller import Project, multiSchain
3 from schainpy.controller import Project#, multiSchain
4 4
5 5 desc = "HF_EXAMPLE"
6 6
General Comments 0
You need to be logged in to leave comments. Login now