##// END OF EJS Templates
Change timezone to 18000 seconds equivalent to 5 hours.
Alexander Valdez -
r1239:995b8a8dbcb0
parent child
Show More
@@ -1,803 +1,803
1 1 '''
2 2 Created on Jul 3, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 # SUBCHANNELS EN VEZ DE CHANNELS
7 7 # BENCHMARKS -> PROBLEMAS CON ARCHIVOS GRANDES -> INCONSTANTE EN EL TIEMPO
8 8 # ACTUALIZACION DE VERSION
9 9 # HEADERS
10 10 # MODULO DE ESCRITURA
11 11 # METADATA
12 12
13 13 import os
14 14 import datetime
15 15 import numpy
16 16 import timeit
17 17 from fractions import Fraction
18 18
19 19 try:
20 20 from gevent import sleep
21 21 except:
22 22 from time import sleep
23 23
24 24 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
25 25 from schainpy.model.data.jrodata import Voltage
26 26 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
27 27 from time import time
28 28
29 29 import cPickle
30 30 try:
31 31 import digital_rf
32 32 except:
33 33 print 'You should install "digital_rf" module if you want to read Digital RF data'
34 34
35 35
36 36 class DigitalRFReader(ProcessingUnit):
37 37 '''
38 38 classdocs
39 39 '''
40 40
41 41 def __init__(self, **kwargs):
42 42 '''
43 43 Constructor
44 44 '''
45 45
46 46 ProcessingUnit.__init__(self, **kwargs)
47 47
48 48 self.dataOut = Voltage()
49 49 self.__printInfo = True
50 50 self.__flagDiscontinuousBlock = False
51 51 self.__bufferIndex = 9999999
52 52 self.__ippKm = None
53 53 self.__codeType = 0
54 54 self.__nCode = None
55 55 self.__nBaud = None
56 56 self.__code = None
57 57 self.dtype = None
58 58 self.oldAverage = None
59 59 self.path = None
60 60
61 61 def close(self):
62 62 print 'Average of writing to digital rf format is ', self.oldAverage * 1000
63 63 return
64 64
65 65 def __getCurrentSecond(self):
66 66
67 67 return self.__thisUnixSample / self.__sample_rate
68 68
69 69 thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.")
70 70
71 71 def __setFileHeader(self):
72 72 '''
73 73 In this method will be initialized every parameter of dataOut object (header, no data)
74 74 '''
75 75 ippSeconds = 1.0 * self.__nSamples / self.__sample_rate
76 76
77 77 nProfiles = 1.0 / ippSeconds # Number of profiles in one second
78 78
79 79 try:
80 80 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
81 81 self.__radarControllerHeader)
82 82 except:
83 83 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
84 84 txA=0,
85 85 txB=0,
86 86 nWindows=1,
87 87 nHeights=self.__nSamples,
88 88 firstHeight=self.__firstHeigth,
89 89 deltaHeight=self.__deltaHeigth,
90 90 codeType=self.__codeType,
91 91 nCode=self.__nCode, nBaud=self.__nBaud,
92 92 code=self.__code)
93 93
94 94 try:
95 95 self.dataOut.systemHeaderObj = SystemHeader(self.__systemHeader)
96 96 except:
97 97 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
98 98 nProfiles=nProfiles,
99 99 nChannels=len(
100 100 self.__channelList),
101 101 adcResolution=14)
102 102 self.dataOut.type = "Voltage"
103 103
104 104 self.dataOut.data = None
105 105
106 106 self.dataOut.dtype = self.dtype
107 107
108 108 # self.dataOut.nChannels = 0
109 109
110 110 # self.dataOut.nHeights = 0
111 111
112 112 self.dataOut.nProfiles = int(nProfiles)
113 113
114 114 self.dataOut.heightList = self.__firstHeigth + \
115 115 numpy.arange(self.__nSamples, dtype=numpy.float) * \
116 116 self.__deltaHeigth
117 117
118 118 self.dataOut.channelList = range(self.__num_subchannels)
119 119
120 120 self.dataOut.blocksize = self.dataOut.getNChannels() * self.dataOut.getNHeights()
121 121
122 122 # self.dataOut.channelIndexList = None
123 123
124 124 self.dataOut.flagNoData = True
125 125
126 126 self.dataOut.flagDataAsBlock = False
127 127 # Set to TRUE if the data is discontinuous
128 128 self.dataOut.flagDiscontinuousBlock = False
129 129
130 130 self.dataOut.utctime = None
131 131
132 132 # timezone like jroheader, difference in minutes between UTC and localtime
133 133 self.dataOut.timeZone = self.__timezone / 60
134 134
135 135 self.dataOut.dstFlag = 0
136 136
137 137 self.dataOut.errorCount = 0
138 138
139 139 try:
140 140 self.dataOut.nCohInt = self.fixed_metadata_dict.get(
141 141 'nCohInt', self.nCohInt)
142 142
143 143 # asumo que la data esta decodificada
144 144 self.dataOut.flagDecodeData = self.fixed_metadata_dict.get(
145 145 'flagDecodeData', self.flagDecodeData)
146 146
147 147 # asumo que la data esta sin flip
148 148 self.dataOut.flagDeflipData = self.fixed_metadata_dict['flagDeflipData']
149 149
150 150 self.dataOut.flagShiftFFT = self.fixed_metadata_dict['flagShiftFFT']
151 151
152 152 self.dataOut.useLocalTime = self.fixed_metadata_dict['useLocalTime']
153 153 except:
154 154 pass
155 155
156 156 self.dataOut.ippSeconds = ippSeconds
157 157
158 158 # Time interval between profiles
159 159 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
160 160
161 161 self.dataOut.frequency = self.__frequency
162 162
163 163 self.dataOut.realtime = self.__online
164 164
165 165 def findDatafiles(self, path, startDate=None, endDate=None):
166 166
167 167 if not os.path.isdir(path):
168 168 return []
169 169
170 170 try:
171 171 digitalReadObj = digital_rf.DigitalRFReader(
172 172 path, load_all_metadata=True)
173 173 except:
174 174 digitalReadObj = digital_rf.DigitalRFReader(path)
175 175
176 176 channelNameList = digitalReadObj.get_channels()
177 177
178 178 if not channelNameList:
179 179 return []
180 180
181 181 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
182 182
183 183 sample_rate = metadata_dict['sample_rate'][0]
184 184
185 185 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
186 186
187 187 try:
188 188 timezone = this_metadata_file['timezone'].value
189 189 except:
190 190 timezone = 0
191 191
192 192 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(
193 193 channelNameList[0]) / sample_rate - timezone
194 194
195 195 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
196 196 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
197 197
198 198 if not startDate:
199 199 startDate = startDatetime.date()
200 200
201 201 if not endDate:
202 202 endDate = endDatatime.date()
203 203
204 204 dateList = []
205 205
206 206 thisDatetime = startDatetime
207 207
208 208 while(thisDatetime <= endDatatime):
209 209
210 210 thisDate = thisDatetime.date()
211 211
212 212 if thisDate < startDate:
213 213 continue
214 214
215 215 if thisDate > endDate:
216 216 break
217 217
218 218 dateList.append(thisDate)
219 219 thisDatetime += datetime.timedelta(1)
220 220
221 221 return dateList
222 222
223 223 def setup(self, path=None,
224 224 startDate=None,
225 225 endDate=None,
226 226 startTime=datetime.time(0, 0, 0),
227 227 endTime=datetime.time(23, 59, 59),
228 228 channelList=None,
229 229 nSamples=None,
230 230 online=False,
231 231 delay=60,
232 232 buffer_size=1024,
233 233 ippKm=None,
234 234 nCohInt=1,
235 235 nCode=1,
236 236 nBaud=1,
237 237 flagDecodeData=False,
238 238 code=numpy.ones((1, 1), dtype=numpy.int),
239 239 **kwargs):
240 240 '''
241 241 In this method we should set all initial parameters.
242 242
243 243 Inputs:
244 244 path
245 245 startDate
246 246 endDate
247 247 startTime
248 248 endTime
249 249 set
250 250 expLabel
251 251 ext
252 252 online
253 253 delay
254 254 '''
255 255 self.path = path
256 256 self.nCohInt = nCohInt
257 257 self.flagDecodeData = flagDecodeData
258 258 self.i = 0
259 259 if not os.path.isdir(path):
260 260 raise ValueError, "[Reading] Directory %s does not exist" % path
261 261
262 262 try:
263 263 self.digitalReadObj = digital_rf.DigitalRFReader(
264 264 path, load_all_metadata=True)
265 265 except:
266 266 self.digitalReadObj = digital_rf.DigitalRFReader(path)
267 267
268 268 channelNameList = self.digitalReadObj.get_channels()
269 269
270 270 if not channelNameList:
271 271 raise ValueError, "[Reading] Directory %s does not have any files" % path
272 272
273 273 if not channelList:
274 274 channelList = range(len(channelNameList))
275 275
276 276 ########## Reading metadata ######################
277 277
278 278 top_properties = self.digitalReadObj.get_properties(
279 279 channelNameList[channelList[0]])
280 280
281 281 self.__num_subchannels = top_properties['num_subchannels']
282 282 self.__sample_rate = 1.0 * \
283 283 top_properties['sample_rate_numerator'] / \
284 284 top_properties['sample_rate_denominator']
285 285 # self.__samples_per_file = top_properties['samples_per_file'][0]
286 286 self.__deltaHeigth = 1e6 * 0.15 / self.__sample_rate # why 0.15?
287 287
288 288 this_metadata_file = self.digitalReadObj.get_digital_metadata(
289 289 channelNameList[channelList[0]])
290 290 metadata_bounds = this_metadata_file.get_bounds()
291 291 self.fixed_metadata_dict = this_metadata_file.read(
292 292 metadata_bounds[0])[metadata_bounds[0]] # GET FIRST HEADER
293 293
294 294 try:
295 295 self.__processingHeader = self.fixed_metadata_dict['processingHeader']
296 296 self.__radarControllerHeader = self.fixed_metadata_dict['radarControllerHeader']
297 297 self.__systemHeader = self.fixed_metadata_dict['systemHeader']
298 298 self.dtype = cPickle.loads(self.fixed_metadata_dict['dtype'])
299 299 except:
300 300 pass
301 301
302 302 self.__frequency = None
303 303
304 304 self.__frequency = self.fixed_metadata_dict.get('frequency', 1)
305 305
306 self.__timezone = self.fixed_metadata_dict.get('timezone', 300)
306 self.__timezone = self.fixed_metadata_dict.get('timezone', 18000)
307 307
308 308 try:
309 309 nSamples = self.fixed_metadata_dict['nSamples']
310 310 except:
311 311 nSamples = None
312 312
313 313 self.__firstHeigth = 0
314 314
315 315 try:
316 316 codeType = self.__radarControllerHeader['codeType']
317 317 except:
318 318 codeType = 0
319 319
320 320 try:
321 321 if codeType:
322 322 nCode = self.__radarControllerHeader['nCode']
323 323 nBaud = self.__radarControllerHeader['nBaud']
324 324 code = self.__radarControllerHeader['code']
325 325 except:
326 326 pass
327 327
328 328 if not ippKm:
329 329 try:
330 330 # seconds to km
331 331 ippKm = self.__radarControllerHeader['ipp']
332 332 except:
333 333 ippKm = None
334 334 ####################################################
335 335 self.__ippKm = ippKm
336 336 startUTCSecond = None
337 337 endUTCSecond = None
338 338
339 339 if startDate:
340 340 startDatetime = datetime.datetime.combine(startDate, startTime)
341 341 startUTCSecond = (
342 342 startDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone
343 343
344 344 if endDate:
345 345 endDatetime = datetime.datetime.combine(endDate, endTime)
346 346 endUTCSecond = (endDatetime - datetime.datetime(1970,
347 347 1, 1)).total_seconds() + self.__timezone
348 348
349 349 start_index, end_index = self.digitalReadObj.get_bounds(
350 350 channelNameList[channelList[0]])
351 351
352 352 if not startUTCSecond:
353 353 startUTCSecond = start_index / self.__sample_rate
354 354
355 355 if start_index > startUTCSecond * self.__sample_rate:
356 356 startUTCSecond = start_index / self.__sample_rate
357 357
358 358 if not endUTCSecond:
359 359 endUTCSecond = end_index / self.__sample_rate
360 360
361 361 if end_index < endUTCSecond * self.__sample_rate:
362 362 endUTCSecond = end_index / self.__sample_rate
363 363 if not nSamples:
364 364 if not ippKm:
365 365 raise ValueError, "[Reading] nSamples or ippKm should be defined"
366 366 nSamples = int(ippKm / (1e6 * 0.15 / self.__sample_rate))
367 367 channelBoundList = []
368 368 channelNameListFiltered = []
369 369
370 370 for thisIndexChannel in channelList:
371 371 thisChannelName = channelNameList[thisIndexChannel]
372 372 start_index, end_index = self.digitalReadObj.get_bounds(
373 373 thisChannelName)
374 374 channelBoundList.append((start_index, end_index))
375 375 channelNameListFiltered.append(thisChannelName)
376 376
377 377 self.profileIndex = 0
378 378 self.i = 0
379 379 self.__delay = delay
380 380
381 381 self.__codeType = codeType
382 382 self.__nCode = nCode
383 383 self.__nBaud = nBaud
384 384 self.__code = code
385 385
386 386 self.__datapath = path
387 387 self.__online = online
388 388 self.__channelList = channelList
389 389 self.__channelNameList = channelNameListFiltered
390 390 self.__channelBoundList = channelBoundList
391 391 self.__nSamples = nSamples
392 392 self.__samples_to_read = long(nSamples) # FIJO: AHORA 40
393 393 self.__nChannels = len(self.__channelList)
394 394
395 395 self.__startUTCSecond = startUTCSecond
396 396 self.__endUTCSecond = endUTCSecond
397 397
398 398 self.__timeInterval = 1.0 * self.__samples_to_read / \
399 399 self.__sample_rate # Time interval
400 400
401 401 if online:
402 402 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
403 403 startUTCSecond = numpy.floor(endUTCSecond)
404 404
405 405 # por que en el otro metodo lo primero q se hace es sumar samplestoread
406 406 self.__thisUnixSample = long(
407 407 startUTCSecond * self.__sample_rate) - self.__samples_to_read
408 408
409 409 self.__data_buffer = numpy.zeros(
410 410 (self.__num_subchannels, self.__samples_to_read), dtype=numpy.complex)
411 411
412 412 self.__setFileHeader()
413 413 self.isConfig = True
414 414
415 415 print "[Reading] Digital RF Data was found from %s to %s " % (
416 416 datetime.datetime.utcfromtimestamp(
417 417 self.__startUTCSecond - self.__timezone),
418 418 datetime.datetime.utcfromtimestamp(
419 419 self.__endUTCSecond - self.__timezone)
420 420 )
421 421
422 422 print "[Reading] Starting process from %s to %s" % (datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
423 423 datetime.datetime.utcfromtimestamp(
424 424 endUTCSecond - self.__timezone)
425 425 )
426 426 self.oldAverage = None
427 427 self.count = 0
428 428 self.executionTime = 0
429 429
430 430 def __reload(self):
431 431 # print
432 432 # print "%s not in range [%s, %s]" %(
433 433 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
434 434 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
435 435 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
436 436 # )
437 437 print "[Reading] reloading metadata ..."
438 438
439 439 try:
440 440 self.digitalReadObj.reload(complete_update=True)
441 441 except:
442 442 self.digitalReadObj = digital_rf.DigitalRFReader(self.path)
443 443 #self.digitalReadObj.reload()
444 444
445 445 start_index, end_index = self.digitalReadObj.get_bounds(
446 446 self.__channelNameList[self.__channelList[0]])
447 447
448 448 if start_index > self.__startUTCSecond * self.__sample_rate:
449 449 self.__startUTCSecond = 1.0 * start_index / self.__sample_rate
450 450
451 451 if end_index > self.__endUTCSecond * self.__sample_rate:
452 452 self.__endUTCSecond = 1.0 * end_index / self.__sample_rate
453 453 print
454 454 print "[Reading] New timerange found [%s, %s] " % (
455 455 datetime.datetime.utcfromtimestamp(
456 456 self.__startUTCSecond - self.__timezone),
457 457 datetime.datetime.utcfromtimestamp(
458 458 self.__endUTCSecond - self.__timezone)
459 459 )
460 460
461 461 return True
462 462
463 463 return False
464 464
465 465 def timeit(self, toExecute):
466 466 t0 = time()
467 467 toExecute()
468 468 self.executionTime = time() - t0
469 469 if self.oldAverage is None:
470 470 self.oldAverage = self.executionTime
471 471 self.oldAverage = (self.executionTime + self.count *
472 472 self.oldAverage) / (self.count + 1.0)
473 473 self.count = self.count + 1.0
474 474 return
475 475
476 476 def __readNextBlock(self, seconds=30, volt_scale=1):
477 477 '''
478 478 '''
479 479
480 480 # Set the next data
481 481 self.__flagDiscontinuousBlock = False
482 482 self.__thisUnixSample += self.__samples_to_read
483 483
484 484 if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate:
485 485 print "[Reading] There are no more data into selected time-range"
486 486 if self.__online:
487 487 sleep(3)
488 488 self.__reload()
489 489 else:
490 490 return False
491 491
492 492 if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate:
493 493 return False
494 494 self.__thisUnixSample -= self.__samples_to_read
495 495
496 496 indexChannel = 0
497 497
498 498 dataOk = False
499 499 for thisChannelName in self.__channelNameList: # TODO VARIOS CHANNELS?
500 500 for indexSubchannel in range(self.__num_subchannels):
501 501 try:
502 502 t0 = time()
503 503 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
504 504 self.__samples_to_read,
505 505 thisChannelName, sub_channel=indexSubchannel)
506 506 self.executionTime = time() - t0
507 507 if self.oldAverage is None:
508 508 self.oldAverage = self.executionTime
509 509 self.oldAverage = (
510 510 self.executionTime + self.count * self.oldAverage) / (self.count + 1.0)
511 511 self.count = self.count + 1.0
512 512
513 513 except IOError, e:
514 514 # read next profile
515 515 self.__flagDiscontinuousBlock = True
516 516 print "[Reading] %s" % datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e
517 517 break
518 518
519 519 if result.shape[0] != self.__samples_to_read:
520 520 self.__flagDiscontinuousBlock = True
521 521 print "[Reading] %s: Too few samples were found, just %d/%d samples" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
522 522 result.shape[0],
523 523 self.__samples_to_read)
524 524 break
525 525
526 526 self.__data_buffer[indexSubchannel, :] = result * volt_scale
527 527
528 528 indexChannel += 1
529 529
530 530 dataOk = True
531 531
532 532 self.__utctime = self.__thisUnixSample / self.__sample_rate
533 533
534 534 if not dataOk:
535 535 return False
536 536
537 537 print "[Reading] %s: %d samples <> %f sec" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
538 538 self.__samples_to_read,
539 539 self.__timeInterval)
540 540
541 541 self.__bufferIndex = 0
542 542
543 543 return True
544 544
545 545 def __isBufferEmpty(self):
546 546 return self.__bufferIndex > self.__samples_to_read - self.__nSamples # 40960 - 40
547 547
548 548 def getData(self, seconds=30, nTries=5):
549 549 '''
550 550 This method gets the data from files and put the data into the dataOut object
551 551
552 552 In addition, increase el the buffer counter in one.
553 553
554 554 Return:
555 555 data : retorna un perfil de voltages (alturas * canales) copiados desde el
556 556 buffer. Si no hay mas archivos a leer retorna None.
557 557
558 558 Affected:
559 559 self.dataOut
560 560 self.profileIndex
561 561 self.flagDiscontinuousBlock
562 562 self.flagIsNewBlock
563 563 '''
564 564
565 565 err_counter = 0
566 566 self.dataOut.flagNoData = True
567 567
568 568 if self.__isBufferEmpty():
569 569 self.__flagDiscontinuousBlock = False
570 570
571 571 while True:
572 572 if self.__readNextBlock():
573 573 break
574 574 if self.__thisUnixSample > self.__endUTCSecond * self.__sample_rate:
575 575 return False
576 576
577 577 if self.__flagDiscontinuousBlock:
578 578 print '[Reading] discontinuous block found ... continue with the next block'
579 579 continue
580 580
581 581 if not self.__online:
582 582 return False
583 583
584 584 err_counter += 1
585 585 if err_counter > nTries:
586 586 return False
587 587
588 588 print '[Reading] waiting %d seconds to read a new block' % seconds
589 589 sleep(seconds)
590 590
591 591 self.dataOut.data = self.__data_buffer[:,
592 592 self.__bufferIndex:self.__bufferIndex + self.__nSamples]
593 593 self.dataOut.utctime = (
594 594 self.__thisUnixSample + self.__bufferIndex) / self.__sample_rate
595 595 self.dataOut.flagNoData = False
596 596 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
597 597 self.dataOut.profileIndex = self.profileIndex
598 598
599 599 self.__bufferIndex += self.__nSamples
600 600 self.profileIndex += 1
601 601
602 602 if self.profileIndex == self.dataOut.nProfiles:
603 603 self.profileIndex = 0
604 604
605 605 return True
606 606
607 607 def printInfo(self):
608 608 '''
609 609 '''
610 610 if self.__printInfo == False:
611 611 return
612 612
613 613 # self.systemHeaderObj.printInfo()
614 614 # self.radarControllerHeaderObj.printInfo()
615 615
616 616 self.__printInfo = False
617 617
618 618 def printNumberOfBlock(self):
619 619 '''
620 620 '''
621 621 return
622 622 # print self.profileIndex
623 623
624 624 def run(self, **kwargs):
625 625 '''
626 626 This method will be called many times so here you should put all your code
627 627 '''
628 628
629 629 if not self.isConfig:
630 630 self.setup(**kwargs)
631 631 #self.i = self.i+1
632 632 self.getData(seconds=self.__delay)
633 633
634 634 return
635 635
636 636
637 637 class DigitalRFWriter(Operation):
638 638 '''
639 639 classdocs
640 640 '''
641 641
642 642 def __init__(self, **kwargs):
643 643 '''
644 644 Constructor
645 645 '''
646 646 Operation.__init__(self, **kwargs)
647 647 self.metadata_dict = {}
648 648 self.dataOut = None
649 649 self.dtype = None
650 650 self.oldAverage = 0
651 651
652 652 def setHeader(self):
653 653
654 654 self.metadata_dict['frequency'] = self.dataOut.frequency
655 655 self.metadata_dict['timezone'] = self.dataOut.timeZone
656 656 self.metadata_dict['dtype'] = cPickle.dumps(self.dataOut.dtype)
657 657 self.metadata_dict['nProfiles'] = self.dataOut.nProfiles
658 658 self.metadata_dict['heightList'] = self.dataOut.heightList
659 659 self.metadata_dict['channelList'] = self.dataOut.channelList
660 660 self.metadata_dict['flagDecodeData'] = self.dataOut.flagDecodeData
661 661 self.metadata_dict['flagDeflipData'] = self.dataOut.flagDeflipData
662 662 self.metadata_dict['flagShiftFFT'] = self.dataOut.flagShiftFFT
663 663 self.metadata_dict['useLocalTime'] = self.dataOut.useLocalTime
664 664 self.metadata_dict['nCohInt'] = self.dataOut.nCohInt
665 665 self.metadata_dict['type'] = self.dataOut.type
666 666 self.metadata_dict['flagDataAsBlock'] = getattr(
667 667 self.dataOut, 'flagDataAsBlock', None) # chequear
668 668
669 669 def setup(self, dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, set=0, metadataFile='metadata', ext='.h5'):
670 670 '''
671 671 In this method we should set all initial parameters.
672 672 Input:
673 673 dataOut: Input data will also be outputa data
674 674 '''
675 675 self.setHeader()
676 676 self.__ippSeconds = dataOut.ippSeconds
677 677 self.__deltaH = dataOut.getDeltaH()
678 678 self.__sample_rate = 1e6 * 0.15 / self.__deltaH
679 679 self.__dtype = dataOut.dtype
680 680 if len(dataOut.dtype) == 2:
681 681 self.__dtype = dataOut.dtype[0]
682 682 self.__nSamples = dataOut.systemHeaderObj.nSamples
683 683 self.__nProfiles = dataOut.nProfiles
684 684
685 685 if self.dataOut.type != 'Voltage':
686 686 raise 'Digital RF cannot be used with this data type'
687 687 self.arr_data = numpy.ones((1, dataOut.nFFTPoints * len(
688 688 self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)])
689 689 else:
690 690 self.arr_data = numpy.ones((self.__nSamples, len(
691 691 self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)])
692 692
693 693 file_cadence_millisecs = 1000
694 694
695 695 sample_rate_fraction = Fraction(self.__sample_rate).limit_denominator()
696 696 sample_rate_numerator = long(sample_rate_fraction.numerator)
697 697 sample_rate_denominator = long(sample_rate_fraction.denominator)
698 698 start_global_index = dataOut.utctime * self.__sample_rate
699 699
700 700 uuid = 'prueba'
701 701 compression_level = 0
702 702 checksum = False
703 703 is_complex = True
704 704 num_subchannels = len(dataOut.channelList)
705 705 is_continuous = True
706 706 marching_periods = False
707 707
708 708 self.digitalWriteObj = digital_rf.DigitalRFWriter(path, self.__dtype, dirCadence,
709 709 fileCadence, start_global_index,
710 710 sample_rate_numerator, sample_rate_denominator, uuid, compression_level, checksum,
711 711 is_complex, num_subchannels, is_continuous, marching_periods)
712 712 metadata_dir = os.path.join(path, 'metadata')
713 713 os.system('mkdir %s' % (metadata_dir))
714 714 self.digitalMetadataWriteObj = digital_rf.DigitalMetadataWriter(metadata_dir, dirCadence, 1, # 236, file_cadence_millisecs / 1000
715 715 sample_rate_numerator, sample_rate_denominator,
716 716 metadataFile)
717 717 self.isConfig = True
718 718 self.currentSample = 0
719 719 self.oldAverage = 0
720 720 self.count = 0
721 721 return
722 722
723 723 def writeMetadata(self):
724 724 start_idx = self.__sample_rate * self.dataOut.utctime
725 725
726 726 self.metadata_dict['processingHeader'] = self.dataOut.processingHeaderObj.getAsDict(
727 727 )
728 728 self.metadata_dict['radarControllerHeader'] = self.dataOut.radarControllerHeaderObj.getAsDict(
729 729 )
730 730 self.metadata_dict['systemHeader'] = self.dataOut.systemHeaderObj.getAsDict(
731 731 )
732 732 self.digitalMetadataWriteObj.write(start_idx, self.metadata_dict)
733 733 return
734 734
735 735 def timeit(self, toExecute):
736 736 t0 = time()
737 737 toExecute()
738 738 self.executionTime = time() - t0
739 739 if self.oldAverage is None:
740 740 self.oldAverage = self.executionTime
741 741 self.oldAverage = (self.executionTime + self.count *
742 742 self.oldAverage) / (self.count + 1.0)
743 743 self.count = self.count + 1.0
744 744 return
745 745
746 746 def writeData(self):
747 747 if self.dataOut.type != 'Voltage':
748 748 raise 'Digital RF cannot be used with this data type'
749 749 for channel in self.dataOut.channelList:
750 750 for i in range(self.dataOut.nFFTPoints):
751 751 self.arr_data[1][channel * self.dataOut.nFFTPoints +
752 752 i]['r'] = self.dataOut.data[channel][i].real
753 753 self.arr_data[1][channel * self.dataOut.nFFTPoints +
754 754 i]['i'] = self.dataOut.data[channel][i].imag
755 755 else:
756 756 for i in range(self.dataOut.systemHeaderObj.nSamples):
757 757 for channel in self.dataOut.channelList:
758 758 self.arr_data[i][channel]['r'] = self.dataOut.data[channel][i].real
759 759 self.arr_data[i][channel]['i'] = self.dataOut.data[channel][i].imag
760 760
761 761 def f(): return self.digitalWriteObj.rf_write(self.arr_data)
762 762 self.timeit(f)
763 763
764 764 return
765 765
766 766 def run(self, dataOut, frequency=49.92e6, path=None, fileCadence=1000, dirCadence=36000, metadataCadence=1, **kwargs):
767 767 '''
768 768 This method will be called many times so here you should put all your code
769 769 Inputs:
770 770 dataOut: object with the data
771 771 '''
772 772 # print dataOut.__dict__
773 773 self.dataOut = dataOut
774 774 if not self.isConfig:
775 775 self.setup(dataOut, path, frequency, fileCadence,
776 776 dirCadence, metadataCadence, **kwargs)
777 777 self.writeMetadata()
778 778
779 779 self.writeData()
780 780
781 781 ## self.currentSample += 1
782 782 # if self.dataOut.flagDataAsBlock or self.currentSample == 1:
783 783 # self.writeMetadata()
784 784 ## if self.currentSample == self.__nProfiles: self.currentSample = 0
785 785
786 786 def close(self):
787 787 print '[Writing] - Closing files '
788 788 print 'Average of writing to digital rf format is ', self.oldAverage * 1000
789 789 try:
790 790 self.digitalWriteObj.close()
791 791 except:
792 792 pass
793 793
794 794
795 795 # raise
796 796 if __name__ == '__main__':
797 797
798 798 readObj = DigitalRFReader()
799 799
800 800 while True:
801 801 readObj.run(path='/home/jchavez/jicamarca/mocked_data/')
802 802 # readObj.printInfo()
803 803 # readObj.printNumberOfBlock()
General Comments 0
You need to be logged in to leave comments. Login now