@@ -1,1254 +1,1257 | |||||
1 | ''' |
|
1 | ''' | |
2 | Updated on January , 2018, for multiprocessing purposes |
|
2 | Updated on January , 2018, for multiprocessing purposes | |
3 | Author: Sergio Cortez |
|
3 | Author: Sergio Cortez | |
4 | Created on September , 2012 |
|
4 | Created on September , 2012 | |
5 | ''' |
|
5 | ''' | |
6 | from platform import python_version |
|
6 | from platform import python_version | |
7 | import sys |
|
7 | import sys | |
8 | import ast |
|
8 | import ast | |
9 | import datetime |
|
9 | import datetime | |
10 | import traceback |
|
10 | import traceback | |
11 | import math |
|
11 | import math | |
12 | import time |
|
12 | import time | |
13 | import zmq |
|
13 | import zmq | |
14 | from multiprocessing import Process, cpu_count |
|
14 | from multiprocessing import Process, cpu_count | |
15 | from threading import Thread |
|
15 | from threading import Thread | |
16 | from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring |
|
16 | from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring | |
17 | from xml.dom import minidom |
|
17 | from xml.dom import minidom | |
18 |
|
18 | |||
19 |
|
19 | |||
20 | from schainpy.admin import Alarm, SchainWarning |
|
20 | from schainpy.admin import Alarm, SchainWarning | |
21 | from schainpy.model import * |
|
21 | from schainpy.model import * | |
22 | from schainpy.utils import log |
|
22 | from schainpy.utils import log | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | DTYPES = { |
|
25 | DTYPES = { | |
26 | 'Voltage': '.r', |
|
26 | 'Voltage': '.r', | |
27 | 'Spectra': '.pdata' |
|
27 | 'Spectra': '.pdata' | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | def MPProject(project, n=cpu_count()): |
|
31 | def MPProject(project, n=cpu_count()): | |
32 | ''' |
|
32 | ''' | |
33 | Project wrapper to run schain in n processes |
|
33 | Project wrapper to run schain in n processes | |
34 | ''' |
|
34 | ''' | |
35 |
|
35 | |||
36 | rconf = project.getReadUnitObj() |
|
36 | rconf = project.getReadUnitObj() | |
37 | op = rconf.getOperationObj('run') |
|
37 | op = rconf.getOperationObj('run') | |
38 | dt1 = op.getParameterValue('startDate') |
|
38 | dt1 = op.getParameterValue('startDate') | |
39 | dt2 = op.getParameterValue('endDate') |
|
39 | dt2 = op.getParameterValue('endDate') | |
40 | tm1 = op.getParameterValue('startTime') |
|
40 | tm1 = op.getParameterValue('startTime') | |
41 | tm2 = op.getParameterValue('endTime') |
|
41 | tm2 = op.getParameterValue('endTime') | |
42 | days = (dt2 - dt1).days |
|
42 | days = (dt2 - dt1).days | |
43 |
|
43 | |||
44 | for day in range(days + 1): |
|
44 | for day in range(days + 1): | |
45 | skip = 0 |
|
45 | skip = 0 | |
46 | cursor = 0 |
|
46 | cursor = 0 | |
47 | processes = [] |
|
47 | processes = [] | |
48 | dt = dt1 + datetime.timedelta(day) |
|
48 | dt = dt1 + datetime.timedelta(day) | |
49 | dt_str = dt.strftime('%Y/%m/%d') |
|
49 | dt_str = dt.strftime('%Y/%m/%d') | |
50 | reader = JRODataReader() |
|
50 | reader = JRODataReader() | |
51 | paths, files = reader.searchFilesOffLine(path=rconf.path, |
|
51 | paths, files = reader.searchFilesOffLine(path=rconf.path, | |
52 | startDate=dt, |
|
52 | startDate=dt, | |
53 | endDate=dt, |
|
53 | endDate=dt, | |
54 | startTime=tm1, |
|
54 | startTime=tm1, | |
55 | endTime=tm2, |
|
55 | endTime=tm2, | |
56 | ext=DTYPES[rconf.datatype]) |
|
56 | ext=DTYPES[rconf.datatype]) | |
57 | nFiles = len(files) |
|
57 | nFiles = len(files) | |
58 | if nFiles == 0: |
|
58 | if nFiles == 0: | |
59 | continue |
|
59 | continue | |
60 | skip = int(math.ceil(nFiles / n)) |
|
60 | skip = int(math.ceil(nFiles / n)) | |
61 | while nFiles > cursor * skip: |
|
61 | while nFiles > cursor * skip: | |
62 | rconf.update(startDate=dt_str, endDate=dt_str, cursor=cursor, |
|
62 | rconf.update(startDate=dt_str, endDate=dt_str, cursor=cursor, | |
63 | skip=skip) |
|
63 | skip=skip) | |
64 | p = project.clone() |
|
64 | p = project.clone() | |
65 | p.start() |
|
65 | p.start() | |
66 | processes.append(p) |
|
66 | processes.append(p) | |
67 | cursor += 1 |
|
67 | cursor += 1 | |
68 |
|
68 | |||
69 | def beforeExit(exctype, value, trace): |
|
69 | def beforeExit(exctype, value, trace): | |
70 | for process in processes: |
|
70 | for process in processes: | |
71 | process.terminate() |
|
71 | process.terminate() | |
72 | process.join() |
|
72 | process.join() | |
73 | print(traceback.print_tb(trace)) |
|
73 | print(traceback.print_tb(trace)) | |
74 |
|
74 | |||
75 | sys.excepthook = beforeExit |
|
75 | sys.excepthook = beforeExit | |
76 |
|
76 | |||
77 | for process in processes: |
|
77 | for process in processes: | |
78 | process.join() |
|
78 | process.join() | |
79 | process.terminate() |
|
79 | process.terminate() | |
80 |
|
80 | |||
81 | time.sleep(3) |
|
81 | time.sleep(3) | |
82 |
|
82 | |||
83 | def wait(context): |
|
83 | def wait(context): | |
84 |
|
84 | |||
85 | time.sleep(1) |
|
85 | time.sleep(1) | |
86 | c = zmq.Context() |
|
86 | c = zmq.Context() | |
87 | receiver = c.socket(zmq.SUB) |
|
87 | receiver = c.socket(zmq.SUB) | |
88 | receiver.connect('ipc:///tmp/schain_{}_pub'.format(self.id)) |
|
88 | receiver.connect('ipc:///tmp/schain_{}_pub'.format(self.id)) | |
89 | receiver.setsockopt(zmq.SUBSCRIBE, self.id.encode()) |
|
89 | receiver.setsockopt(zmq.SUBSCRIBE, self.id.encode()) | |
90 | msg = receiver.recv_multipart()[1] |
|
90 | msg = receiver.recv_multipart()[1] | |
91 | context.terminate() |
|
91 | context.terminate() | |
92 |
|
92 | |||
93 | class ParameterConf(): |
|
93 | class ParameterConf(): | |
94 |
|
94 | |||
95 | id = None |
|
95 | id = None | |
96 | name = None |
|
96 | name = None | |
97 | value = None |
|
97 | value = None | |
98 | format = None |
|
98 | format = None | |
99 |
|
99 | |||
100 | __formated_value = None |
|
100 | __formated_value = None | |
101 |
|
101 | |||
102 | ELEMENTNAME = 'Parameter' |
|
102 | ELEMENTNAME = 'Parameter' | |
103 |
|
103 | |||
104 | def __init__(self): |
|
104 | def __init__(self): | |
105 |
|
105 | |||
106 | self.format = 'str' |
|
106 | self.format = 'str' | |
107 |
|
107 | |||
108 | def getElementName(self): |
|
108 | def getElementName(self): | |
109 |
|
109 | |||
110 | return self.ELEMENTNAME |
|
110 | return self.ELEMENTNAME | |
111 |
|
111 | |||
112 | def getValue(self): |
|
112 | def getValue(self): | |
113 |
|
113 | |||
114 | value = self.value |
|
114 | value = self.value | |
115 | format = self.format |
|
115 | format = self.format | |
116 |
|
116 | |||
117 | if self.__formated_value != None: |
|
117 | if self.__formated_value != None: | |
118 |
|
118 | |||
119 | return self.__formated_value |
|
119 | return self.__formated_value | |
120 |
|
120 | |||
121 | if format == 'obj': |
|
121 | if format == 'obj': | |
122 | return value |
|
122 | return value | |
123 |
|
123 | |||
124 | if format == 'str': |
|
124 | if format == 'str': | |
125 | self.__formated_value = str(value) |
|
125 | self.__formated_value = str(value) | |
126 | return self.__formated_value |
|
126 | return self.__formated_value | |
127 |
|
127 | |||
128 | if value == '': |
|
128 | if value == '': | |
129 | raise ValueError('%s: This parameter value is empty' % self.name) |
|
129 | raise ValueError('%s: This parameter value is empty' % self.name) | |
130 |
|
130 | |||
131 | if format == 'list': |
|
131 | if format == 'list': | |
132 | strList = value.split(',') |
|
132 | strList = value.split(',') | |
133 |
|
133 | |||
134 | self.__formated_value = strList |
|
134 | self.__formated_value = strList | |
135 |
|
135 | |||
136 | return self.__formated_value |
|
136 | return self.__formated_value | |
137 |
|
137 | |||
138 | if format == 'intlist': |
|
138 | if format == 'intlist': | |
139 | ''' |
|
139 | ''' | |
140 | Example: |
|
140 | Example: | |
141 | value = (0,1,2) |
|
141 | value = (0,1,2) | |
142 | ''' |
|
142 | ''' | |
143 |
|
143 | |||
144 | new_value = ast.literal_eval(value) |
|
144 | new_value = ast.literal_eval(value) | |
145 |
|
145 | |||
146 | if type(new_value) not in (tuple, list): |
|
146 | if type(new_value) not in (tuple, list): | |
147 | new_value = [int(new_value)] |
|
147 | new_value = [int(new_value)] | |
148 |
|
148 | |||
149 | self.__formated_value = new_value |
|
149 | self.__formated_value = new_value | |
150 |
|
150 | |||
151 | return self.__formated_value |
|
151 | return self.__formated_value | |
152 |
|
152 | |||
153 | if format == 'floatlist': |
|
153 | if format == 'floatlist': | |
154 | ''' |
|
154 | ''' | |
155 | Example: |
|
155 | Example: | |
156 | value = (0.5, 1.4, 2.7) |
|
156 | value = (0.5, 1.4, 2.7) | |
157 | ''' |
|
157 | ''' | |
158 |
|
158 | |||
159 | new_value = ast.literal_eval(value) |
|
159 | new_value = ast.literal_eval(value) | |
160 |
|
160 | |||
161 | if type(new_value) not in (tuple, list): |
|
161 | if type(new_value) not in (tuple, list): | |
162 | new_value = [float(new_value)] |
|
162 | new_value = [float(new_value)] | |
163 |
|
163 | |||
164 | self.__formated_value = new_value |
|
164 | self.__formated_value = new_value | |
165 |
|
165 | |||
166 | return self.__formated_value |
|
166 | return self.__formated_value | |
167 |
|
167 | |||
168 | if format == 'date': |
|
168 | if format == 'date': | |
169 | strList = value.split('/') |
|
169 | strList = value.split('/') | |
170 | intList = [int(x) for x in strList] |
|
170 | intList = [int(x) for x in strList] | |
171 | date = datetime.date(intList[0], intList[1], intList[2]) |
|
171 | date = datetime.date(intList[0], intList[1], intList[2]) | |
172 |
|
172 | |||
173 | self.__formated_value = date |
|
173 | self.__formated_value = date | |
174 |
|
174 | |||
175 | return self.__formated_value |
|
175 | return self.__formated_value | |
176 |
|
176 | |||
177 | if format == 'time': |
|
177 | if format == 'time': | |
178 | strList = value.split(':') |
|
178 | strList = value.split(':') | |
179 | intList = [int(x) for x in strList] |
|
179 | intList = [int(x) for x in strList] | |
180 | time = datetime.time(intList[0], intList[1], intList[2]) |
|
180 | time = datetime.time(intList[0], intList[1], intList[2]) | |
181 |
|
181 | |||
182 | self.__formated_value = time |
|
182 | self.__formated_value = time | |
183 |
|
183 | |||
184 | return self.__formated_value |
|
184 | return self.__formated_value | |
185 |
|
185 | |||
186 | if format == 'pairslist': |
|
186 | if format == 'pairslist': | |
187 | ''' |
|
187 | ''' | |
188 | Example: |
|
188 | Example: | |
189 | value = (0,1),(1,2) |
|
189 | value = (0,1),(1,2) | |
190 | ''' |
|
190 | ''' | |
191 |
|
191 | |||
192 | new_value = ast.literal_eval(value) |
|
192 | new_value = ast.literal_eval(value) | |
193 |
|
193 | |||
194 | if type(new_value) not in (tuple, list): |
|
194 | if type(new_value) not in (tuple, list): | |
195 | raise ValueError('%s has to be a tuple or list of pairs' % value) |
|
195 | raise ValueError('%s has to be a tuple or list of pairs' % value) | |
196 |
|
196 | |||
197 | if type(new_value[0]) not in (tuple, list): |
|
197 | if type(new_value[0]) not in (tuple, list): | |
198 | if len(new_value) != 2: |
|
198 | if len(new_value) != 2: | |
199 | raise ValueError('%s has to be a tuple or list of pairs' % value) |
|
199 | raise ValueError('%s has to be a tuple or list of pairs' % value) | |
200 | new_value = [new_value] |
|
200 | new_value = [new_value] | |
201 |
|
201 | |||
202 | for thisPair in new_value: |
|
202 | for thisPair in new_value: | |
203 | if len(thisPair) != 2: |
|
203 | if len(thisPair) != 2: | |
204 | raise ValueError('%s has to be a tuple or list of pairs' % value) |
|
204 | raise ValueError('%s has to be a tuple or list of pairs' % value) | |
205 |
|
205 | |||
206 | self.__formated_value = new_value |
|
206 | self.__formated_value = new_value | |
207 |
|
207 | |||
208 | return self.__formated_value |
|
208 | return self.__formated_value | |
209 |
|
209 | |||
210 | if format == 'multilist': |
|
210 | if format == 'multilist': | |
211 | ''' |
|
211 | ''' | |
212 | Example: |
|
212 | Example: | |
213 | value = (0,1,2),(3,4,5) |
|
213 | value = (0,1,2),(3,4,5) | |
214 | ''' |
|
214 | ''' | |
215 | multiList = ast.literal_eval(value) |
|
215 | multiList = ast.literal_eval(value) | |
216 |
|
216 | |||
217 | if type(multiList[0]) == int: |
|
217 | if type(multiList[0]) == int: | |
218 | multiList = ast.literal_eval('(' + value + ')') |
|
218 | multiList = ast.literal_eval('(' + value + ')') | |
219 |
|
219 | |||
220 | self.__formated_value = multiList |
|
220 | self.__formated_value = multiList | |
221 |
|
221 | |||
222 | return self.__formated_value |
|
222 | return self.__formated_value | |
223 |
|
223 | |||
224 | if format == 'bool': |
|
224 | if format == 'bool': | |
225 | value = int(value) |
|
225 | value = int(value) | |
226 |
|
226 | |||
227 | if format == 'int': |
|
227 | if format == 'int': | |
228 | value = float(value) |
|
228 | value = float(value) | |
229 |
|
229 | |||
230 | format_func = eval(format) |
|
230 | format_func = eval(format) | |
231 |
|
231 | |||
232 | self.__formated_value = format_func(value) |
|
232 | self.__formated_value = format_func(value) | |
233 |
|
233 | |||
234 | return self.__formated_value |
|
234 | return self.__formated_value | |
235 |
|
235 | |||
236 | def updateId(self, new_id): |
|
236 | def updateId(self, new_id): | |
237 |
|
237 | |||
238 | self.id = str(new_id) |
|
238 | self.id = str(new_id) | |
239 |
|
239 | |||
240 | def setup(self, id, name, value, format='str'): |
|
240 | def setup(self, id, name, value, format='str'): | |
241 | self.id = str(id) |
|
241 | self.id = str(id) | |
242 | self.name = name |
|
242 | self.name = name | |
243 | if format == 'obj': |
|
243 | if format == 'obj': | |
244 | self.value = value |
|
244 | self.value = value | |
245 | else: |
|
245 | else: | |
246 | self.value = str(value) |
|
246 | self.value = str(value) | |
247 | self.format = str.lower(format) |
|
247 | self.format = str.lower(format) | |
248 |
|
248 | |||
249 | self.getValue() |
|
249 | self.getValue() | |
250 |
|
250 | |||
251 | return 1 |
|
251 | return 1 | |
252 |
|
252 | |||
253 | def update(self, name, value, format='str'): |
|
253 | def update(self, name, value, format='str'): | |
254 |
|
254 | |||
255 | self.name = name |
|
255 | self.name = name | |
256 | self.value = str(value) |
|
256 | self.value = str(value) | |
257 | self.format = format |
|
257 | self.format = format | |
258 |
|
258 | |||
259 | def makeXml(self, opElement): |
|
259 | def makeXml(self, opElement): | |
260 | if self.name not in ('queue',): |
|
260 | if self.name not in ('queue',): | |
261 | parmElement = SubElement(opElement, self.ELEMENTNAME) |
|
261 | parmElement = SubElement(opElement, self.ELEMENTNAME) | |
262 | parmElement.set('id', str(self.id)) |
|
262 | parmElement.set('id', str(self.id)) | |
263 | parmElement.set('name', self.name) |
|
263 | parmElement.set('name', self.name) | |
264 | parmElement.set('value', self.value) |
|
264 | parmElement.set('value', self.value) | |
265 | parmElement.set('format', self.format) |
|
265 | parmElement.set('format', self.format) | |
266 |
|
266 | |||
267 | def readXml(self, parmElement): |
|
267 | def readXml(self, parmElement): | |
268 |
|
268 | |||
269 | self.id = parmElement.get('id') |
|
269 | self.id = parmElement.get('id') | |
270 | self.name = parmElement.get('name') |
|
270 | self.name = parmElement.get('name') | |
271 | self.value = parmElement.get('value') |
|
271 | self.value = parmElement.get('value') | |
272 | self.format = str.lower(parmElement.get('format')) |
|
272 | self.format = str.lower(parmElement.get('format')) | |
273 |
|
273 | |||
274 | # Compatible with old signal chain version |
|
274 | # Compatible with old signal chain version | |
275 | if self.format == 'int' and self.name == 'idfigure': |
|
275 | if self.format == 'int' and self.name == 'idfigure': | |
276 | self.name = 'id' |
|
276 | self.name = 'id' | |
277 |
|
277 | |||
278 | def printattr(self): |
|
278 | def printattr(self): | |
279 |
|
279 | |||
280 | print('Parameter[%s]: name = %s, value = %s, format = %s, project_id = %s' % (self.id, self.name, self.value, self.format, self.project_id)) |
|
280 | print('Parameter[%s]: name = %s, value = %s, format = %s, project_id = %s' % (self.id, self.name, self.value, self.format, self.project_id)) | |
281 |
|
281 | |||
282 | class OperationConf(): |
|
282 | class OperationConf(): | |
283 |
|
283 | |||
284 | ELEMENTNAME = 'Operation' |
|
284 | ELEMENTNAME = 'Operation' | |
285 |
|
285 | |||
286 | def __init__(self): |
|
286 | def __init__(self): | |
287 |
|
287 | |||
288 | self.id = '0' |
|
288 | self.id = '0' | |
289 | self.name = None |
|
289 | self.name = None | |
290 | self.priority = None |
|
290 | self.priority = None | |
291 | self.topic = None |
|
291 | self.topic = None | |
292 |
|
292 | |||
293 | def __getNewId(self): |
|
293 | def __getNewId(self): | |
294 |
|
294 | |||
295 | return int(self.id) * 10 + len(self.parmConfObjList) + 1 |
|
295 | return int(self.id) * 10 + len(self.parmConfObjList) + 1 | |
296 |
|
296 | |||
297 | def getId(self): |
|
297 | def getId(self): | |
298 | return self.id |
|
298 | return self.id | |
299 |
|
299 | |||
300 | def updateId(self, new_id): |
|
300 | def updateId(self, new_id): | |
301 |
|
301 | |||
302 | self.id = str(new_id) |
|
302 | self.id = str(new_id) | |
303 |
|
303 | |||
304 | n = 1 |
|
304 | n = 1 | |
305 | for parmObj in self.parmConfObjList: |
|
305 | for parmObj in self.parmConfObjList: | |
306 |
|
306 | |||
307 | idParm = str(int(new_id) * 10 + n) |
|
307 | idParm = str(int(new_id) * 10 + n) | |
308 | parmObj.updateId(idParm) |
|
308 | parmObj.updateId(idParm) | |
309 |
|
309 | |||
310 | n += 1 |
|
310 | n += 1 | |
311 |
|
311 | |||
312 | def getElementName(self): |
|
312 | def getElementName(self): | |
313 |
|
313 | |||
314 | return self.ELEMENTNAME |
|
314 | return self.ELEMENTNAME | |
315 |
|
315 | |||
316 | def getParameterObjList(self): |
|
316 | def getParameterObjList(self): | |
317 |
|
317 | |||
318 | return self.parmConfObjList |
|
318 | return self.parmConfObjList | |
319 |
|
319 | |||
320 | def getParameterObj(self, parameterName): |
|
320 | def getParameterObj(self, parameterName): | |
321 |
|
321 | |||
322 | for parmConfObj in self.parmConfObjList: |
|
322 | for parmConfObj in self.parmConfObjList: | |
323 |
|
323 | |||
324 | if parmConfObj.name != parameterName: |
|
324 | if parmConfObj.name != parameterName: | |
325 | continue |
|
325 | continue | |
326 |
|
326 | |||
327 | return parmConfObj |
|
327 | return parmConfObj | |
328 |
|
328 | |||
329 | return None |
|
329 | return None | |
330 |
|
330 | |||
331 | def getParameterObjfromValue(self, parameterValue): |
|
331 | def getParameterObjfromValue(self, parameterValue): | |
332 |
|
332 | |||
333 | for parmConfObj in self.parmConfObjList: |
|
333 | for parmConfObj in self.parmConfObjList: | |
334 |
|
334 | |||
335 | if parmConfObj.getValue() != parameterValue: |
|
335 | if parmConfObj.getValue() != parameterValue: | |
336 | continue |
|
336 | continue | |
337 |
|
337 | |||
338 | return parmConfObj.getValue() |
|
338 | return parmConfObj.getValue() | |
339 |
|
339 | |||
340 | return None |
|
340 | return None | |
341 |
|
341 | |||
342 | def getParameterValue(self, parameterName): |
|
342 | def getParameterValue(self, parameterName): | |
343 |
|
343 | |||
344 | parameterObj = self.getParameterObj(parameterName) |
|
344 | parameterObj = self.getParameterObj(parameterName) | |
345 |
|
345 | |||
346 | # if not parameterObj: |
|
346 | # if not parameterObj: | |
347 | # return None |
|
347 | # return None | |
348 |
|
348 | |||
349 | value = parameterObj.getValue() |
|
349 | value = parameterObj.getValue() | |
350 |
|
350 | |||
351 | return value |
|
351 | return value | |
352 |
|
352 | |||
353 | def getKwargs(self): |
|
353 | def getKwargs(self): | |
354 |
|
354 | |||
355 | kwargs = {} |
|
355 | kwargs = {} | |
356 |
|
356 | |||
357 | for parmConfObj in self.parmConfObjList: |
|
357 | for parmConfObj in self.parmConfObjList: | |
358 | if self.name == 'run' and parmConfObj.name == 'datatype': |
|
358 | if self.name == 'run' and parmConfObj.name == 'datatype': | |
359 | continue |
|
359 | continue | |
360 |
|
360 | |||
361 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
361 | kwargs[parmConfObj.name] = parmConfObj.getValue() | |
362 |
|
362 | |||
363 | return kwargs |
|
363 | return kwargs | |
364 |
|
364 | |||
365 | def setup(self, id, name, priority, type, project_id): |
|
365 | def setup(self, id, name, priority, type, project_id): | |
366 |
|
366 | |||
367 | self.id = str(id) |
|
367 | self.id = str(id) | |
368 | self.project_id = project_id |
|
368 | self.project_id = project_id | |
369 | self.name = name |
|
369 | self.name = name | |
370 | self.type = type |
|
370 | self.type = type | |
371 | self.priority = priority |
|
371 | self.priority = priority | |
372 | self.parmConfObjList = [] |
|
372 | self.parmConfObjList = [] | |
373 |
|
373 | |||
374 | def removeParameters(self): |
|
374 | def removeParameters(self): | |
375 |
|
375 | |||
376 | for obj in self.parmConfObjList: |
|
376 | for obj in self.parmConfObjList: | |
377 | del obj |
|
377 | del obj | |
378 |
|
378 | |||
379 | self.parmConfObjList = [] |
|
379 | self.parmConfObjList = [] | |
380 |
|
380 | |||
381 | def addParameter(self, name, value, format='str'): |
|
381 | def addParameter(self, name, value, format='str'): | |
382 |
|
382 | |||
383 | if value is None: |
|
383 | if value is None: | |
384 | return None |
|
384 | return None | |
385 | id = self.__getNewId() |
|
385 | id = self.__getNewId() | |
386 |
|
386 | |||
387 | parmConfObj = ParameterConf() |
|
387 | parmConfObj = ParameterConf() | |
388 | if not parmConfObj.setup(id, name, value, format): |
|
388 | if not parmConfObj.setup(id, name, value, format): | |
389 | return None |
|
389 | return None | |
390 |
|
390 | |||
391 | self.parmConfObjList.append(parmConfObj) |
|
391 | self.parmConfObjList.append(parmConfObj) | |
392 |
|
392 | |||
393 | return parmConfObj |
|
393 | return parmConfObj | |
394 |
|
394 | |||
395 | def changeParameter(self, name, value, format='str'): |
|
395 | def changeParameter(self, name, value, format='str'): | |
396 |
|
396 | |||
397 | parmConfObj = self.getParameterObj(name) |
|
397 | parmConfObj = self.getParameterObj(name) | |
398 | parmConfObj.update(name, value, format) |
|
398 | parmConfObj.update(name, value, format) | |
399 |
|
399 | |||
400 | return parmConfObj |
|
400 | return parmConfObj | |
401 |
|
401 | |||
402 | def makeXml(self, procUnitElement): |
|
402 | def makeXml(self, procUnitElement): | |
403 |
|
403 | |||
404 | opElement = SubElement(procUnitElement, self.ELEMENTNAME) |
|
404 | opElement = SubElement(procUnitElement, self.ELEMENTNAME) | |
405 | opElement.set('id', str(self.id)) |
|
405 | opElement.set('id', str(self.id)) | |
406 | opElement.set('name', self.name) |
|
406 | opElement.set('name', self.name) | |
407 | opElement.set('type', self.type) |
|
407 | opElement.set('type', self.type) | |
408 | opElement.set('priority', str(self.priority)) |
|
408 | opElement.set('priority', str(self.priority)) | |
409 |
|
409 | |||
410 | for parmConfObj in self.parmConfObjList: |
|
410 | for parmConfObj in self.parmConfObjList: | |
411 | parmConfObj.makeXml(opElement) |
|
411 | parmConfObj.makeXml(opElement) | |
412 |
|
412 | |||
413 | def readXml(self, opElement, project_id): |
|
413 | def readXml(self, opElement, project_id): | |
414 |
|
414 | |||
415 | self.id = opElement.get('id') |
|
415 | self.id = opElement.get('id') | |
416 | self.name = opElement.get('name') |
|
416 | self.name = opElement.get('name') | |
417 | self.type = opElement.get('type') |
|
417 | self.type = opElement.get('type') | |
418 | self.priority = opElement.get('priority') |
|
418 | self.priority = opElement.get('priority') | |
419 |
self.project_id = str(project_id) |
|
419 | self.project_id = str(project_id) | |
420 |
|
420 | |||
421 | # Compatible with old signal chain version |
|
421 | # Compatible with old signal chain version | |
422 | # Use of 'run' method instead 'init' |
|
422 | # Use of 'run' method instead 'init' | |
423 | if self.type == 'self' and self.name == 'init': |
|
423 | if self.type == 'self' and self.name == 'init': | |
424 | self.name = 'run' |
|
424 | self.name = 'run' | |
425 |
|
425 | |||
426 | self.parmConfObjList = [] |
|
426 | self.parmConfObjList = [] | |
427 |
|
427 | |||
428 | parmElementList = opElement.iter(ParameterConf().getElementName()) |
|
428 | parmElementList = opElement.iter(ParameterConf().getElementName()) | |
429 |
|
429 | |||
430 | for parmElement in parmElementList: |
|
430 | for parmElement in parmElementList: | |
431 | parmConfObj = ParameterConf() |
|
431 | parmConfObj = ParameterConf() | |
432 | parmConfObj.readXml(parmElement) |
|
432 | parmConfObj.readXml(parmElement) | |
433 |
|
433 | |||
434 | # Compatible with old signal chain version |
|
434 | # Compatible with old signal chain version | |
435 | # If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER |
|
435 | # If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER | |
436 | if self.type != 'self' and self.name == 'Plot': |
|
436 | if self.type != 'self' and self.name == 'Plot': | |
437 | if parmConfObj.format == 'str' and parmConfObj.name == 'type': |
|
437 | if parmConfObj.format == 'str' and parmConfObj.name == 'type': | |
438 | self.name = parmConfObj.value |
|
438 | self.name = parmConfObj.value | |
439 | continue |
|
439 | continue | |
440 |
|
440 | |||
441 | self.parmConfObjList.append(parmConfObj) |
|
441 | self.parmConfObjList.append(parmConfObj) | |
442 |
|
442 | |||
443 | def printattr(self): |
|
443 | def printattr(self): | |
444 |
|
444 | |||
445 | print('%s[%s]: name = %s, type = %s, priority = %s, project_id = %s' % (self.ELEMENTNAME, |
|
445 | print('%s[%s]: name = %s, type = %s, priority = %s, project_id = %s' % (self.ELEMENTNAME, | |
446 | self.id, |
|
446 | self.id, | |
447 | self.name, |
|
447 | self.name, | |
448 | self.type, |
|
448 | self.type, | |
449 | self.priority, |
|
449 | self.priority, | |
450 | self.project_id)) |
|
450 | self.project_id)) | |
451 |
|
451 | |||
452 | for parmConfObj in self.parmConfObjList: |
|
452 | for parmConfObj in self.parmConfObjList: | |
453 | parmConfObj.printattr() |
|
453 | parmConfObj.printattr() | |
454 |
|
454 | |||
455 | def createObject(self): |
|
455 | def createObject(self): | |
456 |
|
456 | |||
457 | className = eval(self.name) |
|
457 | className = eval(self.name) | |
458 |
|
458 | |||
459 | if self.type == 'other': |
|
459 | if self.type == 'other': | |
460 | opObj = className() |
|
460 | opObj = className() | |
461 | elif self.type == 'external': |
|
461 | elif self.type == 'external': | |
462 | kwargs = self.getKwargs() |
|
462 | kwargs = self.getKwargs() | |
463 | opObj = className(self.id, self.project_id, **kwargs) |
|
463 | opObj = className(self.id, self.project_id, **kwargs) | |
464 | opObj.start() |
|
464 | opObj.start() | |
465 |
|
465 | |||
466 | return opObj |
|
466 | return opObj | |
467 |
|
467 | |||
468 | class ProcUnitConf(): |
|
468 | class ProcUnitConf(): | |
469 |
|
469 | |||
470 | ELEMENTNAME = 'ProcUnit' |
|
470 | ELEMENTNAME = 'ProcUnit' | |
471 |
|
471 | |||
472 | def __init__(self): |
|
472 | def __init__(self): | |
473 |
|
473 | |||
474 | self.id = None |
|
474 | self.id = None | |
475 | self.datatype = None |
|
475 | self.datatype = None | |
476 | self.name = None |
|
476 | self.name = None | |
477 | self.inputId = None |
|
477 | self.inputId = None | |
478 | self.opConfObjList = [] |
|
478 | self.opConfObjList = [] | |
479 | self.procUnitObj = None |
|
479 | self.procUnitObj = None | |
480 | self.opObjDict = {} |
|
480 | self.opObjDict = {} | |
481 |
|
481 | |||
482 | def __getPriority(self): |
|
482 | def __getPriority(self): | |
483 |
|
483 | |||
484 | return len(self.opConfObjList) + 1 |
|
484 | return len(self.opConfObjList) + 1 | |
485 |
|
485 | |||
486 | def __getNewId(self): |
|
486 | def __getNewId(self): | |
487 |
|
487 | |||
488 | return int(self.id) * 10 + len(self.opConfObjList) + 1 |
|
488 | return int(self.id) * 10 + len(self.opConfObjList) + 1 | |
489 |
|
489 | |||
490 | def getElementName(self): |
|
490 | def getElementName(self): | |
491 |
|
491 | |||
492 | return self.ELEMENTNAME |
|
492 | return self.ELEMENTNAME | |
493 |
|
493 | |||
494 | def getId(self): |
|
494 | def getId(self): | |
495 |
|
495 | |||
496 | return self.id |
|
496 | return self.id | |
497 |
|
497 | |||
498 | def updateId(self, new_id): |
|
498 | def updateId(self, new_id): | |
499 | ''' |
|
499 | ''' | |
500 | new_id = int(parentId) * 10 + (int(self.id) % 10) |
|
500 | new_id = int(parentId) * 10 + (int(self.id) % 10) | |
501 | new_inputId = int(parentId) * 10 + (int(self.inputId) % 10) |
|
501 | new_inputId = int(parentId) * 10 + (int(self.inputId) % 10) | |
502 |
|
502 | |||
503 | # If this proc unit has not inputs |
|
503 | # If this proc unit has not inputs | |
504 | #if self.inputId == '0': |
|
504 | #if self.inputId == '0': | |
505 | #new_inputId = 0 |
|
505 | #new_inputId = 0 | |
506 |
|
506 | |||
507 | n = 1 |
|
507 | n = 1 | |
508 | for opConfObj in self.opConfObjList: |
|
508 | for opConfObj in self.opConfObjList: | |
509 |
|
509 | |||
510 | idOp = str(int(new_id) * 10 + n) |
|
510 | idOp = str(int(new_id) * 10 + n) | |
511 | opConfObj.updateId(idOp) |
|
511 | opConfObj.updateId(idOp) | |
512 |
|
512 | |||
513 | n += 1 |
|
513 | n += 1 | |
514 |
|
514 | |||
515 | self.parentId = str(parentId) |
|
515 | self.parentId = str(parentId) | |
516 | self.id = str(new_id) |
|
516 | self.id = str(new_id) | |
517 | #self.inputId = str(new_inputId) |
|
517 | #self.inputId = str(new_inputId) | |
518 | ''' |
|
518 | ''' | |
519 | n = 1 |
|
519 | n = 1 | |
520 |
|
520 | |||
521 | def getInputId(self): |
|
521 | def getInputId(self): | |
522 |
|
522 | |||
523 | return self.inputId |
|
523 | return self.inputId | |
524 |
|
524 | |||
525 | def getOperationObjList(self): |
|
525 | def getOperationObjList(self): | |
526 |
|
526 | |||
527 | return self.opConfObjList |
|
527 | return self.opConfObjList | |
528 |
|
528 | |||
529 | def getOperationObj(self, name=None): |
|
529 | def getOperationObj(self, name=None): | |
530 |
|
530 | |||
531 | for opConfObj in self.opConfObjList: |
|
531 | for opConfObj in self.opConfObjList: | |
532 |
|
532 | |||
533 | if opConfObj.name != name: |
|
533 | if opConfObj.name != name: | |
534 | continue |
|
534 | continue | |
535 |
|
535 | |||
536 | return opConfObj |
|
536 | return opConfObj | |
537 |
|
537 | |||
538 | return None |
|
538 | return None | |
539 |
|
539 | |||
540 | def getOpObjfromParamValue(self, value=None): |
|
540 | def getOpObjfromParamValue(self, value=None): | |
541 |
|
541 | |||
542 | for opConfObj in self.opConfObjList: |
|
542 | for opConfObj in self.opConfObjList: | |
543 | if opConfObj.getParameterObjfromValue(parameterValue=value) != value: |
|
543 | if opConfObj.getParameterObjfromValue(parameterValue=value) != value: | |
544 | continue |
|
544 | continue | |
545 | return opConfObj |
|
545 | return opConfObj | |
546 | return None |
|
546 | return None | |
547 |
|
547 | |||
548 | def getProcUnitObj(self): |
|
548 | def getProcUnitObj(self): | |
549 |
|
549 | |||
550 | return self.procUnitObj |
|
550 | return self.procUnitObj | |
551 |
|
551 | |||
552 | def setup(self, project_id, id, name, datatype, inputId): |
|
552 | def setup(self, project_id, id, name, datatype, inputId): | |
553 | ''' |
|
553 | ''' | |
554 | id sera el topico a publicar |
|
554 | id sera el topico a publicar | |
555 | inputId sera el topico a subscribirse |
|
555 | inputId sera el topico a subscribirse | |
556 | ''' |
|
556 | ''' | |
557 |
|
557 | |||
558 | # Compatible with old signal chain version |
|
558 | # Compatible with old signal chain version | |
559 | if datatype == None and name == None: |
|
559 | if datatype == None and name == None: | |
560 | raise ValueError('datatype or name should be defined') |
|
560 | raise ValueError('datatype or name should be defined') | |
561 |
|
561 | |||
562 | #Definir una condicion para inputId cuando sea 0 |
|
562 | #Definir una condicion para inputId cuando sea 0 | |
563 |
|
563 | |||
564 | if name == None: |
|
564 | if name == None: | |
565 | if 'Proc' in datatype: |
|
565 | if 'Proc' in datatype: | |
566 | name = datatype |
|
566 | name = datatype | |
567 | else: |
|
567 | else: | |
568 | name = '%sProc' % (datatype) |
|
568 | name = '%sProc' % (datatype) | |
569 |
|
569 | |||
570 | if datatype == None: |
|
570 | if datatype == None: | |
571 | datatype = name.replace('Proc', '') |
|
571 | datatype = name.replace('Proc', '') | |
572 |
|
572 | |||
573 | self.id = str(id) |
|
573 | self.id = str(id) | |
574 | self.project_id = project_id |
|
574 | self.project_id = project_id | |
575 | self.name = name |
|
575 | self.name = name | |
576 | self.datatype = datatype |
|
576 | self.datatype = datatype | |
577 | self.inputId = inputId |
|
577 | self.inputId = inputId | |
578 | self.opConfObjList = [] |
|
578 | self.opConfObjList = [] | |
579 |
|
579 | |||
580 | self.addOperation(name='run', optype='self') |
|
580 | self.addOperation(name='run', optype='self') | |
581 |
|
581 | |||
582 | def removeOperations(self): |
|
582 | def removeOperations(self): | |
583 |
|
583 | |||
584 | for obj in self.opConfObjList: |
|
584 | for obj in self.opConfObjList: | |
585 | del obj |
|
585 | del obj | |
586 |
|
586 | |||
587 | self.opConfObjList = [] |
|
587 | self.opConfObjList = [] | |
588 | self.addOperation(name='run') |
|
588 | self.addOperation(name='run') | |
589 |
|
589 | |||
590 | def addParameter(self, **kwargs): |
|
590 | def addParameter(self, **kwargs): | |
591 | ''' |
|
591 | ''' | |
592 | Add parameters to 'run' operation |
|
592 | Add parameters to 'run' operation | |
593 | ''' |
|
593 | ''' | |
594 | opObj = self.opConfObjList[0] |
|
594 | opObj = self.opConfObjList[0] | |
595 |
|
595 | |||
596 | opObj.addParameter(**kwargs) |
|
596 | opObj.addParameter(**kwargs) | |
597 |
|
597 | |||
598 | return opObj |
|
598 | return opObj | |
599 |
|
599 | |||
600 | def addOperation(self, name, optype='self'): |
|
600 | def addOperation(self, name, optype='self'): | |
601 | ''' |
|
601 | ''' | |
602 | Actualizacion - > proceso comunicacion |
|
602 | Actualizacion - > proceso comunicacion | |
603 | En el caso de optype='self', elminar. DEfinir comuncacion IPC -> Topic |
|
603 | En el caso de optype='self', elminar. DEfinir comuncacion IPC -> Topic | |
604 | definir el tipoc de socket o comunicacion ipc++ |
|
604 | definir el tipoc de socket o comunicacion ipc++ | |
605 |
|
605 | |||
606 | ''' |
|
606 | ''' | |
607 |
|
607 | |||
608 | id = self.__getNewId() |
|
608 | id = self.__getNewId() | |
609 | priority = self.__getPriority() # Sin mucho sentido, pero puede usarse |
|
609 | priority = self.__getPriority() # Sin mucho sentido, pero puede usarse | |
610 | opConfObj = OperationConf() |
|
610 | opConfObj = OperationConf() | |
611 | opConfObj.setup(id, name=name, priority=priority, type=optype, project_id=self.project_id) |
|
611 | opConfObj.setup(id, name=name, priority=priority, type=optype, project_id=self.project_id) | |
612 | self.opConfObjList.append(opConfObj) |
|
612 | self.opConfObjList.append(opConfObj) | |
613 |
|
613 | |||
614 | return opConfObj |
|
614 | return opConfObj | |
615 |
|
615 | |||
616 | def makeXml(self, projectElement): |
|
616 | def makeXml(self, projectElement): | |
617 |
|
617 | |||
618 | procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
618 | procUnitElement = SubElement(projectElement, self.ELEMENTNAME) | |
619 | procUnitElement.set('id', str(self.id)) |
|
619 | procUnitElement.set('id', str(self.id)) | |
620 | procUnitElement.set('name', self.name) |
|
620 | procUnitElement.set('name', self.name) | |
621 | procUnitElement.set('datatype', self.datatype) |
|
621 | procUnitElement.set('datatype', self.datatype) | |
622 | procUnitElement.set('inputId', str(self.inputId)) |
|
622 | procUnitElement.set('inputId', str(self.inputId)) | |
623 |
|
623 | |||
624 | for opConfObj in self.opConfObjList: |
|
624 | for opConfObj in self.opConfObjList: | |
625 | opConfObj.makeXml(procUnitElement) |
|
625 | opConfObj.makeXml(procUnitElement) | |
626 |
|
626 | |||
627 | def readXml(self, upElement, project_id): |
|
627 | def readXml(self, upElement, project_id): | |
628 |
|
628 | |||
629 | self.id = upElement.get('id') |
|
629 | self.id = upElement.get('id') | |
630 | self.name = upElement.get('name') |
|
630 | self.name = upElement.get('name') | |
631 | self.datatype = upElement.get('datatype') |
|
631 | self.datatype = upElement.get('datatype') | |
632 | self.inputId = upElement.get('inputId') |
|
632 | self.inputId = upElement.get('inputId') | |
633 | self.project_id = str(project_id) |
|
633 | self.project_id = str(project_id) | |
634 |
|
634 | |||
635 | if self.ELEMENTNAME == 'ReadUnit': |
|
635 | if self.ELEMENTNAME == 'ReadUnit': | |
636 | self.datatype = self.datatype.replace('Reader', '') |
|
636 | self.datatype = self.datatype.replace('Reader', '') | |
637 |
|
637 | |||
638 | if self.ELEMENTNAME == 'ProcUnit': |
|
638 | if self.ELEMENTNAME == 'ProcUnit': | |
639 | self.datatype = self.datatype.replace('Proc', '') |
|
639 | self.datatype = self.datatype.replace('Proc', '') | |
640 |
|
640 | |||
641 | if self.inputId == 'None': |
|
641 | if self.inputId == 'None': | |
642 | self.inputId = '0' |
|
642 | self.inputId = '0' | |
643 |
|
643 | |||
644 | self.opConfObjList = [] |
|
644 | self.opConfObjList = [] | |
645 |
|
645 | |||
646 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
646 | opElementList = upElement.iter(OperationConf().getElementName()) | |
647 |
|
647 | |||
648 | for opElement in opElementList: |
|
648 | for opElement in opElementList: | |
649 | opConfObj = OperationConf() |
|
649 | opConfObj = OperationConf() | |
650 | opConfObj.readXml(opElement, project_id) |
|
650 | opConfObj.readXml(opElement, project_id) | |
651 | self.opConfObjList.append(opConfObj) |
|
651 | self.opConfObjList.append(opConfObj) | |
652 |
|
652 | |||
653 | def printattr(self): |
|
653 | def printattr(self): | |
654 |
|
654 | |||
655 | print('%s[%s]: name = %s, datatype = %s, inputId = %s, project_id = %s' % (self.ELEMENTNAME, |
|
655 | print('%s[%s]: name = %s, datatype = %s, inputId = %s, project_id = %s' % (self.ELEMENTNAME, | |
656 | self.id, |
|
656 | self.id, | |
657 | self.name, |
|
657 | self.name, | |
658 | self.datatype, |
|
658 | self.datatype, | |
659 | self.inputId, |
|
659 | self.inputId, | |
660 | self.project_id)) |
|
660 | self.project_id)) | |
661 |
|
661 | |||
662 | for opConfObj in self.opConfObjList: |
|
662 | for opConfObj in self.opConfObjList: | |
663 | opConfObj.printattr() |
|
663 | opConfObj.printattr() | |
664 |
|
664 | |||
665 | def getKwargs(self): |
|
665 | def getKwargs(self): | |
666 |
|
666 | |||
667 | opObj = self.opConfObjList[0] |
|
667 | opObj = self.opConfObjList[0] | |
668 | kwargs = opObj.getKwargs() |
|
668 | kwargs = opObj.getKwargs() | |
669 |
|
669 | |||
670 | return kwargs |
|
670 | return kwargs | |
671 |
|
671 | |||
672 | def createObjects(self): |
|
672 | def createObjects(self): | |
673 | ''' |
|
673 | ''' | |
674 | Instancia de unidades de procesamiento. |
|
674 | Instancia de unidades de procesamiento. | |
675 | ''' |
|
675 | ''' | |
676 |
|
676 | |||
677 | className = eval(self.name) |
|
677 | className = eval(self.name) | |
678 | kwargs = self.getKwargs() |
|
678 | kwargs = self.getKwargs() | |
679 | procUnitObj = className(self.id, self.inputId, self.project_id, **kwargs) # necesitan saber su id y su entrada por fines de ipc |
|
679 | procUnitObj = className(self.id, self.inputId, self.project_id, **kwargs) # necesitan saber su id y su entrada por fines de ipc | |
680 | log.success('creating process...', self.name) |
|
680 | log.success('creating process...', self.name) | |
681 |
|
681 | |||
682 | for opConfObj in self.opConfObjList: |
|
682 | for opConfObj in self.opConfObjList: | |
683 |
|
683 | |||
684 | if opConfObj.type == 'self' and opConfObj.name == 'run': |
|
684 | if opConfObj.type == 'self' and opConfObj.name == 'run': | |
685 | continue |
|
685 | continue | |
686 | elif opConfObj.type == 'self': |
|
686 | elif opConfObj.type == 'self': | |
687 | opObj = getattr(procUnitObj, opConfObj.name) |
|
687 | opObj = getattr(procUnitObj, opConfObj.name) | |
688 | else: |
|
688 | else: | |
689 | opObj = opConfObj.createObject() |
|
689 | opObj = opConfObj.createObject() | |
690 |
|
690 | |||
691 | log.success('creating operation: {}, type:{}'.format( |
|
691 | log.success('creating operation: {}, type:{}'.format( | |
692 | opConfObj.name, |
|
692 | opConfObj.name, | |
693 | opConfObj.type), self.name) |
|
693 | opConfObj.type), self.name) | |
694 |
|
694 | |||
695 | procUnitObj.addOperation(opConfObj, opObj) |
|
695 | procUnitObj.addOperation(opConfObj, opObj) | |
696 |
|
696 | |||
697 | procUnitObj.start() |
|
697 | procUnitObj.start() | |
698 | self.procUnitObj = procUnitObj |
|
698 | self.procUnitObj = procUnitObj | |
699 |
|
699 | |||
700 | def close(self): |
|
700 | def close(self): | |
701 |
|
701 | |||
702 | for opConfObj in self.opConfObjList: |
|
702 | for opConfObj in self.opConfObjList: | |
703 | if opConfObj.type == 'self': |
|
703 | if opConfObj.type == 'self': | |
704 | continue |
|
704 | continue | |
705 |
|
705 | |||
706 | opObj = self.procUnitObj.getOperationObj(opConfObj.id) |
|
706 | opObj = self.procUnitObj.getOperationObj(opConfObj.id) | |
707 | opObj.close() |
|
707 | opObj.close() | |
708 |
|
708 | |||
709 | self.procUnitObj.close() |
|
709 | self.procUnitObj.close() | |
710 |
|
710 | |||
711 | return |
|
711 | return | |
712 |
|
712 | |||
713 |
|
713 | |||
714 | class ReadUnitConf(ProcUnitConf): |
|
714 | class ReadUnitConf(ProcUnitConf): | |
715 |
|
715 | |||
716 | ELEMENTNAME = 'ReadUnit' |
|
716 | ELEMENTNAME = 'ReadUnit' | |
717 |
|
717 | |||
718 | def __init__(self): |
|
718 | def __init__(self): | |
719 |
|
719 | |||
720 | self.id = None |
|
720 | self.id = None | |
721 | self.datatype = None |
|
721 | self.datatype = None | |
722 | self.name = None |
|
722 | self.name = None | |
723 | self.inputId = None |
|
723 | self.inputId = None | |
724 | self.opConfObjList = [] |
|
724 | self.opConfObjList = [] | |
725 |
|
725 | |||
726 | def getElementName(self): |
|
726 | def getElementName(self): | |
727 |
|
727 | |||
728 | return self.ELEMENTNAME |
|
728 | return self.ELEMENTNAME | |
729 |
|
729 | |||
730 | def setup(self, project_id, id, name, datatype, path='', startDate='', endDate='', |
|
730 | def setup(self, project_id, id, name, datatype, path='', startDate='', endDate='', | |
731 | startTime='', endTime='', server=None, **kwargs): |
|
731 | startTime='', endTime='', server=None, **kwargs): | |
732 |
|
732 | |||
733 |
|
733 | |||
734 | ''' |
|
734 | ''' | |
735 | *****el id del proceso sera el Topico |
|
735 | *****el id del proceso sera el Topico | |
736 |
|
736 | |||
737 | Adicion de {topic}, si no esta presente -> error |
|
737 | Adicion de {topic}, si no esta presente -> error | |
738 | kwargs deben ser trasmitidos en la instanciacion |
|
738 | kwargs deben ser trasmitidos en la instanciacion | |
739 |
|
739 | |||
740 | ''' |
|
740 | ''' | |
741 |
|
741 | |||
742 | # Compatible with old signal chain version |
|
742 | # Compatible with old signal chain version | |
743 | if datatype == None and name == None: |
|
743 | if datatype == None and name == None: | |
744 | raise ValueError('datatype or name should be defined') |
|
744 | raise ValueError('datatype or name should be defined') | |
745 | if name == None: |
|
745 | if name == None: | |
746 | if 'Reader' in datatype: |
|
746 | if 'Reader' in datatype: | |
747 | name = datatype |
|
747 | name = datatype | |
748 | datatype = name.replace('Reader','') |
|
748 | datatype = name.replace('Reader','') | |
749 | else: |
|
749 | else: | |
750 | name = '{}Reader'.format(datatype) |
|
750 | name = '{}Reader'.format(datatype) | |
751 | if datatype == None: |
|
751 | if datatype == None: | |
752 | if 'Reader' in name: |
|
752 | if 'Reader' in name: | |
753 | datatype = name.replace('Reader','') |
|
753 | datatype = name.replace('Reader','') | |
754 | else: |
|
754 | else: | |
755 | datatype = name |
|
755 | datatype = name | |
756 | name = '{}Reader'.format(name) |
|
756 | name = '{}Reader'.format(name) | |
757 |
|
757 | |||
758 | self.id = id |
|
758 | self.id = id | |
759 | self.project_id = project_id |
|
759 | self.project_id = project_id | |
760 | self.name = name |
|
760 | self.name = name | |
761 | self.datatype = datatype |
|
761 | self.datatype = datatype | |
762 | if path != '': |
|
762 | if path != '': | |
763 | self.path = os.path.abspath(path) |
|
763 | self.path = os.path.abspath(path) | |
764 | self.startDate = startDate |
|
764 | self.startDate = startDate | |
765 | self.endDate = endDate |
|
765 | self.endDate = endDate | |
766 | self.startTime = startTime |
|
766 | self.startTime = startTime | |
767 | self.endTime = endTime |
|
767 | self.endTime = endTime | |
768 | self.server = server |
|
768 | self.server = server | |
769 | self.addRunOperation(**kwargs) |
|
769 | self.addRunOperation(**kwargs) | |
770 |
|
770 | |||
771 | def update(self, **kwargs): |
|
771 | def update(self, **kwargs): | |
772 |
|
772 | |||
773 | if 'datatype' in kwargs: |
|
773 | if 'datatype' in kwargs: | |
774 | datatype = kwargs.pop('datatype') |
|
774 | datatype = kwargs.pop('datatype') | |
775 | if 'Reader' in datatype: |
|
775 | if 'Reader' in datatype: | |
776 | self.name = datatype |
|
776 | self.name = datatype | |
777 | else: |
|
777 | else: | |
778 | self.name = '%sReader' % (datatype) |
|
778 | self.name = '%sReader' % (datatype) | |
779 | self.datatype = self.name.replace('Reader', '') |
|
779 | self.datatype = self.name.replace('Reader', '') | |
780 |
|
780 | |||
781 | attrs = ('path', 'startDate', 'endDate', |
|
781 | attrs = ('path', 'startDate', 'endDate', | |
782 | 'startTime', 'endTime') |
|
782 | 'startTime', 'endTime') | |
783 |
|
783 | |||
784 | for attr in attrs: |
|
784 | for attr in attrs: | |
785 | if attr in kwargs: |
|
785 | if attr in kwargs: | |
786 | setattr(self, attr, kwargs.pop(attr)) |
|
786 | setattr(self, attr, kwargs.pop(attr)) | |
787 |
|
787 | |||
788 | self.updateRunOperation(**kwargs) |
|
788 | self.updateRunOperation(**kwargs) | |
789 |
|
789 | |||
790 | def removeOperations(self): |
|
790 | def removeOperations(self): | |
791 |
|
791 | |||
792 | for obj in self.opConfObjList: |
|
792 | for obj in self.opConfObjList: | |
793 | del obj |
|
793 | del obj | |
794 |
|
794 | |||
795 | self.opConfObjList = [] |
|
795 | self.opConfObjList = [] | |
796 |
|
796 | |||
797 | def addRunOperation(self, **kwargs): |
|
797 | def addRunOperation(self, **kwargs): | |
798 |
|
798 | |||
799 | opObj = self.addOperation(name='run', optype='self') |
|
799 | opObj = self.addOperation(name='run', optype='self') | |
800 |
|
800 | |||
801 | if self.server is None: |
|
801 | if self.server is None: | |
802 | opObj.addParameter( |
|
802 | opObj.addParameter( | |
803 | name='datatype', value=self.datatype, format='str') |
|
803 | name='datatype', value=self.datatype, format='str') | |
804 | opObj.addParameter(name='path', value=self.path, format='str') |
|
804 | opObj.addParameter(name='path', value=self.path, format='str') | |
805 | opObj.addParameter( |
|
805 | opObj.addParameter( | |
806 | name='startDate', value=self.startDate, format='date') |
|
806 | name='startDate', value=self.startDate, format='date') | |
807 | opObj.addParameter( |
|
807 | opObj.addParameter( | |
808 | name='endDate', value=self.endDate, format='date') |
|
808 | name='endDate', value=self.endDate, format='date') | |
809 | opObj.addParameter( |
|
809 | opObj.addParameter( | |
810 | name='startTime', value=self.startTime, format='time') |
|
810 | name='startTime', value=self.startTime, format='time') | |
811 | opObj.addParameter( |
|
811 | opObj.addParameter( | |
812 | name='endTime', value=self.endTime, format='time') |
|
812 | name='endTime', value=self.endTime, format='time') | |
813 |
|
813 | |||
814 | for key, value in list(kwargs.items()): |
|
814 | for key, value in list(kwargs.items()): | |
815 | opObj.addParameter(name=key, value=value, |
|
815 | opObj.addParameter(name=key, value=value, | |
816 | format=type(value).__name__) |
|
816 | format=type(value).__name__) | |
817 | else: |
|
817 | else: | |
818 | opObj.addParameter(name='server', value=self.server, format='str') |
|
818 | opObj.addParameter(name='server', value=self.server, format='str') | |
819 |
|
819 | |||
820 | return opObj |
|
820 | return opObj | |
821 |
|
821 | |||
822 | def updateRunOperation(self, **kwargs): |
|
822 | def updateRunOperation(self, **kwargs): | |
823 |
|
823 | |||
824 | opObj = self.getOperationObj(name='run') |
|
824 | opObj = self.getOperationObj(name='run') | |
825 | opObj.removeParameters() |
|
825 | opObj.removeParameters() | |
826 |
|
826 | |||
827 | opObj.addParameter(name='datatype', value=self.datatype, format='str') |
|
827 | opObj.addParameter(name='datatype', value=self.datatype, format='str') | |
828 | opObj.addParameter(name='path', value=self.path, format='str') |
|
828 | opObj.addParameter(name='path', value=self.path, format='str') | |
829 | opObj.addParameter( |
|
829 | opObj.addParameter( | |
830 | name='startDate', value=self.startDate, format='date') |
|
830 | name='startDate', value=self.startDate, format='date') | |
831 | opObj.addParameter(name='endDate', value=self.endDate, format='date') |
|
831 | opObj.addParameter(name='endDate', value=self.endDate, format='date') | |
832 | opObj.addParameter( |
|
832 | opObj.addParameter( | |
833 | name='startTime', value=self.startTime, format='time') |
|
833 | name='startTime', value=self.startTime, format='time') | |
834 | opObj.addParameter(name='endTime', value=self.endTime, format='time') |
|
834 | opObj.addParameter(name='endTime', value=self.endTime, format='time') | |
835 |
|
835 | |||
836 | for key, value in list(kwargs.items()): |
|
836 | for key, value in list(kwargs.items()): | |
837 | opObj.addParameter(name=key, value=value, |
|
837 | opObj.addParameter(name=key, value=value, | |
838 | format=type(value).__name__) |
|
838 | format=type(value).__name__) | |
839 |
|
839 | |||
840 | return opObj |
|
840 | return opObj | |
841 |
|
841 | |||
842 | def readXml(self, upElement, project_id): |
|
842 | def readXml(self, upElement, project_id): | |
843 |
|
843 | |||
844 | self.id = upElement.get('id') |
|
844 | self.id = upElement.get('id') | |
845 | self.name = upElement.get('name') |
|
845 | self.name = upElement.get('name') | |
846 | self.datatype = upElement.get('datatype') |
|
846 | self.datatype = upElement.get('datatype') | |
847 | self.project_id = str(project_id) #yong |
|
847 | self.project_id = str(project_id) #yong | |
848 |
|
848 | |||
849 | if self.ELEMENTNAME == 'ReadUnit': |
|
849 | if self.ELEMENTNAME == 'ReadUnit': | |
850 | self.datatype = self.datatype.replace('Reader', '') |
|
850 | self.datatype = self.datatype.replace('Reader', '') | |
851 |
|
851 | |||
852 | self.opConfObjList = [] |
|
852 | self.opConfObjList = [] | |
853 |
|
853 | |||
854 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
854 | opElementList = upElement.iter(OperationConf().getElementName()) | |
855 |
|
855 | |||
856 | for opElement in opElementList: |
|
856 | for opElement in opElementList: | |
857 | opConfObj = OperationConf() |
|
857 | opConfObj = OperationConf() | |
858 | opConfObj.readXml(opElement, project_id) |
|
858 | opConfObj.readXml(opElement, project_id) | |
859 | self.opConfObjList.append(opConfObj) |
|
859 | self.opConfObjList.append(opConfObj) | |
860 |
|
860 | |||
861 | if opConfObj.name == 'run': |
|
861 | if opConfObj.name == 'run': | |
862 | self.path = opConfObj.getParameterValue('path') |
|
862 | self.path = opConfObj.getParameterValue('path') | |
863 | self.startDate = opConfObj.getParameterValue('startDate') |
|
863 | self.startDate = opConfObj.getParameterValue('startDate') | |
864 | self.endDate = opConfObj.getParameterValue('endDate') |
|
864 | self.endDate = opConfObj.getParameterValue('endDate') | |
865 | self.startTime = opConfObj.getParameterValue('startTime') |
|
865 | self.startTime = opConfObj.getParameterValue('startTime') | |
866 | self.endTime = opConfObj.getParameterValue('endTime') |
|
866 | self.endTime = opConfObj.getParameterValue('endTime') | |
867 |
|
867 | |||
868 |
|
868 | |||
869 | class Project(Process): |
|
869 | class Project(Process): | |
870 |
|
870 | |||
871 | ELEMENTNAME = 'Project' |
|
871 | ELEMENTNAME = 'Project' | |
872 |
|
872 | |||
873 | def __init__(self): |
|
873 | def __init__(self): | |
874 |
|
874 | |||
875 | Process.__init__(self) |
|
875 | Process.__init__(self) | |
876 | self.id = None |
|
876 | self.id = None | |
877 | self.filename = None |
|
877 | self.filename = None | |
878 | self.description = None |
|
878 | self.description = None | |
879 | self.email = None |
|
879 | self.email = None | |
880 | self.alarm = None |
|
880 | self.alarm = None | |
881 | self.procUnitConfObjDict = {} |
|
881 | self.procUnitConfObjDict = {} | |
882 |
|
882 | |||
883 | def __getNewId(self): |
|
883 | def __getNewId(self): | |
884 |
|
884 | |||
885 | idList = list(self.procUnitConfObjDict.keys()) |
|
885 | idList = list(self.procUnitConfObjDict.keys()) | |
886 | id = int(self.id) * 10 |
|
886 | id = int(self.id) * 10 | |
887 |
|
887 | |||
888 | while True: |
|
888 | while True: | |
889 | id += 1 |
|
889 | id += 1 | |
890 |
|
890 | |||
891 | if str(id) in idList: |
|
891 | if str(id) in idList: | |
892 | continue |
|
892 | continue | |
893 |
|
893 | |||
894 | break |
|
894 | break | |
895 |
|
895 | |||
896 | return str(id) |
|
896 | return str(id) | |
897 |
|
897 | |||
898 | def getElementName(self): |
|
898 | def getElementName(self): | |
899 |
|
899 | |||
900 | return self.ELEMENTNAME |
|
900 | return self.ELEMENTNAME | |
901 |
|
901 | |||
902 | def getId(self): |
|
902 | def getId(self): | |
903 |
|
903 | |||
904 | return self.id |
|
904 | return self.id | |
905 |
|
905 | |||
906 | def updateId(self, new_id): |
|
906 | def updateId(self, new_id): | |
907 |
|
907 | |||
908 | self.id = str(new_id) |
|
908 | self.id = str(new_id) | |
909 |
|
909 | |||
910 | keyList = list(self.procUnitConfObjDict.keys()) |
|
910 | keyList = list(self.procUnitConfObjDict.keys()) | |
911 | keyList.sort() |
|
911 | keyList.sort() | |
912 |
|
912 | |||
913 | n = 1 |
|
913 | n = 1 | |
914 | newProcUnitConfObjDict = {} |
|
914 | newProcUnitConfObjDict = {} | |
915 |
|
915 | |||
916 | for procKey in keyList: |
|
916 | for procKey in keyList: | |
917 |
|
917 | |||
918 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
918 | procUnitConfObj = self.procUnitConfObjDict[procKey] | |
919 | idProcUnit = str(int(self.id) * 10 + n) |
|
919 | idProcUnit = str(int(self.id) * 10 + n) | |
920 | procUnitConfObj.updateId(idProcUnit) |
|
920 | procUnitConfObj.updateId(idProcUnit) | |
921 | newProcUnitConfObjDict[idProcUnit] = procUnitConfObj |
|
921 | newProcUnitConfObjDict[idProcUnit] = procUnitConfObj | |
922 | n += 1 |
|
922 | n += 1 | |
923 |
|
923 | |||
924 | self.procUnitConfObjDict = newProcUnitConfObjDict |
|
924 | self.procUnitConfObjDict = newProcUnitConfObjDict | |
925 |
|
925 | |||
926 | def setup(self, id=1, name='', description='', email=None, alarm=[]): |
|
926 | def setup(self, id=1, name='', description='', email=None, alarm=[]): | |
927 |
|
927 | |||
928 | print(' ') |
|
928 | print(' ') | |
929 | print('*' * 60) |
|
929 | print('*' * 60) | |
930 | print('* Starting SIGNAL CHAIN PROCESSING (Multiprocessing) v%s *' % schainpy.__version__) |
|
930 | print('* Starting SIGNAL CHAIN PROCESSING (Multiprocessing) v%s *' % schainpy.__version__) | |
931 | print('*' * 60) |
|
931 | print('*' * 60) | |
932 | print("* Python " + python_version() + " *") |
|
932 | print("* Python " + python_version() + " *") | |
933 | print('*' * 19) |
|
933 | print('*' * 19) | |
934 | print(' ') |
|
934 | print(' ') | |
935 | self.id = str(id) |
|
935 | self.id = str(id) | |
936 | self.description = description |
|
936 | self.description = description | |
937 | self.email = email |
|
937 | self.email = email | |
938 | self.alarm = alarm |
|
938 | self.alarm = alarm | |
939 |
|
939 | |||
940 | def update(self, **kwargs): |
|
940 | def update(self, **kwargs): | |
941 |
|
941 | |||
942 | for key, value in list(kwargs.items()): |
|
942 | for key, value in list(kwargs.items()): | |
943 | setattr(self, key, value) |
|
943 | setattr(self, key, value) | |
944 |
|
944 | |||
945 | def clone(self): |
|
945 | def clone(self): | |
946 |
|
946 | |||
947 | p = Project() |
|
947 | p = Project() | |
948 | p.procUnitConfObjDict = self.procUnitConfObjDict |
|
948 | p.procUnitConfObjDict = self.procUnitConfObjDict | |
949 | return p |
|
949 | return p | |
950 |
|
950 | |||
951 | def addReadUnit(self, id=None, datatype=None, name=None, **kwargs): |
|
951 | def addReadUnit(self, id=None, datatype=None, name=None, **kwargs): | |
952 |
|
952 | |||
953 | ''' |
|
953 | ''' | |
954 | Actualizacion: |
|
954 | Actualizacion: | |
955 | Se agrego un nuevo argumento: topic -relativo a la forma de comunicar los procesos simultaneos |
|
955 | Se agrego un nuevo argumento: topic -relativo a la forma de comunicar los procesos simultaneos | |
956 |
|
956 | |||
957 | * El id del proceso sera el topico al que se deben subscribir los procUnits para recibir la informacion(data) |
|
957 | * El id del proceso sera el topico al que se deben subscribir los procUnits para recibir la informacion(data) | |
958 |
|
958 | |||
959 | ''' |
|
959 | ''' | |
960 |
|
960 | |||
961 | if id is None: |
|
961 | if id is None: | |
962 | idReadUnit = self.__getNewId() |
|
962 | idReadUnit = self.__getNewId() | |
963 | else: |
|
963 | else: | |
964 | idReadUnit = str(id) |
|
964 | idReadUnit = str(id) | |
965 |
|
965 | |||
966 | readUnitConfObj = ReadUnitConf() |
|
966 | readUnitConfObj = ReadUnitConf() | |
967 | readUnitConfObj.setup(self.id, idReadUnit, name, datatype, **kwargs) |
|
967 | readUnitConfObj.setup(self.id, idReadUnit, name, datatype, **kwargs) | |
968 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
968 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj | |
969 |
|
969 | |||
970 | return readUnitConfObj |
|
970 | return readUnitConfObj | |
971 |
|
971 | |||
972 | def addProcUnit(self, inputId='0', datatype=None, name=None): |
|
972 | def addProcUnit(self, inputId='0', datatype=None, name=None): | |
973 |
|
973 | |||
974 | ''' |
|
974 | ''' | |
975 | Actualizacion: |
|
975 | Actualizacion: | |
976 | Se agrego dos nuevos argumentos: topic_read (lee data de otro procUnit) y topic_write(escribe o envia data a otro procUnit) |
|
976 | Se agrego dos nuevos argumentos: topic_read (lee data de otro procUnit) y topic_write(escribe o envia data a otro procUnit) | |
977 | Deberia reemplazar a "inputId" |
|
977 | Deberia reemplazar a "inputId" | |
978 |
|
978 | |||
979 | ** A fin de mantener el inputID, este sera la representaacion del topicoal que deben subscribirse. El ID propio de la intancia |
|
979 | ** A fin de mantener el inputID, este sera la representaacion del topicoal que deben subscribirse. El ID propio de la intancia | |
980 | (proceso) sera el topico de la publicacion, todo sera asignado de manera dinamica. |
|
980 | (proceso) sera el topico de la publicacion, todo sera asignado de manera dinamica. | |
981 |
|
981 | |||
982 | ''' |
|
982 | ''' | |
983 |
|
983 | |||
984 | idProcUnit = self.__getNewId() #Topico para subscripcion |
|
984 | idProcUnit = self.__getNewId() #Topico para subscripcion | |
985 | procUnitConfObj = ProcUnitConf() |
|
985 | procUnitConfObj = ProcUnitConf() | |
986 | procUnitConfObj.setup(self.id, idProcUnit, name, datatype, inputId) #topic_read, topic_write, |
|
986 | procUnitConfObj.setup(self.id, idProcUnit, name, datatype, inputId) #topic_read, topic_write, | |
987 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
987 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj | |
988 |
|
988 | |||
989 | return procUnitConfObj |
|
989 | return procUnitConfObj | |
990 |
|
990 | |||
991 | def removeProcUnit(self, id): |
|
991 | def removeProcUnit(self, id): | |
992 |
|
992 | |||
993 | if id in list(self.procUnitConfObjDict.keys()): |
|
993 | if id in list(self.procUnitConfObjDict.keys()): | |
994 | self.procUnitConfObjDict.pop(id) |
|
994 | self.procUnitConfObjDict.pop(id) | |
995 |
|
995 | |||
996 | def getReadUnitId(self): |
|
996 | def getReadUnitId(self): | |
997 |
|
997 | |||
998 | readUnitConfObj = self.getReadUnitObj() |
|
998 | readUnitConfObj = self.getReadUnitObj() | |
999 |
|
999 | |||
1000 | return readUnitConfObj.id |
|
1000 | return readUnitConfObj.id | |
1001 |
|
1001 | |||
1002 | def getReadUnitObj(self): |
|
1002 | def getReadUnitObj(self): | |
1003 |
|
1003 | |||
1004 | for obj in list(self.procUnitConfObjDict.values()): |
|
1004 | for obj in list(self.procUnitConfObjDict.values()): | |
1005 | if obj.getElementName() == 'ReadUnit': |
|
1005 | if obj.getElementName() == 'ReadUnit': | |
1006 | return obj |
|
1006 | return obj | |
1007 |
|
1007 | |||
1008 | return None |
|
1008 | return None | |
1009 |
|
1009 | |||
1010 | def getProcUnitObj(self, id=None, name=None): |
|
1010 | def getProcUnitObj(self, id=None, name=None): | |
1011 |
|
1011 | |||
1012 | if id != None: |
|
1012 | if id != None: | |
1013 | return self.procUnitConfObjDict[id] |
|
1013 | return self.procUnitConfObjDict[id] | |
1014 |
|
1014 | |||
1015 | if name != None: |
|
1015 | if name != None: | |
1016 | return self.getProcUnitObjByName(name) |
|
1016 | return self.getProcUnitObjByName(name) | |
1017 |
|
1017 | |||
1018 | return None |
|
1018 | return None | |
1019 |
|
1019 | |||
1020 | def getProcUnitObjByName(self, name): |
|
1020 | def getProcUnitObjByName(self, name): | |
1021 |
|
1021 | |||
1022 | for obj in list(self.procUnitConfObjDict.values()): |
|
1022 | for obj in list(self.procUnitConfObjDict.values()): | |
1023 | if obj.name == name: |
|
1023 | if obj.name == name: | |
1024 | return obj |
|
1024 | return obj | |
1025 |
|
1025 | |||
1026 | return None |
|
1026 | return None | |
1027 |
|
1027 | |||
1028 | def procUnitItems(self): |
|
1028 | def procUnitItems(self): | |
1029 |
|
1029 | |||
1030 | return list(self.procUnitConfObjDict.items()) |
|
1030 | return list(self.procUnitConfObjDict.items()) | |
1031 |
|
1031 | |||
1032 | def makeXml(self): |
|
1032 | def makeXml(self): | |
1033 |
|
1033 | |||
1034 | projectElement = Element('Project') |
|
1034 | projectElement = Element('Project') | |
1035 | projectElement.set('id', str(self.id)) |
|
1035 | projectElement.set('id', str(self.id)) | |
1036 | projectElement.set('name', self.name) |
|
1036 | projectElement.set('name', self.name) | |
1037 | projectElement.set('description', self.description) |
|
1037 | projectElement.set('description', self.description) | |
1038 |
|
1038 | |||
1039 | for procUnitConfObj in list(self.procUnitConfObjDict.values()): |
|
1039 | for procUnitConfObj in list(self.procUnitConfObjDict.values()): | |
1040 | procUnitConfObj.makeXml(projectElement) |
|
1040 | procUnitConfObj.makeXml(projectElement) | |
1041 |
|
1041 | |||
1042 | self.projectElement = projectElement |
|
1042 | self.projectElement = projectElement | |
1043 |
|
1043 | |||
1044 | def writeXml(self, filename=None): |
|
1044 | def writeXml(self, filename=None): | |
1045 |
|
1045 | |||
1046 | if filename == None: |
|
1046 | if filename == None: | |
1047 | if self.filename: |
|
1047 | if self.filename: | |
1048 | filename = self.filename |
|
1048 | filename = self.filename | |
1049 | else: |
|
1049 | else: | |
1050 | filename = 'schain.xml' |
|
1050 | filename = 'schain.xml' | |
1051 |
|
1051 | |||
1052 | if not filename: |
|
1052 | if not filename: | |
1053 | print('filename has not been defined. Use setFilename(filename) for do it.') |
|
1053 | print('filename has not been defined. Use setFilename(filename) for do it.') | |
1054 | return 0 |
|
1054 | return 0 | |
1055 |
|
1055 | |||
1056 | abs_file = os.path.abspath(filename) |
|
1056 | abs_file = os.path.abspath(filename) | |
1057 |
|
1057 | |||
1058 | if not os.access(os.path.dirname(abs_file), os.W_OK): |
|
1058 | if not os.access(os.path.dirname(abs_file), os.W_OK): | |
1059 | print('No write permission on %s' % os.path.dirname(abs_file)) |
|
1059 | print('No write permission on %s' % os.path.dirname(abs_file)) | |
1060 | return 0 |
|
1060 | return 0 | |
1061 |
|
1061 | |||
1062 | if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)): |
|
1062 | if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)): | |
1063 | print('File %s already exists and it could not be overwriten' % abs_file) |
|
1063 | print('File %s already exists and it could not be overwriten' % abs_file) | |
1064 | return 0 |
|
1064 | return 0 | |
1065 |
|
1065 | |||
1066 | self.makeXml() |
|
1066 | self.makeXml() | |
1067 |
|
1067 | |||
1068 | ElementTree(self.projectElement).write(abs_file, method='xml') |
|
1068 | ElementTree(self.projectElement).write(abs_file, method='xml') | |
1069 |
|
1069 | |||
1070 | self.filename = abs_file |
|
1070 | self.filename = abs_file | |
1071 |
|
1071 | |||
1072 | return 1 |
|
1072 | return 1 | |
1073 |
|
1073 | |||
1074 | def readXml(self, filename=None): |
|
1074 | def readXml(self, filename=None): | |
1075 |
|
1075 | |||
1076 | if not filename: |
|
1076 | if not filename: | |
1077 | print('filename is not defined') |
|
1077 | print('filename is not defined') | |
1078 | return 0 |
|
1078 | return 0 | |
1079 |
|
1079 | |||
1080 | abs_file = os.path.abspath(filename) |
|
1080 | abs_file = os.path.abspath(filename) | |
1081 |
|
1081 | |||
1082 | if not os.path.isfile(abs_file): |
|
1082 | if not os.path.isfile(abs_file): | |
1083 | print('%s file does not exist' % abs_file) |
|
1083 | print('%s file does not exist' % abs_file) | |
1084 | return 0 |
|
1084 | return 0 | |
1085 |
|
1085 | |||
1086 | self.projectElement = None |
|
1086 | self.projectElement = None | |
1087 | self.procUnitConfObjDict = {} |
|
1087 | self.procUnitConfObjDict = {} | |
1088 |
|
1088 | |||
1089 | try: |
|
1089 | try: | |
1090 | self.projectElement = ElementTree().parse(abs_file) |
|
1090 | self.projectElement = ElementTree().parse(abs_file) | |
1091 | except: |
|
1091 | except: | |
1092 | print('Error reading %s, verify file format' % filename) |
|
1092 | print('Error reading %s, verify file format' % filename) | |
1093 | return 0 |
|
1093 | return 0 | |
1094 |
|
1094 | |||
1095 | self.project = self.projectElement.tag |
|
1095 | self.project = self.projectElement.tag | |
1096 |
|
1096 | |||
1097 | self.id = self.projectElement.get('id') |
|
1097 | self.id = self.projectElement.get('id') | |
1098 | self.name = self.projectElement.get('name') |
|
1098 | self.name = self.projectElement.get('name') | |
1099 | self.description = self.projectElement.get('description') |
|
1099 | self.description = self.projectElement.get('description') | |
1100 |
|
1100 | |||
1101 | readUnitElementList = self.projectElement.iter( |
|
1101 | readUnitElementList = self.projectElement.iter( | |
1102 | ReadUnitConf().getElementName()) |
|
1102 | ReadUnitConf().getElementName()) | |
1103 |
|
1103 | |||
1104 | for readUnitElement in readUnitElementList: |
|
1104 | for readUnitElement in readUnitElementList: | |
1105 | readUnitConfObj = ReadUnitConf() |
|
1105 | readUnitConfObj = ReadUnitConf() | |
1106 | readUnitConfObj.readXml(readUnitElement, self.id) |
|
1106 | readUnitConfObj.readXml(readUnitElement, self.id) | |
1107 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
1107 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj | |
1108 |
|
1108 | |||
1109 | procUnitElementList = self.projectElement.iter( |
|
1109 | procUnitElementList = self.projectElement.iter( | |
1110 | ProcUnitConf().getElementName()) |
|
1110 | ProcUnitConf().getElementName()) | |
1111 |
|
1111 | |||
1112 | for procUnitElement in procUnitElementList: |
|
1112 | for procUnitElement in procUnitElementList: | |
1113 | procUnitConfObj = ProcUnitConf() |
|
1113 | procUnitConfObj = ProcUnitConf() | |
1114 | procUnitConfObj.readXml(procUnitElement, self.id) |
|
1114 | procUnitConfObj.readXml(procUnitElement, self.id) | |
1115 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1115 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj | |
1116 |
|
1116 | |||
1117 | self.filename = abs_file |
|
1117 | self.filename = abs_file | |
1118 |
|
1118 | |||
1119 | return 1 |
|
1119 | return 1 | |
1120 |
|
1120 | |||
1121 | def __str__(self): |
|
1121 | def __str__(self): | |
1122 |
|
1122 | |||
1123 | print('Project[%s]: name = %s, description = %s, project_id = %s' % (self.id, |
|
1123 | print('Project[%s]: name = %s, description = %s, project_id = %s' % (self.id, | |
1124 | self.name, |
|
1124 | self.name, | |
1125 | self.description, |
|
1125 | self.description, | |
1126 | self.project_id)) |
|
1126 | self.project_id)) | |
1127 |
|
1127 | |||
1128 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1128 | for procUnitConfObj in self.procUnitConfObjDict.values(): | |
1129 | print(procUnitConfObj) |
|
1129 | print(procUnitConfObj) | |
1130 |
|
1130 | |||
1131 | def createObjects(self): |
|
1131 | def createObjects(self): | |
1132 |
|
1132 | |||
1133 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1133 | ||
1134 |
|
|
1134 | keys = list(self.procUnitConfObjDict.keys()) | |
|
1135 | keys.sort() | |||
|
1136 | for key in keys: | |||
|
1137 | self.procUnitConfObjDict[key].createObjects() | |||
1135 |
|
1138 | |||
1136 | def __handleError(self, procUnitConfObj, modes=None, stdout=True): |
|
1139 | def __handleError(self, procUnitConfObj, modes=None, stdout=True): | |
1137 |
|
1140 | |||
1138 | import socket |
|
1141 | import socket | |
1139 |
|
1142 | |||
1140 | if modes is None: |
|
1143 | if modes is None: | |
1141 | modes = self.alarm |
|
1144 | modes = self.alarm | |
1142 |
|
1145 | |||
1143 | if not self.alarm: |
|
1146 | if not self.alarm: | |
1144 | modes = [] |
|
1147 | modes = [] | |
1145 |
|
1148 | |||
1146 | err = traceback.format_exception(sys.exc_info()[0], |
|
1149 | err = traceback.format_exception(sys.exc_info()[0], | |
1147 | sys.exc_info()[1], |
|
1150 | sys.exc_info()[1], | |
1148 | sys.exc_info()[2]) |
|
1151 | sys.exc_info()[2]) | |
1149 |
|
1152 | |||
1150 | log.error('{}'.format(err[-1]), procUnitConfObj.name) |
|
1153 | log.error('{}'.format(err[-1]), procUnitConfObj.name) | |
1151 |
|
1154 | |||
1152 | message = ''.join(err) |
|
1155 | message = ''.join(err) | |
1153 |
|
1156 | |||
1154 | if stdout: |
|
1157 | if stdout: | |
1155 | sys.stderr.write(message) |
|
1158 | sys.stderr.write(message) | |
1156 |
|
1159 | |||
1157 | subject = 'SChain v%s: Error running %s\n' % ( |
|
1160 | subject = 'SChain v%s: Error running %s\n' % ( | |
1158 | schainpy.__version__, procUnitConfObj.name) |
|
1161 | schainpy.__version__, procUnitConfObj.name) | |
1159 |
|
1162 | |||
1160 | subtitle = '%s: %s\n' % ( |
|
1163 | subtitle = '%s: %s\n' % ( | |
1161 | procUnitConfObj.getElementName(), procUnitConfObj.name) |
|
1164 | procUnitConfObj.getElementName(), procUnitConfObj.name) | |
1162 | subtitle += 'Hostname: %s\n' % socket.gethostbyname( |
|
1165 | subtitle += 'Hostname: %s\n' % socket.gethostbyname( | |
1163 | socket.gethostname()) |
|
1166 | socket.gethostname()) | |
1164 | subtitle += 'Working directory: %s\n' % os.path.abspath('./') |
|
1167 | subtitle += 'Working directory: %s\n' % os.path.abspath('./') | |
1165 | subtitle += 'Configuration file: %s\n' % self.filename |
|
1168 | subtitle += 'Configuration file: %s\n' % self.filename | |
1166 | subtitle += 'Time: %s\n' % str(datetime.datetime.now()) |
|
1169 | subtitle += 'Time: %s\n' % str(datetime.datetime.now()) | |
1167 |
|
1170 | |||
1168 | readUnitConfObj = self.getReadUnitObj() |
|
1171 | readUnitConfObj = self.getReadUnitObj() | |
1169 | if readUnitConfObj: |
|
1172 | if readUnitConfObj: | |
1170 | subtitle += '\nInput parameters:\n' |
|
1173 | subtitle += '\nInput parameters:\n' | |
1171 | subtitle += '[Data path = %s]\n' % readUnitConfObj.path |
|
1174 | subtitle += '[Data path = %s]\n' % readUnitConfObj.path | |
1172 | subtitle += '[Data type = %s]\n' % readUnitConfObj.datatype |
|
1175 | subtitle += '[Data type = %s]\n' % readUnitConfObj.datatype | |
1173 | subtitle += '[Start date = %s]\n' % readUnitConfObj.startDate |
|
1176 | subtitle += '[Start date = %s]\n' % readUnitConfObj.startDate | |
1174 | subtitle += '[End date = %s]\n' % readUnitConfObj.endDate |
|
1177 | subtitle += '[End date = %s]\n' % readUnitConfObj.endDate | |
1175 | subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime |
|
1178 | subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime | |
1176 | subtitle += '[End time = %s]\n' % readUnitConfObj.endTime |
|
1179 | subtitle += '[End time = %s]\n' % readUnitConfObj.endTime | |
1177 |
|
1180 | |||
1178 | a = Alarm( |
|
1181 | a = Alarm( | |
1179 | modes=modes, |
|
1182 | modes=modes, | |
1180 | email=self.email, |
|
1183 | email=self.email, | |
1181 | message=message, |
|
1184 | message=message, | |
1182 | subject=subject, |
|
1185 | subject=subject, | |
1183 | subtitle=subtitle, |
|
1186 | subtitle=subtitle, | |
1184 | filename=self.filename |
|
1187 | filename=self.filename | |
1185 | ) |
|
1188 | ) | |
1186 |
|
1189 | |||
1187 | return a |
|
1190 | return a | |
1188 |
|
1191 | |||
1189 | def isPaused(self): |
|
1192 | def isPaused(self): | |
1190 | return 0 |
|
1193 | return 0 | |
1191 |
|
1194 | |||
1192 | def isStopped(self): |
|
1195 | def isStopped(self): | |
1193 | return 0 |
|
1196 | return 0 | |
1194 |
|
1197 | |||
1195 | def runController(self): |
|
1198 | def runController(self): | |
1196 | ''' |
|
1199 | ''' | |
1197 | returns 0 when this process has been stopped, 1 otherwise |
|
1200 | returns 0 when this process has been stopped, 1 otherwise | |
1198 | ''' |
|
1201 | ''' | |
1199 |
|
1202 | |||
1200 | if self.isPaused(): |
|
1203 | if self.isPaused(): | |
1201 | print('Process suspended') |
|
1204 | print('Process suspended') | |
1202 |
|
1205 | |||
1203 | while True: |
|
1206 | while True: | |
1204 | time.sleep(0.1) |
|
1207 | time.sleep(0.1) | |
1205 |
|
1208 | |||
1206 | if not self.isPaused(): |
|
1209 | if not self.isPaused(): | |
1207 | break |
|
1210 | break | |
1208 |
|
1211 | |||
1209 | if self.isStopped(): |
|
1212 | if self.isStopped(): | |
1210 | break |
|
1213 | break | |
1211 |
|
1214 | |||
1212 | print('Process reinitialized') |
|
1215 | print('Process reinitialized') | |
1213 |
|
1216 | |||
1214 | if self.isStopped(): |
|
1217 | if self.isStopped(): | |
1215 | print('Process stopped') |
|
1218 | print('Process stopped') | |
1216 | return 0 |
|
1219 | return 0 | |
1217 |
|
1220 | |||
1218 | return 1 |
|
1221 | return 1 | |
1219 |
|
1222 | |||
1220 | def setFilename(self, filename): |
|
1223 | def setFilename(self, filename): | |
1221 |
|
1224 | |||
1222 | self.filename = filename |
|
1225 | self.filename = filename | |
1223 |
|
1226 | |||
1224 | def setProxyCom(self): |
|
1227 | def setProxyCom(self): | |
1225 |
|
1228 | |||
1226 | if not os.path.exists('/tmp/schain'): |
|
1229 | if not os.path.exists('/tmp/schain'): | |
1227 | os.mkdir('/tmp/schain') |
|
1230 | os.mkdir('/tmp/schain') | |
1228 |
|
1231 | |||
1229 | self.ctx = zmq.Context() |
|
1232 | self.ctx = zmq.Context() | |
1230 | xpub = self.ctx.socket(zmq.XPUB) |
|
1233 | xpub = self.ctx.socket(zmq.XPUB) | |
1231 | xpub.bind('ipc:///tmp/schain/{}_pub'.format(self.id)) |
|
1234 | xpub.bind('ipc:///tmp/schain/{}_pub'.format(self.id)) | |
1232 | xsub = self.ctx.socket(zmq.XSUB) |
|
1235 | xsub = self.ctx.socket(zmq.XSUB) | |
1233 | xsub.bind('ipc:///tmp/schain/{}_sub'.format(self.id)) |
|
1236 | xsub.bind('ipc:///tmp/schain/{}_sub'.format(self.id)) | |
1234 |
|
1237 | |||
1235 | try: |
|
1238 | try: | |
1236 | zmq.proxy(xpub, xsub) |
|
1239 | zmq.proxy(xpub, xsub) | |
1237 | except: # zmq.ContextTerminated: |
|
1240 | except: # zmq.ContextTerminated: | |
1238 | xpub.close() |
|
1241 | xpub.close() | |
1239 | xsub.close() |
|
1242 | xsub.close() | |
1240 |
|
1243 | |||
1241 | def run(self): |
|
1244 | def run(self): | |
1242 |
|
1245 | |||
1243 | log.success('Starting {}: {}'.format(self.name, self.id), tag='') |
|
1246 | log.success('Starting {}: {}'.format(self.name, self.id), tag='') | |
1244 | self.start_time = time.time() |
|
1247 | self.start_time = time.time() | |
1245 | self.createObjects() |
|
1248 | self.createObjects() | |
1246 | # t = Thread(target=wait, args=(self.ctx, )) |
|
1249 | # t = Thread(target=wait, args=(self.ctx, )) | |
1247 | # t.start() |
|
1250 | # t.start() | |
1248 | self.setProxyCom() |
|
1251 | self.setProxyCom() | |
1249 |
|
1252 | |||
1250 | # Iniciar todos los procesos .start(), monitoreo de procesos. ELiminar lo de abajo |
|
1253 | # Iniciar todos los procesos .start(), monitoreo de procesos. ELiminar lo de abajo | |
1251 |
|
1254 | |||
1252 | log.success('{} Done (time: {}s)'.format( |
|
1255 | log.success('{} Done (time: {}s)'.format( | |
1253 | self.name, |
|
1256 | self.name, | |
1254 | time.time()-self.start_time)) |
|
1257 | time.time()-self.start_time)) |
@@ -1,800 +1,797 | |||||
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 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 |
|
19 | |||
20 | try: |
|
20 | try: | |
21 | from gevent import sleep |
|
21 | from gevent import sleep | |
22 | except: |
|
22 | except: | |
23 | from time import sleep |
|
23 | from time import sleep | |
24 |
|
24 | |||
25 | from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader |
|
25 | from schainpy.model.data.jroheaderIO import RadarControllerHeader, SystemHeader | |
26 | from schainpy.model.data.jrodata import Voltage |
|
26 | from schainpy.model.data.jrodata import Voltage | |
27 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation |
|
27 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator | |
28 | from time import time |
|
28 | from time import time | |
29 |
|
29 | |||
30 | import pickle |
|
30 | import pickle | |
31 | try: |
|
31 | try: | |
32 | import digital_rf |
|
32 | import digital_rf | |
33 | except: |
|
33 | except: | |
34 | print('You should install "digital_rf" module if you want to read Digital RF data') |
|
34 | print('You should install "digital_rf" module if you want to read Digital RF data') | |
35 |
|
35 | |||
36 |
|
36 | @MPDecorator | ||
37 | class DigitalRFReader(ProcessingUnit): |
|
37 | class DigitalRFReader(ProcessingUnit): | |
38 | ''' |
|
38 | ''' | |
39 | classdocs |
|
39 | classdocs | |
40 | ''' |
|
40 | ''' | |
41 |
|
41 | |||
42 |
def __init__(self |
|
42 | def __init__(self): | |
43 | ''' |
|
43 | ''' | |
44 | Constructor |
|
44 | Constructor | |
45 | ''' |
|
45 | ''' | |
46 |
|
46 | |||
47 |
ProcessingUnit.__init__(self |
|
47 | ProcessingUnit.__init__(self) | |
48 |
|
48 | |||
49 | self.dataOut = Voltage() |
|
49 | self.dataOut = Voltage() | |
50 | self.__printInfo = True |
|
50 | self.__printInfo = True | |
51 | self.__flagDiscontinuousBlock = False |
|
51 | self.__flagDiscontinuousBlock = False | |
52 | self.__bufferIndex = 9999999 |
|
52 | self.__bufferIndex = 9999999 | |
53 | self.__ippKm = None |
|
53 | self.__ippKm = None | |
54 | self.__codeType = 0 |
|
54 | self.__codeType = 0 | |
55 | self.__nCode = None |
|
55 | self.__nCode = None | |
56 | self.__nBaud = None |
|
56 | self.__nBaud = None | |
57 | self.__code = None |
|
57 | self.__code = None | |
58 | self.dtype = None |
|
58 | self.dtype = None | |
59 | self.oldAverage = None |
|
59 | self.oldAverage = None | |
60 |
|
60 | |||
61 | def close(self): |
|
61 | def close(self): | |
62 | print('Average of writing to digital rf format is ', self.oldAverage * 1000) |
|
62 | print('Average of writing to digital rf format is ', self.oldAverage * 1000) | |
63 | return |
|
63 | return | |
64 |
|
64 | |||
65 | def __getCurrentSecond(self): |
|
65 | def __getCurrentSecond(self): | |
66 |
|
66 | |||
67 | return self.__thisUnixSample / self.__sample_rate |
|
67 | return self.__thisUnixSample / self.__sample_rate | |
68 |
|
68 | |||
69 | thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.") |
|
69 | thisSecond = property(__getCurrentSecond, "I'm the 'thisSecond' property.") | |
70 |
|
70 | |||
71 | def __setFileHeader(self): |
|
71 | def __setFileHeader(self): | |
72 | ''' |
|
72 | ''' | |
73 | In this method will be initialized every parameter of dataOut object (header, no data) |
|
73 | In this method will be initialized every parameter of dataOut object (header, no data) | |
74 | ''' |
|
74 | ''' | |
75 | ippSeconds = 1.0 * self.__nSamples / self.__sample_rate |
|
75 | ippSeconds = 1.0 * self.__nSamples / self.__sample_rate | |
76 |
|
76 | |||
77 | nProfiles = 1.0 / ippSeconds # Number of profiles in one second |
|
77 | nProfiles = 1.0 / ippSeconds # Number of profiles in one second | |
78 |
|
78 | |||
79 | try: |
|
79 | try: | |
80 | self.dataOut.radarControllerHeaderObj = RadarControllerHeader( |
|
80 | self.dataOut.radarControllerHeaderObj = RadarControllerHeader( | |
81 | self.__radarControllerHeader) |
|
81 | self.__radarControllerHeader) | |
82 | except: |
|
82 | except: | |
83 | self.dataOut.radarControllerHeaderObj = RadarControllerHeader( |
|
83 | self.dataOut.radarControllerHeaderObj = RadarControllerHeader( | |
84 | txA=0, |
|
84 | txA=0, | |
85 | txB=0, |
|
85 | txB=0, | |
86 | nWindows=1, |
|
86 | nWindows=1, | |
87 | nHeights=self.__nSamples, |
|
87 | nHeights=self.__nSamples, | |
88 | firstHeight=self.__firstHeigth, |
|
88 | firstHeight=self.__firstHeigth, | |
89 | deltaHeight=self.__deltaHeigth, |
|
89 | deltaHeight=self.__deltaHeigth, | |
90 | codeType=self.__codeType, |
|
90 | codeType=self.__codeType, | |
91 | nCode=self.__nCode, nBaud=self.__nBaud, |
|
91 | nCode=self.__nCode, nBaud=self.__nBaud, | |
92 | code=self.__code) |
|
92 | code=self.__code) | |
93 |
|
93 | |||
94 | try: |
|
94 | try: | |
95 | self.dataOut.systemHeaderObj = SystemHeader(self.__systemHeader) |
|
95 | self.dataOut.systemHeaderObj = SystemHeader(self.__systemHeader) | |
96 | except: |
|
96 | except: | |
97 | self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples, |
|
97 | self.dataOut.systemHeaderObj = SystemHeader(nSamples=self.__nSamples, | |
98 | nProfiles=nProfiles, |
|
98 | nProfiles=nProfiles, | |
99 | nChannels=len( |
|
99 | nChannels=len( | |
100 | self.__channelList), |
|
100 | self.__channelList), | |
101 | adcResolution=14) |
|
101 | adcResolution=14) | |
102 | self.dataOut.type = "Voltage" |
|
102 | self.dataOut.type = "Voltage" | |
103 |
|
103 | |||
104 | self.dataOut.data = None |
|
104 | self.dataOut.data = None | |
105 |
|
105 | |||
106 | self.dataOut.dtype = self.dtype |
|
106 | self.dataOut.dtype = self.dtype | |
107 |
|
107 | |||
108 | # self.dataOut.nChannels = 0 |
|
108 | # self.dataOut.nChannels = 0 | |
109 |
|
109 | |||
110 | # self.dataOut.nHeights = 0 |
|
110 | # self.dataOut.nHeights = 0 | |
111 |
|
111 | |||
112 | self.dataOut.nProfiles = int(nProfiles) |
|
112 | self.dataOut.nProfiles = int(nProfiles) | |
113 |
|
113 | |||
114 | self.dataOut.heightList = self.__firstHeigth + \ |
|
114 | self.dataOut.heightList = self.__firstHeigth + \ | |
115 | numpy.arange(self.__nSamples, dtype=numpy.float) * \ |
|
115 | numpy.arange(self.__nSamples, dtype=numpy.float) * \ | |
116 | self.__deltaHeigth |
|
116 | self.__deltaHeigth | |
117 |
|
117 | |||
118 | self.dataOut.channelList = list(range(self.__num_subchannels)) |
|
118 | self.dataOut.channelList = list(range(self.__num_subchannels)) | |
119 |
|
119 | |||
120 | self.dataOut.blocksize = self.dataOut.getNChannels() * self.dataOut.getNHeights() |
|
120 | self.dataOut.blocksize = self.dataOut.getNChannels() * self.dataOut.getNHeights() | |
121 |
|
121 | |||
122 | # self.dataOut.channelIndexList = None |
|
122 | # self.dataOut.channelIndexList = None | |
123 |
|
123 | |||
124 | self.dataOut.flagNoData = True |
|
124 | self.dataOut.flagNoData = True | |
125 |
|
125 | |||
126 | self.dataOut.flagDataAsBlock = False |
|
126 | self.dataOut.flagDataAsBlock = False | |
127 | # Set to TRUE if the data is discontinuous |
|
127 | # Set to TRUE if the data is discontinuous | |
128 | self.dataOut.flagDiscontinuousBlock = False |
|
128 | self.dataOut.flagDiscontinuousBlock = False | |
129 |
|
129 | |||
130 | self.dataOut.utctime = None |
|
130 | self.dataOut.utctime = None | |
131 |
|
131 | |||
132 | # timezone like jroheader, difference in minutes between UTC and localtime |
|
132 | # timezone like jroheader, difference in minutes between UTC and localtime | |
133 | self.dataOut.timeZone = self.__timezone / 60 |
|
133 | self.dataOut.timeZone = self.__timezone / 60 | |
134 |
|
134 | |||
135 | self.dataOut.dstFlag = 0 |
|
135 | self.dataOut.dstFlag = 0 | |
136 |
|
136 | |||
137 | self.dataOut.errorCount = 0 |
|
137 | self.dataOut.errorCount = 0 | |
138 |
|
138 | |||
139 | try: |
|
139 | try: | |
140 | self.dataOut.nCohInt = self.fixed_metadata_dict.get( |
|
140 | self.dataOut.nCohInt = self.fixed_metadata_dict.get( | |
141 | 'nCohInt', self.nCohInt) |
|
141 | 'nCohInt', self.nCohInt) | |
142 |
|
142 | |||
143 | # asumo que la data esta decodificada |
|
143 | # asumo que la data esta decodificada | |
144 | self.dataOut.flagDecodeData = self.fixed_metadata_dict.get( |
|
144 | self.dataOut.flagDecodeData = self.fixed_metadata_dict.get( | |
145 | 'flagDecodeData', self.flagDecodeData) |
|
145 | 'flagDecodeData', self.flagDecodeData) | |
146 |
|
146 | |||
147 | # asumo que la data esta sin flip |
|
147 | # asumo que la data esta sin flip | |
148 | self.dataOut.flagDeflipData = self.fixed_metadata_dict['flagDeflipData'] |
|
148 | self.dataOut.flagDeflipData = self.fixed_metadata_dict['flagDeflipData'] | |
149 |
|
149 | |||
150 | self.dataOut.flagShiftFFT = self.fixed_metadata_dict['flagShiftFFT'] |
|
150 | self.dataOut.flagShiftFFT = self.fixed_metadata_dict['flagShiftFFT'] | |
151 |
|
151 | |||
152 | self.dataOut.useLocalTime = self.fixed_metadata_dict['useLocalTime'] |
|
152 | self.dataOut.useLocalTime = self.fixed_metadata_dict['useLocalTime'] | |
153 | except: |
|
153 | except: | |
154 | pass |
|
154 | pass | |
155 |
|
155 | |||
156 | self.dataOut.ippSeconds = ippSeconds |
|
156 | self.dataOut.ippSeconds = ippSeconds | |
157 |
|
157 | |||
158 | # Time interval between profiles |
|
158 | # Time interval between profiles | |
159 | # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt |
|
159 | # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt | |
160 |
|
160 | |||
161 | self.dataOut.frequency = self.__frequency |
|
161 | self.dataOut.frequency = self.__frequency | |
162 |
|
162 | |||
163 | self.dataOut.realtime = self.__online |
|
163 | self.dataOut.realtime = self.__online | |
164 |
|
164 | |||
165 | def findDatafiles(self, path, startDate=None, endDate=None): |
|
165 | def findDatafiles(self, path, startDate=None, endDate=None): | |
166 |
|
166 | |||
167 | if not os.path.isdir(path): |
|
167 | if not os.path.isdir(path): | |
168 | return [] |
|
168 | return [] | |
169 |
|
169 | |||
170 | try: |
|
170 | try: | |
171 | digitalReadObj = digital_rf.DigitalRFReader( |
|
171 | digitalReadObj = digital_rf.DigitalRFReader( | |
172 | path, load_all_metadata=True) |
|
172 | path, load_all_metadata=True) | |
173 | except: |
|
173 | except: | |
174 | digitalReadObj = digital_rf.DigitalRFReader(path) |
|
174 | digitalReadObj = digital_rf.DigitalRFReader(path) | |
175 |
|
175 | |||
176 | channelNameList = digitalReadObj.get_channels() |
|
176 | channelNameList = digitalReadObj.get_channels() | |
177 |
|
177 | |||
178 | if not channelNameList: |
|
178 | if not channelNameList: | |
179 | return [] |
|
179 | return [] | |
180 |
|
180 | |||
181 | metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0]) |
|
181 | metadata_dict = digitalReadObj.get_rf_file_metadata(channelNameList[0]) | |
182 |
|
182 | |||
183 | sample_rate = metadata_dict['sample_rate'][0] |
|
183 | sample_rate = metadata_dict['sample_rate'][0] | |
184 |
|
184 | |||
185 | this_metadata_file = digitalReadObj.get_metadata(channelNameList[0]) |
|
185 | this_metadata_file = digitalReadObj.get_metadata(channelNameList[0]) | |
186 |
|
186 | |||
187 | try: |
|
187 | try: | |
188 | timezone = this_metadata_file['timezone'].value |
|
188 | timezone = this_metadata_file['timezone'].value | |
189 | except: |
|
189 | except: | |
190 | timezone = 0 |
|
190 | timezone = 0 | |
191 |
|
191 | |||
192 | startUTCSecond, endUTCSecond = digitalReadObj.get_bounds( |
|
192 | startUTCSecond, endUTCSecond = digitalReadObj.get_bounds( | |
193 | channelNameList[0]) / sample_rate - timezone |
|
193 | channelNameList[0]) / sample_rate - timezone | |
194 |
|
194 | |||
195 | startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond) |
|
195 | startDatetime = datetime.datetime.utcfromtimestamp(startUTCSecond) | |
196 | endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond) |
|
196 | endDatatime = datetime.datetime.utcfromtimestamp(endUTCSecond) | |
197 |
|
197 | |||
198 | if not startDate: |
|
198 | if not startDate: | |
199 | startDate = startDatetime.date() |
|
199 | startDate = startDatetime.date() | |
200 |
|
200 | |||
201 | if not endDate: |
|
201 | if not endDate: | |
202 | endDate = endDatatime.date() |
|
202 | endDate = endDatatime.date() | |
203 |
|
203 | |||
204 | dateList = [] |
|
204 | dateList = [] | |
205 |
|
205 | |||
206 | thisDatetime = startDatetime |
|
206 | thisDatetime = startDatetime | |
207 |
|
207 | |||
208 | while(thisDatetime <= endDatatime): |
|
208 | while(thisDatetime <= endDatatime): | |
209 |
|
209 | |||
210 | thisDate = thisDatetime.date() |
|
210 | thisDate = thisDatetime.date() | |
211 |
|
211 | |||
212 | if thisDate < startDate: |
|
212 | if thisDate < startDate: | |
213 | continue |
|
213 | continue | |
214 |
|
214 | |||
215 | if thisDate > endDate: |
|
215 | if thisDate > endDate: | |
216 | break |
|
216 | break | |
217 |
|
217 | |||
218 | dateList.append(thisDate) |
|
218 | dateList.append(thisDate) | |
219 | thisDatetime += datetime.timedelta(1) |
|
219 | thisDatetime += datetime.timedelta(1) | |
220 |
|
220 | |||
221 | return dateList |
|
221 | return dateList | |
222 |
|
222 | |||
223 | def setup(self, path=None, |
|
223 | def setup(self, path=None, | |
224 | startDate=None, |
|
224 | startDate=None, | |
225 | endDate=None, |
|
225 | endDate=None, | |
226 | startTime=datetime.time(0, 0, 0), |
|
226 | startTime=datetime.time(0, 0, 0), | |
227 | endTime=datetime.time(23, 59, 59), |
|
227 | endTime=datetime.time(23, 59, 59), | |
228 | channelList=None, |
|
228 | channelList=None, | |
229 | nSamples=None, |
|
229 | nSamples=None, | |
230 | online=False, |
|
230 | online=False, | |
231 | delay=60, |
|
231 | delay=60, | |
232 | buffer_size=1024, |
|
232 | buffer_size=1024, | |
233 | ippKm=None, |
|
233 | ippKm=None, | |
234 | nCohInt=1, |
|
234 | nCohInt=1, | |
235 | nCode=1, |
|
235 | nCode=1, | |
236 | nBaud=1, |
|
236 | nBaud=1, | |
237 | flagDecodeData=False, |
|
237 | flagDecodeData=False, | |
238 | code=numpy.ones((1, 1), dtype=numpy.int), |
|
238 | code=numpy.ones((1, 1), dtype=numpy.int), | |
239 | **kwargs): |
|
239 | **kwargs): | |
240 | ''' |
|
240 | ''' | |
241 | In this method we should set all initial parameters. |
|
241 | In this method we should set all initial parameters. | |
242 |
|
242 | |||
243 | Inputs: |
|
243 | Inputs: | |
244 | path |
|
244 | path | |
245 | startDate |
|
245 | startDate | |
246 | endDate |
|
246 | endDate | |
247 | startTime |
|
247 | startTime | |
248 | endTime |
|
248 | endTime | |
249 | set |
|
249 | set | |
250 | expLabel |
|
250 | expLabel | |
251 | ext |
|
251 | ext | |
252 | online |
|
252 | online | |
253 | delay |
|
253 | delay | |
254 | ''' |
|
254 | ''' | |
255 | self.nCohInt = nCohInt |
|
255 | self.nCohInt = nCohInt | |
256 | self.flagDecodeData = flagDecodeData |
|
256 | self.flagDecodeData = flagDecodeData | |
257 | self.i = 0 |
|
257 | self.i = 0 | |
258 | if not os.path.isdir(path): |
|
258 | if not os.path.isdir(path): | |
259 | raise ValueError("[Reading] Directory %s does not exist" % path) |
|
259 | raise ValueError("[Reading] Directory %s does not exist" % path) | |
260 |
|
260 | |||
261 | try: |
|
261 | try: | |
262 | self.digitalReadObj = digital_rf.DigitalRFReader( |
|
262 | self.digitalReadObj = digital_rf.DigitalRFReader( | |
263 | path, load_all_metadata=True) |
|
263 | path, load_all_metadata=True) | |
264 | except: |
|
264 | except: | |
265 | self.digitalReadObj = digital_rf.DigitalRFReader(path) |
|
265 | self.digitalReadObj = digital_rf.DigitalRFReader(path) | |
266 |
|
266 | |||
267 | channelNameList = self.digitalReadObj.get_channels() |
|
267 | channelNameList = self.digitalReadObj.get_channels() | |
268 |
|
268 | |||
269 | if not channelNameList: |
|
269 | if not channelNameList: | |
270 | raise ValueError("[Reading] Directory %s does not have any files" % path) |
|
270 | raise ValueError("[Reading] Directory %s does not have any files" % path) | |
271 |
|
271 | |||
272 | if not channelList: |
|
272 | if not channelList: | |
273 | channelList = list(range(len(channelNameList))) |
|
273 | channelList = list(range(len(channelNameList))) | |
274 |
|
274 | |||
275 | ########## Reading metadata ###################### |
|
275 | ########## Reading metadata ###################### | |
276 |
|
276 | |||
277 | top_properties = self.digitalReadObj.get_properties( |
|
277 | top_properties = self.digitalReadObj.get_properties( | |
278 | channelNameList[channelList[0]]) |
|
278 | channelNameList[channelList[0]]) | |
279 |
|
279 | |||
280 | self.__num_subchannels = top_properties['num_subchannels'] |
|
280 | self.__num_subchannels = top_properties['num_subchannels'] | |
281 | self.__sample_rate = 1.0 * \ |
|
281 | self.__sample_rate = 1.0 * \ | |
282 | top_properties['sample_rate_numerator'] / \ |
|
282 | top_properties['sample_rate_numerator'] / \ | |
283 | top_properties['sample_rate_denominator'] |
|
283 | top_properties['sample_rate_denominator'] | |
284 | # self.__samples_per_file = top_properties['samples_per_file'][0] |
|
284 | # self.__samples_per_file = top_properties['samples_per_file'][0] | |
285 | self.__deltaHeigth = 1e6 * 0.15 / self.__sample_rate # why 0.15? |
|
285 | self.__deltaHeigth = 1e6 * 0.15 / self.__sample_rate # why 0.15? | |
286 |
|
286 | |||
287 | this_metadata_file = self.digitalReadObj.get_digital_metadata( |
|
287 | this_metadata_file = self.digitalReadObj.get_digital_metadata( | |
288 | channelNameList[channelList[0]]) |
|
288 | channelNameList[channelList[0]]) | |
289 | metadata_bounds = this_metadata_file.get_bounds() |
|
289 | metadata_bounds = this_metadata_file.get_bounds() | |
290 | self.fixed_metadata_dict = this_metadata_file.read( |
|
290 | self.fixed_metadata_dict = this_metadata_file.read( | |
291 | metadata_bounds[0])[metadata_bounds[0]] # GET FIRST HEADER |
|
291 | metadata_bounds[0])[metadata_bounds[0]] # GET FIRST HEADER | |
292 |
|
292 | |||
293 | try: |
|
293 | try: | |
294 | self.__processingHeader = self.fixed_metadata_dict['processingHeader'] |
|
294 | self.__processingHeader = self.fixed_metadata_dict['processingHeader'] | |
295 | self.__radarControllerHeader = self.fixed_metadata_dict['radarControllerHeader'] |
|
295 | self.__radarControllerHeader = self.fixed_metadata_dict['radarControllerHeader'] | |
296 | self.__systemHeader = self.fixed_metadata_dict['systemHeader'] |
|
296 | self.__systemHeader = self.fixed_metadata_dict['systemHeader'] | |
297 | self.dtype = pickle.loads(self.fixed_metadata_dict['dtype']) |
|
297 | self.dtype = pickle.loads(self.fixed_metadata_dict['dtype']) | |
298 | except: |
|
298 | except: | |
299 | pass |
|
299 | pass | |
300 |
|
300 | |||
301 | self.__frequency = None |
|
301 | self.__frequency = None | |
302 |
|
302 | |||
303 | self.__frequency = self.fixed_metadata_dict.get('frequency', 1) |
|
303 | self.__frequency = self.fixed_metadata_dict.get('frequency', 1) | |
304 |
|
304 | |||
305 | self.__timezone = self.fixed_metadata_dict.get('timezone', 300) |
|
305 | self.__timezone = self.fixed_metadata_dict.get('timezone', 300) | |
306 |
|
306 | |||
307 | try: |
|
307 | try: | |
308 | nSamples = self.fixed_metadata_dict['nSamples'] |
|
308 | nSamples = self.fixed_metadata_dict['nSamples'] | |
309 | except: |
|
309 | except: | |
310 | nSamples = None |
|
310 | nSamples = None | |
311 |
|
311 | |||
312 | self.__firstHeigth = 0 |
|
312 | self.__firstHeigth = 0 | |
313 |
|
313 | |||
314 | try: |
|
314 | try: | |
315 | codeType = self.__radarControllerHeader['codeType'] |
|
315 | codeType = self.__radarControllerHeader['codeType'] | |
316 | except: |
|
316 | except: | |
317 | codeType = 0 |
|
317 | codeType = 0 | |
318 |
|
318 | |||
319 | try: |
|
319 | try: | |
320 | if codeType: |
|
320 | if codeType: | |
321 | nCode = self.__radarControllerHeader['nCode'] |
|
321 | nCode = self.__radarControllerHeader['nCode'] | |
322 | nBaud = self.__radarControllerHeader['nBaud'] |
|
322 | nBaud = self.__radarControllerHeader['nBaud'] | |
323 | code = self.__radarControllerHeader['code'] |
|
323 | code = self.__radarControllerHeader['code'] | |
324 | except: |
|
324 | except: | |
325 | pass |
|
325 | pass | |
326 |
|
326 | |||
327 | if not ippKm: |
|
327 | if not ippKm: | |
328 | try: |
|
328 | try: | |
329 | # seconds to km |
|
329 | # seconds to km | |
330 | ippKm = self.__radarControllerHeader['ipp'] |
|
330 | ippKm = self.__radarControllerHeader['ipp'] | |
331 | except: |
|
331 | except: | |
332 | ippKm = None |
|
332 | ippKm = None | |
333 | #################################################### |
|
333 | #################################################### | |
334 | self.__ippKm = ippKm |
|
334 | self.__ippKm = ippKm | |
335 | startUTCSecond = None |
|
335 | startUTCSecond = None | |
336 | endUTCSecond = None |
|
336 | endUTCSecond = None | |
337 |
|
337 | |||
338 | if startDate: |
|
338 | if startDate: | |
339 | startDatetime = datetime.datetime.combine(startDate, startTime) |
|
339 | startDatetime = datetime.datetime.combine(startDate, startTime) | |
340 | startUTCSecond = ( |
|
340 | startUTCSecond = ( | |
341 | startDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone |
|
341 | startDatetime - datetime.datetime(1970, 1, 1)).total_seconds() + self.__timezone | |
342 |
|
342 | |||
343 | if endDate: |
|
343 | if endDate: | |
344 | endDatetime = datetime.datetime.combine(endDate, endTime) |
|
344 | endDatetime = datetime.datetime.combine(endDate, endTime) | |
345 | endUTCSecond = (endDatetime - datetime.datetime(1970, |
|
345 | endUTCSecond = (endDatetime - datetime.datetime(1970, | |
346 | 1, 1)).total_seconds() + self.__timezone |
|
346 | 1, 1)).total_seconds() + self.__timezone | |
347 |
|
347 | |||
348 | start_index, end_index = self.digitalReadObj.get_bounds( |
|
348 | start_index, end_index = self.digitalReadObj.get_bounds( | |
349 | channelNameList[channelList[0]]) |
|
349 | channelNameList[channelList[0]]) | |
350 |
|
350 | |||
351 | if not startUTCSecond: |
|
351 | if not startUTCSecond: | |
352 | startUTCSecond = start_index / self.__sample_rate |
|
352 | startUTCSecond = start_index / self.__sample_rate | |
353 |
|
353 | |||
354 | if start_index > startUTCSecond * self.__sample_rate: |
|
354 | if start_index > startUTCSecond * self.__sample_rate: | |
355 | startUTCSecond = start_index / self.__sample_rate |
|
355 | startUTCSecond = start_index / self.__sample_rate | |
356 |
|
356 | |||
357 | if not endUTCSecond: |
|
357 | if not endUTCSecond: | |
358 | endUTCSecond = end_index / self.__sample_rate |
|
358 | endUTCSecond = end_index / self.__sample_rate | |
359 |
|
359 | |||
360 | if end_index < endUTCSecond * self.__sample_rate: |
|
360 | if end_index < endUTCSecond * self.__sample_rate: | |
361 | endUTCSecond = end_index / self.__sample_rate |
|
361 | endUTCSecond = end_index / self.__sample_rate | |
362 | if not nSamples: |
|
362 | if not nSamples: | |
363 | if not ippKm: |
|
363 | if not ippKm: | |
364 | raise ValueError("[Reading] nSamples or ippKm should be defined") |
|
364 | raise ValueError("[Reading] nSamples or ippKm should be defined") | |
365 | nSamples = int(ippKm / (1e6 * 0.15 / self.__sample_rate)) |
|
365 | nSamples = int(ippKm / (1e6 * 0.15 / self.__sample_rate)) | |
366 | channelBoundList = [] |
|
366 | channelBoundList = [] | |
367 | channelNameListFiltered = [] |
|
367 | channelNameListFiltered = [] | |
368 |
|
368 | |||
369 | for thisIndexChannel in channelList: |
|
369 | for thisIndexChannel in channelList: | |
370 | thisChannelName = channelNameList[thisIndexChannel] |
|
370 | thisChannelName = channelNameList[thisIndexChannel] | |
371 | start_index, end_index = self.digitalReadObj.get_bounds( |
|
371 | start_index, end_index = self.digitalReadObj.get_bounds( | |
372 | thisChannelName) |
|
372 | thisChannelName) | |
373 | channelBoundList.append((start_index, end_index)) |
|
373 | channelBoundList.append((start_index, end_index)) | |
374 | channelNameListFiltered.append(thisChannelName) |
|
374 | channelNameListFiltered.append(thisChannelName) | |
375 |
|
375 | |||
376 | self.profileIndex = 0 |
|
376 | self.profileIndex = 0 | |
377 | self.i = 0 |
|
377 | self.i = 0 | |
378 | self.__delay = delay |
|
378 | self.__delay = delay | |
379 |
|
379 | |||
380 | self.__codeType = codeType |
|
380 | self.__codeType = codeType | |
381 | self.__nCode = nCode |
|
381 | self.__nCode = nCode | |
382 | self.__nBaud = nBaud |
|
382 | self.__nBaud = nBaud | |
383 | self.__code = code |
|
383 | self.__code = code | |
384 |
|
384 | |||
385 | self.__datapath = path |
|
385 | self.__datapath = path | |
386 | self.__online = online |
|
386 | self.__online = online | |
387 | self.__channelList = channelList |
|
387 | self.__channelList = channelList | |
388 | self.__channelNameList = channelNameListFiltered |
|
388 | self.__channelNameList = channelNameListFiltered | |
389 | self.__channelBoundList = channelBoundList |
|
389 | self.__channelBoundList = channelBoundList | |
390 | self.__nSamples = nSamples |
|
390 | self.__nSamples = nSamples | |
391 | self.__samples_to_read = int(nSamples) # FIJO: AHORA 40 |
|
391 | self.__samples_to_read = int(nSamples) # FIJO: AHORA 40 | |
392 | self.__nChannels = len(self.__channelList) |
|
392 | self.__nChannels = len(self.__channelList) | |
393 |
|
393 | |||
394 | self.__startUTCSecond = startUTCSecond |
|
394 | self.__startUTCSecond = startUTCSecond | |
395 | self.__endUTCSecond = endUTCSecond |
|
395 | self.__endUTCSecond = endUTCSecond | |
396 |
|
396 | |||
397 | self.__timeInterval = 1.0 * self.__samples_to_read / \ |
|
397 | self.__timeInterval = 1.0 * self.__samples_to_read / \ | |
398 | self.__sample_rate # Time interval |
|
398 | self.__sample_rate # Time interval | |
399 |
|
399 | |||
400 | if online: |
|
400 | if online: | |
401 | # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read) |
|
401 | # self.__thisUnixSample = int(endUTCSecond*self.__sample_rate - 4*self.__samples_to_read) | |
402 | startUTCSecond = numpy.floor(endUTCSecond) |
|
402 | startUTCSecond = numpy.floor(endUTCSecond) | |
403 |
|
403 | |||
404 | # por que en el otro metodo lo primero q se hace es sumar samplestoread |
|
404 | # por que en el otro metodo lo primero q se hace es sumar samplestoread | |
405 | self.__thisUnixSample = int( |
|
405 | self.__thisUnixSample = int( | |
406 | startUTCSecond * self.__sample_rate) - self.__samples_to_read |
|
406 | startUTCSecond * self.__sample_rate) - self.__samples_to_read | |
407 |
|
407 | |||
408 | self.__data_buffer = numpy.zeros( |
|
408 | self.__data_buffer = numpy.zeros( | |
409 | (self.__num_subchannels, self.__samples_to_read), dtype=numpy.complex) |
|
409 | (self.__num_subchannels, self.__samples_to_read), dtype=numpy.complex) | |
410 |
|
410 | |||
411 | self.__setFileHeader() |
|
411 | self.__setFileHeader() | |
412 | self.isConfig = True |
|
412 | self.isConfig = True | |
413 |
|
413 | |||
414 | print("[Reading] Digital RF Data was found from %s to %s " % ( |
|
414 | print("[Reading] Digital RF Data was found from %s to %s " % ( | |
415 | datetime.datetime.utcfromtimestamp( |
|
415 | datetime.datetime.utcfromtimestamp( | |
416 | self.__startUTCSecond - self.__timezone), |
|
416 | self.__startUTCSecond - self.__timezone), | |
417 | datetime.datetime.utcfromtimestamp( |
|
417 | datetime.datetime.utcfromtimestamp( | |
418 | self.__endUTCSecond - self.__timezone) |
|
418 | self.__endUTCSecond - self.__timezone) | |
419 | )) |
|
419 | )) | |
420 |
|
420 | |||
421 | print("[Reading] Starting process from %s to %s" % (datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone), |
|
421 | print("[Reading] Starting process from %s to %s" % (datetime.datetime.utcfromtimestamp(startUTCSecond - self.__timezone), | |
422 | datetime.datetime.utcfromtimestamp( |
|
422 | datetime.datetime.utcfromtimestamp( | |
423 | endUTCSecond - self.__timezone) |
|
423 | endUTCSecond - self.__timezone) | |
424 | )) |
|
424 | )) | |
425 | self.oldAverage = None |
|
425 | self.oldAverage = None | |
426 | self.count = 0 |
|
426 | self.count = 0 | |
427 | self.executionTime = 0 |
|
427 | self.executionTime = 0 | |
428 |
|
428 | |||
429 | def __reload(self): |
|
429 | def __reload(self): | |
430 |
|
430 | |||
431 | # print "%s not in range [%s, %s]" %( |
|
431 | # print "%s not in range [%s, %s]" %( | |
432 | # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), |
|
432 | # datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), | |
433 | # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone), |
|
433 | # datetime.datetime.utcfromtimestamp(self.__startUTCSecond - self.__timezone), | |
434 | # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone) |
|
434 | # datetime.datetime.utcfromtimestamp(self.__endUTCSecond - self.__timezone) | |
435 | # ) |
|
435 | # ) | |
436 | print("[Reading] reloading metadata ...") |
|
436 | print("[Reading] reloading metadata ...") | |
437 |
|
437 | |||
438 | try: |
|
438 | try: | |
439 | self.digitalReadObj.reload(complete_update=True) |
|
439 | self.digitalReadObj.reload(complete_update=True) | |
440 | except: |
|
440 | except: | |
441 | self.digitalReadObj.reload() |
|
441 | self.digitalReadObj.reload() | |
442 |
|
442 | |||
443 | start_index, end_index = self.digitalReadObj.get_bounds( |
|
443 | start_index, end_index = self.digitalReadObj.get_bounds( | |
444 | self.__channelNameList[self.__channelList[0]]) |
|
444 | self.__channelNameList[self.__channelList[0]]) | |
445 |
|
445 | |||
446 | if start_index > self.__startUTCSecond * self.__sample_rate: |
|
446 | if start_index > self.__startUTCSecond * self.__sample_rate: | |
447 | self.__startUTCSecond = 1.0 * start_index / self.__sample_rate |
|
447 | self.__startUTCSecond = 1.0 * start_index / self.__sample_rate | |
448 |
|
448 | |||
449 | if end_index > self.__endUTCSecond * self.__sample_rate: |
|
449 | if end_index > self.__endUTCSecond * self.__sample_rate: | |
450 | self.__endUTCSecond = 1.0 * end_index / self.__sample_rate |
|
450 | self.__endUTCSecond = 1.0 * end_index / self.__sample_rate | |
451 | print() |
|
451 | print() | |
452 | print("[Reading] New timerange found [%s, %s] " % ( |
|
452 | print("[Reading] New timerange found [%s, %s] " % ( | |
453 | datetime.datetime.utcfromtimestamp( |
|
453 | datetime.datetime.utcfromtimestamp( | |
454 | self.__startUTCSecond - self.__timezone), |
|
454 | self.__startUTCSecond - self.__timezone), | |
455 | datetime.datetime.utcfromtimestamp( |
|
455 | datetime.datetime.utcfromtimestamp( | |
456 | self.__endUTCSecond - self.__timezone) |
|
456 | self.__endUTCSecond - self.__timezone) | |
457 | )) |
|
457 | )) | |
458 |
|
458 | |||
459 | return True |
|
459 | return True | |
460 |
|
460 | |||
461 | return False |
|
461 | return False | |
462 |
|
462 | |||
463 | def timeit(self, toExecute): |
|
463 | def timeit(self, toExecute): | |
464 | t0 = time() |
|
464 | t0 = time() | |
465 | toExecute() |
|
465 | toExecute() | |
466 | self.executionTime = time() - t0 |
|
466 | self.executionTime = time() - t0 | |
467 | if self.oldAverage is None: |
|
467 | if self.oldAverage is None: | |
468 | self.oldAverage = self.executionTime |
|
468 | self.oldAverage = self.executionTime | |
469 | self.oldAverage = (self.executionTime + self.count * |
|
469 | self.oldAverage = (self.executionTime + self.count * | |
470 | self.oldAverage) / (self.count + 1.0) |
|
470 | self.oldAverage) / (self.count + 1.0) | |
471 | self.count = self.count + 1.0 |
|
471 | self.count = self.count + 1.0 | |
472 | return |
|
472 | return | |
473 |
|
473 | |||
474 | def __readNextBlock(self, seconds=30, volt_scale=1): |
|
474 | def __readNextBlock(self, seconds=30, volt_scale=1): | |
475 | ''' |
|
475 | ''' | |
476 | ''' |
|
476 | ''' | |
477 |
|
477 | |||
478 | # Set the next data |
|
478 | # Set the next data | |
479 | self.__flagDiscontinuousBlock = False |
|
479 | self.__flagDiscontinuousBlock = False | |
480 | self.__thisUnixSample += self.__samples_to_read |
|
480 | self.__thisUnixSample += self.__samples_to_read | |
481 |
|
481 | |||
482 | if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate: |
|
482 | if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate: | |
483 | print("[Reading] There are no more data into selected time-range") |
|
483 | print("[Reading] There are no more data into selected time-range") | |
484 | if self.__online: |
|
484 | if self.__online: | |
485 | self.__reload() |
|
485 | self.__reload() | |
486 | else: |
|
486 | else: | |
487 | return False |
|
487 | return False | |
488 |
|
488 | |||
489 | if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate: |
|
489 | if self.__thisUnixSample + 2 * self.__samples_to_read > self.__endUTCSecond * self.__sample_rate: | |
490 | return False |
|
490 | return False | |
491 | self.__thisUnixSample -= self.__samples_to_read |
|
491 | self.__thisUnixSample -= self.__samples_to_read | |
492 |
|
492 | |||
493 | indexChannel = 0 |
|
493 | indexChannel = 0 | |
494 |
|
494 | |||
495 | dataOk = False |
|
495 | dataOk = False | |
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 |
|
524 | |||
525 | indexChannel += 1 |
|
525 | indexChannel += 1 | |
526 |
|
526 | |||
527 | dataOk = True |
|
527 | dataOk = True | |
528 |
|
528 | |||
529 | self.__utctime = self.__thisUnixSample / self.__sample_rate |
|
529 | self.__utctime = self.__thisUnixSample / self.__sample_rate | |
530 |
|
530 | |||
531 | if not dataOk: |
|
531 | if not dataOk: | |
532 | return False |
|
532 | return False | |
533 |
|
533 | |||
534 | print("[Reading] %s: %d samples <> %f sec" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), |
|
534 | print("[Reading] %s: %d samples <> %f sec" % (datetime.datetime.utcfromtimestamp(self.thisSecond - self.__timezone), | |
535 | self.__samples_to_read, |
|
535 | self.__samples_to_read, | |
536 | self.__timeInterval)) |
|
536 | self.__timeInterval)) | |
537 |
|
537 | |||
538 | self.__bufferIndex = 0 |
|
538 | self.__bufferIndex = 0 | |
539 |
|
539 | |||
540 | return True |
|
540 | return True | |
541 |
|
541 | |||
542 | def __isBufferEmpty(self): |
|
542 | def __isBufferEmpty(self): | |
543 | return self.__bufferIndex > self.__samples_to_read - self.__nSamples # 40960 - 40 |
|
543 | return self.__bufferIndex > self.__samples_to_read - self.__nSamples # 40960 - 40 | |
544 |
|
544 | |||
545 | def getData(self, seconds=30, nTries=5): |
|
545 | def getData(self, seconds=30, nTries=5): | |
546 | ''' |
|
546 | ''' | |
547 | This method gets the data from files and put the data into the dataOut object |
|
547 | This method gets the data from files and put the data into the dataOut object | |
548 |
|
548 | |||
549 | In addition, increase el the buffer counter in one. |
|
549 | In addition, increase el the buffer counter in one. | |
550 |
|
550 | |||
551 | Return: |
|
551 | Return: | |
552 | data : retorna un perfil de voltages (alturas * canales) copiados desde el |
|
552 | data : retorna un perfil de voltages (alturas * canales) copiados desde el | |
553 | buffer. Si no hay mas archivos a leer retorna None. |
|
553 | buffer. Si no hay mas archivos a leer retorna None. | |
554 |
|
554 | |||
555 | Affected: |
|
555 | Affected: | |
556 | self.dataOut |
|
556 | self.dataOut | |
557 | self.profileIndex |
|
557 | self.profileIndex | |
558 | self.flagDiscontinuousBlock |
|
558 | self.flagDiscontinuousBlock | |
559 | self.flagIsNewBlock |
|
559 | self.flagIsNewBlock | |
560 | ''' |
|
560 | ''' | |
561 |
|
561 | |||
562 | err_counter = 0 |
|
562 | err_counter = 0 | |
563 | self.dataOut.flagNoData = True |
|
563 | self.dataOut.flagNoData = True | |
564 |
|
564 | |||
565 | if self.__isBufferEmpty(): |
|
565 | if self.__isBufferEmpty(): | |
566 | self.__flagDiscontinuousBlock = False |
|
566 | self.__flagDiscontinuousBlock = False | |
567 |
|
567 | |||
568 | while True: |
|
568 | while True: | |
569 | if self.__readNextBlock(): |
|
569 | if self.__readNextBlock(): | |
570 | break |
|
570 | break | |
571 | if self.__thisUnixSample > self.__endUTCSecond * self.__sample_rate: |
|
571 | if self.__thisUnixSample > self.__endUTCSecond * self.__sample_rate: | |
572 | return False |
|
572 | self.dataOut.error = (1, '') | |
|
573 | return | |||
573 |
|
574 | |||
574 | if self.__flagDiscontinuousBlock: |
|
575 | if self.__flagDiscontinuousBlock: | |
575 | print('[Reading] discontinuous block found ... continue with the next block') |
|
576 | print('[Reading] discontinuous block found ... continue with the next block') | |
576 | continue |
|
577 | self.dataOut.error = (1, '') | |
577 |
|
578 | return | ||
|
579 | ||||
578 | if not self.__online: |
|
580 | if not self.__online: | |
579 | return False |
|
581 | self.dataOut.error = (1, '') | |
|
582 | return | |||
580 |
|
583 | |||
581 | err_counter += 1 |
|
584 | err_counter += 1 | |
582 | if err_counter > nTries: |
|
585 | if err_counter > nTries: | |
583 | return False |
|
586 | self.dataOut.error = (1, '') | |
|
587 | return | |||
584 |
|
588 | |||
585 | print('[Reading] waiting %d seconds to read a new block' % seconds) |
|
589 | print('[Reading] waiting %d seconds to read a new block' % seconds) | |
586 | sleep(seconds) |
|
590 | sleep(seconds) | |
587 |
|
591 | |||
588 | self.dataOut.data = self.__data_buffer[:, |
|
592 | self.dataOut.data = self.__data_buffer[:, | |
589 | self.__bufferIndex:self.__bufferIndex + self.__nSamples] |
|
593 | self.__bufferIndex:self.__bufferIndex + self.__nSamples] | |
590 | self.dataOut.utctime = ( |
|
594 | self.dataOut.utctime = ( | |
591 | self.__thisUnixSample + self.__bufferIndex) / self.__sample_rate |
|
595 | self.__thisUnixSample + self.__bufferIndex) / self.__sample_rate | |
592 | self.dataOut.flagNoData = False |
|
596 | self.dataOut.flagNoData = False | |
593 | self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock |
|
597 | self.dataOut.flagDiscontinuousBlock = self.__flagDiscontinuousBlock | |
594 | self.dataOut.profileIndex = self.profileIndex |
|
598 | self.dataOut.profileIndex = self.profileIndex | |
595 |
|
599 | |||
596 | self.__bufferIndex += self.__nSamples |
|
600 | self.__bufferIndex += self.__nSamples | |
597 | self.profileIndex += 1 |
|
601 | self.profileIndex += 1 | |
598 |
|
602 | |||
599 | if self.profileIndex == self.dataOut.nProfiles: |
|
603 | if self.profileIndex == self.dataOut.nProfiles: | |
600 | self.profileIndex = 0 |
|
604 | self.profileIndex = 0 | |
601 |
|
605 | |||
602 |
return |
|
606 | return | |
603 |
|
607 | |||
604 | def printInfo(self): |
|
608 | def printInfo(self): | |
605 | ''' |
|
609 | ''' | |
606 | ''' |
|
610 | ''' | |
607 | if self.__printInfo == False: |
|
611 | if self.__printInfo == False: | |
608 | return |
|
612 | return | |
609 |
|
613 | |||
610 | # self.systemHeaderObj.printInfo() |
|
614 | # self.systemHeaderObj.printInfo() | |
611 | # self.radarControllerHeaderObj.printInfo() |
|
615 | # self.radarControllerHeaderObj.printInfo() | |
612 |
|
616 | |||
613 | self.__printInfo = False |
|
617 | self.__printInfo = False | |
614 |
|
618 | |||
615 | def printNumberOfBlock(self): |
|
619 | def printNumberOfBlock(self): | |
616 | ''' |
|
620 | ''' | |
617 | ''' |
|
621 | ''' | |
618 | return |
|
622 | return | |
619 | # print self.profileIndex |
|
623 | # print self.profileIndex | |
620 |
|
624 | |||
621 | def run(self, **kwargs): |
|
625 | def run(self, **kwargs): | |
622 | ''' |
|
626 | ''' | |
623 | This method will be called many times so here you should put all your code |
|
627 | This method will be called many times so here you should put all your code | |
624 | ''' |
|
628 | ''' | |
625 |
|
629 | |||
626 | if not self.isConfig: |
|
630 | if not self.isConfig: | |
627 | self.setup(**kwargs) |
|
631 | self.setup(**kwargs) | |
628 | #self.i = self.i+1 |
|
632 | #self.i = self.i+1 | |
629 | self.getData(seconds=self.__delay) |
|
633 | self.getData(seconds=self.__delay) | |
630 |
|
634 | |||
631 | return |
|
635 | return | |
632 |
|
636 | |||
633 |
|
637 | |||
634 | class DigitalRFWriter(Operation): |
|
638 | class DigitalRFWriter(Operation): | |
635 | ''' |
|
639 | ''' | |
636 | classdocs |
|
640 | classdocs | |
637 | ''' |
|
641 | ''' | |
638 |
|
642 | |||
639 | def __init__(self, **kwargs): |
|
643 | def __init__(self, **kwargs): | |
640 | ''' |
|
644 | ''' | |
641 | Constructor |
|
645 | Constructor | |
642 | ''' |
|
646 | ''' | |
643 | Operation.__init__(self, **kwargs) |
|
647 | Operation.__init__(self, **kwargs) | |
644 | self.metadata_dict = {} |
|
648 | self.metadata_dict = {} | |
645 | self.dataOut = None |
|
649 | self.dataOut = None | |
646 | self.dtype = None |
|
650 | self.dtype = None | |
647 | self.oldAverage = 0 |
|
651 | self.oldAverage = 0 | |
648 |
|
652 | |||
649 | def setHeader(self): |
|
653 | def setHeader(self): | |
650 |
|
654 | |||
651 | self.metadata_dict['frequency'] = self.dataOut.frequency |
|
655 | self.metadata_dict['frequency'] = self.dataOut.frequency | |
652 | self.metadata_dict['timezone'] = self.dataOut.timeZone |
|
656 | self.metadata_dict['timezone'] = self.dataOut.timeZone | |
653 | self.metadata_dict['dtype'] = pickle.dumps(self.dataOut.dtype) |
|
657 | self.metadata_dict['dtype'] = pickle.dumps(self.dataOut.dtype) | |
654 | self.metadata_dict['nProfiles'] = self.dataOut.nProfiles |
|
658 | self.metadata_dict['nProfiles'] = self.dataOut.nProfiles | |
655 | self.metadata_dict['heightList'] = self.dataOut.heightList |
|
659 | self.metadata_dict['heightList'] = self.dataOut.heightList | |
656 | self.metadata_dict['channelList'] = self.dataOut.channelList |
|
660 | self.metadata_dict['channelList'] = self.dataOut.channelList | |
657 | self.metadata_dict['flagDecodeData'] = self.dataOut.flagDecodeData |
|
661 | self.metadata_dict['flagDecodeData'] = self.dataOut.flagDecodeData | |
658 | self.metadata_dict['flagDeflipData'] = self.dataOut.flagDeflipData |
|
662 | self.metadata_dict['flagDeflipData'] = self.dataOut.flagDeflipData | |
659 | self.metadata_dict['flagShiftFFT'] = self.dataOut.flagShiftFFT |
|
663 | self.metadata_dict['flagShiftFFT'] = self.dataOut.flagShiftFFT | |
660 | self.metadata_dict['useLocalTime'] = self.dataOut.useLocalTime |
|
664 | self.metadata_dict['useLocalTime'] = self.dataOut.useLocalTime | |
661 | self.metadata_dict['nCohInt'] = self.dataOut.nCohInt |
|
665 | self.metadata_dict['nCohInt'] = self.dataOut.nCohInt | |
662 | self.metadata_dict['type'] = self.dataOut.type |
|
666 | self.metadata_dict['type'] = self.dataOut.type | |
663 | self.metadata_dict['flagDataAsBlock'] = getattr( |
|
667 | self.metadata_dict['flagDataAsBlock'] = getattr( | |
664 | self.dataOut, 'flagDataAsBlock', None) # chequear |
|
668 | self.dataOut, 'flagDataAsBlock', None) # chequear | |
665 |
|
669 | |||
666 | def setup(self, dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, set=0, metadataFile='metadata', ext='.h5'): |
|
670 | def setup(self, dataOut, path, frequency, fileCadence, dirCadence, metadataCadence, set=0, metadataFile='metadata', ext='.h5'): | |
667 | ''' |
|
671 | ''' | |
668 | In this method we should set all initial parameters. |
|
672 | In this method we should set all initial parameters. | |
669 | Input: |
|
673 | Input: | |
670 | dataOut: Input data will also be outputa data |
|
674 | dataOut: Input data will also be outputa data | |
671 | ''' |
|
675 | ''' | |
672 | self.setHeader() |
|
676 | self.setHeader() | |
673 | self.__ippSeconds = dataOut.ippSeconds |
|
677 | self.__ippSeconds = dataOut.ippSeconds | |
674 | self.__deltaH = dataOut.getDeltaH() |
|
678 | self.__deltaH = dataOut.getDeltaH() | |
675 | self.__sample_rate = 1e6 * 0.15 / self.__deltaH |
|
679 | self.__sample_rate = 1e6 * 0.15 / self.__deltaH | |
676 | self.__dtype = dataOut.dtype |
|
680 | self.__dtype = dataOut.dtype | |
677 | if len(dataOut.dtype) == 2: |
|
681 | if len(dataOut.dtype) == 2: | |
678 | self.__dtype = dataOut.dtype[0] |
|
682 | self.__dtype = dataOut.dtype[0] | |
679 | self.__nSamples = dataOut.systemHeaderObj.nSamples |
|
683 | self.__nSamples = dataOut.systemHeaderObj.nSamples | |
680 | self.__nProfiles = dataOut.nProfiles |
|
684 | self.__nProfiles = dataOut.nProfiles | |
681 |
|
685 | |||
682 | if self.dataOut.type != 'Voltage': |
|
686 | if self.dataOut.type != 'Voltage': | |
683 | raise 'Digital RF cannot be used with this data type' |
|
687 | raise 'Digital RF cannot be used with this data type' | |
684 | self.arr_data = numpy.ones((1, dataOut.nFFTPoints * len( |
|
688 | self.arr_data = numpy.ones((1, dataOut.nFFTPoints * len( | |
685 | self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)]) |
|
689 | self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)]) | |
686 | else: |
|
690 | else: | |
687 | self.arr_data = numpy.ones((self.__nSamples, len( |
|
691 | self.arr_data = numpy.ones((self.__nSamples, len( | |
688 | self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)]) |
|
692 | self.dataOut.channelList)), dtype=[('r', self.__dtype), ('i', self.__dtype)]) | |
689 |
|
693 | |||
690 | file_cadence_millisecs = 1000 |
|
694 | file_cadence_millisecs = 1000 | |
691 |
|
695 | |||
692 | sample_rate_fraction = Fraction(self.__sample_rate).limit_denominator() |
|
696 | sample_rate_fraction = Fraction(self.__sample_rate).limit_denominator() | |
693 | sample_rate_numerator = int(sample_rate_fraction.numerator) |
|
697 | sample_rate_numerator = int(sample_rate_fraction.numerator) | |
694 | sample_rate_denominator = int(sample_rate_fraction.denominator) |
|
698 | sample_rate_denominator = int(sample_rate_fraction.denominator) | |
695 | start_global_index = dataOut.utctime * self.__sample_rate |
|
699 | start_global_index = dataOut.utctime * self.__sample_rate | |
696 |
|
700 | |||
697 | uuid = 'prueba' |
|
701 | uuid = 'prueba' | |
698 | compression_level = 0 |
|
702 | compression_level = 0 | |
699 | checksum = False |
|
703 | checksum = False | |
700 | is_complex = True |
|
704 | is_complex = True | |
701 | num_subchannels = len(dataOut.channelList) |
|
705 | num_subchannels = len(dataOut.channelList) | |
702 | is_continuous = True |
|
706 | is_continuous = True | |
703 | marching_periods = False |
|
707 | marching_periods = False | |
704 |
|
708 | |||
705 | self.digitalWriteObj = digital_rf.DigitalRFWriter(path, self.__dtype, dirCadence, |
|
709 | self.digitalWriteObj = digital_rf.DigitalRFWriter(path, self.__dtype, dirCadence, | |
706 | fileCadence, start_global_index, |
|
710 | fileCadence, start_global_index, | |
707 | sample_rate_numerator, sample_rate_denominator, uuid, compression_level, checksum, |
|
711 | sample_rate_numerator, sample_rate_denominator, uuid, compression_level, checksum, | |
708 | is_complex, num_subchannels, is_continuous, marching_periods) |
|
712 | is_complex, num_subchannels, is_continuous, marching_periods) | |
709 | metadata_dir = os.path.join(path, 'metadata') |
|
713 | metadata_dir = os.path.join(path, 'metadata') | |
710 | os.system('mkdir %s' % (metadata_dir)) |
|
714 | os.system('mkdir %s' % (metadata_dir)) | |
711 | self.digitalMetadataWriteObj = digital_rf.DigitalMetadataWriter(metadata_dir, dirCadence, 1, # 236, file_cadence_millisecs / 1000 |
|
715 | self.digitalMetadataWriteObj = digital_rf.DigitalMetadataWriter(metadata_dir, dirCadence, 1, # 236, file_cadence_millisecs / 1000 | |
712 | sample_rate_numerator, sample_rate_denominator, |
|
716 | sample_rate_numerator, sample_rate_denominator, | |
713 | metadataFile) |
|
717 | metadataFile) | |
714 | self.isConfig = True |
|
718 | self.isConfig = True | |
715 | self.currentSample = 0 |
|
719 | self.currentSample = 0 | |
716 | self.oldAverage = 0 |
|
720 | self.oldAverage = 0 | |
717 | self.count = 0 |
|
721 | self.count = 0 | |
718 | return |
|
722 | return | |
719 |
|
723 | |||
720 | def writeMetadata(self): |
|
724 | def writeMetadata(self): | |
721 | start_idx = self.__sample_rate * self.dataOut.utctime |
|
725 | start_idx = self.__sample_rate * self.dataOut.utctime | |
722 |
|
726 | |||
723 | self.metadata_dict['processingHeader'] = self.dataOut.processingHeaderObj.getAsDict( |
|
727 | self.metadata_dict['processingHeader'] = self.dataOut.processingHeaderObj.getAsDict( | |
724 | ) |
|
728 | ) | |
725 | self.metadata_dict['radarControllerHeader'] = self.dataOut.radarControllerHeaderObj.getAsDict( |
|
729 | self.metadata_dict['radarControllerHeader'] = self.dataOut.radarControllerHeaderObj.getAsDict( | |
726 | ) |
|
730 | ) | |
727 | self.metadata_dict['systemHeader'] = self.dataOut.systemHeaderObj.getAsDict( |
|
731 | self.metadata_dict['systemHeader'] = self.dataOut.systemHeaderObj.getAsDict( | |
728 | ) |
|
732 | ) | |
729 | self.digitalMetadataWriteObj.write(start_idx, self.metadata_dict) |
|
733 | self.digitalMetadataWriteObj.write(start_idx, self.metadata_dict) | |
730 | return |
|
734 | return | |
731 |
|
735 | |||
732 | def timeit(self, toExecute): |
|
736 | def timeit(self, toExecute): | |
733 | t0 = time() |
|
737 | t0 = time() | |
734 | toExecute() |
|
738 | toExecute() | |
735 | self.executionTime = time() - t0 |
|
739 | self.executionTime = time() - t0 | |
736 | if self.oldAverage is None: |
|
740 | if self.oldAverage is None: | |
737 | self.oldAverage = self.executionTime |
|
741 | self.oldAverage = self.executionTime | |
738 | self.oldAverage = (self.executionTime + self.count * |
|
742 | self.oldAverage = (self.executionTime + self.count * | |
739 | self.oldAverage) / (self.count + 1.0) |
|
743 | self.oldAverage) / (self.count + 1.0) | |
740 | self.count = self.count + 1.0 |
|
744 | self.count = self.count + 1.0 | |
741 | return |
|
745 | return | |
742 |
|
746 | |||
743 | def writeData(self): |
|
747 | def writeData(self): | |
744 | if self.dataOut.type != 'Voltage': |
|
748 | if self.dataOut.type != 'Voltage': | |
745 | raise 'Digital RF cannot be used with this data type' |
|
749 | raise 'Digital RF cannot be used with this data type' | |
746 | for channel in self.dataOut.channelList: |
|
750 | for channel in self.dataOut.channelList: | |
747 | for i in range(self.dataOut.nFFTPoints): |
|
751 | for i in range(self.dataOut.nFFTPoints): | |
748 | self.arr_data[1][channel * self.dataOut.nFFTPoints + |
|
752 | self.arr_data[1][channel * self.dataOut.nFFTPoints + | |
749 | i]['r'] = self.dataOut.data[channel][i].real |
|
753 | i]['r'] = self.dataOut.data[channel][i].real | |
750 | self.arr_data[1][channel * self.dataOut.nFFTPoints + |
|
754 | self.arr_data[1][channel * self.dataOut.nFFTPoints + | |
751 | i]['i'] = self.dataOut.data[channel][i].imag |
|
755 | i]['i'] = self.dataOut.data[channel][i].imag | |
752 | else: |
|
756 | else: | |
753 | for i in range(self.dataOut.systemHeaderObj.nSamples): |
|
757 | for i in range(self.dataOut.systemHeaderObj.nSamples): | |
754 | for channel in self.dataOut.channelList: |
|
758 | for channel in self.dataOut.channelList: | |
755 | self.arr_data[i][channel]['r'] = self.dataOut.data[channel][i].real |
|
759 | self.arr_data[i][channel]['r'] = self.dataOut.data[channel][i].real | |
756 | self.arr_data[i][channel]['i'] = self.dataOut.data[channel][i].imag |
|
760 | self.arr_data[i][channel]['i'] = self.dataOut.data[channel][i].imag | |
757 |
|
761 | |||
758 | def f(): return self.digitalWriteObj.rf_write(self.arr_data) |
|
762 | def f(): return self.digitalWriteObj.rf_write(self.arr_data) | |
759 | self.timeit(f) |
|
763 | self.timeit(f) | |
760 |
|
764 | |||
761 | return |
|
765 | return | |
762 |
|
766 | |||
763 | def run(self, dataOut, frequency=49.92e6, path=None, fileCadence=1000, dirCadence=36000, metadataCadence=1, **kwargs): |
|
767 | def run(self, dataOut, frequency=49.92e6, path=None, fileCadence=1000, dirCadence=36000, metadataCadence=1, **kwargs): | |
764 | ''' |
|
768 | ''' | |
765 | This method will be called many times so here you should put all your code |
|
769 | This method will be called many times so here you should put all your code | |
766 | Inputs: |
|
770 | Inputs: | |
767 | dataOut: object with the data |
|
771 | dataOut: object with the data | |
768 | ''' |
|
772 | ''' | |
769 | # print dataOut.__dict__ |
|
773 | # print dataOut.__dict__ | |
770 | self.dataOut = dataOut |
|
774 | self.dataOut = dataOut | |
771 | if not self.isConfig: |
|
775 | if not self.isConfig: | |
772 | self.setup(dataOut, path, frequency, fileCadence, |
|
776 | self.setup(dataOut, path, frequency, fileCadence, | |
773 | dirCadence, metadataCadence, **kwargs) |
|
777 | dirCadence, metadataCadence, **kwargs) | |
774 | self.writeMetadata() |
|
778 | self.writeMetadata() | |
775 |
|
779 | |||
776 | self.writeData() |
|
780 | self.writeData() | |
777 |
|
781 | |||
778 | ## self.currentSample += 1 |
|
782 | ## self.currentSample += 1 | |
779 | # if self.dataOut.flagDataAsBlock or self.currentSample == 1: |
|
783 | # if self.dataOut.flagDataAsBlock or self.currentSample == 1: | |
780 | # self.writeMetadata() |
|
784 | # self.writeMetadata() | |
781 | ## if self.currentSample == self.__nProfiles: self.currentSample = 0 |
|
785 | ## if self.currentSample == self.__nProfiles: self.currentSample = 0 | |
782 |
|
786 | |||
|
787 | return dataOut | |||
|
788 | ||||
783 | def close(self): |
|
789 | def close(self): | |
784 | print('[Writing] - Closing files ') |
|
790 | print('[Writing] - Closing files ') | |
785 | print('Average of writing to digital rf format is ', self.oldAverage * 1000) |
|
791 | print('Average of writing to digital rf format is ', self.oldAverage * 1000) | |
786 | try: |
|
792 | try: | |
787 | self.digitalWriteObj.close() |
|
793 | self.digitalWriteObj.close() | |
788 | except: |
|
794 | except: | |
789 | pass |
|
795 | pass | |
790 |
|
796 | |||
791 |
|
797 | |||
792 | # raise |
|
|||
793 | if __name__ == '__main__': |
|
|||
794 |
|
||||
795 | readObj = DigitalRFReader() |
|
|||
796 |
|
||||
797 | while True: |
|
|||
798 | readObj.run(path='/home/jchavez/jicamarca/mocked_data/') |
|
|||
799 | # readObj.printInfo() |
|
|||
800 | # readObj.printNumberOfBlock() No newline at end of file |
|
General Comments 0
You need to be logged in to leave comments.
Login now