##// END OF EJS Templates
Merge with Claire_proc
Juan C. Espinoza -
r1188:b1c63639a885 merge
parent child
Show More

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

@@ -1,116 +1,114
1 1 # Byte-compiled / optimized / DLL files
2 2 __pycache__/
3 3 *.py[cod]
4 4 *$py.class
5 5
6 6 # C extensions
7 7 *.so
8 8
9 9 # Distribution / packaging
10 10 .Python
11 11 env/
12 12 build/
13 13 develop-eggs/
14 14 dist/
15 15 downloads/
16 16 eggs/
17 17 .eggs/
18 18 lib/
19 19 lib64/
20 20 parts/
21 21 sdist/
22 22 var/
23 23 wheels/
24 24 *.egg-info/
25 25 .installed.cfg
26 26 *.egg
27 27
28 28 # PyInstaller
29 29 # Usually these files are written by a python script from a template
30 30 # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 31 *.manifest
32 32 *.spec
33 33
34 34 # Installer logs
35 35 pip-log.txt
36 36 pip-delete-this-directory.txt
37 37
38 38 # Unit test / coverage reports
39 39 htmlcov/
40 40 .tox/
41 41 .coverage
42 42 .coverage.*
43 43 .cache
44 44 nosetests.xml
45 45 coverage.xml
46 46 *,cover
47 47 .hypothesis/
48 48
49 49 # Translations
50 50 *.mo
51 51 *.pot
52 52
53 53 # Django stuff:
54 54 *.log
55 55 local_settings.py
56 56
57 57 # Flask stuff:
58 58 instance/
59 59 .webassets-cache
60 60
61 61 # Scrapy stuff:
62 62 .scrapy
63 63
64 64 # Sphinx documentation
65 65 docs/_build/
66 66
67 67 # PyBuilder
68 68 target/
69 69
70 70 # Jupyter Notebook
71 71 .ipynb_checkpoints
72 72
73 73 # pyenv
74 74 .python-version
75 75
76 76 # celery beat schedule file
77 77 celerybeat-schedule
78 78
79 79 # SageMath parsed files
80 80 *.sage.py
81 81
82 82 # dotenv
83 83 .env
84 84
85 85 # virtualenv
86 86 .venv
87 87 venv/
88 88 ENV/
89 89
90 90 # Spyder project settings
91 91 .spyderproject
92 92 .spyproject
93 93
94 94 # Rope project settings
95 95 .ropeproject
96 96
97 97 # mkdocs documentation
98 98 /site
99 99
100 100 # eclipse
101 101 .project
102 102 .pydevproject
103 103 # vscode
104 104
105 105 .vscode
106 106
107 107 schaingui/node_modules/
108 108 schainpy/scripts/
109 109 .svn/
110 110 *.png
111 111 *.pyc
112 112 .vscode
113 113 trash
114 114 *.log
115 schainpy/scripts/testDigitalRF.py
116 schainpy/scripts/testDigitalRFWriter.py
@@ -1,1342 +1,1345
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 import json
11 11
12 12 from schainpy.utils import log
13 13 from .jroheaderIO import SystemHeader, RadarControllerHeader
14 14
15 15
16 16 def getNumpyDtype(dataTypeCode):
17 17
18 18 if dataTypeCode == 0:
19 19 numpyDtype = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
20 20 elif dataTypeCode == 1:
21 21 numpyDtype = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
22 22 elif dataTypeCode == 2:
23 23 numpyDtype = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
24 24 elif dataTypeCode == 3:
25 25 numpyDtype = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
26 26 elif dataTypeCode == 4:
27 27 numpyDtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
28 28 elif dataTypeCode == 5:
29 29 numpyDtype = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
30 30 else:
31 31 raise ValueError('dataTypeCode was not defined')
32 32
33 33 return numpyDtype
34 34
35 35
36 36 def getDataTypeCode(numpyDtype):
37 37
38 38 if numpyDtype == numpy.dtype([('real', '<i1'), ('imag', '<i1')]):
39 39 datatype = 0
40 40 elif numpyDtype == numpy.dtype([('real', '<i2'), ('imag', '<i2')]):
41 41 datatype = 1
42 42 elif numpyDtype == numpy.dtype([('real', '<i4'), ('imag', '<i4')]):
43 43 datatype = 2
44 44 elif numpyDtype == numpy.dtype([('real', '<i8'), ('imag', '<i8')]):
45 45 datatype = 3
46 46 elif numpyDtype == numpy.dtype([('real', '<f4'), ('imag', '<f4')]):
47 47 datatype = 4
48 48 elif numpyDtype == numpy.dtype([('real', '<f8'), ('imag', '<f8')]):
49 49 datatype = 5
50 50 else:
51 51 datatype = None
52 52
53 53 return datatype
54 54
55 55
56 56 def hildebrand_sekhon(data, navg):
57 57 """
58 58 This method is for the objective determination of the noise level in Doppler spectra. This
59 59 implementation technique is based on the fact that the standard deviation of the spectral
60 60 densities is equal to the mean spectral density for white Gaussian noise
61 61
62 62 Inputs:
63 63 Data : heights
64 64 navg : numbers of averages
65 65
66 66 Return:
67 67 mean : noise's level
68 68 """
69 69
70 70 sortdata = numpy.sort(data, axis=None)
71 71 lenOfData = len(sortdata)
72 72 nums_min = lenOfData*0.2
73 73
74 74 if nums_min <= 5:
75 75
76 76 nums_min = 5
77 77
78 78 sump = 0.
79 79 sumq = 0.
80 80
81 81 j = 0
82 82 cont = 1
83 83
84 84 while((cont == 1)and(j < lenOfData)):
85 85
86 86 sump += sortdata[j]
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
104 104 class Beam:
105 105
106 106 def __init__(self):
107 107 self.codeList = []
108 108 self.azimuthList = []
109 109 self.zenithList = []
110 110
111 111
112 112 class GenericData(object):
113 113
114 114 flagNoData = True
115 115
116 116 def copy(self, inputObj=None):
117 117
118 118 if inputObj == None:
119 119 return copy.deepcopy(self)
120 120
121 121 for key in list(inputObj.__dict__.keys()):
122 122
123 123 attribute = inputObj.__dict__[key]
124 124
125 125 # If this attribute is a tuple or list
126 126 if type(inputObj.__dict__[key]) in (tuple, list):
127 127 self.__dict__[key] = attribute[:]
128 128 continue
129 129
130 130 # If this attribute is another object or instance
131 131 if hasattr(attribute, '__dict__'):
132 132 self.__dict__[key] = attribute.copy()
133 133 continue
134 134
135 135 self.__dict__[key] = inputObj.__dict__[key]
136 136
137 137 def deepcopy(self):
138 138
139 139 return copy.deepcopy(self)
140 140
141 141 def isEmpty(self):
142 142
143 143 return self.flagNoData
144 144
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 radarControllerHeaderObj = RadarControllerHeader()
153 153 # data = None
154 154 type = None
155 155 datatype = None # dtype but in string
156 156 # dtype = None
157 157 # nChannels = None
158 158 # nHeights = None
159 159 nProfiles = None
160 160 heightList = None
161 161 channelList = None
162 162 flagDiscontinuousBlock = False
163 163 useLocalTime = False
164 164 utctime = None
165 165 timeZone = None
166 166 dstFlag = None
167 167 errorCount = None
168 168 blocksize = None
169 169 # nCode = None
170 170 # nBaud = None
171 171 # code = None
172 172 flagDecodeData = False # asumo q la data no esta decodificada
173 173 flagDeflipData = False # asumo q la data no esta sin flip
174 174 flagShiftFFT = False
175 175 # ippSeconds = None
176 176 # timeInterval = None
177 177 nCohInt = None
178 178 # noise = None
179 179 windowOfFilter = 1
180 180 # Speed of ligth
181 181 C = 3e8
182 182 frequency = 49.92e6
183 183 realtime = False
184 184 beacon_heiIndexList = None
185 185 last_block = None
186 186 blocknow = None
187 187 azimuth = None
188 188 zenith = None
189 189 beam = Beam()
190 190 profileIndex = None
191 191 error = None
192 192 data = None
193 data_plt = None
194
193 nmodes = None
195 194
196 195 def __str__(self):
197 196
198 197 return '{} - {}'.format(self.type, self.getDatatime())
199 198
200 199 def getNoise(self):
201 200
202 201 raise NotImplementedError
203 202
204 203 def getNChannels(self):
205 204
206 205 return len(self.channelList)
207 206
208 207 def getChannelIndexList(self):
209 208
210 209 return list(range(self.nChannels))
211 210
212 211 def getNHeights(self):
213 212
214 213 return len(self.heightList)
215 214
216 215 def getHeiRange(self, extrapoints=0):
217 216
218 217 heis = self.heightList
219 218 # deltah = self.heightList[1] - self.heightList[0]
220 219 #
221 220 # heis.append(self.heightList[-1])
222 221
223 222 return heis
224 223
225 224 def getDeltaH(self):
226 225
227 226 delta = self.heightList[1] - self.heightList[0]
228 227
229 228 return delta
230 229
231 230 def getltctime(self):
232 231
233 232 if self.useLocalTime:
234 233 return self.utctime - self.timeZone * 60
235 234
236 235 return self.utctime
237 236
238 237 def getDatatime(self):
239 238
240 239 datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime)
241 240 return datatimeValue
242 241
243 242 def getTimeRange(self):
244 243
245 244 datatime = []
246 245
247 246 datatime.append(self.ltctime)
248 247 datatime.append(self.ltctime + self.timeInterval + 1)
249 248
250 249 datatime = numpy.array(datatime)
251 250
252 251 return datatime
253 252
254 253 def getFmaxTimeResponse(self):
255 254
256 255 period = (10**-6) * self.getDeltaH() / (0.15)
257 256
258 257 PRF = 1. / (period * self.nCohInt)
259 258
260 259 fmax = PRF
261 260
262 261 return fmax
263 262
264 263 def getFmax(self):
265 264 PRF = 1. / (self.ippSeconds * self.nCohInt)
266 265
267 266 fmax = PRF
268 267 return fmax
269 268
270 269 def getVmax(self):
271 270
272 271 _lambda = self.C / self.frequency
273 272
274 273 vmax = self.getFmax() * _lambda / 2
275 274
276 275 return vmax
277 276
278 277 def get_ippSeconds(self):
279 278 '''
280 279 '''
281 280 return self.radarControllerHeaderObj.ippSeconds
282 281
283 282 def set_ippSeconds(self, ippSeconds):
284 283 '''
285 284 '''
286 285
287 286 self.radarControllerHeaderObj.ippSeconds = ippSeconds
288 287
289 288 return
290 289
291 290 def get_dtype(self):
292 291 '''
293 292 '''
294 293 return getNumpyDtype(self.datatype)
295 294
296 295 def set_dtype(self, numpyDtype):
297 296 '''
298 297 '''
299 298
300 299 self.datatype = getDataTypeCode(numpyDtype)
301 300
302 301 def get_code(self):
303 302 '''
304 303 '''
305 304 return self.radarControllerHeaderObj.code
306 305
307 306 def set_code(self, code):
308 307 '''
309 308 '''
310 309 self.radarControllerHeaderObj.code = code
311 310
312 311 return
313 312
314 313 def get_ncode(self):
315 314 '''
316 315 '''
317 316 return self.radarControllerHeaderObj.nCode
318 317
319 318 def set_ncode(self, nCode):
320 319 '''
321 320 '''
322 321 self.radarControllerHeaderObj.nCode = nCode
323 322
324 323 return
325 324
326 325 def get_nbaud(self):
327 326 '''
328 327 '''
329 328 return self.radarControllerHeaderObj.nBaud
330 329
331 330 def set_nbaud(self, nBaud):
332 331 '''
333 332 '''
334 333 self.radarControllerHeaderObj.nBaud = nBaud
335 334
336 335 return
337 336
338 337 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
339 338 channelIndexList = property(
340 339 getChannelIndexList, "I'm the 'channelIndexList' property.")
341 340 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
342 341 #noise = property(getNoise, "I'm the 'nHeights' property.")
343 342 datatime = property(getDatatime, "I'm the 'datatime' property")
344 343 ltctime = property(getltctime, "I'm the 'ltctime' property")
345 344 ippSeconds = property(get_ippSeconds, set_ippSeconds)
346 345 dtype = property(get_dtype, set_dtype)
347 346 # timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
348 347 code = property(get_code, set_code)
349 348 nCode = property(get_ncode, set_ncode)
350 349 nBaud = property(get_nbaud, set_nbaud)
351 350
352 351
353 352 class Voltage(JROData):
354 353
355 354 # data es un numpy array de 2 dmensiones (canales, alturas)
356 355 data = None
357 356
358 357 def __init__(self):
359 358 '''
360 359 Constructor
361 360 '''
362 361
363 362 self.useLocalTime = True
364 363 self.radarControllerHeaderObj = RadarControllerHeader()
365 364 self.systemHeaderObj = SystemHeader()
366 365 self.type = "Voltage"
367 366 self.data = None
368 367 # self.dtype = None
369 368 # self.nChannels = 0
370 369 # self.nHeights = 0
371 370 self.nProfiles = None
372 371 self.heightList = Non
373 372 self.channelList = None
374 373 # self.channelIndexList = None
375 374 self.flagNoData = True
376 375 self.flagDiscontinuousBlock = False
377 376 self.utctime = None
378 377 self.timeZone = None
379 378 self.dstFlag = None
380 379 self.errorCount = None
381 380 self.nCohInt = None
382 381 self.blocksize = None
383 382 self.flagDecodeData = False # asumo q la data no esta decodificada
384 383 self.flagDeflipData = False # asumo q la data no esta sin flip
385 384 self.flagShiftFFT = False
386 385 self.flagDataAsBlock = False # Asumo que la data es leida perfil a perfil
387 386 self.profileIndex = 0
388 387
389 388 def getNoisebyHildebrand(self, channel=None):
390 389 """
391 390 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
392 391
393 392 Return:
394 393 noiselevel
395 394 """
396 395
397 396 if channel != None:
398 397 data = self.data[channel]
399 398 nChannels = 1
400 399 else:
401 400 data = self.data
402 401 nChannels = self.nChannels
403 402
404 403 noise = numpy.zeros(nChannels)
405 404 power = data * numpy.conjugate(data)
406 405
407 406 for thisChannel in range(nChannels):
408 407 if nChannels == 1:
409 408 daux = power[:].real
410 409 else:
411 410 daux = power[thisChannel, :].real
412 411 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
413 412
414 413 return noise
415 414
416 415 def getNoise(self, type=1, channel=None):
417 416
418 417 if type == 1:
419 418 noise = self.getNoisebyHildebrand(channel)
420 419
421 420 return noise
422 421
423 422 def getPower(self, channel=None):
424 423
425 424 if channel != None:
426 425 data = self.data[channel]
427 426 else:
428 427 data = self.data
429 428
430 429 power = data * numpy.conjugate(data)
431 430 powerdB = 10 * numpy.log10(power.real)
432 431 powerdB = numpy.squeeze(powerdB)
433 432
434 433 return powerdB
435 434
436 435 def getTimeInterval(self):
437 436
438 437 timeInterval = self.ippSeconds * self.nCohInt
439 438
440 439 return timeInterval
441 440
442 441 noise = property(getNoise, "I'm the 'nHeights' property.")
443 442 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
444 443
445 444
446 445 class Spectra(JROData):
447 446
448 447 # data spc es un numpy array de 2 dmensiones (canales, perfiles, alturas)
449 448 data_spc = None
450 449 # data cspc es un numpy array de 2 dmensiones (canales, pares, alturas)
451 450 data_cspc = None
452 451 # data dc es un numpy array de 2 dmensiones (canales, alturas)
453 452 data_dc = None
454 453 # data power
455 454 data_pwr = None
456 455 nFFTPoints = None
457 456 # nPairs = None
458 457 pairsList = None
459 458 nIncohInt = None
460 459 wavelength = None # Necesario para cacular el rango de velocidad desde la frecuencia
461 460 nCohInt = None # se requiere para determinar el valor de timeInterval
462 461 ippFactor = None
463 462 profileIndex = 0
464 463 plotting = "spectra"
464
465 465 def __init__(self):
466 466 '''
467 467 Constructor
468 468 '''
469 469
470 470 self.useLocalTime = True
471 471 self.radarControllerHeaderObj = RadarControllerHeader()
472 472 self.systemHeaderObj = SystemHeader()
473 473 self.type = "Spectra"
474 474 # self.data = None
475 475 # self.dtype = None
476 476 # self.nChannels = 0
477 477 # self.nHeights = 0
478 478 self.nProfiles = None
479 479 self.heightList = None
480 480 self.channelList = None
481 481 # self.channelIndexList = None
482 482 self.pairsList = None
483 483 self.flagNoData = True
484 484 self.flagDiscontinuousBlock = False
485 485 self.utctime = None
486 486 self.nCohInt = None
487 487 self.nIncohInt = None
488 488 self.blocksize = None
489 489 self.nFFTPoints = None
490 490 self.wavelength = None
491 491 self.flagDecodeData = False # asumo q la data no esta decodificada
492 492 self.flagDeflipData = False # asumo q la data no esta sin flip
493 493 self.flagShiftFFT = False
494 494 self.ippFactor = 1
495 495 #self.noise = None
496 496 self.beacon_heiIndexList = []
497 497 self.noise_estimation = None
498 498
499 499 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
500 500 """
501 501 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
502 502
503 503 Return:
504 504 noiselevel
505 505 """
506 506
507 507 noise = numpy.zeros(self.nChannels)
508 508
509 509 for channel in range(self.nChannels):
510 510 daux = self.data_spc[channel,
511 511 xmin_index:xmax_index, ymin_index:ymax_index]
512 512 noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
513 513
514 514 return noise
515 515
516 516 def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
517 517
518 518 if self.noise_estimation is not None:
519 519 # this was estimated by getNoise Operation defined in jroproc_spectra.py
520 520 return self.noise_estimation
521 521 else:
522 522 noise = self.getNoisebyHildebrand(
523 523 xmin_index, xmax_index, ymin_index, ymax_index)
524 524 return noise
525 525
526 526 def getFreqRangeTimeResponse(self, extrapoints=0):
527 527
528 528 deltafreq = self.getFmaxTimeResponse() / (self.nFFTPoints * self.ippFactor)
529 529 freqrange = deltafreq * \
530 530 (numpy.arange(self.nFFTPoints + extrapoints) -
531 531 self.nFFTPoints / 2.) - deltafreq / 2
532 532
533 533 return freqrange
534 534
535 535 def getAcfRange(self, extrapoints=0):
536 536
537 537 deltafreq = 10. / (self.getFmax() / (self.nFFTPoints * self.ippFactor))
538 538 freqrange = deltafreq * \
539 539 (numpy.arange(self.nFFTPoints + extrapoints) -
540 540 self.nFFTPoints / 2.) - deltafreq / 2
541 541
542 542 return freqrange
543 543
544 544 def getFreqRange(self, extrapoints=0):
545 545
546 546 deltafreq = self.getFmax() / (self.nFFTPoints * self.ippFactor)
547 547 freqrange = deltafreq * \
548 548 (numpy.arange(self.nFFTPoints + extrapoints) -
549 549 self.nFFTPoints / 2.) - deltafreq / 2
550 550
551 551 return freqrange
552 552
553 553 def getVelRange(self, extrapoints=0):
554 554
555 555 deltav = self.getVmax() / (self.nFFTPoints * self.ippFactor)
556 556 velrange = deltav * (numpy.arange(self.nFFTPoints +
557 extrapoints) - self.nFFTPoints / 2.) # - deltav/2
557 extrapoints) - self.nFFTPoints / 2.)
558 558
559 return velrange
559 if self.nmodes:
560 return velrange/self.nmodes
561 else:
562 return velrange
560 563
561 564 def getNPairs(self):
562 565
563 566 return len(self.pairsList)
564 567
565 568 def getPairsIndexList(self):
566 569
567 570 return list(range(self.nPairs))
568 571
569 572 def getNormFactor(self):
570 573
571 574 pwcode = 1
572 575
573 576 if self.flagDecodeData:
574 577 pwcode = numpy.sum(self.code[0]**2)
575 578 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
576 579 normFactor = self.nProfiles * self.nIncohInt * \
577 580 self.nCohInt * pwcode * self.windowOfFilter
578 581
579 582 return normFactor
580 583
581 584 def getFlagCspc(self):
582 585
583 586 if self.data_cspc is None:
584 587 return True
585 588
586 589 return False
587 590
588 591 def getFlagDc(self):
589 592
590 593 if self.data_dc is None:
591 594 return True
592 595
593 596 return False
594 597
595 598 def getTimeInterval(self):
596 599
597 600 timeInterval = self.ippSeconds * self.nCohInt * \
598 601 self.nIncohInt * self.nProfiles * self.ippFactor
599 602
600 603 return timeInterval
601 604
602 605 def getPower(self):
603 606
604 607 factor = self.normFactor
605 608 z = self.data_spc / factor
606 609 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
607 610 avg = numpy.average(z, axis=1)
608 611
609 612 return 10 * numpy.log10(avg)
610 613
611 614 def getCoherence(self, pairsList=None, phase=False):
612 615
613 616 z = []
614 617 if pairsList is None:
615 618 pairsIndexList = self.pairsIndexList
616 619 else:
617 620 pairsIndexList = []
618 621 for pair in pairsList:
619 622 if pair not in self.pairsList:
620 623 raise ValueError("Pair %s is not in dataOut.pairsList" % (
621 624 pair))
622 625 pairsIndexList.append(self.pairsList.index(pair))
623 626 for i in range(len(pairsIndexList)):
624 627 pair = self.pairsList[pairsIndexList[i]]
625 628 ccf = numpy.average(
626 629 self.data_cspc[pairsIndexList[i], :, :], axis=0)
627 630 powa = numpy.average(self.data_spc[pair[0], :, :], axis=0)
628 631 powb = numpy.average(self.data_spc[pair[1], :, :], axis=0)
629 632 avgcoherenceComplex = ccf / numpy.sqrt(powa * powb)
630 633 if phase:
631 634 data = numpy.arctan2(avgcoherenceComplex.imag,
632 635 avgcoherenceComplex.real) * 180 / numpy.pi
633 636 else:
634 637 data = numpy.abs(avgcoherenceComplex)
635 638
636 639 z.append(data)
637 640
638 641 return numpy.array(z)
639 642
640 643 def setValue(self, value):
641 644
642 645 print("This property should not be initialized")
643 646
644 647 return
645 648
646 649 nPairs = property(getNPairs, setValue, "I'm the 'nPairs' property.")
647 650 pairsIndexList = property(
648 651 getPairsIndexList, setValue, "I'm the 'pairsIndexList' property.")
649 652 normFactor = property(getNormFactor, setValue,
650 653 "I'm the 'getNormFactor' property.")
651 654 flag_cspc = property(getFlagCspc, setValue)
652 655 flag_dc = property(getFlagDc, setValue)
653 656 noise = property(getNoise, setValue, "I'm the 'nHeights' property.")
654 657 timeInterval = property(getTimeInterval, setValue,
655 658 "I'm the 'timeInterval' property")
656 659
657 660
658 661 class SpectraHeis(Spectra):
659 662
660 663 data_spc = None
661 664 data_cspc = None
662 665 data_dc = None
663 666 nFFTPoints = None
664 667 # nPairs = None
665 668 pairsList = None
666 669 nCohInt = None
667 670 nIncohInt = None
668 671
669 672 def __init__(self):
670 673
671 674 self.radarControllerHeaderObj = RadarControllerHeader()
672 675
673 676 self.systemHeaderObj = SystemHeader()
674 677
675 678 self.type = "SpectraHeis"
676 679
677 680 # self.dtype = None
678 681
679 682 # self.nChannels = 0
680 683
681 684 # self.nHeights = 0
682 685
683 686 self.nProfiles = None
684 687
685 688 self.heightList = None
686 689
687 690 self.channelList = None
688 691
689 692 # self.channelIndexList = None
690 693
691 694 self.flagNoData = True
692 695
693 696 self.flagDiscontinuousBlock = False
694 697
695 698 # self.nPairs = 0
696 699
697 700 self.utctime = None
698 701
699 702 self.blocksize = None
700 703
701 704 self.profileIndex = 0
702 705
703 706 self.nCohInt = 1
704 707
705 708 self.nIncohInt = 1
706 709
707 710 def getNormFactor(self):
708 711 pwcode = 1
709 712 if self.flagDecodeData:
710 713 pwcode = numpy.sum(self.code[0]**2)
711 714
712 715 normFactor = self.nIncohInt * self.nCohInt * pwcode
713 716
714 717 return normFactor
715 718
716 719 def getTimeInterval(self):
717 720
718 721 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
719 722
720 723 return timeInterval
721 724
722 725 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
723 726 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
724 727
725 728
726 729 class Fits(JROData):
727 730
728 731 heightList = None
729 732 channelList = None
730 733 flagNoData = True
731 734 flagDiscontinuousBlock = False
732 735 useLocalTime = False
733 736 utctime = None
734 737 timeZone = None
735 738 # ippSeconds = None
736 739 # timeInterval = None
737 740 nCohInt = None
738 741 nIncohInt = None
739 742 noise = None
740 743 windowOfFilter = 1
741 744 # Speed of ligth
742 745 C = 3e8
743 746 frequency = 49.92e6
744 747 realtime = False
745 748
746 749 def __init__(self):
747 750
748 751 self.type = "Fits"
749 752
750 753 self.nProfiles = None
751 754
752 755 self.heightList = None
753 756
754 757 self.channelList = None
755 758
756 759 # self.channelIndexList = None
757 760
758 761 self.flagNoData = True
759 762
760 763 self.utctime = None
761 764
762 765 self.nCohInt = 1
763 766
764 767 self.nIncohInt = 1
765 768
766 769 self.useLocalTime = True
767 770
768 771 self.profileIndex = 0
769 772
770 773 # self.utctime = None
771 774 # self.timeZone = None
772 775 # self.ltctime = None
773 776 # self.timeInterval = None
774 777 # self.header = None
775 778 # self.data_header = None
776 779 # self.data = None
777 780 # self.datatime = None
778 781 # self.flagNoData = False
779 782 # self.expName = ''
780 783 # self.nChannels = None
781 784 # self.nSamples = None
782 785 # self.dataBlocksPerFile = None
783 786 # self.comments = ''
784 787 #
785 788
786 789 def getltctime(self):
787 790
788 791 if self.useLocalTime:
789 792 return self.utctime - self.timeZone * 60
790 793
791 794 return self.utctime
792 795
793 796 def getDatatime(self):
794 797
795 798 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
796 799 return datatime
797 800
798 801 def getTimeRange(self):
799 802
800 803 datatime = []
801 804
802 805 datatime.append(self.ltctime)
803 806 datatime.append(self.ltctime + self.timeInterval)
804 807
805 808 datatime = numpy.array(datatime)
806 809
807 810 return datatime
808 811
809 812 def getHeiRange(self):
810 813
811 814 heis = self.heightList
812 815
813 816 return heis
814 817
815 818 def getNHeights(self):
816 819
817 820 return len(self.heightList)
818 821
819 822 def getNChannels(self):
820 823
821 824 return len(self.channelList)
822 825
823 826 def getChannelIndexList(self):
824 827
825 828 return list(range(self.nChannels))
826 829
827 830 def getNoise(self, type=1):
828 831
829 832 #noise = numpy.zeros(self.nChannels)
830 833
831 834 if type == 1:
832 835 noise = self.getNoisebyHildebrand()
833 836
834 837 if type == 2:
835 838 noise = self.getNoisebySort()
836 839
837 840 if type == 3:
838 841 noise = self.getNoisebyWindow()
839 842
840 843 return noise
841 844
842 845 def getTimeInterval(self):
843 846
844 847 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
845 848
846 849 return timeInterval
847 850
848 851 datatime = property(getDatatime, "I'm the 'datatime' property")
849 852 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
850 853 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
851 854 channelIndexList = property(
852 855 getChannelIndexList, "I'm the 'channelIndexList' property.")
853 856 noise = property(getNoise, "I'm the 'nHeights' property.")
854 857
855 858 ltctime = property(getltctime, "I'm the 'ltctime' property")
856 859 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
857 860
858 861
859 862 class Correlation(JROData):
860 863
861 864 noise = None
862 865 SNR = None
863 866 #--------------------------------------------------
864 867 mode = None
865 868 split = False
866 869 data_cf = None
867 870 lags = None
868 871 lagRange = None
869 872 pairsList = None
870 873 normFactor = None
871 874 #--------------------------------------------------
872 875 # calculateVelocity = None
873 876 nLags = None
874 877 nPairs = None
875 878 nAvg = None
876 879
877 880 def __init__(self):
878 881 '''
879 882 Constructor
880 883 '''
881 884 self.radarControllerHeaderObj = RadarControllerHeader()
882 885
883 886 self.systemHeaderObj = SystemHeader()
884 887
885 888 self.type = "Correlation"
886 889
887 890 self.data = None
888 891
889 892 self.dtype = None
890 893
891 894 self.nProfiles = None
892 895
893 896 self.heightList = None
894 897
895 898 self.channelList = None
896 899
897 900 self.flagNoData = True
898 901
899 902 self.flagDiscontinuousBlock = False
900 903
901 904 self.utctime = None
902 905
903 906 self.timeZone = None
904 907
905 908 self.dstFlag = None
906 909
907 910 self.errorCount = None
908 911
909 912 self.blocksize = None
910 913
911 914 self.flagDecodeData = False # asumo q la data no esta decodificada
912 915
913 916 self.flagDeflipData = False # asumo q la data no esta sin flip
914 917
915 918 self.pairsList = None
916 919
917 920 self.nPoints = None
918 921
919 922 def getPairsList(self):
920 923
921 924 return self.pairsList
922 925
923 926 def getNoise(self, mode=2):
924 927
925 928 indR = numpy.where(self.lagR == 0)[0][0]
926 929 indT = numpy.where(self.lagT == 0)[0][0]
927 930
928 931 jspectra0 = self.data_corr[:, :, indR, :]
929 932 jspectra = copy.copy(jspectra0)
930 933
931 934 num_chan = jspectra.shape[0]
932 935 num_hei = jspectra.shape[2]
933 936
934 937 freq_dc = jspectra.shape[1] / 2
935 938 ind_vel = numpy.array([-2, -1, 1, 2]) + freq_dc
936 939
937 940 if ind_vel[0] < 0:
938 941 ind_vel[list(range(0, 1))] = ind_vel[list(
939 942 range(0, 1))] + self.num_prof
940 943
941 944 if mode == 1:
942 945 jspectra[:, freq_dc, :] = (
943 946 jspectra[:, ind_vel[1], :] + jspectra[:, ind_vel[2], :]) / 2 # CORRECCION
944 947
945 948 if mode == 2:
946 949
947 950 vel = numpy.array([-2, -1, 1, 2])
948 951 xx = numpy.zeros([4, 4])
949 952
950 953 for fil in range(4):
951 954 xx[fil, :] = vel[fil]**numpy.asarray(list(range(4)))
952 955
953 956 xx_inv = numpy.linalg.inv(xx)
954 957 xx_aux = xx_inv[0, :]
955 958
956 959 for ich in range(num_chan):
957 960 yy = jspectra[ich, ind_vel, :]
958 961 jspectra[ich, freq_dc, :] = numpy.dot(xx_aux, yy)
959 962
960 963 junkid = jspectra[ich, freq_dc, :] <= 0
961 964 cjunkid = sum(junkid)
962 965
963 966 if cjunkid.any():
964 967 jspectra[ich, freq_dc, junkid.nonzero()] = (
965 968 jspectra[ich, ind_vel[1], junkid] + jspectra[ich, ind_vel[2], junkid]) / 2
966 969
967 970 noise = jspectra0[:, freq_dc, :] - jspectra[:, freq_dc, :]
968 971
969 972 return noise
970 973
971 974 def getTimeInterval(self):
972 975
973 976 timeInterval = self.ippSeconds * self.nCohInt * self.nProfiles
974 977
975 978 return timeInterval
976 979
977 980 def splitFunctions(self):
978 981
979 982 pairsList = self.pairsList
980 983 ccf_pairs = []
981 984 acf_pairs = []
982 985 ccf_ind = []
983 986 acf_ind = []
984 987 for l in range(len(pairsList)):
985 988 chan0 = pairsList[l][0]
986 989 chan1 = pairsList[l][1]
987 990
988 991 # Obteniendo pares de Autocorrelacion
989 992 if chan0 == chan1:
990 993 acf_pairs.append(chan0)
991 994 acf_ind.append(l)
992 995 else:
993 996 ccf_pairs.append(pairsList[l])
994 997 ccf_ind.append(l)
995 998
996 999 data_acf = self.data_cf[acf_ind]
997 1000 data_ccf = self.data_cf[ccf_ind]
998 1001
999 1002 return acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf
1000 1003
1001 1004 def getNormFactor(self):
1002 1005 acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.splitFunctions()
1003 1006 acf_pairs = numpy.array(acf_pairs)
1004 1007 normFactor = numpy.zeros((self.nPairs, self.nHeights))
1005 1008
1006 1009 for p in range(self.nPairs):
1007 1010 pair = self.pairsList[p]
1008 1011
1009 1012 ch0 = pair[0]
1010 1013 ch1 = pair[1]
1011 1014
1012 1015 ch0_max = numpy.max(data_acf[acf_pairs == ch0, :, :], axis=1)
1013 1016 ch1_max = numpy.max(data_acf[acf_pairs == ch1, :, :], axis=1)
1014 1017 normFactor[p, :] = numpy.sqrt(ch0_max * ch1_max)
1015 1018
1016 1019 return normFactor
1017 1020
1018 1021 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
1019 1022 normFactor = property(getNormFactor, "I'm the 'normFactor property'")
1020 1023
1021 1024
1022 1025 class Parameters(Spectra):
1023 1026
1024 1027 experimentInfo = None # Information about the experiment
1025 1028 # Information from previous data
1026 1029 inputUnit = None # Type of data to be processed
1027 1030 operation = None # Type of operation to parametrize
1028 1031 # normFactor = None #Normalization Factor
1029 1032 groupList = None # List of Pairs, Groups, etc
1030 1033 # Parameters
1031 1034 data_param = None # Parameters obtained
1032 1035 data_pre = None # Data Pre Parametrization
1033 1036 data_SNR = None # Signal to Noise Ratio
1034 1037 # heightRange = None #Heights
1035 1038 abscissaList = None # Abscissa, can be velocities, lags or time
1036 1039 # noise = None #Noise Potency
1037 1040 utctimeInit = None # Initial UTC time
1038 1041 paramInterval = None # Time interval to calculate Parameters in seconds
1039 1042 useLocalTime = True
1040 1043 # Fitting
1041 1044 data_error = None # Error of the estimation
1042 1045 constants = None
1043 1046 library = None
1044 1047 # Output signal
1045 1048 outputInterval = None # Time interval to calculate output signal in seconds
1046 1049 data_output = None # Out signal
1047 1050 nAvg = None
1048 1051 noise_estimation = None
1049 1052 GauSPC = None # Fit gaussian SPC
1050 1053
1051 1054 def __init__(self):
1052 1055 '''
1053 1056 Constructor
1054 1057 '''
1055 1058 self.radarControllerHeaderObj = RadarControllerHeader()
1056 1059
1057 1060 self.systemHeaderObj = SystemHeader()
1058 1061
1059 1062 self.type = "Parameters"
1060 1063
1061 1064 def getTimeRange1(self, interval):
1062 1065
1063 1066 datatime = []
1064 1067
1065 1068 if self.useLocalTime:
1066 1069 time1 = self.utctimeInit - self.timeZone * 60
1067 1070 else:
1068 1071 time1 = self.utctimeInit
1069 1072
1070 1073 datatime.append(time1)
1071 1074 datatime.append(time1 + interval)
1072 1075 datatime = numpy.array(datatime)
1073 1076
1074 1077 return datatime
1075 1078
1076 1079 def getTimeInterval(self):
1077 1080
1078 1081 if hasattr(self, 'timeInterval1'):
1079 1082 return self.timeInterval1
1080 1083 else:
1081 1084 return self.paramInterval
1082 1085
1083 1086 def setValue(self, value):
1084 1087
1085 1088 print("This property should not be initialized")
1086 1089
1087 1090 return
1088 1091
1089 1092 def getNoise(self):
1090 1093
1091 1094 return self.spc_noise
1092 1095
1093 1096 timeInterval = property(getTimeInterval)
1094 1097 noise = property(getNoise, setValue, "I'm the 'Noise' property.")
1095 1098
1096 1099
1097 1100 class PlotterData(object):
1098 1101 '''
1099 1102 Object to hold data to be plotted
1100 1103 '''
1101 1104
1102 1105 MAXNUMX = 100
1103 1106 MAXNUMY = 100
1104 1107
1105 1108 def __init__(self, code, throttle_value, exp_code, buffering=True):
1106 1109
1107 1110 self.throttle = throttle_value
1108 1111 self.exp_code = exp_code
1109 1112 self.buffering = buffering
1110 1113 self.ready = False
1111 1114 self.localtime = False
1112 1115 self.data = {}
1113 1116 self.meta = {}
1114 1117 self.__times = []
1115 1118 self.__heights = []
1116 1119
1117 1120 if 'snr' in code:
1118 1121 self.plottypes = ['snr']
1119 1122 elif code == 'spc':
1120 1123 self.plottypes = ['spc', 'noise', 'rti']
1121 1124 elif code == 'rti':
1122 1125 self.plottypes = ['noise', 'rti']
1123 1126 else:
1124 1127 self.plottypes = [code]
1125 1128
1126 1129 for plot in self.plottypes:
1127 1130 self.data[plot] = {}
1128 1131
1129 1132 def __str__(self):
1130 1133 dum = ['{}{}'.format(key, self.shape(key)) for key in self.data]
1131 1134 return 'Data[{}][{}]'.format(';'.join(dum), len(self.__times))
1132 1135
1133 1136 def __len__(self):
1134 1137 return len(self.__times)
1135 1138
1136 1139 def __getitem__(self, key):
1137 1140
1138 1141 if key not in self.data:
1139 1142 raise KeyError(log.error('Missing key: {}'.format(key)))
1140 1143 if 'spc' in key or not self.buffering:
1141 1144 ret = self.data[key]
1142 1145 else:
1143 1146 ret = numpy.array([self.data[key][x] for x in self.times])
1144 1147 if ret.ndim > 1:
1145 1148 ret = numpy.swapaxes(ret, 0, 1)
1146 1149 return ret
1147 1150
1148 1151 def __contains__(self, key):
1149 1152 return key in self.data
1150 1153
1151 1154 def setup(self):
1152 1155 '''
1153 1156 Configure object
1154 1157 '''
1155 1158
1156 1159 self.type = ''
1157 1160 self.ready = False
1158 1161 self.data = {}
1159 1162 self.__times = []
1160 1163 self.__heights = []
1161 1164 self.__all_heights = set()
1162 1165 for plot in self.plottypes:
1163 1166 if 'snr' in plot:
1164 1167 plot = 'snr'
1165 1168 self.data[plot] = {}
1166 1169
1167 1170 if 'spc' in self.data or 'rti' in self.data:
1168 1171 self.data['noise'] = {}
1169 1172 if 'noise' not in self.plottypes:
1170 1173 self.plottypes.append('noise')
1171 1174
1172 1175 def shape(self, key):
1173 1176 '''
1174 1177 Get the shape of the one-element data for the given key
1175 1178 '''
1176 1179
1177 1180 if len(self.data[key]):
1178 1181 if 'spc' in key or not self.buffering:
1179 1182 return self.data[key].shape
1180 1183 return self.data[key][self.__times[0]].shape
1181 1184 return (0,)
1182 1185
1183 1186 def update(self, dataOut, tm):
1184 1187 '''
1185 1188 Update data object with new dataOut
1186 1189 '''
1187 1190
1188 1191 if tm in self.__times:
1189 1192 return
1190 1193
1191 1194 self.type = dataOut.type
1192 1195 self.parameters = getattr(dataOut, 'parameters', [])
1193 1196 if hasattr(dataOut, 'pairsList'):
1194 1197 self.pairs = dataOut.pairsList
1195 1198 if hasattr(dataOut, 'meta'):
1196 1199 self.meta = dataOut.meta
1197 1200 self.channels = dataOut.channelList
1198 1201 self.interval = dataOut.getTimeInterval()
1199 1202 self.localtime = dataOut.useLocalTime
1200 1203 if 'spc' in self.plottypes or 'cspc' in self.plottypes:
1201 1204 self.xrange = (dataOut.getFreqRange(1)/1000.,
1202 1205 dataOut.getAcfRange(1), dataOut.getVelRange(1))
1203 1206 self.__heights.append(dataOut.heightList)
1204 1207 self.__all_heights.update(dataOut.heightList)
1205 1208 self.__times.append(tm)
1206 1209
1207 1210 for plot in self.plottypes:
1208 1211 if plot == 'spc':
1209 1212 z = dataOut.data_spc/dataOut.normFactor
1210 1213 buffer = 10*numpy.log10(z)
1211 1214 if plot == 'cspc':
1212 1215 buffer = dataOut.data_cspc
1213 1216 if plot == 'noise':
1214 1217 buffer = 10*numpy.log10(dataOut.getNoise()/dataOut.normFactor)
1215 1218 if plot == 'rti':
1216 1219 buffer = dataOut.getPower()
1217 1220 if plot == 'snr_db':
1218 1221 buffer = dataOut.data_SNR
1219 1222 if plot == 'snr':
1220 1223 buffer = 10*numpy.log10(dataOut.data_SNR)
1221 1224 if plot == 'dop':
1222 1225 buffer = 10*numpy.log10(dataOut.data_DOP)
1223 1226 if plot == 'mean':
1224 1227 buffer = dataOut.data_MEAN
1225 1228 if plot == 'std':
1226 1229 buffer = dataOut.data_STD
1227 1230 if plot == 'coh':
1228 1231 buffer = dataOut.getCoherence()
1229 1232 if plot == 'phase':
1230 1233 buffer = dataOut.getCoherence(phase=True)
1231 1234 if plot == 'output':
1232 1235 buffer = dataOut.data_output
1233 1236 if plot == 'param':
1234 1237 buffer = dataOut.data_param
1235 1238
1236 1239 if 'spc' in plot:
1237 1240 self.data[plot] = buffer
1238 1241 else:
1239 1242 if self.buffering:
1240 1243 self.data[plot][tm] = buffer
1241 1244 else:
1242 1245 self.data[plot] = buffer
1243 1246
1244 1247 def normalize_heights(self):
1245 1248 '''
1246 1249 Ensure same-dimension of the data for different heighList
1247 1250 '''
1248 1251
1249 1252 H = numpy.array(list(self.__all_heights))
1250 1253 H.sort()
1251 1254 for key in self.data:
1252 1255 shape = self.shape(key)[:-1] + H.shape
1253 1256 for tm, obj in list(self.data[key].items()):
1254 1257 h = self.__heights[self.__times.index(tm)]
1255 1258 if H.size == h.size:
1256 1259 continue
1257 1260 index = numpy.where(numpy.in1d(H, h))[0]
1258 1261 dummy = numpy.zeros(shape) + numpy.nan
1259 1262 if len(shape) == 2:
1260 1263 dummy[:, index] = obj
1261 1264 else:
1262 1265 dummy[index] = obj
1263 1266 self.data[key][tm] = dummy
1264 1267
1265 1268 self.__heights = [H for tm in self.__times]
1266 1269
1267 1270 def jsonify(self, decimate=False):
1268 1271 '''
1269 1272 Convert data to json
1270 1273 '''
1271 1274
1272 1275 data = {}
1273 1276 tm = self.times[-1]
1274 1277 dy = int(self.heights.size/self.MAXNUMY) + 1
1275 1278 for key in self.data:
1276 1279 if key in ('spc', 'cspc') or not self.buffering:
1277 1280 dx = int(self.data[key].shape[1]/self.MAXNUMX) + 1
1278 1281 data[key] = self.roundFloats(
1279 1282 self.data[key][::, ::dx, ::dy].tolist())
1280 1283 else:
1281 1284 data[key] = self.roundFloats(self.data[key][tm].tolist())
1282 1285
1283 1286 ret = {'data': data}
1284 1287 ret['exp_code'] = self.exp_code
1285 1288 ret['time'] = float(tm)
1286 1289 ret['interval'] = float(self.interval)
1287 1290 ret['localtime'] = self.localtime
1288 1291 ret['yrange'] = self.roundFloats(self.heights[::dy].tolist())
1289 1292 if 'spc' in self.data or 'cspc' in self.data:
1290 1293 ret['xrange'] = self.roundFloats(self.xrange[2][::dx].tolist())
1291 1294 else:
1292 1295 ret['xrange'] = []
1293 1296 if hasattr(self, 'pairs'):
1294 1297 ret['pairs'] = [(int(p[0]), int(p[1])) for p in self.pairs]
1295 1298 else:
1296 1299 ret['pairs'] = []
1297 1300
1298 1301 for key, value in list(self.meta.items()):
1299 1302 ret[key] = value
1300 1303
1301 1304 return json.dumps(ret)
1302 1305
1303 1306 @property
1304 1307 def times(self):
1305 1308 '''
1306 1309 Return the list of times of the current data
1307 1310 '''
1308 1311
1309 1312 ret = numpy.array(self.__times)
1310 1313 ret.sort()
1311 1314 return ret
1312 1315
1313 1316 @property
1314 1317 def min_time(self):
1315 1318 '''
1316 1319 Return the minimun time value
1317 1320 '''
1318 1321
1319 1322 return self.times[0]
1320 1323
1321 1324 @property
1322 1325 def max_time(self):
1323 1326 '''
1324 1327 Return the maximun time value
1325 1328 '''
1326 1329
1327 1330 return self.times[-1]
1328 1331
1329 1332 @property
1330 1333 def heights(self):
1331 1334 '''
1332 1335 Return the list of heights of the current data
1333 1336 '''
1334 1337
1335 1338 return numpy.array(self.__heights[-1])
1336 1339
1337 1340 @staticmethod
1338 1341 def roundFloats(obj):
1339 1342 if isinstance(obj, list):
1340 1343 return list(map(PlotterData.roundFloats, obj))
1341 1344 elif isinstance(obj, float):
1342 1345 return round(obj, 2)
@@ -1,2158 +1,2389
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 from schainpy.model.proc.jroproc_base import MPDecorator
8 8 from schainpy.utils import log
9 9
10 class FitGauPlot_(Figure):
10 class ParamLine_(Figure):
11
12 isConfig = None
13
14 def __init__(self):
15
16 self.isConfig = False
17 self.WIDTH = 300
18 self.HEIGHT = 200
19 self.counter_imagwr = 0
20
21 def getSubplots(self):
22
23 nrow = self.nplots
24 ncol = 3
25 return nrow, ncol
26
27 def setup(self, id, nplots, wintitle, show):
28
29 self.nplots = nplots
30
31 self.createFigure(id=id,
32 wintitle=wintitle,
33 show=show)
34
35 nrow,ncol = self.getSubplots()
36 colspan = 3
37 rowspan = 1
38
39 for i in range(nplots):
40 self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
41
42 def plot_iq(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
43 yreal = y[channelIndexList,:].real
44 yimag = y[channelIndexList,:].imag
45
46 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
47 xlabel = "Range (Km)"
48 ylabel = "Intensity - IQ"
49
50 if not self.isConfig:
51 nplots = len(channelIndexList)
52
53 self.setup(id=id,
54 nplots=nplots,
55 wintitle='',
56 show=show)
57
58 if xmin == None: xmin = numpy.nanmin(x)
59 if xmax == None: xmax = numpy.nanmax(x)
60 if ymin == None: ymin = min(numpy.nanmin(yreal),numpy.nanmin(yimag))
61 if ymax == None: ymax = max(numpy.nanmax(yreal),numpy.nanmax(yimag))
62
63 self.isConfig = True
64
65 self.setWinTitle(title)
66
67 for i in range(len(self.axesList)):
68 title = "Channel %d" %(i)
69 axes = self.axesList[i]
70
71 axes.pline(x, yreal[i,:],
72 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
73 xlabel=xlabel, ylabel=ylabel, title=title)
74
75 axes.addpline(x, yimag[i,:], idline=1, color="red", linestyle="solid", lw=2)
76
77 def plot_power(self, x, y, id, channelIndexList, thisDatetime, wintitle, show, xmin, xmax, ymin, ymax):
78 y = y[channelIndexList,:] * numpy.conjugate(y[channelIndexList,:])
79 yreal = y.real
80
81 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
82 xlabel = "Range (Km)"
83 ylabel = "Intensity"
84
85 if not self.isConfig:
86 nplots = len(channelIndexList)
87
88 self.setup(id=id,
89 nplots=nplots,
90 wintitle='',
91 show=show)
92
93 if xmin == None: xmin = numpy.nanmin(x)
94 if xmax == None: xmax = numpy.nanmax(x)
95 if ymin == None: ymin = numpy.nanmin(yreal)
96 if ymax == None: ymax = numpy.nanmax(yreal)
97
98 self.isConfig = True
99
100 self.setWinTitle(title)
101
102 for i in range(len(self.axesList)):
103 title = "Channel %d" %(i)
104 axes = self.axesList[i]
105 ychannel = yreal[i,:]
106 axes.pline(x, ychannel,
107 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
108 xlabel=xlabel, ylabel=ylabel, title=title)
109
110
111 def run(self, dataOut, id, wintitle="", channelList=None,
112 xmin=None, xmax=None, ymin=None, ymax=None, save=False,
113 figpath='./', figfile=None, show=True, wr_period=1,
114 ftp=False, server=None, folder=None, username=None, password=None):
115
116 """
117
118 Input:
119 dataOut :
120 id :
121 wintitle :
122 channelList :
123 xmin : None,
124 xmax : None,
125 ymin : None,
126 ymax : None,
127 """
128
129 if channelList == None:
130 channelIndexList = dataOut.channelIndexList
131 else:
132 channelIndexList = []
133 for channel in channelList:
134 if channel not in dataOut.channelList:
135 raise ValueError("Channel %d is not in dataOut.channelList" % channel)
136 channelIndexList.append(dataOut.channelList.index(channel))
137
138 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
139
140 y = dataOut.RR
141
142 title = wintitle + " Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
143 xlabel = "Range (Km)"
144 ylabel = "Intensity"
145
146 if not self.isConfig:
147 nplots = len(channelIndexList)
148
149 self.setup(id=id,
150 nplots=nplots,
151 wintitle='',
152 show=show)
153
154 if xmin == None: xmin = numpy.nanmin(x)
155 if xmax == None: xmax = numpy.nanmax(x)
156 if ymin == None: ymin = numpy.nanmin(y)
157 if ymax == None: ymax = numpy.nanmax(y)
158
159 self.isConfig = True
160
161 self.setWinTitle(title)
162
163 for i in range(len(self.axesList)):
164 title = "Channel %d" %(i)
165 axes = self.axesList[i]
166 ychannel = y[i,:]
167 axes.pline(x, ychannel,
168 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
169 xlabel=xlabel, ylabel=ylabel, title=title)
170
171
172 self.draw()
173
174 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S") + "_" + str(dataOut.profileIndex)
175 figfile = self.getFilename(name = str_datetime)
176
177 self.save(figpath=figpath,
178 figfile=figfile,
179 save=save,
180 ftp=ftp,
181 wr_period=wr_period,
182 thisDatetime=thisDatetime)
183
184
185
186 class SpcParamPlot_(Figure):
11 187
12 188 isConfig = None
13 189 __nsubplots = None
14 190
15 191 WIDTHPROF = None
16 192 HEIGHTPROF = None
17 PREFIX = 'fitgau'
193 PREFIX = 'SpcParam'
18 194
19 195 def __init__(self, **kwargs):
20 196 Figure.__init__(self, **kwargs)
21 197 self.isConfig = False
22 198 self.__nsubplots = 1
23 199
24 200 self.WIDTH = 250
25 201 self.HEIGHT = 250
26 202 self.WIDTHPROF = 120
27 203 self.HEIGHTPROF = 0
28 204 self.counter_imagwr = 0
29 205
30 206 self.PLOT_CODE = SPEC_CODE
31 207
32 208 self.FTP_WEI = None
33 209 self.EXP_CODE = None
34 210 self.SUB_EXP_CODE = None
35 211 self.PLOT_POS = None
36 212
37 213 self.__xfilter_ena = False
38 214 self.__yfilter_ena = False
39 215
40 216 def getSubplots(self):
41 217
42 218 ncol = int(numpy.sqrt(self.nplots)+0.9)
43 219 nrow = int(self.nplots*1./ncol + 0.9)
44 220
45 221 return nrow, ncol
46 222
47 223 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
48 224
49 225 self.__showprofile = showprofile
50 226 self.nplots = nplots
51 227
52 228 ncolspan = 1
53 229 colspan = 1
54 230 if showprofile:
55 231 ncolspan = 3
56 232 colspan = 2
57 233 self.__nsubplots = 2
58 234
59 235 self.createFigure(id = id,
60 236 wintitle = wintitle,
61 237 widthplot = self.WIDTH + self.WIDTHPROF,
62 238 heightplot = self.HEIGHT + self.HEIGHTPROF,
63 239 show=show)
64 240
65 241 nrow, ncol = self.getSubplots()
66 242
67 243 counter = 0
68 244 for y in range(nrow):
69 245 for x in range(ncol):
70 246
71 247 if counter >= self.nplots:
72 248 break
73 249
74 250 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
75 251
76 252 if showprofile:
77 253 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
78 254
79 255 counter += 1
80 256
81 257 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
82 258 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
83 259 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
84 260 server=None, folder=None, username=None, password=None,
85 261 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
86 xaxis="frequency", colormap='jet', normFactor=None , GauSelector = 1):
262 xaxis="frequency", colormap='jet', normFactor=None , Selector = 0):
87 263
88 264 """
89 265
90 266 Input:
91 267 dataOut :
92 268 id :
93 269 wintitle :
94 270 channelList :
95 271 showProfile :
96 272 xmin : None,
97 273 xmax : None,
98 274 ymin : None,
99 275 ymax : None,
100 276 zmin : None,
101 277 zmax : None
102 278 """
103 279 if realtime:
104 280 if not(isRealtime(utcdatatime = dataOut.utctime)):
105 281 print('Skipping this plot function')
106 282 return
107 283
108 284 if channelList == None:
109 285 channelIndexList = dataOut.channelIndexList
110 286 else:
111 287 channelIndexList = []
112 288 for channel in channelList:
113 289 if channel not in dataOut.channelList:
114 290 raise ValueError("Channel %d is not in dataOut.channelList" %channel)
115 291 channelIndexList.append(dataOut.channelList.index(channel))
116 292
117 293 # if normFactor is None:
118 294 # factor = dataOut.normFactor
119 295 # else:
120 296 # factor = normFactor
121 297 if xaxis == "frequency":
122 x = dataOut.spc_range[0]
298 x = dataOut.spcparam_range[0]
123 299 xlabel = "Frequency (kHz)"
124 300
125 301 elif xaxis == "time":
126 x = dataOut.spc_range[1]
302 x = dataOut.spcparam_range[1]
127 303 xlabel = "Time (ms)"
128 304
129 else:
130 x = dataOut.spc_range[2]
305 else:
306 x = dataOut.spcparam_range[2]
131 307 xlabel = "Velocity (m/s)"
132 308
133 ylabel = "Range (Km)"
309 ylabel = "Range (km)"
134 310
135 311 y = dataOut.getHeiRange()
136 312
137 z = dataOut.GauSPC[:,GauSelector,:,:] #GauSelector] #dataOut.data_spc/factor
138 print('GausSPC', z[0,32,10:40])
313 z = dataOut.SPCparam[Selector] /1966080.0#/ dataOut.normFactor#GauSelector] #dataOut.data_spc/factor
139 314 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
140 315 zdB = 10*numpy.log10(z)
141 316
142 317 avg = numpy.average(z, axis=1)
143 318 avgdB = 10*numpy.log10(avg)
144 319
145 320 noise = dataOut.spc_noise
146 321 noisedB = 10*numpy.log10(noise)
147 322
148 323 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
149 324 title = wintitle + " Spectra"
150 325 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
151 326 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
152 327
153 328 if not self.isConfig:
154 329
155 330 nplots = len(channelIndexList)
156 331
157 332 self.setup(id=id,
158 333 nplots=nplots,
159 334 wintitle=wintitle,
160 335 showprofile=showprofile,
161 336 show=show)
162 337
163 338 if xmin == None: xmin = numpy.nanmin(x)
164 339 if xmax == None: xmax = numpy.nanmax(x)
165 340 if ymin == None: ymin = numpy.nanmin(y)
166 341 if ymax == None: ymax = numpy.nanmax(y)
167 342 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
168 343 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
169 344
170 345 self.FTP_WEI = ftp_wei
171 346 self.EXP_CODE = exp_code
172 347 self.SUB_EXP_CODE = sub_exp_code
173 348 self.PLOT_POS = plot_pos
174 349
175 350 self.isConfig = True
176 351
177 352 self.setWinTitle(title)
178 353
179 354 for i in range(self.nplots):
180 355 index = channelIndexList[i]
181 356 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
182 357 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
183 358 if len(dataOut.beam.codeList) != 0:
184 359 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)
185 360
186 361 axes = self.axesList[i*self.__nsubplots]
187 362 axes.pcolor(x, y, zdB[index,:,:],
188 363 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
189 364 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
190 365 ticksize=9, cblabel='')
191 366
192 367 if self.__showprofile:
193 368 axes = self.axesList[i*self.__nsubplots +1]
194 369 axes.pline(avgdB[index,:], y,
195 370 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
196 371 xlabel='dB', ylabel='', title='',
197 372 ytick_visible=False,
198 373 grid='x')
199 374
200 375 noiseline = numpy.repeat(noisedB[index], len(y))
201 376 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
202 377
203 378 self.draw()
204 379
205 380 if figfile == None:
206 381 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
207 382 name = str_datetime
208 383 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
209 384 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
210 385 figfile = self.getFilename(name)
211 386
212 387 self.save(figpath=figpath,
213 388 figfile=figfile,
214 389 save=save,
215 390 ftp=ftp,
216 391 wr_period=wr_period,
217 392 thisDatetime=thisDatetime)
218 393
219 394
220 395
221 396 class MomentsPlot_(Figure):
222 397
223 398 isConfig = None
224 399 __nsubplots = None
225 400
226 401 WIDTHPROF = None
227 402 HEIGHTPROF = None
228 403 PREFIX = 'prm'
229 404 def __init__(self):
230 405 Figure.__init__(self)
231 406 self.isConfig = False
232 407 self.__nsubplots = 1
233 408
234 409 self.WIDTH = 280
235 410 self.HEIGHT = 250
236 411 self.WIDTHPROF = 120
237 412 self.HEIGHTPROF = 0
238 413 self.counter_imagwr = 0
239 414
240 415 self.PLOT_CODE = MOMENTS_CODE
241 416
242 417 self.FTP_WEI = None
243 418 self.EXP_CODE = None
244 419 self.SUB_EXP_CODE = None
245 420 self.PLOT_POS = None
246 421
247 422 def getSubplots(self):
248 423
249 424 ncol = int(numpy.sqrt(self.nplots)+0.9)
250 425 nrow = int(self.nplots*1./ncol + 0.9)
251 426
252 427 return nrow, ncol
253 428
254 429 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
255 430
256 431 self.__showprofile = showprofile
257 432 self.nplots = nplots
258 433
259 434 ncolspan = 1
260 435 colspan = 1
261 436 if showprofile:
262 437 ncolspan = 3
263 438 colspan = 2
264 439 self.__nsubplots = 2
265 440
266 441 self.createFigure(id = id,
267 442 wintitle = wintitle,
268 443 widthplot = self.WIDTH + self.WIDTHPROF,
269 444 heightplot = self.HEIGHT + self.HEIGHTPROF,
270 445 show=show)
271 446
272 447 nrow, ncol = self.getSubplots()
273 448
274 449 counter = 0
275 450 for y in range(nrow):
276 451 for x in range(ncol):
277 452
278 453 if counter >= self.nplots:
279 454 break
280 455
281 456 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
282 457
283 458 if showprofile:
284 459 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
285 460
286 461 counter += 1
287 462
288 463 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
289 464 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
290 465 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
291 466 server=None, folder=None, username=None, password=None,
292 467 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
293 468
294 469 """
295 470
296 471 Input:
297 472 dataOut :
298 473 id :
299 474 wintitle :
300 475 channelList :
301 476 showProfile :
302 477 xmin : None,
303 478 xmax : None,
304 479 ymin : None,
305 480 ymax : None,
306 481 zmin : None,
307 482 zmax : None
308 483 """
309 484
310 485 if dataOut.flagNoData:
311 486 return None
312 487
313 488 if realtime:
314 489 if not(isRealtime(utcdatatime = dataOut.utctime)):
315 490 print('Skipping this plot function')
316 491 return
317 492
318 493 if channelList == None:
319 494 channelIndexList = dataOut.channelIndexList
320 495 else:
321 496 channelIndexList = []
322 497 for channel in channelList:
323 498 if channel not in dataOut.channelList:
324 499 raise ValueError("Channel %d is not in dataOut.channelList")
325 500 channelIndexList.append(dataOut.channelList.index(channel))
326 501
327 502 factor = dataOut.normFactor
328 503 x = dataOut.abscissaList
329 504 y = dataOut.heightList
330 505
331 506 z = dataOut.data_pre[channelIndexList,:,:]/factor
332 507 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
333 508 avg = numpy.average(z, axis=1)
334 509 noise = dataOut.noise/factor
335 510
336 511 zdB = 10*numpy.log10(z)
337 512 avgdB = 10*numpy.log10(avg)
338 513 noisedB = 10*numpy.log10(noise)
339 514
340 515 #thisDatetime = dataOut.datatime
341 516 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
342 517 title = wintitle + " Parameters"
343 518 xlabel = "Velocity (m/s)"
344 519 ylabel = "Range (Km)"
345 520
346 521 update_figfile = False
347 522
348 523 if not self.isConfig:
349 524
350 525 nplots = len(channelIndexList)
351 526
352 527 self.setup(id=id,
353 528 nplots=nplots,
354 529 wintitle=wintitle,
355 530 showprofile=showprofile,
356 531 show=show)
357 532
358 533 if xmin == None: xmin = numpy.nanmin(x)
359 534 if xmax == None: xmax = numpy.nanmax(x)
360 535 if ymin == None: ymin = numpy.nanmin(y)
361 536 if ymax == None: ymax = numpy.nanmax(y)
362 537 if zmin == None: zmin = numpy.nanmin(avgdB)*0.9
363 538 if zmax == None: zmax = numpy.nanmax(avgdB)*0.9
364 539
365 540 self.FTP_WEI = ftp_wei
366 541 self.EXP_CODE = exp_code
367 542 self.SUB_EXP_CODE = sub_exp_code
368 543 self.PLOT_POS = plot_pos
369 544
370 545 self.isConfig = True
371 546 update_figfile = True
372 547
373 548 self.setWinTitle(title)
374 549
375 550 for i in range(self.nplots):
376 551 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
377 552 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i], noisedB[i], str_datetime)
378 553 axes = self.axesList[i*self.__nsubplots]
379 554 axes.pcolor(x, y, zdB[i,:,:],
380 555 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
381 556 xlabel=xlabel, ylabel=ylabel, title=title,
382 557 ticksize=9, cblabel='')
383 558 #Mean Line
384 559 mean = dataOut.data_param[i, 1, :]
385 560 axes.addpline(mean, y, idline=0, color="black", linestyle="solid", lw=1)
386 561
387 562 if self.__showprofile:
388 563 axes = self.axesList[i*self.__nsubplots +1]
389 564 axes.pline(avgdB[i], y,
390 565 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
391 566 xlabel='dB', ylabel='', title='',
392 567 ytick_visible=False,
393 568 grid='x')
394 569
395 570 noiseline = numpy.repeat(noisedB[i], len(y))
396 571 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
397 572
398 573 self.draw()
399 574
400 575 self.save(figpath=figpath,
401 576 figfile=figfile,
402 577 save=save,
403 578 ftp=ftp,
404 579 wr_period=wr_period,
405 580 thisDatetime=thisDatetime)
406 581
407 582
408 583 class SkyMapPlot_(Figure):
409 584
410 585 __isConfig = None
411 586 __nsubplots = None
412 587
413 588 WIDTHPROF = None
414 589 HEIGHTPROF = None
415 590 PREFIX = 'mmap'
416 591
417 592 def __init__(self, **kwargs):
418 593 Figure.__init__(self, **kwargs)
419 594 self.isConfig = False
420 595 self.__nsubplots = 1
421 596
422 597 # self.WIDTH = 280
423 598 # self.HEIGHT = 250
424 599 self.WIDTH = 600
425 600 self.HEIGHT = 600
426 601 self.WIDTHPROF = 120
427 602 self.HEIGHTPROF = 0
428 603 self.counter_imagwr = 0
429 604
430 605 self.PLOT_CODE = MSKYMAP_CODE
431 606
432 607 self.FTP_WEI = None
433 608 self.EXP_CODE = None
434 609 self.SUB_EXP_CODE = None
435 610 self.PLOT_POS = None
436 611
437 612 def getSubplots(self):
438 613
439 614 ncol = int(numpy.sqrt(self.nplots)+0.9)
440 615 nrow = int(self.nplots*1./ncol + 0.9)
441 616
442 617 return nrow, ncol
443 618
444 619 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
445 620
446 621 self.__showprofile = showprofile
447 622 self.nplots = nplots
448 623
449 624 ncolspan = 1
450 625 colspan = 1
451 626
452 627 self.createFigure(id = id,
453 628 wintitle = wintitle,
454 629 widthplot = self.WIDTH, #+ self.WIDTHPROF,
455 630 heightplot = self.HEIGHT,# + self.HEIGHTPROF,
456 631 show=show)
457 632
458 633 nrow, ncol = 1,1
459 634 counter = 0
460 635 x = 0
461 636 y = 0
462 637 self.addAxes(1, 1, 0, 0, 1, 1, True)
463 638
464 639 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
465 640 tmin=0, tmax=24, timerange=None,
466 641 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
467 642 server=None, folder=None, username=None, password=None,
468 643 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
469 644
470 645 """
471 646
472 647 Input:
473 648 dataOut :
474 649 id :
475 650 wintitle :
476 651 channelList :
477 652 showProfile :
478 653 xmin : None,
479 654 xmax : None,
480 655 ymin : None,
481 656 ymax : None,
482 657 zmin : None,
483 658 zmax : None
484 659 """
485 660
486 661 arrayParameters = dataOut.data_param
487 662 error = arrayParameters[:,-1]
488 663 indValid = numpy.where(error == 0)[0]
489 664 finalMeteor = arrayParameters[indValid,:]
490 665 finalAzimuth = finalMeteor[:,3]
491 666 finalZenith = finalMeteor[:,4]
492 667
493 668 x = finalAzimuth*numpy.pi/180
494 669 y = finalZenith
495 670 x1 = [dataOut.ltctime, dataOut.ltctime]
496 671
497 672 #thisDatetime = dataOut.datatime
498 673 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
499 674 title = wintitle + " Parameters"
500 675 xlabel = "Zonal Zenith Angle (deg) "
501 676 ylabel = "Meridional Zenith Angle (deg)"
502 677 update_figfile = False
503 678
504 679 if not self.isConfig:
505 680
506 681 nplots = 1
507 682
508 683 self.setup(id=id,
509 684 nplots=nplots,
510 685 wintitle=wintitle,
511 686 showprofile=showprofile,
512 687 show=show)
513 688
514 689 if self.xmin is None and self.xmax is None:
515 690 self.xmin, self.xmax = self.getTimeLim(x1, tmin, tmax, timerange)
516 691
517 692 if timerange != None:
518 693 self.timerange = timerange
519 694 else:
520 695 self.timerange = self.xmax - self.xmin
521 696
522 697 self.FTP_WEI = ftp_wei
523 698 self.EXP_CODE = exp_code
524 699 self.SUB_EXP_CODE = sub_exp_code
525 700 self.PLOT_POS = plot_pos
526 701 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
527 702 self.firstdate = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
528 703 self.isConfig = True
529 704 update_figfile = True
530 705
531 706 self.setWinTitle(title)
532 707
533 708 i = 0
534 709 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
535 710
536 711 axes = self.axesList[i*self.__nsubplots]
537 712 nevents = axes.x_buffer.shape[0] + x.shape[0]
538 713 title = "Meteor Detection Sky Map\n %s - %s \n Number of events: %5.0f\n" %(self.firstdate,str_datetime,nevents)
539 714 axes.polar(x, y,
540 715 title=title, xlabel=xlabel, ylabel=ylabel,
541 716 ticksize=9, cblabel='')
542 717
543 718 self.draw()
544 719
545 720 self.save(figpath=figpath,
546 721 figfile=figfile,
547 722 save=save,
548 723 ftp=ftp,
549 724 wr_period=wr_period,
550 725 thisDatetime=thisDatetime,
551 726 update_figfile=update_figfile)
552 727
553 728 if dataOut.ltctime >= self.xmax:
554 729 self.isConfigmagwr = wr_period
555 730 self.isConfig = False
556 731 update_figfile = True
557 732 axes.__firsttime = True
558 733 self.xmin += self.timerange
559 734 self.xmax += self.timerange
560 735
561 736
562 737
563 738
564 739 class WindProfilerPlot_(Figure):
565 740
566 741 __isConfig = None
567 742 __nsubplots = None
568 743
569 744 WIDTHPROF = None
570 745 HEIGHTPROF = None
571 746 PREFIX = 'wind'
572 747
573 748 def __init__(self, **kwargs):
574 749 Figure.__init__(self, **kwargs)
575 750 self.timerange = None
576 751 self.isConfig = False
577 752 self.__nsubplots = 1
578 753
579 754 self.WIDTH = 800
580 755 self.HEIGHT = 300
581 756 self.WIDTHPROF = 120
582 757 self.HEIGHTPROF = 0
583 758 self.counter_imagwr = 0
584 759
585 760 self.PLOT_CODE = WIND_CODE
586 761
587 762 self.FTP_WEI = None
588 763 self.EXP_CODE = None
589 764 self.SUB_EXP_CODE = None
590 765 self.PLOT_POS = None
591 766 self.tmin = None
592 767 self.tmax = None
593 768
594 769 self.xmin = None
595 770 self.xmax = None
596 771
597 772 self.figfile = None
598 773
599 774 def getSubplots(self):
600 775
601 776 ncol = 1
602 777 nrow = self.nplots
603 778
604 779 return nrow, ncol
605 780
606 781 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
607 782
608 783 self.__showprofile = showprofile
609 784 self.nplots = nplots
610 785
611 786 ncolspan = 1
612 787 colspan = 1
613 788
614 789 self.createFigure(id = id,
615 790 wintitle = wintitle,
616 791 widthplot = self.WIDTH + self.WIDTHPROF,
617 792 heightplot = self.HEIGHT + self.HEIGHTPROF,
618 793 show=show)
619 794
620 795 nrow, ncol = self.getSubplots()
621 796
622 797 counter = 0
623 798 for y in range(nrow):
624 799 if counter >= self.nplots:
625 800 break
626 801
627 802 self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1)
628 803 counter += 1
629 804
630 805 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='False',
631 806 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
632 807 zmax_ver = None, zmin_ver = None, SNRmin = None, SNRmax = None,
633 808 timerange=None, SNRthresh = None,
634 809 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
635 810 server=None, folder=None, username=None, password=None,
636 811 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
637 812 """
638 813
639 814 Input:
640 815 dataOut :
641 816 id :
642 817 wintitle :
643 818 channelList :
644 819 showProfile :
645 820 xmin : None,
646 821 xmax : None,
647 822 ymin : None,
648 823 ymax : None,
649 824 zmin : None,
650 825 zmax : None
651 826 """
652 827
653 828 # if timerange is not None:
654 829 # self.timerange = timerange
655 830 #
656 831 # tmin = None
657 832 # tmax = None
658 833
659 834 x = dataOut.getTimeRange1(dataOut.paramInterval)
660 y = dataOut.heightList
835 y = dataOut.heightList
661 836 z = dataOut.data_output.copy()
662 837 nplots = z.shape[0] #Number of wind dimensions estimated
663 838 nplotsw = nplots
664 839
665 840
666 841 #If there is a SNR function defined
667 842 if dataOut.data_SNR is not None:
668 843 nplots += 1
669 SNR = dataOut.data_SNR
670 SNRavg = numpy.average(SNR, axis=0)
844 SNR = dataOut.data_SNR[0]
845 SNRavg = SNR#numpy.average(SNR, axis=0)
671 846
672 847 SNRdB = 10*numpy.log10(SNR)
673 848 SNRavgdB = 10*numpy.log10(SNRavg)
674 849
675 if SNRthresh == None: SNRthresh = -5.0
850 if SNRthresh == None:
851 SNRthresh = -5.0
676 852 ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0]
677 853
678 854 for i in range(nplotsw):
679 855 z[i,ind] = numpy.nan
680 856
681 857 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
682 858 #thisDatetime = datetime.datetime.now()
683 859 title = wintitle + "Wind"
684 860 xlabel = ""
685 861 ylabel = "Height (km)"
686 862 update_figfile = False
687 863
688 864 if not self.isConfig:
689 865
690 866 self.setup(id=id,
691 867 nplots=nplots,
692 868 wintitle=wintitle,
693 869 showprofile=showprofile,
694 870 show=show)
695 871
696 872 if timerange is not None:
697 873 self.timerange = timerange
698 874
699 875 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
700 876
701 877 if ymin == None: ymin = numpy.nanmin(y)
702 878 if ymax == None: ymax = numpy.nanmax(y)
703 879
704 880 if zmax == None: zmax = numpy.nanmax(abs(z[list(range(2)),:]))
705 881 #if numpy.isnan(zmax): zmax = 50
706 882 if zmin == None: zmin = -zmax
707 883
708 884 if nplotsw == 3:
709 885 if zmax_ver == None: zmax_ver = numpy.nanmax(abs(z[2,:]))
710 886 if zmin_ver == None: zmin_ver = -zmax_ver
711 887
712 888 if dataOut.data_SNR is not None:
713 889 if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB)
714 890 if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB)
715 891
716 892
717 893 self.FTP_WEI = ftp_wei
718 894 self.EXP_CODE = exp_code
719 895 self.SUB_EXP_CODE = sub_exp_code
720 896 self.PLOT_POS = plot_pos
721 897
722 898 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
723 899 self.isConfig = True
724 900 self.figfile = figfile
725 901 update_figfile = True
726 902
727 903 self.setWinTitle(title)
728 904
729 905 if ((self.xmax - x[1]) < (x[1]-x[0])):
730 906 x[1] = self.xmax
731 907
732 908 strWind = ['Zonal', 'Meridional', 'Vertical']
733 909 strCb = ['Velocity (m/s)','Velocity (m/s)','Velocity (cm/s)']
734 910 zmaxVector = [zmax, zmax, zmax_ver]
735 911 zminVector = [zmin, zmin, zmin_ver]
736 912 windFactor = [1,1,100]
737 913
738 914 for i in range(nplotsw):
739 915
740 916 title = "%s Wind: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
741 917 axes = self.axesList[i*self.__nsubplots]
742 918
743 919 z1 = z[i,:].reshape((1,-1))*windFactor[i]
744 #z1=numpy.ma.masked_where(z1==0.,z1)
745
920
746 921 axes.pcolorbuffer(x, y, z1,
747 922 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i],
748 923 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
749 924 ticksize=9, cblabel=strCb[i], cbsize="1%", colormap="seismic" )
750 925
751 926 if dataOut.data_SNR is not None:
752 927 i += 1
753 928 title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
754 929 axes = self.axesList[i*self.__nsubplots]
755 930 SNRavgdB = SNRavgdB.reshape((1,-1))
756 931 axes.pcolorbuffer(x, y, SNRavgdB,
757 932 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
758 933 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
759 934 ticksize=9, cblabel='', cbsize="1%", colormap="jet")
760 935
761 936 self.draw()
762 937
763 938 self.save(figpath=figpath,
764 939 figfile=figfile,
765 940 save=save,
766 941 ftp=ftp,
767 942 wr_period=wr_period,
768 943 thisDatetime=thisDatetime,
769 944 update_figfile=update_figfile)
770 945
771 946 if dataOut.ltctime + dataOut.paramInterval >= self.xmax:
772 947 self.counter_imagwr = wr_period
773 948 self.isConfig = False
774 949 update_figfile = True
775 950
776 951 @MPDecorator
777 952 class ParametersPlot_(Figure):
778 953
779 954 __isConfig = None
780 955 __nsubplots = None
781 956
782 957 WIDTHPROF = None
783 958 HEIGHTPROF = None
784 959 PREFIX = 'param'
785 960
786 961 nplots = None
787 962 nchan = None
788 963
789 964 def __init__(self):#, **kwargs):
790 965 Figure.__init__(self)#, **kwargs)
791 966 self.timerange = None
792 967 self.isConfig = False
793 968 self.__nsubplots = 1
794 969
795 self.WIDTH = 800
796 self.HEIGHT = 180
970 self.WIDTH = 300
971 self.HEIGHT = 550
797 972 self.WIDTHPROF = 120
798 973 self.HEIGHTPROF = 0
799 974 self.counter_imagwr = 0
800 975
801 976 self.PLOT_CODE = RTI_CODE
802 977
803 978 self.FTP_WEI = None
804 979 self.EXP_CODE = None
805 980 self.SUB_EXP_CODE = None
806 981 self.PLOT_POS = None
807 982 self.tmin = None
808 983 self.tmax = None
809 984
810 985 self.xmin = None
811 986 self.xmax = None
812 987
813 988 self.figfile = None
814 989
815 990 def getSubplots(self):
816 991
817 992 ncol = 1
818 993 nrow = self.nplots
819 994
820 995 return nrow, ncol
821 996
822 997 def setup(self, id, nplots, wintitle, show=True):
823 998
824 999 self.nplots = nplots
825 1000
826 1001 ncolspan = 1
827 1002 colspan = 1
828 1003
829 1004 self.createFigure(id = id,
830 1005 wintitle = wintitle,
831 1006 widthplot = self.WIDTH + self.WIDTHPROF,
832 1007 heightplot = self.HEIGHT + self.HEIGHTPROF,
833 1008 show=show)
834 1009
835 1010 nrow, ncol = self.getSubplots()
836 1011
837 1012 counter = 0
838 1013 for y in range(nrow):
839 1014 for x in range(ncol):
840 1015
841 1016 if counter >= self.nplots:
842 1017 break
843 1018
844 1019 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
845 1020
846 1021 counter += 1
847 1022
848 1023 def run(self, dataOut, id, wintitle="", channelList=None, paramIndex = 0, colormap="jet",
849 1024 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None, timerange=None,
850 1025 showSNR=False, SNRthresh = -numpy.inf, SNRmin=None, SNRmax=None,
851 1026 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
852 1027 server=None, folder=None, username=None, password=None,
853 1028 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, HEIGHT=None):
854 1029 """
855 1030
856 1031 Input:
857 1032 dataOut :
858 1033 id :
859 1034 wintitle :
860 1035 channelList :
861 1036 showProfile :
862 1037 xmin : None,
863 1038 xmax : None,
864 1039 ymin : None,
865 1040 ymax : None,
866 1041 zmin : None,
867 1042 zmax : None
868 1043 """
869 1044 if dataOut.flagNoData:
870 1045 return dataOut
871 1046
872 1047
873 1048 if HEIGHT is not None:
874 1049 self.HEIGHT = HEIGHT
875 1050
876 1051
877 1052 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
878 1053 return
879 1054
880 1055 if channelList == None:
881 1056 channelIndexList = list(range(dataOut.data_param.shape[0]))
882 1057 else:
883 1058 channelIndexList = []
884 1059 for channel in channelList:
885 1060 if channel not in dataOut.channelList:
886 1061 raise ValueError("Channel %d is not in dataOut.channelList")
887 1062 channelIndexList.append(dataOut.channelList.index(channel))
888 1063
889 1064 x = dataOut.getTimeRange1(dataOut.paramInterval)
890 1065 y = dataOut.getHeiRange()
891 1066
892 1067 if dataOut.data_param.ndim == 3:
893 1068 z = dataOut.data_param[channelIndexList,paramIndex,:]
894 1069 else:
895 1070 z = dataOut.data_param[channelIndexList,:]
896 1071
897 1072 if showSNR:
898 1073 #SNR data
899 1074 SNRarray = dataOut.data_SNR[channelIndexList,:]
900 1075 SNRdB = 10*numpy.log10(SNRarray)
901 1076 ind = numpy.where(SNRdB < SNRthresh)
902 1077 z[ind] = numpy.nan
903 1078
904 1079 thisDatetime = dataOut.datatime
905 1080 # thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
906 1081 title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
907 1082 xlabel = ""
908 ylabel = "Range (Km)"
1083 ylabel = "Range (km)"
909 1084
910 1085 update_figfile = False
911 1086
912 1087 if not self.isConfig:
913 1088
914 1089 nchan = len(channelIndexList)
915 1090 self.nchan = nchan
916 1091 self.plotFact = 1
917 1092 nplots = nchan
918 1093
919 1094 if showSNR:
920 1095 nplots = nchan*2
921 1096 self.plotFact = 2
922 1097 if SNRmin == None: SNRmin = numpy.nanmin(SNRdB)
923 1098 if SNRmax == None: SNRmax = numpy.nanmax(SNRdB)
924 1099
925 1100 self.setup(id=id,
926 1101 nplots=nplots,
927 1102 wintitle=wintitle,
928 1103 show=show)
929 1104
930 1105 if timerange != None:
931 1106 self.timerange = timerange
932 1107
933 1108 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
934 1109
935 1110 if ymin == None: ymin = numpy.nanmin(y)
936 1111 if ymax == None: ymax = numpy.nanmax(y)
937 1112 if zmin == None: zmin = numpy.nanmin(z)
938 1113 if zmax == None: zmax = numpy.nanmax(z)
939 1114
940 1115 self.FTP_WEI = ftp_wei
941 1116 self.EXP_CODE = exp_code
942 1117 self.SUB_EXP_CODE = sub_exp_code
943 1118 self.PLOT_POS = plot_pos
944 1119
945 1120 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
946 1121 self.isConfig = True
947 1122 self.figfile = figfile
948 1123 update_figfile = True
949 1124
950 1125 self.setWinTitle(title)
951 1126
952 for i in range(self.nchan):
953 index = channelIndexList[i]
954 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
955 axes = self.axesList[i*self.plotFact]
956 z1 = z[i,:].reshape((1,-1))
957 axes.pcolorbuffer(x, y, z1,
958 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
959 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
960 ticksize=9, cblabel='', cbsize="1%",colormap=colormap)
961
962 if showSNR:
963 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
964 axes = self.axesList[i*self.plotFact + 1]
965 SNRdB1 = SNRdB[i,:].reshape((1,-1))
966 axes.pcolorbuffer(x, y, SNRdB1,
967 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
968 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
969 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
1127 # for i in range(self.nchan):
1128 # index = channelIndexList[i]
1129 # title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1130 # axes = self.axesList[i*self.plotFact]
1131 # z1 = z[i,:].reshape((1,-1))
1132 # axes.pcolorbuffer(x, y, z1,
1133 # xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1134 # xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1135 # ticksize=9, cblabel='', cbsize="1%",colormap=colormap)
1136 #
1137 # if showSNR:
1138 # title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1139 # axes = self.axesList[i*self.plotFact + 1]
1140 # SNRdB1 = SNRdB[i,:].reshape((1,-1))
1141 # axes.pcolorbuffer(x, y, SNRdB1,
1142 # xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1143 # xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1144 # ticksize=9, cblabel='', cbsize="1%",colormap='jet')
1145
1146 i=0
1147 index = channelIndexList[i]
1148 title = "Factor de reflectividad Z [dBZ]"
1149 axes = self.axesList[i*self.plotFact]
1150 z1 = z[i,:].reshape((1,-1))
1151 axes.pcolorbuffer(x, y, z1,
1152 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1153 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1154 ticksize=9, cblabel='', cbsize="1%",colormap=colormap)
1155
1156 if showSNR:
1157 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1158 axes = self.axesList[i*self.plotFact + 1]
1159 SNRdB1 = SNRdB[i,:].reshape((1,-1))
1160 axes.pcolorbuffer(x, y, SNRdB1,
1161 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1162 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1163 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
1164
1165 i=1
1166 index = channelIndexList[i]
1167 title = "Velocidad vertical Doppler [m/s]"
1168 axes = self.axesList[i*self.plotFact]
1169 z1 = z[i,:].reshape((1,-1))
1170 axes.pcolorbuffer(x, y, z1,
1171 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=-10, zmax=10,
1172 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1173 ticksize=9, cblabel='', cbsize="1%",colormap='seismic_r')
1174
1175 if showSNR:
1176 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1177 axes = self.axesList[i*self.plotFact + 1]
1178 SNRdB1 = SNRdB[i,:].reshape((1,-1))
1179 axes.pcolorbuffer(x, y, SNRdB1,
1180 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1181 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1182 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
1183
1184 i=2
1185 index = channelIndexList[i]
1186 title = "Intensidad de lluvia [mm/h]"
1187 axes = self.axesList[i*self.plotFact]
1188 z1 = z[i,:].reshape((1,-1))
1189 axes.pcolorbuffer(x, y, z1,
1190 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=0, zmax=40,
1191 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1192 ticksize=9, cblabel='', cbsize="1%",colormap='ocean_r')
1193
1194 if showSNR:
1195 title = "Channel %d SNR: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1196 axes = self.axesList[i*self.plotFact + 1]
1197 SNRdB1 = SNRdB[i,:].reshape((1,-1))
1198 axes.pcolorbuffer(x, y, SNRdB1,
1199 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1200 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1201 ticksize=9, cblabel='', cbsize="1%",colormap='jet')
970 1202
971 1203
972 1204 self.draw()
973 1205
974 1206 if dataOut.ltctime >= self.xmax:
975 1207 self.counter_imagwr = wr_period
976 1208 self.isConfig = False
977 1209 update_figfile = True
978 1210
979 1211 self.save(figpath=figpath,
980 1212 figfile=figfile,
981 1213 save=save,
982 1214 ftp=ftp,
983 1215 wr_period=wr_period,
984 1216 thisDatetime=thisDatetime,
985 1217 update_figfile=update_figfile)
986 1218
987 1219 return dataOut
988 1220 @MPDecorator
989 1221 class Parameters1Plot_(Figure):
990 1222
991 1223 __isConfig = None
992 1224 __nsubplots = None
993 1225
994 1226 WIDTHPROF = None
995 1227 HEIGHTPROF = None
996 1228 PREFIX = 'prm'
997 1229
998 1230 def __init__(self):
999 1231 Figure.__init__(self)
1000 1232 self.timerange = 2*60*60
1001 1233 self.isConfig = False
1002 1234 self.__nsubplots = 1
1003 1235
1004 1236 self.WIDTH = 800
1005 1237 self.HEIGHT = 180
1006 1238 self.WIDTHPROF = 120
1007 1239 self.HEIGHTPROF = 0
1008 1240 self.counter_imagwr = 0
1009 1241
1010 1242 self.PLOT_CODE = PARMS_CODE
1011 1243
1012 1244 self.FTP_WEI = None
1013 1245 self.EXP_CODE = None
1014 1246 self.SUB_EXP_CODE = None
1015 1247 self.PLOT_POS = None
1016 1248 self.tmin = None
1017 1249 self.tmax = None
1018 1250
1019 1251 self.xmin = None
1020 1252 self.xmax = None
1021 1253
1022 1254 self.figfile = None
1023 1255
1024 1256 def getSubplots(self):
1025 1257
1026 1258 ncol = 1
1027 1259 nrow = self.nplots
1028 1260
1029 1261 return nrow, ncol
1030 1262
1031 1263 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1032 1264
1033 1265 self.__showprofile = showprofile
1034 1266 self.nplots = nplots
1035 1267
1036 1268 ncolspan = 1
1037 1269 colspan = 1
1038 1270
1039 1271 self.createFigure(id = id,
1040 1272 wintitle = wintitle,
1041 1273 widthplot = self.WIDTH + self.WIDTHPROF,
1042 1274 heightplot = self.HEIGHT + self.HEIGHTPROF,
1043 1275 show=show)
1044 1276
1045 1277 nrow, ncol = self.getSubplots()
1046 1278
1047 1279 counter = 0
1048 1280 for y in range(nrow):
1049 1281 for x in range(ncol):
1050 1282
1051 1283 if counter >= self.nplots:
1052 1284 break
1053 1285
1054 1286 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1055 1287
1056 1288 if showprofile:
1057 1289 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
1058 1290
1059 1291 counter += 1
1060 1292
1061 1293 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=False,
1062 1294 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,timerange=None,
1063 1295 parameterIndex = None, onlyPositive = False,
1064 1296 SNRthresh = -numpy.inf, SNR = True, SNRmin = None, SNRmax = None, onlySNR = False,
1065 1297 DOP = True,
1066 1298 zlabel = "", parameterName = "", parameterObject = "data_param",
1067 1299 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
1068 1300 server=None, folder=None, username=None, password=None,
1069 1301 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1070 #print inspect.getargspec(self.run).args
1071 """
1072 1302
1303 """
1073 1304 Input:
1074 1305 dataOut :
1075 1306 id :
1076 1307 wintitle :
1077 1308 channelList :
1078 1309 showProfile :
1079 1310 xmin : None,
1080 1311 xmax : None,
1081 1312 ymin : None,
1082 1313 ymax : None,
1083 1314 zmin : None,
1084 1315 zmax : None
1085 1316 """
1086 1317 if dataOut.flagNoData:
1087 1318 return dataOut
1088 1319
1089 1320 data_param = getattr(dataOut, parameterObject)
1090 1321
1091 1322 if channelList == None:
1092 1323 channelIndexList = numpy.arange(data_param.shape[0])
1093 1324 else:
1094 1325 channelIndexList = numpy.array(channelList)
1095 1326
1096 1327 nchan = len(channelIndexList) #Number of channels being plotted
1097 1328
1098 1329 if nchan < 1:
1099 1330 return
1100 1331
1101 1332 nGraphsByChannel = 0
1102 1333
1103 1334 if SNR:
1104 1335 nGraphsByChannel += 1
1105 1336 if DOP:
1106 1337 nGraphsByChannel += 1
1107 1338
1108 1339 if nGraphsByChannel < 1:
1109 1340 return
1110 1341
1111 1342 nplots = nGraphsByChannel*nchan
1112 1343
1113 1344 if timerange is not None:
1114 1345 self.timerange = timerange
1115 1346
1116 1347 #tmin = None
1117 1348 #tmax = None
1118 1349 if parameterIndex == None:
1119 1350 parameterIndex = 1
1120 1351
1121 1352 x = dataOut.getTimeRange1(dataOut.paramInterval)
1122 1353 y = dataOut.heightList
1123 1354
1124 1355 if dataOut.data_param.ndim == 3:
1125 1356 z = dataOut.data_param[channelIndexList,parameterIndex,:]
1126 1357 else:
1127 1358 z = dataOut.data_param[channelIndexList,:]
1128 1359
1129 1360 if dataOut.data_SNR is not None:
1130 1361 if dataOut.data_SNR.ndim == 2:
1131 1362 SNRavg = numpy.average(dataOut.data_SNR, axis=0)
1132 1363 else:
1133 1364 SNRavg = dataOut.data_SNR
1134 1365 SNRdB = 10*numpy.log10(SNRavg)
1135 1366
1136 1367 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1137 1368 title = wintitle + " Parameters Plot" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
1138 1369 xlabel = ""
1139 1370 ylabel = "Range (Km)"
1140 1371
1141 1372 if onlyPositive:
1142 1373 colormap = "jet"
1143 1374 zmin = 0
1144 1375 else: colormap = "RdBu_r"
1145 1376
1146 1377 if not self.isConfig:
1147 1378
1148 1379 self.setup(id=id,
1149 1380 nplots=nplots,
1150 1381 wintitle=wintitle,
1151 1382 showprofile=showprofile,
1152 1383 show=show)
1153 1384
1154 1385 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1155 1386
1156 1387 if ymin == None: ymin = numpy.nanmin(y)
1157 1388 if ymax == None: ymax = numpy.nanmax(y)
1158 1389 if zmin == None: zmin = numpy.nanmin(z)
1159 1390 if zmax == None: zmax = numpy.nanmax(z)
1160 1391
1161 1392 if SNR:
1162 1393 if SNRmin == None: SNRmin = numpy.nanmin(SNRdB)
1163 1394 if SNRmax == None: SNRmax = numpy.nanmax(SNRdB)
1164 1395
1165 1396 self.FTP_WEI = ftp_wei
1166 1397 self.EXP_CODE = exp_code
1167 1398 self.SUB_EXP_CODE = sub_exp_code
1168 1399 self.PLOT_POS = plot_pos
1169 1400
1170 1401 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1171 1402 self.isConfig = True
1172 1403 self.figfile = figfile
1173 1404
1174 1405 self.setWinTitle(title)
1175 1406
1176 1407 if ((self.xmax - x[1]) < (x[1]-x[0])):
1177 1408 x[1] = self.xmax
1178 1409
1179 1410 for i in range(nchan):
1180 1411
1181 1412 if (SNR and not onlySNR): j = 2*i
1182 1413 else: j = i
1183 1414
1184 1415 j = nGraphsByChannel*i
1185 1416
1186 1417 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1187 1418 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
1188 1419
1189 1420 if not onlySNR:
1190 1421 axes = self.axesList[j*self.__nsubplots]
1191 1422 z1 = z[i,:].reshape((1,-1))
1192 1423 axes.pcolorbuffer(x, y, z1,
1193 1424 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1194 1425 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap,
1195 1426 ticksize=9, cblabel=zlabel, cbsize="1%")
1196 1427
1197 1428 if DOP:
1198 1429 title = "%s Channel %d: %s" %(parameterName, channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1199 1430
1200 1431 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1201 1432 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
1202 1433 axes = self.axesList[j]
1203 1434 z1 = z[i,:].reshape((1,-1))
1204 1435 axes.pcolorbuffer(x, y, z1,
1205 1436 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
1206 1437 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap=colormap,
1207 1438 ticksize=9, cblabel=zlabel, cbsize="1%")
1208 1439
1209 1440 if SNR:
1210 1441 title = "Channel %d Signal Noise Ratio (SNR): %s" %(channelIndexList[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1211 1442 axes = self.axesList[(j)*self.__nsubplots]
1212 1443 if not onlySNR:
1213 1444 axes = self.axesList[(j + 1)*self.__nsubplots]
1214 1445
1215 1446 axes = self.axesList[(j + nGraphsByChannel-1)]
1216 1447 z1 = SNRdB.reshape((1,-1))
1217 1448 axes.pcolorbuffer(x, y, z1,
1218 1449 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1219 1450 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,colormap="jet",
1220 1451 ticksize=9, cblabel=zlabel, cbsize="1%")
1221 1452
1222 1453
1223 1454
1224 1455 self.draw()
1225 1456
1226 1457 if x[1] >= self.axesList[0].xmax:
1227 1458 self.counter_imagwr = wr_period
1228 1459 self.isConfig = False
1229 1460 self.figfile = None
1230 1461
1231 1462 self.save(figpath=figpath,
1232 1463 figfile=figfile,
1233 1464 save=save,
1234 1465 ftp=ftp,
1235 1466 wr_period=wr_period,
1236 1467 thisDatetime=thisDatetime,
1237 1468 update_figfile=False)
1238 1469 return dataOut
1239 1470
1240 1471 class SpectralFittingPlot_(Figure):
1241 1472
1242 1473 __isConfig = None
1243 1474 __nsubplots = None
1244 1475
1245 1476 WIDTHPROF = None
1246 1477 HEIGHTPROF = None
1247 1478 PREFIX = 'prm'
1248 1479
1249 1480
1250 1481 N = None
1251 1482 ippSeconds = None
1252 1483
1253 1484 def __init__(self, **kwargs):
1254 1485 Figure.__init__(self, **kwargs)
1255 1486 self.isConfig = False
1256 1487 self.__nsubplots = 1
1257 1488
1258 1489 self.PLOT_CODE = SPECFIT_CODE
1259 1490
1260 1491 self.WIDTH = 450
1261 1492 self.HEIGHT = 250
1262 1493 self.WIDTHPROF = 0
1263 1494 self.HEIGHTPROF = 0
1264 1495
1265 1496 def getSubplots(self):
1266 1497
1267 1498 ncol = int(numpy.sqrt(self.nplots)+0.9)
1268 1499 nrow = int(self.nplots*1./ncol + 0.9)
1269 1500
1270 1501 return nrow, ncol
1271 1502
1272 1503 def setup(self, id, nplots, wintitle, showprofile=False, show=True):
1273 1504
1274 1505 showprofile = False
1275 1506 self.__showprofile = showprofile
1276 1507 self.nplots = nplots
1277 1508
1278 1509 ncolspan = 5
1279 1510 colspan = 4
1280 1511 if showprofile:
1281 1512 ncolspan = 5
1282 1513 colspan = 4
1283 1514 self.__nsubplots = 2
1284 1515
1285 1516 self.createFigure(id = id,
1286 1517 wintitle = wintitle,
1287 1518 widthplot = self.WIDTH + self.WIDTHPROF,
1288 1519 heightplot = self.HEIGHT + self.HEIGHTPROF,
1289 1520 show=show)
1290 1521
1291 1522 nrow, ncol = self.getSubplots()
1292 1523
1293 1524 counter = 0
1294 1525 for y in range(nrow):
1295 1526 for x in range(ncol):
1296 1527
1297 1528 if counter >= self.nplots:
1298 1529 break
1299 1530
1300 1531 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1301 1532
1302 1533 if showprofile:
1303 1534 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
1304 1535
1305 1536 counter += 1
1306 1537
1307 1538 def run(self, dataOut, id, cutHeight=None, fit=False, wintitle="", channelList=None, showprofile=True,
1308 1539 xmin=None, xmax=None, ymin=None, ymax=None,
1309 1540 save=False, figpath='./', figfile=None, show=True):
1310 1541
1311 1542 """
1312 1543
1313 1544 Input:
1314 1545 dataOut :
1315 1546 id :
1316 1547 wintitle :
1317 1548 channelList :
1318 1549 showProfile :
1319 1550 xmin : None,
1320 1551 xmax : None,
1321 1552 zmin : None,
1322 1553 zmax : None
1323 1554 """
1324 1555
1325 1556 if cutHeight==None:
1326 1557 h=270
1327 1558 heightindex = numpy.abs(cutHeight - dataOut.heightList).argmin()
1328 1559 cutHeight = dataOut.heightList[heightindex]
1329 1560
1330 1561 factor = dataOut.normFactor
1331 1562 x = dataOut.abscissaList[:-1]
1332 1563 #y = dataOut.getHeiRange()
1333 1564
1334 1565 z = dataOut.data_pre[:,:,heightindex]/factor
1335 1566 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1336 1567 avg = numpy.average(z, axis=1)
1337 1568 listChannels = z.shape[0]
1338 1569
1339 1570 #Reconstruct Function
1340 1571 if fit==True:
1341 1572 groupArray = dataOut.groupList
1342 1573 listChannels = groupArray.reshape((groupArray.size))
1343 1574 listChannels.sort()
1344 1575 spcFitLine = numpy.zeros(z.shape)
1345 1576 constants = dataOut.constants
1346 1577
1347 1578 nGroups = groupArray.shape[0]
1348 1579 nChannels = groupArray.shape[1]
1349 1580 nProfiles = z.shape[1]
1350 1581
1351 1582 for f in range(nGroups):
1352 1583 groupChann = groupArray[f,:]
1353 1584 p = dataOut.data_param[f,:,heightindex]
1354 1585 # p = numpy.array([ 89.343967,0.14036615,0.17086219,18.89835291,1.58388365,1.55099167])
1355 1586 fitLineAux = dataOut.library.modelFunction(p, constants)*nProfiles
1356 1587 fitLineAux = fitLineAux.reshape((nChannels,nProfiles))
1357 1588 spcFitLine[groupChann,:] = fitLineAux
1358 1589 # spcFitLine = spcFitLine/factor
1359 1590
1360 1591 z = z[listChannels,:]
1361 1592 spcFitLine = spcFitLine[listChannels,:]
1362 1593 spcFitLinedB = 10*numpy.log10(spcFitLine)
1363 1594
1364 1595 zdB = 10*numpy.log10(z)
1365 1596 #thisDatetime = dataOut.datatime
1366 1597 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1367 1598 title = wintitle + " Doppler Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1368 1599 xlabel = "Velocity (m/s)"
1369 1600 ylabel = "Spectrum"
1370 1601
1371 1602 if not self.isConfig:
1372 1603
1373 1604 nplots = listChannels.size
1374 1605
1375 1606 self.setup(id=id,
1376 1607 nplots=nplots,
1377 1608 wintitle=wintitle,
1378 1609 showprofile=showprofile,
1379 1610 show=show)
1380 1611
1381 1612 if xmin == None: xmin = numpy.nanmin(x)
1382 1613 if xmax == None: xmax = numpy.nanmax(x)
1383 1614 if ymin == None: ymin = numpy.nanmin(zdB)
1384 1615 if ymax == None: ymax = numpy.nanmax(zdB)+2
1385 1616
1386 1617 self.isConfig = True
1387 1618
1388 1619 self.setWinTitle(title)
1389 1620 for i in range(self.nplots):
1390 1621 # title = "Channel %d: %4.2fdB" %(dataOut.channelList[i]+1, noisedB[i])
1391 1622 title = "Height %4.1f km\nChannel %d:" %(cutHeight, listChannels[i])
1392 1623 axes = self.axesList[i*self.__nsubplots]
1393 1624 if fit == False:
1394 1625 axes.pline(x, zdB[i,:],
1395 1626 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1396 1627 xlabel=xlabel, ylabel=ylabel, title=title
1397 1628 )
1398 1629 if fit == True:
1399 1630 fitline=spcFitLinedB[i,:]
1400 1631 y=numpy.vstack([zdB[i,:],fitline] )
1401 1632 legendlabels=['Data','Fitting']
1402 1633 axes.pmultilineyaxis(x, y,
1403 1634 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1404 1635 xlabel=xlabel, ylabel=ylabel, title=title,
1405 1636 legendlabels=legendlabels, marker=None,
1406 1637 linestyle='solid', grid='both')
1407 1638
1408 1639 self.draw()
1409 1640
1410 1641 self.save(figpath=figpath,
1411 1642 figfile=figfile,
1412 1643 save=save,
1413 1644 ftp=ftp,
1414 1645 wr_period=wr_period,
1415 1646 thisDatetime=thisDatetime)
1416 1647
1417 1648
1418 1649 class EWDriftsPlot_(Figure):
1419 1650
1420 1651 __isConfig = None
1421 1652 __nsubplots = None
1422 1653
1423 1654 WIDTHPROF = None
1424 1655 HEIGHTPROF = None
1425 1656 PREFIX = 'drift'
1426 1657
1427 1658 def __init__(self, **kwargs):
1428 1659 Figure.__init__(self, **kwargs)
1429 1660 self.timerange = 2*60*60
1430 1661 self.isConfig = False
1431 1662 self.__nsubplots = 1
1432 1663
1433 1664 self.WIDTH = 800
1434 1665 self.HEIGHT = 150
1435 1666 self.WIDTHPROF = 120
1436 1667 self.HEIGHTPROF = 0
1437 1668 self.counter_imagwr = 0
1438 1669
1439 1670 self.PLOT_CODE = EWDRIFT_CODE
1440 1671
1441 1672 self.FTP_WEI = None
1442 1673 self.EXP_CODE = None
1443 1674 self.SUB_EXP_CODE = None
1444 1675 self.PLOT_POS = None
1445 1676 self.tmin = None
1446 1677 self.tmax = None
1447 1678
1448 1679 self.xmin = None
1449 1680 self.xmax = None
1450 1681
1451 1682 self.figfile = None
1452 1683
1453 1684 def getSubplots(self):
1454 1685
1455 1686 ncol = 1
1456 1687 nrow = self.nplots
1457 1688
1458 1689 return nrow, ncol
1459 1690
1460 1691 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1461 1692
1462 1693 self.__showprofile = showprofile
1463 1694 self.nplots = nplots
1464 1695
1465 1696 ncolspan = 1
1466 1697 colspan = 1
1467 1698
1468 1699 self.createFigure(id = id,
1469 1700 wintitle = wintitle,
1470 1701 widthplot = self.WIDTH + self.WIDTHPROF,
1471 1702 heightplot = self.HEIGHT + self.HEIGHTPROF,
1472 1703 show=show)
1473 1704
1474 1705 nrow, ncol = self.getSubplots()
1475 1706
1476 1707 counter = 0
1477 1708 for y in range(nrow):
1478 1709 if counter >= self.nplots:
1479 1710 break
1480 1711
1481 1712 self.addAxes(nrow, ncol*ncolspan, y, 0, colspan, 1)
1482 1713 counter += 1
1483 1714
1484 1715 def run(self, dataOut, id, wintitle="", channelList=None,
1485 1716 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
1486 1717 zmaxVertical = None, zminVertical = None, zmaxZonal = None, zminZonal = None,
1487 1718 timerange=None, SNRthresh = -numpy.inf, SNRmin = None, SNRmax = None, SNR_1 = False,
1488 1719 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
1489 1720 server=None, folder=None, username=None, password=None,
1490 1721 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1491 1722 """
1492 1723
1493 1724 Input:
1494 1725 dataOut :
1495 1726 id :
1496 1727 wintitle :
1497 1728 channelList :
1498 1729 showProfile :
1499 1730 xmin : None,
1500 1731 xmax : None,
1501 1732 ymin : None,
1502 1733 ymax : None,
1503 1734 zmin : None,
1504 1735 zmax : None
1505 1736 """
1506 1737
1507 1738 if timerange is not None:
1508 1739 self.timerange = timerange
1509 1740
1510 1741 tmin = None
1511 1742 tmax = None
1512 1743
1513 1744 x = dataOut.getTimeRange1(dataOut.outputInterval)
1514 1745 # y = dataOut.heightList
1515 1746 y = dataOut.heightList
1516 1747
1517 1748 z = dataOut.data_output
1518 1749 nplots = z.shape[0] #Number of wind dimensions estimated
1519 1750 nplotsw = nplots
1520 1751
1521 1752 #If there is a SNR function defined
1522 1753 if dataOut.data_SNR is not None:
1523 1754 nplots += 1
1524 1755 SNR = dataOut.data_SNR
1525 1756
1526 1757 if SNR_1:
1527 1758 SNR += 1
1528 1759
1529 1760 SNRavg = numpy.average(SNR, axis=0)
1530 1761
1531 1762 SNRdB = 10*numpy.log10(SNR)
1532 1763 SNRavgdB = 10*numpy.log10(SNRavg)
1533 1764
1534 1765 ind = numpy.where(SNRavg < 10**(SNRthresh/10))[0]
1535 1766
1536 1767 for i in range(nplotsw):
1537 1768 z[i,ind] = numpy.nan
1538 1769
1539 1770
1540 1771 showprofile = False
1541 1772 # thisDatetime = dataOut.datatime
1542 1773 thisDatetime = datetime.datetime.utcfromtimestamp(x[1])
1543 1774 title = wintitle + " EW Drifts"
1544 1775 xlabel = ""
1545 1776 ylabel = "Height (Km)"
1546 1777
1547 1778 if not self.isConfig:
1548 1779
1549 1780 self.setup(id=id,
1550 1781 nplots=nplots,
1551 1782 wintitle=wintitle,
1552 1783 showprofile=showprofile,
1553 1784 show=show)
1554 1785
1555 1786 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1556 1787
1557 1788 if ymin == None: ymin = numpy.nanmin(y)
1558 1789 if ymax == None: ymax = numpy.nanmax(y)
1559 1790
1560 1791 if zmaxZonal == None: zmaxZonal = numpy.nanmax(abs(z[0,:]))
1561 1792 if zminZonal == None: zminZonal = -zmaxZonal
1562 1793 if zmaxVertical == None: zmaxVertical = numpy.nanmax(abs(z[1,:]))
1563 1794 if zminVertical == None: zminVertical = -zmaxVertical
1564 1795
1565 1796 if dataOut.data_SNR is not None:
1566 1797 if SNRmin == None: SNRmin = numpy.nanmin(SNRavgdB)
1567 1798 if SNRmax == None: SNRmax = numpy.nanmax(SNRavgdB)
1568 1799
1569 1800 self.FTP_WEI = ftp_wei
1570 1801 self.EXP_CODE = exp_code
1571 1802 self.SUB_EXP_CODE = sub_exp_code
1572 1803 self.PLOT_POS = plot_pos
1573 1804
1574 1805 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1575 1806 self.isConfig = True
1576 1807
1577 1808
1578 1809 self.setWinTitle(title)
1579 1810
1580 1811 if ((self.xmax - x[1]) < (x[1]-x[0])):
1581 1812 x[1] = self.xmax
1582 1813
1583 1814 strWind = ['Zonal','Vertical']
1584 1815 strCb = 'Velocity (m/s)'
1585 1816 zmaxVector = [zmaxZonal, zmaxVertical]
1586 1817 zminVector = [zminZonal, zminVertical]
1587 1818
1588 1819 for i in range(nplotsw):
1589 1820
1590 1821 title = "%s Drifts: %s" %(strWind[i], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1591 1822 axes = self.axesList[i*self.__nsubplots]
1592 1823
1593 1824 z1 = z[i,:].reshape((1,-1))
1594 1825
1595 1826 axes.pcolorbuffer(x, y, z1,
1596 1827 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zminVector[i], zmax=zmaxVector[i],
1597 1828 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1598 1829 ticksize=9, cblabel=strCb, cbsize="1%", colormap="RdBu_r")
1599 1830
1600 1831 if dataOut.data_SNR is not None:
1601 1832 i += 1
1602 1833 if SNR_1:
1603 1834 title = "Signal Noise Ratio + 1 (SNR+1): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1604 1835 else:
1605 1836 title = "Signal Noise Ratio (SNR): %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1606 1837 axes = self.axesList[i*self.__nsubplots]
1607 1838 SNRavgdB = SNRavgdB.reshape((1,-1))
1608 1839
1609 1840 axes.pcolorbuffer(x, y, SNRavgdB,
1610 1841 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=SNRmin, zmax=SNRmax,
1611 1842 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
1612 1843 ticksize=9, cblabel='', cbsize="1%", colormap="jet")
1613 1844
1614 1845 self.draw()
1615 1846
1616 1847 if x[1] >= self.axesList[0].xmax:
1617 1848 self.counter_imagwr = wr_period
1618 1849 self.isConfig = False
1619 1850 self.figfile = None
1620 1851
1621 1852
1622 1853
1623 1854
1624 1855 class PhasePlot_(Figure):
1625 1856
1626 1857 __isConfig = None
1627 1858 __nsubplots = None
1628 1859
1629 1860 PREFIX = 'mphase'
1630 1861
1631 1862
1632 1863 def __init__(self, **kwargs):
1633 1864 Figure.__init__(self, **kwargs)
1634 1865 self.timerange = 24*60*60
1635 1866 self.isConfig = False
1636 1867 self.__nsubplots = 1
1637 1868 self.counter_imagwr = 0
1638 1869 self.WIDTH = 600
1639 1870 self.HEIGHT = 300
1640 1871 self.WIDTHPROF = 120
1641 1872 self.HEIGHTPROF = 0
1642 1873 self.xdata = None
1643 1874 self.ydata = None
1644 1875
1645 1876 self.PLOT_CODE = MPHASE_CODE
1646 1877
1647 1878 self.FTP_WEI = None
1648 1879 self.EXP_CODE = None
1649 1880 self.SUB_EXP_CODE = None
1650 1881 self.PLOT_POS = None
1651 1882
1652 1883
1653 1884 self.filename_phase = None
1654 1885
1655 1886 self.figfile = None
1656 1887
1657 1888 def getSubplots(self):
1658 1889
1659 1890 ncol = 1
1660 1891 nrow = 1
1661 1892
1662 1893 return nrow, ncol
1663 1894
1664 1895 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1665 1896
1666 1897 self.__showprofile = showprofile
1667 1898 self.nplots = nplots
1668 1899
1669 1900 ncolspan = 7
1670 1901 colspan = 6
1671 1902 self.__nsubplots = 2
1672 1903
1673 1904 self.createFigure(id = id,
1674 1905 wintitle = wintitle,
1675 1906 widthplot = self.WIDTH+self.WIDTHPROF,
1676 1907 heightplot = self.HEIGHT+self.HEIGHTPROF,
1677 1908 show=show)
1678 1909
1679 1910 nrow, ncol = self.getSubplots()
1680 1911
1681 1912 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1682 1913
1683 1914
1684 1915 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1685 1916 xmin=None, xmax=None, ymin=None, ymax=None,
1686 1917 timerange=None,
1687 1918 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1688 1919 server=None, folder=None, username=None, password=None,
1689 1920 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1690 1921
1691 1922
1692 1923 tmin = None
1693 1924 tmax = None
1694 1925 x = dataOut.getTimeRange1(dataOut.outputInterval)
1695 1926 y = dataOut.getHeiRange()
1696 1927
1697 1928
1698 1929 #thisDatetime = dataOut.datatime
1699 1930 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
1700 1931 title = wintitle + " Phase of Beacon Signal" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1701 1932 xlabel = "Local Time"
1702 1933 ylabel = "Phase"
1703 1934
1704 1935
1705 1936 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1706 1937 phase_beacon = dataOut.data_output
1707 1938 update_figfile = False
1708 1939
1709 1940 if not self.isConfig:
1710 1941
1711 1942 self.nplots = phase_beacon.size
1712 1943
1713 1944 self.setup(id=id,
1714 1945 nplots=self.nplots,
1715 1946 wintitle=wintitle,
1716 1947 showprofile=showprofile,
1717 1948 show=show)
1718 1949
1719 1950 if timerange is not None:
1720 1951 self.timerange = timerange
1721 1952
1722 1953 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1723 1954
1724 1955 if ymin == None: ymin = numpy.nanmin(phase_beacon) - 10.0
1725 1956 if ymax == None: ymax = numpy.nanmax(phase_beacon) + 10.0
1726 1957
1727 1958 self.FTP_WEI = ftp_wei
1728 1959 self.EXP_CODE = exp_code
1729 1960 self.SUB_EXP_CODE = sub_exp_code
1730 1961 self.PLOT_POS = plot_pos
1731 1962
1732 1963 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1733 1964 self.isConfig = True
1734 1965 self.figfile = figfile
1735 1966 self.xdata = numpy.array([])
1736 1967 self.ydata = numpy.array([])
1737 1968
1738 1969 #open file beacon phase
1739 1970 path = '%s%03d' %(self.PREFIX, self.id)
1740 1971 beacon_file = os.path.join(path,'%s.txt'%self.name)
1741 1972 self.filename_phase = os.path.join(figpath,beacon_file)
1742 1973 update_figfile = True
1743 1974
1744 1975
1745 1976 #store data beacon phase
1746 1977 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1747 1978
1748 1979 self.setWinTitle(title)
1749 1980
1750 1981
1751 1982 title = "Phase Offset %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1752 1983
1753 1984 legendlabels = ["phase %d"%(chan) for chan in numpy.arange(self.nplots)]
1754 1985
1755 1986 axes = self.axesList[0]
1756 1987
1757 1988 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1758 1989
1759 1990 if len(self.ydata)==0:
1760 1991 self.ydata = phase_beacon.reshape(-1,1)
1761 1992 else:
1762 1993 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1763 1994
1764 1995
1765 1996 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1766 1997 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1767 1998 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1768 1999 XAxisAsTime=True, grid='both'
1769 2000 )
1770 2001
1771 2002 self.draw()
1772 2003
1773 2004 self.save(figpath=figpath,
1774 2005 figfile=figfile,
1775 2006 save=save,
1776 2007 ftp=ftp,
1777 2008 wr_period=wr_period,
1778 2009 thisDatetime=thisDatetime,
1779 2010 update_figfile=update_figfile)
1780 2011
1781 2012 if dataOut.ltctime + dataOut.outputInterval >= self.xmax:
1782 2013 self.counter_imagwr = wr_period
1783 2014 self.isConfig = False
1784 2015 update_figfile = True
1785 2016
1786 2017
1787 2018
1788 2019 class NSMeteorDetection1Plot_(Figure):
1789 2020
1790 2021 isConfig = None
1791 2022 __nsubplots = None
1792 2023
1793 2024 WIDTHPROF = None
1794 2025 HEIGHTPROF = None
1795 2026 PREFIX = 'nsm'
1796 2027
1797 2028 zminList = None
1798 2029 zmaxList = None
1799 2030 cmapList = None
1800 2031 titleList = None
1801 2032 nPairs = None
1802 2033 nChannels = None
1803 2034 nParam = None
1804 2035
1805 2036 def __init__(self, **kwargs):
1806 2037 Figure.__init__(self, **kwargs)
1807 2038 self.isConfig = False
1808 2039 self.__nsubplots = 1
1809 2040
1810 2041 self.WIDTH = 750
1811 2042 self.HEIGHT = 250
1812 2043 self.WIDTHPROF = 120
1813 2044 self.HEIGHTPROF = 0
1814 2045 self.counter_imagwr = 0
1815 2046
1816 2047 self.PLOT_CODE = SPEC_CODE
1817 2048
1818 2049 self.FTP_WEI = None
1819 2050 self.EXP_CODE = None
1820 2051 self.SUB_EXP_CODE = None
1821 2052 self.PLOT_POS = None
1822 2053
1823 2054 self.__xfilter_ena = False
1824 2055 self.__yfilter_ena = False
1825 2056
1826 2057 def getSubplots(self):
1827 2058
1828 2059 ncol = 3
1829 2060 nrow = int(numpy.ceil(self.nplots/3.0))
1830 2061
1831 2062 return nrow, ncol
1832 2063
1833 2064 def setup(self, id, nplots, wintitle, show=True):
1834 2065
1835 2066 self.nplots = nplots
1836 2067
1837 2068 ncolspan = 1
1838 2069 colspan = 1
1839 2070
1840 2071 self.createFigure(id = id,
1841 2072 wintitle = wintitle,
1842 2073 widthplot = self.WIDTH + self.WIDTHPROF,
1843 2074 heightplot = self.HEIGHT + self.HEIGHTPROF,
1844 2075 show=show)
1845 2076
1846 2077 nrow, ncol = self.getSubplots()
1847 2078
1848 2079 counter = 0
1849 2080 for y in range(nrow):
1850 2081 for x in range(ncol):
1851 2082
1852 2083 if counter >= self.nplots:
1853 2084 break
1854 2085
1855 2086 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1856 2087
1857 2088 counter += 1
1858 2089
1859 2090 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
1860 2091 xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None,
1861 2092 vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA',
1862 2093 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1863 2094 server=None, folder=None, username=None, password=None,
1864 2095 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
1865 2096 xaxis="frequency"):
1866 2097
1867 2098 """
1868 2099
1869 2100 Input:
1870 2101 dataOut :
1871 2102 id :
1872 2103 wintitle :
1873 2104 channelList :
1874 2105 showProfile :
1875 2106 xmin : None,
1876 2107 xmax : None,
1877 2108 ymin : None,
1878 2109 ymax : None,
1879 2110 zmin : None,
1880 2111 zmax : None
1881 2112 """
1882 2113 #SEPARAR EN DOS PLOTS
1883 2114 nParam = dataOut.data_param.shape[1] - 3
1884 2115
1885 2116 utctime = dataOut.data_param[0,0]
1886 2117 tmet = dataOut.data_param[:,1].astype(int)
1887 2118 hmet = dataOut.data_param[:,2].astype(int)
1888 2119
1889 2120 x = dataOut.abscissaList
1890 2121 y = dataOut.heightList
1891 2122
1892 2123 z = numpy.zeros((nParam, y.size, x.size - 1))
1893 2124 z[:,:] = numpy.nan
1894 2125 z[:,hmet,tmet] = dataOut.data_param[:,3:].T
1895 2126 z[0,:,:] = 10*numpy.log10(z[0,:,:])
1896 2127
1897 2128 xlabel = "Time (s)"
1898 2129 ylabel = "Range (km)"
1899 2130
1900 2131 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
1901 2132
1902 2133 if not self.isConfig:
1903 2134
1904 2135 nplots = nParam
1905 2136
1906 2137 self.setup(id=id,
1907 2138 nplots=nplots,
1908 2139 wintitle=wintitle,
1909 2140 show=show)
1910 2141
1911 2142 if xmin is None: xmin = numpy.nanmin(x)
1912 2143 if xmax is None: xmax = numpy.nanmax(x)
1913 2144 if ymin is None: ymin = numpy.nanmin(y)
1914 2145 if ymax is None: ymax = numpy.nanmax(y)
1915 2146 if SNRmin is None: SNRmin = numpy.nanmin(z[0,:])
1916 2147 if SNRmax is None: SNRmax = numpy.nanmax(z[0,:])
1917 2148 if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:]))
1918 2149 if vmin is None: vmin = -vmax
1919 2150 if wmin is None: wmin = 0
1920 2151 if wmax is None: wmax = 50
1921 2152
1922 2153 pairsList = dataOut.groupList
1923 2154 self.nPairs = len(dataOut.groupList)
1924 2155
1925 2156 zminList = [SNRmin, vmin, cmin] + [pmin]*self.nPairs
1926 2157 zmaxList = [SNRmax, vmax, cmax] + [pmax]*self.nPairs
1927 2158 titleList = ["SNR","Radial Velocity","Coherence"]
1928 2159 cmapList = ["jet","RdBu_r","jet"]
1929 2160
1930 2161 for i in range(self.nPairs):
1931 2162 strAux1 = "Phase Difference "+ str(pairsList[i][0]) + str(pairsList[i][1])
1932 2163 titleList = titleList + [strAux1]
1933 2164 cmapList = cmapList + ["RdBu_r"]
1934 2165
1935 2166 self.zminList = zminList
1936 2167 self.zmaxList = zmaxList
1937 2168 self.cmapList = cmapList
1938 2169 self.titleList = titleList
1939 2170
1940 2171 self.FTP_WEI = ftp_wei
1941 2172 self.EXP_CODE = exp_code
1942 2173 self.SUB_EXP_CODE = sub_exp_code
1943 2174 self.PLOT_POS = plot_pos
1944 2175
1945 2176 self.isConfig = True
1946 2177
1947 2178 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
1948 2179
1949 2180 for i in range(nParam):
1950 2181 title = self.titleList[i] + ": " +str_datetime
1951 2182 axes = self.axesList[i]
1952 2183 axes.pcolor(x, y, z[i,:].T,
1953 2184 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i],
1954 2185 xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='')
1955 2186 self.draw()
1956 2187
1957 2188 if figfile == None:
1958 2189 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
1959 2190 name = str_datetime
1960 2191 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
1961 2192 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
1962 2193 figfile = self.getFilename(name)
1963 2194
1964 2195 self.save(figpath=figpath,
1965 2196 figfile=figfile,
1966 2197 save=save,
1967 2198 ftp=ftp,
1968 2199 wr_period=wr_period,
1969 2200 thisDatetime=thisDatetime)
1970 2201
1971 2202
1972 2203 class NSMeteorDetection2Plot_(Figure):
1973 2204
1974 2205 isConfig = None
1975 2206 __nsubplots = None
1976 2207
1977 2208 WIDTHPROF = None
1978 2209 HEIGHTPROF = None
1979 2210 PREFIX = 'nsm'
1980 2211
1981 2212 zminList = None
1982 2213 zmaxList = None
1983 2214 cmapList = None
1984 2215 titleList = None
1985 2216 nPairs = None
1986 2217 nChannels = None
1987 2218 nParam = None
1988 2219
1989 2220 def __init__(self, **kwargs):
1990 2221 Figure.__init__(self, **kwargs)
1991 2222 self.isConfig = False
1992 2223 self.__nsubplots = 1
1993 2224
1994 2225 self.WIDTH = 750
1995 2226 self.HEIGHT = 250
1996 2227 self.WIDTHPROF = 120
1997 2228 self.HEIGHTPROF = 0
1998 2229 self.counter_imagwr = 0
1999 2230
2000 2231 self.PLOT_CODE = SPEC_CODE
2001 2232
2002 2233 self.FTP_WEI = None
2003 2234 self.EXP_CODE = None
2004 2235 self.SUB_EXP_CODE = None
2005 2236 self.PLOT_POS = None
2006 2237
2007 2238 self.__xfilter_ena = False
2008 2239 self.__yfilter_ena = False
2009 2240
2010 2241 def getSubplots(self):
2011 2242
2012 2243 ncol = 3
2013 2244 nrow = int(numpy.ceil(self.nplots/3.0))
2014 2245
2015 2246 return nrow, ncol
2016 2247
2017 2248 def setup(self, id, nplots, wintitle, show=True):
2018 2249
2019 2250 self.nplots = nplots
2020 2251
2021 2252 ncolspan = 1
2022 2253 colspan = 1
2023 2254
2024 2255 self.createFigure(id = id,
2025 2256 wintitle = wintitle,
2026 2257 widthplot = self.WIDTH + self.WIDTHPROF,
2027 2258 heightplot = self.HEIGHT + self.HEIGHTPROF,
2028 2259 show=show)
2029 2260
2030 2261 nrow, ncol = self.getSubplots()
2031 2262
2032 2263 counter = 0
2033 2264 for y in range(nrow):
2034 2265 for x in range(ncol):
2035 2266
2036 2267 if counter >= self.nplots:
2037 2268 break
2038 2269
2039 2270 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
2040 2271
2041 2272 counter += 1
2042 2273
2043 2274 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
2044 2275 xmin=None, xmax=None, ymin=None, ymax=None, SNRmin=None, SNRmax=None,
2045 2276 vmin=None, vmax=None, wmin=None, wmax=None, mode = 'SA',
2046 2277 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
2047 2278 server=None, folder=None, username=None, password=None,
2048 2279 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
2049 2280 xaxis="frequency"):
2050 2281
2051 2282 """
2052 2283
2053 2284 Input:
2054 2285 dataOut :
2055 2286 id :
2056 2287 wintitle :
2057 2288 channelList :
2058 2289 showProfile :
2059 2290 xmin : None,
2060 2291 xmax : None,
2061 2292 ymin : None,
2062 2293 ymax : None,
2063 2294 zmin : None,
2064 2295 zmax : None
2065 2296 """
2066 2297 #Rebuild matrix
2067 2298 utctime = dataOut.data_param[0,0]
2068 2299 cmet = dataOut.data_param[:,1].astype(int)
2069 2300 tmet = dataOut.data_param[:,2].astype(int)
2070 2301 hmet = dataOut.data_param[:,3].astype(int)
2071 2302
2072 2303 nParam = 3
2073 2304 nChan = len(dataOut.groupList)
2074 2305 x = dataOut.abscissaList
2075 2306 y = dataOut.heightList
2076 2307
2077 2308 z = numpy.full((nChan, nParam, y.size, x.size - 1),numpy.nan)
2078 2309 z[cmet,:,hmet,tmet] = dataOut.data_param[:,4:]
2079 2310 z[:,0,:,:] = 10*numpy.log10(z[:,0,:,:]) #logarithmic scale
2080 2311 z = numpy.reshape(z, (nChan*nParam, y.size, x.size-1))
2081 2312
2082 2313 xlabel = "Time (s)"
2083 2314 ylabel = "Range (km)"
2084 2315
2085 2316 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.ltctime)
2086 2317
2087 2318 if not self.isConfig:
2088 2319
2089 2320 nplots = nParam*nChan
2090 2321
2091 2322 self.setup(id=id,
2092 2323 nplots=nplots,
2093 2324 wintitle=wintitle,
2094 2325 show=show)
2095 2326
2096 2327 if xmin is None: xmin = numpy.nanmin(x)
2097 2328 if xmax is None: xmax = numpy.nanmax(x)
2098 2329 if ymin is None: ymin = numpy.nanmin(y)
2099 2330 if ymax is None: ymax = numpy.nanmax(y)
2100 2331 if SNRmin is None: SNRmin = numpy.nanmin(z[0,:])
2101 2332 if SNRmax is None: SNRmax = numpy.nanmax(z[0,:])
2102 2333 if vmax is None: vmax = numpy.nanmax(numpy.abs(z[1,:]))
2103 2334 if vmin is None: vmin = -vmax
2104 2335 if wmin is None: wmin = 0
2105 2336 if wmax is None: wmax = 50
2106 2337
2107 2338 self.nChannels = nChan
2108 2339
2109 2340 zminList = []
2110 2341 zmaxList = []
2111 2342 titleList = []
2112 2343 cmapList = []
2113 2344 for i in range(self.nChannels):
2114 2345 strAux1 = "SNR Channel "+ str(i)
2115 2346 strAux2 = "Radial Velocity Channel "+ str(i)
2116 2347 strAux3 = "Spectral Width Channel "+ str(i)
2117 2348
2118 2349 titleList = titleList + [strAux1,strAux2,strAux3]
2119 2350 cmapList = cmapList + ["jet","RdBu_r","jet"]
2120 2351 zminList = zminList + [SNRmin,vmin,wmin]
2121 2352 zmaxList = zmaxList + [SNRmax,vmax,wmax]
2122 2353
2123 2354 self.zminList = zminList
2124 2355 self.zmaxList = zmaxList
2125 2356 self.cmapList = cmapList
2126 2357 self.titleList = titleList
2127 2358
2128 2359 self.FTP_WEI = ftp_wei
2129 2360 self.EXP_CODE = exp_code
2130 2361 self.SUB_EXP_CODE = sub_exp_code
2131 2362 self.PLOT_POS = plot_pos
2132 2363
2133 2364 self.isConfig = True
2134 2365
2135 2366 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
2136 2367
2137 2368 for i in range(self.nplots):
2138 2369 title = self.titleList[i] + ": " +str_datetime
2139 2370 axes = self.axesList[i]
2140 2371 axes.pcolor(x, y, z[i,:].T,
2141 2372 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=self.zminList[i], zmax=self.zmaxList[i],
2142 2373 xlabel=xlabel, ylabel=ylabel, title=title, colormap=self.cmapList[i],ticksize=9, cblabel='')
2143 2374 self.draw()
2144 2375
2145 2376 if figfile == None:
2146 2377 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
2147 2378 name = str_datetime
2148 2379 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
2149 2380 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
2150 2381 figfile = self.getFilename(name)
2151 2382
2152 2383 self.save(figpath=figpath,
2153 2384 figfile=figfile,
2154 2385 save=save,
2155 2386 ftp=ftp,
2156 2387 wr_period=wr_period,
2157 2388 thisDatetime=thisDatetime)
2158 2389 No newline at end of file
@@ -1,1587 +1,1588
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 from schainpy.model.proc.jroproc_base import MPDecorator
13 13
14 14 from schainpy.utils import log
15 15
16 16 @MPDecorator
17 17 class SpectraPlot_(Figure):
18 18
19 19 isConfig = None
20 20 __nsubplots = None
21 21
22 22 WIDTHPROF = None
23 23 HEIGHTPROF = None
24 24 PREFIX = 'spc'
25 25
26 26 def __init__(self):
27 27 Figure.__init__(self)
28 28 self.isConfig = False
29 29 self.__nsubplots = 1
30 30 self.WIDTH = 250
31 31 self.HEIGHT = 250
32 32 self.WIDTHPROF = 120
33 33 self.HEIGHTPROF = 0
34 34 self.counter_imagwr = 0
35 35
36 36 self.PLOT_CODE = SPEC_CODE
37 37
38 38 self.FTP_WEI = None
39 39 self.EXP_CODE = None
40 40 self.SUB_EXP_CODE = None
41 41 self.PLOT_POS = None
42 42
43 43 self.__xfilter_ena = False
44 44 self.__yfilter_ena = False
45
46 self.indice=1
45 47
46 48 def getSubplots(self):
47 49
48 50 ncol = int(numpy.sqrt(self.nplots)+0.9)
49 51 nrow = int(self.nplots*1./ncol + 0.9)
50 52
51 53 return nrow, ncol
52 54
53 55 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
54 56
55 57 self.__showprofile = showprofile
56 58 self.nplots = nplots
57 59
58 60 ncolspan = 1
59 61 colspan = 1
60 62 if showprofile:
61 63 ncolspan = 3
62 64 colspan = 2
63 65 self.__nsubplots = 2
64 66
65 67 self.createFigure(id = id,
66 68 wintitle = wintitle,
67 69 widthplot = self.WIDTH + self.WIDTHPROF,
68 70 heightplot = self.HEIGHT + self.HEIGHTPROF,
69 71 show=show)
70 72
71 73 nrow, ncol = self.getSubplots()
72 74
73 75 counter = 0
74 76 for y in range(nrow):
75 77 for x in range(ncol):
76 78
77 79 if counter >= self.nplots:
78 80 break
79 81
80 82 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
81 83
82 84 if showprofile:
83 85 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
84 86
85 87 counter += 1
86 88
87 89 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
88 90 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
89 91 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
90 92 server=None, folder=None, username=None, password=None,
91 93 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False,
92 94 xaxis="frequency", colormap='jet', normFactor=None):
93 95
94 96 """
95 97
96 98 Input:
97 99 dataOut :
98 100 id :
99 101 wintitle :
100 102 channelList :
101 103 showProfile :
102 104 xmin : None,
103 105 xmax : None,
104 106 ymin : None,
105 107 ymax : None,
106 108 zmin : None,
107 109 zmax : None
108 110 """
109 111 if dataOut.flagNoData:
110 112 return dataOut
111 113
112 114 if realtime:
113 115 if not(isRealtime(utcdatatime = dataOut.utctime)):
114 116 print('Skipping this plot function')
115 117 return
116 118
117 119 if channelList == None:
118 120 channelIndexList = dataOut.channelIndexList
119 121 else:
120 122 channelIndexList = []
121 123 for channel in channelList:
122 124 if channel not in dataOut.channelList:
123 125 raise ValueError("Channel %d is not in dataOut.channelList" %channel)
124 126 channelIndexList.append(dataOut.channelList.index(channel))
125 127
126 128 if normFactor is None:
127 129 factor = dataOut.normFactor
128 130 else:
129 131 factor = normFactor
130 132 if xaxis == "frequency":
131 133 x = dataOut.getFreqRange(1)/1000.
132 134 xlabel = "Frequency (kHz)"
133 135
134 136 elif xaxis == "time":
135 137 x = dataOut.getAcfRange(1)
136 138 xlabel = "Time (ms)"
137 139
138 140 else:
139 141 x = dataOut.getVelRange(1)
140 142 xlabel = "Velocity (m/s)"
141 143
142 ylabel = "Range (Km)"
144 ylabel = "Range (km)"
143 145
144 146 y = dataOut.getHeiRange()
145
146 147 z = dataOut.data_spc/factor
147 148 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
148 149 zdB = 10*numpy.log10(z)
149 150
150 151 avg = numpy.average(z, axis=1)
151 152 avgdB = 10*numpy.log10(avg)
152 153
153 154 noise = dataOut.getNoise()/factor
154 155 noisedB = 10*numpy.log10(noise)
155 156
156 157 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
157 158 title = wintitle + " Spectra"
159
158 160 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
159 161 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
160 162
161 163 if not self.isConfig:
162 164
163 165 nplots = len(channelIndexList)
164 166
165 167 self.setup(id=id,
166 168 nplots=nplots,
167 169 wintitle=wintitle,
168 170 showprofile=showprofile,
169 171 show=show)
170 172
171 173 if xmin == None: xmin = numpy.nanmin(x)
172 174 if xmax == None: xmax = numpy.nanmax(x)
173 175 if ymin == None: ymin = numpy.nanmin(y)
174 176 if ymax == None: ymax = numpy.nanmax(y)
175 177 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
176 178 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
177 179
178 180 self.FTP_WEI = ftp_wei
179 181 self.EXP_CODE = exp_code
180 182 self.SUB_EXP_CODE = sub_exp_code
181 183 self.PLOT_POS = plot_pos
182 184
183 185 self.isConfig = True
184 186
185 187 self.setWinTitle(title)
186 188
187 189 for i in range(self.nplots):
188 190 index = channelIndexList[i]
189 191 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
190 192 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[index], noisedB[index], str_datetime)
191 193 if len(dataOut.beam.codeList) != 0:
192 194 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)
193 195
194 196 axes = self.axesList[i*self.__nsubplots]
195 197 axes.pcolor(x, y, zdB[index,:,:],
196 198 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
197 199 xlabel=xlabel, ylabel=ylabel, title=title, colormap=colormap,
198 200 ticksize=9, cblabel='')
199 201
200 202 if self.__showprofile:
201 203 axes = self.axesList[i*self.__nsubplots +1]
202 204 axes.pline(avgdB[index,:], y,
203 205 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
204 206 xlabel='dB', ylabel='', title='',
205 207 ytick_visible=False,
206 208 grid='x')
207 209
208 210 noiseline = numpy.repeat(noisedB[index], len(y))
209 211 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
210 212
211 213 self.draw()
212 214
213 215 if figfile == None:
214 216 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
215 217 name = str_datetime
216 218 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
217 219 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
218 220 figfile = self.getFilename(name)
219 221
220 222 self.save(figpath=figpath,
221 223 figfile=figfile,
222 224 save=save,
223 225 ftp=ftp,
224 226 wr_period=wr_period,
225 227 thisDatetime=thisDatetime)
228
226 229
227 230 return dataOut
228 231 @MPDecorator
229 232 class CrossSpectraPlot_(Figure):
230 233
231 234 isConfig = None
232 235 __nsubplots = None
233 236
234 237 WIDTH = None
235 238 HEIGHT = None
236 239 WIDTHPROF = None
237 240 HEIGHTPROF = None
238 241 PREFIX = 'cspc'
239 242
240 243 def __init__(self):
241 244 Figure.__init__(self)
242 245 self.isConfig = False
243 246 self.__nsubplots = 4
244 247 self.counter_imagwr = 0
245 248 self.WIDTH = 250
246 249 self.HEIGHT = 250
247 250 self.WIDTHPROF = 0
248 251 self.HEIGHTPROF = 0
249 252
250 253 self.PLOT_CODE = CROSS_CODE
251 254 self.FTP_WEI = None
252 255 self.EXP_CODE = None
253 256 self.SUB_EXP_CODE = None
254 257 self.PLOT_POS = None
258
259 self.indice=0
255 260
256 261 def getSubplots(self):
257 262
258 263 ncol = 4
259 264 nrow = self.nplots
260 265
261 266 return nrow, ncol
262 267
263 268 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
264 269
265 270 self.__showprofile = showprofile
266 271 self.nplots = nplots
267 272
268 273 ncolspan = 1
269 274 colspan = 1
270 275
271 276 self.createFigure(id = id,
272 277 wintitle = wintitle,
273 278 widthplot = self.WIDTH + self.WIDTHPROF,
274 279 heightplot = self.HEIGHT + self.HEIGHTPROF,
275 280 show=True)
276 281
277 282 nrow, ncol = self.getSubplots()
278 283
279 284 counter = 0
280 285 for y in range(nrow):
281 286 for x in range(ncol):
282 287 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
283 288
284 289 counter += 1
285 290
286 291 def run(self, dataOut, id, wintitle="", pairsList=None,
287 292 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
288 293 coh_min=None, coh_max=None, phase_min=None, phase_max=None,
289 294 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
290 295 power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
291 296 server=None, folder=None, username=None, password=None,
292 297 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None,
293 298 xaxis='frequency'):
294 299
295 300 """
296 301
297 302 Input:
298 303 dataOut :
299 304 id :
300 305 wintitle :
301 306 channelList :
302 307 showProfile :
303 308 xmin : None,
304 309 xmax : None,
305 310 ymin : None,
306 311 ymax : None,
307 312 zmin : None,
308 313 zmax : None
309 314 """
310 315
311 316 if dataOut.flagNoData:
312 317 return dataOut
313 318
314 319 if pairsList == None:
315 320 pairsIndexList = dataOut.pairsIndexList
316 321 else:
317 322 pairsIndexList = []
318 323 for pair in pairsList:
319 324 if pair not in dataOut.pairsList:
320 325 raise ValueError("Pair %s is not in dataOut.pairsList" %str(pair))
321 326 pairsIndexList.append(dataOut.pairsList.index(pair))
322 327
323 328 if not pairsIndexList:
324 329 return
325 330
326 331 if len(pairsIndexList) > 4:
327 332 pairsIndexList = pairsIndexList[0:4]
328 333
329 334 if normFactor is None:
330 335 factor = dataOut.normFactor
331 336 else:
332 337 factor = normFactor
333 338 x = dataOut.getVelRange(1)
334 339 y = dataOut.getHeiRange()
335 340 z = dataOut.data_spc[:,:,:]/factor
336 341 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
337 342
338 343 noise = dataOut.noise/factor
339 344
340 345 zdB = 10*numpy.log10(z)
341 346 noisedB = 10*numpy.log10(noise)
342 347
343 348 if coh_min == None:
344 349 coh_min = 0.0
345 350 if coh_max == None:
346 351 coh_max = 1.0
347 352
348 353 if phase_min == None:
349 354 phase_min = -180
350 355 if phase_max == None:
351 356 phase_max = 180
352 357
353 358 #thisDatetime = dataOut.datatime
354 359 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
355 360 title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
356 361 # xlabel = "Velocity (m/s)"
357 362 ylabel = "Range (Km)"
358 363
359 364 if xaxis == "frequency":
360 365 x = dataOut.getFreqRange(1)/1000.
361 366 xlabel = "Frequency (kHz)"
362 367
363 368 elif xaxis == "time":
364 369 x = dataOut.getAcfRange(1)
365 370 xlabel = "Time (ms)"
366 371
367 372 else:
368 373 x = dataOut.getVelRange(1)
369 374 xlabel = "Velocity (m/s)"
370 375
371 376 if not self.isConfig:
372 377
373 378 nplots = len(pairsIndexList)
374 379
375 380 self.setup(id=id,
376 381 nplots=nplots,
377 382 wintitle=wintitle,
378 383 showprofile=False,
379 384 show=show)
380 385
381 386 avg = numpy.abs(numpy.average(z, axis=1))
382 387 avgdB = 10*numpy.log10(avg)
383 388
384 389 if xmin == None: xmin = numpy.nanmin(x)
385 390 if xmax == None: xmax = numpy.nanmax(x)
386 391 if ymin == None: ymin = numpy.nanmin(y)
387 392 if ymax == None: ymax = numpy.nanmax(y)
388 393 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
389 394 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
390 395
391 396 self.FTP_WEI = ftp_wei
392 397 self.EXP_CODE = exp_code
393 398 self.SUB_EXP_CODE = sub_exp_code
394 399 self.PLOT_POS = plot_pos
395 400
396 401 self.isConfig = True
397 402
398 403 self.setWinTitle(title)
404
399 405
400 406 for i in range(self.nplots):
401 407 pair = dataOut.pairsList[pairsIndexList[i]]
402 408
403 409 chan_index0 = dataOut.channelList.index(pair[0])
404 410 chan_index1 = dataOut.channelList.index(pair[1])
405 411
406 412 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
407 413 title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[chan_index0], str_datetime)
408 414 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index0,:,:]/factor)
409 415 axes0 = self.axesList[i*self.__nsubplots]
410 416 axes0.pcolor(x, y, zdB,
411 417 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
412 418 xlabel=xlabel, ylabel=ylabel, title=title,
413 419 ticksize=9, colormap=power_cmap, cblabel='')
414 420
415 421 title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[chan_index1], str_datetime)
416 422 zdB = 10.*numpy.log10(dataOut.data_spc[chan_index1,:,:]/factor)
417 423 axes0 = self.axesList[i*self.__nsubplots+1]
418 424 axes0.pcolor(x, y, zdB,
419 425 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
420 426 xlabel=xlabel, ylabel=ylabel, title=title,
421 427 ticksize=9, colormap=power_cmap, cblabel='')
422 428
423 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[chan_index0,:,:]*dataOut.data_spc[chan_index1,:,:])
429 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:] / numpy.sqrt( dataOut.data_spc[chan_index0,:,:]*dataOut.data_spc[chan_index1,:,:] )
424 430 coherence = numpy.abs(coherenceComplex)
425 431 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
426 432 phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi
427 433
428 434 title = "Coherence Ch%d * Ch%d" %(pair[0], pair[1])
429 435 axes0 = self.axesList[i*self.__nsubplots+2]
430 436 axes0.pcolor(x, y, coherence,
431 437 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=coh_min, zmax=coh_max,
432 438 xlabel=xlabel, ylabel=ylabel, title=title,
433 439 ticksize=9, colormap=coherence_cmap, cblabel='')
434 440
435 441 title = "Phase Ch%d * Ch%d" %(pair[0], pair[1])
436 442 axes0 = self.axesList[i*self.__nsubplots+3]
437 443 axes0.pcolor(x, y, phase,
438 444 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
439 445 xlabel=xlabel, ylabel=ylabel, title=title,
440 446 ticksize=9, colormap=phase_cmap, cblabel='')
441 447
442
443
444 448 self.draw()
445 449
446 450 self.save(figpath=figpath,
447 451 figfile=figfile,
448 452 save=save,
449 453 ftp=ftp,
450 454 wr_period=wr_period,
451 455 thisDatetime=thisDatetime)
452 456
453 457 return dataOut
454 458
455 459 @MPDecorator
456 460 class RTIPlot_(Figure):
457 461
458 462 __isConfig = None
459 463 __nsubplots = None
460 464
461 465 WIDTHPROF = None
462 466 HEIGHTPROF = None
463 467 PREFIX = 'rti'
464 468
465 469 def __init__(self):
466 470
467 471 Figure.__init__(self)
468 472 self.timerange = None
469 473 self.isConfig = False
470 474 self.__nsubplots = 1
471 475
472 476 self.WIDTH = 800
473 self.HEIGHT = 180
477 self.HEIGHT = 250
474 478 self.WIDTHPROF = 120
475 479 self.HEIGHTPROF = 0
476 480 self.counter_imagwr = 0
477 481
478 482 self.PLOT_CODE = RTI_CODE
479 483
480 484 self.FTP_WEI = None
481 485 self.EXP_CODE = None
482 486 self.SUB_EXP_CODE = None
483 487 self.PLOT_POS = None
484 488 self.tmin = None
485 489 self.tmax = None
486 490
487 491 self.xmin = None
488 492 self.xmax = None
489 493
490 494 self.figfile = None
491 495
492 496 def getSubplots(self):
493 497
494 498 ncol = 1
495 499 nrow = self.nplots
496 500
497 501 return nrow, ncol
498 502
499 503 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
500 504
501 505 self.__showprofile = showprofile
502 506 self.nplots = nplots
503 507
504 508 ncolspan = 1
505 509 colspan = 1
506 510 if showprofile:
507 511 ncolspan = 7
508 512 colspan = 6
509 513 self.__nsubplots = 2
510 514
511 515 self.createFigure(id = id,
512 516 wintitle = wintitle,
513 517 widthplot = self.WIDTH + self.WIDTHPROF,
514 518 heightplot = self.HEIGHT + self.HEIGHTPROF,
515 519 show=show)
516 520
517 521 nrow, ncol = self.getSubplots()
518 522
519 523 counter = 0
520 524 for y in range(nrow):
521 525 for x in range(ncol):
522 526
523 527 if counter >= self.nplots:
524 528 break
525 529
526 530 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
527 531
528 532 if showprofile:
529 533 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
530 534
531 535 counter += 1
532 536
533 537 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
534 538 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
535 539 timerange=None, colormap='jet',
536 540 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
537 541 server=None, folder=None, username=None, password=None,
538 542 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, normFactor=None, HEIGHT=None):
539 543
540 544 """
541 545
542 546 Input:
543 547 dataOut :
544 548 id :
545 549 wintitle :
546 550 channelList :
547 551 showProfile :
548 552 xmin : None,
549 553 xmax : None,
550 554 ymin : None,
551 555 ymax : None,
552 556 zmin : None,
553 557 zmax : None
554 558 """
555 559 if dataOut.flagNoData:
556 560 return dataOut
557 561
558 562 #colormap = kwargs.get('colormap', 'jet')
559 563 if HEIGHT is not None:
560 564 self.HEIGHT = HEIGHT
561 565
562 566 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
563 567 return
564 568
565 569 if channelList == None:
566 570 channelIndexList = dataOut.channelIndexList
567 571 else:
568 572 channelIndexList = []
569 573 for channel in channelList:
570 574 if channel not in dataOut.channelList:
571 575 raise ValueError("Channel %d is not in dataOut.channelList")
572 576 channelIndexList.append(dataOut.channelList.index(channel))
573 577
574 578 if normFactor is None:
575 579 factor = dataOut.normFactor
576 580 else:
577 581 factor = normFactor
578 582
579 583 #factor = dataOut.normFactor
580 584 x = dataOut.getTimeRange()
581 585 y = dataOut.getHeiRange()
582 586
583 587 z = dataOut.data_spc/factor
584 588 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
585 589 avg = numpy.average(z, axis=1)
586 590 avgdB = 10.*numpy.log10(avg)
587 591 # avgdB = dataOut.getPower()
588 592
589 593
590 594 thisDatetime = dataOut.datatime
591 595 #thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
592 596 title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
593 597 xlabel = ""
594 598 ylabel = "Range (Km)"
595 599
596 600 update_figfile = False
597 601
598 602 if self.xmax is not None and dataOut.ltctime >= self.xmax: #yong
599 603 self.counter_imagwr = wr_period
600 604 self.isConfig = False
601 605 update_figfile = True
602 606
603 607 if not self.isConfig:
604 608
605 609 nplots = len(channelIndexList)
606 610
607 611 self.setup(id=id,
608 612 nplots=nplots,
609 613 wintitle=wintitle,
610 614 showprofile=showprofile,
611 615 show=show)
612 616
613 617 if timerange != None:
614 618 self.timerange = timerange
615 619
616 620 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
617 621
618 622 noise = dataOut.noise/factor
619 623 noisedB = 10*numpy.log10(noise)
620 624
621 625 if ymin == None: ymin = numpy.nanmin(y)
622 626 if ymax == None: ymax = numpy.nanmax(y)
623 627 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
624 628 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
625 629
626 630 self.FTP_WEI = ftp_wei
627 631 self.EXP_CODE = exp_code
628 632 self.SUB_EXP_CODE = sub_exp_code
629 633 self.PLOT_POS = plot_pos
630 634
631 635 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
632 636 self.isConfig = True
633 637 self.figfile = figfile
634 638 update_figfile = True
635 639
636 640 self.setWinTitle(title)
637 641
638 642 for i in range(self.nplots):
639 643 index = channelIndexList[i]
640 644 title = "Channel %d: %s" %(dataOut.channelList[index], thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
641 645 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
642 646 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
643 647 axes = self.axesList[i*self.__nsubplots]
644 648 zdB = avgdB[index].reshape((1,-1))
645 649 axes.pcolorbuffer(x, y, zdB,
646 650 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
647 651 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
648 652 ticksize=9, cblabel='', cbsize="1%", colormap=colormap)
649 653
650 654 if self.__showprofile:
651 655 axes = self.axesList[i*self.__nsubplots +1]
652 656 axes.pline(avgdB[index], y,
653 657 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
654 658 xlabel='dB', ylabel='', title='',
655 659 ytick_visible=False,
656 660 grid='x')
657 661
658 662 self.draw()
659 663
660 664 self.save(figpath=figpath,
661 665 figfile=figfile,
662 666 save=save,
663 667 ftp=ftp,
664 668 wr_period=wr_period,
665 669 thisDatetime=thisDatetime,
666 670 update_figfile=update_figfile)
667 671 return dataOut
668 672
669 673 @MPDecorator
670 674 class CoherenceMap_(Figure):
671 675 isConfig = None
672 676 __nsubplots = None
673 677
674 678 WIDTHPROF = None
675 679 HEIGHTPROF = None
676 680 PREFIX = 'cmap'
677 681
678 682 def __init__(self):
679 683 Figure.__init__(self)
680 684 self.timerange = 2*60*60
681 685 self.isConfig = False
682 686 self.__nsubplots = 1
683 687
684 688 self.WIDTH = 800
685 689 self.HEIGHT = 180
686 690 self.WIDTHPROF = 120
687 691 self.HEIGHTPROF = 0
688 692 self.counter_imagwr = 0
689 693
690 694 self.PLOT_CODE = COH_CODE
691 695
692 696 self.FTP_WEI = None
693 697 self.EXP_CODE = None
694 698 self.SUB_EXP_CODE = None
695 699 self.PLOT_POS = None
696 700 self.counter_imagwr = 0
697 701
698 702 self.xmin = None
699 703 self.xmax = None
700 704
701 705 def getSubplots(self):
702 706 ncol = 1
703 707 nrow = self.nplots*2
704 708
705 709 return nrow, ncol
706 710
707 711 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
708 712 self.__showprofile = showprofile
709 713 self.nplots = nplots
710 714
711 715 ncolspan = 1
712 716 colspan = 1
713 717 if showprofile:
714 718 ncolspan = 7
715 719 colspan = 6
716 720 self.__nsubplots = 2
717 721
718 722 self.createFigure(id = id,
719 723 wintitle = wintitle,
720 724 widthplot = self.WIDTH + self.WIDTHPROF,
721 725 heightplot = self.HEIGHT + self.HEIGHTPROF,
722 726 show=True)
723 727
724 728 nrow, ncol = self.getSubplots()
725 729
726 730 for y in range(nrow):
727 731 for x in range(ncol):
728 732
729 733 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
730 734
731 735 if showprofile:
732 736 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
733 737
734 738 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
735 739 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
736 740 timerange=None, phase_min=None, phase_max=None,
737 741 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
738 742 coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
739 743 server=None, folder=None, username=None, password=None,
740 744 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
741 745
742 746
743 747 if dataOut.flagNoData:
744 748 return dataOut
745 749
746 750 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
747 751 return
748 752
749 753 if pairsList == None:
750 754 pairsIndexList = dataOut.pairsIndexList
751 755 else:
752 756 pairsIndexList = []
753 757 for pair in pairsList:
754 758 if pair not in dataOut.pairsList:
755 759 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
756 760 pairsIndexList.append(dataOut.pairsList.index(pair))
757 761
758 762 if pairsIndexList == []:
759 763 return
760 764
761 765 if len(pairsIndexList) > 4:
762 766 pairsIndexList = pairsIndexList[0:4]
763 767
764 768 if phase_min == None:
765 769 phase_min = -180
766 770 if phase_max == None:
767 771 phase_max = 180
768 772
769 773 x = dataOut.getTimeRange()
770 774 y = dataOut.getHeiRange()
771 775
772 776 thisDatetime = dataOut.datatime
773 777
774 778 title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
775 779 xlabel = ""
776 780 ylabel = "Range (Km)"
777 781 update_figfile = False
778 782
779 783 if not self.isConfig:
780 784 nplots = len(pairsIndexList)
781 785 self.setup(id=id,
782 786 nplots=nplots,
783 787 wintitle=wintitle,
784 788 showprofile=showprofile,
785 789 show=show)
786 790
787 791 if timerange != None:
788 792 self.timerange = timerange
789 793
790 794 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
791 795
792 796 if ymin == None: ymin = numpy.nanmin(y)
793 797 if ymax == None: ymax = numpy.nanmax(y)
794 798 if zmin == None: zmin = 0.
795 799 if zmax == None: zmax = 1.
796 800
797 801 self.FTP_WEI = ftp_wei
798 802 self.EXP_CODE = exp_code
799 803 self.SUB_EXP_CODE = sub_exp_code
800 804 self.PLOT_POS = plot_pos
801 805
802 806 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
803 807
804 808 self.isConfig = True
805 809 update_figfile = True
806 810
807 811 self.setWinTitle(title)
808 812
809 813 for i in range(self.nplots):
810 814
811 815 pair = dataOut.pairsList[pairsIndexList[i]]
812 816
813 817 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
814 818 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
815 819 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
816 820
817 821
818 822 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
819 823 coherence = numpy.abs(avgcoherenceComplex)
820 824
821 825 z = coherence.reshape((1,-1))
822 826
823 827 counter = 0
824 828
825 829 title = "Coherence Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
826 830 axes = self.axesList[i*self.__nsubplots*2]
827 831 axes.pcolorbuffer(x, y, z,
828 832 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
829 833 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
830 834 ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%")
831 835
832 836 if self.__showprofile:
833 837 counter += 1
834 838 axes = self.axesList[i*self.__nsubplots*2 + counter]
835 839 axes.pline(coherence, y,
836 840 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
837 841 xlabel='', ylabel='', title='', ticksize=7,
838 842 ytick_visible=False, nxticks=5,
839 843 grid='x')
840 844
841 845 counter += 1
842 846
843 847 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
844 848
845 849 z = phase.reshape((1,-1))
846 850
847 851 title = "Phase Ch%d * Ch%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
848 852 axes = self.axesList[i*self.__nsubplots*2 + counter]
849 853 axes.pcolorbuffer(x, y, z,
850 854 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=phase_min, zmax=phase_max,
851 855 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
852 856 ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%")
853 857
854 858 if self.__showprofile:
855 859 counter += 1
856 860 axes = self.axesList[i*self.__nsubplots*2 + counter]
857 861 axes.pline(phase, y,
858 862 xmin=phase_min, xmax=phase_max, ymin=ymin, ymax=ymax,
859 863 xlabel='', ylabel='', title='', ticksize=7,
860 864 ytick_visible=False, nxticks=4,
861 865 grid='x')
862 866
863 867 self.draw()
864 868
865 869 if dataOut.ltctime >= self.xmax:
866 870 self.counter_imagwr = wr_period
867 871 self.isConfig = False
868 872 update_figfile = True
869 873
870 874 self.save(figpath=figpath,
871 875 figfile=figfile,
872 876 save=save,
873 877 ftp=ftp,
874 878 wr_period=wr_period,
875 879 thisDatetime=thisDatetime,
876 880 update_figfile=update_figfile)
877 881
878 882 return dataOut
879 883
880 884 @MPDecorator
881 885 class PowerProfilePlot_(Figure):
882 886
883 887 isConfig = None
884 888 __nsubplots = None
885 889
886 890 WIDTHPROF = None
887 891 HEIGHTPROF = None
888 892 PREFIX = 'spcprofile'
889 893
890 894 def __init__(self):
891 895 Figure.__init__(self)
892 896 self.isConfig = False
893 897 self.__nsubplots = 1
894 898
895 899 self.PLOT_CODE = POWER_CODE
896 900
897 901 self.WIDTH = 300
898 902 self.HEIGHT = 500
899 903 self.counter_imagwr = 0
900 904
901 905 def getSubplots(self):
902 906 ncol = 1
903 907 nrow = 1
904 908
905 909 return nrow, ncol
906 910
907 911 def setup(self, id, nplots, wintitle, show):
908 912
909 913 self.nplots = nplots
910 914
911 915 ncolspan = 1
912 916 colspan = 1
913 917
914 918 self.createFigure(id = id,
915 919 wintitle = wintitle,
916 920 widthplot = self.WIDTH,
917 921 heightplot = self.HEIGHT,
918 922 show=show)
919 923
920 924 nrow, ncol = self.getSubplots()
921 925
922 926 counter = 0
923 927 for y in range(nrow):
924 928 for x in range(ncol):
925 929 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
926 930
927 931 def run(self, dataOut, id, wintitle="", channelList=None,
928 932 xmin=None, xmax=None, ymin=None, ymax=None,
929 933 save=False, figpath='./', figfile=None, show=True,
930 934 ftp=False, wr_period=1, server=None,
931 935 folder=None, username=None, password=None):
932 936
933 937 if dataOut.flagNoData:
934 938 return dataOut
935 939
936 940
937 941 if channelList == None:
938 942 channelIndexList = dataOut.channelIndexList
939 943 channelList = dataOut.channelList
940 944 else:
941 945 channelIndexList = []
942 946 for channel in channelList:
943 947 if channel not in dataOut.channelList:
944 948 raise ValueError("Channel %d is not in dataOut.channelList")
945 949 channelIndexList.append(dataOut.channelList.index(channel))
946 950
947 951 factor = dataOut.normFactor
948 952
949 953 y = dataOut.getHeiRange()
950 954
951 955 #for voltage
952 956 if dataOut.type == 'Voltage':
953 957 x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
954 958 x = x.real
955 959 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
956 960
957 961 #for spectra
958 962 if dataOut.type == 'Spectra':
959 963 x = dataOut.data_spc[channelIndexList,:,:]/factor
960 964 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
961 965 x = numpy.average(x, axis=1)
962 966
963 967
964 968 xdB = 10*numpy.log10(x)
965 969
966 970 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
967 971 title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y"))
968 972 xlabel = "dB"
969 973 ylabel = "Range (Km)"
970 974
971 975 if not self.isConfig:
972 976
973 977 nplots = 1
974 978
975 979 self.setup(id=id,
976 980 nplots=nplots,
977 981 wintitle=wintitle,
978 982 show=show)
979 983
980 984 if ymin == None: ymin = numpy.nanmin(y)
981 985 if ymax == None: ymax = numpy.nanmax(y)
982 986 if xmin == None: xmin = numpy.nanmin(xdB)*0.9
983 987 if xmax == None: xmax = numpy.nanmax(xdB)*1.1
984 988
985 989 self.isConfig = True
986 990
987 991 self.setWinTitle(title)
988 992
989 993 title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
990 994 axes = self.axesList[0]
991 995
992 996 legendlabels = ["channel %d"%x for x in channelList]
993 997 axes.pmultiline(xdB, y,
994 998 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
995 999 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
996 1000 ytick_visible=True, nxticks=5,
997 1001 grid='x')
998 1002
999 1003 self.draw()
1000 1004
1001 1005 self.save(figpath=figpath,
1002 1006 figfile=figfile,
1003 1007 save=save,
1004 1008 ftp=ftp,
1005 1009 wr_period=wr_period,
1006 1010 thisDatetime=thisDatetime)
1007 1011
1008 1012 return dataOut
1009 1013
1010 1014 @MPDecorator
1011 1015 class SpectraCutPlot_(Figure):
1012 1016
1013 1017 isConfig = None
1014 1018 __nsubplots = None
1015 1019
1016 1020 WIDTHPROF = None
1017 1021 HEIGHTPROF = None
1018 1022 PREFIX = 'spc_cut'
1019 1023
1020 1024 def __init__(self):
1021 1025 Figure.__init__(self)
1022 1026 self.isConfig = False
1023 1027 self.__nsubplots = 1
1024 1028
1025 1029 self.PLOT_CODE = POWER_CODE
1026 1030
1027 1031 self.WIDTH = 700
1028 1032 self.HEIGHT = 500
1029 1033 self.counter_imagwr = 0
1030 1034
1031 1035 def getSubplots(self):
1032 1036 ncol = 1
1033 1037 nrow = 1
1034 1038
1035 1039 return nrow, ncol
1036 1040
1037 1041 def setup(self, id, nplots, wintitle, show):
1038 1042
1039 1043 self.nplots = nplots
1040 1044
1041 1045 ncolspan = 1
1042 1046 colspan = 1
1043 1047
1044 1048 self.createFigure(id = id,
1045 1049 wintitle = wintitle,
1046 1050 widthplot = self.WIDTH,
1047 1051 heightplot = self.HEIGHT,
1048 1052 show=show)
1049 1053
1050 1054 nrow, ncol = self.getSubplots()
1051 1055
1052 1056 counter = 0
1053 1057 for y in range(nrow):
1054 1058 for x in range(ncol):
1055 1059 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
1056 1060
1057 1061 def run(self, dataOut, id, wintitle="", channelList=None,
1058 1062 xmin=None, xmax=None, ymin=None, ymax=None,
1059 1063 save=False, figpath='./', figfile=None, show=True,
1060 1064 ftp=False, wr_period=1, server=None,
1061 1065 folder=None, username=None, password=None,
1062 1066 xaxis="frequency"):
1063 1067
1064 1068 if dataOut.flagNoData:
1065 1069 return dataOut
1066 1070
1067 1071 if channelList == None:
1068 1072 channelIndexList = dataOut.channelIndexList
1069 1073 channelList = dataOut.channelList
1070 1074 else:
1071 1075 channelIndexList = []
1072 1076 for channel in channelList:
1073 1077 if channel not in dataOut.channelList:
1074 1078 raise ValueError("Channel %d is not in dataOut.channelList")
1075 1079 channelIndexList.append(dataOut.channelList.index(channel))
1076 1080
1077 1081 factor = dataOut.normFactor
1078 1082
1079 1083 y = dataOut.getHeiRange()
1080 1084
1081 1085 z = dataOut.data_spc/factor
1082 1086 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
1083 1087
1084 1088 hei_index = numpy.arange(25)*3 + 20
1085 1089
1086 1090 if xaxis == "frequency":
1087 1091 x = dataOut.getFreqRange()/1000.
1088 1092 zdB = 10*numpy.log10(z[0,:,hei_index])
1089 1093 xlabel = "Frequency (kHz)"
1090 1094 ylabel = "Power (dB)"
1091 1095
1092 1096 elif xaxis == "time":
1093 1097 x = dataOut.getAcfRange()
1094 1098 zdB = z[0,:,hei_index]
1095 1099 xlabel = "Time (ms)"
1096 1100 ylabel = "ACF"
1097 1101
1098 1102 else:
1099 1103 x = dataOut.getVelRange()
1100 1104 zdB = 10*numpy.log10(z[0,:,hei_index])
1101 1105 xlabel = "Velocity (m/s)"
1102 1106 ylabel = "Power (dB)"
1103 1107
1104 1108 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1105 1109 title = wintitle + " Range Cuts %s" %(thisDatetime.strftime("%d-%b-%Y"))
1106 1110
1107 1111 if not self.isConfig:
1108 1112
1109 1113 nplots = 1
1110 1114
1111 1115 self.setup(id=id,
1112 1116 nplots=nplots,
1113 1117 wintitle=wintitle,
1114 1118 show=show)
1115 1119
1116 1120 if xmin == None: xmin = numpy.nanmin(x)*0.9
1117 1121 if xmax == None: xmax = numpy.nanmax(x)*1.1
1118 1122 if ymin == None: ymin = numpy.nanmin(zdB)
1119 1123 if ymax == None: ymax = numpy.nanmax(zdB)
1120 1124
1121 1125 self.isConfig = True
1122 1126
1123 1127 self.setWinTitle(title)
1124 1128
1125 1129 title = "Spectra Cuts: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
1126 1130 axes = self.axesList[0]
1127 1131
1128 1132 legendlabels = ["Range = %dKm" %y[i] for i in hei_index]
1129 1133
1130 1134 axes.pmultilineyaxis( x, zdB,
1131 1135 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
1132 1136 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
1133 1137 ytick_visible=True, nxticks=5,
1134 1138 grid='x')
1135 1139
1136 1140 self.draw()
1137 1141
1138 1142 self.save(figpath=figpath,
1139 1143 figfile=figfile,
1140 1144 save=save,
1141 1145 ftp=ftp,
1142 1146 wr_period=wr_period,
1143 1147 thisDatetime=thisDatetime)
1144 1148
1145 1149 return dataOut
1146 1150
1147 1151 @MPDecorator
1148 1152 class Noise_(Figure):
1149 1153
1150 1154 isConfig = None
1151 1155 __nsubplots = None
1152 1156
1153 1157 PREFIX = 'noise'
1154 1158
1155 1159
1156 1160 def __init__(self):
1157 1161 Figure.__init__(self)
1158 1162 self.timerange = 24*60*60
1159 1163 self.isConfig = False
1160 1164 self.__nsubplots = 1
1161 1165 self.counter_imagwr = 0
1162 1166 self.WIDTH = 800
1163 1167 self.HEIGHT = 400
1164 1168 self.WIDTHPROF = 120
1165 1169 self.HEIGHTPROF = 0
1166 1170 self.xdata = None
1167 1171 self.ydata = None
1168 1172
1169 1173 self.PLOT_CODE = NOISE_CODE
1170 1174
1171 1175 self.FTP_WEI = None
1172 1176 self.EXP_CODE = None
1173 1177 self.SUB_EXP_CODE = None
1174 1178 self.PLOT_POS = None
1175 1179 self.figfile = None
1176 1180
1177 1181 self.xmin = None
1178 1182 self.xmax = None
1179 1183
1180 1184 def getSubplots(self):
1181 1185
1182 1186 ncol = 1
1183 1187 nrow = 1
1184 1188
1185 1189 return nrow, ncol
1186 1190
1187 1191 def openfile(self, filename):
1188 1192 dirname = os.path.dirname(filename)
1189 1193
1190 1194 if not os.path.exists(dirname):
1191 1195 os.mkdir(dirname)
1192 1196
1193 1197 f = open(filename,'w+')
1194 1198 f.write('\n\n')
1195 1199 f.write('JICAMARCA RADIO OBSERVATORY - Noise \n')
1196 1200 f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' )
1197 1201 f.close()
1198 1202
1199 1203 def save_data(self, filename_phase, data, data_datetime):
1200 1204
1201 1205 f=open(filename_phase,'a')
1202 1206
1203 1207 timetuple_data = data_datetime.timetuple()
1204 1208 day = str(timetuple_data.tm_mday)
1205 1209 month = str(timetuple_data.tm_mon)
1206 1210 year = str(timetuple_data.tm_year)
1207 1211 hour = str(timetuple_data.tm_hour)
1208 1212 minute = str(timetuple_data.tm_min)
1209 1213 second = str(timetuple_data.tm_sec)
1210 1214
1211 1215 data_msg = ''
1212 1216 for i in range(len(data)):
1213 1217 data_msg += str(data[i]) + ' '
1214 1218
1215 1219 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' ' + data_msg + '\n')
1216 1220 f.close()
1217 1221
1218 1222
1219 1223 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1220 1224
1221 1225 self.__showprofile = showprofile
1222 1226 self.nplots = nplots
1223 1227
1224 1228 ncolspan = 7
1225 1229 colspan = 6
1226 1230 self.__nsubplots = 2
1227 1231
1228 1232 self.createFigure(id = id,
1229 1233 wintitle = wintitle,
1230 1234 widthplot = self.WIDTH+self.WIDTHPROF,
1231 1235 heightplot = self.HEIGHT+self.HEIGHTPROF,
1232 1236 show=show)
1233 1237
1234 1238 nrow, ncol = self.getSubplots()
1235 1239
1236 1240 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1237 1241
1238 1242
1239 1243 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1240 1244 xmin=None, xmax=None, ymin=None, ymax=None,
1241 1245 timerange=None,
1242 1246 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1243 1247 server=None, folder=None, username=None, password=None,
1244 1248 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1245 1249
1246 1250 if dataOut.flagNoData:
1247 1251 return dataOut
1248 1252
1249 1253 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1250 1254 return
1251 1255
1252 1256 if channelList == None:
1253 1257 channelIndexList = dataOut.channelIndexList
1254 1258 channelList = dataOut.channelList
1255 1259 else:
1256 1260 channelIndexList = []
1257 1261 for channel in channelList:
1258 1262 if channel not in dataOut.channelList:
1259 1263 raise ValueError("Channel %d is not in dataOut.channelList")
1260 1264 channelIndexList.append(dataOut.channelList.index(channel))
1261 1265
1262 1266 x = dataOut.getTimeRange()
1263 1267 #y = dataOut.getHeiRange()
1264 1268 factor = dataOut.normFactor
1265 1269 noise = dataOut.noise[channelIndexList]/factor
1266 1270 noisedB = 10*numpy.log10(noise)
1267 1271
1268 1272 thisDatetime = dataOut.datatime
1269 1273
1270 1274 title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1271 1275 xlabel = ""
1272 1276 ylabel = "Intensity (dB)"
1273 1277 update_figfile = False
1274 1278
1275 1279 if not self.isConfig:
1276 1280
1277 1281 nplots = 1
1278 1282
1279 1283 self.setup(id=id,
1280 1284 nplots=nplots,
1281 1285 wintitle=wintitle,
1282 1286 showprofile=showprofile,
1283 1287 show=show)
1284 1288
1285 1289 if timerange != None:
1286 1290 self.timerange = timerange
1287 1291
1288 1292 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1289 1293
1290 1294 if ymin == None: ymin = numpy.floor(numpy.nanmin(noisedB)) - 10.0
1291 1295 if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0
1292 1296
1293 1297 self.FTP_WEI = ftp_wei
1294 1298 self.EXP_CODE = exp_code
1295 1299 self.SUB_EXP_CODE = sub_exp_code
1296 1300 self.PLOT_POS = plot_pos
1297 1301
1298 1302
1299 1303 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1300 1304 self.isConfig = True
1301 1305 self.figfile = figfile
1302 1306 self.xdata = numpy.array([])
1303 1307 self.ydata = numpy.array([])
1304 1308
1305 1309 update_figfile = True
1306 1310
1307 1311 #open file beacon phase
1308 1312 path = '%s%03d' %(self.PREFIX, self.id)
1309 1313 noise_file = os.path.join(path,'%s.txt'%self.name)
1310 1314 self.filename_noise = os.path.join(figpath,noise_file)
1311 1315
1312 1316 self.setWinTitle(title)
1313 1317
1314 1318 title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1315 1319
1316 1320 legendlabels = ["channel %d"%(idchannel) for idchannel in channelList]
1317 1321 axes = self.axesList[0]
1318 1322
1319 1323 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1320 1324
1321 1325 if len(self.ydata)==0:
1322 1326 self.ydata = noisedB.reshape(-1,1)
1323 1327 else:
1324 1328 self.ydata = numpy.hstack((self.ydata, noisedB.reshape(-1,1)))
1325 1329
1326 1330
1327 1331 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1328 1332 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1329 1333 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1330 1334 XAxisAsTime=True, grid='both'
1331 1335 )
1332 1336
1333 1337 self.draw()
1334 1338
1335 1339 if dataOut.ltctime >= self.xmax:
1336 1340 self.counter_imagwr = wr_period
1337 1341 self.isConfig = False
1338 1342 update_figfile = True
1339 1343
1340 1344 self.save(figpath=figpath,
1341 1345 figfile=figfile,
1342 1346 save=save,
1343 1347 ftp=ftp,
1344 1348 wr_period=wr_period,
1345 1349 thisDatetime=thisDatetime,
1346 1350 update_figfile=update_figfile)
1347 1351
1348 1352 #store data beacon phase
1349 1353 if save:
1350 1354 self.save_data(self.filename_noise, noisedB, thisDatetime)
1351 1355
1352 1356 return dataOut
1353 1357
1354 1358 @MPDecorator
1355 1359 class BeaconPhase_(Figure):
1356 1360
1357 1361 __isConfig = None
1358 1362 __nsubplots = None
1359 1363
1360 1364 PREFIX = 'beacon_phase'
1361 1365
1362 1366 def __init__(self):
1363 1367 Figure.__init__(self)
1364 1368 self.timerange = 24*60*60
1365 1369 self.isConfig = False
1366 1370 self.__nsubplots = 1
1367 1371 self.counter_imagwr = 0
1368 1372 self.WIDTH = 800
1369 1373 self.HEIGHT = 400
1370 1374 self.WIDTHPROF = 120
1371 1375 self.HEIGHTPROF = 0
1372 1376 self.xdata = None
1373 1377 self.ydata = None
1374 1378
1375 1379 self.PLOT_CODE = BEACON_CODE
1376 1380
1377 1381 self.FTP_WEI = None
1378 1382 self.EXP_CODE = None
1379 1383 self.SUB_EXP_CODE = None
1380 1384 self.PLOT_POS = None
1381 1385
1382 1386 self.filename_phase = None
1383 1387
1384 1388 self.figfile = None
1385 1389
1386 1390 self.xmin = None
1387 1391 self.xmax = None
1388 1392
1389 1393 def getSubplots(self):
1390 1394
1391 1395 ncol = 1
1392 1396 nrow = 1
1393 1397
1394 1398 return nrow, ncol
1395 1399
1396 1400 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1397 1401
1398 1402 self.__showprofile = showprofile
1399 1403 self.nplots = nplots
1400 1404
1401 1405 ncolspan = 7
1402 1406 colspan = 6
1403 1407 self.__nsubplots = 2
1404 1408
1405 1409 self.createFigure(id = id,
1406 1410 wintitle = wintitle,
1407 1411 widthplot = self.WIDTH+self.WIDTHPROF,
1408 1412 heightplot = self.HEIGHT+self.HEIGHTPROF,
1409 1413 show=show)
1410 1414
1411 1415 nrow, ncol = self.getSubplots()
1412 1416
1413 1417 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1414 1418
1415 1419 def save_phase(self, filename_phase):
1416 1420 f = open(filename_phase,'w+')
1417 1421 f.write('\n\n')
1418 1422 f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n')
1419 1423 f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' )
1420 1424 f.close()
1421 1425
1422 1426 def save_data(self, filename_phase, data, data_datetime):
1423 1427 f=open(filename_phase,'a')
1424 1428 timetuple_data = data_datetime.timetuple()
1425 1429 day = str(timetuple_data.tm_mday)
1426 1430 month = str(timetuple_data.tm_mon)
1427 1431 year = str(timetuple_data.tm_year)
1428 1432 hour = str(timetuple_data.tm_hour)
1429 1433 minute = str(timetuple_data.tm_min)
1430 1434 second = str(timetuple_data.tm_sec)
1431 1435 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n')
1432 1436 f.close()
1433 1437
1434 1438
1435 1439 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1436 1440 xmin=None, xmax=None, ymin=None, ymax=None, hmin=None, hmax=None,
1437 1441 timerange=None,
1438 1442 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1439 1443 server=None, folder=None, username=None, password=None,
1440 1444 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1441 1445
1442 1446 if dataOut.flagNoData:
1443 1447 return dataOut
1444 1448
1445 1449 if not isTimeInHourRange(dataOut.datatime, xmin, xmax):
1446 1450 return
1447 1451
1448 1452 if pairsList == None:
1449 1453 pairsIndexList = dataOut.pairsIndexList[:10]
1450 1454 else:
1451 1455 pairsIndexList = []
1452 1456 for pair in pairsList:
1453 1457 if pair not in dataOut.pairsList:
1454 1458 raise ValueError("Pair %s is not in dataOut.pairsList" %(pair))
1455 1459 pairsIndexList.append(dataOut.pairsList.index(pair))
1456 1460
1457 1461 if pairsIndexList == []:
1458 1462 return
1459 1463
1460 1464 # if len(pairsIndexList) > 4:
1461 1465 # pairsIndexList = pairsIndexList[0:4]
1462 1466
1463 1467 hmin_index = None
1464 1468 hmax_index = None
1465 1469
1466 1470 if hmin != None and hmax != None:
1467 1471 indexes = numpy.arange(dataOut.nHeights)
1468 1472 hmin_list = indexes[dataOut.heightList >= hmin]
1469 1473 hmax_list = indexes[dataOut.heightList <= hmax]
1470 1474
1471 1475 if hmin_list.any():
1472 1476 hmin_index = hmin_list[0]
1473 1477
1474 1478 if hmax_list.any():
1475 1479 hmax_index = hmax_list[-1]+1
1476 1480
1477 1481 x = dataOut.getTimeRange()
1478 1482 #y = dataOut.getHeiRange()
1479 1483
1480 1484
1481 1485 thisDatetime = dataOut.datatime
1482 1486
1483 1487 title = wintitle + " Signal Phase" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1484 1488 xlabel = "Local Time"
1485 1489 ylabel = "Phase (degrees)"
1486 1490
1487 1491 update_figfile = False
1488 1492
1489 1493 nplots = len(pairsIndexList)
1490 1494 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1491 1495 phase_beacon = numpy.zeros(len(pairsIndexList))
1492 1496 for i in range(nplots):
1493 1497 pair = dataOut.pairsList[pairsIndexList[i]]
1494 1498 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i], :, hmin_index:hmax_index], axis=0)
1495 1499 powa = numpy.average(dataOut.data_spc[pair[0], :, hmin_index:hmax_index], axis=0)
1496 1500 powb = numpy.average(dataOut.data_spc[pair[1], :, hmin_index:hmax_index], axis=0)
1497 1501 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
1498 1502 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
1499 1503
1500 #print "Phase %d%d" %(pair[0], pair[1])
1501 #print phase[dataOut.beacon_heiIndexList]
1502
1503 1504 if dataOut.beacon_heiIndexList:
1504 1505 phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList])
1505 1506 else:
1506 1507 phase_beacon[i] = numpy.average(phase)
1507 1508
1508 1509 if not self.isConfig:
1509 1510
1510 1511 nplots = len(pairsIndexList)
1511 1512
1512 1513 self.setup(id=id,
1513 1514 nplots=nplots,
1514 1515 wintitle=wintitle,
1515 1516 showprofile=showprofile,
1516 1517 show=show)
1517 1518
1518 1519 if timerange != None:
1519 1520 self.timerange = timerange
1520 1521
1521 1522 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1522 1523
1523 1524 if ymin == None: ymin = 0
1524 1525 if ymax == None: ymax = 360
1525 1526
1526 1527 self.FTP_WEI = ftp_wei
1527 1528 self.EXP_CODE = exp_code
1528 1529 self.SUB_EXP_CODE = sub_exp_code
1529 1530 self.PLOT_POS = plot_pos
1530 1531
1531 1532 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1532 1533 self.isConfig = True
1533 1534 self.figfile = figfile
1534 1535 self.xdata = numpy.array([])
1535 1536 self.ydata = numpy.array([])
1536 1537
1537 1538 update_figfile = True
1538 1539
1539 1540 #open file beacon phase
1540 1541 path = '%s%03d' %(self.PREFIX, self.id)
1541 1542 beacon_file = os.path.join(path,'%s.txt'%self.name)
1542 1543 self.filename_phase = os.path.join(figpath,beacon_file)
1543 1544 #self.save_phase(self.filename_phase)
1544 1545
1545 1546
1546 1547 #store data beacon phase
1547 1548 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1548 1549
1549 1550 self.setWinTitle(title)
1550 1551
1551 1552
1552 1553 title = "Phase Plot %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1553 1554
1554 1555 legendlabels = ["Pair (%d,%d)"%(pair[0], pair[1]) for pair in dataOut.pairsList]
1555 1556
1556 1557 axes = self.axesList[0]
1557 1558
1558 1559 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1559 1560
1560 1561 if len(self.ydata)==0:
1561 1562 self.ydata = phase_beacon.reshape(-1,1)
1562 1563 else:
1563 1564 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1564 1565
1565 1566
1566 1567 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1567 1568 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1568 1569 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1569 1570 XAxisAsTime=True, grid='both'
1570 1571 )
1571 1572
1572 1573 self.draw()
1573 1574
1574 1575 if dataOut.ltctime >= self.xmax:
1575 1576 self.counter_imagwr = wr_period
1576 1577 self.isConfig = False
1577 1578 update_figfile = True
1578 1579
1579 1580 self.save(figpath=figpath,
1580 1581 figfile=figfile,
1581 1582 save=save,
1582 1583 ftp=ftp,
1583 1584 wr_period=wr_period,
1584 1585 thisDatetime=thisDatetime,
1585 1586 update_figfile=update_figfile)
1586 1587
1587 1588 return dataOut No newline at end of file
@@ -1,501 +1,500
1 1 import os
2 2 import sys
3 3 import datetime
4 4 import numpy
5 5 import matplotlib
6 6
7 7 if 'BACKEND' in os.environ:
8 8 matplotlib.use(os.environ['BACKEND'])
9 9 elif 'linux' in sys.platform:
10 10 matplotlib.use("TkAgg")
11 11 elif 'darwin' in sys.platform:
12 12 matplotlib.use('TkAgg')
13 13 else:
14 14 from schainpy.utils import log
15 15 log.warning('Using default Backend="Agg"', 'INFO')
16 16 matplotlib.use('Agg')
17 17 # Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'
18 18 import matplotlib.pyplot
19 19
20 20 from mpl_toolkits.axes_grid1 import make_axes_locatable
21 21 from matplotlib.ticker import FuncFormatter, LinearLocator
22 22
23 23 ###########################################
24 24 # Actualizacion de las funciones del driver
25 25 ###########################################
26 26
27 27 # create jro colormap
28 28
29 29 jet_values = matplotlib.pyplot.get_cmap("jet", 100)(numpy.arange(100))[10:90]
30 30 blu_values = matplotlib.pyplot.get_cmap(
31 31 "seismic_r", 20)(numpy.arange(20))[10:15]
32 32 ncmap = matplotlib.colors.LinearSegmentedColormap.from_list(
33 33 "jro", numpy.vstack((blu_values, jet_values)))
34 34 matplotlib.pyplot.register_cmap(cmap=ncmap)
35 35
36 36
37 37 def createFigure(id, wintitle, width, height, facecolor="w", show=True, dpi=80):
38 38
39 39 matplotlib.pyplot.ioff()
40 40
41 41 fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor, figsize=(
42 42 1.0 * width / dpi, 1.0 * height / dpi))
43 43 fig.canvas.manager.set_window_title(wintitle)
44 44 # fig.canvas.manager.resize(width, height)
45 45 matplotlib.pyplot.ion()
46 46
47 47 if show:
48 48 matplotlib.pyplot.show()
49 49
50 50 return fig
51 51
52 52
53 53 def closeFigure(show=False, fig=None):
54 54
55 55 # matplotlib.pyplot.ioff()
56 56 # matplotlib.pyplot.pause(0)
57 57
58 58 if show:
59 59 matplotlib.pyplot.show()
60 60
61 61 if fig != None:
62 62 matplotlib.pyplot.close(fig)
63 63 # matplotlib.pyplot.pause(0)
64 64 # matplotlib.pyplot.ion()
65 65
66 66 return
67 67
68 68 matplotlib.pyplot.close("all")
69 69 # matplotlib.pyplot.pause(0)
70 70 # matplotlib.pyplot.ion()
71 71
72 72 return
73 73
74 74
75 75 def saveFigure(fig, filename):
76 76
77 77 # matplotlib.pyplot.ioff()
78 78 fig.savefig(filename, dpi=matplotlib.pyplot.gcf().dpi)
79 79 # matplotlib.pyplot.ion()
80 80
81 81
82 82 def clearFigure(fig):
83 83
84 84 fig.clf()
85 85
86 86
87 87 def setWinTitle(fig, title):
88 88
89 89 fig.canvas.manager.set_window_title(title)
90 90
91 91
92 92 def setTitle(fig, title):
93 93
94 94 fig.suptitle(title)
95 95
96 96
97 97 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan, polar=False):
98 98
99 99 matplotlib.pyplot.ioff()
100 100 matplotlib.pyplot.figure(fig.number)
101 101 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
102 102 (xpos, ypos),
103 103 colspan=colspan,
104 104 rowspan=rowspan,
105 105 polar=polar)
106 106
107 107 matplotlib.pyplot.ion()
108 108 return axes
109 109
110 110
111 111 def setAxesText(ax, text):
112 112
113 113 ax.annotate(text,
114 114 xy=(.1, .99),
115 115 xycoords='figure fraction',
116 116 horizontalalignment='left',
117 117 verticalalignment='top',
118 118 fontsize=10)
119 119
120 120
121 121 def printLabels(ax, xlabel, ylabel, title):
122 122
123 123 ax.set_xlabel(xlabel, size=11)
124 124 ax.set_ylabel(ylabel, size=11)
125 125 ax.set_title(title, size=8)
126 126
127 127
128 128 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='',
129 129 ticksize=9, xtick_visible=True, ytick_visible=True,
130 130 nxticks=4, nyticks=10,
131 131 grid=None, color='blue'):
132 132 """
133 133
134 134 Input:
135 135 grid : None, 'both', 'x', 'y'
136 136 """
137 137
138 138 matplotlib.pyplot.ioff()
139 139
140 140 ax.set_xlim([xmin, xmax])
141 141 ax.set_ylim([ymin, ymax])
142 142
143 143 printLabels(ax, xlabel, ylabel, title)
144 144
145 145 ######################################################
146 146 if (xmax - xmin) <= 1:
147 147 xtickspos = numpy.linspace(xmin, xmax, nxticks)
148 148 xtickspos = numpy.array([float("%.1f" % i) for i in xtickspos])
149 149 ax.set_xticks(xtickspos)
150 150 else:
151 151 xtickspos = numpy.arange(nxticks) * \
152 152 int((xmax - xmin) / (nxticks)) + int(xmin)
153 153 # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin)
154 154 ax.set_xticks(xtickspos)
155 155
156 156 for tick in ax.get_xticklabels():
157 157 tick.set_visible(xtick_visible)
158 158
159 159 for tick in ax.xaxis.get_major_ticks():
160 160 tick.label.set_fontsize(ticksize)
161 161
162 162 ######################################################
163 163 for tick in ax.get_yticklabels():
164 164 tick.set_visible(ytick_visible)
165 165
166 166 for tick in ax.yaxis.get_major_ticks():
167 167 tick.label.set_fontsize(ticksize)
168 168
169 169 ax.plot(x, y, color=color)
170 170 iplot = ax.lines[-1]
171 171
172 172 ######################################################
173 173 if '0.' in matplotlib.__version__[0:2]:
174 174 print("The matplotlib version has to be updated to 1.1 or newer")
175 175 return iplot
176 176
177 177 if '1.0.' in matplotlib.__version__[0:4]:
178 178 print("The matplotlib version has to be updated to 1.1 or newer")
179 179 return iplot
180 180
181 181 if grid != None:
182 182 ax.grid(b=True, which='major', axis=grid)
183 183
184 184 matplotlib.pyplot.tight_layout()
185 185
186 186 matplotlib.pyplot.ion()
187 187
188 188 return iplot
189 189
190 190
191 191 def set_linedata(ax, x, y, idline):
192 192
193 193 ax.lines[idline].set_data(x, y)
194 194
195 195
196 196 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
197 197
198 198 ax = iplot.axes
199 199
200 200 printLabels(ax, xlabel, ylabel, title)
201 201
202 202 set_linedata(ax, x, y, idline=0)
203 203
204 204
205 205 def addpline(ax, x, y, color, linestyle, lw):
206 206
207 207 ax.plot(x, y, color=color, linestyle=linestyle, lw=lw)
208 208
209 209
210 210 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
211 211 xlabel='', ylabel='', title='', ticksize=9,
212 212 colormap='jet', cblabel='', cbsize="5%",
213 213 XAxisAsTime=False):
214 214
215 215 matplotlib.pyplot.ioff()
216 216
217 217 divider = make_axes_locatable(ax)
218 218 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
219 219 fig = ax.get_figure()
220 220 fig.add_axes(ax_cb)
221 221
222 222 ax.set_xlim([xmin, xmax])
223 223 ax.set_ylim([ymin, ymax])
224 224
225 225 printLabels(ax, xlabel, ylabel, title)
226 226
227 227 z = numpy.ma.masked_invalid(z)
228 228 cmap = matplotlib.pyplot.get_cmap(colormap)
229 229 cmap.set_bad('black', 1.)
230 230 imesh = ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap)
231 231 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
232 232 cb.set_label(cblabel)
233 233
234 234 # for tl in ax_cb.get_yticklabels():
235 235 # tl.set_visible(True)
236 236
237 237 for tick in ax.yaxis.get_major_ticks():
238 238 tick.label.set_fontsize(ticksize)
239 239
240 240 for tick in ax.xaxis.get_major_ticks():
241 241 tick.label.set_fontsize(ticksize)
242 242
243 243 for tick in cb.ax.get_yticklabels():
244 244 tick.set_fontsize(ticksize)
245 245
246 246 ax_cb.yaxis.tick_right()
247 247
248 248 if '0.' in matplotlib.__version__[0:2]:
249 249 print("The matplotlib version has to be updated to 1.1 or newer")
250 250 return imesh
251 251
252 252 if '1.0.' in matplotlib.__version__[0:4]:
253 253 print("The matplotlib version has to be updated to 1.1 or newer")
254 254 return imesh
255 255
256 256 matplotlib.pyplot.tight_layout()
257 257
258 258 if XAxisAsTime:
259 259
260 260 def func(x, pos): return ('%s') % (
261 261 datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
262 262 ax.xaxis.set_major_formatter(FuncFormatter(func))
263 263 ax.xaxis.set_major_locator(LinearLocator(7))
264 264
265 265 matplotlib.pyplot.ion()
266 266 return imesh
267 267
268 268
269 269 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
270 270
271 271 z = z.T
272 272 ax = imesh.axes
273 273 printLabels(ax, xlabel, ylabel, title)
274 274 imesh.set_array(z.ravel())
275 275
276 276
277 277 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
278 278
279 279 printLabels(ax, xlabel, ylabel, title)
280 280
281 281 ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax,
282 282 cmap=matplotlib.pyplot.get_cmap(colormap))
283 283
284 284
285 285 def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
286 286
287 287 printLabels(ax, xlabel, ylabel, title)
288 288
289 289 ax.collections.remove(ax.collections[0])
290 290
291 291 z = numpy.ma.masked_invalid(z)
292 292
293 293 cmap = matplotlib.pyplot.get_cmap(colormap)
294 294 cmap.set_bad('black', 1.)
295 295
296 296 ax.pcolormesh(x, y, z.T, vmin=zmin, vmax=zmax, cmap=cmap)
297 297
298 298
299 299 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
300 300 ticksize=9, xtick_visible=True, ytick_visible=True,
301 301 nxticks=4, nyticks=10,
302 302 grid=None):
303 303 """
304 304
305 305 Input:
306 306 grid : None, 'both', 'x', 'y'
307 307 """
308 308
309 309 matplotlib.pyplot.ioff()
310 310
311 311 lines = ax.plot(x.T, y)
312 312 leg = ax.legend(lines, legendlabels, loc='upper right')
313 313 leg.get_frame().set_alpha(0.5)
314 314 ax.set_xlim([xmin, xmax])
315 315 ax.set_ylim([ymin, ymax])
316 316 printLabels(ax, xlabel, ylabel, title)
317 317
318 318 xtickspos = numpy.arange(nxticks) * \
319 319 int((xmax - xmin) / (nxticks)) + int(xmin)
320 320 ax.set_xticks(xtickspos)
321 321
322 322 for tick in ax.get_xticklabels():
323 323 tick.set_visible(xtick_visible)
324 324
325 325 for tick in ax.xaxis.get_major_ticks():
326 326 tick.label.set_fontsize(ticksize)
327 327
328 328 for tick in ax.get_yticklabels():
329 329 tick.set_visible(ytick_visible)
330 330
331 331 for tick in ax.yaxis.get_major_ticks():
332 332 tick.label.set_fontsize(ticksize)
333 333
334 334 iplot = ax.lines[-1]
335 335
336 336 if '0.' in matplotlib.__version__[0:2]:
337 337 print("The matplotlib version has to be updated to 1.1 or newer")
338 338 return iplot
339 339
340 340 if '1.0.' in matplotlib.__version__[0:4]:
341 341 print("The matplotlib version has to be updated to 1.1 or newer")
342 342 return iplot
343 343
344 344 if grid != None:
345 345 ax.grid(b=True, which='major', axis=grid)
346 346
347 347 matplotlib.pyplot.tight_layout()
348 348
349 349 matplotlib.pyplot.ion()
350 350
351 351 return iplot
352 352
353 353
354 354 def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''):
355 355
356 356 ax = iplot.axes
357 357
358 358 printLabels(ax, xlabel, ylabel, title)
359 359
360 360 for i in range(len(ax.lines)):
361 361 line = ax.lines[i]
362 362 line.set_data(x[i, :], y)
363 363
364 364
365 365 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
366 366 ticksize=9, xtick_visible=True, ytick_visible=True,
367 367 nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None",
368 368 grid=None, XAxisAsTime=False):
369 369 """
370 370
371 371 Input:
372 372 grid : None, 'both', 'x', 'y'
373 373 """
374 374
375 375 matplotlib.pyplot.ioff()
376 376
377 377 # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
378 378 lines = ax.plot(x, y.T)
379 379 # leg = ax.legend(lines, legendlabels, loc=2, bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \
380 380 # handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.)
381 381
382 382 leg = ax.legend(lines, legendlabels,
383 383 loc='upper right', bbox_to_anchor=(1.16, 1), borderaxespad=0)
384 384
385 385 for label in leg.get_texts():
386 386 label.set_fontsize(9)
387 387
388 388 ax.set_xlim([xmin, xmax])
389 389 ax.set_ylim([ymin, ymax])
390 390 printLabels(ax, xlabel, ylabel, title)
391 391
392 392 # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
393 393 # ax.set_xticks(xtickspos)
394 394
395 395 for tick in ax.get_xticklabels():
396 396 tick.set_visible(xtick_visible)
397 397
398 398 for tick in ax.xaxis.get_major_ticks():
399 399 tick.label.set_fontsize(ticksize)
400 400
401 401 for tick in ax.get_yticklabels():
402 402 tick.set_visible(ytick_visible)
403 403
404 404 for tick in ax.yaxis.get_major_ticks():
405 405 tick.label.set_fontsize(ticksize)
406 406
407 407 iplot = ax.lines[-1]
408 408
409 409 if '0.' in matplotlib.__version__[0:2]:
410 410 print("The matplotlib version has to be updated to 1.1 or newer")
411 411 return iplot
412 412
413 413 if '1.0.' in matplotlib.__version__[0:4]:
414 414 print("The matplotlib version has to be updated to 1.1 or newer")
415 415 return iplot
416 416
417 417 if grid != None:
418 418 ax.grid(b=True, which='major', axis=grid)
419 419
420 420 matplotlib.pyplot.tight_layout()
421 421
422 422 if XAxisAsTime:
423 423
424 424 def func(x, pos): return ('%s') % (
425 425 datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
426 426 ax.xaxis.set_major_formatter(FuncFormatter(func))
427 427 ax.xaxis.set_major_locator(LinearLocator(7))
428 428
429 429 matplotlib.pyplot.ion()
430 430
431 431 return iplot
432 432
433 433
434 434 def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
435 435
436 436 ax = iplot.axes
437
438 437 printLabels(ax, xlabel, ylabel, title)
439 438
440 439 for i in range(len(ax.lines)):
441 440 line = ax.lines[i]
442 441 line.set_data(x, y[i, :])
443 442
444 443
445 444 def createPolar(ax, x, y,
446 445 xlabel='', ylabel='', title='', ticksize=9,
447 446 colormap='jet', cblabel='', cbsize="5%",
448 447 XAxisAsTime=False):
449 448
450 449 matplotlib.pyplot.ioff()
451 450
452 451 ax.plot(x, y, 'bo', markersize=5)
453 452 # ax.set_rmax(90)
454 453 ax.set_ylim(0, 90)
455 454 ax.set_yticks(numpy.arange(0, 90, 20))
456 455 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center' ,size='11')
457 456 # ax.text(0, 50, ylabel, rotation='vertical', va ='center', ha = 'left' ,size='11')
458 457 # ax.text(100, 100, 'example', ha='left', va='center', rotation='vertical')
459 458 ax.yaxis.labelpad = 40
460 459 printLabels(ax, xlabel, ylabel, title)
461 460 iplot = ax.lines[-1]
462 461
463 462 if '0.' in matplotlib.__version__[0:2]:
464 463 print("The matplotlib version has to be updated to 1.1 or newer")
465 464 return iplot
466 465
467 466 if '1.0.' in matplotlib.__version__[0:4]:
468 467 print("The matplotlib version has to be updated to 1.1 or newer")
469 468 return iplot
470 469
471 470 # if grid != None:
472 471 # ax.grid(b=True, which='major', axis=grid)
473 472
474 473 matplotlib.pyplot.tight_layout()
475 474
476 475 matplotlib.pyplot.ion()
477 476
478 477 return iplot
479 478
480 479
481 480 def polar(iplot, x, y, xlabel='', ylabel='', title=''):
482 481
483 482 ax = iplot.axes
484 483
485 484 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center',size='11')
486 485 printLabels(ax, xlabel, ylabel, title)
487 486
488 487 set_linedata(ax, x, y, idline=0)
489 488
490 489
491 490 def draw(fig):
492 491
493 492 if type(fig) == 'int':
494 493 raise ValueError("Error drawing: Fig parameter should be a matplotlib figure object figure")
495 494
496 495 fig.canvas.draw()
497 496
498 497
499 498 def pause(interval=0.000001):
500 499
501 500 matplotlib.pyplot.pause(interval) No newline at end of file
@@ -1,369 +1,368
1 1 '''
2 2 Created on Nov 9, 2016
3 3
4 4 @author: roj- LouVD
5 5 '''
6 6
7 7
8 8 import os
9 9 import sys
10 10 import time
11 11 import glob
12 12 import datetime
13 13
14 14 import numpy
15 15
16 16 from schainpy.model.proc.jroproc_base import ProcessingUnit, MPDecorator
17 17 from schainpy.model.data.jrodata import Parameters
18 18 from schainpy.model.io.jroIO_base import JRODataReader, isNumber
19 19 from schainpy.utils import log
20 20
21 21 FILE_HEADER_STRUCTURE = numpy.dtype([
22 22 ('FMN', '<u4'),
23 23 ('nrec', '<u4'),
24 24 ('fr_offset', '<u4'),
25 25 ('id', '<u4'),
26 26 ('site', 'u1', (32,))
27 27 ])
28 28
29 29 REC_HEADER_STRUCTURE = numpy.dtype([
30 30 ('rmn', '<u4'),
31 31 ('rcounter', '<u4'),
32 32 ('nr_offset', '<u4'),
33 33 ('tr_offset', '<u4'),
34 34 ('time', '<u4'),
35 35 ('time_msec', '<u4'),
36 36 ('tag', 'u1', (32,)),
37 37 ('comments', 'u1', (32,)),
38 38 ('lat', '<f4'),
39 39 ('lon', '<f4'),
40 40 ('gps_status', '<u4'),
41 41 ('freq', '<u4'),
42 42 ('freq0', '<u4'),
43 43 ('nchan', '<u4'),
44 44 ('delta_r', '<u4'),
45 45 ('nranges', '<u4'),
46 46 ('r0', '<u4'),
47 47 ('prf', '<u4'),
48 48 ('ncoh', '<u4'),
49 49 ('npoints', '<u4'),
50 50 ('polarization', '<i4'),
51 51 ('rx_filter', '<u4'),
52 52 ('nmodes', '<u4'),
53 53 ('dmode_index', '<u4'),
54 54 ('dmode_rngcorr', '<u4'),
55 55 ('nrxs', '<u4'),
56 56 ('acf_length', '<u4'),
57 57 ('acf_lags', '<u4'),
58 58 ('sea_to_atmos', '<f4'),
59 59 ('sea_notch', '<u4'),
60 60 ('lh_sea', '<u4'),
61 61 ('hh_sea', '<u4'),
62 62 ('nbins_sea', '<u4'),
63 63 ('min_snr', '<f4'),
64 64 ('min_cc', '<f4'),
65 65 ('max_time_diff', '<f4')
66 66 ])
67 67
68 68 DATA_STRUCTURE = numpy.dtype([
69 69 ('range', '<u4'),
70 70 ('status', '<u4'),
71 71 ('zonal', '<f4'),
72 72 ('meridional', '<f4'),
73 73 ('vertical', '<f4'),
74 74 ('zonal_a', '<f4'),
75 75 ('meridional_a', '<f4'),
76 76 ('corrected_fading', '<f4'), # seconds
77 77 ('uncorrected_fading', '<f4'), # seconds
78 78 ('time_diff', '<f4'),
79 79 ('major_axis', '<f4'),
80 80 ('axial_ratio', '<f4'),
81 81 ('orientation', '<f4'),
82 82 ('sea_power', '<u4'),
83 83 ('sea_algorithm', '<u4')
84 84 ])
85 85
86 86 @MPDecorator
87 87 class BLTRParamReader(JRODataReader, ProcessingUnit):
88 88 '''
89 89 Boundary Layer and Tropospheric Radar (BLTR) reader, Wind velocities and SNR from *.sswma files
90 90 '''
91 91
92 92 ext = '.sswma'
93 93
94 94 def __init__(self):
95 95
96 96 ProcessingUnit.__init__(self)
97 97
98 98 self.dataOut = Parameters()
99 99 self.counter_records = 0
100 100 self.flagNoMoreFiles = 0
101 101 self.isConfig = False
102 102 self.filename = None
103 103
104 104 def setup(self,
105 105 path=None,
106 106 startDate=None,
107 107 endDate=None,
108 108 ext=None,
109 109 startTime=datetime.time(0, 0, 0),
110 110 endTime=datetime.time(23, 59, 59),
111 111 timezone=0,
112 112 status_value=0,
113 113 **kwargs):
114
115 114 self.path = path
116 115 self.startDate = startDate
117 116 self.endDate = endDate
118 117 self.startTime = startTime
119 118 self.endTime = endTime
120 119 self.status_value = status_value
121 120 self.datatime = datetime.datetime(1900,1,1)
122 121
123 122 if self.path is None:
124 123 raise ValueError("The path is not valid")
125 124
126 125 if ext is None:
127 126 ext = self.ext
128 127
129 128 self.search_files(self.path, startDate, endDate, ext)
130 129 self.timezone = timezone
131 130 self.fileIndex = 0
132 131
133 132 if not self.fileList:
134 133 raise Warning("There is no files matching these date in the folder: %s. \n Check 'startDate' and 'endDate' " % (
135 134 path))
136 135
137 136 self.setNextFile()
138 137
139 138 def search_files(self, path, startDate, endDate, ext):
140 139 '''
141 140 Searching for BLTR rawdata file in path
142 141 Creating a list of file to proces included in [startDate,endDate]
143 142
144 143 Input:
145 144 path - Path to find BLTR rawdata files
146 145 startDate - Select file from this date
147 146 enDate - Select file until this date
148 147 ext - Extension of the file to read
149 148 '''
150 149
151 150 log.success('Searching files in {} '.format(path), 'BLTRParamReader')
152 151 foldercounter = 0
153 152 fileList0 = glob.glob1(path, "*%s" % ext)
154 153 fileList0.sort()
155 154
156 155 self.fileList = []
157 156 self.dateFileList = []
158 157
159 158 for thisFile in fileList0:
160 159 year = thisFile[-14:-10]
161 160 if not isNumber(year):
162 161 continue
163 162
164 163 month = thisFile[-10:-8]
165 164 if not isNumber(month):
166 165 continue
167 166
168 167 day = thisFile[-8:-6]
169 168 if not isNumber(day):
170 169 continue
171 170
172 171 year, month, day = int(year), int(month), int(day)
173 172 dateFile = datetime.date(year, month, day)
174 173
175 174 if (startDate > dateFile) or (endDate < dateFile):
176 175 continue
177 176
178 177 self.fileList.append(thisFile)
179 178 self.dateFileList.append(dateFile)
180 179
181 180 return
182 181
183 182 def setNextFile(self):
184 183
185 184 file_id = self.fileIndex
186 185
187 186 if file_id == len(self.fileList):
188 187 self.flagNoMoreFiles = 1
189 188 return 0
190 189
191 190 log.success('Opening {}'.format(self.fileList[file_id]), 'BLTRParamReader')
192 191 filename = os.path.join(self.path, self.fileList[file_id])
193 192
194 193 dirname, name = os.path.split(filename)
195 194 # 'peru2' ---> Piura - 'peru1' ---> Huancayo or Porcuya
196 195 self.siteFile = name.split('.')[0]
197 196 if self.filename is not None:
198 197 self.fp.close()
199 198 self.filename = filename
200 199 self.fp = open(self.filename, 'rb')
201 200 self.header_file = numpy.fromfile(self.fp, FILE_HEADER_STRUCTURE, 1)
202 201 self.nrecords = self.header_file['nrec'][0]
203 202 self.sizeOfFile = os.path.getsize(self.filename)
204 203 self.counter_records = 0
205 204 self.flagIsNewFile = 0
206 205 self.fileIndex += 1
207 206
208 207 return 1
209 208
210 209 def readNextBlock(self):
211 210
212 211 while True:
213 212 if self.counter_records == self.nrecords:
214 213 self.flagIsNewFile = 1
215 214 if not self.setNextFile():
216 215 return 0
217 216
218 217 self.readBlock()
219 218
220 219 if (self.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or \
221 220 (self.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
222 221 log.warning(
223 222 'Reading Record No. {}/{} -> {} [Skipping]'.format(
224 223 self.counter_records,
225 224 self.nrecords,
226 225 self.datatime.ctime()),
227 226 'BLTRParamReader')
228 227 continue
229 228 break
230 229
231 230 log.log('Reading Record No. {}/{} -> {}'.format(
232 231 self.counter_records,
233 232 self.nrecords,
234 233 self.datatime.ctime()), 'BLTRParamReader')
235 234
236 235 return 1
237 236
238 237 def readBlock(self):
239 238
240 239 pointer = self.fp.tell()
241 240 header_rec = numpy.fromfile(self.fp, REC_HEADER_STRUCTURE, 1)
242 241 self.nchannels = int(header_rec['nchan'][0] / 2)
243 242 self.kchan = header_rec['nrxs'][0]
244 243 self.nmodes = header_rec['nmodes'][0]
245 244 self.nranges = header_rec['nranges'][0]
246 245 self.fp.seek(pointer)
247 246 self.height = numpy.empty((self.nmodes, self.nranges))
248 247 self.snr = numpy.empty((self.nmodes, int(self.nchannels), self.nranges))
249 248 self.buffer = numpy.empty((self.nmodes, 3, self.nranges))
250 249 self.flagDiscontinuousBlock = 0
251 250
252 251 for mode in range(self.nmodes):
253 252 self.readHeader()
254 253 data = self.readData()
255 254 self.height[mode] = (data[0] - self.correction) / 1000.
256 255 self.buffer[mode] = data[1]
257 256 self.snr[mode] = data[2]
258 257
259 258 self.counter_records = self.counter_records + self.nmodes
260 259
261 260 return
262 261
263 262 def readHeader(self):
264 263 '''
265 264 RecordHeader of BLTR rawdata file
266 265 '''
267 266
268 267 header_structure = numpy.dtype(
269 268 REC_HEADER_STRUCTURE.descr + [
270 269 ('antenna_coord', 'f4', (2, int(self.nchannels))),
271 270 ('rx_gains', 'u4', (int(self.nchannels),)),
272 271 ('rx_analysis', 'u4', (int(self.nchannels),))
273 272 ]
274 273 )
275 274
276 275 self.header_rec = numpy.fromfile(self.fp, header_structure, 1)
277 276 self.lat = self.header_rec['lat'][0]
278 277 self.lon = self.header_rec['lon'][0]
279 278 self.delta = self.header_rec['delta_r'][0]
280 279 self.correction = self.header_rec['dmode_rngcorr'][0]
281 280 self.imode = self.header_rec['dmode_index'][0]
282 281 self.antenna = self.header_rec['antenna_coord']
283 282 self.rx_gains = self.header_rec['rx_gains']
284 283 self.time = self.header_rec['time'][0]
285 284 dt = datetime.datetime.utcfromtimestamp(self.time)
286 285 if dt.date()>self.datatime.date():
287 286 self.flagDiscontinuousBlock = 1
288 287 self.datatime = dt
289 288
290 289 def readData(self):
291 290 '''
292 291 Reading and filtering data block record of BLTR rawdata file, filtering is according to status_value.
293 292
294 293 Input:
295 294 status_value - Array data is set to NAN for values that are not equal to status_value
296 295
297 296 '''
298 297 self.nchannels = int(self.nchannels)
299 298
300 299 data_structure = numpy.dtype(
301 300 DATA_STRUCTURE.descr + [
302 301 ('rx_saturation', 'u4', (self.nchannels,)),
303 302 ('chan_offset', 'u4', (2 * self.nchannels,)),
304 303 ('rx_amp', 'u4', (self.nchannels,)),
305 304 ('rx_snr', 'f4', (self.nchannels,)),
306 305 ('cross_snr', 'f4', (self.kchan,)),
307 306 ('sea_power_relative', 'f4', (self.kchan,))]
308 307 )
309 308
310 309 data = numpy.fromfile(self.fp, data_structure, self.nranges)
311 310
312 311 height = data['range']
313 312 winds = numpy.array(
314 313 (data['zonal'], data['meridional'], data['vertical']))
315 314 snr = data['rx_snr'].T
316 315
317 316 winds[numpy.where(winds == -9999.)] = numpy.nan
318 317 winds[:, numpy.where(data['status'] != self.status_value)] = numpy.nan
319 318 snr[numpy.where(snr == -9999.)] = numpy.nan
320 319 snr[:, numpy.where(data['status'] != self.status_value)] = numpy.nan
321 320 snr = numpy.power(10, snr / 10)
322 321
323 322 return height, winds, snr
324 323
325 324 def set_output(self):
326 325 '''
327 326 Storing data from databuffer to dataOut object
328 327 '''
329 328
330 329 self.dataOut.data_SNR = self.snr
331 330 self.dataOut.height = self.height
332 331 self.dataOut.data = self.buffer
333 332 self.dataOut.utctimeInit = self.time
334 333 self.dataOut.utctime = self.dataOut.utctimeInit
335 334 self.dataOut.useLocalTime = False
336 335 self.dataOut.paramInterval = 157
337 336 self.dataOut.timezone = self.timezone
338 337 self.dataOut.site = self.siteFile
339 338 self.dataOut.nrecords = self.nrecords / self.nmodes
340 339 self.dataOut.sizeOfFile = self.sizeOfFile
341 340 self.dataOut.lat = self.lat
342 341 self.dataOut.lon = self.lon
343 342 self.dataOut.channelList = list(range(self.nchannels))
344 343 self.dataOut.kchan = self.kchan
345 344 self.dataOut.delta = self.delta
346 345 self.dataOut.correction = self.correction
347 346 self.dataOut.nmodes = self.nmodes
348 347 self.dataOut.imode = self.imode
349 348 self.dataOut.antenna = self.antenna
350 349 self.dataOut.rx_gains = self.rx_gains
351 350 self.dataOut.flagNoData = False
352 351 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
353 352
354 353 def getData(self):
355 354 '''
356 355 Storing data from databuffer to dataOut object
357 356 '''
358 357 if self.flagNoMoreFiles:
359 358 self.dataOut.flagNoData = True
360 359 self.dataOut.error = (1, 'No More files to read')
361 360
362 361 if not self.readNextBlock():
363 362 self.dataOut.flagNoData = True
364 363 return 0
365 364
366 365 self.set_output()
367 366
368 367 return 1
369 368 No newline at end of file
@@ -1,1828 +1,1828
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
14 14 import datetime
15 15 import traceback
16 16 import zmq
17 17
18 18 try:
19 19 from gevent import sleep
20 20 except:
21 21 from time import sleep
22 22
23 23 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
24 24 from schainpy.model.data.jroheaderIO import get_dtype_index, get_numpy_dtype, get_procflag_dtype, get_dtype_width
25 25 from schainpy.utils import log
26 26 import schainpy.admin
27 27
28 28 LOCALTIME = True
29 29
30 30
31 31 def isNumber(cad):
32 32 """
33 33 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
34 34
35 35 Excepciones:
36 36 Si un determinado string no puede ser convertido a numero
37 37 Input:
38 38 str, string al cual se le analiza para determinar si convertible a un numero o no
39 39
40 40 Return:
41 41 True : si el string es uno numerico
42 42 False : no es un string numerico
43 43 """
44 44 try:
45 45 float(cad)
46 46 return True
47 47 except:
48 48 return False
49 49
50 50
51 51 def isFileInEpoch(filename, startUTSeconds, endUTSeconds):
52 52 """
53 53 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
54 54
55 55 Inputs:
56 56 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
57 57
58 58 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
59 59 segundos contados desde 01/01/1970.
60 60 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
61 61 segundos contados desde 01/01/1970.
62 62
63 63 Return:
64 64 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
65 65 fecha especificado, de lo contrario retorna False.
66 66
67 67 Excepciones:
68 68 Si el archivo no existe o no puede ser abierto
69 69 Si la cabecera no puede ser leida.
70 70
71 71 """
72 72 basicHeaderObj = BasicHeader(LOCALTIME)
73 73
74 74 try:
75 75 fp = open(filename, 'rb')
76 76 except IOError:
77 77 print("The file %s can't be opened" % (filename))
78 78 return 0
79 79
80 80 sts = basicHeaderObj.read(fp)
81 81 fp.close()
82 82
83 83 if not(sts):
84 84 print("Skipping the file %s because it has not a valid header" % (filename))
85 85 return 0
86 86
87 87 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
88 88 return 0
89 89
90 90 return 1
91 91
92 92
93 93 def isTimeInRange(thisTime, startTime, endTime):
94 94 if endTime >= startTime:
95 95 if (thisTime < startTime) or (thisTime > endTime):
96 96 return 0
97 97 return 1
98 98 else:
99 99 if (thisTime < startTime) and (thisTime > endTime):
100 100 return 0
101 101 return 1
102 102
103 103
104 104 def isFileInTimeRange(filename, startDate, endDate, startTime, endTime):
105 105 """
106 106 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
107 107
108 108 Inputs:
109 109 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
110 110
111 111 startDate : fecha inicial del rango seleccionado en formato datetime.date
112 112
113 113 endDate : fecha final del rango seleccionado en formato datetime.date
114 114
115 115 startTime : tiempo inicial del rango seleccionado en formato datetime.time
116 116
117 117 endTime : tiempo final del rango seleccionado en formato datetime.time
118 118
119 119 Return:
120 120 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
121 121 fecha especificado, de lo contrario retorna False.
122 122
123 123 Excepciones:
124 124 Si el archivo no existe o no puede ser abierto
125 125 Si la cabecera no puede ser leida.
126 126
127 127 """
128 128
129 129 try:
130 130 fp = open(filename, 'rb')
131 131 except IOError:
132 132 print("The file %s can't be opened" % (filename))
133 133 return None
134 134
135 135 firstBasicHeaderObj = BasicHeader(LOCALTIME)
136 136 systemHeaderObj = SystemHeader()
137 137 radarControllerHeaderObj = RadarControllerHeader()
138 138 processingHeaderObj = ProcessingHeader()
139 139
140 140 lastBasicHeaderObj = BasicHeader(LOCALTIME)
141 141
142 142 sts = firstBasicHeaderObj.read(fp)
143 143
144 144 if not(sts):
145 145 print("[Reading] Skipping the file %s because it has not a valid header" % (filename))
146 146 return None
147 147
148 148 if not systemHeaderObj.read(fp):
149 149 return None
150 150
151 151 if not radarControllerHeaderObj.read(fp):
152 152 return None
153 153
154 154 if not processingHeaderObj.read(fp):
155 155 return None
156 156
157 157 filesize = os.path.getsize(filename)
158 158
159 159 offset = processingHeaderObj.blockSize + 24 # header size
160 160
161 161 if filesize <= offset:
162 162 print("[Reading] %s: This file has not enough data" % filename)
163 163 return None
164 164
165 165 fp.seek(-offset, 2)
166 166
167 167 sts = lastBasicHeaderObj.read(fp)
168 168
169 169 fp.close()
170 170
171 171 thisDatetime = lastBasicHeaderObj.datatime
172 172 thisTime_last_block = thisDatetime.time()
173 173
174 174 thisDatetime = firstBasicHeaderObj.datatime
175 175 thisDate = thisDatetime.date()
176 176 thisTime_first_block = thisDatetime.time()
177 177
178 178 # General case
179 179 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
180 180 #-----------o----------------------------o-----------
181 181 # startTime endTime
182 182
183 183 if endTime >= startTime:
184 184 if (thisTime_last_block < startTime) or (thisTime_first_block > endTime):
185 185 return None
186 186
187 187 return thisDatetime
188 188
189 189 # If endTime < startTime then endTime belongs to the next day
190 190
191 191 #<<<<<<<<<<<o o>>>>>>>>>>>
192 192 #-----------o----------------------------o-----------
193 193 # endTime startTime
194 194
195 195 if (thisDate == startDate) and (thisTime_last_block < startTime):
196 196 return None
197 197
198 198 if (thisDate == endDate) and (thisTime_first_block > endTime):
199 199 return None
200 200
201 201 if (thisTime_last_block < startTime) and (thisTime_first_block > endTime):
202 202 return None
203 203
204 204 return thisDatetime
205 205
206 206
207 207 def isFolderInDateRange(folder, startDate=None, endDate=None):
208 208 """
209 209 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
210 210
211 211 Inputs:
212 212 folder : nombre completo del directorio.
213 213 Su formato deberia ser "/path_root/?YYYYDDD"
214 214
215 215 siendo:
216 216 YYYY : Anio (ejemplo 2015)
217 217 DDD : Dia del anio (ejemplo 305)
218 218
219 219 startDate : fecha inicial del rango seleccionado en formato datetime.date
220 220
221 221 endDate : fecha final del rango seleccionado en formato datetime.date
222 222
223 223 Return:
224 224 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
225 225 fecha especificado, de lo contrario retorna False.
226 226 Excepciones:
227 227 Si el directorio no tiene el formato adecuado
228 228 """
229 229
230 230 basename = os.path.basename(folder)
231 231
232 232 if not isRadarFolder(basename):
233 233 print("The folder %s has not the rigth format" % folder)
234 234 return 0
235 235
236 236 if startDate and endDate:
237 237 thisDate = getDateFromRadarFolder(basename)
238 238
239 239 if thisDate < startDate:
240 240 return 0
241 241
242 242 if thisDate > endDate:
243 243 return 0
244 244
245 245 return 1
246 246
247 247
248 248 def isFileInDateRange(filename, startDate=None, endDate=None):
249 249 """
250 250 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
251 251
252 252 Inputs:
253 253 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
254 254
255 255 Su formato deberia ser "?YYYYDDDsss"
256 256
257 257 siendo:
258 258 YYYY : Anio (ejemplo 2015)
259 259 DDD : Dia del anio (ejemplo 305)
260 260 sss : set
261 261
262 262 startDate : fecha inicial del rango seleccionado en formato datetime.date
263 263
264 264 endDate : fecha final del rango seleccionado en formato datetime.date
265 265
266 266 Return:
267 267 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
268 268 fecha especificado, de lo contrario retorna False.
269 269 Excepciones:
270 270 Si el archivo no tiene el formato adecuado
271 271 """
272 272
273 273 basename = os.path.basename(filename)
274 274
275 275 if not isRadarFile(basename):
276 276 print("The filename %s has not the rigth format" % filename)
277 277 return 0
278 278
279 279 if startDate and endDate:
280 280 thisDate = getDateFromRadarFile(basename)
281 281
282 282 if thisDate < startDate:
283 283 return 0
284 284
285 285 if thisDate > endDate:
286 286 return 0
287 287
288 288 return 1
289 289
290 290
291 291 def getFileFromSet(path, ext, set):
292 292 validFilelist = []
293 293 fileList = os.listdir(path)
294 294
295 295 # 0 1234 567 89A BCDE
296 296 # H YYYY DDD SSS .ext
297 297
298 298 for thisFile in fileList:
299 299 try:
300 300 year = int(thisFile[1:5])
301 301 doy = int(thisFile[5:8])
302 302 except:
303 303 continue
304 304
305 305 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
306 306 continue
307 307
308 308 validFilelist.append(thisFile)
309 309
310 310 myfile = fnmatch.filter(
311 311 validFilelist, '*%4.4d%3.3d%3.3d*' % (year, doy, set))
312 312
313 313 if len(myfile) != 0:
314 314 return myfile[0]
315 315 else:
316 316 filename = '*%4.4d%3.3d%3.3d%s' % (year, doy, set, ext.lower())
317 317 print('the filename %s does not exist' % filename)
318 318 print('...going to the last file: ')
319 319
320 320 if validFilelist:
321 321 validFilelist = sorted(validFilelist, key=str.lower)
322 322 return validFilelist[-1]
323 323
324 324 return None
325 325
326 326
327 327 def getlastFileFromPath(path, ext):
328 328 """
329 329 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
330 330 al final de la depuracion devuelve el ultimo file de la lista que quedo.
331 331
332 332 Input:
333 333 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
334 334 ext : extension de los files contenidos en una carpeta
335 335
336 336 Return:
337 337 El ultimo file de una determinada carpeta, no se considera el path.
338 338 """
339 339 validFilelist = []
340 340 fileList = os.listdir(path)
341 341
342 342 # 0 1234 567 89A BCDE
343 343 # H YYYY DDD SSS .ext
344 344
345 345 for thisFile in fileList:
346 346
347 347 year = thisFile[1:5]
348 348 if not isNumber(year):
349 349 continue
350 350
351 351 doy = thisFile[5:8]
352 352 if not isNumber(doy):
353 353 continue
354 354
355 355 year = int(year)
356 356 doy = int(doy)
357 357
358 358 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
359 359 continue
360 360
361 361 validFilelist.append(thisFile)
362 362
363 363 if validFilelist:
364 364 validFilelist = sorted(validFilelist, key=str.lower)
365 365 return validFilelist[-1]
366 366
367 367 return None
368 368
369 369
370 370 def checkForRealPath(path, foldercounter, year, doy, set, ext):
371 371 """
372 372 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
373 373 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
374 374 el path exacto de un determinado file.
375 375
376 376 Example :
377 377 nombre correcto del file es .../.../D2009307/P2009307367.ext
378 378
379 379 Entonces la funcion prueba con las siguientes combinaciones
380 380 .../.../y2009307367.ext
381 381 .../.../Y2009307367.ext
382 382 .../.../x2009307/y2009307367.ext
383 383 .../.../x2009307/Y2009307367.ext
384 384 .../.../X2009307/y2009307367.ext
385 385 .../.../X2009307/Y2009307367.ext
386 386 siendo para este caso, la ultima combinacion de letras, identica al file buscado
387 387
388 388 Return:
389 389 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
390 390 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
391 391 para el filename
392 392 """
393 393 fullfilename = None
394 394 find_flag = False
395 395 filename = None
396 396
397 397 prefixDirList = [None, 'd', 'D']
398 398 if ext.lower() == ".r": # voltage
399 399 prefixFileList = ['d', 'D']
400 400 elif ext.lower() == ".pdata": # spectra
401 401 prefixFileList = ['p', 'P']
402 402 else:
403 403 return None, filename
404 404
405 405 # barrido por las combinaciones posibles
406 406 for prefixDir in prefixDirList:
407 407 thispath = path
408 408 if prefixDir != None:
409 409 # formo el nombre del directorio xYYYYDDD (x=d o x=D)
410 410 if foldercounter == 0:
411 411 thispath = os.path.join(path, "%s%04d%03d" %
412 412 (prefixDir, year, doy))
413 413 else:
414 414 thispath = os.path.join(path, "%s%04d%03d_%02d" % (
415 415 prefixDir, year, doy, foldercounter))
416 416 for prefixFile in prefixFileList: # barrido por las dos combinaciones posibles de "D"
417 417 # formo el nombre del file xYYYYDDDSSS.ext
418 418 filename = "%s%04d%03d%03d%s" % (prefixFile, year, doy, set, ext)
419 419 fullfilename = os.path.join(
420 420 thispath, filename) # formo el path completo
421 421
422 422 if os.path.exists(fullfilename): # verifico que exista
423 423 find_flag = True
424 424 break
425 425 if find_flag:
426 426 break
427 427
428 428 if not(find_flag):
429 429 return None, filename
430 430
431 431 return fullfilename, filename
432 432
433 433
434 434 def isRadarFolder(folder):
435 435 try:
436 436 year = int(folder[1:5])
437 437 doy = int(folder[5:8])
438 438 except:
439 439 return 0
440 440
441 441 return 1
442 442
443 443
444 444 def isRadarFile(file):
445 445 try:
446 446 year = int(file[1:5])
447 447 doy = int(file[5:8])
448 448 set = int(file[8:11])
449 449 except:
450 450 return 0
451 451
452 452 return 1
453 453
454 454
455 455 def getDateFromRadarFile(file):
456 456 try:
457 457 year = int(file[1:5])
458 458 doy = int(file[5:8])
459 459 set = int(file[8:11])
460 460 except:
461 461 return None
462 462
463 463 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
464 464 return thisDate
465 465
466 466
467 467 def getDateFromRadarFolder(folder):
468 468 try:
469 469 year = int(folder[1:5])
470 470 doy = int(folder[5:8])
471 471 except:
472 472 return None
473 473
474 474 thisDate = datetime.date(year, 1, 1) + datetime.timedelta(doy - 1)
475 475 return thisDate
476 476
477 477
478 478 class JRODataIO:
479 479
480 480 c = 3E8
481 481
482 482 isConfig = False
483 483
484 484 basicHeaderObj = None
485 485
486 486 systemHeaderObj = None
487 487
488 488 radarControllerHeaderObj = None
489 489
490 490 processingHeaderObj = None
491 491
492 492 dtype = None
493 493
494 494 pathList = []
495 495
496 496 filenameList = []
497 497
498 498 filename = None
499 499
500 500 ext = None
501 501
502 502 flagIsNewFile = 1
503 503
504 504 flagDiscontinuousBlock = 0
505 505
506 506 flagIsNewBlock = 0
507 507
508 508 fp = None
509 509
510 510 firstHeaderSize = 0
511 511
512 512 basicHeaderSize = 24
513 513
514 514 versionFile = 1103
515 515
516 516 fileSize = None
517 517
518 518 # ippSeconds = None
519 519
520 520 fileSizeByHeader = None
521 521
522 522 fileIndex = None
523 523
524 524 profileIndex = None
525 525
526 526 blockIndex = None
527 527
528 528 nTotalBlocks = None
529 529
530 530 maxTimeStep = 30
531 531
532 532 lastUTTime = None
533 533
534 534 datablock = None
535 535
536 536 dataOut = None
537 537
538 538 blocksize = None
539 539
540 540 getByBlock = False
541 541
542 542 def __init__(self):
543 543
544 544 raise NotImplementedError
545 545
546 546 def run(self):
547 547
548 548 raise NotImplementedError
549 549
550 550 def getDtypeWidth(self):
551 551
552 552 dtype_index = get_dtype_index(self.dtype)
553 553 dtype_width = get_dtype_width(dtype_index)
554 554
555 555 return dtype_width
556 556
557 557 def getAllowedArgs(self):
558 558 if hasattr(self, '__attrs__'):
559 559 return self.__attrs__
560 560 else:
561 561 return inspect.getargspec(self.run).args
562 562
563 563
564 564 class JRODataReader(JRODataIO):
565 565
566 566 online = 0
567 567
568 568 realtime = 0
569 569
570 570 nReadBlocks = 0
571 571
572 572 delay = 10 # number of seconds waiting a new file
573 573
574 574 nTries = 3 # quantity tries
575 575
576 576 nFiles = 3 # number of files for searching
577 577
578 578 path = None
579 579
580 580 foldercounter = 0
581 581
582 582 flagNoMoreFiles = 0
583 583
584 584 datetimeList = []
585 585
586 586 __isFirstTimeOnline = 1
587 587
588 588 __printInfo = True
589 589
590 590 profileIndex = None
591 591
592 592 nTxs = 1
593 593
594 594 txIndex = None
595 595
596 596 # Added--------------------
597 597
598 598 selBlocksize = None
599 599
600 600 selBlocktime = None
601 601
602 602 def __init__(self):
603 603 """
604 604 This class is used to find data files
605 605
606 606 Example:
607 607 reader = JRODataReader()
608 608 fileList = reader.findDataFiles()
609 609
610 610 """
611 611 pass
612 612
613 613 def createObjByDefault(self):
614 614 """
615 615
616 616 """
617 617 raise NotImplementedError
618 618
619 619 def getBlockDimension(self):
620 620
621 621 raise NotImplementedError
622 622
623 623 def searchFilesOffLine(self,
624 624 path,
625 625 startDate=None,
626 626 endDate=None,
627 627 startTime=datetime.time(0, 0, 0),
628 628 endTime=datetime.time(23, 59, 59),
629 629 set=None,
630 630 expLabel='',
631 631 ext='.r',
632 632 cursor=None,
633 633 skip=None,
634 634 walk=True):
635 635
636 636 self.filenameList = []
637 637 self.datetimeList = []
638 638
639 639 pathList = []
640 640
641 641 dateList, pathList = self.findDatafiles(
642 642 path, startDate, endDate, expLabel, ext, walk, include_path=True)
643 643
644 644 if dateList == []:
645 645 return [], []
646 646
647 647 if len(dateList) > 1:
648 648 print("[Reading] Data found for date range [%s - %s]: total days = %d" % (startDate, endDate, len(dateList)))
649 649 else:
650 650 print("[Reading] Data found for date range [%s - %s]: date = %s" % (startDate, endDate, dateList[0]))
651 651
652 652 filenameList = []
653 653 datetimeList = []
654 654
655 655 for thisPath in pathList:
656 656
657 657 fileList = glob.glob1(thisPath, "*%s" % ext)
658 658 fileList.sort()
659 659
660 660 for file in fileList:
661 661
662 662 filename = os.path.join(thisPath, file)
663 663
664 664 if not isFileInDateRange(filename, startDate, endDate):
665 665 continue
666 666
667 667 thisDatetime = isFileInTimeRange(
668 668 filename, startDate, endDate, startTime, endTime)
669 669
670 670 if not(thisDatetime):
671 671 continue
672 672
673 673 filenameList.append(filename)
674 674 datetimeList.append(thisDatetime)
675 675
676 676 if cursor is not None and skip is not None:
677 677 filenameList = filenameList[cursor * skip:cursor * skip + skip]
678 678 datetimeList = datetimeList[cursor * skip:cursor * skip + skip]
679 679
680 680 if not(filenameList):
681 681 print("[Reading] Time range selected invalid [%s - %s]: No *%s files in %s)" % (startTime, endTime, ext, path))
682 682 return [], []
683 683
684 684 print("[Reading] %d file(s) was(were) found in time range: %s - %s" % (len(filenameList), startTime, endTime))
685 685
686 686 # for i in range(len(filenameList)):
687 687 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
688 688
689 689 self.filenameList = filenameList
690 690 self.datetimeList = datetimeList
691 691
692 692 return pathList, filenameList
693 693
694 694 def __searchFilesOnLine(self, path, expLabel="", ext=None, walk=True, set=None):
695 695 """
696 696 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
697 697 devuelve el archivo encontrado ademas de otros datos.
698 698
699 699 Input:
700 700 path : carpeta donde estan contenidos los files que contiene data
701 701
702 702 expLabel : Nombre del subexperimento (subfolder)
703 703
704 704 ext : extension de los files
705 705
706 706 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
707 707
708 708 Return:
709 709 directory : eL directorio donde esta el file encontrado
710 710 filename : el ultimo file de una determinada carpeta
711 711 year : el anho
712 712 doy : el numero de dia del anho
713 713 set : el set del archivo
714 714
715 715
716 716 """
717 717 if not os.path.isdir(path):
718 718 return None, None, None, None, None, None
719 719
720 720 dirList = []
721 721
722 722 if not walk:
723 723 fullpath = path
724 724 foldercounter = 0
725 725 else:
726 726 # Filtra solo los directorios
727 727 for thisPath in os.listdir(path):
728 728 if not os.path.isdir(os.path.join(path, thisPath)):
729 729 continue
730 730 if not isRadarFolder(thisPath):
731 731 continue
732 732
733 733 dirList.append(thisPath)
734 734
735 735 if not(dirList):
736 736 return None, None, None, None, None, None
737 737
738 738 dirList = sorted(dirList, key=str.lower)
739 739
740 740 doypath = dirList[-1]
741 741 foldercounter = int(doypath.split('_')[1]) if len(
742 742 doypath.split('_')) > 1 else 0
743 743 fullpath = os.path.join(path, doypath, expLabel)
744 744
745 745 print("[Reading] %s folder was found: " % (fullpath))
746 746
747 747 if set == None:
748 748 filename = getlastFileFromPath(fullpath, ext)
749 749 else:
750 750 filename = getFileFromSet(fullpath, ext, set)
751 751
752 752 if not(filename):
753 753 return None, None, None, None, None, None
754 754
755 755 print("[Reading] %s file was found" % (filename))
756 756
757 757 if not(self.__verifyFile(os.path.join(fullpath, filename))):
758 758 return None, None, None, None, None, None
759 759
760 760 year = int(filename[1:5])
761 761 doy = int(filename[5:8])
762 762 set = int(filename[8:11])
763 763
764 764 return fullpath, foldercounter, filename, year, doy, set
765 765
766 766 def __setNextFileOffline(self):
767 767
768 768 idFile = self.fileIndex
769 769
770 770 while (True):
771 771 idFile += 1
772 772 if not(idFile < len(self.filenameList)):
773 773 self.flagNoMoreFiles = 1
774 774 # print "[Reading] No more Files"
775 775 return 0
776 776
777 777 filename = self.filenameList[idFile]
778 778
779 779 if not(self.__verifyFile(filename)):
780 780 continue
781 781
782 782 fileSize = os.path.getsize(filename)
783 783 fp = open(filename, 'rb')
784 784 break
785 785
786 786 self.flagIsNewFile = 1
787 787 self.fileIndex = idFile
788 788 self.filename = filename
789 789 self.fileSize = fileSize
790 790 self.fp = fp
791 791
792 792 # print "[Reading] Setting the file: %s"%self.filename
793 793
794 794 return 1
795 795
796 796 def __setNextFileOnline(self):
797 797 """
798 798 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
799 799 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
800 800 siguientes.
801 801
802 802 Affected:
803 803 self.flagIsNewFile
804 804 self.filename
805 805 self.fileSize
806 806 self.fp
807 807 self.set
808 808 self.flagNoMoreFiles
809 809
810 810 Return:
811 811 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
812 812 1 : si el file fue abierto con exito y esta listo a ser leido
813 813
814 814 Excepciones:
815 815 Si un determinado file no puede ser abierto
816 816 """
817 817 nFiles = 0
818 818 fileOk_flag = False
819 819 firstTime_flag = True
820 820
821 821 self.set += 1
822 822
823 823 if self.set > 999:
824 824 self.set = 0
825 825 self.foldercounter += 1
826 826
827 827 # busca el 1er file disponible
828 828 fullfilename, filename = checkForRealPath(
829 829 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
830 830 if fullfilename:
831 831 if self.__verifyFile(fullfilename, False):
832 832 fileOk_flag = True
833 833
834 834 # si no encuentra un file entonces espera y vuelve a buscar
835 835 if not(fileOk_flag):
836 836 # busco en los siguientes self.nFiles+1 files posibles
837 837 for nFiles in range(self.nFiles + 1):
838 838
839 839 if firstTime_flag: # si es la 1era vez entonces hace el for self.nTries veces
840 840 tries = self.nTries
841 841 else:
842 842 tries = 1 # si no es la 1era vez entonces solo lo hace una vez
843 843
844 844 for nTries in range(tries):
845 845 if firstTime_flag:
846 846 print("\t[Reading] Waiting %0.2f sec for the next file: \"%s\" , try %03d ..." % (self.delay, filename, nTries + 1))
847 847 sleep(self.delay)
848 848 else:
849 849 print("\t[Reading] Searching the next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext))
850 850
851 851 fullfilename, filename = checkForRealPath(
852 852 self.path, self.foldercounter, self.year, self.doy, self.set, self.ext)
853 853 if fullfilename:
854 854 if self.__verifyFile(fullfilename):
855 855 fileOk_flag = True
856 856 break
857 857
858 858 if fileOk_flag:
859 859 break
860 860
861 861 firstTime_flag = False
862 862
863 863 log.warning('Skipping the file {} due to this file doesn\'t exist'.format(filename))
864 864 self.set += 1
865 865
866 866 # si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
867 867 if nFiles == (self.nFiles - 1):
868 868 self.set = 0
869 869 self.doy += 1
870 870 self.foldercounter = 0
871 871
872 872 if fileOk_flag:
873 873 self.fileSize = os.path.getsize(fullfilename)
874 874 self.filename = fullfilename
875 875 self.flagIsNewFile = 1
876 876 if self.fp != None:
877 877 self.fp.close()
878 878 self.fp = open(fullfilename, 'rb')
879 879 self.flagNoMoreFiles = 0
880 880 # print '[Reading] Setting the file: %s' % fullfilename
881 881 else:
882 882 self.fileSize = 0
883 883 self.filename = None
884 884 self.flagIsNewFile = 0
885 885 self.fp = None
886 886 self.flagNoMoreFiles = 1
887 887 # print '[Reading] No more files to read'
888 888
889 889 return fileOk_flag
890 890
891 891 def setNextFile(self):
892 892 if self.fp != None:
893 893 self.fp.close()
894 894
895 895 if self.online:
896 896 newFile = self.__setNextFileOnline()
897 897 else:
898 898 newFile = self.__setNextFileOffline()
899 899
900 900 if not(newFile):
901 901 self.dataOut.error = (-1, 'No more files to read')
902 902 return 0
903 903
904 904 if self.verbose:
905 905 print('[Reading] Setting the file: %s' % self.filename)
906 906
907 907 self.__readFirstHeader()
908 908 self.nReadBlocks = 0
909 909 return 1
910 910
911 911 def __waitNewBlock(self):
912 912 """
913 913 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
914 914
915 915 Si el modo de lectura es OffLine siempre retorn 0
916 916 """
917 917 if not self.online:
918 918 return 0
919 919
920 920 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
921 921 return 0
922 922
923 923 currentPointer = self.fp.tell()
924 924
925 925 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
926 926
927 927 for nTries in range(self.nTries):
928 928
929 929 self.fp.close()
930 930 self.fp = open(self.filename, 'rb')
931 931 self.fp.seek(currentPointer)
932 932
933 933 self.fileSize = os.path.getsize(self.filename)
934 934 currentSize = self.fileSize - currentPointer
935 935
936 936 if (currentSize >= neededSize):
937 937 self.basicHeaderObj.read(self.fp)
938 938 return 1
939 939
940 940 if self.fileSize == self.fileSizeByHeader:
941 941 # self.flagEoF = True
942 942 return 0
943 943
944 944 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
945 945 sleep(self.delay)
946 946
947 947 return 0
948 948
949 949 def waitDataBlock(self, pointer_location):
950 950
951 951 currentPointer = pointer_location
952 952
953 953 neededSize = self.processingHeaderObj.blockSize # + self.basicHeaderSize
954 954
955 955 for nTries in range(self.nTries):
956 956 self.fp.close()
957 957 self.fp = open(self.filename, 'rb')
958 958 self.fp.seek(currentPointer)
959 959
960 960 self.fileSize = os.path.getsize(self.filename)
961 961 currentSize = self.fileSize - currentPointer
962 962
963 963 if (currentSize >= neededSize):
964 964 return 1
965 965
966 966 print("[Reading] Waiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries + 1))
967 967 sleep(self.delay)
968 968
969 969 return 0
970 970
971 971 def __jumpToLastBlock(self):
972 972
973 973 if not(self.__isFirstTimeOnline):
974 974 return
975 975
976 976 csize = self.fileSize - self.fp.tell()
977 977 blocksize = self.processingHeaderObj.blockSize
978 978
979 979 # salta el primer bloque de datos
980 980 if csize > self.processingHeaderObj.blockSize:
981 981 self.fp.seek(self.fp.tell() + blocksize)
982 982 else:
983 983 return
984 984
985 985 csize = self.fileSize - self.fp.tell()
986 986 neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
987 987 while True:
988 988
989 989 if self.fp.tell() < self.fileSize:
990 990 self.fp.seek(self.fp.tell() + neededsize)
991 991 else:
992 992 self.fp.seek(self.fp.tell() - neededsize)
993 993 break
994 994
995 995 # csize = self.fileSize - self.fp.tell()
996 996 # neededsize = self.processingHeaderObj.blockSize + self.basicHeaderSize
997 997 # factor = int(csize/neededsize)
998 998 # if factor > 0:
999 999 # self.fp.seek(self.fp.tell() + factor*neededsize)
1000 1000
1001 1001 self.flagIsNewFile = 0
1002 1002 self.__isFirstTimeOnline = 0
1003 1003
1004 1004 def __setNewBlock(self):
1005 1005 # if self.server is None:
1006 1006 if self.fp == None:
1007 1007 return 0
1008 1008
1009 1009 # if self.online:
1010 1010 # self.__jumpToLastBlock()
1011 1011
1012 1012 if self.flagIsNewFile:
1013 1013 self.lastUTTime = self.basicHeaderObj.utc
1014 1014 return 1
1015 1015
1016 1016 if self.realtime:
1017 1017 self.flagDiscontinuousBlock = 1
1018 1018 if not(self.setNextFile()):
1019 1019 return 0
1020 1020 else:
1021 1021 return 1
1022 1022 # if self.server is None:
1023 1023 currentSize = self.fileSize - self.fp.tell()
1024 1024 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
1025 1025 if (currentSize >= neededSize):
1026 1026 self.basicHeaderObj.read(self.fp)
1027 1027 self.lastUTTime = self.basicHeaderObj.utc
1028 1028 return 1
1029 1029 # else:
1030 1030 # self.basicHeaderObj.read(self.zHeader)
1031 1031 # self.lastUTTime = self.basicHeaderObj.utc
1032 1032 # return 1
1033 1033 if self.__waitNewBlock():
1034 1034 self.lastUTTime = self.basicHeaderObj.utc
1035 1035 return 1
1036 1036 # if self.server is None:
1037 1037 if not(self.setNextFile()):
1038 1038 return 0
1039 1039
1040 1040 deltaTime = self.basicHeaderObj.utc - self.lastUTTime
1041 1041 self.lastUTTime = self.basicHeaderObj.utc
1042 1042
1043 1043 self.flagDiscontinuousBlock = 0
1044 1044
1045 1045 if deltaTime > self.maxTimeStep:
1046 1046 self.flagDiscontinuousBlock = 1
1047 1047
1048 1048 return 1
1049 1049
1050 1050 def readNextBlock(self):
1051 1051
1052 1052 # Skip block out of startTime and endTime
1053 1053 while True:
1054 1054 if not(self.__setNewBlock()):
1055 1055 self.dataOut.error = (-1, 'No more files to read')
1056 1056 return 0
1057 1057
1058 1058 if not(self.readBlock()):
1059 1059 return 0
1060 1060
1061 1061 self.getBasicHeader()
1062 1062 if (self.dataOut.datatime < datetime.datetime.combine(self.startDate, self.startTime)) or (self.dataOut.datatime > datetime.datetime.combine(self.endDate, self.endTime)):
1063 1063 print("[Reading] Block No. %d/%d -> %s [Skipping]" % (self.nReadBlocks,
1064 1064 self.processingHeaderObj.dataBlocksPerFile,
1065 1065 self.dataOut.datatime.ctime()))
1066 1066 continue
1067 1067
1068 1068 break
1069 1069
1070 1070 if self.verbose:
1071 1071 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
1072 1072 self.processingHeaderObj.dataBlocksPerFile,
1073 1073 self.dataOut.datatime.ctime()))
1074 1074 return 1
1075 1075
1076 1076 def __readFirstHeader(self):
1077 1077
1078 1078 self.basicHeaderObj.read(self.fp)
1079 1079 self.systemHeaderObj.read(self.fp)
1080 1080 self.radarControllerHeaderObj.read(self.fp)
1081 1081 self.processingHeaderObj.read(self.fp)
1082 1082
1083 1083 self.firstHeaderSize = self.basicHeaderObj.size
1084 1084
1085 1085 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
1086 1086 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
1087 1087 if datatype == 0:
1088 1088 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
1089 1089 elif datatype == 1:
1090 1090 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
1091 1091 elif datatype == 2:
1092 1092 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
1093 1093 elif datatype == 3:
1094 1094 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
1095 1095 elif datatype == 4:
1096 1096 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
1097 1097 elif datatype == 5:
1098 1098 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
1099 1099 else:
1100 1100 raise ValueError('Data type was not defined')
1101 1101
1102 1102 self.dtype = datatype_str
1103 1103 #self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
1104 1104 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + \
1105 1105 self.firstHeaderSize + self.basicHeaderSize * \
1106 1106 (self.processingHeaderObj.dataBlocksPerFile - 1)
1107 1107 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
1108 1108 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
1109 1109 self.getBlockDimension()
1110 1110
1111 1111 def __verifyFile(self, filename, msgFlag=True):
1112 1112
1113 1113 msg = None
1114 1114
1115 1115 try:
1116 1116 fp = open(filename, 'rb')
1117 1117 except IOError:
1118 1118
1119 1119 if msgFlag:
1120 1120 print("[Reading] File %s can't be opened" % (filename))
1121 1121
1122 1122 return False
1123 1123
1124 1124 currentPosition = fp.tell()
1125 1125 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
1126 1126
1127 1127 if neededSize == 0:
1128 1128 basicHeaderObj = BasicHeader(LOCALTIME)
1129 1129 systemHeaderObj = SystemHeader()
1130 1130 radarControllerHeaderObj = RadarControllerHeader()
1131 1131 processingHeaderObj = ProcessingHeader()
1132 1132
1133 1133 if not(basicHeaderObj.read(fp)):
1134 1134 fp.close()
1135 1135 return False
1136 1136
1137 1137 if not(systemHeaderObj.read(fp)):
1138 1138 fp.close()
1139 1139 return False
1140 1140
1141 1141 if not(radarControllerHeaderObj.read(fp)):
1142 1142 fp.close()
1143 1143 return False
1144 1144
1145 1145 if not(processingHeaderObj.read(fp)):
1146 1146 fp.close()
1147 1147 return False
1148 1148
1149 1149 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
1150 1150 else:
1151 1151 msg = "[Reading] Skipping the file %s due to it hasn't enough data" % filename
1152 1152
1153 1153 fp.close()
1154 1154
1155 1155 fileSize = os.path.getsize(filename)
1156 1156 currentSize = fileSize - currentPosition
1157 1157
1158 1158 if currentSize < neededSize:
1159 1159 if msgFlag and (msg != None):
1160 1160 print(msg)
1161 1161 return False
1162 1162
1163 1163 return True
1164 1164
1165 1165 def findDatafiles(self, path, startDate=None, endDate=None, expLabel='', ext='.r', walk=True, include_path=False):
1166 1166
1167 1167 path_empty = True
1168 1168
1169 1169 dateList = []
1170 1170 pathList = []
1171 1171
1172 1172 multi_path = path.split(',')
1173 1173
1174 1174 if not walk:
1175 1175
1176 1176 for single_path in multi_path:
1177 1177
1178 1178 if not os.path.isdir(single_path):
1179 1179 continue
1180 1180
1181 1181 fileList = glob.glob1(single_path, "*" + ext)
1182 1182
1183 1183 if not fileList:
1184 1184 continue
1185 1185
1186 1186 path_empty = False
1187 1187
1188 1188 fileList.sort()
1189 1189
1190 1190 for thisFile in fileList:
1191 1191
1192 1192 if not os.path.isfile(os.path.join(single_path, thisFile)):
1193 1193 continue
1194 1194
1195 1195 if not isRadarFile(thisFile):
1196 1196 continue
1197 1197
1198 1198 if not isFileInDateRange(thisFile, startDate, endDate):
1199 1199 continue
1200 1200
1201 1201 thisDate = getDateFromRadarFile(thisFile)
1202 1202
1203 1203 if thisDate in dateList:
1204 1204 continue
1205 1205
1206 1206 dateList.append(thisDate)
1207 1207 pathList.append(single_path)
1208 1208
1209 1209 else:
1210 1210 for single_path in multi_path:
1211 1211
1212 1212 if not os.path.isdir(single_path):
1213 1213 continue
1214 1214
1215 1215 dirList = []
1216 1216
1217 1217 for thisPath in os.listdir(single_path):
1218 1218
1219 1219 if not os.path.isdir(os.path.join(single_path, thisPath)):
1220 1220 continue
1221 1221
1222 1222 if not isRadarFolder(thisPath):
1223 1223 continue
1224 1224
1225 1225 if not isFolderInDateRange(thisPath, startDate, endDate):
1226 1226 continue
1227 1227
1228 1228 dirList.append(thisPath)
1229 1229
1230 1230 if not dirList:
1231 1231 continue
1232 1232
1233 1233 dirList.sort()
1234 1234
1235 1235 for thisDir in dirList:
1236 1236
1237 1237 datapath = os.path.join(single_path, thisDir, expLabel)
1238 1238 fileList = glob.glob1(datapath, "*" + ext)
1239 1239
1240 1240 if not fileList:
1241 1241 continue
1242 1242
1243 1243 path_empty = False
1244 1244
1245 1245 thisDate = getDateFromRadarFolder(thisDir)
1246 1246
1247 1247 pathList.append(datapath)
1248 1248 dateList.append(thisDate)
1249 1249
1250 1250 dateList.sort()
1251 1251
1252 1252 if walk:
1253 1253 pattern_path = os.path.join(multi_path[0], "[dYYYYDDD]", expLabel)
1254 1254 else:
1255 1255 pattern_path = multi_path[0]
1256 1256
1257 1257 if path_empty:
1258 1258 print("[Reading] No *%s files in %s for %s to %s" % (ext, pattern_path, startDate, endDate))
1259 1259 else:
1260 1260 if not dateList:
1261 1261 print("[Reading] Date range selected invalid [%s - %s]: No *%s files in %s)" % (startDate, endDate, ext, path))
1262 1262
1263 1263 if include_path:
1264 1264 return dateList, pathList
1265 1265
1266 1266 return dateList
1267 1267
1268 1268 def setup(self,
1269 1269 path=None,
1270 1270 startDate=None,
1271 1271 endDate=None,
1272 1272 startTime=datetime.time(0, 0, 0),
1273 1273 endTime=datetime.time(23, 59, 59),
1274 1274 set=None,
1275 1275 expLabel="",
1276 1276 ext=None,
1277 1277 online=False,
1278 1278 delay=60,
1279 1279 walk=True,
1280 1280 getblock=False,
1281 1281 nTxs=1,
1282 1282 realtime=False,
1283 1283 blocksize=None,
1284 1284 blocktime=None,
1285 1285 skip=None,
1286 1286 cursor=None,
1287 1287 warnings=True,
1288 1288 verbose=True,
1289 1289 server=None,
1290 1290 format=None,
1291 1291 oneDDict=None,
1292 1292 twoDDict=None,
1293 1293 ind2DList=None):
1294 1294 if server is not None:
1295 1295 if 'tcp://' in server:
1296 1296 address = server
1297 1297 else:
1298 1298 address = 'ipc:///tmp/%s' % server
1299 1299 self.server = address
1300 1300 self.context = zmq.Context()
1301 1301 self.receiver = self.context.socket(zmq.PULL)
1302 1302 self.receiver.connect(self.server)
1303 1303 time.sleep(0.5)
1304 1304 print('[Starting] ReceiverData from {}'.format(self.server))
1305 1305 else:
1306 1306 self.server = None
1307 1307 if path == None:
1308 1308 raise ValueError("[Reading] The path is not valid")
1309 1309
1310 1310 if ext == None:
1311 1311 ext = self.ext
1312 1312
1313 1313 if online:
1314 1314 print("[Reading] Searching files in online mode...")
1315 1315
1316 1316 for nTries in range(self.nTries):
1317 1317 fullpath, foldercounter, file, year, doy, set = self.__searchFilesOnLine(
1318 1318 path=path, expLabel=expLabel, ext=ext, walk=walk, set=set)
1319 1319
1320 1320 if fullpath:
1321 1321 break
1322 1322
1323 1323 print('[Reading] Waiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries + 1))
1324 1324 sleep(self.delay)
1325 1325
1326 1326 if not(fullpath):
1327 1327 self.dataOut.error = (-1, 'There isn\'t any valid file in {}'.format(path))
1328 1328 return
1329 1329
1330 1330 self.year = year
1331 1331 self.doy = doy
1332 1332 self.set = set - 1
1333 1333 self.path = path
1334 1334 self.foldercounter = foldercounter
1335 1335 last_set = None
1336 1336 else:
1337 1337 print("[Reading] Searching files in offline mode ...")
1338 1338 pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate,
1339 1339 startTime=startTime, endTime=endTime,
1340 1340 set=set, expLabel=expLabel, ext=ext,
1341 1341 walk=walk, cursor=cursor,
1342 1342 skip=skip)
1343 1343
1344 1344 if not(pathList):
1345 1345 self.fileIndex = -1
1346 1346 self.pathList = []
1347 1347 self.filenameList = []
1348 1348 return
1349 1349
1350 1350 self.fileIndex = -1
1351 1351 self.pathList = pathList
1352 1352 self.filenameList = filenameList
1353 1353 file_name = os.path.basename(filenameList[-1])
1354 1354 basename, ext = os.path.splitext(file_name)
1355 1355 last_set = int(basename[-3:])
1356 1356
1357 1357 self.online = online
1358 1358 self.realtime = realtime
1359 1359 self.delay = delay
1360 1360 ext = ext.lower()
1361 1361 self.ext = ext
1362 1362 self.getByBlock = getblock
1363 1363 self.nTxs = nTxs
1364 1364 self.startTime = startTime
1365 1365 self.endTime = endTime
1366 1366 self.endDate = endDate
1367 1367 self.startDate = startDate
1368 1368 # Added-----------------
1369 1369 self.selBlocksize = blocksize
1370 1370 self.selBlocktime = blocktime
1371 1371
1372 1372 # Verbose-----------
1373 1373 self.verbose = verbose
1374 1374 self.warnings = warnings
1375 1375
1376 1376 if not(self.setNextFile()):
1377 1377 if (startDate != None) and (endDate != None):
1378 1378 print("[Reading] No files in range: %s - %s" % (datetime.datetime.combine(startDate, startTime).ctime(), datetime.datetime.combine(endDate, endTime).ctime()))
1379 1379 elif startDate != None:
1380 1380 print("[Reading] No files in range: %s" % (datetime.datetime.combine(startDate, startTime).ctime()))
1381 1381 else:
1382 1382 print("[Reading] No files")
1383 1383
1384 1384 self.fileIndex = -1
1385 1385 self.pathList = []
1386 1386 self.filenameList = []
1387 1387 return
1388 1388
1389 1389 # self.getBasicHeader()
1390 1390
1391 1391 if last_set != None:
1392 1392 self.dataOut.last_block = last_set * \
1393 1393 self.processingHeaderObj.dataBlocksPerFile + self.basicHeaderObj.dataBlock
1394 1394 return
1395 1395
1396 1396 def getBasicHeader(self):
1397 1397
1398 1398 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
1399 1399 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
1400 1400
1401 1401 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
1402 1402
1403 1403 self.dataOut.timeZone = self.basicHeaderObj.timeZone
1404 1404
1405 1405 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
1406 1406
1407 1407 self.dataOut.errorCount = self.basicHeaderObj.errorCount
1408 1408
1409 1409 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
1410 1410
1411 1411 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
1412 1412
1413 1413 # self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock*self.nTxs
1414 1414
1415 1415 def getFirstHeader(self):
1416 1416
1417 1417 raise NotImplementedError
1418 1418
1419 1419 def getData(self):
1420 1420
1421 1421 raise NotImplementedError
1422 1422
1423 1423 def hasNotDataInBuffer(self):
1424 1424
1425 1425 raise NotImplementedError
1426 1426
1427 1427 def readBlock(self):
1428 1428
1429 1429 raise NotImplementedError
1430 1430
1431 1431 def isEndProcess(self):
1432 1432
1433 1433 return self.flagNoMoreFiles
1434 1434
1435 1435 def printReadBlocks(self):
1436 1436
1437 1437 print("[Reading] Number of read blocks per file %04d" % self.nReadBlocks)
1438 1438
1439 1439 def printTotalBlocks(self):
1440 1440
1441 1441 print("[Reading] Number of read blocks %04d" % self.nTotalBlocks)
1442 1442
1443 1443 def printNumberOfBlock(self):
1444 1444 'SPAM!'
1445 1445
1446 1446 # if self.flagIsNewBlock:
1447 1447 # print "[Reading] Block No. %d/%d -> %s" %(self.nReadBlocks,
1448 1448 # self.processingHeaderObj.dataBlocksPerFile,
1449 1449 # self.dataOut.datatime.ctime())
1450 1450
1451 1451 def printInfo(self):
1452 1452
1453 1453 if self.__printInfo == False:
1454 1454 return
1455 1455
1456 1456 self.basicHeaderObj.printInfo()
1457 1457 self.systemHeaderObj.printInfo()
1458 1458 self.radarControllerHeaderObj.printInfo()
1459 1459 self.processingHeaderObj.printInfo()
1460 1460
1461 1461 self.__printInfo = False
1462 1462
1463 1463 def run(self,
1464 1464 path=None,
1465 1465 startDate=None,
1466 1466 endDate=None,
1467 1467 startTime=datetime.time(0, 0, 0),
1468 1468 endTime=datetime.time(23, 59, 59),
1469 1469 set=None,
1470 1470 expLabel="",
1471 1471 ext=None,
1472 1472 online=False,
1473 1473 delay=60,
1474 1474 walk=True,
1475 1475 getblock=False,
1476 1476 nTxs=1,
1477 1477 realtime=False,
1478 1478 blocksize=None,
1479 1479 blocktime=None,
1480 1480 skip=None,
1481 1481 cursor=None,
1482 1482 warnings=True,
1483 1483 server=None,
1484 1484 verbose=True,
1485 1485 format=None,
1486 1486 oneDDict=None,
1487 1487 twoDDict=None,
1488 1488 ind2DList=None, **kwargs):
1489 1489
1490 1490 if not(self.isConfig):
1491 1491 self.setup(path=path,
1492 1492 startDate=startDate,
1493 1493 endDate=endDate,
1494 1494 startTime=startTime,
1495 1495 endTime=endTime,
1496 1496 set=set,
1497 1497 expLabel=expLabel,
1498 1498 ext=ext,
1499 1499 online=online,
1500 1500 delay=delay,
1501 1501 walk=walk,
1502 1502 getblock=getblock,
1503 1503 nTxs=nTxs,
1504 1504 realtime=realtime,
1505 1505 blocksize=blocksize,
1506 1506 blocktime=blocktime,
1507 1507 skip=skip,
1508 1508 cursor=cursor,
1509 1509 warnings=warnings,
1510 1510 server=server,
1511 1511 verbose=verbose,
1512 1512 format=format,
1513 1513 oneDDict=oneDDict,
1514 1514 twoDDict=twoDDict,
1515 1515 ind2DList=ind2DList)
1516 1516 self.isConfig = True
1517 1517 if server is None:
1518 1518 self.getData()
1519 1519 else:
1520 1520 self.getFromServer()
1521 1521
1522 1522
1523 1523 class JRODataWriter(JRODataIO):
1524 1524
1525 1525 """
1526 1526 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
1527 1527 de los datos siempre se realiza por bloques.
1528 1528 """
1529 1529
1530 1530 blockIndex = 0
1531 1531
1532 1532 path = None
1533 1533
1534 1534 setFile = None
1535 1535
1536 1536 profilesPerBlock = None
1537 1537
1538 1538 blocksPerFile = None
1539 1539
1540 1540 nWriteBlocks = 0
1541 1541
1542 1542 fileDate = None
1543 1543
1544 1544 def __init__(self, dataOut=None):
1545 1545 raise NotImplementedError
1546 1546
1547 1547 def hasAllDataInBuffer(self):
1548 1548 raise NotImplementedError
1549 1549
1550 1550 def setBlockDimension(self):
1551 1551 raise NotImplementedError
1552 1552
1553 1553 def writeBlock(self):
1554 1554 raise NotImplementedError
1555 1555
1556 1556 def putData(self):
1557 1557 raise NotImplementedError
1558 1558
1559 1559 def getProcessFlags(self):
1560 1560
1561 1561 processFlags = 0
1562 1562
1563 1563 dtype_index = get_dtype_index(self.dtype)
1564 1564 procflag_dtype = get_procflag_dtype(dtype_index)
1565 1565
1566 1566 processFlags += procflag_dtype
1567 1567
1568 1568 if self.dataOut.flagDecodeData:
1569 1569 processFlags += PROCFLAG.DECODE_DATA
1570 1570
1571 1571 if self.dataOut.flagDeflipData:
1572 1572 processFlags += PROCFLAG.DEFLIP_DATA
1573 1573
1574 1574 if self.dataOut.code is not None:
1575 1575 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1576 1576
1577 1577 if self.dataOut.nCohInt > 1:
1578 1578 processFlags += PROCFLAG.COHERENT_INTEGRATION
1579 1579
1580 1580 if self.dataOut.type == "Spectra":
1581 1581 if self.dataOut.nIncohInt > 1:
1582 1582 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
1583 1583
1584 1584 if self.dataOut.data_dc is not None:
1585 1585 processFlags += PROCFLAG.SAVE_CHANNELS_DC
1586 1586
1587 1587 if self.dataOut.flagShiftFFT:
1588 1588 processFlags += PROCFLAG.SHIFT_FFT_DATA
1589 1589
1590 1590 return processFlags
1591 1591
1592 1592 def setBasicHeader(self):
1593 1593
1594 1594 self.basicHeaderObj.size = self.basicHeaderSize # bytes
1595 1595 self.basicHeaderObj.version = self.versionFile
1596 1596 self.basicHeaderObj.dataBlock = self.nTotalBlocks
1597 1597
1598 1598 utc = numpy.floor(self.dataOut.utctime)
1599 1599 milisecond = (self.dataOut.utctime - utc) * 1000.0
1600 1600
1601 1601 self.basicHeaderObj.utc = utc
1602 1602 self.basicHeaderObj.miliSecond = milisecond
1603 1603 self.basicHeaderObj.timeZone = self.dataOut.timeZone
1604 1604 self.basicHeaderObj.dstFlag = self.dataOut.dstFlag
1605 1605 self.basicHeaderObj.errorCount = self.dataOut.errorCount
1606 1606
1607 1607 def setFirstHeader(self):
1608 1608 """
1609 1609 Obtiene una copia del First Header
1610 1610
1611 1611 Affected:
1612 1612
1613 1613 self.basicHeaderObj
1614 1614 self.systemHeaderObj
1615 1615 self.radarControllerHeaderObj
1616 1616 self.processingHeaderObj self.
1617 1617
1618 1618 Return:
1619 1619 None
1620 1620 """
1621 1621
1622 1622 raise NotImplementedError
1623 1623
1624 1624 def __writeFirstHeader(self):
1625 1625 """
1626 1626 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
1627 1627
1628 1628 Affected:
1629 1629 __dataType
1630 1630
1631 1631 Return:
1632 1632 None
1633 1633 """
1634 1634
1635 1635 # CALCULAR PARAMETROS
1636 1636
1637 1637 sizeLongHeader = self.systemHeaderObj.size + \
1638 1638 self.radarControllerHeaderObj.size + self.processingHeaderObj.size
1639 1639 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
1640 1640
1641 1641 self.basicHeaderObj.write(self.fp)
1642 1642 self.systemHeaderObj.write(self.fp)
1643 1643 self.radarControllerHeaderObj.write(self.fp)
1644 1644 self.processingHeaderObj.write(self.fp)
1645 1645
1646 1646 def __setNewBlock(self):
1647 1647 """
1648 1648 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
1649 1649
1650 1650 Return:
1651 1651 0 : si no pudo escribir nada
1652 1652 1 : Si escribio el Basic el First Header
1653 1653 """
1654 1654 if self.fp == None:
1655 1655 self.setNextFile()
1656 1656
1657 1657 if self.flagIsNewFile:
1658 1658 return 1
1659 1659
1660 1660 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
1661 1661 self.basicHeaderObj.write(self.fp)
1662 1662 return 1
1663 1663
1664 1664 if not(self.setNextFile()):
1665 1665 return 0
1666 1666
1667 1667 return 1
1668 1668
1669 1669 def writeNextBlock(self):
1670 1670 """
1671 1671 Selecciona el bloque siguiente de datos y los escribe en un file
1672 1672
1673 1673 Return:
1674 1674 0 : Si no hizo pudo escribir el bloque de datos
1675 1675 1 : Si no pudo escribir el bloque de datos
1676 1676 """
1677 1677 if not(self.__setNewBlock()):
1678 1678 return 0
1679 1679
1680 1680 self.writeBlock()
1681 1681
1682 1682 print("[Writing] Block No. %d/%d" % (self.blockIndex,
1683 1683 self.processingHeaderObj.dataBlocksPerFile))
1684 1684
1685 1685 return 1
1686 1686
1687 1687 def setNextFile(self):
1688 1688 """
1689 1689 Determina el siguiente file que sera escrito
1690 1690
1691 1691 Affected:
1692 1692 self.filename
1693 1693 self.subfolder
1694 1694 self.fp
1695 1695 self.setFile
1696 1696 self.flagIsNewFile
1697 1697
1698 1698 Return:
1699 1699 0 : Si el archivo no puede ser escrito
1700 1700 1 : Si el archivo esta listo para ser escrito
1701 1701 """
1702 1702 ext = self.ext
1703 1703 path = self.path
1704 1704
1705 1705 if self.fp != None:
1706 1706 self.fp.close()
1707 1707
1708 1708 timeTuple = time.localtime(self.dataOut.utctime)
1709 1709 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
1710 1710
1711 1711 fullpath = os.path.join(path, subfolder)
1712 1712 setFile = self.setFile
1713 1713
1714 1714 if not(os.path.exists(fullpath)):
1715 1715 os.mkdir(fullpath)
1716 1716 setFile = -1 # inicializo mi contador de seteo
1717 1717 else:
1718 1718 filesList = os.listdir(fullpath)
1719 1719 if len(filesList) > 0:
1720 1720 filesList = sorted(filesList, key=str.lower)
1721 1721 filen = filesList[-1]
1722 1722 # el filename debera tener el siguiente formato
1723 1723 # 0 1234 567 89A BCDE (hex)
1724 1724 # x YYYY DDD SSS .ext
1725 1725 if isNumber(filen[8:11]):
1726 1726 # inicializo mi contador de seteo al seteo del ultimo file
1727 1727 setFile = int(filen[8:11])
1728 1728 else:
1729 1729 setFile = -1
1730 1730 else:
1731 1731 setFile = -1 # inicializo mi contador de seteo
1732 1732
1733 1733 setFile += 1
1734 1734
1735 1735 # If this is a new day it resets some values
1736 1736 if self.dataOut.datatime.date() > self.fileDate:
1737 1737 setFile = 0
1738 1738 self.nTotalBlocks = 0
1739 1739
1740 1740 filen = '{}{:04d}{:03d}{:03d}{}'.format(
1741 1741 self.optchar, timeTuple.tm_year, timeTuple.tm_yday, setFile, ext)
1742 1742
1743 1743 filename = os.path.join(path, subfolder, filen)
1744 1744
1745 1745 fp = open(filename, 'wb')
1746 1746
1747 1747 self.blockIndex = 0
1748 1748
1749 1749 # guardando atributos
1750 1750 self.filename = filename
1751 1751 self.subfolder = subfolder
1752 1752 self.fp = fp
1753 1753 self.setFile = setFile
1754 1754 self.flagIsNewFile = 1
1755 1755 self.fileDate = self.dataOut.datatime.date()
1756 1756
1757 1757 self.setFirstHeader()
1758 1758
1759 1759 print('[Writing] Opening file: %s' % self.filename)
1760 1760
1761 1761 self.__writeFirstHeader()
1762 1762
1763 1763 return 1
1764 1764
1765 1765 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4):
1766 1766 """
1767 1767 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1768 1768
1769 1769 Inputs:
1770 1770 path : directory where data will be saved
1771 1771 profilesPerBlock : number of profiles per block
1772 1772 set : initial file set
1773 1773 datatype : An integer number that defines data type:
1774 1774 0 : int8 (1 byte)
1775 1775 1 : int16 (2 bytes)
1776 1776 2 : int32 (4 bytes)
1777 1777 3 : int64 (8 bytes)
1778 1778 4 : float32 (4 bytes)
1779 1779 5 : double64 (8 bytes)
1780 1780
1781 1781 Return:
1782 1782 0 : Si no realizo un buen seteo
1783 1783 1 : Si realizo un buen seteo
1784 1784 """
1785 1785
1786 1786 if ext == None:
1787 1787 ext = self.ext
1788 1788
1789 1789 self.ext = ext.lower()
1790 1790
1791 1791 self.path = path
1792 1792
1793 1793 if set is None:
1794 1794 self.setFile = -1
1795 1795 else:
1796 1796 self.setFile = set - 1
1797 1797
1798 1798 self.blocksPerFile = blocksPerFile
1799 1799
1800 1800 self.profilesPerBlock = profilesPerBlock
1801 1801
1802 1802 self.dataOut = dataOut
1803 1803 self.fileDate = self.dataOut.datatime.date()
1804 1804 # By default
1805 1805 self.dtype = self.dataOut.dtype
1806 1806
1807 1807 if datatype is not None:
1808 1808 self.dtype = get_numpy_dtype(datatype)
1809 1809
1810 1810 if not(self.setNextFile()):
1811 1811 print("[Writing] There isn't a next file")
1812 1812 return 0
1813 1813
1814 1814 self.setBlockDimension()
1815 1815
1816 1816 return 1
1817 1817
1818 def run(self, dataOut, path, blocksPerFile, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1818 def run(self, dataOut, path, blocksPerFile=100, profilesPerBlock=64, set=None, ext=None, datatype=4, **kwargs):
1819 1819
1820 1820 if not(self.isConfig):
1821 1821
1822 1822 self.setup(dataOut, path, blocksPerFile, profilesPerBlock=profilesPerBlock,
1823 1823 set=set, ext=ext, datatype=datatype, **kwargs)
1824 1824 self.isConfig = True
1825 1825
1826 1826 self.dataOut = dataOut
1827 1827 self.putData()
1828 1828 return self.dataOut No newline at end of file
This diff has been collapsed as it changes many lines, (607 lines changed) Show them Hide them
@@ -1,1180 +1,697
1 1 import os
2 2 import sys
3 3 import glob
4 4 import fnmatch
5 5 import datetime
6 6 import time
7 7 import re
8 8 import h5py
9 9 import numpy
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 numpy.ma.core import getdata
17 17
18 18 SPEED_OF_LIGHT = 299792458
19 19 SPEED_OF_LIGHT = 3e8
20 20
21 21 try:
22 22 from gevent import sleep
23 23 except:
24 24 from time import sleep
25 25
26 26 from schainpy.model.data.jrodata import Spectra
27 27 #from schainpy.model.data.BLTRheaderIO import FileHeader, RecordHeader
28 28 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
29 29 #from schainpy.model.io.jroIO_bltr import BLTRReader
30 30 from numpy import imag, shape, NaN
31 31
32 32 from .jroIO_base import JRODataReader
33 33
34 34
35 35 class Header(object):
36 36
37 37 def __init__(self):
38 38 raise NotImplementedError
39 39
40 40 def read(self):
41 41
42 42 raise NotImplementedError
43 43
44 44 def write(self):
45 45
46 46 raise NotImplementedError
47 47
48 48 def printInfo(self):
49 49
50 50 message = "#" * 50 + "\n"
51 51 message += self.__class__.__name__.upper() + "\n"
52 52 message += "#" * 50 + "\n"
53 53
54 54 keyList = list(self.__dict__.keys())
55 55 keyList.sort()
56 56
57 57 for key in keyList:
58 58 message += "%s = %s" % (key, self.__dict__[key]) + "\n"
59 59
60 60 if "size" not in keyList:
61 61 attr = getattr(self, "size")
62 62
63 63 if attr:
64 64 message += "%s = %s" % ("size", attr) + "\n"
65 65
66 # print message
67
68
69 66 FILE_STRUCTURE = numpy.dtype([ # HEADER 48bytes
70 67 ('FileMgcNumber', '<u4'), # 0x23020100
71 68 # No Of FDT data records in this file (0 or more)
72 69 ('nFDTdataRecors', '<u4'),
73 70 ('OffsetStartHeader', '<u4'),
74 71 ('RadarUnitId', '<u4'),
75 72 ('SiteName', numpy.str_, 32), # Null terminated
76 73 ])
77 74
78 75
79 76 class FileHeaderBLTR(Header):
80 77
81 78 def __init__(self):
82 79
83 80 self.FileMgcNumber = 0 # 0x23020100
84 81 # No Of FDT data records in this file (0 or more)
85 82 self.nFDTdataRecors = 0
86 83 self.RadarUnitId = 0
87 84 self.OffsetStartHeader = 0
88 85 self.SiteName = ""
89 86 self.size = 48
90 87
91 88 def FHread(self, fp):
92 89 # try:
93 90 startFp = open(fp, "rb")
94 91
95 92 header = numpy.fromfile(startFp, FILE_STRUCTURE, 1)
96 93
97 print(' ')
98 print('puntero file header', startFp.tell())
99 print(' ')
100
101 ''' numpy.fromfile(file, dtype, count, sep='')
102 file : file or str
103 Open file object or filename.
104
105 dtype : data-type
106 Data type of the returned array. For binary files, it is used to determine
107 the size and byte-order of the items in the file.
108
109 count : int
110 Number of items to read. -1 means all items (i.e., the complete file).
111
112 sep : str
113 Separator between items if file is a text file. Empty ("") separator means
114 the file should be treated as binary. Spaces (" ") in the separator match zero
115 or more whitespace characters. A separator consisting only of spaces must match
116 at least one whitespace.
117
118 '''
119
120 94 self.FileMgcNumber = hex(header['FileMgcNumber'][0])
121 95 # No Of FDT data records in this file (0 or more)
122 96 self.nFDTdataRecors = int(header['nFDTdataRecors'][0])
123 97 self.RadarUnitId = int(header['RadarUnitId'][0])
124 98 self.OffsetStartHeader = int(header['OffsetStartHeader'][0])
125 99 self.SiteName = str(header['SiteName'][0])
126 100
127 # print 'Numero de bloques', self.nFDTdataRecors
128
129 101 if self.size < 48:
130 102 return 0
131 103
132 104 return 1
133 105
134 106 def write(self, fp):
135 107
136 108 headerTuple = (self.FileMgcNumber,
137 109 self.nFDTdataRecors,
138 110 self.RadarUnitId,
139 111 self.SiteName,
140 112 self.size)
141 113
142 114 header = numpy.array(headerTuple, FILE_STRUCTURE)
143 115 # numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)
144 116 header.tofile(fp)
145 117 ''' ndarray.tofile(fid, sep, format) Write array to a file as text or binary (default).
146 118
147 119 fid : file or str
148 120 An open file object, or a string containing a filename.
149 121
150 122 sep : str
151 123 Separator between array items for text output. If "" (empty), a binary file is written,
152 124 equivalent to file.write(a.tobytes()).
153 125
154 126 format : str
155 127 Format string for text file output. Each entry in the array is formatted to text by
156 128 first converting it to the closest Python type, and then using "format" % item.
157 129
158 130 '''
159 131
160 132 return 1
161 133
162 134
163 135 RECORD_STRUCTURE = numpy.dtype([ # RECORD HEADER 180+20N bytes
164 136 ('RecMgcNumber', '<u4'), # 0x23030001
165 137 ('RecCounter', '<u4'), # Record counter(0,1, ...)
166 138 # Offset to start of next record form start of this record
167 139 ('Off2StartNxtRec', '<u4'),
168 140 # Offset to start of data from start of this record
169 141 ('Off2StartData', '<u4'),
170 142 # Epoch time stamp of start of acquisition (seconds)
171 143 ('nUtime', '<i4'),
172 144 # Millisecond component of time stamp (0,...,999)
173 145 ('nMilisec', '<u4'),
174 146 # Experiment tag name (null terminated)
175 147 ('ExpTagName', numpy.str_, 32),
176 148 # Experiment comment (null terminated)
177 149 ('ExpComment', numpy.str_, 32),
178 150 # Site latitude (from GPS) in degrees (positive implies North)
179 151 ('SiteLatDegrees', '<f4'),
180 152 # Site longitude (from GPS) in degrees (positive implies East)
181 153 ('SiteLongDegrees', '<f4'),
182 154 # RTC GPS engine status (0=SEEK, 1=LOCK, 2=NOT FITTED, 3=UNAVAILABLE)
183 155 ('RTCgpsStatus', '<u4'),
184 156 ('TransmitFrec', '<u4'), # Transmit frequency (Hz)
185 157 ('ReceiveFrec', '<u4'), # Receive frequency
186 158 # First local oscillator frequency (Hz)
187 159 ('FirstOsciFrec', '<u4'),
188 160 # (0="O", 1="E", 2="linear 1", 3="linear2")
189 161 ('Polarisation', '<u4'),
190 162 # Receiver filter settings (0,1,2,3)
191 163 ('ReceiverFiltSett', '<u4'),
192 164 # Number of modes in use (1 or 2)
193 165 ('nModesInUse', '<u4'),
194 166 # Dual Mode index number for these data (0 or 1)
195 167 ('DualModeIndex', '<u4'),
196 168 # Dual Mode range correction for these data (m)
197 169 ('DualModeRange', '<u4'),
198 170 # Number of digital channels acquired (2*N)
199 171 ('nDigChannels', '<u4'),
200 172 # Sampling resolution (meters)
201 173 ('SampResolution', '<u4'),
202 174 # Number of range gates sampled
203 175 ('nHeights', '<u4'),
204 176 # Start range of sampling (meters)
205 177 ('StartRangeSamp', '<u4'),
206 178 ('PRFhz', '<u4'), # PRF (Hz)
207 179 ('nCohInt', '<u4'), # Integrations
208 180 # Number of data points transformed
209 181 ('nProfiles', '<u4'),
210 182 # Number of receive beams stored in file (1 or N)
211 183 ('nChannels', '<u4'),
212 184 ('nIncohInt', '<u4'), # Number of spectral averages
213 185 # FFT windowing index (0 = no window)
214 186 ('FFTwindowingInd', '<u4'),
215 187 # Beam steer angle (azimuth) in degrees (clockwise from true North)
216 188 ('BeamAngleAzim', '<f4'),
217 189 # Beam steer angle (zenith) in degrees (0=> vertical)
218 190 ('BeamAngleZen', '<f4'),
219 191 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
220 192 ('AntennaCoord0', '<f4'),
221 193 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
222 194 ('AntennaAngl0', '<f4'),
223 195 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
224 196 ('AntennaCoord1', '<f4'),
225 197 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
226 198 ('AntennaAngl1', '<f4'),
227 199 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
228 200 ('AntennaCoord2', '<f4'),
229 201 # Antenna coordinates (Range(meters), Bearing(degrees)) - N pairs
230 202 ('AntennaAngl2', '<f4'),
231 203 # Receiver phase calibration (degrees) - N values
232 204 ('RecPhaseCalibr0', '<f4'),
233 205 # Receiver phase calibration (degrees) - N values
234 206 ('RecPhaseCalibr1', '<f4'),
235 207 # Receiver phase calibration (degrees) - N values
236 208 ('RecPhaseCalibr2', '<f4'),
237 209 # Receiver amplitude calibration (ratio relative to receiver one) - N values
238 210 ('RecAmpCalibr0', '<f4'),
239 211 # Receiver amplitude calibration (ratio relative to receiver one) - N values
240 212 ('RecAmpCalibr1', '<f4'),
241 213 # Receiver amplitude calibration (ratio relative to receiver one) - N values
242 214 ('RecAmpCalibr2', '<f4'),
243 215 # Receiver gains in dB - N values
244 216 ('ReceiverGaindB0', '<i4'),
245 217 # Receiver gains in dB - N values
246 218 ('ReceiverGaindB1', '<i4'),
247 219 # Receiver gains in dB - N values
248 220 ('ReceiverGaindB2', '<i4'),
249 221 ])
250 222
251 223
252 224 class RecordHeaderBLTR(Header):
253 225
254 226 def __init__(self, RecMgcNumber=None, RecCounter=0, Off2StartNxtRec=811248,
255 227 nUtime=0, nMilisec=0, ExpTagName=None,
256 228 ExpComment=None, SiteLatDegrees=0, SiteLongDegrees=0,
257 229 RTCgpsStatus=0, TransmitFrec=0, ReceiveFrec=0,
258 230 FirstOsciFrec=0, Polarisation=0, ReceiverFiltSett=0,
259 231 nModesInUse=0, DualModeIndex=0, DualModeRange=0,
260 232 nDigChannels=0, SampResolution=0, nHeights=0,
261 233 StartRangeSamp=0, PRFhz=0, nCohInt=0,
262 234 nProfiles=0, nChannels=0, nIncohInt=0,
263 235 FFTwindowingInd=0, BeamAngleAzim=0, BeamAngleZen=0,
264 236 AntennaCoord0=0, AntennaCoord1=0, AntennaCoord2=0,
265 237 RecPhaseCalibr0=0, RecPhaseCalibr1=0, RecPhaseCalibr2=0,
266 238 RecAmpCalibr0=0, RecAmpCalibr1=0, RecAmpCalibr2=0,
267 239 AntennaAngl0=0, AntennaAngl1=0, AntennaAngl2=0,
268 240 ReceiverGaindB0=0, ReceiverGaindB1=0, ReceiverGaindB2=0, Off2StartData=0, OffsetStartHeader=0):
269 241
270 242 self.RecMgcNumber = RecMgcNumber # 0x23030001
271 243 self.RecCounter = RecCounter
272 244 self.Off2StartNxtRec = Off2StartNxtRec
273 245 self.Off2StartData = Off2StartData
274 246 self.nUtime = nUtime
275 247 self.nMilisec = nMilisec
276 248 self.ExpTagName = ExpTagName
277 249 self.ExpComment = ExpComment
278 250 self.SiteLatDegrees = SiteLatDegrees
279 251 self.SiteLongDegrees = SiteLongDegrees
280 252 self.RTCgpsStatus = RTCgpsStatus
281 253 self.TransmitFrec = TransmitFrec
282 254 self.ReceiveFrec = ReceiveFrec
283 255 self.FirstOsciFrec = FirstOsciFrec
284 256 self.Polarisation = Polarisation
285 257 self.ReceiverFiltSett = ReceiverFiltSett
286 258 self.nModesInUse = nModesInUse
287 259 self.DualModeIndex = DualModeIndex
288 260 self.DualModeRange = DualModeRange
289 261 self.nDigChannels = nDigChannels
290 262 self.SampResolution = SampResolution
291 263 self.nHeights = nHeights
292 264 self.StartRangeSamp = StartRangeSamp
293 265 self.PRFhz = PRFhz
294 266 self.nCohInt = nCohInt
295 267 self.nProfiles = nProfiles
296 268 self.nChannels = nChannels
297 269 self.nIncohInt = nIncohInt
298 270 self.FFTwindowingInd = FFTwindowingInd
299 271 self.BeamAngleAzim = BeamAngleAzim
300 272 self.BeamAngleZen = BeamAngleZen
301 273 self.AntennaCoord0 = AntennaCoord0
302 274 self.AntennaAngl0 = AntennaAngl0
303 275 self.AntennaAngl1 = AntennaAngl1
304 276 self.AntennaAngl2 = AntennaAngl2
305 277 self.AntennaCoord1 = AntennaCoord1
306 278 self.AntennaCoord2 = AntennaCoord2
307 279 self.RecPhaseCalibr0 = RecPhaseCalibr0
308 280 self.RecPhaseCalibr1 = RecPhaseCalibr1
309 281 self.RecPhaseCalibr2 = RecPhaseCalibr2
310 282 self.RecAmpCalibr0 = RecAmpCalibr0
311 283 self.RecAmpCalibr1 = RecAmpCalibr1
312 284 self.RecAmpCalibr2 = RecAmpCalibr2
313 285 self.ReceiverGaindB0 = ReceiverGaindB0
314 286 self.ReceiverGaindB1 = ReceiverGaindB1
315 287 self.ReceiverGaindB2 = ReceiverGaindB2
316 288 self.OffsetStartHeader = 48
317 289
318 290 def RHread(self, fp):
319 # print fp
320 # 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.
321 # The method tell() returns the current position of the file read/write pointer within the file.
322 291 startFp = open(fp, "rb")
323 # RecCounter=0
324 # Off2StartNxtRec=811248
325 292 OffRHeader = self.OffsetStartHeader + self.RecCounter * self.Off2StartNxtRec
326 print(' ')
327 print('puntero Record Header', startFp.tell())
328 print(' ')
329
330 293 startFp.seek(OffRHeader, os.SEEK_SET)
331
332 print(' ')
333 print('puntero Record Header con seek', startFp.tell())
334 print(' ')
335
336 # print 'Posicion del bloque: ',OffRHeader
337
338 294 header = numpy.fromfile(startFp, RECORD_STRUCTURE, 1)
339
340 print(' ')
341 print('puntero Record Header con seek', startFp.tell())
342 print(' ')
343
344 print(' ')
345 #
346 # print 'puntero Record Header despues de seek', header.tell()
347 print(' ')
348
349 295 self.RecMgcNumber = hex(header['RecMgcNumber'][0]) # 0x23030001
350 296 self.RecCounter = int(header['RecCounter'][0])
351 297 self.Off2StartNxtRec = int(header['Off2StartNxtRec'][0])
352 298 self.Off2StartData = int(header['Off2StartData'][0])
353 299 self.nUtime = header['nUtime'][0]
354 300 self.nMilisec = header['nMilisec'][0]
355 301 self.ExpTagName = str(header['ExpTagName'][0])
356 302 self.ExpComment = str(header['ExpComment'][0])
357 303 self.SiteLatDegrees = header['SiteLatDegrees'][0]
358 304 self.SiteLongDegrees = header['SiteLongDegrees'][0]
359 305 self.RTCgpsStatus = header['RTCgpsStatus'][0]
360 306 self.TransmitFrec = header['TransmitFrec'][0]
361 307 self.ReceiveFrec = header['ReceiveFrec'][0]
362 308 self.FirstOsciFrec = header['FirstOsciFrec'][0]
363 309 self.Polarisation = header['Polarisation'][0]
364 310 self.ReceiverFiltSett = header['ReceiverFiltSett'][0]
365 311 self.nModesInUse = header['nModesInUse'][0]
366 312 self.DualModeIndex = header['DualModeIndex'][0]
367 313 self.DualModeRange = header['DualModeRange'][0]
368 314 self.nDigChannels = header['nDigChannels'][0]
369 315 self.SampResolution = header['SampResolution'][0]
370 316 self.nHeights = header['nHeights'][0]
371 317 self.StartRangeSamp = header['StartRangeSamp'][0]
372 318 self.PRFhz = header['PRFhz'][0]
373 319 self.nCohInt = header['nCohInt'][0]
374 320 self.nProfiles = header['nProfiles'][0]
375 321 self.nChannels = header['nChannels'][0]
376 322 self.nIncohInt = header['nIncohInt'][0]
377 323 self.FFTwindowingInd = header['FFTwindowingInd'][0]
378 324 self.BeamAngleAzim = header['BeamAngleAzim'][0]
379 325 self.BeamAngleZen = header['BeamAngleZen'][0]
380 326 self.AntennaCoord0 = header['AntennaCoord0'][0]
381 327 self.AntennaAngl0 = header['AntennaAngl0'][0]
382 328 self.AntennaCoord1 = header['AntennaCoord1'][0]
383 329 self.AntennaAngl1 = header['AntennaAngl1'][0]
384 330 self.AntennaCoord2 = header['AntennaCoord2'][0]
385 331 self.AntennaAngl2 = header['AntennaAngl2'][0]
386 332 self.RecPhaseCalibr0 = header['RecPhaseCalibr0'][0]
387 333 self.RecPhaseCalibr1 = header['RecPhaseCalibr1'][0]
388 334 self.RecPhaseCalibr2 = header['RecPhaseCalibr2'][0]
389 335 self.RecAmpCalibr0 = header['RecAmpCalibr0'][0]
390 336 self.RecAmpCalibr1 = header['RecAmpCalibr1'][0]
391 337 self.RecAmpCalibr2 = header['RecAmpCalibr2'][0]
392 338 self.ReceiverGaindB0 = header['ReceiverGaindB0'][0]
393 339 self.ReceiverGaindB1 = header['ReceiverGaindB1'][0]
394 340 self.ReceiverGaindB2 = header['ReceiverGaindB2'][0]
395 341
396 342 self.ipp = 0.5 * (SPEED_OF_LIGHT / self.PRFhz)
397 343
398 344 self.RHsize = 180 + 20 * self.nChannels
399 345 self.Datasize = self.nProfiles * self.nChannels * self.nHeights * 2 * 4
400 # print 'Datasize',self.Datasize
401 346 endFp = self.OffsetStartHeader + self.RecCounter * self.Off2StartNxtRec
402 347
403 print('==============================================')
404 print('RecMgcNumber ', self.RecMgcNumber)
405 print('RecCounter ', self.RecCounter)
406 print('Off2StartNxtRec ', self.Off2StartNxtRec)
407 print('Off2StartData ', self.Off2StartData)
408 print('Range Resolution ', self.SampResolution)
409 print('First Height ', self.StartRangeSamp)
410 print('PRF (Hz) ', self.PRFhz)
411 print('Heights (K) ', self.nHeights)
412 print('Channels (N) ', self.nChannels)
413 print('Profiles (J) ', self.nProfiles)
414 print('iCoh ', self.nCohInt)
415 print('iInCoh ', self.nIncohInt)
416 print('BeamAngleAzim ', self.BeamAngleAzim)
417 print('BeamAngleZen ', self.BeamAngleZen)
418
419 # print 'ModoEnUso ',self.DualModeIndex
420 # print 'UtcTime ',self.nUtime
421 # print 'MiliSec ',self.nMilisec
422 # print 'Exp TagName ',self.ExpTagName
423 # print 'Exp Comment ',self.ExpComment
424 # print 'FFT Window Index ',self.FFTwindowingInd
425 # print 'N Dig. Channels ',self.nDigChannels
426 print('Size de bloque ', self.RHsize)
427 print('DataSize ', self.Datasize)
428 print('BeamAngleAzim ', self.BeamAngleAzim)
429 # print 'AntennaCoord0 ',self.AntennaCoord0
430 # print 'AntennaAngl0 ',self.AntennaAngl0
431 # print 'AntennaCoord1 ',self.AntennaCoord1
432 # print 'AntennaAngl1 ',self.AntennaAngl1
433 # print 'AntennaCoord2 ',self.AntennaCoord2
434 # print 'AntennaAngl2 ',self.AntennaAngl2
435 print('RecPhaseCalibr0 ', self.RecPhaseCalibr0)
436 print('RecPhaseCalibr1 ', self.RecPhaseCalibr1)
437 print('RecPhaseCalibr2 ', self.RecPhaseCalibr2)
438 print('RecAmpCalibr0 ', self.RecAmpCalibr0)
439 print('RecAmpCalibr1 ', self.RecAmpCalibr1)
440 print('RecAmpCalibr2 ', self.RecAmpCalibr2)
441 print('ReceiverGaindB0 ', self.ReceiverGaindB0)
442 print('ReceiverGaindB1 ', self.ReceiverGaindB1)
443 print('ReceiverGaindB2 ', self.ReceiverGaindB2)
444 print('==============================================')
445
348
446 349 if OffRHeader > endFp:
447 350 sys.stderr.write(
448 351 "Warning %s: Size value read from System Header is lower than it has to be\n" % fp)
449 352 return 0
450 353
451 354 if OffRHeader < endFp:
452 355 sys.stderr.write(
453 356 "Warning %s: Size value read from System Header size is greater than it has to be\n" % fp)
454 357 return 0
455 358
456 359 return 1
457 360
458 361
459 362 class BLTRSpectraReader (ProcessingUnit, FileHeaderBLTR, RecordHeaderBLTR, JRODataReader):
460 363
461 364 path = None
462 365 startDate = None
463 366 endDate = None
464 367 startTime = None
465 368 endTime = None
466 369 walk = None
467 370 isConfig = False
468 371
469 372 fileList = None
470 373
471 374 # metadata
472 375 TimeZone = None
473 376 Interval = None
474 377 heightList = None
475 378
476 379 # data
477 380 data = None
478 381 utctime = None
479 382
480 383 def __init__(self, **kwargs):
481 384
482 385 # Eliminar de la base la herencia
483 386 ProcessingUnit.__init__(self, **kwargs)
484 387
485 388 #self.isConfig = False
486 389
487 390 #self.pts2read_SelfSpectra = 0
488 391 #self.pts2read_CrossSpectra = 0
489 392 #self.pts2read_DCchannels = 0
490 393 #self.datablock = None
491 394 self.utc = None
492 395 self.ext = ".fdt"
493 396 self.optchar = "P"
494 397 self.fpFile = None
495 398 self.fp = None
496 399 self.BlockCounter = 0
497 400 self.dtype = None
498 401 self.fileSizeByHeader = None
499 402 self.filenameList = []
500 403 self.fileSelector = 0
501 404 self.Off2StartNxtRec = 0
502 405 self.RecCounter = 0
503 406 self.flagNoMoreFiles = 0
504 407 self.data_spc = None
505 408 self.data_cspc = None
506 409 self.data_output = None
507 410 self.path = None
508 411 self.OffsetStartHeader = 0
509 412 self.Off2StartData = 0
510 413 self.ipp = 0
511 414 self.nFDTdataRecors = 0
512 415 self.blocksize = 0
513 416 self.dataOut = Spectra()
514 417 self.profileIndex = 1 # Always
515 418 self.dataOut.flagNoData = False
516 419 self.dataOut.nRdPairs = 0
517 420 self.dataOut.data_spc = None
518 421 self.dataOut.velocityX = []
519 422 self.dataOut.velocityY = []
520 423 self.dataOut.velocityV = []
521 424
522 425 def Files2Read(self, fp):
523 426 '''
524 427 Function that indicates the number of .fdt files that exist in the folder to be read.
525 428 It also creates an organized list with the names of the files to read.
526 429 '''
527 430 # self.__checkPath()
528 431
529 432 # Gets the list of files within the fp address
530 433 ListaData = os.listdir(fp)
531 434 # Sort the list of files from least to largest by names
532 435 ListaData = sorted(ListaData)
533 436 nFiles = 0 # File Counter
534 437 FileList = [] # A list is created that will contain the .fdt files
535 438 for IndexFile in ListaData:
536 439 if '.fdt' in IndexFile:
537 440 FileList.append(IndexFile)
538 441 nFiles += 1
539 442
540 # print 'Files2Read'
541 # print 'Existen '+str(nFiles)+' archivos .fdt'
542
543 443 self.filenameList = FileList # List of files from least to largest by names
544 444
545 445 def run(self, **kwargs):
546 446 '''
547 447 This method will be the one that will initiate the data entry, will be called constantly.
548 448 You should first verify that your Setup () is set up and then continue to acquire
549 449 the data to be processed with getData ().
550 450 '''
551 451 if not self.isConfig:
552 452 self.setup(**kwargs)
553 453 self.isConfig = True
554 454
555 455 self.getData()
556 # print 'running'
557 456
558 457 def setup(self, path=None,
559 458 startDate=None,
560 459 endDate=None,
561 460 startTime=None,
562 461 endTime=None,
563 462 walk=True,
564 463 timezone='utc',
565 464 code=None,
566 465 online=False,
567 466 ReadMode=None,
568 467 **kwargs):
569 468
570 469 self.isConfig = True
571 470
572 471 self.path = path
573 472 self.startDate = startDate
574 473 self.endDate = endDate
575 474 self.startTime = startTime
576 475 self.endTime = endTime
577 476 self.walk = walk
578 477 self.ReadMode = int(ReadMode)
579 478
580 479 pass
581 480
582 481 def getData(self):
583 482 '''
584 483 Before starting this function, you should check that there is still an unread file,
585 484 If there are still blocks to read or if the data block is empty.
586 485
587 486 You should call the file "read".
588 487
589 488 '''
590 489
591 490 if self.flagNoMoreFiles:
592 491 self.dataOut.flagNoData = True
593 print('NoData se vuelve true')
594 492 return 0
595 493
596 494 self.fp = self.path
597 495 self.Files2Read(self.fp)
598 496 self.readFile(self.fp)
599 497 self.dataOut.data_spc = self.data_spc
600 self.dataOut.data_cspc = self.data_cspc
601 self.dataOut.data_output = self.data_output
602
603 print('self.dataOut.data_output', shape(self.dataOut.data_output))
604
605 # self.removeDC()
606 return self.dataOut.data_spc
607
608 def readFile(self, fp):
498 self.dataOut.data_cspc =self.data_cspc
499 self.dataOut.data_output=self.data_output
500
501 return self.dataOut.data_spc
502
503
504 def readFile(self,fp):
609 505 '''
610 506 You must indicate if you are reading in Online or Offline mode and load the
611 507 The parameters for this file reading mode.
612 508
613 509 Then you must do 2 actions:
614 510
615 511 1. Get the BLTR FileHeader.
616 512 2. Start reading the first block.
617 513 '''
618
619 # The address of the folder is generated the name of the .fdt file that will be read
620 print("File: ", self.fileSelector + 1)
621
514
622 515 if self.fileSelector < len(self.filenameList):
623 516
624 517 self.fpFile = str(fp) + '/' + \
625 518 str(self.filenameList[self.fileSelector])
626 # print self.fpFile
627 519 fheader = FileHeaderBLTR()
628 520 fheader.FHread(self.fpFile) # Bltr FileHeader Reading
629 521 self.nFDTdataRecors = fheader.nFDTdataRecors
630 522
631 523 self.readBlock() # Block reading
632 524 else:
633 print('readFile FlagNoData becomes true')
634 self.flagNoMoreFiles = True
525 self.flagNoMoreFiles=True
635 526 self.dataOut.flagNoData = True
636 527 return 0
637 528
638 529 def getVelRange(self, extrapoints=0):
639 530 Lambda = SPEED_OF_LIGHT / 50000000
640 531 # 1./(self.dataOut.ippSeconds * self.dataOut.nCohInt)
641 532 PRF = self.dataOut.PRF
642 533 Vmax = -Lambda / (4. * (1. / PRF) * self.dataOut.nCohInt * 2.)
643 534 deltafreq = PRF / (self.nProfiles)
644 535 deltavel = (Vmax * 2) / (self.nProfiles)
645 536 freqrange = deltafreq * \
646 537 (numpy.arange(self.nProfiles) - self.nProfiles / 2.) - deltafreq / 2
647 538 velrange = deltavel * \
648 539 (numpy.arange(self.nProfiles) - self.nProfiles / 2.)
649 540 return velrange
650 541
651 542 def readBlock(self):
652 543 '''
653 544 It should be checked if the block has data, if it is not passed to the next file.
654 545
655 546 Then the following is done:
656 547
657 548 1. Read the RecordHeader
658 549 2. Fill the buffer with the current block number.
659 550
660 551 '''
661
662 if self.BlockCounter < self.nFDTdataRecors - 2:
663 print(self.nFDTdataRecors, 'CONDICION!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
664 if self.ReadMode == 1:
665 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter + 1)
666 elif self.ReadMode == 0:
552
553 if self.BlockCounter < self.nFDTdataRecors-1:
554 if self.ReadMode==1:
555 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter+1)
556 elif self.ReadMode==0:
667 557 rheader = RecordHeaderBLTR(RecCounter=self.BlockCounter)
668 558
669 559 rheader.RHread(self.fpFile) # Bltr FileHeader Reading
670 560
671 561 self.OffsetStartHeader = rheader.OffsetStartHeader
672 562 self.RecCounter = rheader.RecCounter
673 563 self.Off2StartNxtRec = rheader.Off2StartNxtRec
674 564 self.Off2StartData = rheader.Off2StartData
675 565 self.nProfiles = rheader.nProfiles
676 566 self.nChannels = rheader.nChannels
677 567 self.nHeights = rheader.nHeights
678 568 self.frequency = rheader.TransmitFrec
679 569 self.DualModeIndex = rheader.DualModeIndex
680 570
681 571 self.pairsList = [(0, 1), (0, 2), (1, 2)]
682 572 self.dataOut.pairsList = self.pairsList
683 573
684 574 self.nRdPairs = len(self.dataOut.pairsList)
685 575 self.dataOut.nRdPairs = self.nRdPairs
686
687 self.__firstHeigth = rheader.StartRangeSamp
688 self.__deltaHeigth = rheader.SampResolution
689 self.dataOut.heightList = self.__firstHeigth + \
690 numpy.array(list(range(self.nHeights))) * self.__deltaHeigth
691 self.dataOut.channelList = list(range(self.nChannels))
692 self.dataOut.nProfiles = rheader.nProfiles
693 self.dataOut.nIncohInt = rheader.nIncohInt
694 self.dataOut.nCohInt = rheader.nCohInt
695 self.dataOut.ippSeconds = 1 / float(rheader.PRFhz)
696 self.dataOut.PRF = rheader.PRFhz
697 self.dataOut.nFFTPoints = rheader.nProfiles
698 self.dataOut.utctime = rheader.nUtime
699 self.dataOut.timeZone = 0
700 self.dataOut.normFactor = self.dataOut.nProfiles * \
701 self.dataOut.nIncohInt * self.dataOut.nCohInt
702 self.dataOut.outputInterval = self.dataOut.ippSeconds * \
703 self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles
704
705 self.data_output = numpy.ones([3, rheader.nHeights]) * numpy.NaN
706 print('self.data_output', shape(self.data_output))
707 self.dataOut.velocityX = []
708 self.dataOut.velocityY = []
709 self.dataOut.velocityV = []
710
576 self.__firstHeigth=rheader.StartRangeSamp
577 self.__deltaHeigth=rheader.SampResolution
578 self.dataOut.heightList= self.__firstHeigth + numpy.array(range(self.nHeights))*self.__deltaHeigth
579 self.dataOut.channelList = range(self.nChannels)
580 self.dataOut.nProfiles=rheader.nProfiles
581 self.dataOut.nIncohInt=rheader.nIncohInt
582 self.dataOut.nCohInt=rheader.nCohInt
583 self.dataOut.ippSeconds= 1/float(rheader.PRFhz)
584 self.dataOut.PRF=rheader.PRFhz
585 self.dataOut.nFFTPoints=rheader.nProfiles
586 self.dataOut.utctime=rheader.nUtime
587 self.dataOut.timeZone=0
588 self.dataOut.normFactor= self.dataOut.nProfiles*self.dataOut.nIncohInt*self.dataOut.nCohInt
589 self.dataOut.outputInterval= self.dataOut.ippSeconds * self.dataOut.nCohInt * self.dataOut.nIncohInt * self.nProfiles
590
591 self.data_output=numpy.ones([3,rheader.nHeights])*numpy.NaN
592 self.dataOut.velocityX=[]
593 self.dataOut.velocityY=[]
594 self.dataOut.velocityV=[]
595
711 596 '''Block Reading, the Block Data is received and Reshape is used to give it
712 597 shape.
713 598 '''
714 599
715 600 # Procedure to take the pointer to where the date block starts
716 601 startDATA = open(self.fpFile, "rb")
717 602 OffDATA = self.OffsetStartHeader + self.RecCounter * \
718 603 self.Off2StartNxtRec + self.Off2StartData
719 604 startDATA.seek(OffDATA, os.SEEK_SET)
720 605
721 606 def moving_average(x, N=2):
722 607 return numpy.convolve(x, numpy.ones((N,)) / N)[(N - 1):]
723 608
724 609 def gaus(xSamples, a, x0, sigma):
725 610 return a * exp(-(xSamples - x0)**2 / (2 * sigma**2))
726 611
727 612 def Find(x, value):
728 613 for index in range(len(x)):
729 614 if x[index] == value:
730 615 return index
731 616
732 617 def pol2cart(rho, phi):
733 618 x = rho * numpy.cos(phi)
734 619 y = rho * numpy.sin(phi)
735 620 return(x, y)
736 621
737 if self.DualModeIndex == self.ReadMode:
738
739 self.data_fft = numpy.fromfile(
740 startDATA, [('complex', '<c8')], self.nProfiles * self.nChannels * self.nHeights)
741
742 self.data_fft = self.data_fft.astype(numpy.dtype('complex'))
743
744 self.data_block = numpy.reshape(
745 self.data_fft, (self.nHeights, self.nChannels, self.nProfiles))
746
747 self.data_block = numpy.transpose(self.data_block, (1, 2, 0))
748
622 if self.DualModeIndex==self.ReadMode:
623
624 self.data_fft = numpy.fromfile( startDATA, [('complex','<c8')],self.nProfiles*self.nChannels*self.nHeights )
625 self.data_fft = numpy.empty(101376)
626
627 self.data_fft=self.data_fft.astype(numpy.dtype('complex'))
628
629 self.data_block=numpy.reshape(self.data_fft,(self.nHeights, self.nChannels, self.nProfiles ))
630
631 self.data_block = numpy.transpose(self.data_block, (1,2,0))
632
749 633 copy = self.data_block.copy()
750 634 spc = copy * numpy.conjugate(copy)
751 635
752 636 self.data_spc = numpy.absolute(
753 637 spc) # valor absoluto o magnitud
754 638
755 639 factor = self.dataOut.normFactor
756 640
757 641 z = self.data_spc.copy() # /factor
758 642 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
759 #zdB = 10*numpy.log10(z)
760 print(' ')
761 print('Z: ')
762 print(shape(z))
763 print(' ')
764 print(' ')
765
766 self.dataOut.data_spc = self.data_spc
767
768 self.noise = self.dataOut.getNoise(
769 ymin_index=80, ymax_index=132) # /factor
770 #noisedB = 10*numpy.log10(self.noise)
643 self.dataOut.data_spc=self.data_spc
644 self.noise = self.dataOut.getNoise(ymin_index=80, ymax_index=132)#/factor
771 645
772 646 ySamples = numpy.ones([3, self.nProfiles])
773 647 phase = numpy.ones([3, self.nProfiles])
774 648 CSPCSamples = numpy.ones(
775 649 [3, self.nProfiles], dtype=numpy.complex_)
776 650 coherence = numpy.ones([3, self.nProfiles])
777 651 PhaseSlope = numpy.ones(3)
778 652 PhaseInter = numpy.ones(3)
779 653
780 654 '''****** Getting CrossSpectra ******'''
781 cspc = self.data_block.copy()
782 self.data_cspc = self.data_block.copy()
783
784 xFrec = self.getVelRange(1)
785 VelRange = self.getVelRange(1)
786 self.dataOut.VelRange = VelRange
787 # print ' '
788 # print ' '
789 # print 'xFrec',xFrec
790 # print ' '
791 # print ' '
792 # Height=35
793 for i in range(self.nRdPairs):
794
655 cspc=self.data_block.copy()
656 self.data_cspc=self.data_block.copy()
657
658 xFrec=self.getVelRange(1)
659 VelRange=self.getVelRange(1)
660 self.dataOut.VelRange=VelRange
661
662
663 for i in range(self.nRdPairs):
664
795 665 chan_index0 = self.dataOut.pairsList[i][0]
796 666 chan_index1 = self.dataOut.pairsList[i][1]
797 667
798 668 self.data_cspc[i, :, :] = cspc[chan_index0, :,
799 669 :] * numpy.conjugate(cspc[chan_index1, :, :])
800 670
801 671 '''Getting Eij and Nij'''
802 672 (AntennaX0, AntennaY0) = pol2cart(
803 673 rheader.AntennaCoord0, rheader.AntennaAngl0 * numpy.pi / 180)
804 674 (AntennaX1, AntennaY1) = pol2cart(
805 675 rheader.AntennaCoord1, rheader.AntennaAngl1 * numpy.pi / 180)
806 676 (AntennaX2, AntennaY2) = pol2cart(
807 677 rheader.AntennaCoord2, rheader.AntennaAngl2 * numpy.pi / 180)
808 678
809 679 E01 = AntennaX0 - AntennaX1
810 680 N01 = AntennaY0 - AntennaY1
811 681
812 682 E02 = AntennaX0 - AntennaX2
813 683 N02 = AntennaY0 - AntennaY2
814 684
815 685 E12 = AntennaX1 - AntennaX2
816 686 N12 = AntennaY1 - AntennaY2
817 687
818 688 self.ChanDist = numpy.array(
819 689 [[E01, N01], [E02, N02], [E12, N12]])
820 690
821 691 self.dataOut.ChanDist = self.ChanDist
822 692
823
824 # for Height in range(self.nHeights):
825 #
826 # for i in range(self.nRdPairs):
827 #
828 # '''****** Line of Data SPC ******'''
829 # zline=z[i,:,Height]
830 #
831 # '''****** DC is removed ******'''
832 # DC=Find(zline,numpy.amax(zline))
833 # zline[DC]=(zline[DC-1]+zline[DC+1])/2
834 #
835 #
836 # '''****** SPC is normalized ******'''
837 # FactNorm= zline.copy() / numpy.sum(zline.copy())
838 # FactNorm= FactNorm/numpy.sum(FactNorm)
839 #
840 # SmoothSPC=moving_average(FactNorm,N=3)
841 #
842 # xSamples = ar(range(len(SmoothSPC)))
843 # ySamples[i] = SmoothSPC-self.noise[i]
844 #
845 # for i in range(self.nRdPairs):
846 #
847 # '''****** Line of Data CSPC ******'''
848 # cspcLine=self.data_cspc[i,:,Height].copy()
849 #
850 #
851 #
852 # '''****** CSPC is normalized ******'''
853 # chan_index0 = self.dataOut.pairsList[i][0]
854 # chan_index1 = self.dataOut.pairsList[i][1]
855 # CSPCFactor= numpy.sum(ySamples[chan_index0]) * numpy.sum(ySamples[chan_index1])
856 #
857 #
858 # CSPCNorm= cspcLine.copy() / numpy.sqrt(CSPCFactor)
859 #
860 #
861 # CSPCSamples[i] = CSPCNorm-self.noise[i]
862 # coherence[i] = numpy.abs(CSPCSamples[i]) / numpy.sqrt(CSPCFactor)
863 #
864 # '''****** DC is removed ******'''
865 # DC=Find(coherence[i],numpy.amax(coherence[i]))
866 # coherence[i][DC]=(coherence[i][DC-1]+coherence[i][DC+1])/2
867 # coherence[i]= moving_average(coherence[i],N=2)
868 #
869 # phase[i] = moving_average( numpy.arctan2(CSPCSamples[i].imag, CSPCSamples[i].real),N=1)#*180/numpy.pi
870 #
871 #
872 # '''****** Getting fij width ******'''
873 #
874 # yMean=[]
875 # yMean2=[]
876 #
877 # for j in range(len(ySamples[1])):
878 # yMean=numpy.append(yMean,numpy.average([ySamples[0,j],ySamples[1,j],ySamples[2,j]]))
879 #
880 # '''******* Getting fitting Gaussian ******'''
881 # meanGauss=sum(xSamples*yMean) / len(xSamples)
882 # sigma=sum(yMean*(xSamples-meanGauss)**2) / len(xSamples)
883 # #print 'Height',Height,'SNR', meanGauss/sigma**2
884 #
885 # if (abs(meanGauss/sigma**2) > 0.0001) :
886 #
887 # try:
888 # popt,pcov = curve_fit(gaus,xSamples,yMean,p0=[1,meanGauss,sigma])
889 #
890 # if numpy.amax(popt)>numpy.amax(yMean)*0.3:
891 # FitGauss=gaus(xSamples,*popt)
892 #
893 # else:
894 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
895 # print 'Verificador: Dentro', Height
896 # except RuntimeError:
897 #
898 # try:
899 # for j in range(len(ySamples[1])):
900 # yMean2=numpy.append(yMean2,numpy.average([ySamples[1,j],ySamples[2,j]]))
901 # popt,pcov = curve_fit(gaus,xSamples,yMean2,p0=[1,meanGauss,sigma])
902 # FitGauss=gaus(xSamples,*popt)
903 # print 'Verificador: Exepcion1', Height
904 # except RuntimeError:
905 #
906 # try:
907 # popt,pcov = curve_fit(gaus,xSamples,ySamples[1],p0=[1,meanGauss,sigma])
908 # FitGauss=gaus(xSamples,*popt)
909 # print 'Verificador: Exepcion2', Height
910 # except RuntimeError:
911 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
912 # print 'Verificador: Exepcion3', Height
913 # else:
914 # FitGauss=numpy.ones(len(xSamples))*numpy.mean(yMean)
915 # #print 'Verificador: Fuera', Height
916 #
917 #
918 #
919 # Maximun=numpy.amax(yMean)
920 # eMinus1=Maximun*numpy.exp(-1)
921 #
922 # HWpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-eMinus1)))
923 # HalfWidth= xFrec[HWpos]
924 # GCpos=Find(FitGauss, numpy.amax(FitGauss))
925 # Vpos=Find(FactNorm, numpy.amax(FactNorm))
926 # #Vpos=numpy.sum(FactNorm)/len(FactNorm)
927 # #Vpos=Find(FactNorm, min(FactNorm, key=lambda value:abs(value- numpy.mean(FactNorm) )))
928 # #print 'GCpos',GCpos, numpy.amax(FitGauss), 'HWpos',HWpos
929 # '''****** Getting Fij ******'''
930 #
931 # GaussCenter=xFrec[GCpos]
932 # if (GaussCenter<0 and HalfWidth>0) or (GaussCenter>0 and HalfWidth<0):
933 # Fij=abs(GaussCenter)+abs(HalfWidth)+0.0000001
934 # else:
935 # Fij=abs(GaussCenter-HalfWidth)+0.0000001
936 #
937 # '''****** Getting Frecuency range of significant data ******'''
938 #
939 # Rangpos=Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.10)))
940 #
941 # if Rangpos<GCpos:
942 # Range=numpy.array([Rangpos,2*GCpos-Rangpos])
943 # else:
944 # Range=numpy.array([2*GCpos-Rangpos,Rangpos])
945 #
946 # FrecRange=xFrec[Range[0]:Range[1]]
947 #
948 # #print 'FrecRange', FrecRange
949 # '''****** Getting SCPC Slope ******'''
950 #
951 # for i in range(self.nRdPairs):
952 #
953 # if len(FrecRange)>5 and len(FrecRange)<self.nProfiles*0.5:
954 # PhaseRange=moving_average(phase[i,Range[0]:Range[1]],N=3)
955 #
956 # slope, intercept, r_value, p_value, std_err = stats.linregress(FrecRange,PhaseRange)
957 # PhaseSlope[i]=slope
958 # PhaseInter[i]=intercept
959 # else:
960 # PhaseSlope[i]=0
961 # PhaseInter[i]=0
962 #
963 # # plt.figure(i+15)
964 # # plt.title('FASE ( CH%s*CH%s )' %(self.dataOut.pairsList[i][0],self.dataOut.pairsList[i][1]))
965 # # plt.xlabel('Frecuencia (KHz)')
966 # # plt.ylabel('Magnitud')
967 # # #plt.subplot(311+i)
968 # # plt.plot(FrecRange,PhaseRange,'b')
969 # # plt.plot(FrecRange,FrecRange*PhaseSlope[i]+PhaseInter[i],'r')
970 #
971 # #plt.axis([-0.6, 0.2, -3.2, 3.2])
972 #
973 #
974 # '''Getting constant C'''
975 # cC=(Fij*numpy.pi)**2
976 #
977 # # '''Getting Eij and Nij'''
978 # # (AntennaX0,AntennaY0)=pol2cart(rheader.AntennaCoord0, rheader.AntennaAngl0*numpy.pi/180)
979 # # (AntennaX1,AntennaY1)=pol2cart(rheader.AntennaCoord1, rheader.AntennaAngl1*numpy.pi/180)
980 # # (AntennaX2,AntennaY2)=pol2cart(rheader.AntennaCoord2, rheader.AntennaAngl2*numpy.pi/180)
981 # #
982 # # E01=AntennaX0-AntennaX1
983 # # N01=AntennaY0-AntennaY1
984 # #
985 # # E02=AntennaX0-AntennaX2
986 # # N02=AntennaY0-AntennaY2
987 # #
988 # # E12=AntennaX1-AntennaX2
989 # # N12=AntennaY1-AntennaY2
990 #
991 # '''****** Getting constants F and G ******'''
992 # MijEijNij=numpy.array([[E02,N02], [E12,N12]])
993 # MijResult0=(-PhaseSlope[1]*cC) / (2*numpy.pi)
994 # MijResult1=(-PhaseSlope[2]*cC) / (2*numpy.pi)
995 # MijResults=numpy.array([MijResult0,MijResult1])
996 # (cF,cG) = numpy.linalg.solve(MijEijNij, MijResults)
997 #
998 # '''****** Getting constants A, B and H ******'''
999 # W01=numpy.amax(coherence[0])
1000 # W02=numpy.amax(coherence[1])
1001 # W12=numpy.amax(coherence[2])
1002 #
1003 # WijResult0=((cF*E01+cG*N01)**2)/cC - numpy.log(W01 / numpy.sqrt(numpy.pi/cC))
1004 # WijResult1=((cF*E02+cG*N02)**2)/cC - numpy.log(W02 / numpy.sqrt(numpy.pi/cC))
1005 # WijResult2=((cF*E12+cG*N12)**2)/cC - numpy.log(W12 / numpy.sqrt(numpy.pi/cC))
1006 #
1007 # WijResults=numpy.array([WijResult0, WijResult1, WijResult2])
1008 #
1009 # WijEijNij=numpy.array([ [E01**2, N01**2, 2*E01*N01] , [E02**2, N02**2, 2*E02*N02] , [E12**2, N12**2, 2*E12*N12] ])
1010 # (cA,cB,cH) = numpy.linalg.solve(WijEijNij, WijResults)
1011 #
1012 # VxVy=numpy.array([[cA,cH],[cH,cB]])
1013 #
1014 # VxVyResults=numpy.array([-cF,-cG])
1015 # (Vx,Vy) = numpy.linalg.solve(VxVy, VxVyResults)
1016 # Vzon = Vy
1017 # Vmer = Vx
1018 # Vmag=numpy.sqrt(Vzon**2+Vmer**2)
1019 # Vang=numpy.arctan2(Vmer,Vzon)
1020 #
1021 # if abs(Vy)<100 and abs(Vy)> 0.:
1022 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, Vzon) #Vmag
1023 # #print 'Vmag',Vmag
1024 # else:
1025 # self.dataOut.velocityX=numpy.append(self.dataOut.velocityX, NaN)
1026 #
1027 # if abs(Vx)<100 and abs(Vx) > 0.:
1028 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, Vmer) #Vang
1029 # #print 'Vang',Vang
1030 # else:
1031 # self.dataOut.velocityY=numpy.append(self.dataOut.velocityY, NaN)
1032 #
1033 # if abs(GaussCenter)<2:
1034 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, xFrec[Vpos])
1035 #
1036 # else:
1037 # self.dataOut.velocityV=numpy.append(self.dataOut.velocityV, NaN)
1038 #
1039 #
1040 # # print '********************************************'
1041 # # print 'HalfWidth ', HalfWidth
1042 # # print 'Maximun ', Maximun
1043 # # print 'eMinus1 ', eMinus1
1044 # # print 'Rangpos ', Rangpos
1045 # # print 'GaussCenter ',GaussCenter
1046 # # print 'E01 ',E01
1047 # # print 'N01 ',N01
1048 # # print 'E02 ',E02
1049 # # print 'N02 ',N02
1050 # # print 'E12 ',E12
1051 # # print 'N12 ',N12
1052 # #print 'self.dataOut.velocityX ', self.dataOut.velocityX
1053 # # print 'Fij ', Fij
1054 # # print 'cC ', cC
1055 # # print 'cF ', cF
1056 # # print 'cG ', cG
1057 # # print 'cA ', cA
1058 # # print 'cB ', cB
1059 # # print 'cH ', cH
1060 # # print 'Vx ', Vx
1061 # # print 'Vy ', Vy
1062 # # print 'Vmag ', Vmag
1063 # # print 'Vang ', Vang*180/numpy.pi
1064 # # print 'PhaseSlope ',PhaseSlope[0]
1065 # # print 'PhaseSlope ',PhaseSlope[1]
1066 # # print 'PhaseSlope ',PhaseSlope[2]
1067 # # print '********************************************'
1068 # #print 'data_output',shape(self.dataOut.velocityX), shape(self.dataOut.velocityY)
1069 #
1070 # #print 'self.dataOut.velocityX', len(self.dataOut.velocityX)
1071 # #print 'self.dataOut.velocityY', len(self.dataOut.velocityY)
1072 # #print 'self.dataOut.velocityV', self.dataOut.velocityV
1073 #
1074 # self.data_output[0]=numpy.array(self.dataOut.velocityX)
1075 # self.data_output[1]=numpy.array(self.dataOut.velocityY)
1076 # self.data_output[2]=numpy.array(self.dataOut.velocityV)
1077 #
1078 # prin= self.data_output[0][~numpy.isnan(self.data_output[0])]
1079 # print ' '
1080 # print 'VmagAverage',numpy.mean(prin)
1081 # print ' '
1082 # # plt.figure(5)
1083 # # plt.subplot(211)
1084 # # plt.plot(self.dataOut.velocityX,'yo:')
1085 # # plt.subplot(212)
1086 # # plt.plot(self.dataOut.velocityY,'yo:')
1087 #
1088 # # plt.figure(1)
1089 # # # plt.subplot(121)
1090 # # # plt.plot(xFrec,ySamples[0],'k',label='Ch0')
1091 # # # plt.plot(xFrec,ySamples[1],'g',label='Ch1')
1092 # # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1093 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1094 # # # plt.legend()
1095 # # plt.title('DATOS A ALTURA DE 2850 METROS')
1096 # #
1097 # # plt.xlabel('Frecuencia (KHz)')
1098 # # plt.ylabel('Magnitud')
1099 # # # plt.subplot(122)
1100 # # # plt.title('Fit for Time Constant')
1101 # # #plt.plot(xFrec,zline)
1102 # # #plt.plot(xFrec,SmoothSPC,'g')
1103 # # plt.plot(xFrec,FactNorm)
1104 # # plt.axis([-4, 4, 0, 0.15])
1105 # # # plt.xlabel('SelfSpectra KHz')
1106 # #
1107 # # plt.figure(10)
1108 # # # plt.subplot(121)
1109 # # plt.plot(xFrec,ySamples[0],'b',label='Ch0')
1110 # # plt.plot(xFrec,ySamples[1],'y',label='Ch1')
1111 # # plt.plot(xFrec,ySamples[2],'r',label='Ch2')
1112 # # # plt.plot(xFrec,FitGauss,'yo:',label='fit')
1113 # # plt.legend()
1114 # # plt.title('SELFSPECTRA EN CANALES')
1115 # #
1116 # # plt.xlabel('Frecuencia (KHz)')
1117 # # plt.ylabel('Magnitud')
1118 # # # plt.subplot(122)
1119 # # # plt.title('Fit for Time Constant')
1120 # # #plt.plot(xFrec,zline)
1121 # # #plt.plot(xFrec,SmoothSPC,'g')
1122 # # # plt.plot(xFrec,FactNorm)
1123 # # # plt.axis([-4, 4, 0, 0.15])
1124 # # # plt.xlabel('SelfSpectra KHz')
1125 # #
1126 # # plt.figure(9)
1127 # #
1128 # #
1129 # # plt.title('DATOS SUAVIZADOS')
1130 # # plt.xlabel('Frecuencia (KHz)')
1131 # # plt.ylabel('Magnitud')
1132 # # plt.plot(xFrec,SmoothSPC,'g')
1133 # #
1134 # # #plt.plot(xFrec,FactNorm)
1135 # # plt.axis([-4, 4, 0, 0.15])
1136 # # # plt.xlabel('SelfSpectra KHz')
1137 # # #
1138 # # plt.figure(2)
1139 # # # #plt.subplot(121)
1140 # # plt.plot(xFrec,yMean,'r',label='Mean SelfSpectra')
1141 # # plt.plot(xFrec,FitGauss,'yo:',label='Ajuste Gaussiano')
1142 # # # plt.plot(xFrec[Rangpos],FitGauss[Find(FitGauss,min(FitGauss, key=lambda value:abs(value-Maximun*0.1)))],'bo')
1143 # # # #plt.plot(xFrec,phase)
1144 # # # plt.xlabel('Suavizado, promediado KHz')
1145 # # plt.title('SELFSPECTRA PROMEDIADO')
1146 # # # #plt.subplot(122)
1147 # # # #plt.plot(xSamples,zline)
1148 # # plt.xlabel('Frecuencia (KHz)')
1149 # # plt.ylabel('Magnitud')
1150 # # plt.legend()
1151 # # #
1152 # # # plt.figure(3)
1153 # # # plt.subplot(311)
1154 # # # #plt.plot(xFrec,phase[0])
1155 # # # plt.plot(xFrec,phase[0],'g')
1156 # # # plt.subplot(312)
1157 # # # plt.plot(xFrec,phase[1],'g')
1158 # # # plt.subplot(313)
1159 # # # plt.plot(xFrec,phase[2],'g')
1160 # # # #plt.plot(xFrec,phase[2])
1161 # # #
1162 # # # plt.figure(4)
1163 # # #
1164 # # # plt.plot(xSamples,coherence[0],'b')
1165 # # # plt.plot(xSamples,coherence[1],'r')
1166 # # # plt.plot(xSamples,coherence[2],'g')
1167 # # plt.show()
1168 # # #
1169 # # # plt.clf()
1170 # # # plt.cla()
1171 # # # plt.close()
1172 #
1173 # print ' '
1174
1175 self.BlockCounter += 2
1176
693 self.BlockCounter+=2
694
1177 695 else:
1178 self.fileSelector += 1
1179 self.BlockCounter = 0
1180 print("Next File") No newline at end of file
696 self.fileSelector+=1
697 self.BlockCounter=0
@@ -1,1103 +1,1093
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, MPDecorator
10 10 # from .jroIO_base import *
11 11 from schainpy.model.io.jroIO_base import *
12 12 import schainpy
13 13 from schainpy.utils import log
14 14
15 15 @MPDecorator
16 16 class ParamReader(JRODataReader,ProcessingUnit):
17 17 '''
18 18 Reads HDF5 format files
19 19
20 20 path
21 21
22 22 startDate
23 23
24 24 endDate
25 25
26 26 startTime
27 27
28 28 endTime
29 29 '''
30 30
31 31 ext = ".hdf5"
32 32
33 33 optchar = "D"
34 34
35 35 timezone = None
36 36
37 37 startTime = None
38 38
39 39 endTime = None
40 40
41 41 fileIndex = None
42 42
43 43 utcList = None #To select data in the utctime list
44 44
45 45 blockList = None #List to blocks to be read from the file
46 46
47 47 blocksPerFile = None #Number of blocks to be read
48 48
49 49 blockIndex = None
50 50
51 51 path = None
52 52
53 53 #List of Files
54 54
55 55 filenameList = None
56 56
57 57 datetimeList = None
58 58
59 59 #Hdf5 File
60 60
61 61 listMetaname = None
62 62
63 63 listMeta = None
64 64
65 65 listDataname = None
66 66
67 67 listData = None
68 68
69 69 listShapes = None
70 70
71 71 fp = None
72 72
73 73 #dataOut reconstruction
74 74
75 75 dataOut = None
76 76
77 77
78 78 def __init__(self):#, **kwargs):
79 79 ProcessingUnit.__init__(self) #, **kwargs)
80 80 self.dataOut = Parameters()
81 81 return
82 82
83 83 def setup(self, **kwargs):
84 84
85 85 path = kwargs['path']
86 86 startDate = kwargs['startDate']
87 87 endDate = kwargs['endDate']
88 88 startTime = kwargs['startTime']
89 89 endTime = kwargs['endTime']
90 90 walk = kwargs['walk']
91 91 if 'ext' in kwargs:
92 92 ext = kwargs['ext']
93 93 else:
94 94 ext = '.hdf5'
95 95 if 'timezone' in kwargs:
96 96 self.timezone = kwargs['timezone']
97 97 else:
98 98 self.timezone = 'lt'
99 99
100 100 print("[Reading] Searching files in offline mode ...")
101 101 pathList, filenameList = self.searchFilesOffLine(path, startDate=startDate, endDate=endDate,
102 102 startTime=startTime, endTime=endTime,
103 103 ext=ext, walk=walk)
104 104
105 105 if not(filenameList):
106 106 print("There is no files into the folder: %s"%(path))
107 107 sys.exit(-1)
108 108
109 109 self.fileIndex = -1
110 110 self.startTime = startTime
111 111 self.endTime = endTime
112 112
113 113 self.__readMetadata()
114 114
115 115 self.__setNextFileOffline()
116 116
117 117 return
118 118
119 119 def searchFilesOffLine(self,
120 120 path,
121 121 startDate=None,
122 122 endDate=None,
123 123 startTime=datetime.time(0,0,0),
124 124 endTime=datetime.time(23,59,59),
125 125 ext='.hdf5',
126 126 walk=True):
127 127
128 128 expLabel = ''
129 129 self.filenameList = []
130 130 self.datetimeList = []
131 131
132 132 pathList = []
133 133
134 134 JRODataObj = JRODataReader()
135 135 dateList, pathList = JRODataObj.findDatafiles(path, startDate, endDate, expLabel, ext, walk, include_path=True)
136 136
137 137 if dateList == []:
138 138 print("[Reading] No *%s files in %s from %s to %s)"%(ext, path,
139 139 datetime.datetime.combine(startDate,startTime).ctime(),
140 140 datetime.datetime.combine(endDate,endTime).ctime()))
141 141
142 142 return None, None
143 143
144 144 if len(dateList) > 1:
145 145 print("[Reading] %d days were found in date range: %s - %s" %(len(dateList), startDate, endDate))
146 146 else:
147 147 print("[Reading] data was found for the date %s" %(dateList[0]))
148 148
149 149 filenameList = []
150 150 datetimeList = []
151 151
152 152 #----------------------------------------------------------------------------------
153 153
154 154 for thisPath in pathList:
155 155 # thisPath = pathList[pathDict[file]]
156 156
157 157 fileList = glob.glob1(thisPath, "*%s" %ext)
158 158 fileList.sort()
159 159
160 160 for file in fileList:
161 161
162 162 filename = os.path.join(thisPath,file)
163 163
164 164 if not isFileInDateRange(filename, startDate, endDate):
165 165 continue
166 166
167 167 thisDatetime = self.__isFileInTimeRange(filename, startDate, endDate, startTime, endTime)
168 168
169 169 if not(thisDatetime):
170 170 continue
171 171
172 172 filenameList.append(filename)
173 173 datetimeList.append(thisDatetime)
174 174
175 175 if not(filenameList):
176 176 print("[Reading] Any file was found int time range %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()))
177 177 return None, None
178 178
179 179 print("[Reading] %d file(s) was(were) found in time range: %s - %s" %(len(filenameList), startTime, endTime))
180 180 print()
181 181
182 # for i in range(len(filenameList)):
183 # print "[Reading] %s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
184
185 182 self.filenameList = filenameList
186 183 self.datetimeList = datetimeList
187 184
188 185 return pathList, filenameList
189 186
190 187 def __isFileInTimeRange(self,filename, startDate, endDate, startTime, endTime):
191 188
192 189 """
193 190 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
194 191
195 192 Inputs:
196 193 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
197 194
198 195 startDate : fecha inicial del rango seleccionado en formato datetime.date
199 196
200 197 endDate : fecha final del rango seleccionado en formato datetime.date
201 198
202 199 startTime : tiempo inicial del rango seleccionado en formato datetime.time
203 200
204 201 endTime : tiempo final del rango seleccionado en formato datetime.time
205 202
206 203 Return:
207 204 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
208 205 fecha especificado, de lo contrario retorna False.
209 206
210 207 Excepciones:
211 208 Si el archivo no existe o no puede ser abierto
212 209 Si la cabecera no puede ser leida.
213 210
214 211 """
215 212
216 213 try:
217 214 fp = h5py.File(filename,'r')
218 215 grp1 = fp['Data']
219 216
220 217 except IOError:
221 218 traceback.print_exc()
222 219 raise IOError("The file %s can't be opened" %(filename))
223 220 #chino rata
224 221 #In case has utctime attribute
225 222 grp2 = grp1['utctime']
226 223 # thisUtcTime = grp2.value[0] - 5*3600 #To convert to local time
227 224 thisUtcTime = grp2.value[0]
228 225
229 226 fp.close()
230 227
231 228 if self.timezone == 'lt':
232 229 thisUtcTime -= 5*3600
233 230
234 231 thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600)
235 232 # thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0])
236 233 thisDate = thisDatetime.date()
237 234 thisTime = thisDatetime.time()
238 235
239 236 startUtcTime = (datetime.datetime.combine(thisDate,startTime)- datetime.datetime(1970, 1, 1)).total_seconds()
240 237 endUtcTime = (datetime.datetime.combine(thisDate,endTime)- datetime.datetime(1970, 1, 1)).total_seconds()
241 238
242 239 #General case
243 240 # o>>>>>>>>>>>>>><<<<<<<<<<<<<<o
244 241 #-----------o----------------------------o-----------
245 242 # startTime endTime
246 243
247 244 if endTime >= startTime:
248 245 thisUtcLog = numpy.logical_and(thisUtcTime > startUtcTime, thisUtcTime < endUtcTime)
249 246 if numpy.any(thisUtcLog): #If there is one block between the hours mentioned
250 247 return thisDatetime
251 248 return None
252 249
253 250 #If endTime < startTime then endTime belongs to the next day
254 251 #<<<<<<<<<<<o o>>>>>>>>>>>
255 252 #-----------o----------------------------o-----------
256 253 # endTime startTime
257 254
258 255 if (thisDate == startDate) and numpy.all(thisUtcTime < startUtcTime):
259 256 return None
260 257
261 258 if (thisDate == endDate) and numpy.all(thisUtcTime > endUtcTime):
262 259 return None
263 260
264 261 if numpy.all(thisUtcTime < startUtcTime) and numpy.all(thisUtcTime > endUtcTime):
265 262 return None
266 263
267 264 return thisDatetime
268 265
269 266 def __setNextFileOffline(self):
270 267
271 268 self.fileIndex += 1
272 269 idFile = self.fileIndex
273 270
274 271 if not(idFile < len(self.filenameList)):
275 272 print("No more Files")
276 273 return 0
277 274
278 275 filename = self.filenameList[idFile]
279 276
280 277 filePointer = h5py.File(filename,'r')
281 278
282 279 self.filename = filename
283 280
284 281 self.fp = filePointer
285 282
286 283 print("Setting the file: %s"%self.filename)
287 284
288 285 # self.__readMetadata()
289 286 self.__setBlockList()
290 287 self.__readData()
291 288 # self.nRecords = self.fp['Data'].attrs['blocksPerFile']
292 289 # self.nRecords = self.fp['Data'].attrs['nRecords']
293 290 self.blockIndex = 0
294 291 return 1
295 292
296 293 def __setBlockList(self):
297 294 '''
298 295 Selects the data within the times defined
299 296
300 297 self.fp
301 298 self.startTime
302 299 self.endTime
303 300
304 301 self.blockList
305 302 self.blocksPerFile
306 303
307 304 '''
308 305 fp = self.fp
309 306 startTime = self.startTime
310 307 endTime = self.endTime
311 308
312 309 grp = fp['Data']
313 310 thisUtcTime = grp['utctime'].value.astype(numpy.float)[0]
314 311
315 312 #ERROOOOR
316 313 if self.timezone == 'lt':
317 314 thisUtcTime -= 5*3600
318 315
319 316 thisDatetime = datetime.datetime.fromtimestamp(thisUtcTime[0] + 5*3600)
320 317
321 318 thisDate = thisDatetime.date()
322 319 thisTime = thisDatetime.time()
323 320
324 321 startUtcTime = (datetime.datetime.combine(thisDate,startTime) - datetime.datetime(1970, 1, 1)).total_seconds()
325 322 endUtcTime = (datetime.datetime.combine(thisDate,endTime) - datetime.datetime(1970, 1, 1)).total_seconds()
326 323
327 324 ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0]
328 325
329 326 self.blockList = ind
330 327 self.blocksPerFile = len(ind)
331 328
332 329 return
333 330
334 331 def __readMetadata(self):
335 332 '''
336 333 Reads Metadata
337 334
338 335 self.pathMeta
339 336
340 337 self.listShapes
341 338 self.listMetaname
342 339 self.listMeta
343 340
344 341 '''
345 342
346 343 # grp = self.fp['Data']
347 344 # pathMeta = os.path.join(self.path, grp.attrs['metadata'])
348 345 #
349 346 # if pathMeta == self.pathMeta:
350 347 # return
351 348 # else:
352 349 # self.pathMeta = pathMeta
353 350 #
354 351 # filePointer = h5py.File(self.pathMeta,'r')
355 352 # groupPointer = filePointer['Metadata']
356 353
357 354 filename = self.filenameList[0]
358 355
359 356 fp = h5py.File(filename,'r')
360 357
361 358 gp = fp['Metadata']
362 359
363 360 listMetaname = []
364 361 listMetadata = []
365 362 for item in list(gp.items()):
366 363 name = item[0]
367 364
368 365 if name=='array dimensions':
369 366 table = gp[name][:]
370 367 listShapes = {}
371 368 for shapes in table:
372 369 listShapes[shapes[0]] = numpy.array([shapes[1],shapes[2],shapes[3],shapes[4],shapes[5]])
373 370 else:
374 371 data = gp[name].value
375 372 listMetaname.append(name)
376 373 listMetadata.append(data)
377 374
378 375 # if name=='type':
379 376 # self.__initDataOut(data)
380 377
381 378 self.listShapes = listShapes
382 379 self.listMetaname = listMetaname
383 380 self.listMeta = listMetadata
384 381
385 382 fp.close()
386 383 return
387 384
388 385 def __readData(self):
389 386 grp = self.fp['Data']
390 387 listdataname = []
391 388 listdata = []
392 389
393 390 for item in list(grp.items()):
394 391 name = item[0]
395 392 listdataname.append(name)
396 393
397 394 array = self.__setDataArray(grp[name],self.listShapes[name])
398 395 listdata.append(array)
399 396
400 397 self.listDataname = listdataname
401 398 self.listData = listdata
402 399 return
403 400
404 401 def __setDataArray(self, dataset, shapes):
405 402
406 403 nDims = shapes[0]
407 404
408 405 nDim2 = shapes[1] #Dimension 0
409 406
410 407 nDim1 = shapes[2] #Dimension 1, number of Points or Parameters
411 408
412 409 nDim0 = shapes[3] #Dimension 2, number of samples or ranges
413 410
414 411 mode = shapes[4] #Mode of storing
415 412
416 413 blockList = self.blockList
417 414
418 415 blocksPerFile = self.blocksPerFile
419 416
420 417 #Depending on what mode the data was stored
421 418 if mode == 0: #Divided in channels
422 419 arrayData = dataset.value.astype(numpy.float)[0][blockList]
423 420 if mode == 1: #Divided in parameter
424 421 strds = 'table'
425 422 nDatas = nDim1
426 423 newShapes = (blocksPerFile,nDim2,nDim0)
427 424 elif mode==2: #Concatenated in a table
428 425 strds = 'table0'
429 426 arrayData = dataset[strds].value
430 427 #Selecting part of the dataset
431 428 utctime = arrayData[:,0]
432 429 u, indices = numpy.unique(utctime, return_index=True)
433 430
434 431 if blockList.size != indices.size:
435 432 indMin = indices[blockList[0]]
436 433 if blockList[1] + 1 >= indices.size:
437 434 arrayData = arrayData[indMin:,:]
438 435 else:
439 436 indMax = indices[blockList[1] + 1]
440 437 arrayData = arrayData[indMin:indMax,:]
441 438 return arrayData
442 439
443 440 # One dimension
444 441 if nDims == 0:
445 442 arrayData = dataset.value.astype(numpy.float)[0][blockList]
446 443
447 444 # Two dimensions
448 445 elif nDims == 2:
449 446 arrayData = numpy.zeros((blocksPerFile,nDim1,nDim0))
450 447 newShapes = (blocksPerFile,nDim0)
451 448 nDatas = nDim1
452 449
453 450 for i in range(nDatas):
454 451 data = dataset[strds + str(i)].value
455 452 arrayData[:,i,:] = data[blockList,:]
456 453
457 454 # Three dimensions
458 455 else:
459 456 arrayData = numpy.zeros((blocksPerFile,nDim2,nDim1,nDim0))
460 457 for i in range(nDatas):
461 458
462 459 data = dataset[strds + str(i)].value
463 460
464 461 for b in range(blockList.size):
465 462 arrayData[b,:,i,:] = data[:,:,blockList[b]]
466 463
467 464 return arrayData
468 465
469 466 def __setDataOut(self):
470 467 listMeta = self.listMeta
471 468 listMetaname = self.listMetaname
472 469 listDataname = self.listDataname
473 470 listData = self.listData
474 471 listShapes = self.listShapes
475 472
476 473 blockIndex = self.blockIndex
477 474 # blockList = self.blockList
478 475
479 476 for i in range(len(listMeta)):
480 477 setattr(self.dataOut,listMetaname[i],listMeta[i])
481 478
482 479 for j in range(len(listData)):
483 480 nShapes = listShapes[listDataname[j]][0]
484 481 mode = listShapes[listDataname[j]][4]
485 482 if nShapes == 1:
486 483 setattr(self.dataOut,listDataname[j],listData[j][blockIndex])
487 484 elif nShapes > 1:
488 485 setattr(self.dataOut,listDataname[j],listData[j][blockIndex,:])
489 486 elif mode==0:
490 487 setattr(self.dataOut,listDataname[j],listData[j][blockIndex])
491 488 #Mode Meteors
492 489 elif mode ==2:
493 490 selectedData = self.__selectDataMode2(listData[j], blockIndex)
494 491 setattr(self.dataOut, listDataname[j], selectedData)
495 492 return
496 493
497 494 def __selectDataMode2(self, data, blockIndex):
498 495 utctime = data[:,0]
499 496 aux, indices = numpy.unique(utctime, return_inverse=True)
500 497 selInd = numpy.where(indices == blockIndex)[0]
501 498 selData = data[selInd,:]
502 499
503 500 return selData
504 501
505 502 def getData(self):
506 503
507 # if self.flagNoMoreFiles:
508 # self.dataOut.flagNoData = True
509 # print 'Process finished'
510 # return 0
511 #
512 504 if self.blockIndex==self.blocksPerFile:
513 505 if not( self.__setNextFileOffline() ):
514 506 self.dataOut.flagNoData = True
515 507 return 0
516 508
517 # if self.datablock == None: # setear esta condicion cuando no hayan datos por leers
518 # self.dataOut.flagNoData = True
519 # return 0
520 # self.__readData()
521 509 self.__setDataOut()
522 510 self.dataOut.flagNoData = False
523 511
524 512 self.blockIndex += 1
525 513
526 514 return
527 515
528 516 def run(self, **kwargs):
529 517
530 518 if not(self.isConfig):
531 519 self.setup(**kwargs)
532 520 # self.setObjProperties()
533 521 self.isConfig = True
534 522
535 523 self.getData()
536 524
537 525 return
538 526 @MPDecorator
539 527 class ParamWriter(Operation):
540 528 '''
541 529 HDF5 Writer, stores parameters data in HDF5 format files
542 530
543 531 path: path where the files will be stored
544 532
545 533 blocksPerFile: number of blocks that will be saved in per HDF5 format file
546 534
547 535 mode: selects the data stacking mode: '0' channels, '1' parameters, '3' table (for meteors)
548 536
549 537 metadataList: list of attributes that will be stored as metadata
550 538
551 539 dataList: list of attributes that will be stores as data
552 540
553 541 '''
554 542
555 543
556 544 ext = ".hdf5"
557 545
558 546 optchar = "D"
559 547
560 548 metaoptchar = "M"
561 549
562 550 metaFile = None
563 551
564 552 filename = None
565 553
566 554 path = None
567 555
568 556 setFile = None
569 557
570 558 fp = None
571 559
572 560 grp = None
573 561
574 562 ds = None
575 563
576 564 firsttime = True
577 565
578 566 #Configurations
579 567
580 568 blocksPerFile = None
581 569
582 570 blockIndex = None
583 571
584 572 dataOut = None
585 573
586 574 #Data Arrays
587 575
588 576 dataList = None
589 577
590 578 metadataList = None
591 579
592 580 # arrayDim = None
593 581
594 582 dsList = None #List of dictionaries with dataset properties
595 583
596 584 tableDim = None
597 585
598 586 # dtype = [('arrayName', 'S20'),('nChannels', 'i'), ('nPoints', 'i'), ('nSamples', 'i'),('mode', 'b')]
599 587
600 588 dtype = [('arrayName', 'S20'),('nDimensions', 'i'), ('dim2', 'i'), ('dim1', 'i'),('dim0', 'i'),('mode', 'b')]
601 589
602 590 currentDay = None
603 591
604 592 lastTime = None
605 593
606 594 def __init__(self):#, **kwargs):
607 595 Operation.__init__(self)#, **kwargs)
608 596 #self.isConfig = False
609 597 return
610 598
611 599 def setup(self, dataOut, path=None, blocksPerFile=10, metadataList=None, dataList=None, mode=None, **kwargs):
612 600 self.path = path
613 601 self.blocksPerFile = blocksPerFile
614 602 self.metadataList = metadataList
615 603 self.dataList = dataList
616 604 self.dataOut = dataOut
617 605 self.mode = mode
618 606 if self.mode is not None:
619 607 self.mode = numpy.zeros(len(self.dataList)) + mode
620 608 else:
621 609 #self.mode = numpy.ones(len(self.dataList),int)
622 610 self.mode = numpy.ones(len(self.dataList))
623 611 log.error(self.mode)#yong
624 612
625 613 arrayDim = numpy.zeros((len(self.dataList),5))
626 614
627 615 #Table dimensions
628 616 dtype0 = self.dtype
629 617 tableList = []
630 618
631 619 #Dictionary and list of tables
632 620 dsList = []
633 621
634 622 for i in range(len(self.dataList)):
635 623 dsDict = {}
636 624 dataAux = getattr(self.dataOut, self.dataList[i])
637 625 dsDict['variable'] = self.dataList[i]
638 626 #--------------------- Conditionals ------------------------
639 627 #There is no data
628
629
640 630 if dataAux is None:
631
641 632 return 0
642 633
643 634 #Not array, just a number
644 635 #Mode 0
645 636 #log.error(mode)#yong
646 637 #log.error(len(mode))#yong
647 638 #log.error(type(mode))#yong
648 639 if type(dataAux)==float or type(dataAux)==int:
649 640 dsDict['mode'] = 0
650 641 dsDict['nDim'] = 0
651 642 arrayDim[i,0] = 0
652 643 dsList.append(dsDict)
653 644
654 645 #Mode 2: meteors
655 646 elif self.mode[i] == 2:
656 647 # dsDict['nDim'] = 0
657 648 dsDict['dsName'] = 'table0'
658 649 dsDict['mode'] = 2 # Mode meteors
659 650 dsDict['shape'] = dataAux.shape[-1]
660 651 dsDict['nDim'] = 0
661 652 dsDict['dsNumber'] = 1
662 653
663 654 arrayDim[i,3] = dataAux.shape[-1]
664 655 arrayDim[i,4] = self.mode[i] #Mode the data was stored
665 656
666 657 dsList.append(dsDict)
667 658
668 659 #Mode 1
669 660 else:
670 661 arrayDim0 = dataAux.shape #Data dimensions
671 662 arrayDim[i,0] = len(arrayDim0) #Number of array dimensions
672 663 arrayDim[i,4] = self.mode[i] #Mode the data was stored
673 664
674 665 strtable = 'table'
675 666 dsDict['mode'] = 1 # Mode parameters
676 667
677 668 # Three-dimension arrays
678 669 if len(arrayDim0) == 3:
679 670 arrayDim[i,1:-1] = numpy.array(arrayDim0)
680 671 nTables = int(arrayDim[i,2])
681 672 dsDict['dsNumber'] = nTables
682 673 dsDict['shape'] = arrayDim[i,2:4]
683 674 dsDict['nDim'] = 3
684 675
685 676 for j in range(nTables):
686 677 dsDict = dsDict.copy()
687 678 dsDict['dsName'] = strtable + str(j)
688 679 dsList.append(dsDict)
689 680
690 681 # Two-dimension arrays
691 682 elif len(arrayDim0) == 2:
692 683 arrayDim[i,2:-1] = numpy.array(arrayDim0)
693 684 nTables = int(arrayDim[i,2])
694 685 dsDict['dsNumber'] = nTables
695 686 dsDict['shape'] = arrayDim[i,3]
696 687 dsDict['nDim'] = 2
697 688
698 689 for j in range(nTables):
699 690 dsDict = dsDict.copy()
700 691 dsDict['dsName'] = strtable + str(j)
701 692 dsList.append(dsDict)
702 693
703 694 # One-dimension arrays
704 695 elif len(arrayDim0) == 1:
705 696 arrayDim[i,3] = arrayDim0[0]
706 697 dsDict['shape'] = arrayDim0[0]
707 698 dsDict['dsNumber'] = 1
708 699 dsDict['dsName'] = strtable + str(0)
709 700 dsDict['nDim'] = 1
710 701 dsList.append(dsDict)
711 702
712 703 table = numpy.array((self.dataList[i],) + tuple(arrayDim[i,:]),dtype = dtype0)
713 704 tableList.append(table)
714 705
715 706 # self.arrayDim = arrayDim
716 707 self.dsList = dsList
717 708 self.tableDim = numpy.array(tableList, dtype = dtype0)
718 709 self.blockIndex = 0
719 710
720 711 timeTuple = time.localtime(dataOut.utctime)
721 712 self.currentDay = timeTuple.tm_yday
722 713 return 1
723 714
724 715 def putMetadata(self):
725 716
726 717 fp = self.createMetadataFile()
727 718 self.writeMetadata(fp)
728 719 fp.close()
729 720 return
730 721
731 722 def createMetadataFile(self):
732 723 ext = self.ext
733 724 path = self.path
734 725 setFile = self.setFile
735 726
736 727 timeTuple = time.localtime(self.dataOut.utctime)
737 728
738 729 subfolder = ''
739 730 fullpath = os.path.join( path, subfolder )
740 731
741 732 if not( os.path.exists(fullpath) ):
742 733 os.mkdir(fullpath)
743 734 setFile = -1 #inicializo mi contador de seteo
744 735
745 736 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
746 737 fullpath = os.path.join( path, subfolder )
747 738
748 739 if not( os.path.exists(fullpath) ):
749 740 os.mkdir(fullpath)
750 741 setFile = -1 #inicializo mi contador de seteo
751 742
752 743 else:
753 744 filesList = os.listdir( fullpath )
754 745 filesList = sorted( filesList, key=str.lower )
755 746 if len( filesList ) > 0:
756 747 filesList = [k for k in filesList if 'M' in k]
757 748 filen = filesList[-1]
758 749 # el filename debera tener el siguiente formato
759 750 # 0 1234 567 89A BCDE (hex)
760 751 # x YYYY DDD SSS .ext
761 752 if isNumber( filen[8:11] ):
762 753 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
763 754 else:
764 755 setFile = -1
765 756 else:
766 757 setFile = -1 #inicializo mi contador de seteo
767 758
768 759 if self.setType is None:
769 760 setFile += 1
770 761 file = '%s%4.4d%3.3d%03d%s' % (self.metaoptchar,
771 762 timeTuple.tm_year,
772 763 timeTuple.tm_yday,
773 764 setFile,
774 765 ext )
775 766 else:
776 767 setFile = timeTuple.tm_hour*60+timeTuple.tm_min
777 768 file = '%s%4.4d%3.3d%04d%s' % (self.metaoptchar,
778 769 timeTuple.tm_year,
779 770 timeTuple.tm_yday,
780 771 setFile,
781 772 ext )
782 773
783 774 filename = os.path.join( path, subfolder, file )
784 775 self.metaFile = file
785 776 #Setting HDF5 File
786 777 fp = h5py.File(filename,'w')
787 778
788 779 return fp
789 780
790 781 def writeMetadata(self, fp):
791 782
792 783 grp = fp.create_group("Metadata")
793 784 grp.create_dataset('array dimensions', data = self.tableDim, dtype = self.dtype)
794 785
795 786 for i in range(len(self.metadataList)):
796 787 grp.create_dataset(self.metadataList[i], data=getattr(self.dataOut, self.metadataList[i]))
797 788 return
798 789
799 790 def timeFlag(self):
800 791 currentTime = self.dataOut.utctime
801 792
802 793 if self.lastTime is None:
803 794 self.lastTime = currentTime
804 795
805 796 #Day
806 797 timeTuple = time.localtime(currentTime)
807 798 dataDay = timeTuple.tm_yday
808 799
809 800 #Time
810 801 timeDiff = currentTime - self.lastTime
811 802
812 803 #Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
813 804 if dataDay != self.currentDay:
814 805 self.currentDay = dataDay
815 806 return True
816 807 elif timeDiff > 3*60*60:
817 808 self.lastTime = currentTime
818 809 return True
819 810 else:
820 811 self.lastTime = currentTime
821 812 return False
822 813
823 814 def setNextFile(self):
824
815
825 816 ext = self.ext
826 817 path = self.path
827 818 setFile = self.setFile
828 819 mode = self.mode
829 820
830 821 timeTuple = time.localtime(self.dataOut.utctime)
831 822 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
832 823
833 824 fullpath = os.path.join( path, subfolder )
834 825
835 826 if os.path.exists(fullpath):
836 827 filesList = os.listdir( fullpath )
837 828 filesList = [k for k in filesList if 'D' in k]
838 829 if len( filesList ) > 0:
839 830 filesList = sorted( filesList, key=str.lower )
840 831 filen = filesList[-1]
841 832 # el filename debera tener el siguiente formato
842 833 # 0 1234 567 89A BCDE (hex)
843 834 # x YYYY DDD SSS .ext
844 835 if isNumber( filen[8:11] ):
845 836 setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
846 837 else:
847 838 setFile = -1
848 839 else:
849 840 setFile = -1 #inicializo mi contador de seteo
850 841 else:
851 842 os.makedirs(fullpath)
852 843 setFile = -1 #inicializo mi contador de seteo
853 844
854 845 if None is None:
855 846 setFile += 1
856 847 file = '%s%4.4d%3.3d%03d%s' % (self.metaoptchar,
857 848 timeTuple.tm_year,
858 849 timeTuple.tm_yday,
859 850 setFile,
860 851 ext )
861 852 else:
862 853 setFile = timeTuple.tm_hour*60+timeTuple.tm_min
863 854 file = '%s%4.4d%3.3d%04d%s' % (self.metaoptchar,
864 855 timeTuple.tm_year,
865 856 timeTuple.tm_yday,
866 857 setFile,
867 858 ext )
868 859
869 860 filename = os.path.join( path, subfolder, file )
870 861
871 862 #Setting HDF5 File
872 863 fp = h5py.File(filename,'w')
873 864 #write metadata
874 865 self.writeMetadata(fp)
875 866 #Write data
876 867 grp = fp.create_group("Data")
877 868 # grp.attrs['metadata'] = self.metaFile
878 869
879 870 # grp.attrs['blocksPerFile'] = 0
880 871 ds = []
881 872 data = []
882 873 dsList = self.dsList
883 874 i = 0
884 875 while i < len(dsList):
885 876 dsInfo = dsList[i]
886 877 #One-dimension data
887 878 if dsInfo['mode'] == 0:
888 879 # ds0 = grp.create_dataset(self.dataList[i], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype='S20')
889 880 ds0 = grp.create_dataset(dsInfo['variable'], (1,1), maxshape=(1,self.blocksPerFile) , chunks = True, dtype=numpy.float64)
890 881 ds.append(ds0)
891 882 data.append([])
892 883 i += 1
893 884 continue
894 885 # nDimsForDs.append(nDims[i])
895 886
896 887 elif dsInfo['mode'] == 2:
897 888 grp0 = grp.create_group(dsInfo['variable'])
898 889 ds0 = grp0.create_dataset(dsInfo['dsName'], (1,dsInfo['shape']), data = numpy.zeros((1,dsInfo['shape'])) , maxshape=(None,dsInfo['shape']), chunks=True)
899 890 ds.append(ds0)
900 891 data.append([])
901 892 i += 1
902 893 continue
903 894
904 895 elif dsInfo['mode'] == 1:
905 896 grp0 = grp.create_group(dsInfo['variable'])
906 897
907 898 for j in range(dsInfo['dsNumber']):
908 899 dsInfo = dsList[i]
909 900 tableName = dsInfo['dsName']
910 901
911 902
912 903 if dsInfo['nDim'] == 3:
913 904 shape = dsInfo['shape'].astype(int)
914 905 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)
915 906 else:
916 907 shape = int(dsInfo['shape'])
917 908 ds0 = grp0.create_dataset(tableName, (1,shape), data = numpy.zeros((1,shape)) , maxshape=(None,shape), chunks=True)
918 909
919 910 ds.append(ds0)
920 911 data.append([])
921 912 i += 1
922 913 # nDimsForDs.append(nDims[i])
923 914
924 915 fp.flush()
925 916 fp.close()
926 917
927 918 # self.nDatas = nDatas
928 919 # self.nDims = nDims
929 920 # self.nDimsForDs = nDimsForDs
930 921 #Saving variables
931 922 print('Writing the file: %s'%filename)
932 923 self.filename = filename
933 924 # self.fp = fp
934 925 # self.grp = grp
935 926 # self.grp.attrs.modify('nRecords', 1)
936 927 self.ds = ds
937 928 self.data = data
938 929 # self.setFile = setFile
939 930 self.firsttime = True
940 931 self.blockIndex = 0
941 932 return
942 933
943 934 def putData(self):
944 935
945 936 if self.blockIndex == self.blocksPerFile or self.timeFlag():
946 937 self.setNextFile()
947 938
948 939 # if not self.firsttime:
949 940 self.readBlock()
950 941 self.setBlock() #Prepare data to be written
951 942 self.writeBlock() #Write data
952 943
953 944 return
954 945
955 946 def readBlock(self):
956 947
957 948 '''
958 949 data Array configured
959 950
960 951
961 952 self.data
962 953 '''
963 954 dsList = self.dsList
964 955 ds = self.ds
965 956 #Setting HDF5 File
966 957 fp = h5py.File(self.filename,'r+')
967 958 grp = fp["Data"]
968 959 ind = 0
969 960
970 961 # grp.attrs['blocksPerFile'] = 0
971 962 while ind < len(dsList):
972 963 dsInfo = dsList[ind]
973 964
974 965 if dsInfo['mode'] == 0:
975 966 ds0 = grp[dsInfo['variable']]
976 967 ds[ind] = ds0
977 968 ind += 1
978 969 else:
979 970
980 971 grp0 = grp[dsInfo['variable']]
981 972
982 973 for j in range(dsInfo['dsNumber']):
983 974 dsInfo = dsList[ind]
984 975 ds0 = grp0[dsInfo['dsName']]
985 976 ds[ind] = ds0
986 977 ind += 1
987 978
988 979 self.fp = fp
989 980 self.grp = grp
990 981 self.ds = ds
991 982
992 983 return
993 984
994 985 def setBlock(self):
995 986 '''
996 987 data Array configured
997 988
998 989
999 990 self.data
1000 991 '''
1001 992 #Creating Arrays
1002 993 dsList = self.dsList
1003 994 data = self.data
1004 995 ind = 0
1005 996
1006 997 while ind < len(dsList):
1007 998 dsInfo = dsList[ind]
1008 999 dataAux = getattr(self.dataOut, dsInfo['variable'])
1009 1000
1010 1001 mode = dsInfo['mode']
1011 1002 nDim = dsInfo['nDim']
1012 1003
1013 1004 if mode == 0 or mode == 2 or nDim == 1:
1014 1005 data[ind] = dataAux
1015 1006 ind += 1
1016 1007 # elif nDim == 1:
1017 1008 # data[ind] = numpy.reshape(dataAux,(numpy.size(dataAux),1))
1018 1009 # ind += 1
1019 1010 elif nDim == 2:
1020 1011 for j in range(dsInfo['dsNumber']):
1021 1012 data[ind] = dataAux[j,:]
1022 1013 ind += 1
1023 1014 elif nDim == 3:
1024 1015 for j in range(dsInfo['dsNumber']):
1025 1016 data[ind] = dataAux[:,j,:]
1026 1017 ind += 1
1027 1018
1028 1019 self.data = data
1029 1020 return
1030 1021
1031 1022 def writeBlock(self):
1032 1023 '''
1033 1024 Saves the block in the HDF5 file
1034 1025 '''
1035 1026 dsList = self.dsList
1036 1027
1037 1028 for i in range(len(self.ds)):
1038 1029 dsInfo = dsList[i]
1039 1030 nDim = dsInfo['nDim']
1040 1031 mode = dsInfo['mode']
1041 1032
1042 1033 # First time
1043 1034 if self.firsttime:
1044 1035 # self.ds[i].resize(self.data[i].shape)
1045 1036 # self.ds[i][self.blockIndex,:] = self.data[i]
1046 1037 if type(self.data[i]) == numpy.ndarray:
1047 1038
1048 1039 if nDim == 3:
1049 1040 self.data[i] = self.data[i].reshape((self.data[i].shape[0],self.data[i].shape[1],1))
1050 1041 self.ds[i].resize(self.data[i].shape)
1051 1042 if mode == 2:
1052 1043 self.ds[i].resize(self.data[i].shape)
1053 1044 self.ds[i][:] = self.data[i]
1054 1045 else:
1055 1046
1056 1047 # From second time
1057 1048 # Meteors!
1058 1049 if mode == 2:
1059 1050 dataShape = self.data[i].shape
1060 1051 dsShape = self.ds[i].shape
1061 1052 self.ds[i].resize((self.ds[i].shape[0] + dataShape[0],self.ds[i].shape[1]))
1062 1053 self.ds[i][dsShape[0]:,:] = self.data[i]
1063 1054 # No dimension
1064 1055 elif mode == 0:
1065 1056 self.ds[i].resize((self.ds[i].shape[0], self.ds[i].shape[1] + 1))
1066 1057 self.ds[i][0,-1] = self.data[i]
1067 1058 # One dimension
1068 1059 elif nDim == 1:
1069 1060 self.ds[i].resize((self.ds[i].shape[0] + 1, self.ds[i].shape[1]))
1070 1061 self.ds[i][-1,:] = self.data[i]
1071 1062 # Two dimension
1072 1063 elif nDim == 2:
1073 1064 self.ds[i].resize((self.ds[i].shape[0] + 1,self.ds[i].shape[1]))
1074 1065 self.ds[i][self.blockIndex,:] = self.data[i]
1075 1066 # Three dimensions
1076 1067 elif nDim == 3:
1077 1068 self.ds[i].resize((self.ds[i].shape[0],self.ds[i].shape[1],self.ds[i].shape[2]+1))
1078 1069 self.ds[i][:,:,-1] = self.data[i]
1079 1070
1080 1071 self.firsttime = False
1081 1072 self.blockIndex += 1
1082 1073
1083 1074 #Close to save changes
1084 1075 self.fp.flush()
1085 1076 self.fp.close()
1086 1077 return
1087 1078
1088 1079 def run(self, dataOut, path, blocksPerFile=10, metadataList=None, dataList=None, mode=None, **kwargs):
1089 1080
1090 1081 if not(self.isConfig):
1091 1082 flagdata = self.setup(dataOut, path=path, blocksPerFile=blocksPerFile,
1092 1083 metadataList=metadataList, dataList=dataList, mode=mode, **kwargs)
1093 1084
1094 1085 if not(flagdata):
1095 1086 return
1096 1087
1097 1088 self.isConfig = True
1098 # self.putMetadata()
1099 1089 self.setNextFile()
1100 1090
1101 1091 self.putData()
1102 1092 return
1103 1093 No newline at end of file
@@ -1,679 +1,678
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 schainpy.model.io.jroIO_base import LOCALTIME, JRODataReader, JRODataWriter
9 9 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
10 10 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
11 11 from schainpy.model.data.jrodata import Spectra
12 12 from schainpy.utils import log
13 13
14 14 @MPDecorator
15 15 class SpectraReader(JRODataReader, ProcessingUnit):
16 16 """
17 17 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
18 18 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
19 19 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
20 20
21 21 paresCanalesIguales * alturas * perfiles (Self Spectra)
22 22 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
23 23 canales * alturas (DC Channels)
24 24
25 25 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
26 26 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
27 27 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
28 28 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
29 29
30 30 Example:
31 31 dpath = "/home/myuser/data"
32 32
33 33 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
34 34
35 35 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
36 36
37 37 readerObj = SpectraReader()
38 38
39 39 readerObj.setup(dpath, startTime, endTime)
40 40
41 41 while(True):
42 42
43 43 readerObj.getData()
44 44
45 45 print readerObj.data_spc
46 46
47 47 print readerObj.data_cspc
48 48
49 49 print readerObj.data_dc
50 50
51 51 if readerObj.flagNoMoreFiles:
52 52 break
53 53
54 54 """
55 55
56 56 pts2read_SelfSpectra = 0
57 57
58 58 pts2read_CrossSpectra = 0
59 59
60 60 pts2read_DCchannels = 0
61 61
62 62 ext = ".pdata"
63 63
64 64 optchar = "P"
65 65
66 66 dataOut = None
67 67
68 68 nRdChannels = None
69 69
70 70 nRdPairs = None
71 71
72 72 rdPairList = []
73 73
74 74 def __init__(self):#, **kwargs):
75 75 """
76 76 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
77 77
78 78 Inputs:
79 79 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
80 80 almacenar un perfil de datos cada vez que se haga un requerimiento
81 81 (getData). El perfil sera obtenido a partir del buffer de datos,
82 82 si el buffer esta vacio se hara un nuevo proceso de lectura de un
83 83 bloque de datos.
84 84 Si este parametro no es pasado se creara uno internamente.
85 85
86 86 Affected:
87 87 self.dataOut
88 88
89 89 Return : None
90 90 """
91 91
92 92 #Eliminar de la base la herencia
93 93 ProcessingUnit.__init__(self)#, **kwargs)
94 94
95 95 # self.isConfig = False
96 96
97 97 self.pts2read_SelfSpectra = 0
98 98
99 99 self.pts2read_CrossSpectra = 0
100 100
101 101 self.pts2read_DCchannels = 0
102 102
103 103 self.datablock = None
104 104
105 105 self.utc = None
106 106
107 107 self.ext = ".pdata"
108 108
109 109 self.optchar = "P"
110 110
111 111 self.basicHeaderObj = BasicHeader(LOCALTIME)
112 112
113 113 self.systemHeaderObj = SystemHeader()
114 114
115 115 self.radarControllerHeaderObj = RadarControllerHeader()
116 116
117 117 self.processingHeaderObj = ProcessingHeader()
118 118
119 119 self.online = 0
120 120
121 121 self.fp = None
122 122
123 123 self.idFile = None
124 124
125 125 self.dtype = None
126 126
127 127 self.fileSizeByHeader = None
128 128
129 129 self.filenameList = []
130 130
131 131 self.filename = None
132 132
133 133 self.fileSize = None
134 134
135 135 self.firstHeaderSize = 0
136 136
137 137 self.basicHeaderSize = 24
138 138
139 139 self.pathList = []
140 140
141 141 self.lastUTTime = 0
142 142
143 143 self.maxTimeStep = 30
144 144
145 145 self.flagNoMoreFiles = 0
146 146
147 147 self.set = 0
148 148
149 149 self.path = None
150 150
151 151 self.delay = 60 #seconds
152 152
153 153 self.nTries = 3 #quantity tries
154 154
155 155 self.nFiles = 3 #number of files for searching
156 156
157 157 self.nReadBlocks = 0
158 158
159 159 self.flagIsNewFile = 1
160 160
161 161 self.__isFirstTimeOnline = 1
162 162
163 163 # self.ippSeconds = 0
164 164
165 165 self.flagDiscontinuousBlock = 0
166 166
167 167 self.flagIsNewBlock = 0
168 168
169 169 self.nTotalBlocks = 0
170 170
171 171 self.blocksize = 0
172 172
173 173 self.dataOut = self.createObjByDefault()
174 174
175 175 self.profileIndex = 1 #Always
176 176
177 177
178 178 def createObjByDefault(self):
179 179
180 180 dataObj = Spectra()
181 181
182 182 return dataObj
183 183
184 184 def __hasNotDataInBuffer(self):
185 185 return 1
186 186
187 187
188 188 def getBlockDimension(self):
189 189 """
190 190 Obtiene la cantidad de puntos a leer por cada bloque de datos
191 191
192 192 Affected:
193 193 self.nRdChannels
194 194 self.nRdPairs
195 195 self.pts2read_SelfSpectra
196 196 self.pts2read_CrossSpectra
197 197 self.pts2read_DCchannels
198 198 self.blocksize
199 199 self.dataOut.nChannels
200 200 self.dataOut.nPairs
201 201
202 202 Return:
203 203 None
204 204 """
205 205 self.nRdChannels = 0
206 206 self.nRdPairs = 0
207 207 self.rdPairList = []
208 208
209 209 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
210 210 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
211 211 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
212 212 else:
213 213 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
214 214 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
215 215
216 216 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
217 217
218 218 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
219 219 self.blocksize = self.pts2read_SelfSpectra
220 220
221 221 if self.processingHeaderObj.flag_cspc:
222 222 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
223 223 self.blocksize += self.pts2read_CrossSpectra
224 224
225 225 if self.processingHeaderObj.flag_dc:
226 226 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
227 227 self.blocksize += self.pts2read_DCchannels
228 228
229 229 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
230 230
231 231
232 232 def readBlock(self):
233 233 """
234 234 Lee el bloque de datos desde la posicion actual del puntero del archivo
235 235 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
236 236 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
237 237 es seteado a 0
238 238
239 239 Return: None
240 240
241 241 Variables afectadas:
242 242
243 243 self.flagIsNewFile
244 244 self.flagIsNewBlock
245 245 self.nTotalBlocks
246 246 self.data_spc
247 247 self.data_cspc
248 248 self.data_dc
249 249
250 250 Exceptions:
251 251 Si un bloque leido no es un bloque valido
252 252 """
253 253 blockOk_flag = False
254 254 fpointer = self.fp.tell()
255 255
256 256 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
257 257 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
258 258
259 259 if self.processingHeaderObj.flag_cspc:
260 260 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
261 261 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
262 262
263 263 if self.processingHeaderObj.flag_dc:
264 264 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
265 265 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
266 266
267 267
268 268 if not self.processingHeaderObj.shif_fft:
269 269 #desplaza a la derecha en el eje 2 determinadas posiciones
270 270 shift = int(self.processingHeaderObj.profilesPerBlock/2)
271 271 spc = numpy.roll( spc, shift , axis=2 )
272 272
273 273 if self.processingHeaderObj.flag_cspc:
274 274 #desplaza a la derecha en el eje 2 determinadas posiciones
275 275 cspc = numpy.roll( cspc, shift, axis=2 )
276 276
277 277 #Dimensions : nChannels, nProfiles, nSamples
278 278 spc = numpy.transpose( spc, (0,2,1) )
279 279 self.data_spc = spc
280 280
281 281 if self.processingHeaderObj.flag_cspc:
282 282 cspc = numpy.transpose( cspc, (0,2,1) )
283 283 self.data_cspc = cspc['real'] + cspc['imag']*1j
284 284 else:
285 285 self.data_cspc = None
286 286
287 287 if self.processingHeaderObj.flag_dc:
288 288 self.data_dc = dc['real'] + dc['imag']*1j
289 289 else:
290 290 self.data_dc = None
291 291
292 292 self.flagIsNewFile = 0
293 293 self.flagIsNewBlock = 1
294 294
295 295 self.nTotalBlocks += 1
296 296 self.nReadBlocks += 1
297 297
298 298 return 1
299 299
300 300 def getFirstHeader(self):
301 301
302 302 self.getBasicHeader()
303 303
304 304 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
305 305
306 306 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
307 307
308 308 # self.dataOut.ippSeconds = self.ippSeconds
309 309
310 310 # self.dataOut.timeInterval = self.radarControllerHeaderObj.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.processingHeaderObj.profilesPerBlock
311 311
312 312 self.dataOut.dtype = self.dtype
313 313
314 314 # self.dataOut.nPairs = self.nPairs
315 315
316 316 self.dataOut.pairsList = self.rdPairList
317 317
318 318 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
319 319
320 320 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
321 321
322 322 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
323 323
324 324 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
325 325
326 326 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
327 327
328 328 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
329 329
330 330 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
331 331
332 332 self.dataOut.flagShiftFFT = True #Data is always shifted
333 333
334 334 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode #asumo q la data no esta decodificada
335 335
336 336 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip #asumo q la data esta sin flip
337 337
338 338 def getData(self):
339 339 """
340 340 First method to execute before "RUN" is called.
341 341
342 342 Copia el buffer de lectura a la clase "Spectra",
343 343 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
344 344 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
345 345
346 346 Return:
347 347 0 : Si no hay mas archivos disponibles
348 348 1 : Si hizo una buena copia del buffer
349 349
350 350 Affected:
351 351 self.dataOut
352 352
353 353 self.flagDiscontinuousBlock
354 354 self.flagIsNewBlock
355 355 """
356 356
357 357 if self.flagNoMoreFiles:
358 358 self.dataOut.flagNoData = True
359 359 print('Process finished')
360 360 return 0
361 361
362 362 self.flagDiscontinuousBlock = 0
363 363 self.flagIsNewBlock = 0
364 364
365 365 if self.__hasNotDataInBuffer():
366 366
367 367 if not( self.readNextBlock() ):
368 368 self.dataOut.flagNoData = True
369 369 return 0
370 370
371 371 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
372 372
373 373 if self.data_spc is None:
374 374 self.dataOut.flagNoData = True
375 375 return 0
376 376
377 377 self.getBasicHeader()
378 378
379 379 self.getFirstHeader()
380 380
381 381 self.dataOut.data_spc = self.data_spc
382 382
383 383 self.dataOut.data_cspc = self.data_cspc
384 384
385 385 self.dataOut.data_dc = self.data_dc
386 386
387 387 self.dataOut.flagNoData = False
388 388
389 389 self.dataOut.realtime = self.online
390 390
391 391 return self.dataOut.data_spc
392 392 @MPDecorator
393 393 class SpectraWriter(JRODataWriter, Operation):
394 394
395 395 """
396 396 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
397 397 de los datos siempre se realiza por bloques.
398 398 """
399 399
400 400 ext = ".pdata"
401 401
402 402 optchar = "P"
403 403
404 404 shape_spc_Buffer = None
405 405
406 406 shape_cspc_Buffer = None
407 407
408 408 shape_dc_Buffer = None
409 409
410 410 data_spc = None
411 411
412 412 data_cspc = None
413 413
414 414 data_dc = None
415 415
416 # dataOut = None
417
418 def __init__(self):#, **kwargs):
416 def __init__(self):
419 417 """
420 418 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
421 419
422 420 Affected:
423 421 self.dataOut
424 422 self.basicHeaderObj
425 423 self.systemHeaderObj
426 424 self.radarControllerHeaderObj
427 425 self.processingHeaderObj
428 426
429 427 Return: None
430 428 """
431 429
432 Operation.__init__(self)#, **kwargs)
433
434 #self.isConfig = False
430 Operation.__init__(self)
435 431
436 432 self.nTotalBlocks = 0
437 433
438 434 self.data_spc = None
439 435
440 436 self.data_cspc = None
441 437
442 438 self.data_dc = None
443 439
444 440 self.fp = None
445 441
446 442 self.flagIsNewFile = 1
447 443
448 444 self.nTotalBlocks = 0
449 445
450 446 self.flagIsNewBlock = 0
451 447
452 448 self.setFile = None
453 449
454 450 self.dtype = None
455 451
456 452 self.path = None
457 453
458 454 self.noMoreFiles = 0
459 455
460 456 self.filename = None
461 457
462 458 self.basicHeaderObj = BasicHeader(LOCALTIME)
463 459
464 460 self.systemHeaderObj = SystemHeader()
465 461
466 462 self.radarControllerHeaderObj = RadarControllerHeader()
467 463
468 464 self.processingHeaderObj = ProcessingHeader()
469 465
470 466
471 467 def hasAllDataInBuffer(self):
472 468 return 1
473 469
474 470
475 471 def setBlockDimension(self):
476 472 """
477 473 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
478 474
479 475 Affected:
480 476 self.shape_spc_Buffer
481 477 self.shape_cspc_Buffer
482 478 self.shape_dc_Buffer
483 479
484 480 Return: None
485 481 """
486 482 self.shape_spc_Buffer = (self.dataOut.nChannels,
487 483 self.processingHeaderObj.nHeights,
488 484 self.processingHeaderObj.profilesPerBlock)
489 485
490 486 self.shape_cspc_Buffer = (self.dataOut.nPairs,
491 487 self.processingHeaderObj.nHeights,
492 488 self.processingHeaderObj.profilesPerBlock)
493 489
494 490 self.shape_dc_Buffer = (self.dataOut.nChannels,
495 491 self.processingHeaderObj.nHeights)
496 492
497 493
498 494 def writeBlock(self):
499 """
495 """processingHeaderObj
500 496 Escribe el buffer en el file designado
501 497
502 498 Affected:
503 499 self.data_spc
504 500 self.data_cspc
505 501 self.data_dc
506 502 self.flagIsNewFile
507 503 self.flagIsNewBlock
508 504 self.nTotalBlocks
509 505 self.nWriteBlocks
510 506
511 507 Return: None
512 508 """
513 509
514 510 spc = numpy.transpose( self.data_spc, (0,2,1) )
515 511 if not self.processingHeaderObj.shif_fft:
516 512 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
517 513 data = spc.reshape((-1))
518 514 data = data.astype(self.dtype[0])
519 515 data.tofile(self.fp)
520 516
521 517 if self.data_cspc is not None:
522 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
518
523 519 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
520 #data = numpy.zeros( numpy.shape(cspc), self.dtype )
521 #print 'data.shape', self.shape_cspc_Buffer
524 522 if not self.processingHeaderObj.shif_fft:
525 523 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
526 524 data['real'] = cspc.real
527 525 data['imag'] = cspc.imag
528 526 data = data.reshape((-1))
529 527 data.tofile(self.fp)
530 528
531 529 if self.data_dc is not None:
532 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
530
533 531 dc = self.data_dc
532 data = numpy.zeros( numpy.shape(dc), self.dtype )
534 533 data['real'] = dc.real
535 534 data['imag'] = dc.imag
536 535 data = data.reshape((-1))
537 536 data.tofile(self.fp)
538 537
539 538 # self.data_spc.fill(0)
540 539 #
541 540 # if self.data_dc is not None:
542 541 # self.data_dc.fill(0)
543 542 #
544 543 # if self.data_cspc is not None:
545 544 # self.data_cspc.fill(0)
546 545
547 546 self.flagIsNewFile = 0
548 547 self.flagIsNewBlock = 1
549 548 self.nTotalBlocks += 1
550 549 self.nWriteBlocks += 1
551 550 self.blockIndex += 1
552 551
553 552 # print "[Writing] Block = %d04" %self.blockIndex
554 553
555 554 def putData(self):
556 555 """
557 556 Setea un bloque de datos y luego los escribe en un file
558 557
559 558 Affected:
560 559 self.data_spc
561 560 self.data_cspc
562 561 self.data_dc
563 562
564 563 Return:
565 564 0 : Si no hay data o no hay mas files que puedan escribirse
566 565 1 : Si se escribio la data de un bloque en un file
567 566 """
568 567
569 568 if self.dataOut.flagNoData:
570 569 return 0
571 570
572 571 self.flagIsNewBlock = 0
573 572
574 573 if self.dataOut.flagDiscontinuousBlock:
575 574 self.data_spc.fill(0)
576 575 if self.dataOut.data_cspc is not None:
577 576 self.data_cspc.fill(0)
578 577 if self.dataOut.data_dc is not None:
579 578 self.data_dc.fill(0)
580 579 self.setNextFile()
581 580
582 581 if self.flagIsNewFile == 0:
583 582 self.setBasicHeader()
584 583
585 584 self.data_spc = self.dataOut.data_spc.copy()
586 585
587 586 if self.dataOut.data_cspc is not None:
588 587 self.data_cspc = self.dataOut.data_cspc.copy()
589 588
590 589 if self.dataOut.data_dc is not None:
591 590 self.data_dc = self.dataOut.data_dc.copy()
592 591
593 592 # #self.processingHeaderObj.dataBlocksPerFile)
594 593 if self.hasAllDataInBuffer():
595 594 # self.setFirstHeader()
596 595 self.writeNextBlock()
597 596
598 597 def __getBlockSize(self):
599 598 '''
600 599 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
601 600 '''
602 601
603 602 dtype_width = self.getDtypeWidth()
604 603
605 604 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
606 605
607 606 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
608 607 blocksize = (pts2write_SelfSpectra*dtype_width)
609 608
610 609 if self.dataOut.data_cspc is not None:
611 610 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
612 611 blocksize += (pts2write_CrossSpectra*dtype_width*2)
613 612
614 613 if self.dataOut.data_dc is not None:
615 614 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
616 615 blocksize += (pts2write_DCchannels*dtype_width*2)
617 616
618 617 # blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
619 618
620 619 return blocksize
621 620
622 621 def setFirstHeader(self):
623 622
624 623 """
625 624 Obtiene una copia del First Header
626 625
627 626 Affected:
628 627 self.systemHeaderObj
629 628 self.radarControllerHeaderObj
630 629 self.dtype
631 630
632 631 Return:
633 632 None
634 633 """
635 634
636 635 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
637 636 self.systemHeaderObj.nChannels = self.dataOut.nChannels
638 637 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
639 638
640 639 self.processingHeaderObj.dtype = 1 # Spectra
641 640 self.processingHeaderObj.blockSize = self.__getBlockSize()
642 641 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
643 642 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
644 643 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
645 644 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
646 645 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
647 646 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
648 647 self.processingHeaderObj.shif_fft = self.dataOut.flagShiftFFT
649 648
650 649 if self.processingHeaderObj.totalSpectra > 0:
651 650 channelList = []
652 651 for channel in range(self.dataOut.nChannels):
653 652 channelList.append(channel)
654 653 channelList.append(channel)
655 654
656 655 pairsList = []
657 656 if self.dataOut.nPairs > 0:
658 657 for pair in self.dataOut.pairsList:
659 658 pairsList.append(pair[0])
660 659 pairsList.append(pair[1])
661 660
662 661 spectraComb = channelList + pairsList
663 662 spectraComb = numpy.array(spectraComb, dtype="u1")
664 663 self.processingHeaderObj.spectraComb = spectraComb
665 664
666 665 if self.dataOut.code is not None:
667 666 self.processingHeaderObj.code = self.dataOut.code
668 667 self.processingHeaderObj.nCode = self.dataOut.nCode
669 668 self.processingHeaderObj.nBaud = self.dataOut.nBaud
670 669
671 670 if self.processingHeaderObj.nWindows != 0:
672 671 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
673 672 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
674 673 self.processingHeaderObj.nHeights = self.dataOut.nHeights
675 674 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
676 675
677 676 self.processingHeaderObj.processFlags = self.getProcessFlags()
678 677
679 678 self.setBasicHeader() No newline at end of file
1 NO CONTENT: modified file chmod 100644 => 100755
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: file was removed
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