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