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