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