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