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