@@ -1,131 +1,132 | |||
|
1 | 1 | # Signal Chain |
|
2 | 2 | |
|
3 | 3 | ## Introduction |
|
4 | 4 | |
|
5 | 5 | Signal Chain (SCh) is a radar data processing library developed using [Python](www.python.org) at JRO. SCh provides modules to read, write, process and plot data. |
|
6 | 6 | |
|
7 | 7 | ## Installation |
|
8 | 8 | |
|
9 |
Install system dependencies, |
|
|
9 | Install system dependencies, clone the latest version from [git](http://jro-dev.igp.gob.pe/rhodecode/schain/) and install it as a normal python package. | |
|
10 | 10 | |
|
11 | 11 | ``` |
|
12 | 12 | $ sudo apt-get install python-pip python-dev gfortran libpng-dev freetype* libblas-dev liblapack-dev libatlas-base-dev python-qt4 python-tk libssl-dev libhdf5-dev |
|
13 | $ tar xvzf schainpy-2.2.5.tar.gz | |
|
14 | $ cd schainpy-2.2.5 | |
|
13 | $ sudo pip install numpy | |
|
14 | $ git clone http://jro-dev.igp.gob.pe/rhodecode/schain/ | |
|
15 | $ cd schain | |
|
15 | 16 | $ sudo pip install ./ |
|
16 | 17 | ``` |
|
17 | 18 | |
|
18 | 19 | **Its recommended to install schain in a virtual environment** |
|
19 | 20 | |
|
20 | 21 | ``` |
|
21 | 22 | $ sudo pip install virtualenv |
|
22 | 23 | $ virtualenv /path/to/virtual --system-site-packages |
|
23 | 24 | $ source /path/to/virtual/bin/activate |
|
24 |
(virtual) $ cd schain |
|
|
25 | (virtual) $ cd schain | |
|
25 | 26 | (virtual) $ pip install ./ |
|
26 | 27 | ``` |
|
27 | 28 | |
|
28 | 29 | ## First Script |
|
29 | 30 | |
|
30 | 31 | Read Spectra data (.pdata) - remove dc - plot spectra & RTI |
|
31 | 32 | |
|
32 | 33 | Import SCh and creating a project |
|
33 | 34 | |
|
34 | 35 | ```python |
|
35 | 36 | #!/usr/bin/python |
|
36 | 37 | |
|
37 | 38 | from schainpy.controller import Project |
|
38 | 39 | |
|
39 | 40 | controller = Project() |
|
40 | 41 | controller.setup(id = '100', |
|
41 | 42 | name='test', |
|
42 | 43 | description='Basic experiment') |
|
43 | 44 | |
|
44 | 45 | |
|
45 | 46 | ``` |
|
46 | 47 | |
|
47 | 48 | Adding read unit and operations |
|
48 | 49 | |
|
49 | 50 | ```python |
|
50 | 51 | read_unit = controller.addReadUnit(datatype='Spectra', |
|
51 | 52 | path='/path/to/pdata/', |
|
52 | 53 | startDate='2014/01/31', |
|
53 | 54 | endDate='2014/03/31', |
|
54 | 55 | startTime='00:00:00', |
|
55 | 56 | endTime='23:59:59', |
|
56 | 57 | online=0, |
|
57 | 58 | walk=0) |
|
58 | 59 | |
|
59 | 60 | proc_unit = controller.addProcUnit(datatype='Spectra', |
|
60 | 61 | inputId=read_unit.getId()) |
|
61 | 62 | |
|
62 | 63 | op = proc_unit.addOperation(name='selectChannels') |
|
63 | 64 | op.addParameter(name='channelList', value='0,1', format='intlist') |
|
64 | 65 | |
|
65 | 66 | op = proc_unit.addOperation(name='selectHeights') |
|
66 | 67 | op.addParameter(name='minHei', value='80', format='float') |
|
67 | 68 | op.addParameter(name='maxHei', value='200', format='float') |
|
68 | 69 | |
|
69 | 70 | op = proc_unit.addOperation(name='removeDC') |
|
70 | 71 | |
|
71 | 72 | ``` |
|
72 | 73 | |
|
73 | 74 | Plotting data & start project |
|
74 | 75 | |
|
75 | 76 | ```python |
|
76 | 77 | op = proc_unit.addOperation(name='SpectraPlot', optype='other') |
|
77 | 78 | op.addParameter(name='id', value='1', format='int') |
|
78 | 79 | op.addParameter(name='wintitle', value='Spectra', format='str') |
|
79 | 80 | |
|
80 | 81 | op = procUnitConfObj1.addOperation(name='RTIPlot', optype='other') |
|
81 | 82 | op.addParameter(name='id', value='2', format='int') |
|
82 | 83 | op.addParameter(name='wintitle', value='RTI', format='str') |
|
83 | 84 | |
|
84 | 85 | controller.start() |
|
85 | 86 | |
|
86 | 87 | ``` |
|
87 | 88 | |
|
88 | 89 | Full script |
|
89 | 90 | |
|
90 | 91 | |
|
91 | 92 | ```python |
|
92 | 93 | #!/usr/bin/python |
|
93 | 94 | |
|
94 | 95 | from schainpy.controller import Project |
|
95 | 96 | |
|
96 | 97 | controller = Project() |
|
97 | 98 | controller.setup(id = '100', |
|
98 | 99 | name='test', |
|
99 | 100 | description='Basic experiment') |
|
100 | 101 | read_unit = controller.addReadUnit(datatype='Spectra', |
|
101 | 102 | path='/path/to/pdata/', |
|
102 | 103 | startDate='2014/01/31', |
|
103 | 104 | endDate='2014/03/31', |
|
104 | 105 | startTime='00:00:00', |
|
105 | 106 | endTime='23:59:59', |
|
106 | 107 | online=0, |
|
107 | 108 | walk=0) |
|
108 | 109 | |
|
109 | 110 | proc_unit = controller.addProcUnit(datatype='Spectra', |
|
110 | 111 | inputId=read_unit.getId()) |
|
111 | 112 | |
|
112 | 113 | op = proc_unit.addOperation(name='selectChannels') |
|
113 | 114 | op.addParameter(name='channelList', value='0,1', format='intlist') |
|
114 | 115 | |
|
115 | 116 | op = proc_unit.addOperation(name='selectHeights') |
|
116 | 117 | op.addParameter(name='minHei', value='80', format='float') |
|
117 | 118 | op.addParameter(name='maxHei', value='200', format='float') |
|
118 | 119 | |
|
119 | 120 | op = proc_unit.addOperation(name='removeDC') |
|
120 | 121 | |
|
121 | 122 | op = proc_unit.addOperation(name='SpectraPlot', optype='other') |
|
122 | 123 | op.addParameter(name='id', value='6', format='int') |
|
123 | 124 | op.addParameter(name='wintitle', value='Spectra', format='str') |
|
124 | 125 | |
|
125 | 126 | op = procUnitConfObj1.addOperation(name='RTIPlot', optype='other') |
|
126 | 127 | op.addParameter(name='id', value='2', format='int') |
|
127 | 128 | op.addParameter(name='wintitle', value='RTI', format='str') |
|
128 | 129 | |
|
129 | 130 | controller.start() |
|
130 | 131 | |
|
131 | 132 | ``` |
@@ -1,1324 +1,1326 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on September , 2012 |
|
3 | 3 | @author: |
|
4 | 4 | ''' |
|
5 | 5 | |
|
6 | 6 | import sys |
|
7 | 7 | import ast |
|
8 | 8 | import datetime |
|
9 | 9 | import traceback |
|
10 | 10 | import math |
|
11 | 11 | import time |
|
12 | 12 | from multiprocessing import Process, Queue, cpu_count |
|
13 | 13 | |
|
14 | 14 | import schainpy |
|
15 | 15 | import schainpy.admin |
|
16 | 16 | |
|
17 | 17 | from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring |
|
18 | 18 | from xml.dom import minidom |
|
19 | 19 | |
|
20 | 20 | from schainpy.model import * |
|
21 | 21 | from time import sleep |
|
22 | 22 | |
|
23 | 23 | def prettify(elem): |
|
24 | 24 | """Return a pretty-printed XML string for the Element. |
|
25 | 25 | """ |
|
26 | 26 | rough_string = tostring(elem, 'utf-8') |
|
27 | 27 | reparsed = minidom.parseString(rough_string) |
|
28 | 28 | return reparsed.toprettyxml(indent=" ") |
|
29 | 29 | |
|
30 | 30 | def multiSchain(child, nProcess=cpu_count(), startDate=None, endDate=None, by_day=False): |
|
31 | 31 | skip = 0 |
|
32 | 32 | cursor = 0 |
|
33 | 33 | nFiles = None |
|
34 | 34 | processes = [] |
|
35 | 35 | dt1 = datetime.datetime.strptime(startDate, '%Y/%m/%d') |
|
36 | 36 | dt2 = datetime.datetime.strptime(endDate, '%Y/%m/%d') |
|
37 | 37 | days = (dt2 - dt1).days |
|
38 | 38 | |
|
39 | 39 | for day in range(days+1): |
|
40 | 40 | skip = 0 |
|
41 | 41 | cursor = 0 |
|
42 | 42 | q = Queue() |
|
43 | 43 | processes = [] |
|
44 | 44 | dt = (dt1 + datetime.timedelta(day)).strftime('%Y/%m/%d') |
|
45 | 45 | firstProcess = Process(target=child, args=(cursor, skip, q, dt)) |
|
46 | 46 | firstProcess.start() |
|
47 | 47 | if by_day: |
|
48 | 48 | continue |
|
49 | 49 | nFiles = q.get() |
|
50 | if nFiles==0: | |
|
51 | continue | |
|
50 | 52 | firstProcess.terminate() |
|
51 | 53 | skip = int(math.ceil(nFiles/nProcess)) |
|
52 | 54 | while True: |
|
53 | 55 | processes.append(Process(target=child, args=(cursor, skip, q, dt))) |
|
54 | 56 | processes[cursor].start() |
|
55 | 57 | if nFiles < cursor*skip: |
|
56 | 58 | break |
|
57 | 59 | cursor += 1 |
|
58 | 60 | |
|
59 | 61 | def beforeExit(exctype, value, trace): |
|
60 | 62 | for process in processes: |
|
61 | 63 | process.terminate() |
|
62 | 64 | process.join() |
|
63 | 65 | print traceback.print_tb(trace) |
|
64 | 66 | |
|
65 | 67 | sys.excepthook = beforeExit |
|
66 | 68 | |
|
67 | 69 | for process in processes: |
|
68 | 70 | process.join() |
|
69 | 71 | process.terminate() |
|
70 | 72 | time.sleep(3) |
|
71 | 73 | |
|
72 | 74 | class ParameterConf(): |
|
73 | 75 | |
|
74 | 76 | id = None |
|
75 | 77 | name = None |
|
76 | 78 | value = None |
|
77 | 79 | format = None |
|
78 | 80 | |
|
79 | 81 | __formated_value = None |
|
80 | 82 | |
|
81 | 83 | ELEMENTNAME = 'Parameter' |
|
82 | 84 | |
|
83 | 85 | def __init__(self): |
|
84 | 86 | |
|
85 | 87 | self.format = 'str' |
|
86 | 88 | |
|
87 | 89 | def getElementName(self): |
|
88 | 90 | |
|
89 | 91 | return self.ELEMENTNAME |
|
90 | 92 | |
|
91 | 93 | def getValue(self): |
|
92 | 94 | |
|
93 | 95 | value = self.value |
|
94 | 96 | format = self.format |
|
95 | 97 | |
|
96 | 98 | if self.__formated_value != None: |
|
97 | 99 | |
|
98 | 100 | return self.__formated_value |
|
99 | 101 | |
|
100 | 102 | if format == 'obj': |
|
101 | 103 | return value |
|
102 | 104 | |
|
103 | 105 | if format == 'str': |
|
104 | 106 | self.__formated_value = str(value) |
|
105 | 107 | return self.__formated_value |
|
106 | 108 | |
|
107 | 109 | if value == '': |
|
108 | 110 | raise ValueError, "%s: This parameter value is empty" %self.name |
|
109 | 111 | |
|
110 | 112 | if format == 'list': |
|
111 | 113 | strList = value.split(',') |
|
112 | 114 | |
|
113 | 115 | self.__formated_value = strList |
|
114 | 116 | |
|
115 | 117 | return self.__formated_value |
|
116 | 118 | |
|
117 | 119 | if format == 'intlist': |
|
118 | 120 | """ |
|
119 | 121 | Example: |
|
120 | 122 | value = (0,1,2) |
|
121 | 123 | """ |
|
122 | 124 | |
|
123 | 125 | new_value = ast.literal_eval(value) |
|
124 | 126 | |
|
125 | 127 | if type(new_value) not in (tuple, list): |
|
126 | 128 | new_value = [int(new_value)] |
|
127 | 129 | |
|
128 | 130 | self.__formated_value = new_value |
|
129 | 131 | |
|
130 | 132 | return self.__formated_value |
|
131 | 133 | |
|
132 | 134 | if format == 'floatlist': |
|
133 | 135 | """ |
|
134 | 136 | Example: |
|
135 | 137 | value = (0.5, 1.4, 2.7) |
|
136 | 138 | """ |
|
137 | 139 | |
|
138 | 140 | new_value = ast.literal_eval(value) |
|
139 | 141 | |
|
140 | 142 | if type(new_value) not in (tuple, list): |
|
141 | 143 | new_value = [float(new_value)] |
|
142 | 144 | |
|
143 | 145 | self.__formated_value = new_value |
|
144 | 146 | |
|
145 | 147 | return self.__formated_value |
|
146 | 148 | |
|
147 | 149 | if format == 'date': |
|
148 | 150 | strList = value.split('/') |
|
149 | 151 | intList = [int(x) for x in strList] |
|
150 | 152 | date = datetime.date(intList[0], intList[1], intList[2]) |
|
151 | 153 | |
|
152 | 154 | self.__formated_value = date |
|
153 | 155 | |
|
154 | 156 | return self.__formated_value |
|
155 | 157 | |
|
156 | 158 | if format == 'time': |
|
157 | 159 | strList = value.split(':') |
|
158 | 160 | intList = [int(x) for x in strList] |
|
159 | 161 | time = datetime.time(intList[0], intList[1], intList[2]) |
|
160 | 162 | |
|
161 | 163 | self.__formated_value = time |
|
162 | 164 | |
|
163 | 165 | return self.__formated_value |
|
164 | 166 | |
|
165 | 167 | if format == 'pairslist': |
|
166 | 168 | """ |
|
167 | 169 | Example: |
|
168 | 170 | value = (0,1),(1,2) |
|
169 | 171 | """ |
|
170 | 172 | |
|
171 | 173 | new_value = ast.literal_eval(value) |
|
172 | 174 | |
|
173 | 175 | if type(new_value) not in (tuple, list): |
|
174 | 176 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
175 | 177 | |
|
176 | 178 | if type(new_value[0]) not in (tuple, list): |
|
177 | 179 | if len(new_value) != 2: |
|
178 | 180 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
179 | 181 | new_value = [new_value] |
|
180 | 182 | |
|
181 | 183 | for thisPair in new_value: |
|
182 | 184 | if len(thisPair) != 2: |
|
183 | 185 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
184 | 186 | |
|
185 | 187 | self.__formated_value = new_value |
|
186 | 188 | |
|
187 | 189 | return self.__formated_value |
|
188 | 190 | |
|
189 | 191 | if format == 'multilist': |
|
190 | 192 | """ |
|
191 | 193 | Example: |
|
192 | 194 | value = (0,1,2),(3,4,5) |
|
193 | 195 | """ |
|
194 | 196 | multiList = ast.literal_eval(value) |
|
195 | 197 | |
|
196 | 198 | if type(multiList[0]) == int: |
|
197 | 199 | multiList = ast.literal_eval("(" + value + ")") |
|
198 | 200 | |
|
199 | 201 | self.__formated_value = multiList |
|
200 | 202 | |
|
201 | 203 | return self.__formated_value |
|
202 | 204 | |
|
203 | 205 | if format == 'bool': |
|
204 | 206 | value = int(value) |
|
205 | 207 | |
|
206 | 208 | if format == 'int': |
|
207 | 209 | value = float(value) |
|
208 | 210 | |
|
209 | 211 | format_func = eval(format) |
|
210 | 212 | |
|
211 | 213 | self.__formated_value = format_func(value) |
|
212 | 214 | |
|
213 | 215 | return self.__formated_value |
|
214 | 216 | |
|
215 | 217 | def updateId(self, new_id): |
|
216 | 218 | |
|
217 | 219 | self.id = str(new_id) |
|
218 | 220 | |
|
219 | 221 | def setup(self, id, name, value, format='str'): |
|
220 | 222 | self.id = str(id) |
|
221 | 223 | self.name = name |
|
222 | 224 | if format == 'obj': |
|
223 | 225 | self.value = value |
|
224 | 226 | else: |
|
225 | 227 | self.value = str(value) |
|
226 | 228 | self.format = str.lower(format) |
|
227 | 229 | |
|
228 | 230 | self.getValue() |
|
229 | 231 | |
|
230 | 232 | return 1 |
|
231 | 233 | |
|
232 | 234 | def update(self, name, value, format='str'): |
|
233 | 235 | |
|
234 | 236 | self.name = name |
|
235 | 237 | self.value = str(value) |
|
236 | 238 | self.format = format |
|
237 | 239 | |
|
238 | 240 | def makeXml(self, opElement): |
|
239 | 241 | if self.name not in ('queue',): |
|
240 | 242 | parmElement = SubElement(opElement, self.ELEMENTNAME) |
|
241 | 243 | parmElement.set('id', str(self.id)) |
|
242 | 244 | parmElement.set('name', self.name) |
|
243 | 245 | parmElement.set('value', self.value) |
|
244 | 246 | parmElement.set('format', self.format) |
|
245 | 247 | |
|
246 | 248 | def readXml(self, parmElement): |
|
247 | 249 | |
|
248 | 250 | self.id = parmElement.get('id') |
|
249 | 251 | self.name = parmElement.get('name') |
|
250 | 252 | self.value = parmElement.get('value') |
|
251 | 253 | self.format = str.lower(parmElement.get('format')) |
|
252 | 254 | |
|
253 | 255 | #Compatible with old signal chain version |
|
254 | 256 | if self.format == 'int' and self.name == 'idfigure': |
|
255 | 257 | self.name = 'id' |
|
256 | 258 | |
|
257 | 259 | def printattr(self): |
|
258 | 260 | |
|
259 | 261 | print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format) |
|
260 | 262 | |
|
261 | 263 | class OperationConf(): |
|
262 | 264 | |
|
263 | 265 | id = None |
|
264 | 266 | name = None |
|
265 | 267 | priority = None |
|
266 | 268 | type = None |
|
267 | 269 | |
|
268 | 270 | parmConfObjList = [] |
|
269 | 271 | |
|
270 | 272 | ELEMENTNAME = 'Operation' |
|
271 | 273 | |
|
272 | 274 | def __init__(self): |
|
273 | 275 | |
|
274 | 276 | self.id = '0' |
|
275 | 277 | self.name = None |
|
276 | 278 | self.priority = None |
|
277 | 279 | self.type = 'self' |
|
278 | 280 | |
|
279 | 281 | |
|
280 | 282 | def __getNewId(self): |
|
281 | 283 | |
|
282 | 284 | return int(self.id)*10 + len(self.parmConfObjList) + 1 |
|
283 | 285 | |
|
284 | 286 | def updateId(self, new_id): |
|
285 | 287 | |
|
286 | 288 | self.id = str(new_id) |
|
287 | 289 | |
|
288 | 290 | n = 1 |
|
289 | 291 | for parmObj in self.parmConfObjList: |
|
290 | 292 | |
|
291 | 293 | idParm = str(int(new_id)*10 + n) |
|
292 | 294 | parmObj.updateId(idParm) |
|
293 | 295 | |
|
294 | 296 | n += 1 |
|
295 | 297 | |
|
296 | 298 | def getElementName(self): |
|
297 | 299 | |
|
298 | 300 | return self.ELEMENTNAME |
|
299 | 301 | |
|
300 | 302 | def getParameterObjList(self): |
|
301 | 303 | |
|
302 | 304 | return self.parmConfObjList |
|
303 | 305 | |
|
304 | 306 | def getParameterObj(self, parameterName): |
|
305 | 307 | |
|
306 | 308 | for parmConfObj in self.parmConfObjList: |
|
307 | 309 | |
|
308 | 310 | if parmConfObj.name != parameterName: |
|
309 | 311 | continue |
|
310 | 312 | |
|
311 | 313 | return parmConfObj |
|
312 | 314 | |
|
313 | 315 | return None |
|
314 | 316 | |
|
315 | 317 | def getParameterObjfromValue(self, parameterValue): |
|
316 | 318 | |
|
317 | 319 | for parmConfObj in self.parmConfObjList: |
|
318 | 320 | |
|
319 | 321 | if parmConfObj.getValue() != parameterValue: |
|
320 | 322 | continue |
|
321 | 323 | |
|
322 | 324 | return parmConfObj.getValue() |
|
323 | 325 | |
|
324 | 326 | return None |
|
325 | 327 | |
|
326 | 328 | def getParameterValue(self, parameterName): |
|
327 | 329 | |
|
328 | 330 | parameterObj = self.getParameterObj(parameterName) |
|
329 | 331 | |
|
330 | 332 | # if not parameterObj: |
|
331 | 333 | # return None |
|
332 | 334 | |
|
333 | 335 | value = parameterObj.getValue() |
|
334 | 336 | |
|
335 | 337 | return value |
|
336 | 338 | |
|
337 | 339 | |
|
338 | 340 | def getKwargs(self): |
|
339 | 341 | |
|
340 | 342 | kwargs = {} |
|
341 | 343 | |
|
342 | 344 | for parmConfObj in self.parmConfObjList: |
|
343 | 345 | if self.name == 'run' and parmConfObj.name == 'datatype': |
|
344 | 346 | continue |
|
345 | 347 | |
|
346 | 348 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
347 | 349 | |
|
348 | 350 | return kwargs |
|
349 | 351 | |
|
350 | 352 | def setup(self, id, name, priority, type): |
|
351 | 353 | |
|
352 | 354 | self.id = str(id) |
|
353 | 355 | self.name = name |
|
354 | 356 | self.type = type |
|
355 | 357 | self.priority = priority |
|
356 | 358 | |
|
357 | 359 | self.parmConfObjList = [] |
|
358 | 360 | |
|
359 | 361 | def removeParameters(self): |
|
360 | 362 | |
|
361 | 363 | for obj in self.parmConfObjList: |
|
362 | 364 | del obj |
|
363 | 365 | |
|
364 | 366 | self.parmConfObjList = [] |
|
365 | 367 | |
|
366 | 368 | def addParameter(self, name, value, format='str'): |
|
367 | 369 | |
|
368 | 370 | id = self.__getNewId() |
|
369 | 371 | |
|
370 | 372 | parmConfObj = ParameterConf() |
|
371 | 373 | if not parmConfObj.setup(id, name, value, format): |
|
372 | 374 | return None |
|
373 | 375 | |
|
374 | 376 | self.parmConfObjList.append(parmConfObj) |
|
375 | 377 | |
|
376 | 378 | return parmConfObj |
|
377 | 379 | |
|
378 | 380 | def changeParameter(self, name, value, format='str'): |
|
379 | 381 | |
|
380 | 382 | parmConfObj = self.getParameterObj(name) |
|
381 | 383 | parmConfObj.update(name, value, format) |
|
382 | 384 | |
|
383 | 385 | return parmConfObj |
|
384 | 386 | |
|
385 | 387 | def makeXml(self, procUnitElement): |
|
386 | 388 | |
|
387 | 389 | opElement = SubElement(procUnitElement, self.ELEMENTNAME) |
|
388 | 390 | opElement.set('id', str(self.id)) |
|
389 | 391 | opElement.set('name', self.name) |
|
390 | 392 | opElement.set('type', self.type) |
|
391 | 393 | opElement.set('priority', str(self.priority)) |
|
392 | 394 | |
|
393 | 395 | for parmConfObj in self.parmConfObjList: |
|
394 | 396 | parmConfObj.makeXml(opElement) |
|
395 | 397 | |
|
396 | 398 | def readXml(self, opElement): |
|
397 | 399 | |
|
398 | 400 | self.id = opElement.get('id') |
|
399 | 401 | self.name = opElement.get('name') |
|
400 | 402 | self.type = opElement.get('type') |
|
401 | 403 | self.priority = opElement.get('priority') |
|
402 | 404 | |
|
403 | 405 | #Compatible with old signal chain version |
|
404 | 406 | #Use of 'run' method instead 'init' |
|
405 | 407 | if self.type == 'self' and self.name == 'init': |
|
406 | 408 | self.name = 'run' |
|
407 | 409 | |
|
408 | 410 | self.parmConfObjList = [] |
|
409 | 411 | |
|
410 | 412 | parmElementList = opElement.iter(ParameterConf().getElementName()) |
|
411 | 413 | |
|
412 | 414 | for parmElement in parmElementList: |
|
413 | 415 | parmConfObj = ParameterConf() |
|
414 | 416 | parmConfObj.readXml(parmElement) |
|
415 | 417 | |
|
416 | 418 | #Compatible with old signal chain version |
|
417 | 419 | #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER |
|
418 | 420 | if self.type != 'self' and self.name == 'Plot': |
|
419 | 421 | if parmConfObj.format == 'str' and parmConfObj.name == 'type': |
|
420 | 422 | self.name = parmConfObj.value |
|
421 | 423 | continue |
|
422 | 424 | |
|
423 | 425 | self.parmConfObjList.append(parmConfObj) |
|
424 | 426 | |
|
425 | 427 | def printattr(self): |
|
426 | 428 | |
|
427 | 429 | print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME, |
|
428 | 430 | self.id, |
|
429 | 431 | self.name, |
|
430 | 432 | self.type, |
|
431 | 433 | self.priority) |
|
432 | 434 | |
|
433 | 435 | for parmConfObj in self.parmConfObjList: |
|
434 | 436 | parmConfObj.printattr() |
|
435 | 437 | |
|
436 | 438 | def createObject(self, plotter_queue=None): |
|
437 | 439 | |
|
438 | 440 | |
|
439 | 441 | if self.type == 'self': |
|
440 | 442 | raise ValueError, "This operation type cannot be created" |
|
441 | 443 | |
|
442 | 444 | if self.type == 'plotter': |
|
443 | 445 | #Plotter(plotter_name) |
|
444 | 446 | if not plotter_queue: |
|
445 | 447 | raise ValueError, "plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)" |
|
446 | 448 | |
|
447 | 449 | opObj = Plotter(self.name, plotter_queue) |
|
448 | 450 | |
|
449 | 451 | if self.type == 'external' or self.type == 'other': |
|
450 | 452 | |
|
451 | 453 | className = eval(self.name) |
|
452 | 454 | kwargs = self.getKwargs() |
|
453 | 455 | |
|
454 | 456 | opObj = className(**kwargs) |
|
455 | 457 | |
|
456 | 458 | return opObj |
|
457 | 459 | |
|
458 | 460 | |
|
459 | 461 | class ProcUnitConf(): |
|
460 | 462 | |
|
461 | 463 | id = None |
|
462 | 464 | name = None |
|
463 | 465 | datatype = None |
|
464 | 466 | inputId = None |
|
465 | 467 | parentId = None |
|
466 | 468 | |
|
467 | 469 | opConfObjList = [] |
|
468 | 470 | |
|
469 | 471 | procUnitObj = None |
|
470 | 472 | opObjList = [] |
|
471 | 473 | |
|
472 | 474 | ELEMENTNAME = 'ProcUnit' |
|
473 | 475 | |
|
474 | 476 | def __init__(self): |
|
475 | 477 | |
|
476 | 478 | self.id = None |
|
477 | 479 | self.datatype = None |
|
478 | 480 | self.name = None |
|
479 | 481 | self.inputId = None |
|
480 | 482 | |
|
481 | 483 | self.opConfObjList = [] |
|
482 | 484 | |
|
483 | 485 | self.procUnitObj = None |
|
484 | 486 | self.opObjDict = {} |
|
485 | 487 | |
|
486 | 488 | def __getPriority(self): |
|
487 | 489 | |
|
488 | 490 | return len(self.opConfObjList)+1 |
|
489 | 491 | |
|
490 | 492 | def __getNewId(self): |
|
491 | 493 | |
|
492 | 494 | return int(self.id)*10 + len(self.opConfObjList) + 1 |
|
493 | 495 | |
|
494 | 496 | def getElementName(self): |
|
495 | 497 | |
|
496 | 498 | return self.ELEMENTNAME |
|
497 | 499 | |
|
498 | 500 | def getId(self): |
|
499 | 501 | |
|
500 | 502 | return self.id |
|
501 | 503 | |
|
502 | 504 | def updateId(self, new_id, parentId=parentId): |
|
503 | 505 | |
|
504 | 506 | |
|
505 | 507 | new_id = int(parentId)*10 + (int(self.id) % 10) |
|
506 | 508 | new_inputId = int(parentId)*10 + (int(self.inputId) % 10) |
|
507 | 509 | |
|
508 | 510 | #If this proc unit has not inputs |
|
509 | 511 | if self.inputId == '0': |
|
510 | 512 | new_inputId = 0 |
|
511 | 513 | |
|
512 | 514 | n = 1 |
|
513 | 515 | for opConfObj in self.opConfObjList: |
|
514 | 516 | |
|
515 | 517 | idOp = str(int(new_id)*10 + n) |
|
516 | 518 | opConfObj.updateId(idOp) |
|
517 | 519 | |
|
518 | 520 | n += 1 |
|
519 | 521 | |
|
520 | 522 | self.parentId = str(parentId) |
|
521 | 523 | self.id = str(new_id) |
|
522 | 524 | self.inputId = str(new_inputId) |
|
523 | 525 | |
|
524 | 526 | |
|
525 | 527 | def getInputId(self): |
|
526 | 528 | |
|
527 | 529 | return self.inputId |
|
528 | 530 | |
|
529 | 531 | def getOperationObjList(self): |
|
530 | 532 | |
|
531 | 533 | return self.opConfObjList |
|
532 | 534 | |
|
533 | 535 | def getOperationObj(self, name=None): |
|
534 | 536 | |
|
535 | 537 | for opConfObj in self.opConfObjList: |
|
536 | 538 | |
|
537 | 539 | if opConfObj.name != name: |
|
538 | 540 | continue |
|
539 | 541 | |
|
540 | 542 | return opConfObj |
|
541 | 543 | |
|
542 | 544 | return None |
|
543 | 545 | |
|
544 | 546 | def getOpObjfromParamValue(self, value=None): |
|
545 | 547 | |
|
546 | 548 | for opConfObj in self.opConfObjList: |
|
547 | 549 | if opConfObj.getParameterObjfromValue(parameterValue=value) != value: |
|
548 | 550 | continue |
|
549 | 551 | return opConfObj |
|
550 | 552 | return None |
|
551 | 553 | |
|
552 | 554 | def getProcUnitObj(self): |
|
553 | 555 | |
|
554 | 556 | return self.procUnitObj |
|
555 | 557 | |
|
556 | 558 | def setup(self, id, name, datatype, inputId, parentId=None): |
|
557 | 559 | |
|
558 | 560 | #Compatible with old signal chain version |
|
559 | 561 | if datatype==None and name==None: |
|
560 | 562 | raise ValueError, "datatype or name should be defined" |
|
561 | 563 | |
|
562 | 564 | if name==None: |
|
563 | 565 | if 'Proc' in datatype: |
|
564 | 566 | name = datatype |
|
565 | 567 | else: |
|
566 | 568 | name = '%sProc' %(datatype) |
|
567 | 569 | |
|
568 | 570 | if datatype==None: |
|
569 | 571 | datatype = name.replace('Proc','') |
|
570 | 572 | |
|
571 | 573 | self.id = str(id) |
|
572 | 574 | self.name = name |
|
573 | 575 | self.datatype = datatype |
|
574 | 576 | self.inputId = inputId |
|
575 | 577 | self.parentId = parentId |
|
576 | 578 | |
|
577 | 579 | self.opConfObjList = [] |
|
578 | 580 | |
|
579 | 581 | self.addOperation(name='run', optype='self') |
|
580 | 582 | |
|
581 | 583 | def removeOperations(self): |
|
582 | 584 | |
|
583 | 585 | for obj in self.opConfObjList: |
|
584 | 586 | del obj |
|
585 | 587 | |
|
586 | 588 | self.opConfObjList = [] |
|
587 | 589 | self.addOperation(name='run') |
|
588 | 590 | |
|
589 | 591 | def addParameter(self, **kwargs): |
|
590 | 592 | ''' |
|
591 | 593 | Add parameters to "run" operation |
|
592 | 594 | ''' |
|
593 | 595 | opObj = self.opConfObjList[0] |
|
594 | 596 | |
|
595 | 597 | opObj.addParameter(**kwargs) |
|
596 | 598 | |
|
597 | 599 | return opObj |
|
598 | 600 | |
|
599 | 601 | def addOperation(self, name, optype='self'): |
|
600 | 602 | |
|
601 | 603 | id = self.__getNewId() |
|
602 | 604 | priority = self.__getPriority() |
|
603 | 605 | |
|
604 | 606 | opConfObj = OperationConf() |
|
605 | 607 | opConfObj.setup(id, name=name, priority=priority, type=optype) |
|
606 | 608 | |
|
607 | 609 | self.opConfObjList.append(opConfObj) |
|
608 | 610 | |
|
609 | 611 | return opConfObj |
|
610 | 612 | |
|
611 | 613 | def makeXml(self, projectElement): |
|
612 | 614 | |
|
613 | 615 | procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
614 | 616 | procUnitElement.set('id', str(self.id)) |
|
615 | 617 | procUnitElement.set('name', self.name) |
|
616 | 618 | procUnitElement.set('datatype', self.datatype) |
|
617 | 619 | procUnitElement.set('inputId', str(self.inputId)) |
|
618 | 620 | |
|
619 | 621 | for opConfObj in self.opConfObjList: |
|
620 | 622 | opConfObj.makeXml(procUnitElement) |
|
621 | 623 | |
|
622 | 624 | def readXml(self, upElement): |
|
623 | 625 | |
|
624 | 626 | self.id = upElement.get('id') |
|
625 | 627 | self.name = upElement.get('name') |
|
626 | 628 | self.datatype = upElement.get('datatype') |
|
627 | 629 | self.inputId = upElement.get('inputId') |
|
628 | 630 | |
|
629 | 631 | if self.ELEMENTNAME == "ReadUnit": |
|
630 | 632 | self.datatype = self.datatype.replace("Reader", "") |
|
631 | 633 | |
|
632 | 634 | if self.ELEMENTNAME == "ProcUnit": |
|
633 | 635 | self.datatype = self.datatype.replace("Proc", "") |
|
634 | 636 | |
|
635 | 637 | if self.inputId == 'None': |
|
636 | 638 | self.inputId = '0' |
|
637 | 639 | |
|
638 | 640 | self.opConfObjList = [] |
|
639 | 641 | |
|
640 | 642 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
641 | 643 | |
|
642 | 644 | for opElement in opElementList: |
|
643 | 645 | opConfObj = OperationConf() |
|
644 | 646 | opConfObj.readXml(opElement) |
|
645 | 647 | self.opConfObjList.append(opConfObj) |
|
646 | 648 | |
|
647 | 649 | def printattr(self): |
|
648 | 650 | |
|
649 | 651 | print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME, |
|
650 | 652 | self.id, |
|
651 | 653 | self.name, |
|
652 | 654 | self.datatype, |
|
653 | 655 | self.inputId) |
|
654 | 656 | |
|
655 | 657 | for opConfObj in self.opConfObjList: |
|
656 | 658 | opConfObj.printattr() |
|
657 | 659 | |
|
658 | 660 | |
|
659 | 661 | def getKwargs(self): |
|
660 | 662 | |
|
661 | 663 | opObj = self.opConfObjList[0] |
|
662 | 664 | kwargs = opObj.getKwargs() |
|
663 | 665 | |
|
664 | 666 | return kwargs |
|
665 | 667 | |
|
666 | 668 | def createObjects(self, plotter_queue=None): |
|
667 | 669 | |
|
668 | 670 | className = eval(self.name) |
|
669 | 671 | kwargs = self.getKwargs() |
|
670 | 672 | procUnitObj = className(**kwargs) |
|
671 | 673 | |
|
672 | 674 | for opConfObj in self.opConfObjList: |
|
673 | 675 | |
|
674 | 676 | if opConfObj.type=='self' and self.name=='run': |
|
675 | 677 | continue |
|
676 | 678 | elif opConfObj.type=='self': |
|
677 | 679 | procUnitObj.addOperationKwargs(opConfObj.id, **opConfObj.getKwargs()) |
|
678 | 680 | continue |
|
679 | 681 | |
|
680 | 682 | opObj = opConfObj.createObject(plotter_queue) |
|
681 | 683 | |
|
682 | 684 | self.opObjDict[opConfObj.id] = opObj |
|
683 | 685 | |
|
684 | 686 | procUnitObj.addOperation(opObj, opConfObj.id) |
|
685 | 687 | |
|
686 | 688 | self.procUnitObj = procUnitObj |
|
687 | 689 | |
|
688 | 690 | return procUnitObj |
|
689 | 691 | |
|
690 | 692 | def run(self): |
|
691 | 693 | |
|
692 | 694 | is_ok = False |
|
693 | 695 | |
|
694 | 696 | for opConfObj in self.opConfObjList: |
|
695 | 697 | |
|
696 | 698 | kwargs = {} |
|
697 | 699 | for parmConfObj in opConfObj.getParameterObjList(): |
|
698 | 700 | if opConfObj.name == 'run' and parmConfObj.name == 'datatype': |
|
699 | 701 | continue |
|
700 | 702 | |
|
701 | 703 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
702 | 704 | |
|
703 | 705 | #ini = time.time() |
|
704 | 706 | |
|
705 | 707 | #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id) |
|
706 | 708 | sts = self.procUnitObj.call(opType = opConfObj.type, |
|
707 | 709 | opName = opConfObj.name, |
|
708 | 710 | opId = opConfObj.id, |
|
709 | 711 | ) |
|
710 | 712 | |
|
711 | 713 | # total_time = time.time() - ini |
|
712 | 714 | # |
|
713 | 715 | # if total_time > 0.002: |
|
714 | 716 | # print "%s::%s took %f seconds" %(self.name, opConfObj.name, total_time) |
|
715 | 717 | |
|
716 | 718 | is_ok = is_ok or sts |
|
717 | 719 | |
|
718 | 720 | return is_ok |
|
719 | 721 | |
|
720 | 722 | def close(self): |
|
721 | 723 | |
|
722 | 724 | for opConfObj in self.opConfObjList: |
|
723 | 725 | if opConfObj.type == 'self': |
|
724 | 726 | continue |
|
725 | 727 | |
|
726 | 728 | opObj = self.procUnitObj.getOperationObj(opConfObj.id) |
|
727 | 729 | opObj.close() |
|
728 | 730 | |
|
729 | 731 | self.procUnitObj.close() |
|
730 | 732 | |
|
731 | 733 | return |
|
732 | 734 | |
|
733 | 735 | class ReadUnitConf(ProcUnitConf): |
|
734 | 736 | |
|
735 | 737 | path = None |
|
736 | 738 | startDate = None |
|
737 | 739 | endDate = None |
|
738 | 740 | startTime = None |
|
739 | 741 | endTime = None |
|
740 | 742 | |
|
741 | 743 | ELEMENTNAME = 'ReadUnit' |
|
742 | 744 | |
|
743 | 745 | def __init__(self): |
|
744 | 746 | |
|
745 | 747 | self.id = None |
|
746 | 748 | self.datatype = None |
|
747 | 749 | self.name = None |
|
748 | 750 | self.inputId = None |
|
749 | 751 | |
|
750 | 752 | self.parentId = None |
|
751 | 753 | |
|
752 | 754 | self.opConfObjList = [] |
|
753 | 755 | self.opObjList = [] |
|
754 | 756 | |
|
755 | 757 | def getElementName(self): |
|
756 | 758 | |
|
757 | 759 | return self.ELEMENTNAME |
|
758 | 760 | |
|
759 | 761 | def setup(self, id, name, datatype, path='', startDate="", endDate="", startTime="", |
|
760 | 762 | endTime="", parentId=None, queue=None, server=None, **kwargs): |
|
761 | 763 | |
|
762 | 764 | #Compatible with old signal chain version |
|
763 | 765 | if datatype==None and name==None: |
|
764 | 766 | raise ValueError, "datatype or name should be defined" |
|
765 | 767 | |
|
766 | 768 | if name==None: |
|
767 | 769 | if 'Reader' in datatype: |
|
768 | 770 | name = datatype |
|
769 | 771 | else: |
|
770 | 772 | name = '%sReader' %(datatype) |
|
771 | 773 | if datatype==None: |
|
772 | 774 | datatype = name.replace('Reader','') |
|
773 | 775 | |
|
774 | 776 | self.id = id |
|
775 | 777 | self.name = name |
|
776 | 778 | self.datatype = datatype |
|
777 | 779 | if path != '': |
|
778 | 780 | self.path = os.path.abspath(path) |
|
779 | 781 | self.startDate = startDate |
|
780 | 782 | self.endDate = endDate |
|
781 | 783 | self.startTime = startTime |
|
782 | 784 | self.endTime = endTime |
|
783 | 785 | |
|
784 | 786 | self.inputId = '0' |
|
785 | 787 | self.parentId = parentId |
|
786 | 788 | self.queue = queue |
|
787 | 789 | self.server = server |
|
788 | 790 | self.addRunOperation(**kwargs) |
|
789 | 791 | |
|
790 | 792 | def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs): |
|
791 | 793 | |
|
792 | 794 | #Compatible with old signal chain version |
|
793 | 795 | if datatype==None and name==None: |
|
794 | 796 | raise ValueError, "datatype or name should be defined" |
|
795 | 797 | |
|
796 | 798 | if name==None: |
|
797 | 799 | if 'Reader' in datatype: |
|
798 | 800 | name = datatype |
|
799 | 801 | else: |
|
800 | 802 | name = '%sReader' %(datatype) |
|
801 | 803 | |
|
802 | 804 | if datatype==None: |
|
803 | 805 | datatype = name.replace('Reader','') |
|
804 | 806 | |
|
805 | 807 | self.datatype = datatype |
|
806 | 808 | self.name = name |
|
807 | 809 | self.path = path |
|
808 | 810 | self.startDate = startDate |
|
809 | 811 | self.endDate = endDate |
|
810 | 812 | self.startTime = startTime |
|
811 | 813 | self.endTime = endTime |
|
812 | 814 | |
|
813 | 815 | self.inputId = '0' |
|
814 | 816 | self.parentId = parentId |
|
815 | 817 | |
|
816 | 818 | self.updateRunOperation(**kwargs) |
|
817 | 819 | |
|
818 | 820 | def removeOperations(self): |
|
819 | 821 | |
|
820 | 822 | for obj in self.opConfObjList: |
|
821 | 823 | del obj |
|
822 | 824 | |
|
823 | 825 | self.opConfObjList = [] |
|
824 | 826 | |
|
825 | 827 | def addRunOperation(self, **kwargs): |
|
826 | 828 | |
|
827 | 829 | opObj = self.addOperation(name = 'run', optype = 'self') |
|
828 | 830 | |
|
829 | 831 | if self.server is None: |
|
830 | 832 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
831 | 833 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
832 | 834 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
833 | 835 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
834 | 836 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
835 | 837 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
836 | 838 | opObj.addParameter(name='queue' , value=self.queue, format='obj') |
|
837 | 839 | for key, value in kwargs.items(): |
|
838 | 840 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
839 | 841 | else: |
|
840 | 842 | opObj.addParameter(name='server' , value=self.server, format='str') |
|
841 | 843 | |
|
842 | 844 | |
|
843 | 845 | return opObj |
|
844 | 846 | |
|
845 | 847 | def updateRunOperation(self, **kwargs): |
|
846 | 848 | |
|
847 | 849 | opObj = self.getOperationObj(name = 'run') |
|
848 | 850 | opObj.removeParameters() |
|
849 | 851 | |
|
850 | 852 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
851 | 853 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
852 | 854 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
853 | 855 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
854 | 856 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
855 | 857 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
856 | 858 | |
|
857 | 859 | for key, value in kwargs.items(): |
|
858 | 860 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
859 | 861 | |
|
860 | 862 | return opObj |
|
861 | 863 | |
|
862 | 864 | # def makeXml(self, projectElement): |
|
863 | 865 | # |
|
864 | 866 | # procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
865 | 867 | # procUnitElement.set('id', str(self.id)) |
|
866 | 868 | # procUnitElement.set('name', self.name) |
|
867 | 869 | # procUnitElement.set('datatype', self.datatype) |
|
868 | 870 | # procUnitElement.set('inputId', str(self.inputId)) |
|
869 | 871 | # |
|
870 | 872 | # for opConfObj in self.opConfObjList: |
|
871 | 873 | # opConfObj.makeXml(procUnitElement) |
|
872 | 874 | |
|
873 | 875 | def readXml(self, upElement): |
|
874 | 876 | |
|
875 | 877 | self.id = upElement.get('id') |
|
876 | 878 | self.name = upElement.get('name') |
|
877 | 879 | self.datatype = upElement.get('datatype') |
|
878 | 880 | self.inputId = upElement.get('inputId') |
|
879 | 881 | |
|
880 | 882 | if self.ELEMENTNAME == "ReadUnit": |
|
881 | 883 | self.datatype = self.datatype.replace("Reader", "") |
|
882 | 884 | |
|
883 | 885 | if self.inputId == 'None': |
|
884 | 886 | self.inputId = '0' |
|
885 | 887 | |
|
886 | 888 | self.opConfObjList = [] |
|
887 | 889 | |
|
888 | 890 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
889 | 891 | |
|
890 | 892 | for opElement in opElementList: |
|
891 | 893 | opConfObj = OperationConf() |
|
892 | 894 | opConfObj.readXml(opElement) |
|
893 | 895 | self.opConfObjList.append(opConfObj) |
|
894 | 896 | |
|
895 | 897 | if opConfObj.name == 'run': |
|
896 | 898 | self.path = opConfObj.getParameterValue('path') |
|
897 | 899 | self.startDate = opConfObj.getParameterValue('startDate') |
|
898 | 900 | self.endDate = opConfObj.getParameterValue('endDate') |
|
899 | 901 | self.startTime = opConfObj.getParameterValue('startTime') |
|
900 | 902 | self.endTime = opConfObj.getParameterValue('endTime') |
|
901 | 903 | |
|
902 | 904 | class Project(): |
|
903 | 905 | |
|
904 | 906 | id = None |
|
905 | 907 | name = None |
|
906 | 908 | description = None |
|
907 | 909 | filename = None |
|
908 | 910 | |
|
909 | 911 | procUnitConfObjDict = None |
|
910 | 912 | |
|
911 | 913 | ELEMENTNAME = 'Project' |
|
912 | 914 | |
|
913 | 915 | plotterQueue = None |
|
914 | 916 | |
|
915 | 917 | def __init__(self, plotter_queue=None): |
|
916 | 918 | |
|
917 | 919 | self.id = None |
|
918 | 920 | self.name = None |
|
919 | 921 | self.description = None |
|
920 | 922 | |
|
921 | 923 | self.plotterQueue = plotter_queue |
|
922 | 924 | |
|
923 | 925 | self.procUnitConfObjDict = {} |
|
924 | 926 | |
|
925 | 927 | def __getNewId(self): |
|
926 | 928 | |
|
927 | 929 | idList = self.procUnitConfObjDict.keys() |
|
928 | 930 | |
|
929 | 931 | id = int(self.id)*10 |
|
930 | 932 | |
|
931 | 933 | while True: |
|
932 | 934 | id += 1 |
|
933 | 935 | |
|
934 | 936 | if str(id) in idList: |
|
935 | 937 | continue |
|
936 | 938 | |
|
937 | 939 | break |
|
938 | 940 | |
|
939 | 941 | return str(id) |
|
940 | 942 | |
|
941 | 943 | def getElementName(self): |
|
942 | 944 | |
|
943 | 945 | return self.ELEMENTNAME |
|
944 | 946 | |
|
945 | 947 | def getId(self): |
|
946 | 948 | |
|
947 | 949 | return self.id |
|
948 | 950 | |
|
949 | 951 | def updateId(self, new_id): |
|
950 | 952 | |
|
951 | 953 | self.id = str(new_id) |
|
952 | 954 | |
|
953 | 955 | keyList = self.procUnitConfObjDict.keys() |
|
954 | 956 | keyList.sort() |
|
955 | 957 | |
|
956 | 958 | n = 1 |
|
957 | 959 | newProcUnitConfObjDict = {} |
|
958 | 960 | |
|
959 | 961 | for procKey in keyList: |
|
960 | 962 | |
|
961 | 963 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
962 | 964 | idProcUnit = str(int(self.id)*10 + n) |
|
963 | 965 | procUnitConfObj.updateId(idProcUnit, parentId = self.id) |
|
964 | 966 | |
|
965 | 967 | newProcUnitConfObjDict[idProcUnit] = procUnitConfObj |
|
966 | 968 | n += 1 |
|
967 | 969 | |
|
968 | 970 | self.procUnitConfObjDict = newProcUnitConfObjDict |
|
969 | 971 | |
|
970 | 972 | def setup(self, id, name, description): |
|
971 | 973 | |
|
972 | 974 | self.id = str(id) |
|
973 | 975 | self.name = name |
|
974 | 976 | self.description = description |
|
975 | 977 | |
|
976 | 978 | def update(self, name, description): |
|
977 | 979 | |
|
978 | 980 | self.name = name |
|
979 | 981 | self.description = description |
|
980 | 982 | |
|
981 | 983 | def addReadUnit(self, id=None, datatype=None, name=None, **kwargs): |
|
982 | 984 | |
|
983 | 985 | if id is None: |
|
984 | 986 | idReadUnit = self.__getNewId() |
|
985 | 987 | else: |
|
986 | 988 | idReadUnit = str(id) |
|
987 | 989 | |
|
988 | 990 | readUnitConfObj = ReadUnitConf() |
|
989 | 991 | readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs) |
|
990 | 992 | |
|
991 | 993 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
992 | 994 | |
|
993 | 995 | return readUnitConfObj |
|
994 | 996 | |
|
995 | 997 | def addProcUnit(self, inputId='0', datatype=None, name=None): |
|
996 | 998 | |
|
997 | 999 | idProcUnit = self.__getNewId() |
|
998 | 1000 | |
|
999 | 1001 | procUnitConfObj = ProcUnitConf() |
|
1000 | 1002 | procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id) |
|
1001 | 1003 | |
|
1002 | 1004 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1003 | 1005 | |
|
1004 | 1006 | return procUnitConfObj |
|
1005 | 1007 | |
|
1006 | 1008 | def removeProcUnit(self, id): |
|
1007 | 1009 | |
|
1008 | 1010 | if id in self.procUnitConfObjDict.keys(): |
|
1009 | 1011 | self.procUnitConfObjDict.pop(id) |
|
1010 | 1012 | |
|
1011 | 1013 | def getReadUnitId(self): |
|
1012 | 1014 | |
|
1013 | 1015 | readUnitConfObj = self.getReadUnitObj() |
|
1014 | 1016 | |
|
1015 | 1017 | return readUnitConfObj.id |
|
1016 | 1018 | |
|
1017 | 1019 | def getReadUnitObj(self): |
|
1018 | 1020 | |
|
1019 | 1021 | for obj in self.procUnitConfObjDict.values(): |
|
1020 | 1022 | if obj.getElementName() == "ReadUnit": |
|
1021 | 1023 | return obj |
|
1022 | 1024 | |
|
1023 | 1025 | return None |
|
1024 | 1026 | |
|
1025 | 1027 | def getProcUnitObj(self, id=None, name=None): |
|
1026 | 1028 | |
|
1027 | 1029 | if id != None: |
|
1028 | 1030 | return self.procUnitConfObjDict[id] |
|
1029 | 1031 | |
|
1030 | 1032 | if name != None: |
|
1031 | 1033 | return self.getProcUnitObjByName(name) |
|
1032 | 1034 | |
|
1033 | 1035 | return None |
|
1034 | 1036 | |
|
1035 | 1037 | def getProcUnitObjByName(self, name): |
|
1036 | 1038 | |
|
1037 | 1039 | for obj in self.procUnitConfObjDict.values(): |
|
1038 | 1040 | if obj.name == name: |
|
1039 | 1041 | return obj |
|
1040 | 1042 | |
|
1041 | 1043 | return None |
|
1042 | 1044 | |
|
1043 | 1045 | def procUnitItems(self): |
|
1044 | 1046 | |
|
1045 | 1047 | return self.procUnitConfObjDict.items() |
|
1046 | 1048 | |
|
1047 | 1049 | def makeXml(self): |
|
1048 | 1050 | |
|
1049 | 1051 | projectElement = Element('Project') |
|
1050 | 1052 | projectElement.set('id', str(self.id)) |
|
1051 | 1053 | projectElement.set('name', self.name) |
|
1052 | 1054 | projectElement.set('description', self.description) |
|
1053 | 1055 | |
|
1054 | 1056 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1055 | 1057 | procUnitConfObj.makeXml(projectElement) |
|
1056 | 1058 | |
|
1057 | 1059 | self.projectElement = projectElement |
|
1058 | 1060 | |
|
1059 | 1061 | def writeXml(self, filename=None): |
|
1060 | 1062 | |
|
1061 | 1063 | if filename == None: |
|
1062 | 1064 | if self.filename: |
|
1063 | 1065 | filename = self.filename |
|
1064 | 1066 | else: |
|
1065 | 1067 | filename = "schain.xml" |
|
1066 | 1068 | |
|
1067 | 1069 | if not filename: |
|
1068 | 1070 | print "filename has not been defined. Use setFilename(filename) for do it." |
|
1069 | 1071 | return 0 |
|
1070 | 1072 | |
|
1071 | 1073 | abs_file = os.path.abspath(filename) |
|
1072 | 1074 | |
|
1073 | 1075 | if not os.access(os.path.dirname(abs_file), os.W_OK): |
|
1074 | 1076 | print "No write permission on %s" %os.path.dirname(abs_file) |
|
1075 | 1077 | return 0 |
|
1076 | 1078 | |
|
1077 | 1079 | if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)): |
|
1078 | 1080 | print "File %s already exists and it could not be overwriten" %abs_file |
|
1079 | 1081 | return 0 |
|
1080 | 1082 | |
|
1081 | 1083 | self.makeXml() |
|
1082 | 1084 | |
|
1083 | 1085 | ElementTree(self.projectElement).write(abs_file, method='xml') |
|
1084 | 1086 | |
|
1085 | 1087 | self.filename = abs_file |
|
1086 | 1088 | |
|
1087 | 1089 | return 1 |
|
1088 | 1090 | |
|
1089 | 1091 | def readXml(self, filename = None): |
|
1090 | 1092 | |
|
1091 | 1093 | if not filename: |
|
1092 | 1094 | print "filename is not defined" |
|
1093 | 1095 | return 0 |
|
1094 | 1096 | |
|
1095 | 1097 | abs_file = os.path.abspath(filename) |
|
1096 | 1098 | |
|
1097 | 1099 | if not os.path.isfile(abs_file): |
|
1098 | 1100 | print "%s file does not exist" %abs_file |
|
1099 | 1101 | return 0 |
|
1100 | 1102 | |
|
1101 | 1103 | self.projectElement = None |
|
1102 | 1104 | self.procUnitConfObjDict = {} |
|
1103 | 1105 | |
|
1104 | 1106 | try: |
|
1105 | 1107 | self.projectElement = ElementTree().parse(abs_file) |
|
1106 | 1108 | except: |
|
1107 | 1109 | print "Error reading %s, verify file format" %filename |
|
1108 | 1110 | return 0 |
|
1109 | 1111 | |
|
1110 | 1112 | self.project = self.projectElement.tag |
|
1111 | 1113 | |
|
1112 | 1114 | self.id = self.projectElement.get('id') |
|
1113 | 1115 | self.name = self.projectElement.get('name') |
|
1114 | 1116 | self.description = self.projectElement.get('description') |
|
1115 | 1117 | |
|
1116 | 1118 | readUnitElementList = self.projectElement.iter(ReadUnitConf().getElementName()) |
|
1117 | 1119 | |
|
1118 | 1120 | for readUnitElement in readUnitElementList: |
|
1119 | 1121 | readUnitConfObj = ReadUnitConf() |
|
1120 | 1122 | readUnitConfObj.readXml(readUnitElement) |
|
1121 | 1123 | |
|
1122 | 1124 | if readUnitConfObj.parentId == None: |
|
1123 | 1125 | readUnitConfObj.parentId = self.id |
|
1124 | 1126 | |
|
1125 | 1127 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
1126 | 1128 | |
|
1127 | 1129 | procUnitElementList = self.projectElement.iter(ProcUnitConf().getElementName()) |
|
1128 | 1130 | |
|
1129 | 1131 | for procUnitElement in procUnitElementList: |
|
1130 | 1132 | procUnitConfObj = ProcUnitConf() |
|
1131 | 1133 | procUnitConfObj.readXml(procUnitElement) |
|
1132 | 1134 | |
|
1133 | 1135 | if procUnitConfObj.parentId == None: |
|
1134 | 1136 | procUnitConfObj.parentId = self.id |
|
1135 | 1137 | |
|
1136 | 1138 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1137 | 1139 | |
|
1138 | 1140 | self.filename = abs_file |
|
1139 | 1141 | |
|
1140 | 1142 | return 1 |
|
1141 | 1143 | |
|
1142 | 1144 | def printattr(self): |
|
1143 | 1145 | |
|
1144 | 1146 | print "Project[%s]: name = %s, description = %s" %(self.id, |
|
1145 | 1147 | self.name, |
|
1146 | 1148 | self.description) |
|
1147 | 1149 | |
|
1148 | 1150 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1149 | 1151 | procUnitConfObj.printattr() |
|
1150 | 1152 | |
|
1151 | 1153 | def createObjects(self): |
|
1152 | 1154 | |
|
1153 | 1155 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1154 | 1156 | procUnitConfObj.createObjects(self.plotterQueue) |
|
1155 | 1157 | |
|
1156 | 1158 | def __connect(self, objIN, thisObj): |
|
1157 | 1159 | |
|
1158 | 1160 | thisObj.setInput(objIN.getOutputObj()) |
|
1159 | 1161 | |
|
1160 | 1162 | def connectObjects(self): |
|
1161 | 1163 | |
|
1162 | 1164 | for thisPUConfObj in self.procUnitConfObjDict.values(): |
|
1163 | 1165 | |
|
1164 | 1166 | inputId = thisPUConfObj.getInputId() |
|
1165 | 1167 | |
|
1166 | 1168 | if int(inputId) == 0: |
|
1167 | 1169 | continue |
|
1168 | 1170 | |
|
1169 | 1171 | #Get input object |
|
1170 | 1172 | puConfINObj = self.procUnitConfObjDict[inputId] |
|
1171 | 1173 | puObjIN = puConfINObj.getProcUnitObj() |
|
1172 | 1174 | |
|
1173 | 1175 | #Get current object |
|
1174 | 1176 | thisPUObj = thisPUConfObj.getProcUnitObj() |
|
1175 | 1177 | |
|
1176 | 1178 | self.__connect(puObjIN, thisPUObj) |
|
1177 | 1179 | |
|
1178 | 1180 | def __handleError(self, procUnitConfObj, send_email=True): |
|
1179 | 1181 | |
|
1180 | 1182 | import socket |
|
1181 | 1183 | |
|
1182 | 1184 | err = traceback.format_exception(sys.exc_info()[0], |
|
1183 | 1185 | sys.exc_info()[1], |
|
1184 | 1186 | sys.exc_info()[2]) |
|
1185 | 1187 | |
|
1186 | 1188 | print "***** Error occurred in %s *****" %(procUnitConfObj.name) |
|
1187 | 1189 | print "***** %s" %err[-1] |
|
1188 | 1190 | |
|
1189 | 1191 | message = "".join(err) |
|
1190 | 1192 | |
|
1191 | 1193 | sys.stderr.write(message) |
|
1192 | 1194 | |
|
1193 | 1195 | if not send_email: |
|
1194 | 1196 | return |
|
1195 | 1197 | |
|
1196 | 1198 | subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, procUnitConfObj.name) |
|
1197 | 1199 | |
|
1198 | 1200 | subtitle = "%s: %s\n" %(procUnitConfObj.getElementName() ,procUnitConfObj.name) |
|
1199 | 1201 | subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname()) |
|
1200 | 1202 | subtitle += "Working directory: %s\n" %os.path.abspath("./") |
|
1201 | 1203 | subtitle += "Configuration file: %s\n" %self.filename |
|
1202 | 1204 | subtitle += "Time: %s\n" %str(datetime.datetime.now()) |
|
1203 | 1205 | |
|
1204 | 1206 | readUnitConfObj = self.getReadUnitObj() |
|
1205 | 1207 | if readUnitConfObj: |
|
1206 | 1208 | subtitle += "\nInput parameters:\n" |
|
1207 | 1209 | subtitle += "[Data path = %s]\n" %readUnitConfObj.path |
|
1208 | 1210 | subtitle += "[Data type = %s]\n" %readUnitConfObj.datatype |
|
1209 | 1211 | subtitle += "[Start date = %s]\n" %readUnitConfObj.startDate |
|
1210 | 1212 | subtitle += "[End date = %s]\n" %readUnitConfObj.endDate |
|
1211 | 1213 | subtitle += "[Start time = %s]\n" %readUnitConfObj.startTime |
|
1212 | 1214 | subtitle += "[End time = %s]\n" %readUnitConfObj.endTime |
|
1213 | 1215 | |
|
1214 | 1216 | adminObj = schainpy.admin.SchainNotify() |
|
1215 | 1217 | adminObj.sendAlert(message=message, |
|
1216 | 1218 | subject=subject, |
|
1217 | 1219 | subtitle=subtitle, |
|
1218 | 1220 | filename=self.filename) |
|
1219 | 1221 | |
|
1220 | 1222 | def isPaused(self): |
|
1221 | 1223 | return 0 |
|
1222 | 1224 | |
|
1223 | 1225 | def isStopped(self): |
|
1224 | 1226 | return 0 |
|
1225 | 1227 | |
|
1226 | 1228 | def runController(self): |
|
1227 | 1229 | """ |
|
1228 | 1230 | returns 0 when this process has been stopped, 1 otherwise |
|
1229 | 1231 | """ |
|
1230 | 1232 | |
|
1231 | 1233 | if self.isPaused(): |
|
1232 | 1234 | print "Process suspended" |
|
1233 | 1235 | |
|
1234 | 1236 | while True: |
|
1235 | 1237 | sleep(0.1) |
|
1236 | 1238 | |
|
1237 | 1239 | if not self.isPaused(): |
|
1238 | 1240 | break |
|
1239 | 1241 | |
|
1240 | 1242 | if self.isStopped(): |
|
1241 | 1243 | break |
|
1242 | 1244 | |
|
1243 | 1245 | print "Process reinitialized" |
|
1244 | 1246 | |
|
1245 | 1247 | if self.isStopped(): |
|
1246 | 1248 | print "Process stopped" |
|
1247 | 1249 | return 0 |
|
1248 | 1250 | |
|
1249 | 1251 | return 1 |
|
1250 | 1252 | |
|
1251 | 1253 | def setFilename(self, filename): |
|
1252 | 1254 | |
|
1253 | 1255 | self.filename = filename |
|
1254 | 1256 | |
|
1255 | 1257 | def setPlotterQueue(self, plotter_queue): |
|
1256 | 1258 | |
|
1257 | 1259 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1258 | 1260 | |
|
1259 | 1261 | def getPlotterQueue(self): |
|
1260 | 1262 | |
|
1261 | 1263 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1262 | 1264 | |
|
1263 | 1265 | def useExternalPlotter(self): |
|
1264 | 1266 | |
|
1265 | 1267 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1266 | 1268 | |
|
1267 | 1269 | def run(self): |
|
1268 | 1270 | |
|
1269 | 1271 | |
|
1270 | 1272 | print "*"*60 |
|
1271 | 1273 | print " Starting SIGNAL CHAIN PROCESSING v%s " %schainpy.__version__ |
|
1272 | 1274 | print "*"*60 |
|
1273 | 1275 | |
|
1274 | 1276 | |
|
1275 | 1277 | keyList = self.procUnitConfObjDict.keys() |
|
1276 | 1278 | keyList.sort() |
|
1277 | 1279 | |
|
1278 | 1280 | while(True): |
|
1279 | 1281 | |
|
1280 | 1282 | is_ok = False |
|
1281 | 1283 | |
|
1282 | 1284 | for procKey in keyList: |
|
1283 | 1285 | # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id) |
|
1284 | 1286 | |
|
1285 | 1287 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1286 | 1288 | |
|
1287 | 1289 | try: |
|
1288 | 1290 | sts = procUnitConfObj.run() |
|
1289 | 1291 | is_ok = is_ok or sts |
|
1290 | 1292 | except KeyboardInterrupt: |
|
1291 | 1293 | is_ok = False |
|
1292 | 1294 | break |
|
1293 | 1295 | except ValueError, e: |
|
1294 | 1296 | sleep(0.5) |
|
1295 | 1297 | self.__handleError(procUnitConfObj, send_email=True) |
|
1296 | 1298 | is_ok = False |
|
1297 | 1299 | break |
|
1298 | 1300 | except: |
|
1299 | 1301 | sleep(0.5) |
|
1300 | 1302 | self.__handleError(procUnitConfObj) |
|
1301 | 1303 | is_ok = False |
|
1302 | 1304 | break |
|
1303 | 1305 | |
|
1304 | 1306 | #If every process unit finished so end process |
|
1305 | 1307 | if not(is_ok): |
|
1306 | 1308 | # print "Every process unit have finished" |
|
1307 | 1309 | break |
|
1308 | 1310 | |
|
1309 | 1311 | if not self.runController(): |
|
1310 | 1312 | break |
|
1311 | 1313 | |
|
1312 | 1314 | #Closing every process |
|
1313 | 1315 | for procKey in keyList: |
|
1314 | 1316 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1315 | 1317 | procUnitConfObj.close() |
|
1316 | 1318 | |
|
1317 | 1319 | print "Process finished" |
|
1318 | 1320 | |
|
1319 | 1321 | def start(self, filename=None): |
|
1320 | 1322 | |
|
1321 | 1323 | self.writeXml(filename) |
|
1322 | 1324 | self.createObjects() |
|
1323 | 1325 | self.connectObjects() |
|
1324 | 1326 | self.run() |
@@ -1,58 +1,58 | |||
|
1 | 1 | """. |
|
2 | 2 | |
|
3 | 3 | Created on Jul 16, 2014 |
|
4 | 4 | |
|
5 | 5 | @author: Miguel Urco |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | from schainpy import __version__ | |
|
8 | import numpy | |
|
9 | 9 | from setuptools import setup, Extension |
|
10 | from schainpy import __version__ | |
|
10 | 11 | |
|
11 | 12 | setup(name="schainpy", |
|
12 | 13 | version=__version__, |
|
13 | 14 | description="Python tools to read, write and process Jicamarca data", |
|
14 | 15 | author="Miguel Urco", |
|
15 | 16 | author_email="miguel.urco@jro.igp.gob.pe", |
|
16 | 17 | url="http://jro.igp.gob.pe", |
|
17 | 18 | packages={'schainpy', |
|
18 | 19 | 'schainpy.model', |
|
19 | 20 | 'schainpy.model.data', |
|
20 | 21 | 'schainpy.model.graphics', |
|
21 | 22 | 'schainpy.model.io', |
|
22 | 23 | 'schainpy.model.proc', |
|
23 | 24 | 'schainpy.model.serializer', |
|
24 | 25 | 'schainpy.model.utils', |
|
25 | 26 | 'schainpy.gui', |
|
26 | 27 | 'schainpy.gui.figures', |
|
27 | 28 | 'schainpy.gui.viewcontroller', |
|
28 | 29 | 'schainpy.gui.viewer', |
|
29 | 30 | 'schainpy.gui.viewer.windows'}, |
|
30 | 31 | ext_package='schainpy', |
|
31 | 32 | py_modules=[''], |
|
32 | 33 | package_data={'': ['schain.conf.template'], |
|
33 | 34 | 'schainpy.gui.figures': ['*.png', '*.jpg'], |
|
34 | 35 | }, |
|
35 | 36 | include_package_data=False, |
|
36 | 37 | entry_points={ |
|
37 | 38 | 'console_scripts': [ |
|
38 | 39 | 'schain = schaincli.cli:main', |
|
39 | 40 | ], |
|
40 | 41 | }, |
|
41 | 42 | scripts=['schainpy/gui/schainGUI'], |
|
42 | 43 | ext_modules=[Extension("cSchain", ["schainpy/model/proc/extensions.c"], include_dirs=[numpy.get_include()])], |
|
43 | 44 | install_requires=[ |
|
44 | 45 | "scipy >= 0.14.0", |
|
45 | 46 | "h5py >= 2.2.1", |
|
46 | 47 | "matplotlib >= 1.4.2", |
|
47 | 48 | "pyfits >= 3.4", |
|
48 | "numpy >= 1.11.2", | |
|
49 | 49 | "paramiko >= 2.1.2", |
|
50 | 50 | "paho-mqtt >= 1.2", |
|
51 | 51 | "zmq", |
|
52 | 52 | "fuzzywuzzy", |
|
53 | 53 | "click", |
|
54 | 54 | "colorama", |
|
55 | 55 | "python-Levenshtein" |
|
56 | 56 | ], |
|
57 | 57 | ) |
|
58 | 58 |
General Comments 0
You need to be logged in to leave comments.
Login now