##// END OF EJS Templates
Python 2to3, Voltage (all operations) working
George Yong -
r1168:889c94859eae
parent child
Show More
@@ -1,907 +1,906
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $
5 5 '''
6 6 import sys
7 7 import numpy
8 8 import copy
9 9 import datetime
10 10 import inspect
11 11 from schainpy.utils import log
12 12
13 13 SPEED_OF_LIGHT = 299792458
14 14 SPEED_OF_LIGHT = 3e8
15 15
16 16 BASIC_STRUCTURE = numpy.dtype([
17 17 ('nSize', '<u4'),
18 18 ('nVersion', '<u2'),
19 19 ('nDataBlockId', '<u4'),
20 20 ('nUtime', '<u4'),
21 21 ('nMilsec', '<u2'),
22 22 ('nTimezone', '<i2'),
23 23 ('nDstflag', '<i2'),
24 24 ('nErrorCount', '<u4')
25 25 ])
26 26
27 27 SYSTEM_STRUCTURE = numpy.dtype([
28 28 ('nSize', '<u4'),
29 29 ('nNumSamples', '<u4'),
30 30 ('nNumProfiles', '<u4'),
31 31 ('nNumChannels', '<u4'),
32 32 ('nADCResolution', '<u4'),
33 33 ('nPCDIOBusWidth', '<u4'),
34 34 ])
35 35
36 36 RADAR_STRUCTURE = numpy.dtype([
37 37 ('nSize', '<u4'),
38 38 ('nExpType', '<u4'),
39 39 ('nNTx', '<u4'),
40 40 ('fIpp', '<f4'),
41 41 ('fTxA', '<f4'),
42 42 ('fTxB', '<f4'),
43 43 ('nNumWindows', '<u4'),
44 44 ('nNumTaus', '<u4'),
45 45 ('nCodeType', '<u4'),
46 46 ('nLine6Function', '<u4'),
47 47 ('nLine5Function', '<u4'),
48 48 ('fClock', '<f4'),
49 49 ('nPrePulseBefore', '<u4'),
50 50 ('nPrePulseAfter', '<u4'),
51 51 ('sRangeIPP', '<a20'),
52 52 ('sRangeTxA', '<a20'),
53 53 ('sRangeTxB', '<a20'),
54 54 ])
55 55
56 56 SAMPLING_STRUCTURE = numpy.dtype(
57 57 [('h0', '<f4'), ('dh', '<f4'), ('nsa', '<u4')])
58 58
59 59
60 60 PROCESSING_STRUCTURE = numpy.dtype([
61 61 ('nSize', '<u4'),
62 62 ('nDataType', '<u4'),
63 63 ('nSizeOfDataBlock', '<u4'),
64 64 ('nProfilesperBlock', '<u4'),
65 65 ('nDataBlocksperFile', '<u4'),
66 66 ('nNumWindows', '<u4'),
67 67 ('nProcessFlags', '<u4'),
68 68 ('nCoherentIntegrations', '<u4'),
69 69 ('nIncoherentIntegrations', '<u4'),
70 70 ('nTotalSpectra', '<u4')
71 71 ])
72 72
73 73
74 74 class Header(object):
75 75
76 76 def __init__(self):
77 77 raise NotImplementedError
78 78
79 79 def copy(self):
80 80 return copy.deepcopy(self)
81 81
82 82 def read(self):
83 83
84 84 raise NotImplementedError
85 85
86 86 def write(self):
87 87
88 88 raise NotImplementedError
89 89
90 90 def getAllowedArgs(self):
91 91 args = inspect.getargspec(self.__init__).args
92 92 try:
93 93 args.remove('self')
94 94 except:
95 95 pass
96 96 return args
97 97
98 98 def getAsDict(self):
99 99 args = self.getAllowedArgs()
100 100 asDict = {}
101 101 for x in args:
102 102 asDict[x] = self[x]
103 103 return asDict
104 104
105 105 def __getitem__(self, name):
106 106 return getattr(self, name)
107 107
108 108 def printInfo(self):
109 109
110 110 message = "#" * 50 + "\n"
111 111 message += self.__class__.__name__.upper() + "\n"
112 112 message += "#" * 50 + "\n"
113 113
114 114 keyList = list(self.__dict__.keys())
115 115 keyList.sort()
116 116
117 117 for key in keyList:
118 118 message += "%s = %s" % (key, self.__dict__[key]) + "\n"
119 119
120 120 if "size" not in keyList:
121 121 attr = getattr(self, "size")
122 122
123 123 if attr:
124 124 message += "%s = %s" % ("size", attr) + "\n"
125 125
126 126 print(message)
127 127
128 128
129 129 class BasicHeader(Header):
130 130
131 131 size = None
132 132 version = None
133 133 dataBlock = None
134 134 utc = None
135 135 ltc = None
136 136 miliSecond = None
137 137 timeZone = None
138 138 dstFlag = None
139 139 errorCount = None
140 140 datatime = None
141 141 structure = BASIC_STRUCTURE
142 142 __LOCALTIME = None
143 143
144 144 def __init__(self, useLocalTime=True):
145 145
146 146 self.size = 24
147 147 self.version = 0
148 148 self.dataBlock = 0
149 149 self.utc = 0
150 150 self.miliSecond = 0
151 151 self.timeZone = 0
152 152 self.dstFlag = 0
153 153 self.errorCount = 0
154 154
155 155 self.useLocalTime = useLocalTime
156 156
157 157 def read(self, fp):
158 158
159 159 self.length = 0
160 160 try:
161 161 if hasattr(fp, 'read'):
162 162 header = numpy.fromfile(fp, BASIC_STRUCTURE, 1)
163 163 else:
164 164 header = numpy.fromstring(fp, BASIC_STRUCTURE, 1)
165 165 except Exception as e:
166 166 print("BasicHeader: ")
167 167 print(e)
168 168 return 0
169 169
170 170 self.size = int(header['nSize'][0])
171 171 self.version = int(header['nVersion'][0])
172 172 self.dataBlock = int(header['nDataBlockId'][0])
173 173 self.utc = int(header['nUtime'][0])
174 174 self.miliSecond = int(header['nMilsec'][0])
175 175 self.timeZone = int(header['nTimezone'][0])
176 176 self.dstFlag = int(header['nDstflag'][0])
177 177 self.errorCount = int(header['nErrorCount'][0])
178 178
179 179 if self.size < 24:
180 180 return 0
181 181
182 182 self.length = header.nbytes
183 183 return 1
184 184
185 185 def write(self, fp):
186 186
187 187 headerTuple = (self.size, self.version, self.dataBlock, self.utc,
188 188 self.miliSecond, self.timeZone, self.dstFlag, self.errorCount)
189 189 header = numpy.array(headerTuple, BASIC_STRUCTURE)
190 190 header.tofile(fp)
191 191
192 192 return 1
193 193
194 194 def get_ltc(self):
195 195
196 196 return self.utc - self.timeZone * 60
197 197
198 198 def set_ltc(self, value):
199 199
200 200 self.utc = value + self.timeZone * 60
201 201
202 202 def get_datatime(self):
203 203
204 204 return datetime.datetime.utcfromtimestamp(self.ltc)
205 205
206 206 ltc = property(get_ltc, set_ltc)
207 207 datatime = property(get_datatime)
208 208
209 209
210 210 class SystemHeader(Header):
211 211
212 212 size = None
213 213 nSamples = None
214 214 nProfiles = None
215 215 nChannels = None
216 216 adcResolution = None
217 217 pciDioBusWidth = None
218 218 structure = SYSTEM_STRUCTURE
219 219
220 220 def __init__(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWidth=0):
221 221
222 222 self.size = 24
223 223 self.nSamples = nSamples
224 224 self.nProfiles = nProfiles
225 225 self.nChannels = nChannels
226 226 self.adcResolution = adcResolution
227 227 self.pciDioBusWidth = pciDioBusWidth
228 228
229 229 def read(self, fp):
230 230 self.length = 0
231 231 try:
232 232 startFp = fp.tell()
233 233 except Exception as e:
234 234 startFp = None
235 235 pass
236 236
237 237 try:
238 238 if hasattr(fp, 'read'):
239 239 header = numpy.fromfile(fp, SYSTEM_STRUCTURE, 1)
240 240 else:
241 241 header = numpy.fromstring(fp, SYSTEM_STRUCTURE, 1)
242 242 except Exception as e:
243 243 print("System Header: " + str(e))
244 244 return 0
245 245
246 246 self.size = header['nSize'][0]
247 247 self.nSamples = header['nNumSamples'][0]
248 248 self.nProfiles = header['nNumProfiles'][0]
249 249 self.nChannels = header['nNumChannels'][0]
250 250 self.adcResolution = header['nADCResolution'][0]
251 251 self.pciDioBusWidth = header['nPCDIOBusWidth'][0]
252 252
253 253 if startFp is not None:
254 254 endFp = self.size + startFp
255 255
256 256 if fp.tell() > endFp:
257 257 sys.stderr.write(
258 258 "Warning %s: Size value read from System Header is lower than it has to be\n" % fp.name)
259 259 return 0
260 260
261 261 if fp.tell() < endFp:
262 262 sys.stderr.write(
263 263 "Warning %s: Size value read from System Header size is greater than it has to be\n" % fp.name)
264 264 return 0
265 265
266 266 self.length = header.nbytes
267 267 return 1
268 268
269 269 def write(self, fp):
270 270
271 271 headerTuple = (self.size, self.nSamples, self.nProfiles,
272 272 self.nChannels, self.adcResolution, self.pciDioBusWidth)
273 273 header = numpy.array(headerTuple, SYSTEM_STRUCTURE)
274 274 header.tofile(fp)
275 275
276 276 return 1
277 277
278 278
279 279 class RadarControllerHeader(Header):
280 280
281 281 expType = None
282 282 nTx = None
283 283 ipp = None
284 284 txA = None
285 285 txB = None
286 286 nWindows = None
287 287 numTaus = None
288 288 codeType = None
289 289 line6Function = None
290 290 line5Function = None
291 291 fClock = None
292 292 prePulseBefore = None
293 293 prePulseAfter = None
294 294 rangeIpp = None
295 295 rangeTxA = None
296 296 rangeTxB = None
297 297 structure = RADAR_STRUCTURE
298 298 __size = None
299 299
300 300 def __init__(self, expType=2, nTx=1,
301 301 ipp=None, txA=0, txB=0,
302 302 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
303 303 numTaus=0, line6Function=0, line5Function=0, fClock=None,
304 304 prePulseBefore=0, prePulseAfter=0,
305 305 codeType=0, nCode=0, nBaud=0, code=None,
306 306 flip1=0, flip2=0):
307 307
308 308 # self.size = 116
309 309 self.expType = expType
310 310 self.nTx = nTx
311 311 self.ipp = ipp
312 312 self.txA = txA
313 313 self.txB = txB
314 314 self.rangeIpp = ipp
315 315 self.rangeTxA = txA
316 316 self.rangeTxB = txB
317 317
318 318 self.nWindows = nWindows
319 319 self.numTaus = numTaus
320 320 self.codeType = codeType
321 321 self.line6Function = line6Function
322 322 self.line5Function = line5Function
323 323 self.fClock = fClock
324 324 self.prePulseBefore = prePulseBefore
325 325 self.prePulseAfter = prePulseAfter
326 326
327 327 self.nHeights = nHeights
328 328 self.firstHeight = firstHeight
329 329 self.deltaHeight = deltaHeight
330 330 self.samplesWin = nHeights
331 331
332 332 self.nCode = nCode
333 333 self.nBaud = nBaud
334 334 self.code = code
335 335 self.flip1 = flip1
336 336 self.flip2 = flip2
337 337
338 338 self.code_size = int(numpy.ceil(self.nBaud / 32.)) * self.nCode * 4
339 339 # self.dynamic = numpy.array([],numpy.dtype('byte'))
340 340
341 341 if self.fClock is None and self.deltaHeight is not None:
342 342 self.fClock = 0.15 / (deltaHeight * 1e-6) # 0.15Km / (height * 1u)
343 343
344 344 def read(self, fp):
345 345 self.length = 0
346 346 try:
347 347 startFp = fp.tell()
348 348 except Exception as e:
349 349 startFp = None
350 350 pass
351 351
352 352 try:
353 353 if hasattr(fp, 'read'):
354 354 header = numpy.fromfile(fp, RADAR_STRUCTURE, 1)
355 355 else:
356 356 header = numpy.fromstring(fp, RADAR_STRUCTURE, 1)
357 357 self.length += header.nbytes
358 358 except Exception as e:
359 359 print("RadarControllerHeader: " + str(e))
360 360 return 0
361 361
362 362 size = int(header['nSize'][0])
363 363 self.expType = int(header['nExpType'][0])
364 364 self.nTx = int(header['nNTx'][0])
365 365 self.ipp = float(header['fIpp'][0])
366 366 self.txA = float(header['fTxA'][0])
367 367 self.txB = float(header['fTxB'][0])
368 368 self.nWindows = int(header['nNumWindows'][0])
369 369 self.numTaus = int(header['nNumTaus'][0])
370 370 self.codeType = int(header['nCodeType'][0])
371 371 self.line6Function = int(header['nLine6Function'][0])
372 372 self.line5Function = int(header['nLine5Function'][0])
373 373 self.fClock = float(header['fClock'][0])
374 374 self.prePulseBefore = int(header['nPrePulseBefore'][0])
375 375 self.prePulseAfter = int(header['nPrePulseAfter'][0])
376 376 self.rangeIpp = header['sRangeIPP'][0]
377 377 self.rangeTxA = header['sRangeTxA'][0]
378 378 self.rangeTxB = header['sRangeTxB'][0]
379 379
380 380 try:
381 381 if hasattr(fp, 'read'):
382 382 samplingWindow = numpy.fromfile(
383 383 fp, SAMPLING_STRUCTURE, self.nWindows)
384 384 else:
385 385 samplingWindow = numpy.fromstring(
386 386 fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
387 387 self.length += samplingWindow.nbytes
388 388 except Exception as e:
389 389 print("RadarControllerHeader: " + str(e))
390 390 return 0
391 391 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
392 392 self.firstHeight = samplingWindow['h0']
393 393 self.deltaHeight = samplingWindow['dh']
394 394 self.samplesWin = samplingWindow['nsa']
395 395
396 396 try:
397 397 if hasattr(fp, 'read'):
398 398 self.Taus = numpy.fromfile(fp, '<f4', self.numTaus)
399 399 else:
400 400 self.Taus = numpy.fromstring(
401 401 fp[self.length:], '<f4', self.numTaus)
402 402 self.length += self.Taus.nbytes
403 403 except Exception as e:
404 404 print("RadarControllerHeader: " + str(e))
405 405 return 0
406 406
407 407 self.code_size = 0
408 408 if self.codeType != 0:
409 409
410 410 try:
411 411 if hasattr(fp, 'read'):
412 412 self.nCode = numpy.fromfile(fp, '<u4', 1)[0]
413 413 self.length += self.nCode.nbytes
414 414 self.nBaud = numpy.fromfile(fp, '<u4', 1)[0]
415 415 self.length += self.nBaud.nbytes
416 416 else:
417 417 self.nCode = numpy.fromstring(
418 418 fp[self.length:], '<u4', 1)[0]
419 419 self.length += self.nCode.nbytes
420 420 self.nBaud = numpy.fromstring(
421 421 fp[self.length:], '<u4', 1)[0]
422 422 self.length += self.nBaud.nbytes
423 423 except Exception as e:
424 424 print("RadarControllerHeader: " + str(e))
425 425 return 0
426 426 code = numpy.empty([self.nCode, self.nBaud], dtype='i1')
427 427
428 428 for ic in range(self.nCode):
429 429 try:
430 430 if hasattr(fp, 'read'):
431 431 temp = numpy.fromfile(fp, 'u4', int(
432 432 numpy.ceil(self.nBaud / 32.)))
433 433 else:
434 434 temp = numpy.fromstring(
435 435 fp, 'u4', int(numpy.ceil(self.nBaud / 32.)))
436 436 self.length += temp.nbytes
437 437 except Exception as e:
438 438 print("RadarControllerHeader: " + str(e))
439 439 return 0
440 440
441 441 for ib in range(self.nBaud - 1, -1, -1):
442 log.error(ib / 32)
443 442 code[ic, ib] = temp[int(ib / 32)] % 2
444 443 temp[int(ib / 32)] = temp[int(ib / 32)] / 2
445 444
446 445 self.code = 2.0 * code - 1.0
447 446 self.code_size = int(numpy.ceil(self.nBaud / 32.)) * self.nCode * 4
448 447
449 448 # if self.line5Function == RCfunction.FLIP:
450 449 # self.flip1 = numpy.fromfile(fp,'<u4',1)
451 450 #
452 451 # if self.line6Function == RCfunction.FLIP:
453 452 # self.flip2 = numpy.fromfile(fp,'<u4',1)
454 453 if startFp is not None:
455 454 endFp = size + startFp
456 455
457 456 if fp.tell() != endFp:
458 457 # fp.seek(endFp)
459 458 print("%s: Radar Controller Header size is not consistent: from data [%d] != from header field [%d]" % (fp.name, fp.tell() - startFp, size))
460 459 # return 0
461 460
462 461 if fp.tell() > endFp:
463 462 sys.stderr.write(
464 463 "Warning %s: Size value read from Radar Controller header is lower than it has to be\n" % fp.name)
465 464 # return 0
466 465
467 466 if fp.tell() < endFp:
468 467 sys.stderr.write(
469 468 "Warning %s: Size value read from Radar Controller header is greater than it has to be\n" % fp.name)
470 469
471 470 return 1
472 471
473 472 def write(self, fp):
474 473
475 474 headerTuple = (self.size,
476 475 self.expType,
477 476 self.nTx,
478 477 self.ipp,
479 478 self.txA,
480 479 self.txB,
481 480 self.nWindows,
482 481 self.numTaus,
483 482 self.codeType,
484 483 self.line6Function,
485 484 self.line5Function,
486 485 self.fClock,
487 486 self.prePulseBefore,
488 487 self.prePulseAfter,
489 488 self.rangeIpp,
490 489 self.rangeTxA,
491 490 self.rangeTxB)
492 491
493 492 header = numpy.array(headerTuple, RADAR_STRUCTURE)
494 493 header.tofile(fp)
495 494
496 495 sampleWindowTuple = (
497 496 self.firstHeight, self.deltaHeight, self.samplesWin)
498 497 samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE)
499 498 samplingWindow.tofile(fp)
500 499
501 500 if self.numTaus > 0:
502 501 self.Taus.tofile(fp)
503 502
504 503 if self.codeType != 0:
505 504 nCode = numpy.array(self.nCode, '<u4')
506 505 nCode.tofile(fp)
507 506 nBaud = numpy.array(self.nBaud, '<u4')
508 507 nBaud.tofile(fp)
509 508 code1 = (self.code + 1.0) / 2.
510 509
511 510 for ic in range(self.nCode):
512 511 tempx = numpy.zeros(int(numpy.ceil(self.nBaud / 32.)))
513 512 start = 0
514 513 end = 32
515 514 for i in range(len(tempx)):
516 515 code_selected = code1[ic, start:end]
517 516 for j in range(len(code_selected) - 1, -1, -1):
518 517 if code_selected[j] == 1:
519 518 tempx[i] = tempx[i] + \
520 519 2**(len(code_selected) - 1 - j)
521 520 start = start + 32
522 521 end = end + 32
523 522
524 523 tempx = tempx.astype('u4')
525 524 tempx.tofile(fp)
526 525
527 526 # if self.line5Function == RCfunction.FLIP:
528 527 # self.flip1.tofile(fp)
529 528 #
530 529 # if self.line6Function == RCfunction.FLIP:
531 530 # self.flip2.tofile(fp)
532 531
533 532 return 1
534 533
535 534 def get_ippSeconds(self):
536 535 '''
537 536 '''
538 537 ippSeconds = 2.0 * 1000 * self.ipp / SPEED_OF_LIGHT
539 538
540 539 return ippSeconds
541 540
542 541 def set_ippSeconds(self, ippSeconds):
543 542 '''
544 543 '''
545 544
546 545 self.ipp = ippSeconds * SPEED_OF_LIGHT / (2.0 * 1000)
547 546
548 547 return
549 548
550 549 def get_size(self):
551 550
552 551 self.__size = 116 + 12 * self.nWindows + 4 * self.numTaus
553 552
554 553 if self.codeType != 0:
555 554 self.__size += 4 + 4 + 4 * self.nCode * \
556 555 numpy.ceil(self.nBaud / 32.)
557 556
558 557 return self.__size
559 558
560 559 def set_size(self, value):
561 560
562 561 raise IOError("size is a property and it cannot be set, just read")
563 562
564 563 return
565 564
566 565 ippSeconds = property(get_ippSeconds, set_ippSeconds)
567 566 size = property(get_size, set_size)
568 567
569 568
570 569 class ProcessingHeader(Header):
571 570
572 571 # size = None
573 572 dtype = None
574 573 blockSize = None
575 574 profilesPerBlock = None
576 575 dataBlocksPerFile = None
577 576 nWindows = None
578 577 processFlags = None
579 578 nCohInt = None
580 579 nIncohInt = None
581 580 totalSpectra = None
582 581 structure = PROCESSING_STRUCTURE
583 582 flag_dc = None
584 583 flag_cspc = None
585 584
586 585 def __init__(self, dtype=0, blockSize=0, profilesPerBlock=0, dataBlocksPerFile=0, nWindows=0, processFlags=0, nCohInt=0,
587 586 nIncohInt=0, totalSpectra=0, nHeights=0, firstHeight=0, deltaHeight=0, samplesWin=0, spectraComb=0, nCode=0,
588 587 code=0, nBaud=None, shif_fft=False, flag_dc=False, flag_cspc=False, flag_decode=False, flag_deflip=False
589 588 ):
590 589
591 590 # self.size = 0
592 591 self.dtype = dtype
593 592 self.blockSize = blockSize
594 593 self.profilesPerBlock = 0
595 594 self.dataBlocksPerFile = 0
596 595 self.nWindows = 0
597 596 self.processFlags = 0
598 597 self.nCohInt = 0
599 598 self.nIncohInt = 0
600 599 self.totalSpectra = 0
601 600
602 601 self.nHeights = 0
603 602 self.firstHeight = 0
604 603 self.deltaHeight = 0
605 604 self.samplesWin = 0
606 605 self.spectraComb = 0
607 606 self.nCode = None
608 607 self.code = None
609 608 self.nBaud = None
610 609
611 610 self.shif_fft = False
612 611 self.flag_dc = False
613 612 self.flag_cspc = False
614 613 self.flag_decode = False
615 614 self.flag_deflip = False
616 615 self.length = 0
617 616
618 617 def read(self, fp):
619 618 self.length = 0
620 619 try:
621 620 startFp = fp.tell()
622 621 except Exception as e:
623 622 startFp = None
624 623 pass
625 624
626 625 try:
627 626 if hasattr(fp, 'read'):
628 627 header = numpy.fromfile(fp, PROCESSING_STRUCTURE, 1)
629 628 else:
630 629 header = numpy.fromstring(fp, PROCESSING_STRUCTURE, 1)
631 630 self.length += header.nbytes
632 631 except Exception as e:
633 632 print("ProcessingHeader: " + str(e))
634 633 return 0
635 634
636 635 size = int(header['nSize'][0])
637 636 self.dtype = int(header['nDataType'][0])
638 637 self.blockSize = int(header['nSizeOfDataBlock'][0])
639 638 self.profilesPerBlock = int(header['nProfilesperBlock'][0])
640 639 self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0])
641 640 self.nWindows = int(header['nNumWindows'][0])
642 641 self.processFlags = header['nProcessFlags']
643 642 self.nCohInt = int(header['nCoherentIntegrations'][0])
644 643 self.nIncohInt = int(header['nIncoherentIntegrations'][0])
645 644 self.totalSpectra = int(header['nTotalSpectra'][0])
646 645
647 646 try:
648 647 if hasattr(fp, 'read'):
649 648 samplingWindow = numpy.fromfile(
650 649 fp, SAMPLING_STRUCTURE, self.nWindows)
651 650 else:
652 651 samplingWindow = numpy.fromstring(
653 652 fp[self.length:], SAMPLING_STRUCTURE, self.nWindows)
654 653 self.length += samplingWindow.nbytes
655 654 except Exception as e:
656 655 print("ProcessingHeader: " + str(e))
657 656 return 0
658 657
659 658 self.nHeights = int(numpy.sum(samplingWindow['nsa']))
660 659 self.firstHeight = float(samplingWindow['h0'][0])
661 660 self.deltaHeight = float(samplingWindow['dh'][0])
662 661 self.samplesWin = samplingWindow['nsa'][0]
663 662
664 663 try:
665 664 if hasattr(fp, 'read'):
666 665 self.spectraComb = numpy.fromfile(
667 666 fp, 'u1', 2 * self.totalSpectra)
668 667 else:
669 668 self.spectraComb = numpy.fromstring(
670 669 fp[self.length:], 'u1', 2 * self.totalSpectra)
671 670 self.length += self.spectraComb.nbytes
672 671 except Exception as e:
673 672 print("ProcessingHeader: " + str(e))
674 673 return 0
675 674
676 675 if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE):
677 676 self.nCode = int(numpy.fromfile(fp, '<u4', 1))
678 677 self.nBaud = int(numpy.fromfile(fp, '<u4', 1))
679 678 self.code = numpy.fromfile(
680 679 fp, '<f4', self.nCode * self.nBaud).reshape(self.nCode, self.nBaud)
681 680
682 681 if ((self.processFlags & PROCFLAG.EXP_NAME_ESP) == PROCFLAG.EXP_NAME_ESP):
683 682 exp_name_len = int(numpy.fromfile(fp, '<u4', 1))
684 683 exp_name = numpy.fromfile(fp, 'u1', exp_name_len + 1)
685 684
686 685 if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA):
687 686 self.shif_fft = True
688 687 else:
689 688 self.shif_fft = False
690 689
691 690 if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC):
692 691 self.flag_dc = True
693 692 else:
694 693 self.flag_dc = False
695 694
696 695 if ((self.processFlags & PROCFLAG.DECODE_DATA) == PROCFLAG.DECODE_DATA):
697 696 self.flag_decode = True
698 697 else:
699 698 self.flag_decode = False
700 699
701 700 if ((self.processFlags & PROCFLAG.DEFLIP_DATA) == PROCFLAG.DEFLIP_DATA):
702 701 self.flag_deflip = True
703 702 else:
704 703 self.flag_deflip = False
705 704
706 705 nChannels = 0
707 706 nPairs = 0
708 707 pairList = []
709 708
710 709 for i in range(0, self.totalSpectra * 2, 2):
711 710 if self.spectraComb[i] == self.spectraComb[i + 1]:
712 711 nChannels = nChannels + 1 # par de canales iguales
713 712 else:
714 713 nPairs = nPairs + 1 # par de canales diferentes
715 714 pairList.append((self.spectraComb[i], self.spectraComb[i + 1]))
716 715
717 716 self.flag_cspc = False
718 717 if nPairs > 0:
719 718 self.flag_cspc = True
720 719
721 720 if startFp is not None:
722 721 endFp = size + startFp
723 722 if fp.tell() > endFp:
724 723 sys.stderr.write(
725 724 "Warning: Processing header size is lower than it has to be")
726 725 return 0
727 726
728 727 if fp.tell() < endFp:
729 728 sys.stderr.write(
730 729 "Warning: Processing header size is greater than it is considered")
731 730
732 731 return 1
733 732
734 733 def write(self, fp):
735 734 # Clear DEFINE_PROCESS_CODE
736 735 self.processFlags = self.processFlags & (~PROCFLAG.DEFINE_PROCESS_CODE)
737 736
738 737 headerTuple = (self.size,
739 738 self.dtype,
740 739 self.blockSize,
741 740 self.profilesPerBlock,
742 741 self.dataBlocksPerFile,
743 742 self.nWindows,
744 743 self.processFlags,
745 744 self.nCohInt,
746 745 self.nIncohInt,
747 746 self.totalSpectra)
748 747
749 748 header = numpy.array(headerTuple, PROCESSING_STRUCTURE)
750 749 header.tofile(fp)
751 750
752 751 if self.nWindows != 0:
753 752 sampleWindowTuple = (
754 753 self.firstHeight, self.deltaHeight, self.samplesWin)
755 754 samplingWindow = numpy.array(sampleWindowTuple, SAMPLING_STRUCTURE)
756 755 samplingWindow.tofile(fp)
757 756
758 757 if self.totalSpectra != 0:
759 758 # spectraComb = numpy.array([],numpy.dtype('u1'))
760 759 spectraComb = self.spectraComb
761 760 spectraComb.tofile(fp)
762 761
763 762 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
764 763 # nCode = numpy.array([self.nCode], numpy.dtype('u4')) #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba
765 764 # nCode.tofile(fp)
766 765 #
767 766 # nBaud = numpy.array([self.nBaud], numpy.dtype('u4'))
768 767 # nBaud.tofile(fp)
769 768 #
770 769 # code = self.code.reshape(self.nCode*self.nBaud)
771 770 # code = code.astype(numpy.dtype('<f4'))
772 771 # code.tofile(fp)
773 772
774 773 return 1
775 774
776 775 def get_size(self):
777 776
778 777 self.__size = 40 + 12 * self.nWindows + 2 * self.totalSpectra
779 778
780 779 # if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
781 780 # self.__size += 4 + 4 + 4*self.nCode*numpy.ceil(self.nBaud/32.)
782 781 # self.__size += 4 + 4 + 4 * self.nCode * self.nBaud
783 782
784 783 return self.__size
785 784
786 785 def set_size(self, value):
787 786
788 787 raise IOError("size is a property and it cannot be set, just read")
789 788
790 789 return
791 790
792 791 size = property(get_size, set_size)
793 792
794 793
795 794 class RCfunction:
796 795 NONE = 0
797 796 FLIP = 1
798 797 CODE = 2
799 798 SAMPLING = 3
800 799 LIN6DIV256 = 4
801 800 SYNCHRO = 5
802 801
803 802
804 803 class nCodeType:
805 804 NONE = 0
806 805 USERDEFINE = 1
807 806 BARKER2 = 2
808 807 BARKER3 = 3
809 808 BARKER4 = 4
810 809 BARKER5 = 5
811 810 BARKER7 = 6
812 811 BARKER11 = 7
813 812 BARKER13 = 8
814 813 AC128 = 9
815 814 COMPLEMENTARYCODE2 = 10
816 815 COMPLEMENTARYCODE4 = 11
817 816 COMPLEMENTARYCODE8 = 12
818 817 COMPLEMENTARYCODE16 = 13
819 818 COMPLEMENTARYCODE32 = 14
820 819 COMPLEMENTARYCODE64 = 15
821 820 COMPLEMENTARYCODE128 = 16
822 821 CODE_BINARY28 = 17
823 822
824 823
825 824 class PROCFLAG:
826 825
827 826 COHERENT_INTEGRATION = numpy.uint32(0x00000001)
828 827 DECODE_DATA = numpy.uint32(0x00000002)
829 828 SPECTRA_CALC = numpy.uint32(0x00000004)
830 829 INCOHERENT_INTEGRATION = numpy.uint32(0x00000008)
831 830 POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010)
832 831 SHIFT_FFT_DATA = numpy.uint32(0x00000020)
833 832
834 833 DATATYPE_CHAR = numpy.uint32(0x00000040)
835 834 DATATYPE_SHORT = numpy.uint32(0x00000080)
836 835 DATATYPE_LONG = numpy.uint32(0x00000100)
837 836 DATATYPE_INT64 = numpy.uint32(0x00000200)
838 837 DATATYPE_FLOAT = numpy.uint32(0x00000400)
839 838 DATATYPE_DOUBLE = numpy.uint32(0x00000800)
840 839
841 840 DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000)
842 841 DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000)
843 842 DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000)
844 843
845 844 SAVE_CHANNELS_DC = numpy.uint32(0x00008000)
846 845 DEFLIP_DATA = numpy.uint32(0x00010000)
847 846 DEFINE_PROCESS_CODE = numpy.uint32(0x00020000)
848 847
849 848 ACQ_SYS_NATALIA = numpy.uint32(0x00040000)
850 849 ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000)
851 850 ACQ_SYS_ADRXD = numpy.uint32(0x000C0000)
852 851 ACQ_SYS_JULIA = numpy.uint32(0x00100000)
853 852 ACQ_SYS_XXXXXX = numpy.uint32(0x00140000)
854 853
855 854 EXP_NAME_ESP = numpy.uint32(0x00200000)
856 855 CHANNEL_NAMES_ESP = numpy.uint32(0x00400000)
857 856
858 857 OPERATION_MASK = numpy.uint32(0x0000003F)
859 858 DATATYPE_MASK = numpy.uint32(0x00000FC0)
860 859 DATAARRANGE_MASK = numpy.uint32(0x00007000)
861 860 ACQ_SYS_MASK = numpy.uint32(0x001C0000)
862 861
863 862
864 863 dtype0 = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
865 864 dtype1 = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
866 865 dtype2 = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
867 866 dtype3 = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
868 867 dtype4 = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
869 868 dtype5 = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
870 869
871 870 NUMPY_DTYPE_LIST = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
872 871
873 872 PROCFLAG_DTYPE_LIST = [PROCFLAG.DATATYPE_CHAR,
874 873 PROCFLAG.DATATYPE_SHORT,
875 874 PROCFLAG.DATATYPE_LONG,
876 875 PROCFLAG.DATATYPE_INT64,
877 876 PROCFLAG.DATATYPE_FLOAT,
878 877 PROCFLAG.DATATYPE_DOUBLE]
879 878
880 879 DTYPE_WIDTH = [1, 2, 4, 8, 4, 8]
881 880
882 881
883 882 def get_dtype_index(numpy_dtype):
884 883
885 884 index = None
886 885
887 886 for i in range(len(NUMPY_DTYPE_LIST)):
888 887 if numpy_dtype == NUMPY_DTYPE_LIST[i]:
889 888 index = i
890 889 break
891 890
892 891 return index
893 892
894 893
895 894 def get_numpy_dtype(index):
896 895
897 896 return NUMPY_DTYPE_LIST[index]
898 897
899 898
900 899 def get_procflag_dtype(index):
901 900
902 901 return PROCFLAG_DTYPE_LIST[index]
903 902
904 903
905 904 def get_dtype_width(index):
906 905
907 906 return DTYPE_WIDTH[index] No newline at end of file
@@ -1,1320 +1,1321
1 1 import sys
2 2 import numpy
3 3 from scipy import interpolate
4 4 #TODO
5 5 #from schainpy import cSchain
6 6 from .jroproc_base import ProcessingUnit, Operation
7 7 from schainpy.model.data.jrodata import Voltage
8 8 from time import time
9 from schainpy.utils import log
10
9 11
10 12 class VoltageProc(ProcessingUnit):
11 13
12 14
13 15 def __init__(self, **kwargs):
14 16
15 17 ProcessingUnit.__init__(self, **kwargs)
16 18
17 19 # self.objectDict = {}
18 20 self.dataOut = Voltage()
19 21 self.flip = 1
20 22
21 23 def run(self):
22 24 if self.dataIn.type == 'AMISR':
23 25 self.__updateObjFromAmisrInput()
24 26
25 27 if self.dataIn.type == 'Voltage':
26 28 self.dataOut.copy(self.dataIn)
27 29
28 30 # self.dataOut.copy(self.dataIn)
29 31
30 32 def __updateObjFromAmisrInput(self):
31 33
32 34 self.dataOut.timeZone = self.dataIn.timeZone
33 35 self.dataOut.dstFlag = self.dataIn.dstFlag
34 36 self.dataOut.errorCount = self.dataIn.errorCount
35 37 self.dataOut.useLocalTime = self.dataIn.useLocalTime
36 38
37 39 self.dataOut.flagNoData = self.dataIn.flagNoData
38 40 self.dataOut.data = self.dataIn.data
39 41 self.dataOut.utctime = self.dataIn.utctime
40 42 self.dataOut.channelList = self.dataIn.channelList
41 43 # self.dataOut.timeInterval = self.dataIn.timeInterval
42 44 self.dataOut.heightList = self.dataIn.heightList
43 45 self.dataOut.nProfiles = self.dataIn.nProfiles
44 46
45 47 self.dataOut.nCohInt = self.dataIn.nCohInt
46 48 self.dataOut.ippSeconds = self.dataIn.ippSeconds
47 49 self.dataOut.frequency = self.dataIn.frequency
48 50
49 51 self.dataOut.azimuth = self.dataIn.azimuth
50 52 self.dataOut.zenith = self.dataIn.zenith
51 53
52 54 self.dataOut.beam.codeList = self.dataIn.beam.codeList
53 55 self.dataOut.beam.azimuthList = self.dataIn.beam.azimuthList
54 56 self.dataOut.beam.zenithList = self.dataIn.beam.zenithList
55 57 #
56 58 # pass#
57 59 #
58 60 # def init(self):
59 61 #
60 62 #
61 63 # if self.dataIn.type == 'AMISR':
62 64 # self.__updateObjFromAmisrInput()
63 65 #
64 66 # if self.dataIn.type == 'Voltage':
65 67 # self.dataOut.copy(self.dataIn)
66 68 # # No necesita copiar en cada init() los atributos de dataIn
67 69 # # la copia deberia hacerse por cada nuevo bloque de datos
68 70
69 71 def selectChannels(self, channelList):
70 72
71 73 channelIndexList = []
72 74
73 75 for channel in channelList:
74 76 if channel not in self.dataOut.channelList:
75 77 raise ValueError("Channel %d is not in %s" %(channel, str(self.dataOut.channelList)))
76 78
77 79 index = self.dataOut.channelList.index(channel)
78 80 channelIndexList.append(index)
79 81
80 82 self.selectChannelsByIndex(channelIndexList)
81 83
82 84 def selectChannelsByIndex(self, channelIndexList):
83 85 """
84 86 Selecciona un bloque de datos en base a canales segun el channelIndexList
85 87
86 88 Input:
87 89 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
88 90
89 91 Affected:
90 92 self.dataOut.data
91 93 self.dataOut.channelIndexList
92 94 self.dataOut.nChannels
93 95 self.dataOut.m_ProcessingHeader.totalSpectra
94 96 self.dataOut.systemHeaderObj.numChannels
95 97 self.dataOut.m_ProcessingHeader.blockSize
96 98
97 99 Return:
98 100 None
99 101 """
100 102
101 103 for channelIndex in channelIndexList:
102 104 if channelIndex not in self.dataOut.channelIndexList:
103 105 print(channelIndexList)
104 106 raise ValueError("The value %d in channelIndexList is not valid" %channelIndex)
105 107
106 108 if self.dataOut.flagDataAsBlock:
107 109 """
108 110 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
109 111 """
110 112 data = self.dataOut.data[channelIndexList,:,:]
111 113 else:
112 114 data = self.dataOut.data[channelIndexList,:]
113 115
114 116 self.dataOut.data = data
115 117 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
116 118 # self.dataOut.nChannels = nChannels
117 119
118 120 return 1
119 121
120 122 def selectHeights(self, minHei=None, maxHei=None):
121 123 """
122 124 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
123 125 minHei <= height <= maxHei
124 126
125 127 Input:
126 128 minHei : valor minimo de altura a considerar
127 129 maxHei : valor maximo de altura a considerar
128 130
129 131 Affected:
130 132 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
131 133
132 134 Return:
133 135 1 si el metodo se ejecuto con exito caso contrario devuelve 0
134 136 """
135 137
136 138 if minHei == None:
137 139 minHei = self.dataOut.heightList[0]
138 140
139 141 if maxHei == None:
140 142 maxHei = self.dataOut.heightList[-1]
141 143
142 144 if (minHei < self.dataOut.heightList[0]):
143 145 minHei = self.dataOut.heightList[0]
144 146
145 147 if (maxHei > self.dataOut.heightList[-1]):
146 148 maxHei = self.dataOut.heightList[-1]
147 149
148 150 minIndex = 0
149 151 maxIndex = 0
150 152 heights = self.dataOut.heightList
151 153
152 154 inda = numpy.where(heights >= minHei)
153 155 indb = numpy.where(heights <= maxHei)
154 156
155 157 try:
156 158 minIndex = inda[0][0]
157 159 except:
158 160 minIndex = 0
159 161
160 162 try:
161 163 maxIndex = indb[0][-1]
162 164 except:
163 165 maxIndex = len(heights)
164 166
165 167 self.selectHeightsByIndex(minIndex, maxIndex)
166 168
167 169 return 1
168 170
169 171
170 172 def selectHeightsByIndex(self, minIndex, maxIndex):
171 173 """
172 174 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
173 175 minIndex <= index <= maxIndex
174 176
175 177 Input:
176 178 minIndex : valor de indice minimo de altura a considerar
177 179 maxIndex : valor de indice maximo de altura a considerar
178 180
179 181 Affected:
180 182 self.dataOut.data
181 183 self.dataOut.heightList
182 184
183 185 Return:
184 186 1 si el metodo se ejecuto con exito caso contrario devuelve 0
185 187 """
186 188
187 189 if (minIndex < 0) or (minIndex > maxIndex):
188 190 raise ValueError("Height index range (%d,%d) is not valid" % (minIndex, maxIndex))
189 191
190 192 if (maxIndex >= self.dataOut.nHeights):
191 193 maxIndex = self.dataOut.nHeights
192 194
193 195 #voltage
194 196 if self.dataOut.flagDataAsBlock:
195 197 """
196 198 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
197 199 """
198 200 data = self.dataOut.data[:,:, minIndex:maxIndex]
199 201 else:
200 202 data = self.dataOut.data[:, minIndex:maxIndex]
201 203
202 204 # firstHeight = self.dataOut.heightList[minIndex]
203 205
204 206 self.dataOut.data = data
205 207 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex]
206 208
207 209 if self.dataOut.nHeights <= 1:
208 210 raise ValueError("selectHeights: Too few heights. Current number of heights is %d" %(self.dataOut.nHeights))
209 211
210 212 return 1
211 213
212 214
213 215 def filterByHeights(self, window):
214 216
215 217 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
216 218
217 219 if window == None:
218 220 window = (self.dataOut.radarControllerHeaderObj.txA/self.dataOut.radarControllerHeaderObj.nBaud) / deltaHeight
219 221
220 222 newdelta = deltaHeight * window
221 223 r = self.dataOut.nHeights % window
222 224 newheights = (self.dataOut.nHeights-r)/window
223 225
224 226 if newheights <= 1:
225 227 raise ValueError("filterByHeights: Too few heights. Current number of heights is %d and window is %d" %(self.dataOut.nHeights, window))
226 228
227 229 if self.dataOut.flagDataAsBlock:
228 230 """
229 231 Si la data es obtenida por bloques, dimension = [nChannels, nProfiles, nHeis]
230 232 """
231 233 buffer = self.dataOut.data[:, :, 0:self.dataOut.nHeights-r]
232 234 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nProfiles,self.dataOut.nHeights/window,window)
233 235 buffer = numpy.sum(buffer,3)
234 236
235 237 else:
236 238 buffer = self.dataOut.data[:,0:self.dataOut.nHeights-r]
237 239 buffer = buffer.reshape(self.dataOut.nChannels,self.dataOut.nHeights/window,window)
238 240 buffer = numpy.sum(buffer,2)
239 241
240 242 self.dataOut.data = buffer
241 243 self.dataOut.heightList = self.dataOut.heightList[0] + numpy.arange( newheights )*newdelta
242 244 self.dataOut.windowOfFilter = window
243 245
244 246 def setH0(self, h0, deltaHeight = None):
245 247
246 248 if not deltaHeight:
247 249 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
248 250
249 251 nHeights = self.dataOut.nHeights
250 252
251 253 newHeiRange = h0 + numpy.arange(nHeights)*deltaHeight
252 254
253 255 self.dataOut.heightList = newHeiRange
254 256
255 257 def deFlip(self, channelList = []):
256 258
257 259 data = self.dataOut.data.copy()
258 260
259 261 if self.dataOut.flagDataAsBlock:
260 262 flip = self.flip
261 263 profileList = list(range(self.dataOut.nProfiles))
262 264
263 265 if not channelList:
264 266 for thisProfile in profileList:
265 267 data[:,thisProfile,:] = data[:,thisProfile,:]*flip
266 268 flip *= -1.0
267 269 else:
268 270 for thisChannel in channelList:
269 271 if thisChannel not in self.dataOut.channelList:
270 272 continue
271 273
272 274 for thisProfile in profileList:
273 275 data[thisChannel,thisProfile,:] = data[thisChannel,thisProfile,:]*flip
274 276 flip *= -1.0
275 277
276 278 self.flip = flip
277 279
278 280 else:
279 281 if not channelList:
280 282 data[:,:] = data[:,:]*self.flip
281 283 else:
282 284 for thisChannel in channelList:
283 285 if thisChannel not in self.dataOut.channelList:
284 286 continue
285 287
286 288 data[thisChannel,:] = data[thisChannel,:]*self.flip
287 289
288 290 self.flip *= -1.
289 291
290 292 self.dataOut.data = data
291 293
292 294 def setRadarFrequency(self, frequency=None):
293 295
294 296 if frequency != None:
295 297 self.dataOut.frequency = frequency
296 298
297 299 return 1
298 300
299 301 def interpolateHeights(self, topLim, botLim):
300 302 #69 al 72 para julia
301 303 #82-84 para meteoros
302 304 if len(numpy.shape(self.dataOut.data))==2:
303 305 sampInterp = (self.dataOut.data[:,botLim-1] + self.dataOut.data[:,topLim+1])/2
304 306 sampInterp = numpy.transpose(numpy.tile(sampInterp,(topLim-botLim + 1,1)))
305 307 #self.dataOut.data[:,botLim:limSup+1] = sampInterp
306 308 self.dataOut.data[:,botLim:topLim+1] = sampInterp
307 309 else:
308 310 nHeights = self.dataOut.data.shape[2]
309 311 x = numpy.hstack((numpy.arange(botLim),numpy.arange(topLim+1,nHeights)))
310 312 y = self.dataOut.data[:,:,list(range(botLim))+list(range(topLim+1,nHeights))]
311 313 f = interpolate.interp1d(x, y, axis = 2)
312 314 xnew = numpy.arange(botLim,topLim+1)
313 315 ynew = f(xnew)
314 316
315 317 self.dataOut.data[:,:,botLim:topLim+1] = ynew
316 318
317 319 # import collections
318 320
319 321 class CohInt(Operation):
320 322
321 323 isConfig = False
322 324 __profIndex = 0
323 325 __byTime = False
324 326 __initime = None
325 327 __lastdatatime = None
326 328 __integrationtime = None
327 329 __buffer = None
328 330 __bufferStride = []
329 331 __dataReady = False
330 332 __profIndexStride = 0
331 333 __dataToPutStride = False
332 334 n = None
333 335
334 336 def __init__(self, **kwargs):
335 337
336 338 Operation.__init__(self, **kwargs)
337 339
338 340 # self.isConfig = False
339 341
340 342 def setup(self, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False):
341 343 """
342 344 Set the parameters of the integration class.
343 345
344 346 Inputs:
345 347
346 348 n : Number of coherent integrations
347 349 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
348 350 overlapping :
349 351 """
350 352
351 353 self.__initime = None
352 354 self.__lastdatatime = 0
353 355 self.__buffer = None
354 356 self.__dataReady = False
355 357 self.byblock = byblock
356 358 self.stride = stride
357 359
358 360 if n == None and timeInterval == None:
359 361 raise ValueError("n or timeInterval should be specified ...")
360 362
361 363 if n != None:
362 364 self.n = n
363 365 self.__byTime = False
364 366 else:
365 367 self.__integrationtime = timeInterval #* 60. #if (type(timeInterval)!=integer) -> change this line
366 368 self.n = 9999
367 369 self.__byTime = True
368 370
369 371 if overlapping:
370 372 self.__withOverlapping = True
371 373 self.__buffer = None
372 374 else:
373 375 self.__withOverlapping = False
374 376 self.__buffer = 0
375 377
376 378 self.__profIndex = 0
377 379
378 380 def putData(self, data):
379 381
380 382 """
381 383 Add a profile to the __buffer and increase in one the __profileIndex
382 384
383 385 """
384 386
385 387 if not self.__withOverlapping:
386 388 self.__buffer += data.copy()
387 389 self.__profIndex += 1
388 390 return
389 391
390 392 #Overlapping data
391 393 nChannels, nHeis = data.shape
392 394 data = numpy.reshape(data, (1, nChannels, nHeis))
393 395
394 396 #If the buffer is empty then it takes the data value
395 397 if self.__buffer is None:
396 398 self.__buffer = data
397 399 self.__profIndex += 1
398 400 return
399 401
400 402 #If the buffer length is lower than n then stakcing the data value
401 403 if self.__profIndex < self.n:
402 404 self.__buffer = numpy.vstack((self.__buffer, data))
403 405 self.__profIndex += 1
404 406 return
405 407
406 408 #If the buffer length is equal to n then replacing the last buffer value with the data value
407 409 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
408 410 self.__buffer[self.n-1] = data
409 411 self.__profIndex = self.n
410 412 return
411 413
412 414
413 415 def pushData(self):
414 416 """
415 417 Return the sum of the last profiles and the profiles used in the sum.
416 418
417 419 Affected:
418 420
419 421 self.__profileIndex
420 422
421 423 """
422 424
423 425 if not self.__withOverlapping:
424 426 data = self.__buffer
425 427 n = self.__profIndex
426 428
427 429 self.__buffer = 0
428 430 self.__profIndex = 0
429 431
430 432 return data, n
431 433
432 434 #Integration with Overlapping
433 435 data = numpy.sum(self.__buffer, axis=0)
434 436 # print data
435 437 # raise
436 438 n = self.__profIndex
437 439
438 440 return data, n
439 441
440 442 def byProfiles(self, data):
441 443
442 444 self.__dataReady = False
443 445 avgdata = None
444 446 # n = None
445 447 # print data
446 448 # raise
447 449 self.putData(data)
448 450
449 451 if self.__profIndex == self.n:
450 452 avgdata, n = self.pushData()
451 453 self.__dataReady = True
452 454
453 455 return avgdata
454 456
455 457 def byTime(self, data, datatime):
456 458
457 459 self.__dataReady = False
458 460 avgdata = None
459 461 n = None
460 462
461 463 self.putData(data)
462 464
463 465 if (datatime - self.__initime) >= self.__integrationtime:
464 466 avgdata, n = self.pushData()
465 467 self.n = n
466 468 self.__dataReady = True
467 469
468 470 return avgdata
469 471
470 472 def integrateByStride(self, data, datatime):
471 473 # print data
472 474 if self.__profIndex == 0:
473 475 self.__buffer = [[data.copy(), datatime]]
474 476 else:
475 477 self.__buffer.append([data.copy(),datatime])
476 478 self.__profIndex += 1
477 479 self.__dataReady = False
478 480
479 481 if self.__profIndex == self.n * self.stride :
480 482 self.__dataToPutStride = True
481 483 self.__profIndexStride = 0
482 484 self.__profIndex = 0
483 485 self.__bufferStride = []
484 486 for i in range(self.stride):
485 487 current = self.__buffer[i::self.stride]
486 488 data = numpy.sum([t[0] for t in current], axis=0)
487 489 avgdatatime = numpy.average([t[1] for t in current])
488 490 # print data
489 491 self.__bufferStride.append((data, avgdatatime))
490 492
491 493 if self.__dataToPutStride:
492 494 self.__dataReady = True
493 495 self.__profIndexStride += 1
494 496 if self.__profIndexStride == self.stride:
495 497 self.__dataToPutStride = False
496 498 # print self.__bufferStride[self.__profIndexStride - 1]
497 499 # raise
498 500 return self.__bufferStride[self.__profIndexStride - 1]
499 501
500 502
501 503 return None, None
502 504
503 505 def integrate(self, data, datatime=None):
504 506
505 507 if self.__initime == None:
506 508 self.__initime = datatime
507 509
508 510 if self.__byTime:
509 511 avgdata = self.byTime(data, datatime)
510 512 else:
511 513 avgdata = self.byProfiles(data)
512 514
513 515
514 516 self.__lastdatatime = datatime
515 517
516 518 if avgdata is None:
517 519 return None, None
518 520
519 521 avgdatatime = self.__initime
520 522
521 523 deltatime = datatime - self.__lastdatatime
522 524
523 525 if not self.__withOverlapping:
524 526 self.__initime = datatime
525 527 else:
526 528 self.__initime += deltatime
527 529
528 530 return avgdata, avgdatatime
529 531
530 532 def integrateByBlock(self, dataOut):
531 533
532 534 times = int(dataOut.data.shape[1]/self.n)
533 535 avgdata = numpy.zeros((dataOut.nChannels, times, dataOut.nHeights), dtype=numpy.complex)
534 536
535 537 id_min = 0
536 538 id_max = self.n
537 539
538 540 for i in range(times):
539 541 junk = dataOut.data[:,id_min:id_max,:]
540 542 avgdata[:,i,:] = junk.sum(axis=1)
541 543 id_min += self.n
542 544 id_max += self.n
543 545
544 546 timeInterval = dataOut.ippSeconds*self.n
545 547 avgdatatime = (times - 1) * timeInterval + dataOut.utctime
546 548 self.__dataReady = True
547 549 return avgdata, avgdatatime
548 550
549 551 def run(self, dataOut, n=None, timeInterval=None, stride=None, overlapping=False, byblock=False, **kwargs):
550 552 if not self.isConfig:
551 553 self.setup(n=n, stride=stride, timeInterval=timeInterval, overlapping=overlapping, byblock=byblock, **kwargs)
552 554 self.isConfig = True
553 555
554 556 if dataOut.flagDataAsBlock:
555 557 """
556 558 Si la data es leida por bloques, dimension = [nChannels, nProfiles, nHeis]
557 559 """
558 560 avgdata, avgdatatime = self.integrateByBlock(dataOut)
559 561 dataOut.nProfiles /= self.n
560 562 else:
561 563 if stride is None:
562 564 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
563 565 else:
564 566 avgdata, avgdatatime = self.integrateByStride(dataOut.data, dataOut.utctime)
565 567
566 568
567 569 # dataOut.timeInterval *= n
568 570 dataOut.flagNoData = True
569 571
570 572 if self.__dataReady:
571 573 dataOut.data = avgdata
572 574 dataOut.nCohInt *= self.n
573 575 dataOut.utctime = avgdatatime
574 576 # print avgdata, avgdatatime
575 577 # raise
576 578 # dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
577 579 dataOut.flagNoData = False
578 580
579 581 class Decoder(Operation):
580 582
581 583 isConfig = False
582 584 __profIndex = 0
583 585
584 586 code = None
585 587
586 588 nCode = None
587 589 nBaud = None
588 590
589 591 def __init__(self, **kwargs):
590 592
591 593 Operation.__init__(self, **kwargs)
592 594
593 595 self.times = None
594 596 self.osamp = None
595 597 # self.__setValues = False
596 598 self.isConfig = False
597 599
598 600 def setup(self, code, osamp, dataOut):
599 601
600 602 self.__profIndex = 0
601 603
602 604 self.code = code
603 605
604 606 self.nCode = len(code)
605 607 self.nBaud = len(code[0])
606 608
607 609 if (osamp != None) and (osamp >1):
608 610 self.osamp = osamp
609 611 self.code = numpy.repeat(code, repeats=self.osamp, axis=1)
610 612 self.nBaud = self.nBaud*self.osamp
611 613
612 614 self.__nChannels = dataOut.nChannels
613 615 self.__nProfiles = dataOut.nProfiles
614 616 self.__nHeis = dataOut.nHeights
615 617
616 618 if self.__nHeis < self.nBaud:
617 619 raise ValueError('Number of heights (%d) should be greater than number of bauds (%d)' %(self.__nHeis, self.nBaud))
618 620
619 621 #Frequency
620 622 __codeBuffer = numpy.zeros((self.nCode, self.__nHeis), dtype=numpy.complex)
621 623
622 624 __codeBuffer[:,0:self.nBaud] = self.code
623 625
624 626 self.fft_code = numpy.conj(numpy.fft.fft(__codeBuffer, axis=1))
625 627
626 628 if dataOut.flagDataAsBlock:
627 629
628 630 self.ndatadec = self.__nHeis #- self.nBaud + 1
629 631
630 632 self.datadecTime = numpy.zeros((self.__nChannels, self.__nProfiles, self.ndatadec), dtype=numpy.complex)
631 633
632 634 else:
633 635
634 636 #Time
635 637 self.ndatadec = self.__nHeis #- self.nBaud + 1
636 638
637 639 self.datadecTime = numpy.zeros((self.__nChannels, self.ndatadec), dtype=numpy.complex)
638 640
639 641 def __convolutionInFreq(self, data):
640 642
641 643 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
642 644
643 645 fft_data = numpy.fft.fft(data, axis=1)
644 646
645 647 conv = fft_data*fft_code
646 648
647 649 data = numpy.fft.ifft(conv,axis=1)
648 650
649 651 return data
650 652
651 653 def __convolutionInFreqOpt(self, data):
652 654
653 655 raise NotImplementedError
654 656
655 657 def __convolutionInTime(self, data):
656 658
657 659 code = self.code[self.__profIndex]
658 660 for i in range(self.__nChannels):
659 661 self.datadecTime[i,:] = numpy.correlate(data[i,:], code, mode='full')[self.nBaud-1:]
660 662
661 663 return self.datadecTime
662 664
663 665 def __convolutionByBlockInTime(self, data):
664 666
665 667 repetitions = self.__nProfiles / self.nCode
666 668
667 669 junk = numpy.lib.stride_tricks.as_strided(self.code, (repetitions, self.code.size), (0, self.code.itemsize))
668 670 junk = junk.flatten()
669 671 code_block = numpy.reshape(junk, (self.nCode*repetitions, self.nBaud))
670 672 profilesList = range(self.__nProfiles)
671 673
672 674 for i in range(self.__nChannels):
673 675 for j in profilesList:
674 676 self.datadecTime[i,j,:] = numpy.correlate(data[i,j,:], code_block[j,:], mode='full')[self.nBaud-1:]
675 677 return self.datadecTime
676 678
677 679 def __convolutionByBlockInFreq(self, data):
678 680
679 681 raise NotImplementedError("Decoder by frequency fro Blocks not implemented")
680 682
681 683
682 684 fft_code = self.fft_code[self.__profIndex].reshape(1,-1)
683 685
684 686 fft_data = numpy.fft.fft(data, axis=2)
685 687
686 688 conv = fft_data*fft_code
687 689
688 690 data = numpy.fft.ifft(conv,axis=2)
689 691
690 692 return data
691 693
692 694
693 695 def run(self, dataOut, code=None, nCode=None, nBaud=None, mode = 0, osamp=None, times=None):
694 696
695 697 if dataOut.flagDecodeData:
696 698 print("This data is already decoded, recoding again ...")
697 699
698 700 if not self.isConfig:
699 701
700 702 if code is None:
701 703 if dataOut.code is None:
702 704 raise ValueError("Code could not be read from %s instance. Enter a value in Code parameter" %dataOut.type)
703 705
704 706 code = dataOut.code
705 707 else:
706 708 code = numpy.array(code).reshape(nCode,nBaud)
707 709 self.setup(code, osamp, dataOut)
708 710
709 711 self.isConfig = True
710 712
711 713 if mode == 3:
712 714 sys.stderr.write("Decoder Warning: mode=%d is not valid, using mode=0\n" %mode)
713 715
714 716 if times != None:
715 717 sys.stderr.write("Decoder Warning: Argument 'times' in not used anymore\n")
716 718
717 719 if self.code is None:
718 720 print("Fail decoding: Code is not defined.")
719 721 return
720 722
721 723 self.__nProfiles = dataOut.nProfiles
722 724 datadec = None
723 725
724 726 if mode == 3:
725 727 mode = 0
726 728
727 729 if dataOut.flagDataAsBlock:
728 730 """
729 731 Decoding when data have been read as block,
730 732 """
731 733
732 734 if mode == 0:
733 735 datadec = self.__convolutionByBlockInTime(dataOut.data)
734 736 if mode == 1:
735 737 datadec = self.__convolutionByBlockInFreq(dataOut.data)
736 738 else:
737 739 """
738 740 Decoding when data have been read profile by profile
739 741 """
740 742 if mode == 0:
741 743 datadec = self.__convolutionInTime(dataOut.data)
742 744
743 745 if mode == 1:
744 746 datadec = self.__convolutionInFreq(dataOut.data)
745 747
746 748 if mode == 2:
747 749 datadec = self.__convolutionInFreqOpt(dataOut.data)
748 750
749 751 if datadec is None:
750 752 raise ValueError("Codification mode selected is not valid: mode=%d. Try selecting 0 or 1" %mode)
751 753
752 754 dataOut.code = self.code
753 755 dataOut.nCode = self.nCode
754 756 dataOut.nBaud = self.nBaud
755 757
756 758 dataOut.data = datadec
757 759
758 760 dataOut.heightList = dataOut.heightList[0:datadec.shape[-1]]
759 761
760 762 dataOut.flagDecodeData = True #asumo q la data esta decodificada
761 763
762 764 if self.__profIndex == self.nCode-1:
763 765 self.__profIndex = 0
764 766 return 1
765 767
766 768 self.__profIndex += 1
767 769
768 770 return 1
769 771 # dataOut.flagDeflipData = True #asumo q la data no esta sin flip
770 772
771 773
772 774 class ProfileConcat(Operation):
773 775
774 776 isConfig = False
775 777 buffer = None
776 778
777 779 def __init__(self, **kwargs):
778 780
779 781 Operation.__init__(self, **kwargs)
780 782 self.profileIndex = 0
781 783
782 784 def reset(self):
783 785 self.buffer = numpy.zeros_like(self.buffer)
784 786 self.start_index = 0
785 787 self.times = 1
786 788
787 789 def setup(self, data, m, n=1):
788 790 self.buffer = numpy.zeros((data.shape[0],data.shape[1]*m),dtype=type(data[0,0]))
789 791 self.nHeights = data.shape[1]#.nHeights
790 792 self.start_index = 0
791 793 self.times = 1
792 794
793 795 def concat(self, data):
794 796
795 797 self.buffer[:,self.start_index:self.nHeights*self.times] = data.copy()
796 798 self.start_index = self.start_index + self.nHeights
797 799
798 800 def run(self, dataOut, m):
799 801
800 802 dataOut.flagNoData = True
801 803
802 804 if not self.isConfig:
803 805 self.setup(dataOut.data, m, 1)
804 806 self.isConfig = True
805 807
806 808 if dataOut.flagDataAsBlock:
807 809 raise ValueError("ProfileConcat can only be used when voltage have been read profile by profile, getBlock = False")
808 810
809 811 else:
810 812 self.concat(dataOut.data)
811 813 self.times += 1
812 814 if self.times > m:
813 815 dataOut.data = self.buffer
814 816 self.reset()
815 817 dataOut.flagNoData = False
816 818 # se deben actualizar mas propiedades del header y del objeto dataOut, por ejemplo, las alturas
817 819 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
818 820 xf = dataOut.heightList[0] + dataOut.nHeights * deltaHeight * m
819 821 dataOut.heightList = numpy.arange(dataOut.heightList[0], xf, deltaHeight)
820 822 dataOut.ippSeconds *= m
821 823
822 824 class ProfileSelector(Operation):
823 825
824 826 profileIndex = None
825 827 # Tamanho total de los perfiles
826 828 nProfiles = None
827 829
828 830 def __init__(self, **kwargs):
829 831
830 832 Operation.__init__(self, **kwargs)
831 833 self.profileIndex = 0
832 834
833 835 def incProfileIndex(self):
834 836
835 837 self.profileIndex += 1
836 838
837 839 if self.profileIndex >= self.nProfiles:
838 840 self.profileIndex = 0
839 841
840 842 def isThisProfileInRange(self, profileIndex, minIndex, maxIndex):
841 843
842 844 if profileIndex < minIndex:
843 845 return False
844 846
845 847 if profileIndex > maxIndex:
846 848 return False
847 849
848 850 return True
849 851
850 852 def isThisProfileInList(self, profileIndex, profileList):
851 853
852 854 if profileIndex not in profileList:
853 855 return False
854 856
855 857 return True
856 858
857 859 def run(self, dataOut, profileList=None, profileRangeList=None, beam=None, byblock=False, rangeList = None, nProfiles=None):
858 860
859 861 """
860 862 ProfileSelector:
861 863
862 864 Inputs:
863 865 profileList : Index of profiles selected. Example: profileList = (0,1,2,7,8)
864 866
865 867 profileRangeList : Minimum and maximum profile indexes. Example: profileRangeList = (4, 30)
866 868
867 869 rangeList : List of profile ranges. Example: rangeList = ((4, 30), (32, 64), (128, 256))
868 870
869 871 """
870 872
871 873 if rangeList is not None:
872 874 if type(rangeList[0]) not in (tuple, list):
873 875 rangeList = [rangeList]
874 876
875 877 dataOut.flagNoData = True
876 878
877 879 if dataOut.flagDataAsBlock:
878 880 """
879 881 data dimension = [nChannels, nProfiles, nHeis]
880 882 """
881 883 if profileList != None:
882 884 dataOut.data = dataOut.data[:,profileList,:]
883 885
884 886 if profileRangeList != None:
885 887 minIndex = profileRangeList[0]
886 888 maxIndex = profileRangeList[1]
887 889 profileList = list(range(minIndex, maxIndex+1))
888 890
889 891 dataOut.data = dataOut.data[:,minIndex:maxIndex+1,:]
890 892
891 893 if rangeList != None:
892 894
893 895 profileList = []
894 896
895 897 for thisRange in rangeList:
896 898 minIndex = thisRange[0]
897 899 maxIndex = thisRange[1]
898 900
899 901 profileList.extend(list(range(minIndex, maxIndex+1)))
900 902
901 903 dataOut.data = dataOut.data[:,profileList,:]
902 904
903 905 dataOut.nProfiles = len(profileList)
904 906 dataOut.profileIndex = dataOut.nProfiles - 1
905 907 dataOut.flagNoData = False
906 908
907 909 return True
908 910
909 911 """
910 912 data dimension = [nChannels, nHeis]
911 913 """
912 914
913 915 if profileList != None:
914 916
915 917 if self.isThisProfileInList(dataOut.profileIndex, profileList):
916 918
917 919 self.nProfiles = len(profileList)
918 920 dataOut.nProfiles = self.nProfiles
919 921 dataOut.profileIndex = self.profileIndex
920 922 dataOut.flagNoData = False
921 923
922 924 self.incProfileIndex()
923 925 return True
924 926
925 927 if profileRangeList != None:
926 928
927 929 minIndex = profileRangeList[0]
928 930 maxIndex = profileRangeList[1]
929 931
930 932 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
931 933
932 934 self.nProfiles = maxIndex - minIndex + 1
933 935 dataOut.nProfiles = self.nProfiles
934 936 dataOut.profileIndex = self.profileIndex
935 937 dataOut.flagNoData = False
936 938
937 939 self.incProfileIndex()
938 940 return True
939 941
940 942 if rangeList != None:
941 943
942 944 nProfiles = 0
943 945
944 946 for thisRange in rangeList:
945 947 minIndex = thisRange[0]
946 948 maxIndex = thisRange[1]
947 949
948 950 nProfiles += maxIndex - minIndex + 1
949 951
950 952 for thisRange in rangeList:
951 953
952 954 minIndex = thisRange[0]
953 955 maxIndex = thisRange[1]
954 956
955 957 if self.isThisProfileInRange(dataOut.profileIndex, minIndex, maxIndex):
956 958
957 959 self.nProfiles = nProfiles
958 960 dataOut.nProfiles = self.nProfiles
959 961 dataOut.profileIndex = self.profileIndex
960 962 dataOut.flagNoData = False
961 963
962 964 self.incProfileIndex()
963 965
964 966 break
965 967
966 968 return True
967 969
968 970
969 971 if beam != None: #beam is only for AMISR data
970 972 if self.isThisProfileInList(dataOut.profileIndex, dataOut.beamRangeDict[beam]):
971 973 dataOut.flagNoData = False
972 974 dataOut.profileIndex = self.profileIndex
973 975
974 976 self.incProfileIndex()
975 977
976 978 return True
977 979
978 980 raise ValueError("ProfileSelector needs profileList, profileRangeList or rangeList parameter")
979 981
980 982 return False
981 983
982 984 class Reshaper(Operation):
983 985
984 986 def __init__(self, **kwargs):
985 987
986 988 Operation.__init__(self, **kwargs)
987 989
988 990 self.__buffer = None
989 991 self.__nitems = 0
990 992
991 993 def __appendProfile(self, dataOut, nTxs):
992 994
993 995 if self.__buffer is None:
994 996 shape = (dataOut.nChannels, int(dataOut.nHeights/nTxs) )
995 997 self.__buffer = numpy.empty(shape, dtype = dataOut.data.dtype)
996 998
997 999 ini = dataOut.nHeights * self.__nitems
998 1000 end = ini + dataOut.nHeights
999 1001
1000 1002 self.__buffer[:, ini:end] = dataOut.data
1001 1003
1002 1004 self.__nitems += 1
1003 1005
1004 1006 return int(self.__nitems*nTxs)
1005 1007
1006 1008 def __getBuffer(self):
1007 1009
1008 1010 if self.__nitems == int(1./self.__nTxs):
1009 1011
1010 1012 self.__nitems = 0
1011 1013
1012 1014 return self.__buffer.copy()
1013 1015
1014 1016 return None
1015 1017
1016 1018 def __checkInputs(self, dataOut, shape, nTxs):
1017 1019
1018 1020 if shape is None and nTxs is None:
1019 1021 raise ValueError("Reshaper: shape of factor should be defined")
1020 1022
1021 1023 if nTxs:
1022 1024 if nTxs < 0:
1023 1025 raise ValueError("nTxs should be greater than 0")
1024 1026
1025 1027 if nTxs < 1 and dataOut.nProfiles % (1./nTxs) != 0:
1026 1028 raise ValueError("nProfiles= %d is not divisibled by (1./nTxs) = %f" %(dataOut.nProfiles, (1./nTxs)))
1027 1029
1028 1030 shape = [dataOut.nChannels, dataOut.nProfiles*nTxs, dataOut.nHeights/nTxs]
1029 1031
1030 1032 return shape, nTxs
1031 1033
1032 1034 if len(shape) != 2 and len(shape) != 3:
1033 1035 raise ValueError("shape dimension should be equal to 2 or 3. shape = (nProfiles, nHeis) or (nChannels, nProfiles, nHeis). Actually shape = (%d, %d, %d)" %(dataOut.nChannels, dataOut.nProfiles, dataOut.nHeights))
1034 1036
1035 1037 if len(shape) == 2:
1036 1038 shape_tuple = [dataOut.nChannels]
1037 1039 shape_tuple.extend(shape)
1038 1040 else:
1039 1041 shape_tuple = list(shape)
1040 1042
1041 1043 nTxs = 1.0*shape_tuple[1]/dataOut.nProfiles
1042 1044
1043 1045 return shape_tuple, nTxs
1044 1046
1045 1047 def run(self, dataOut, shape=None, nTxs=None):
1046 1048
1047 1049 shape_tuple, self.__nTxs = self.__checkInputs(dataOut, shape, nTxs)
1048 1050
1049 1051 dataOut.flagNoData = True
1050 1052 profileIndex = None
1051 1053
1052 1054 if dataOut.flagDataAsBlock:
1053 1055
1054 1056 dataOut.data = numpy.reshape(dataOut.data, shape_tuple)
1055 1057 dataOut.flagNoData = False
1056 1058
1057 1059 profileIndex = int(dataOut.nProfiles*self.__nTxs) - 1
1058 1060
1059 1061 else:
1060 1062
1061 1063 if self.__nTxs < 1:
1062 1064
1063 1065 self.__appendProfile(dataOut, self.__nTxs)
1064 1066 new_data = self.__getBuffer()
1065 1067
1066 1068 if new_data is not None:
1067 1069 dataOut.data = new_data
1068 1070 dataOut.flagNoData = False
1069 1071
1070 1072 profileIndex = dataOut.profileIndex*nTxs
1071 1073
1072 1074 else:
1073 1075 raise ValueError("nTxs should be greater than 0 and lower than 1, or use VoltageReader(..., getblock=True)")
1074 1076
1075 1077 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1076 1078
1077 1079 dataOut.heightList = numpy.arange(dataOut.nHeights/self.__nTxs) * deltaHeight + dataOut.heightList[0]
1078 1080
1079 1081 dataOut.nProfiles = int(dataOut.nProfiles*self.__nTxs)
1080 1082
1081 1083 dataOut.profileIndex = profileIndex
1082 1084
1083 1085 dataOut.ippSeconds /= self.__nTxs
1084 1086
1085 1087 class SplitProfiles(Operation):
1086 1088
1087 1089 def __init__(self, **kwargs):
1088 1090
1089 1091 Operation.__init__(self, **kwargs)
1090 1092
1091 1093 def run(self, dataOut, n):
1092 1094
1093 1095 dataOut.flagNoData = True
1094 1096 profileIndex = None
1095 1097
1096 1098 if dataOut.flagDataAsBlock:
1097 1099
1098 1100 #nchannels, nprofiles, nsamples
1099 1101 shape = dataOut.data.shape
1100 1102
1101 1103 if shape[2] % n != 0:
1102 1104 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[2]))
1103 1105
1104 new_shape = shape[0], shape[1]*n, shape[2]/n
1105
1106 new_shape = shape[0], shape[1]*n, int(shape[2]/n)
1106 1107 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1107 1108 dataOut.flagNoData = False
1108 1109
1109 1110 profileIndex = int(dataOut.nProfiles/n) - 1
1110 1111
1111 1112 else:
1112 1113
1113 1114 raise ValueError("Could not split the data when is read Profile by Profile. Use VoltageReader(..., getblock=True)")
1114 1115
1115 1116 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1116 1117
1117 1118 dataOut.heightList = numpy.arange(dataOut.nHeights/n) * deltaHeight + dataOut.heightList[0]
1118 1119
1119 1120 dataOut.nProfiles = int(dataOut.nProfiles*n)
1120 1121
1121 1122 dataOut.profileIndex = profileIndex
1122 1123
1123 1124 dataOut.ippSeconds /= n
1124 1125
1125 1126 class CombineProfiles(Operation):
1126 1127 def __init__(self, **kwargs):
1127 1128
1128 1129 Operation.__init__(self, **kwargs)
1129 1130
1130 1131 self.__remData = None
1131 1132 self.__profileIndex = 0
1132 1133
1133 1134 def run(self, dataOut, n):
1134 1135
1135 1136 dataOut.flagNoData = True
1136 1137 profileIndex = None
1137 1138
1138 1139 if dataOut.flagDataAsBlock:
1139 1140
1140 1141 #nchannels, nprofiles, nsamples
1141 1142 shape = dataOut.data.shape
1142 1143 new_shape = shape[0], shape[1]/n, shape[2]*n
1143 1144
1144 1145 if shape[1] % n != 0:
1145 1146 raise ValueError("Could not split the data, n=%d has to be multiple of %d" %(n, shape[1]))
1146 1147
1147 1148 dataOut.data = numpy.reshape(dataOut.data, new_shape)
1148 1149 dataOut.flagNoData = False
1149 1150
1150 1151 profileIndex = int(dataOut.nProfiles*n) - 1
1151 1152
1152 1153 else:
1153 1154
1154 1155 #nchannels, nsamples
1155 1156 if self.__remData is None:
1156 1157 newData = dataOut.data
1157 1158 else:
1158 1159 newData = numpy.concatenate((self.__remData, dataOut.data), axis=1)
1159 1160
1160 1161 self.__profileIndex += 1
1161 1162
1162 1163 if self.__profileIndex < n:
1163 1164 self.__remData = newData
1164 1165 #continue
1165 1166 return
1166 1167
1167 1168 self.__profileIndex = 0
1168 1169 self.__remData = None
1169 1170
1170 1171 dataOut.data = newData
1171 1172 dataOut.flagNoData = False
1172 1173
1173 1174 profileIndex = dataOut.profileIndex/n
1174 1175
1175 1176
1176 1177 deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1177 1178
1178 1179 dataOut.heightList = numpy.arange(dataOut.nHeights*n) * deltaHeight + dataOut.heightList[0]
1179 1180
1180 1181 dataOut.nProfiles = int(dataOut.nProfiles/n)
1181 1182
1182 1183 dataOut.profileIndex = profileIndex
1183 1184
1184 1185 dataOut.ippSeconds *= n
1185 1186
1186 1187 # import collections
1187 1188 # from scipy.stats import mode
1188 1189 #
1189 1190 # class Synchronize(Operation):
1190 1191 #
1191 1192 # isConfig = False
1192 1193 # __profIndex = 0
1193 1194 #
1194 1195 # def __init__(self, **kwargs):
1195 1196 #
1196 1197 # Operation.__init__(self, **kwargs)
1197 1198 # # self.isConfig = False
1198 1199 # self.__powBuffer = None
1199 1200 # self.__startIndex = 0
1200 1201 # self.__pulseFound = False
1201 1202 #
1202 1203 # def __findTxPulse(self, dataOut, channel=0, pulse_with = None):
1203 1204 #
1204 1205 # #Read data
1205 1206 #
1206 1207 # powerdB = dataOut.getPower(channel = channel)
1207 1208 # noisedB = dataOut.getNoise(channel = channel)[0]
1208 1209 #
1209 1210 # self.__powBuffer.extend(powerdB.flatten())
1210 1211 #
1211 1212 # dataArray = numpy.array(self.__powBuffer)
1212 1213 #
1213 1214 # filteredPower = numpy.correlate(dataArray, dataArray[0:self.__nSamples], "same")
1214 1215 #
1215 1216 # maxValue = numpy.nanmax(filteredPower)
1216 1217 #
1217 1218 # if maxValue < noisedB + 10:
1218 1219 # #No se encuentra ningun pulso de transmision
1219 1220 # return None
1220 1221 #
1221 1222 # maxValuesIndex = numpy.where(filteredPower > maxValue - 0.1*abs(maxValue))[0]
1222 1223 #
1223 1224 # if len(maxValuesIndex) < 2:
1224 1225 # #Solo se encontro un solo pulso de transmision de un baudio, esperando por el siguiente TX
1225 1226 # return None
1226 1227 #
1227 1228 # phasedMaxValuesIndex = maxValuesIndex - self.__nSamples
1228 1229 #
1229 1230 # #Seleccionar solo valores con un espaciamiento de nSamples
1230 1231 # pulseIndex = numpy.intersect1d(maxValuesIndex, phasedMaxValuesIndex)
1231 1232 #
1232 1233 # if len(pulseIndex) < 2:
1233 1234 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1234 1235 # return None
1235 1236 #
1236 1237 # spacing = pulseIndex[1:] - pulseIndex[:-1]
1237 1238 #
1238 1239 # #remover senales que se distancien menos de 10 unidades o muestras
1239 1240 # #(No deberian existir IPP menor a 10 unidades)
1240 1241 #
1241 1242 # realIndex = numpy.where(spacing > 10 )[0]
1242 1243 #
1243 1244 # if len(realIndex) < 2:
1244 1245 # #Solo se encontro un pulso de transmision con ancho mayor a 1
1245 1246 # return None
1246 1247 #
1247 1248 # #Eliminar pulsos anchos (deja solo la diferencia entre IPPs)
1248 1249 # realPulseIndex = pulseIndex[realIndex]
1249 1250 #
1250 1251 # period = mode(realPulseIndex[1:] - realPulseIndex[:-1])[0][0]
1251 1252 #
1252 1253 # print "IPP = %d samples" %period
1253 1254 #
1254 1255 # self.__newNSamples = dataOut.nHeights #int(period)
1255 1256 # self.__startIndex = int(realPulseIndex[0])
1256 1257 #
1257 1258 # return 1
1258 1259 #
1259 1260 #
1260 1261 # def setup(self, nSamples, nChannels, buffer_size = 4):
1261 1262 #
1262 1263 # self.__powBuffer = collections.deque(numpy.zeros( buffer_size*nSamples,dtype=numpy.float),
1263 1264 # maxlen = buffer_size*nSamples)
1264 1265 #
1265 1266 # bufferList = []
1266 1267 #
1267 1268 # for i in range(nChannels):
1268 1269 # bufferByChannel = collections.deque(numpy.zeros( buffer_size*nSamples, dtype=numpy.complex) + numpy.NAN,
1269 1270 # maxlen = buffer_size*nSamples)
1270 1271 #
1271 1272 # bufferList.append(bufferByChannel)
1272 1273 #
1273 1274 # self.__nSamples = nSamples
1274 1275 # self.__nChannels = nChannels
1275 1276 # self.__bufferList = bufferList
1276 1277 #
1277 1278 # def run(self, dataOut, channel = 0):
1278 1279 #
1279 1280 # if not self.isConfig:
1280 1281 # nSamples = dataOut.nHeights
1281 1282 # nChannels = dataOut.nChannels
1282 1283 # self.setup(nSamples, nChannels)
1283 1284 # self.isConfig = True
1284 1285 #
1285 1286 # #Append new data to internal buffer
1286 1287 # for thisChannel in range(self.__nChannels):
1287 1288 # bufferByChannel = self.__bufferList[thisChannel]
1288 1289 # bufferByChannel.extend(dataOut.data[thisChannel])
1289 1290 #
1290 1291 # if self.__pulseFound:
1291 1292 # self.__startIndex -= self.__nSamples
1292 1293 #
1293 1294 # #Finding Tx Pulse
1294 1295 # if not self.__pulseFound:
1295 1296 # indexFound = self.__findTxPulse(dataOut, channel)
1296 1297 #
1297 1298 # if indexFound == None:
1298 1299 # dataOut.flagNoData = True
1299 1300 # return
1300 1301 #
1301 1302 # self.__arrayBuffer = numpy.zeros((self.__nChannels, self.__newNSamples), dtype = numpy.complex)
1302 1303 # self.__pulseFound = True
1303 1304 # self.__startIndex = indexFound
1304 1305 #
1305 1306 # #If pulse was found ...
1306 1307 # for thisChannel in range(self.__nChannels):
1307 1308 # bufferByChannel = self.__bufferList[thisChannel]
1308 1309 # #print self.__startIndex
1309 1310 # x = numpy.array(bufferByChannel)
1310 1311 # self.__arrayBuffer[thisChannel] = x[self.__startIndex:self.__startIndex+self.__newNSamples]
1311 1312 #
1312 1313 # deltaHeight = dataOut.heightList[1] - dataOut.heightList[0]
1313 1314 # dataOut.heightList = numpy.arange(self.__newNSamples)*deltaHeight
1314 1315 # # dataOut.ippSeconds = (self.__newNSamples / deltaHeight)/1e6
1315 1316 #
1316 1317 # dataOut.data = self.__arrayBuffer
1317 1318 #
1318 1319 # self.__startIndex += self.__newNSamples
1319 1320 #
1320 1321 # return No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now