##// END OF EJS Templates
USRP Reade updated to read Sandra's data
Miguel Valdez -
r812:e72772d52a99
parent child
Show More
@@ -1,7 +1,7
1 '''
1 '''
2 Created on Feb 7, 2012
2 Created on Feb 7, 2012
3
3
4 @author $Author$
4 @author $Author$
5 @version $Id$
5 @version $Id$
6 '''
6 '''
7 __version__ = "2.2.4" No newline at end of file
7 __version__ = "2.2.4.1" No newline at end of file
@@ -1,587 +1,595
1 '''
1 '''
2 Created on Jul 3, 2014
2 Created on Jul 3, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import os
6 import os
7 import datetime
7 import datetime
8 import numpy
8 import numpy
9
9
10 try:
10 try:
11 from gevent import sleep
11 from gevent import sleep
12 except:
12 except:
13 from time import sleep
13 from time import sleep
14
14
15 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
15 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
16 from schainpy.model.data.jrodata import Voltage
16 from schainpy.model.data.jrodata import Voltage
17 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
17 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
18
18
19 try:
19 try:
20 import digital_rf_hdf5
20 import digital_rf_hdf5
21 except:
21 except:
22 print 'You should install "digital_rf_hdf5" module if you want to read USRP data'
22 print 'You should install "digital_rf_hdf5" module if you want to read USRP data'
23
23
24 class USRPReader(ProcessingUnit):
24 class USRPReader(ProcessingUnit):
25 '''
25 '''
26 classdocs
26 classdocs
27 '''
27 '''
28
28
29 def __init__(self):
29 def __init__(self):
30 '''
30 '''
31 Constructor
31 Constructor
32 '''
32 '''
33
33
34 ProcessingUnit.__init__(self)
34 ProcessingUnit.__init__(self)
35
35
36 self.dataOut = Voltage()
36 self.dataOut = Voltage()
37 self.__printInfo = True
37 self.__printInfo = True
38 self.__flagDiscontinuousBlock = False
38 self.__flagDiscontinuousBlock = False
39 self.__bufferIndex = 9999999
39 self.__bufferIndex = 9999999
40
40
41 self.__ippKm = None
41 self.__ippKm = None
42 self.__codeType = 0
42 self.__codeType = 0
43 self.__nCode = None
43 self.__nCode = None
44 self.__nBaud = None
44 self.__nBaud = None
45 self.__code = None
45 self.__code = None
46
46
47 def __getCurrentSecond(self):
47 def __getCurrentSecond(self):
48
48
49 return self.__thisUnixSample/self.__sample_rate
49 return self.__thisUnixSample/self.__sample_rate
50
50
51 thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.")
51 thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.")
52
52
53 def __setFileHeader(self):
53 def __setFileHeader(self):
54 '''
54 '''
55 In this method will be initialized every parameter of dataOut object (header, no data)
55 In this method will be initialized every parameter of dataOut object (header, no data)
56 '''
56 '''
57 nProfiles = self.__sample_rate #Number of profiles by second
57 nProfiles = self.__sample_rate #Number of profiles by second
58
58
59 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(ippKm=self.__ippKm,
59 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(ippKm=self.__ippKm,
60 txA=0,
60 txA=0,
61 txB=0,
61 txB=0,
62 nWindows=1,
62 nWindows=1,
63 nHeights=self.__nSamples,
63 nHeights=self.__nSamples,
64 firstHeight=self.__firstHeigth,
64 firstHeight=self.__firstHeigth,
65 deltaHeight=self.__deltaHeigth,
65 deltaHeight=self.__deltaHeigth,
66 codeType=self.__codeType,
66 codeType=self.__codeType,
67 nCode=self.__nCode, nBaud=self.__nBaud,
67 nCode=self.__nCode, nBaud=self.__nBaud,
68 code = self.__code)
68 code = self.__code)
69
69
70 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
70 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
71 nProfiles=nProfiles,
71 nProfiles=nProfiles,
72 nChannels=len(self.__channelList),
72 nChannels=len(self.__channelList),
73 adcResolution=14)
73 adcResolution=14)
74
74
75 self.dataOut.type = "Voltage"
75 self.dataOut.type = "Voltage"
76
76
77 self.dataOut.data = None
77 self.dataOut.data = None
78
78
79 self.dataOut.dtype = numpy.dtype([('real','<i8'),('imag','<i8')])
79 self.dataOut.dtype = numpy.dtype([('real','<i8'),('imag','<i8')])
80
80
81 # self.dataOut.nChannels = 0
81 # self.dataOut.nChannels = 0
82
82
83 # self.dataOut.nHeights = 0
83 # self.dataOut.nHeights = 0
84
84
85 self.dataOut.nProfiles = nProfiles
85 self.dataOut.nProfiles = nProfiles
86
86
87 self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth
87 self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype = numpy.float)*self.__deltaHeigth
88
88
89 self.dataOut.channelList = self.__channelList
89 self.dataOut.channelList = self.__channelList
90
90
91 self.dataOut.blocksize = self.dataOut.getNChannels() * self.dataOut.getNHeights()
91 self.dataOut.blocksize = self.dataOut.getNChannels() * self.dataOut.getNHeights()
92
92
93 # self.dataOut.channelIndexList = None
93 # self.dataOut.channelIndexList = None
94
94
95 self.dataOut.flagNoData = True
95 self.dataOut.flagNoData = True
96
96
97 #Set to TRUE if the data is discontinuous
97 #Set to TRUE if the data is discontinuous
98 self.dataOut.flagDiscontinuousBlock = False
98 self.dataOut.flagDiscontinuousBlock = False
99
99
100 self.dataOut.utctime = None
100 self.dataOut.utctime = None
101
101
102 self.dataOut.timeZone = self.__timezone/60 #timezone like jroheader, difference in minutes between UTC and localtime
102 self.dataOut.timeZone = self.__timezone/60 #timezone like jroheader, difference in minutes between UTC and localtime
103
103
104 self.dataOut.dstFlag = 0
104 self.dataOut.dstFlag = 0
105
105
106 self.dataOut.errorCount = 0
106 self.dataOut.errorCount = 0
107
107
108 self.dataOut.nCohInt = 1
108 self.dataOut.nCohInt = 1
109
109
110 self.dataOut.flagDecodeData = False #asumo que la data esta decodificada
110 self.dataOut.flagDecodeData = False #asumo que la data esta decodificada
111
111
112 self.dataOut.flagDeflipData = False #asumo que la data esta sin flip
112 self.dataOut.flagDeflipData = False #asumo que la data esta sin flip
113
113
114 self.dataOut.flagShiftFFT = False
114 self.dataOut.flagShiftFFT = False
115
115
116 self.dataOut.ippSeconds = 1.0*self.__nSamples/self.__sample_rate
116 self.dataOut.ippSeconds = 1.0*self.__nSamples/self.__sample_rate
117
117
118 #Time interval between profiles
118 #Time interval between profiles
119 #self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
119 #self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
120
120
121 self.dataOut.frequency = self.__frequency
121 self.dataOut.frequency = self.__frequency
122
122
123 self.dataOut.realtime = self.__online
123 self.dataOut.realtime = self.__online
124
124
125 def findDatafiles(self, path, startDate=None, endDate=None):
125 def findDatafiles(self, path, startDate=None, endDate=None):
126
126
127 if not os.path.isdir(path):
127 if not os.path.isdir(path):
128 return []
128 return []
129
129
130 try:
130 try:
131 digitalReadObj = digital_rf_hdf5.read_hdf5(path, load_all_metadata=True)
131 digitalReadObj = digital_rf_hdf5.read_hdf5(path, load_all_metadata=True)
132 except:
132 except:
133 digitalReadObj = digital_rf_hdf5.read_hdf5(path)
133 digitalReadObj = digital_rf_hdf5.read_hdf5(path)
134
134
135 channelNameList = digitalReadObj.get_channels()
135 channelNameList = digitalReadObj.get_channels()
136
136
137 if not channelNameList:
137 if not channelNameList:
138 return []
138 return []
139
139
140 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
140 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
141
141
142 sample_rate = metadata_dict['sample_rate'][0]
142 sample_rate = metadata_dict['sample_rate'][0]
143
143
144 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
144 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
145
145
146 try:
146 try:
147 timezone = this_metadata_file['timezone'].value
147 timezone = this_metadata_file['timezone'].value
148 except:
148 except:
149 timezone = 0
149 timezone = 0
150
150
151 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(channelNameList[0])/sample_rate - timezone
151 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(channelNameList[0])/sample_rate - timezone
152
152
153 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
153 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
154 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
154 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
155
155
156 if not startDate:
156 if not startDate:
157 startDate = startDatetime.date()
157 startDate = startDatetime.date()
158
158
159 if not endDate:
159 if not endDate:
160 endDate = endDatatime.date()
160 endDate = endDatatime.date()
161
161
162 dateList = []
162 dateList = []
163
163
164 thisDatetime = startDatetime
164 thisDatetime = startDatetime
165
165
166 while(thisDatetime<=endDatatime):
166 while(thisDatetime<=endDatatime):
167
167
168 thisDate = thisDatetime.date()
168 thisDate = thisDatetime.date()
169
169
170 if thisDate < startDate:
170 if thisDate < startDate:
171 continue
171 continue
172
172
173 if thisDate > endDate:
173 if thisDate > endDate:
174 break
174 break
175
175
176 dateList.append(thisDate)
176 dateList.append(thisDate)
177 thisDatetime += datetime.timedelta(1)
177 thisDatetime += datetime.timedelta(1)
178
178
179 return dateList
179 return dateList
180
180
181 def setup(self, path = None,
181 def setup(self, path = None,
182 startDate = None,
182 startDate = None,
183 endDate = None,
183 endDate = None,
184 startTime = datetime.time(0,0,0),
184 startTime = datetime.time(0,0,0),
185 endTime = datetime.time(23,59,59),
185 endTime = datetime.time(23,59,59),
186 channelList = None,
186 channelList = None,
187 nSamples = None,
187 nSamples = None,
188 ippKm = 60,
188 ippKm = 60,
189 online = False,
189 online = False,
190 delay = 60,
190 delay = 60,
191 buffer_size = 1024,
191 buffer_size = 1024,
192 **kwargs):
192 **kwargs):
193 '''
193 '''
194 In this method we should set all initial parameters.
194 In this method we should set all initial parameters.
195
195
196 Inputs:
196 Inputs:
197 path
197 path
198 startDate
198 startDate
199 endDate
199 endDate
200 startTime
200 startTime
201 endTime
201 endTime
202 set
202 set
203 expLabel
203 expLabel
204 ext
204 ext
205 online
205 online
206 delay
206 delay
207 '''
207 '''
208
208
209 if not os.path.isdir(path):
209 if not os.path.isdir(path):
210 raise ValueError, "[Reading] Directory %s does not exist" %path
210 raise ValueError, "[Reading] Directory %s does not exist" %path
211
211
212 try:
212 try:
213 self.digitalReadObj = digital_rf_hdf5.read_hdf5(path, load_all_metadata=True)
213 self.digitalReadObj = digital_rf_hdf5.read_hdf5(path, load_all_metadata=True)
214 except:
214 except:
215 self.digitalReadObj = digital_rf_hdf5.read_hdf5(path)
215 self.digitalReadObj = digital_rf_hdf5.read_hdf5(path)
216
216
217 channelNameList = self.digitalReadObj.get_channels()
217 channelNameList = self.digitalReadObj.get_channels()
218
218
219 if not channelNameList:
219 if not channelNameList:
220 raise ValueError, "[Reading] Directory %s does not have any files" %path
220 raise ValueError, "[Reading] Directory %s does not have any files" %path
221
221
222 if not channelList:
222 if not channelList:
223 channelList = range(len(channelNameList))
223 channelList = range(len(channelNameList))
224
224
225 ########## Reading metadata ######################
225 ########## Reading metadata ######################
226
226
227 metadata_dict = self.digitalReadObj.get_rf_file_metadata(channelNameList[channelList[0]])
227 metadata_dict = self.digitalReadObj.get_rf_file_metadata(channelNameList[channelList[0]])
228
228
229 self.__sample_rate = metadata_dict['sample_rate'][0]
229 self.__sample_rate = metadata_dict['sample_rate'][0]
230 self.__samples_per_file = metadata_dict['samples_per_file'][0]
230 # self.__samples_per_file = metadata_dict['samples_per_file'][0]
231 self.__deltaHeigth = 1e6*0.15/self.__sample_rate
231 self.__deltaHeigth = 1e6*0.15/self.__sample_rate
232
232
233 this_metadata_file = self.digitalReadObj.get_metadata(channelNameList[channelList[0]])
233 this_metadata_file = self.digitalReadObj.get_metadata(channelNameList[channelList[0]])
234
234
235 self.__frequency = this_metadata_file['center_frequencies'].value
235 self.__frequency = None
236 try:
237 self.__frequency = this_metadata_file['center_frequencies'].value
238 except:
239 self.__frequency = this_metadata_file['fc'].value
240
241 if not self.__frequency:
242 raise ValueError, "Center Frequency is not defined in metadata file"
243
236 try:
244 try:
237 self.__timezone = this_metadata_file['timezone'].value
245 self.__timezone = this_metadata_file['timezone'].value
238 except:
246 except:
239 self.__timezone = 0
247 self.__timezone = 0
240
248
241 self.__firstHeigth = 0
249 self.__firstHeigth = 0
242
250
243 try:
251 try:
244 codeType = this_metadata_file['codeType'].value
252 codeType = this_metadata_file['codeType'].value
245 except:
253 except:
246 codeType = 0
254 codeType = 0
247
255
248 nCode = 1
256 nCode = 1
249 nBaud = 1
257 nBaud = 1
250 code = numpy.ones((nCode, nBaud), dtype=numpy.int)
258 code = numpy.ones((nCode, nBaud), dtype=numpy.int)
251
259
252 if codeType:
260 if codeType:
253 nCode = this_metadata_file['nCode'].value
261 nCode = this_metadata_file['nCode'].value
254 nBaud = this_metadata_file['nBaud'].value
262 nBaud = this_metadata_file['nBaud'].value
255 code = this_metadata_file['code'].value
263 code = this_metadata_file['code'].value
256
264
257 if not ippKm:
265 if not ippKm:
258 try:
266 try:
259 #seconds to km
267 #seconds to km
260 ippKm = 1e6*0.15*this_metadata_file['ipp'].value
268 ippKm = 1e6*0.15*this_metadata_file['ipp'].value
261 except:
269 except:
262 ippKm = None
270 ippKm = None
263
271
264 ####################################################
272 ####################################################
265 startUTCSecond = None
273 startUTCSecond = None
266 endUTCSecond = None
274 endUTCSecond = None
267
275
268 if startDate:
276 if startDate:
269 startDatetime = datetime.datetime.combine(startDate, startTime)
277 startDatetime = datetime.datetime.combine(startDate, startTime)
270 startUTCSecond = (startDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
278 startUTCSecond = (startDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
271
279
272 if endDate:
280 if endDate:
273 endDatetime = datetime.datetime.combine(endDate, endTime)
281 endDatetime = datetime.datetime.combine(endDate, endTime)
274 endUTCSecond = (endDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
282 endUTCSecond = (endDatetime-datetime.datetime(1970,1,1)).total_seconds() + self.__timezone
275
283
276 start_index, end_index = self.digitalReadObj.get_bounds(channelNameList[channelList[0]])
284 start_index, end_index = self.digitalReadObj.get_bounds(channelNameList[channelList[0]])
277
285
278 if not startUTCSecond:
286 if not startUTCSecond:
279 startUTCSecond = start_index/self.__sample_rate
287 startUTCSecond = start_index/self.__sample_rate
280
288
281 if start_index > startUTCSecond*self.__sample_rate:
289 if start_index > startUTCSecond*self.__sample_rate:
282 startUTCSecond = start_index/self.__sample_rate
290 startUTCSecond = start_index/self.__sample_rate
283
291
284 if not endUTCSecond:
292 if not endUTCSecond:
285 endUTCSecond = end_index/self.__sample_rate
293 endUTCSecond = end_index/self.__sample_rate
286
294
287 if end_index < endUTCSecond*self.__sample_rate:
295 if end_index < endUTCSecond*self.__sample_rate:
288 endUTCSecond = end_index/self.__sample_rate
296 endUTCSecond = end_index/self.__sample_rate
289
297
290 if not nSamples:
298 if not nSamples:
291 if not ippKm:
299 if not ippKm:
292 raise ValueError, "[Reading] nSamples or ippKm should be defined"
300 raise ValueError, "[Reading] nSamples or ippKm should be defined"
293
301
294 nSamples = int(ippKm / (1e6*0.15/self.__sample_rate))
302 nSamples = int(ippKm / (1e6*0.15/self.__sample_rate))
295
303
296 channelBoundList = []
304 channelBoundList = []
297 channelNameListFiltered = []
305 channelNameListFiltered = []
298
306
299 for thisIndexChannel in channelList:
307 for thisIndexChannel in channelList:
300 thisChannelName = channelNameList[thisIndexChannel]
308 thisChannelName = channelNameList[thisIndexChannel]
301 start_index, end_index = self.digitalReadObj.get_bounds(thisChannelName)
309 start_index, end_index = self.digitalReadObj.get_bounds(thisChannelName)
302 channelBoundList.append((start_index, end_index))
310 channelBoundList.append((start_index, end_index))
303 channelNameListFiltered.append(thisChannelName)
311 channelNameListFiltered.append(thisChannelName)
304
312
305 self.profileIndex = 0
313 self.profileIndex = 0
306
314
307 self.__delay = delay
315 self.__delay = delay
308 self.__ippKm = ippKm
316 self.__ippKm = ippKm
309 self.__codeType = codeType
317 self.__codeType = codeType
310 self.__nCode = nCode
318 self.__nCode = nCode
311 self.__nBaud = nBaud
319 self.__nBaud = nBaud
312 self.__code = code
320 self.__code = code
313
321
314 self.__datapath = path
322 self.__datapath = path
315 self.__online = online
323 self.__online = online
316 self.__channelList = channelList
324 self.__channelList = channelList
317 self.__channelNameList = channelNameListFiltered
325 self.__channelNameList = channelNameListFiltered
318 self.__channelBoundList = channelBoundList
326 self.__channelBoundList = channelBoundList
319 self.__nSamples = nSamples
327 self.__nSamples = nSamples
320 self.__samples_to_read = int(buffer_size*nSamples)
328 self.__samples_to_read = int(buffer_size*nSamples)
321 self.__nChannels = len(self.__channelList)
329 self.__nChannels = len(self.__channelList)
322
330
323 self.__startUTCSecond = startUTCSecond
331 self.__startUTCSecond = startUTCSecond
324 self.__endUTCSecond = endUTCSecond
332 self.__endUTCSecond = endUTCSecond
325
333
326 self.__timeInterval = 1.0 * self.__samples_to_read/self.__sample_rate #Time interval
334 self.__timeInterval = 1.0 * self.__samples_to_read/self.__sample_rate #Time interval
327
335
328 if online:
336 if online:
329 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
337 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
330 startUTCSecond = numpy.floor(endUTCSecond)
338 startUTCSecond = numpy.floor(endUTCSecond)
331
339
332 self.__thisUnixSample = int(startUTCSecond*self.__sample_rate) - self.__samples_to_read
340 self.__thisUnixSample = int(startUTCSecond*self.__sample_rate) - self.__samples_to_read
333
341
334 self.__data_buffer = numpy.zeros((self.__nChannels, self.__samples_to_read), dtype = numpy.complex)
342 self.__data_buffer = numpy.zeros((self.__nChannels, self.__samples_to_read), dtype = numpy.complex)
335
343
336 self.__setFileHeader()
344 self.__setFileHeader()
337 self.isConfig = True
345 self.isConfig = True
338
346
339 print "[Reading] USRP Data was found from %s to %s " %(
347 print "[Reading] USRP Data was found from %s to %s " %(
340 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
348 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
341 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
349 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
342 )
350 )
343
351
344 print "[Reading] Starting process from %s to %s" %(datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
352 print "[Reading] Starting process from %s to %s" %(datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
345 datetime.datetime.utcfromtimestamp(endUTCSecond - self.__timezone)
353 datetime.datetime.utcfromtimestamp(endUTCSecond - self.__timezone)
346 )
354 )
347
355
348 def __reload(self):
356 def __reload(self):
349
357
350 if not self.__online:
358 if not self.__online:
351 return
359 return
352
360
353 # print
361 # print
354 # print "%s not in range [%s, %s]" %(
362 # print "%s not in range [%s, %s]" %(
355 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
363 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
356 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
364 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
357 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
365 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
358 # )
366 # )
359 print "[Reading] reloading metadata ..."
367 print "[Reading] reloading metadata ..."
360
368
361 try:
369 try:
362 self.digitalReadObj.reload(complete_update=True)
370 self.digitalReadObj.reload(complete_update=True)
363 except:
371 except:
364 self.digitalReadObj.reload()
372 self.digitalReadObj.reload()
365
373
366 start_index, end_index = self.digitalReadObj.get_bounds(self.__channelNameList[self.__channelList[0]])
374 start_index, end_index = self.digitalReadObj.get_bounds(self.__channelNameList[self.__channelList[0]])
367
375
368 if start_index > self.__startUTCSecond*self.__sample_rate:
376 if start_index > self.__startUTCSecond*self.__sample_rate:
369 self.__startUTCSecond = 1.0*start_index/self.__sample_rate
377 self.__startUTCSecond = 1.0*start_index/self.__sample_rate
370
378
371 if end_index > self.__endUTCSecond*self.__sample_rate:
379 if end_index > self.__endUTCSecond*self.__sample_rate:
372 self.__endUTCSecond = 1.0*end_index/self.__sample_rate
380 self.__endUTCSecond = 1.0*end_index/self.__sample_rate
373 print
381 print
374 print "[Reading] New timerange found [%s, %s] " %(
382 print "[Reading] New timerange found [%s, %s] " %(
375 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
383 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
376 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
384 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
377 )
385 )
378
386
379 return True
387 return True
380
388
381 return False
389 return False
382
390
383 def __readNextBlock(self, seconds=30, volt_scale = 218776):
391 def __readNextBlock(self, seconds=30, volt_scale = 218776):
384 '''
392 '''
385 '''
393 '''
386
394
387 #Set the next data
395 #Set the next data
388 self.__flagDiscontinuousBlock = False
396 self.__flagDiscontinuousBlock = False
389 self.__thisUnixSample += self.__samples_to_read
397 self.__thisUnixSample += self.__samples_to_read
390
398
391 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
399 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
392 print "[Reading] There are no more data into selected time-range"
400 print "[Reading] There are no more data into selected time-range"
393
401
394 self.__reload()
402 self.__reload()
395
403
396 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
404 if self.__thisUnixSample + 2*self.__samples_to_read > self.__endUTCSecond*self.__sample_rate:
397 self.__thisUnixSample -= self.__samples_to_read
405 self.__thisUnixSample -= self.__samples_to_read
398 return False
406 return False
399
407
400 indexChannel = 0
408 indexChannel = 0
401
409
402 dataOk = False
410 dataOk = False
403
411
404 for thisChannelName in self.__channelNameList:
412 for thisChannelName in self.__channelNameList:
405
413
406 try:
414 try:
407 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
415 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
408 self.__samples_to_read,
416 self.__samples_to_read,
409 thisChannelName)
417 thisChannelName)
410
418
411 except IOError, e:
419 except IOError, e:
412 #read next profile
420 #read next profile
413 self.__flagDiscontinuousBlock = True
421 self.__flagDiscontinuousBlock = True
414 print "[Reading] %s" %datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e
422 print "[Reading] %s" %datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e
415 break
423 break
416
424
417 if result.shape[0] != self.__samples_to_read:
425 if result.shape[0] != self.__samples_to_read:
418 self.__flagDiscontinuousBlock = True
426 self.__flagDiscontinuousBlock = True
419 print "[Reading] %s: Too few samples were found, just %d/%d samples" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
427 print "[Reading] %s: Too few samples were found, just %d/%d samples" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
420 result.shape[0],
428 result.shape[0],
421 self.__samples_to_read)
429 self.__samples_to_read)
422 break
430 break
423
431
424 self.__data_buffer[indexChannel,:] = result*volt_scale
432 self.__data_buffer[indexChannel,:] = result*volt_scale
425
433
426 indexChannel += 1
434 indexChannel += 1
427
435
428 dataOk = True
436 dataOk = True
429
437
430 self.__utctime = self.__thisUnixSample/self.__sample_rate
438 self.__utctime = self.__thisUnixSample/self.__sample_rate
431
439
432 if not dataOk:
440 if not dataOk:
433 return False
441 return False
434
442
435 print "[Reading] %s: %d samples <> %f sec" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
443 print "[Reading] %s: %d samples <> %f sec" %(datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
436 self.__samples_to_read,
444 self.__samples_to_read,
437 self.__timeInterval)
445 self.__timeInterval)
438
446
439 self.__bufferIndex = 0
447 self.__bufferIndex = 0
440
448
441 return True
449 return True
442
450
443 def __isBufferEmpty(self):
451 def __isBufferEmpty(self):
444
452
445 if self.__bufferIndex <= self.__samples_to_read - self.__nSamples:
453 if self.__bufferIndex <= self.__samples_to_read - self.__nSamples:
446 return False
454 return False
447
455
448 return True
456 return True
449
457
450 def getData(self, seconds=30, nTries=5):
458 def getData(self, seconds=30, nTries=5):
451
459
452 '''
460 '''
453 This method gets the data from files and put the data into the dataOut object
461 This method gets the data from files and put the data into the dataOut object
454
462
455 In addition, increase el the buffer counter in one.
463 In addition, increase el the buffer counter in one.
456
464
457 Return:
465 Return:
458 data : retorna un perfil de voltages (alturas * canales) copiados desde el
466 data : retorna un perfil de voltages (alturas * canales) copiados desde el
459 buffer. Si no hay mas archivos a leer retorna None.
467 buffer. Si no hay mas archivos a leer retorna None.
460
468
461 Affected:
469 Affected:
462 self.dataOut
470 self.dataOut
463 self.profileIndex
471 self.profileIndex
464 self.flagDiscontinuousBlock
472 self.flagDiscontinuousBlock
465 self.flagIsNewBlock
473 self.flagIsNewBlock
466 '''
474 '''
467
475
468 err_counter = 0
476 err_counter = 0
469 self.dataOut.flagNoData = True
477 self.dataOut.flagNoData = True
470
478
471 if self.__isBufferEmpty():
479 if self.__isBufferEmpty():
472
480
473 self.__flagDiscontinuousBlock = False
481 self.__flagDiscontinuousBlock = False
474
482
475 while True:
483 while True:
476 if self.__readNextBlock():
484 if self.__readNextBlock():
477 break
485 break
478
486
479 if self.__thisUnixSample > self.__endUTCSecond*self.__sample_rate:
487 if self.__thisUnixSample > self.__endUTCSecond*self.__sample_rate:
480 return False
488 return False
481
489
482 if self.__flagDiscontinuousBlock:
490 if self.__flagDiscontinuousBlock:
483 print '[Reading] discontinuous block found ... continue with the next block'
491 print '[Reading] discontinuous block found ... continue with the next block'
484 continue
492 continue
485
493
486 if not self.__online:
494 if not self.__online:
487 return False
495 return False
488
496
489 err_counter += 1
497 err_counter += 1
490 if err_counter > nTries:
498 if err_counter > nTries:
491 return False
499 return False
492
500
493 print '[Reading] waiting %d seconds to read a new block' %seconds
501 print '[Reading] waiting %d seconds to read a new block' %seconds
494 sleep(seconds)
502 sleep(seconds)
495
503
496 self.dataOut.data = self.__data_buffer[:,self.__bufferIndex:self.__bufferIndex+self.__nSamples]
504 self.dataOut.data = self.__data_buffer[:,self.__bufferIndex:self.__bufferIndex+self.__nSamples]
497 self.dataOut.utctime = (self.__thisUnixSample + self.__bufferIndex)/self.__sample_rate
505 self.dataOut.utctime = (self.__thisUnixSample + self.__bufferIndex)/self.__sample_rate
498 self.dataOut.flagNoData = False
506 self.dataOut.flagNoData = False
499 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
507 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
500
508
501 self.__bufferIndex += self.__nSamples
509 self.__bufferIndex += self.__nSamples
502 self.profileIndex += 1
510 self.profileIndex += 1
503
511
504 return True
512 return True
505
513
506 def printInfo(self):
514 def printInfo(self):
507 '''
515 '''
508 '''
516 '''
509 if self.__printInfo == False:
517 if self.__printInfo == False:
510 return
518 return
511
519
512 # self.systemHeaderObj.printInfo()
520 # self.systemHeaderObj.printInfo()
513 # self.radarControllerHeaderObj.printInfo()
521 # self.radarControllerHeaderObj.printInfo()
514
522
515 self.__printInfo = False
523 self.__printInfo = False
516
524
517 def printNumberOfBlock(self):
525 def printNumberOfBlock(self):
518 '''
526 '''
519 '''
527 '''
520
528
521 print self.profileIndex
529 print self.profileIndex
522
530
523 def run(self, **kwargs):
531 def run(self, **kwargs):
524 '''
532 '''
525 This method will be called many times so here you should put all your code
533 This method will be called many times so here you should put all your code
526 '''
534 '''
527
535
528 if not self.isConfig:
536 if not self.isConfig:
529 self.setup(**kwargs)
537 self.setup(**kwargs)
530
538
531 self.getData(seconds=self.__delay)
539 self.getData(seconds=self.__delay)
532
540
533 return
541 return
534
542
535 class USRPWriter(Operation):
543 class USRPWriter(Operation):
536 '''
544 '''
537 classdocs
545 classdocs
538 '''
546 '''
539
547
540 def __init__(self):
548 def __init__(self):
541 '''
549 '''
542 Constructor
550 Constructor
543 '''
551 '''
544 self.dataOut = None
552 self.dataOut = None
545
553
546 def setup(self, dataIn, path, blocksPerFile, set=0, ext=None):
554 def setup(self, dataIn, path, blocksPerFile, set=0, ext=None):
547 '''
555 '''
548 In this method we should set all initial parameters.
556 In this method we should set all initial parameters.
549
557
550 Input:
558 Input:
551 dataIn : Input data will also be outputa data
559 dataIn : Input data will also be outputa data
552
560
553 '''
561 '''
554 self.dataOut = dataIn
562 self.dataOut = dataIn
555
563
556
564
557
565
558
566
559
567
560 self.isConfig = True
568 self.isConfig = True
561
569
562 return
570 return
563
571
564 def run(self, dataIn, **kwargs):
572 def run(self, dataIn, **kwargs):
565 '''
573 '''
566 This method will be called many times so here you should put all your code
574 This method will be called many times so here you should put all your code
567
575
568 Inputs:
576 Inputs:
569
577
570 dataIn : object with the data
578 dataIn : object with the data
571
579
572 '''
580 '''
573
581
574 if not self.isConfig:
582 if not self.isConfig:
575 self.setup(dataIn, **kwargs)
583 self.setup(dataIn, **kwargs)
576
584
577
585
578 if __name__ == '__main__':
586 if __name__ == '__main__':
579
587
580 readObj = USRPReader()
588 readObj = USRPReader()
581
589
582 while True:
590 while True:
583 readObj.run(path='/Volumes/DATA/haystack/passive_radar/')
591 readObj.run(path='/Volumes/DATA/haystack/passive_radar/')
584 # readObj.printInfo()
592 # readObj.printInfo()
585 readObj.printNumberOfBlock()
593 readObj.printNumberOfBlock()
586
594
587 No newline at end of file
595
General Comments 0
You need to be logged in to leave comments. Login now