##// 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 NO CONTENT: modified file
General Comments 0
You need to be logged in to leave comments. Login now