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