##// END OF EJS Templates
Remove numpy.float, numpy.int, numpy.complex
Juan C. Espinoza -
r1594:16c6f41ffb3e
parent child
Show More
@@ -1,793 +1,793
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
74
75 nProfiles = 1.0 / ippSeconds # Number of profiles in one second
75 nProfiles = 1.0 / ippSeconds # Number of profiles in one second
76
76
77 try:
77 try:
78 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
78 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
79 self.__radarControllerHeader)
79 self.__radarControllerHeader)
80 except:
80 except:
81 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
81 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(
82 txA=0,
82 txA=0,
83 txB=0,
83 txB=0,
84 nWindows=1,
84 nWindows=1,
85 nHeights=self.__nSamples,
85 nHeights=self.__nSamples,
86 firstHeight=self.__firstHeigth,
86 firstHeight=self.__firstHeigth,
87 deltaHeight=self.__deltaHeigth,
87 deltaHeight=self.__deltaHeigth,
88 codeType=self.__codeType,
88 codeType=self.__codeType,
89 nCode=self.__nCode, nBaud=self.__nBaud,
89 nCode=self.__nCode, nBaud=self.__nBaud,
90 code=self.__code)
90 code=self.__code)
91
91
92 try:
92 try:
93 self.dataOut.systemHeaderObj = SystemHeader(self.__systemHeader)
93 self.dataOut.systemHeaderObj = SystemHeader(self.__systemHeader)
94 except:
94 except:
95 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
95 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
96 nProfiles=nProfiles,
96 nProfiles=nProfiles,
97 nChannels=len(
97 nChannels=len(
98 self.__channelList),
98 self.__channelList),
99 adcResolution=14)
99 adcResolution=14)
100 self.dataOut.type = "Voltage"
100 self.dataOut.type = "Voltage"
101
101
102 self.dataOut.data = None
102 self.dataOut.data = None
103
103
104 self.dataOut.dtype = self.dtype
104 self.dataOut.dtype = self.dtype
105
105
106 # self.dataOut.nChannels = 0
106 # self.dataOut.nChannels = 0
107
107
108 # self.dataOut.nHeights = 0
108 # self.dataOut.nHeights = 0
109
109
110 self.dataOut.nProfiles = int(nProfiles)
110 self.dataOut.nProfiles = int(nProfiles)
111
111
112 self.dataOut.heightList = self.__firstHeigth + \
112 self.dataOut.heightList = self.__firstHeigth + \
113 numpy.arange(self.__nSamples, dtype=numpy.float) * \
113 numpy.arange(self.__nSamples, dtype=numpy.float32) * \
114 self.__deltaHeigth
114 self.__deltaHeigth
115
115
116 self.dataOut.channelList = list(range(self.__num_subchannels))
116 self.dataOut.channelList = list(range(self.__num_subchannels))
117
117
118 self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights
118 self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights
119
119
120 # self.dataOut.channelIndexList = None
120 # self.dataOut.channelIndexList = None
121
121
122 self.dataOut.flagNoData = True
122 self.dataOut.flagNoData = True
123
123
124 self.dataOut.flagDataAsBlock = False
124 self.dataOut.flagDataAsBlock = False
125 # Set to TRUE if the data is discontinuous
125 # Set to TRUE if the data is discontinuous
126 self.dataOut.flagDiscontinuousBlock = False
126 self.dataOut.flagDiscontinuousBlock = False
127
127
128 self.dataOut.utctime = None
128 self.dataOut.utctime = None
129
129
130 # timezone like jroheader, difference in minutes between UTC and localtime
130 # timezone like jroheader, difference in minutes between UTC and localtime
131 self.dataOut.timeZone = self.__timezone / 60
131 self.dataOut.timeZone = self.__timezone / 60
132
132
133 self.dataOut.dstFlag = 0
133 self.dataOut.dstFlag = 0
134
134
135 self.dataOut.errorCount = 0
135 self.dataOut.errorCount = 0
136
136
137 try:
137 try:
138 self.dataOut.nCohInt = self.fixed_metadata_dict.get(
138 self.dataOut.nCohInt = self.fixed_metadata_dict.get(
139 'nCohInt', self.nCohInt)
139 'nCohInt', self.nCohInt)
140
140
141 # asumo que la data esta decodificada
141 # asumo que la data esta decodificada
142 self.dataOut.flagDecodeData = self.fixed_metadata_dict.get(
142 self.dataOut.flagDecodeData = self.fixed_metadata_dict.get(
143 'flagDecodeData', self.flagDecodeData)
143 'flagDecodeData', self.flagDecodeData)
144
144
145 # asumo que la data esta sin flip
145 # asumo que la data esta sin flip
146 self.dataOut.flagDeflipData = self.fixed_metadata_dict['flagDeflipData']
146 self.dataOut.flagDeflipData = self.fixed_metadata_dict['flagDeflipData']
147
147
148 self.dataOut.flagShiftFFT = self.fixed_metadata_dict['flagShiftFFT']
148 self.dataOut.flagShiftFFT = self.fixed_metadata_dict['flagShiftFFT']
149
149
150 self.dataOut.useLocalTime = self.fixed_metadata_dict['useLocalTime']
150 self.dataOut.useLocalTime = self.fixed_metadata_dict['useLocalTime']
151 except:
151 except:
152 pass
152 pass
153
153
154 self.dataOut.ippSeconds = ippSeconds
154 self.dataOut.ippSeconds = ippSeconds
155
155
156 # Time interval between profiles
156 # Time interval between profiles
157 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
157 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
158
158
159 self.dataOut.frequency = self.__frequency
159 self.dataOut.frequency = self.__frequency
160
160
161 self.dataOut.realtime = self.__online
161 self.dataOut.realtime = self.__online
162
162
163 def findDatafiles(self, path, startDate=None, endDate=None):
163 def findDatafiles(self, path, startDate=None, endDate=None):
164
164
165 if not os.path.isdir(path):
165 if not os.path.isdir(path):
166 return []
166 return []
167
167
168 try:
168 try:
169 digitalReadObj = digital_rf.DigitalRFReader(
169 digitalReadObj = digital_rf.DigitalRFReader(
170 path, load_all_metadata=True)
170 path, load_all_metadata=True)
171 except:
171 except:
172 digitalReadObj = digital_rf.DigitalRFReader(path)
172 digitalReadObj = digital_rf.DigitalRFReader(path)
173
173
174 channelNameList = digitalReadObj.get_channels()
174 channelNameList = digitalReadObj.get_channels()
175
175
176 if not channelNameList:
176 if not channelNameList:
177 return []
177 return []
178
178
179 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
179 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
180
180
181 sample_rate = metadata_dict['sample_rate'][0]
181 sample_rate = metadata_dict['sample_rate'][0]
182
182
183 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
183 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
184
184
185 try:
185 try:
186 timezone = this_metadata_file['timezone'].value
186 timezone = this_metadata_file['timezone'].value
187 except:
187 except:
188 timezone = 0
188 timezone = 0
189
189
190 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(
190 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(
191 channelNameList[0]) / sample_rate - timezone
191 channelNameList[0]) / sample_rate - timezone
192
192
193 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
193 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
194 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
194 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
195
195
196 if not startDate:
196 if not startDate:
197 startDate = startDatetime.date()
197 startDate = startDatetime.date()
198
198
199 if not endDate:
199 if not endDate:
200 endDate = endDatatime.date()
200 endDate = endDatatime.date()
201
201
202 dateList = []
202 dateList = []
203
203
204 thisDatetime = startDatetime
204 thisDatetime = startDatetime
205
205
206 while(thisDatetime <= endDatatime):
206 while(thisDatetime <= endDatatime):
207
207
208 thisDate = thisDatetime.date()
208 thisDate = thisDatetime.date()
209
209
210 if thisDate < startDate:
210 if thisDate < startDate:
211 continue
211 continue
212
212
213 if thisDate > endDate:
213 if thisDate > endDate:
214 break
214 break
215
215
216 dateList.append(thisDate)
216 dateList.append(thisDate)
217 thisDatetime += datetime.timedelta(1)
217 thisDatetime += datetime.timedelta(1)
218
218
219 return dateList
219 return dateList
220
220
221 def setup(self, path=None,
221 def setup(self, path=None,
222 startDate=None,
222 startDate=None,
223 endDate=None,
223 endDate=None,
224 startTime=datetime.time(0, 0, 0),
224 startTime=datetime.time(0, 0, 0),
225 endTime=datetime.time(23, 59, 59),
225 endTime=datetime.time(23, 59, 59),
226 channelList=None,
226 channelList=None,
227 nSamples=None,
227 nSamples=None,
228 online=False,
228 online=False,
229 delay=60,
229 delay=60,
230 buffer_size=1024,
230 buffer_size=1024,
231 ippKm=None,
231 ippKm=None,
232 nCohInt=1,
232 nCohInt=1,
233 nCode=1,
233 nCode=1,
234 nBaud=1,
234 nBaud=1,
235 flagDecodeData=False,
235 flagDecodeData=False,
236 code=numpy.ones((1, 1), dtype=numpy.int),
236 code=numpy.ones((1, 1), dtype=numpy.int32),
237 **kwargs):
237 **kwargs):
238 '''
238 '''
239 In this method we should set all initial parameters.
239 In this method we should set all initial parameters.
240
240
241 Inputs:
241 Inputs:
242 path
242 path
243 startDate
243 startDate
244 endDate
244 endDate
245 startTime
245 startTime
246 endTime
246 endTime
247 set
247 set
248 expLabel
248 expLabel
249 ext
249 ext
250 online
250 online
251 delay
251 delay
252 '''
252 '''
253 self.path = path
253 self.path = path
254 self.nCohInt = nCohInt
254 self.nCohInt = nCohInt
255 self.flagDecodeData = flagDecodeData
255 self.flagDecodeData = flagDecodeData
256 self.i = 0
256 self.i = 0
257 if not os.path.isdir(path):
257 if not os.path.isdir(path):
258 raise ValueError("[Reading] Directory %s does not exist" % path)
258 raise ValueError("[Reading] Directory %s does not exist" % path)
259
259
260 try:
260 try:
261 self.digitalReadObj = digital_rf.DigitalRFReader(
261 self.digitalReadObj = digital_rf.DigitalRFReader(
262 path, load_all_metadata=True)
262 path, load_all_metadata=True)
263 except:
263 except:
264 self.digitalReadObj = digital_rf.DigitalRFReader(path)
264 self.digitalReadObj = digital_rf.DigitalRFReader(path)
265
265
266 channelNameList = self.digitalReadObj.get_channels()
266 channelNameList = self.digitalReadObj.get_channels()
267
267
268 if not channelNameList:
268 if not channelNameList:
269 raise ValueError("[Reading] Directory %s does not have any files" % path)
269 raise ValueError("[Reading] Directory %s does not have any files" % path)
270
270
271 if not channelList:
271 if not channelList:
272 channelList = list(range(len(channelNameList)))
272 channelList = list(range(len(channelNameList)))
273
273
274 ########## Reading metadata ######################
274 ########## Reading metadata ######################
275
275
276 top_properties = self.digitalReadObj.get_properties(
276 top_properties = self.digitalReadObj.get_properties(
277 channelNameList[channelList[0]])
277 channelNameList[channelList[0]])
278
278
279 self.__num_subchannels = top_properties['num_subchannels']
279 self.__num_subchannels = top_properties['num_subchannels']
280 self.__sample_rate = 1.0 * \
280 self.__sample_rate = 1.0 * \
281 top_properties['sample_rate_numerator'] / \
281 top_properties['sample_rate_numerator'] / \
282 top_properties['sample_rate_denominator']
282 top_properties['sample_rate_denominator']
283 # self.__samples_per_file = top_properties['samples_per_file'][0]
283 # self.__samples_per_file = top_properties['samples_per_file'][0]
284 self.__deltaHeigth = 1e6 * 0.15 / self.__sample_rate # why 0.15?
284 self.__deltaHeigth = 1e6 * 0.15 / self.__sample_rate # why 0.15?
285
285
286 this_metadata_file = self.digitalReadObj.get_digital_metadata(
286 this_metadata_file = self.digitalReadObj.get_digital_metadata(
287 channelNameList[channelList[0]])
287 channelNameList[channelList[0]])
288 metadata_bounds = this_metadata_file.get_bounds()
288 metadata_bounds = this_metadata_file.get_bounds()
289 self.fixed_metadata_dict = this_metadata_file.read(
289 self.fixed_metadata_dict = this_metadata_file.read(
290 metadata_bounds[0])[metadata_bounds[0]] # GET FIRST HEADER
290 metadata_bounds[0])[metadata_bounds[0]] # GET FIRST HEADER
291
291
292 try:
292 try:
293 self.__processingHeader = self.fixed_metadata_dict['processingHeader']
293 self.__processingHeader = self.fixed_metadata_dict['processingHeader']
294 self.__radarControllerHeader = self.fixed_metadata_dict['radarControllerHeader']
294 self.__radarControllerHeader = self.fixed_metadata_dict['radarControllerHeader']
295 self.__systemHeader = self.fixed_metadata_dict['systemHeader']
295 self.__systemHeader = self.fixed_metadata_dict['systemHeader']
296 self.dtype = pickle.loads(self.fixed_metadata_dict['dtype'])
296 self.dtype = pickle.loads(self.fixed_metadata_dict['dtype'])
297 except:
297 except:
298 pass
298 pass
299
299
300 self.__frequency = None
300 self.__frequency = None
301
301
302 self.__frequency = self.fixed_metadata_dict.get('frequency', 1)
302 self.__frequency = self.fixed_metadata_dict.get('frequency', 1)
303
303
304 self.__timezone = self.fixed_metadata_dict.get('timezone', 18000)
304 self.__timezone = self.fixed_metadata_dict.get('timezone', 18000)
305
305
306 try:
306 try:
307 nSamples = self.fixed_metadata_dict['nSamples']
307 nSamples = self.fixed_metadata_dict['nSamples']
308 except:
308 except:
309 nSamples = None
309 nSamples = None
310
310
311 self.__firstHeigth = 0
311 self.__firstHeigth = 0
312
312
313 try:
313 try:
314 codeType = self.__radarControllerHeader['codeType']
314 codeType = self.__radarControllerHeader['codeType']
315 except:
315 except:
316 codeType = 0
316 codeType = 0
317
317
318 try:
318 try:
319 if codeType:
319 if codeType:
320 nCode = self.__radarControllerHeader['nCode']
320 nCode = self.__radarControllerHeader['nCode']
321 nBaud = self.__radarControllerHeader['nBaud']
321 nBaud = self.__radarControllerHeader['nBaud']
322 code = self.__radarControllerHeader['code']
322 code = self.__radarControllerHeader['code']
323 except:
323 except:
324 pass
324 pass
325
325
326 if not ippKm:
326 if not ippKm:
327 try:
327 try:
328 # seconds to km
328 # seconds to km
329 ippKm = self.__radarControllerHeader['ipp']
329 ippKm = self.__radarControllerHeader['ipp']
330 except:
330 except:
331 ippKm = None
331 ippKm = None
332 ####################################################
332 ####################################################
333 self.__ippKm = ippKm
333 self.__ippKm = ippKm
334 startUTCSecond = None
334 startUTCSecond = None
335 endUTCSecond = None
335 endUTCSecond = None
336
336
337 if startDate:
337 if startDate:
338 startDatetime = datetime.datetime.combine(startDate, startTime)
338 startDatetime = datetime.datetime.combine(startDate, startTime)
339 startUTCSecond = (
339 startUTCSecond = (
340 startDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone
340 startDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone
341
341
342 if endDate:
342 if endDate:
343 endDatetime = datetime.datetime.combine(endDate, endTime)
343 endDatetime = datetime.datetime.combine(endDate, endTime)
344 endUTCSecond = (endDatetime - datetime.datetime(1970,
344 endUTCSecond = (endDatetime - datetime.datetime(1970,
345 1, 1)).total_seconds() + self.__timezone
345 1, 1)).total_seconds() + self.__timezone
346
346
347 start_index, end_index = self.digitalReadObj.get_bounds(
347 start_index, end_index = self.digitalReadObj.get_bounds(
348 channelNameList[channelList[0]])
348 channelNameList[channelList[0]])
349
349
350 if not startUTCSecond:
350 if not startUTCSecond:
351 startUTCSecond = start_index / self.__sample_rate
351 startUTCSecond = start_index / self.__sample_rate
352
352
353 if start_index > startUTCSecond * self.__sample_rate:
353 if start_index > startUTCSecond * self.__sample_rate:
354 startUTCSecond = start_index / self.__sample_rate
354 startUTCSecond = start_index / self.__sample_rate
355
355
356 if not endUTCSecond:
356 if not endUTCSecond:
357 endUTCSecond = end_index / self.__sample_rate
357 endUTCSecond = end_index / self.__sample_rate
358
358
359 if end_index < endUTCSecond * self.__sample_rate:
359 if end_index < endUTCSecond * self.__sample_rate:
360 endUTCSecond = end_index / self.__sample_rate
360 endUTCSecond = end_index / self.__sample_rate
361 if not nSamples:
361 if not nSamples:
362 if not ippKm:
362 if not ippKm:
363 raise ValueError("[Reading] nSamples or ippKm should be defined")
363 raise ValueError("[Reading] nSamples or ippKm should be defined")
364 nSamples = int(ippKm / (1e6 * 0.15 / self.__sample_rate))
364 nSamples = int(ippKm / (1e6 * 0.15 / self.__sample_rate))
365 channelBoundList = []
365 channelBoundList = []
366 channelNameListFiltered = []
366 channelNameListFiltered = []
367
367
368 for thisIndexChannel in channelList:
368 for thisIndexChannel in channelList:
369 thisChannelName = channelNameList[thisIndexChannel]
369 thisChannelName = channelNameList[thisIndexChannel]
370 start_index, end_index = self.digitalReadObj.get_bounds(
370 start_index, end_index = self.digitalReadObj.get_bounds(
371 thisChannelName)
371 thisChannelName)
372 channelBoundList.append((start_index, end_index))
372 channelBoundList.append((start_index, end_index))
373 channelNameListFiltered.append(thisChannelName)
373 channelNameListFiltered.append(thisChannelName)
374
374
375 self.profileIndex = 0
375 self.profileIndex = 0
376 self.i = 0
376 self.i = 0
377 self.__delay = delay
377 self.__delay = delay
378
378
379 self.__codeType = codeType
379 self.__codeType = codeType
380 self.__nCode = nCode
380 self.__nCode = nCode
381 self.__nBaud = nBaud
381 self.__nBaud = nBaud
382 self.__code = code
382 self.__code = code
383
383
384 self.__datapath = path
384 self.__datapath = path
385 self.__online = online
385 self.__online = online
386 self.__channelList = channelList
386 self.__channelList = channelList
387 self.__channelNameList = channelNameListFiltered
387 self.__channelNameList = channelNameListFiltered
388 self.__channelBoundList = channelBoundList
388 self.__channelBoundList = channelBoundList
389 self.__nSamples = nSamples
389 self.__nSamples = nSamples
390 self.__samples_to_read = int(nSamples) # FIJO: AHORA 40
390 self.__samples_to_read = int(nSamples) # FIJO: AHORA 40
391 self.__nChannels = len(self.__channelList)
391 self.__nChannels = len(self.__channelList)
392
392
393 self.__startUTCSecond = startUTCSecond
393 self.__startUTCSecond = startUTCSecond
394 self.__endUTCSecond = endUTCSecond
394 self.__endUTCSecond = endUTCSecond
395
395
396 self.__timeInterval = 1.0 * self.__samples_to_read / \
396 self.__timeInterval = 1.0 * self.__samples_to_read / \
397 self.__sample_rate # Time interval
397 self.__sample_rate # Time interval
398
398
399 if online:
399 if online:
400 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
400 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
401 startUTCSecond = numpy.floor(endUTCSecond)
401 startUTCSecond = numpy.floor(endUTCSecond)
402
402
403 # por que en el otro metodo lo primero q se hace es sumar samplestoread
403 # 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
404 self.__thisUnixSample = int(startUTCSecond * self.__sample_rate) - self.__samples_to_read
405
405
406 self.__data_buffer = numpy.zeros(
406 self.__data_buffer = numpy.zeros(
407 (self.__num_subchannels, self.__samples_to_read), dtype=numpy.complex)
407 (self.__num_subchannels, self.__samples_to_read), dtype=complex)
408
408
409 self.__setFileHeader()
409 self.__setFileHeader()
410 self.isConfig = True
410 self.isConfig = True
411
411
412 print("[Reading] Digital RF Data was found from %s to %s " % (
412 print("[Reading] Digital RF Data was found from %s to %s " % (
413 datetime.datetime.utcfromtimestamp(
413 datetime.datetime.utcfromtimestamp(
414 self.__startUTCSecond - self.__timezone),
414 self.__startUTCSecond - self.__timezone),
415 datetime.datetime.utcfromtimestamp(
415 datetime.datetime.utcfromtimestamp(
416 self.__endUTCSecond - self.__timezone)
416 self.__endUTCSecond - self.__timezone)
417 ))
417 ))
418
418
419 print("[Reading] Starting process from %s to %s" % (datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
419 print("[Reading] Starting process from %s to %s" % (datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
420 datetime.datetime.utcfromtimestamp(
420 datetime.datetime.utcfromtimestamp(
421 endUTCSecond - self.__timezone)
421 endUTCSecond - self.__timezone)
422 ))
422 ))
423 self.oldAverage = None
423 self.oldAverage = None
424 self.count = 0
424 self.count = 0
425 self.executionTime = 0
425 self.executionTime = 0
426
426
427 def __reload(self):
427 def __reload(self):
428 # print
428 # print
429 # print "%s not in range [%s, %s]" %(
429 # print "%s not in range [%s, %s]" %(
430 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
430 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
431 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
431 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
432 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
432 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
433 # )
433 # )
434 print("[Reading] reloading metadata ...")
434 print("[Reading] reloading metadata ...")
435
435
436 try:
436 try:
437 self.digitalReadObj.reload(complete_update=True)
437 self.digitalReadObj.reload(complete_update=True)
438 except:
438 except:
439 self.digitalReadObj = digital_rf.DigitalRFReader(self.path)
439 self.digitalReadObj = digital_rf.DigitalRFReader(self.path)
440
440
441 start_index, end_index = self.digitalReadObj.get_bounds(
441 start_index, end_index = self.digitalReadObj.get_bounds(
442 self.__channelNameList[self.__channelList[0]])
442 self.__channelNameList[self.__channelList[0]])
443
443
444 if start_index > self.__startUTCSecond * self.__sample_rate:
444 if start_index > self.__startUTCSecond * self.__sample_rate:
445 self.__startUTCSecond = 1.0 * start_index / self.__sample_rate
445 self.__startUTCSecond = 1.0 * start_index / self.__sample_rate
446
446
447 if end_index > self.__endUTCSecond * self.__sample_rate:
447 if end_index > self.__endUTCSecond * self.__sample_rate:
448 self.__endUTCSecond = 1.0 * end_index / self.__sample_rate
448 self.__endUTCSecond = 1.0 * end_index / self.__sample_rate
449 print()
449 print()
450 print("[Reading] New timerange found [%s, %s] " % (
450 print("[Reading] New timerange found [%s, %s] " % (
451 datetime.datetime.utcfromtimestamp(
451 datetime.datetime.utcfromtimestamp(
452 self.__startUTCSecond - self.__timezone),
452 self.__startUTCSecond - self.__timezone),
453 datetime.datetime.utcfromtimestamp(
453 datetime.datetime.utcfromtimestamp(
454 self.__endUTCSecond - self.__timezone)
454 self.__endUTCSecond - self.__timezone)
455 ))
455 ))
456
456
457 return True
457 return True
458
458
459 return False
459 return False
460
460
461 def timeit(self, toExecute):
461 def timeit(self, toExecute):
462 t0 = time.time()
462 t0 = time.time()
463 toExecute()
463 toExecute()
464 self.executionTime = time.time() - t0
464 self.executionTime = time.time() - t0
465 if self.oldAverage is None:
465 if self.oldAverage is None:
466 self.oldAverage = self.executionTime
466 self.oldAverage = self.executionTime
467 self.oldAverage = (self.executionTime + self.count *
467 self.oldAverage = (self.executionTime + self.count *
468 self.oldAverage) / (self.count + 1.0)
468 self.oldAverage) / (self.count + 1.0)
469 self.count = self.count + 1.0
469 self.count = self.count + 1.0
470 return
470 return
471
471
472 def __readNextBlock(self, seconds=30, volt_scale=1):
472 def __readNextBlock(self, seconds=30, volt_scale=1):
473 '''
473 '''
474 '''
474 '''
475
475
476 # Set the next data
476 # Set the next data
477 self.__flagDiscontinuousBlock = False
477 self.__flagDiscontinuousBlock = False
478 self.__thisUnixSample += self.__samples_to_read
478 self.__thisUnixSample += self.__samples_to_read
479
479
480 if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate:
480 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")
481 print ("[Reading] There are no more data into selected time-range")
482 if self.__online:
482 if self.__online:
483 sleep(3)
483 sleep(3)
484 self.__reload()
484 self.__reload()
485 else:
485 else:
486 return False
486 return False
487
487
488 if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate:
488 if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate:
489 return False
489 return False
490 self.__thisUnixSample -= self.__samples_to_read
490 self.__thisUnixSample -= self.__samples_to_read
491
491
492 indexChannel = 0
492 indexChannel = 0
493
493
494 dataOk = False
494 dataOk = False
495
495
496 for thisChannelName in self.__channelNameList: # TODO VARIOS CHANNELS?
496 for thisChannelName in self.__channelNameList: # TODO VARIOS CHANNELS?
497 for indexSubchannel in range(self.__num_subchannels):
497 for indexSubchannel in range(self.__num_subchannels):
498 try:
498 try:
499 t0 = time()
499 t0 = time()
500 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
500 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
501 self.__samples_to_read,
501 self.__samples_to_read,
502 thisChannelName, sub_channel=indexSubchannel)
502 thisChannelName, sub_channel=indexSubchannel)
503 self.executionTime = time() - t0
503 self.executionTime = time() - t0
504 if self.oldAverage is None:
504 if self.oldAverage is None:
505 self.oldAverage = self.executionTime
505 self.oldAverage = self.executionTime
506 self.oldAverage = (
506 self.oldAverage = (
507 self.executionTime + self.count * self.oldAverage) / (self.count + 1.0)
507 self.executionTime + self.count * self.oldAverage) / (self.count + 1.0)
508 self.count = self.count + 1.0
508 self.count = self.count + 1.0
509
509
510 except IOError as e:
510 except IOError as e:
511 # read next profile
511 # read next profile
512 self.__flagDiscontinuousBlock = True
512 self.__flagDiscontinuousBlock = True
513 print("[Reading] %s" % datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e)
513 print("[Reading] %s" % datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e)
514 break
514 break
515
515
516 if result.shape[0] != self.__samples_to_read:
516 if result.shape[0] != self.__samples_to_read:
517 self.__flagDiscontinuousBlock = True
517 self.__flagDiscontinuousBlock = True
518 print("[Reading] %s: Too few samples were found, just %d/%d samples" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
518 print("[Reading] %s: Too few samples were found, just %d/%d samples" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
519 result.shape[0],
519 result.shape[0],
520 self.__samples_to_read))
520 self.__samples_to_read))
521 break
521 break
522
522
523 self.__data_buffer[indexSubchannel, :] = result * volt_scale
523 self.__data_buffer[indexSubchannel, :] = result * volt_scale
524 indexChannel += 1
524 indexChannel += 1
525
525
526 dataOk = True
526 dataOk = True
527
527
528 self.__utctime = self.__thisUnixSample / self.__sample_rate
528 self.__utctime = self.__thisUnixSample / self.__sample_rate
529
529
530 if not dataOk:
530 if not dataOk:
531 return False
531 return False
532
532
533 print("[Reading] %s: %d samples <> %f sec" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
533 print("[Reading] %s: %d samples <> %f sec" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
534 self.__samples_to_read,
534 self.__samples_to_read,
535 self.__timeInterval))
535 self.__timeInterval))
536
536
537 self.__bufferIndex = 0
537 self.__bufferIndex = 0
538
538
539 return True
539 return True
540
540
541 def __isBufferEmpty(self):
541 def __isBufferEmpty(self):
542 return self.__bufferIndex > self.__samples_to_read - self.__nSamples # 40960 - 40
542 return self.__bufferIndex > self.__samples_to_read - self.__nSamples # 40960 - 40
543
543
544 def getData(self, seconds=30, nTries=5):
544 def getData(self, seconds=30, nTries=5):
545 '''
545 '''
546 This method gets the data from files and put the data into the dataOut object
546 This method gets the data from files and put the data into the dataOut object
547
547
548 In addition, increase el the buffer counter in one.
548 In addition, increase el the buffer counter in one.
549
549
550 Return:
550 Return:
551 data : retorna un perfil de voltages (alturas * canales) copiados desde el
551 data : retorna un perfil de voltages (alturas * canales) copiados desde el
552 buffer. Si no hay mas archivos a leer retorna None.
552 buffer. Si no hay mas archivos a leer retorna None.
553
553
554 Affected:
554 Affected:
555 self.dataOut
555 self.dataOut
556 self.profileIndex
556 self.profileIndex
557 self.flagDiscontinuousBlock
557 self.flagDiscontinuousBlock
558 self.flagIsNewBlock
558 self.flagIsNewBlock
559 '''
559 '''
560 # print("getdata")
560 # print("getdata")
561 err_counter = 0
561 err_counter = 0
562 self.dataOut.flagNoData = True
562 self.dataOut.flagNoData = True
563
563
564 if self.__isBufferEmpty():
564 if self.__isBufferEmpty():
565 # print("hi")
565 # print("hi")
566 self.__flagDiscontinuousBlock = False
566 self.__flagDiscontinuousBlock = False
567
567
568 while True:
568 while True:
569 # print ("q ha pasado")
569 # print ("q ha pasado")
570 if self.__readNextBlock():
570 if self.__readNextBlock():
571 break
571 break
572 if self.__thisUnixSample > self.__endUTCSecond * self.__sample_rate:
572 if self.__thisUnixSample > self.__endUTCSecond * self.__sample_rate:
573 raise schainpy.admin.SchainError('Error')
573 raise schainpy.admin.SchainError('Error')
574 return
574 return
575
575
576 if self.__flagDiscontinuousBlock:
576 if self.__flagDiscontinuousBlock:
577 raise schainpy.admin.SchainError('discontinuous block found')
577 raise schainpy.admin.SchainError('discontinuous block found')
578 return
578 return
579
579
580 if not self.__online:
580 if not self.__online:
581 raise schainpy.admin.SchainError('Online?')
581 raise schainpy.admin.SchainError('Online?')
582 return
582 return
583
583
584 err_counter += 1
584 err_counter += 1
585 if err_counter > nTries:
585 if err_counter > nTries:
586 raise schainpy.admin.SchainError('Max retrys reach')
586 raise schainpy.admin.SchainError('Max retrys reach')
587 return
587 return
588
588
589 print('[Reading] waiting %d seconds to read a new block' % seconds)
589 print('[Reading] waiting %d seconds to read a new block' % seconds)
590 time.sleep(seconds)
590 time.sleep(seconds)
591
591
592 self.dataOut.data = self.__data_buffer[:, self.__bufferIndex:self.__bufferIndex + self.__nSamples]
592 self.dataOut.data = self.__data_buffer[:, self.__bufferIndex:self.__bufferIndex + self.__nSamples]
593 self.dataOut.utctime = (self.__thisUnixSample + self.__bufferIndex) / self.__sample_rate
593 self.dataOut.utctime = (self.__thisUnixSample + self.__bufferIndex) / self.__sample_rate
594 self.dataOut.flagNoData = False
594 self.dataOut.flagNoData = False
595 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
595 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
596 self.dataOut.profileIndex = self.profileIndex
596 self.dataOut.profileIndex = self.profileIndex
597
597
598 self.__bufferIndex += self.__nSamples
598 self.__bufferIndex += self.__nSamples
599 self.profileIndex += 1
599 self.profileIndex += 1
600
600
601 if self.profileIndex == self.dataOut.nProfiles:
601 if self.profileIndex == self.dataOut.nProfiles:
602 self.profileIndex = 0
602 self.profileIndex = 0
603
603
604 return True
604 return True
605
605
606 def printInfo(self):
606 def printInfo(self):
607 '''
607 '''
608 '''
608 '''
609 if self.__printInfo == False:
609 if self.__printInfo == False:
610 return
610 return
611
611
612 # self.systemHeaderObj.printInfo()
612 # self.systemHeaderObj.printInfo()
613 # self.radarControllerHeaderObj.printInfo()
613 # self.radarControllerHeaderObj.printInfo()
614
614
615 self.__printInfo = False
615 self.__printInfo = False
616
616
617 def printNumberOfBlock(self):
617 def printNumberOfBlock(self):
618 '''
618 '''
619 '''
619 '''
620 return
620 return
621 # print self.profileIndex
621 # print self.profileIndex
622
622
623 def run(self, **kwargs):
623 def run(self, **kwargs):
624 '''
624 '''
625 This method will be called many times so here you should put all your code
625 This method will be called many times so here you should put all your code
626 '''
626 '''
627
627
628 if not self.isConfig:
628 if not self.isConfig:
629 self.setup(**kwargs)
629 self.setup(**kwargs)
630 # self.i = self.i+1
630 # self.i = self.i+1
631 self.getData(seconds=self.__delay)
631 self.getData(seconds=self.__delay)
632
632
633 return
633 return
634
634
635 @MPDecorator
635 @MPDecorator
636 class DigitalRFWriter(Operation):
636 class DigitalRFWriter(Operation):
637 '''
637 '''
638 classdocs
638 classdocs
639 '''
639 '''
640
640
641 def __init__(self, **kwargs):
641 def __init__(self, **kwargs):
642 '''
642 '''
643 Constructor
643 Constructor
644 '''
644 '''
645 Operation.__init__(self, **kwargs)
645 Operation.__init__(self, **kwargs)
646 self.metadata_dict = {}
646 self.metadata_dict = {}
647 self.dataOut = None
647 self.dataOut = None
648 self.dtype = None
648 self.dtype = None
649 self.oldAverage = 0
649 self.oldAverage = 0
650
650
651 def setHeader(self):
651 def setHeader(self):
652
652
653 self.metadata_dict['frequency'] = self.dataOut.frequency
653 self.metadata_dict['frequency'] = self.dataOut.frequency
654 self.metadata_dict['timezone'] = self.dataOut.timeZone
654 self.metadata_dict['timezone'] = self.dataOut.timeZone
655 self.metadata_dict['dtype'] = pickle.dumps(self.dataOut.dtype)
655 self.metadata_dict['dtype'] = pickle.dumps(self.dataOut.dtype)
656 self.metadata_dict['nProfiles'] = self.dataOut.nProfiles
656 self.metadata_dict['nProfiles'] = self.dataOut.nProfiles
657 self.metadata_dict['heightList'] = self.dataOut.heightList
657 self.metadata_dict['heightList'] = self.dataOut.heightList
658 self.metadata_dict['channelList'] = self.dataOut.channelList
658 self.metadata_dict['channelList'] = self.dataOut.channelList
659 self.metadata_dict['flagDecodeData'] = self.dataOut.flagDecodeData
659 self.metadata_dict['flagDecodeData'] = self.dataOut.flagDecodeData
660 self.metadata_dict['flagDeflipData'] = self.dataOut.flagDeflipData
660 self.metadata_dict['flagDeflipData'] = self.dataOut.flagDeflipData
661 self.metadata_dict['flagShiftFFT'] = self.dataOut.flagShiftFFT
661 self.metadata_dict['flagShiftFFT'] = self.dataOut.flagShiftFFT
662 self.metadata_dict['useLocalTime'] = self.dataOut.useLocalTime
662 self.metadata_dict['useLocalTime'] = self.dataOut.useLocalTime
663 self.metadata_dict['nCohInt'] = self.dataOut.nCohInt
663 self.metadata_dict['nCohInt'] = self.dataOut.nCohInt
664 self.metadata_dict['type'] = self.dataOut.type
664 self.metadata_dict['type'] = self.dataOut.type
665 self.metadata_dict['flagDataAsBlock'] = getattr(
665 self.metadata_dict['flagDataAsBlock'] = getattr(
666 self.dataOut, 'flagDataAsBlock', None) # chequear
666 self.dataOut, 'flagDataAsBlock', None) # chequear
667
667
668 def setup(self, dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, set=0, metadataFile='metadata', ext='.h5'):
668 def setup(self, dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, set=0, metadataFile='metadata', ext='.h5'):
669 '''
669 '''
670 In this method we should set all initial parameters.
670 In this method we should set all initial parameters.
671 Input:
671 Input:
672 dataOut: Input data will also be outputa data
672 dataOut: Input data will also be outputa data
673 '''
673 '''
674 self.setHeader()
674 self.setHeader()
675 self.__ippSeconds = dataOut.ippSeconds
675 self.__ippSeconds = dataOut.ippSeconds
676 self.__deltaH = dataOut.getDeltaH()
676 self.__deltaH = dataOut.getDeltaH()
677 self.__sample_rate = 1e6 * 0.15 / self.__deltaH
677 self.__sample_rate = 1e6 * 0.15 / self.__deltaH
678 self.__dtype = dataOut.dtype
678 self.__dtype = dataOut.dtype
679 if len(dataOut.dtype) == 2:
679 if len(dataOut.dtype) == 2:
680 self.__dtype = dataOut.dtype[0]
680 self.__dtype = dataOut.dtype[0]
681 self.__nSamples = dataOut.systemHeaderObj.nSamples
681 self.__nSamples = dataOut.systemHeaderObj.nSamples
682 self.__nProfiles = dataOut.nProfiles
682 self.__nProfiles = dataOut.nProfiles
683
683
684 if self.dataOut.type != 'Voltage':
684 if self.dataOut.type != 'Voltage':
685 raise 'Digital RF cannot be used with this data type'
685 raise 'Digital RF cannot be used with this data type'
686 self.arr_data = numpy.ones((1, dataOut.nFFTPoints * len(
686 self.arr_data = numpy.ones((1, dataOut.nFFTPoints * len(
687 self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)])
687 self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)])
688 else:
688 else:
689 self.arr_data = numpy.ones((self.__nSamples, len(
689 self.arr_data = numpy.ones((self.__nSamples, len(
690 self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)])
690 self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)])
691
691
692 file_cadence_millisecs = 1000
692 file_cadence_millisecs = 1000
693
693
694 sample_rate_fraction = Fraction(self.__sample_rate).limit_denominator()
694 sample_rate_fraction = Fraction(self.__sample_rate).limit_denominator()
695 sample_rate_numerator = int(sample_rate_fraction.numerator)
695 sample_rate_numerator = int(sample_rate_fraction.numerator)
696 sample_rate_denominator = int(sample_rate_fraction.denominator)
696 sample_rate_denominator = int(sample_rate_fraction.denominator)
697 start_global_index = dataOut.utctime * self.__sample_rate
697 start_global_index = dataOut.utctime * self.__sample_rate
698
698
699 uuid = 'prueba'
699 uuid = 'prueba'
700 compression_level = 0
700 compression_level = 0
701 checksum = False
701 checksum = False
702 is_complex = True
702 is_complex = True
703 num_subchannels = len(dataOut.channelList)
703 num_subchannels = len(dataOut.channelList)
704 is_continuous = True
704 is_continuous = True
705 marching_periods = False
705 marching_periods = False
706
706
707 self.digitalWriteObj = digital_rf.DigitalRFWriter(path, self.__dtype, dirCadence,
707 self.digitalWriteObj = digital_rf.DigitalRFWriter(path, self.__dtype, dirCadence,
708 fileCadence, start_global_index,
708 fileCadence, start_global_index,
709 sample_rate_numerator, sample_rate_denominator, uuid, compression_level, checksum,
709 sample_rate_numerator, sample_rate_denominator, uuid, compression_level, checksum,
710 is_complex, num_subchannels, is_continuous, marching_periods)
710 is_complex, num_subchannels, is_continuous, marching_periods)
711 metadata_dir = os.path.join(path, 'metadata')
711 metadata_dir = os.path.join(path, 'metadata')
712 os.system('mkdir %s' % (metadata_dir))
712 os.system('mkdir %s' % (metadata_dir))
713 self.digitalMetadataWriteObj = digital_rf.DigitalMetadataWriter(metadata_dir, dirCadence, 1, # 236, file_cadence_millisecs / 1000
713 self.digitalMetadataWriteObj = digital_rf.DigitalMetadataWriter(metadata_dir, dirCadence, 1, # 236, file_cadence_millisecs / 1000
714 sample_rate_numerator, sample_rate_denominator,
714 sample_rate_numerator, sample_rate_denominator,
715 metadataFile)
715 metadataFile)
716 self.isConfig = True
716 self.isConfig = True
717 self.currentSample = 0
717 self.currentSample = 0
718 self.oldAverage = 0
718 self.oldAverage = 0
719 self.count = 0
719 self.count = 0
720 return
720 return
721
721
722 def writeMetadata(self):
722 def writeMetadata(self):
723 start_idx = self.__sample_rate * self.dataOut.utctime
723 start_idx = self.__sample_rate * self.dataOut.utctime
724
724
725 self.metadata_dict['processingHeader'] = self.dataOut.processingHeaderObj.getAsDict(
725 self.metadata_dict['processingHeader'] = self.dataOut.processingHeaderObj.getAsDict(
726 )
726 )
727 self.metadata_dict['radarControllerHeader'] = self.dataOut.radarControllerHeaderObj.getAsDict(
727 self.metadata_dict['radarControllerHeader'] = self.dataOut.radarControllerHeaderObj.getAsDict(
728 )
728 )
729 self.metadata_dict['systemHeader'] = self.dataOut.systemHeaderObj.getAsDict(
729 self.metadata_dict['systemHeader'] = self.dataOut.systemHeaderObj.getAsDict(
730 )
730 )
731 self.digitalMetadataWriteObj.write(start_idx, self.metadata_dict)
731 self.digitalMetadataWriteObj.write(start_idx, self.metadata_dict)
732 return
732 return
733
733
734 def timeit(self, toExecute):
734 def timeit(self, toExecute):
735 t0 = time()
735 t0 = time()
736 toExecute()
736 toExecute()
737 self.executionTime = time() - t0
737 self.executionTime = time() - t0
738 if self.oldAverage is None:
738 if self.oldAverage is None:
739 self.oldAverage = self.executionTime
739 self.oldAverage = self.executionTime
740 self.oldAverage = (self.executionTime + self.count *
740 self.oldAverage = (self.executionTime + self.count *
741 self.oldAverage) / (self.count + 1.0)
741 self.oldAverage) / (self.count + 1.0)
742 self.count = self.count + 1.0
742 self.count = self.count + 1.0
743 return
743 return
744
744
745 def writeData(self):
745 def writeData(self):
746 if self.dataOut.type != 'Voltage':
746 if self.dataOut.type != 'Voltage':
747 raise 'Digital RF cannot be used with this data type'
747 raise 'Digital RF cannot be used with this data type'
748 for channel in self.dataOut.channelList:
748 for channel in self.dataOut.channelList:
749 for i in range(self.dataOut.nFFTPoints):
749 for i in range(self.dataOut.nFFTPoints):
750 self.arr_data[1][channel * self.dataOut.nFFTPoints +
750 self.arr_data[1][channel * self.dataOut.nFFTPoints +
751 i]['r'] = self.dataOut.data[channel][i].real
751 i]['r'] = self.dataOut.data[channel][i].real
752 self.arr_data[1][channel * self.dataOut.nFFTPoints +
752 self.arr_data[1][channel * self.dataOut.nFFTPoints +
753 i]['i'] = self.dataOut.data[channel][i].imag
753 i]['i'] = self.dataOut.data[channel][i].imag
754 else:
754 else:
755 for i in range(self.dataOut.systemHeaderObj.nSamples):
755 for i in range(self.dataOut.systemHeaderObj.nSamples):
756 for channel in self.dataOut.channelList:
756 for channel in self.dataOut.channelList:
757 self.arr_data[i][channel]['r'] = self.dataOut.data[channel][i].real
757 self.arr_data[i][channel]['r'] = self.dataOut.data[channel][i].real
758 self.arr_data[i][channel]['i'] = self.dataOut.data[channel][i].imag
758 self.arr_data[i][channel]['i'] = self.dataOut.data[channel][i].imag
759
759
760 def f(): return self.digitalWriteObj.rf_write(self.arr_data)
760 def f(): return self.digitalWriteObj.rf_write(self.arr_data)
761 self.timeit(f)
761 self.timeit(f)
762
762
763 return
763 return
764
764
765 def run(self, dataOut, frequency=49.92e6, path=None, fileCadence=1000, dirCadence=36000, metadataCadence=1, **kwargs):
765 def run(self, dataOut, frequency=49.92e6, path=None, fileCadence=1000, dirCadence=36000, metadataCadence=1, **kwargs):
766 '''
766 '''
767 This method will be called many times so here you should put all your code
767 This method will be called many times so here you should put all your code
768 Inputs:
768 Inputs:
769 dataOut: object with the data
769 dataOut: object with the data
770 '''
770 '''
771 # print dataOut.__dict__
771 # print dataOut.__dict__
772 self.dataOut = dataOut
772 self.dataOut = dataOut
773 if not self.isConfig:
773 if not self.isConfig:
774 self.setup(dataOut, path, frequency, fileCadence,
774 self.setup(dataOut, path, frequency, fileCadence,
775 dirCadence, metadataCadence, **kwargs)
775 dirCadence, metadataCadence, **kwargs)
776 self.writeMetadata()
776 self.writeMetadata()
777
777
778 self.writeData()
778 self.writeData()
779
779
780 # # self.currentSample += 1
780 # # self.currentSample += 1
781 # if self.dataOut.flagDataAsBlock or self.currentSample == 1:
781 # if self.dataOut.flagDataAsBlock or self.currentSample == 1:
782 # self.writeMetadata()
782 # self.writeMetadata()
783 # # if self.currentSample == self.__nProfiles: self.currentSample = 0
783 # # if self.currentSample == self.__nProfiles: self.currentSample = 0
784
784
785 return dataOut # en la version 2.7 no aparece este return
785 return dataOut # en la version 2.7 no aparece este return
786
786
787 def close(self):
787 def close(self):
788 print('[Writing] - Closing files ')
788 print('[Writing] - Closing files ')
789 print('Average of writing to digital rf format is ', self.oldAverage * 1000)
789 print('Average of writing to digital rf format is ', self.oldAverage * 1000)
790 try:
790 try:
791 self.digitalWriteObj.close()
791 self.digitalWriteObj.close()
792 except:
792 except:
793 pass
793 pass
@@ -1,862 +1,862
1 '''
1 '''
2 Created on Jul 3, 2014
2 Created on Jul 3, 2014
3
3
4 @author: roj-com0419
4 @author: roj-com0419
5 '''
5 '''
6
6
7 import os, sys
7 import os, sys
8 import time, datetime
8 import time, datetime
9 import h5py
9 import h5py
10 import numpy
10 import numpy
11 import fnmatch
11 import fnmatch
12 import re
12 import re
13
13
14 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
14 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
15 from schainpy.model.data.jrodata import Voltage
15 from schainpy.model.data.jrodata import Voltage
16 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
16 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
17
17
18
18
19 def isNumber(str):
19 def isNumber(str):
20 """
20 """
21 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
21 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
22
22
23 Excepciones:
23 Excepciones:
24 Si un determinado string no puede ser convertido a numero
24 Si un determinado string no puede ser convertido a numero
25 Input:
25 Input:
26 str, string al cual se le analiza para determinar si convertible a un numero o no
26 str, string al cual se le analiza para determinar si convertible a un numero o no
27
27
28 Return:
28 Return:
29 True : si el string es uno numerico
29 True : si el string es uno numerico
30 False : no es un string numerico
30 False : no es un string numerico
31 """
31 """
32 try:
32 try:
33 float(str)
33 float(str)
34 return True
34 return True
35 except:
35 except:
36 return False
36 return False
37
37
38 def getFileFromSet(path, ext, set=None):
38 def getFileFromSet(path, ext, set=None):
39 validFilelist = []
39 validFilelist = []
40 fileList = os.listdir(path)
40 fileList = os.listdir(path)
41
41
42
42
43 if len(fileList) < 1:
43 if len(fileList) < 1:
44 return None
44 return None
45
45
46 # 0 1234 567 89A BCDE
46 # 0 1234 567 89A BCDE
47 # H YYYY DDD SSS .ext
47 # H YYYY DDD SSS .ext
48
48
49 for thisFile in fileList:
49 for thisFile in fileList:
50 try:
50 try:
51 number = int(thisFile[6:16])
51 number = int(thisFile[6:16])
52
52
53 # year = int(thisFile[1:5])
53 # year = int(thisFile[1:5])
54 # doy = int(thisFile[5:8])
54 # doy = int(thisFile[5:8])
55 except:
55 except:
56 continue
56 continue
57
57
58 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
58 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
59 continue
59 continue
60
60
61 validFilelist.append(thisFile)
61 validFilelist.append(thisFile)
62
62
63 if len(validFilelist) < 1:
63 if len(validFilelist) < 1:
64 return None
64 return None
65
65
66 validFilelist = sorted(validFilelist, key=str.lower)
66 validFilelist = sorted(validFilelist, key=str.lower)
67
67
68 if set == None:
68 if set == None:
69 return validFilelist[-1]
69 return validFilelist[-1]
70
70
71 print("set =" , set)
71 print("set =" , set)
72 for thisFile in validFilelist:
72 for thisFile in validFilelist:
73 if set <= int(thisFile[6:16]):
73 if set <= int(thisFile[6:16]):
74 print(thisFile, int(thisFile[6:16]))
74 print(thisFile, int(thisFile[6:16]))
75 return thisFile
75 return thisFile
76
76
77 return validFilelist[-1]
77 return validFilelist[-1]
78
78
79 myfile = fnmatch.filter(validFilelist, '*%10d*' % (set))
79 myfile = fnmatch.filter(validFilelist, '*%10d*' % (set))
80 # myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
80 # myfile = fnmatch.filter(validFilelist,'*%4.4d%3.3d%3.3d*'%(year,doy,set))
81
81
82 if len(myfile) != 0:
82 if len(myfile) != 0:
83 return myfile[0]
83 return myfile[0]
84 else:
84 else:
85 filename = '*%10.10d%s' % (set, ext.lower())
85 filename = '*%10.10d%s' % (set, ext.lower())
86 print('the filename %s does not exist' % filename)
86 print('the filename %s does not exist' % filename)
87 print('...going to the last file: ')
87 print('...going to the last file: ')
88
88
89 if validFilelist:
89 if validFilelist:
90 validFilelist = sorted(validFilelist, key=str.lower)
90 validFilelist = sorted(validFilelist, key=str.lower)
91 return validFilelist[-1]
91 return validFilelist[-1]
92
92
93 return None
93 return None
94
94
95 def getlastFileFromPath(path, ext):
95 def getlastFileFromPath(path, ext):
96 """
96 """
97 Depura el fileList dejando solo los que cumplan el formato de "res-xxxxxx.ext"
97 Depura el fileList dejando solo los que cumplan el formato de "res-xxxxxx.ext"
98 al final de la depuracion devuelve el ultimo file de la lista que quedo.
98 al final de la depuracion devuelve el ultimo file de la lista que quedo.
99
99
100 Input:
100 Input:
101 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
101 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
102 ext : extension de los files contenidos en una carpeta
102 ext : extension de los files contenidos en una carpeta
103
103
104 Return:
104 Return:
105 El ultimo file de una determinada carpeta, no se considera el path.
105 El ultimo file de una determinada carpeta, no se considera el path.
106 """
106 """
107 validFilelist = []
107 validFilelist = []
108 fileList = os.listdir(path)
108 fileList = os.listdir(path)
109
109
110 # 0 1234 567 89A BCDE
110 # 0 1234 567 89A BCDE
111 # H YYYY DDD SSS .ext
111 # H YYYY DDD SSS .ext
112
112
113 for thisFile in fileList:
113 for thisFile in fileList:
114
114
115 try:
115 try:
116 number = int(thisFile[6:16])
116 number = int(thisFile[6:16])
117 except:
117 except:
118 print("There is a file or folder with different format")
118 print("There is a file or folder with different format")
119 if not isNumber(number):
119 if not isNumber(number):
120 continue
120 continue
121
121
122 # year = thisFile[1:5]
122 # year = thisFile[1:5]
123 # if not isNumber(year):
123 # if not isNumber(year):
124 # continue
124 # continue
125
125
126 # doy = thisFile[5:8]
126 # doy = thisFile[5:8]
127 # if not isNumber(doy):
127 # if not isNumber(doy):
128 # continue
128 # continue
129
129
130 number = int(number)
130 number = int(number)
131 # year = int(year)
131 # year = int(year)
132 # doy = int(doy)
132 # doy = int(doy)
133
133
134 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
134 if (os.path.splitext(thisFile)[-1].lower() != ext.lower()):
135 continue
135 continue
136
136
137
137
138 validFilelist.append(thisFile)
138 validFilelist.append(thisFile)
139
139
140
140
141 if validFilelist:
141 if validFilelist:
142 validFilelist = sorted(validFilelist, key=str.lower)
142 validFilelist = sorted(validFilelist, key=str.lower)
143 return validFilelist[-1]
143 return validFilelist[-1]
144
144
145 return None
145 return None
146
146
147
147
148
148
149 class HFReader(ProcessingUnit):
149 class HFReader(ProcessingUnit):
150 '''
150 '''
151 classdocs
151 classdocs
152 '''
152 '''
153 path = None
153 path = None
154 startDate = None
154 startDate = None
155 endDate = None
155 endDate = None
156 startTime = None
156 startTime = None
157 endTime = None
157 endTime = None
158 walk = None
158 walk = None
159 isConfig = False
159 isConfig = False
160 dataOut = None
160 dataOut = None
161 nTries = 3
161 nTries = 3
162 ext = ".hdf5"
162 ext = ".hdf5"
163
163
164 def __init__(self, **kwargs):
164 def __init__(self, **kwargs):
165 '''
165 '''
166 Constructor
166 Constructor
167 '''
167 '''
168 ProcessingUnit.__init__(self, **kwargs)
168 ProcessingUnit.__init__(self, **kwargs)
169
169
170 self.isConfig = False
170 self.isConfig = False
171
171
172 self.datablock = None
172 self.datablock = None
173
173
174 self.filename_current = None
174 self.filename_current = None
175
175
176 self.utc = 0
176 self.utc = 0
177
177
178 self.ext = '.hdf5'
178 self.ext = '.hdf5'
179
179
180 self.flagIsNewFile = 1
180 self.flagIsNewFile = 1
181
181
182 #-------------------------------------------------
182 #-------------------------------------------------
183 self.fileIndex = None
183 self.fileIndex = None
184
184
185 self.profileIndex_offset = None
185 self.profileIndex_offset = None
186
186
187 self.filenameList = []
187 self.filenameList = []
188
188
189 self.hfFilePointer = None
189 self.hfFilePointer = None
190
190
191 self.filename_online = None
191 self.filename_online = None
192
192
193 self.status = True
193 self.status = True
194
194
195 self.flagNoMoreFiles = False
195 self.flagNoMoreFiles = False
196
196
197 self.__waitForNewFile = 20
197 self.__waitForNewFile = 20
198
198
199
199
200 #--------------------------------------------------
200 #--------------------------------------------------
201
201
202 self.dataOut = self.createObjByDefault()
202 self.dataOut = self.createObjByDefault()
203
203
204
204
205 def createObjByDefault(self):
205 def createObjByDefault(self):
206
206
207 dataObj = Voltage()
207 dataObj = Voltage()
208
208
209 return dataObj
209 return dataObj
210
210
211 def setObjProperties(self):
211 def setObjProperties(self):
212
212
213 pass
213 pass
214
214
215 def getBlockDimension(self):
215 def getBlockDimension(self):
216 """
216 """
217 Obtiene la cantidad de puntos a leer por cada bloque de datos
217 Obtiene la cantidad de puntos a leer por cada bloque de datos
218
218
219 Affected:
219 Affected:
220 self.blocksize
220 self.blocksize
221
221
222 Return:
222 Return:
223 None
223 None
224 """
224 """
225 pts2read = self.nChannels * self.nHeights * self.nProfiles
225 pts2read = self.nChannels * self.nHeights * self.nProfiles
226 self.blocksize = pts2read
226 self.blocksize = pts2read
227
227
228 def __readHeader(self):
228 def __readHeader(self):
229
229
230 self.nProfiles = 100
230 self.nProfiles = 100
231 self.nHeights = 1000
231 self.nHeights = 1000
232 self.nChannels = 2
232 self.nChannels = 2
233 self.__firstHeigth = 0
233 self.__firstHeigth = 0
234 self.__nSamples = 1000
234 self.__nSamples = 1000
235 self.__deltaHeigth = 1.5
235 self.__deltaHeigth = 1.5
236 self.__sample_rate = 1e5
236 self.__sample_rate = 1e5
237 # self.__frequency=2.72e6
237 # self.__frequency=2.72e6
238 # self.__frequency=3.64e6
238 # self.__frequency=3.64e6
239 self.__frequency = None
239 self.__frequency = None
240 self.__online = False
240 self.__online = False
241 self.filename_next_set = None
241 self.filename_next_set = None
242
242
243 # print "Frequency of Operation:", self.__frequency
243 # print "Frequency of Operation:", self.__frequency
244
244
245
245
246 def __setParameters(self, path='', startDate='', endDate='', startTime='', endTime='', walk=''):
246 def __setParameters(self, path='', startDate='', endDate='', startTime='', endTime='', walk=''):
247 self.path = path
247 self.path = path
248 self.startDate = startDate
248 self.startDate = startDate
249 self.endDate = endDate
249 self.endDate = endDate
250 self.startTime = startTime
250 self.startTime = startTime
251 self.endTime = endTime
251 self.endTime = endTime
252 self.walk = walk
252 self.walk = walk
253
253
254 def __checkPath(self):
254 def __checkPath(self):
255 if os.path.exists(self.path):
255 if os.path.exists(self.path):
256 self.status = 1
256 self.status = 1
257 else:
257 else:
258 self.status = 0
258 self.status = 0
259 print('Path %s does not exits' % self.path)
259 print('Path %s does not exits' % self.path)
260 return
260 return
261 return
261 return
262
262
263 def __selDates(self, hf_dirname_format):
263 def __selDates(self, hf_dirname_format):
264 try:
264 try:
265 dir_hf_filename = self.path + "/" + hf_dirname_format
265 dir_hf_filename = self.path + "/" + hf_dirname_format
266 fp = h5py.File(dir_hf_filename, 'r')
266 fp = h5py.File(dir_hf_filename, 'r')
267 hipoc = fp['t'].value
267 hipoc = fp['t'].value
268 fp.close()
268 fp.close()
269 date_time = datetime.datetime.utcfromtimestamp(hipoc)
269 date_time = datetime.datetime.utcfromtimestamp(hipoc)
270 year = int(date_time[0:4])
270 year = int(date_time[0:4])
271 month = int(date_time[5:7])
271 month = int(date_time[5:7])
272 dom = int(date_time[8:10])
272 dom = int(date_time[8:10])
273 thisDate = datetime.date(year, month, dom)
273 thisDate = datetime.date(year, month, dom)
274 if (thisDate >= self.startDate and thisDate <= self.endDate):
274 if (thisDate >= self.startDate and thisDate <= self.endDate):
275 return hf_dirname_format
275 return hf_dirname_format
276 except:
276 except:
277 return None
277 return None
278
278
279 def __findDataForDates(self, online=False):
279 def __findDataForDates(self, online=False):
280 if not(self.status):
280 if not(self.status):
281 return None
281 return None
282
282
283 pat = '\d+.\d+'
283 pat = '\d+.\d+'
284 dirnameList = [re.search(pat, x) for x in os.listdir(self.path)]
284 dirnameList = [re.search(pat, x) for x in os.listdir(self.path)]
285 dirnameList = [x for x in dirnameList if x != None]
285 dirnameList = [x for x in dirnameList if x != None]
286 dirnameList = [x.string for x in dirnameList]
286 dirnameList = [x.string for x in dirnameList]
287 if not(online):
287 if not(online):
288
288
289 dirnameList = [self.__selDates(x) for x in dirnameList]
289 dirnameList = [self.__selDates(x) for x in dirnameList]
290 dirnameList = [x for x in dirnameList if x != None]
290 dirnameList = [x for x in dirnameList if x != None]
291
291
292 if len(dirnameList) > 0:
292 if len(dirnameList) > 0:
293 self.status = 1
293 self.status = 1
294 self.dirnameList = dirnameList
294 self.dirnameList = dirnameList
295 self.dirnameList.sort()
295 self.dirnameList.sort()
296
296
297 else:
297 else:
298 self.status = 0
298 self.status = 0
299 return None
299 return None
300
300
301 def __getTimeFromData(self):
301 def __getTimeFromData(self):
302 startDateTime_Reader = datetime.datetime.combine(self.startDate, self.startTime)
302 startDateTime_Reader = datetime.datetime.combine(self.startDate, self.startTime)
303 endDateTime_Reader = datetime.datetime.combine(self.endDate, self.endTime)
303 endDateTime_Reader = datetime.datetime.combine(self.endDate, self.endTime)
304 print('Filtering Files from %s to %s' % (startDateTime_Reader, endDateTime_Reader))
304 print('Filtering Files from %s to %s' % (startDateTime_Reader, endDateTime_Reader))
305 print('........................................')
305 print('........................................')
306 filter_filenameList = []
306 filter_filenameList = []
307 self.filenameList.sort()
307 self.filenameList.sort()
308 for i in range(len(self.filenameList) - 1):
308 for i in range(len(self.filenameList) - 1):
309 filename = self.filenameList[i]
309 filename = self.filenameList[i]
310 dir_hf_filename = filename
310 dir_hf_filename = filename
311 fp = h5py.File(dir_hf_filename, 'r')
311 fp = h5py.File(dir_hf_filename, 'r')
312 hipoc = fp['t'].value
312 hipoc = fp['t'].value
313 hipoc = hipoc + self.timezone
313 hipoc = hipoc + self.timezone
314 date_time = datetime.datetime.utcfromtimestamp(hipoc)
314 date_time = datetime.datetime.utcfromtimestamp(hipoc)
315 fp.close()
315 fp.close()
316 year = int(date_time[0:4])
316 year = int(date_time[0:4])
317 month = int(date_time[5:7])
317 month = int(date_time[5:7])
318 dom = int(date_time[8:10])
318 dom = int(date_time[8:10])
319 hour = int(date_time[11:13])
319 hour = int(date_time[11:13])
320 min = int(date_time[14:16])
320 min = int(date_time[14:16])
321 sec = int(date_time[17:19])
321 sec = int(date_time[17:19])
322 this_time = datetime.datetime(year, month, dom, hour, min, sec)
322 this_time = datetime.datetime(year, month, dom, hour, min, sec)
323 if (this_time >= startDateTime_Reader and this_time <= endDateTime_Reader):
323 if (this_time >= startDateTime_Reader and this_time <= endDateTime_Reader):
324 filter_filenameList.append(filename)
324 filter_filenameList.append(filename)
325 filter_filenameList.sort()
325 filter_filenameList.sort()
326 self.filenameList = filter_filenameList
326 self.filenameList = filter_filenameList
327 return 1
327 return 1
328
328
329 def __getFilenameList(self):
329 def __getFilenameList(self):
330 # print "hola"
330 # print "hola"
331 # print self.dirnameList
331 # print self.dirnameList
332 dirList = [os.path.join(self.path, x) for x in self.dirnameList]
332 dirList = [os.path.join(self.path, x) for x in self.dirnameList]
333 self.filenameList = dirList
333 self.filenameList = dirList
334 # print self.filenameList
334 # print self.filenameList
335 # print "pase",len(self.filenameList)
335 # print "pase",len(self.filenameList)
336
336
337 def __selectDataForTimes(self, online=False):
337 def __selectDataForTimes(self, online=False):
338
338
339 if not(self.status):
339 if not(self.status):
340 return None
340 return None
341 #----------------
341 #----------------
342 self.__getFilenameList()
342 self.__getFilenameList()
343 #----------------
343 #----------------
344 if not(online):
344 if not(online):
345 if not(self.all):
345 if not(self.all):
346 self.__getTimeFromData()
346 self.__getTimeFromData()
347 if len(self.filenameList) > 0:
347 if len(self.filenameList) > 0:
348 self.status = 1
348 self.status = 1
349 self.filenameList.sort()
349 self.filenameList.sort()
350 else:
350 else:
351 self.status = 0
351 self.status = 0
352 return None
352 return None
353 else:
353 else:
354 if self.set != None:
354 if self.set != None:
355
355
356 filename = getFileFromSet(self.path, self.ext, self.set)
356 filename = getFileFromSet(self.path, self.ext, self.set)
357
357
358 if self.flag_nextfile == True:
358 if self.flag_nextfile == True:
359 self.dirnameList = [filename]
359 self.dirnameList = [filename]
360 fullfilename = self.path + "/" + filename
360 fullfilename = self.path + "/" + filename
361 self.filenameList = [fullfilename]
361 self.filenameList = [fullfilename]
362 self.filename_next_set = int(filename[6:16]) + 10
362 self.filename_next_set = int(filename[6:16]) + 10
363
363
364 self.flag_nextfile = False
364 self.flag_nextfile = False
365 else:
365 else:
366 print(filename)
366 print(filename)
367 print("PRIMERA CONDICION")
367 print("PRIMERA CONDICION")
368 # if self.filename_next_set== int(filename[6:16]):
368 # if self.filename_next_set== int(filename[6:16]):
369 print("TODO BIEN")
369 print("TODO BIEN")
370
370
371 if filename == None:
371 if filename == None:
372 raise ValueError("corregir")
372 raise ValueError("corregir")
373
373
374 self.dirnameList = [filename]
374 self.dirnameList = [filename]
375 fullfilename = self.path + "/" + filename
375 fullfilename = self.path + "/" + filename
376 self.filenameList = [fullfilename]
376 self.filenameList = [fullfilename]
377 self.filename_next_set = int(filename[6:16]) + 10
377 self.filename_next_set = int(filename[6:16]) + 10
378 print("Setting next file", self.filename_next_set)
378 print("Setting next file", self.filename_next_set)
379 self.set = int(filename[6:16])
379 self.set = int(filename[6:16])
380 if True:
380 if True:
381 pass
381 pass
382 else:
382 else:
383 print("ESTOY AQUI PORQUE NO EXISTE EL SIGUIENTE ARCHIVO")
383 print("ESTOY AQUI PORQUE NO EXISTE EL SIGUIENTE ARCHIVO")
384
384
385 else:
385 else:
386 filename = getlastFileFromPath(self.path, self.ext)
386 filename = getlastFileFromPath(self.path, self.ext)
387
387
388 if self.flag_nextfile == True:
388 if self.flag_nextfile == True:
389 self.dirnameList = [filename]
389 self.dirnameList = [filename]
390 fullfilename = self.path + "/" + filename
390 fullfilename = self.path + "/" + filename
391 self.filenameList = [self.filenameList[-1]]
391 self.filenameList = [self.filenameList[-1]]
392 self.filename_next_set = int(filename[6:16]) + 10
392 self.filename_next_set = int(filename[6:16]) + 10
393
393
394 self.flag_nextfile = False
394 self.flag_nextfile = False
395 else:
395 else:
396 filename = getFileFromSet(self.path, self.ext, self.set)
396 filename = getFileFromSet(self.path, self.ext, self.set)
397 print(filename)
397 print(filename)
398 print("PRIMERA CONDICION")
398 print("PRIMERA CONDICION")
399 # if self.filename_next_set== int(filename[6:16]):
399 # if self.filename_next_set== int(filename[6:16]):
400 print("TODO BIEN")
400 print("TODO BIEN")
401
401
402 if filename == None:
402 if filename == None:
403 raise ValueError("corregir")
403 raise ValueError("corregir")
404
404
405 self.dirnameList = [filename]
405 self.dirnameList = [filename]
406 fullfilename = self.path + "/" + filename
406 fullfilename = self.path + "/" + filename
407 self.filenameList = [fullfilename]
407 self.filenameList = [fullfilename]
408 self.filename_next_set = int(filename[6:16]) + 10
408 self.filename_next_set = int(filename[6:16]) + 10
409 print("Setting next file", self.filename_next_set)
409 print("Setting next file", self.filename_next_set)
410 self.set = int(filename[6:16])
410 self.set = int(filename[6:16])
411 if True:
411 if True:
412 pass
412 pass
413 else:
413 else:
414 print("ESTOY AQUI PORQUE NO EXISTE EL SIGUIENTE ARCHIVO")
414 print("ESTOY AQUI PORQUE NO EXISTE EL SIGUIENTE ARCHIVO")
415
415
416
416
417
417
418 def searchFilesOffLine(self,
418 def searchFilesOffLine(self,
419 path,
419 path,
420 startDate,
420 startDate,
421 endDate,
421 endDate,
422 ext,
422 ext,
423 startTime=datetime.time(0, 0, 0),
423 startTime=datetime.time(0, 0, 0),
424 endTime=datetime.time(23, 59, 59),
424 endTime=datetime.time(23, 59, 59),
425 walk=True):
425 walk=True):
426
426
427 self.__setParameters(path, startDate, endDate, startTime, endTime, walk)
427 self.__setParameters(path, startDate, endDate, startTime, endTime, walk)
428
428
429 self.__checkPath()
429 self.__checkPath()
430
430
431 self.__findDataForDates()
431 self.__findDataForDates()
432 # print self.dirnameList
432 # print self.dirnameList
433
433
434 self.__selectDataForTimes()
434 self.__selectDataForTimes()
435
435
436 for i in range(len(self.filenameList)):
436 for i in range(len(self.filenameList)):
437 print("%s" % (self.filenameList[i]))
437 print("%s" % (self.filenameList[i]))
438
438
439 return
439 return
440
440
441 def searchFilesOnLine(self,
441 def searchFilesOnLine(self,
442 path,
442 path,
443 expLabel="",
443 expLabel="",
444 ext=None,
444 ext=None,
445 startDate=None,
445 startDate=None,
446 endDate=None,
446 endDate=None,
447 walk=True,
447 walk=True,
448 set=None):
448 set=None):
449
449
450
450
451 startDate = datetime.datetime.utcnow().date()
451 startDate = datetime.datetime.utcnow().date()
452 endDate = datetime.datetime.utcnow().date()
452 endDate = datetime.datetime.utcnow().date()
453
453
454 self.__setParameters(path=path, startDate=startDate, endDate=endDate, walk=walk)
454 self.__setParameters(path=path, startDate=startDate, endDate=endDate, walk=walk)
455
455
456 self.__checkPath()
456 self.__checkPath()
457
457
458 fullpath = path
458 fullpath = path
459 print("%s folder was found: " % (fullpath))
459 print("%s folder was found: " % (fullpath))
460
460
461 if set == None:
461 if set == None:
462 self.set = None
462 self.set = None
463 filename = getlastFileFromPath(fullpath, ext)
463 filename = getlastFileFromPath(fullpath, ext)
464 startDate = datetime.datetime.utcnow().date
464 startDate = datetime.datetime.utcnow().date
465 endDate = datetime.datetime.utcnow().date()
465 endDate = datetime.datetime.utcnow().date()
466 #
466 #
467 else:
467 else:
468 filename = getFileFromSet(fullpath, ext, set)
468 filename = getFileFromSet(fullpath, ext, set)
469 startDate = None
469 startDate = None
470 endDate = None
470 endDate = None
471 #
471 #
472 if not (filename):
472 if not (filename):
473 return None, None, None, None, None
473 return None, None, None, None, None
474 # print "%s file was found" %(filename)
474 # print "%s file was found" %(filename)
475
475
476 #
476 #
477 # dir_hf_filename= self.path+"/"+filename
477 # dir_hf_filename= self.path+"/"+filename
478 # fp= h5py.File(dir_hf_filename,'r')
478 # fp= h5py.File(dir_hf_filename,'r')
479 # hipoc=fp['t'].value
479 # hipoc=fp['t'].value
480 # fp.close()
480 # fp.close()
481 # date_time=datetime.datetime.utcfromtimestamp(hipoc)
481 # date_time=datetime.datetime.utcfromtimestamp(hipoc)
482 #
482 #
483 # year =int(date_time[0:4])
483 # year =int(date_time[0:4])
484 # month=int(date_time[5:7])
484 # month=int(date_time[5:7])
485 # dom =int(date_time[8:10])
485 # dom =int(date_time[8:10])
486 # set= int(filename[4:10])
486 # set= int(filename[4:10])
487 # self.set=set-1
487 # self.set=set-1
488 # self.dirnameList=[filename]
488 # self.dirnameList=[filename]
489 filenameList = fullpath + "/" + filename
489 filenameList = fullpath + "/" + filename
490 self.dirnameList = [filename]
490 self.dirnameList = [filename]
491 self.filenameList = [filenameList]
491 self.filenameList = [filenameList]
492 self.flag_nextfile = True
492 self.flag_nextfile = True
493
493
494 # self.__findDataForDates(online=True)
494 # self.__findDataForDates(online=True)
495 # self.dirnameList=[self.dirnameList[-1]]
495 # self.dirnameList=[self.dirnameList[-1]]
496 # print self.dirnameList
496 # print self.dirnameList
497 # self.__selectDataForTimes(online=True)
497 # self.__selectDataForTimes(online=True)
498 # return fullpath,filename,year,month,dom,set
498 # return fullpath,filename,year,month,dom,set
499 return
499 return
500
500
501 def __setNextFile(self, online=False):
501 def __setNextFile(self, online=False):
502 """
502 """
503 """
503 """
504 if not(online):
504 if not(online):
505 newFile = self.__setNextFileOffline()
505 newFile = self.__setNextFileOffline()
506 else:
506 else:
507 newFile = self.__setNextFileOnline()
507 newFile = self.__setNextFileOnline()
508
508
509 if not(newFile):
509 if not(newFile):
510 return 0
510 return 0
511 return 1
511 return 1
512
512
513 def __setNextFileOffline(self):
513 def __setNextFileOffline(self):
514 """
514 """
515 """
515 """
516 idFile = self.fileIndex
516 idFile = self.fileIndex
517 while(True):
517 while(True):
518 idFile += 1
518 idFile += 1
519 if not (idFile < len(self.filenameList)):
519 if not (idFile < len(self.filenameList)):
520 self.flagNoMoreFiles = 1
520 self.flagNoMoreFiles = 1
521 print("No more Files")
521 print("No more Files")
522 return 0
522 return 0
523 filename = self.filenameList[idFile]
523 filename = self.filenameList[idFile]
524 hfFilePointer = h5py.File(filename, 'r')
524 hfFilePointer = h5py.File(filename, 'r')
525
525
526 epoc = hfFilePointer['t'].value
526 epoc = hfFilePointer['t'].value
527 # this_time=datetime.datetime(year,month,dom,hour,min,sec)
527 # this_time=datetime.datetime(year,month,dom,hour,min,sec)
528 break
528 break
529
529
530 self.flagIsNewFile = 1
530 self.flagIsNewFile = 1
531 self.fileIndex = idFile
531 self.fileIndex = idFile
532 self.filename = filename
532 self.filename = filename
533
533
534 self.hfFilePointer = hfFilePointer
534 self.hfFilePointer = hfFilePointer
535 hfFilePointer.close()
535 hfFilePointer.close()
536 self.__t0 = epoc
536 self.__t0 = epoc
537 print("Setting the file: %s" % self.filename)
537 print("Setting the file: %s" % self.filename)
538
538
539 return 1
539 return 1
540
540
541 def __setNextFileOnline(self):
541 def __setNextFileOnline(self):
542 """
542 """
543 """
543 """
544 print("SOY NONE", self.set)
544 print("SOY NONE", self.set)
545 if self.set == None:
545 if self.set == None:
546 pass
546 pass
547 else:
547 else:
548 self.set += 10
548 self.set += 10
549
549
550 filename = self.filenameList[0] # fullfilename
550 filename = self.filenameList[0] # fullfilename
551 if self.filename_online != None:
551 if self.filename_online != None:
552 self.__selectDataForTimes(online=True)
552 self.__selectDataForTimes(online=True)
553 filename = self.filenameList[0]
553 filename = self.filenameList[0]
554 while self.filename_online == filename:
554 while self.filename_online == filename:
555 print('waiting %d seconds to get a new file...' % (self.__waitForNewFile))
555 print('waiting %d seconds to get a new file...' % (self.__waitForNewFile))
556 time.sleep(self.__waitForNewFile)
556 time.sleep(self.__waitForNewFile)
557 # self.__findDataForDates(online=True)
557 # self.__findDataForDates(online=True)
558 self.set = self.filename_next_set
558 self.set = self.filename_next_set
559 self.__selectDataForTimes(online=True)
559 self.__selectDataForTimes(online=True)
560 filename = self.filenameList[0]
560 filename = self.filenameList[0]
561 sizeoffile = os.path.getsize(filename)
561 sizeoffile = os.path.getsize(filename)
562
562
563 # print filename
563 # print filename
564 sizeoffile = os.path.getsize(filename)
564 sizeoffile = os.path.getsize(filename)
565 if sizeoffile < 1670240:
565 if sizeoffile < 1670240:
566 print("%s is not the rigth size" % filename)
566 print("%s is not the rigth size" % filename)
567 delay = 50
567 delay = 50
568 print('waiting %d seconds for delay...' % (delay))
568 print('waiting %d seconds for delay...' % (delay))
569 time.sleep(delay)
569 time.sleep(delay)
570 sizeoffile = os.path.getsize(filename)
570 sizeoffile = os.path.getsize(filename)
571 if sizeoffile < 1670240:
571 if sizeoffile < 1670240:
572 delay = 50
572 delay = 50
573 print('waiting %d more seconds for delay...' % (delay))
573 print('waiting %d more seconds for delay...' % (delay))
574 time.sleep(delay)
574 time.sleep(delay)
575
575
576 sizeoffile = os.path.getsize(filename)
576 sizeoffile = os.path.getsize(filename)
577 if sizeoffile < 1670240:
577 if sizeoffile < 1670240:
578 delay = 50
578 delay = 50
579 print('waiting %d more seconds for delay...' % (delay))
579 print('waiting %d more seconds for delay...' % (delay))
580 time.sleep(delay)
580 time.sleep(delay)
581
581
582 try:
582 try:
583 hfFilePointer = h5py.File(filename, 'r')
583 hfFilePointer = h5py.File(filename, 'r')
584
584
585 except:
585 except:
586 print("Error reading file %s" % filename)
586 print("Error reading file %s" % filename)
587
587
588 self.filename_online = filename
588 self.filename_online = filename
589 epoc = hfFilePointer['t'].value
589 epoc = hfFilePointer['t'].value
590
590
591 self.hfFilePointer = hfFilePointer
591 self.hfFilePointer = hfFilePointer
592 hfFilePointer.close()
592 hfFilePointer.close()
593 self.__t0 = epoc
593 self.__t0 = epoc
594
594
595
595
596 self.flagIsNewFile = 1
596 self.flagIsNewFile = 1
597 self.filename = filename
597 self.filename = filename
598
598
599 print("Setting the file: %s" % self.filename)
599 print("Setting the file: %s" % self.filename)
600 return 1
600 return 1
601
601
602 def __getExpParameters(self):
602 def __getExpParameters(self):
603 if not(self.status):
603 if not(self.status):
604 return None
604 return None
605
605
606 def setup(self,
606 def setup(self,
607 path=None,
607 path=None,
608 startDate=None,
608 startDate=None,
609 endDate=None,
609 endDate=None,
610 startTime=datetime.time(0, 0, 0),
610 startTime=datetime.time(0, 0, 0),
611 endTime=datetime.time(23, 59, 59),
611 endTime=datetime.time(23, 59, 59),
612 set=None,
612 set=None,
613 expLabel="",
613 expLabel="",
614 ext=None,
614 ext=None,
615 all=0,
615 all=0,
616 timezone=0,
616 timezone=0,
617 online=False,
617 online=False,
618 delay=60,
618 delay=60,
619 walk=True):
619 walk=True):
620 '''
620 '''
621 In this method we should set all initial parameters.
621 In this method we should set all initial parameters.
622
622
623 '''
623 '''
624 if path == None:
624 if path == None:
625 raise ValueError("The path is not valid")
625 raise ValueError("The path is not valid")
626
626
627 if ext == None:
627 if ext == None:
628 ext = self.ext
628 ext = self.ext
629
629
630 self.timezone = timezone
630 self.timezone = timezone
631 self.online = online
631 self.online = online
632 self.all = all
632 self.all = all
633 # if set==None:
633 # if set==None:
634
634
635 # print set
635 # print set
636 if not(online):
636 if not(online):
637 print("Searching files in offline mode...")
637 print("Searching files in offline mode...")
638
638
639 self.searchFilesOffLine(path, startDate, endDate, ext, startTime, endTime, walk)
639 self.searchFilesOffLine(path, startDate, endDate, ext, startTime, endTime, walk)
640 else:
640 else:
641 print("Searching files in online mode...")
641 print("Searching files in online mode...")
642 self.searchFilesOnLine(path, walk, ext, set=set)
642 self.searchFilesOnLine(path, walk, ext, set=set)
643 if set == None:
643 if set == None:
644 pass
644 pass
645 else:
645 else:
646 self.set = set - 10
646 self.set = set - 10
647
647
648 # for nTries in range(self.nTries):
648 # for nTries in range(self.nTries):
649 #
649 #
650 # fullpath,file,year,month,day,set = self.searchFilesOnLine(path=path,expLabel=expLabel,ext=ext, walk=walk,set=set)
650 # fullpath,file,year,month,day,set = self.searchFilesOnLine(path=path,expLabel=expLabel,ext=ext, walk=walk,set=set)
651 #
651 #
652 # if fullpath:
652 # if fullpath:
653 # break
653 # break
654 # print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
654 # print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
655 # time.sleep(self.delay)
655 # time.sleep(self.delay)
656 # if not(fullpath):
656 # if not(fullpath):
657 # print "There ins't valid files in %s" % path
657 # print "There ins't valid files in %s" % path
658 # return None
658 # return None
659
659
660
660
661 if not(self.filenameList):
661 if not(self.filenameList):
662 print("There is no files into the folder: %s" % (path))
662 print("There is no files into the folder: %s" % (path))
663 sys.exit(-1)
663 sys.exit(-1)
664
664
665 self.__getExpParameters()
665 self.__getExpParameters()
666
666
667
667
668 self.fileIndex = -1
668 self.fileIndex = -1
669
669
670 self.__setNextFile(online)
670 self.__setNextFile(online)
671
671
672 self.__readMetadata()
672 self.__readMetadata()
673
673
674 self.__setLocalVariables()
674 self.__setLocalVariables()
675
675
676 self.__setHeaderDO()
676 self.__setHeaderDO()
677 # self.profileIndex_offset= 0
677 # self.profileIndex_offset= 0
678
678
679 # self.profileIndex = self.profileIndex_offset
679 # self.profileIndex = self.profileIndex_offset
680
680
681 self.isConfig = True
681 self.isConfig = True
682
682
683 def __readMetadata(self):
683 def __readMetadata(self):
684 self.__readHeader()
684 self.__readHeader()
685
685
686
686
687 def __setLocalVariables(self):
687 def __setLocalVariables(self):
688
688
689 self.datablock = numpy.zeros((self.nChannels, self.nHeights, self.nProfiles), dtype=numpy.complex)
689 self.datablock = numpy.zeros((self.nChannels, self.nHeights, self.nProfiles), dtype=complex)
690 #
690 #
691
691
692
692
693
693
694 self.profileIndex = 9999
694 self.profileIndex = 9999
695
695
696
696
697 def __setHeaderDO(self):
697 def __setHeaderDO(self):
698
698
699
699
700 self.dataOut.radarControllerHeaderObj = RadarControllerHeader()
700 self.dataOut.radarControllerHeaderObj = RadarControllerHeader()
701
701
702 self.dataOut.systemHeaderObj = SystemHeader()
702 self.dataOut.systemHeaderObj = SystemHeader()
703
703
704
704
705 #---------------------------------------------------------
705 #---------------------------------------------------------
706 self.dataOut.systemHeaderObj.nProfiles = 100
706 self.dataOut.systemHeaderObj.nProfiles = 100
707 self.dataOut.systemHeaderObj.nSamples = 1000
707 self.dataOut.systemHeaderObj.nSamples = 1000
708
708
709
709
710 SAMPLING_STRUCTURE = [('h0', '<f4'), ('dh', '<f4'), ('nsa', '<u4')]
710 SAMPLING_STRUCTURE = [('h0', '<f4'), ('dh', '<f4'), ('nsa', '<u4')]
711 self.dataOut.radarControllerHeaderObj.samplingWindow = numpy.zeros((1,), SAMPLING_STRUCTURE)
711 self.dataOut.radarControllerHeaderObj.samplingWindow = numpy.zeros((1,), SAMPLING_STRUCTURE)
712 self.dataOut.radarControllerHeaderObj.samplingWindow['h0'] = 0
712 self.dataOut.radarControllerHeaderObj.samplingWindow['h0'] = 0
713 self.dataOut.radarControllerHeaderObj.samplingWindow['dh'] = 1.5
713 self.dataOut.radarControllerHeaderObj.samplingWindow['dh'] = 1.5
714 self.dataOut.radarControllerHeaderObj.samplingWindow['nsa'] = 1000
714 self.dataOut.radarControllerHeaderObj.samplingWindow['nsa'] = 1000
715 self.dataOut.radarControllerHeaderObj.nHeights = int(self.dataOut.radarControllerHeaderObj.samplingWindow['nsa'])
715 self.dataOut.radarControllerHeaderObj.nHeights = int(self.dataOut.radarControllerHeaderObj.samplingWindow['nsa'])
716 self.dataOut.radarControllerHeaderObj.firstHeight = self.dataOut.radarControllerHeaderObj.samplingWindow['h0']
716 self.dataOut.radarControllerHeaderObj.firstHeight = self.dataOut.radarControllerHeaderObj.samplingWindow['h0']
717 self.dataOut.radarControllerHeaderObj.deltaHeight = self.dataOut.radarControllerHeaderObj.samplingWindow['dh']
717 self.dataOut.radarControllerHeaderObj.deltaHeight = self.dataOut.radarControllerHeaderObj.samplingWindow['dh']
718 self.dataOut.radarControllerHeaderObj.samplesWin = self.dataOut.radarControllerHeaderObj.samplingWindow['nsa']
718 self.dataOut.radarControllerHeaderObj.samplesWin = self.dataOut.radarControllerHeaderObj.samplingWindow['nsa']
719
719
720 self.dataOut.radarControllerHeaderObj.nWindows = 1
720 self.dataOut.radarControllerHeaderObj.nWindows = 1
721 self.dataOut.radarControllerHeaderObj.codetype = 0
721 self.dataOut.radarControllerHeaderObj.codetype = 0
722 self.dataOut.radarControllerHeaderObj.numTaus = 0
722 self.dataOut.radarControllerHeaderObj.numTaus = 0
723 # self.dataOut.radarControllerHeaderObj.Taus = numpy.zeros((1,),'<f4')
723 # self.dataOut.radarControllerHeaderObj.Taus = numpy.zeros((1,),'<f4')
724
724
725
725
726 # self.dataOut.radarControllerHeaderObj.nCode=numpy.zeros((1,), '<u4')
726 # self.dataOut.radarControllerHeaderObj.nCode=numpy.zeros((1,), '<u4')
727 # self.dataOut.radarControllerHeaderObj.nBaud=numpy.zeros((1,), '<u4')
727 # self.dataOut.radarControllerHeaderObj.nBaud=numpy.zeros((1,), '<u4')
728 # self.dataOut.radarControllerHeaderObj.code=numpy.zeros(0)
728 # self.dataOut.radarControllerHeaderObj.code=numpy.zeros(0)
729
729
730 self.dataOut.radarControllerHeaderObj.code_size = 0
730 self.dataOut.radarControllerHeaderObj.code_size = 0
731 self.dataOut.nBaud = 0
731 self.dataOut.nBaud = 0
732 self.dataOut.nCode = 0
732 self.dataOut.nCode = 0
733 self.dataOut.nPairs = 0
733 self.dataOut.nPairs = 0
734
734
735
735
736 #---------------------------------------------------------
736 #---------------------------------------------------------
737
737
738 self.dataOut.type = "Voltage"
738 self.dataOut.type = "Voltage"
739
739
740 self.dataOut.data = None
740 self.dataOut.data = None
741
741
742 self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
742 self.dataOut.dtype = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
743
743
744 self.dataOut.nProfiles = 1
744 self.dataOut.nProfiles = 1
745
745
746 self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype=numpy.float) * self.__deltaHeigth
746 self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype=numpy.float32) * self.__deltaHeigth
747
747
748 self.dataOut.channelList = list(range(self.nChannels))
748 self.dataOut.channelList = list(range(self.nChannels))
749
749
750 # self.dataOut.channelIndexList = None
750 # self.dataOut.channelIndexList = None
751
751
752 self.dataOut.flagNoData = True
752 self.dataOut.flagNoData = True
753
753
754 # Set to TRUE if the data is discontinuous
754 # Set to TRUE if the data is discontinuous
755 self.dataOut.flagDiscontinuousBlock = False
755 self.dataOut.flagDiscontinuousBlock = False
756
756
757 self.dataOut.utctime = None
757 self.dataOut.utctime = None
758
758
759 self.dataOut.timeZone = self.timezone
759 self.dataOut.timeZone = self.timezone
760
760
761 self.dataOut.dstFlag = 0
761 self.dataOut.dstFlag = 0
762
762
763 self.dataOut.errorCount = 0
763 self.dataOut.errorCount = 0
764
764
765 self.dataOut.nCohInt = 1
765 self.dataOut.nCohInt = 1
766
766
767 self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights
767 self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights
768
768
769 self.dataOut.flagDecodeData = False # asumo que la data esta decodificada
769 self.dataOut.flagDecodeData = False # asumo que la data esta decodificada
770
770
771 self.dataOut.flagDeflipData = False # asumo que la data esta sin flip
771 self.dataOut.flagDeflipData = False # asumo que la data esta sin flip
772
772
773 self.dataOut.flagShiftFFT = False
773 self.dataOut.flagShiftFFT = False
774
774
775 self.dataOut.ippSeconds = 1.0 * self.__nSamples / self.__sample_rate
775 self.dataOut.ippSeconds = 1.0 * self.__nSamples / self.__sample_rate
776
776
777 # Time interval between profiles
777 # Time interval between profiles
778 # self.dataOut.timeInterval =self.dataOut.ippSeconds * self.dataOut.nCohInt
778 # self.dataOut.timeInterval =self.dataOut.ippSeconds * self.dataOut.nCohInt
779
779
780
780
781 self.dataOut.frequency = self.__frequency
781 self.dataOut.frequency = self.__frequency
782
782
783 self.dataOut.realtime = self.__online
783 self.dataOut.realtime = self.__online
784
784
785 def __hasNotDataInBuffer(self):
785 def __hasNotDataInBuffer(self):
786
786
787 if self.profileIndex >= self.nProfiles:
787 if self.profileIndex >= self.nProfiles:
788 return 1
788 return 1
789
789
790 return 0
790 return 0
791
791
792 def readNextBlock(self):
792 def readNextBlock(self):
793 if not(self.__setNewBlock()):
793 if not(self.__setNewBlock()):
794 return 0
794 return 0
795
795
796 if not(self.readBlock()):
796 if not(self.readBlock()):
797 return 0
797 return 0
798
798
799 return 1
799 return 1
800
800
801 def __setNewBlock(self):
801 def __setNewBlock(self):
802
802
803 if self.hfFilePointer == None:
803 if self.hfFilePointer == None:
804 return 0
804 return 0
805
805
806 if self.flagIsNewFile:
806 if self.flagIsNewFile:
807 return 1
807 return 1
808
808
809 if self.profileIndex < self.nProfiles:
809 if self.profileIndex < self.nProfiles:
810 return 1
810 return 1
811
811
812 self.__setNextFile(self.online)
812 self.__setNextFile(self.online)
813
813
814 return 1
814 return 1
815
815
816
816
817
817
818 def readBlock(self):
818 def readBlock(self):
819 fp = h5py.File(self.filename, 'r')
819 fp = h5py.File(self.filename, 'r')
820 # Puntero que apunta al archivo hdf5
820 # Puntero que apunta al archivo hdf5
821 ch0 = (fp['ch0']).value # Primer canal (100,1000)--(perfiles,alturas)
821 ch0 = (fp['ch0']).value # Primer canal (100,1000)--(perfiles,alturas)
822 ch1 = (fp['ch1']).value # Segundo canal (100,1000)--(perfiles,alturas)
822 ch1 = (fp['ch1']).value # Segundo canal (100,1000)--(perfiles,alturas)
823 fp.close()
823 fp.close()
824 ch0 = ch0.swapaxes(0, 1) # Primer canal (100,1000)--(alturas,perfiles)
824 ch0 = ch0.swapaxes(0, 1) # Primer canal (100,1000)--(alturas,perfiles)
825 ch1 = ch1.swapaxes(0, 1) # Segundo canal (100,1000)--(alturas,perfiles)
825 ch1 = ch1.swapaxes(0, 1) # Segundo canal (100,1000)--(alturas,perfiles)
826 self.datablock = numpy.array([ch0, ch1])
826 self.datablock = numpy.array([ch0, ch1])
827 self.flagIsNewFile = 0
827 self.flagIsNewFile = 0
828
828
829 self.profileIndex = 0
829 self.profileIndex = 0
830
830
831 return 1
831 return 1
832
832
833 def getData(self):
833 def getData(self):
834 if self.flagNoMoreFiles:
834 if self.flagNoMoreFiles:
835 self.dataOut.flagNoData = True
835 self.dataOut.flagNoData = True
836 return 0
836 return 0
837
837
838 if self.__hasNotDataInBuffer():
838 if self.__hasNotDataInBuffer():
839 if not(self.readNextBlock()):
839 if not(self.readNextBlock()):
840 self.dataOut.flagNodata = True
840 self.dataOut.flagNodata = True
841 return 0
841 return 0
842
842
843 ##############################
843 ##############################
844 ##############################
844 ##############################
845 self.dataOut.data = self.datablock[:, :, self.profileIndex]
845 self.dataOut.data = self.datablock[:, :, self.profileIndex]
846 self.dataOut.utctime = self.__t0 + self.dataOut.ippSeconds * self.profileIndex
846 self.dataOut.utctime = self.__t0 + self.dataOut.ippSeconds * self.profileIndex
847 self.dataOut.profileIndex = self.profileIndex
847 self.dataOut.profileIndex = self.profileIndex
848 self.dataOut.flagNoData = False
848 self.dataOut.flagNoData = False
849 self.profileIndex += 1
849 self.profileIndex += 1
850
850
851 return self.dataOut.data
851 return self.dataOut.data
852
852
853
853
854 def run(self, **kwargs):
854 def run(self, **kwargs):
855 '''
855 '''
856 This method will be called many times so here you should put all your code
856 This method will be called many times so here you should put all your code
857 '''
857 '''
858
858
859 if not self.isConfig:
859 if not self.isConfig:
860 self.setup(**kwargs)
860 self.setup(**kwargs)
861 self.isConfig = True
861 self.isConfig = True
862 self.getData()
862 self.getData()
@@ -1,873 +1,887
1 import os
1 import os
2 import time
2 import time
3 import datetime
3 import datetime
4
4
5 import numpy
5 import numpy
6 import h5py
6 import h5py
7
7
8 import schainpy.admin
8 import schainpy.admin
9 from schainpy.model.data.jrodata import *
9 from schainpy.model.data.jrodata import *
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
10 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
11 from schainpy.model.io.jroIO_base import *
11 from schainpy.model.io.jroIO_base import *
12 from schainpy.utils import log
12 from schainpy.utils import log
13
13
14
14
15 class HDFReader(Reader, ProcessingUnit):
15 class HDFReader(Reader, ProcessingUnit):
16 """Processing unit to read HDF5 format files
16 """Processing unit to read HDF5 format files
17
17
18 This unit reads HDF5 files created with `HDFWriter` operation contains
18 This unit reads HDF5 files created with `HDFWriter` operation contains
19 by default two groups Data and Metadata all variables would be saved as `dataOut`
19 by default two groups Data and Metadata all variables would be saved as `dataOut`
20 attributes.
20 attributes.
21 It is possible to read any HDF5 file by given the structure in the `description`
21 It is possible to read any HDF5 file by given the structure in the `description`
22 parameter, also you can add extra values to metadata with the parameter `extras`.
22 parameter, also you can add extra values to metadata with the parameter `extras`.
23
23
24 Parameters:
24 Parameters:
25 -----------
25 -----------
26 path : str
26 path : str
27 Path where files are located.
27 Path where files are located.
28 startDate : date
28 startDate : date
29 Start date of the files
29 Start date of the files
30 endDate : list
30 endDate : list
31 End date of the files
31 End date of the files
32 startTime : time
32 startTime : time
33 Start time of the files
33 Start time of the files
34 endTime : time
34 endTime : time
35 End time of the files
35 End time of the files
36 description : dict, optional
36 description : dict, optional
37 Dictionary with the description of the HDF5 file
37 Dictionary with the description of the HDF5 file
38 extras : dict, optional
38 extras : dict, optional
39 Dictionary with extra metadata to be be added to `dataOut`
39 Dictionary with extra metadata to be be added to `dataOut`
40
40
41 Examples
41 Examples
42 --------
42 --------
43
43
44 desc = {
44 desc = {
45 'Data': {
45 'Data': {
46 'data_output': ['u', 'v', 'w'],
46 'data_output': ['u', 'v', 'w'],
47 'utctime': 'timestamps',
47 'utctime': 'timestamps',
48 } ,
48 } ,
49 'Metadata': {
49 'Metadata': {
50 'heightList': 'heights'
50 'heightList': 'heights'
51 }
51 }
52 }
52 }
53
53
54 desc = {
54 desc = {
55 'Data': {
55 'Data': {
56 'data_output': 'winds',
56 'data_output': 'winds',
57 'utctime': 'timestamps'
57 'utctime': 'timestamps'
58 },
58 },
59 'Metadata': {
59 'Metadata': {
60 'heightList': 'heights'
60 'heightList': 'heights'
61 }
61 }
62 }
62 }
63
63
64 extras = {
64 extras = {
65 'timeZone': 300
65 'timeZone': 300
66 }
66 }
67
67
68 reader = project.addReadUnit(
68 reader = project.addReadUnit(
69 name='HDFReader',
69 name='HDFReader',
70 path='/path/to/files',
70 path='/path/to/files',
71 startDate='2019/01/01',
71 startDate='2019/01/01',
72 endDate='2019/01/31',
72 endDate='2019/01/31',
73 startTime='00:00:00',
73 startTime='00:00:00',
74 endTime='23:59:59',
74 endTime='23:59:59',
75 # description=json.dumps(desc),
75 # description=json.dumps(desc),
76 # extras=json.dumps(extras),
76 # extras=json.dumps(extras),
77 )
77 )
78
78
79 """
79 """
80
80
81 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'description', 'extras']
81 __attrs__ = ['path', 'startDate', 'endDate', 'startTime', 'endTime', 'description', 'extras']
82
82
83 def __init__(self):
83 def __init__(self):
84 ProcessingUnit.__init__(self)
84 ProcessingUnit.__init__(self)
85 self.dataOut = Parameters()
85 self.dataOut = Parameters()
86 self.ext = ".hdf5"
86 self.ext = ".hdf5"
87 self.optchar = "D"
87 self.optchar = "D"
88 self.meta = {}
88 self.meta = {}
89 self.data = {}
89 self.data = {}
90 self.open_file = h5py.File
90 self.open_file = h5py.File
91 self.open_mode = 'r'
91 self.open_mode = 'r'
92 self.description = {}
92 self.description = {}
93 self.extras = {}
93 self.extras = {}
94 self.filefmt = "*%Y%j***"
94 self.filefmt = "*%Y%j***"
95 self.folderfmt = "*%Y%j"
95 self.folderfmt = "*%Y%j"
96 self.utcoffset = 0
96 self.utcoffset = 0
97
97
98 def setup(self, **kwargs):
98 def setup(self, **kwargs):
99
99
100 self.set_kwargs(**kwargs)
100 self.set_kwargs(**kwargs)
101 if not self.ext.startswith('.'):
101 if not self.ext.startswith('.'):
102 self.ext = '.{}'.format(self.ext)
102 self.ext = '.{}'.format(self.ext)
103
103
104 if self.online:
104 if self.online:
105 log.log("Searching files in online mode...", self.name)
105 log.log("Searching files in online mode...", self.name)
106
106
107 for nTries in range(self.nTries):
107 for nTries in range(self.nTries):
108 fullpath = self.searchFilesOnLine(self.path, self.startDate,
108 fullpath = self.searchFilesOnLine(self.path, self.startDate,
109 self.endDate, self.expLabel, self.ext, self.walk,
109 self.endDate, self.expLabel, self.ext, self.walk,
110 self.filefmt, self.folderfmt)
110 self.filefmt, self.folderfmt)
111 try:
111 try:
112 fullpath = next(fullpath)
112 fullpath = next(fullpath)
113 except:
113 except:
114 fullpath = None
114 fullpath = None
115
115
116 if fullpath:
116 if fullpath:
117 break
117 break
118
118
119 log.warning(
119 log.warning(
120 'Waiting {} sec for a valid file in {}: try {} ...'.format(
120 'Waiting {} sec for a valid file in {}: try {} ...'.format(
121 self.delay, self.path, nTries + 1),
121 self.delay, self.path, nTries + 1),
122 self.name)
122 self.name)
123 time.sleep(self.delay)
123 time.sleep(self.delay)
124
124
125 if not(fullpath):
125 if not(fullpath):
126 raise schainpy.admin.SchainError(
126 raise schainpy.admin.SchainError(
127 'There isn\'t any valid file in {}'.format(self.path))
127 'There isn\'t any valid file in {}'.format(self.path))
128
128
129 pathname, filename = os.path.split(fullpath)
129 pathname, filename = os.path.split(fullpath)
130 self.year = int(filename[1:5])
130 self.year = int(filename[1:5])
131 self.doy = int(filename[5:8])
131 self.doy = int(filename[5:8])
132 self.set = int(filename[8:11]) - 1
132 self.set = int(filename[8:11]) - 1
133 else:
133 else:
134 log.log("Searching files in {}".format(self.path), self.name)
134 log.log("Searching files in {}".format(self.path), self.name)
135 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
135 self.filenameList = self.searchFilesOffLine(self.path, self.startDate,
136 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
136 self.endDate, self.expLabel, self.ext, self.walk, self.filefmt, self.folderfmt)
137
137
138 self.setNextFile()
138 self.setNextFile()
139
139
140 return
140 return
141
141
142 def readFirstHeader(self):
142 def readFirstHeader(self):
143 '''Read metadata and data'''
143 '''Read metadata and data'''
144
144
145 self.__readMetadata()
145 self.__readMetadata()
146 self.__readData()
146 self.__readData()
147 self.__setBlockList()
147 self.__setBlockList()
148
148
149 for attr in self.meta:
149 for attr in self.meta:
150 setattr(self.dataOut, attr, self.meta[attr])
150 setattr(self.dataOut, attr, self.meta[attr])
151
151
152 self.blockIndex = 0
152 self.blockIndex = 0
153
153
154 return
154 return
155
155
156 def __setBlockList(self):
156 def __setBlockList(self):
157 '''
157 '''
158 Selects the data within the times defined
158 Selects the data within the times defined
159
159
160 self.fp
160 self.fp
161 self.startTime
161 self.startTime
162 self.endTime
162 self.endTime
163 self.blockList
163 self.blockList
164 self.blocksPerFile
164 self.blocksPerFile
165
165
166 '''
166 '''
167
167
168 startTime = self.startTime
168 startTime = self.startTime
169 endTime = self.endTime
169 endTime = self.endTime
170 thisUtcTime = self.data['utctime'] + self.utcoffset
170 thisUtcTime = self.data['utctime'] + self.utcoffset
171 self.interval = numpy.min(thisUtcTime[1:] - thisUtcTime[:-1])
171 self.interval = numpy.min(thisUtcTime[1:] - thisUtcTime[:-1])
172 thisDatetime = datetime.datetime.utcfromtimestamp(thisUtcTime[0])
172 thisDatetime = datetime.datetime.utcfromtimestamp(thisUtcTime[0])
173
173
174 thisDate = thisDatetime.date()
174 thisDate = thisDatetime.date()
175 thisTime = thisDatetime.time()
175 thisTime = thisDatetime.time()
176
176
177 startUtcTime = (datetime.datetime.combine(thisDate, startTime) - datetime.datetime(1970, 1, 1)).total_seconds()
177 startUtcTime = (datetime.datetime.combine(thisDate, startTime) - datetime.datetime(1970, 1, 1)).total_seconds()
178 endUtcTime = (datetime.datetime.combine(thisDate, endTime) - datetime.datetime(1970, 1, 1)).total_seconds()
178 endUtcTime = (datetime.datetime.combine(thisDate, endTime) - datetime.datetime(1970, 1, 1)).total_seconds()
179
179
180 ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0]
180 ind = numpy.where(numpy.logical_and(thisUtcTime >= startUtcTime, thisUtcTime < endUtcTime))[0]
181
181
182 self.blockList = ind
182 self.blockList = ind
183 self.blocksPerFile = len(ind)
183 self.blocksPerFile = len(ind)
184 return
184 return
185
185
186 def __readMetadata(self):
186 def __readMetadata(self):
187 '''
187 '''
188 Reads Metadata
188 Reads Metadata
189 '''
189 '''
190
190
191 meta = {}
191 meta = {}
192
192
193 if self.description:
193 if self.description:
194 for key, value in self.description['Metadata'].items():
194 for key, value in self.description['Metadata'].items():
195 meta[key] = self.fp[value][()]
195 meta[key] = self.fp[value][()]
196 else:
196 else:
197 grp = self.fp['Metadata']
197 grp = self.fp['Metadata']
198 for name in grp:
198 for name in grp:
199 meta[name] = grp[name][()]
199 meta[name] = grp[name][()]
200
200
201 if self.extras:
201 if self.extras:
202 for key, value in self.extras.items():
202 for key, value in self.extras.items():
203 meta[key] = value
203 meta[key] = value
204 self.meta = meta
204 self.meta = meta
205
205
206 return
206 return
207
207
208 def __readData(self):
208 def __readData(self):
209
209
210 data = {}
210 data = {}
211
211
212 if self.description:
212 if self.description:
213 for key, value in self.description['Data'].items():
213 for key, value in self.description['Data'].items():
214 if isinstance(value, str):
214 if isinstance(value, str):
215 if isinstance(self.fp[value], h5py.Dataset):
215 if isinstance(self.fp[value], h5py.Dataset):
216 data[key] = self.fp[value][()]
216 data[key] = self.fp[value][()]
217 elif isinstance(self.fp[value], h5py.Group):
217 elif isinstance(self.fp[value], h5py.Group):
218 array = []
218 array = []
219 for ch in self.fp[value]:
219 for ch in self.fp[value]:
220 array.append(self.fp[value][ch][()])
220 array.append(self.fp[value][ch][()])
221 data[key] = numpy.array(array)
221 data[key] = numpy.array(array)
222 elif isinstance(value, list):
222 elif isinstance(value, list):
223 array = []
223 array = []
224 for ch in value:
224 for ch in value:
225 array.append(self.fp[ch][()])
225 array.append(self.fp[ch][()])
226 data[key] = numpy.array(array)
226 data[key] = numpy.array(array)
227 else:
227 else:
228 grp = self.fp['Data']
228 grp = self.fp['Data']
229 for name in grp:
229 for name in grp:
230 if isinstance(grp[name], h5py.Dataset):
230 if isinstance(grp[name], h5py.Dataset):
231 array = grp[name][()]
231 array = grp[name][()]
232 elif isinstance(grp[name], h5py.Group):
232 elif isinstance(grp[name], h5py.Group):
233 array = []
233 array = []
234 for ch in grp[name]:
234 for ch in grp[name]:
235 array.append(grp[name][ch][()])
235 array.append(grp[name][ch][()])
236 array = numpy.array(array)
236 array = numpy.array(array)
237 else:
237 else:
238 log.warning('Unknown type: {}'.format(name))
238 log.warning('Unknown type: {}'.format(name))
239
239
240 if name in self.description:
240 if name in self.description:
241 key = self.description[name]
241 key = self.description[name]
242 else:
242 else:
243 key = name
243 key = name
244 data[key] = array
244 data[key] = array
245
245
246 self.data = data
246 self.data = data
247 return
247 return
248
248
249 def getData(self):
249 def getData(self):
250
250
251 for attr in self.data:
251 for attr in self.data:
252 if self.data[attr].ndim == 1:
252 if self.data[attr].ndim == 1:
253 setattr(self.dataOut, attr, self.data[attr][self.blockIndex])
253 setattr(self.dataOut, attr, self.data[attr][self.blockIndex])
254 else:
254 else:
255 setattr(self.dataOut, attr, self.data[attr][:, self.blockIndex])
255 setattr(self.dataOut, attr, self.data[attr][:, self.blockIndex])
256
256
257 self.dataOut.flagNoData = False
257 self.dataOut.flagNoData = False
258 self.blockIndex += 1
258 self.blockIndex += 1
259
259
260 log.log("Block No. {}/{} -> {}".format(
260 log.log("Block No. {}/{} -> {}".format(
261 self.blockIndex,
261 self.blockIndex,
262 self.blocksPerFile,
262 self.blocksPerFile,
263 self.dataOut.datatime.ctime()), self.name)
263 self.dataOut.datatime.ctime()), self.name)
264
264
265 return
265 return
266
266
267 def run(self, **kwargs):
267 def run(self, **kwargs):
268
268
269 if not(self.isConfig):
269 if not(self.isConfig):
270 self.setup(**kwargs)
270 self.setup(**kwargs)
271 self.isConfig = True
271 self.isConfig = True
272
272
273 if self.blockIndex == self.blocksPerFile:
273 if self.blockIndex == self.blocksPerFile:
274 self.setNextFile()
274 self.setNextFile()
275
275
276 self.getData()
276 self.getData()
277
277
278 if 'type' in self.meta:
278 if 'type' in self.meta:
279 self.dataOut.type = self.meta['type'].decode('utf-8')
279 self.dataOut.type = self.meta['type'].decode('utf-8')
280
280
281 return
281 return
282
282
283 @MPDecorator
283 @MPDecorator
284 class HDFWriter(Operation):
284 class HDFWriter(Operation):
285 """Operation to write HDF5 files.
285 """Operation to write HDF5 files.
286
286
287 The HDF5 file contains by default two groups Data and Metadata where
287 The HDF5 file contains by default two groups Data and Metadata where
288 you can save any `dataOut` attribute specified by `dataList` and `metadataList`
288 you can save any `dataOut` attribute specified by `dataList` and `metadataList`
289 parameters, data attributes are normaly time dependent where the metadata
289 parameters, data attributes are normaly time dependent where the metadata
290 are not.
290 are not.
291 It is possible to customize the structure of the HDF5 file with the
291 It is possible to customize the structure of the HDF5 file with the
292 optional description parameter see the examples.
292 optional description parameter see the examples.
293
293
294 Parameters:
294 Parameters:
295 -----------
295 -----------
296 path : str
296 path : str
297 Path where files will be saved.
297 Path where files will be saved.
298 blocksPerFile : int
298 blocksPerFile : int
299 Number of blocks per file
299 Number of blocks per file
300 metadataList : list
300 metadataList : list
301 List of the dataOut attributes that will be saved as metadata
301 List of the dataOut attributes that will be saved as metadata
302 dataList : int
302 dataList : int
303 List of the dataOut attributes that will be saved as data
303 List of the dataOut attributes that will be saved as data
304 setType : bool
304 setType : bool
305 If True the name of the files corresponds to the timestamp of the data
305 If True the name of the files corresponds to the timestamp of the data
306 description : dict, optional
306 description : dict, optional
307 Dictionary with the desired description of the HDF5 file
307 Dictionary with the desired description of the HDF5 file
308
308
309 Examples
309 Examples
310 --------
310 --------
311
311
312 desc = {
312 desc = {
313 'data_output': {'winds': ['z', 'w', 'v']},
313 'data_output': {'winds': ['z', 'w', 'v']},
314 'utctime': 'timestamps',
314 'utctime': 'timestamps',
315 'heightList': 'heights'
315 'heightList': 'heights'
316 }
316 }
317 desc = {
317 desc = {
318 'data_output': ['z', 'w', 'v'],
318 'data_output': ['z', 'w', 'v'],
319 'utctime': 'timestamps',
319 'utctime': 'timestamps',
320 'heightList': 'heights'
320 'heightList': 'heights'
321 }
321 }
322 desc = {
322 desc = {
323 'Data': {
323 'Data': {
324 'data_output': 'winds',
324 'data_output': 'winds',
325 'utctime': 'timestamps'
325 'utctime': 'timestamps'
326 },
326 },
327 'Metadata': {
327 'Metadata': {
328 'heightList': 'heights'
328 'heightList': 'heights'
329 }
329 }
330 }
330 }
331
331
332 writer = proc_unit.addOperation(name='HDFWriter')
332 writer = proc_unit.addOperation(name='HDFWriter')
333 writer.addParameter(name='path', value='/path/to/file')
333 writer.addParameter(name='path', value='/path/to/file')
334 writer.addParameter(name='blocksPerFile', value='32')
334 writer.addParameter(name='blocksPerFile', value='32')
335 writer.addParameter(name='metadataList', value='heightList,timeZone')
335 writer.addParameter(name='metadataList', value='heightList,timeZone')
336 writer.addParameter(name='dataList',value='data_output,utctime')
336 writer.addParameter(name='dataList',value='data_output,utctime')
337 # writer.addParameter(name='description',value=json.dumps(desc))
337 # writer.addParameter(name='description',value=json.dumps(desc))
338
338
339 """
339 """
340
340
341 ext = ".hdf5"
341 ext = ".hdf5"
342 optchar = "D"
342 optchar = "D"
343 filename = None
343 filename = None
344 path = None
344 path = None
345 setFile = None
345 setFile = None
346 fp = None
346 fp = None
347 firsttime = True
347 firsttime = True
348 # Configurations
348 # Configurations
349 blocksPerFile = None
349 blocksPerFile = None
350 blockIndex = None
350 blockIndex = None
351 dataOut = None
351 dataOut = None
352 # Data Arrays
352 # Data Arrays
353 dataList = None
353 dataList = None
354 metadataList = None
354 metadataList = None
355 currentDay = None
355 currentDay = None
356 lastTime = None
356 lastTime = None
357
357
358 def __init__(self):
358 def __init__(self):
359
359
360 Operation.__init__(self)
360 Operation.__init__(self)
361 return
361 return
362
362
363 def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None, description=None):
363 def set_kwargs(self, **kwargs):
364
365 for key, value in kwargs.items():
366 setattr(self, key, value)
367
368 def set_kwargs_obj(self, obj, **kwargs):
369
370 for key, value in kwargs.items():
371 setattr(obj, key, value)
372
373 def setup(self, path=None, blocksPerFile=10, metadataList=None, dataList=None, setType=None, description=None, **kwargs):
364 self.path = path
374 self.path = path
365 self.blocksPerFile = blocksPerFile
375 self.blocksPerFile = blocksPerFile
366 self.metadataList = metadataList
376 self.metadataList = metadataList
367 self.dataList = [s.strip() for s in dataList]
377 self.dataList = [s.strip() for s in dataList]
368 self.setType = setType
378 self.setType = setType
369 self.description = description
379 self.description = description
380 self.set_kwargs(**kwargs)
370
381
371 if self.metadataList is None:
382 if self.metadataList is None:
372 self.metadataList = self.dataOut.metadata_list
383 self.metadataList = self.dataOut.metadata_list
373
384
374 tableList = []
385 tableList = []
375 dsList = []
386 dsList = []
376
387
377 for i in range(len(self.dataList)):
388 for i in range(len(self.dataList)):
378 dsDict = {}
389 dsDict = {}
379 if hasattr(self.dataOut, self.dataList[i]):
390 if hasattr(self.dataOut, self.dataList[i]):
380 dataAux = getattr(self.dataOut, self.dataList[i])
391 dataAux = getattr(self.dataOut, self.dataList[i])
381 dsDict['variable'] = self.dataList[i]
392 dsDict['variable'] = self.dataList[i]
382 else:
393 else:
383 log.warning('Attribute {} not found in dataOut', self.name)
394 log.warning('Attribute {} not found in dataOut', self.name)
384 continue
395 continue
385
396
386 if dataAux is None:
397 if dataAux is None:
387 continue
398 continue
388 elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)):
399 elif isinstance(dataAux, (int, float, numpy.integer, numpy.float32)):
389 dsDict['nDim'] = 0
400 dsDict['nDim'] = 0
390 else:
401 else:
391 dsDict['nDim'] = len(dataAux.shape)
402 dsDict['nDim'] = len(dataAux.shape)
392 dsDict['shape'] = dataAux.shape
403 dsDict['shape'] = dataAux.shape
393 dsDict['dsNumber'] = dataAux.shape[0]
404 dsDict['dsNumber'] = dataAux.shape[0]
394 dsDict['dtype'] = dataAux.dtype
405 dsDict['dtype'] = dataAux.dtype
395
406
396 dsList.append(dsDict)
407 dsList.append(dsDict)
397
408
398 self.dsList = dsList
409 self.dsList = dsList
399 self.currentDay = self.dataOut.datatime.date()
410 self.currentDay = self.dataOut.datatime.date()
400
411
401 def timeFlag(self):
412 def timeFlag(self):
402 currentTime = self.dataOut.utctime
413 currentTime = self.dataOut.utctime
403 timeTuple = time.localtime(currentTime)
414 timeTuple = time.localtime(currentTime)
404 dataDay = timeTuple.tm_yday
415 dataDay = timeTuple.tm_yday
405
416
406 if self.lastTime is None:
417 if self.lastTime is None:
407 self.lastTime = currentTime
418 self.lastTime = currentTime
408 self.currentDay = dataDay
419 self.currentDay = dataDay
409 return False
420 return False
410
421
411 timeDiff = currentTime - self.lastTime
422 timeDiff = currentTime - self.lastTime
412
423
413 # Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
424 # Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
414 if dataDay != self.currentDay:
425 if dataDay != self.currentDay:
415 self.currentDay = dataDay
426 self.currentDay = dataDay
416 return True
427 return True
417 elif timeDiff > 3 * 60 * 60:
428 elif timeDiff > 3 * 60 * 60:
418 self.lastTime = currentTime
429 self.lastTime = currentTime
419 return True
430 return True
420 else:
431 else:
421 self.lastTime = currentTime
432 self.lastTime = currentTime
422 return False
433 return False
423
434
424 def run(self, dataOut, path, blocksPerFile=10, metadataList=None,
435 def run(self, dataOut, path, blocksPerFile=10, metadataList=None,
425 dataList=[], setType=None, description={}):
436 dataList=[], setType=None, description={}, **kwargs):
426
437
427 self.dataOut = dataOut
438 self.dataOut = dataOut
439 self.set_kwargs_obj(self.dataOut, **kwargs)
428 if not(self.isConfig):
440 if not(self.isConfig):
429 self.setup(path=path, blocksPerFile=blocksPerFile,
441 self.setup(path=path, blocksPerFile=blocksPerFile,
430 metadataList=metadataList, dataList=dataList,
442 metadataList=metadataList, dataList=dataList,
431 setType=setType, description=description)
443 setType=setType, description=description, **kwargs)
432
444
433 self.isConfig = True
445 self.isConfig = True
434 self.setNextFile()
446 self.setNextFile()
435
447
436 self.putData()
448 self.putData()
437 return
449 return
438
450
439 def setNextFile(self):
451 def setNextFile(self):
440
452
441 ext = self.ext
453 ext = self.ext
442 path = self.path
454 path = self.path
443 setFile = self.setFile
455 setFile = self.setFile
444
456
445 timeTuple = time.localtime(self.dataOut.utctime)
457 timeTuple = time.localtime(self.dataOut.utctime)
446 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
458 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
447 fullpath = os.path.join(path, subfolder)
459 fullpath = os.path.join(path, subfolder)
448
460
449 if os.path.exists(fullpath):
461 if os.path.exists(fullpath):
450 filesList = os.listdir(fullpath)
462 filesList = os.listdir(fullpath)
451 filesList = [k for k in filesList if k.startswith(self.optchar)]
463 filesList = [k for k in filesList if k.startswith(self.optchar)]
452 if len(filesList) > 0:
464 if len(filesList) > 0:
453 filesList = sorted(filesList, key=str.lower)
465 filesList = sorted(filesList, key=str.lower)
454 filen = filesList[-1]
466 filen = filesList[-1]
455 # el filename debera tener el siguiente formato
467 # el filename debera tener el siguiente formato
456 # 0 1234 567 89A BCDE (hex)
468 # 0 1234 567 89A BCDE (hex)
457 # x YYYY DDD SSS .ext
469 # x YYYY DDD SSS .ext
458 if isNumber(filen[8:11]):
470 if isNumber(filen[8:11]):
459 setFile = int(filen[8:11]) # inicializo mi contador de seteo al seteo del ultimo file
471 setFile = int(filen[8:11]) # inicializo mi contador de seteo al seteo del ultimo file
460 else:
472 else:
461 setFile = -1
473 setFile = -1
462 else:
474 else:
463 setFile = -1 # inicializo mi contador de seteo
475 setFile = -1 # inicializo mi contador de seteo
464 else:
476 else:
465 os.makedirs(fullpath)
477 os.makedirs(fullpath)
466 setFile = -1 # inicializo mi contador de seteo
478 setFile = -1 # inicializo mi contador de seteo
467
479
468 if self.setType is None:
480 if self.setType is None:
469 setFile += 1
481 setFile += 1
470 file = '%s%4.4d%3.3d%03d%s' % (self.optchar,
482 file = '%s%4.4d%3.3d%03d%s' % (self.optchar,
471 timeTuple.tm_year,
483 timeTuple.tm_year,
472 timeTuple.tm_yday,
484 timeTuple.tm_yday,
473 setFile,
485 setFile,
474 ext)
486 ext)
475 else:
487 else:
476 setFile = timeTuple.tm_hour * 60 + timeTuple.tm_min
488 setFile = timeTuple.tm_hour * 60 + timeTuple.tm_min
477 file = '%s%4.4d%3.3d%04d%s' % (self.optchar,
489 file = '%s%4.4d%3.3d%04d%s' % (self.optchar,
478 timeTuple.tm_year,
490 timeTuple.tm_year,
479 timeTuple.tm_yday,
491 timeTuple.tm_yday,
480 setFile,
492 setFile,
481 ext)
493 ext)
482
494
483 self.filename = os.path.join(path, subfolder, file)
495 self.filename = os.path.join(path, subfolder, file)
484
496
485 # Setting HDF5 File
497 # Setting HDF5 File
486 self.fp = h5py.File(self.filename, 'w')
498 self.fp = h5py.File(self.filename, 'w')
487 # write metadata
499 # write metadata
488 self.writeMetadata(self.fp)
500 self.writeMetadata(self.fp)
489 # Write data
501 # Write data
490 self.writeData(self.fp)
502 self.writeData(self.fp)
491
503
492 def getLabel(self, name, x=None):
504 def getLabel(self, name, x=None):
493
505
494 if x is None:
506 if x is None:
495 if 'Data' in self.description:
507 if 'Data' in self.description:
496 data = self.description['Data']
508 data = self.description['Data']
497 if 'Metadata' in self.description:
509 if 'Metadata' in self.description:
498 data.update(self.description['Metadata'])
510 data.update(self.description['Metadata'])
499 else:
511 else:
500 data = self.description
512 data = self.description
501 if name in data:
513 if name in data:
502 if isinstance(data[name], str):
514 if isinstance(data[name], str):
503 return data[name]
515 return data[name]
504 elif isinstance(data[name], list):
516 elif isinstance(data[name], list):
505 return None
517 return None
506 elif isinstance(data[name], dict):
518 elif isinstance(data[name], dict):
507 for key, value in data[name].items():
519 for key, value in data[name].items():
508 return key
520 return key
509 return name
521 return name
510 else:
522 else:
511 if 'Metadata' in self.description:
523 if 'Data' in self.description:
512 meta = self.description['Metadata']
524 data = self.description['Data']
525 if 'Metadata' in self.description:
526 data.update(self.description['Metadata'])
513 else:
527 else:
514 meta = self.description
528 data = self.description
515 if name in meta:
529 if name in data:
516 if isinstance(meta[name], list):
530 if isinstance(data[name], list):
517 return meta[name][x]
531 return data[name][x]
518 elif isinstance(meta[name], dict):
532 elif isinstance(data[name], dict):
519 for key, value in meta[name].items():
533 for key, value in data[name].items():
520 return value[x]
534 return value[x]
521 if 'cspc' in name:
535 if 'cspc' in name:
522 return 'pair{:02d}'.format(x)
536 return 'pair{:02d}'.format(x)
523 else:
537 else:
524 return 'channel{:02d}'.format(x)
538 return 'channel{:02d}'.format(x)
525
539
526 def writeMetadata(self, fp):
540 def writeMetadata(self, fp):
527
541
528 if self.description:
542 if self.description:
529 if 'Metadata' in self.description:
543 if 'Metadata' in self.description:
530 grp = fp.create_group('Metadata')
544 grp = fp.create_group('Metadata')
531 else:
545 else:
532 grp = fp
546 grp = fp
533 else:
547 else:
534 grp = fp.create_group('Metadata')
548 grp = fp.create_group('Metadata')
535
549
536 for i in range(len(self.metadataList)):
550 for i in range(len(self.metadataList)):
537 if not hasattr(self.dataOut, self.metadataList[i]):
551 if not hasattr(self.dataOut, self.metadataList[i]):
538 log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name)
552 log.warning('Metadata: `{}` not found'.format(self.metadataList[i]), self.name)
539 continue
553 continue
540 value = getattr(self.dataOut, self.metadataList[i])
554 value = getattr(self.dataOut, self.metadataList[i])
541 if isinstance(value, bool):
555 if isinstance(value, bool):
542 if value is True:
556 if value is True:
543 value = 1
557 value = 1
544 else:
558 else:
545 value = 0
559 value = 0
546 grp.create_dataset(self.getLabel(self.metadataList[i]), data=value)
560 grp.create_dataset(self.getLabel(self.metadataList[i]), data=value)
547 return
561 return
548
562
549 def writeData(self, fp):
563 def writeData(self, fp):
550
564
551 if self.description:
565 if self.description:
552 if 'Data' in self.description:
566 if 'Data' in self.description:
553 grp = fp.create_group('Data')
567 grp = fp.create_group('Data')
554 else:
568 else:
555 grp = fp
569 grp = fp
556 else:
570 else:
557 grp = fp.create_group('Data')
571 grp = fp.create_group('Data')
558
572
559 dtsets = []
573 dtsets = []
560 data = []
574 data = []
561
575
562 for dsInfo in self.dsList:
576 for dsInfo in self.dsList:
563 if dsInfo['nDim'] == 0:
577 if dsInfo['nDim'] == 0:
564 ds = grp.create_dataset(
578 ds = grp.create_dataset(
565 self.getLabel(dsInfo['variable']),
579 self.getLabel(dsInfo['variable']),
566 (self.blocksPerFile,),
580 (self.blocksPerFile,),
567 chunks=True,
581 chunks=True,
568 dtype=numpy.float64)
582 dtype=numpy.float64)
569 dtsets.append(ds)
583 dtsets.append(ds)
570 data.append((dsInfo['variable'], -1))
584 data.append((dsInfo['variable'], -1))
571 else:
585 else:
572 label = self.getLabel(dsInfo['variable'])
586 label = self.getLabel(dsInfo['variable'])
573 if label is not None:
587 if label is not None:
574 sgrp = grp.create_group(label)
588 sgrp = grp.create_group(label)
575 else:
589 else:
576 sgrp = grp
590 sgrp = grp
577 for i in range(dsInfo['dsNumber']):
591 for i in range(dsInfo['dsNumber']):
578 ds = sgrp.create_dataset(
592 ds = sgrp.create_dataset(
579 self.getLabel(dsInfo['variable'], i),
593 self.getLabel(dsInfo['variable'], i),
580 (self.blocksPerFile,) + dsInfo['shape'][1:],
594 (self.blocksPerFile,) + dsInfo['shape'][1:],
581 chunks=True,
595 chunks=True,
582 dtype=dsInfo['dtype'])
596 dtype=dsInfo['dtype'])
583 dtsets.append(ds)
597 dtsets.append(ds)
584 data.append((dsInfo['variable'], i))
598 data.append((dsInfo['variable'], i))
585 fp.flush()
599 fp.flush()
586
600
587 log.log('Creating file: {}'.format(fp.filename), self.name)
601 log.log('Creating file: {}'.format(fp.filename), self.name)
588
602
589 self.ds = dtsets
603 self.ds = dtsets
590 self.data = data
604 self.data = data
591 self.firsttime = True
605 self.firsttime = True
592 self.blockIndex = 0
606 self.blockIndex = 0
593 return
607 return
594
608
595 def putData(self):
609 def putData(self):
596
610
597 if (self.blockIndex == self.blocksPerFile) or self.timeFlag():
611 if (self.blockIndex == self.blocksPerFile) or self.timeFlag():
598 self.closeFile()
612 self.closeFile()
599 self.setNextFile()
613 self.setNextFile()
600
614
601 for i, ds in enumerate(self.ds):
615 for i, ds in enumerate(self.ds):
602 attr, ch = self.data[i]
616 attr, ch = self.data[i]
603 if ch == -1:
617 if ch == -1:
604 ds[self.blockIndex] = getattr(self.dataOut, attr)
618 ds[self.blockIndex] = getattr(self.dataOut, attr)
605 else:
619 else:
606 ds[self.blockIndex] = getattr(self.dataOut, attr)[ch]
620 ds[self.blockIndex] = getattr(self.dataOut, attr)[ch]
607
621
608 self.fp.flush()
622 self.fp.flush()
609 self.blockIndex += 1
623 self.blockIndex += 1
610 log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name)
624 log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name)
611
625
612 return
626 return
613
627
614 def closeFile(self):
628 def closeFile(self):
615
629
616 if self.blockIndex != self.blocksPerFile:
630 if self.blockIndex != self.blocksPerFile:
617 for ds in self.ds:
631 for ds in self.ds:
618 ds.resize(self.blockIndex, axis=0)
632 ds.resize(self.blockIndex, axis=0)
619
633
620 if self.fp:
634 if self.fp:
621 self.fp.flush()
635 self.fp.flush()
622 self.fp.close()
636 self.fp.close()
623
637
624 def close(self):
638 def close(self):
625
639
626 self.closeFile()
640 self.closeFile()
627
641
628
642
629 @MPDecorator
643 @MPDecorator
630 class ASCIIWriter(Operation):
644 class ASCIIWriter(Operation):
631 """Operation to write data in ascii files.
645 """Operation to write data in ascii files.
632
646
633 Parameters:
647 Parameters:
634 -----------
648 -----------
635 path : str
649 path : str
636 Path where files will be saved.
650 Path where files will be saved.
637 blocksPerFile : int
651 blocksPerFile : int
638 Number of blocks per file
652 Number of blocks per file
639 metadataList : list
653 metadataList : list
640 List of the dataOut attributes that will be saved as metadata
654 List of the dataOut attributes that will be saved as metadata
641 dataDict : dict
655 dataDict : dict
642 Dictionary with the varaibles to be saved
656 Dictionary with the varaibles to be saved
643 setType : bool
657 setType : bool
644 If True the name of the files corresponds to the timestamp of the data
658 If True the name of the files corresponds to the timestamp of the data
645
659
646 Examples
660 Examples
647 --------
661 --------
648
662
649 data = {
663 data = {
650 'data_output': ['z', 'w', 'v'],
664 'data_output': ['z', 'w', 'v'],
651 'utctime': 'time',
665 'utctime': 'time',
652 'heightList': 'height'
666 'heightList': 'height'
653 }
667 }
654
668
655 writer = proc_unit.addOperation(name='ASCIIWriter')
669 writer = proc_unit.addOperation(name='ASCIIWriter')
656 writer.addParameter(name='path', value='/path/to/file')
670 writer.addParameter(name='path', value='/path/to/file')
657 writer.addParameter(name='blocksPerFile', value='32')
671 writer.addParameter(name='blocksPerFile', value='32')
658 writer.addParameter(name='dataDict',value=json.dumps(data))
672 writer.addParameter(name='dataDict',value=json.dumps(data))
659
673
660 """
674 """
661
675
662 ext = ".txt"
676 ext = ".txt"
663 optchar = "D"
677 optchar = "D"
664 filename = None
678 filename = None
665 path = None
679 path = None
666 setFile = None
680 setFile = None
667 fp = None
681 fp = None
668 firsttime = True
682 firsttime = True
669 # Configurations
683 # Configurations
670 blocksPerFile = None
684 blocksPerFile = None
671 blockIndex = None
685 blockIndex = None
672 dataOut = None
686 dataOut = None
673 # Data Arrays
687 # Data Arrays
674 dataDict = None
688 dataDict = None
675 metadataList = None
689 metadataList = None
676 currentDay = None
690 currentDay = None
677 lastTime = None
691 lastTime = None
678 localtime = True
692 localtime = True
679
693
680 def __init__(self):
694 def __init__(self):
681
695
682 Operation.__init__(self)
696 Operation.__init__(self)
683 return
697 return
684
698
685 def setup(self, path=None, blocksPerFile=10, metadataList=None, dataDict=None, setType=None, localtime=True):
699 def setup(self, path=None, blocksPerFile=10, metadataList=None, dataDict=None, setType=None, localtime=True):
686 self.path = path
700 self.path = path
687 self.blocksPerFile = blocksPerFile
701 self.blocksPerFile = blocksPerFile
688 self.metadataList = metadataList
702 self.metadataList = metadataList
689 self.dataDict = dataDict
703 self.dataDict = dataDict
690 self.setType = setType
704 self.setType = setType
691 self.localtime = localtime
705 self.localtime = localtime
692
706
693 if self.metadataList is None:
707 if self.metadataList is None:
694 self.metadataList = self.dataOut.metadata_list
708 self.metadataList = self.dataOut.metadata_list
695
709
696 dsList = []
710 dsList = []
697
711
698 for key, value in self.dataDict.items():
712 for key, value in self.dataDict.items():
699 dsDict = {}
713 dsDict = {}
700 if hasattr(self.dataOut, key):
714 if hasattr(self.dataOut, key):
701 dataAux = getattr(self.dataOut, key)
715 dataAux = getattr(self.dataOut, key)
702 dsDict['variable'] = key
716 dsDict['variable'] = key
703 else:
717 else:
704 log.warning('Attribute {} not found in dataOut', self.name)
718 log.warning('Attribute {} not found in dataOut', self.name)
705 continue
719 continue
706
720
707 if dataAux is None:
721 if dataAux is None:
708 continue
722 continue
709 elif isinstance(dataAux, (int, float, numpy.integer, numpy.float)):
723 elif isinstance(dataAux, (int, float, numpy.integer, numpy.float32)):
710 dsDict['nDim'] = 0
724 dsDict['nDim'] = 0
711 else:
725 else:
712 dsDict['nDim'] = len(dataAux.shape)
726 dsDict['nDim'] = len(dataAux.shape)
713 dsDict['shape'] = dataAux.shape
727 dsDict['shape'] = dataAux.shape
714 dsDict['dsNumber'] = dataAux.shape[0]
728 dsDict['dsNumber'] = dataAux.shape[0]
715 dsDict['dtype'] = dataAux.dtype
729 dsDict['dtype'] = dataAux.dtype
716
730
717 dsList.append(dsDict)
731 dsList.append(dsDict)
718 self.dsList = dsList
732 self.dsList = dsList
719 self.currentDay = self.dataOut.datatime.date()
733 self.currentDay = self.dataOut.datatime.date()
720
734
721 def timeFlag(self):
735 def timeFlag(self):
722 currentTime = self.dataOut.utctime
736 currentTime = self.dataOut.utctime
723 if self.localtime:
737 if self.localtime:
724 timeTuple = time.localtime(currentTime)
738 timeTuple = time.localtime(currentTime)
725 else:
739 else:
726 timeTuple = time.gmtime(currentTime)
740 timeTuple = time.gmtime(currentTime)
727
741
728 dataDay = timeTuple.tm_yday
742 dataDay = timeTuple.tm_yday
729
743
730 if self.lastTime is None:
744 if self.lastTime is None:
731 self.lastTime = currentTime
745 self.lastTime = currentTime
732 self.currentDay = dataDay
746 self.currentDay = dataDay
733 return False
747 return False
734
748
735 timeDiff = currentTime - self.lastTime
749 timeDiff = currentTime - self.lastTime
736
750
737 # Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
751 # Si el dia es diferente o si la diferencia entre un dato y otro supera la hora
738 if dataDay != self.currentDay:
752 if dataDay != self.currentDay:
739 self.currentDay = dataDay
753 self.currentDay = dataDay
740 return True
754 return True
741 elif timeDiff > 3 * 60 * 60:
755 elif timeDiff > 3 * 60 * 60:
742 self.lastTime = currentTime
756 self.lastTime = currentTime
743 return True
757 return True
744 else:
758 else:
745 self.lastTime = currentTime
759 self.lastTime = currentTime
746 return False
760 return False
747
761
748 def run(self, dataOut, path, blocksPerFile=10, metadataList=None,
762 def run(self, dataOut, path, blocksPerFile=10, metadataList=None,
749 dataDict={}, setType=None, localtime=True):
763 dataDict={}, setType=None, localtime=True):
750
764
751 self.dataOut = dataOut
765 self.dataOut = dataOut
752 if not(self.isConfig):
766 if not(self.isConfig):
753 self.setup(path=path, blocksPerFile=blocksPerFile,
767 self.setup(path=path, blocksPerFile=blocksPerFile,
754 metadataList=metadataList, dataDict=dataDict,
768 metadataList=metadataList, dataDict=dataDict,
755 setType=setType, localtime=localtime)
769 setType=setType, localtime=localtime)
756
770
757 self.isConfig = True
771 self.isConfig = True
758 self.setNextFile()
772 self.setNextFile()
759
773
760 self.putData()
774 self.putData()
761 return
775 return
762
776
763 def setNextFile(self):
777 def setNextFile(self):
764
778
765 ext = self.ext
779 ext = self.ext
766 path = self.path
780 path = self.path
767 setFile = self.setFile
781 setFile = self.setFile
768 if self.localtime:
782 if self.localtime:
769 timeTuple = time.localtime(self.dataOut.utctime)
783 timeTuple = time.localtime(self.dataOut.utctime)
770 else:
784 else:
771 timeTuple = time.gmtime(self.dataOut.utctime)
785 timeTuple = time.gmtime(self.dataOut.utctime)
772 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
786 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year, timeTuple.tm_yday)
773 fullpath = os.path.join(path, subfolder)
787 fullpath = os.path.join(path, subfolder)
774
788
775 if os.path.exists(fullpath):
789 if os.path.exists(fullpath):
776 filesList = os.listdir(fullpath)
790 filesList = os.listdir(fullpath)
777 filesList = [k for k in filesList if k.startswith(self.optchar)]
791 filesList = [k for k in filesList if k.startswith(self.optchar)]
778 if len(filesList) > 0:
792 if len(filesList) > 0:
779 filesList = sorted(filesList, key=str.lower)
793 filesList = sorted(filesList, key=str.lower)
780 filen = filesList[-1]
794 filen = filesList[-1]
781 # el filename debera tener el siguiente formato
795 # el filename debera tener el siguiente formato
782 # 0 1234 567 89A BCDE (hex)
796 # 0 1234 567 89A BCDE (hex)
783 # x YYYY DDD SSS .ext
797 # x YYYY DDD SSS .ext
784 if isNumber(filen[8:11]):
798 if isNumber(filen[8:11]):
785 setFile = int(filen[8:11]) # inicializo mi contador de seteo al seteo del ultimo file
799 setFile = int(filen[8:11]) # inicializo mi contador de seteo al seteo del ultimo file
786 else:
800 else:
787 setFile = -1
801 setFile = -1
788 else:
802 else:
789 setFile = -1 # inicializo mi contador de seteo
803 setFile = -1 # inicializo mi contador de seteo
790 else:
804 else:
791 os.makedirs(fullpath)
805 os.makedirs(fullpath)
792 setFile = -1 # inicializo mi contador de seteo
806 setFile = -1 # inicializo mi contador de seteo
793
807
794 if self.setType is None:
808 if self.setType is None:
795 setFile += 1
809 setFile += 1
796 file = '%s%4.4d%3.3d%03d%s' % (self.optchar,
810 file = '%s%4.4d%3.3d%03d%s' % (self.optchar,
797 timeTuple.tm_year,
811 timeTuple.tm_year,
798 timeTuple.tm_yday,
812 timeTuple.tm_yday,
799 setFile,
813 setFile,
800 ext)
814 ext)
801 else:
815 else:
802 setFile = timeTuple.tm_hour * 60 + timeTuple.tm_min
816 setFile = timeTuple.tm_hour * 60 + timeTuple.tm_min
803 file = '%s%4.4d%3.3d%04d%s' % (self.optchar,
817 file = '%s%4.4d%3.3d%04d%s' % (self.optchar,
804 timeTuple.tm_year,
818 timeTuple.tm_year,
805 timeTuple.tm_yday,
819 timeTuple.tm_yday,
806 setFile,
820 setFile,
807 ext)
821 ext)
808
822
809 self.filename = os.path.join(path, subfolder, file)
823 self.filename = os.path.join(path, subfolder, file)
810
824
811 # Setting HDF5 File
825 # Setting HDF5 File
812 self.fp = open(self.filename, 'w')
826 self.fp = open(self.filename, 'w')
813 # write metadata
827 # write metadata
814 self.writeMetadata(self.fp)
828 self.writeMetadata(self.fp)
815 # Write data
829 # Write data
816 self.writeData(self.fp)
830 self.writeData(self.fp)
817
831
818 def writeMetadata(self, fp):
832 def writeMetadata(self, fp):
819
833
820 line = ''
834 line = ''
821 for d in self.dsList:
835 for d in self.dsList:
822 par = self.dataDict[d['variable']]
836 par = self.dataDict[d['variable']]
823 if isinstance(par, (list,tuple)):
837 if isinstance(par, (list,tuple)):
824 for p in par:
838 for p in par:
825 line += '{:>16}'.format(p)
839 line += '{:>16}'.format(p)
826 else:
840 else:
827 line += '{:>16}'.format(par)
841 line += '{:>16}'.format(par)
828
842
829 line += '\n'
843 line += '\n'
830 fp.write(line)
844 fp.write(line)
831
845
832 def writeData(self, fp):
846 def writeData(self, fp):
833
847
834 log.log('Creating file: {}'.format(self.filename), self.name)
848 log.log('Creating file: {}'.format(self.filename), self.name)
835
849
836 self.firsttime = True
850 self.firsttime = True
837 self.blockIndex = 0
851 self.blockIndex = 0
838 return
852 return
839
853
840 def putData(self):
854 def putData(self):
841
855
842 if (self.blockIndex == self.blocksPerFile) or self.timeFlag():
856 if (self.blockIndex == self.blocksPerFile) or self.timeFlag():
843 self.closeFile()
857 self.closeFile()
844 self.setNextFile()
858 self.setNextFile()
845
859
846 line = ''
860 line = ''
847 for j in range(len(self.dataOut.heightList)):
861 for j in range(len(self.dataOut.heightList)):
848 for ds in self.dsList:
862 for ds in self.dsList:
849 par = self.dataDict[ds['variable']]
863 par = self.dataDict[ds['variable']]
850 if ds['nDim'] == 2:
864 if ds['nDim'] == 2:
851 for i in range(len(par)):
865 for i in range(len(par)):
852 line += '{:>16}'.format('%8.2f' % getattr(self.dataOut, ds['variable'])[i][j])
866 line += '{:>16}'.format('%8.2f' % getattr(self.dataOut, ds['variable'])[i][j])
853 elif ds['nDim'] == 1:
867 elif ds['nDim'] == 1:
854 line += '{:>16}'.format('%8.2f' % getattr(self.dataOut, ds['variable'])[j])
868 line += '{:>16}'.format('%8.2f' % getattr(self.dataOut, ds['variable'])[j])
855 else:
869 else:
856 line += '{:>16}'.format('%8.2f' % getattr(self.dataOut, ds['variable']))
870 line += '{:>16}'.format('%8.2f' % getattr(self.dataOut, ds['variable']))
857
871
858 line += '\n'
872 line += '\n'
859 self.fp.write(line)
873 self.fp.write(line)
860
874
861 self.blockIndex += 1
875 self.blockIndex += 1
862 log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name)
876 log.log('Block No. {}/{}'.format(self.blockIndex, self.blocksPerFile), self.name)
863
877
864 return
878 return
865
879
866 def closeFile(self):
880 def closeFile(self):
867
881
868 if self.fp:
882 if self.fp:
869 self.fp.close()
883 self.fp.close()
870
884
871 def close(self):
885 def close(self):
872
886
873 self.closeFile()
887 self.closeFile()
@@ -1,519 +1,519
1 import numpy, math, random, time
1 import numpy, math, random, time
2 #---------------1 Heredamos JRODatareader
2 #---------------1 Heredamos JRODatareader
3 from schainpy.model.io.jroIO_base import *
3 from schainpy.model.io.jroIO_base import *
4 #---------------2 Heredamos las propiedades de ProcessingUnit
4 #---------------2 Heredamos las propiedades de ProcessingUnit
5 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
5 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
6 #---------------3 Importaremos las clases BascicHeader, SystemHeader, RadarControlHeader, ProcessingHeader
6 #---------------3 Importaremos las clases BascicHeader, SystemHeader, RadarControlHeader, ProcessingHeader
7 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
7 from schainpy.model.data.jroheaderIO import PROCFLAG, BasicHeader, SystemHeader, RadarControllerHeader, ProcessingHeader
8 #---------------4 Importaremos el objeto Voltge
8 #---------------4 Importaremos el objeto Voltge
9 from schainpy.model.data.jrodata import Voltage
9 from schainpy.model.data.jrodata import Voltage
10
10
11 class SimulatorReader(JRODataReader, ProcessingUnit):
11 class SimulatorReader(JRODataReader, ProcessingUnit):
12 incIntFactor = 1
12 incIntFactor = 1
13 nFFTPoints = 0
13 nFFTPoints = 0
14 FixPP_IncInt = 1
14 FixPP_IncInt = 1
15 FixRCP_IPP = 1000
15 FixRCP_IPP = 1000
16 FixPP_CohInt = 1
16 FixPP_CohInt = 1
17 Tau_0 = 250
17 Tau_0 = 250
18 AcqH0_0 = 70
18 AcqH0_0 = 70
19 H0 = AcqH0_0
19 H0 = AcqH0_0
20 AcqDH_0 = 1.25
20 AcqDH_0 = 1.25
21 DH0 = AcqDH_0
21 DH0 = AcqDH_0
22 Bauds = 32
22 Bauds = 32
23 BaudWidth = None
23 BaudWidth = None
24 FixRCP_TXA = 40
24 FixRCP_TXA = 40
25 FixRCP_TXB = 70
25 FixRCP_TXB = 70
26 fAngle = 2.0 * math.pi * (1 / 16)
26 fAngle = 2.0 * math.pi * (1 / 16)
27 DC_level = 500
27 DC_level = 500
28 stdev = 8
28 stdev = 8
29 Num_Codes = 2
29 Num_Codes = 2
30 # code0 = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1])
30 # code0 = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1])
31 # code1 = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0])
31 # code1 = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0])
32 # Dyn_snCode = numpy.array([Num_Codes,Bauds])
32 # Dyn_snCode = numpy.array([Num_Codes,Bauds])
33 Dyn_snCode = None
33 Dyn_snCode = None
34 Samples = 200
34 Samples = 200
35 channels = 2
35 channels = 2
36 pulses = None
36 pulses = None
37 Reference = None
37 Reference = None
38 pulse_size = None
38 pulse_size = None
39 prof_gen = None
39 prof_gen = None
40 Fdoppler = 100
40 Fdoppler = 100
41 Hdoppler = 36
41 Hdoppler = 36
42 Adoppler = 300
42 Adoppler = 300
43 frequency = 9345
43 frequency = 9345
44 nTotalReadFiles = 1000
44 nTotalReadFiles = 1000
45
45
46 def __init__(self):
46 def __init__(self):
47 """
47 """
48 Inicializador de la clases SimulatorReader para
48 Inicializador de la clases SimulatorReader para
49 generar datos de voltage simulados.
49 generar datos de voltage simulados.
50 Input:
50 Input:
51 dataOut: Objeto de la clase Voltage.
51 dataOut: Objeto de la clase Voltage.
52 Este Objeto sera utilizado apra almacenar
52 Este Objeto sera utilizado apra almacenar
53 un perfil de datos cada vez qe se haga
53 un perfil de datos cada vez qe se haga
54 un requerimiento (getData)
54 un requerimiento (getData)
55 """
55 """
56 ProcessingUnit.__init__(self)
56 ProcessingUnit.__init__(self)
57 print(" [ START ] init - Metodo Simulator Reader")
57 print(" [ START ] init - Metodo Simulator Reader")
58
58
59 self.isConfig = False
59 self.isConfig = False
60 self.basicHeaderObj = BasicHeader(LOCALTIME)
60 self.basicHeaderObj = BasicHeader(LOCALTIME)
61 self.systemHeaderObj = SystemHeader()
61 self.systemHeaderObj = SystemHeader()
62 self.radarControllerHeaderObj = RadarControllerHeader()
62 self.radarControllerHeaderObj = RadarControllerHeader()
63 self.processingHeaderObj = ProcessingHeader()
63 self.processingHeaderObj = ProcessingHeader()
64 self.profileIndex = 2 ** 32 - 1
64 self.profileIndex = 2 ** 32 - 1
65 self.dataOut = Voltage()
65 self.dataOut = Voltage()
66 # code0 = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1])
66 # code0 = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,0,1,1,1,0,1])
67 code0 = numpy.array([1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1])
67 code0 = numpy.array([1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1])
68 # code1 = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0])
68 # code1 = numpy.array([1,1,1,0,1,1,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,0,1,0])
69 code1 = numpy.array([1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1])
69 code1 = numpy.array([1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1])
70 # self.Dyn_snCode = numpy.array([code0,code1])
70 # self.Dyn_snCode = numpy.array([code0,code1])
71 self.Dyn_snCode = None
71 self.Dyn_snCode = None
72
72
73 def set_kwargs(self, **kwargs):
73 def set_kwargs(self, **kwargs):
74 for key, value in kwargs.items():
74 for key, value in kwargs.items():
75 setattr(self, key, value)
75 setattr(self, key, value)
76
76
77 def __hasNotDataInBuffer(self):
77 def __hasNotDataInBuffer(self):
78
78
79 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock * self.nTxs:
79 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock * self.nTxs:
80 if self.nReadBlocks > 0:
80 if self.nReadBlocks > 0:
81 tmp = self.dataOut.utctime
81 tmp = self.dataOut.utctime
82 tmp_utc = int(self.dataOut.utctime)
82 tmp_utc = int(self.dataOut.utctime)
83 tmp_milisecond = int((tmp - tmp_utc) * 1000)
83 tmp_milisecond = int((tmp - tmp_utc) * 1000)
84 self.basicHeaderObj.utc = tmp_utc
84 self.basicHeaderObj.utc = tmp_utc
85 self.basicHeaderObj.miliSecond = tmp_milisecond
85 self.basicHeaderObj.miliSecond = tmp_milisecond
86 return 1
86 return 1
87 return 0
87 return 0
88
88
89 def setNextFile(self):
89 def setNextFile(self):
90 """Set the next file to be readed open it and parse de file header"""
90 """Set the next file to be readed open it and parse de file header"""
91
91
92 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
92 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
93 self.nReadFiles = self.nReadFiles + 1
93 self.nReadFiles = self.nReadFiles + 1
94 if self.nReadFiles > self.nTotalReadFiles:
94 if self.nReadFiles > self.nTotalReadFiles:
95 self.flagNoMoreFiles = 1
95 self.flagNoMoreFiles = 1
96 raise schainpy.admin.SchainWarning('No more files to read')
96 raise schainpy.admin.SchainWarning('No more files to read')
97
97
98 print('------------------- [Opening file] ------------------------------', self.nReadFiles)
98 print('------------------- [Opening file] ------------------------------', self.nReadFiles)
99 self.nReadBlocks = 0
99 self.nReadBlocks = 0
100 # if self.nReadBlocks==0:
100 # if self.nReadBlocks==0:
101 # self.readFirstHeader()
101 # self.readFirstHeader()
102
102
103 def __setNewBlock(self):
103 def __setNewBlock(self):
104 self.setNextFile()
104 self.setNextFile()
105 if self.flagIsNewFile:
105 if self.flagIsNewFile:
106 return 1
106 return 1
107
107
108 def readNextBlock(self):
108 def readNextBlock(self):
109 while True:
109 while True:
110 self.__setNewBlock()
110 self.__setNewBlock()
111 if not(self.readBlock()):
111 if not(self.readBlock()):
112 return 0
112 return 0
113 self.getBasicHeader()
113 self.getBasicHeader()
114 break
114 break
115 if self.verbose:
115 if self.verbose:
116 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
116 print("[Reading] Block No. %d/%d -> %s" % (self.nReadBlocks,
117 self.processingHeaderObj.dataBlocksPerFile,
117 self.processingHeaderObj.dataBlocksPerFile,
118 self.dataOut.datatime.ctime()))
118 self.dataOut.datatime.ctime()))
119 return 1
119 return 1
120
120
121 def getFirstHeader(self):
121 def getFirstHeader(self):
122 self.getBasicHeader()
122 self.getBasicHeader()
123 self.dataOut.processingHeaderObj = self.processingHeaderObj.copy()
123 self.dataOut.processingHeaderObj = self.processingHeaderObj.copy()
124 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
124 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
125 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
125 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
126 self.dataOut.dtype = self.dtype
126 self.dataOut.dtype = self.dtype
127
127
128 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
128 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
129 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights) * self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
129 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.nHeights) * self.processingHeaderObj.deltaHeight + self.processingHeaderObj.firstHeight
130 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
130 self.dataOut.channelList = list(range(self.systemHeaderObj.nChannels))
131 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
131 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
132 # asumo q la data no esta decodificada
132 # asumo q la data no esta decodificada
133 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode
133 self.dataOut.flagDecodeData = self.processingHeaderObj.flag_decode
134 # asumo q la data no esta sin flip
134 # asumo q la data no esta sin flip
135 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip
135 self.dataOut.flagDeflipData = self.processingHeaderObj.flag_deflip
136 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
136 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
137 self.dataOut.frequency = self.frequency
137 self.dataOut.frequency = self.frequency
138
138
139 def getBasicHeader(self):
139 def getBasicHeader(self):
140 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
140 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond / \
141 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
141 1000. + self.profileIndex * self.radarControllerHeaderObj.ippSeconds
142
142
143 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
143 self.dataOut.flagDiscontinuousBlock = self.flagDiscontinuousBlock
144 self.dataOut.timeZone = self.basicHeaderObj.timeZone
144 self.dataOut.timeZone = self.basicHeaderObj.timeZone
145 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
145 self.dataOut.dstFlag = self.basicHeaderObj.dstFlag
146 self.dataOut.errorCount = self.basicHeaderObj.errorCount
146 self.dataOut.errorCount = self.basicHeaderObj.errorCount
147 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
147 self.dataOut.useLocalTime = self.basicHeaderObj.useLocalTime
148 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
148 self.dataOut.ippSeconds = self.radarControllerHeaderObj.ippSeconds / self.nTxs
149
149
150 def readFirstHeader(self):
150 def readFirstHeader(self):
151
151
152 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
152 datatype = int(numpy.log2((self.processingHeaderObj.processFlags &
153 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
153 PROCFLAG.DATATYPE_MASK)) - numpy.log2(PROCFLAG.DATATYPE_CHAR))
154 if datatype == 0:
154 if datatype == 0:
155 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
155 datatype_str = numpy.dtype([('real', '<i1'), ('imag', '<i1')])
156 elif datatype == 1:
156 elif datatype == 1:
157 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
157 datatype_str = numpy.dtype([('real', '<i2'), ('imag', '<i2')])
158 elif datatype == 2:
158 elif datatype == 2:
159 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
159 datatype_str = numpy.dtype([('real', '<i4'), ('imag', '<i4')])
160 elif datatype == 3:
160 elif datatype == 3:
161 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
161 datatype_str = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
162 elif datatype == 4:
162 elif datatype == 4:
163 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
163 datatype_str = numpy.dtype([('real', '<f4'), ('imag', '<f4')])
164 elif datatype == 5:
164 elif datatype == 5:
165 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
165 datatype_str = numpy.dtype([('real', '<f8'), ('imag', '<f8')])
166 else:
166 else:
167 raise ValueError('Data type was not defined')
167 raise ValueError('Data type was not defined')
168
168
169 self.dtype = datatype_str
169 self.dtype = datatype_str
170
170
171
171
172 def set_RCH(self, expType=2, nTx=1, ipp=None, txA=0, txB=0,
172 def set_RCH(self, expType=2, nTx=1, ipp=None, txA=0, txB=0,
173 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
173 nWindows=None, nHeights=None, firstHeight=None, deltaHeight=None,
174 numTaus=0, line6Function=0, line5Function=0, fClock=None,
174 numTaus=0, line6Function=0, line5Function=0, fClock=None,
175 prePulseBefore=0, prePulseAfter=0,
175 prePulseBefore=0, prePulseAfter=0,
176 codeType=0, nCode=0, nBaud=0, code=None,
176 codeType=0, nCode=0, nBaud=0, code=None,
177 flip1=0, flip2=0, Taus=0):
177 flip1=0, flip2=0, Taus=0):
178 self.radarControllerHeaderObj.expType = expType
178 self.radarControllerHeaderObj.expType = expType
179 self.radarControllerHeaderObj.nTx = nTx
179 self.radarControllerHeaderObj.nTx = nTx
180 self.radarControllerHeaderObj.ipp = float(ipp)
180 self.radarControllerHeaderObj.ipp = float(ipp)
181 self.radarControllerHeaderObj.txA = float(txA)
181 self.radarControllerHeaderObj.txA = float(txA)
182 self.radarControllerHeaderObj.txB = float(txB)
182 self.radarControllerHeaderObj.txB = float(txB)
183 self.radarControllerHeaderObj.rangeIpp = b'A\n' # ipp
183 self.radarControllerHeaderObj.rangeIpp = b'A\n' # ipp
184 self.radarControllerHeaderObj.rangeTxA = b''
184 self.radarControllerHeaderObj.rangeTxA = b''
185 self.radarControllerHeaderObj.rangeTxB = b''
185 self.radarControllerHeaderObj.rangeTxB = b''
186
186
187 self.radarControllerHeaderObj.nHeights = int(nHeights)
187 self.radarControllerHeaderObj.nHeights = int(nHeights)
188 self.radarControllerHeaderObj.firstHeight = numpy.array([firstHeight])
188 self.radarControllerHeaderObj.firstHeight = numpy.array([firstHeight])
189 self.radarControllerHeaderObj.deltaHeight = numpy.array([deltaHeight])
189 self.radarControllerHeaderObj.deltaHeight = numpy.array([deltaHeight])
190 self.radarControllerHeaderObj.samplesWin = numpy.array([nHeights])
190 self.radarControllerHeaderObj.samplesWin = numpy.array([nHeights])
191
191
192
192
193 self.radarControllerHeaderObj.nWindows = nWindows
193 self.radarControllerHeaderObj.nWindows = nWindows
194 self.radarControllerHeaderObj.numTaus = numTaus
194 self.radarControllerHeaderObj.numTaus = numTaus
195 self.radarControllerHeaderObj.codeType = codeType
195 self.radarControllerHeaderObj.codeType = codeType
196 self.radarControllerHeaderObj.line6Function = line6Function
196 self.radarControllerHeaderObj.line6Function = line6Function
197 self.radarControllerHeaderObj.line5Function = line5Function
197 self.radarControllerHeaderObj.line5Function = line5Function
198 # self.radarControllerHeaderObj.fClock = fClock
198 # self.radarControllerHeaderObj.fClock = fClock
199 self.radarControllerHeaderObj.prePulseBefore = prePulseBefore
199 self.radarControllerHeaderObj.prePulseBefore = prePulseBefore
200 self.radarControllerHeaderObj.prePulseAfter = prePulseAfter
200 self.radarControllerHeaderObj.prePulseAfter = prePulseAfter
201
201
202 self.radarControllerHeaderObj.flip1 = flip1
202 self.radarControllerHeaderObj.flip1 = flip1
203 self.radarControllerHeaderObj.flip2 = flip2
203 self.radarControllerHeaderObj.flip2 = flip2
204
204
205 self.radarControllerHeaderObj.code_size = 0
205 self.radarControllerHeaderObj.code_size = 0
206 if self.radarControllerHeaderObj.codeType != 0:
206 if self.radarControllerHeaderObj.codeType != 0:
207 self.radarControllerHeaderObj.nCode = nCode
207 self.radarControllerHeaderObj.nCode = nCode
208 self.radarControllerHeaderObj.nBaud = nBaud
208 self.radarControllerHeaderObj.nBaud = nBaud
209 self.radarControllerHeaderObj.code = code
209 self.radarControllerHeaderObj.code = code
210 self.radarControllerHeaderObj.code_size = int(numpy.ceil(nBaud / 32.)) * nCode * 4
210 self.radarControllerHeaderObj.code_size = int(numpy.ceil(nBaud / 32.)) * nCode * 4
211
211
212 if fClock is None and deltaHeight is not None:
212 if fClock is None and deltaHeight is not None:
213 self.fClock = 0.15 / (deltaHeight * 1e-6)
213 self.fClock = 0.15 / (deltaHeight * 1e-6)
214 self.radarControllerHeaderObj.fClock = self.fClock
214 self.radarControllerHeaderObj.fClock = self.fClock
215 if numTaus == 0:
215 if numTaus == 0:
216 self.radarControllerHeaderObj.Taus = numpy.array(0, '<f4')
216 self.radarControllerHeaderObj.Taus = numpy.array(0, '<f4')
217 else:
217 else:
218 self.radarControllerHeaderObj.Taus = numpy.array(Taus, '<f4')
218 self.radarControllerHeaderObj.Taus = numpy.array(Taus, '<f4')
219
219
220 def set_PH(self, dtype=0, blockSize=0, profilesPerBlock=0,
220 def set_PH(self, dtype=0, blockSize=0, profilesPerBlock=0,
221 dataBlocksPerFile=0, nWindows=0, processFlags=0, nCohInt=0,
221 dataBlocksPerFile=0, nWindows=0, processFlags=0, nCohInt=0,
222 nIncohInt=0, totalSpectra=0, nHeights=0, firstHeight=0,
222 nIncohInt=0, totalSpectra=0, nHeights=0, firstHeight=0,
223 deltaHeight=0, samplesWin=0, spectraComb=0, nCode=0,
223 deltaHeight=0, samplesWin=0, spectraComb=0, nCode=0,
224 code=0, nBaud=None, shif_fft=False, flag_dc=False,
224 code=0, nBaud=None, shif_fft=False, flag_dc=False,
225 flag_cspc=False, flag_decode=False, flag_deflip=False):
225 flag_cspc=False, flag_decode=False, flag_deflip=False):
226
226
227 self.processingHeaderObj.dtype = dtype
227 self.processingHeaderObj.dtype = dtype
228 self.processingHeaderObj.profilesPerBlock = profilesPerBlock
228 self.processingHeaderObj.profilesPerBlock = profilesPerBlock
229 self.processingHeaderObj.dataBlocksPerFile = dataBlocksPerFile
229 self.processingHeaderObj.dataBlocksPerFile = dataBlocksPerFile
230 self.processingHeaderObj.nWindows = nWindows
230 self.processingHeaderObj.nWindows = nWindows
231 self.processingHeaderObj.processFlags = processFlags
231 self.processingHeaderObj.processFlags = processFlags
232 self.processingHeaderObj.nCohInt = nCohInt
232 self.processingHeaderObj.nCohInt = nCohInt
233 self.processingHeaderObj.nIncohInt = nIncohInt
233 self.processingHeaderObj.nIncohInt = nIncohInt
234 self.processingHeaderObj.totalSpectra = totalSpectra
234 self.processingHeaderObj.totalSpectra = totalSpectra
235
235
236 self.processingHeaderObj.nHeights = int(nHeights)
236 self.processingHeaderObj.nHeights = int(nHeights)
237 self.processingHeaderObj.firstHeight = firstHeight # numpy.array([firstHeight])#firstHeight
237 self.processingHeaderObj.firstHeight = firstHeight # numpy.array([firstHeight])#firstHeight
238 self.processingHeaderObj.deltaHeight = deltaHeight # numpy.array([deltaHeight])#deltaHeight
238 self.processingHeaderObj.deltaHeight = deltaHeight # numpy.array([deltaHeight])#deltaHeight
239 self.processingHeaderObj.samplesWin = nHeights # numpy.array([nHeights])#nHeights
239 self.processingHeaderObj.samplesWin = nHeights # numpy.array([nHeights])#nHeights
240
240
241 def set_BH(self, utc=0, miliSecond=0, timeZone=0):
241 def set_BH(self, utc=0, miliSecond=0, timeZone=0):
242 self.basicHeaderObj.utc = utc
242 self.basicHeaderObj.utc = utc
243 self.basicHeaderObj.miliSecond = miliSecond
243 self.basicHeaderObj.miliSecond = miliSecond
244 self.basicHeaderObj.timeZone = timeZone
244 self.basicHeaderObj.timeZone = timeZone
245
245
246 def set_SH(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWidth=32):
246 def set_SH(self, nSamples=0, nProfiles=0, nChannels=0, adcResolution=14, pciDioBusWidth=32):
247 # self.systemHeaderObj.size = size
247 # self.systemHeaderObj.size = size
248 self.systemHeaderObj.nSamples = nSamples
248 self.systemHeaderObj.nSamples = nSamples
249 self.systemHeaderObj.nProfiles = nProfiles
249 self.systemHeaderObj.nProfiles = nProfiles
250 self.systemHeaderObj.nChannels = nChannels
250 self.systemHeaderObj.nChannels = nChannels
251 self.systemHeaderObj.adcResolution = adcResolution
251 self.systemHeaderObj.adcResolution = adcResolution
252 self.systemHeaderObj.pciDioBusWidth = pciDioBusWidth
252 self.systemHeaderObj.pciDioBusWidth = pciDioBusWidth
253
253
254 def init_acquisition(self):
254 def init_acquisition(self):
255
255
256 if self.nFFTPoints != 0:
256 if self.nFFTPoints != 0:
257 self.incIntFactor = m_nProfilesperBlock / self.nFFTPoints
257 self.incIntFactor = m_nProfilesperBlock / self.nFFTPoints
258 if (self.FixPP_IncInt > self.incIntFactor):
258 if (self.FixPP_IncInt > self.incIntFactor):
259 self.incIntFactor = self.FixPP_IncInt / self.incIntFactor
259 self.incIntFactor = self.FixPP_IncInt / self.incIntFactor
260 elif(self.FixPP_IncInt < self.incIntFactor):
260 elif(self.FixPP_IncInt < self.incIntFactor):
261 print("False alert...")
261 print("False alert...")
262
262
263 ProfilesperBlock = self.processingHeaderObj.profilesPerBlock
263 ProfilesperBlock = self.processingHeaderObj.profilesPerBlock
264
264
265 self.timeperblock = int(((self.FixRCP_IPP
265 self.timeperblock = int(((self.FixRCP_IPP
266 * ProfilesperBlock
266 * ProfilesperBlock
267 * self.FixPP_CohInt
267 * self.FixPP_CohInt
268 * self.incIntFactor)
268 * self.incIntFactor)
269 / 150.0)
269 / 150.0)
270 * 0.9
270 * 0.9
271 + 0.5)
271 + 0.5)
272 # para cada canal
272 # para cada canal
273 self.profiles = ProfilesperBlock * self.FixPP_CohInt
273 self.profiles = ProfilesperBlock * self.FixPP_CohInt
274 self.profiles = ProfilesperBlock
274 self.profiles = ProfilesperBlock
275 self.Reference = int((self.Tau_0 - self.AcqH0_0) / (self.AcqDH_0) + 0.5)
275 self.Reference = int((self.Tau_0 - self.AcqH0_0) / (self.AcqDH_0) + 0.5)
276 self.BaudWidth = int((self.FixRCP_TXA / self.AcqDH_0) / self.Bauds + 0.5)
276 self.BaudWidth = int((self.FixRCP_TXA / self.AcqDH_0) / self.Bauds + 0.5)
277
277
278 if (self.BaudWidth == 0):
278 if (self.BaudWidth == 0):
279 self.BaudWidth = 1
279 self.BaudWidth = 1
280
280
281 def init_pulse(self, Num_Codes=Num_Codes, Bauds=Bauds, BaudWidth=BaudWidth, Dyn_snCode=Dyn_snCode):
281 def init_pulse(self, Num_Codes=Num_Codes, Bauds=Bauds, BaudWidth=BaudWidth, Dyn_snCode=Dyn_snCode):
282
282
283 Num_Codes = Num_Codes
283 Num_Codes = Num_Codes
284 Bauds = Bauds
284 Bauds = Bauds
285 BaudWidth = BaudWidth
285 BaudWidth = BaudWidth
286 Dyn_snCode = Dyn_snCode
286 Dyn_snCode = Dyn_snCode
287
287
288 if Dyn_snCode:
288 if Dyn_snCode:
289 print("EXISTE")
289 print("EXISTE")
290 else:
290 else:
291 print("No existe")
291 print("No existe")
292
292
293 if Dyn_snCode: # if Bauds:
293 if Dyn_snCode: # if Bauds:
294 pulses = list(range(0, Num_Codes))
294 pulses = list(range(0, Num_Codes))
295 num_codes = Num_Codes
295 num_codes = Num_Codes
296 for i in range(num_codes):
296 for i in range(num_codes):
297 pulse_size = Bauds * BaudWidth
297 pulse_size = Bauds * BaudWidth
298 pulses[i] = numpy.zeros(pulse_size)
298 pulses[i] = numpy.zeros(pulse_size)
299 for j in range(Bauds):
299 for j in range(Bauds):
300 for k in range(BaudWidth):
300 for k in range(BaudWidth):
301 pulses[i][j * BaudWidth + k] = int(Dyn_snCode[i][j] * 600)
301 pulses[i][j * BaudWidth + k] = int(Dyn_snCode[i][j] * 600)
302 else:
302 else:
303 print("sin code")
303 print("sin code")
304 pulses = list(range(1))
304 pulses = list(range(1))
305 if self.AcqDH_0 > 0.149:
305 if self.AcqDH_0 > 0.149:
306 pulse_size = int(self.FixRCP_TXB / 0.15 + 0.5)
306 pulse_size = int(self.FixRCP_TXB / 0.15 + 0.5)
307 else:
307 else:
308 pulse_size = int((self.FixRCP_TXB / self.AcqDH_0) + 0.5) # 0.0375
308 pulse_size = int((self.FixRCP_TXB / self.AcqDH_0) + 0.5) # 0.0375
309 pulses[0] = numpy.ones(pulse_size)
309 pulses[0] = numpy.ones(pulse_size)
310 pulses = 600 * pulses[0]
310 pulses = 600 * pulses[0]
311
311
312 return pulses, pulse_size
312 return pulses, pulse_size
313
313
314 def jro_GenerateBlockOfData(self, Samples=Samples, DC_level=DC_level, stdev=stdev,
314 def jro_GenerateBlockOfData(self, Samples=Samples, DC_level=DC_level, stdev=stdev,
315 Reference=Reference, pulses=pulses,
315 Reference=Reference, pulses=pulses,
316 Num_Codes=Num_Codes, pulse_size=pulse_size,
316 Num_Codes=Num_Codes, pulse_size=pulse_size,
317 prof_gen=prof_gen, H0=H0, DH0=DH0,
317 prof_gen=prof_gen, H0=H0, DH0=DH0,
318 Adoppler=Adoppler, Fdoppler=Fdoppler, Hdoppler=Hdoppler):
318 Adoppler=Adoppler, Fdoppler=Fdoppler, Hdoppler=Hdoppler):
319 Samples = Samples
319 Samples = Samples
320 DC_level = DC_level
320 DC_level = DC_level
321 stdev = stdev
321 stdev = stdev
322 m_nR = Reference
322 m_nR = Reference
323 pulses = pulses
323 pulses = pulses
324 num_codes = Num_Codes
324 num_codes = Num_Codes
325 ps = pulse_size
325 ps = pulse_size
326 prof_gen = prof_gen
326 prof_gen = prof_gen
327 channels = self.channels
327 channels = self.channels
328 H0 = H0
328 H0 = H0
329 DH0 = DH0
329 DH0 = DH0
330 ippSec = self.radarControllerHeaderObj.ippSeconds
330 ippSec = self.radarControllerHeaderObj.ippSeconds
331 Fdoppler = self.Fdoppler
331 Fdoppler = self.Fdoppler
332 Hdoppler = self.Hdoppler
332 Hdoppler = self.Hdoppler
333 Adoppler = self.Adoppler
333 Adoppler = self.Adoppler
334
334
335 self.datablock = numpy.zeros([channels, prof_gen, Samples], dtype=numpy.complex64)
335 self.datablock = numpy.zeros([channels, prof_gen, Samples], dtype=complex)
336 for i in range(channels):
336 for i in range(channels):
337 for k in range(prof_gen):
337 for k in range(prof_gen):
338 #-----------------------NOISE---------------
338 #-----------------------NOISE---------------
339 Noise_r = numpy.random.normal(DC_level, stdev, Samples)
339 Noise_r = numpy.random.normal(DC_level, stdev, Samples)
340 Noise_i = numpy.random.normal(DC_level, stdev, Samples)
340 Noise_i = numpy.random.normal(DC_level, stdev, Samples)
341 Noise = numpy.zeros(Samples, dtype=complex)
341 Noise = numpy.zeros(Samples, dtype=complex)
342 Noise.real = Noise_r
342 Noise.real = Noise_r
343 Noise.imag = Noise_i
343 Noise.imag = Noise_i
344 #-----------------------PULSOS--------------
344 #-----------------------PULSOS--------------
345 Pulso = numpy.zeros(pulse_size, dtype=complex)
345 Pulso = numpy.zeros(pulse_size, dtype=complex)
346 Pulso.real = pulses[k % num_codes]
346 Pulso.real = pulses[k % num_codes]
347 Pulso.imag = pulses[k % num_codes]
347 Pulso.imag = pulses[k % num_codes]
348 #--------------------- PULSES+NOISE----------
348 #--------------------- PULSES+NOISE----------
349 InBuffer = numpy.zeros(Samples, dtype=complex)
349 InBuffer = numpy.zeros(Samples, dtype=complex)
350 InBuffer[m_nR:m_nR + ps] = Pulso
350 InBuffer[m_nR:m_nR + ps] = Pulso
351 InBuffer = InBuffer + Noise
351 InBuffer = InBuffer + Noise
352 #--------------------- ANGLE -------------------------------
352 #--------------------- ANGLE -------------------------------
353 InBuffer.real[m_nR:m_nR + ps] = InBuffer.real[m_nR:m_nR + ps] * (math.cos(self.fAngle) * 5)
353 InBuffer.real[m_nR:m_nR + ps] = InBuffer.real[m_nR:m_nR + ps] * (math.cos(self.fAngle) * 5)
354 InBuffer.imag[m_nR:m_nR + ps] = InBuffer.imag[m_nR:m_nR + ps] * (math.sin(self.fAngle) * 5)
354 InBuffer.imag[m_nR:m_nR + ps] = InBuffer.imag[m_nR:m_nR + ps] * (math.sin(self.fAngle) * 5)
355 InBuffer = InBuffer
355 InBuffer = InBuffer
356 self.datablock[i][k] = InBuffer
356 self.datablock[i][k] = InBuffer
357
357
358 #----------------DOPPLER SIGNAL...............................................
358 #----------------DOPPLER SIGNAL...............................................
359 time_vec = numpy.linspace(0, (prof_gen - 1) * ippSec, int(prof_gen)) + self.nReadBlocks * ippSec * prof_gen + (self.nReadFiles - 1) * ippSec * prof_gen
359 time_vec = numpy.linspace(0, (prof_gen - 1) * ippSec, int(prof_gen)) + self.nReadBlocks * ippSec * prof_gen + (self.nReadFiles - 1) * ippSec * prof_gen
360 fd = Fdoppler # +(600.0/120)*self.nReadBlocks
360 fd = Fdoppler # +(600.0/120)*self.nReadBlocks
361 d_signal = Adoppler * numpy.array(numpy.exp(1.0j * 2.0 * math.pi * fd * time_vec), dtype=numpy.complex64)
361 d_signal = Adoppler * numpy.array(numpy.exp(1.0j * 2.0 * math.pi * fd * time_vec), dtype=complex)
362 #-------------Senal con ancho espectral--------------------
362 #-------------Senal con ancho espectral--------------------
363 if prof_gen % 2 == 0:
363 if prof_gen % 2 == 0:
364 min = int(prof_gen / 2.0 - 1.0)
364 min = int(prof_gen / 2.0 - 1.0)
365 max = int(prof_gen / 2.0)
365 max = int(prof_gen / 2.0)
366 else:
366 else:
367 min = int(prof_gen / 2.0)
367 min = int(prof_gen / 2.0)
368 max = int(prof_gen / 2.0)
368 max = int(prof_gen / 2.0)
369 specw_sig = numpy.linspace(-min, max, prof_gen)
369 specw_sig = numpy.linspace(-min, max, prof_gen)
370 w = 4
370 w = 4
371 A = 20
371 A = 20
372 specw_sig = specw_sig / w
372 specw_sig = specw_sig / w
373 specw_sig = numpy.sinc(specw_sig)
373 specw_sig = numpy.sinc(specw_sig)
374 specw_sig = A * numpy.array(specw_sig, dtype=numpy.complex64)
374 specw_sig = A * numpy.array(specw_sig, dtype=complex)
375 #------------------ DATABLOCK + DOPPLER--------------------
375 #------------------ DATABLOCK + DOPPLER--------------------
376 HD = int(Hdoppler / self.AcqDH_0)
376 HD = int(Hdoppler / self.AcqDH_0)
377 for i in range(12):
377 for i in range(12):
378 self.datablock[0, :, HD + i] = self.datablock[0, :, HD + i] + d_signal # RESULT
378 self.datablock[0, :, HD + i] = self.datablock[0, :, HD + i] + d_signal # RESULT
379 #------------------ DATABLOCK + DOPPLER*Sinc(x)--------------------
379 #------------------ DATABLOCK + DOPPLER*Sinc(x)--------------------
380 HD = int(Hdoppler / self.AcqDH_0)
380 HD = int(Hdoppler / self.AcqDH_0)
381 HD = int(HD / 2)
381 HD = int(HD / 2)
382 for i in range(12):
382 for i in range(12):
383 self.datablock[0, :, HD + i] = self.datablock[0, :, HD + i] + specw_sig * d_signal # RESULT
383 self.datablock[0, :, HD + i] = self.datablock[0, :, HD + i] + specw_sig * d_signal # RESULT
384
384
385 def readBlock(self):
385 def readBlock(self):
386
386
387 self.jro_GenerateBlockOfData(Samples=self.samples, DC_level=self.DC_level,
387 self.jro_GenerateBlockOfData(Samples=self.samples, DC_level=self.DC_level,
388 stdev=self.stdev, Reference=self.Reference,
388 stdev=self.stdev, Reference=self.Reference,
389 pulses=self.pulses, Num_Codes=self.Num_Codes,
389 pulses=self.pulses, Num_Codes=self.Num_Codes,
390 pulse_size=self.pulse_size, prof_gen=self.profiles,
390 pulse_size=self.pulse_size, prof_gen=self.profiles,
391 H0=self.H0, DH0=self.DH0)
391 H0=self.H0, DH0=self.DH0)
392
392
393 self.profileIndex = 0
393 self.profileIndex = 0
394 self.flagIsNewFile = 0
394 self.flagIsNewFile = 0
395 self.flagIsNewBlock = 1
395 self.flagIsNewBlock = 1
396 self.nTotalBlocks += 1
396 self.nTotalBlocks += 1
397 self.nReadBlocks += 1
397 self.nReadBlocks += 1
398
398
399 return 1
399 return 1
400
400
401
401
402 def getData(self):
402 def getData(self):
403 if self.flagNoMoreFiles:
403 if self.flagNoMoreFiles:
404 self.dataOut.flagNodata = True
404 self.dataOut.flagNodata = True
405 return 0
405 return 0
406 self.flagDiscontinuousBlock = 0
406 self.flagDiscontinuousBlock = 0
407 self.flagIsNewBlock = 0
407 self.flagIsNewBlock = 0
408 if self.__hasNotDataInBuffer(): # aqui es verdad
408 if self.__hasNotDataInBuffer(): # aqui es verdad
409 if not(self.readNextBlock()): # return 1 y por eso el if not salta a getBasic Header
409 if not(self.readNextBlock()): # return 1 y por eso el if not salta a getBasic Header
410 return 0
410 return 0
411 self.getFirstHeader() # atributo
411 self.getFirstHeader() # atributo
412
412
413 if not self.getByBlock:
413 if not self.getByBlock:
414 self.dataOut.flagDataAsBlock = False
414 self.dataOut.flagDataAsBlock = False
415 self.dataOut.data = self.datablock[:, self.profileIndex, :]
415 self.dataOut.data = self.datablock[:, self.profileIndex, :]
416 self.dataOut.profileIndex = self.profileIndex
416 self.dataOut.profileIndex = self.profileIndex
417 self.profileIndex += 1
417 self.profileIndex += 1
418 else:
418 else:
419 pass
419 pass
420 self.dataOut.flagNoData = False
420 self.dataOut.flagNoData = False
421 self.getBasicHeader()
421 self.getBasicHeader()
422 self.dataOut.realtime = self.online
422 self.dataOut.realtime = self.online
423 return self.dataOut.data
423 return self.dataOut.data
424
424
425
425
426 def setup(self, frequency=49.92e6, incIntFactor=1, nFFTPoints=0, FixPP_IncInt=1, FixRCP_IPP=1000,
426 def setup(self, frequency=49.92e6, incIntFactor=1, nFFTPoints=0, FixPP_IncInt=1, FixRCP_IPP=1000,
427 FixPP_CohInt=1, Tau_0=250, AcqH0_0=70 , AcqDH_0=1.25, Bauds=32,
427 FixPP_CohInt=1, Tau_0=250, AcqH0_0=70 , AcqDH_0=1.25, Bauds=32,
428 FixRCP_TXA=40, FixRCP_TXB=50, fAngle=2.0 * math.pi * (1 / 16), DC_level=50,
428 FixRCP_TXA=40, FixRCP_TXB=50, fAngle=2.0 * math.pi * (1 / 16), DC_level=50,
429 stdev=8, Num_Codes=1 , Dyn_snCode=None, samples=200,
429 stdev=8, Num_Codes=1 , Dyn_snCode=None, samples=200,
430 channels=2, Fdoppler=20, Hdoppler=36, Adoppler=500,
430 channels=2, Fdoppler=20, Hdoppler=36, Adoppler=500,
431 profilesPerBlock=300, dataBlocksPerFile=120, nTotalReadFiles=10000,
431 profilesPerBlock=300, dataBlocksPerFile=120, nTotalReadFiles=10000,
432 **kwargs):
432 **kwargs):
433
433
434 self.set_kwargs(**kwargs)
434 self.set_kwargs(**kwargs)
435 self.nReadBlocks = 0
435 self.nReadBlocks = 0
436 self.nReadFiles = 1
436 self.nReadFiles = 1
437 print('------------------- [Opening file: ] ------------------------------', self.nReadFiles)
437 print('------------------- [Opening file: ] ------------------------------', self.nReadFiles)
438
438
439 tmp = time.time()
439 tmp = time.time()
440 tmp_utc = int(tmp)
440 tmp_utc = int(tmp)
441 tmp_milisecond = int((tmp - tmp_utc) * 1000)
441 tmp_milisecond = int((tmp - tmp_utc) * 1000)
442 print(" SETUP -basicHeaderObj.utc", datetime.datetime.utcfromtimestamp(tmp))
442 print(" SETUP -basicHeaderObj.utc", datetime.datetime.utcfromtimestamp(tmp))
443 if Dyn_snCode is None:
443 if Dyn_snCode is None:
444 Num_Codes = 1
444 Num_Codes = 1
445 Bauds = 1
445 Bauds = 1
446
446
447
447
448
448
449 self.set_BH(utc=tmp_utc, miliSecond=tmp_milisecond, timeZone=300)
449 self.set_BH(utc=tmp_utc, miliSecond=tmp_milisecond, timeZone=300)
450 self.set_RCH(expType=0, nTx=150, ipp=FixRCP_IPP, txA=FixRCP_TXA, txB=FixRCP_TXB,
450 self.set_RCH(expType=0, nTx=150, ipp=FixRCP_IPP, txA=FixRCP_TXA, txB=FixRCP_TXB,
451 nWindows=1 , nHeights=samples, firstHeight=AcqH0_0, deltaHeight=AcqDH_0,
451 nWindows=1 , nHeights=samples, firstHeight=AcqH0_0, deltaHeight=AcqDH_0,
452 numTaus=1, line6Function=0, line5Function=0, fClock=None,
452 numTaus=1, line6Function=0, line5Function=0, fClock=None,
453 prePulseBefore=0, prePulseAfter=0,
453 prePulseBefore=0, prePulseAfter=0,
454 codeType=0, nCode=Num_Codes, nBaud=32, code=Dyn_snCode,
454 codeType=0, nCode=Num_Codes, nBaud=32, code=Dyn_snCode,
455 flip1=0, flip2=0, Taus=Tau_0)
455 flip1=0, flip2=0, Taus=Tau_0)
456
456
457 self.set_PH(dtype=0, blockSize=0, profilesPerBlock=profilesPerBlock,
457 self.set_PH(dtype=0, blockSize=0, profilesPerBlock=profilesPerBlock,
458 dataBlocksPerFile=dataBlocksPerFile, nWindows=1, processFlags=numpy.array([1024]), nCohInt=1,
458 dataBlocksPerFile=dataBlocksPerFile, nWindows=1, processFlags=numpy.array([1024]), nCohInt=1,
459 nIncohInt=1, totalSpectra=0, nHeights=samples, firstHeight=AcqH0_0,
459 nIncohInt=1, totalSpectra=0, nHeights=samples, firstHeight=AcqH0_0,
460 deltaHeight=AcqDH_0, samplesWin=samples, spectraComb=0, nCode=0,
460 deltaHeight=AcqDH_0, samplesWin=samples, spectraComb=0, nCode=0,
461 code=0, nBaud=None, shif_fft=False, flag_dc=False,
461 code=0, nBaud=None, shif_fft=False, flag_dc=False,
462 flag_cspc=False, flag_decode=False, flag_deflip=False)
462 flag_cspc=False, flag_decode=False, flag_deflip=False)
463
463
464 self.set_SH(nSamples=samples, nProfiles=profilesPerBlock, nChannels=channels)
464 self.set_SH(nSamples=samples, nProfiles=profilesPerBlock, nChannels=channels)
465
465
466 self.readFirstHeader()
466 self.readFirstHeader()
467
467
468 self.frequency = frequency
468 self.frequency = frequency
469 self.incIntFactor = incIntFactor
469 self.incIntFactor = incIntFactor
470 self.nFFTPoints = nFFTPoints
470 self.nFFTPoints = nFFTPoints
471 self.FixPP_IncInt = FixPP_IncInt
471 self.FixPP_IncInt = FixPP_IncInt
472 self.FixRCP_IPP = FixRCP_IPP
472 self.FixRCP_IPP = FixRCP_IPP
473 self.FixPP_CohInt = FixPP_CohInt
473 self.FixPP_CohInt = FixPP_CohInt
474 self.Tau_0 = Tau_0
474 self.Tau_0 = Tau_0
475 self.AcqH0_0 = AcqH0_0
475 self.AcqH0_0 = AcqH0_0
476 self.H0 = AcqH0_0
476 self.H0 = AcqH0_0
477 self.AcqDH_0 = AcqDH_0
477 self.AcqDH_0 = AcqDH_0
478 self.DH0 = AcqDH_0
478 self.DH0 = AcqDH_0
479 self.Bauds = Bauds
479 self.Bauds = Bauds
480 self.FixRCP_TXA = FixRCP_TXA
480 self.FixRCP_TXA = FixRCP_TXA
481 self.FixRCP_TXB = FixRCP_TXB
481 self.FixRCP_TXB = FixRCP_TXB
482 self.fAngle = fAngle
482 self.fAngle = fAngle
483 self.DC_level = DC_level
483 self.DC_level = DC_level
484 self.stdev = stdev
484 self.stdev = stdev
485 self.Num_Codes = Num_Codes
485 self.Num_Codes = Num_Codes
486 self.Dyn_snCode = Dyn_snCode
486 self.Dyn_snCode = Dyn_snCode
487 self.samples = samples
487 self.samples = samples
488 self.channels = channels
488 self.channels = channels
489 self.profiles = None
489 self.profiles = None
490 self.m_nReference = None
490 self.m_nReference = None
491 self.Baudwidth = None
491 self.Baudwidth = None
492 self.Fdoppler = Fdoppler
492 self.Fdoppler = Fdoppler
493 self.Hdoppler = Hdoppler
493 self.Hdoppler = Hdoppler
494 self.Adoppler = Adoppler
494 self.Adoppler = Adoppler
495 self.nTotalReadFiles = int(nTotalReadFiles)
495 self.nTotalReadFiles = int(nTotalReadFiles)
496
496
497 print("IPP ", self.FixRCP_IPP)
497 print("IPP ", self.FixRCP_IPP)
498 print("Tau_0 ", self.Tau_0)
498 print("Tau_0 ", self.Tau_0)
499 print("AcqH0_0", self.AcqH0_0)
499 print("AcqH0_0", self.AcqH0_0)
500 print("samples,window ", self.samples)
500 print("samples,window ", self.samples)
501 print("AcqDH_0", AcqDH_0)
501 print("AcqDH_0", AcqDH_0)
502 print("FixRCP_TXA", self.FixRCP_TXA)
502 print("FixRCP_TXA", self.FixRCP_TXA)
503 print("FixRCP_TXB", self.FixRCP_TXB)
503 print("FixRCP_TXB", self.FixRCP_TXB)
504 print("Dyn_snCode", Dyn_snCode)
504 print("Dyn_snCode", Dyn_snCode)
505 print("Fdoppler", Fdoppler)
505 print("Fdoppler", Fdoppler)
506 print("Hdoppler", Hdoppler)
506 print("Hdoppler", Hdoppler)
507 print("Vdopplermax", Fdoppler * (3.0e8 / self.frequency) / 2.0)
507 print("Vdopplermax", Fdoppler * (3.0e8 / self.frequency) / 2.0)
508 print("nTotalReadFiles", nTotalReadFiles)
508 print("nTotalReadFiles", nTotalReadFiles)
509
509
510 self.init_acquisition()
510 self.init_acquisition()
511 self.pulses, self.pulse_size = self.init_pulse(Num_Codes=self.Num_Codes, Bauds=self.Bauds, BaudWidth=self.BaudWidth, Dyn_snCode=Dyn_snCode)
511 self.pulses, self.pulse_size = self.init_pulse(Num_Codes=self.Num_Codes, Bauds=self.Bauds, BaudWidth=self.BaudWidth, Dyn_snCode=Dyn_snCode)
512 print(" [ END ] - SETUP metodo")
512 print(" [ END ] - SETUP metodo")
513 return
513 return
514
514
515 def run(self, **kwargs): # metodo propio
515 def run(self, **kwargs): # metodo propio
516 if not(self.isConfig):
516 if not(self.isConfig):
517 self.setup(**kwargs)
517 self.setup(**kwargs)
518 self.isConfig = True
518 self.isConfig = True
519 self.getData()
519 self.getData()
@@ -1,602 +1,602
1 '''
1 '''
2 Created on Jul 3, 2014
2 Created on Jul 3, 2014
3
3
4 @author: roj-idl71
4 @author: roj-idl71
5 '''
5 '''
6 import os
6 import os
7 import datetime
7 import datetime
8 import numpy
8 import numpy
9
9
10 try:
10 try:
11 from gevent import sleep
11 from gevent import sleep
12 except:
12 except:
13 from time import sleep
13 from time import sleep
14
14
15 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
15 from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader
16 from schainpy.model.data.jrodata import Voltage
16 from schainpy.model.data.jrodata import Voltage
17 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
17 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator
18
18
19 try:
19 try:
20 import digital_rf_hdf5
20 import digital_rf_hdf5
21 except:
21 except:
22 pass
22 pass
23
23
24 class USRPReader(ProcessingUnit):
24 class USRPReader(ProcessingUnit):
25 '''
25 '''
26 classdocs
26 classdocs
27 '''
27 '''
28
28
29 def __init__(self, **kwargs):
29 def __init__(self, **kwargs):
30 '''
30 '''
31 Constructor
31 Constructor
32 '''
32 '''
33
33
34 ProcessingUnit.__init__(self, **kwargs)
34 ProcessingUnit.__init__(self, **kwargs)
35
35
36 self.dataOut = Voltage()
36 self.dataOut = Voltage()
37 self.__printInfo = True
37 self.__printInfo = True
38 self.__flagDiscontinuousBlock = False
38 self.__flagDiscontinuousBlock = False
39 self.__bufferIndex = 9999999
39 self.__bufferIndex = 9999999
40
40
41 self.__ippKm = None
41 self.__ippKm = None
42 self.__codeType = 0
42 self.__codeType = 0
43 self.__nCode = None
43 self.__nCode = None
44 self.__nBaud = None
44 self.__nBaud = None
45 self.__code = None
45 self.__code = None
46
46
47 def __getCurrentSecond(self):
47 def __getCurrentSecond(self):
48
48
49 return self.__thisUnixSample / self.__sample_rate
49 return self.__thisUnixSample / self.__sample_rate
50
50
51 thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.")
51 thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.")
52
52
53 def __setFileHeader(self):
53 def __setFileHeader(self):
54 '''
54 '''
55 In this method will be initialized every parameter of dataOut object (header, no data)
55 In this method will be initialized every parameter of dataOut object (header, no data)
56 '''
56 '''
57 ippSeconds = 1.0 * self.__nSamples / self.__sample_rate
57 ippSeconds = 1.0 * self.__nSamples / self.__sample_rate
58
58
59 nProfiles = 1.0 / ippSeconds # Number of profiles in one second
59 nProfiles = 1.0 / ippSeconds # Number of profiles in one second
60
60
61 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(ipp=self.__ippKm,
61 self.dataOut.radarControllerHeaderObj = RadarControllerHeader(ipp=self.__ippKm,
62 txA=0,
62 txA=0,
63 txB=0,
63 txB=0,
64 nWindows=1,
64 nWindows=1,
65 nHeights=self.__nSamples,
65 nHeights=self.__nSamples,
66 firstHeight=self.__firstHeigth,
66 firstHeight=self.__firstHeigth,
67 deltaHeight=self.__deltaHeigth,
67 deltaHeight=self.__deltaHeigth,
68 codeType=self.__codeType,
68 codeType=self.__codeType,
69 nCode=self.__nCode, nBaud=self.__nBaud,
69 nCode=self.__nCode, nBaud=self.__nBaud,
70 code=self.__code)
70 code=self.__code)
71
71
72 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
72 self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples,
73 nProfiles=nProfiles,
73 nProfiles=nProfiles,
74 nChannels=len(self.__channelList),
74 nChannels=len(self.__channelList),
75 adcResolution=14)
75 adcResolution=14)
76
76
77 self.dataOut.type = "Voltage"
77 self.dataOut.type = "Voltage"
78
78
79 self.dataOut.data = None
79 self.dataOut.data = None
80
80
81 self.dataOut.dtype = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
81 self.dataOut.dtype = numpy.dtype([('real', '<i8'), ('imag', '<i8')])
82
82
83 # self.dataOut.nChannels = 0
83 # self.dataOut.nChannels = 0
84
84
85 # self.dataOut.nHeights = 0
85 # self.dataOut.nHeights = 0
86
86
87 self.dataOut.nProfiles = nProfiles
87 self.dataOut.nProfiles = nProfiles
88
88
89 self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype=numpy.float) * self.__deltaHeigth
89 self.dataOut.heightList = self.__firstHeigth + numpy.arange(self.__nSamples, dtype=numpy.float32) * self.__deltaHeigth
90
90
91 self.dataOut.channelList = self.__channelList
91 self.dataOut.channelList = self.__channelList
92
92
93 self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights
93 self.dataOut.blocksize = self.dataOut.nChannels * self.dataOut.nHeights
94
94
95 # self.dataOut.channelIndexList = None
95 # self.dataOut.channelIndexList = None
96
96
97 self.dataOut.flagNoData = True
97 self.dataOut.flagNoData = True
98
98
99 # Set to TRUE if the data is discontinuous
99 # Set to TRUE if the data is discontinuous
100 self.dataOut.flagDiscontinuousBlock = False
100 self.dataOut.flagDiscontinuousBlock = False
101
101
102 self.dataOut.utctime = None
102 self.dataOut.utctime = None
103
103
104 self.dataOut.timeZone = self.__timezone / 60 # timezone like jroheader, difference in minutes between UTC and localtime
104 self.dataOut.timeZone = self.__timezone / 60 # timezone like jroheader, difference in minutes between UTC and localtime
105
105
106 self.dataOut.dstFlag = 0
106 self.dataOut.dstFlag = 0
107
107
108 self.dataOut.errorCount = 0
108 self.dataOut.errorCount = 0
109
109
110 self.dataOut.nCohInt = 1
110 self.dataOut.nCohInt = 1
111
111
112 self.dataOut.flagDecodeData = False # asumo que la data esta decodificada
112 self.dataOut.flagDecodeData = False # asumo que la data esta decodificada
113
113
114 self.dataOut.flagDeflipData = False # asumo que la data esta sin flip
114 self.dataOut.flagDeflipData = False # asumo que la data esta sin flip
115
115
116 self.dataOut.flagShiftFFT = False
116 self.dataOut.flagShiftFFT = False
117
117
118 self.dataOut.ippSeconds = ippSeconds
118 self.dataOut.ippSeconds = ippSeconds
119
119
120 # Time interval between profiles
120 # Time interval between profiles
121 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
121 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
122
122
123 self.dataOut.frequency = self.__frequency
123 self.dataOut.frequency = self.__frequency
124
124
125 self.dataOut.realtime = self.__online
125 self.dataOut.realtime = self.__online
126
126
127 def findDatafiles(self, path, startDate=None, endDate=None):
127 def findDatafiles(self, path, startDate=None, endDate=None):
128
128
129 if not os.path.isdir(path):
129 if not os.path.isdir(path):
130 return []
130 return []
131
131
132 try:
132 try:
133 digitalReadObj = digital_rf_hdf5.read_hdf5(path, load_all_metadata=True)
133 digitalReadObj = digital_rf_hdf5.read_hdf5(path, load_all_metadata=True)
134 except:
134 except:
135 digitalReadObj = digital_rf_hdf5.read_hdf5(path)
135 digitalReadObj = digital_rf_hdf5.read_hdf5(path)
136
136
137 channelNameList = digitalReadObj.get_channels()
137 channelNameList = digitalReadObj.get_channels()
138
138
139 if not channelNameList:
139 if not channelNameList:
140 return []
140 return []
141
141
142 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
142 metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0])
143
143
144 sample_rate = metadata_dict['sample_rate'][0]
144 sample_rate = metadata_dict['sample_rate'][0]
145
145
146 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
146 this_metadata_file = digitalReadObj.get_metadata(channelNameList[0])
147
147
148 try:
148 try:
149 timezone = this_metadata_file['timezone'].value
149 timezone = this_metadata_file['timezone'].value
150 except:
150 except:
151 timezone = 0
151 timezone = 0
152
152
153 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(channelNameList[0]) / sample_rate - timezone
153 startUTCSecond, endUTCSecond = digitalReadObj.get_bounds(channelNameList[0]) / sample_rate - timezone
154
154
155 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
155 startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond)
156 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
156 endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond)
157
157
158 if not startDate:
158 if not startDate:
159 startDate = startDatetime.date()
159 startDate = startDatetime.date()
160
160
161 if not endDate:
161 if not endDate:
162 endDate = endDatatime.date()
162 endDate = endDatatime.date()
163
163
164 dateList = []
164 dateList = []
165
165
166 thisDatetime = startDatetime
166 thisDatetime = startDatetime
167
167
168 while(thisDatetime <= endDatatime):
168 while(thisDatetime <= endDatatime):
169
169
170 thisDate = thisDatetime.date()
170 thisDate = thisDatetime.date()
171
171
172 if thisDate < startDate:
172 if thisDate < startDate:
173 continue
173 continue
174
174
175 if thisDate > endDate:
175 if thisDate > endDate:
176 break
176 break
177
177
178 dateList.append(thisDate)
178 dateList.append(thisDate)
179 thisDatetime += datetime.timedelta(1)
179 thisDatetime += datetime.timedelta(1)
180
180
181 return dateList
181 return dateList
182
182
183 def setup(self, path=None,
183 def setup(self, path=None,
184 startDate=None,
184 startDate=None,
185 endDate=None,
185 endDate=None,
186 startTime=datetime.time(0, 0, 0),
186 startTime=datetime.time(0, 0, 0),
187 endTime=datetime.time(23, 59, 59),
187 endTime=datetime.time(23, 59, 59),
188 channelList=None,
188 channelList=None,
189 nSamples=None,
189 nSamples=None,
190 ippKm=60,
190 ippKm=60,
191 online=False,
191 online=False,
192 delay=60,
192 delay=60,
193 buffer_size=1024,
193 buffer_size=1024,
194 **kwargs):
194 **kwargs):
195 '''
195 '''
196 In this method we should set all initial parameters.
196 In this method we should set all initial parameters.
197
197
198 Inputs:
198 Inputs:
199 path
199 path
200 startDate
200 startDate
201 endDate
201 endDate
202 startTime
202 startTime
203 endTime
203 endTime
204 set
204 set
205 expLabel
205 expLabel
206 ext
206 ext
207 online
207 online
208 delay
208 delay
209 '''
209 '''
210
210
211 if not os.path.isdir(path):
211 if not os.path.isdir(path):
212 raise ValueError("[Reading] Directory %s does not exist" % path)
212 raise ValueError("[Reading] Directory %s does not exist" % path)
213
213
214 try:
214 try:
215 self.digitalReadObj = digital_rf_hdf5.read_hdf5(path, load_all_metadata=True)
215 self.digitalReadObj = digital_rf_hdf5.read_hdf5(path, load_all_metadata=True)
216 except:
216 except:
217 self.digitalReadObj = digital_rf_hdf5.read_hdf5(path)
217 self.digitalReadObj = digital_rf_hdf5.read_hdf5(path)
218
218
219 channelNameList = self.digitalReadObj.get_channels()
219 channelNameList = self.digitalReadObj.get_channels()
220
220
221 if not channelNameList:
221 if not channelNameList:
222 raise ValueError("[Reading] Directory %s does not have any files" % path)
222 raise ValueError("[Reading] Directory %s does not have any files" % path)
223
223
224 if not channelList:
224 if not channelList:
225 channelList = list(range(len(channelNameList)))
225 channelList = list(range(len(channelNameList)))
226
226
227 ########## Reading metadata ######################
227 ########## Reading metadata ######################
228
228
229 metadata_dict = self.digitalReadObj.get_rf_file_metadata(channelNameList[channelList[0]])
229 metadata_dict = self.digitalReadObj.get_rf_file_metadata(channelNameList[channelList[0]])
230
230
231 self.__sample_rate = metadata_dict['sample_rate'][0]
231 self.__sample_rate = metadata_dict['sample_rate'][0]
232 # self.__samples_per_file = metadata_dict['samples_per_file'][0]
232 # self.__samples_per_file = metadata_dict['samples_per_file'][0]
233 self.__deltaHeigth = 1e6 * 0.15 / self.__sample_rate
233 self.__deltaHeigth = 1e6 * 0.15 / self.__sample_rate
234
234
235 this_metadata_file = self.digitalReadObj.get_metadata(channelNameList[channelList[0]])
235 this_metadata_file = self.digitalReadObj.get_metadata(channelNameList[channelList[0]])
236
236
237 self.__frequency = None
237 self.__frequency = None
238 try:
238 try:
239 self.__frequency = this_metadata_file['center_frequencies'].value
239 self.__frequency = this_metadata_file['center_frequencies'].value
240 except:
240 except:
241 self.__frequency = this_metadata_file['fc'].value
241 self.__frequency = this_metadata_file['fc'].value
242
242
243 if not self.__frequency:
243 if not self.__frequency:
244 raise ValueError("Center Frequency is not defined in metadata file")
244 raise ValueError("Center Frequency is not defined in metadata file")
245
245
246 try:
246 try:
247 self.__timezone = this_metadata_file['timezone'].value
247 self.__timezone = this_metadata_file['timezone'].value
248 except:
248 except:
249 self.__timezone = 0
249 self.__timezone = 0
250
250
251 self.__firstHeigth = 0
251 self.__firstHeigth = 0
252
252
253 try:
253 try:
254 codeType = this_metadata_file['codeType'].value
254 codeType = this_metadata_file['codeType'].value
255 except:
255 except:
256 codeType = 0
256 codeType = 0
257
257
258 nCode = 1
258 nCode = 1
259 nBaud = 1
259 nBaud = 1
260 code = numpy.ones((nCode, nBaud), dtype=numpy.int)
260 code = numpy.ones((nCode, nBaud), dtype=numpy.int32)
261
261
262 if codeType:
262 if codeType:
263 nCode = this_metadata_file['nCode'].value
263 nCode = this_metadata_file['nCode'].value
264 nBaud = this_metadata_file['nBaud'].value
264 nBaud = this_metadata_file['nBaud'].value
265 code = this_metadata_file['code'].value
265 code = this_metadata_file['code'].value
266
266
267 if not ippKm:
267 if not ippKm:
268 try:
268 try:
269 # seconds to km
269 # seconds to km
270 ippKm = 1e6 * 0.15 * this_metadata_file['ipp'].value
270 ippKm = 1e6 * 0.15 * this_metadata_file['ipp'].value
271 except:
271 except:
272 ippKm = None
272 ippKm = None
273
273
274 ####################################################
274 ####################################################
275 startUTCSecond = None
275 startUTCSecond = None
276 endUTCSecond = None
276 endUTCSecond = None
277
277
278 if startDate:
278 if startDate:
279 startDatetime = datetime.datetime.combine(startDate, startTime)
279 startDatetime = datetime.datetime.combine(startDate, startTime)
280 startUTCSecond = (startDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone
280 startUTCSecond = (startDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone
281
281
282 if endDate:
282 if endDate:
283 endDatetime = datetime.datetime.combine(endDate, endTime)
283 endDatetime = datetime.datetime.combine(endDate, endTime)
284 endUTCSecond = (endDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone
284 endUTCSecond = (endDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone
285
285
286 start_index, end_index = self.digitalReadObj.get_bounds(channelNameList[channelList[0]])
286 start_index, end_index = self.digitalReadObj.get_bounds(channelNameList[channelList[0]])
287
287
288 if not startUTCSecond:
288 if not startUTCSecond:
289 startUTCSecond = start_index / self.__sample_rate
289 startUTCSecond = start_index / self.__sample_rate
290
290
291 if start_index > startUTCSecond * self.__sample_rate:
291 if start_index > startUTCSecond * self.__sample_rate:
292 startUTCSecond = start_index / self.__sample_rate
292 startUTCSecond = start_index / self.__sample_rate
293
293
294 if not endUTCSecond:
294 if not endUTCSecond:
295 endUTCSecond = end_index / self.__sample_rate
295 endUTCSecond = end_index / self.__sample_rate
296
296
297 if end_index < endUTCSecond * self.__sample_rate:
297 if end_index < endUTCSecond * self.__sample_rate:
298 endUTCSecond = end_index / self.__sample_rate
298 endUTCSecond = end_index / self.__sample_rate
299
299
300 if not nSamples:
300 if not nSamples:
301 if not ippKm:
301 if not ippKm:
302 raise ValueError("[Reading] nSamples or ippKm should be defined")
302 raise ValueError("[Reading] nSamples or ippKm should be defined")
303
303
304 nSamples = int(ippKm / (1e6 * 0.15 / self.__sample_rate))
304 nSamples = int(ippKm / (1e6 * 0.15 / self.__sample_rate))
305
305
306 channelBoundList = []
306 channelBoundList = []
307 channelNameListFiltered = []
307 channelNameListFiltered = []
308
308
309 for thisIndexChannel in channelList:
309 for thisIndexChannel in channelList:
310 thisChannelName = channelNameList[thisIndexChannel]
310 thisChannelName = channelNameList[thisIndexChannel]
311 start_index, end_index = self.digitalReadObj.get_bounds(thisChannelName)
311 start_index, end_index = self.digitalReadObj.get_bounds(thisChannelName)
312 channelBoundList.append((start_index, end_index))
312 channelBoundList.append((start_index, end_index))
313 channelNameListFiltered.append(thisChannelName)
313 channelNameListFiltered.append(thisChannelName)
314
314
315 self.profileIndex = 0
315 self.profileIndex = 0
316
316
317 self.__delay = delay
317 self.__delay = delay
318 self.__ippKm = ippKm
318 self.__ippKm = ippKm
319 self.__codeType = codeType
319 self.__codeType = codeType
320 self.__nCode = nCode
320 self.__nCode = nCode
321 self.__nBaud = nBaud
321 self.__nBaud = nBaud
322 self.__code = code
322 self.__code = code
323
323
324 self.__datapath = path
324 self.__datapath = path
325 self.__online = online
325 self.__online = online
326 self.__channelList = channelList
326 self.__channelList = channelList
327 self.__channelNameList = channelNameListFiltered
327 self.__channelNameList = channelNameListFiltered
328 self.__channelBoundList = channelBoundList
328 self.__channelBoundList = channelBoundList
329 self.__nSamples = nSamples
329 self.__nSamples = nSamples
330 self.__samples_to_read = int(buffer_size * nSamples)
330 self.__samples_to_read = int(buffer_size * nSamples)
331 self.__nChannels = len(self.__channelList)
331 self.__nChannels = len(self.__channelList)
332
332
333 self.__startUTCSecond = startUTCSecond
333 self.__startUTCSecond = startUTCSecond
334 self.__endUTCSecond = endUTCSecond
334 self.__endUTCSecond = endUTCSecond
335
335
336 self.__timeInterval = 1.0 * self.__samples_to_read / self.__sample_rate # Time interval
336 self.__timeInterval = 1.0 * self.__samples_to_read / self.__sample_rate # Time interval
337
337
338 if online:
338 if online:
339 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
339 # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read)
340 startUTCSecond = numpy.floor(endUTCSecond)
340 startUTCSecond = numpy.floor(endUTCSecond)
341
341
342 self.__thisUnixSample = int(startUTCSecond * self.__sample_rate) - self.__samples_to_read
342 self.__thisUnixSample = int(startUTCSecond * self.__sample_rate) - self.__samples_to_read
343
343
344 self.__data_buffer = numpy.zeros((self.__nChannels, self.__samples_to_read), dtype=numpy.complex)
344 self.__data_buffer = numpy.zeros((self.__nChannels, self.__samples_to_read), dtype=complex)
345
345
346 self.__setFileHeader()
346 self.__setFileHeader()
347 self.isConfig = True
347 self.isConfig = True
348
348
349 print("[Reading] USRP Data was found from %s to %s " % (
349 print("[Reading] USRP Data was found from %s to %s " % (
350 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
350 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
351 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
351 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
352 ))
352 ))
353
353
354 print("[Reading] Starting process from %s to %s" % (datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
354 print("[Reading] Starting process from %s to %s" % (datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone),
355 datetime.datetime.utcfromtimestamp(endUTCSecond - self.__timezone)
355 datetime.datetime.utcfromtimestamp(endUTCSecond - self.__timezone)
356 ))
356 ))
357
357
358 def __reload(self):
358 def __reload(self):
359
359
360 if not self.__online:
360 if not self.__online:
361 return
361 return
362
362
363 # print
363 # print
364 # print "%s not in range [%s, %s]" %(
364 # print "%s not in range [%s, %s]" %(
365 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
365 # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
366 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
366 # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
367 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
367 # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
368 # )
368 # )
369 print("[Reading] reloading metadata ...")
369 print("[Reading] reloading metadata ...")
370
370
371 try:
371 try:
372 self.digitalReadObj.reload(complete_update=True)
372 self.digitalReadObj.reload(complete_update=True)
373 except:
373 except:
374 self.digitalReadObj.reload()
374 self.digitalReadObj.reload()
375
375
376 start_index, end_index = self.digitalReadObj.get_bounds(self.__channelNameList[self.__channelList[0]])
376 start_index, end_index = self.digitalReadObj.get_bounds(self.__channelNameList[self.__channelList[0]])
377
377
378 if start_index > self.__startUTCSecond * self.__sample_rate:
378 if start_index > self.__startUTCSecond * self.__sample_rate:
379 self.__startUTCSecond = 1.0 * start_index / self.__sample_rate
379 self.__startUTCSecond = 1.0 * start_index / self.__sample_rate
380
380
381 if end_index > self.__endUTCSecond * self.__sample_rate:
381 if end_index > self.__endUTCSecond * self.__sample_rate:
382 self.__endUTCSecond = 1.0 * end_index / self.__sample_rate
382 self.__endUTCSecond = 1.0 * end_index / self.__sample_rate
383 print()
383 print()
384 print("[Reading] New timerange found [%s, %s] " % (
384 print("[Reading] New timerange found [%s, %s] " % (
385 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
385 datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone),
386 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
386 datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone)
387 ))
387 ))
388
388
389 return True
389 return True
390
390
391 return False
391 return False
392
392
393 def __readNextBlock(self, seconds=30, volt_scale=218776):
393 def __readNextBlock(self, seconds=30, volt_scale=218776):
394 '''
394 '''
395 '''
395 '''
396
396
397 # Set the next data
397 # Set the next data
398 self.__flagDiscontinuousBlock = False
398 self.__flagDiscontinuousBlock = False
399 self.__thisUnixSample += self.__samples_to_read
399 self.__thisUnixSample += self.__samples_to_read
400
400
401 if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate:
401 if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate:
402 print("[Reading] There are no more data into selected time-range")
402 print("[Reading] There are no more data into selected time-range")
403
403
404 self.__reload()
404 self.__reload()
405
405
406 if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate:
406 if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate:
407 self.__thisUnixSample -= self.__samples_to_read
407 self.__thisUnixSample -= self.__samples_to_read
408 return False
408 return False
409
409
410 indexChannel = 0
410 indexChannel = 0
411
411
412 dataOk = False
412 dataOk = False
413
413
414 for thisChannelName in self.__channelNameList:
414 for thisChannelName in self.__channelNameList:
415
415
416 try:
416 try:
417 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
417 result = self.digitalReadObj.read_vector_c81d(self.__thisUnixSample,
418 self.__samples_to_read,
418 self.__samples_to_read,
419 thisChannelName)
419 thisChannelName)
420
420
421 except IOError as e:
421 except IOError as e:
422 # read next profile
422 # read next profile
423 self.__flagDiscontinuousBlock = True
423 self.__flagDiscontinuousBlock = True
424 print("[Reading] %s" % datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e)
424 print("[Reading] %s" % datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), e)
425 break
425 break
426
426
427 if result.shape[0] != self.__samples_to_read:
427 if result.shape[0] != self.__samples_to_read:
428 self.__flagDiscontinuousBlock = True
428 self.__flagDiscontinuousBlock = True
429 print("[Reading] %s: Too few samples were found, just %d/%d samples" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
429 print("[Reading] %s: Too few samples were found, just %d/%d samples" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
430 result.shape[0],
430 result.shape[0],
431 self.__samples_to_read))
431 self.__samples_to_read))
432 break
432 break
433
433
434 self.__data_buffer[indexChannel, :] = result * volt_scale
434 self.__data_buffer[indexChannel, :] = result * volt_scale
435
435
436 indexChannel += 1
436 indexChannel += 1
437
437
438 dataOk = True
438 dataOk = True
439
439
440 self.__utctime = self.__thisUnixSample / self.__sample_rate
440 self.__utctime = self.__thisUnixSample / self.__sample_rate
441
441
442 if not dataOk:
442 if not dataOk:
443 return False
443 return False
444
444
445 print("[Reading] %s: %d samples <> %f sec" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
445 print("[Reading] %s: %d samples <> %f sec" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone),
446 self.__samples_to_read,
446 self.__samples_to_read,
447 self.__timeInterval))
447 self.__timeInterval))
448
448
449 self.__bufferIndex = 0
449 self.__bufferIndex = 0
450
450
451 return True
451 return True
452
452
453 def __isBufferEmpty(self):
453 def __isBufferEmpty(self):
454
454
455 if self.__bufferIndex <= self.__samples_to_read - self.__nSamples:
455 if self.__bufferIndex <= self.__samples_to_read - self.__nSamples:
456 return False
456 return False
457
457
458 return True
458 return True
459
459
460 def getData(self, seconds=30, nTries=5):
460 def getData(self, seconds=30, nTries=5):
461
461
462 '''
462 '''
463 This method gets the data from files and put the data into the dataOut object
463 This method gets the data from files and put the data into the dataOut object
464
464
465 In addition, increase el the buffer counter in one.
465 In addition, increase el the buffer counter in one.
466
466
467 Return:
467 Return:
468 data : retorna un perfil de voltages (alturas * canales) copiados desde el
468 data : retorna un perfil de voltages (alturas * canales) copiados desde el
469 buffer. Si no hay mas archivos a leer retorna None.
469 buffer. Si no hay mas archivos a leer retorna None.
470
470
471 Affected:
471 Affected:
472 self.dataOut
472 self.dataOut
473 self.profileIndex
473 self.profileIndex
474 self.flagDiscontinuousBlock
474 self.flagDiscontinuousBlock
475 self.flagIsNewBlock
475 self.flagIsNewBlock
476 '''
476 '''
477
477
478 err_counter = 0
478 err_counter = 0
479 self.dataOut.flagNoData = True
479 self.dataOut.flagNoData = True
480
480
481 if self.__isBufferEmpty():
481 if self.__isBufferEmpty():
482
482
483 self.__flagDiscontinuousBlock = False
483 self.__flagDiscontinuousBlock = False
484
484
485 while True:
485 while True:
486 if self.__readNextBlock():
486 if self.__readNextBlock():
487 break
487 break
488
488
489 if self.__thisUnixSample > self.__endUTCSecond * self.__sample_rate:
489 if self.__thisUnixSample > self.__endUTCSecond * self.__sample_rate:
490 return False
490 return False
491
491
492 if self.__flagDiscontinuousBlock:
492 if self.__flagDiscontinuousBlock:
493 print('[Reading] discontinuous block found ... continue with the next block')
493 print('[Reading] discontinuous block found ... continue with the next block')
494 continue
494 continue
495
495
496 if not self.__online:
496 if not self.__online:
497 return False
497 return False
498
498
499 err_counter += 1
499 err_counter += 1
500 if err_counter > nTries:
500 if err_counter > nTries:
501 return False
501 return False
502
502
503 print('[Reading] waiting %d seconds to read a new block' % seconds)
503 print('[Reading] waiting %d seconds to read a new block' % seconds)
504 sleep(seconds)
504 sleep(seconds)
505
505
506 self.dataOut.data = self.__data_buffer[:, self.__bufferIndex:self.__bufferIndex + self.__nSamples]
506 self.dataOut.data = self.__data_buffer[:, self.__bufferIndex:self.__bufferIndex + self.__nSamples]
507 self.dataOut.utctime = (self.__thisUnixSample + self.__bufferIndex) / self.__sample_rate
507 self.dataOut.utctime = (self.__thisUnixSample + self.__bufferIndex) / self.__sample_rate
508 self.dataOut.flagNoData = False
508 self.dataOut.flagNoData = False
509 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
509 self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock
510 self.dataOut.profileIndex = self.profileIndex
510 self.dataOut.profileIndex = self.profileIndex
511
511
512 self.__bufferIndex += self.__nSamples
512 self.__bufferIndex += self.__nSamples
513 self.profileIndex += 1
513 self.profileIndex += 1
514
514
515 if self.profileIndex == self.dataOut.nProfiles:
515 if self.profileIndex == self.dataOut.nProfiles:
516 self.profileIndex = 0
516 self.profileIndex = 0
517
517
518 return True
518 return True
519
519
520 def printInfo(self):
520 def printInfo(self):
521 '''
521 '''
522 '''
522 '''
523 if self.__printInfo == False:
523 if self.__printInfo == False:
524 return
524 return
525
525
526 # self.systemHeaderObj.printInfo()
526 # self.systemHeaderObj.printInfo()
527 # self.radarControllerHeaderObj.printInfo()
527 # self.radarControllerHeaderObj.printInfo()
528
528
529 self.__printInfo = False
529 self.__printInfo = False
530
530
531 def printNumberOfBlock(self):
531 def printNumberOfBlock(self):
532 '''
532 '''
533 '''
533 '''
534
534
535 print(self.profileIndex)
535 print(self.profileIndex)
536
536
537 def run(self, **kwargs):
537 def run(self, **kwargs):
538 '''
538 '''
539 This method will be called many times so here you should put all your code
539 This method will be called many times so here you should put all your code
540 '''
540 '''
541
541
542 if not self.isConfig:
542 if not self.isConfig:
543 self.setup(**kwargs)
543 self.setup(**kwargs)
544
544
545 self.getData(seconds=self.__delay)
545 self.getData(seconds=self.__delay)
546
546
547 return
547 return
548
548
549
549
550 @MPDecorator
550 @MPDecorator
551 class USRPWriter(Operation):
551 class USRPWriter(Operation):
552 '''
552 '''
553 classdocs
553 classdocs
554 '''
554 '''
555
555
556 def __init__(self, **kwargs):
556 def __init__(self, **kwargs):
557 '''
557 '''
558 Constructor
558 Constructor
559 '''
559 '''
560 Operation.__init__(self, **kwargs)
560 Operation.__init__(self, **kwargs)
561 self.dataOut = None
561 self.dataOut = None
562
562
563 def setup(self, dataIn, path, blocksPerFile, set=0, ext=None):
563 def setup(self, dataIn, path, blocksPerFile, set=0, ext=None):
564 '''
564 '''
565 In this method we should set all initial parameters.
565 In this method we should set all initial parameters.
566
566
567 Input:
567 Input:
568 dataIn : Input data will also be outputa data
568 dataIn : Input data will also be outputa data
569
569
570 '''
570 '''
571 self.dataOut = dataIn
571 self.dataOut = dataIn
572
572
573
573
574
574
575
575
576
576
577 self.isConfig = True
577 self.isConfig = True
578
578
579 return
579 return
580
580
581 def run(self, dataIn, **kwargs):
581 def run(self, dataIn, **kwargs):
582 '''
582 '''
583 This method will be called many times so here you should put all your code
583 This method will be called many times so here you should put all your code
584
584
585 Inputs:
585 Inputs:
586
586
587 dataIn : object with the data
587 dataIn : object with the data
588
588
589 '''
589 '''
590
590
591 if not self.isConfig:
591 if not self.isConfig:
592 self.setup(dataIn, **kwargs)
592 self.setup(dataIn, **kwargs)
593
593
594
594
595 if __name__ == '__main__':
595 if __name__ == '__main__':
596
596
597 readObj = USRPReader()
597 readObj = USRPReader()
598
598
599 while True:
599 while True:
600 readObj.run(path='/Volumes/DATA/haystack/passive_radar/')
600 readObj.run(path='/Volumes/DATA/haystack/passive_radar/')
601 # readObj.printInfo()
601 # readObj.printInfo()
602 readObj.printNumberOfBlock()
602 readObj.printNumberOfBlock()
General Comments 0
You need to be logged in to leave comments. Login now