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