##// END OF EJS Templates
15/08/2017
ebocanegra -
r1001:08c8d3a7a244
parent child
Show More

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

1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
@@ -1,1220 +1,1229
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JROData.py 173 2012-11-20 15:06:21Z murco $
5 5 '''
6 6
7 7 import copy
8 8 import numpy
9 9 import datetime
10 10
11 11 from jroheaderIO import SystemHeader, RadarControllerHeader
12 12 from schainpy import cSchain
13 13
14 14
15 15 def getNumpyDtype(dataTypeCode):
16 16
17 17 if dataTypeCode == 0:
18 18 numpyDtype = numpy.dtype([('real','<i1'),('imag','<i1')])
19 19 elif dataTypeCode == 1:
20 20 numpyDtype = numpy.dtype([('real','<i2'),('imag','<i2')])
21 21 elif dataTypeCode == 2:
22 22 numpyDtype = numpy.dtype([('real','<i4'),('imag','<i4')])
23 23 elif dataTypeCode == 3:
24 24 numpyDtype = numpy.dtype([('real','<i8'),('imag','<i8')])
25 25 elif dataTypeCode == 4:
26 26 numpyDtype = numpy.dtype([('real','<f4'),('imag','<f4')])
27 27 elif dataTypeCode == 5:
28 28 numpyDtype = numpy.dtype([('real','<f8'),('imag','<f8')])
29 29 else:
30 30 raise ValueError, 'dataTypeCode was not defined'
31 31
32 32 return numpyDtype
33 33
34 34 def getDataTypeCode(numpyDtype):
35 35
36 36 if numpyDtype == numpy.dtype([('real','<i1'),('imag','<i1')]):
37 37 datatype = 0
38 38 elif numpyDtype == numpy.dtype([('real','<i2'),('imag','<i2')]):
39 39 datatype = 1
40 40 elif numpyDtype == numpy.dtype([('real','<i4'),('imag','<i4')]):
41 41 datatype = 2
42 42 elif numpyDtype == numpy.dtype([('real','<i8'),('imag','<i8')]):
43 43 datatype = 3
44 44 elif numpyDtype == numpy.dtype([('real','<f4'),('imag','<f4')]):
45 45 datatype = 4
46 46 elif numpyDtype == numpy.dtype([('real','<f8'),('imag','<f8')]):
47 47 datatype = 5
48 48 else:
49 49 datatype = None
50 50
51 51 return datatype
52 52
53 53 def hildebrand_sekhon(data, navg):
54 54 """
55 55 This method is for the objective determination of the noise level in Doppler spectra. This
56 56 implementation technique is based on the fact that the standard deviation of the spectral
57 57 densities is equal to the mean spectral density for white Gaussian noise
58 58
59 59 Inputs:
60 60 Data : heights
61 61 navg : numbers of averages
62 62
63 63 Return:
64 64 -1 : any error
65 65 anoise : noise's level
66 66 """
67 67
68 68 sortdata = numpy.sort(data, axis=None)
69 69 # lenOfData = len(sortdata)
70 70 # nums_min = lenOfData*0.2
71 71 #
72 72 # if nums_min <= 5:
73 73 # nums_min = 5
74 74 #
75 75 # sump = 0.
76 76 #
77 77 # sumq = 0.
78 78 #
79 79 # j = 0
80 80 #
81 81 # cont = 1
82 82 #
83 83 # while((cont==1)and(j<lenOfData)):
84 84 #
85 85 # sump += sortdata[j]
86 86 #
87 87 # sumq += sortdata[j]**2
88 88 #
89 89 # if j > nums_min:
90 90 # rtest = float(j)/(j-1) + 1.0/navg
91 91 # if ((sumq*j) > (rtest*sump**2)):
92 92 # j = j - 1
93 93 # sump = sump - sortdata[j]
94 94 # sumq = sumq - sortdata[j]**2
95 95 # cont = 0
96 96 #
97 97 # j += 1
98 98 #
99 99 # lnoise = sump /j
100 100 #
101 101 # return lnoise
102 102
103 103 return cSchain.hildebrand_sekhon(sortdata, navg)
104 104
105 105
106 106 class Beam:
107 107
108 108 def __init__(self):
109 109 self.codeList = []
110 110 self.azimuthList = []
111 111 self.zenithList = []
112 112
113 113 class GenericData(object):
114 114
115 115 flagNoData = True
116 116
117 117 def copy(self, inputObj=None):
118 118
119 119 if inputObj == None:
120 120 return copy.deepcopy(self)
121 121
122 122 for key in inputObj.__dict__.keys():
123 123
124 124 attribute = inputObj.__dict__[key]
125 125
126 126 #If this attribute is a tuple or list
127 127 if type(inputObj.__dict__[key]) in (tuple, list):
128 128 self.__dict__[key] = attribute[:]
129 129 continue
130 130
131 131 #If this attribute is another object or instance
132 132 if hasattr(attribute, '__dict__'):
133 133 self.__dict__[key] = attribute.copy()
134 134 continue
135 135
136 136 self.__dict__[key] = inputObj.__dict__[key]
137 137
138 138 def deepcopy(self):
139 139
140 140 return copy.deepcopy(self)
141 141
142 142 def isEmpty(self):
143 143
144 144 return self.flagNoData
145 145
146 146 class JROData(GenericData):
147 147
148 148 # m_BasicHeader = BasicHeader()
149 149 # m_ProcessingHeader = ProcessingHeader()
150 150
151 151 systemHeaderObj = SystemHeader()
152 152
153 153 radarControllerHeaderObj = RadarControllerHeader()
154 154
155 155 # data = None
156 156
157 157 type = None
158 158
159 159 datatype = None #dtype but in string
160 160
161 161 # dtype = None
162 162
163 163 # nChannels = None
164 164
165 165 # nHeights = None
166 166
167 167 nProfiles = None
168 168
169 169 heightList = None
170 170
171 171 channelList = None
172 172
173 173 flagDiscontinuousBlock = False
174 174
175 175 useLocalTime = False
176 176
177 177 utctime = None
178 178
179 179 timeZone = None
180 180
181 181 dstFlag = None
182 182
183 183 errorCount = None
184 184
185 185 blocksize = None
186 186
187 187 # nCode = None
188 188 #
189 189 # nBaud = None
190 190 #
191 191 # code = None
192 192
193 193 flagDecodeData = False #asumo q la data no esta decodificada
194 194
195 195 flagDeflipData = False #asumo q la data no esta sin flip
196 196
197 197 flagShiftFFT = False
198 198
199 199 # ippSeconds = None
200 200
201 201 # timeInterval = None
202 202
203 203 nCohInt = None
204 204
205 205 # noise = None
206 206
207 207 windowOfFilter = 1
208 208
209 209 #Speed of ligth
210 210 C = 3e8
211 211
212 212 frequency = 49.92e6
213 213
214 214 realtime = False
215 215
216 216 beacon_heiIndexList = None
217 217
218 218 last_block = None
219 219
220 220 blocknow = None
221 221
222 222 azimuth = None
223 223
224 224 zenith = None
225 225
226 226 beam = Beam()
227 227
228 228 profileIndex = None
229 229
230 230 def getNoise(self):
231 231
232 232 raise NotImplementedError
233 233
234 234 def getNChannels(self):
235 235
236 236 return len(self.channelList)
237 237
238 238 def getChannelIndexList(self):
239 239
240 240 return range(self.nChannels)
241 241
242 242 def getNHeights(self):
243 243
244 244 return len(self.heightList)
245 245
246 246 def getHeiRange(self, extrapoints=0):
247 247
248 248 heis = self.heightList
249 249 # deltah = self.heightList[1] - self.heightList[0]
250 250 #
251 251 # heis.append(self.heightList[-1])
252 252
253 253 return heis
254 254
255 255 def getDeltaH(self):
256 256
257 257 delta = self.heightList[1] - self.heightList[0]
258 258
259 259 return delta
260 260
261 261 def getltctime(self):
262 262
263 263 if self.useLocalTime:
264 264 return self.utctime - self.timeZone*60
265 265
266 266 return self.utctime
267 267
268 268 def getDatatime(self):
269 269
270 270 datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime)
271 271 return datatimeValue
272 272
273 273 def getTimeRange(self):
274 274
275 275 datatime = []
276 276
277 277 datatime.append(self.ltctime)
278 278 datatime.append(self.ltctime + self.timeInterval+1)
279 279
280 280 datatime = numpy.array(datatime)
281 281
282 282 return datatime
283 283
284 284 def getFmaxTimeResponse(self):
285 285
286 286 period = (10**-6)*self.getDeltaH()/(0.15)
287 287
288 288 PRF = 1./(period * self.nCohInt)
289 289
290 290 fmax = PRF
291 291
292 292 return fmax
293 293
294 294 def getFmax(self):
295 295
296 296 PRF = 1./(self.ippSeconds * self.nCohInt)
297 297
298 298 fmax = PRF
299 299
300 300 return fmax
301 301
302 302 def getVmax(self):
303 303
304 304 _lambda = self.C/self.frequency
305 305
306 306 vmax = self.getFmax() * _lambda/2
307 307
308 308 return vmax
309 309
310 310 def get_ippSeconds(self):
311 311 '''
312 312 '''
313 313 return self.radarControllerHeaderObj.ippSeconds
314 314
315 315 def set_ippSeconds(self, ippSeconds):
316 316 '''
317 317 '''
318 318
319 319 self.radarControllerHeaderObj.ippSeconds = ippSeconds
320 320
321 321 return
322 322
323 323 def get_dtype(self):
324 324 '''
325 325 '''
326 326 return getNumpyDtype(self.datatype)
327 327
328 328 def set_dtype(self, numpyDtype):
329 329 '''
330 330 '''
331 331
332 332 self.datatype = getDataTypeCode(numpyDtype)
333 333
334 334 def get_code(self):
335 335 '''
336 336 '''
337 337 return self.radarControllerHeaderObj.code
338 338
339 339 def set_code(self, code):
340 340 '''
341 341 '''
342 342 self.radarControllerHeaderObj.code = code
343 343
344 344 return
345 345
346 346 def get_ncode(self):
347 347 '''
348 348 '''
349 349 return self.radarControllerHeaderObj.nCode
350 350
351 351 def set_ncode(self, nCode):
352 352 '''
353 353 '''
354 354 self.radarControllerHeaderObj.nCode = nCode
355 355
356 356 return
357 357
358 358 def get_nbaud(self):
359 359 '''
360 360 '''
361 361 return self.radarControllerHeaderObj.nBaud
362 362
363 363 def set_nbaud(self, nBaud):
364 364 '''
365 365 '''
366 366 self.radarControllerHeaderObj.nBaud = nBaud
367 367
368 368 return
369 369
370 370 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
371 371 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
372 372 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
373 373 #noise = property(getNoise, "I'm the 'nHeights' property.")
374 374 datatime = property(getDatatime, "I'm the 'datatime' property")
375 375 ltctime = property(getltctime, "I'm the 'ltctime' property")
376 376 ippSeconds = property(get_ippSeconds, set_ippSeconds)
377 377 dtype = property(get_dtype, set_dtype)
378 378 # timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
379 379 code = property(get_code, set_code)
380 380 nCode = property(get_ncode, set_ncode)
381 381 nBaud = property(get_nbaud, set_nbaud)
382 382
383 383 class Voltage(JROData):
384 384
385 385 #data es un numpy array de 2 dmensiones (canales, alturas)
386 386 data = None
387 387
388 388 def __init__(self):
389 389 '''
390 390 Constructor
391 391 '''
392 392
393 393 self.useLocalTime = True
394 394
395 395 self.radarControllerHeaderObj = RadarControllerHeader()
396 396
397 397 self.systemHeaderObj = SystemHeader()
398 398
399 399 self.type = "Voltage"
400 400
401 401 self.data = None
402 402
403 403 # self.dtype = None
404 404
405 405 # self.nChannels = 0
406 406
407 407 # self.nHeights = 0
408 408
409 409 self.nProfiles = None
410 410
411 411 self.heightList = None
412 412
413 413 self.channelList = None
414 414
415 415 # self.channelIndexList = None
416 416
417 417 self.flagNoData = True
418 418
419 419 self.flagDiscontinuousBlock = False
420 420
421 421 self.utctime = None
422 422
423 423 self.timeZone = None
424 424
425 425 self.dstFlag = None
426 426
427 427 self.errorCount = None
428 428
429 429 self.nCohInt = None
430 430
431 431 self.blocksize = None
432 432
433 433 self.flagDecodeData = False #asumo q la data no esta decodificada
434 434
435 435 self.flagDeflipData = False #asumo q la data no esta sin flip
436 436
437 437 self.flagShiftFFT = False
438 438
439 439 self.flagDataAsBlock = False #Asumo que la data es leida perfil a perfil
440 440
441 441 self.profileIndex = 0
442 442
443 443 def getNoisebyHildebrand(self, channel = None):
444 444 """
445 445 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
446 446
447 447 Return:
448 448 noiselevel
449 449 """
450 450
451 451 if channel != None:
452 452 data = self.data[channel]
453 453 nChannels = 1
454 454 else:
455 455 data = self.data
456 456 nChannels = self.nChannels
457 457
458 458 noise = numpy.zeros(nChannels)
459 459 power = data * numpy.conjugate(data)
460 460
461 461 for thisChannel in range(nChannels):
462 462 if nChannels == 1:
463 463 daux = power[:].real
464 464 else:
465 465 daux = power[thisChannel,:].real
466 466 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
467 467
468 468 return noise
469 469
470 470 def getNoise(self, type = 1, channel = None):
471 471
472 472 if type == 1:
473 473 noise = self.getNoisebyHildebrand(channel)
474 474
475 475 return noise
476 476
477 477 def getPower(self, channel = None):
478 478
479 479 if channel != None:
480 480 data = self.data[channel]
481 481 else:
482 482 data = self.data
483 483
484 484 power = data * numpy.conjugate(data)
485 485 powerdB = 10*numpy.log10(power.real)
486 486 powerdB = numpy.squeeze(powerdB)
487 487
488 488 return powerdB
489 489
490 490 def getTimeInterval(self):
491 491
492 492 timeInterval = self.ippSeconds * self.nCohInt
493 493
494 494 return timeInterval
495 495
496 496 noise = property(getNoise, "I'm the 'nHeights' property.")
497 497 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
498 498
499 499 class Spectra(JROData):
500 500
501 501 #data spc es un numpy array de 2 dmensiones (canales, perfiles, alturas)
502 502 data_spc = None
503 503
504 504 #data cspc es un numpy array de 2 dmensiones (canales, pares, alturas)
505 505 data_cspc = None
506 506
507 507 #data dc es un numpy array de 2 dmensiones (canales, alturas)
508 508 data_dc = None
509 509
510 510 #data power
511 511 data_pwr = None
512 512
513 513 nFFTPoints = None
514 514
515 515 # nPairs = None
516 516
517 517 pairsList = None
518 518
519 519 nIncohInt = None
520 520
521 521 wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia
522 522
523 523 nCohInt = None #se requiere para determinar el valor de timeInterval
524 524
525 525 ippFactor = None
526 526
527 527 profileIndex = 0
528 528
529 529 plotting = "spectra"
530 530
531 531 def __init__(self):
532 532 '''
533 533 Constructor
534 534 '''
535 535
536 536 self.useLocalTime = True
537 537
538 538 self.radarControllerHeaderObj = RadarControllerHeader()
539 539
540 540 self.systemHeaderObj = SystemHeader()
541 541
542 542 self.type = "Spectra"
543 543
544 544 # self.data = None
545 545
546 546 # self.dtype = None
547 547
548 548 # self.nChannels = 0
549 549
550 550 # self.nHeights = 0
551 551
552 552 self.nProfiles = None
553 553
554 554 self.heightList = None
555 555
556 556 self.channelList = None
557 557
558 558 # self.channelIndexList = None
559 559
560 560 self.pairsList = None
561 561
562 562 self.flagNoData = True
563 563
564 564 self.flagDiscontinuousBlock = False
565 565
566 566 self.utctime = None
567 567
568 568 self.nCohInt = None
569 569
570 570 self.nIncohInt = None
571 571
572 572 self.blocksize = None
573 573
574 574 self.nFFTPoints = None
575 575
576 576 self.wavelength = None
577 577
578 578 self.flagDecodeData = False #asumo q la data no esta decodificada
579 579
580 580 self.flagDeflipData = False #asumo q la data no esta sin flip
581 581
582 582 self.flagShiftFFT = False
583 583
584 584 self.ippFactor = 1
585 585
586 586 #self.noise = None
587 587
588 588 self.beacon_heiIndexList = []
589 589
590 590 self.noise_estimation = None
591 591
592 592
593 593 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
594 594 """
595 595 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
596 596
597 597 Return:
598 598 noiselevel
599 599 """
600 600
601 601 noise = numpy.zeros(self.nChannels)
602 602
603 603 for channel in range(self.nChannels):
604 604 daux = self.data_spc[channel,xmin_index:xmax_index,ymin_index:ymax_index]
605 605 noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
606 606
607 607 return noise
608 608
609 609 def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
610 610
611 611 if self.noise_estimation is not None:
612 612 return self.noise_estimation #this was estimated by getNoise Operation defined in jroproc_spectra.py
613 613 else:
614 614 noise = self.getNoisebyHildebrand(xmin_index, xmax_index, ymin_index, ymax_index)
615 615 return noise
616 616
617 617 def getFreqRangeTimeResponse(self, extrapoints=0):
618 618
619 619 deltafreq = self.getFmaxTimeResponse() / (self.nFFTPoints*self.ippFactor)
620 620 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
621 621
622 622 return freqrange
623 623
624 624 def getAcfRange(self, extrapoints=0):
625 625
626 626 deltafreq = 10./(self.getFmax() / (self.nFFTPoints*self.ippFactor))
627 627 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
628 628
629 629 return freqrange
630 630
631 631 def getFreqRange(self, extrapoints=0):
632 632
633 633 deltafreq = self.getFmax() / (self.nFFTPoints*self.ippFactor)
634 634 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
635 635
636 636 return freqrange
637 637
638 638 def getVelRange(self, extrapoints=0):
639 639
640 640 deltav = self.getVmax() / (self.nFFTPoints*self.ippFactor)
641 641 velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) #- deltav/2
642 642
643 643 return velrange
644 644
645 645 def getNPairs(self):
646 646
647 647 return len(self.pairsList)
648 648
649 649 def getPairsIndexList(self):
650 650
651 651 return range(self.nPairs)
652 652
653 653 def getNormFactor(self):
654 654
655 655 pwcode = 1
656 656
657 657 if self.flagDecodeData:
658 658 pwcode = numpy.sum(self.code[0]**2)
659 659 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
660 660 normFactor = self.nProfiles*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
661 661
662 662 return normFactor
663 663
664 664 def getFlagCspc(self):
665 665
666 666 if self.data_cspc is None:
667 667 return True
668 668
669 669 return False
670 670
671 671 def getFlagDc(self):
672 672
673 673 if self.data_dc is None:
674 674 return True
675 675
676 676 return False
677 677
678 678 def getTimeInterval(self):
679 679
680 680 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles
681 681
682 682 return timeInterval
683 683
684 684 def getPower(self):
685 685
686 686 factor = self.normFactor
687 687 z = self.data_spc/factor
688 688 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
689 689 avg = numpy.average(z, axis=1)
690 690
691 691 return 10*numpy.log10(avg)
692 692
693 693 def getCoherence(self, pairsList=None, phase=False):
694 694
695 695 z = []
696 696 if pairsList is None:
697 697 pairsIndexList = self.pairsIndexList
698 698 else:
699 699 pairsIndexList = []
700 700 for pair in pairsList:
701 701 if pair not in self.pairsList:
702 702 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
703 703 pairsIndexList.append(self.pairsList.index(pair))
704 704 for i in range(len(pairsIndexList)):
705 705 pair = self.pairsList[pairsIndexList[i]]
706 706 ccf = numpy.average(self.data_cspc[pairsIndexList[i], :, :], axis=0)
707 707 powa = numpy.average(self.data_spc[pair[0], :, :], axis=0)
708 708 powb = numpy.average(self.data_spc[pair[1], :, :], axis=0)
709 709 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
710 710 if phase:
711 711 data = numpy.arctan2(avgcoherenceComplex.imag,
712 712 avgcoherenceComplex.real)*180/numpy.pi
713 713 else:
714 714 data = numpy.abs(avgcoherenceComplex)
715 715
716 716 z.append(data)
717 717
718 718 return numpy.array(z)
719 719
720 720 def setValue(self, value):
721 721
722 722 print "This property should not be initialized"
723 723
724 724 return
725 725
726 726 nPairs = property(getNPairs, setValue, "I'm the 'nPairs' property.")
727 727 pairsIndexList = property(getPairsIndexList, setValue, "I'm the 'pairsIndexList' property.")
728 728 normFactor = property(getNormFactor, setValue, "I'm the 'getNormFactor' property.")
729 729 flag_cspc = property(getFlagCspc, setValue)
730 730 flag_dc = property(getFlagDc, setValue)
731 731 noise = property(getNoise, setValue, "I'm the 'nHeights' property.")
732 732 timeInterval = property(getTimeInterval, setValue, "I'm the 'timeInterval' property")
733 733
734 734 class SpectraHeis(Spectra):
735 735
736 736 data_spc = None
737 737
738 738 data_cspc = None
739 739
740 740 data_dc = None
741 741
742 742 nFFTPoints = None
743 743
744 744 # nPairs = None
745 745
746 746 pairsList = None
747 747
748 748 nCohInt = None
749 749
750 750 nIncohInt = None
751 751
752 752 def __init__(self):
753 753
754 754 self.radarControllerHeaderObj = RadarControllerHeader()
755 755
756 756 self.systemHeaderObj = SystemHeader()
757 757
758 758 self.type = "SpectraHeis"
759 759
760 760 # self.dtype = None
761 761
762 762 # self.nChannels = 0
763 763
764 764 # self.nHeights = 0
765 765
766 766 self.nProfiles = None
767 767
768 768 self.heightList = None
769 769
770 770 self.channelList = None
771 771
772 772 # self.channelIndexList = None
773 773
774 774 self.flagNoData = True
775 775
776 776 self.flagDiscontinuousBlock = False
777 777
778 778 # self.nPairs = 0
779 779
780 780 self.utctime = None
781 781
782 782 self.blocksize = None
783 783
784 784 self.profileIndex = 0
785 785
786 786 self.nCohInt = 1
787 787
788 788 self.nIncohInt = 1
789 789
790 790 def getNormFactor(self):
791 791 pwcode = 1
792 792 if self.flagDecodeData:
793 793 pwcode = numpy.sum(self.code[0]**2)
794 794
795 795 normFactor = self.nIncohInt*self.nCohInt*pwcode
796 796
797 797 return normFactor
798 798
799 799 def getTimeInterval(self):
800 800
801 801 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
802 802
803 803 return timeInterval
804 804
805 805 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
806 806 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
807 807
808 808 class Fits(JROData):
809 809
810 810 heightList = None
811 811
812 812 channelList = None
813 813
814 814 flagNoData = True
815 815
816 816 flagDiscontinuousBlock = False
817 817
818 818 useLocalTime = False
819 819
820 820 utctime = None
821 821
822 822 timeZone = None
823 823
824 824 # ippSeconds = None
825 825
826 826 # timeInterval = None
827 827
828 828 nCohInt = None
829 829
830 830 nIncohInt = None
831 831
832 832 noise = None
833 833
834 834 windowOfFilter = 1
835 835
836 836 #Speed of ligth
837 837 C = 3e8
838 838
839 839 frequency = 49.92e6
840 840
841 841 realtime = False
842 842
843 843
844 844 def __init__(self):
845 845
846 846 self.type = "Fits"
847 847
848 848 self.nProfiles = None
849 849
850 850 self.heightList = None
851 851
852 852 self.channelList = None
853 853
854 854 # self.channelIndexList = None
855 855
856 856 self.flagNoData = True
857 857
858 858 self.utctime = None
859 859
860 860 self.nCohInt = 1
861 861
862 862 self.nIncohInt = 1
863 863
864 864 self.useLocalTime = True
865 865
866 866 self.profileIndex = 0
867 867
868 868 # self.utctime = None
869 869 # self.timeZone = None
870 870 # self.ltctime = None
871 871 # self.timeInterval = None
872 872 # self.header = None
873 873 # self.data_header = None
874 874 # self.data = None
875 875 # self.datatime = None
876 876 # self.flagNoData = False
877 877 # self.expName = ''
878 878 # self.nChannels = None
879 879 # self.nSamples = None
880 880 # self.dataBlocksPerFile = None
881 881 # self.comments = ''
882 882 #
883 883
884 884
885 885 def getltctime(self):
886 886
887 887 if self.useLocalTime:
888 888 return self.utctime - self.timeZone*60
889 889
890 890 return self.utctime
891 891
892 892 def getDatatime(self):
893 893
894 894 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
895 895 return datatime
896 896
897 897 def getTimeRange(self):
898 898
899 899 datatime = []
900 900
901 901 datatime.append(self.ltctime)
902 902 datatime.append(self.ltctime + self.timeInterval)
903 903
904 904 datatime = numpy.array(datatime)
905 905
906 906 return datatime
907 907
908 908 def getHeiRange(self):
909 909
910 910 heis = self.heightList
911 911
912 912 return heis
913 913
914 914 def getNHeights(self):
915 915
916 916 return len(self.heightList)
917 917
918 918 def getNChannels(self):
919 919
920 920 return len(self.channelList)
921 921
922 922 def getChannelIndexList(self):
923 923
924 924 return range(self.nChannels)
925 925
926 926 def getNoise(self, type = 1):
927 927
928 928 #noise = numpy.zeros(self.nChannels)
929 929
930 930 if type == 1:
931 931 noise = self.getNoisebyHildebrand()
932 932
933 933 if type == 2:
934 934 noise = self.getNoisebySort()
935 935
936 936 if type == 3:
937 937 noise = self.getNoisebyWindow()
938 938
939 939 return noise
940 940
941 941 def getTimeInterval(self):
942 942
943 943 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
944 944
945 945 return timeInterval
946 946
947 947 datatime = property(getDatatime, "I'm the 'datatime' property")
948 948 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
949 949 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
950 950 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
951 951 noise = property(getNoise, "I'm the 'nHeights' property.")
952 952
953 953 ltctime = property(getltctime, "I'm the 'ltctime' property")
954 954 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
955 955
956 956
957 957 class Correlation(JROData):
958 958
959 959 noise = None
960 960
961 961 SNR = None
962 962
963 963 #--------------------------------------------------
964 964
965 965 mode = None
966 966
967 967 split = False
968 968
969 969 data_cf = None
970 970
971 971 lags = None
972 972
973 973 lagRange = None
974 974
975 975 pairsList = None
976 976
977 977 normFactor = None
978 978
979 979 #--------------------------------------------------
980 980
981 981 # calculateVelocity = None
982 982
983 983 nLags = None
984 984
985 985 nPairs = None
986 986
987 987 nAvg = None
988 988
989 989
990 990 def __init__(self):
991 991 '''
992 992 Constructor
993 993 '''
994 994 self.radarControllerHeaderObj = RadarControllerHeader()
995 995
996 996 self.systemHeaderObj = SystemHeader()
997 997
998 998 self.type = "Correlation"
999 999
1000 1000 self.data = None
1001 1001
1002 1002 self.dtype = None
1003 1003
1004 1004 self.nProfiles = None
1005 1005
1006 1006 self.heightList = None
1007 1007
1008 1008 self.channelList = None
1009 1009
1010 1010 self.flagNoData = True
1011 1011
1012 1012 self.flagDiscontinuousBlock = False
1013 1013
1014 1014 self.utctime = None
1015 1015
1016 1016 self.timeZone = None
1017 1017
1018 1018 self.dstFlag = None
1019 1019
1020 1020 self.errorCount = None
1021 1021
1022 1022 self.blocksize = None
1023 1023
1024 1024 self.flagDecodeData = False #asumo q la data no esta decodificada
1025 1025
1026 1026 self.flagDeflipData = False #asumo q la data no esta sin flip
1027 1027
1028 1028 self.pairsList = None
1029 1029
1030 1030 self.nPoints = None
1031 1031
1032 1032 def getPairsList(self):
1033 1033
1034 1034 return self.pairsList
1035 1035
1036 1036 def getNoise(self, mode = 2):
1037 1037
1038 1038 indR = numpy.where(self.lagR == 0)[0][0]
1039 1039 indT = numpy.where(self.lagT == 0)[0][0]
1040 1040
1041 1041 jspectra0 = self.data_corr[:,:,indR,:]
1042 1042 jspectra = copy.copy(jspectra0)
1043 1043
1044 1044 num_chan = jspectra.shape[0]
1045 1045 num_hei = jspectra.shape[2]
1046 1046
1047 1047 freq_dc = jspectra.shape[1]/2
1048 1048 ind_vel = numpy.array([-2,-1,1,2]) + freq_dc
1049 1049
1050 1050 if ind_vel[0]<0:
1051 1051 ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof
1052 1052
1053 1053 if mode == 1:
1054 1054 jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION
1055 1055
1056 1056 if mode == 2:
1057 1057
1058 1058 vel = numpy.array([-2,-1,1,2])
1059 1059 xx = numpy.zeros([4,4])
1060 1060
1061 1061 for fil in range(4):
1062 1062 xx[fil,:] = vel[fil]**numpy.asarray(range(4))
1063 1063
1064 1064 xx_inv = numpy.linalg.inv(xx)
1065 1065 xx_aux = xx_inv[0,:]
1066 1066
1067 1067 for ich in range(num_chan):
1068 1068 yy = jspectra[ich,ind_vel,:]
1069 1069 jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy)
1070 1070
1071 1071 junkid = jspectra[ich,freq_dc,:]<=0
1072 1072 cjunkid = sum(junkid)
1073 1073
1074 1074 if cjunkid.any():
1075 1075 jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2
1076 1076
1077 1077 noise = jspectra0[:,freq_dc,:] - jspectra[:,freq_dc,:]
1078 1078
1079 1079 return noise
1080 1080
1081 1081 def getTimeInterval(self):
1082 1082
1083 1083 timeInterval = self.ippSeconds * self.nCohInt * self.nProfiles
1084 1084
1085 1085 return timeInterval
1086 1086
1087 1087 def splitFunctions(self):
1088 1088
1089 1089 pairsList = self.pairsList
1090 1090 ccf_pairs = []
1091 1091 acf_pairs = []
1092 1092 ccf_ind = []
1093 1093 acf_ind = []
1094 1094 for l in range(len(pairsList)):
1095 1095 chan0 = pairsList[l][0]
1096 1096 chan1 = pairsList[l][1]
1097 1097
1098 1098 #Obteniendo pares de Autocorrelacion
1099 1099 if chan0 == chan1:
1100 1100 acf_pairs.append(chan0)
1101 1101 acf_ind.append(l)
1102 1102 else:
1103 1103 ccf_pairs.append(pairsList[l])
1104 1104 ccf_ind.append(l)
1105 1105
1106 1106 data_acf = self.data_cf[acf_ind]
1107 1107 data_ccf = self.data_cf[ccf_ind]
1108 1108
1109 1109 return acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf
1110 1110
1111 1111 def getNormFactor(self):
1112 1112 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.splitFunctions()
1113 1113 acf_pairs = numpy.array(acf_pairs)
1114 1114 normFactor = numpy.zeros((self.nPairs,self.nHeights))
1115 1115
1116 1116 for p in range(self.nPairs):
1117 1117 pair = self.pairsList[p]
1118 1118
1119 1119 ch0 = pair[0]
1120 1120 ch1 = pair[1]
1121 1121
1122 1122 ch0_max = numpy.max(data_acf[acf_pairs==ch0,:,:], axis=1)
1123 1123 ch1_max = numpy.max(data_acf[acf_pairs==ch1,:,:], axis=1)
1124 1124 normFactor[p,:] = numpy.sqrt(ch0_max*ch1_max)
1125 1125
1126 1126 return normFactor
1127 1127
1128 1128 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
1129 1129 normFactor = property(getNormFactor, "I'm the 'normFactor property'")
1130 1130
1131 1131 class Parameters(Spectra):
1132 1132
1133 1133 experimentInfo = None #Information about the experiment
1134 1134
1135 1135 #Information from previous data
1136 1136
1137 1137 inputUnit = None #Type of data to be processed
1138 1138
1139 1139 operation = None #Type of operation to parametrize
1140 1140
1141 1141 #normFactor = None #Normalization Factor
1142 1142
1143 1143 groupList = None #List of Pairs, Groups, etc
1144 1144
1145 1145 #Parameters
1146 1146
1147 1147 data_param = None #Parameters obtained
1148 1148
1149 1149 data_pre = None #Data Pre Parametrization
1150 1150
1151 1151 data_SNR = None #Signal to Noise Ratio
1152 1152
1153 1153 # heightRange = None #Heights
1154 1154
1155 1155 abscissaList = None #Abscissa, can be velocities, lags or time
1156 1156
1157 1157 # noise = None #Noise Potency
1158 1158
1159 1159 utctimeInit = None #Initial UTC time
1160 1160
1161 1161 paramInterval = None #Time interval to calculate Parameters in seconds
1162 1162
1163 1163 useLocalTime = True
1164 1164
1165 1165 #Fitting
1166 1166
1167 1167 data_error = None #Error of the estimation
1168 1168
1169 1169 constants = None
1170 1170
1171 1171 library = None
1172 1172
1173 1173 #Output signal
1174 1174
1175 1175 outputInterval = None #Time interval to calculate output signal in seconds
1176 1176
1177 1177 data_output = None #Out signal
1178 1178
1179 1179 nAvg = None
1180 1180
1181 1181 noise_estimation = None
1182
1183 GauSPC = None #Fit gaussian SPC
1182 1184
1183 1185
1184 1186 def __init__(self):
1185 1187 '''
1186 1188 Constructor
1187 1189 '''
1188 1190 self.radarControllerHeaderObj = RadarControllerHeader()
1189 1191
1190 1192 self.systemHeaderObj = SystemHeader()
1191 1193
1192 1194 self.type = "Parameters"
1193 1195
1194 1196 def getTimeRange1(self, interval):
1195 1197
1196 1198 datatime = []
1197 1199
1198 1200 if self.useLocalTime:
1199 1201 time1 = self.utctimeInit - self.timeZone*60
1200 1202 else:
1201 1203 time1 = self.utctimeInit
1202
1204 print 'interval',interval
1203 1205 datatime.append(time1)
1204 1206 datatime.append(time1 + interval)
1205 1207 datatime = numpy.array(datatime)
1206 1208
1207 1209 return datatime
1208 1210
1209 1211 def getTimeInterval(self):
1210 1212
1211 1213 if hasattr(self, 'timeInterval1'):
1212 1214 return self.timeInterval1
1213 1215 else:
1214 1216 return self.paramInterval
1215 1217
1218 def setValue(self, value):
1219
1220 print "This property should not be initialized"
1221
1222 return
1223
1216 1224 def getNoise(self):
1217 1225
1218 1226 return self.spc_noise
1219 1227
1220 1228 timeInterval = property(getTimeInterval)
1229 noise = property(getNoise, setValue, "I'm the 'Noise' property.")
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
@@ -1,1945 +1,2159
1 1 import os
2 2 import datetime
3 3 import numpy
4 4 import inspect
5 5 from figure import Figure, isRealtime, isTimeInHourRange
6 6 from plotting_codes import *
7 7
8 8
9 class FitGauPlot(Figure):
10
11 isConfig = None
12 __nsubplots = None
13
14 WIDTHPROF = None
15 HEIGHTPROF = None
16 PREFIX = 'fitgau'
17
18 def __init__(self, **kwargs):
19 Figure.__init__(self, **kwargs)
20 self.isConfig = False
21 self.__nsubplots = 1
22
23 self.WIDTH = 250
24 self.HEIGHT = 250
25 self.WIDTHPROF = 120
26 self.HEIGHTPROF = 0
27 self.counter_imagwr = 0
28
29 self.PLOT_CODE = SPEC_CODE
30
31 self.FTP_WEI = None
32 self.EXP_CODE = None
33 self.SUB_EXP_CODE = None
34 self.PLOT_POS = None
35
36 self.__xfilter_ena = False
37 self.__yfilter_ena = False
38
39 def getSubplots(self):
40
41 ncol = int(numpy.sqrt(self.nplots)+0.9)
42 nrow = int(self.nplots*1./ncol + 0.9)
43
44 return nrow, ncol
45
46 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
47
48 self.__showprofile = showprofile
49 self.nplots = nplots
50
51 ncolspan = 1
52 colspan = 1
53 if showprofile:
54 ncolspan = 3
55 colspan = 2
56 self.__nsubplots = 2
57
58 self.createFigure(id = id,
59 wintitle = wintitle,
60 widthplot = self.WIDTH + self.WIDTHPROF,
61 heightplot = self.HEIGHT + self.HEIGHTPROF,
62 show=show)
63
64 nrow, ncol = self.getSubplots()
65
66 counter = 0
67 for y in range(nrow):
68 for x in range(ncol):
69
70 if counter >= self.nplots:
71 break
72
73 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
74
75 if showprofile:
76 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
77
78 counter += 1
79
80 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
81 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
82 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
83 server=None, folder=None, username=None, password=None,
84 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
85 xaxis="frequency", colormap='jet', normFactor=None , GauSelector = 1):
86
87 """
88
89 Input:
90 dataOut :
91 id :
92 wintitle :
93 channelList :
94 showProfile :
95 xmin : None,
96 xmax : None,
97 ymin : None,
98 ymax : None,
99 zmin : None,
100 zmax : None
101 """
102 if realtime:
103 if not(isRealtime(utcdatatime = dataOut.utctime)):
104 print 'Skipping this plot function'
105 return
106
107 if channelList == None:
108 channelIndexList = dataOut.channelIndexList
109 else:
110 channelIndexList = []
111 for channel in channelList:
112 if channel not in dataOut.channelList:
113 raise ValueError, "Channel %d is not in dataOut.channelList" %channel
114 channelIndexList.append(dataOut.channelList.index(channel))
115
116 # if normFactor is None:
117 # factor = dataOut.normFactor
118 # else:
119 # factor = normFactor
120 if xaxis == "frequency":
121 x = dataOut.spc_range[0]
122 xlabel = "Frequency (kHz)"
123
124 elif xaxis == "time":
125 x = dataOut.spc_range[1]
126 xlabel = "Time (ms)"
127
128 else:
129 x = dataOut.spc_range[2]
130 xlabel = "Velocity (m/s)"
131
132 ylabel = "Range (Km)"
133
134 y = dataOut.getHeiRange()
135
136 z = dataOut.GauSPC[:,GauSelector,:,:] #GauSelector] #dataOut.data_spc/factor
137 print 'GausSPC', z[0,32,10:40]
138 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
139 zdB = 10*numpy.log10(z)
140
141 avg = numpy.average(z, axis=1)
142 avgdB = 10*numpy.log10(avg)
143
144 noise = dataOut.spc_noise
145 noisedB = 10*numpy.log10(noise)
146
147 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
148 title = wintitle + " Spectra"
149 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
150 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
151
152 if not self.isConfig:
153
154 nplots = len(channelIndexList)
155
156 self.setup(id=id,
157 nplots=nplots,
158 wintitle=wintitle,
159 showprofile=showprofile,
160 show=show)
161
162 if xmin == None: xmin = numpy.nanmin(x)
163 if xmax == None: xmax = numpy.nanmax(x)
164 if ymin == None: ymin = numpy.nanmin(y)
165 if ymax == None: ymax = numpy.nanmax(y)
166 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
167 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
168
169 self.FTP_WEI = ftp_wei
170 self.EXP_CODE = exp_code
171 self.SUB_EXP_CODE = sub_exp_code
172 self.PLOT_POS = plot_pos
173
174 self.isConfig = True
175
176 self.setWinTitle(title)
177
178 for i in range(self.nplots):
179 index = channelIndexList[i]
180 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
181 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
182 if len(dataOut.beam.codeList) != 0:
183 title = "Ch%d:%4.2fdB,%2.2f,%2.2f:%s" %(dataOut.channelList[index], noisedB[index], dataOut.beam.azimuthList[index], dataOut.beam.zenithList[index], str_datetime)
184
185 axes = self.axesList[i*self.__nsubplots]
186 axes.pcolor(x, y, zdB[index,:,:],
187 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
188 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
189 ticksize=9, cblabel='')
190
191 if self.__showprofile:
192 axes = self.axesList[i*self.__nsubplots +1]
193 axes.pline(avgdB[index,:], y,
194 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
195 xlabel='dB', ylabel='', title='',
196 ytick_visible=False,
197 grid='x')
198
199 noiseline = numpy.repeat(noisedB[index], len(y))
200 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
201
202 self.draw()
203
204 if figfile == None:
205 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
206 name = str_datetime
207 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
208 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
209 figfile = self.getFilename(name)
210
211 self.save(figpath=figpath,
212 figfile=figfile,
213 save=save,
214 ftp=ftp,
215 wr_period=wr_period,
216 thisDatetime=thisDatetime)
217
218
219
9 220 class MomentsPlot(Figure):
10 221
11 222 isConfig = None
12 223 __nsubplots = None
13 224
14 225 WIDTHPROF = None
15 226 HEIGHTPROF = None
16 227 PREFIX = 'prm'
17 228
18 229 def __init__(self, **kwargs):
19 230 Figure.__init__(self, **kwargs)
20 231 self.isConfig = False
21 232 self.__nsubplots = 1
22 233
23 234 self.WIDTH = 280
24 235 self.HEIGHT = 250
25 236 self.WIDTHPROF = 120
26 237 self.HEIGHTPROF = 0
27 238 self.counter_imagwr = 0
28 239
29 240 self.PLOT_CODE = MOMENTS_CODE
30 241
31 242 self.FTP_WEI = None
32 243 self.EXP_CODE = None
33 244 self.SUB_EXP_CODE = None
34 245 self.PLOT_POS = None
35 246
36 247 def getSubplots(self):
37 248
38 249 ncol = int(numpy.sqrt(self.nplots)+0.9)
39 250 nrow = int(self.nplots*1./ncol + 0.9)
40 251
41 252 return nrow, ncol
42 253
43 254 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
44 255
45 256 self.__showprofile = showprofile
46 257 self.nplots = nplots
47 258
48 259 ncolspan = 1
49 260 colspan = 1
50 261 if showprofile:
51 262 ncolspan = 3
52 263 colspan = 2
53 264 self.__nsubplots = 2
54 265
55 266 self.createFigure(id = id,
56 267 wintitle = wintitle,
57 268 widthplot = self.WIDTH + self.WIDTHPROF,
58 269 heightplot = self.HEIGHT + self.HEIGHTPROF,
59 270 show=show)
60 271
61 272 nrow, ncol = self.getSubplots()
62 273
63 274 counter = 0
64 275 for y in range(nrow):
65 276 for x in range(ncol):
66 277
67 278 if counter >= self.nplots:
68 279 break
69 280
70 281 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
71 282
72 283 if showprofile:
73 284 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
74 285
75 286 counter += 1
76 287
77 288 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
78 289 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
79 290 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
80 291 server=None, folder=None, username=None, password=None,
81 292 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
82 293
83 294 """
84 295
85 296 Input:
86 297 dataOut :
87 298 id :
88 299 wintitle :
89 300 channelList :
90 301 showProfile :
91 302 xmin : None,
92 303 xmax : None,
93 304 ymin : None,
94 305 ymax : None,
95 306 zmin : None,
96 307 zmax : None
97 308 """
98 309
99 310 if dataOut.flagNoData:
100 311 return None
101 312
102 313 if realtime:
103 314 if not(isRealtime(utcdatatime = dataOut.utctime)):
104 315 print 'Skipping this plot function'
105 316 return
106 317
107 318 if channelList == None:
108 319 channelIndexList = dataOut.channelIndexList
109 320 else:
110 321 channelIndexList = []
111 322 for channel in channelList:
112 323 if channel not in dataOut.channelList:
113 324 raise ValueError, "Channel %d is not in dataOut.channelList"
114 325 channelIndexList.append(dataOut.channelList.index(channel))
115 326
116 327 factor = dataOut.normFactor
117 328 x = dataOut.abscissaList
118 329 y = dataOut.heightList
119 330
120 331 z = dataOut.data_pre[channelIndexList,:,:]/factor
121 332 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
122 333 avg = numpy.average(z, axis=1)
123 334 noise = dataOut.noise/factor
124 335
125 336 zdB = 10*numpy.log10(z)
126 337 avgdB = 10*numpy.log10(avg)
127 338 noisedB = 10*numpy.log10(noise)
128 339
129 340 #thisDatetime = dataOut.datatime
130 341 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
131 342 title = wintitle + " Parameters"
132 343 xlabel = "Velocity (m/s)"
133 344 ylabel = "Range (Km)"
134 345
135 346 update_figfile = False
136 347
137 348 if not self.isConfig:
138 349
139 350 nplots = len(channelIndexList)
140 351
141 352 self.setup(id=id,
142 353 nplots=nplots,
143 354 wintitle=wintitle,
144 355 showprofile=showprofile,
145 356 show=show)
146 357
147 358 if xmin == None: xmin = numpy.nanmin(x)
148 359 if xmax == None: xmax = numpy.nanmax(x)
149 360 if ymin == None: ymin = numpy.nanmin(y)
150 361 if ymax == None: ymax = numpy.nanmax(y)
151 362 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
152 363 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
153 364
154 365 self.FTP_WEI = ftp_wei
155 366 self.EXP_CODE = exp_code
156 367 self.SUB_EXP_CODE = sub_exp_code
157 368 self.PLOT_POS = plot_pos
158 369
159 370 self.isConfig = True
160 371 update_figfile = True
161 372
162 373 self.setWinTitle(title)
163 374
164 375 for i in range(self.nplots):
165 376 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
166 377 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i], noisedB[i], str_datetime)
167 378 axes = self.axesList[i*self.__nsubplots]
168 379 axes.pcolor(x, y, zdB[i,:,:],
169 380 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
170 381 xlabel=xlabel, ylabel=ylabel, title=title,
171 382 ticksize=9, cblabel='')
172 383 #Mean Line
173 384 mean = dataOut.data_param[i, 1, :]
174 385 axes.addpline(mean, y, idline=0, color="black", linestyle="solid", lw=1)
175 386
176 387 if self.__showprofile:
177 388 axes = self.axesList[i*self.__nsubplots +1]
178 389 axes.pline(avgdB[i], y,
179 390 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
180 391 xlabel='dB', ylabel='', title='',
181 392 ytick_visible=False,
182 393 grid='x')
183 394
184 395 noiseline = numpy.repeat(noisedB[i], len(y))
185 396 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
186 397
187 398 self.draw()
188 399
189 400 self.save(figpath=figpath,
190 401 figfile=figfile,
191 402 save=save,
192 403 ftp=ftp,
193 404 wr_period=wr_period,
194 405 thisDatetime=thisDatetime)
195 406
196 407
197 408
198 409 class SkyMapPlot(Figure):
199 410
200 411 __isConfig = None
201 412 __nsubplots = None
202 413
203 414 WIDTHPROF = None
204 415 HEIGHTPROF = None
205 416 PREFIX = 'mmap'
206 417
207 418 def __init__(self, **kwargs):
208 419 Figure.__init__(self, **kwargs)
209 420 self.isConfig = False
210 421 self.__nsubplots = 1
211 422
212 423 # self.WIDTH = 280
213 424 # self.HEIGHT = 250
214 425 self.WIDTH = 600
215 426 self.HEIGHT = 600
216 427 self.WIDTHPROF = 120
217 428 self.HEIGHTPROF = 0
218 429 self.counter_imagwr = 0
219 430
220 431 self.PLOT_CODE = MSKYMAP_CODE
221 432
222 433 self.FTP_WEI = None
223 434 self.EXP_CODE = None
224 435 self.SUB_EXP_CODE = None
225 436 self.PLOT_POS = None
226 437
227 438 def getSubplots(self):
228 439
229 440 ncol = int(numpy.sqrt(self.nplots)+0.9)
230 441 nrow = int(self.nplots*1./ncol + 0.9)
231 442
232 443 return nrow, ncol
233 444
234 445 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
235 446
236 447 self.__showprofile = showprofile
237 448 self.nplots = nplots
238 449
239 450 ncolspan = 1
240 451 colspan = 1
241 452
242 453 self.createFigure(id = id,
243 454 wintitle = wintitle,
244 455 widthplot = self.WIDTH, #+ self.WIDTHPROF,
245 456 heightplot = self.HEIGHT,# + self.HEIGHTPROF,
246 457 show=show)
247 458
248 459 nrow, ncol = 1,1
249 460 counter = 0
250 461 x = 0
251 462 y = 0
252 463 self.addAxes(1, 1, 0, 0, 1, 1, True)
253 464
254 465 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
255 466 tmin=0, tmax=24, timerange=None,
256 467 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
257 468 server=None, folder=None, username=None, password=None,
258 469 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
259 470
260 471 """
261 472
262 473 Input:
263 474 dataOut :
264 475 id :
265 476 wintitle :
266 477 channelList :
267 478 showProfile :
268 479 xmin : None,
269 480 xmax : None,
270 481 ymin : None,
271 482 ymax : None,
272 483 zmin : None,
273 484 zmax : None
274 485 """
275 486
276 487 arrayParameters = dataOut.data_param
277 488 error = arrayParameters[:,-1]
278 489 indValid = numpy.where(error == 0)[0]
279 490 finalMeteor = arrayParameters[indValid,:]
280 491 finalAzimuth = finalMeteor[:,3]
281 492 finalZenith = finalMeteor[:,4]
282 493
283 494 x = finalAzimuth*numpy.pi/180
284 495 y = finalZenith
285 496 x1 = [dataOut.ltctime, dataOut.ltctime]
286 497
287 498 #thisDatetime = dataOut.datatime
288 499 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
289 500 title = wintitle + " Parameters"
290 501 xlabel = "Zonal Zenith Angle (deg) "
291 502 ylabel = "Meridional Zenith Angle (deg)"
292 503 update_figfile = False
293 504
294 505 if not self.isConfig:
295 506
296 507 nplots = 1
297 508
298 509 self.setup(id=id,
299 510 nplots=nplots,
300 511 wintitle=wintitle,
301 512 showprofile=showprofile,
302 513 show=show)
303 514
304 515 if self.xmin is None and self.xmax is None:
305 516 self.xmin, self.xmax = self.getTimeLim(x1, tmin, tmax, timerange)
306 517
307 518 if timerange != None:
308 519 self.timerange = timerange
309 520 else:
310 521 self.timerange = self.xmax - self.xmin
311 522
312 523 self.FTP_WEI = ftp_wei
313 524 self.EXP_CODE = exp_code
314 525 self.SUB_EXP_CODE = sub_exp_code
315 526 self.PLOT_POS = plot_pos
316 527 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
317 528 self.firstdate = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
318 529 self.isConfig = True
319 530 update_figfile = True
320 531
321 532 self.setWinTitle(title)
322 533
323 534 i = 0
324 535 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
325 536
326 537 axes = self.axesList[i*self.__nsubplots]
327 538 nevents = axes.x_buffer.shape[0] + x.shape[0]
328 539 title = "Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n" %(self.firstdate,str_datetime,nevents)
329 540 axes.polar(x, y,
330 541 title=title, xlabel=xlabel, ylabel=ylabel,
331 542 ticksize=9, cblabel='')
332 543
333 544 self.draw()
334 545
335 546 self.save(figpath=figpath,
336 547 figfile=figfile,
337 548 save=save,
338 549 ftp=ftp,
339 550 wr_period=wr_period,
340 551 thisDatetime=thisDatetime,
341 552 update_figfile=update_figfile)
342 553
343 554 if dataOut.ltctime >= self.xmax:
344 555 self.isConfigmagwr = wr_period
345 556 self.isConfig = False
346 557 update_figfile = True
347 558 axes.__firsttime = True
348 559 self.xmin += self.timerange
349 560 self.xmax += self.timerange
350 561
351 562
352 563
353 564
354 565 class WindProfilerPlot(Figure):
355 566
356 567 __isConfig = None
357 568 __nsubplots = None
358 569
359 570 WIDTHPROF = None
360 571 HEIGHTPROF = None
361 572 PREFIX = 'wind'
362 573
363 574 def __init__(self, **kwargs):
364 575 Figure.__init__(self, **kwargs)
365 576 self.timerange = None
366 577 self.isConfig = False
367 578 self.__nsubplots = 1
368 579
369 580 self.WIDTH = 800
370 581 self.HEIGHT = 300
371 582 self.WIDTHPROF = 120
372 583 self.HEIGHTPROF = 0
373 584 self.counter_imagwr = 0
374 585
375 586 self.PLOT_CODE = WIND_CODE
376 587
377 588 self.FTP_WEI = None
378 589 self.EXP_CODE = None
379 590 self.SUB_EXP_CODE = None
380 591 self.PLOT_POS = None
381 592 self.tmin = None
382 593 self.tmax = None
383 594
384 595 self.xmin = None
385 596 self.xmax = None
386 597
387 598 self.figfile = None
388 599
389 600 def getSubplots(self):
390 601
391 602 ncol = 1
392 603 nrow = self.nplots
393 604
394 605 return nrow, ncol
395 606
396 607 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
397 608
398 609 self.__showprofile = showprofile
399 610 self.nplots = nplots
400 611
401 612 ncolspan = 1
402 613 colspan = 1
403 614
404 615 self.createFigure(id = id,
405 616 wintitle = wintitle,
406 617 widthplot = self.WIDTH + self.WIDTHPROF,
407 618 heightplot = self.HEIGHT + self.HEIGHTPROF,
408 619 show=show)
409 620
410 621 nrow, ncol = self.getSubplots()
411 622
412 623 counter = 0
413 624 for y in range(nrow):
414 625 if counter >= self.nplots:
415 626 break
416 627
417 628 self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1)
418 629 counter += 1
419 630
420 631 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='False',
421 632 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
422 633 zmax_ver = None, zmin_ver = None, SNRmin = None, SNRmax = None,
423 634 timerange=None, SNRthresh = None,
424 635 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
425 636 server=None, folder=None, username=None, password=None,
426 637 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
427 638 """
428 639
429 640 Input:
430 641 dataOut :
431 642 id :
432 643 wintitle :
433 644 channelList :
434 645 showProfile :
435 646 xmin : None,
436 647 xmax : None,
437 648 ymin : None,
438 649 ymax : None,
439 650 zmin : None,
440 651 zmax : None
441 652 """
442 653
443 654 # if timerange is not None:
444 655 # self.timerange = timerange
445 656 #
446 657 # tmin = None
447 658 # tmax = None
448 659
449
450 x = dataOut.getTimeRange1(dataOut.outputInterval)
660 x = dataOut.getTimeRange1(dataOut.paramInterval)
451 661 y = dataOut.heightList
452 662 z = dataOut.data_output.copy()
663 print ' '
664 print 'Xvel',z[0]
665 print ' '
666 print 'Yvel',z[1]
667 print ' '
453 668 nplots = z.shape[0] #Number of wind dimensions estimated
454 669 nplotsw = nplots
455 670
456 671
457 672 #If there is a SNR function defined
458 673 if dataOut.data_SNR is not None:
459 674 nplots += 1
460 675 SNR = dataOut.data_SNR
461 676 SNRavg = numpy.average(SNR, axis=0)
462 677
463 678 SNRdB = 10*numpy.log10(SNR)
464 679 SNRavgdB = 10*numpy.log10(SNRavg)
465 680
466 681 if SNRthresh == None: SNRthresh = -5.0
467 682 ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0]
468 683
469 684 for i in range(nplotsw):
470 685 z[i,ind] = numpy.nan
471 686
472 687 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
473 688 #thisDatetime = datetime.datetime.now()
474 689 title = wintitle + "Wind"
475 690 xlabel = ""
476 691 ylabel = "Height (km)"
477 692 update_figfile = False
478 693
479 694 if not self.isConfig:
480 695
481 696 self.setup(id=id,
482 697 nplots=nplots,
483 698 wintitle=wintitle,
484 699 showprofile=showprofile,
485 700 show=show)
486 701
487 702 if timerange is not None:
488 703 self.timerange = timerange
489 704
490 705 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
491 706
492 707 if ymin == None: ymin = numpy.nanmin(y)
493 708 if ymax == None: ymax = numpy.nanmax(y)
494 709
495 710 if zmax == None: zmax = numpy.nanmax(abs(z[range(2),:]))
496 711 #if numpy.isnan(zmax): zmax = 50
497 712 if zmin == None: zmin = -zmax
498 713
499 714 if nplotsw == 3:
500 715 if zmax_ver == None: zmax_ver = numpy.nanmax(abs(z[2,:]))
501 716 if zmin_ver == None: zmin_ver = -zmax_ver
502 717
503 718 if dataOut.data_SNR is not None:
504 719 if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB)
505 720 if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB)
506 721
507 722
508 723 self.FTP_WEI = ftp_wei
509 724 self.EXP_CODE = exp_code
510 725 self.SUB_EXP_CODE = sub_exp_code
511 726 self.PLOT_POS = plot_pos
512 727
513 728 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
514 729 self.isConfig = True
515 730 self.figfile = figfile
516 731 update_figfile = True
517 732
518 733 self.setWinTitle(title)
519 734
520 735 if ((self.xmax - x[1]) < (x[1]-x[0])):
521 736 x[1] = self.xmax
522 737
523 738 strWind = ['Zonal', 'Meridional', 'Vertical']
524 739 strCb = ['Velocity (m/s)','Velocity (m/s)','Velocity (cm/s)']
525 740 zmaxVector = [zmax, zmax, zmax_ver]
526 741 zminVector = [zmin, zmin, zmin_ver]
527 742 windFactor = [1,1,100]
528 743
529 744 for i in range(nplotsw):
530 745
531 746 title = "%s Wind: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
532 747 axes = self.axesList[i*self.__nsubplots]
533 748
534 749 z1 = z[i,:].reshape((1,-1))*windFactor[i]
535 750 #z1=numpy.ma.masked_where(z1==0.,z1)
536 751
537 752 axes.pcolorbuffer(x, y, z1,
538 753 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i],
539 754 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
540 755 ticksize=9, cblabel=strCb[i], cbsize="1%", colormap="seismic" )
541 756
542 757 if dataOut.data_SNR is not None:
543 758 i += 1
544 759 title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
545 760 axes = self.axesList[i*self.__nsubplots]
546 761 SNRavgdB = SNRavgdB.reshape((1,-1))
547 762 axes.pcolorbuffer(x, y, SNRavgdB,
548 763 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
549 764 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
550 765 ticksize=9, cblabel='', cbsize="1%", colormap="jet")
551 766
552 767 self.draw()
553 768
554 769 self.save(figpath=figpath,
555 770 figfile=figfile,
556 771 save=save,
557 772 ftp=ftp,
558 773 wr_period=wr_period,
559 774 thisDatetime=thisDatetime,
560 775 update_figfile=update_figfile)
561 776
562 if dataOut.ltctime + dataOut.outputInterval >= self.xmax:
777 if dataOut.ltctime + dataOut.paramInterval >= self.xmax:
563 778 self.counter_imagwr = wr_period
564 779 self.isConfig = False
565 780 update_figfile = True
566 781
567 782
568 783 class ParametersPlot(Figure):
569 784
570 785 __isConfig = None
571 786 __nsubplots = None
572 787
573 788 WIDTHPROF = None
574 789 HEIGHTPROF = None
575 790 PREFIX = 'param'
576 791
577 792 nplots = None
578 793 nchan = None
579 794
580 795 def __init__(self, **kwargs):
581 796 Figure.__init__(self, **kwargs)
582 797 self.timerange = None
583 798 self.isConfig = False
584 799 self.__nsubplots = 1
585 800
586 801 self.WIDTH = 800
587 802 self.HEIGHT = 180
588 803 self.WIDTHPROF = 120
589 804 self.HEIGHTPROF = 0
590 805 self.counter_imagwr = 0
591 806
592 807 self.PLOT_CODE = RTI_CODE
593 808
594 809 self.FTP_WEI = None
595 810 self.EXP_CODE = None
596 811 self.SUB_EXP_CODE = None
597 812 self.PLOT_POS = None
598 813 self.tmin = None
599 814 self.tmax = None
600 815
601 816 self.xmin = None
602 817 self.xmax = None
603 818
604 819 self.figfile = None
605 820
606 821 def getSubplots(self):
607 822
608 823 ncol = 1
609 824 nrow = self.nplots
610 825
611 826 return nrow, ncol
612 827
613 828 def setup(self, id, nplots, wintitle, show=True):
614 829
615 830 self.nplots = nplots
616 831
617 832 ncolspan = 1
618 833 colspan = 1
619 834
620 835 self.createFigure(id = id,
621 836 wintitle = wintitle,
622 837 widthplot = self.WIDTH + self.WIDTHPROF,
623 838 heightplot = self.HEIGHT + self.HEIGHTPROF,
624 839 show=show)
625 840
626 841 nrow, ncol = self.getSubplots()
627 842
628 843 counter = 0
629 844 for y in range(nrow):
630 845 for x in range(ncol):
631 846
632 847 if counter >= self.nplots:
633 848 break
634 849
635 850 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
636 851
637 852 counter += 1
638 853
639 def run(self, dataOut, id, wintitle="", channelList=None, paramIndex = 0, colormap=True,
854 def run(self, dataOut, id, wintitle="", channelList=None, paramIndex = 0, colormap="jet",
640 855 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, timerange=None,
641 856 showSNR=False, SNRthresh = -numpy.inf, SNRmin=None, SNRmax=None,
642 857 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
643 858 server=None, folder=None, username=None, password=None,
644 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
859 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, HEIGHT=None):
645 860 """
646 861
647 862 Input:
648 863 dataOut :
649 864 id :
650 865 wintitle :
651 866 channelList :
652 867 showProfile :
653 868 xmin : None,
654 869 xmax : None,
655 870 ymin : None,
656 871 ymax : None,
657 872 zmin : None,
658 873 zmax : None
659 874 """
660
661 if colormap:
662 colormap="jet"
663 else:
664 colormap="RdBu_r"
665
875
876 if HEIGHT is not None:
877 self.HEIGHT = HEIGHT
878
879
666 880 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
667 881 return
668 882
669 883 if channelList == None:
670 884 channelIndexList = range(dataOut.data_param.shape[0])
671 885 else:
672 886 channelIndexList = []
673 887 for channel in channelList:
674 888 if channel not in dataOut.channelList:
675 889 raise ValueError, "Channel %d is not in dataOut.channelList"
676 890 channelIndexList.append(dataOut.channelList.index(channel))
677 891
678 892 x = dataOut.getTimeRange1(dataOut.paramInterval)
679 893 y = dataOut.getHeiRange()
680 894
681 895 if dataOut.data_param.ndim == 3:
682 896 z = dataOut.data_param[channelIndexList,paramIndex,:]
683 897 else:
684 898 z = dataOut.data_param[channelIndexList,:]
685 899
686 900 if showSNR:
687 901 #SNR data
688 902 SNRarray = dataOut.data_SNR[channelIndexList,:]
689 903 SNRdB = 10*numpy.log10(SNRarray)
690 904 ind = numpy.where(SNRdB < SNRthresh)
691 905 z[ind] = numpy.nan
692 906
693 907 thisDatetime = dataOut.datatime
694 908 # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
695 909 title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
696 910 xlabel = ""
697 911 ylabel = "Range (Km)"
698 912
699 913 update_figfile = False
700 914
701 915 if not self.isConfig:
702 916
703 917 nchan = len(channelIndexList)
704 918 self.nchan = nchan
705 919 self.plotFact = 1
706 920 nplots = nchan
707 921
708 922 if showSNR:
709 923 nplots = nchan*2
710 924 self.plotFact = 2
711 925 if SNRmin == None: SNRmin = numpy.nanmin(SNRdB)
712 926 if SNRmax == None: SNRmax = numpy.nanmax(SNRdB)
713 927
714 928 self.setup(id=id,
715 929 nplots=nplots,
716 930 wintitle=wintitle,
717 931 show=show)
718 932
719 933 if timerange != None:
720 934 self.timerange = timerange
721 935
722 936 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
723 937
724 938 if ymin == None: ymin = numpy.nanmin(y)
725 939 if ymax == None: ymax = numpy.nanmax(y)
726 940 if zmin == None: zmin = numpy.nanmin(z)
727 941 if zmax == None: zmax = numpy.nanmax(z)
728 942
729 943 self.FTP_WEI = ftp_wei
730 944 self.EXP_CODE = exp_code
731 945 self.SUB_EXP_CODE = sub_exp_code
732 946 self.PLOT_POS = plot_pos
733 947
734 948 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
735 949 self.isConfig = True
736 950 self.figfile = figfile
737 951 update_figfile = True
738 952
739 953 self.setWinTitle(title)
740 954
741 955 for i in range(self.nchan):
742 956 index = channelIndexList[i]
743 957 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
744 958 axes = self.axesList[i*self.plotFact]
745 959 z1 = z[i,:].reshape((1,-1))
746 960 axes.pcolorbuffer(x, y, z1,
747 961 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
748 962 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
749 963 ticksize=9, cblabel='', cbsize="1%",colormap=colormap)
750 964
751 965 if showSNR:
752 966 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
753 967 axes = self.axesList[i*self.plotFact + 1]
754 968 SNRdB1 = SNRdB[i,:].reshape((1,-1))
755 969 axes.pcolorbuffer(x, y, SNRdB1,
756 970 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
757 971 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
758 972 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
759 973
760 974
761 975 self.draw()
762 976
763 977 if dataOut.ltctime >= self.xmax:
764 978 self.counter_imagwr = wr_period
765 979 self.isConfig = False
766 980 update_figfile = True
767 981
768 982 self.save(figpath=figpath,
769 983 figfile=figfile,
770 984 save=save,
771 985 ftp=ftp,
772 986 wr_period=wr_period,
773 987 thisDatetime=thisDatetime,
774 988 update_figfile=update_figfile)
775 989
776 990
777 991
778 992 class Parameters1Plot(Figure):
779 993
780 994 __isConfig = None
781 995 __nsubplots = None
782 996
783 997 WIDTHPROF = None
784 998 HEIGHTPROF = None
785 999 PREFIX = 'prm'
786 1000
787 1001 def __init__(self, **kwargs):
788 1002 Figure.__init__(self, **kwargs)
789 1003 self.timerange = 2*60*60
790 1004 self.isConfig = False
791 1005 self.__nsubplots = 1
792 1006
793 1007 self.WIDTH = 800
794 1008 self.HEIGHT = 180
795 1009 self.WIDTHPROF = 120
796 1010 self.HEIGHTPROF = 0
797 1011 self.counter_imagwr = 0
798 1012
799 1013 self.PLOT_CODE = PARMS_CODE
800 1014
801 1015 self.FTP_WEI = None
802 1016 self.EXP_CODE = None
803 1017 self.SUB_EXP_CODE = None
804 1018 self.PLOT_POS = None
805 1019 self.tmin = None
806 1020 self.tmax = None
807 1021
808 1022 self.xmin = None
809 1023 self.xmax = None
810 1024
811 1025 self.figfile = None
812 1026
813 1027 def getSubplots(self):
814 1028
815 1029 ncol = 1
816 1030 nrow = self.nplots
817 1031
818 1032 return nrow, ncol
819 1033
820 1034 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
821 1035
822 1036 self.__showprofile = showprofile
823 1037 self.nplots = nplots
824 1038
825 1039 ncolspan = 1
826 1040 colspan = 1
827 1041
828 1042 self.createFigure(id = id,
829 1043 wintitle = wintitle,
830 1044 widthplot = self.WIDTH + self.WIDTHPROF,
831 1045 heightplot = self.HEIGHT + self.HEIGHTPROF,
832 1046 show=show)
833 1047
834 1048 nrow, ncol = self.getSubplots()
835 1049
836 1050 counter = 0
837 1051 for y in range(nrow):
838 1052 for x in range(ncol):
839 1053
840 1054 if counter >= self.nplots:
841 1055 break
842 1056
843 1057 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
844 1058
845 1059 if showprofile:
846 1060 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
847 1061
848 1062 counter += 1
849 1063
850 1064 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
851 1065 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,timerange=None,
852 1066 parameterIndex = None, onlyPositive = False,
853 1067 SNRthresh = -numpy.inf, SNR = True, SNRmin = None, SNRmax = None, onlySNR = False,
854 1068 DOP = True,
855 1069 zlabel = "", parameterName = "", parameterObject = "data_param",
856 1070 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
857 1071 server=None, folder=None, username=None, password=None,
858 1072 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
859 1073 #print inspect.getargspec(self.run).args
860 1074 """
861 1075
862 1076 Input:
863 1077 dataOut :
864 1078 id :
865 1079 wintitle :
866 1080 channelList :
867 1081 showProfile :
868 1082 xmin : None,
869 1083 xmax : None,
870 1084 ymin : None,
871 1085 ymax : None,
872 1086 zmin : None,
873 1087 zmax : None
874 1088 """
875 1089
876 1090 data_param = getattr(dataOut, parameterObject)
877 1091
878 1092 if channelList == None:
879 1093 channelIndexList = numpy.arange(data_param.shape[0])
880 1094 else:
881 1095 channelIndexList = numpy.array(channelList)
882 1096
883 1097 nchan = len(channelIndexList) #Number of channels being plotted
884 1098
885 1099 if nchan < 1:
886 1100 return
887 1101
888 1102 nGraphsByChannel = 0
889 1103
890 1104 if SNR:
891 1105 nGraphsByChannel += 1
892 1106 if DOP:
893 1107 nGraphsByChannel += 1
894 1108
895 1109 if nGraphsByChannel < 1:
896 1110 return
897 1111
898 1112 nplots = nGraphsByChannel*nchan
899 1113
900 1114 if timerange is not None:
901 1115 self.timerange = timerange
902 1116
903 1117 #tmin = None
904 1118 #tmax = None
905 1119 if parameterIndex == None:
906 1120 parameterIndex = 1
907 1121
908 1122 x = dataOut.getTimeRange1(dataOut.paramInterval)
909 1123 y = dataOut.heightList
910 1124 z = data_param[channelIndexList,parameterIndex,:].copy()
911 1125
912 1126 zRange = dataOut.abscissaList
913 1127 # nChannels = z.shape[0] #Number of wind dimensions estimated
914 1128 # thisDatetime = dataOut.datatime
915 1129
916 1130 if dataOut.data_SNR is not None:
917 1131 SNRarray = dataOut.data_SNR[channelIndexList,:]
918 1132 SNRdB = 10*numpy.log10(SNRarray)
919 1133 # SNRavgdB = 10*numpy.log10(SNRavg)
920 1134 ind = numpy.where(SNRdB < 10**(SNRthresh/10))
921 1135 z[ind] = numpy.nan
922 1136
923 1137 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
924 1138 title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
925 1139 xlabel = ""
926 1140 ylabel = "Range (Km)"
927 1141
928 1142 if (SNR and not onlySNR): nplots = 2*nplots
929 1143
930 1144 if onlyPositive:
931 1145 colormap = "jet"
932 1146 zmin = 0
933 1147 else: colormap = "RdBu_r"
934 1148
935 1149 if not self.isConfig:
936 1150
937 1151 self.setup(id=id,
938 1152 nplots=nplots,
939 1153 wintitle=wintitle,
940 1154 showprofile=showprofile,
941 1155 show=show)
942 1156
943 1157 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
944 1158
945 1159 if ymin == None: ymin = numpy.nanmin(y)
946 1160 if ymax == None: ymax = numpy.nanmax(y)
947 1161 if zmin == None: zmin = numpy.nanmin(zRange)
948 1162 if zmax == None: zmax = numpy.nanmax(zRange)
949 1163
950 1164 if SNR:
951 1165 if SNRmin == None: SNRmin = numpy.nanmin(SNRdB)
952 1166 if SNRmax == None: SNRmax = numpy.nanmax(SNRdB)
953 1167
954 1168 self.FTP_WEI = ftp_wei
955 1169 self.EXP_CODE = exp_code
956 1170 self.SUB_EXP_CODE = sub_exp_code
957 1171 self.PLOT_POS = plot_pos
958 1172
959 1173 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
960 1174 self.isConfig = True
961 1175 self.figfile = figfile
962 1176
963 1177 self.setWinTitle(title)
964 1178
965 1179 if ((self.xmax - x[1]) < (x[1]-x[0])):
966 1180 x[1] = self.xmax
967 1181
968 1182 for i in range(nchan):
969 1183
970 1184 if (SNR and not onlySNR): j = 2*i
971 1185 else: j = i
972 1186
973 1187 j = nGraphsByChannel*i
974 1188
975 1189 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
976 1190 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
977 1191
978 1192 if not onlySNR:
979 1193 axes = self.axesList[j*self.__nsubplots]
980 1194 z1 = z[i,:].reshape((1,-1))
981 1195 axes.pcolorbuffer(x, y, z1,
982 1196 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
983 1197 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap,
984 1198 ticksize=9, cblabel=zlabel, cbsize="1%")
985 1199
986 1200 if DOP:
987 1201 title = "%s Channel %d: %s" %(parameterName, channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
988 1202
989 1203 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
990 1204 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
991 1205 axes = self.axesList[j]
992 1206 z1 = z[i,:].reshape((1,-1))
993 1207 axes.pcolorbuffer(x, y, z1,
994 1208 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
995 1209 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap,
996 1210 ticksize=9, cblabel=zlabel, cbsize="1%")
997 1211
998 1212 if SNR:
999 1213 title = "Channel %d Signal Noise Ratio (SNR): %s" %(channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1000 1214 axes = self.axesList[(j)*self.__nsubplots]
1001 1215 if not onlySNR:
1002 1216 axes = self.axesList[(j + 1)*self.__nsubplots]
1003 1217
1004 1218 axes = self.axesList[(j + nGraphsByChannel-1)]
1005 1219
1006 1220 z1 = SNRdB[i,:].reshape((1,-1))
1007 1221 axes.pcolorbuffer(x, y, z1,
1008 1222 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1009 1223 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap="jet",
1010 1224 ticksize=9, cblabel=zlabel, cbsize="1%")
1011 1225
1012 1226
1013 1227
1014 1228 self.draw()
1015 1229
1016 1230 if x[1] >= self.axesList[0].xmax:
1017 1231 self.counter_imagwr = wr_period
1018 1232 self.isConfig = False
1019 1233 self.figfile = None
1020 1234
1021 1235 self.save(figpath=figpath,
1022 1236 figfile=figfile,
1023 1237 save=save,
1024 1238 ftp=ftp,
1025 1239 wr_period=wr_period,
1026 1240 thisDatetime=thisDatetime,
1027 1241 update_figfile=False)
1028 1242
1029 1243 class SpectralFittingPlot(Figure):
1030 1244
1031 1245 __isConfig = None
1032 1246 __nsubplots = None
1033 1247
1034 1248 WIDTHPROF = None
1035 1249 HEIGHTPROF = None
1036 1250 PREFIX = 'prm'
1037 1251
1038 1252
1039 1253 N = None
1040 1254 ippSeconds = None
1041 1255
1042 1256 def __init__(self, **kwargs):
1043 1257 Figure.__init__(self, **kwargs)
1044 1258 self.isConfig = False
1045 1259 self.__nsubplots = 1
1046 1260
1047 1261 self.PLOT_CODE = SPECFIT_CODE
1048 1262
1049 1263 self.WIDTH = 450
1050 1264 self.HEIGHT = 250
1051 1265 self.WIDTHPROF = 0
1052 1266 self.HEIGHTPROF = 0
1053 1267
1054 1268 def getSubplots(self):
1055 1269
1056 1270 ncol = int(numpy.sqrt(self.nplots)+0.9)
1057 1271 nrow = int(self.nplots*1./ncol + 0.9)
1058 1272
1059 1273 return nrow, ncol
1060 1274
1061 1275 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
1062 1276
1063 1277 showprofile = False
1064 1278 self.__showprofile = showprofile
1065 1279 self.nplots = nplots
1066 1280
1067 1281 ncolspan = 5
1068 1282 colspan = 4
1069 1283 if showprofile:
1070 1284 ncolspan = 5
1071 1285 colspan = 4
1072 1286 self.__nsubplots = 2
1073 1287
1074 1288 self.createFigure(id = id,
1075 1289 wintitle = wintitle,
1076 1290 widthplot = self.WIDTH + self.WIDTHPROF,
1077 1291 heightplot = self.HEIGHT + self.HEIGHTPROF,
1078 1292 show=show)
1079 1293
1080 1294 nrow, ncol = self.getSubplots()
1081 1295
1082 1296 counter = 0
1083 1297 for y in range(nrow):
1084 1298 for x in range(ncol):
1085 1299
1086 1300 if counter >= self.nplots:
1087 1301 break
1088 1302
1089 1303 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1090 1304
1091 1305 if showprofile:
1092 1306 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
1093 1307
1094 1308 counter += 1
1095 1309
1096 1310 def run(self, dataOut, id, cutHeight=None, fit=False, wintitle="", channelList=None, showprofile=True,
1097 1311 xmin=None, xmax=None, ymin=None, ymax=None,
1098 1312 save=False, figpath='./', figfile=None, show=True):
1099 1313
1100 1314 """
1101 1315
1102 1316 Input:
1103 1317 dataOut :
1104 1318 id :
1105 1319 wintitle :
1106 1320 channelList :
1107 1321 showProfile :
1108 1322 xmin : None,
1109 1323 xmax : None,
1110 1324 zmin : None,
1111 1325 zmax : None
1112 1326 """
1113 1327
1114 1328 if cutHeight==None:
1115 1329 h=270
1116 1330 heightindex = numpy.abs(cutHeight - dataOut.heightList).argmin()
1117 1331 cutHeight = dataOut.heightList[heightindex]
1118 1332
1119 1333 factor = dataOut.normFactor
1120 1334 x = dataOut.abscissaList[:-1]
1121 1335 #y = dataOut.getHeiRange()
1122 1336
1123 1337 z = dataOut.data_pre[:,:,heightindex]/factor
1124 1338 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1125 1339 avg = numpy.average(z, axis=1)
1126 1340 listChannels = z.shape[0]
1127 1341
1128 1342 #Reconstruct Function
1129 1343 if fit==True:
1130 1344 groupArray = dataOut.groupList
1131 1345 listChannels = groupArray.reshape((groupArray.size))
1132 1346 listChannels.sort()
1133 1347 spcFitLine = numpy.zeros(z.shape)
1134 1348 constants = dataOut.constants
1135 1349
1136 1350 nGroups = groupArray.shape[0]
1137 1351 nChannels = groupArray.shape[1]
1138 1352 nProfiles = z.shape[1]
1139 1353
1140 1354 for f in range(nGroups):
1141 1355 groupChann = groupArray[f,:]
1142 1356 p = dataOut.data_param[f,:,heightindex]
1143 1357 # p = numpy.array([ 89.343967,0.14036615,0.17086219,18.89835291,1.58388365,1.55099167])
1144 1358 fitLineAux = dataOut.library.modelFunction(p, constants)*nProfiles
1145 1359 fitLineAux = fitLineAux.reshape((nChannels,nProfiles))
1146 1360 spcFitLine[groupChann,:] = fitLineAux
1147 1361 # spcFitLine = spcFitLine/factor
1148 1362
1149 1363 z = z[listChannels,:]
1150 1364 spcFitLine = spcFitLine[listChannels,:]
1151 1365 spcFitLinedB = 10*numpy.log10(spcFitLine)
1152 1366
1153 1367 zdB = 10*numpy.log10(z)
1154 1368 #thisDatetime = dataOut.datatime
1155 1369 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1156 1370 title = wintitle + " Doppler Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1157 1371 xlabel = "Velocity (m/s)"
1158 1372 ylabel = "Spectrum"
1159 1373
1160 1374 if not self.isConfig:
1161 1375
1162 1376 nplots = listChannels.size
1163 1377
1164 1378 self.setup(id=id,
1165 1379 nplots=nplots,
1166 1380 wintitle=wintitle,
1167 1381 showprofile=showprofile,
1168 1382 show=show)
1169 1383
1170 1384 if xmin == None: xmin = numpy.nanmin(x)
1171 1385 if xmax == None: xmax = numpy.nanmax(x)
1172 1386 if ymin == None: ymin = numpy.nanmin(zdB)
1173 1387 if ymax == None: ymax = numpy.nanmax(zdB)+2
1174 1388
1175 1389 self.isConfig = True
1176 1390
1177 1391 self.setWinTitle(title)
1178 1392 for i in range(self.nplots):
1179 1393 # title = "Channel %d: %4.2fdB" %(dataOut.channelList[i]+1, noisedB[i])
1180 1394 title = "Height %4.1f km\nChannel %d:" %(cutHeight, listChannels[i])
1181 1395 axes = self.axesList[i*self.__nsubplots]
1182 1396 if fit == False:
1183 1397 axes.pline(x, zdB[i,:],
1184 1398 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1185 1399 xlabel=xlabel, ylabel=ylabel, title=title
1186 1400 )
1187 1401 if fit == True:
1188 1402 fitline=spcFitLinedB[i,:]
1189 1403 y=numpy.vstack([zdB[i,:],fitline] )
1190 1404 legendlabels=['Data','Fitting']
1191 1405 axes.pmultilineyaxis(x, y,
1192 1406 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1193 1407 xlabel=xlabel, ylabel=ylabel, title=title,
1194 1408 legendlabels=legendlabels, marker=None,
1195 1409 linestyle='solid', grid='both')
1196 1410
1197 1411 self.draw()
1198 1412
1199 1413 self.save(figpath=figpath,
1200 1414 figfile=figfile,
1201 1415 save=save,
1202 1416 ftp=ftp,
1203 1417 wr_period=wr_period,
1204 1418 thisDatetime=thisDatetime)
1205 1419
1206 1420
1207 1421 class EWDriftsPlot(Figure):
1208 1422
1209 1423 __isConfig = None
1210 1424 __nsubplots = None
1211 1425
1212 1426 WIDTHPROF = None
1213 1427 HEIGHTPROF = None
1214 1428 PREFIX = 'drift'
1215 1429
1216 1430 def __init__(self, **kwargs):
1217 1431 Figure.__init__(self, **kwargs)
1218 1432 self.timerange = 2*60*60
1219 1433 self.isConfig = False
1220 1434 self.__nsubplots = 1
1221 1435
1222 1436 self.WIDTH = 800
1223 1437 self.HEIGHT = 150
1224 1438 self.WIDTHPROF = 120
1225 1439 self.HEIGHTPROF = 0
1226 1440 self.counter_imagwr = 0
1227 1441
1228 1442 self.PLOT_CODE = EWDRIFT_CODE
1229 1443
1230 1444 self.FTP_WEI = None
1231 1445 self.EXP_CODE = None
1232 1446 self.SUB_EXP_CODE = None
1233 1447 self.PLOT_POS = None
1234 1448 self.tmin = None
1235 1449 self.tmax = None
1236 1450
1237 1451 self.xmin = None
1238 1452 self.xmax = None
1239 1453
1240 1454 self.figfile = None
1241 1455
1242 1456 def getSubplots(self):
1243 1457
1244 1458 ncol = 1
1245 1459 nrow = self.nplots
1246 1460
1247 1461 return nrow, ncol
1248 1462
1249 1463 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1250 1464
1251 1465 self.__showprofile = showprofile
1252 1466 self.nplots = nplots
1253 1467
1254 1468 ncolspan = 1
1255 1469 colspan = 1
1256 1470
1257 1471 self.createFigure(id = id,
1258 1472 wintitle = wintitle,
1259 1473 widthplot = self.WIDTH + self.WIDTHPROF,
1260 1474 heightplot = self.HEIGHT + self.HEIGHTPROF,
1261 1475 show=show)
1262 1476
1263 1477 nrow, ncol = self.getSubplots()
1264 1478
1265 1479 counter = 0
1266 1480 for y in range(nrow):
1267 1481 if counter >= self.nplots:
1268 1482 break
1269 1483
1270 1484 self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1)
1271 1485 counter += 1
1272 1486
1273 1487 def run(self, dataOut, id, wintitle="", channelList=None,
1274 1488 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
1275 1489 zmaxVertical = None, zminVertical = None, zmaxZonal = None, zminZonal = None,
1276 1490 timerange=None, SNRthresh = -numpy.inf, SNRmin = None, SNRmax = None, SNR_1 = False,
1277 1491 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
1278 1492 server=None, folder=None, username=None, password=None,
1279 1493 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1280 1494 """
1281 1495
1282 1496 Input:
1283 1497 dataOut :
1284 1498 id :
1285 1499 wintitle :
1286 1500 channelList :
1287 1501 showProfile :
1288 1502 xmin : None,
1289 1503 xmax : None,
1290 1504 ymin : None,
1291 1505 ymax : None,
1292 1506 zmin : None,
1293 1507 zmax : None
1294 1508 """
1295 1509
1296 1510 if timerange is not None:
1297 1511 self.timerange = timerange
1298 1512
1299 1513 tmin = None
1300 1514 tmax = None
1301 1515
1302 1516 x = dataOut.getTimeRange1(dataOut.outputInterval)
1303 1517 # y = dataOut.heightList
1304 1518 y = dataOut.heightList
1305 1519
1306 1520 z = dataOut.data_output
1307 1521 nplots = z.shape[0] #Number of wind dimensions estimated
1308 1522 nplotsw = nplots
1309 1523
1310 1524 #If there is a SNR function defined
1311 1525 if dataOut.data_SNR is not None:
1312 1526 nplots += 1
1313 1527 SNR = dataOut.data_SNR
1314 1528
1315 1529 if SNR_1:
1316 1530 SNR += 1
1317 1531
1318 1532 SNRavg = numpy.average(SNR, axis=0)
1319 1533
1320 1534 SNRdB = 10*numpy.log10(SNR)
1321 1535 SNRavgdB = 10*numpy.log10(SNRavg)
1322 1536
1323 1537 ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0]
1324 1538
1325 1539 for i in range(nplotsw):
1326 1540 z[i,ind] = numpy.nan
1327 1541
1328 1542
1329 1543 showprofile = False
1330 1544 # thisDatetime = dataOut.datatime
1331 1545 thisDatetime = datetime.datetime.utcfromtimestamp(x[1])
1332 1546 title = wintitle + " EW Drifts"
1333 1547 xlabel = ""
1334 1548 ylabel = "Height (Km)"
1335 1549
1336 1550 if not self.isConfig:
1337 1551
1338 1552 self.setup(id=id,
1339 1553 nplots=nplots,
1340 1554 wintitle=wintitle,
1341 1555 showprofile=showprofile,
1342 1556 show=show)
1343 1557
1344 1558 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1345 1559
1346 1560 if ymin == None: ymin = numpy.nanmin(y)
1347 1561 if ymax == None: ymax = numpy.nanmax(y)
1348 1562
1349 1563 if zmaxZonal == None: zmaxZonal = numpy.nanmax(abs(z[0,:]))
1350 1564 if zminZonal == None: zminZonal = -zmaxZonal
1351 1565 if zmaxVertical == None: zmaxVertical = numpy.nanmax(abs(z[1,:]))
1352 1566 if zminVertical == None: zminVertical = -zmaxVertical
1353 1567
1354 1568 if dataOut.data_SNR is not None:
1355 1569 if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB)
1356 1570 if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB)
1357 1571
1358 1572 self.FTP_WEI = ftp_wei
1359 1573 self.EXP_CODE = exp_code
1360 1574 self.SUB_EXP_CODE = sub_exp_code
1361 1575 self.PLOT_POS = plot_pos
1362 1576
1363 1577 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1364 1578 self.isConfig = True
1365 1579
1366 1580
1367 1581 self.setWinTitle(title)
1368 1582
1369 1583 if ((self.xmax - x[1]) < (x[1]-x[0])):
1370 1584 x[1] = self.xmax
1371 1585
1372 1586 strWind = ['Zonal','Vertical']
1373 1587 strCb = 'Velocity (m/s)'
1374 1588 zmaxVector = [zmaxZonal, zmaxVertical]
1375 1589 zminVector = [zminZonal, zminVertical]
1376 1590
1377 1591 for i in range(nplotsw):
1378 1592
1379 1593 title = "%s Drifts: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1380 1594 axes = self.axesList[i*self.__nsubplots]
1381 1595
1382 1596 z1 = z[i,:].reshape((1,-1))
1383 1597
1384 1598 axes.pcolorbuffer(x, y, z1,
1385 1599 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i],
1386 1600 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1387 1601 ticksize=9, cblabel=strCb, cbsize="1%", colormap="RdBu_r")
1388 1602
1389 1603 if dataOut.data_SNR is not None:
1390 1604 i += 1
1391 1605 if SNR_1:
1392 1606 title = "Signal Noise Ratio + 1 (SNR+1): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1393 1607 else:
1394 1608 title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1395 1609 axes = self.axesList[i*self.__nsubplots]
1396 1610 SNRavgdB = SNRavgdB.reshape((1,-1))
1397 1611
1398 1612 axes.pcolorbuffer(x, y, SNRavgdB,
1399 1613 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1400 1614 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1401 1615 ticksize=9, cblabel='', cbsize="1%", colormap="jet")
1402 1616
1403 1617 self.draw()
1404 1618
1405 1619 if x[1] >= self.axesList[0].xmax:
1406 1620 self.counter_imagwr = wr_period
1407 1621 self.isConfig = False
1408 1622 self.figfile = None
1409 1623
1410 1624
1411 1625
1412 1626
1413 1627 class PhasePlot(Figure):
1414 1628
1415 1629 __isConfig = None
1416 1630 __nsubplots = None
1417 1631
1418 1632 PREFIX = 'mphase'
1419 1633
1420 1634 def __init__(self, **kwargs):
1421 1635 Figure.__init__(self, **kwargs)
1422 1636 self.timerange = 24*60*60
1423 1637 self.isConfig = False
1424 1638 self.__nsubplots = 1
1425 1639 self.counter_imagwr = 0
1426 1640 self.WIDTH = 600
1427 1641 self.HEIGHT = 300
1428 1642 self.WIDTHPROF = 120
1429 1643 self.HEIGHTPROF = 0
1430 1644 self.xdata = None
1431 1645 self.ydata = None
1432 1646
1433 1647 self.PLOT_CODE = MPHASE_CODE
1434 1648
1435 1649 self.FTP_WEI = None
1436 1650 self.EXP_CODE = None
1437 1651 self.SUB_EXP_CODE = None
1438 1652 self.PLOT_POS = None
1439 1653
1440 1654
1441 1655 self.filename_phase = None
1442 1656
1443 1657 self.figfile = None
1444 1658
1445 1659 def getSubplots(self):
1446 1660
1447 1661 ncol = 1
1448 1662 nrow = 1
1449 1663
1450 1664 return nrow, ncol
1451 1665
1452 1666 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1453 1667
1454 1668 self.__showprofile = showprofile
1455 1669 self.nplots = nplots
1456 1670
1457 1671 ncolspan = 7
1458 1672 colspan = 6
1459 1673 self.__nsubplots = 2
1460 1674
1461 1675 self.createFigure(id = id,
1462 1676 wintitle = wintitle,
1463 1677 widthplot = self.WIDTH+self.WIDTHPROF,
1464 1678 heightplot = self.HEIGHT+self.HEIGHTPROF,
1465 1679 show=show)
1466 1680
1467 1681 nrow, ncol = self.getSubplots()
1468 1682
1469 1683 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1470 1684
1471 1685
1472 1686 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1473 1687 xmin=None, xmax=None, ymin=None, ymax=None,
1474 1688 timerange=None,
1475 1689 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1476 1690 server=None, folder=None, username=None, password=None,
1477 1691 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1478 1692
1479 1693
1480 1694 tmin = None
1481 1695 tmax = None
1482 1696 x = dataOut.getTimeRange1(dataOut.outputInterval)
1483 1697 y = dataOut.getHeiRange()
1484 1698
1485 1699
1486 1700 #thisDatetime = dataOut.datatime
1487 1701 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
1488 1702 title = wintitle + " Phase of Beacon Signal" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1489 1703 xlabel = "Local Time"
1490 1704 ylabel = "Phase"
1491 1705
1492 1706
1493 1707 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1494 1708 phase_beacon = dataOut.data_output
1495 1709 update_figfile = False
1496 1710
1497 1711 if not self.isConfig:
1498 1712
1499 1713 self.nplots = phase_beacon.size
1500 1714
1501 1715 self.setup(id=id,
1502 1716 nplots=self.nplots,
1503 1717 wintitle=wintitle,
1504 1718 showprofile=showprofile,
1505 1719 show=show)
1506 1720
1507 1721 if timerange is not None:
1508 1722 self.timerange = timerange
1509 1723
1510 1724 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1511 1725
1512 1726 if ymin == None: ymin = numpy.nanmin(phase_beacon) - 10.0
1513 1727 if ymax == None: ymax = numpy.nanmax(phase_beacon) + 10.0
1514 1728
1515 1729 self.FTP_WEI = ftp_wei
1516 1730 self.EXP_CODE = exp_code
1517 1731 self.SUB_EXP_CODE = sub_exp_code
1518 1732 self.PLOT_POS = plot_pos
1519 1733
1520 1734 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1521 1735 self.isConfig = True
1522 1736 self.figfile = figfile
1523 1737 self.xdata = numpy.array([])
1524 1738 self.ydata = numpy.array([])
1525 1739
1526 1740 #open file beacon phase
1527 1741 path = '%s%03d' %(self.PREFIX, self.id)
1528 1742 beacon_file = os.path.join(path,'%s.txt'%self.name)
1529 1743 self.filename_phase = os.path.join(figpath,beacon_file)
1530 1744 update_figfile = True
1531 1745
1532 1746
1533 1747 #store data beacon phase
1534 1748 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1535 1749
1536 1750 self.setWinTitle(title)
1537 1751
1538 1752
1539 1753 title = "Phase Offset %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1540 1754
1541 1755 legendlabels = ["phase %d"%(chan) for chan in numpy.arange(self.nplots)]
1542 1756
1543 1757 axes = self.axesList[0]
1544 1758
1545 1759 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1546 1760
1547 1761 if len(self.ydata)==0:
1548 1762 self.ydata = phase_beacon.reshape(-1,1)
1549 1763 else:
1550 1764 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1551 1765
1552 1766
1553 1767 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1554 1768 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1555 1769 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1556 1770 XAxisAsTime=True, grid='both'
1557 1771 )
1558 1772
1559 1773 self.draw()
1560 1774
1561 1775 self.save(figpath=figpath,
1562 1776 figfile=figfile,
1563 1777 save=save,
1564 1778 ftp=ftp,
1565 1779 wr_period=wr_period,
1566 1780 thisDatetime=thisDatetime,
1567 1781 update_figfile=update_figfile)
1568 1782
1569 1783 if dataOut.ltctime + dataOut.outputInterval >= self.xmax:
1570 1784 self.counter_imagwr = wr_period
1571 1785 self.isConfig = False
1572 1786 update_figfile = True
1573 1787
1574 1788
1575 1789
1576 1790 class NSMeteorDetection1Plot(Figure):
1577 1791
1578 1792 isConfig = None
1579 1793 __nsubplots = None
1580 1794
1581 1795 WIDTHPROF = None
1582 1796 HEIGHTPROF = None
1583 1797 PREFIX = 'nsm'
1584 1798
1585 1799 zminList = None
1586 1800 zmaxList = None
1587 1801 cmapList = None
1588 1802 titleList = None
1589 1803 nPairs = None
1590 1804 nChannels = None
1591 1805 nParam = None
1592 1806
1593 1807 def __init__(self, **kwargs):
1594 1808 Figure.__init__(self, **kwargs)
1595 1809 self.isConfig = False
1596 1810 self.__nsubplots = 1
1597 1811
1598 1812 self.WIDTH = 750
1599 1813 self.HEIGHT = 250
1600 1814 self.WIDTHPROF = 120
1601 1815 self.HEIGHTPROF = 0
1602 1816 self.counter_imagwr = 0
1603 1817
1604 1818 self.PLOT_CODE = SPEC_CODE
1605 1819
1606 1820 self.FTP_WEI = None
1607 1821 self.EXP_CODE = None
1608 1822 self.SUB_EXP_CODE = None
1609 1823 self.PLOT_POS = None
1610 1824
1611 1825 self.__xfilter_ena = False
1612 1826 self.__yfilter_ena = False
1613 1827
1614 1828 def getSubplots(self):
1615 1829
1616 1830 ncol = 3
1617 1831 nrow = int(numpy.ceil(self.nplots/3.0))
1618 1832
1619 1833 return nrow, ncol
1620 1834
1621 1835 def setup(self, id, nplots, wintitle, show=True):
1622 1836
1623 1837 self.nplots = nplots
1624 1838
1625 1839 ncolspan = 1
1626 1840 colspan = 1
1627 1841
1628 1842 self.createFigure(id = id,
1629 1843 wintitle = wintitle,
1630 1844 widthplot = self.WIDTH + self.WIDTHPROF,
1631 1845 heightplot = self.HEIGHT + self.HEIGHTPROF,
1632 1846 show=show)
1633 1847
1634 1848 nrow, ncol = self.getSubplots()
1635 1849
1636 1850 counter = 0
1637 1851 for y in range(nrow):
1638 1852 for x in range(ncol):
1639 1853
1640 1854 if counter >= self.nplots:
1641 1855 break
1642 1856
1643 1857 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1644 1858
1645 1859 counter += 1
1646 1860
1647 1861 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
1648 1862 xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None,
1649 1863 vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA',
1650 1864 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1651 1865 server=None, folder=None, username=None, password=None,
1652 1866 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
1653 1867 xaxis="frequency"):
1654 1868
1655 1869 """
1656 1870
1657 1871 Input:
1658 1872 dataOut :
1659 1873 id :
1660 1874 wintitle :
1661 1875 channelList :
1662 1876 showProfile :
1663 1877 xmin : None,
1664 1878 xmax : None,
1665 1879 ymin : None,
1666 1880 ymax : None,
1667 1881 zmin : None,
1668 1882 zmax : None
1669 1883 """
1670 1884 #SEPARAR EN DOS PLOTS
1671 1885 nParam = dataOut.data_param.shape[1] - 3
1672 1886
1673 1887 utctime = dataOut.data_param[0,0]
1674 1888 tmet = dataOut.data_param[:,1].astype(int)
1675 1889 hmet = dataOut.data_param[:,2].astype(int)
1676 1890
1677 1891 x = dataOut.abscissaList
1678 1892 y = dataOut.heightList
1679 1893
1680 1894 z = numpy.zeros((nParam, y.size, x.size - 1))
1681 1895 z[:,:] = numpy.nan
1682 1896 z[:,hmet,tmet] = dataOut.data_param[:,3:].T
1683 1897 z[0,:,:] = 10*numpy.log10(z[0,:,:])
1684 1898
1685 1899 xlabel = "Time (s)"
1686 1900 ylabel = "Range (km)"
1687 1901
1688 1902 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
1689 1903
1690 1904 if not self.isConfig:
1691 1905
1692 1906 nplots = nParam
1693 1907
1694 1908 self.setup(id=id,
1695 1909 nplots=nplots,
1696 1910 wintitle=wintitle,
1697 1911 show=show)
1698 1912
1699 1913 if xmin is None: xmin = numpy.nanmin(x)
1700 1914 if xmax is None: xmax = numpy.nanmax(x)
1701 1915 if ymin is None: ymin = numpy.nanmin(y)
1702 1916 if ymax is None: ymax = numpy.nanmax(y)
1703 1917 if SNRmin is None: SNRmin = numpy.nanmin(z[0,:])
1704 1918 if SNRmax is None: SNRmax = numpy.nanmax(z[0,:])
1705 1919 if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:]))
1706 1920 if vmin is None: vmin = -vmax
1707 1921 if wmin is None: wmin = 0
1708 1922 if wmax is None: wmax = 50
1709 1923
1710 1924 pairsList = dataOut.groupList
1711 1925 self.nPairs = len(dataOut.groupList)
1712 1926
1713 1927 zminList = [SNRmin, vmin, cmin] + [pmin]*self.nPairs
1714 1928 zmaxList = [SNRmax, vmax, cmax] + [pmax]*self.nPairs
1715 1929 titleList = ["SNR","Radial Velocity","Coherence"]
1716 1930 cmapList = ["jet","RdBu_r","jet"]
1717 1931
1718 1932 for i in range(self.nPairs):
1719 1933 strAux1 = "Phase Difference "+ str(pairsList[i][0]) + str(pairsList[i][1])
1720 1934 titleList = titleList + [strAux1]
1721 1935 cmapList = cmapList + ["RdBu_r"]
1722 1936
1723 1937 self.zminList = zminList
1724 1938 self.zmaxList = zmaxList
1725 1939 self.cmapList = cmapList
1726 1940 self.titleList = titleList
1727 1941
1728 1942 self.FTP_WEI = ftp_wei
1729 1943 self.EXP_CODE = exp_code
1730 1944 self.SUB_EXP_CODE = sub_exp_code
1731 1945 self.PLOT_POS = plot_pos
1732 1946
1733 1947 self.isConfig = True
1734 1948
1735 1949 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
1736 1950
1737 1951 for i in range(nParam):
1738 1952 title = self.titleList[i] + ": " +str_datetime
1739 1953 axes = self.axesList[i]
1740 1954 axes.pcolor(x, y, z[i,:].T,
1741 1955 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i],
1742 1956 xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='')
1743 1957 self.draw()
1744 1958
1745 1959 if figfile == None:
1746 1960 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
1747 1961 name = str_datetime
1748 1962 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1749 1963 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
1750 1964 figfile = self.getFilename(name)
1751 1965
1752 1966 self.save(figpath=figpath,
1753 1967 figfile=figfile,
1754 1968 save=save,
1755 1969 ftp=ftp,
1756 1970 wr_period=wr_period,
1757 1971 thisDatetime=thisDatetime)
1758 1972
1759 1973
1760 1974 class NSMeteorDetection2Plot(Figure):
1761 1975
1762 1976 isConfig = None
1763 1977 __nsubplots = None
1764 1978
1765 1979 WIDTHPROF = None
1766 1980 HEIGHTPROF = None
1767 1981 PREFIX = 'nsm'
1768 1982
1769 1983 zminList = None
1770 1984 zmaxList = None
1771 1985 cmapList = None
1772 1986 titleList = None
1773 1987 nPairs = None
1774 1988 nChannels = None
1775 1989 nParam = None
1776 1990
1777 1991 def __init__(self, **kwargs):
1778 1992 Figure.__init__(self, **kwargs)
1779 1993 self.isConfig = False
1780 1994 self.__nsubplots = 1
1781 1995
1782 1996 self.WIDTH = 750
1783 1997 self.HEIGHT = 250
1784 1998 self.WIDTHPROF = 120
1785 1999 self.HEIGHTPROF = 0
1786 2000 self.counter_imagwr = 0
1787 2001
1788 2002 self.PLOT_CODE = SPEC_CODE
1789 2003
1790 2004 self.FTP_WEI = None
1791 2005 self.EXP_CODE = None
1792 2006 self.SUB_EXP_CODE = None
1793 2007 self.PLOT_POS = None
1794 2008
1795 2009 self.__xfilter_ena = False
1796 2010 self.__yfilter_ena = False
1797 2011
1798 2012 def getSubplots(self):
1799 2013
1800 2014 ncol = 3
1801 2015 nrow = int(numpy.ceil(self.nplots/3.0))
1802 2016
1803 2017 return nrow, ncol
1804 2018
1805 2019 def setup(self, id, nplots, wintitle, show=True):
1806 2020
1807 2021 self.nplots = nplots
1808 2022
1809 2023 ncolspan = 1
1810 2024 colspan = 1
1811 2025
1812 2026 self.createFigure(id = id,
1813 2027 wintitle = wintitle,
1814 2028 widthplot = self.WIDTH + self.WIDTHPROF,
1815 2029 heightplot = self.HEIGHT + self.HEIGHTPROF,
1816 2030 show=show)
1817 2031
1818 2032 nrow, ncol = self.getSubplots()
1819 2033
1820 2034 counter = 0
1821 2035 for y in range(nrow):
1822 2036 for x in range(ncol):
1823 2037
1824 2038 if counter >= self.nplots:
1825 2039 break
1826 2040
1827 2041 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1828 2042
1829 2043 counter += 1
1830 2044
1831 2045 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
1832 2046 xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None,
1833 2047 vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA',
1834 2048 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1835 2049 server=None, folder=None, username=None, password=None,
1836 2050 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
1837 2051 xaxis="frequency"):
1838 2052
1839 2053 """
1840 2054
1841 2055 Input:
1842 2056 dataOut :
1843 2057 id :
1844 2058 wintitle :
1845 2059 channelList :
1846 2060 showProfile :
1847 2061 xmin : None,
1848 2062 xmax : None,
1849 2063 ymin : None,
1850 2064 ymax : None,
1851 2065 zmin : None,
1852 2066 zmax : None
1853 2067 """
1854 2068 #Rebuild matrix
1855 2069 utctime = dataOut.data_param[0,0]
1856 2070 cmet = dataOut.data_param[:,1].astype(int)
1857 2071 tmet = dataOut.data_param[:,2].astype(int)
1858 2072 hmet = dataOut.data_param[:,3].astype(int)
1859 2073
1860 2074 nParam = 3
1861 2075 nChan = len(dataOut.groupList)
1862 2076 x = dataOut.abscissaList
1863 2077 y = dataOut.heightList
1864 2078
1865 2079 z = numpy.full((nChan, nParam, y.size, x.size - 1),numpy.nan)
1866 2080 z[cmet,:,hmet,tmet] = dataOut.data_param[:,4:]
1867 2081 z[:,0,:,:] = 10*numpy.log10(z[:,0,:,:]) #logarithmic scale
1868 2082 z = numpy.reshape(z, (nChan*nParam, y.size, x.size-1))
1869 2083
1870 2084 xlabel = "Time (s)"
1871 2085 ylabel = "Range (km)"
1872 2086
1873 2087 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
1874 2088
1875 2089 if not self.isConfig:
1876 2090
1877 2091 nplots = nParam*nChan
1878 2092
1879 2093 self.setup(id=id,
1880 2094 nplots=nplots,
1881 2095 wintitle=wintitle,
1882 2096 show=show)
1883 2097
1884 2098 if xmin is None: xmin = numpy.nanmin(x)
1885 2099 if xmax is None: xmax = numpy.nanmax(x)
1886 2100 if ymin is None: ymin = numpy.nanmin(y)
1887 2101 if ymax is None: ymax = numpy.nanmax(y)
1888 2102 if SNRmin is None: SNRmin = numpy.nanmin(z[0,:])
1889 2103 if SNRmax is None: SNRmax = numpy.nanmax(z[0,:])
1890 2104 if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:]))
1891 2105 if vmin is None: vmin = -vmax
1892 2106 if wmin is None: wmin = 0
1893 2107 if wmax is None: wmax = 50
1894 2108
1895 2109 self.nChannels = nChan
1896 2110
1897 2111 zminList = []
1898 2112 zmaxList = []
1899 2113 titleList = []
1900 2114 cmapList = []
1901 2115 for i in range(self.nChannels):
1902 2116 strAux1 = "SNR Channel "+ str(i)
1903 2117 strAux2 = "Radial Velocity Channel "+ str(i)
1904 2118 strAux3 = "Spectral Width Channel "+ str(i)
1905 2119
1906 2120 titleList = titleList + [strAux1,strAux2,strAux3]
1907 2121 cmapList = cmapList + ["jet","RdBu_r","jet"]
1908 2122 zminList = zminList + [SNRmin,vmin,wmin]
1909 2123 zmaxList = zmaxList + [SNRmax,vmax,wmax]
1910 2124
1911 2125 self.zminList = zminList
1912 2126 self.zmaxList = zmaxList
1913 2127 self.cmapList = cmapList
1914 2128 self.titleList = titleList
1915 2129
1916 2130 self.FTP_WEI = ftp_wei
1917 2131 self.EXP_CODE = exp_code
1918 2132 self.SUB_EXP_CODE = sub_exp_code
1919 2133 self.PLOT_POS = plot_pos
1920 2134
1921 2135 self.isConfig = True
1922 2136
1923 2137 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
1924 2138
1925 2139 for i in range(self.nplots):
1926 2140 title = self.titleList[i] + ": " +str_datetime
1927 2141 axes = self.axesList[i]
1928 2142 axes.pcolor(x, y, z[i,:].T,
1929 2143 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i],
1930 2144 xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='')
1931 2145 self.draw()
1932 2146
1933 2147 if figfile == None:
1934 2148 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
1935 2149 name = str_datetime
1936 2150 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1937 2151 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
1938 2152 figfile = self.getFilename(name)
1939 2153
1940 2154 self.save(figpath=figpath,
1941 2155 figfile=figfile,
1942 2156 save=save,
1943 2157 ftp=ftp,
1944 2158 wr_period=wr_period,
1945 2159 thisDatetime=thisDatetime)
1 NO CONTENT: modified file, binary diff hidden
@@ -1,1541 +1,1541
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import datetime
8 8 import numpy
9 9
10 10 from figure import Figure, isRealtime, isTimeInHourRange
11 11 from plotting_codes import *
12 12
13 13 class SpectraPlot(Figure):
14 14
15 15 isConfig = None
16 16 __nsubplots = None
17 17
18 18 WIDTHPROF = None
19 19 HEIGHTPROF = None
20 20 PREFIX = 'spc'
21 21
22 22 def __init__(self, **kwargs):
23 23 Figure.__init__(self, **kwargs)
24 24 self.isConfig = False
25 25 self.__nsubplots = 1
26 26
27 27 self.WIDTH = 250
28 28 self.HEIGHT = 250
29 29 self.WIDTHPROF = 120
30 30 self.HEIGHTPROF = 0
31 31 self.counter_imagwr = 0
32 32
33 33 self.PLOT_CODE = SPEC_CODE
34 34
35 35 self.FTP_WEI = None
36 36 self.EXP_CODE = None
37 37 self.SUB_EXP_CODE = None
38 38 self.PLOT_POS = None
39 39
40 40 self.__xfilter_ena = False
41 41 self.__yfilter_ena = False
42 42
43 43 def getSubplots(self):
44 44
45 45 ncol = int(numpy.sqrt(self.nplots)+0.9)
46 46 nrow = int(self.nplots*1./ncol + 0.9)
47 47
48 48 return nrow, ncol
49 49
50 50 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
51 51
52 52 self.__showprofile = showprofile
53 53 self.nplots = nplots
54 54
55 55 ncolspan = 1
56 56 colspan = 1
57 57 if showprofile:
58 58 ncolspan = 3
59 59 colspan = 2
60 60 self.__nsubplots = 2
61 61
62 62 self.createFigure(id = id,
63 63 wintitle = wintitle,
64 64 widthplot = self.WIDTH + self.WIDTHPROF,
65 65 heightplot = self.HEIGHT + self.HEIGHTPROF,
66 66 show=show)
67 67
68 68 nrow, ncol = self.getSubplots()
69 69
70 70 counter = 0
71 71 for y in range(nrow):
72 72 for x in range(ncol):
73 73
74 74 if counter >= self.nplots:
75 75 break
76 76
77 77 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
78 78
79 79 if showprofile:
80 80 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
81 81
82 82 counter += 1
83 83
84 84 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
85 85 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
86 86 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
87 87 server=None, folder=None, username=None, password=None,
88 88 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
89 89 xaxis="frequency", colormap='jet', normFactor=None):
90 90
91 91 """
92 92
93 93 Input:
94 94 dataOut :
95 95 id :
96 96 wintitle :
97 97 channelList :
98 98 showProfile :
99 99 xmin : None,
100 100 xmax : None,
101 101 ymin : None,
102 102 ymax : None,
103 103 zmin : None,
104 104 zmax : None
105 105 """
106
107 colormap = kwargs.get('colormap','jet')
108
109 106 if realtime:
110 107 if not(isRealtime(utcdatatime = dataOut.utctime)):
111 108 print 'Skipping this plot function'
112 109 return
113 110
114 111 if channelList == None:
115 112 channelIndexList = dataOut.channelIndexList
116 113 else:
117 114 channelIndexList = []
118 115 for channel in channelList:
119 116 if channel not in dataOut.channelList:
120 117 raise ValueError, "Channel %d is not in dataOut.channelList" %channel
121 118 channelIndexList.append(dataOut.channelList.index(channel))
122 119
123 120 if normFactor is None:
124 121 factor = dataOut.normFactor
125 122 else:
126 123 factor = normFactor
127 124 if xaxis == "frequency":
128 125 x = dataOut.getFreqRange(1)/1000.
129 126 xlabel = "Frequency (kHz)"
130 127
131 128 elif xaxis == "time":
132 129 x = dataOut.getAcfRange(1)
133 130 xlabel = "Time (ms)"
134 131
135 132 else:
136 133 x = dataOut.getVelRange(1)
137 134 xlabel = "Velocity (m/s)"
138 135
139 136 ylabel = "Range (Km)"
140 137
141 138 y = dataOut.getHeiRange()
142 139
143 140 z = dataOut.data_spc/factor
144 141 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
145 142 zdB = 10*numpy.log10(z)
146 143
147 144 avg = numpy.average(z, axis=1)
148 145 avgdB = 10*numpy.log10(avg)
149 146
150 147 noise = dataOut.getNoise()/factor
151 148 noisedB = 10*numpy.log10(noise)
152 149
153 150 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
154 151 title = wintitle + " Spectra"
155 152 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
156 153 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
157 154
158 155 if not self.isConfig:
159 156
160 157 nplots = len(channelIndexList)
161 158
162 159 self.setup(id=id,
163 160 nplots=nplots,
164 161 wintitle=wintitle,
165 162 showprofile=showprofile,
166 163 show=show)
167 164
168 165 if xmin == None: xmin = numpy.nanmin(x)
169 166 if xmax == None: xmax = numpy.nanmax(x)
170 167 if ymin == None: ymin = numpy.nanmin(y)
171 168 if ymax == None: ymax = numpy.nanmax(y)
172 169 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
173 170 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
174 171
175 172 self.FTP_WEI = ftp_wei
176 173 self.EXP_CODE = exp_code
177 174 self.SUB_EXP_CODE = sub_exp_code
178 175 self.PLOT_POS = plot_pos
179 176
180 177 self.isConfig = True
181 178
182 179 self.setWinTitle(title)
183 180
184 181 for i in range(self.nplots):
185 182 index = channelIndexList[i]
186 183 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
187 184 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
188 185 if len(dataOut.beam.codeList) != 0:
189 186 title = "Ch%d:%4.2fdB,%2.2f,%2.2f:%s" %(dataOut.channelList[index], noisedB[index], dataOut.beam.azimuthList[index], dataOut.beam.zenithList[index], str_datetime)
190 187
191 188 axes = self.axesList[i*self.__nsubplots]
192 189 axes.pcolor(x, y, zdB[index,:,:],
193 190 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
194 191 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
195 192 ticksize=9, cblabel='')
196 193
197 194 if self.__showprofile:
198 195 axes = self.axesList[i*self.__nsubplots +1]
199 196 axes.pline(avgdB[index,:], y,
200 197 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
201 198 xlabel='dB', ylabel='', title='',
202 199 ytick_visible=False,
203 200 grid='x')
204 201
205 202 noiseline = numpy.repeat(noisedB[index], len(y))
206 203 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
207 204
208 205 self.draw()
209 206
210 207 if figfile == None:
211 208 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
212 209 name = str_datetime
213 210 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
214 211 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
215 212 figfile = self.getFilename(name)
216 213
217 214 self.save(figpath=figpath,
218 215 figfile=figfile,
219 216 save=save,
220 217 ftp=ftp,
221 218 wr_period=wr_period,
222 219 thisDatetime=thisDatetime)
223 220
224 221 class CrossSpectraPlot(Figure):
225 222
226 223 isConfig = None
227 224 __nsubplots = None
228 225
229 226 WIDTH = None
230 227 HEIGHT = None
231 228 WIDTHPROF = None
232 229 HEIGHTPROF = None
233 230 PREFIX = 'cspc'
234 231
235 232 def __init__(self, **kwargs):
236 233 Figure.__init__(self, **kwargs)
237 234 self.isConfig = False
238 235 self.__nsubplots = 4
239 236 self.counter_imagwr = 0
240 237 self.WIDTH = 250
241 238 self.HEIGHT = 250
242 239 self.WIDTHPROF = 0
243 240 self.HEIGHTPROF = 0
244 241
245 242 self.PLOT_CODE = CROSS_CODE
246 243 self.FTP_WEI = None
247 244 self.EXP_CODE = None
248 245 self.SUB_EXP_CODE = None
249 246 self.PLOT_POS = None
250 247
251 248 def getSubplots(self):
252 249
253 250 ncol = 4
254 251 nrow = self.nplots
255 252
256 253 return nrow, ncol
257 254
258 255 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
259 256
260 257 self.__showprofile = showprofile
261 258 self.nplots = nplots
262 259
263 260 ncolspan = 1
264 261 colspan = 1
265 262
266 263 self.createFigure(id = id,
267 264 wintitle = wintitle,
268 265 widthplot = self.WIDTH + self.WIDTHPROF,
269 266 heightplot = self.HEIGHT + self.HEIGHTPROF,
270 267 show=True)
271 268
272 269 nrow, ncol = self.getSubplots()
273 270
274 271 counter = 0
275 272 for y in range(nrow):
276 273 for x in range(ncol):
277 274 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
278 275
279 276 counter += 1
280 277
281 278 def run(self, dataOut, id, wintitle="", pairsList=None,
282 279 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
283 280 coh_min=None, coh_max=None, phase_min=None, phase_max=None,
284 281 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
285 282 power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
286 283 server=None, folder=None, username=None, password=None,
287 284 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None,
288 285 xaxis='frequency'):
289 286
290 287 """
291 288
292 289 Input:
293 290 dataOut :
294 291 id :
295 292 wintitle :
296 293 channelList :
297 294 showProfile :
298 295 xmin : None,
299 296 xmax : None,
300 297 ymin : None,
301 298 ymax : None,
302 299 zmin : None,
303 300 zmax : None
304 301 """
305 302
306 303 if pairsList == None:
307 304 pairsIndexList = dataOut.pairsIndexList
308 305 else:
309 306 pairsIndexList = []
310 307 for pair in pairsList:
311 308 if pair not in dataOut.pairsList:
312 309 raise ValueError, "Pair %s is not in dataOut.pairsList" %str(pair)
313 310 pairsIndexList.append(dataOut.pairsList.index(pair))
314 311
315 312 if not pairsIndexList:
316 313 return
317 314
318 315 if len(pairsIndexList) > 4:
319 316 pairsIndexList = pairsIndexList[0:4]
320 317
321 318 if normFactor is None:
322 319 factor = dataOut.normFactor
323 320 else:
324 321 factor = normFactor
325 322 x = dataOut.getVelRange(1)
326 323 y = dataOut.getHeiRange()
327 324 z = dataOut.data_spc[:,:,:]/factor
328 325 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
329 326
330 327 noise = dataOut.noise/factor
331 328
332 329 zdB = 10*numpy.log10(z)
333 330 noisedB = 10*numpy.log10(noise)
334 331
335 332 if coh_min == None:
336 333 coh_min = 0.0
337 334 if coh_max == None:
338 335 coh_max = 1.0
339 336
340 337 if phase_min == None:
341 338 phase_min = -180
342 339 if phase_max == None:
343 340 phase_max = 180
344 341
345 342 #thisDatetime = dataOut.datatime
346 343 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
347 344 title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
348 345 # xlabel = "Velocity (m/s)"
349 346 ylabel = "Range (Km)"
350 347
351 348 if xaxis == "frequency":
352 349 x = dataOut.getFreqRange(1)/1000.
353 350 xlabel = "Frequency (kHz)"
354 351
355 352 elif xaxis == "time":
356 353 x = dataOut.getAcfRange(1)
357 354 xlabel = "Time (ms)"
358 355
359 356 else:
360 357 x = dataOut.getVelRange(1)
361 358 xlabel = "Velocity (m/s)"
362 359
363 360 if not self.isConfig:
364 361
365 362 nplots = len(pairsIndexList)
366 363
367 364 self.setup(id=id,
368 365 nplots=nplots,
369 366 wintitle=wintitle,
370 367 showprofile=False,
371 368 show=show)
372 369
373 370 avg = numpy.abs(numpy.average(z, axis=1))
374 371 avgdB = 10*numpy.log10(avg)
375 372
376 373 if xmin == None: xmin = numpy.nanmin(x)
377 374 if xmax == None: xmax = numpy.nanmax(x)
378 375 if ymin == None: ymin = numpy.nanmin(y)
379 376 if ymax == None: ymax = numpy.nanmax(y)
380 377 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
381 378 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
382 379
383 380 self.FTP_WEI = ftp_wei
384 381 self.EXP_CODE = exp_code
385 382 self.SUB_EXP_CODE = sub_exp_code
386 383 self.PLOT_POS = plot_pos
387 384
388 385 self.isConfig = True
389 386
390 387 self.setWinTitle(title)
391 388
392 389 for i in range(self.nplots):
393 390 pair = dataOut.pairsList[pairsIndexList[i]]
394 391
395 392 chan_index0 = dataOut.channelList.index(pair[0])
396 393 chan_index1 = dataOut.channelList.index(pair[1])
397 394
398 395 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
399 396 title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[chan_index0], str_datetime)
400 397 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index0,:,:]/factor)
401 398 axes0 = self.axesList[i*self.__nsubplots]
402 399 axes0.pcolor(x, y, zdB,
403 400 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
404 401 xlabel=xlabel, ylabel=ylabel, title=title,
405 402 ticksize=9, colormap=power_cmap, cblabel='')
406 403
407 404 title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[chan_index1], str_datetime)
408 405 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index1,:,:]/factor)
409 406 axes0 = self.axesList[i*self.__nsubplots+1]
410 407 axes0.pcolor(x, y, zdB,
411 408 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
412 409 xlabel=xlabel, ylabel=ylabel, title=title,
413 410 ticksize=9, colormap=power_cmap, cblabel='')
414 411
415 412 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[chan_index0,:,:]*dataOut.data_spc[chan_index1,:,:])
416 413 coherence = numpy.abs(coherenceComplex)
417 414 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
418 415 phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi
419 416
420 417 title = "Coherence Ch%d * Ch%d" %(pair[0], pair[1])
421 418 axes0 = self.axesList[i*self.__nsubplots+2]
422 419 axes0.pcolor(x, y, coherence,
423 420 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=coh_min, zmax=coh_max,
424 421 xlabel=xlabel, ylabel=ylabel, title=title,
425 422 ticksize=9, colormap=coherence_cmap, cblabel='')
426 423
427 424 title = "Phase Ch%d * Ch%d" %(pair[0], pair[1])
428 425 axes0 = self.axesList[i*self.__nsubplots+3]
429 426 axes0.pcolor(x, y, phase,
430 427 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
431 428 xlabel=xlabel, ylabel=ylabel, title=title,
432 429 ticksize=9, colormap=phase_cmap, cblabel='')
433 430
434 431
435 432
436 433 self.draw()
437 434
438 435 self.save(figpath=figpath,
439 436 figfile=figfile,
440 437 save=save,
441 438 ftp=ftp,
442 439 wr_period=wr_period,
443 440 thisDatetime=thisDatetime)
444 441
445 442
446 443 class RTIPlot(Figure):
447 444
448 445 __isConfig = None
449 446 __nsubplots = None
450 447
451 448 WIDTHPROF = None
452 449 HEIGHTPROF = None
453 450 PREFIX = 'rti'
454 451
455 452 def __init__(self, **kwargs):
456 453
457 454 Figure.__init__(self, **kwargs)
458 455 self.timerange = None
459 456 self.isConfig = False
460 457 self.__nsubplots = 1
461 458
462 459 self.WIDTH = 800
463 460 self.HEIGHT = 180
464 461 self.WIDTHPROF = 120
465 462 self.HEIGHTPROF = 0
466 463 self.counter_imagwr = 0
467 464
468 465 self.PLOT_CODE = RTI_CODE
469 466
470 467 self.FTP_WEI = None
471 468 self.EXP_CODE = None
472 469 self.SUB_EXP_CODE = None
473 470 self.PLOT_POS = None
474 471 self.tmin = None
475 472 self.tmax = None
476 473
477 474 self.xmin = None
478 475 self.xmax = None
479 476
480 477 self.figfile = None
481 478
482 479 def getSubplots(self):
483 480
484 481 ncol = 1
485 482 nrow = self.nplots
486 483
487 484 return nrow, ncol
488 485
489 486 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
490 487
491 488 self.__showprofile = showprofile
492 489 self.nplots = nplots
493 490
494 491 ncolspan = 1
495 492 colspan = 1
496 493 if showprofile:
497 494 ncolspan = 7
498 495 colspan = 6
499 496 self.__nsubplots = 2
500 497
501 498 self.createFigure(id = id,
502 499 wintitle = wintitle,
503 500 widthplot = self.WIDTH + self.WIDTHPROF,
504 501 heightplot = self.HEIGHT + self.HEIGHTPROF,
505 502 show=show)
506 503
507 504 nrow, ncol = self.getSubplots()
508 505
509 506 counter = 0
510 507 for y in range(nrow):
511 508 for x in range(ncol):
512 509
513 510 if counter >= self.nplots:
514 511 break
515 512
516 513 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
517 514
518 515 if showprofile:
519 516 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
520 517
521 518 counter += 1
522 519
523 520 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
524 521 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
525 522 timerange=None, colormap='jet',
526 523 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
527 524 server=None, folder=None, username=None, password=None,
528 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None):
525 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None, HEIGHT=None):
529 526
530 527 """
531 528
532 529 Input:
533 530 dataOut :
534 531 id :
535 532 wintitle :
536 533 channelList :
537 534 showProfile :
538 535 xmin : None,
539 536 xmax : None,
540 537 ymin : None,
541 538 ymax : None,
542 539 zmin : None,
543 540 zmax : None
544 541 """
545 542
546 colormap = kwargs.get('colormap', 'jet')
543 #colormap = kwargs.get('colormap', 'jet')
544 if HEIGHT is not None:
545 self.HEIGHT = HEIGHT
546
547 547 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
548 548 return
549 549
550 550 if channelList == None:
551 551 channelIndexList = dataOut.channelIndexList
552 552 else:
553 553 channelIndexList = []
554 554 for channel in channelList:
555 555 if channel not in dataOut.channelList:
556 556 raise ValueError, "Channel %d is not in dataOut.channelList"
557 557 channelIndexList.append(dataOut.channelList.index(channel))
558 558
559 559 if normFactor is None:
560 560 factor = dataOut.normFactor
561 561 else:
562 562 factor = normFactor
563 563
564 564 # factor = dataOut.normFactor
565 565 x = dataOut.getTimeRange()
566 566 y = dataOut.getHeiRange()
567 567
568 568 z = dataOut.data_spc/factor
569 569 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
570 570 avg = numpy.average(z, axis=1)
571 571 avgdB = 10.*numpy.log10(avg)
572 572 # avgdB = dataOut.getPower()
573 573
574 574
575 575 thisDatetime = dataOut.datatime
576 576 # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
577 577 title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
578 578 xlabel = ""
579 579 ylabel = "Range (Km)"
580 580
581 581 update_figfile = False
582 582
583 583 if dataOut.ltctime >= self.xmax:
584 584 self.counter_imagwr = wr_period
585 585 self.isConfig = False
586 586 update_figfile = True
587 587
588 588 if not self.isConfig:
589 589
590 590 nplots = len(channelIndexList)
591 591
592 592 self.setup(id=id,
593 593 nplots=nplots,
594 594 wintitle=wintitle,
595 595 showprofile=showprofile,
596 596 show=show)
597 597
598 598 if timerange != None:
599 599 self.timerange = timerange
600 600
601 601 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
602 602
603 603 noise = dataOut.noise/factor
604 604 noisedB = 10*numpy.log10(noise)
605 605
606 606 if ymin == None: ymin = numpy.nanmin(y)
607 607 if ymax == None: ymax = numpy.nanmax(y)
608 608 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
609 609 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
610 610
611 611 self.FTP_WEI = ftp_wei
612 612 self.EXP_CODE = exp_code
613 613 self.SUB_EXP_CODE = sub_exp_code
614 614 self.PLOT_POS = plot_pos
615 615
616 616 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
617 617 self.isConfig = True
618 618 self.figfile = figfile
619 619 update_figfile = True
620 620
621 621 self.setWinTitle(title)
622 622
623 623 for i in range(self.nplots):
624 624 index = channelIndexList[i]
625 625 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
626 626 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
627 627 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
628 628 axes = self.axesList[i*self.__nsubplots]
629 629 zdB = avgdB[index].reshape((1,-1))
630 630 axes.pcolorbuffer(x, y, zdB,
631 631 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
632 632 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
633 633 ticksize=9, cblabel='', cbsize="1%", colormap=colormap)
634 634
635 635 if self.__showprofile:
636 636 axes = self.axesList[i*self.__nsubplots +1]
637 637 axes.pline(avgdB[index], y,
638 638 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
639 639 xlabel='dB', ylabel='', title='',
640 640 ytick_visible=False,
641 641 grid='x')
642 642
643 643 self.draw()
644 644
645 645 self.save(figpath=figpath,
646 646 figfile=figfile,
647 647 save=save,
648 648 ftp=ftp,
649 649 wr_period=wr_period,
650 650 thisDatetime=thisDatetime,
651 651 update_figfile=update_figfile)
652 652
653 653 class CoherenceMap(Figure):
654 654 isConfig = None
655 655 __nsubplots = None
656 656
657 657 WIDTHPROF = None
658 658 HEIGHTPROF = None
659 659 PREFIX = 'cmap'
660 660
661 661 def __init__(self, **kwargs):
662 662 Figure.__init__(self, **kwargs)
663 663 self.timerange = 2*60*60
664 664 self.isConfig = False
665 665 self.__nsubplots = 1
666 666
667 667 self.WIDTH = 800
668 668 self.HEIGHT = 180
669 669 self.WIDTHPROF = 120
670 670 self.HEIGHTPROF = 0
671 671 self.counter_imagwr = 0
672 672
673 673 self.PLOT_CODE = COH_CODE
674 674
675 675 self.FTP_WEI = None
676 676 self.EXP_CODE = None
677 677 self.SUB_EXP_CODE = None
678 678 self.PLOT_POS = None
679 679 self.counter_imagwr = 0
680 680
681 681 self.xmin = None
682 682 self.xmax = None
683 683
684 684 def getSubplots(self):
685 685 ncol = 1
686 686 nrow = self.nplots*2
687 687
688 688 return nrow, ncol
689 689
690 690 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
691 691 self.__showprofile = showprofile
692 692 self.nplots = nplots
693 693
694 694 ncolspan = 1
695 695 colspan = 1
696 696 if showprofile:
697 697 ncolspan = 7
698 698 colspan = 6
699 699 self.__nsubplots = 2
700 700
701 701 self.createFigure(id = id,
702 702 wintitle = wintitle,
703 703 widthplot = self.WIDTH + self.WIDTHPROF,
704 704 heightplot = self.HEIGHT + self.HEIGHTPROF,
705 705 show=True)
706 706
707 707 nrow, ncol = self.getSubplots()
708 708
709 709 for y in range(nrow):
710 710 for x in range(ncol):
711 711
712 712 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
713 713
714 714 if showprofile:
715 715 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
716 716
717 717 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
718 718 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
719 719 timerange=None, phase_min=None, phase_max=None,
720 720 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
721 721 coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
722 722 server=None, folder=None, username=None, password=None,
723 723 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
724 724
725 725 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
726 726 return
727 727
728 728 if pairsList == None:
729 729 pairsIndexList = dataOut.pairsIndexList
730 730 else:
731 731 pairsIndexList = []
732 732 for pair in pairsList:
733 733 if pair not in dataOut.pairsList:
734 734 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
735 735 pairsIndexList.append(dataOut.pairsList.index(pair))
736 736
737 737 if pairsIndexList == []:
738 738 return
739 739
740 740 if len(pairsIndexList) > 4:
741 741 pairsIndexList = pairsIndexList[0:4]
742 742
743 743 if phase_min == None:
744 744 phase_min = -180
745 745 if phase_max == None:
746 746 phase_max = 180
747 747
748 748 x = dataOut.getTimeRange()
749 749 y = dataOut.getHeiRange()
750 750
751 751 thisDatetime = dataOut.datatime
752 752
753 753 title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
754 754 xlabel = ""
755 755 ylabel = "Range (Km)"
756 756 update_figfile = False
757 757
758 758 if not self.isConfig:
759 759 nplots = len(pairsIndexList)
760 760 self.setup(id=id,
761 761 nplots=nplots,
762 762 wintitle=wintitle,
763 763 showprofile=showprofile,
764 764 show=show)
765 765
766 766 if timerange != None:
767 767 self.timerange = timerange
768 768
769 769 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
770 770
771 771 if ymin == None: ymin = numpy.nanmin(y)
772 772 if ymax == None: ymax = numpy.nanmax(y)
773 773 if zmin == None: zmin = 0.
774 774 if zmax == None: zmax = 1.
775 775
776 776 self.FTP_WEI = ftp_wei
777 777 self.EXP_CODE = exp_code
778 778 self.SUB_EXP_CODE = sub_exp_code
779 779 self.PLOT_POS = plot_pos
780 780
781 781 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
782 782
783 783 self.isConfig = True
784 784 update_figfile = True
785 785
786 786 self.setWinTitle(title)
787 787
788 788 for i in range(self.nplots):
789 789
790 790 pair = dataOut.pairsList[pairsIndexList[i]]
791 791
792 792 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
793 793 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
794 794 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
795 795
796 796
797 797 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
798 798 coherence = numpy.abs(avgcoherenceComplex)
799 799
800 800 z = coherence.reshape((1,-1))
801 801
802 802 counter = 0
803 803
804 804 title = "Coherence Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
805 805 axes = self.axesList[i*self.__nsubplots*2]
806 806 axes.pcolorbuffer(x, y, z,
807 807 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
808 808 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
809 809 ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%")
810 810
811 811 if self.__showprofile:
812 812 counter += 1
813 813 axes = self.axesList[i*self.__nsubplots*2 + counter]
814 814 axes.pline(coherence, y,
815 815 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
816 816 xlabel='', ylabel='', title='', ticksize=7,
817 817 ytick_visible=False, nxticks=5,
818 818 grid='x')
819 819
820 820 counter += 1
821 821
822 822 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
823 823
824 824 z = phase.reshape((1,-1))
825 825
826 826 title = "Phase Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
827 827 axes = self.axesList[i*self.__nsubplots*2 + counter]
828 828 axes.pcolorbuffer(x, y, z,
829 829 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
830 830 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
831 831 ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%")
832 832
833 833 if self.__showprofile:
834 834 counter += 1
835 835 axes = self.axesList[i*self.__nsubplots*2 + counter]
836 836 axes.pline(phase, y,
837 837 xmin=phase_min, xmax=phase_max, ymin=ymin, ymax=ymax,
838 838 xlabel='', ylabel='', title='', ticksize=7,
839 839 ytick_visible=False, nxticks=4,
840 840 grid='x')
841 841
842 842 self.draw()
843 843
844 844 if dataOut.ltctime >= self.xmax:
845 845 self.counter_imagwr = wr_period
846 846 self.isConfig = False
847 847 update_figfile = True
848 848
849 849 self.save(figpath=figpath,
850 850 figfile=figfile,
851 851 save=save,
852 852 ftp=ftp,
853 853 wr_period=wr_period,
854 854 thisDatetime=thisDatetime,
855 855 update_figfile=update_figfile)
856 856
857 857 class PowerProfilePlot(Figure):
858 858
859 859 isConfig = None
860 860 __nsubplots = None
861 861
862 862 WIDTHPROF = None
863 863 HEIGHTPROF = None
864 864 PREFIX = 'spcprofile'
865 865
866 866 def __init__(self, **kwargs):
867 867 Figure.__init__(self, **kwargs)
868 868 self.isConfig = False
869 869 self.__nsubplots = 1
870 870
871 871 self.PLOT_CODE = POWER_CODE
872 872
873 873 self.WIDTH = 300
874 874 self.HEIGHT = 500
875 875 self.counter_imagwr = 0
876 876
877 877 def getSubplots(self):
878 878 ncol = 1
879 879 nrow = 1
880 880
881 881 return nrow, ncol
882 882
883 883 def setup(self, id, nplots, wintitle, show):
884 884
885 885 self.nplots = nplots
886 886
887 887 ncolspan = 1
888 888 colspan = 1
889 889
890 890 self.createFigure(id = id,
891 891 wintitle = wintitle,
892 892 widthplot = self.WIDTH,
893 893 heightplot = self.HEIGHT,
894 894 show=show)
895 895
896 896 nrow, ncol = self.getSubplots()
897 897
898 898 counter = 0
899 899 for y in range(nrow):
900 900 for x in range(ncol):
901 901 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
902 902
903 903 def run(self, dataOut, id, wintitle="", channelList=None,
904 904 xmin=None, xmax=None, ymin=None, ymax=None,
905 905 save=False, figpath='./', figfile=None, show=True,
906 906 ftp=False, wr_period=1, server=None,
907 907 folder=None, username=None, password=None):
908 908
909 909
910 910 if channelList == None:
911 911 channelIndexList = dataOut.channelIndexList
912 912 channelList = dataOut.channelList
913 913 else:
914 914 channelIndexList = []
915 915 for channel in channelList:
916 916 if channel not in dataOut.channelList:
917 917 raise ValueError, "Channel %d is not in dataOut.channelList"
918 918 channelIndexList.append(dataOut.channelList.index(channel))
919 919
920 920 factor = dataOut.normFactor
921 921
922 922 y = dataOut.getHeiRange()
923 923
924 924 #for voltage
925 925 if dataOut.type == 'Voltage':
926 926 x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
927 927 x = x.real
928 928 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
929 929
930 930 #for spectra
931 931 if dataOut.type == 'Spectra':
932 932 x = dataOut.data_spc[channelIndexList,:,:]/factor
933 933 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
934 934 x = numpy.average(x, axis=1)
935 935
936 936
937 937 xdB = 10*numpy.log10(x)
938 938
939 939 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
940 940 title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y"))
941 941 xlabel = "dB"
942 942 ylabel = "Range (Km)"
943 943
944 944 if not self.isConfig:
945 945
946 946 nplots = 1
947 947
948 948 self.setup(id=id,
949 949 nplots=nplots,
950 950 wintitle=wintitle,
951 951 show=show)
952 952
953 953 if ymin == None: ymin = numpy.nanmin(y)
954 954 if ymax == None: ymax = numpy.nanmax(y)
955 955 if xmin == None: xmin = numpy.nanmin(xdB)*0.9
956 956 if xmax == None: xmax = numpy.nanmax(xdB)*1.1
957 957
958 958 self.isConfig = True
959 959
960 960 self.setWinTitle(title)
961 961
962 962 title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
963 963 axes = self.axesList[0]
964 964
965 965 legendlabels = ["channel %d"%x for x in channelList]
966 966 axes.pmultiline(xdB, y,
967 967 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
968 968 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
969 969 ytick_visible=True, nxticks=5,
970 970 grid='x')
971 971
972 972 self.draw()
973 973
974 974 self.save(figpath=figpath,
975 975 figfile=figfile,
976 976 save=save,
977 977 ftp=ftp,
978 978 wr_period=wr_period,
979 979 thisDatetime=thisDatetime)
980 980
981 981 class SpectraCutPlot(Figure):
982 982
983 983 isConfig = None
984 984 __nsubplots = None
985 985
986 986 WIDTHPROF = None
987 987 HEIGHTPROF = None
988 988 PREFIX = 'spc_cut'
989 989
990 990 def __init__(self, **kwargs):
991 991 Figure.__init__(self, **kwargs)
992 992 self.isConfig = False
993 993 self.__nsubplots = 1
994 994
995 995 self.PLOT_CODE = POWER_CODE
996 996
997 997 self.WIDTH = 700
998 998 self.HEIGHT = 500
999 999 self.counter_imagwr = 0
1000 1000
1001 1001 def getSubplots(self):
1002 1002 ncol = 1
1003 1003 nrow = 1
1004 1004
1005 1005 return nrow, ncol
1006 1006
1007 1007 def setup(self, id, nplots, wintitle, show):
1008 1008
1009 1009 self.nplots = nplots
1010 1010
1011 1011 ncolspan = 1
1012 1012 colspan = 1
1013 1013
1014 1014 self.createFigure(id = id,
1015 1015 wintitle = wintitle,
1016 1016 widthplot = self.WIDTH,
1017 1017 heightplot = self.HEIGHT,
1018 1018 show=show)
1019 1019
1020 1020 nrow, ncol = self.getSubplots()
1021 1021
1022 1022 counter = 0
1023 1023 for y in range(nrow):
1024 1024 for x in range(ncol):
1025 1025 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1026 1026
1027 1027 def run(self, dataOut, id, wintitle="", channelList=None,
1028 1028 xmin=None, xmax=None, ymin=None, ymax=None,
1029 1029 save=False, figpath='./', figfile=None, show=True,
1030 1030 ftp=False, wr_period=1, server=None,
1031 1031 folder=None, username=None, password=None,
1032 1032 xaxis="frequency"):
1033 1033
1034 1034
1035 1035 if channelList == None:
1036 1036 channelIndexList = dataOut.channelIndexList
1037 1037 channelList = dataOut.channelList
1038 1038 else:
1039 1039 channelIndexList = []
1040 1040 for channel in channelList:
1041 1041 if channel not in dataOut.channelList:
1042 1042 raise ValueError, "Channel %d is not in dataOut.channelList"
1043 1043 channelIndexList.append(dataOut.channelList.index(channel))
1044 1044
1045 1045 factor = dataOut.normFactor
1046 1046
1047 1047 y = dataOut.getHeiRange()
1048 1048
1049 1049 z = dataOut.data_spc/factor
1050 1050 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1051 1051
1052 1052 hei_index = numpy.arange(25)*3 + 20
1053 1053
1054 1054 if xaxis == "frequency":
1055 1055 x = dataOut.getFreqRange()/1000.
1056 1056 zdB = 10*numpy.log10(z[0,:,hei_index])
1057 1057 xlabel = "Frequency (kHz)"
1058 1058 ylabel = "Power (dB)"
1059 1059
1060 1060 elif xaxis == "time":
1061 1061 x = dataOut.getAcfRange()
1062 1062 zdB = z[0,:,hei_index]
1063 1063 xlabel = "Time (ms)"
1064 1064 ylabel = "ACF"
1065 1065
1066 1066 else:
1067 1067 x = dataOut.getVelRange()
1068 1068 zdB = 10*numpy.log10(z[0,:,hei_index])
1069 1069 xlabel = "Velocity (m/s)"
1070 1070 ylabel = "Power (dB)"
1071 1071
1072 1072 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1073 1073 title = wintitle + " Range Cuts %s" %(thisDatetime.strftime("%d-%b-%Y"))
1074 1074
1075 1075 if not self.isConfig:
1076 1076
1077 1077 nplots = 1
1078 1078
1079 1079 self.setup(id=id,
1080 1080 nplots=nplots,
1081 1081 wintitle=wintitle,
1082 1082 show=show)
1083 1083
1084 1084 if xmin == None: xmin = numpy.nanmin(x)*0.9
1085 1085 if xmax == None: xmax = numpy.nanmax(x)*1.1
1086 1086 if ymin == None: ymin = numpy.nanmin(zdB)
1087 1087 if ymax == None: ymax = numpy.nanmax(zdB)
1088 1088
1089 1089 self.isConfig = True
1090 1090
1091 1091 self.setWinTitle(title)
1092 1092
1093 1093 title = "Spectra Cuts: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1094 1094 axes = self.axesList[0]
1095 1095
1096 1096 legendlabels = ["Range = %dKm" %y[i] for i in hei_index]
1097 1097
1098 1098 axes.pmultilineyaxis( x, zdB,
1099 1099 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1100 1100 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
1101 1101 ytick_visible=True, nxticks=5,
1102 1102 grid='x')
1103 1103
1104 1104 self.draw()
1105 1105
1106 1106 self.save(figpath=figpath,
1107 1107 figfile=figfile,
1108 1108 save=save,
1109 1109 ftp=ftp,
1110 1110 wr_period=wr_period,
1111 1111 thisDatetime=thisDatetime)
1112 1112
1113 1113 class Noise(Figure):
1114 1114
1115 1115 isConfig = None
1116 1116 __nsubplots = None
1117 1117
1118 1118 PREFIX = 'noise'
1119 1119
1120 1120
1121 1121 def __init__(self, **kwargs):
1122 1122 Figure.__init__(self, **kwargs)
1123 1123 self.timerange = 24*60*60
1124 1124 self.isConfig = False
1125 1125 self.__nsubplots = 1
1126 1126 self.counter_imagwr = 0
1127 1127 self.WIDTH = 800
1128 1128 self.HEIGHT = 400
1129 1129 self.WIDTHPROF = 120
1130 1130 self.HEIGHTPROF = 0
1131 1131 self.xdata = None
1132 1132 self.ydata = None
1133 1133
1134 1134 self.PLOT_CODE = NOISE_CODE
1135 1135
1136 1136 self.FTP_WEI = None
1137 1137 self.EXP_CODE = None
1138 1138 self.SUB_EXP_CODE = None
1139 1139 self.PLOT_POS = None
1140 1140 self.figfile = None
1141 1141
1142 1142 self.xmin = None
1143 1143 self.xmax = None
1144 1144
1145 1145 def getSubplots(self):
1146 1146
1147 1147 ncol = 1
1148 1148 nrow = 1
1149 1149
1150 1150 return nrow, ncol
1151 1151
1152 1152 def openfile(self, filename):
1153 1153 dirname = os.path.dirname(filename)
1154 1154
1155 1155 if not os.path.exists(dirname):
1156 1156 os.mkdir(dirname)
1157 1157
1158 1158 f = open(filename,'w+')
1159 1159 f.write('\n\n')
1160 1160 f.write('JICAMARCA RADIO OBSERVATORY - Noise \n')
1161 1161 f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' )
1162 1162 f.close()
1163 1163
1164 1164 def save_data(self, filename_phase, data, data_datetime):
1165 1165
1166 1166 f=open(filename_phase,'a')
1167 1167
1168 1168 timetuple_data = data_datetime.timetuple()
1169 1169 day = str(timetuple_data.tm_mday)
1170 1170 month = str(timetuple_data.tm_mon)
1171 1171 year = str(timetuple_data.tm_year)
1172 1172 hour = str(timetuple_data.tm_hour)
1173 1173 minute = str(timetuple_data.tm_min)
1174 1174 second = str(timetuple_data.tm_sec)
1175 1175
1176 1176 data_msg = ''
1177 1177 for i in range(len(data)):
1178 1178 data_msg += str(data[i]) + ' '
1179 1179
1180 1180 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' ' + data_msg + '\n')
1181 1181 f.close()
1182 1182
1183 1183
1184 1184 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1185 1185
1186 1186 self.__showprofile = showprofile
1187 1187 self.nplots = nplots
1188 1188
1189 1189 ncolspan = 7
1190 1190 colspan = 6
1191 1191 self.__nsubplots = 2
1192 1192
1193 1193 self.createFigure(id = id,
1194 1194 wintitle = wintitle,
1195 1195 widthplot = self.WIDTH+self.WIDTHPROF,
1196 1196 heightplot = self.HEIGHT+self.HEIGHTPROF,
1197 1197 show=show)
1198 1198
1199 1199 nrow, ncol = self.getSubplots()
1200 1200
1201 1201 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1202 1202
1203 1203
1204 1204 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1205 1205 xmin=None, xmax=None, ymin=None, ymax=None,
1206 1206 timerange=None,
1207 1207 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1208 1208 server=None, folder=None, username=None, password=None,
1209 1209 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1210 1210
1211 1211 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1212 1212 return
1213 1213
1214 1214 if channelList == None:
1215 1215 channelIndexList = dataOut.channelIndexList
1216 1216 channelList = dataOut.channelList
1217 1217 else:
1218 1218 channelIndexList = []
1219 1219 for channel in channelList:
1220 1220 if channel not in dataOut.channelList:
1221 1221 raise ValueError, "Channel %d is not in dataOut.channelList"
1222 1222 channelIndexList.append(dataOut.channelList.index(channel))
1223 1223
1224 1224 x = dataOut.getTimeRange()
1225 1225 #y = dataOut.getHeiRange()
1226 1226 factor = dataOut.normFactor
1227 1227 noise = dataOut.noise[channelIndexList]/factor
1228 1228 noisedB = 10*numpy.log10(noise)
1229 1229
1230 1230 thisDatetime = dataOut.datatime
1231 1231
1232 1232 title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1233 1233 xlabel = ""
1234 1234 ylabel = "Intensity (dB)"
1235 1235 update_figfile = False
1236 1236
1237 1237 if not self.isConfig:
1238 1238
1239 1239 nplots = 1
1240 1240
1241 1241 self.setup(id=id,
1242 1242 nplots=nplots,
1243 1243 wintitle=wintitle,
1244 1244 showprofile=showprofile,
1245 1245 show=show)
1246 1246
1247 1247 if timerange != None:
1248 1248 self.timerange = timerange
1249 1249
1250 1250 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1251 1251
1252 1252 if ymin == None: ymin = numpy.floor(numpy.nanmin(noisedB)) - 10.0
1253 1253 if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0
1254 1254
1255 1255 self.FTP_WEI = ftp_wei
1256 1256 self.EXP_CODE = exp_code
1257 1257 self.SUB_EXP_CODE = sub_exp_code
1258 1258 self.PLOT_POS = plot_pos
1259 1259
1260 1260
1261 1261 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1262 1262 self.isConfig = True
1263 1263 self.figfile = figfile
1264 1264 self.xdata = numpy.array([])
1265 1265 self.ydata = numpy.array([])
1266 1266
1267 1267 update_figfile = True
1268 1268
1269 1269 #open file beacon phase
1270 1270 path = '%s%03d' %(self.PREFIX, self.id)
1271 1271 noise_file = os.path.join(path,'%s.txt'%self.name)
1272 1272 self.filename_noise = os.path.join(figpath,noise_file)
1273 1273
1274 1274 self.setWinTitle(title)
1275 1275
1276 1276 title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1277 1277
1278 1278 legendlabels = ["channel %d"%(idchannel) for idchannel in channelList]
1279 1279 axes = self.axesList[0]
1280 1280
1281 1281 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1282 1282
1283 1283 if len(self.ydata)==0:
1284 1284 self.ydata = noisedB.reshape(-1,1)
1285 1285 else:
1286 1286 self.ydata = numpy.hstack((self.ydata, noisedB.reshape(-1,1)))
1287 1287
1288 1288
1289 1289 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1290 1290 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1291 1291 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1292 1292 XAxisAsTime=True, grid='both'
1293 1293 )
1294 1294
1295 1295 self.draw()
1296 1296
1297 1297 if dataOut.ltctime >= self.xmax:
1298 1298 self.counter_imagwr = wr_period
1299 1299 self.isConfig = False
1300 1300 update_figfile = True
1301 1301
1302 1302 self.save(figpath=figpath,
1303 1303 figfile=figfile,
1304 1304 save=save,
1305 1305 ftp=ftp,
1306 1306 wr_period=wr_period,
1307 1307 thisDatetime=thisDatetime,
1308 1308 update_figfile=update_figfile)
1309 1309
1310 1310 #store data beacon phase
1311 1311 if save:
1312 1312 self.save_data(self.filename_noise, noisedB, thisDatetime)
1313 1313
1314 1314 class BeaconPhase(Figure):
1315 1315
1316 1316 __isConfig = None
1317 1317 __nsubplots = None
1318 1318
1319 1319 PREFIX = 'beacon_phase'
1320 1320
1321 1321 def __init__(self, **kwargs):
1322 1322 Figure.__init__(self, **kwargs)
1323 1323 self.timerange = 24*60*60
1324 1324 self.isConfig = False
1325 1325 self.__nsubplots = 1
1326 1326 self.counter_imagwr = 0
1327 1327 self.WIDTH = 800
1328 1328 self.HEIGHT = 400
1329 1329 self.WIDTHPROF = 120
1330 1330 self.HEIGHTPROF = 0
1331 1331 self.xdata = None
1332 1332 self.ydata = None
1333 1333
1334 1334 self.PLOT_CODE = BEACON_CODE
1335 1335
1336 1336 self.FTP_WEI = None
1337 1337 self.EXP_CODE = None
1338 1338 self.SUB_EXP_CODE = None
1339 1339 self.PLOT_POS = None
1340 1340
1341 1341 self.filename_phase = None
1342 1342
1343 1343 self.figfile = None
1344 1344
1345 1345 self.xmin = None
1346 1346 self.xmax = None
1347 1347
1348 1348 def getSubplots(self):
1349 1349
1350 1350 ncol = 1
1351 1351 nrow = 1
1352 1352
1353 1353 return nrow, ncol
1354 1354
1355 1355 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1356 1356
1357 1357 self.__showprofile = showprofile
1358 1358 self.nplots = nplots
1359 1359
1360 1360 ncolspan = 7
1361 1361 colspan = 6
1362 1362 self.__nsubplots = 2
1363 1363
1364 1364 self.createFigure(id = id,
1365 1365 wintitle = wintitle,
1366 1366 widthplot = self.WIDTH+self.WIDTHPROF,
1367 1367 heightplot = self.HEIGHT+self.HEIGHTPROF,
1368 1368 show=show)
1369 1369
1370 1370 nrow, ncol = self.getSubplots()
1371 1371
1372 1372 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1373 1373
1374 1374 def save_phase(self, filename_phase):
1375 1375 f = open(filename_phase,'w+')
1376 1376 f.write('\n\n')
1377 1377 f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n')
1378 1378 f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' )
1379 1379 f.close()
1380 1380
1381 1381 def save_data(self, filename_phase, data, data_datetime):
1382 1382 f=open(filename_phase,'a')
1383 1383 timetuple_data = data_datetime.timetuple()
1384 1384 day = str(timetuple_data.tm_mday)
1385 1385 month = str(timetuple_data.tm_mon)
1386 1386 year = str(timetuple_data.tm_year)
1387 1387 hour = str(timetuple_data.tm_hour)
1388 1388 minute = str(timetuple_data.tm_min)
1389 1389 second = str(timetuple_data.tm_sec)
1390 1390 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n')
1391 1391 f.close()
1392 1392
1393 1393
1394 1394 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1395 1395 xmin=None, xmax=None, ymin=None, ymax=None, hmin=None, hmax=None,
1396 1396 timerange=None,
1397 1397 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1398 1398 server=None, folder=None, username=None, password=None,
1399 1399 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1400 1400
1401 1401 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1402 1402 return
1403 1403
1404 1404 if pairsList == None:
1405 1405 pairsIndexList = dataOut.pairsIndexList[:10]
1406 1406 else:
1407 1407 pairsIndexList = []
1408 1408 for pair in pairsList:
1409 1409 if pair not in dataOut.pairsList:
1410 1410 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
1411 1411 pairsIndexList.append(dataOut.pairsList.index(pair))
1412 1412
1413 1413 if pairsIndexList == []:
1414 1414 return
1415 1415
1416 1416 # if len(pairsIndexList) > 4:
1417 1417 # pairsIndexList = pairsIndexList[0:4]
1418 1418
1419 1419 hmin_index = None
1420 1420 hmax_index = None
1421 1421
1422 1422 if hmin != None and hmax != None:
1423 1423 indexes = numpy.arange(dataOut.nHeights)
1424 1424 hmin_list = indexes[dataOut.heightList >= hmin]
1425 1425 hmax_list = indexes[dataOut.heightList <= hmax]
1426 1426
1427 1427 if hmin_list.any():
1428 1428 hmin_index = hmin_list[0]
1429 1429
1430 1430 if hmax_list.any():
1431 1431 hmax_index = hmax_list[-1]+1
1432 1432
1433 1433 x = dataOut.getTimeRange()
1434 1434 #y = dataOut.getHeiRange()
1435 1435
1436 1436
1437 1437 thisDatetime = dataOut.datatime
1438 1438
1439 1439 title = wintitle + " Signal Phase" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1440 1440 xlabel = "Local Time"
1441 1441 ylabel = "Phase (degrees)"
1442 1442
1443 1443 update_figfile = False
1444 1444
1445 1445 nplots = len(pairsIndexList)
1446 1446 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1447 1447 phase_beacon = numpy.zeros(len(pairsIndexList))
1448 1448 for i in range(nplots):
1449 1449 pair = dataOut.pairsList[pairsIndexList[i]]
1450 1450 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i], :, hmin_index:hmax_index], axis=0)
1451 1451 powa = numpy.average(dataOut.data_spc[pair[0], :, hmin_index:hmax_index], axis=0)
1452 1452 powb = numpy.average(dataOut.data_spc[pair[1], :, hmin_index:hmax_index], axis=0)
1453 1453 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
1454 1454 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
1455 1455
1456 1456 #print "Phase %d%d" %(pair[0], pair[1])
1457 1457 #print phase[dataOut.beacon_heiIndexList]
1458 1458
1459 1459 if dataOut.beacon_heiIndexList:
1460 1460 phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList])
1461 1461 else:
1462 1462 phase_beacon[i] = numpy.average(phase)
1463 1463
1464 1464 if not self.isConfig:
1465 1465
1466 1466 nplots = len(pairsIndexList)
1467 1467
1468 1468 self.setup(id=id,
1469 1469 nplots=nplots,
1470 1470 wintitle=wintitle,
1471 1471 showprofile=showprofile,
1472 1472 show=show)
1473 1473
1474 1474 if timerange != None:
1475 1475 self.timerange = timerange
1476 1476
1477 1477 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1478 1478
1479 1479 if ymin == None: ymin = 0
1480 1480 if ymax == None: ymax = 360
1481 1481
1482 1482 self.FTP_WEI = ftp_wei
1483 1483 self.EXP_CODE = exp_code
1484 1484 self.SUB_EXP_CODE = sub_exp_code
1485 1485 self.PLOT_POS = plot_pos
1486 1486
1487 1487 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1488 1488 self.isConfig = True
1489 1489 self.figfile = figfile
1490 1490 self.xdata = numpy.array([])
1491 1491 self.ydata = numpy.array([])
1492 1492
1493 1493 update_figfile = True
1494 1494
1495 1495 #open file beacon phase
1496 1496 path = '%s%03d' %(self.PREFIX, self.id)
1497 1497 beacon_file = os.path.join(path,'%s.txt'%self.name)
1498 1498 self.filename_phase = os.path.join(figpath,beacon_file)
1499 1499 #self.save_phase(self.filename_phase)
1500 1500
1501 1501
1502 1502 #store data beacon phase
1503 1503 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1504 1504
1505 1505 self.setWinTitle(title)
1506 1506
1507 1507
1508 1508 title = "Phase Plot %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1509 1509
1510 1510 legendlabels = ["Pair (%d,%d)"%(pair[0], pair[1]) for pair in dataOut.pairsList]
1511 1511
1512 1512 axes = self.axesList[0]
1513 1513
1514 1514 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1515 1515
1516 1516 if len(self.ydata)==0:
1517 1517 self.ydata = phase_beacon.reshape(-1,1)
1518 1518 else:
1519 1519 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1520 1520
1521 1521
1522 1522 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1523 1523 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1524 1524 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1525 1525 XAxisAsTime=True, grid='both'
1526 1526 )
1527 1527
1528 1528 self.draw()
1529 1529
1530 1530 if dataOut.ltctime >= self.xmax:
1531 1531 self.counter_imagwr = wr_period
1532 1532 self.isConfig = False
1533 1533 update_figfile = True
1534 1534
1535 1535 self.save(figpath=figpath,
1536 1536 figfile=figfile,
1537 1537 save=save,
1538 1538 ftp=ftp,
1539 1539 wr_period=wr_period,
1540 1540 thisDatetime=thisDatetime,
1541 1541 update_figfile=update_figfile)
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
@@ -1,482 +1,481
1 1 import numpy
2 2 import datetime
3 3 import sys
4 4 import matplotlib
5 5
6 6 if 'linux' in sys.platform:
7 7 matplotlib.use("TKAgg")
8 8
9 9 if 'darwin' in sys.platform:
10 10 matplotlib.use('TKAgg')
11 11 #Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'
12 12 import matplotlib.pyplot
13 13
14 14 from mpl_toolkits.axes_grid1 import make_axes_locatable
15 15 from matplotlib.ticker import FuncFormatter, LinearLocator
16 16
17 17 ###########################################
18 18 #Actualizacion de las funciones del driver
19 19 ###########################################
20 20
21 21 # create jro colormap
22 22
23 23 jet_values = matplotlib.pyplot.get_cmap("jet", 100)(numpy.arange(100))[10:90]
24 24 blu_values = matplotlib.pyplot.get_cmap("seismic_r", 20)(numpy.arange(20))[10:15]
25 25 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list("jro", numpy.vstack((blu_values, jet_values)))
26 26 matplotlib.pyplot.register_cmap(cmap=ncmap)
27 27
28 28 def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi = 80):
29 29
30 30 matplotlib.pyplot.ioff()
31 31
32 32 fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=(1.0*width/dpi, 1.0*height/dpi))
33 33 fig.canvas.manager.set_window_title(wintitle)
34 34 # fig.canvas.manager.resize(width, height)
35 35 matplotlib.pyplot.ion()
36 36
37 37 if show:
38 38 matplotlib.pyplot.show()
39 39
40 40 return fig
41 41
42 42 def closeFigure(show=False, fig=None):
43 43
44 44 # matplotlib.pyplot.ioff()
45 45 # matplotlib.pyplot.pause(0)
46 46
47 47 if show:
48 48 matplotlib.pyplot.show()
49 49
50 50 if fig != None:
51 51 matplotlib.pyplot.close(fig)
52 52 # matplotlib.pyplot.pause(0)
53 53 # matplotlib.pyplot.ion()
54 54
55 55 return
56 56
57 57 matplotlib.pyplot.close("all")
58 58 # matplotlib.pyplot.pause(0)
59 59 # matplotlib.pyplot.ion()
60 60
61 61 return
62 62
63 63 def saveFigure(fig, filename):
64 64
65 65 # matplotlib.pyplot.ioff()
66 66 fig.savefig(filename, dpi=matplotlib.pyplot.gcf().dpi)
67 67 # matplotlib.pyplot.ion()
68 68
69 69 def clearFigure(fig):
70 70
71 71 fig.clf()
72 72
73 73 def setWinTitle(fig, title):
74 74
75 75 fig.canvas.manager.set_window_title(title)
76 76
77 77 def setTitle(fig, title):
78 78
79 79 fig.suptitle(title)
80 80
81 81 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan, polar=False):
82 82
83 83 matplotlib.pyplot.ioff()
84 84 matplotlib.pyplot.figure(fig.number)
85 85 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
86 86 (xpos, ypos),
87 87 colspan=colspan,
88 88 rowspan=rowspan,
89 89 polar=polar)
90 90
91 91 axes.grid(True)
92 92
93 93 matplotlib.pyplot.ion()
94 94 return axes
95 95
96 96 def setAxesText(ax, text):
97 97
98 98 ax.annotate(text,
99 99 xy = (.1, .99),
100 100 xycoords = 'figure fraction',
101 101 horizontalalignment = 'left',
102 102 verticalalignment = 'top',
103 103 fontsize = 10)
104 104
105 105 def printLabels(ax, xlabel, ylabel, title):
106 106
107 107 ax.set_xlabel(xlabel, size=11)
108 108 ax.set_ylabel(ylabel, size=11)
109 109 ax.set_title(title, size=8)
110 110
111 111 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='',
112 112 ticksize=9, xtick_visible=True, ytick_visible=True,
113 113 nxticks=4, nyticks=10,
114 114 grid=None,color='blue'):
115 115
116 116 """
117 117
118 118 Input:
119 119 grid : None, 'both', 'x', 'y'
120 120 """
121 121
122 122 matplotlib.pyplot.ioff()
123 123
124 124 ax.set_xlim([xmin,xmax])
125 125 ax.set_ylim([ymin,ymax])
126 126
127 127 printLabels(ax, xlabel, ylabel, title)
128 128
129 129 ######################################################
130 130 if (xmax-xmin)<=1:
131 131 xtickspos = numpy.linspace(xmin,xmax,nxticks)
132 132 xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos])
133 133 ax.set_xticks(xtickspos)
134 134 else:
135 135 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
136 136 # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin)
137 137 ax.set_xticks(xtickspos)
138 138
139 139 for tick in ax.get_xticklabels():
140 140 tick.set_visible(xtick_visible)
141 141
142 142 for tick in ax.xaxis.get_major_ticks():
143 143 tick.label.set_fontsize(ticksize)
144 144
145 145 ######################################################
146 146 for tick in ax.get_yticklabels():
147 147 tick.set_visible(ytick_visible)
148 148
149 149 for tick in ax.yaxis.get_major_ticks():
150 150 tick.label.set_fontsize(ticksize)
151 151
152 152 ax.plot(x, y, color=color)
153 153 iplot = ax.lines[-1]
154 154
155 155 ######################################################
156 156 if '0.' in matplotlib.__version__[0:2]:
157 157 print "The matplotlib version has to be updated to 1.1 or newer"
158 158 return iplot
159 159
160 160 if '1.0.' in matplotlib.__version__[0:4]:
161 161 print "The matplotlib version has to be updated to 1.1 or newer"
162 162 return iplot
163 163
164 164 if grid != None:
165 165 ax.grid(b=True, which='major', axis=grid)
166 166
167 167 matplotlib.pyplot.tight_layout()
168 168
169 169 matplotlib.pyplot.ion()
170 170
171 171 return iplot
172 172
173 173 def set_linedata(ax, x, y, idline):
174 174
175 175 ax.lines[idline].set_data(x,y)
176 176
177 177 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
178 178
179 179 ax = iplot.get_axes()
180 180
181 181 printLabels(ax, xlabel, ylabel, title)
182 182
183 183 set_linedata(ax, x, y, idline=0)
184 184
185 185 def addpline(ax, x, y, color, linestyle, lw):
186 186
187 187 ax.plot(x,y,color=color,linestyle=linestyle,lw=lw)
188 188
189 189
190 190 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
191 191 xlabel='', ylabel='', title='', ticksize = 9,
192 192 colormap='jet',cblabel='', cbsize="5%",
193 193 XAxisAsTime=False):
194 194
195 195 matplotlib.pyplot.ioff()
196 196
197 197 divider = make_axes_locatable(ax)
198 198 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
199 199 fig = ax.get_figure()
200 200 fig.add_axes(ax_cb)
201 201
202 202 ax.set_xlim([xmin,xmax])
203 203 ax.set_ylim([ymin,ymax])
204 204
205 205 printLabels(ax, xlabel, ylabel, title)
206 206
207 207 z = numpy.ma.masked_invalid(z)
208 208 cmap=matplotlib.pyplot.get_cmap(colormap)
209 209 cmap.set_bad('white',1.)
210 210 imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=cmap)
211 211 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
212 212 cb.set_label(cblabel)
213 213
214 214 # for tl in ax_cb.get_yticklabels():
215 215 # tl.set_visible(True)
216 216
217 217 for tick in ax.yaxis.get_major_ticks():
218 218 tick.label.set_fontsize(ticksize)
219 219
220 220 for tick in ax.xaxis.get_major_ticks():
221 221 tick.label.set_fontsize(ticksize)
222 222
223 223 for tick in cb.ax.get_yticklabels():
224 224 tick.set_fontsize(ticksize)
225 225
226 226 ax_cb.yaxis.tick_right()
227 227
228 228 if '0.' in matplotlib.__version__[0:2]:
229 229 print "The matplotlib version has to be updated to 1.1 or newer"
230 230 return imesh
231 231
232 232 if '1.0.' in matplotlib.__version__[0:4]:
233 233 print "The matplotlib version has to be updated to 1.1 or newer"
234 234 return imesh
235 235
236 236 matplotlib.pyplot.tight_layout()
237 237
238 238 if XAxisAsTime:
239 239
240 240 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
241 241 ax.xaxis.set_major_formatter(FuncFormatter(func))
242 242 ax.xaxis.set_major_locator(LinearLocator(7))
243 243
244 244 ax.grid(True)
245 245 matplotlib.pyplot.ion()
246 246 return imesh
247 247
248 248 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
249 249
250 250 z = numpy.ma.masked_invalid(z)
251 251
252 252 cmap=matplotlib.pyplot.get_cmap('jet')
253 253 cmap.set_bad('white',1.)
254 254
255 255 z = z.T
256 256 ax = imesh.get_axes()
257 257 printLabels(ax, xlabel, ylabel, title)
258 258 imesh.set_array(z.ravel())
259 259 ax.grid(True)
260 260
261 261
262 262 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
263 263
264 264 printLabels(ax, xlabel, ylabel, title)
265 265 z = numpy.ma.masked_invalid(z)
266 266 cmap=matplotlib.pyplot.get_cmap(colormap)
267 267 cmap.set_bad('white',1.)
268 268 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
269 269 ax.grid(True)
270 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
271 270
272 271 def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
273 272
274 273 printLabels(ax, xlabel, ylabel, title)
275 274
276 275 ax.collections.remove(ax.collections[0])
277 276
278 277 z = numpy.ma.masked_invalid(z)
279 278
280 279 cmap=matplotlib.pyplot.get_cmap(colormap)
281 280 cmap.set_bad('white',1.)
282 281
283 282 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=cmap)
284 283 ax.grid(True)
285 284
286 285
287 286 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
288 287 ticksize=9, xtick_visible=True, ytick_visible=True,
289 288 nxticks=4, nyticks=10,
290 289 grid=None):
291 290
292 291 """
293 292
294 293 Input:
295 294 grid : None, 'both', 'x', 'y'
296 295 """
297 296
298 297 matplotlib.pyplot.ioff()
299 298
300 299 lines = ax.plot(x.T, y)
301 300 leg = ax.legend(lines, legendlabels, loc='upper right')
302 301 leg.get_frame().set_alpha(0.5)
303 302 ax.set_xlim([xmin,xmax])
304 303 ax.set_ylim([ymin,ymax])
305 304 printLabels(ax, xlabel, ylabel, title)
306 305
307 306 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
308 307 ax.set_xticks(xtickspos)
309 308
310 309 for tick in ax.get_xticklabels():
311 310 tick.set_visible(xtick_visible)
312 311
313 312 for tick in ax.xaxis.get_major_ticks():
314 313 tick.label.set_fontsize(ticksize)
315 314
316 315 for tick in ax.get_yticklabels():
317 316 tick.set_visible(ytick_visible)
318 317
319 318 for tick in ax.yaxis.get_major_ticks():
320 319 tick.label.set_fontsize(ticksize)
321 320
322 321 iplot = ax.lines[-1]
323 322
324 323 if '0.' in matplotlib.__version__[0:2]:
325 324 print "The matplotlib version has to be updated to 1.1 or newer"
326 325 return iplot
327 326
328 327 if '1.0.' in matplotlib.__version__[0:4]:
329 328 print "The matplotlib version has to be updated to 1.1 or newer"
330 329 return iplot
331 330
332 331 if grid != None:
333 332 ax.grid(b=True, which='major', axis=grid)
334 333
335 334 matplotlib.pyplot.tight_layout()
336 335
337 336 matplotlib.pyplot.ion()
338 337
339 338 return iplot
340 339
341 340
342 341 def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''):
343 342
344 343 ax = iplot.get_axes()
345 344
346 345 printLabels(ax, xlabel, ylabel, title)
347 346
348 347 for i in range(len(ax.lines)):
349 348 line = ax.lines[i]
350 349 line.set_data(x[i,:],y)
351 350
352 351 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
353 352 ticksize=9, xtick_visible=True, ytick_visible=True,
354 353 nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None",
355 354 grid=None, XAxisAsTime=False):
356 355
357 356 """
358 357
359 358 Input:
360 359 grid : None, 'both', 'x', 'y'
361 360 """
362 361
363 362 matplotlib.pyplot.ioff()
364 363
365 364 # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
366 365 lines = ax.plot(x, y.T)
367 366 # leg = ax.legend(lines, legendlabels, loc=2, bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \
368 367 # handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.)
369 368
370 369 leg = ax.legend(lines, legendlabels,
371 370 loc='upper right', bbox_to_anchor=(1.16, 1), borderaxespad=0)
372 371
373 372 for label in leg.get_texts(): label.set_fontsize(9)
374 373
375 374 ax.set_xlim([xmin,xmax])
376 375 ax.set_ylim([ymin,ymax])
377 376 printLabels(ax, xlabel, ylabel, title)
378 377
379 378 # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
380 379 # ax.set_xticks(xtickspos)
381 380
382 381 for tick in ax.get_xticklabels():
383 382 tick.set_visible(xtick_visible)
384 383
385 384 for tick in ax.xaxis.get_major_ticks():
386 385 tick.label.set_fontsize(ticksize)
387 386
388 387 for tick in ax.get_yticklabels():
389 388 tick.set_visible(ytick_visible)
390 389
391 390 for tick in ax.yaxis.get_major_ticks():
392 391 tick.label.set_fontsize(ticksize)
393 392
394 393 iplot = ax.lines[-1]
395 394
396 395 if '0.' in matplotlib.__version__[0:2]:
397 396 print "The matplotlib version has to be updated to 1.1 or newer"
398 397 return iplot
399 398
400 399 if '1.0.' in matplotlib.__version__[0:4]:
401 400 print "The matplotlib version has to be updated to 1.1 or newer"
402 401 return iplot
403 402
404 403 if grid != None:
405 404 ax.grid(b=True, which='major', axis=grid)
406 405
407 406 matplotlib.pyplot.tight_layout()
408 407
409 408 if XAxisAsTime:
410 409
411 410 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
412 411 ax.xaxis.set_major_formatter(FuncFormatter(func))
413 412 ax.xaxis.set_major_locator(LinearLocator(7))
414 413
415 414 matplotlib.pyplot.ion()
416 415
417 416 return iplot
418 417
419 418 def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
420 419
421 420 ax = iplot.get_axes()
422 421 printLabels(ax, xlabel, ylabel, title)
423 422
424 423 for i in range(len(ax.lines)):
425 424 line = ax.lines[i]
426 425 line.set_data(x,y[i,:])
427 426
428 427 def createPolar(ax, x, y,
429 428 xlabel='', ylabel='', title='', ticksize = 9,
430 429 colormap='jet',cblabel='', cbsize="5%",
431 430 XAxisAsTime=False):
432 431
433 432 matplotlib.pyplot.ioff()
434 433
435 434 ax.plot(x,y,'bo', markersize=5)
436 435 # ax.set_rmax(90)
437 436 ax.set_ylim(0,90)
438 437 ax.set_yticks(numpy.arange(0,90,20))
439 438 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center' ,size='11')
440 439 # ax.text(0, 50, ylabel, rotation='vertical', va ='center', ha = 'left' ,size='11')
441 440 # ax.text(100, 100, 'example', ha='left', va='center', rotation='vertical')
442 441 ax.yaxis.labelpad = 230
443 442 printLabels(ax, xlabel, ylabel, title)
444 443 iplot = ax.lines[-1]
445 444
446 445 if '0.' in matplotlib.__version__[0:2]:
447 446 print "The matplotlib version has to be updated to 1.1 or newer"
448 447 return iplot
449 448
450 449 if '1.0.' in matplotlib.__version__[0:4]:
451 450 print "The matplotlib version has to be updated to 1.1 or newer"
452 451 return iplot
453 452
454 453 # if grid != None:
455 454 # ax.grid(b=True, which='major', axis=grid)
456 455
457 456 matplotlib.pyplot.tight_layout()
458 457
459 458 matplotlib.pyplot.ion()
460 459
461 460
462 461 return iplot
463 462
464 463 def polar(iplot, x, y, xlabel='', ylabel='', title=''):
465 464
466 465 ax = iplot.get_axes()
467 466
468 467 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center',size='11')
469 468 printLabels(ax, xlabel, ylabel, title)
470 469
471 470 set_linedata(ax, x, y, idline=0)
472 471
473 472 def draw(fig):
474 473
475 474 if type(fig) == 'int':
476 475 raise ValueError, "Error drawing: Fig parameter should be a matplotlib figure object figure"
477 476
478 477 fig.canvas.draw()
479 478
480 479 def pause(interval=0.000001):
481 480
482 481 matplotlib.pyplot.pause(interval)
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
@@ -1,1794 +1,1795
1 1 '''
2 2 Created on Jul 2, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import sys
8 8 import glob
9 9 import time
10 10 import numpy
11 11 import fnmatch
12 12 import inspect
13 13 import time, datetime
14 14 #import h5py
15 15 import traceback
16 16
17 17 try:
18 18 from gevent import sleep
19 19 except:
20 20 from time import sleep
21 21
22 22 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
23 23 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
24 24
25 25 LOCALTIME = True
26 26
27 27 def isNumber(cad):
28 28 """
29 29 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
30 30
31 31 Excepciones:
32 32 Si un determinado string no puede ser convertido a numero
33 33 Input:
34 34 str, string al cual se le analiza para determinar si convertible a un numero o no
35 35
36 36 Return:
37 37 True : si el string es uno numerico
38 38 False : no es un string numerico
39 39 """
40 40 try:
41 41 float( cad )
42 42 return True
43 43 except:
44 44 return False
45 45
46 46 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
47 47 """
48 48 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
49 49
50 50 Inputs:
51 51 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
52 52
53 53 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
54 54 segundos contados desde 01/01/1970.
55 55 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
56 56 segundos contados desde 01/01/1970.
57 57
58 58 Return:
59 59 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
60 60 fecha especificado, de lo contrario retorna False.
61 61
62 62 Excepciones:
63 63 Si el archivo no existe o no puede ser abierto
64 64 Si la cabecera no puede ser leida.
65 65
66 66 """
67 67 basicHeaderObj = BasicHeader(LOCALTIME)
68 68
69 69 try:
70 70 fp = open(filename,'rb')
71 71 except IOError:
72 72 print "The file %s can't be opened" %(filename)
73 73 return 0
74 74
75 75 sts = basicHeaderObj.read(fp)
76 76 fp.close()
77 77
78 78 if not(sts):
79 79 print "Skipping the file %s because it has not a valid header" %(filename)
80 80 return 0
81 81
82 82 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
83 83 return 0
84 84
85 85 return 1
86 86
87 87 def isTimeInRange(thisTime, startTime, endTime):
88 88
89 89 if endTime >= startTime:
90 90 if (thisTime < startTime) or (thisTime > endTime):
91 91 return 0
92 92
93 93 return 1
94 94 else:
95 95 if (thisTime < startTime) and (thisTime > endTime):
96 96 return 0
97 97
98 98 return 1
99 99
100 100 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
101 101 """
102 102 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
103 103
104 104 Inputs:
105 105 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
106 106
107 107 startDate : fecha inicial del rango seleccionado en formato datetime.date
108 108
109 109 endDate : fecha final del rango seleccionado en formato datetime.date
110 110
111 111 startTime : tiempo inicial del rango seleccionado en formato datetime.time
112 112
113 113 endTime : tiempo final del rango seleccionado en formato datetime.time
114 114
115 115 Return:
116 116 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
117 117 fecha especificado, de lo contrario retorna False.
118 118
119 119 Excepciones:
120 120 Si el archivo no existe o no puede ser abierto
121 121 Si la cabecera no puede ser leida.
122 122
123 123 """
124 124
125 125
126 126 try:
127 127 fp = open(filename,'rb')
128 128 except IOError:
129 129 print "The file %s can't be opened" %(filename)
130 130 return None
131 131
132 132 firstBasicHeaderObj = BasicHeader(LOCALTIME)
133 133 systemHeaderObj = SystemHeader()
134 134 radarControllerHeaderObj = RadarControllerHeader()
135 135 processingHeaderObj = ProcessingHeader()
136 136
137 137 lastBasicHeaderObj = BasicHeader(LOCALTIME)
138 138
139 139 sts = firstBasicHeaderObj.read(fp)
140 140
141 141 if not(sts):
142 142 print "[Reading] Skipping the file %s because it has not a valid header" %(filename)
143 143 return None
144 144
145 145 if not systemHeaderObj.read(fp):
146 146 return None
147 147
148 148 if not radarControllerHeaderObj.read(fp):
149 149 return None
150 150
151 151 if not processingHeaderObj.read(fp):
152 152 return None
153 153
154 154 filesize = os.path.getsize(filename)
155 155
156 156 offset = processingHeaderObj.blockSize + 24 #header size
157 157
158 158 if filesize <= offset:
159 159 print "[Reading] %s: This file has not enough data" %filename
160 160 return None
161 161
162 162 fp.seek(-offset, 2)
163 163
164 164 sts = lastBasicHeaderObj.read(fp)
165 165
166 166 fp.close()
167 167
168 168 thisDatetime = lastBasicHeaderObj.datatime
169 169 thisTime_last_block = thisDatetime.time()
170 170
171 171 thisDatetime = firstBasicHeaderObj.datatime
172 172 thisDate = thisDatetime.date()
173 173 thisTime_first_block = thisDatetime.time()
174 174
175 175 #General case
176 176 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
177 177 #-----------o----------------------------o-----------
178 178 # startTime endTime
179 179
180 180 if endTime >= startTime:
181 181 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
182 182 return None
183 183
184 184 return thisDatetime
185 185
186 186 #If endTime < startTime then endTime belongs to the next day
187 187
188 188
189 189 #<<<<<<<<<<<o o>>>>>>>>>>>
190 190 #-----------o----------------------------o-----------
191 191 # endTime startTime
192 192
193 193 if (thisDate == startDate) and (thisTime_last_block < startTime):
194 194 return None
195 195
196 196 if (thisDate == endDate) and (thisTime_first_block > endTime):
197 197 return None
198 198
199 199 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
200 200 return None
201 201
202 202 return thisDatetime
203 203
204 204 def isFolderInDateRange(folder, startDate=None, endDate=None):
205 205 """
206 206 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
207 207
208 208 Inputs:
209 209 folder : nombre completo del directorio.
210 210 Su formato deberia ser "/path_root/?YYYYDDD"
211 211
212 212 siendo:
213 213 YYYY : Anio (ejemplo 2015)
214 214 DDD : Dia del anio (ejemplo 305)
215 215
216 216 startDate : fecha inicial del rango seleccionado en formato datetime.date
217 217
218 218 endDate : fecha final del rango seleccionado en formato datetime.date
219 219
220 220 Return:
221 221 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
222 222 fecha especificado, de lo contrario retorna False.
223 223 Excepciones:
224 224 Si el directorio no tiene el formato adecuado
225 225 """
226 226
227 227 basename = os.path.basename(folder)
228 228
229 229 if not isRadarFolder(basename):
230 230 print "The folder %s has not the rigth format" %folder
231 231 return 0
232 232
233 233 if startDate and endDate:
234 234 thisDate = getDateFromRadarFolder(basename)
235 235
236 236 if thisDate < startDate:
237 237 return 0
238 238
239 239 if thisDate > endDate:
240 240 return 0
241 241
242 242 return 1
243 243
244 244 def isFileInDateRange(filename, startDate=None, endDate=None):
245 245 """
246 246 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
247 247
248 248 Inputs:
249 249 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
250 250
251 251 Su formato deberia ser "?YYYYDDDsss"
252 252
253 253 siendo:
254 254 YYYY : Anio (ejemplo 2015)
255 255 DDD : Dia del anio (ejemplo 305)
256 256 sss : set
257 257
258 258 startDate : fecha inicial del rango seleccionado en formato datetime.date
259 259
260 260 endDate : fecha final del rango seleccionado en formato datetime.date
261 261
262 262 Return:
263 263 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
264 264 fecha especificado, de lo contrario retorna False.
265 265 Excepciones:
266 266 Si el archivo no tiene el formato adecuado
267 267 """
268 268
269 269 basename = os.path.basename(filename)
270 270
271 271 if not isRadarFile(basename):
272 272 print "The filename %s has not the rigth format" %filename
273 273 return 0
274 274
275 275 if startDate and endDate:
276 276 thisDate = getDateFromRadarFile(basename)
277 277
278 278 if thisDate < startDate:
279 279 return 0
280 280
281 281 if thisDate > endDate:
282 282 return 0
283 283
284 284 return 1
285 285
286 286 def getFileFromSet(path, ext, set):
287 287 validFilelist = []
288 288 fileList = os.listdir(path)
289 289
290 290 # 0 1234 567 89A BCDE
291 291 # H YYYY DDD SSS .ext
292 292
293 293 for thisFile in fileList:
294 294 try:
295 295 year = int(thisFile[1:5])
296 296 doy = int(thisFile[5:8])
297 297 except:
298 298 continue
299 299
300 300 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
301 301 continue
302 302
303 303 validFilelist.append(thisFile)
304 304
305 305 myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
306 306
307 307 if len(myfile)!= 0:
308 308 return myfile[0]
309 309 else:
310 310 filename = '*%4.4d%3.3d%3.3d%s'%(year,doy,set,ext.lower())
311 311 print 'the filename %s does not exist'%filename
312 312 print '...going to the last file: '
313 313
314 314 if validFilelist:
315 315 validFilelist = sorted( validFilelist, key=str.lower )
316 316 return validFilelist[-1]
317 317
318 318 return None
319 319
320 320 def getlastFileFromPath(path, ext):
321 321 """
322 322 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
323 323 al final de la depuracion devuelve el ultimo file de la lista que quedo.
324 324
325 325 Input:
326 326 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
327 327 ext : extension de los files contenidos en una carpeta
328 328
329 329 Return:
330 330 El ultimo file de una determinada carpeta, no se considera el path.
331 331 """
332 332 validFilelist = []
333 333 fileList = os.listdir(path)
334 334
335 335 # 0 1234 567 89A BCDE
336 336 # H YYYY DDD SSS .ext
337 337
338 338 for thisFile in fileList:
339 339
340 340 year = thisFile[1:5]
341 341 if not isNumber(year):
342 342 continue
343 343
344 344 doy = thisFile[5:8]
345 345 if not isNumber(doy):
346 346 continue
347 347
348 348 year = int(year)
349 349 doy = int(doy)
350 350
351 351 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
352 352 continue
353 353
354 354 validFilelist.append(thisFile)
355 355
356 356 if validFilelist:
357 357 validFilelist = sorted( validFilelist, key=str.lower )
358 358 return validFilelist[-1]
359 359
360 360 return None
361 361
362 362 def checkForRealPath(path, foldercounter, year, doy, set, ext):
363 363 """
364 364 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
365 365 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
366 366 el path exacto de un determinado file.
367 367
368 368 Example :
369 369 nombre correcto del file es .../.../D2009307/P2009307367.ext
370 370
371 371 Entonces la funcion prueba con las siguientes combinaciones
372 372 .../.../y2009307367.ext
373 373 .../.../Y2009307367.ext
374 374 .../.../x2009307/y2009307367.ext
375 375 .../.../x2009307/Y2009307367.ext
376 376 .../.../X2009307/y2009307367.ext
377 377 .../.../X2009307/Y2009307367.ext
378 378 siendo para este caso, la ultima combinacion de letras, identica al file buscado
379 379
380 380 Return:
381 381 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
382 382 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
383 383 para el filename
384 384 """
385 385 fullfilename = None
386 386 find_flag = False
387 387 filename = None
388 388
389 389 prefixDirList = [None,'d','D']
390 390 if ext.lower() == ".r": #voltage
391 391 prefixFileList = ['d','D']
392 392 elif ext.lower() == ".pdata": #spectra
393 393 prefixFileList = ['p','P']
394 394 else:
395 395 return None, filename
396 396
397 397 #barrido por las combinaciones posibles
398 398 for prefixDir in prefixDirList:
399 399 thispath = path
400 400 if prefixDir != None:
401 401 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
402 402 if foldercounter == 0:
403 403 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
404 404 else:
405 405 thispath = os.path.join(path, "%s%04d%03d_%02d" % ( prefixDir, year, doy , foldercounter))
406 406 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
407 407 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
408 408 fullfilename = os.path.join( thispath, filename ) #formo el path completo
409 409
410 410 if os.path.exists( fullfilename ): #verifico que exista
411 411 find_flag = True
412 412 break
413 413 if find_flag:
414 414 break
415 415
416 416 if not(find_flag):
417 417 return None, filename
418 418
419 419 return fullfilename, filename
420 420
421 421 def isRadarFolder(folder):
422 422 try:
423 423 year = int(folder[1:5])
424 424 doy = int(folder[5:8])
425 425 except:
426 426 return 0
427 427
428 428 return 1
429 429
430 430 def isRadarFile(file):
431 431 try:
432 432 year = int(file[1:5])
433 433 doy = int(file[5:8])
434 434 set = int(file[8:11])
435 435 except:
436 436 return 0
437 437
438 438 return 1
439 439
440 440 def getDateFromRadarFile(file):
441 441 try:
442 442 year = int(file[1:5])
443 443 doy = int(file[5:8])
444 444 set = int(file[8:11])
445 445 except:
446 446 return None
447 447
448 448 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
449 449 return thisDate
450 450
451 451 def getDateFromRadarFolder(folder):
452 452 try:
453 453 year = int(folder[1:5])
454 454 doy = int(folder[5:8])
455 455 except:
456 456 return None
457 457
458 458 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy-1)
459 459 return thisDate
460 460
461 461 class JRODataIO:
462 462
463 463 c = 3E8
464 464
465 465 isConfig = False
466 466
467 467 basicHeaderObj = None
468 468
469 469 systemHeaderObj = None
470 470
471 471 radarControllerHeaderObj = None
472 472
473 473 processingHeaderObj = None
474 474
475 475 dtype = None
476 476
477 477 pathList = []
478 478
479 479 filenameList = []
480 480
481 481 filename = None
482 482
483 483 ext = None
484 484
485 485 flagIsNewFile = 1
486 486
487 487 flagDiscontinuousBlock = 0
488 488
489 489 flagIsNewBlock = 0
490 490
491 491 fp = None
492 492
493 493 firstHeaderSize = 0
494 494
495 495 basicHeaderSize = 24
496 496
497 497 versionFile = 1103
498 498
499 499 fileSize = None
500 500
501 501 # ippSeconds = None
502 502
503 503 fileSizeByHeader = None
504 504
505 505 fileIndex = None
506 506
507 507 profileIndex = None
508 508
509 509 blockIndex = None
510 510
511 511 nTotalBlocks = None
512 512
513 513 maxTimeStep = 30
514 514
515 515 lastUTTime = None
516 516
517 517 datablock = None
518 518
519 519 dataOut = None
520 520
521 521 blocksize = None
522 522
523 523 getByBlock = False
524 524
525 525 def __init__(self):
526 526
527 527 raise NotImplementedError
528 528
529 529 def run(self):
530 530
531 531 raise NotImplementedError
532 532
533 533 def getDtypeWidth(self):
534 534
535 535 dtype_index = get_dtype_index(self.dtype)
536 536 dtype_width = get_dtype_width(dtype_index)
537 537
538 538 return dtype_width
539 539
540 540 def getAllowedArgs(self):
541 541 return inspect.getargspec(self.run).args
542 542
543 543 class JRODataReader(JRODataIO):
544 544
545 545
546 546 online = 0
547 547
548 548 realtime = 0
549 549
550 550 nReadBlocks = 0
551 551
552 552 delay = 10 #number of seconds waiting a new file
553 553
554 554 nTries = 3 #quantity tries
555 555
556 556 nFiles = 3 #number of files for searching
557 557
558 558 path = None
559 559
560 560 foldercounter = 0
561 561
562 562 flagNoMoreFiles = 0
563 563
564 564 datetimeList = []
565 565
566 566 __isFirstTimeOnline = 1
567 567
568 568 __printInfo = True
569 569
570 570 profileIndex = None
571 571
572 572 nTxs = 1
573 573
574 574 txIndex = None
575 575
576 576 #Added--------------------
577 577
578 578 selBlocksize = None
579 579
580 580 selBlocktime = None
581 581
582 582
583 583 def __init__(self):
584 584
585 585 """
586 586 This class is used to find data files
587 587
588 588 Example:
589 589 reader = JRODataReader()
590 590 fileList = reader.findDataFiles()
591 591
592 592 """
593 593 pass
594 594
595 595
596 596 def createObjByDefault(self):
597 597 """
598 598
599 599 """
600 600 raise NotImplementedError
601 601
602 602 def getBlockDimension(self):
603 603
604 604 raise NotImplementedError
605 605
606 606 def __searchFilesOffLine(self,
607 607 path,
608 608 startDate=None,
609 609 endDate=None,
610 610 startTime=datetime.time(0,0,0),
611 611 endTime=datetime.time(23,59,59),
612 612 set=None,
613 613 expLabel='',
614 614 ext='.r',
615 615 queue=None,
616 616 cursor=None,
617 617 skip=None,
618 618 walk=True):
619 619
620 620 self.filenameList = []
621 621 self.datetimeList = []
622 622
623 623 pathList = []
624 624
625 625 dateList, pathList = self.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
626 626
627 627 if dateList == []:
628 628 # print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
629 629 return None, None
630 630
631 631 if len(dateList) > 1:
632 632 print "[Reading] Data found for date range [%s - %s]: total days = %d" %(startDate, endDate, len(dateList))
633 633 else:
634 634 print "[Reading] Data found for date range [%s - %s]: date = %s" %(startDate, endDate, dateList[0])
635 635
636 636 filenameList = []
637 637 datetimeList = []
638 638
639 639 for thisPath in pathList:
640 640 # thisPath = pathList[pathDict[file]]
641 641
642 642 fileList = glob.glob1(thisPath, "*%s" %ext)
643 643 fileList.sort()
644 644
645 645 skippedFileList = []
646 646
647 647 if cursor is not None and skip is not None:
648 648 # if cursor*skip > len(fileList):
649 649 if skip == 0:
650 650 if queue is not None:
651 651 queue.put(len(fileList))
652 652 skippedFileList = []
653 653 else:
654 654 skippedFileList = fileList[cursor*skip: cursor*skip + skip]
655 655
656 656 else:
657 657 skippedFileList = fileList
658 658
659 659 for file in skippedFileList:
660 660
661 661 filename = os.path.join(thisPath,file)
662 662
663 663 if not isFileInDateRange(filename, startDate, endDate):
664 664 continue
665 665
666 666 thisDatetime = isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
667 667
668 668 if not(thisDatetime):
669 669 continue
670 670
671 671 filenameList.append(filename)
672 672 datetimeList.append(thisDatetime)
673 673
674 674 if not(filenameList):
675 675 print "[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" %(startTime, endTime, ext, path)
676 676 return None, None
677 677
678 678 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
679 679 print
680 680
681 681 for i in range(len(filenameList)):
682 682 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
683 683
684 684 self.filenameList = filenameList
685 685 self.datetimeList = datetimeList
686 686
687 687 return pathList, filenameList
688 688
689 689 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True, set=None):
690 690
691 691 """
692 692 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
693 693 devuelve el archivo encontrado ademas de otros datos.
694 694
695 695 Input:
696 696 path : carpeta donde estan contenidos los files que contiene data
697 697
698 698 expLabel : Nombre del subexperimento (subfolder)
699 699
700 700 ext : extension de los files
701 701
702 702 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
703 703
704 704 Return:
705 705 directory : eL directorio donde esta el file encontrado
706 706 filename : el ultimo file de una determinada carpeta
707 707 year : el anho
708 708 doy : el numero de dia del anho
709 709 set : el set del archivo
710 710
711 711
712 712 """
713 713 if not os.path.isdir(path):
714 714 return None, None, None, None, None, None
715 715
716 716 dirList = []
717 717
718 718 if not walk:
719 719 fullpath = path
720 720 foldercounter = 0
721 721 else:
722 722 #Filtra solo los directorios
723 723 for thisPath in os.listdir(path):
724 724 if not os.path.isdir(os.path.join(path,thisPath)):
725 725 continue
726 726 if not isRadarFolder(thisPath):
727 727 continue
728 728
729 729 dirList.append(thisPath)
730 730
731 731 if not(dirList):
732 732 return None, None, None, None, None, None
733 733
734 734 dirList = sorted( dirList, key=str.lower )
735 735
736 736 doypath = dirList[-1]
737 737 foldercounter = int(doypath.split('_')[1]) if len(doypath.split('_'))>1 else 0
738 738 fullpath = os.path.join(path, doypath, expLabel)
739 739
740 740
741 741 print "[Reading] %s folder was found: " %(fullpath )
742 742
743 743 if set == None:
744 744 filename = getlastFileFromPath(fullpath, ext)
745 745 else:
746 746 filename = getFileFromSet(fullpath, ext, set)
747 747
748 748 if not(filename):
749 749 return None, None, None, None, None, None
750 750
751 751 print "[Reading] %s file was found" %(filename)
752 752
753 753 if not(self.__verifyFile(os.path.join(fullpath, filename))):
754 754 return None, None, None, None, None, None
755 755
756 756 year = int( filename[1:5] )
757 757 doy = int( filename[5:8] )
758 758 set = int( filename[8:11] )
759 759
760 760 return fullpath, foldercounter, filename, year, doy, set
761 761
762 762 def __setNextFileOffline(self):
763 763
764 764 idFile = self.fileIndex
765 765
766 766 while (True):
767 767 idFile += 1
768 768 if not(idFile < len(self.filenameList)):
769 769 self.flagNoMoreFiles = 1
770 770 # print "[Reading] No more Files"
771 771 return 0
772 772
773 773 filename = self.filenameList[idFile]
774 774
775 775 if not(self.__verifyFile(filename)):
776 776 continue
777 777
778 778 fileSize = os.path.getsize(filename)
779 779 fp = open(filename,'rb')
780 780 break
781 781
782 782 self.flagIsNewFile = 1
783 783 self.fileIndex = idFile
784 784 self.filename = filename
785 785 self.fileSize = fileSize
786 786 self.fp = fp
787 787
788 788 # print "[Reading] Setting the file: %s"%self.filename
789 789
790 790 return 1
791 791
792 792 def __setNextFileOnline(self):
793 793 """
794 794 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
795 795 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
796 796 siguientes.
797 797
798 798 Affected:
799 799 self.flagIsNewFile
800 800 self.filename
801 801 self.fileSize
802 802 self.fp
803 803 self.set
804 804 self.flagNoMoreFiles
805 805
806 806 Return:
807 807 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
808 808 1 : si el file fue abierto con exito y esta listo a ser leido
809 809
810 810 Excepciones:
811 811 Si un determinado file no puede ser abierto
812 812 """
813 813 nFiles = 0
814 814 fileOk_flag = False
815 815 firstTime_flag = True
816 816
817 817 self.set += 1
818 818
819 819 if self.set > 999:
820 820 self.set = 0
821 821 self.foldercounter += 1
822 822
823 823 #busca el 1er file disponible
824 824 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
825 825 if fullfilename:
826 826 if self.__verifyFile(fullfilename, False):
827 827 fileOk_flag = True
828 828
829 829 #si no encuentra un file entonces espera y vuelve a buscar
830 830 if not(fileOk_flag):
831 831 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
832 832
833 833 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
834 834 tries = self.nTries
835 835 else:
836 836 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
837 837
838 838 for nTries in range( tries ):
839 839 if firstTime_flag:
840 840 print "\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
841 841 sleep( self.delay )
842 842 else:
843 843 print "\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
844 844
845 845 fullfilename, filename = checkForRealPath( self.path, self.foldercounter, self.year, self.doy, self.set, self.ext )
846 846 if fullfilename:
847 847 if self.__verifyFile(fullfilename):
848 848 fileOk_flag = True
849 849 break
850 850
851 851 if fileOk_flag:
852 852 break
853 853
854 854 firstTime_flag = False
855 855
856 856 print "\t[Reading] Skipping the file \"%s\" due to this file doesn't exist" % filename
857 857 self.set += 1
858 858
859 859 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
860 860 self.set = 0
861 861 self.doy += 1
862 862 self.foldercounter = 0
863 863
864 864 if fileOk_flag:
865 865 self.fileSize = os.path.getsize( fullfilename )
866 866 self.filename = fullfilename
867 867 self.flagIsNewFile = 1
868 868 if self.fp != None: self.fp.close()
869 869 self.fp = open(fullfilename, 'rb')
870 870 self.flagNoMoreFiles = 0
871 871 # print '[Reading] Setting the file: %s' % fullfilename
872 872 else:
873 873 self.fileSize = 0
874 874 self.filename = None
875 875 self.flagIsNewFile = 0
876 876 self.fp = None
877 877 self.flagNoMoreFiles = 1
878 878 # print '[Reading] No more files to read'
879 879
880 880 return fileOk_flag
881 881
882 882 def setNextFile(self):
883 883 if self.fp != None:
884 884 self.fp.close()
885 885
886 886 if self.online:
887 887 newFile = self.__setNextFileOnline()
888 888 else:
889 889 newFile = self.__setNextFileOffline()
890 890
891 891 if not(newFile):
892 892 print '[Reading] No more files to read'
893 893 return 0
894 894
895 895 if self.verbose:
896 896 print '[Reading] Setting the file: %s' % self.filename
897 897
898 898 self.__readFirstHeader()
899 899 self.nReadBlocks = 0
900 900 return 1
901 901
902 902 def __waitNewBlock(self):
903 903 """
904 904 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
905 905
906 906 Si el modo de lectura es OffLine siempre retorn 0
907 907 """
908 908 if not self.online:
909 909 return 0
910 910
911 911 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
912 912 return 0
913 913
914 914 currentPointer = self.fp.tell()
915 915
916 916 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
917 917
918 918 for nTries in range( self.nTries ):
919 919
920 920 self.fp.close()
921 921 self.fp = open( self.filename, 'rb' )
922 922 self.fp.seek( currentPointer )
923 923
924 924 self.fileSize = os.path.getsize( self.filename )
925 925 currentSize = self.fileSize - currentPointer
926 926
927 927 if ( currentSize >= neededSize ):
928 928 self.basicHeaderObj.read(self.fp)
929 929 return 1
930 930
931 931 if self.fileSize == self.fileSizeByHeader:
932 932 # self.flagEoF = True
933 933 return 0
934 934
935 935 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
936 936 sleep( self.delay )
937 937
938 938
939 939 return 0
940 940
941 941 def waitDataBlock(self,pointer_location):
942 942
943 943 currentPointer = pointer_location
944 944
945 945 neededSize = self.processingHeaderObj.blockSize #+ self.basicHeaderSize
946 946
947 947 for nTries in range( self.nTries ):
948 948 self.fp.close()
949 949 self.fp = open( self.filename, 'rb' )
950 950 self.fp.seek( currentPointer )
951 951
952 952 self.fileSize = os.path.getsize( self.filename )
953 953 currentSize = self.fileSize - currentPointer
954 954
955 955 if ( currentSize >= neededSize ):
956 956 return 1
957 957
958 958 print "[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
959 959 sleep( self.delay )
960 960
961 961 return 0
962 962
963 963 def __jumpToLastBlock(self):
964 964
965 965 if not(self.__isFirstTimeOnline):
966 966 return
967 967
968 968 csize = self.fileSize - self.fp.tell()
969 969 blocksize = self.processingHeaderObj.blockSize
970 970
971 971 #salta el primer bloque de datos
972 972 if csize > self.processingHeaderObj.blockSize:
973 973 self.fp.seek(self.fp.tell() + blocksize)
974 974 else:
975 975 return
976 976
977 977 csize = self.fileSize - self.fp.tell()
978 978 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
979 979 while True:
980 980
981 981 if self.fp.tell()<self.fileSize:
982 982 self.fp.seek(self.fp.tell() + neededsize)
983 983 else:
984 984 self.fp.seek(self.fp.tell() - neededsize)
985 985 break
986 986
987 987 # csize = self.fileSize - self.fp.tell()
988 988 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
989 989 # factor = int(csize/neededsize)
990 990 # if factor > 0:
991 991 # self.fp.seek(self.fp.tell() + factor*neededsize)
992 992
993 993 self.flagIsNewFile = 0
994 994 self.__isFirstTimeOnline = 0
995 995
996 996 def __setNewBlock(self):
997 997
998 998 if self.fp == None:
999 999 return 0
1000 1000
1001 1001 # if self.online:
1002 1002 # self.__jumpToLastBlock()
1003 1003
1004 1004 if self.flagIsNewFile:
1005 1005 self.lastUTTime = self.basicHeaderObj.utc
1006 1006 return 1
1007 1007
1008 1008 if self.realtime:
1009 1009 self.flagDiscontinuousBlock = 1
1010 1010 if not(self.setNextFile()):
1011 1011 return 0
1012 1012 else:
1013 1013 return 1
1014 1014
1015 1015 currentSize = self.fileSize - self.fp.tell()
1016 1016 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1017 1017
1018 1018 if (currentSize >= neededSize):
1019 1019 self.basicHeaderObj.read(self.fp)
1020 1020 self.lastUTTime = self.basicHeaderObj.utc
1021 1021 return 1
1022 1022
1023 1023 if self.__waitNewBlock():
1024 1024 self.lastUTTime = self.basicHeaderObj.utc
1025 1025 return 1
1026 1026
1027 1027 if not(self.setNextFile()):
1028 1028 return 0
1029 1029
1030 1030 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
1031 1031 self.lastUTTime = self.basicHeaderObj.utc
1032 1032
1033 1033 self.flagDiscontinuousBlock = 0
1034 1034
1035 1035 if deltaTime > self.maxTimeStep:
1036 1036 self.flagDiscontinuousBlock = 1
1037 1037
1038 1038 return 1
1039 1039
1040 1040 def readNextBlock(self):
1041 1041
1042 1042 #Skip block out of startTime and endTime
1043 1043 while True:
1044 1044 if not(self.__setNewBlock()):
1045 1045 return 0
1046 1046
1047 1047 if not(self.readBlock()):
1048 1048 return 0
1049 1049
1050 1050 self.getBasicHeader()
1051 1051
1052 1052 if not isTimeInRange(self.dataOut.datatime.time(), self.startTime, self.endTime):
1053 1053
1054 1054 print "[Reading] Block No. %d/%d -> %s [Skipping]" %(self.nReadBlocks,
1055 1055 self.processingHeaderObj.dataBlocksPerFile,
1056 1056 self.dataOut.datatime.ctime())
1057 1057 continue
1058 1058
1059 1059 break
1060 1060
1061 if self.verbose:
1062 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1063 self.processingHeaderObj.dataBlocksPerFile,
1064 self.dataOut.datatime.ctime())
1061 # if self.verbose:
1062 # print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1063 # self.processingHeaderObj.dataBlocksPerFile,
1064 # self.dataOut.datatime.ctime())
1065 1065 return 1
1066 1066
1067 1067 def __readFirstHeader(self):
1068 1068
1069 1069 self.basicHeaderObj.read(self.fp)
1070 1070 self.systemHeaderObj.read(self.fp)
1071 1071 self.radarControllerHeaderObj.read(self.fp)
1072 1072 self.processingHeaderObj.read(self.fp)
1073 1073
1074 1074 self.firstHeaderSize = self.basicHeaderObj.size
1075 1075
1076 1076 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
1077 1077 if datatype == 0:
1078 1078 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
1079 1079 elif datatype == 1:
1080 1080 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
1081 1081 elif datatype == 2:
1082 1082 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
1083 1083 elif datatype == 3:
1084 1084 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
1085 1085 elif datatype == 4:
1086 1086 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
1087 1087 elif datatype == 5:
1088 1088 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
1089 1089 else:
1090 1090 raise ValueError, 'Data type was not defined'
1091 1091
1092 1092 self.dtype = datatype_str
1093 1093 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1094 1094 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
1095 1095 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1096 1096 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1097 1097 self.getBlockDimension()
1098 1098
1099 1099 def __verifyFile(self, filename, msgFlag=True):
1100 1100
1101 1101 msg = None
1102 1102
1103 1103 try:
1104 1104 fp = open(filename, 'rb')
1105 1105 except IOError:
1106 1106
1107 1107 if msgFlag:
1108 1108 print "[Reading] File %s can't be opened" % (filename)
1109 1109
1110 1110 return False
1111 1111
1112 1112 currentPosition = fp.tell()
1113 1113 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1114 1114
1115 1115 if neededSize == 0:
1116 1116 basicHeaderObj = BasicHeader(LOCALTIME)
1117 1117 systemHeaderObj = SystemHeader()
1118 1118 radarControllerHeaderObj = RadarControllerHeader()
1119 1119 processingHeaderObj = ProcessingHeader()
1120 1120
1121 1121 if not( basicHeaderObj.read(fp) ):
1122 1122 fp.close()
1123 1123 return False
1124 1124
1125 1125 if not( systemHeaderObj.read(fp) ):
1126 1126 fp.close()
1127 1127 return False
1128 1128
1129 1129 if not( radarControllerHeaderObj.read(fp) ):
1130 1130 fp.close()
1131 1131 return False
1132 1132
1133 1133 if not( processingHeaderObj.read(fp) ):
1134 1134 fp.close()
1135 1135 return False
1136 1136
1137 1137 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1138 1138 else:
1139 1139 msg = "[Reading] Skipping the file %s due to it hasn't enough data" %filename
1140 1140
1141 1141 fp.close()
1142 1142
1143 1143 fileSize = os.path.getsize(filename)
1144 1144 currentSize = fileSize - currentPosition
1145 1145
1146 1146 if currentSize < neededSize:
1147 1147 if msgFlag and (msg != None):
1148 1148 print msg
1149 1149 return False
1150 1150
1151 1151 return True
1152 1152
1153 1153 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1154 1154
1155 1155 path_empty = True
1156 1156
1157 1157 dateList = []
1158 1158 pathList = []
1159 1159
1160 1160 multi_path = path.split(',')
1161 1161
1162 1162 if not walk:
1163 1163
1164 1164 for single_path in multi_path:
1165 1165
1166 1166 if not os.path.isdir(single_path):
1167 1167 continue
1168 1168
1169 1169 fileList = glob.glob1(single_path, "*"+ext)
1170 1170
1171 1171 if not fileList:
1172 1172 continue
1173 1173
1174 1174 path_empty = False
1175 1175
1176 1176 fileList.sort()
1177 1177
1178 1178 for thisFile in fileList:
1179 1179
1180 1180 if not os.path.isfile(os.path.join(single_path, thisFile)):
1181 1181 continue
1182 1182
1183 1183 if not isRadarFile(thisFile):
1184 1184 continue
1185 1185
1186 1186 if not isFileInDateRange(thisFile, startDate, endDate):
1187 1187 continue
1188 1188
1189 1189 thisDate = getDateFromRadarFile(thisFile)
1190 1190
1191 1191 if thisDate in dateList:
1192 1192 continue
1193 1193
1194 1194 dateList.append(thisDate)
1195 1195 pathList.append(single_path)
1196 1196
1197 1197 else:
1198 1198 for single_path in multi_path:
1199 1199
1200 1200 if not os.path.isdir(single_path):
1201 1201 continue
1202 1202
1203 1203 dirList = []
1204 1204
1205 1205 for thisPath in os.listdir(single_path):
1206 1206
1207 1207 if not os.path.isdir(os.path.join(single_path,thisPath)):
1208 1208 continue
1209 1209
1210 1210 if not isRadarFolder(thisPath):
1211 1211 continue
1212 1212
1213 1213 if not isFolderInDateRange(thisPath, startDate, endDate):
1214 1214 continue
1215 1215
1216 1216 dirList.append(thisPath)
1217 1217
1218 1218 if not dirList:
1219 1219 continue
1220 1220
1221 1221 dirList.sort()
1222 1222
1223 1223 for thisDir in dirList:
1224 1224
1225 1225 datapath = os.path.join(single_path, thisDir, expLabel)
1226 1226 fileList = glob.glob1(datapath, "*"+ext)
1227 1227
1228 1228 if not fileList:
1229 1229 continue
1230 1230
1231 1231 path_empty = False
1232 1232
1233 1233 thisDate = getDateFromRadarFolder(thisDir)
1234 1234
1235 1235 pathList.append(datapath)
1236 1236 dateList.append(thisDate)
1237 1237
1238 1238 dateList.sort()
1239 1239
1240 1240 if walk:
1241 1241 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1242 1242 else:
1243 1243 pattern_path = multi_path[0]
1244 1244
1245 1245 if path_empty:
1246 1246 print "[Reading] No *%s files in %s for %s to %s" %(ext, pattern_path, startDate, endDate)
1247 1247 else:
1248 1248 if not dateList:
1249 1249 print "[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" %(startDate, endDate, ext, path)
1250 1250
1251 1251 if include_path:
1252 1252 return dateList, pathList
1253 1253
1254 1254 return dateList
1255 1255
1256 1256 def setup(self,
1257 1257 path=None,
1258 1258 startDate=None,
1259 1259 endDate=None,
1260 1260 startTime=datetime.time(0,0,0),
1261 1261 endTime=datetime.time(23,59,59),
1262 1262 set=None,
1263 1263 expLabel = "",
1264 1264 ext = None,
1265 1265 online = False,
1266 1266 delay = 60,
1267 1267 walk = True,
1268 1268 getblock = False,
1269 1269 nTxs = 1,
1270 1270 realtime=False,
1271 1271 blocksize=None,
1272 1272 blocktime=None,
1273 1273 queue=None,
1274 1274 skip=None,
1275 1275 cursor=None,
1276 1276 warnings=True,
1277 1277 verbose=True):
1278 1278
1279 1279 if path == None:
1280 1280 raise ValueError, "[Reading] The path is not valid"
1281 1281
1282 1282 if ext == None:
1283 1283 ext = self.ext
1284 1284
1285 1285 if online:
1286 1286 print "[Reading] Searching files in online mode..."
1287 1287
1288 1288 for nTries in range( self.nTries ):
1289 1289 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1290 1290
1291 1291 if fullpath:
1292 1292 break
1293 1293
1294 1294 print '[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
1295 1295 sleep( self.delay )
1296 1296
1297 1297 if not(fullpath):
1298 1298 print "[Reading] There 'isn't any valid file in %s" % path
1299 1299 return
1300 1300
1301 1301 self.year = year
1302 1302 self.doy = doy
1303 1303 self.set = set - 1
1304 1304 self.path = path
1305 1305 self.foldercounter = foldercounter
1306 1306 last_set = None
1307 1307
1308 1308 else:
1309 1309 print "[Reading] Searching files in offline mode ..."
1310 1310 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1311 1311 startTime=startTime, endTime=endTime,
1312 1312 set=set, expLabel=expLabel, ext=ext,
1313 1313 walk=walk, cursor=cursor,
1314 1314 skip=skip, queue=queue)
1315 1315
1316 1316 if not(pathList):
1317 1317 # print "[Reading] No *%s files in %s (%s - %s)"%(ext, path,
1318 1318 # datetime.datetime.combine(startDate,startTime).ctime(),
1319 1319 # datetime.datetime.combine(endDate,endTime).ctime())
1320 1320
1321 1321 # sys.exit(-1)
1322 1322
1323 1323 self.fileIndex = -1
1324 1324 self.pathList = []
1325 1325 self.filenameList = []
1326 1326 return
1327 1327
1328 1328 self.fileIndex = -1
1329 1329 self.pathList = pathList
1330 1330 self.filenameList = filenameList
1331 1331 file_name = os.path.basename(filenameList[-1])
1332 1332 basename, ext = os.path.splitext(file_name)
1333 1333 last_set = int(basename[-3:])
1334 1334
1335 1335 self.online = online
1336 1336 self.realtime = realtime
1337 1337 self.delay = delay
1338 1338 ext = ext.lower()
1339 1339 self.ext = ext
1340 1340 self.getByBlock = getblock
1341 1341 self.nTxs = nTxs
1342 1342 self.startTime = startTime
1343 1343 self.endTime = endTime
1344 1344
1345 1345 #Added-----------------
1346 1346 self.selBlocksize = blocksize
1347 1347 self.selBlocktime = blocktime
1348 1348
1349 1349 # Verbose-----------
1350 1350 self.verbose = verbose
1351 1351 self.warnings = warnings
1352 1352
1353 1353 if not(self.setNextFile()):
1354 1354 if (startDate!=None) and (endDate!=None):
1355 1355 print "[Reading] No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
1356 1356 elif startDate != None:
1357 1357 print "[Reading] No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
1358 1358 else:
1359 1359 print "[Reading] No files"
1360 1360
1361 1361 self.fileIndex = -1
1362 1362 self.pathList = []
1363 1363 self.filenameList = []
1364 1364 return
1365 1365
1366 1366 # self.getBasicHeader()
1367 1367
1368 1368 if last_set != None:
1369 1369 self.dataOut.last_block = last_set * self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1370 1370 return
1371 1371
1372 1372 def getBasicHeader(self):
1373 1373
1374 1374 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1375 1375
1376 1376 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1377 1377
1378 1378 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1379 1379
1380 1380 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1381 1381
1382 1382 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1383 1383
1384 1384 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1385 1385
1386 1386 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
1387 1387
1388 1388 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1389 1389
1390 1390
1391 1391 def getFirstHeader(self):
1392 1392
1393 1393 raise NotImplementedError
1394 1394
1395 1395 def getData(self):
1396 1396
1397 1397 raise NotImplementedError
1398 1398
1399 1399 def hasNotDataInBuffer(self):
1400 1400
1401 1401 raise NotImplementedError
1402 1402
1403 1403 def readBlock(self):
1404 1404
1405 1405 raise NotImplementedError
1406 1406
1407 1407 def isEndProcess(self):
1408 1408
1409 1409 return self.flagNoMoreFiles
1410 1410
1411 1411 def printReadBlocks(self):
1412 1412
1413 1413 print "[Reading] Number of read blocks per file %04d" %self.nReadBlocks
1414 1414
1415 1415 def printTotalBlocks(self):
1416 1416
1417 1417 print "[Reading] Number of read blocks %04d" %self.nTotalBlocks
1418 1418
1419 1419 def printNumberOfBlock(self):
1420 'SPAM!'
1420 1421
1421 if self.flagIsNewBlock:
1422 print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1423 self.processingHeaderObj.dataBlocksPerFile,
1424 self.dataOut.datatime.ctime())
1422 # if self.flagIsNewBlock:
1423 # print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1424 # self.processingHeaderObj.dataBlocksPerFile,
1425 # self.dataOut.datatime.ctime())
1425 1426
1426 1427 def printInfo(self):
1427 1428
1428 1429 if self.__printInfo == False:
1429 1430 return
1430 1431
1431 1432 self.basicHeaderObj.printInfo()
1432 1433 self.systemHeaderObj.printInfo()
1433 1434 self.radarControllerHeaderObj.printInfo()
1434 1435 self.processingHeaderObj.printInfo()
1435 1436
1436 1437 self.__printInfo = False
1437 1438
1438 1439
1439 1440 def run(self,
1440 1441 path=None,
1441 1442 startDate=None,
1442 1443 endDate=None,
1443 1444 startTime=datetime.time(0,0,0),
1444 1445 endTime=datetime.time(23,59,59),
1445 1446 set=None,
1446 1447 expLabel = "",
1447 1448 ext = None,
1448 1449 online = False,
1449 1450 delay = 60,
1450 1451 walk = True,
1451 1452 getblock = False,
1452 1453 nTxs = 1,
1453 1454 realtime=False,
1454 1455 blocksize=None,
1455 1456 blocktime=None,
1456 1457 queue=None,
1457 1458 skip=None,
1458 1459 cursor=None,
1459 1460 warnings=True,
1460 1461 verbose=True, **kwargs):
1461 1462
1462 1463 if not(self.isConfig):
1463 1464 # self.dataOut = dataOut
1464 1465 self.setup( path=path,
1465 1466 startDate=startDate,
1466 1467 endDate=endDate,
1467 1468 startTime=startTime,
1468 1469 endTime=endTime,
1469 1470 set=set,
1470 1471 expLabel=expLabel,
1471 1472 ext=ext,
1472 1473 online=online,
1473 1474 delay=delay,
1474 1475 walk=walk,
1475 1476 getblock=getblock,
1476 1477 nTxs=nTxs,
1477 1478 realtime=realtime,
1478 1479 blocksize=blocksize,
1479 1480 blocktime=blocktime,
1480 1481 queue=queue,
1481 1482 skip=skip,
1482 1483 cursor=cursor,
1483 1484 warnings=warnings,
1484 1485 verbose=verbose)
1485 1486 self.isConfig = True
1486 1487
1487 1488 self.getData()
1488 1489
1489 1490 class JRODataWriter(JRODataIO):
1490 1491
1491 1492 """
1492 1493 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1493 1494 de los datos siempre se realiza por bloques.
1494 1495 """
1495 1496
1496 1497 blockIndex = 0
1497 1498
1498 1499 path = None
1499 1500
1500 1501 setFile = None
1501 1502
1502 1503 profilesPerBlock = None
1503 1504
1504 1505 blocksPerFile = None
1505 1506
1506 1507 nWriteBlocks = 0
1507 1508
1508 1509 fileDate = None
1509 1510
1510 1511 def __init__(self, dataOut=None):
1511 1512 raise NotImplementedError
1512 1513
1513 1514
1514 1515 def hasAllDataInBuffer(self):
1515 1516 raise NotImplementedError
1516 1517
1517 1518
1518 1519 def setBlockDimension(self):
1519 1520 raise NotImplementedError
1520 1521
1521 1522
1522 1523 def writeBlock(self):
1523 1524 raise NotImplementedError
1524 1525
1525 1526
1526 1527 def putData(self):
1527 1528 raise NotImplementedError
1528 1529
1529 1530
1530 1531 def getProcessFlags(self):
1531 1532
1532 1533 processFlags = 0
1533 1534
1534 1535 dtype_index = get_dtype_index(self.dtype)
1535 1536 procflag_dtype = get_procflag_dtype(dtype_index)
1536 1537
1537 1538 processFlags += procflag_dtype
1538 1539
1539 1540 if self.dataOut.flagDecodeData:
1540 1541 processFlags += PROCFLAG.DECODE_DATA
1541 1542
1542 1543 if self.dataOut.flagDeflipData:
1543 1544 processFlags += PROCFLAG.DEFLIP_DATA
1544 1545
1545 1546 if self.dataOut.code is not None:
1546 1547 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1547 1548
1548 1549 if self.dataOut.nCohInt > 1:
1549 1550 processFlags += PROCFLAG.COHERENT_INTEGRATION
1550 1551
1551 1552 if self.dataOut.type == "Spectra":
1552 1553 if self.dataOut.nIncohInt > 1:
1553 1554 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1554 1555
1555 1556 if self.dataOut.data_dc is not None:
1556 1557 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1557 1558
1558 1559 if self.dataOut.flagShiftFFT:
1559 1560 processFlags += PROCFLAG.SHIFT_FFT_DATA
1560 1561
1561 1562 return processFlags
1562 1563
1563 1564 def setBasicHeader(self):
1564 1565
1565 1566 self.basicHeaderObj.size = self.basicHeaderSize #bytes
1566 1567 self.basicHeaderObj.version = self.versionFile
1567 1568 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1568 1569
1569 1570 utc = numpy.floor(self.dataOut.utctime)
1570 1571 milisecond = (self.dataOut.utctime - utc)* 1000.0
1571 1572
1572 1573 self.basicHeaderObj.utc = utc
1573 1574 self.basicHeaderObj.miliSecond = milisecond
1574 1575 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1575 1576 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1576 1577 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1577 1578
1578 1579 def setFirstHeader(self):
1579 1580 """
1580 1581 Obtiene una copia del First Header
1581 1582
1582 1583 Affected:
1583 1584
1584 1585 self.basicHeaderObj
1585 1586 self.systemHeaderObj
1586 1587 self.radarControllerHeaderObj
1587 1588 self.processingHeaderObj self.
1588 1589
1589 1590 Return:
1590 1591 None
1591 1592 """
1592 1593
1593 1594 raise NotImplementedError
1594 1595
1595 1596 def __writeFirstHeader(self):
1596 1597 """
1597 1598 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1598 1599
1599 1600 Affected:
1600 1601 __dataType
1601 1602
1602 1603 Return:
1603 1604 None
1604 1605 """
1605 1606
1606 1607 # CALCULAR PARAMETROS
1607 1608
1608 1609 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1609 1610 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1610 1611
1611 1612 self.basicHeaderObj.write(self.fp)
1612 1613 self.systemHeaderObj.write(self.fp)
1613 1614 self.radarControllerHeaderObj.write(self.fp)
1614 1615 self.processingHeaderObj.write(self.fp)
1615 1616
1616 1617 def __setNewBlock(self):
1617 1618 """
1618 1619 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1619 1620
1620 1621 Return:
1621 1622 0 : si no pudo escribir nada
1622 1623 1 : Si escribio el Basic el First Header
1623 1624 """
1624 1625 if self.fp == None:
1625 1626 self.setNextFile()
1626 1627
1627 1628 if self.flagIsNewFile:
1628 1629 return 1
1629 1630
1630 1631 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1631 1632 self.basicHeaderObj.write(self.fp)
1632 1633 return 1
1633 1634
1634 1635 if not( self.setNextFile() ):
1635 1636 return 0
1636 1637
1637 1638 return 1
1638 1639
1639 1640
1640 1641 def writeNextBlock(self):
1641 1642 """
1642 1643 Selecciona el bloque siguiente de datos y los escribe en un file
1643 1644
1644 1645 Return:
1645 1646 0 : Si no hizo pudo escribir el bloque de datos
1646 1647 1 : Si no pudo escribir el bloque de datos
1647 1648 """
1648 1649 if not( self.__setNewBlock() ):
1649 1650 return 0
1650 1651
1651 1652 self.writeBlock()
1652 1653
1653 1654 print "[Writing] Block No. %d/%d" %(self.blockIndex,
1654 1655 self.processingHeaderObj.dataBlocksPerFile)
1655 1656
1656 1657 return 1
1657 1658
1658 1659 def setNextFile(self):
1659 1660 """
1660 1661 Determina el siguiente file que sera escrito
1661 1662
1662 1663 Affected:
1663 1664 self.filename
1664 1665 self.subfolder
1665 1666 self.fp
1666 1667 self.setFile
1667 1668 self.flagIsNewFile
1668 1669
1669 1670 Return:
1670 1671 0 : Si el archivo no puede ser escrito
1671 1672 1 : Si el archivo esta listo para ser escrito
1672 1673 """
1673 1674 ext = self.ext
1674 1675 path = self.path
1675 1676
1676 1677 if self.fp != None:
1677 1678 self.fp.close()
1678 1679
1679 1680 timeTuple = time.localtime( self.dataOut.utctime)
1680 1681 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1681 1682
1682 1683 fullpath = os.path.join( path, subfolder )
1683 1684 setFile = self.setFile
1684 1685
1685 1686 if not( os.path.exists(fullpath) ):
1686 1687 os.mkdir(fullpath)
1687 1688 setFile = -1 #inicializo mi contador de seteo
1688 1689 else:
1689 1690 filesList = os.listdir( fullpath )
1690 1691 if len( filesList ) > 0:
1691 1692 filesList = sorted( filesList, key=str.lower )
1692 1693 filen = filesList[-1]
1693 1694 # el filename debera tener el siguiente formato
1694 1695 # 0 1234 567 89A BCDE (hex)
1695 1696 # x YYYY DDD SSS .ext
1696 1697 if isNumber( filen[8:11] ):
1697 1698 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1698 1699 else:
1699 1700 setFile = -1
1700 1701 else:
1701 1702 setFile = -1 #inicializo mi contador de seteo
1702 1703
1703 1704 setFile += 1
1704 1705
1705 1706 #If this is a new day it resets some values
1706 1707 if self.dataOut.datatime.date() > self.fileDate:
1707 1708 setFile = 0
1708 1709 self.nTotalBlocks = 0
1709 1710
1710 1711 filen = '%s%4.4d%3.3d%3.3d%s' % (self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext )
1711 1712
1712 1713 filename = os.path.join( path, subfolder, filen )
1713 1714
1714 1715 fp = open( filename,'wb' )
1715 1716
1716 1717 self.blockIndex = 0
1717 1718
1718 1719 #guardando atributos
1719 1720 self.filename = filename
1720 1721 self.subfolder = subfolder
1721 1722 self.fp = fp
1722 1723 self.setFile = setFile
1723 1724 self.flagIsNewFile = 1
1724 1725 self.fileDate = self.dataOut.datatime.date()
1725 1726
1726 1727 self.setFirstHeader()
1727 1728
1728 1729 print '[Writing] Opening file: %s'%self.filename
1729 1730
1730 1731 self.__writeFirstHeader()
1731 1732
1732 1733 return 1
1733 1734
1734 1735 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1735 1736 """
1736 1737 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1737 1738
1738 1739 Inputs:
1739 1740 path : directory where data will be saved
1740 1741 profilesPerBlock : number of profiles per block
1741 1742 set : initial file set
1742 1743 datatype : An integer number that defines data type:
1743 1744 0 : int8 (1 byte)
1744 1745 1 : int16 (2 bytes)
1745 1746 2 : int32 (4 bytes)
1746 1747 3 : int64 (8 bytes)
1747 1748 4 : float32 (4 bytes)
1748 1749 5 : double64 (8 bytes)
1749 1750
1750 1751 Return:
1751 1752 0 : Si no realizo un buen seteo
1752 1753 1 : Si realizo un buen seteo
1753 1754 """
1754 1755
1755 1756 if ext == None:
1756 1757 ext = self.ext
1757 1758
1758 1759 self.ext = ext.lower()
1759 1760
1760 1761 self.path = path
1761 1762
1762 1763 if set is None:
1763 1764 self.setFile = -1
1764 1765 else:
1765 1766 self.setFile = set - 1
1766 1767
1767 1768 self.blocksPerFile = blocksPerFile
1768 1769
1769 1770 self.profilesPerBlock = profilesPerBlock
1770 1771
1771 1772 self.dataOut = dataOut
1772 1773 self.fileDate = self.dataOut.datatime.date()
1773 1774 #By default
1774 1775 self.dtype = self.dataOut.dtype
1775 1776
1776 1777 if datatype is not None:
1777 1778 self.dtype = get_numpy_dtype(datatype)
1778 1779
1779 1780 if not(self.setNextFile()):
1780 1781 print "[Writing] There isn't a next file"
1781 1782 return 0
1782 1783
1783 1784 self.setBlockDimension()
1784 1785
1785 1786 return 1
1786 1787
1787 1788 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1788 1789
1789 1790 if not(self.isConfig):
1790 1791
1791 1792 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock, set=set, ext=ext, datatype=datatype, **kwargs)
1792 1793 self.isConfig = True
1793 1794
1794 1795 self.putData()
1 NO CONTENT: modified file, binary diff hidden
@@ -1,1191 +1,1193
1 1 import os, sys
2 2 import glob
3 3 import fnmatch
4 4 import datetime
5 5 import time
6 6 import re
7 7 import h5py
8 8 import numpy
9 9 import matplotlib.pyplot as plt
10 10
11 11 import pylab as plb
12 12 from scipy.optimize import curve_fit
13 13 from scipy import asarray as ar,exp
14 14 from scipy import stats
15 15
16 16 from duplicity.path import Path
17 17 from numpy.ma.core import getdata
18 18
19 19 SPEED_OF_LIGHT = 299792458
20 20 SPEED_OF_LIGHT = 3e8
21 21
22 22 try:
23 23 from gevent import sleep
24 24 except:
25 25 from time import sleep
26 26
27 27 from schainpy.model.data.jrodata import Spectra
28 28 #from schainpy.model.data.BLTRheaderIO import FileHeader, RecordHeader
29 29 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
30 30 #from schainpy.model.io.jroIO_bltr import BLTRReader
31 31 from numpy import imag, shape, NaN
32 32
33 from jroIO_base import JRODataReader
33 34
34 35
35 36 class Header(object):
36 37
37 38 def __init__(self):
38 39 raise NotImplementedError
39 40
40 41
41 42 def read(self):
42 43
43 44 raise NotImplementedError
44 45
45 46 def write(self):
46 47
47 48 raise NotImplementedError
48 49
49 50 def printInfo(self):
50 51
51 52 message = "#"*50 + "\n"
52 53 message += self.__class__.__name__.upper() + "\n"
53 54 message += "#"*50 + "\n"
54 55
55 56 keyList = self.__dict__.keys()
56 57 keyList.sort()
57 58
58 59 for key in keyList:
59 60 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
60 61
61 62 if "size" not in keyList:
62 63 attr = getattr(self, "size")
63 64
64 65 if attr:
65 66 message += "%s = %s" %("size", attr) + "\n"
66 67
67 68 #print message
68 69
69 70
70 71
71 72
72 73
73 74 FILE_STRUCTURE = numpy.dtype([ #HEADER 48bytes
74 75 ('FileMgcNumber','<u4'), #0x23020100
75 76 ('nFDTdataRecors','<u4'), #No Of FDT data records in this file (0 or more)
76 77 ('OffsetStartHeader','<u4'),
77 78 ('RadarUnitId','<u4'),
78 79 ('SiteName',numpy.str_,32), #Null terminated
79 80 ])
80 81
81 82 class FileHeaderBLTR(Header):
82 83
83 84 def __init__(self):
84 85
85 86 self.FileMgcNumber= 0 #0x23020100
86 87 self.nFDTdataRecors=0 #No Of FDT data records in this file (0 or more)
87 88 self.RadarUnitId= 0
88 89 self.OffsetStartHeader=0
89 90 self.SiteName= ""
90 91 self.size = 48
91 92
92 93 def FHread(self, fp):
93 94 #try:
94 95 startFp = open(fp,"rb")
95 96
96 97 header = numpy.fromfile(startFp, FILE_STRUCTURE,1)
97 98
98 99 print ' '
99 100 print 'puntero file header', startFp.tell()
100 101 print ' '
101 102
102 103
103 104 ''' numpy.fromfile(file, dtype, count, sep='')
104 105 file : file or str
105 106 Open file object or filename.
106 107
107 108 dtype : data-type
108 109 Data type of the returned array. For binary files, it is used to determine
109 110 the size and byte-order of the items in the file.
110 111
111 112 count : int
112 113 Number of items to read. -1 means all items (i.e., the complete file).
113 114
114 115 sep : str
115 116 Separator between items if file is a text file. Empty ("") separator means
116 117 the file should be treated as binary. Spaces (" ") in the separator match zero
117 118 or more whitespace characters. A separator consisting only of spaces must match
118 119 at least one whitespace.
119 120
120 121 '''
121 122
122 123
123 124
124 125 self.FileMgcNumber= hex(header['FileMgcNumber'][0])
125 126 self.nFDTdataRecors=int(header['nFDTdataRecors'][0]) #No Of FDT data records in this file (0 or more)
126 127 self.RadarUnitId= int(header['RadarUnitId'][0])
127 128 self.OffsetStartHeader= int(header['OffsetStartHeader'][0])
128 129 self.SiteName= str(header['SiteName'][0])
129 130
130 131 #print 'Numero de bloques', self.nFDTdataRecors
131 132
132 133
133 134 if self.size <48:
134 135 return 0
135 136
136 137 return 1
137 138
138 139
139 140 def write(self, fp):
140 141
141 142 headerTuple = (self.FileMgcNumber,
142 143 self.nFDTdataRecors,
143 144 self.RadarUnitId,
144 145 self.SiteName,
145 146 self.size)
146 147
147 148
148 149 header = numpy.array(headerTuple, FILE_STRUCTURE)
149 150 # numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
150 151 header.tofile(fp)
151 152 ''' ndarray.tofile(fid, sep, format) Write array to a file as text or binary (default).
152 153
153 154 fid : file or str
154 155 An open file object, or a string containing a filename.
155 156
156 157 sep : str
157 158 Separator between array items for text output. If "" (empty), a binary file is written,
158 159 equivalent to file.write(a.tobytes()).
159 160
160 161 format : str
161 162 Format string for text file output. Each entry in the array is formatted to text by
162 163 first converting it to the closest Python type, and then using "format" % item.
163 164
164 165 '''
165 166
166 167 return 1
167 168
168 169
169 170
170 171
171 172
172 173 RECORD_STRUCTURE = numpy.dtype([ #RECORD HEADER 180+20N bytes
173 174 ('RecMgcNumber','<u4'), #0x23030001
174 175 ('RecCounter','<u4'), #Record counter(0,1, ...)
175 176 ('Off2StartNxtRec','<u4'), #Offset to start of next record form start of this record
176 177 ('Off2StartData','<u4'), #Offset to start of data from start of this record
177 178 ('nUtime','<i4'), #Epoch time stamp of start of acquisition (seconds)
178 179 ('nMilisec','<u4'), #Millisecond component of time stamp (0,...,999)
179 180 ('ExpTagName',numpy.str_,32), #Experiment tag name (null terminated)
180 181 ('ExpComment',numpy.str_,32), #Experiment comment (null terminated)
181 182 ('SiteLatDegrees','<f4'), #Site latitude (from GPS) in degrees (positive implies North)
182 183 ('SiteLongDegrees','<f4'), #Site longitude (from GPS) in degrees (positive implies East)
183 184 ('RTCgpsStatus','<u4'), #RTC GPS engine status (0=SEEK, 1=LOCK, 2=NOT FITTED, 3=UNAVAILABLE)
184 185 ('TransmitFrec','<u4'), #Transmit frequency (Hz)
185 186 ('ReceiveFrec','<u4'), #Receive frequency
186 187 ('FirstOsciFrec','<u4'), #First local oscillator frequency (Hz)
187 188 ('Polarisation','<u4'), #(0="O", 1="E", 2="linear 1", 3="linear2")
188 189 ('ReceiverFiltSett','<u4'), #Receiver filter settings (0,1,2,3)
189 190 ('nModesInUse','<u4'), #Number of modes in use (1 or 2)
190 191 ('DualModeIndex','<u4'), #Dual Mode index number for these data (0 or 1)
191 192 ('DualModeRange','<u4'), #Dual Mode range correction for these data (m)
192 193 ('nDigChannels','<u4'), #Number of digital channels acquired (2*N)
193 194 ('SampResolution','<u4'), #Sampling resolution (meters)
194 195 ('nHeights','<u4'), #Number of range gates sampled
195 196 ('StartRangeSamp','<u4'), #Start range of sampling (meters)
196 197 ('PRFhz','<u4'), #PRF (Hz)
197 198 ('nCohInt','<u4'), #Integrations
198 199 ('nProfiles','<u4'), #Number of data points transformed
199 200 ('nChannels','<u4'), #Number of receive beams stored in file (1 or N)
200 201 ('nIncohInt','<u4'), #Number of spectral averages
201 202 ('FFTwindowingInd','<u4'), #FFT windowing index (0 = no window)
202 203 ('BeamAngleAzim','<f4'), #Beam steer angle (azimuth) in degrees (clockwise from true North)
203 204 ('BeamAngleZen','<f4'), #Beam steer angle (zenith) in degrees (0=> vertical)
204 205 ('AntennaCoord0','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
205 206 ('AntennaAngl0','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
206 207 ('AntennaCoord1','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
207 208 ('AntennaAngl1','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
208 209 ('AntennaCoord2','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
209 210 ('AntennaAngl2','<f4'), #Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
210 211 ('RecPhaseCalibr0','<f4'), #Receiver phase calibration (degrees) - N values
211 212 ('RecPhaseCalibr1','<f4'), #Receiver phase calibration (degrees) - N values
212 213 ('RecPhaseCalibr2','<f4'), #Receiver phase calibration (degrees) - N values
213 214 ('RecAmpCalibr0','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
214 215 ('RecAmpCalibr1','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
215 216 ('RecAmpCalibr2','<f4'), #Receiver amplitude calibration (ratio relative to receiver one) - N values
216 217 ('ReceiverGaindB0','<i4'), #Receiver gains in dB - N values
217 218 ('ReceiverGaindB1','<i4'), #Receiver gains in dB - N values
218 219 ('ReceiverGaindB2','<i4'), #Receiver gains in dB - N values
219 220 ])
220 221
221 222
222 223 class RecordHeaderBLTR(Header):
223 224
224 225 def __init__(self, RecMgcNumber=None, RecCounter= 0, Off2StartNxtRec= 811248,
225 226 nUtime= 0, nMilisec= 0, ExpTagName= None,
226 227 ExpComment=None, SiteLatDegrees=0, SiteLongDegrees= 0,
227 228 RTCgpsStatus= 0, TransmitFrec= 0, ReceiveFrec= 0,
228 229 FirstOsciFrec= 0, Polarisation= 0, ReceiverFiltSett= 0,
229 230 nModesInUse= 0, DualModeIndex= 0, DualModeRange= 0,
230 231 nDigChannels= 0, SampResolution= 0, nHeights= 0,
231 232 StartRangeSamp= 0, PRFhz= 0, nCohInt= 0,
232 233 nProfiles= 0, nChannels= 0, nIncohInt= 0,
233 234 FFTwindowingInd= 0, BeamAngleAzim= 0, BeamAngleZen= 0,
234 235 AntennaCoord0= 0, AntennaCoord1= 0, AntennaCoord2= 0,
235 236 RecPhaseCalibr0= 0, RecPhaseCalibr1= 0, RecPhaseCalibr2= 0,
236 237 RecAmpCalibr0= 0, RecAmpCalibr1= 0, RecAmpCalibr2= 0,
237 238 AntennaAngl0=0, AntennaAngl1=0, AntennaAngl2=0,
238 239 ReceiverGaindB0= 0, ReceiverGaindB1= 0, ReceiverGaindB2= 0, Off2StartData=0, OffsetStartHeader=0):
239 240
240 241 self.RecMgcNumber = RecMgcNumber #0x23030001
241 242 self.RecCounter = RecCounter
242 243 self.Off2StartNxtRec = Off2StartNxtRec
243 244 self.Off2StartData = Off2StartData
244 245 self.nUtime = nUtime
245 246 self.nMilisec = nMilisec
246 247 self.ExpTagName = ExpTagName
247 248 self.ExpComment = ExpComment
248 249 self.SiteLatDegrees = SiteLatDegrees
249 250 self.SiteLongDegrees = SiteLongDegrees
250 251 self.RTCgpsStatus = RTCgpsStatus
251 252 self.TransmitFrec = TransmitFrec
252 253 self.ReceiveFrec = ReceiveFrec
253 254 self.FirstOsciFrec = FirstOsciFrec
254 255 self.Polarisation = Polarisation
255 256 self.ReceiverFiltSett = ReceiverFiltSett
256 257 self.nModesInUse = nModesInUse
257 258 self.DualModeIndex = DualModeIndex
258 259 self.DualModeRange = DualModeRange
259 260 self.nDigChannels = nDigChannels
260 261 self.SampResolution = SampResolution
261 262 self.nHeights = nHeights
262 263 self.StartRangeSamp = StartRangeSamp
263 264 self.PRFhz = PRFhz
264 265 self.nCohInt = nCohInt
265 266 self.nProfiles = nProfiles
266 267 self.nChannels = nChannels
267 268 self.nIncohInt = nIncohInt
268 269 self.FFTwindowingInd = FFTwindowingInd
269 270 self.BeamAngleAzim = BeamAngleAzim
270 271 self.BeamAngleZen = BeamAngleZen
271 272 self.AntennaCoord0 = AntennaCoord0
272 273 self.AntennaAngl0 = AntennaAngl0
273 274 self.AntennaAngl1 = AntennaAngl1
274 275 self.AntennaAngl2 = AntennaAngl2
275 276 self.AntennaCoord1 = AntennaCoord1
276 277 self.AntennaCoord2 = AntennaCoord2
277 278 self.RecPhaseCalibr0 = RecPhaseCalibr0
278 279 self.RecPhaseCalibr1 = RecPhaseCalibr1
279 280 self.RecPhaseCalibr2 = RecPhaseCalibr2
280 281 self.RecAmpCalibr0 = RecAmpCalibr0
281 282 self.RecAmpCalibr1 = RecAmpCalibr1
282 283 self.RecAmpCalibr2 = RecAmpCalibr2
283 284 self.ReceiverGaindB0 = ReceiverGaindB0
284 285 self.ReceiverGaindB1 = ReceiverGaindB1
285 286 self.ReceiverGaindB2 = ReceiverGaindB2
286 287 self.OffsetStartHeader = 48
287 288
288 289
289 290
290 291 def RHread(self, fp):
291 292 #print fp
292 293 #startFp = open('/home/erick/Documents/Data/huancayo.20161019.22.fdt',"rb") #The method tell() returns the current position of the file read/write pointer within the file.
293 294 startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
294 295 #RecCounter=0
295 296 #Off2StartNxtRec=811248
296 297 OffRHeader= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
297 298 print ' '
298 299 print 'puntero Record Header', startFp.tell()
299 300 print ' '
300 301
301 302
302 303 startFp.seek(OffRHeader, os.SEEK_SET)
303 304
304 305 print ' '
305 306 print 'puntero Record Header con seek', startFp.tell()
306 307 print ' '
307 308
308 309 #print 'Posicion del bloque: ',OffRHeader
309 310
310 311 header = numpy.fromfile(startFp,RECORD_STRUCTURE,1)
311 312
312 313 print ' '
313 314 print 'puntero Record Header con seek', startFp.tell()
314 315 print ' '
315 316
316 317 print ' '
317 318 #
318 319 #print 'puntero Record Header despues de seek', header.tell()
319 320 print ' '
320 321
321 322 self.RecMgcNumber = hex(header['RecMgcNumber'][0]) #0x23030001
322 323 self.RecCounter = int(header['RecCounter'][0])
323 324 self.Off2StartNxtRec = int(header['Off2StartNxtRec'][0])
324 325 self.Off2StartData = int(header['Off2StartData'][0])
325 326 self.nUtime = header['nUtime'][0]
326 327 self.nMilisec = header['nMilisec'][0]
327 328 self.ExpTagName = str(header['ExpTagName'][0])
328 329 self.ExpComment = str(header['ExpComment'][0])
329 330 self.SiteLatDegrees = header['SiteLatDegrees'][0]
330 331 self.SiteLongDegrees = header['SiteLongDegrees'][0]
331 332 self.RTCgpsStatus = header['RTCgpsStatus'][0]
332 333 self.TransmitFrec = header['TransmitFrec'][0]
333 334 self.ReceiveFrec = header['ReceiveFrec'][0]
334 335 self.FirstOsciFrec = header['FirstOsciFrec'][0]
335 336 self.Polarisation = header['Polarisation'][0]
336 337 self.ReceiverFiltSett = header['ReceiverFiltSett'][0]
337 338 self.nModesInUse = header['nModesInUse'][0]
338 339 self.DualModeIndex = header['DualModeIndex'][0]
339 340 self.DualModeRange = header['DualModeRange'][0]
340 341 self.nDigChannels = header['nDigChannels'][0]
341 342 self.SampResolution = header['SampResolution'][0]
342 343 self.nHeights = header['nHeights'][0]
343 344 self.StartRangeSamp = header['StartRangeSamp'][0]
344 345 self.PRFhz = header['PRFhz'][0]
345 346 self.nCohInt = header['nCohInt'][0]
346 347 self.nProfiles = header['nProfiles'][0]
347 348 self.nChannels = header['nChannels'][0]
348 349 self.nIncohInt = header['nIncohInt'][0]
349 350 self.FFTwindowingInd = header['FFTwindowingInd'][0]
350 351 self.BeamAngleAzim = header['BeamAngleAzim'][0]
351 352 self.BeamAngleZen = header['BeamAngleZen'][0]
352 353 self.AntennaCoord0 = header['AntennaCoord0'][0]
353 354 self.AntennaAngl0 = header['AntennaAngl0'][0]
354 355 self.AntennaCoord1 = header['AntennaCoord1'][0]
355 356 self.AntennaAngl1 = header['AntennaAngl1'][0]
356 357 self.AntennaCoord2 = header['AntennaCoord2'][0]
357 358 self.AntennaAngl2 = header['AntennaAngl2'][0]
358 359 self.RecPhaseCalibr0 = header['RecPhaseCalibr0'][0]
359 360 self.RecPhaseCalibr1 = header['RecPhaseCalibr1'][0]
360 361 self.RecPhaseCalibr2 = header['RecPhaseCalibr2'][0]
361 362 self.RecAmpCalibr0 = header['RecAmpCalibr0'][0]
362 363 self.RecAmpCalibr1 = header['RecAmpCalibr1'][0]
363 364 self.RecAmpCalibr2 = header['RecAmpCalibr2'][0]
364 365 self.ReceiverGaindB0 = header['ReceiverGaindB0'][0]
365 366 self.ReceiverGaindB1 = header['ReceiverGaindB1'][0]
366 367 self.ReceiverGaindB2 = header['ReceiverGaindB2'][0]
367 368
368 369 self.ipp= 0.5*(SPEED_OF_LIGHT/self.PRFhz)
369 370
370 371 self.RHsize = 180+20*self.nChannels
371 372 self.Datasize= self.nProfiles*self.nChannels*self.nHeights*2*4
372 373 #print 'Datasize',self.Datasize
373 374 endFp = self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
374 375
375 376 print '=============================================='
376 377 print 'RecMgcNumber ',self.RecMgcNumber
377 378 print 'RecCounter ',self.RecCounter
378 379 print 'Off2StartNxtRec ',self.Off2StartNxtRec
379 380 print 'Off2StartData ',self.Off2StartData
380 381 print 'Range Resolution ',self.SampResolution
381 382 print 'First Height ',self.StartRangeSamp
382 383 print 'PRF (Hz) ',self.PRFhz
383 384 print 'Heights (K) ',self.nHeights
384 385 print 'Channels (N) ',self.nChannels
385 386 print 'Profiles (J) ',self.nProfiles
386 387 print 'iCoh ',self.nCohInt
387 388 print 'iInCoh ',self.nIncohInt
388 389 print 'BeamAngleAzim ',self.BeamAngleAzim
389 390 print 'BeamAngleZen ',self.BeamAngleZen
390 391
391 392 #print 'ModoEnUso ',self.DualModeIndex
392 393 #print 'UtcTime ',self.nUtime
393 394 #print 'MiliSec ',self.nMilisec
394 395 #print 'Exp TagName ',self.ExpTagName
395 396 #print 'Exp Comment ',self.ExpComment
396 397 #print 'FFT Window Index ',self.FFTwindowingInd
397 398 #print 'N Dig. Channels ',self.nDigChannels
398 399 print 'Size de bloque ',self.RHsize
399 400 print 'DataSize ',self.Datasize
400 401 print 'BeamAngleAzim ',self.BeamAngleAzim
401 402 #print 'AntennaCoord0 ',self.AntennaCoord0
402 403 #print 'AntennaAngl0 ',self.AntennaAngl0
403 404 #print 'AntennaCoord1 ',self.AntennaCoord1
404 405 #print 'AntennaAngl1 ',self.AntennaAngl1
405 406 #print 'AntennaCoord2 ',self.AntennaCoord2
406 407 #print 'AntennaAngl2 ',self.AntennaAngl2
407 408 print 'RecPhaseCalibr0 ',self.RecPhaseCalibr0
408 409 print 'RecPhaseCalibr1 ',self.RecPhaseCalibr1
409 410 print 'RecPhaseCalibr2 ',self.RecPhaseCalibr2
410 411 print 'RecAmpCalibr0 ',self.RecAmpCalibr0
411 412 print 'RecAmpCalibr1 ',self.RecAmpCalibr1
412 413 print 'RecAmpCalibr2 ',self.RecAmpCalibr2
413 414 print 'ReceiverGaindB0 ',self.ReceiverGaindB0
414 415 print 'ReceiverGaindB1 ',self.ReceiverGaindB1
415 416 print 'ReceiverGaindB2 ',self.ReceiverGaindB2
416 417 print '=============================================='
417 418
418 419 if OffRHeader > endFp:
419 420 sys.stderr.write("Warning %s: Size value read from System Header is lower than it has to be\n" %fp)
420 421 return 0
421 422
422 423 if OffRHeader < endFp:
423 424 sys.stderr.write("Warning %s: Size value read from System Header size is greater than it has to be\n" %fp)
424 425 return 0
425 426
426 427 return 1
427 428
428 429
429 class BLTRReader (ProcessingUnit,FileHeaderBLTR,RecordHeaderBLTR):
430 class BLTRReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODataReader):
430 431
431 432 path = None
432 433 startDate = None
433 434 endDate = None
434 435 startTime = None
435 436 endTime = None
436 437 walk = None
437 438 isConfig = False
438 439
439 440
440 441 fileList= None
441 442
442 443 #metadata
443 444 TimeZone= None
444 445 Interval= None
445 446 heightList= None
446 447
447 448 #data
448 449 data= None
449 450 utctime= None
450 451
451 452
452 453
453 def __init__(self):
454 def __init__(self, **kwargs):
454 455
455 456 #Eliminar de la base la herencia
456 ProcessingUnit.__init__(self)
457 ProcessingUnit.__init__(self, **kwargs)
457 458
458 459 # self.isConfig = False
459 460
460 461 #self.pts2read_SelfSpectra = 0
461 462 #self.pts2read_CrossSpectra = 0
462 463 #self.pts2read_DCchannels = 0
463 464 #self.datablock = None
464 465 self.utc = None
465 466 self.ext = ".fdt"
466 467 self.optchar = "P"
467 468 self.fpFile=None
468 469 self.fp = None
469 470 self.BlockCounter=0
470 471 self.dtype = None
471 472 self.fileSizeByHeader = None
472 473 self.filenameList = []
473 474 self.fileSelector = 0
474 475 self.Off2StartNxtRec=0
475 476 self.RecCounter=0
476 477 self.flagNoMoreFiles = 0
477 478 self.data_spc=None
478 479 self.data_cspc=None
479 480 self.data_output=None
480 481 self.path = None
481 482 self.OffsetStartHeader=0
482 483 self.Off2StartData=0
483 484 self.ipp = 0
484 485 self.nFDTdataRecors=0
485 486 self.blocksize = 0
486 487 self.dataOut = Spectra()
487 488 self.profileIndex = 1 #Always
488 489 self.dataOut.flagNoData=False
489 490 self.dataOut.nRdPairs = 0
490 491 self.dataOut.pairsList = []
491 492 self.dataOut.data_spc=None
492 493 self.dataOut.noise=[]
493 494 self.dataOut.velocityX=[]
494 495 self.dataOut.velocityY=[]
495 496 self.dataOut.velocityV=[]
496 497
497 498
498 499
499 500 def Files2Read(self, fp):
500 501 '''
501 502 Function that indicates the number of .fdt files that exist in the folder to be read.
502 503 It also creates an organized list with the names of the files to read.
503 504 '''
504 505 #self.__checkPath()
505 506
506 507 ListaData=os.listdir(fp) #Gets the list of files within the fp address
507 508 ListaData=sorted(ListaData) #Sort the list of files from least to largest by names
508 509 nFiles=0 #File Counter
509 510 FileList=[] #A list is created that will contain the .fdt files
510 511 for IndexFile in ListaData :
511 512 if '.fdt' in IndexFile:
512 513 FileList.append(IndexFile)
513 514 nFiles+=1
514 515
515 516 #print 'Files2Read'
516 517 #print 'Existen '+str(nFiles)+' archivos .fdt'
517 518
518 519 self.filenameList=FileList #List of files from least to largest by names
519 520
520 521
521 522 def run(self, **kwargs):
522 523 '''
523 524 This method will be the one that will initiate the data entry, will be called constantly.
524 525 You should first verify that your Setup () is set up and then continue to acquire
525 526 the data to be processed with getData ().
526 527 '''
527 528 if not self.isConfig:
528 529 self.setup(**kwargs)
529 530 self.isConfig = True
530 531
531 532 self.getData()
532 533 #print 'running'
533 534
534 535
535 536 def setup(self, path=None,
536 537 startDate=None,
537 538 endDate=None,
538 539 startTime=None,
539 540 endTime=None,
540 541 walk=True,
541 542 timezone='utc',
542 543 code = None,
543 544 online=False,
544 ReadMode=None):
545 ReadMode=None,
546 **kwargs):
545 547
546 548 self.isConfig = True
547 549
548 550 self.path=path
549 551 self.startDate=startDate
550 552 self.endDate=endDate
551 553 self.startTime=startTime
552 554 self.endTime=endTime
553 555 self.walk=walk
554 556 self.ReadMode=int(ReadMode)
555 557
556 558 pass
557 559
558 560
559 561 def getData(self):
560 562 '''
561 563 Before starting this function, you should check that there is still an unread file,
562 564 If there are still blocks to read or if the data block is empty.
563 565
564 566 You should call the file "read".
565 567
566 568 '''
567 569
568 570 if self.flagNoMoreFiles:
569 571 self.dataOut.flagNoData = True
570 572 print 'NoData se vuelve true'
571 573 return 0
572 574
573 575 self.fp=self.path
574 576 self.Files2Read(self.fp)
575 577 self.readFile(self.fp)
576 578 self.dataOut.data_spc = self.data_spc
577 579 self.dataOut.data_cspc =self.data_cspc
578 580 self.dataOut.data_output=self.data_output
579 581
580 582 print 'self.dataOut.data_output', shape(self.dataOut.data_output)
581 583
582 584 #self.removeDC()
583 585 return self.dataOut.data_spc
584 586
585 587
586 588 def readFile(self,fp):
587 589 '''
588 590 You must indicate if you are reading in Online or Offline mode and load the
589 591 The parameters for this file reading mode.
590 592
591 593 Then you must do 2 actions:
592 594
593 595 1. Get the BLTR FileHeader.
594 596 2. Start reading the first block.
595 597 '''
596 598
597 599 #The address of the folder is generated the name of the .fdt file that will be read
598 600 print "File: ",self.fileSelector+1
599 601
600 602 if self.fileSelector < len(self.filenameList):
601 603
602 604 self.fpFile=str(fp)+'/'+str(self.filenameList[self.fileSelector])
603 605 #print self.fpFile
604 606 fheader = FileHeaderBLTR()
605 607 fheader.FHread(self.fpFile) #Bltr FileHeader Reading
606 608 self.nFDTdataRecors=fheader.nFDTdataRecors
607 609
608 610 self.readBlock() #Block reading
609 611 else:
610 612 print 'readFile FlagNoData becomes true'
611 613 self.flagNoMoreFiles=True
612 614 self.dataOut.flagNoData = True
613 615 return 0
614 616
615 617 def getVelRange(self, extrapoints=0):
616 618 Lambda= SPEED_OF_LIGHT/50000000
617 619 PRF = self.dataOut.PRF#1./(self.dataOut.ippSeconds * self.dataOut.nCohInt)
618 620 Vmax=-Lambda/(4.*(1./PRF)*self.dataOut.nCohInt*2.)
619 621 deltafreq = PRF / (self.nProfiles)
620 622 deltavel = (Vmax*2) / (self.nProfiles)
621 623 freqrange = deltafreq*(numpy.arange(self.nProfiles)-self.nProfiles/2.) - deltafreq/2
622 624 velrange = deltavel*(numpy.arange(self.nProfiles)-self.nProfiles/2.)
623 625 return velrange
624 626
625 627 def readBlock(self):
626 628 '''
627 629 It should be checked if the block has data, if it is not passed to the next file.
628 630
629 631 Then the following is done:
630 632
631 633 1. Read the RecordHeader
632 634 2. Fill the buffer with the current block number.
633 635
634 636 '''
635 637
636 638 if self.BlockCounter < self.nFDTdataRecors-2:
637 639 print self.nFDTdataRecors, 'CONDICION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
638 640 if self.ReadMode==1:
639 641 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter+1)
640 642 elif self.ReadMode==0:
641 643 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter)
642 644
643 645 rheader.RHread(self.fpFile) #Bltr FileHeader Reading
644 646
645 647 self.OffsetStartHeader=rheader.OffsetStartHeader
646 648 self.RecCounter=rheader.RecCounter
647 649 self.Off2StartNxtRec=rheader.Off2StartNxtRec
648 650 self.Off2StartData=rheader.Off2StartData
649 651 self.nProfiles=rheader.nProfiles
650 652 self.nChannels=rheader.nChannels
651 653 self.nHeights=rheader.nHeights
652 654 self.frequency=rheader.TransmitFrec
653 655 self.DualModeIndex=rheader.DualModeIndex
654 656
655 657 self.pairsList =[(0,1),(0,2),(1,2)]
656 658 self.dataOut.pairsList = self.pairsList
657 659
658 660 self.nRdPairs=len(self.dataOut.pairsList)
659 661 self.dataOut.nRdPairs = self.nRdPairs
660 662
661 663 self.__firstHeigth=rheader.StartRangeSamp
662 664 self.__deltaHeigth=rheader.SampResolution
663 665 self.dataOut.heightList= self.__firstHeigth + numpy.array(range(self.nHeights))*self.__deltaHeigth
664 666 self.dataOut.channelList = range(self.nChannels)
665 667 self.dataOut.nProfiles=rheader.nProfiles
666 668 self.dataOut.nIncohInt=rheader.nIncohInt
667 669 self.dataOut.nCohInt=rheader.nCohInt
668 670 self.dataOut.ippSeconds= 1/float(rheader.PRFhz)
669 671 self.dataOut.PRF=rheader.PRFhz
670 672 self.dataOut.nFFTPoints=rheader.nProfiles
671 673 self.dataOut.utctime=rheader.nUtime
672 674 self.dataOut.timeZone=0
673 675 self.dataOut.normFactor= self.dataOut.nProfiles*self.dataOut.nIncohInt*self.dataOut.nCohInt
674 676 self.dataOut.outputInterval= self.dataOut.ippSeconds * self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles
675 677
676 678 self.data_output=numpy.ones([3,rheader.nHeights])*numpy.NaN
677 679 print 'self.data_output', shape(self.data_output)
678 680 self.dataOut.velocityX=[]
679 681 self.dataOut.velocityY=[]
680 682 self.dataOut.velocityV=[]
681 683
682 684 '''Block Reading, the Block Data is received and Reshape is used to give it
683 685 shape.
684 686 '''
685 687
686 688 #Procedure to take the pointer to where the date block starts
687 689 startDATA = open(self.fpFile,"rb")
688 690 OffDATA= self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec+self.Off2StartData
689 691 startDATA.seek(OffDATA, os.SEEK_SET)
690 692
691 693 def moving_average(x, N=2):
692 694 return numpy.convolve(x, numpy.ones((N,))/N)[(N-1):]
693 695
694 696 def gaus(xSamples,a,x0,sigma):
695 697 return a*exp(-(xSamples-x0)**2/(2*sigma**2))
696 698
697 699 def Find(x,value):
698 700 for index in range(len(x)):
699 701 if x[index]==value:
700 702 return index
701 703
702 704 def pol2cart(rho, phi):
703 705 x = rho * numpy.cos(phi)
704 706 y = rho * numpy.sin(phi)
705 707 return(x, y)
706 708
707 709
708 710
709 711
710 712 if self.DualModeIndex==self.ReadMode:
711 713
712 714 self.data_fft = numpy.fromfile( startDATA, [('complex','<c8')],self.nProfiles*self.nChannels*self.nHeights )
713 715
714 716 self.data_fft=self.data_fft.astype(numpy.dtype('complex'))
715 717
716 718 self.data_block=numpy.reshape(self.data_fft,(self.nHeights, self.nChannels, self.nProfiles ))
717 719
718 720 self.data_block = numpy.transpose(self.data_block, (1,2,0))
719 721
720
721 spc = self.data_block.copy() * numpy.conjugate(self.data_block.copy())
722 copy = self.data_block.copy()
723 spc = copy * numpy.conjugate(copy)
722 724
723 725 self.data_spc = numpy.absolute(spc) # valor absoluto o magnitud
724 726
725 727 factor = self.dataOut.normFactor
726 728
727 729
728 730 z = self.data_spc.copy()#/factor
729 731 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
730 732 #zdB = 10*numpy.log10(z)
731 733 print ' '
732 734 print 'Z: '
733 735 print shape(z)
734 736 print ' '
735 737 print ' '
736 738
737 739 self.dataOut.data_spc=self.data_spc
738 740
739 741 self.noise = self.dataOut.getNoise(ymin_index=80, ymax_index=132)#/factor
740 742 #noisedB = 10*numpy.log10(self.noise)
741 743
742 744
743 745 ySamples=numpy.ones([3,self.nProfiles])
744 746 phase=numpy.ones([3,self.nProfiles])
745 747 CSPCSamples=numpy.ones([3,self.nProfiles],dtype=numpy.complex_)
746 748 coherence=numpy.ones([3,self.nProfiles])
747 749 PhaseSlope=numpy.ones(3)
748 750 PhaseInter=numpy.ones(3)
749 751
750 752 '''****** Getting CrossSpectra ******'''
751 753 cspc=self.data_block.copy()
752 754 self.data_cspc=self.data_block.copy()
753 755
754 756 xFrec=self.getVelRange(1)
755 757 VelRange=self.getVelRange(1)
756 758 self.dataOut.VelRange=VelRange
757 759 #print ' '
758 760 #print ' '
759 761 #print 'xFrec',xFrec
760 762 #print ' '
761 763 #print ' '
762 764 #Height=35
763 765 for i in range(self.nRdPairs):
764 766
765 767 chan_index0 = self.dataOut.pairsList[i][0]
766 768 chan_index1 = self.dataOut.pairsList[i][1]
767 769
768 770 self.data_cspc[i,:,:]=cspc[chan_index0,:,:] * numpy.conjugate(cspc[chan_index1,:,:])
769 771
770 772
771 773 '''Getting Eij and Nij'''
772 774 (AntennaX0,AntennaY0)=pol2cart(rheader.AntennaCoord0, rheader.AntennaAngl0*numpy.pi/180)
773 775 (AntennaX1,AntennaY1)=pol2cart(rheader.AntennaCoord1, rheader.AntennaAngl1*numpy.pi/180)
774 776 (AntennaX2,AntennaY2)=pol2cart(rheader.AntennaCoord2, rheader.AntennaAngl2*numpy.pi/180)
775 777
776 778 E01=AntennaX0-AntennaX1
777 779 N01=AntennaY0-AntennaY1
778 780
779 781 E02=AntennaX0-AntennaX2
780 782 N02=AntennaY0-AntennaY2
781 783
782 784 E12=AntennaX1-AntennaX2
783 785 N12=AntennaY1-AntennaY2
784 786
785 787 self.ChanDist= numpy.array([[E01, N01],[E02,N02],[E12,N12]])
786 788
787 789 self.dataOut.ChanDist = self.ChanDist
788 790
789 791
790 792 # for Height in range(self.nHeights):
791 793 #
792 794 # for i in range(self.nRdPairs):
793 795 #
794 796 # '''****** Line of Data SPC ******'''
795 797 # zline=z[i,:,Height]
796 798 #
797 799 # '''****** DC is removed ******'''
798 800 # DC=Find(zline,numpy.amax(zline))
799 801 # zline[DC]=(zline[DC-1]+zline[DC+1])/2
800 802 #
801 803 #
802 804 # '''****** SPC is normalized ******'''
803 805 # FactNorm= zline.copy() / numpy.sum(zline.copy())
804 806 # FactNorm= FactNorm/numpy.sum(FactNorm)
805 807 #
806 808 # SmoothSPC=moving_average(FactNorm,N=3)
807 809 #
808 810 # xSamples = ar(range(len(SmoothSPC)))
809 811 # ySamples[i] = SmoothSPC-self.noise[i]
810 812 #
811 813 # for i in range(self.nRdPairs):
812 814 #
813 815 # '''****** Line of Data CSPC ******'''
814 816 # cspcLine=self.data_cspc[i,:,Height].copy()
815 817 #
816 818 #
817 819 #
818 820 # '''****** CSPC is normalized ******'''
819 821 # chan_index0 = self.dataOut.pairsList[i][0]
820 822 # chan_index1 = self.dataOut.pairsList[i][1]
821 823 # CSPCFactor= numpy.sum(ySamples[chan_index0]) * numpy.sum(ySamples[chan_index1])
822 824 #
823 825 #
824 826 # CSPCNorm= cspcLine.copy() / numpy.sqrt(CSPCFactor)
825 827 #
826 828 #
827 829 # CSPCSamples[i] = CSPCNorm-self.noise[i]
828 830 # coherence[i] = numpy.abs(CSPCSamples[i]) / numpy.sqrt(CSPCFactor)
829 831 #
830 832 # '''****** DC is removed ******'''
831 833 # DC=Find(coherence[i],numpy.amax(coherence[i]))
832 834 # coherence[i][DC]=(coherence[i][DC-1]+coherence[i][DC+1])/2
833 835 # coherence[i]= moving_average(coherence[i],N=2)
834 836 #
835 837 # phase[i] = moving_average( numpy.arctan2(CSPCSamples[i].imag, CSPCSamples[i].real),N=1)#*180/numpy.pi
836 838 #
837 839 #
838 840 # '''****** Getting fij width ******'''
839 841 #
840 842 # yMean=[]
841 843 # yMean2=[]
842 844 #
843 845 # for j in range(len(ySamples[1])):
844 846 # yMean=numpy.append(yMean,numpy.average([ySamples[0,j],ySamples[1,j],ySamples[2,j]]))
845 847 #
846 848 # '''******* Getting fitting Gaussian ******'''
847 849 # meanGauss=sum(xSamples*yMean) / len(xSamples)
848 850 # sigma=sum(yMean*(xSamples-meanGauss)**2) / len(xSamples)
849 851 # #print 'Height',Height,'SNR', meanGauss/sigma**2
850 852 #
851 853 # if (abs(meanGauss/sigma**2) > 0.0001) :
852 854 #
853 855 # try:
854 856 # popt,pcov = curve_fit(gaus,xSamples,yMean,p0=[1,meanGauss,sigma])
855 857 #
856 858 # if numpy.amax(popt)>numpy.amax(yMean)*0.3:
857 859 # FitGauss=gaus(xSamples,*popt)
858 860 #
859 861 # else:
860 862 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
861 863 # print 'Verificador: Dentro', Height
862 864 # except RuntimeError:
863 865 #
864 866 # try:
865 867 # for j in range(len(ySamples[1])):
866 868 # yMean2=numpy.append(yMean2,numpy.average([ySamples[1,j],ySamples[2,j]]))
867 869 # popt,pcov = curve_fit(gaus,xSamples,yMean2,p0=[1,meanGauss,sigma])
868 870 # FitGauss=gaus(xSamples,*popt)
869 871 # print 'Verificador: Exepcion1', Height
870 872 # except RuntimeError:
871 873 #
872 874 # try:
873 875 # popt,pcov = curve_fit(gaus,xSamples,ySamples[1],p0=[1,meanGauss,sigma])
874 876 # FitGauss=gaus(xSamples,*popt)
875 877 # print 'Verificador: Exepcion2', Height
876 878 # except RuntimeError:
877 879 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
878 880 # print 'Verificador: Exepcion3', Height
879 881 # else:
880 882 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
881 883 # #print 'Verificador: Fuera', Height
882 884 #
883 885 #
884 886 #
885 887 # Maximun=numpy.amax(yMean)
886 888 # eMinus1=Maximun*numpy.exp(-1)
887 889 #
888 890 # HWpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-eMinus1)))
889 891 # HalfWidth= xFrec[HWpos]
890 892 # GCpos=Find(FitGauss, numpy.amax(FitGauss))
891 893 # Vpos=Find(FactNorm, numpy.amax(FactNorm))
892 894 # #Vpos=numpy.sum(FactNorm)/len(FactNorm)
893 895 # #Vpos=Find(FactNorm, min(FactNorm, key=lambda value:abs(value- numpy.mean(FactNorm) )))
894 896 # #print 'GCpos',GCpos, numpy.amax(FitGauss), 'HWpos',HWpos
895 897 # '''****** Getting Fij ******'''
896 898 #
897 899 # GaussCenter=xFrec[GCpos]
898 900 # if (GaussCenter<0 and HalfWidth>0) or (GaussCenter>0 and HalfWidth<0):
899 901 # Fij=abs(GaussCenter)+abs(HalfWidth)+0.0000001
900 902 # else:
901 903 # Fij=abs(GaussCenter-HalfWidth)+0.0000001
902 904 #
903 905 # '''****** Getting Frecuency range of significant data ******'''
904 906 #
905 907 # Rangpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.10)))
906 908 #
907 909 # if Rangpos<GCpos:
908 910 # Range=numpy.array([Rangpos,2*GCpos-Rangpos])
909 911 # else:
910 912 # Range=numpy.array([2*GCpos-Rangpos,Rangpos])
911 913 #
912 914 # FrecRange=xFrec[Range[0]:Range[1]]
913 915 #
914 916 # #print 'FrecRange', FrecRange
915 917 # '''****** Getting SCPC Slope ******'''
916 918 #
917 919 # for i in range(self.nRdPairs):
918 920 #
919 921 # if len(FrecRange)>5 and len(FrecRange)<self.nProfiles*0.5:
920 922 # PhaseRange=moving_average(phase[i,Range[0]:Range[1]],N=3)
921 923 #
922 924 # slope, intercept, r_value, p_value, std_err = stats.linregress(FrecRange,PhaseRange)
923 925 # PhaseSlope[i]=slope
924 926 # PhaseInter[i]=intercept
925 927 # else:
926 928 # PhaseSlope[i]=0
927 929 # PhaseInter[i]=0
928 930 #
929 931 # # plt.figure(i+15)
930 932 # # plt.title('FASE ( CH%s*CH%s )' %(self.dataOut.pairsList[i][0],self.dataOut.pairsList[i][1]))
931 933 # # plt.xlabel('Frecuencia (KHz)')
932 934 # # plt.ylabel('Magnitud')
933 935 # # #plt.subplot(311+i)
934 936 # # plt.plot(FrecRange,PhaseRange,'b')
935 937 # # plt.plot(FrecRange,FrecRange*PhaseSlope[i]+PhaseInter[i],'r')
936 938 #
937 939 # #plt.axis([-0.6, 0.2, -3.2, 3.2])
938 940 #
939 941 #
940 942 # '''Getting constant C'''
941 943 # cC=(Fij*numpy.pi)**2
942 944 #
943 945 # # '''Getting Eij and Nij'''
944 946 # # (AntennaX0,AntennaY0)=pol2cart(rheader.AntennaCoord0, rheader.AntennaAngl0*numpy.pi/180)
945 947 # # (AntennaX1,AntennaY1)=pol2cart(rheader.AntennaCoord1, rheader.AntennaAngl1*numpy.pi/180)
946 948 # # (AntennaX2,AntennaY2)=pol2cart(rheader.AntennaCoord2, rheader.AntennaAngl2*numpy.pi/180)
947 949 # #
948 950 # # E01=AntennaX0-AntennaX1
949 951 # # N01=AntennaY0-AntennaY1
950 952 # #
951 953 # # E02=AntennaX0-AntennaX2
952 954 # # N02=AntennaY0-AntennaY2
953 955 # #
954 956 # # E12=AntennaX1-AntennaX2
955 957 # # N12=AntennaY1-AntennaY2
956 958 #
957 959 # '''****** Getting constants F and G ******'''
958 960 # MijEijNij=numpy.array([[E02,N02], [E12,N12]])
959 961 # MijResult0=(-PhaseSlope[1]*cC) / (2*numpy.pi)
960 962 # MijResult1=(-PhaseSlope[2]*cC) / (2*numpy.pi)
961 963 # MijResults=numpy.array([MijResult0,MijResult1])
962 964 # (cF,cG) = numpy.linalg.solve(MijEijNij, MijResults)
963 965 #
964 966 # '''****** Getting constants A, B and H ******'''
965 967 # W01=numpy.amax(coherence[0])
966 968 # W02=numpy.amax(coherence[1])
967 969 # W12=numpy.amax(coherence[2])
968 970 #
969 971 # WijResult0=((cF*E01+cG*N01)**2)/cC - numpy.log(W01 / numpy.sqrt(numpy.pi/cC))
970 972 # WijResult1=((cF*E02+cG*N02)**2)/cC - numpy.log(W02 / numpy.sqrt(numpy.pi/cC))
971 973 # WijResult2=((cF*E12+cG*N12)**2)/cC - numpy.log(W12 / numpy.sqrt(numpy.pi/cC))
972 974 #
973 975 # WijResults=numpy.array([WijResult0, WijResult1, WijResult2])
974 976 #
975 977 # WijEijNij=numpy.array([ [E01**2, N01**2, 2*E01*N01] , [E02**2, N02**2, 2*E02*N02] , [E12**2, N12**2, 2*E12*N12] ])
976 978 # (cA,cB,cH) = numpy.linalg.solve(WijEijNij, WijResults)
977 979 #
978 980 # VxVy=numpy.array([[cA,cH],[cH,cB]])
979 981 #
980 982 # VxVyResults=numpy.array([-cF,-cG])
981 983 # (Vx,Vy) = numpy.linalg.solve(VxVy, VxVyResults)
982 984 # Vzon = Vy
983 985 # Vmer = Vx
984 986 # Vmag=numpy.sqrt(Vzon**2+Vmer**2)
985 987 # Vang=numpy.arctan2(Vmer,Vzon)
986 988 #
987 989 # if abs(Vy)<100 and abs(Vy)> 0.:
988 990 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, Vzon) #Vmag
989 991 # #print 'Vmag',Vmag
990 992 # else:
991 993 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, NaN)
992 994 #
993 995 # if abs(Vx)<100 and abs(Vx) > 0.:
994 996 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, Vmer) #Vang
995 997 # #print 'Vang',Vang
996 998 # else:
997 999 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, NaN)
998 1000 #
999 1001 # if abs(GaussCenter)<2:
1000 1002 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, xFrec[Vpos])
1001 1003 #
1002 1004 # else:
1003 1005 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, NaN)
1004 1006 #
1005 1007 #
1006 1008 # # print '********************************************'
1007 1009 # # print 'HalfWidth ', HalfWidth
1008 1010 # # print 'Maximun ', Maximun
1009 1011 # # print 'eMinus1 ', eMinus1
1010 1012 # # print 'Rangpos ', Rangpos
1011 1013 # # print 'GaussCenter ',GaussCenter
1012 1014 # # print 'E01 ',E01
1013 1015 # # print 'N01 ',N01
1014 1016 # # print 'E02 ',E02
1015 1017 # # print 'N02 ',N02
1016 1018 # # print 'E12 ',E12
1017 1019 # # print 'N12 ',N12
1018 1020 # #print 'self.dataOut.velocityX ', self.dataOut.velocityX
1019 1021 # # print 'Fij ', Fij
1020 1022 # # print 'cC ', cC
1021 1023 # # print 'cF ', cF
1022 1024 # # print 'cG ', cG
1023 1025 # # print 'cA ', cA
1024 1026 # # print 'cB ', cB
1025 1027 # # print 'cH ', cH
1026 1028 # # print 'Vx ', Vx
1027 1029 # # print 'Vy ', Vy
1028 1030 # # print 'Vmag ', Vmag
1029 1031 # # print 'Vang ', Vang*180/numpy.pi
1030 1032 # # print 'PhaseSlope ',PhaseSlope[0]
1031 1033 # # print 'PhaseSlope ',PhaseSlope[1]
1032 1034 # # print 'PhaseSlope ',PhaseSlope[2]
1033 1035 # # print '********************************************'
1034 1036 # #print 'data_output',shape(self.dataOut.velocityX), shape(self.dataOut.velocityY)
1035 1037 #
1036 1038 # #print 'self.dataOut.velocityX', len(self.dataOut.velocityX)
1037 1039 # #print 'self.dataOut.velocityY', len(self.dataOut.velocityY)
1038 1040 # #print 'self.dataOut.velocityV', self.dataOut.velocityV
1039 1041 #
1040 1042 # self.data_output[0]=numpy.array(self.dataOut.velocityX)
1041 1043 # self.data_output[1]=numpy.array(self.dataOut.velocityY)
1042 1044 # self.data_output[2]=numpy.array(self.dataOut.velocityV)
1043 1045 #
1044 1046 # prin= self.data_output[0][~numpy.isnan(self.data_output[0])]
1045 1047 # print ' '
1046 1048 # print 'VmagAverage',numpy.mean(prin)
1047 1049 # print ' '
1048 1050 # # plt.figure(5)
1049 1051 # # plt.subplot(211)
1050 1052 # # plt.plot(self.dataOut.velocityX,'yo:')
1051 1053 # # plt.subplot(212)
1052 1054 # # plt.plot(self.dataOut.velocityY,'yo:')
1053 1055 #
1054 1056 # # plt.figure(1)
1055 1057 # # # plt.subplot(121)
1056 1058 # # # plt.plot(xFrec,ySamples[0],'k',label='Ch0')
1057 1059 # # # plt.plot(xFrec,ySamples[1],'g',label='Ch1')
1058 1060 # # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1059 1061 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1060 1062 # # # plt.legend()
1061 1063 # # plt.title('DATOS A ALTURA DE 2850 METROS')
1062 1064 # #
1063 1065 # # plt.xlabel('Frecuencia (KHz)')
1064 1066 # # plt.ylabel('Magnitud')
1065 1067 # # # plt.subplot(122)
1066 1068 # # # plt.title('Fit for Time Constant')
1067 1069 # # #plt.plot(xFrec,zline)
1068 1070 # # #plt.plot(xFrec,SmoothSPC,'g')
1069 1071 # # plt.plot(xFrec,FactNorm)
1070 1072 # # plt.axis([-4, 4, 0, 0.15])
1071 1073 # # # plt.xlabel('SelfSpectra KHz')
1072 1074 # #
1073 1075 # # plt.figure(10)
1074 1076 # # # plt.subplot(121)
1075 1077 # # plt.plot(xFrec,ySamples[0],'b',label='Ch0')
1076 1078 # # plt.plot(xFrec,ySamples[1],'y',label='Ch1')
1077 1079 # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1078 1080 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1079 1081 # # plt.legend()
1080 1082 # # plt.title('SELFSPECTRA EN CANALES')
1081 1083 # #
1082 1084 # # plt.xlabel('Frecuencia (KHz)')
1083 1085 # # plt.ylabel('Magnitud')
1084 1086 # # # plt.subplot(122)
1085 1087 # # # plt.title('Fit for Time Constant')
1086 1088 # # #plt.plot(xFrec,zline)
1087 1089 # # #plt.plot(xFrec,SmoothSPC,'g')
1088 1090 # # # plt.plot(xFrec,FactNorm)
1089 1091 # # # plt.axis([-4, 4, 0, 0.15])
1090 1092 # # # plt.xlabel('SelfSpectra KHz')
1091 1093 # #
1092 1094 # # plt.figure(9)
1093 1095 # #
1094 1096 # #
1095 1097 # # plt.title('DATOS SUAVIZADOS')
1096 1098 # # plt.xlabel('Frecuencia (KHz)')
1097 1099 # # plt.ylabel('Magnitud')
1098 1100 # # plt.plot(xFrec,SmoothSPC,'g')
1099 1101 # #
1100 1102 # # #plt.plot(xFrec,FactNorm)
1101 1103 # # plt.axis([-4, 4, 0, 0.15])
1102 1104 # # # plt.xlabel('SelfSpectra KHz')
1103 1105 # # #
1104 1106 # # plt.figure(2)
1105 1107 # # # #plt.subplot(121)
1106 1108 # # plt.plot(xFrec,yMean,'r',label='Mean SelfSpectra')
1107 1109 # # plt.plot(xFrec,FitGauss,'yo:',label='Ajuste Gaussiano')
1108 1110 # # # plt.plot(xFrec[Rangpos],FitGauss[Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.1)))],'bo')
1109 1111 # # # #plt.plot(xFrec,phase)
1110 1112 # # # plt.xlabel('Suavizado, promediado KHz')
1111 1113 # # plt.title('SELFSPECTRA PROMEDIADO')
1112 1114 # # # #plt.subplot(122)
1113 1115 # # # #plt.plot(xSamples,zline)
1114 1116 # # plt.xlabel('Frecuencia (KHz)')
1115 1117 # # plt.ylabel('Magnitud')
1116 1118 # # plt.legend()
1117 1119 # # #
1118 1120 # # # plt.figure(3)
1119 1121 # # # plt.subplot(311)
1120 1122 # # # #plt.plot(xFrec,phase[0])
1121 1123 # # # plt.plot(xFrec,phase[0],'g')
1122 1124 # # # plt.subplot(312)
1123 1125 # # # plt.plot(xFrec,phase[1],'g')
1124 1126 # # # plt.subplot(313)
1125 1127 # # # plt.plot(xFrec,phase[2],'g')
1126 1128 # # # #plt.plot(xFrec,phase[2])
1127 1129 # # #
1128 1130 # # # plt.figure(4)
1129 1131 # # #
1130 1132 # # # plt.plot(xSamples,coherence[0],'b')
1131 1133 # # # plt.plot(xSamples,coherence[1],'r')
1132 1134 # # # plt.plot(xSamples,coherence[2],'g')
1133 1135 # # plt.show()
1134 1136 # # #
1135 1137 # # # plt.clf()
1136 1138 # # # plt.cla()
1137 1139 # # # plt.close()
1138 1140 #
1139 1141 # print ' '
1140 1142
1141 1143
1142 1144
1143 1145 self.BlockCounter+=2
1144 1146
1145 1147 else:
1146 1148 self.fileSelector+=1
1147 1149 self.BlockCounter=0
1148 1150 print "Next File"
1149 1151
1150 1152
1151 1153
1152 1154 class BLTRWriter(ProcessingUnit):
1153 1155 '''
1154 1156 classdocs
1155 1157 '''
1156 1158
1157 1159 def __init__(self):
1158 1160 '''
1159 1161 Constructor
1160 1162 '''
1161 1163 self.dataOut = None
1162 1164
1163 1165 self.isConfig = False
1164 1166
1165 1167 def setup(self, dataIn, path, blocksPerFile, set=0, ext=None):
1166 1168 '''
1167 1169 In this method we should set all initial parameters.
1168 1170
1169 1171 Input:
1170 1172 dataIn : Input data will also be outputa data
1171 1173
1172 1174 '''
1173 1175 self.dataOut = dataIn
1174 1176
1175 1177 self.isConfig = True
1176 1178
1177 1179 return
1178 1180
1179 1181 def run(self, dataIn, **kwargs):
1180 1182 '''
1181 1183 This method will be called many times so here you should put all your code
1182 1184
1183 1185 Inputs:
1184 1186
1185 1187 dataIn : object with the data
1186 1188
1187 1189 '''
1188 1190
1189 1191 if not self.isConfig:
1190 1192 self.setup(dataIn, **kwargs)
1191 1193
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
@@ -1,794 +1,804
1 1 import os, sys
2 2 import glob
3 3 import fnmatch
4 4 import datetime
5 5 import time
6 6 import re
7 7 import h5py
8 8 import numpy
9 9 import matplotlib.pyplot as plt
10 10
11 11 import pylab as plb
12 12 from scipy.optimize import curve_fit
13 13 from scipy import asarray as ar,exp
14 14 from scipy import stats
15 15
16 16 from duplicity.path import Path
17 17 from numpy.ma.core import getdata
18 18
19 19 SPEED_OF_LIGHT = 299792458
20 20 SPEED_OF_LIGHT = 3e8
21 21
22 22 try:
23 23 from gevent import sleep
24 24 except:
25 25 from time import sleep
26 26
27 27 from schainpy.model.data.jrodata import Spectra
28 28 #from schainpy.model.data.BLTRheaderIO import FileHeader, RecordHeader
29 29 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
30 30 #from schainpy.model.io.jroIO_bltr import BLTRReader
31 31 from numpy import imag, shape, NaN, empty
32 32
33 33
34 34
35 35 class Header(object):
36 36
37 37 def __init__(self):
38 38 raise NotImplementedError
39 39
40 40
41 41 def read(self):
42 42
43 43 raise NotImplementedError
44 44
45 45 def write(self):
46 46
47 47 raise NotImplementedError
48 48
49 49 def printInfo(self):
50 50
51 51 message = "#"*50 + "\n"
52 52 message += self.__class__.__name__.upper() + "\n"
53 53 message += "#"*50 + "\n"
54 54
55 55 keyList = self.__dict__.keys()
56 56 keyList.sort()
57 57
58 58 for key in keyList:
59 59 message += "%s = %s" %(key, self.__dict__[key]) + "\n"
60 60
61 61 if "size" not in keyList:
62 62 attr = getattr(self, "size")
63 63
64 64 if attr:
65 65 message += "%s = %s" %("size", attr) + "\n"
66 66
67 67 #print message
68 68
69 69
70 70 FILE_HEADER = numpy.dtype([ #HEADER 1024bytes
71 71 ('Hname','a32'), #Original file name
72 72 ('Htime',numpy.str_,32), #Date and time when the file was created
73 73 ('Hoper',numpy.str_,64), #Name of operator who created the file
74 74 ('Hplace',numpy.str_,128), #Place where the measurements was carried out
75 75 ('Hdescr',numpy.str_,256), #Description of measurements
76 76 ('Hdummy',numpy.str_,512), #Reserved space
77 77 #Main chunk 8bytes
78 78 ('Msign',numpy.str_,4), #Main chunk signature FZKF or NUIG
79 79 ('MsizeData','<i4'), #Size of data block main chunk
80 80 #Processing DSP parameters 36bytes
81 81 ('PPARsign',numpy.str_,4), #PPAR signature
82 82 ('PPARsize','<i4'), #PPAR size of block
83 83 ('PPARprf','<i4'), #Pulse repetition frequency
84 84 ('PPARpdr','<i4'), #Pulse duration
85 85 ('PPARsft','<i4'), #FFT length
86 86 ('PPARavc','<i4'), #Number of spectral (in-coherent) averages
87 87 ('PPARihp','<i4'), #Number of lowest range gate for moment estimation
88 88 ('PPARchg','<i4'), #Count for gates for moment estimation
89 89 ('PPARpol','<i4'), #switch on/off polarimetric measurements. Should be 1.
90 90 #Service DSP parameters 112bytes
91 91 ('SPARatt','<i4'), #STC attenuation on the lowest ranges on/off
92 92 ('SPARtx','<i4'), #OBSOLETE
93 93 ('SPARaddGain0','<f4'), #OBSOLETE
94 94 ('SPARaddGain1','<f4'), #OBSOLETE
95 95 ('SPARwnd','<i4'), #Debug only. It normal mode it is 0.
96 96 ('SPARpos','<i4'), #Delay between sync pulse and tx pulse for phase corr, ns
97 97 ('SPARadd','<i4'), #"add to pulse" to compensate for delay between the leading edge of driver pulse and envelope of the RF signal.
98 98 ('SPARlen','<i4'), #Time for measuring txn pulse phase. OBSOLETE
99 99 ('SPARcal','<i4'), #OBSOLETE
100 100 ('SPARnos','<i4'), #OBSOLETE
101 101 ('SPARof0','<i4'), #detection threshold
102 102 ('SPARof1','<i4'), #OBSOLETE
103 103 ('SPARswt','<i4'), #2nd moment estimation threshold
104 104 ('SPARsum','<i4'), #OBSOLETE
105 105 ('SPARosc','<i4'), #flag Oscillosgram mode
106 106 ('SPARtst','<i4'), #OBSOLETE
107 107 ('SPARcor','<i4'), #OBSOLETE
108 108 ('SPARofs','<i4'), #OBSOLETE
109 109 ('SPARhsn','<i4'), #Hildebrand div noise detection on noise gate
110 110 ('SPARhsa','<f4'), #Hildebrand div noise detection on all gates
111 111 ('SPARcalibPow_M','<f4'), #OBSOLETE
112 112 ('SPARcalibSNR_M','<f4'), #OBSOLETE
113 113 ('SPARcalibPow_S','<f4'), #OBSOLETE
114 114 ('SPARcalibSNR_S','<f4'), #OBSOLETE
115 115 ('SPARrawGate1','<i4'), #Lowest range gate for spectra saving Raw_Gate1 >=5
116 116 ('SPARrawGate2','<i4'), #Number of range gates with atmospheric signal
117 117 ('SPARraw','<i4'), #flag - IQ or spectra saving on/off
118 118 ('SPARprc','<i4'),]) #flag - Moment estimation switched on/off
119 119
120 120
121 121
122 122 class FileHeaderMIRA35c(Header):
123 123
124 124 def __init__(self):
125 125
126 126 self.Hname= None
127 127 self.Htime= None
128 128 self.Hoper= None
129 129 self.Hplace= None
130 130 self.Hdescr= None
131 131 self.Hdummy= None
132 132
133 133 self.Msign=None
134 134 self.MsizeData=None
135 135
136 136 self.PPARsign=None
137 137 self.PPARsize=None
138 138 self.PPARprf=None
139 139 self.PPARpdr=None
140 140 self.PPARsft=None
141 141 self.PPARavc=None
142 142 self.PPARihp=None
143 143 self.PPARchg=None
144 144 self.PPARpol=None
145 145 #Service DSP parameters
146 146 self.SPARatt=None
147 147 self.SPARtx=None
148 148 self.SPARaddGain0=None
149 149 self.SPARaddGain1=None
150 150 self.SPARwnd=None
151 151 self.SPARpos=None
152 152 self.SPARadd=None
153 153 self.SPARlen=None
154 154 self.SPARcal=None
155 155 self.SPARnos=None
156 156 self.SPARof0=None
157 157 self.SPARof1=None
158 158 self.SPARswt=None
159 159 self.SPARsum=None
160 160 self.SPARosc=None
161 161 self.SPARtst=None
162 162 self.SPARcor=None
163 163 self.SPARofs=None
164 164 self.SPARhsn=None
165 165 self.SPARhsa=None
166 166 self.SPARcalibPow_M=None
167 167 self.SPARcalibSNR_M=None
168 168 self.SPARcalibPow_S=None
169 169 self.SPARcalibSNR_S=None
170 170 self.SPARrawGate1=None
171 171 self.SPARrawGate2=None
172 172 self.SPARraw=None
173 173 self.SPARprc=None
174 174
175 175 self.FHsize=1180
176 176
177 177 def FHread(self, fp):
178 178
179 179 header = numpy.fromfile(fp, FILE_HEADER,1)
180 180 ''' numpy.fromfile(file, dtype, count, sep='')
181 181 file : file or str
182 182 Open file object or filename.
183 183
184 184 dtype : data-type
185 185 Data type of the returned array. For binary files, it is used to determine
186 186 the size and byte-order of the items in the file.
187 187
188 188 count : int
189 189 Number of items to read. -1 means all items (i.e., the complete file).
190 190
191 191 sep : str
192 192 Separator between items if file is a text file. Empty ("") separator means
193 193 the file should be treated as binary. Spaces (" ") in the separator match zero
194 194 or more whitespace characters. A separator consisting only of spaces must match
195 195 at least one whitespace.
196 196
197 197 '''
198 198
199 199
200 200 self.Hname= str(header['Hname'][0])
201 201 self.Htime= str(header['Htime'][0])
202 202 self.Hoper= str(header['Hoper'][0])
203 203 self.Hplace= str(header['Hplace'][0])
204 204 self.Hdescr= str(header['Hdescr'][0])
205 205 self.Hdummy= str(header['Hdummy'][0])
206 206 #1024
207 207
208 208 self.Msign=str(header['Msign'][0])
209 209 self.MsizeData=header['MsizeData'][0]
210 210 #8
211 211
212 212 self.PPARsign=str(header['PPARsign'][0])
213 213 self.PPARsize=header['PPARsize'][0]
214 214 self.PPARprf=header['PPARprf'][0]
215 215 self.PPARpdr=header['PPARpdr'][0]
216 216 self.PPARsft=header['PPARsft'][0]
217 217 self.PPARavc=header['PPARavc'][0]
218 218 self.PPARihp=header['PPARihp'][0]
219 219 self.PPARchg=header['PPARchg'][0]
220 220 self.PPARpol=header['PPARpol'][0]
221 221 #Service DSP parameters
222 222 #36
223 223
224 224 self.SPARatt=header['SPARatt'][0]
225 225 self.SPARtx=header['SPARtx'][0]
226 226 self.SPARaddGain0=header['SPARaddGain0'][0]
227 227 self.SPARaddGain1=header['SPARaddGain1'][0]
228 228 self.SPARwnd=header['SPARwnd'][0]
229 229 self.SPARpos=header['SPARpos'][0]
230 230 self.SPARadd=header['SPARadd'][0]
231 231 self.SPARlen=header['SPARlen'][0]
232 232 self.SPARcal=header['SPARcal'][0]
233 233 self.SPARnos=header['SPARnos'][0]
234 234 self.SPARof0=header['SPARof0'][0]
235 235 self.SPARof1=header['SPARof1'][0]
236 236 self.SPARswt=header['SPARswt'][0]
237 237 self.SPARsum=header['SPARsum'][0]
238 238 self.SPARosc=header['SPARosc'][0]
239 239 self.SPARtst=header['SPARtst'][0]
240 240 self.SPARcor=header['SPARcor'][0]
241 241 self.SPARofs=header['SPARofs'][0]
242 242 self.SPARhsn=header['SPARhsn'][0]
243 243 self.SPARhsa=header['SPARhsa'][0]
244 244 self.SPARcalibPow_M=header['SPARcalibPow_M'][0]
245 245 self.SPARcalibSNR_M=header['SPARcalibSNR_M'][0]
246 246 self.SPARcalibPow_S=header['SPARcalibPow_S'][0]
247 247 self.SPARcalibSNR_S=header['SPARcalibSNR_S'][0]
248 248 self.SPARrawGate1=header['SPARrawGate1'][0]
249 249 self.SPARrawGate2=header['SPARrawGate2'][0]
250 250 self.SPARraw=header['SPARraw'][0]
251 251 self.SPARprc=header['SPARprc'][0]
252 252 #112
253 253 #1180
254 254 #print 'Pointer fp header', fp.tell()
255 255 #print ' '
256 256 #print 'SPARrawGate'
257 257 #print self.SPARrawGate2 - self.SPARrawGate1
258 258
259 259 #print ' '
260 260 #print 'Hname'
261 261 #print self.Hname
262 262
263 263 #print ' '
264 264 #print 'Msign'
265 265 #print self.Msign
266 266
267 267 def write(self, fp):
268 268
269 269 headerTuple = (self.Hname,
270 270 self.Htime,
271 271 self.Hoper,
272 272 self.Hplace,
273 273 self.Hdescr,
274 274 self.Hdummy)
275 275
276 276
277 277 header = numpy.array(headerTuple, FILE_HEADER)
278 278 # numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
279 279 header.tofile(fp)
280 280 ''' ndarray.tofile(fid, sep, format) Write array to a file as text or binary (default).
281 281
282 282 fid : file or str
283 283 An open file object, or a string containing a filename.
284 284
285 285 sep : str
286 286 Separator between array items for text output. If "" (empty), a binary file is written,
287 287 equivalent to file.write(a.tobytes()).
288 288
289 289 format : str
290 290 Format string for text file output. Each entry in the array is formatted to text by
291 291 first converting it to the closest Python type, and then using "format" % item.
292 292
293 293 '''
294 294
295 295 return 1
296 296
297 297 SRVI_HEADER = numpy.dtype([
298 298 ('SignatureSRVI1',numpy.str_,4),#
299 299 ('SizeOfDataBlock1','<i4'),#
300 300 ('DataBlockTitleSRVI1',numpy.str_,4),#
301 301 ('SizeOfSRVI1','<i4'),])#
302 302
303 303 class SRVIHeader(Header):
304 304 def __init__(self, SignatureSRVI1=0, SizeOfDataBlock1=0, DataBlockTitleSRVI1=0, SizeOfSRVI1=0):
305 305
306 306 self.SignatureSRVI1 = SignatureSRVI1
307 307 self.SizeOfDataBlock1 = SizeOfDataBlock1
308 308 self.DataBlockTitleSRVI1 = DataBlockTitleSRVI1
309 309 self.SizeOfSRVI1 = SizeOfSRVI1
310 310
311 311 self.SRVIHsize=16
312 312
313 313 def SRVIread(self, fp):
314 314
315 315 header = numpy.fromfile(fp, SRVI_HEADER,1)
316 316
317 317 self.SignatureSRVI1 = str(header['SignatureSRVI1'][0])
318 318 self.SizeOfDataBlock1 = header['SizeOfDataBlock1'][0]
319 319 self.DataBlockTitleSRVI1 = str(header['DataBlockTitleSRVI1'][0])
320 320 self.SizeOfSRVI1 = header['SizeOfSRVI1'][0]
321 321 #16
322 322 print 'Pointer fp SRVIheader', fp.tell()
323 323
324 324
325 325 SRVI_STRUCTURE = numpy.dtype([
326 326 ('frame_cnt','<u4'),#
327 327 ('time_t','<u4'), #
328 328 ('tpow','<f4'), #
329 329 ('npw1','<f4'), #
330 330 ('npw2','<f4'), #
331 331 ('cpw1','<f4'), #
332 332 ('pcw2','<f4'), #
333 333 ('ps_err','<u4'), #
334 334 ('te_err','<u4'), #
335 335 ('rc_err','<u4'), #
336 336 ('grs1','<u4'), #
337 337 ('grs2','<u4'), #
338 338 ('azipos','<f4'), #
339 339 ('azivel','<f4'), #
340 340 ('elvpos','<f4'), #
341 341 ('elvvel','<f4'), #
342 342 ('northAngle','<f4'), #
343 343 ('microsec','<u4'), #
344 344 ('azisetvel','<f4'), #
345 345 ('elvsetpos','<f4'), #
346 346 ('RadarConst','<f4'),]) #
347 347
348 348
349 349
350 350
351 351 class RecordHeader(Header):
352 352
353 353
354 354 def __init__(self, frame_cnt=0, time_t= 0, tpow=0, npw1=0, npw2=0,
355 355 cpw1=0, pcw2=0, ps_err=0, te_err=0, rc_err=0, grs1=0,
356 356 grs2=0, azipos=0, azivel=0, elvpos=0, elvvel=0, northangle=0,
357 357 microsec=0, azisetvel=0, elvsetpos=0, RadarConst=0 , RecCounter=0, Off2StartNxtRec=0):
358 358
359 359
360 360 self.frame_cnt = frame_cnt
361 361 self.dwell = time_t
362 362 self.tpow = tpow
363 363 self.npw1 = npw1
364 364 self.npw2 = npw2
365 365 self.cpw1 = cpw1
366 366 self.pcw2 = pcw2
367 367 self.ps_err = ps_err
368 368 self.te_err = te_err
369 369 self.rc_err = rc_err
370 370 self.grs1 = grs1
371 371 self.grs2 = grs2
372 372 self.azipos = azipos
373 373 self.azivel = azivel
374 374 self.elvpos = elvpos
375 375 self.elvvel = elvvel
376 376 self.northAngle = northangle
377 377 self.microsec = microsec
378 378 self.azisetvel = azisetvel
379 379 self.elvsetpos = elvsetpos
380 380 self.RadarConst = RadarConst
381 381 self.RHsize=84
382 382 self.RecCounter = RecCounter
383 383 self.Off2StartNxtRec=Off2StartNxtRec
384 384
385 385 def RHread(self, fp):
386 386
387 387 #startFp = open(fp,"rb") #The method tell() returns the current position of the file read/write pointer within the file.
388 388
389 389 #OffRHeader= 1180 + self.RecCounter*(self.Off2StartNxtRec)
390 390 #startFp.seek(OffRHeader, os.SEEK_SET)
391 391
392 392 #print 'Posicion del bloque: ',OffRHeader
393 393
394 394 header = numpy.fromfile(fp,SRVI_STRUCTURE,1)
395 395
396 396 self.frame_cnt = header['frame_cnt'][0]#
397 397 self.time_t = header['time_t'][0] #
398 398 self.tpow = header['tpow'][0] #
399 399 self.npw1 = header['npw1'][0] #
400 400 self.npw2 = header['npw2'][0] #
401 401 self.cpw1 = header['cpw1'][0] #
402 402 self.pcw2 = header['pcw2'][0] #
403 403 self.ps_err = header['ps_err'][0] #
404 404 self.te_err = header['te_err'][0] #
405 405 self.rc_err = header['rc_err'][0] #
406 406 self.grs1 = header['grs1'][0] #
407 407 self.grs2 = header['grs2'][0] #
408 408 self.azipos = header['azipos'][0] #
409 409 self.azivel = header['azivel'][0] #
410 410 self.elvpos = header['elvpos'][0] #
411 411 self.elvvel = header['elvvel'][0] #
412 412 self.northAngle = header['northAngle'][0] #
413 413 self.microsec = header['microsec'][0] #
414 414 self.azisetvel = header['azisetvel'][0] #
415 415 self.elvsetpos = header['elvsetpos'][0] #
416 416 self.RadarConst = header['RadarConst'][0] #
417 417 #84
418 418
419 print 'Pointer fp RECheader', fp.tell()
419 #print 'Pointer fp RECheader', fp.tell()
420 420
421 421 #self.ipp= 0.5*(SPEED_OF_LIGHT/self.PRFhz)
422 422
423 423 #self.RHsize = 180+20*self.nChannels
424 424 #self.Datasize= self.nProfiles*self.nChannels*self.nHeights*2*4
425 425 #print 'Datasize',self.Datasize
426 426 #endFp = self.OffsetStartHeader + self.RecCounter*self.Off2StartNxtRec
427 427
428 428 print '=============================================='
429 429
430 430 print '=============================================='
431 431
432 432
433 433 return 1
434 434
435 435 class MIRA35CReader (ProcessingUnit,FileHeaderMIRA35c,SRVIHeader,RecordHeader):
436 436
437 437 path = None
438 438 startDate = None
439 439 endDate = None
440 440 startTime = None
441 441 endTime = None
442 442 walk = None
443 443 isConfig = False
444 444
445 445
446 446 fileList= None
447 447
448 448 #metadata
449 449 TimeZone= None
450 450 Interval= None
451 451 heightList= None
452 452
453 453 #data
454 454 data= None
455 455 utctime= None
456 456
457 457
458 458
459 def __init__(self):
459 def __init__(self, **kwargs):
460 460
461 461 #Eliminar de la base la herencia
462 ProcessingUnit.__init__(self)
462 ProcessingUnit.__init__(self, **kwargs)
463 463 self.PointerReader = 0
464 464 self.FileHeaderFlag = False
465 465 self.utc = None
466 466 self.ext = ".zspca"
467 467 self.optchar = "P"
468 468 self.fpFile=None
469 469 self.fp = None
470 470 self.BlockCounter=0
471 471 self.dtype = None
472 472 self.fileSizeByHeader = None
473 473 self.filenameList = []
474 474 self.fileSelector = 0
475 475 self.Off2StartNxtRec=0
476 476 self.RecCounter=0
477 477 self.flagNoMoreFiles = 0
478 478 self.data_spc=None
479 479 #self.data_cspc=None
480 480 self.data_output=None
481 481 self.path = None
482 482 self.OffsetStartHeader=0
483 483 self.Off2StartData=0
484 484 self.ipp = 0
485 485 self.nFDTdataRecors=0
486 486 self.blocksize = 0
487 487 self.dataOut = Spectra()
488 488 self.profileIndex = 1 #Always
489 489 self.dataOut.flagNoData=False
490 490 self.dataOut.nRdPairs = 0
491 491 self.dataOut.pairsList = []
492 492 self.dataOut.data_spc=None
493 self.dataOut.noise=[]
493
494 494 self.dataOut.normFactor=1
495 495 self.nextfileflag = True
496 496 self.dataOut.RadarConst = 0
497 497 self.dataOut.HSDV = []
498 498 self.dataOut.NPW = []
499 499 self.dataOut.COFA = []
500 self.dataOut.noise = 0
500 501
501 502
502 503 def Files2Read(self, fp):
503 504 '''
504 505 Function that indicates the number of .fdt files that exist in the folder to be read.
505 506 It also creates an organized list with the names of the files to read.
506 507 '''
507 508 #self.__checkPath()
508 509
509 510 ListaData=os.listdir(fp) #Gets the list of files within the fp address
510 511 ListaData=sorted(ListaData) #Sort the list of files from least to largest by names
511 512 nFiles=0 #File Counter
512 513 FileList=[] #A list is created that will contain the .fdt files
513 514 for IndexFile in ListaData :
514 515 if '.zspca' in IndexFile and '.gz' not in IndexFile:
515 516 FileList.append(IndexFile)
516 517 nFiles+=1
517 518
518 519 #print 'Files2Read'
519 520 #print 'Existen '+str(nFiles)+' archivos .fdt'
520 521
521 522 self.filenameList=FileList #List of files from least to largest by names
522 523
523 524
524 525 def run(self, **kwargs):
525 526 '''
526 527 This method will be the one that will initiate the data entry, will be called constantly.
527 528 You should first verify that your Setup () is set up and then continue to acquire
528 529 the data to be processed with getData ().
529 530 '''
530 531 if not self.isConfig:
531 532 self.setup(**kwargs)
532 533 self.isConfig = True
533 534
534 535 self.getData()
535 536
536 537
537 538 def setup(self, path=None,
538 539 startDate=None,
539 540 endDate=None,
540 541 startTime=None,
541 542 endTime=None,
542 543 walk=True,
543 544 timezone='utc',
544 545 code = None,
545 546 online=False,
546 ReadMode=None):
547 ReadMode=None, **kwargs):
547 548
548 549 self.isConfig = True
549 550
550 551 self.path=path
551 552 self.startDate=startDate
552 553 self.endDate=endDate
553 554 self.startTime=startTime
554 555 self.endTime=endTime
555 556 self.walk=walk
556 self.ReadMode=int(ReadMode)
557 #self.ReadMode=int(ReadMode)
557 558
558 559 pass
559 560
560 561
561 562 def getData(self):
562 563 '''
563 564 Before starting this function, you should check that there is still an unread file,
564 565 If there are still blocks to read or if the data block is empty.
565 566
566 567 You should call the file "read".
567 568
568 569 '''
569 570
570 571 if self.flagNoMoreFiles:
571 572 self.dataOut.flagNoData = True
572 573 print 'NoData se vuelve true'
573 574 return 0
574 575
575 576 self.fp=self.path
576 577 self.Files2Read(self.fp)
577 578 self.readFile(self.fp)
578 579
579 self.dataOut.data_spc = self.data_spc.copy()
580 self.dataOut.data_spc = self.dataOut_spc#self.data_spc.copy()
580 581 self.dataOut.RadarConst = self.RadarConst
581 582 self.dataOut.data_output=self.data_output
583 self.dataOut.noise = self.dataOut.getNoise()
584 #print 'ACAAAAAA', self.dataOut.noise
585 self.dataOut.data_spc = self.dataOut.data_spc+self.dataOut.noise
586 #print 'self.dataOut.noise',self.dataOut.noise
582 587
583 588
584 589 return self.dataOut.data_spc
585 590
586 591
587 592 def readFile(self,fp):
588 593 '''
589 594 You must indicate if you are reading in Online or Offline mode and load the
590 595 The parameters for this file reading mode.
591 596
592 597 Then you must do 2 actions:
593 598
594 599 1. Get the BLTR FileHeader.
595 600 2. Start reading the first block.
596 601 '''
597 602
598 603 #The address of the folder is generated the name of the .fdt file that will be read
599 604 print "File: ",self.fileSelector+1
600 605
601 606 if self.fileSelector < len(self.filenameList):
602 607
603 608 self.fpFile=str(fp)+'/'+str(self.filenameList[self.fileSelector])
604 609
605 610 if self.nextfileflag==True:
606 611 self.fp = open(self.fpFile,"rb")
607 612 self.nextfileflag==False
608 613
609 614 '''HERE STARTING THE FILE READING'''
610 615
611 616
612 617 self.fheader = FileHeaderMIRA35c()
613 618 self.fheader.FHread(self.fp) #Bltr FileHeader Reading
614 619
615 620
616 621 self.SPARrawGate1 = self.fheader.SPARrawGate1
617 622 self.SPARrawGate2 = self.fheader.SPARrawGate2
618 623 self.Num_Hei = self.SPARrawGate2 - self.SPARrawGate1
619 624 self.Num_Bins = self.fheader.PPARsft
620 625 self.dataOut.nFFTPoints = self.fheader.PPARsft
621 626
622 627
623 628 self.Num_inCoh = self.fheader.PPARavc
624 629 self.dataOut.PRF = self.fheader.PPARprf
625 630 self.dataOut.frequency = 34.85*10**9
626 631 self.Lambda = SPEED_OF_LIGHT/self.dataOut.frequency
627 632 self.dataOut.ippSeconds= 1./float(self.dataOut.PRF)
628 633
629 634 pulse_width = self.fheader.PPARpdr * 10**-9
630 635 self.__deltaHeigth = 0.5 * SPEED_OF_LIGHT * pulse_width
631 636
632 self.data_spc = numpy.zeros((self.Num_Hei, self.Num_Bins,2))
637 self.data_spc = numpy.zeros((self.Num_Hei, self.Num_Bins,2))#
633 638 self.dataOut.HSDV = numpy.zeros((self.Num_Hei, 2))
634 639
635 640 self.Ze = numpy.zeros(self.Num_Hei)
636 641 self.ETA = numpy.zeros(([2,self.Num_Hei]))
637 642
638 643
639 644
640 645 self.readBlock() #Block reading
641 646
642 647 else:
643 648 print 'readFile FlagNoData becomes true'
644 649 self.flagNoMoreFiles=True
645 650 self.dataOut.flagNoData = True
646 651 self.FileHeaderFlag == True
647 652 return 0
648 653
649 654
650 655
651 656 def readBlock(self):
652 657 '''
653 658 It should be checked if the block has data, if it is not passed to the next file.
654 659
655 660 Then the following is done:
656 661
657 662 1. Read the RecordHeader
658 663 2. Fill the buffer with the current block number.
659 664
660 665 '''
661 666
662 667 if self.PointerReader > 1180:
663 668 self.fp.seek(self.PointerReader , os.SEEK_SET)
664 669 self.FirstPoint = self.PointerReader
665 670
666 671 else :
667 672 self.FirstPoint = 1180
668 673
669 674
670 675
671 676 self.srviHeader = SRVIHeader()
672 677
673 678 self.srviHeader.SRVIread(self.fp) #Se obtiene la cabecera del SRVI
674 679
675 680 self.blocksize = self.srviHeader.SizeOfDataBlock1 # Se obtiene el tamao del bloque
676 681
677 682 if self.blocksize == 148:
678 683 print 'blocksize == 148 bug'
679 684 jump = numpy.fromfile(self.fp,[('jump',numpy.str_,140)] ,1)
680 685
681 686 self.srviHeader.SRVIread(self.fp) #Se obtiene la cabecera del SRVI
682 687
683 688 if not self.srviHeader.SizeOfSRVI1:
684 689 self.fileSelector+=1
685 690 self.nextfileflag==True
686 691 self.FileHeaderFlag == True
687 692
688 693 self.recordheader = RecordHeader()
689 694 self.recordheader.RHread(self.fp)
690 695 self.RadarConst = self.recordheader.RadarConst
691 696 dwell = self.recordheader.time_t
692 697 npw1 = self.recordheader.npw1
693 698 npw2 = self.recordheader.npw2
694 699
695 700
696 self.dataOut.channelList = range(2)
701 self.dataOut.channelList = range(1)
697 702 self.dataOut.nIncohInt = self.Num_inCoh
698 703 self.dataOut.nProfiles = self.Num_Bins
699 704 self.dataOut.nCohInt = 1
700 705 self.dataOut.windowOfFilter = 1
701 706 self.dataOut.utctime = dwell
702 707 self.dataOut.timeZone=0
703 708
704 709 self.dataOut.outputInterval = self.dataOut.getTimeInterval()
705 710 self.dataOut.heightList = self.SPARrawGate1*self.__deltaHeigth + numpy.array(range(self.Num_Hei))*self.__deltaHeigth
706 711
707 712
708 713
709 714 self.HSDVsign = numpy.fromfile( self.fp, [('HSDV',numpy.str_,4)],1)
710 715 self.SizeHSDV = numpy.fromfile( self.fp, [('SizeHSDV','<i4')],1)
711 716 self.HSDV_Co = numpy.fromfile( self.fp, [('HSDV_Co','<f4')],self.Num_Hei)
712 717 self.HSDV_Cx = numpy.fromfile( self.fp, [('HSDV_Cx','<f4')],self.Num_Hei)
713 718
714 719 self.COFAsign = numpy.fromfile( self.fp, [('COFA',numpy.str_,4)],1)
715 720 self.SizeCOFA = numpy.fromfile( self.fp, [('SizeCOFA','<i4')],1)
716 721 self.COFA_Co = numpy.fromfile( self.fp, [('COFA_Co','<f4')],self.Num_Hei)
717 722 self.COFA_Cx = numpy.fromfile( self.fp, [('COFA_Cx','<f4')],self.Num_Hei)
718 723
719 724 self.ZSPCsign = numpy.fromfile(self.fp, [('ZSPCsign',numpy.str_,4)],1)
720 725 self.SizeZSPC = numpy.fromfile(self.fp, [('SizeZSPC','<i4')],1)
721 726
722 727 self.dataOut.HSDV[0]=self.HSDV_Co[:][0]
723 728 self.dataOut.HSDV[1]=self.HSDV_Cx[:][0]
724 729
725 730 for irg in range(self.Num_Hei):
726 731 nspc = numpy.fromfile(self.fp, [('nspc','int16')],1)[0][0] # Number of spectral sub pieces containing significant power
727 732
728 733 for k in range(nspc):
729 734 binIndex = numpy.fromfile(self.fp, [('binIndex','int16')],1)[0][0] # Index of the spectral bin where the piece is beginning
730 735 nbins = numpy.fromfile(self.fp, [('nbins','int16')],1)[0][0] # Number of bins of the piece
731 736
732 737 #Co_Channel
733 738 jbin = numpy.fromfile(self.fp, [('jbin','uint16')],nbins)[0][0] # Spectrum piece to be normaliced
734 739 jmax = numpy.fromfile(self.fp, [('jmax','float32')],1)[0][0] # Maximun piece to be normaliced
735 740
736 741
737 742 self.data_spc[irg,binIndex:binIndex+nbins,0] = self.data_spc[irg,binIndex:binIndex+nbins,0]+jbin/65530.*jmax
738 743
739 744 #Cx_Channel
740 745 jbin = numpy.fromfile(self.fp, [('jbin','uint16')],nbins)[0][0]
741 746 jmax = numpy.fromfile(self.fp, [('jmax','float32')],1)[0][0]
742 747
743 748
744 749 self.data_spc[irg,binIndex:binIndex+nbins,1] = self.data_spc[irg,binIndex:binIndex+nbins,1]+jbin/65530.*jmax
745 750
746 751 for bin in range(self.Num_Bins):
747 752
748 753 self.data_spc[:,bin,0] = self.data_spc[:,bin,0] - self.dataOut.HSDV[:,0]
749 754
750 755 self.data_spc[:,bin,1] = self.data_spc[:,bin,1] - self.dataOut.HSDV[:,1]
751 756
752 757
753 758 numpy.set_printoptions(threshold='nan')
754 759
755 760 self.data_spc = numpy.where(self.data_spc > 0. , self.data_spc, 0)
756 761
757 762 self.dataOut.COFA = numpy.array([self.COFA_Co , self.COFA_Cx])
758 763
759
760 764 print ' '
761 765 print 'SPC',numpy.shape(self.dataOut.data_spc)
762 766 #print 'SPC',self.dataOut.data_spc
763 767
764 768 noinor1 = 713031680
765 769 noinor2 = 30
766 770
767 #print 'npw1 db' , npw1
768
769 npw1 = 10**(npw1/10) * noinor1 * noinor2
770 npw2 = 10**(npw2/10) * noinor1 * noinor2
771 npw1 = 1#0**(npw1/10) * noinor1 * noinor2
772 npw2 = 1#0**(npw2/10) * noinor1 * noinor2
771 773 self.dataOut.NPW = numpy.array([npw1, npw2])
772 774
773
774 775 print ' '
775 #print numpy.__version__
776
776 777 self.data_spc = numpy.transpose(self.data_spc, (2,1,0))
777 778 self.data_spc = numpy.fft.fftshift(self.data_spc, axes = 1)
778 779
779 780 self.data_spc = numpy.fliplr(self.data_spc)
780 781
782 self.data_spc = numpy.where(self.data_spc > 0. , self.data_spc, 0)
783 self.dataOut_spc= numpy.ones([1, self.Num_Bins , self.Num_Hei])
784 self.dataOut_spc[0,:,:] = self.data_spc[0,:,:]
785 #print 'SHAPE', self.dataOut_spc.shape
786 #For nyquist correction:
787 #fix = 20 # ~3m/s
788 #shift = self.Num_Bins/2 + fix
789 #self.data_spc = numpy.array([ self.data_spc[: , self.Num_Bins-shift+1: , :] , self.data_spc[: , 0:self.Num_Bins-shift , :]])
790
781 791
782 792
783 793 '''Block Reading, the Block Data is received and Reshape is used to give it
784 794 shape.
785 795 '''
786 796
787 797 self.PointerReader = self.fp.tell()
788 798
789 799
790 800
791 801
792 802
793 803
794 804 No newline at end of file
1 NO CONTENT: modified file, binary diff hidden
@@ -1,1090 +1,1090
1 1 import numpy
2 2 import time
3 3 import os
4 4 import h5py
5 5 import re
6 6 import datetime
7 7
8 8 from schainpy.model.data.jrodata import *
9 9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
10 10 # from jroIO_base import *
11 11 from schainpy.model.io.jroIO_base import *
12 12 import schainpy
13 13
14 14
15 15 class ParamReader(ProcessingUnit):
16 16 '''
17 17 Reads HDF5 format files
18 18
19 19 path
20 20
21 21 startDate
22 22
23 23 endDate
24 24
25 25 startTime
26 26
27 27 endTime
28 28 '''
29 29
30 30 ext = ".hdf5"
31 31
32 32 optchar = "D"
33 33
34 34 timezone = None
35 35
36 36 startTime = None
37 37
38 38 endTime = None
39 39
40 40 fileIndex = None
41 41
42 42 utcList = None #To select data in the utctime list
43 43
44 44 blockList = None #List to blocks to be read from the file
45 45
46 46 blocksPerFile = None #Number of blocks to be read
47 47
48 48 blockIndex = None
49 49
50 50 path = None
51 51
52 52 #List of Files
53 53
54 54 filenameList = None
55 55
56 56 datetimeList = None
57 57
58 58 #Hdf5 File
59 59
60 60 listMetaname = None
61 61
62 62 listMeta = None
63 63
64 64 listDataname = None
65 65
66 66 listData = None
67 67
68 68 listShapes = None
69 69
70 70 fp = None
71 71
72 72 #dataOut reconstruction
73 73
74 74 dataOut = None
75 75
76 76
77 77 def __init__(self, **kwargs):
78 78 ProcessingUnit.__init__(self, **kwargs)
79 79 self.dataOut = Parameters()
80 80 return
81 81
82 82 def setup(self, **kwargs):
83 83
84 84 path = kwargs['path']
85 85 startDate = kwargs['startDate']
86 86 endDate = kwargs['endDate']
87 87 startTime = kwargs['startTime']
88 88 endTime = kwargs['endTime']
89 89 walk = kwargs['walk']
90 90 if kwargs.has_key('ext'):
91 91 ext = kwargs['ext']
92 92 else:
93 93 ext = '.hdf5'
94 94 if kwargs.has_key('timezone'):
95 95 self.timezone = kwargs['timezone']
96 96 else:
97 97 self.timezone = 'lt'
98 98
99 99 print "[Reading] Searching files in offline mode ..."
100 100 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
101 101 startTime=startTime, endTime=endTime,
102 102 ext=ext, walk=walk)
103 103
104 104 if not(filenameList):
105 105 print "There is no files into the folder: %s"%(path)
106 106 sys.exit(-1)
107 107
108 108 self.fileIndex = -1
109 109 self.startTime = startTime
110 110 self.endTime = endTime
111 111
112 112 self.__readMetadata()
113 113
114 114 self.__setNextFileOffline()
115 115
116 116 return
117 117
118 118 def __searchFilesOffLine(self,
119 119 path,
120 120 startDate=None,
121 121 endDate=None,
122 122 startTime=datetime.time(0,0,0),
123 123 endTime=datetime.time(23,59,59),
124 124 ext='.hdf5',
125 125 walk=True):
126 126
127 127 expLabel = ''
128 128 self.filenameList = []
129 129 self.datetimeList = []
130 130
131 131 pathList = []
132 132
133 133 JRODataObj = JRODataReader()
134 134 dateList, pathList = JRODataObj.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
135 135
136 136 if dateList == []:
137 137 print "[Reading] No *%s files in %s from %s to %s)"%(ext, path,
138 138 datetime.datetime.combine(startDate,startTime).ctime(),
139 139 datetime.datetime.combine(endDate,endTime).ctime())
140 140
141 141 return None, None
142 142
143 143 if len(dateList) > 1:
144 144 print "[Reading] %d days were found in date range: %s - %s" %(len(dateList), startDate, endDate)
145 145 else:
146 146 print "[Reading] data was found for the date %s" %(dateList[0])
147 147
148 148 filenameList = []
149 149 datetimeList = []
150 150
151 151 #----------------------------------------------------------------------------------
152 152
153 153 for thisPath in pathList:
154 154 # thisPath = pathList[pathDict[file]]
155 155
156 156 fileList = glob.glob1(thisPath, "*%s" %ext)
157 157 fileList.sort()
158 158
159 159 for file in fileList:
160 160
161 161 filename = os.path.join(thisPath,file)
162 162
163 163 if not isFileInDateRange(filename, startDate, endDate):
164 164 continue
165 165
166 166 thisDatetime = self.__isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
167 167
168 168 if not(thisDatetime):
169 169 continue
170 170
171 171 filenameList.append(filename)
172 172 datetimeList.append(thisDatetime)
173 173
174 174 if not(filenameList):
175 175 print "[Reading] Any file was found int time range %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
176 176 return None, None
177 177
178 178 print "[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime)
179 179 print
180 180
181 for i in range(len(filenameList)):
182 print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
181 # for i in range(len(filenameList)):
182 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
183 183
184 184 self.filenameList = filenameList
185 185 self.datetimeList = datetimeList
186 186
187 187 return pathList, filenameList
188 188
189 189 def __isFileInTimeRange(self,filename, startDate, endDate, startTime, endTime):
190 190
191 191 """
192 192 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
193 193
194 194 Inputs:
195 195 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
196 196
197 197 startDate : fecha inicial del rango seleccionado en formato datetime.date
198 198
199 199 endDate : fecha final del rango seleccionado en formato datetime.date
200 200
201 201 startTime : tiempo inicial del rango seleccionado en formato datetime.time
202 202
203 203 endTime : tiempo final del rango seleccionado en formato datetime.time
204 204
205 205 Return:
206 206 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
207 207 fecha especificado, de lo contrario retorna False.
208 208
209 209 Excepciones:
210 210 Si el archivo no existe o no puede ser abierto
211 211 Si la cabecera no puede ser leida.
212 212
213 213 """
214 214
215 215 try:
216 216 fp = h5py.File(filename,'r')
217 217 grp1 = fp['Data']
218 218
219 219 except IOError:
220 220 traceback.print_exc()
221 221 raise IOError, "The file %s can't be opened" %(filename)
222 222 #chino rata
223 223 #In case has utctime attribute
224 224 grp2 = grp1['utctime']
225 225 # thisUtcTime = grp2.value[0] - 5*3600 #To convert to local time
226 226 thisUtcTime = grp2.value[0]
227 227
228 228 fp.close()
229 229
230 230 if self.timezone == 'lt':
231 231 thisUtcTime -= 5*3600
232 232
233 233 thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600)
234 234 # thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0])
235 235 thisDate = thisDatetime.date()
236 236 thisTime = thisDatetime.time()
237 237
238 238 startUtcTime = (datetime.datetime.combine(thisDate,startTime)- datetime.datetime(1970, 1, 1)).total_seconds()
239 239 endUtcTime = (datetime.datetime.combine(thisDate,endTime)- datetime.datetime(1970, 1, 1)).total_seconds()
240 240
241 241 #General case
242 242 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
243 243 #-----------o----------------------------o-----------
244 244 # startTime endTime
245 245
246 246 if endTime >= startTime:
247 247 thisUtcLog = numpy.logical_and(thisUtcTime > startUtcTime, thisUtcTime < endUtcTime)
248 248 if numpy.any(thisUtcLog): #If there is one block between the hours mentioned
249 249 return thisDatetime
250 250 return None
251 251
252 252 #If endTime < startTime then endTime belongs to the next day
253 253 #<<<<<<<<<<<o o>>>>>>>>>>>
254 254 #-----------o----------------------------o-----------
255 255 # endTime startTime
256 256
257 257 if (thisDate == startDate) and numpy.all(thisUtcTime < startUtcTime):
258 258 return None
259 259
260 260 if (thisDate == endDate) and numpy.all(thisUtcTime > endUtcTime):
261 261 return None
262 262
263 263 if numpy.all(thisUtcTime < startUtcTime) and numpy.all(thisUtcTime > endUtcTime):
264 264 return None
265 265
266 266 return thisDatetime
267 267
268 268 def __setNextFileOffline(self):
269 269
270 270 self.fileIndex += 1
271 271 idFile = self.fileIndex
272 272
273 273 if not(idFile < len(self.filenameList)):
274 274 print "No more Files"
275 275 return 0
276 276
277 277 filename = self.filenameList[idFile]
278 278
279 279 filePointer = h5py.File(filename,'r')
280 280
281 281 self.filename = filename
282 282
283 283 self.fp = filePointer
284 284
285 285 print "Setting the file: %s"%self.filename
286 286
287 287 # self.__readMetadata()
288 288 self.__setBlockList()
289 289 self.__readData()
290 290 # self.nRecords = self.fp['Data'].attrs['blocksPerFile']
291 291 # self.nRecords = self.fp['Data'].attrs['nRecords']
292 292 self.blockIndex = 0
293 293 return 1
294 294
295 295 def __setBlockList(self):
296 296 '''
297 297 Selects the data within the times defined
298 298
299 299 self.fp
300 300 self.startTime
301 301 self.endTime
302 302
303 303 self.blockList
304 304 self.blocksPerFile
305 305
306 306 '''
307 307 fp = self.fp
308 308 startTime = self.startTime
309 309 endTime = self.endTime
310 310
311 311 grp = fp['Data']
312 312 thisUtcTime = grp['utctime'].value.astype(numpy.float)[0]
313 313
314 314 #ERROOOOR
315 315 if self.timezone == 'lt':
316 316 thisUtcTime -= 5*3600
317 317
318 318 thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600)
319 319
320 320 thisDate = thisDatetime.date()
321 321 thisTime = thisDatetime.time()
322 322
323 323 startUtcTime = (datetime.datetime.combine(thisDate,startTime) - datetime.datetime(1970, 1, 1)).total_seconds()
324 324 endUtcTime = (datetime.datetime.combine(thisDate,endTime) - datetime.datetime(1970, 1, 1)).total_seconds()
325 325
326 326 ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0]
327 327
328 328 self.blockList = ind
329 329 self.blocksPerFile = len(ind)
330 330
331 331 return
332 332
333 333 def __readMetadata(self):
334 334 '''
335 335 Reads Metadata
336 336
337 337 self.pathMeta
338 338
339 339 self.listShapes
340 340 self.listMetaname
341 341 self.listMeta
342 342
343 343 '''
344 344
345 345 # grp = self.fp['Data']
346 346 # pathMeta = os.path.join(self.path, grp.attrs['metadata'])
347 347 #
348 348 # if pathMeta == self.pathMeta:
349 349 # return
350 350 # else:
351 351 # self.pathMeta = pathMeta
352 352 #
353 353 # filePointer = h5py.File(self.pathMeta,'r')
354 354 # groupPointer = filePointer['Metadata']
355 355
356 356 filename = self.filenameList[0]
357 357
358 358 fp = h5py.File(filename,'r')
359 359
360 360 gp = fp['Metadata']
361 361
362 362 listMetaname = []
363 363 listMetadata = []
364 364 for item in gp.items():
365 365 name = item[0]
366 366
367 367 if name=='array dimensions':
368 368 table = gp[name][:]
369 369 listShapes = {}
370 370 for shapes in table:
371 371 listShapes[shapes[0]] = numpy.array([shapes[1],shapes[2],shapes[3],shapes[4],shapes[5]])
372 372 else:
373 373 data = gp[name].value
374 374 listMetaname.append(name)
375 375 listMetadata.append(data)
376 376
377 377 # if name=='type':
378 378 # self.__initDataOut(data)
379 379
380 380 self.listShapes = listShapes
381 381 self.listMetaname = listMetaname
382 382 self.listMeta = listMetadata
383 383
384 384 fp.close()
385 385 return
386 386
387 387 def __readData(self):
388 388 grp = self.fp['Data']
389 389 listdataname = []
390 390 listdata = []
391 391
392 392 for item in grp.items():
393 393 name = item[0]
394 394 listdataname.append(name)
395 395
396 396 array = self.__setDataArray(grp[name],self.listShapes[name])
397 397 listdata.append(array)
398 398
399 399 self.listDataname = listdataname
400 400 self.listData = listdata
401 401 return
402 402
403 403 def __setDataArray(self, dataset, shapes):
404 404
405 405 nDims = shapes[0]
406 406
407 407 nDim2 = shapes[1] #Dimension 0
408 408
409 409 nDim1 = shapes[2] #Dimension 1, number of Points or Parameters
410 410
411 411 nDim0 = shapes[3] #Dimension 2, number of samples or ranges
412 412
413 413 mode = shapes[4] #Mode of storing
414 414
415 415 blockList = self.blockList
416 416
417 417 blocksPerFile = self.blocksPerFile
418 418
419 419 #Depending on what mode the data was stored
420 420 if mode == 0: #Divided in channels
421 421 arrayData = dataset.value.astype(numpy.float)[0][blockList]
422 422 if mode == 1: #Divided in parameter
423 423 strds = 'table'
424 424 nDatas = nDim1
425 425 newShapes = (blocksPerFile,nDim2,nDim0)
426 426 elif mode==2: #Concatenated in a table
427 427 strds = 'table0'
428 428 arrayData = dataset[strds].value
429 429 #Selecting part of the dataset
430 430 utctime = arrayData[:,0]
431 431 u, indices = numpy.unique(utctime, return_index=True)
432 432
433 433 if blockList.size != indices.size:
434 434 indMin = indices[blockList[0]]
435 435 if blockList[1] + 1 >= indices.size:
436 436 arrayData = arrayData[indMin:,:]
437 437 else:
438 438 indMax = indices[blockList[1] + 1]
439 439 arrayData = arrayData[indMin:indMax,:]
440 440 return arrayData
441 441
442 442 # One dimension
443 443 if nDims == 0:
444 444 arrayData = dataset.value.astype(numpy.float)[0][blockList]
445 445
446 446 # Two dimensions
447 447 elif nDims == 2:
448 448 arrayData = numpy.zeros((blocksPerFile,nDim1,nDim0))
449 449 newShapes = (blocksPerFile,nDim0)
450 450 nDatas = nDim1
451 451
452 452 for i in range(nDatas):
453 453 data = dataset[strds + str(i)].value
454 454 arrayData[:,i,:] = data[blockList,:]
455 455
456 456 # Three dimensions
457 457 else:
458 458 arrayData = numpy.zeros((blocksPerFile,nDim2,nDim1,nDim0))
459 459 for i in range(nDatas):
460 460
461 461 data = dataset[strds + str(i)].value
462 462
463 463 for b in range(blockList.size):
464 464 arrayData[b,:,i,:] = data[:,:,blockList[b]]
465 465
466 466 return arrayData
467 467
468 468 def __setDataOut(self):
469 469 listMeta = self.listMeta
470 470 listMetaname = self.listMetaname
471 471 listDataname = self.listDataname
472 472 listData = self.listData
473 473 listShapes = self.listShapes
474 474
475 475 blockIndex = self.blockIndex
476 476 # blockList = self.blockList
477 477
478 478 for i in range(len(listMeta)):
479 479 setattr(self.dataOut,listMetaname[i],listMeta[i])
480 480
481 481 for j in range(len(listData)):
482 482 nShapes = listShapes[listDataname[j]][0]
483 483 mode = listShapes[listDataname[j]][4]
484 484 if nShapes == 1:
485 485 setattr(self.dataOut,listDataname[j],listData[j][blockIndex])
486 486 elif nShapes > 1:
487 487 setattr(self.dataOut,listDataname[j],listData[j][blockIndex,:])
488 488 elif mode==0:
489 489 setattr(self.dataOut,listDataname[j],listData[j][blockIndex])
490 490 #Mode Meteors
491 491 elif mode ==2:
492 492 selectedData = self.__selectDataMode2(listData[j], blockIndex)
493 493 setattr(self.dataOut, listDataname[j], selectedData)
494 494 return
495 495
496 496 def __selectDataMode2(self, data, blockIndex):
497 497 utctime = data[:,0]
498 498 aux, indices = numpy.unique(utctime, return_inverse=True)
499 499 selInd = numpy.where(indices == blockIndex)[0]
500 500 selData = data[selInd,:]
501 501
502 502 return selData
503 503
504 504 def getData(self):
505 505
506 506 # if self.flagNoMoreFiles:
507 507 # self.dataOut.flagNoData = True
508 508 # print 'Process finished'
509 509 # return 0
510 510 #
511 511 if self.blockIndex==self.blocksPerFile:
512 512 if not( self.__setNextFileOffline() ):
513 513 self.dataOut.flagNoData = True
514 514 return 0
515 515
516 516 # if self.datablock == None: # setear esta condicion cuando no hayan datos por leers
517 517 # self.dataOut.flagNoData = True
518 518 # return 0
519 519 # self.__readData()
520 520 self.__setDataOut()
521 521 self.dataOut.flagNoData = False
522 522
523 523 self.blockIndex += 1
524 524
525 525 return
526 526
527 527 def run(self, **kwargs):
528 528
529 529 if not(self.isConfig):
530 530 self.setup(**kwargs)
531 531 # self.setObjProperties()
532 532 self.isConfig = True
533 533
534 534 self.getData()
535 535
536 536 return
537 537
538 538 class ParamWriter(Operation):
539 539 '''
540 540 HDF5 Writer, stores parameters data in HDF5 format files
541 541
542 542 path: path where the files will be stored
543 543
544 544 blocksPerFile: number of blocks that will be saved in per HDF5 format file
545 545
546 546 mode: selects the data stacking mode: '0' channels, '1' parameters, '3' table (for meteors)
547 547
548 548 metadataList: list of attributes that will be stored as metadata
549 549
550 550 dataList: list of attributes that will be stores as data
551 551
552 552 '''
553 553
554 554
555 555 ext = ".hdf5"
556 556
557 557 optchar = "D"
558 558
559 559 metaoptchar = "M"
560 560
561 561 metaFile = None
562 562
563 563 filename = None
564 564
565 565 path = None
566 566
567 567 setFile = None
568 568
569 569 fp = None
570 570
571 571 grp = None
572 572
573 573 ds = None
574 574
575 575 firsttime = True
576 576
577 577 #Configurations
578 578
579 579 blocksPerFile = None
580 580
581 581 blockIndex = None
582 582
583 583 dataOut = None
584 584
585 585 #Data Arrays
586 586
587 587 dataList = None
588 588
589 589 metadataList = None
590 590
591 591 # arrayDim = None
592 592
593 593 dsList = None #List of dictionaries with dataset properties
594 594
595 595 tableDim = None
596 596
597 597 # dtype = [('arrayName', 'S20'),('nChannels', 'i'), ('nPoints', 'i'), ('nSamples', 'i'),('mode', 'b')]
598 598
599 599 dtype = [('arrayName', 'S20'),('nDimensions', 'i'), ('dim2', 'i'), ('dim1', 'i'),('dim0', 'i'),('mode', 'b')]
600 600
601 601 currentDay = None
602 602
603 603 lastTime = None
604 604
605 605 def __init__(self, **kwargs):
606 606 Operation.__init__(self, **kwargs)
607 607 self.isConfig = False
608 608 return
609 609
610 610 def setup(self, dataOut, **kwargs):
611 611
612 612 self.path = kwargs['path']
613 613
614 614 if kwargs.has_key('blocksPerFile'):
615 615 self.blocksPerFile = kwargs['blocksPerFile']
616 616 else:
617 617 self.blocksPerFile = 10
618 618
619 619 self.metadataList = kwargs['metadataList']
620 620 self.dataList = kwargs['dataList']
621 621 self.dataOut = dataOut
622 622
623 623 if kwargs.has_key('mode'):
624 624 mode = kwargs['mode']
625 625
626 626 if type(mode) == int:
627 627 mode = numpy.zeros(len(self.dataList)) + mode
628 628 else:
629 629 mode = numpy.ones(len(self.dataList))
630 630
631 631 self.mode = mode
632 632
633 633 arrayDim = numpy.zeros((len(self.dataList),5))
634 634
635 635 #Table dimensions
636 636 dtype0 = self.dtype
637 637 tableList = []
638 638
639 639 #Dictionary and list of tables
640 640 dsList = []
641 641
642 642 for i in range(len(self.dataList)):
643 643 dsDict = {}
644 644 dataAux = getattr(self.dataOut, self.dataList[i])
645 645 dsDict['variable'] = self.dataList[i]
646 646 #--------------------- Conditionals ------------------------
647 647 #There is no data
648 648 if dataAux is None:
649 649 return 0
650 650
651 651 #Not array, just a number
652 652 #Mode 0
653 653 if type(dataAux)==float or type(dataAux)==int:
654 654 dsDict['mode'] = 0
655 655 dsDict['nDim'] = 0
656 656 arrayDim[i,0] = 0
657 657 dsList.append(dsDict)
658 658
659 659 #Mode 2: meteors
660 660 elif mode[i] == 2:
661 661 # dsDict['nDim'] = 0
662 662 dsDict['dsName'] = 'table0'
663 663 dsDict['mode'] = 2 # Mode meteors
664 664 dsDict['shape'] = dataAux.shape[-1]
665 665 dsDict['nDim'] = 0
666 666 dsDict['dsNumber'] = 1
667 667
668 668 arrayDim[i,3] = dataAux.shape[-1]
669 669 arrayDim[i,4] = mode[i] #Mode the data was stored
670 670
671 671 dsList.append(dsDict)
672 672
673 673 #Mode 1
674 674 else:
675 675 arrayDim0 = dataAux.shape #Data dimensions
676 676 arrayDim[i,0] = len(arrayDim0) #Number of array dimensions
677 677 arrayDim[i,4] = mode[i] #Mode the data was stored
678 678
679 679 strtable = 'table'
680 680 dsDict['mode'] = 1 # Mode parameters
681 681
682 682 # Three-dimension arrays
683 683 if len(arrayDim0) == 3:
684 684 arrayDim[i,1:-1] = numpy.array(arrayDim0)
685 685 nTables = int(arrayDim[i,2])
686 686 dsDict['dsNumber'] = nTables
687 687 dsDict['shape'] = arrayDim[i,2:4]
688 688 dsDict['nDim'] = 3
689 689
690 690 for j in range(nTables):
691 691 dsDict = dsDict.copy()
692 692 dsDict['dsName'] = strtable + str(j)
693 693 dsList.append(dsDict)
694 694
695 695 # Two-dimension arrays
696 696 elif len(arrayDim0) == 2:
697 697 arrayDim[i,2:-1] = numpy.array(arrayDim0)
698 698 nTables = int(arrayDim[i,2])
699 699 dsDict['dsNumber'] = nTables
700 700 dsDict['shape'] = arrayDim[i,3]
701 701 dsDict['nDim'] = 2
702 702
703 703 for j in range(nTables):
704 704 dsDict = dsDict.copy()
705 705 dsDict['dsName'] = strtable + str(j)
706 706 dsList.append(dsDict)
707 707
708 708 # One-dimension arrays
709 709 elif len(arrayDim0) == 1:
710 710 arrayDim[i,3] = arrayDim0[0]
711 711 dsDict['shape'] = arrayDim0[0]
712 712 dsDict['dsNumber'] = 1
713 713 dsDict['dsName'] = strtable + str(0)
714 714 dsDict['nDim'] = 1
715 715 dsList.append(dsDict)
716 716
717 717 table = numpy.array((self.dataList[i],) + tuple(arrayDim[i,:]),dtype = dtype0)
718 718 tableList.append(table)
719 719
720 720 # self.arrayDim = arrayDim
721 721 self.dsList = dsList
722 722 self.tableDim = numpy.array(tableList, dtype = dtype0)
723 723 self.blockIndex = 0
724 724
725 725 timeTuple = time.localtime(dataOut.utctime)
726 726 self.currentDay = timeTuple.tm_yday
727 727 return 1
728 728
729 729 def putMetadata(self):
730 730
731 731 fp = self.createMetadataFile()
732 732 self.writeMetadata(fp)
733 733 fp.close()
734 734 return
735 735
736 736 def createMetadataFile(self):
737 737 ext = self.ext
738 738 path = self.path
739 739 setFile = self.setFile
740 740
741 741 timeTuple = time.localtime(self.dataOut.utctime)
742 742
743 743 subfolder = ''
744 744 fullpath = os.path.join( path, subfolder )
745 745
746 746 if not( os.path.exists(fullpath) ):
747 747 os.mkdir(fullpath)
748 748 setFile = -1 #inicializo mi contador de seteo
749 749
750 750 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
751 751 fullpath = os.path.join( path, subfolder )
752 752
753 753 if not( os.path.exists(fullpath) ):
754 754 os.mkdir(fullpath)
755 755 setFile = -1 #inicializo mi contador de seteo
756 756
757 757 else:
758 758 filesList = os.listdir( fullpath )
759 759 filesList = sorted( filesList, key=str.lower )
760 760 if len( filesList ) > 0:
761 761 filesList = [k for k in filesList if 'M' in k]
762 762 filen = filesList[-1]
763 763 # el filename debera tener el siguiente formato
764 764 # 0 1234 567 89A BCDE (hex)
765 765 # x YYYY DDD SSS .ext
766 766 if isNumber( filen[8:11] ):
767 767 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
768 768 else:
769 769 setFile = -1
770 770 else:
771 771 setFile = -1 #inicializo mi contador de seteo
772 772
773 773 setFile += 1
774 774
775 775 file = '%s%4.4d%3.3d%3.3d%s' % (self.metaoptchar,
776 776 timeTuple.tm_year,
777 777 timeTuple.tm_yday,
778 778 setFile,
779 779 ext )
780 780
781 781 filename = os.path.join( path, subfolder, file )
782 782 self.metaFile = file
783 783 #Setting HDF5 File
784 784 fp = h5py.File(filename,'w')
785 785
786 786 return fp
787 787
788 788 def writeMetadata(self, fp):
789 789
790 790 grp = fp.create_group("Metadata")
791 791 grp.create_dataset('array dimensions', data = self.tableDim, dtype = self.dtype)
792 792
793 793 for i in range(len(self.metadataList)):
794 794 grp.create_dataset(self.metadataList[i], data=getattr(self.dataOut, self.metadataList[i]))
795 795 return
796 796
797 797 def timeFlag(self):
798 798 currentTime = self.dataOut.utctime
799 799
800 800 if self.lastTime is None:
801 801 self.lastTime = currentTime
802 802
803 803 #Day
804 804 timeTuple = time.localtime(currentTime)
805 805 dataDay = timeTuple.tm_yday
806 806
807 807 #Time
808 808 timeDiff = currentTime - self.lastTime
809 809
810 810 #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
811 811 if dataDay != self.currentDay:
812 812 self.currentDay = dataDay
813 813 return True
814 814 elif timeDiff > 3*60*60:
815 815 self.lastTime = currentTime
816 816 return True
817 817 else:
818 818 self.lastTime = currentTime
819 819 return False
820 820
821 821 def setNextFile(self):
822 822
823 823 ext = self.ext
824 824 path = self.path
825 825 setFile = self.setFile
826 826 mode = self.mode
827 827
828 828 timeTuple = time.localtime(self.dataOut.utctime)
829 829 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
830 830
831 831 fullpath = os.path.join( path, subfolder )
832 832
833 833 if os.path.exists(fullpath):
834 834 filesList = os.listdir( fullpath )
835 835 filesList = [k for k in filesList if 'D' in k]
836 836 if len( filesList ) > 0:
837 837 filesList = sorted( filesList, key=str.lower )
838 838 filen = filesList[-1]
839 839 # el filename debera tener el siguiente formato
840 840 # 0 1234 567 89A BCDE (hex)
841 841 # x YYYY DDD SSS .ext
842 842 if isNumber( filen[8:11] ):
843 843 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
844 844 else:
845 845 setFile = -1
846 846 else:
847 847 setFile = -1 #inicializo mi contador de seteo
848 848 else:
849 849 os.makedirs(fullpath)
850 850 setFile = -1 #inicializo mi contador de seteo
851 851
852 852 setFile += 1
853 853
854 854 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
855 855 timeTuple.tm_year,
856 856 timeTuple.tm_yday,
857 857 setFile,
858 858 ext )
859 859
860 860 filename = os.path.join( path, subfolder, file )
861 861
862 862 #Setting HDF5 File
863 863 fp = h5py.File(filename,'w')
864 864 #write metadata
865 865 self.writeMetadata(fp)
866 866 #Write data
867 867 grp = fp.create_group("Data")
868 868 # grp.attrs['metadata'] = self.metaFile
869 869
870 870 # grp.attrs['blocksPerFile'] = 0
871 871 ds = []
872 872 data = []
873 873 dsList = self.dsList
874 874 i = 0
875 875 while i < len(dsList):
876 876 dsInfo = dsList[i]
877 877 #One-dimension data
878 878 if dsInfo['mode'] == 0:
879 879 # ds0 = grp.create_dataset(self.dataList[i], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype='S20')
880 880 ds0 = grp.create_dataset(dsInfo['variable'], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype=numpy.float64)
881 881 ds.append(ds0)
882 882 data.append([])
883 883 i += 1
884 884 continue
885 885 # nDimsForDs.append(nDims[i])
886 886
887 887 elif dsInfo['mode'] == 2:
888 888 grp0 = grp.create_group(dsInfo['variable'])
889 889 ds0 = grp0.create_dataset(dsInfo['dsName'], (1,dsInfo['shape']), data = numpy.zeros((1,dsInfo['shape'])) , maxshape=(None,dsInfo['shape']), chunks=True)
890 890 ds.append(ds0)
891 891 data.append([])
892 892 i += 1
893 893 continue
894 894
895 895 elif dsInfo['mode'] == 1:
896 896 grp0 = grp.create_group(dsInfo['variable'])
897 897
898 898 for j in range(dsInfo['dsNumber']):
899 899 dsInfo = dsList[i]
900 900 tableName = dsInfo['dsName']
901 901 shape = int(dsInfo['shape'])
902 902
903 903 if dsInfo['nDim'] == 3:
904 904 ds0 = grp0.create_dataset(tableName, (shape[0],shape[1],1) , data = numpy.zeros((shape[0],shape[1],1)), maxshape = (None,shape[1],None), chunks=True)
905 905 else:
906 906 ds0 = grp0.create_dataset(tableName, (1,shape), data = numpy.zeros((1,shape)) , maxshape=(None,shape), chunks=True)
907 907
908 908 ds.append(ds0)
909 909 data.append([])
910 910 i += 1
911 911 # nDimsForDs.append(nDims[i])
912 912
913 913 fp.flush()
914 914 fp.close()
915 915
916 916 # self.nDatas = nDatas
917 917 # self.nDims = nDims
918 918 # self.nDimsForDs = nDimsForDs
919 919 #Saving variables
920 920 print 'Writing the file: %s'%filename
921 921 self.filename = filename
922 922 # self.fp = fp
923 923 # self.grp = grp
924 924 # self.grp.attrs.modify('nRecords', 1)
925 925 self.ds = ds
926 926 self.data = data
927 927 # self.setFile = setFile
928 928 self.firsttime = True
929 929 self.blockIndex = 0
930 930 return
931 931
932 932 def putData(self):
933 933
934 934 if self.blockIndex == self.blocksPerFile or self.timeFlag():
935 935 self.setNextFile()
936 936
937 937 # if not self.firsttime:
938 938 self.readBlock()
939 939 self.setBlock() #Prepare data to be written
940 940 self.writeBlock() #Write data
941 941
942 942 return
943 943
944 944 def readBlock(self):
945 945
946 946 '''
947 947 data Array configured
948 948
949 949
950 950 self.data
951 951 '''
952 952 dsList = self.dsList
953 953 ds = self.ds
954 954 #Setting HDF5 File
955 955 fp = h5py.File(self.filename,'r+')
956 956 grp = fp["Data"]
957 957 ind = 0
958 958
959 959 # grp.attrs['blocksPerFile'] = 0
960 960 while ind < len(dsList):
961 961 dsInfo = dsList[ind]
962 962
963 963 if dsInfo['mode'] == 0:
964 964 ds0 = grp[dsInfo['variable']]
965 965 ds[ind] = ds0
966 966 ind += 1
967 967 else:
968 968
969 969 grp0 = grp[dsInfo['variable']]
970 970
971 971 for j in range(dsInfo['dsNumber']):
972 972 dsInfo = dsList[ind]
973 973 ds0 = grp0[dsInfo['dsName']]
974 974 ds[ind] = ds0
975 975 ind += 1
976 976
977 977 self.fp = fp
978 978 self.grp = grp
979 979 self.ds = ds
980 980
981 981 return
982 982
983 983 def setBlock(self):
984 984 '''
985 985 data Array configured
986 986
987 987
988 988 self.data
989 989 '''
990 990 #Creating Arrays
991 991 dsList = self.dsList
992 992 data = self.data
993 993 ind = 0
994 994
995 995 while ind < len(dsList):
996 996 dsInfo = dsList[ind]
997 997 dataAux = getattr(self.dataOut, dsInfo['variable'])
998 998
999 999 mode = dsInfo['mode']
1000 1000 nDim = dsInfo['nDim']
1001 1001
1002 1002 if mode == 0 or mode == 2 or nDim == 1:
1003 1003 data[ind] = dataAux
1004 1004 ind += 1
1005 1005 # elif nDim == 1:
1006 1006 # data[ind] = numpy.reshape(dataAux,(numpy.size(dataAux),1))
1007 1007 # ind += 1
1008 1008 elif nDim == 2:
1009 1009 for j in range(dsInfo['dsNumber']):
1010 1010 data[ind] = dataAux[j,:]
1011 1011 ind += 1
1012 1012 elif nDim == 3:
1013 1013 for j in range(dsInfo['dsNumber']):
1014 1014 data[ind] = dataAux[:,j,:]
1015 1015 ind += 1
1016 1016
1017 1017 self.data = data
1018 1018 return
1019 1019
1020 1020 def writeBlock(self):
1021 1021 '''
1022 1022 Saves the block in the HDF5 file
1023 1023 '''
1024 1024 dsList = self.dsList
1025 1025
1026 1026 for i in range(len(self.ds)):
1027 1027 dsInfo = dsList[i]
1028 1028 nDim = dsInfo['nDim']
1029 1029 mode = dsInfo['mode']
1030 1030
1031 1031 # First time
1032 1032 if self.firsttime:
1033 1033 # self.ds[i].resize(self.data[i].shape)
1034 1034 # self.ds[i][self.blockIndex,:] = self.data[i]
1035 1035 if type(self.data[i]) == numpy.ndarray:
1036 1036
1037 1037 if nDim == 3:
1038 1038 self.data[i] = self.data[i].reshape((self.data[i].shape[0],self.data[i].shape[1],1))
1039 1039 self.ds[i].resize(self.data[i].shape)
1040 1040 if mode == 2:
1041 1041 self.ds[i].resize(self.data[i].shape)
1042 1042 self.ds[i][:] = self.data[i]
1043 1043 else:
1044 1044
1045 1045 # From second time
1046 1046 # Meteors!
1047 1047 if mode == 2:
1048 1048 dataShape = self.data[i].shape
1049 1049 dsShape = self.ds[i].shape
1050 1050 self.ds[i].resize((self.ds[i].shape[0] + dataShape[0],self.ds[i].shape[1]))
1051 1051 self.ds[i][dsShape[0]:,:] = self.data[i]
1052 1052 # No dimension
1053 1053 elif mode == 0:
1054 1054 self.ds[i].resize((self.ds[i].shape[0], self.ds[i].shape[1] + 1))
1055 1055 self.ds[i][0,-1] = self.data[i]
1056 1056 # One dimension
1057 1057 elif nDim == 1:
1058 1058 self.ds[i].resize((self.ds[i].shape[0] + 1, self.ds[i].shape[1]))
1059 1059 self.ds[i][-1,:] = self.data[i]
1060 1060 # Two dimension
1061 1061 elif nDim == 2:
1062 1062 self.ds[i].resize((self.ds[i].shape[0] + 1,self.ds[i].shape[1]))
1063 1063 self.ds[i][self.blockIndex,:] = self.data[i]
1064 1064 # Three dimensions
1065 1065 elif nDim == 3:
1066 1066 self.ds[i].resize((self.ds[i].shape[0],self.ds[i].shape[1],self.ds[i].shape[2]+1))
1067 1067 self.ds[i][:,:,-1] = self.data[i]
1068 1068
1069 1069 self.firsttime = False
1070 1070 self.blockIndex += 1
1071 1071
1072 1072 #Close to save changes
1073 1073 self.fp.flush()
1074 1074 self.fp.close()
1075 1075 return
1076 1076
1077 1077 def run(self, dataOut, **kwargs):
1078 1078
1079 1079 if not(self.isConfig):
1080 1080 flagdata = self.setup(dataOut, **kwargs)
1081 1081
1082 1082 if not(flagdata):
1083 1083 return
1084 1084
1085 1085 self.isConfig = True
1086 1086 # self.putMetadata()
1087 1087 self.setNextFile()
1088 1088
1089 1089 self.putData()
1090 1090 return
1 NO CONTENT: modified file, binary diff hidden
This diff has been collapsed as it changes many lines, (568 lines changed) Show them Hide them
@@ -1,1237 +1,707
1 1 '''
2 2 Created on Jul 2, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import numpy
7 7
8 8 from jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
9 9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
10 10 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
11 11 from schainpy.model.data.jrodata import Spectra
12 12
13 13 class SpectraReader(JRODataReader, ProcessingUnit):
14 <<<<<<< HEAD
14
15 15 """
16 16 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
17 17 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
18 =======
19 """
20 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
21 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
22 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
23 18 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
24 19
25 20 paresCanalesIguales * alturas * perfiles (Self Spectra)
26 21 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
27 22 canales * alturas (DC Channels)
28 23
29 <<<<<<< HEAD
24
30 25 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
31 26 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
32 27 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
33 28 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
34 29
35 30 Example:
36 31 dpath = "/home/myuser/data"
37 32
38 33 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
39 34
40 35 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
41 36
42 37 readerObj = SpectraReader()
43 38
44 39 readerObj.setup(dpath, startTime, endTime)
45 40
46 41 while(True):
47 42
48 43 readerObj.getData()
49 44
50 45 print readerObj.data_spc
51 46
52 47 print readerObj.data_cspc
53 48
54 49 print readerObj.data_dc
55 50
56 51 if readerObj.flagNoMoreFiles:
57 52 break
58 53
59 54 """
60 55
61 56 pts2read_SelfSpectra = 0
62 57
63 58 pts2read_CrossSpectra = 0
64 59
65 60 pts2read_DCchannels = 0
66 61
67 62 ext = ".pdata"
68 63
69 64 optchar = "P"
70 65
71 66 dataOut = None
72 67
73 68 nRdChannels = None
74 69
75 70 nRdPairs = None
76 71
77 72 rdPairList = []
78 73
79 74 def __init__(self, **kwargs):
80 75 """
81 76 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
82 77
83 78 Inputs:
84 =======
85 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
86 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
87 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
88 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
89
90 Example:
91 dpath = "/home/myuser/data"
92
93 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
94
95 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
96
97 readerObj = SpectraReader()
98
99 readerObj.setup(dpath, startTime, endTime)
100
101 while(True):
102
103 readerObj.getData()
104
105 print readerObj.data_spc
106
107 print readerObj.data_cspc
108
109 print readerObj.data_dc
110
111 if readerObj.flagNoMoreFiles:
112 break
113
114 """
115
116 pts2read_SelfSpectra = 0
117
118 pts2read_CrossSpectra = 0
119
120 pts2read_DCchannels = 0
121
122 ext = ".pdata"
123
124 optchar = "P"
125
126 dataOut = None
127
128 nRdChannels = None
129
130 nRdPairs = None
131
132 rdPairList = []
133
134 def __init__(self):
135 """
136 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
137 79
138 Inputs:
139 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
140 80 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
141 81 almacenar un perfil de datos cada vez que se haga un requerimiento
142 82 (getData). El perfil sera obtenido a partir del buffer de datos,
143 83 si el buffer esta vacio se hara un nuevo proceso de lectura de un
144 84 bloque de datos.
145 85 Si este parametro no es pasado se creara uno internamente.
146 <<<<<<< HEAD
147 86
148 Affected:
149 =======
150 87
151 88 Affected:
152 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
89
153 90 self.dataOut
154 91
155 92 Return : None
156 93 """
157 <<<<<<< HEAD
158
159 #Eliminar de la base la herencia
160 ProcessingUnit.__init__(self, **kwargs)
161
162 # self.isConfig = False
163
164 self.pts2read_SelfSpectra = 0
165
166 self.pts2read_CrossSpectra = 0
167
168 self.pts2read_DCchannels = 0
169
170 self.datablock = None
171
172 self.utc = None
173
174 self.ext = ".pdata"
175
176 self.optchar = "P"
177
178 self.basicHeaderObj = BasicHeader(LOCALTIME)
179
180 self.systemHeaderObj = SystemHeader()
181
182 self.radarControllerHeaderObj = RadarControllerHeader()
183
184 self.processingHeaderObj = ProcessingHeader()
185
186 self.online = 0
187
188 self.fp = None
189
190 self.idFile = None
191
192 self.dtype = None
193
194 self.fileSizeByHeader = None
195
196 self.filenameList = []
197
198 self.filename = None
199
200 self.fileSize = None
201
202 self.firstHeaderSize = 0
203
204 self.basicHeaderSize = 24
205
206 self.pathList = []
207
208 self.lastUTTime = 0
209
210 self.maxTimeStep = 30
211
212 self.flagNoMoreFiles = 0
213
214 self.set = 0
215
216 self.path = None
217
218 self.delay = 60 #seconds
219
220 self.nTries = 3 #quantity tries
221
222 self.nFiles = 3 #number of files for searching
223
224 self.nReadBlocks = 0
225
226 self.flagIsNewFile = 1
227
228 self.__isFirstTimeOnline = 1
229
230 # self.ippSeconds = 0
231
232 self.flagDiscontinuousBlock = 0
233
234 self.flagIsNewBlock = 0
235 94
236 self.nTotalBlocks = 0
237
238 self.blocksize = 0
239
240 self.dataOut = self.createObjByDefault()
241
242 =======
243 95
244 96 #Eliminar de la base la herencia
245 ProcessingUnit.__init__(self)
97 ProcessingUnit.__init__(self, **kwargs)
246 98
247 99 # self.isConfig = False
248 100
249 101 self.pts2read_SelfSpectra = 0
250 102
251 103 self.pts2read_CrossSpectra = 0
252 104
253 105 self.pts2read_DCchannels = 0
254 106
255 107 self.datablock = None
256 108
257 109 self.utc = None
258 110
259 111 self.ext = ".pdata"
260 112
261 113 self.optchar = "P"
262 114
263 115 self.basicHeaderObj = BasicHeader(LOCALTIME)
264 116
265 117 self.systemHeaderObj = SystemHeader()
266 118
267 119 self.radarControllerHeaderObj = RadarControllerHeader()
268 120
269 121 self.processingHeaderObj = ProcessingHeader()
270 122
271 123 self.online = 0
272 124
273 125 self.fp = None
274 126
275 127 self.idFile = None
276 128
277 129 self.dtype = None
278 130
279 131 self.fileSizeByHeader = None
280 132
281 133 self.filenameList = []
282 134
283 135 self.filename = None
284 136
285 137 self.fileSize = None
286 138
287 139 self.firstHeaderSize = 0
288 140
289 141 self.basicHeaderSize = 24
290 142
291 143 self.pathList = []
292 144
293 145 self.lastUTTime = 0
294 146
295 147 self.maxTimeStep = 30
296 148
297 149 self.flagNoMoreFiles = 0
298 150
299 151 self.set = 0
300 152
301 153 self.path = None
302 154
303 155 self.delay = 60 #seconds
304 156
305 157 self.nTries = 3 #quantity tries
306 158
307 159 self.nFiles = 3 #number of files for searching
308 160
309 161 self.nReadBlocks = 0
310 162
311 163 self.flagIsNewFile = 1
312 164
313 165 self.__isFirstTimeOnline = 1
314 166
315 167 # self.ippSeconds = 0
316 168
317 169 self.flagDiscontinuousBlock = 0
318 170
319 171 self.flagIsNewBlock = 0
320 172
321 173 self.nTotalBlocks = 0
322 174
323 175 self.blocksize = 0
324 176
325 177 self.dataOut = self.createObjByDefault()
326 178
327 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
328 179 self.profileIndex = 1 #Always
329 180
330 181
331 182 def createObjByDefault(self):
332 <<<<<<< HEAD
333
334 dataObj = Spectra()
335
336 return dataObj
337
338 =======
339 183
340 184 dataObj = Spectra()
341 185
342 186 return dataObj
343 187
344 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
345 188 def __hasNotDataInBuffer(self):
346 189 return 1
347 190
348 191
349 192 def getBlockDimension(self):
350 193 """
351 194 Obtiene la cantidad de puntos a leer por cada bloque de datos
352 <<<<<<< HEAD
353
354 =======
355 195
356 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
357 196 Affected:
358 197 self.nRdChannels
359 198 self.nRdPairs
360 199 self.pts2read_SelfSpectra
361 200 self.pts2read_CrossSpectra
362 201 self.pts2read_DCchannels
363 202 self.blocksize
364 203 self.dataOut.nChannels
365 204 self.dataOut.nPairs
366 205
367 206 Return:
368 207 None
369 208 """
370 209 self.nRdChannels = 0
371 210 self.nRdPairs = 0
372 211 self.rdPairList = []
373 <<<<<<< HEAD
374 212
375 213 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
376 214 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
377 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
378 =======
379
380
381 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
382 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
383 215 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
384 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
216
385 217 else:
386 218 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
387 219 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
388 220
389 221 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
390 <<<<<<< HEAD
391
392 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
393 self.blocksize = self.pts2read_SelfSpectra
394
395 if self.processingHeaderObj.flag_cspc:
396 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
397 self.blocksize += self.pts2read_CrossSpectra
398
399 if self.processingHeaderObj.flag_dc:
400 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
401 self.blocksize += self.pts2read_DCchannels
402
403 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
404
405
406 =======
407 222
408 223 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
409 224 self.blocksize = self.pts2read_SelfSpectra
410 225
411 226 if self.processingHeaderObj.flag_cspc:
412 227 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
413 228 self.blocksize += self.pts2read_CrossSpectra
414 229
415 230 if self.processingHeaderObj.flag_dc:
416 231 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
417 232 self.blocksize += self.pts2read_DCchannels
418 233
419 234 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
420 235
421 236
422 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
423 237 def readBlock(self):
424 238 """
425 239 Lee el bloque de datos desde la posicion actual del puntero del archivo
426 240 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
427 241 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
428 242 es seteado a 0
429 <<<<<<< HEAD
430
431 Return: None
432
433 Variables afectadas:
434
435 =======
436 243
437 244 Return: None
438 245
439 246 Variables afectadas:
440 247
441 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
248
442 249 self.flagIsNewFile
443 250 self.flagIsNewBlock
444 251 self.nTotalBlocks
445 252 self.data_spc
446 253 self.data_cspc
447 254 self.data_dc
448 255
449 <<<<<<< HEAD
450 Exceptions:
451 Si un bloque leido no es un bloque valido
452 """
453 =======
454 256 Exceptions:
455 257 Si un bloque leido no es un bloque valido
456 258 """
457 259 print ' ======================================================== '
458 260 print ' '
459 261 print ' '
460 262 print self.processingHeaderObj.totalSpectra, 'TotalSpectra', type(self.processingHeaderObj.totalSpectra)
461 263 print self.processingHeaderObj.spectraComb, 'SpectraComb', type(self.processingHeaderObj.spectraComb)
462 264 print ' '
463 265 print ' '
464 266 print ' ======================================================== '
465 267
466 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
268
467 269 blockOk_flag = False
468 270 fpointer = self.fp.tell()
469 271
470 272 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
471 273 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
472 <<<<<<< HEAD
473
474 if self.processingHeaderObj.flag_cspc:
475 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
476 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
477
478 if self.processingHeaderObj.flag_dc:
479 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
480 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
481
482 274
483 =======
484
485 275 if self.processingHeaderObj.flag_cspc:
486 276 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
487 277 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
488 278
489 279 if self.processingHeaderObj.flag_dc:
490 280 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
491 281 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
492 282
493 283
494 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
495 284 if not(self.processingHeaderObj.shif_fft):
496 285 #desplaza a la derecha en el eje 2 determinadas posiciones
497 286 shift = int(self.processingHeaderObj.profilesPerBlock/2)
498 287 spc = numpy.roll( spc, shift , axis=2 )
499 <<<<<<< HEAD
500
501 if self.processingHeaderObj.flag_cspc:
502 #desplaza a la derecha en el eje 2 determinadas posiciones
503 cspc = numpy.roll( cspc, shift, axis=2 )
504
505 #Dimensions : nChannels, nProfiles, nSamples
506 spc = numpy.transpose( spc, (0,2,1) )
507 self.data_spc = spc
508
509 if self.processingHeaderObj.flag_cspc:
510 =======
511 288
512 289 if self.processingHeaderObj.flag_cspc:
513 290 #desplaza a la derecha en el eje 2 determinadas posiciones
514 291 cspc = numpy.roll( cspc, shift, axis=2 )
515 292
516 293 #Dimensions : nChannels, nProfiles, nSamples
517 294 spc = numpy.transpose( spc, (0,2,1) )
518 295 self.data_spc = spc
519 296
520 297 if self.processingHeaderObj.flag_cspc:
521 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
298
522 299 cspc = numpy.transpose( cspc, (0,2,1) )
523 300 self.data_cspc = cspc['real'] + cspc['imag']*1j
524 301 else:
525 302 self.data_cspc = None
526 <<<<<<< HEAD
527
528 =======
529 303
530 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
304
531 305 if self.processingHeaderObj.flag_dc:
532 306 self.data_dc = dc['real'] + dc['imag']*1j
533 307 else:
534 308 self.data_dc = None
535 309
536 310 self.flagIsNewFile = 0
537 311 self.flagIsNewBlock = 1
538 312
539 313 self.nTotalBlocks += 1
540 314 self.nReadBlocks += 1
541 315
542 316 return 1
543 <<<<<<< HEAD
544
545 def getFirstHeader(self):
546
547 self.getBasicHeader()
548
549 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
550
551 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
552
553 # self.dataOut.ippSeconds = self.ippSeconds
554
555 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock
556
557 self.dataOut.dtype = self.dtype
558
559 # self.dataOut.nPairs = self.nPairs
560
561 self.dataOut.pairsList = self.rdPairList
562
563 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
564
565 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
566
567 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
568
569 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
570
571 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
572
573 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
574
575 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
576
577 self.dataOut.flagShiftFFT = True #Data is always shifted
578
579 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
580
581 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data esta sin flip
582
583 def getData(self):
584 """
585 First method to execute before "RUN" is called.
586
587 Copia el buffer de lectura a la clase "Spectra",
588 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
589 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
590
591 Return:
592 0 : Si no hay mas archivos disponibles
593 1 : Si hizo una buena copia del buffer
594
595 Affected:
596 self.dataOut
597
598 =======
599 317
600 318 def getFirstHeader(self):
601 319
602 320 self.getBasicHeader()
603 321
604 322 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
605 323
606 324 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
607 325
608 326 # self.dataOut.ippSeconds = self.ippSeconds
609 327
610 328 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock
611 329
612 330 self.dataOut.dtype = self.dtype
613 331
614 332 # self.dataOut.nPairs = self.nPairs
615 333
616 334 self.dataOut.pairsList = self.rdPairList
617 335
618 336 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
619 337
620 338 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
621 339
622 340 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
623 341
624 342 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
625 343
626 344 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
627 345
628 346 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
629 347
630 348 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
631 349
632 350 self.dataOut.flagShiftFFT = True #Data is always shifted
633 351
634 352 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
635 353
636 354 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data esta sin flip
637 355
638 356 def getData(self):
639 357 """
640 358 First method to execute before "RUN" is called.
641 359
642 360 Copia el buffer de lectura a la clase "Spectra",
643 361 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
644 362 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
645 363
646 364 Return:
647 365 0 : Si no hay mas archivos disponibles
648 366 1 : Si hizo una buena copia del buffer
649 367
650 368 Affected:
651 369 self.dataOut
652 370
653 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
654 371 self.flagDiscontinuousBlock
655 372 self.flagIsNewBlock
656 373 """
657 374
658 375 if self.flagNoMoreFiles:
659 376 self.dataOut.flagNoData = True
660 377 print 'Process finished'
661 378 return 0
662 <<<<<<< HEAD
663
664 self.flagDiscontinuousBlock = 0
665 self.flagIsNewBlock = 0
666
667 if self.__hasNotDataInBuffer():
668 =======
669 379
670 380 self.flagDiscontinuousBlock = 0
671 381 self.flagIsNewBlock = 0
672 382
673 383 if self.__hasNotDataInBuffer():
674 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
675 384
676 385 if not( self.readNextBlock() ):
677 386 self.dataOut.flagNoData = True
678 387 return 0
679 <<<<<<< HEAD
680
681 =======
682 388
683 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
389
684 390 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
685 391
686 392 if self.data_spc is None:
687 393 self.dataOut.flagNoData = True
688 394 return 0
689 <<<<<<< HEAD
690
691 self.getBasicHeader()
692
693 self.getFirstHeader()
694
695 self.dataOut.data_spc = self.data_spc
696
697 self.dataOut.data_cspc = self.data_cspc
698
699 self.dataOut.data_dc = self.data_dc
700
701 self.dataOut.flagNoData = False
702
703 self.dataOut.realtime = self.online
704
705 return self.dataOut.data_spc
706
707 class SpectraWriter(JRODataWriter, Operation):
708
709 """
710 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
711 de los datos siempre se realiza por bloques.
712 """
713
714 ext = ".pdata"
715
716 optchar = "P"
717
718 shape_spc_Buffer = None
719
720 shape_cspc_Buffer = None
721
722 shape_dc_Buffer = None
723
724 data_spc = None
725
726 data_cspc = None
727
728 data_dc = None
729
730 # dataOut = None
731
732 def __init__(self, **kwargs):
733 """
734 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
735
736 Affected:
737 =======
738 395
739 396 self.getBasicHeader()
740 397
741 398 self.getFirstHeader()
742 399
743 400 self.dataOut.data_spc = self.data_spc
744 401
745 402 self.dataOut.data_cspc = self.data_cspc
746 403
747 404 self.dataOut.data_dc = self.data_dc
748 405
749 406 self.dataOut.flagNoData = False
750 407
751 408 self.dataOut.realtime = self.online
752 409
753 410 return self.dataOut.data_spc
754 411
755 412 class SpectraWriter(JRODataWriter, Operation):
756 413
757 414 """
758 415 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
759 416 de los datos siempre se realiza por bloques.
760 417 """
761 418
762 419 ext = ".pdata"
763 420
764 421 optchar = "P"
765 422
766 423 shape_spc_Buffer = None
767 424
768 425 shape_cspc_Buffer = None
769 426
770 427 shape_dc_Buffer = None
771 428
772 429 data_spc = None
773 430
774 431 data_cspc = None
775 432
776 433 data_dc = None
777 434
778 435 # dataOut = None
779 436
780 437 def __init__(self):
781 438 """
782 439 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
783 440
784 441 Affected:
785 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
442
786 443 self.dataOut
787 444 self.basicHeaderObj
788 445 self.systemHeaderObj
789 446 self.radarControllerHeaderObj
790 447 self.processingHeaderObj
791 448
792 449 Return: None
793 450 """
794 <<<<<<< HEAD
795
796 Operation.__init__(self, **kwargs)
797
798 self.isConfig = False
799
800 self.nTotalBlocks = 0
801
802 self.data_spc = None
803
804 self.data_cspc = None
805
806 =======
807 451
808 452 Operation.__init__(self)
809 453
810 454 self.isConfig = False
811 455
812 456 self.nTotalBlocks = 0
813 457
814 458 self.data_spc = None
815 459
816 460 self.data_cspc = None
817 461
818 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
462
819 463 self.data_dc = None
820 464
821 465 self.fp = None
822 466
823 467 self.flagIsNewFile = 1
824 <<<<<<< HEAD
825
826 self.nTotalBlocks = 0
827
828 self.flagIsNewBlock = 0
829
830 self.setFile = None
831
832 self.dtype = None
833
834 self.path = None
835
836 self.noMoreFiles = 0
837
838 self.filename = None
839
840 self.basicHeaderObj = BasicHeader(LOCALTIME)
841
842 self.systemHeaderObj = SystemHeader()
843
844 self.radarControllerHeaderObj = RadarControllerHeader()
845
846 self.processingHeaderObj = ProcessingHeader()
847
848
849 def hasAllDataInBuffer(self):
850 return 1
851
852
853 =======
854 468
855 469 self.nTotalBlocks = 0
856 470
857 471 self.flagIsNewBlock = 0
858 472
859 473 self.setFile = None
860 474
861 475 self.dtype = None
862 476
863 477 self.path = None
864 478
865 479 self.noMoreFiles = 0
866 480
867 481 self.filename = None
868 482
869 483 self.basicHeaderObj = BasicHeader(LOCALTIME)
870 484
871 485 self.systemHeaderObj = SystemHeader()
872 486
873 487 self.radarControllerHeaderObj = RadarControllerHeader()
874 488
875 489 self.processingHeaderObj = ProcessingHeader()
876 490
877 491
878 492 def hasAllDataInBuffer(self):
879 493 return 1
880 494
881 495
882 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
496
883 497 def setBlockDimension(self):
884 498 """
885 499 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
886 500
887 501 Affected:
888 502 self.shape_spc_Buffer
889 503 self.shape_cspc_Buffer
890 504 self.shape_dc_Buffer
891 505
892 506 Return: None
893 507 """
894 508 self.shape_spc_Buffer = (self.dataOut.nChannels,
895 509 self.processingHeaderObj.nHeights,
896 510 self.processingHeaderObj.profilesPerBlock)
897 511
898 512 self.shape_cspc_Buffer = (self.dataOut.nPairs,
899 513 self.processingHeaderObj.nHeights,
900 514 self.processingHeaderObj.profilesPerBlock)
901 <<<<<<< HEAD
902
903 self.shape_dc_Buffer = (self.dataOut.nChannels,
904 self.processingHeaderObj.nHeights)
905
906
907 def writeBlock(self):
908 """
909 Escribe el buffer en el file designado
910
911 =======
912 515
913 516 self.shape_dc_Buffer = (self.dataOut.nChannels,
914 517 self.processingHeaderObj.nHeights)
915 518
916 519
917 520 def writeBlock(self):
918 521 """
919 522 Escribe el buffer en el file designado
920 523
921 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
524
922 525 Affected:
923 526 self.data_spc
924 527 self.data_cspc
925 528 self.data_dc
926 529 self.flagIsNewFile
927 530 self.flagIsNewBlock
928 531 self.nTotalBlocks
929 <<<<<<< HEAD
930 self.nWriteBlocks
931
932 Return: None
933 """
934
935 =======
936 532 self.nWriteBlocks
937 533
938 534 Return: None
939 535 """
940 536
941 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
942 537 spc = numpy.transpose( self.data_spc, (0,2,1) )
943 538 if not( self.processingHeaderObj.shif_fft ):
944 539 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
945 540 data = spc.reshape((-1))
946 541 data = data.astype(self.dtype[0])
947 542 data.tofile(self.fp)
948 543
949 544 if self.data_cspc is not None:
950 545 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
951 546 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
952 547 if not( self.processingHeaderObj.shif_fft ):
953 548 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
954 549 data['real'] = cspc.real
955 550 data['imag'] = cspc.imag
956 551 data = data.reshape((-1))
957 552 data.tofile(self.fp)
958 <<<<<<< HEAD
959
960 =======
961 553
962 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
554
963 555 if self.data_dc is not None:
964 556 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
965 557 dc = self.data_dc
966 558 data['real'] = dc.real
967 559 data['imag'] = dc.imag
968 560 data = data.reshape((-1))
969 561 data.tofile(self.fp)
970 562
971 563 # self.data_spc.fill(0)
972 <<<<<<< HEAD
973 #
974 # if self.data_dc is not None:
975 # self.data_dc.fill(0)
976 #
977 # if self.data_cspc is not None:
978 # self.data_cspc.fill(0)
979
980 =======
981 564 #
982 565 # if self.data_dc is not None:
983 566 # self.data_dc.fill(0)
984 567 #
985 568 # if self.data_cspc is not None:
986 569 # self.data_cspc.fill(0)
987 570
988 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
571
989 572 self.flagIsNewFile = 0
990 573 self.flagIsNewBlock = 1
991 574 self.nTotalBlocks += 1
992 575 self.nWriteBlocks += 1
993 576 self.blockIndex += 1
994 <<<<<<< HEAD
995
996 # print "[Writing] Block = %d04" %self.blockIndex
997
998 def putData(self):
999 """
1000 Setea un bloque de datos y luego los escribe en un file
1001
1002 =======
1003 577
1004 578 # print "[Writing] Block = %d04" %self.blockIndex
1005 579
1006 580 def putData(self):
1007 581 """
1008 582 Setea un bloque de datos y luego los escribe en un file
1009 583
1010 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
584
1011 585 Affected:
1012 586 self.data_spc
1013 587 self.data_cspc
1014 588 self.data_dc
1015 589
1016 <<<<<<< HEAD
1017 Return:
1018 0 : Si no hay data o no hay mas files que puedan escribirse
1019 1 : Si se escribio la data de un bloque en un file
1020 """
1021
1022 if self.dataOut.flagNoData:
1023 return 0
1024
1025 self.flagIsNewBlock = 0
1026
1027 if self.dataOut.flagDiscontinuousBlock:
1028 self.data_spc.fill(0)
1029 if self.dataOut.data_cspc is not None:
1030 self.data_cspc.fill(0)
1031 if self.dataOut.data_dc is not None:
1032 self.data_dc.fill(0)
1033 self.setNextFile()
1034
1035 if self.flagIsNewFile == 0:
1036 self.setBasicHeader()
1037
1038 self.data_spc = self.dataOut.data_spc.copy()
1039
1040 if self.dataOut.data_cspc is not None:
1041 self.data_cspc = self.dataOut.data_cspc.copy()
1042
1043 if self.dataOut.data_dc is not None:
1044 self.data_dc = self.dataOut.data_dc.copy()
1045
1046 =======
1047 590 Return:
1048 591 0 : Si no hay data o no hay mas files que puedan escribirse
1049 592 1 : Si se escribio la data de un bloque en un file
1050 593 """
1051 594
1052 595 if self.dataOut.flagNoData:
1053 596 return 0
1054 597
1055 598 self.flagIsNewBlock = 0
1056 599
1057 600 if self.dataOut.flagDiscontinuousBlock:
1058 601 self.data_spc.fill(0)
1059 602 self.data_cspc.fill(0)
1060 603 self.data_dc.fill(0)
1061 604 self.setNextFile()
1062 605
1063 606 if self.flagIsNewFile == 0:
1064 607 self.setBasicHeader()
1065 608
1066 609 self.data_spc = self.dataOut.data_spc.copy()
1067 610
1068 611 if self.dataOut.data_cspc is not None:
1069 612 self.data_cspc = self.dataOut.data_cspc.copy()
1070 613
1071 614 if self.dataOut.data_dc is not None:
1072 615 self.data_dc = self.dataOut.data_dc.copy()
1073 616
1074 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
1075 617 # #self.processingHeaderObj.dataBlocksPerFile)
1076 618 if self.hasAllDataInBuffer():
1077 619 # self.setFirstHeader()
1078 620 self.writeNextBlock()
1079 <<<<<<< HEAD
1080
1081 return 1
1082
1083 =======
1084 621
1085 622 return 1
1086 623
1087 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
624
1088 625 def __getBlockSize(self):
1089 626 '''
1090 627 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
1091 628 '''
1092 <<<<<<< HEAD
1093
1094 dtype_width = self.getDtypeWidth()
1095
1096 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
1097
1098 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
1099 blocksize = (pts2write_SelfSpectra*dtype_width)
1100
1101 if self.dataOut.data_cspc is not None:
1102 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
1103 blocksize += (pts2write_CrossSpectra*dtype_width*2)
1104
1105 if self.dataOut.data_dc is not None:
1106 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
1107 blocksize += (pts2write_DCchannels*dtype_width*2)
1108
1109 # blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
1110
1111 return blocksize
1112
1113 def setFirstHeader(self):
1114
1115 """
1116 Obtiene una copia del First Header
1117
1118 =======
1119 629
1120 630 dtype_width = self.getDtypeWidth()
1121 631
1122 632 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
1123 633
1124 634 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
1125 635 blocksize = (pts2write_SelfSpectra*dtype_width)
1126 636
1127 637 if self.dataOut.data_cspc is not None:
1128 638 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
1129 639 blocksize += (pts2write_CrossSpectra*dtype_width*2)
1130 640
1131 641 if self.dataOut.data_dc is not None:
1132 642 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
1133 643 blocksize += (pts2write_DCchannels*dtype_width*2)
1134 644
1135 645 # blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
1136 646
1137 647 return blocksize
1138 648
1139 649 def setFirstHeader(self):
1140 650
1141 651 """
1142 652 Obtiene una copia del First Header
1143 653
1144 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
1145 654 Affected:
1146 655 self.systemHeaderObj
1147 656 self.radarControllerHeaderObj
1148 657 self.dtype
1149 658
1150 <<<<<<< HEAD
1151 Return:
1152 None
1153 """
1154
1155 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
1156 self.systemHeaderObj.nChannels = self.dataOut.nChannels
1157 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
1158
1159 =======
1160 659 Return:
1161 660 None
1162 661 """
1163 662
1164 663 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
1165 664 self.systemHeaderObj.nChannels = self.dataOut.nChannels
1166 665 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
1167 666
1168 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
1169 667 self.processingHeaderObj.dtype = 1 # Spectra
1170 668 self.processingHeaderObj.blockSize = self.__getBlockSize()
1171 669 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
1172 670 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
1173 671 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
1174 672 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
1175 <<<<<<< HEAD
1176 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
1177 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
1178 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
1179
1180 =======
1181 673 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
1182 674 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
1183 675 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
1184 676
1185 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
677
1186 678 if self.processingHeaderObj.totalSpectra > 0:
1187 679 channelList = []
1188 680 for channel in range(self.dataOut.nChannels):
1189 681 channelList.append(channel)
1190 682 channelList.append(channel)
1191 <<<<<<< HEAD
1192
1193 =======
1194 683
1195 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
1196 684 pairsList = []
1197 685 if self.dataOut.nPairs > 0:
1198 686 for pair in self.dataOut.pairsList:
1199 687 pairsList.append(pair[0])
1200 688 pairsList.append(pair[1])
1201 <<<<<<< HEAD
1202
1203 spectraComb = channelList + pairsList
1204 spectraComb = numpy.array(spectraComb, dtype="u1")
1205 self.processingHeaderObj.spectraComb = spectraComb
1206
1207 =======
1208 689
1209 690 spectraComb = channelList + pairsList
1210 691 spectraComb = numpy.array(spectraComb, dtype="u1")
1211 692 self.processingHeaderObj.spectraComb = spectraComb
1212 693
1213 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
1214 694 if self.dataOut.code is not None:
1215 695 self.processingHeaderObj.code = self.dataOut.code
1216 696 self.processingHeaderObj.nCode = self.dataOut.nCode
1217 697 self.processingHeaderObj.nBaud = self.dataOut.nBaud
1218 <<<<<<< HEAD
1219
1220 =======
1221 698
1222 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
1223 699 if self.processingHeaderObj.nWindows != 0:
1224 700 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
1225 701 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
1226 702 self.processingHeaderObj.nHeights = self.dataOut.nHeights
1227 703 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
1228 <<<<<<< HEAD
1229
1230 self.processingHeaderObj.processFlags = self.getProcessFlags()
1231
1232 =======
1233 704
1234 705 self.processingHeaderObj.processFlags = self.getProcessFlags()
1235 706
1236 >>>>>>> 08c4507d6c3c48f6c52326d5dedfa1972fb26356
1237 707 self.setBasicHeader()
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
@@ -1,637 +1,643
1 1 '''
2 2 Created on Jul 2, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6
7 7 import numpy
8 8
9 9 from jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
10 10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
11 11 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
12 12 from schainpy.model.data.jrodata import Voltage
13 13 # from _sha import blocksize
14 14
15 15 class VoltageReader(JRODataReader, ProcessingUnit):
16 16 """
17 17 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
18 18 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
19 19 perfiles*alturas*canales) son almacenados en la variable "buffer".
20 20
21 21 perfiles * alturas * canales
22 22
23 23 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
24 24 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
25 25 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
26 26 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
27 27
28 28 Example:
29 29
30 30 dpath = "/home/myuser/data"
31 31
32 32 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
33 33
34 34 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
35 35
36 36 readerObj = VoltageReader()
37 37
38 38 readerObj.setup(dpath, startTime, endTime)
39 39
40 40 while(True):
41 41
42 42 #to get one profile
43 43 profile = readerObj.getData()
44 44
45 45 #print the profile
46 46 print profile
47 47
48 48 #If you want to see all datablock
49 49 print readerObj.datablock
50 50
51 51 if readerObj.flagNoMoreFiles:
52 52 break
53 53
54 54 """
55 55
56 56 ext = ".r"
57 57
58 58 optchar = "D"
59 59 dataOut = None
60 60
61 61 def __init__(self, **kwargs):
62 62 """
63 63 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
64 64
65 65 Input:
66 66 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
67 67 almacenar un perfil de datos cada vez que se haga un requerimiento
68 68 (getData). El perfil sera obtenido a partir del buffer de datos,
69 69 si el buffer esta vacio se hara un nuevo proceso de lectura de un
70 70 bloque de datos.
71 71 Si este parametro no es pasado se creara uno internamente.
72 72
73 73 Variables afectadas:
74 74 self.dataOut
75 75
76 76 Return:
77 77 None
78 78 """
79 79
80 80 ProcessingUnit.__init__(self, **kwargs)
81 81
82 82 self.isConfig = False
83 83
84 84 self.datablock = None
85 85
86 86 self.utc = 0
87 87
88 88 self.ext = ".r"
89 89
90 90 self.optchar = "D"
91 91
92 92 self.basicHeaderObj = BasicHeader(LOCALTIME)
93 93
94 94 self.systemHeaderObj = SystemHeader()
95 95
96 96 self.radarControllerHeaderObj = RadarControllerHeader()
97 97
98 98 self.processingHeaderObj = ProcessingHeader()
99 99
100 100 self.online = 0
101 101
102 102 self.fp = None
103 103
104 104 self.idFile = None
105 105
106 106 self.dtype = None
107 107
108 108 self.fileSizeByHeader = None
109 109
110 110 self.filenameList = []
111 111
112 112 self.filename = None
113 113
114 114 self.fileSize = None
115 115
116 116 self.firstHeaderSize = 0
117 117
118 118 self.basicHeaderSize = 24
119 119
120 120 self.pathList = []
121 121
122 122 self.filenameList = []
123 123
124 124 self.lastUTTime = 0
125 125
126 126 self.maxTimeStep = 30
127 127
128 128 self.flagNoMoreFiles = 0
129 129
130 130 self.set = 0
131 131
132 132 self.path = None
133 133
134 134 self.profileIndex = 2**32-1
135 135
136 136 self.delay = 3 #seconds
137 137
138 138 self.nTries = 3 #quantity tries
139 139
140 140 self.nFiles = 3 #number of files for searching
141 141
142 142 self.nReadBlocks = 0
143 143
144 144 self.flagIsNewFile = 1
145 145
146 146 self.__isFirstTimeOnline = 1
147 147
148 148 # self.ippSeconds = 0
149 149
150 150 self.flagDiscontinuousBlock = 0
151 151
152 152 self.flagIsNewBlock = 0
153 153
154 154 self.nTotalBlocks = 0
155 155
156 156 self.blocksize = 0
157 157
158 158 self.dataOut = self.createObjByDefault()
159 159
160 160 self.nTxs = 1
161 161
162 162 self.txIndex = 0
163 163
164 164 def createObjByDefault(self):
165 165
166 166 dataObj = Voltage()
167 167
168 168 return dataObj
169 169
170 170 def __hasNotDataInBuffer(self):
171 171
172 172 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock*self.nTxs:
173 173 return 1
174 174
175 175 return 0
176 176
177 177
178 178 def getBlockDimension(self):
179 179 """
180 180 Obtiene la cantidad de puntos a leer por cada bloque de datos
181 181
182 182 Affected:
183 183 self.blocksize
184 184
185 185 Return:
186 186 None
187 187 """
188 188 pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
189 189 self.blocksize = pts2read
190 190
191 191
192 192 def readBlock(self):
193 193 """
194 194 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
195 195 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
196 196 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
197 197 es seteado a 0
198 198
199 199 Inputs:
200 200 None
201 201
202 202 Return:
203 203 None
204 204
205 205 Affected:
206 206 self.profileIndex
207 207 self.datablock
208 208 self.flagIsNewFile
209 209 self.flagIsNewBlock
210 210 self.nTotalBlocks
211 211
212 212 Exceptions:
213 213 Si un bloque leido no es un bloque valido
214 214 """
215 215 current_pointer_location = self.fp.tell()
216 216 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
217 217
218 218 try:
219 219 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
220 220 except:
221 221 #print "The read block (%3d) has not enough data" %self.nReadBlocks
222 222
223 223 if self.waitDataBlock(pointer_location=current_pointer_location):
224 224 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
225 225 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
226 226 # return 0
227 227
228 228 #Dimensions : nChannels, nProfiles, nSamples
229 229
230 230 junk = numpy.transpose(junk, (2,0,1))
231 231 self.datablock = junk['real'] + junk['imag']*1j
232 232
233 233 self.profileIndex = 0
234 234
235 235 self.flagIsNewFile = 0
236 236 self.flagIsNewBlock = 1
237 237
238 238 self.nTotalBlocks += 1
239 239 self.nReadBlocks += 1
240 240
241 241 return 1
242 242
243 243 def getFirstHeader(self):
244 244
245 245 self.getBasicHeader()
246 246
247 247 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
248 248
249 249 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
250 250
251 251 if self.nTxs > 1:
252 252 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
253 253
254 254 #Time interval and code are propierties of dataOut. Its value depends of radarControllerHeaderObj.
255 255
256 256 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt
257 257 #
258 258 # if self.radarControllerHeaderObj.code is not None:
259 259 #
260 260 # self.dataOut.nCode = self.radarControllerHeaderObj.nCode
261 261 #
262 262 # self.dataOut.nBaud = self.radarControllerHeaderObj.nBaud
263 263 #
264 264 # self.dataOut.code = self.radarControllerHeaderObj.code
265 265
266 266 self.dataOut.dtype = self.dtype
267 267
268 268 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
269 269
270 270 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights) *self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
271 271
272 272 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
273 273
274 274 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
275 275
276 276 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
277 277
278 278 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data no esta sin flip
279 279
280 280 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
281 281
282 282 def reshapeData(self):
283 283
284 284 if self.nTxs < 0:
285 285 return
286 286
287 287 if self.nTxs == 1:
288 288 return
289 289
290 290 if self.nTxs < 1 and self.processingHeaderObj.profilesPerBlock % (1./self.nTxs) != 0:
291 291 raise ValueError, "1./nTxs (=%f), should be a multiple of nProfiles (=%d)" %(1./self.nTxs, self.processingHeaderObj.profilesPerBlock)
292 292
293 293 if self.nTxs > 1 and self.processingHeaderObj.nHeights % self.nTxs != 0:
294 294 raise ValueError, "nTxs (=%d), should be a multiple of nHeights (=%d)" %(self.nTxs, self.processingHeaderObj.nHeights)
295 295
296 296 self.datablock = self.datablock.reshape((self.systemHeaderObj.nChannels, self.processingHeaderObj.profilesPerBlock*self.nTxs, self.processingHeaderObj.nHeights/self.nTxs))
297 297
298 298 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
299 299 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights/self.nTxs) *self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
300 300 self.dataOut.radarControllerHeaderObj.ippSeconds = self.radarControllerHeaderObj.ippSeconds/self.nTxs
301 301
302 302 return
303 303
304 304 def getData(self):
305 305 """
306 306 getData obtiene una unidad de datos del buffer de lectura, un perfil, y la copia al objeto self.dataOut
307 307 del tipo "Voltage" con todos los parametros asociados a este (metadata). cuando no hay datos
308 308 en el buffer de lectura es necesario hacer una nueva lectura de los bloques de datos usando
309 309 "readNextBlock"
310 310
311 311 Ademas incrementa el contador del buffer "self.profileIndex" en 1.
312 312
313 313 Return:
314 314
315 315 Si el flag self.getByBlock ha sido seteado el bloque completo es copiado a self.dataOut y el self.profileIndex
316 316 es igual al total de perfiles leidos desde el archivo.
317 317
318 318 Si self.getByBlock == False:
319 319
320 320 self.dataOut.data = buffer[:, thisProfile, :]
321 321
322 322 shape = [nChannels, nHeis]
323 323
324 324 Si self.getByBlock == True:
325 325
326 326 self.dataOut.data = buffer[:, :, :]
327 327
328 328 shape = [nChannels, nProfiles, nHeis]
329 329
330 330 Variables afectadas:
331 331 self.dataOut
332 332 self.profileIndex
333 333
334 334 Affected:
335 335 self.dataOut
336 336 self.profileIndex
337 337 self.flagDiscontinuousBlock
338 338 self.flagIsNewBlock
339 339 """
340 340
341 341 if self.flagNoMoreFiles:
342 342 self.dataOut.flagNoData = True
343 343 print 'Process finished'
344 344 return 0
345 345
346 346 self.flagDiscontinuousBlock = 0
347 347 self.flagIsNewBlock = 0
348 348
349 349 if self.__hasNotDataInBuffer():
350 350
351 351 if not( self.readNextBlock() ):
352 352 return 0
353 353
354 354 self.getFirstHeader()
355 355
356 356 self.reshapeData()
357 357
358 358 if self.datablock is None:
359 359 self.dataOut.flagNoData = True
360 360 return 0
361 361
362 362 if not self.getByBlock:
363 363
364 364 """
365 365 Return profile by profile
366 366
367 367 If nTxs > 1 then one profile is divided by nTxs and number of total
368 368 blocks is increased by nTxs (nProfiles *= nTxs)
369 369 """
370 370 self.dataOut.flagDataAsBlock = False
371 371 self.dataOut.data = self.datablock[:,self.profileIndex,:]
372 372 self.dataOut.profileIndex = self.profileIndex
373 373
374 374 self.profileIndex += 1
375 375
376 376 # elif self.selBlocksize==None or self.selBlocksize==self.dataOut.nProfiles:
377 377 # """
378 378 # Return all block
379 379 # """
380 380 # self.dataOut.flagDataAsBlock = True
381 381 # self.dataOut.data = self.datablock
382 382 # self.dataOut.profileIndex = self.dataOut.nProfiles - 1
383 383 #
384 384 # self.profileIndex = self.dataOut.nProfiles
385 385
386 386 else:
387 387 """
388 388 Return a block
389 389 """
390 390 if self.selBlocksize == None: self.selBlocksize = self.dataOut.nProfiles
391 391 if self.selBlocktime != None:
392 392 if self.dataOut.nCohInt is not None:
393 393 nCohInt = self.dataOut.nCohInt
394 394 else:
395 395 nCohInt = 1
396 396 self.selBlocksize = int(self.dataOut.nProfiles*round(self.selBlocktime/(nCohInt*self.dataOut.ippSeconds*self.dataOut.nProfiles)))
397 397
398 398 self.dataOut.data = self.datablock[:,self.profileIndex:self.profileIndex+self.selBlocksize,:]
399 399 self.profileIndex += self.selBlocksize
400 400 datasize = self.dataOut.data.shape[1]
401 401
402 402 if datasize < self.selBlocksize:
403 403 buffer = numpy.zeros((self.dataOut.data.shape[0],self.selBlocksize,self.dataOut.data.shape[2]), dtype = 'complex')
404 404 buffer[:,:datasize,:] = self.dataOut.data
405 405
406 406 while datasize < self.selBlocksize: #Not enough profiles to fill the block
407 407 if not( self.readNextBlock() ):
408 408 return 0
409 409 self.getFirstHeader()
410 410 self.reshapeData()
411 411 if self.datablock is None:
412 412 self.dataOut.flagNoData = True
413 413 return 0
414 414 #stack data
415 415 blockIndex = self.selBlocksize - datasize
416 416 datablock1 = self.datablock[:,:blockIndex,:]
417 417
418 418 buffer[:,datasize:datasize+datablock1.shape[1],:] = datablock1
419 419 datasize += datablock1.shape[1]
420 420
421 421 self.dataOut.data = buffer
422 422 self.profileIndex = blockIndex
423 423
424 424 self.dataOut.flagDataAsBlock = True
425 425 self.dataOut.nProfiles = self.dataOut.data.shape[1]
426 426
427 427 self.dataOut.flagNoData = False
428 428
429 429 self.getBasicHeader()
430
430
431 #print self.basicHeaderObj.printInfo()
432 #print self.systemHeaderObj.printInfo()
433 #print self.radarControllerHeaderObj.printInfo()
434 #print self.processingHeaderObj.printInfo()
435
431 436 self.dataOut.realtime = self.online
432 437
433 438 return self.dataOut.data
439
434 440
435 441 class VoltageWriter(JRODataWriter, Operation):
436 442 """
437 443 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
438 444 de los datos siempre se realiza por bloques.
439 445 """
440 446
441 447 ext = ".r"
442 448
443 449 optchar = "D"
444 450
445 451 shapeBuffer = None
446 452
447 453
448 454 def __init__(self, **kwargs):
449 455 """
450 456 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
451 457
452 458 Affected:
453 459 self.dataOut
454 460
455 461 Return: None
456 462 """
457 463 Operation.__init__(self, **kwargs)
458 464
459 465 self.nTotalBlocks = 0
460 466
461 467 self.profileIndex = 0
462 468
463 469 self.isConfig = False
464 470
465 471 self.fp = None
466 472
467 473 self.flagIsNewFile = 1
468 474
469 475 self.blockIndex = 0
470 476
471 477 self.flagIsNewBlock = 0
472 478
473 479 self.setFile = None
474 480
475 481 self.dtype = None
476 482
477 483 self.path = None
478 484
479 485 self.filename = None
480 486
481 487 self.basicHeaderObj = BasicHeader(LOCALTIME)
482 488
483 489 self.systemHeaderObj = SystemHeader()
484 490
485 491 self.radarControllerHeaderObj = RadarControllerHeader()
486 492
487 493 self.processingHeaderObj = ProcessingHeader()
488 494
489 495 def hasAllDataInBuffer(self):
490 496 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
491 497 return 1
492 498 return 0
493 499
494 500
495 501 def setBlockDimension(self):
496 502 """
497 503 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
498 504
499 505 Affected:
500 506 self.shape_spc_Buffer
501 507 self.shape_cspc_Buffer
502 508 self.shape_dc_Buffer
503 509
504 510 Return: None
505 511 """
506 512 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
507 513 self.processingHeaderObj.nHeights,
508 514 self.systemHeaderObj.nChannels)
509 515
510 516 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
511 517 self.processingHeaderObj.profilesPerBlock,
512 518 self.processingHeaderObj.nHeights),
513 519 dtype=numpy.dtype('complex64'))
514 520
515 521 def writeBlock(self):
516 522 """
517 523 Escribe el buffer en el file designado
518 524
519 525 Affected:
520 526 self.profileIndex
521 527 self.flagIsNewFile
522 528 self.flagIsNewBlock
523 529 self.nTotalBlocks
524 530 self.blockIndex
525 531
526 532 Return: None
527 533 """
528 534 data = numpy.zeros( self.shapeBuffer, self.dtype )
529 535
530 536 junk = numpy.transpose(self.datablock, (1,2,0))
531 537
532 538 data['real'] = junk.real
533 539 data['imag'] = junk.imag
534 540
535 541 data = data.reshape( (-1) )
536 542
537 543 data.tofile( self.fp )
538 544
539 545 self.datablock.fill(0)
540 546
541 547 self.profileIndex = 0
542 548 self.flagIsNewFile = 0
543 549 self.flagIsNewBlock = 1
544 550
545 551 self.blockIndex += 1
546 552 self.nTotalBlocks += 1
547 553
548 554 # print "[Writing] Block = %04d" %self.blockIndex
549 555
550 556 def putData(self):
551 557 """
552 558 Setea un bloque de datos y luego los escribe en un file
553 559
554 560 Affected:
555 561 self.flagIsNewBlock
556 562 self.profileIndex
557 563
558 564 Return:
559 565 0 : Si no hay data o no hay mas files que puedan escribirse
560 566 1 : Si se escribio la data de un bloque en un file
561 567 """
562 568 if self.dataOut.flagNoData:
563 569 return 0
564 570
565 571 self.flagIsNewBlock = 0
566 572
567 573 if self.dataOut.flagDiscontinuousBlock:
568 574 self.datablock.fill(0)
569 575 self.profileIndex = 0
570 576 self.setNextFile()
571 577
572 578 if self.profileIndex == 0:
573 579 self.setBasicHeader()
574 580
575 581 self.datablock[:,self.profileIndex,:] = self.dataOut.data
576 582
577 583 self.profileIndex += 1
578 584
579 585 if self.hasAllDataInBuffer():
580 586 #if self.flagIsNewFile:
581 587 self.writeNextBlock()
582 588 # self.setFirstHeader()
583 589
584 590 return 1
585 591
586 592 def __getBlockSize(self):
587 593 '''
588 594 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
589 595 '''
590 596
591 597 dtype_width = self.getDtypeWidth()
592 598
593 599 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.profilesPerBlock * dtype_width * 2)
594 600
595 601 return blocksize
596 602
597 603 def setFirstHeader(self):
598 604
599 605 """
600 606 Obtiene una copia del First Header
601 607
602 608 Affected:
603 609 self.systemHeaderObj
604 610 self.radarControllerHeaderObj
605 611 self.dtype
606 612
607 613 Return:
608 614 None
609 615 """
610 616
611 617 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
612 618 self.systemHeaderObj.nChannels = self.dataOut.nChannels
613 619 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
614 620
615 621 self.processingHeaderObj.dtype = 0 # Voltage
616 622 self.processingHeaderObj.blockSize = self.__getBlockSize()
617 623 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
618 624 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
619 625 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
620 626 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
621 627 self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage
622 628 self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage
623 629
624 630 if self.dataOut.code is not None:
625 631 self.processingHeaderObj.code = self.dataOut.code
626 632 self.processingHeaderObj.nCode = self.dataOut.nCode
627 633 self.processingHeaderObj.nBaud = self.dataOut.nBaud
628 634
629 635 if self.processingHeaderObj.nWindows != 0:
630 636 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
631 637 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
632 638 self.processingHeaderObj.nHeights = self.dataOut.nHeights
633 639 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
634 640
635 641 self.processingHeaderObj.processFlags = self.getProcessFlags()
636 642
637 643 self.setBasicHeader()
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file, binary diff hidden
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now