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