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