@@ -1,800 +1,803 | |||
|
1 | ||
|
2 | 1 |
|
|
3 | 2 | Created on Jul 3, 2014 |
|
4 | 3 | |
|
5 | 4 | @author: roj-idl71 |
|
6 | 5 | ''' |
|
7 | 6 | # SUBCHANNELS EN VEZ DE CHANNELS |
|
8 | 7 | # BENCHMARKS -> PROBLEMAS CON ARCHIVOS GRANDES -> INCONSTANTE EN EL TIEMPO |
|
9 | 8 | # ACTUALIZACION DE VERSION |
|
10 | 9 | # HEADERS |
|
11 | 10 | # MODULO DE ESCRITURA |
|
12 | 11 | # METADATA |
|
13 | 12 | |
|
14 | 13 | import os |
|
15 | 14 | import datetime |
|
16 | 15 | import numpy |
|
17 | 16 | import timeit |
|
18 | 17 | from fractions import Fraction |
|
19 | 18 | |
|
20 | 19 | try: |
|
21 | 20 | from gevent import sleep |
|
22 | 21 | except: |
|
23 | 22 | from time import sleep |
|
24 | 23 | |
|
25 | 24 | from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader |
|
26 | 25 | from schainpy.model.data.jrodata import Voltage |
|
27 | 26 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation |
|
28 | 27 | from time import time |
|
29 | 28 | |
|
30 | 29 | import cPickle |
|
31 | 30 | try: |
|
32 | 31 | import digital_rf |
|
33 | 32 | except: |
|
34 | 33 | print 'You should install "digital_rf" module if you want to read Digital RF data' |
|
35 | 34 | |
|
36 | 35 | |
|
37 | 36 | class DigitalRFReader(ProcessingUnit): |
|
38 | 37 | ''' |
|
39 | 38 | classdocs |
|
40 | 39 | ''' |
|
41 | 40 | |
|
42 | 41 | def __init__(self, **kwargs): |
|
43 | 42 | ''' |
|
44 | 43 | Constructor |
|
45 | 44 | ''' |
|
46 | 45 | |
|
47 | 46 | ProcessingUnit.__init__(self, **kwargs) |
|
48 | 47 | |
|
49 | 48 | self.dataOut = Voltage() |
|
50 | 49 | self.__printInfo = True |
|
51 | 50 | self.__flagDiscontinuousBlock = False |
|
52 | 51 | self.__bufferIndex = 9999999 |
|
53 | 52 | self.__ippKm = None |
|
54 | 53 | self.__codeType = 0 |
|
55 | 54 | self.__nCode = None |
|
56 | 55 | self.__nBaud = None |
|
57 | 56 | self.__code = None |
|
58 | 57 | self.dtype = None |
|
59 | 58 | self.oldAverage = None |
|
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 | self.path = path | |
|
255 | 256 | self.nCohInt = nCohInt |
|
256 | 257 | self.flagDecodeData = flagDecodeData |
|
257 | 258 | self.i = 0 |
|
258 | 259 | if not os.path.isdir(path): |
|
259 | 260 | raise ValueError, "[Reading] Directory %s does not exist" % path |
|
260 | 261 | |
|
261 | 262 | try: |
|
262 | 263 | self.digitalReadObj = digital_rf.DigitalRFReader( |
|
263 | 264 | path, load_all_metadata=True) |
|
264 | 265 | except: |
|
265 | 266 | self.digitalReadObj = digital_rf.DigitalRFReader(path) |
|
266 | 267 | |
|
267 | 268 | channelNameList = self.digitalReadObj.get_channels() |
|
268 | 269 | |
|
269 | 270 | if not channelNameList: |
|
270 | 271 | raise ValueError, "[Reading] Directory %s does not have any files" % path |
|
271 | 272 | |
|
272 | 273 | if not channelList: |
|
273 | 274 | channelList = range(len(channelNameList)) |
|
274 | 275 | |
|
275 | 276 | ########## Reading metadata ###################### |
|
276 | 277 | |
|
277 | 278 | top_properties = self.digitalReadObj.get_properties( |
|
278 | 279 | channelNameList[channelList[0]]) |
|
279 | 280 | |
|
280 | 281 | self.__num_subchannels = top_properties['num_subchannels'] |
|
281 | 282 | self.__sample_rate = 1.0 * \ |
|
282 | 283 | top_properties['sample_rate_numerator'] / \ |
|
283 | 284 | top_properties['sample_rate_denominator'] |
|
284 | 285 | # self.__samples_per_file = top_properties['samples_per_file'][0] |
|
285 | 286 | self.__deltaHeigth = 1e6 * 0.15 / self.__sample_rate # why 0.15? |
|
286 | 287 | |
|
287 | 288 | this_metadata_file = self.digitalReadObj.get_digital_metadata( |
|
288 | 289 | channelNameList[channelList[0]]) |
|
289 | 290 | metadata_bounds = this_metadata_file.get_bounds() |
|
290 | 291 | self.fixed_metadata_dict = this_metadata_file.read( |
|
291 | 292 | metadata_bounds[0])[metadata_bounds[0]] # GET FIRST HEADER |
|
292 | 293 | |
|
293 | 294 | try: |
|
294 | 295 | self.__processingHeader = self.fixed_metadata_dict['processingHeader'] |
|
295 | 296 | self.__radarControllerHeader = self.fixed_metadata_dict['radarControllerHeader'] |
|
296 | 297 | self.__systemHeader = self.fixed_metadata_dict['systemHeader'] |
|
297 | 298 | self.dtype = cPickle.loads(self.fixed_metadata_dict['dtype']) |
|
298 | 299 | except: |
|
299 | 300 | pass |
|
300 | 301 | |
|
301 | 302 | self.__frequency = None |
|
302 | 303 | |
|
303 | 304 | self.__frequency = self.fixed_metadata_dict.get('frequency', 1) |
|
304 | 305 | |
|
305 | 306 | self.__timezone = self.fixed_metadata_dict.get('timezone', 300) |
|
306 | 307 | |
|
307 | 308 | try: |
|
308 | 309 | nSamples = self.fixed_metadata_dict['nSamples'] |
|
309 | 310 | except: |
|
310 | 311 | nSamples = None |
|
311 | 312 | |
|
312 | 313 | self.__firstHeigth = 0 |
|
313 | 314 | |
|
314 | 315 | try: |
|
315 | 316 | codeType = self.__radarControllerHeader['codeType'] |
|
316 | 317 | except: |
|
317 | 318 | codeType = 0 |
|
318 | 319 | |
|
319 | 320 | try: |
|
320 | 321 | if codeType: |
|
321 | 322 | nCode = self.__radarControllerHeader['nCode'] |
|
322 | 323 | nBaud = self.__radarControllerHeader['nBaud'] |
|
323 | 324 | code = self.__radarControllerHeader['code'] |
|
324 | 325 | except: |
|
325 | 326 | pass |
|
326 | 327 | |
|
327 | 328 | if not ippKm: |
|
328 | 329 | try: |
|
329 | 330 | # seconds to km |
|
330 | 331 | ippKm = self.__radarControllerHeader['ipp'] |
|
331 | 332 | except: |
|
332 | 333 | ippKm = None |
|
333 | 334 | #################################################### |
|
334 | 335 | self.__ippKm = ippKm |
|
335 | 336 | startUTCSecond = None |
|
336 | 337 | endUTCSecond = None |
|
337 | 338 | |
|
338 | 339 | if startDate: |
|
339 | 340 | startDatetime = datetime.datetime.combine(startDate, startTime) |
|
340 | 341 | startUTCSecond = ( |
|
341 | 342 | startDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone |
|
342 | 343 | |
|
343 | 344 | if endDate: |
|
344 | 345 | endDatetime = datetime.datetime.combine(endDate, endTime) |
|
345 | 346 | endUTCSecond = (endDatetime - datetime.datetime(1970, |
|
346 | 347 | 1, 1)).total_seconds() + self.__timezone |
|
347 | 348 | |
|
348 | 349 | start_index, end_index = self.digitalReadObj.get_bounds( |
|
349 | 350 | channelNameList[channelList[0]]) |
|
350 | 351 | |
|
351 | 352 | if not startUTCSecond: |
|
352 | 353 | startUTCSecond = start_index / self.__sample_rate |
|
353 | 354 | |
|
354 | 355 | if start_index > startUTCSecond * self.__sample_rate: |
|
355 | 356 | startUTCSecond = start_index / self.__sample_rate |
|
356 | 357 | |
|
357 | 358 | if not endUTCSecond: |
|
358 | 359 | endUTCSecond = end_index / self.__sample_rate |
|
359 | 360 | |
|
360 | 361 | if end_index < endUTCSecond * self.__sample_rate: |
|
361 | 362 | endUTCSecond = end_index / self.__sample_rate |
|
362 | 363 | if not nSamples: |
|
363 | 364 | if not ippKm: |
|
364 | 365 | raise ValueError, "[Reading] nSamples or ippKm should be defined" |
|
365 | 366 | nSamples = int(ippKm / (1e6 * 0.15 / self.__sample_rate)) |
|
366 | 367 | channelBoundList = [] |
|
367 | 368 | channelNameListFiltered = [] |
|
368 | 369 | |
|
369 | 370 | for thisIndexChannel in channelList: |
|
370 | 371 | thisChannelName = channelNameList[thisIndexChannel] |
|
371 | 372 | start_index, end_index = self.digitalReadObj.get_bounds( |
|
372 | 373 | thisChannelName) |
|
373 | 374 | channelBoundList.append((start_index, end_index)) |
|
374 | 375 | channelNameListFiltered.append(thisChannelName) |
|
375 | 376 | |
|
376 | 377 | self.profileIndex = 0 |
|
377 | 378 | self.i = 0 |
|
378 | 379 | self.__delay = delay |
|
379 | 380 | |
|
380 | 381 | self.__codeType = codeType |
|
381 | 382 | self.__nCode = nCode |
|
382 | 383 | self.__nBaud = nBaud |
|
383 | 384 | self.__code = code |
|
384 | 385 | |
|
385 | 386 | self.__datapath = path |
|
386 | 387 | self.__online = online |
|
387 | 388 | self.__channelList = channelList |
|
388 | 389 | self.__channelNameList = channelNameListFiltered |
|
389 | 390 | self.__channelBoundList = channelBoundList |
|
390 | 391 | self.__nSamples = nSamples |
|
391 | 392 | self.__samples_to_read = long(nSamples) # FIJO: AHORA 40 |
|
392 | 393 | self.__nChannels = len(self.__channelList) |
|
393 | 394 | |
|
394 | 395 | self.__startUTCSecond = startUTCSecond |
|
395 | 396 | self.__endUTCSecond = endUTCSecond |
|
396 | 397 | |
|
397 | 398 | self.__timeInterval = 1.0 * self.__samples_to_read / \ |
|
398 | 399 | self.__sample_rate # Time interval |
|
399 | 400 | |
|
400 | 401 | if online: |
|
401 | 402 | # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read) |
|
402 | 403 | startUTCSecond = numpy.floor(endUTCSecond) |
|
403 | 404 | |
|
404 | 405 | # por que en el otro metodo lo primero q se hace es sumar samplestoread |
|
405 | 406 | self.__thisUnixSample = long( |
|
406 | 407 | startUTCSecond * self.__sample_rate) - self.__samples_to_read |
|
407 | 408 | |
|
408 | 409 | self.__data_buffer = numpy.zeros( |
|
409 | 410 | (self.__num_subchannels, self.__samples_to_read), dtype=numpy.complex) |
|
410 | 411 | |
|
411 | 412 | self.__setFileHeader() |
|
412 | 413 | self.isConfig = True |
|
413 | 414 | |
|
414 | 415 | print "[Reading] Digital RF Data was found from %s to %s " % ( |
|
415 | 416 | datetime.datetime.utcfromtimestamp( |
|
416 | 417 | self.__startUTCSecond - self.__timezone), |
|
417 | 418 | datetime.datetime.utcfromtimestamp( |
|
418 | 419 | self.__endUTCSecond - self.__timezone) |
|
419 | 420 | ) |
|
420 | 421 | |
|
421 | 422 | print "[Reading] Starting process from %s to %s" % (datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone), |
|
422 | 423 | datetime.datetime.utcfromtimestamp( |
|
423 | 424 | endUTCSecond - self.__timezone) |
|
424 | 425 | ) |
|
425 | 426 | self.oldAverage = None |
|
426 | 427 | self.count = 0 |
|
427 | 428 | self.executionTime = 0 |
|
428 | 429 | |
|
429 | 430 | def __reload(self): |
|
430 | 431 | |
|
431 | 432 | # print "%s not in range [%s, %s]" %( |
|
432 | 433 | # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), |
|
433 | 434 | # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone), |
|
434 | 435 | # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone) |
|
435 | 436 | # ) |
|
436 | 437 | print "[Reading] reloading metadata ..." |
|
437 | 438 | |
|
438 | 439 | try: |
|
439 | 440 | self.digitalReadObj.reload(complete_update=True) |
|
440 | 441 | except: |
|
441 |
self.digitalReadObj. |
|
|
442 | self.digitalReadObj = digital_rf.DigitalRFReader(self.path) | |
|
443 | #self.digitalReadObj.reload() | |
|
442 | 444 | |
|
443 | 445 | start_index, end_index = self.digitalReadObj.get_bounds( |
|
444 | 446 | self.__channelNameList[self.__channelList[0]]) |
|
445 | 447 | |
|
446 | 448 | if start_index > self.__startUTCSecond * self.__sample_rate: |
|
447 | 449 | self.__startUTCSecond = 1.0 * start_index / self.__sample_rate |
|
448 | 450 | |
|
449 | 451 | if end_index > self.__endUTCSecond * self.__sample_rate: |
|
450 | 452 | self.__endUTCSecond = 1.0 * end_index / self.__sample_rate |
|
451 | 453 | |
|
452 | 454 | print "[Reading] New timerange found [%s, %s] " % ( |
|
453 | 455 | datetime.datetime.utcfromtimestamp( |
|
454 | 456 | self.__startUTCSecond - self.__timezone), |
|
455 | 457 | datetime.datetime.utcfromtimestamp( |
|
456 | 458 | self.__endUTCSecond - self.__timezone) |
|
457 | 459 | ) |
|
458 | 460 | |
|
459 | 461 | return True |
|
460 | 462 | |
|
461 | 463 | return False |
|
462 | 464 | |
|
463 | 465 | def timeit(self, toExecute): |
|
464 | 466 | t0 = time() |
|
465 | 467 | toExecute() |
|
466 | 468 | self.executionTime = time() - t0 |
|
467 | 469 | if self.oldAverage is None: |
|
468 | 470 | self.oldAverage = self.executionTime |
|
469 | 471 | self.oldAverage = (self.executionTime + self.count * |
|
470 | 472 | self.oldAverage) / (self.count + 1.0) |
|
471 | 473 | self.count = self.count + 1.0 |
|
472 | 474 | return |
|
473 | 475 | |
|
474 | 476 | def __readNextBlock(self, seconds=30, volt_scale=1): |
|
475 | 477 | ''' |
|
476 | 478 | ''' |
|
477 | 479 | |
|
478 | 480 | # Set the next data |
|
479 | 481 | self.__flagDiscontinuousBlock = False |
|
480 | 482 | self.__thisUnixSample += self.__samples_to_read |
|
481 | 483 | |
|
482 | 484 | if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate: |
|
483 | 485 | print "[Reading] There are no more data into selected time-range" |
|
484 | 486 | if self.__online: |
|
487 | sleep(3) | |
|
485 | 488 | self.__reload() |
|
486 | 489 | else: |
|
487 | 490 | return False |
|
488 | 491 | |
|
489 | 492 | if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate: |
|
490 | 493 | return False |
|
491 | 494 | self.__thisUnixSample -= self.__samples_to_read |
|
492 | 495 | |
|
493 | 496 | indexChannel = 0 |
|
494 | 497 | |
|
495 | 498 | dataOk = False |
|
496 | 499 | for thisChannelName in self.__channelNameList: # TODO VARIOS CHANNELS? |
|
497 | 500 | for indexSubchannel in range(self.__num_subchannels): |
|
498 | 501 | try: |
|
499 | 502 | t0 = time() |
|
500 | 503 | result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample, |
|
501 | 504 | self.__samples_to_read, |
|
502 | 505 | thisChannelName, sub_channel=indexSubchannel) |
|
503 | 506 | self.executionTime = time() - t0 |
|
504 | 507 | if self.oldAverage is None: |
|
505 | 508 | self.oldAverage = self.executionTime |
|
506 | 509 | self.oldAverage = ( |
|
507 | 510 | self.executionTime + self.count * self.oldAverage) / (self.count + 1.0) |
|
508 | 511 | self.count = self.count + 1.0 |
|
509 | 512 | |
|
510 | 513 | except IOError, e: |
|
511 | 514 | # read next profile |
|
512 | 515 | self.__flagDiscontinuousBlock = True |
|
513 | 516 | print "[Reading] %s" % datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e |
|
514 | 517 | break |
|
515 | 518 | |
|
516 | 519 | if result.shape[0] != self.__samples_to_read: |
|
517 | 520 | self.__flagDiscontinuousBlock = True |
|
518 | 521 | print "[Reading] %s: Too few samples were found, just %d/%d samples" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), |
|
519 | 522 | result.shape[0], |
|
520 | 523 | self.__samples_to_read) |
|
521 | 524 | break |
|
522 | 525 | |
|
523 | 526 | self.__data_buffer[indexSubchannel, :] = result * volt_scale |
|
524 | 527 | |
|
525 | 528 | indexChannel += 1 |
|
526 | 529 | |
|
527 | 530 | dataOk = True |
|
528 | 531 | |
|
529 | 532 | self.__utctime = self.__thisUnixSample / self.__sample_rate |
|
530 | 533 | |
|
531 | 534 | if not dataOk: |
|
532 | 535 | return False |
|
533 | 536 | |
|
534 | 537 | print "[Reading] %s: %d samples <> %f sec" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), |
|
535 | 538 | self.__samples_to_read, |
|
536 | 539 | self.__timeInterval) |
|
537 | 540 | |
|
538 | 541 | self.__bufferIndex = 0 |
|
539 | 542 | |
|
540 | 543 | return True |
|
541 | 544 | |
|
542 | 545 | def __isBufferEmpty(self): |
|
543 | 546 | return self.__bufferIndex > self.__samples_to_read - self.__nSamples # 40960 - 40 |
|
544 | 547 | |
|
545 | 548 | def getData(self, seconds=30, nTries=5): |
|
546 | 549 | ''' |
|
547 | 550 | This method gets the data from files and put the data into the dataOut object |
|
548 | 551 | |
|
549 | 552 | In addition, increase el the buffer counter in one. |
|
550 | 553 | |
|
551 | 554 | Return: |
|
552 | 555 | data : retorna un perfil de voltages (alturas * canales) copiados desde el |
|
553 | 556 | buffer. Si no hay mas archivos a leer retorna None. |
|
554 | 557 | |
|
555 | 558 | Affected: |
|
556 | 559 | self.dataOut |
|
557 | 560 | self.profileIndex |
|
558 | 561 | self.flagDiscontinuousBlock |
|
559 | 562 | self.flagIsNewBlock |
|
560 | 563 | ''' |
|
561 | 564 | |
|
562 | 565 | err_counter = 0 |
|
563 | 566 | self.dataOut.flagNoData = True |
|
564 | 567 | |
|
565 | 568 | if self.__isBufferEmpty(): |
|
566 | 569 | self.__flagDiscontinuousBlock = False |
|
567 | 570 | |
|
568 | 571 | while True: |
|
569 | 572 | if self.__readNextBlock(): |
|
570 | 573 | break |
|
571 | 574 | if self.__thisUnixSample > self.__endUTCSecond * self.__sample_rate: |
|
572 | 575 | return False |
|
573 | 576 | |
|
574 | 577 | if self.__flagDiscontinuousBlock: |
|
575 | 578 | print '[Reading] discontinuous block found ... continue with the next block' |
|
576 | 579 | continue |
|
577 | 580 | |
|
578 | 581 | if not self.__online: |
|
579 | 582 | return False |
|
580 | 583 | |
|
581 | 584 | err_counter += 1 |
|
582 | 585 | if err_counter > nTries: |
|
583 | 586 | return False |
|
584 | 587 | |
|
585 | 588 | print '[Reading] waiting %d seconds to read a new block' % seconds |
|
586 | 589 | sleep(seconds) |
|
587 | 590 | |
|
588 | 591 | self.dataOut.data = self.__data_buffer[:, |
|
589 | 592 | self.__bufferIndex:self.__bufferIndex + self.__nSamples] |
|
590 | 593 | self.dataOut.utctime = ( |
|
591 | 594 | self.__thisUnixSample + self.__bufferIndex) / self.__sample_rate |
|
592 | 595 | self.dataOut.flagNoData = False |
|
593 | 596 | self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock |
|
594 | 597 | self.dataOut.profileIndex = self.profileIndex |
|
595 | 598 | |
|
596 | 599 | self.__bufferIndex += self.__nSamples |
|
597 | 600 | self.profileIndex += 1 |
|
598 | 601 | |
|
599 | 602 | if self.profileIndex == self.dataOut.nProfiles: |
|
600 | 603 | self.profileIndex = 0 |
|
601 | 604 | |
|
602 | 605 | return True |
|
603 | 606 | |
|
604 | 607 | def printInfo(self): |
|
605 | 608 | ''' |
|
606 | 609 | ''' |
|
607 | 610 | if self.__printInfo == False: |
|
608 | 611 | return |
|
609 | 612 | |
|
610 | 613 | # self.systemHeaderObj.printInfo() |
|
611 | 614 | # self.radarControllerHeaderObj.printInfo() |
|
612 | 615 | |
|
613 | 616 | self.__printInfo = False |
|
614 | 617 | |
|
615 | 618 | def printNumberOfBlock(self): |
|
616 | 619 | ''' |
|
617 | 620 | ''' |
|
618 | 621 | return |
|
619 | 622 | # print self.profileIndex |
|
620 | 623 | |
|
621 | 624 | def run(self, **kwargs): |
|
622 | 625 | ''' |
|
623 | 626 | This method will be called many times so here you should put all your code |
|
624 | 627 | ''' |
|
625 | 628 | |
|
626 | 629 | if not self.isConfig: |
|
627 | 630 | self.setup(**kwargs) |
|
628 | 631 | #self.i = self.i+1 |
|
629 | 632 | self.getData(seconds=self.__delay) |
|
630 | 633 | |
|
631 | 634 | return |
|
632 | 635 | |
|
633 | 636 | |
|
634 | 637 | class DigitalRFWriter(Operation): |
|
635 | 638 | ''' |
|
636 | 639 | classdocs |
|
637 | 640 | ''' |
|
638 | 641 | |
|
639 | 642 | def __init__(self, **kwargs): |
|
640 | 643 | ''' |
|
641 | 644 | Constructor |
|
642 | 645 | ''' |
|
643 | 646 | Operation.__init__(self, **kwargs) |
|
644 | 647 | self.metadata_dict = {} |
|
645 | 648 | self.dataOut = None |
|
646 | 649 | self.dtype = None |
|
647 | 650 | self.oldAverage = 0 |
|
648 | 651 | |
|
649 | 652 | def setHeader(self): |
|
650 | 653 | |
|
651 | 654 | self.metadata_dict['frequency'] = self.dataOut.frequency |
|
652 | 655 | self.metadata_dict['timezone'] = self.dataOut.timeZone |
|
653 | 656 | self.metadata_dict['dtype'] = cPickle.dumps(self.dataOut.dtype) |
|
654 | 657 | self.metadata_dict['nProfiles'] = self.dataOut.nProfiles |
|
655 | 658 | self.metadata_dict['heightList'] = self.dataOut.heightList |
|
656 | 659 | self.metadata_dict['channelList'] = self.dataOut.channelList |
|
657 | 660 | self.metadata_dict['flagDecodeData'] = self.dataOut.flagDecodeData |
|
658 | 661 | self.metadata_dict['flagDeflipData'] = self.dataOut.flagDeflipData |
|
659 | 662 | self.metadata_dict['flagShiftFFT'] = self.dataOut.flagShiftFFT |
|
660 | 663 | self.metadata_dict['useLocalTime'] = self.dataOut.useLocalTime |
|
661 | 664 | self.metadata_dict['nCohInt'] = self.dataOut.nCohInt |
|
662 | 665 | self.metadata_dict['type'] = self.dataOut.type |
|
663 | 666 | self.metadata_dict['flagDataAsBlock'] = getattr( |
|
664 | 667 | self.dataOut, 'flagDataAsBlock', None) # chequear |
|
665 | 668 | |
|
666 | 669 | def setup(self, dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, set=0, metadataFile='metadata', ext='.h5'): |
|
667 | 670 | ''' |
|
668 | 671 | In this method we should set all initial parameters. |
|
669 | 672 | Input: |
|
670 | 673 | dataOut: Input data will also be outputa data |
|
671 | 674 | ''' |
|
672 | 675 | self.setHeader() |
|
673 | 676 | self.__ippSeconds = dataOut.ippSeconds |
|
674 | 677 | self.__deltaH = dataOut.getDeltaH() |
|
675 | 678 | self.__sample_rate = 1e6 * 0.15 / self.__deltaH |
|
676 | 679 | self.__dtype = dataOut.dtype |
|
677 | 680 | if len(dataOut.dtype) == 2: |
|
678 | 681 | self.__dtype = dataOut.dtype[0] |
|
679 | 682 | self.__nSamples = dataOut.systemHeaderObj.nSamples |
|
680 | 683 | self.__nProfiles = dataOut.nProfiles |
|
681 | 684 | |
|
682 | 685 | if self.dataOut.type != 'Voltage': |
|
683 | 686 | raise 'Digital RF cannot be used with this data type' |
|
684 | 687 | self.arr_data = numpy.ones((1, dataOut.nFFTPoints * len( |
|
685 | 688 | self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)]) |
|
686 | 689 | else: |
|
687 | 690 | self.arr_data = numpy.ones((self.__nSamples, len( |
|
688 | 691 | self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)]) |
|
689 | 692 | |
|
690 | 693 | file_cadence_millisecs = 1000 |
|
691 | 694 | |
|
692 | 695 | sample_rate_fraction = Fraction(self.__sample_rate).limit_denominator() |
|
693 | 696 | sample_rate_numerator = long(sample_rate_fraction.numerator) |
|
694 | 697 | sample_rate_denominator = long(sample_rate_fraction.denominator) |
|
695 | 698 | start_global_index = dataOut.utctime * self.__sample_rate |
|
696 | 699 | |
|
697 | 700 | uuid = 'prueba' |
|
698 | 701 | compression_level = 0 |
|
699 | 702 | checksum = False |
|
700 | 703 | is_complex = True |
|
701 | 704 | num_subchannels = len(dataOut.channelList) |
|
702 | 705 | is_continuous = True |
|
703 | 706 | marching_periods = False |
|
704 | 707 | |
|
705 | 708 | self.digitalWriteObj = digital_rf.DigitalRFWriter(path, self.__dtype, dirCadence, |
|
706 | 709 | fileCadence, start_global_index, |
|
707 | 710 | sample_rate_numerator, sample_rate_denominator, uuid, compression_level, checksum, |
|
708 | 711 | is_complex, num_subchannels, is_continuous, marching_periods) |
|
709 | 712 | metadata_dir = os.path.join(path, 'metadata') |
|
710 | 713 | os.system('mkdir %s' % (metadata_dir)) |
|
711 | 714 | self.digitalMetadataWriteObj = digital_rf.DigitalMetadataWriter(metadata_dir, dirCadence, 1, # 236, file_cadence_millisecs / 1000 |
|
712 | 715 | sample_rate_numerator, sample_rate_denominator, |
|
713 | 716 | metadataFile) |
|
714 | 717 | self.isConfig = True |
|
715 | 718 | self.currentSample = 0 |
|
716 | 719 | self.oldAverage = 0 |
|
717 | 720 | self.count = 0 |
|
718 | 721 | return |
|
719 | 722 | |
|
720 | 723 | def writeMetadata(self): |
|
721 | 724 | start_idx = self.__sample_rate * self.dataOut.utctime |
|
722 | 725 | |
|
723 | 726 | self.metadata_dict['processingHeader'] = self.dataOut.processingHeaderObj.getAsDict( |
|
724 | 727 | ) |
|
725 | 728 | self.metadata_dict['radarControllerHeader'] = self.dataOut.radarControllerHeaderObj.getAsDict( |
|
726 | 729 | ) |
|
727 | 730 | self.metadata_dict['systemHeader'] = self.dataOut.systemHeaderObj.getAsDict( |
|
728 | 731 | ) |
|
729 | 732 | self.digitalMetadataWriteObj.write(start_idx, self.metadata_dict) |
|
730 | 733 | return |
|
731 | 734 | |
|
732 | 735 | def timeit(self, toExecute): |
|
733 | 736 | t0 = time() |
|
734 | 737 | toExecute() |
|
735 | 738 | self.executionTime = time() - t0 |
|
736 | 739 | if self.oldAverage is None: |
|
737 | 740 | self.oldAverage = self.executionTime |
|
738 | 741 | self.oldAverage = (self.executionTime + self.count * |
|
739 | 742 | self.oldAverage) / (self.count + 1.0) |
|
740 | 743 | self.count = self.count + 1.0 |
|
741 | 744 | return |
|
742 | 745 | |
|
743 | 746 | def writeData(self): |
|
744 | 747 | if self.dataOut.type != 'Voltage': |
|
745 | 748 | raise 'Digital RF cannot be used with this data type' |
|
746 | 749 | for channel in self.dataOut.channelList: |
|
747 | 750 | for i in range(self.dataOut.nFFTPoints): |
|
748 | 751 | self.arr_data[1][channel * self.dataOut.nFFTPoints + |
|
749 | 752 | i]['r'] = self.dataOut.data[channel][i].real |
|
750 | 753 | self.arr_data[1][channel * self.dataOut.nFFTPoints + |
|
751 | 754 | i]['i'] = self.dataOut.data[channel][i].imag |
|
752 | 755 | else: |
|
753 | 756 | for i in range(self.dataOut.systemHeaderObj.nSamples): |
|
754 | 757 | for channel in self.dataOut.channelList: |
|
755 | 758 | self.arr_data[i][channel]['r'] = self.dataOut.data[channel][i].real |
|
756 | 759 | self.arr_data[i][channel]['i'] = self.dataOut.data[channel][i].imag |
|
757 | 760 | |
|
758 | 761 | def f(): return self.digitalWriteObj.rf_write(self.arr_data) |
|
759 | 762 | self.timeit(f) |
|
760 | 763 | |
|
761 | 764 | return |
|
762 | 765 | |
|
763 | 766 | def run(self, dataOut, frequency=49.92e6, path=None, fileCadence=1000, dirCadence=36000, metadataCadence=1, **kwargs): |
|
764 | 767 | ''' |
|
765 | 768 | This method will be called many times so here you should put all your code |
|
766 | 769 | Inputs: |
|
767 | 770 | dataOut: object with the data |
|
768 | 771 | ''' |
|
769 | 772 | # print dataOut.__dict__ |
|
770 | 773 | self.dataOut = dataOut |
|
771 | 774 | if not self.isConfig: |
|
772 | 775 | self.setup(dataOut, path, frequency, fileCadence, |
|
773 | 776 | dirCadence, metadataCadence, **kwargs) |
|
774 | 777 | self.writeMetadata() |
|
775 | 778 | |
|
776 | 779 | self.writeData() |
|
777 | 780 | |
|
778 | 781 | ## self.currentSample += 1 |
|
779 | 782 | # if self.dataOut.flagDataAsBlock or self.currentSample == 1: |
|
780 | 783 | # self.writeMetadata() |
|
781 | 784 | ## if self.currentSample == self.__nProfiles: self.currentSample = 0 |
|
782 | 785 | |
|
783 | 786 | def close(self): |
|
784 | 787 | print '[Writing] - Closing files ' |
|
785 | 788 | print 'Average of writing to digital rf format is ', self.oldAverage * 1000 |
|
786 | 789 | try: |
|
787 | 790 | self.digitalWriteObj.close() |
|
788 | 791 | except: |
|
789 | 792 | pass |
|
790 | 793 | |
|
791 | 794 | |
|
792 | 795 | # raise |
|
793 | 796 | if __name__ == '__main__': |
|
794 | 797 | |
|
795 | 798 | readObj = DigitalRFReader() |
|
796 | 799 | |
|
797 | 800 | while True: |
|
798 | 801 | readObj.run(path='/home/jchavez/jicamarca/mocked_data/') |
|
799 | 802 | # readObj.printInfo() |
|
800 | 803 | # readObj.printNumberOfBlock() |
General Comments 0
You need to be logged in to leave comments.
Login now