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