@@ -1,1321 +1,1321 | |||
|
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 | 50 | firstProcess.terminate() |
|
51 | 51 | skip = int(math.ceil(nFiles/nProcess)) |
|
52 | 52 | while True: |
|
53 | 53 | processes.append(Process(target=child, args=(cursor, skip, q, dt))) |
|
54 | 54 | processes[cursor].start() |
|
55 | 55 | if nFiles < cursor*skip: |
|
56 | 56 | break |
|
57 | 57 | cursor += 1 |
|
58 | 58 | |
|
59 | 59 | def beforeExit(exctype, value, trace): |
|
60 | 60 | for process in processes: |
|
61 | 61 | process.terminate() |
|
62 | 62 | process.join() |
|
63 | 63 | print traceback.print_tb(trace) |
|
64 | ||
|
64 | ||
|
65 | 65 | sys.excepthook = beforeExit |
|
66 | 66 | |
|
67 | 67 | for process in processes: |
|
68 | 68 | process.join() |
|
69 | 69 | process.terminate() |
|
70 | 70 | time.sleep(3) |
|
71 | 71 | |
|
72 | 72 | class ParameterConf(): |
|
73 | 73 | |
|
74 | 74 | id = None |
|
75 | 75 | name = None |
|
76 | 76 | value = None |
|
77 | 77 | format = None |
|
78 | 78 | |
|
79 | 79 | __formated_value = None |
|
80 | 80 | |
|
81 | 81 | ELEMENTNAME = 'Parameter' |
|
82 | 82 | |
|
83 | 83 | def __init__(self): |
|
84 | 84 | |
|
85 | 85 | self.format = 'str' |
|
86 | 86 | |
|
87 | 87 | def getElementName(self): |
|
88 | 88 | |
|
89 | 89 | return self.ELEMENTNAME |
|
90 | 90 | |
|
91 | 91 | def getValue(self): |
|
92 | 92 | |
|
93 | 93 | value = self.value |
|
94 | 94 | format = self.format |
|
95 | 95 | |
|
96 | 96 | if self.__formated_value != None: |
|
97 | 97 | |
|
98 | 98 | return self.__formated_value |
|
99 | 99 | |
|
100 | 100 | if format == 'obj': |
|
101 | 101 | return value |
|
102 | 102 | |
|
103 | 103 | if format == 'str': |
|
104 | 104 | self.__formated_value = str(value) |
|
105 | 105 | return self.__formated_value |
|
106 | 106 | |
|
107 | 107 | if value == '': |
|
108 | 108 | raise ValueError, "%s: This parameter value is empty" %self.name |
|
109 | 109 | |
|
110 | 110 | if format == 'list': |
|
111 | 111 | strList = value.split(',') |
|
112 | 112 | |
|
113 | 113 | self.__formated_value = strList |
|
114 | 114 | |
|
115 | 115 | return self.__formated_value |
|
116 | 116 | |
|
117 | 117 | if format == 'intlist': |
|
118 | 118 | """ |
|
119 | 119 | Example: |
|
120 | 120 | value = (0,1,2) |
|
121 | 121 | """ |
|
122 | 122 | |
|
123 | 123 | new_value = ast.literal_eval(value) |
|
124 | 124 | |
|
125 | 125 | if type(new_value) not in (tuple, list): |
|
126 | 126 | new_value = [int(new_value)] |
|
127 | 127 | |
|
128 | 128 | self.__formated_value = new_value |
|
129 | 129 | |
|
130 | 130 | return self.__formated_value |
|
131 | 131 | |
|
132 | 132 | if format == 'floatlist': |
|
133 | 133 | """ |
|
134 | 134 | Example: |
|
135 | 135 | value = (0.5, 1.4, 2.7) |
|
136 | 136 | """ |
|
137 | 137 | |
|
138 | 138 | new_value = ast.literal_eval(value) |
|
139 | 139 | |
|
140 | 140 | if type(new_value) not in (tuple, list): |
|
141 | 141 | new_value = [float(new_value)] |
|
142 | 142 | |
|
143 | 143 | self.__formated_value = new_value |
|
144 | 144 | |
|
145 | 145 | return self.__formated_value |
|
146 | 146 | |
|
147 | 147 | if format == 'date': |
|
148 | 148 | strList = value.split('/') |
|
149 | 149 | intList = [int(x) for x in strList] |
|
150 | 150 | date = datetime.date(intList[0], intList[1], intList[2]) |
|
151 | 151 | |
|
152 | 152 | self.__formated_value = date |
|
153 | 153 | |
|
154 | 154 | return self.__formated_value |
|
155 | 155 | |
|
156 | 156 | if format == 'time': |
|
157 | 157 | strList = value.split(':') |
|
158 | 158 | intList = [int(x) for x in strList] |
|
159 | 159 | time = datetime.time(intList[0], intList[1], intList[2]) |
|
160 | 160 | |
|
161 | 161 | self.__formated_value = time |
|
162 | 162 | |
|
163 | 163 | return self.__formated_value |
|
164 | 164 | |
|
165 | 165 | if format == 'pairslist': |
|
166 | 166 | """ |
|
167 | 167 | Example: |
|
168 | 168 | value = (0,1),(1,2) |
|
169 | 169 | """ |
|
170 | 170 | |
|
171 | 171 | new_value = ast.literal_eval(value) |
|
172 | 172 | |
|
173 | 173 | if type(new_value) not in (tuple, list): |
|
174 | 174 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
175 | 175 | |
|
176 | 176 | if type(new_value[0]) not in (tuple, list): |
|
177 | 177 | if len(new_value) != 2: |
|
178 | 178 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
179 | 179 | new_value = [new_value] |
|
180 | 180 | |
|
181 | 181 | for thisPair in new_value: |
|
182 | 182 | if len(thisPair) != 2: |
|
183 | 183 | raise ValueError, "%s has to be a tuple or list of pairs" %value |
|
184 | 184 | |
|
185 | 185 | self.__formated_value = new_value |
|
186 | 186 | |
|
187 | 187 | return self.__formated_value |
|
188 | 188 | |
|
189 | 189 | if format == 'multilist': |
|
190 | 190 | """ |
|
191 | 191 | Example: |
|
192 | 192 | value = (0,1,2),(3,4,5) |
|
193 | 193 | """ |
|
194 | 194 | multiList = ast.literal_eval(value) |
|
195 | 195 | |
|
196 | 196 | if type(multiList[0]) == int: |
|
197 | 197 | multiList = ast.literal_eval("(" + value + ")") |
|
198 | 198 | |
|
199 | 199 | self.__formated_value = multiList |
|
200 | 200 | |
|
201 | 201 | return self.__formated_value |
|
202 | 202 | |
|
203 | 203 | if format == 'bool': |
|
204 | 204 | value = int(value) |
|
205 | 205 | |
|
206 | 206 | if format == 'int': |
|
207 | 207 | value = float(value) |
|
208 | 208 | |
|
209 | 209 | format_func = eval(format) |
|
210 | 210 | |
|
211 | 211 | self.__formated_value = format_func(value) |
|
212 | 212 | |
|
213 | 213 | return self.__formated_value |
|
214 | 214 | |
|
215 | 215 | def updateId(self, new_id): |
|
216 | 216 | |
|
217 | 217 | self.id = str(new_id) |
|
218 | 218 | |
|
219 | 219 | def setup(self, id, name, value, format='str'): |
|
220 | 220 | |
|
221 | 221 | self.id = str(id) |
|
222 | 222 | self.name = name |
|
223 | 223 | if format == 'obj': |
|
224 | 224 | self.value = value |
|
225 | 225 | else: |
|
226 | 226 | self.value = str(value) |
|
227 | 227 | self.format = str.lower(format) |
|
228 | 228 | |
|
229 | 229 | self.getValue() |
|
230 | 230 | |
|
231 | 231 | return 1 |
|
232 | 232 | |
|
233 | 233 | def update(self, name, value, format='str'): |
|
234 | 234 | |
|
235 | 235 | self.name = name |
|
236 | 236 | self.value = str(value) |
|
237 | 237 | self.format = format |
|
238 | 238 | |
|
239 | 239 | def makeXml(self, opElement): |
|
240 | 240 | if self.name not in ('queue',): |
|
241 | 241 | parmElement = SubElement(opElement, self.ELEMENTNAME) |
|
242 | 242 | parmElement.set('id', str(self.id)) |
|
243 | 243 | parmElement.set('name', self.name) |
|
244 | 244 | parmElement.set('value', self.value) |
|
245 | 245 | parmElement.set('format', self.format) |
|
246 | 246 | |
|
247 | 247 | def readXml(self, parmElement): |
|
248 | 248 | |
|
249 | 249 | self.id = parmElement.get('id') |
|
250 | 250 | self.name = parmElement.get('name') |
|
251 | 251 | self.value = parmElement.get('value') |
|
252 | 252 | self.format = str.lower(parmElement.get('format')) |
|
253 | 253 | |
|
254 | 254 | #Compatible with old signal chain version |
|
255 | 255 | if self.format == 'int' and self.name == 'idfigure': |
|
256 | 256 | self.name = 'id' |
|
257 | 257 | |
|
258 | 258 | def printattr(self): |
|
259 | 259 | |
|
260 | 260 | print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format) |
|
261 | 261 | |
|
262 | 262 | class OperationConf(): |
|
263 | 263 | |
|
264 | 264 | id = None |
|
265 | 265 | name = None |
|
266 | 266 | priority = None |
|
267 | 267 | type = None |
|
268 | 268 | |
|
269 | 269 | parmConfObjList = [] |
|
270 | 270 | |
|
271 | 271 | ELEMENTNAME = 'Operation' |
|
272 | 272 | |
|
273 | 273 | def __init__(self): |
|
274 | 274 | |
|
275 | 275 | self.id = '0' |
|
276 | 276 | self.name = None |
|
277 | 277 | self.priority = None |
|
278 | 278 | self.type = 'self' |
|
279 | 279 | |
|
280 | 280 | |
|
281 | 281 | def __getNewId(self): |
|
282 | 282 | |
|
283 | 283 | return int(self.id)*10 + len(self.parmConfObjList) + 1 |
|
284 | 284 | |
|
285 | 285 | def updateId(self, new_id): |
|
286 | 286 | |
|
287 | 287 | self.id = str(new_id) |
|
288 | 288 | |
|
289 | 289 | n = 1 |
|
290 | 290 | for parmObj in self.parmConfObjList: |
|
291 | 291 | |
|
292 | 292 | idParm = str(int(new_id)*10 + n) |
|
293 | 293 | parmObj.updateId(idParm) |
|
294 | 294 | |
|
295 | 295 | n += 1 |
|
296 | 296 | |
|
297 | 297 | def getElementName(self): |
|
298 | 298 | |
|
299 | 299 | return self.ELEMENTNAME |
|
300 | 300 | |
|
301 | 301 | def getParameterObjList(self): |
|
302 | 302 | |
|
303 | 303 | return self.parmConfObjList |
|
304 | 304 | |
|
305 | 305 | def getParameterObj(self, parameterName): |
|
306 | 306 | |
|
307 | 307 | for parmConfObj in self.parmConfObjList: |
|
308 | 308 | |
|
309 | 309 | if parmConfObj.name != parameterName: |
|
310 | 310 | continue |
|
311 | 311 | |
|
312 | 312 | return parmConfObj |
|
313 | 313 | |
|
314 | 314 | return None |
|
315 | 315 | |
|
316 | 316 | def getParameterObjfromValue(self, parameterValue): |
|
317 | 317 | |
|
318 | 318 | for parmConfObj in self.parmConfObjList: |
|
319 | 319 | |
|
320 | 320 | if parmConfObj.getValue() != parameterValue: |
|
321 | 321 | continue |
|
322 | 322 | |
|
323 | 323 | return parmConfObj.getValue() |
|
324 | 324 | |
|
325 | 325 | return None |
|
326 | 326 | |
|
327 | 327 | def getParameterValue(self, parameterName): |
|
328 | 328 | |
|
329 | 329 | parameterObj = self.getParameterObj(parameterName) |
|
330 | 330 | |
|
331 | 331 | # if not parameterObj: |
|
332 | 332 | # return None |
|
333 | 333 | |
|
334 | 334 | value = parameterObj.getValue() |
|
335 | 335 | |
|
336 | 336 | return value |
|
337 | 337 | |
|
338 | 338 | |
|
339 | 339 | def getKwargs(self): |
|
340 | 340 | |
|
341 | 341 | kwargs = {} |
|
342 | 342 | |
|
343 | 343 | for parmConfObj in self.parmConfObjList: |
|
344 | 344 | if self.name == 'run' and parmConfObj.name == 'datatype': |
|
345 | 345 | continue |
|
346 | 346 | |
|
347 | 347 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
348 | 348 | |
|
349 | 349 | return kwargs |
|
350 | 350 | |
|
351 | 351 | def setup(self, id, name, priority, type): |
|
352 | 352 | |
|
353 | 353 | self.id = str(id) |
|
354 | 354 | self.name = name |
|
355 | 355 | self.type = type |
|
356 | 356 | self.priority = priority |
|
357 | 357 | |
|
358 | 358 | self.parmConfObjList = [] |
|
359 | 359 | |
|
360 | 360 | def removeParameters(self): |
|
361 | 361 | |
|
362 | 362 | for obj in self.parmConfObjList: |
|
363 | 363 | del obj |
|
364 | 364 | |
|
365 | 365 | self.parmConfObjList = [] |
|
366 | 366 | |
|
367 | 367 | def addParameter(self, name, value, format='str'): |
|
368 | 368 | |
|
369 | 369 | id = self.__getNewId() |
|
370 | 370 | |
|
371 | 371 | parmConfObj = ParameterConf() |
|
372 | 372 | if not parmConfObj.setup(id, name, value, format): |
|
373 | 373 | return None |
|
374 | 374 | |
|
375 | 375 | self.parmConfObjList.append(parmConfObj) |
|
376 | 376 | |
|
377 | 377 | return parmConfObj |
|
378 | 378 | |
|
379 | 379 | def changeParameter(self, name, value, format='str'): |
|
380 | 380 | |
|
381 | 381 | parmConfObj = self.getParameterObj(name) |
|
382 | 382 | parmConfObj.update(name, value, format) |
|
383 | 383 | |
|
384 | 384 | return parmConfObj |
|
385 | 385 | |
|
386 | 386 | def makeXml(self, procUnitElement): |
|
387 | 387 | |
|
388 | 388 | opElement = SubElement(procUnitElement, self.ELEMENTNAME) |
|
389 | 389 | opElement.set('id', str(self.id)) |
|
390 | 390 | opElement.set('name', self.name) |
|
391 | 391 | opElement.set('type', self.type) |
|
392 | 392 | opElement.set('priority', str(self.priority)) |
|
393 | 393 | |
|
394 | 394 | for parmConfObj in self.parmConfObjList: |
|
395 | 395 | parmConfObj.makeXml(opElement) |
|
396 | 396 | |
|
397 | 397 | def readXml(self, opElement): |
|
398 | 398 | |
|
399 | 399 | self.id = opElement.get('id') |
|
400 | 400 | self.name = opElement.get('name') |
|
401 | 401 | self.type = opElement.get('type') |
|
402 | 402 | self.priority = opElement.get('priority') |
|
403 | 403 | |
|
404 | 404 | #Compatible with old signal chain version |
|
405 | 405 | #Use of 'run' method instead 'init' |
|
406 | 406 | if self.type == 'self' and self.name == 'init': |
|
407 | 407 | self.name = 'run' |
|
408 | 408 | |
|
409 | 409 | self.parmConfObjList = [] |
|
410 | 410 | |
|
411 | 411 | parmElementList = opElement.iter(ParameterConf().getElementName()) |
|
412 | 412 | |
|
413 | 413 | for parmElement in parmElementList: |
|
414 | 414 | parmConfObj = ParameterConf() |
|
415 | 415 | parmConfObj.readXml(parmElement) |
|
416 | 416 | |
|
417 | 417 | #Compatible with old signal chain version |
|
418 | 418 | #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER |
|
419 | 419 | if self.type != 'self' and self.name == 'Plot': |
|
420 | 420 | if parmConfObj.format == 'str' and parmConfObj.name == 'type': |
|
421 | 421 | self.name = parmConfObj.value |
|
422 | 422 | continue |
|
423 | 423 | |
|
424 | 424 | self.parmConfObjList.append(parmConfObj) |
|
425 | 425 | |
|
426 | 426 | def printattr(self): |
|
427 | 427 | |
|
428 | 428 | print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME, |
|
429 | 429 | self.id, |
|
430 | 430 | self.name, |
|
431 | 431 | self.type, |
|
432 | 432 | self.priority) |
|
433 | 433 | |
|
434 | 434 | for parmConfObj in self.parmConfObjList: |
|
435 | 435 | parmConfObj.printattr() |
|
436 | 436 | |
|
437 | 437 | def createObject(self, plotter_queue=None): |
|
438 | 438 | |
|
439 | 439 | |
|
440 | 440 | if self.type == 'self': |
|
441 | 441 | raise ValueError, "This operation type cannot be created" |
|
442 | 442 | |
|
443 | 443 | if self.type == 'plotter': |
|
444 | 444 | #Plotter(plotter_name) |
|
445 | 445 | if not plotter_queue: |
|
446 | 446 | raise ValueError, "plotter_queue is not defined. Use:\nmyProject = Project()\nmyProject.setPlotterQueue(plotter_queue)" |
|
447 | 447 | |
|
448 | 448 | opObj = Plotter(self.name, plotter_queue) |
|
449 | 449 | |
|
450 | 450 | if self.type == 'external' or self.type == 'other': |
|
451 | 451 | |
|
452 | 452 | className = eval(self.name) |
|
453 | 453 | kwargs = self.getKwargs() |
|
454 | 454 | |
|
455 | 455 | opObj = className(**kwargs) |
|
456 | 456 | |
|
457 | 457 | return opObj |
|
458 | 458 | |
|
459 | 459 | |
|
460 | 460 | class ProcUnitConf(): |
|
461 | 461 | |
|
462 | 462 | id = None |
|
463 | 463 | name = None |
|
464 | 464 | datatype = None |
|
465 | 465 | inputId = None |
|
466 | 466 | parentId = None |
|
467 | 467 | |
|
468 | 468 | opConfObjList = [] |
|
469 | 469 | |
|
470 | 470 | procUnitObj = None |
|
471 | 471 | opObjList = [] |
|
472 | 472 | |
|
473 | 473 | ELEMENTNAME = 'ProcUnit' |
|
474 | 474 | |
|
475 | 475 | def __init__(self): |
|
476 | 476 | |
|
477 | 477 | self.id = None |
|
478 | 478 | self.datatype = None |
|
479 | 479 | self.name = None |
|
480 | 480 | self.inputId = None |
|
481 | 481 | |
|
482 | 482 | self.opConfObjList = [] |
|
483 | 483 | |
|
484 | 484 | self.procUnitObj = None |
|
485 | 485 | self.opObjDict = {} |
|
486 | 486 | |
|
487 | 487 | def __getPriority(self): |
|
488 | 488 | |
|
489 | 489 | return len(self.opConfObjList)+1 |
|
490 | 490 | |
|
491 | 491 | def __getNewId(self): |
|
492 | 492 | |
|
493 | 493 | return int(self.id)*10 + len(self.opConfObjList) + 1 |
|
494 | 494 | |
|
495 | 495 | def getElementName(self): |
|
496 | 496 | |
|
497 | 497 | return self.ELEMENTNAME |
|
498 | 498 | |
|
499 | 499 | def getId(self): |
|
500 | 500 | |
|
501 | 501 | return self.id |
|
502 | 502 | |
|
503 | 503 | def updateId(self, new_id, parentId=parentId): |
|
504 | 504 | |
|
505 | 505 | |
|
506 | 506 | new_id = int(parentId)*10 + (int(self.id) % 10) |
|
507 | 507 | new_inputId = int(parentId)*10 + (int(self.inputId) % 10) |
|
508 | 508 | |
|
509 | 509 | #If this proc unit has not inputs |
|
510 | 510 | if self.inputId == '0': |
|
511 | 511 | new_inputId = 0 |
|
512 | 512 | |
|
513 | 513 | n = 1 |
|
514 | 514 | for opConfObj in self.opConfObjList: |
|
515 | 515 | |
|
516 | 516 | idOp = str(int(new_id)*10 + n) |
|
517 | 517 | opConfObj.updateId(idOp) |
|
518 | 518 | |
|
519 | 519 | n += 1 |
|
520 | 520 | |
|
521 | 521 | self.parentId = str(parentId) |
|
522 | 522 | self.id = str(new_id) |
|
523 | 523 | self.inputId = str(new_inputId) |
|
524 | 524 | |
|
525 | 525 | |
|
526 | 526 | def getInputId(self): |
|
527 | 527 | |
|
528 | 528 | return self.inputId |
|
529 | 529 | |
|
530 | 530 | def getOperationObjList(self): |
|
531 | 531 | |
|
532 | 532 | return self.opConfObjList |
|
533 | 533 | |
|
534 | 534 | def getOperationObj(self, name=None): |
|
535 | 535 | |
|
536 | 536 | for opConfObj in self.opConfObjList: |
|
537 | 537 | |
|
538 | 538 | if opConfObj.name != name: |
|
539 | 539 | continue |
|
540 | 540 | |
|
541 | 541 | return opConfObj |
|
542 | 542 | |
|
543 | 543 | return None |
|
544 | 544 | |
|
545 | 545 | def getOpObjfromParamValue(self, value=None): |
|
546 | 546 | |
|
547 | 547 | for opConfObj in self.opConfObjList: |
|
548 | 548 | if opConfObj.getParameterObjfromValue(parameterValue=value) != value: |
|
549 | 549 | continue |
|
550 | 550 | return opConfObj |
|
551 | 551 | return None |
|
552 | 552 | |
|
553 | 553 | def getProcUnitObj(self): |
|
554 | 554 | |
|
555 | 555 | return self.procUnitObj |
|
556 | 556 | |
|
557 | 557 | def setup(self, id, name, datatype, inputId, parentId=None): |
|
558 | 558 | |
|
559 | 559 | #Compatible with old signal chain version |
|
560 | 560 | if datatype==None and name==None: |
|
561 | 561 | raise ValueError, "datatype or name should be defined" |
|
562 | 562 | |
|
563 | 563 | if name==None: |
|
564 | 564 | if 'Proc' in datatype: |
|
565 | 565 | name = datatype |
|
566 | 566 | else: |
|
567 | 567 | name = '%sProc' %(datatype) |
|
568 | 568 | |
|
569 | 569 | if datatype==None: |
|
570 | 570 | datatype = name.replace('Proc','') |
|
571 | 571 | |
|
572 | 572 | self.id = str(id) |
|
573 | 573 | self.name = name |
|
574 | 574 | self.datatype = datatype |
|
575 | 575 | self.inputId = inputId |
|
576 | 576 | self.parentId = parentId |
|
577 | 577 | |
|
578 | 578 | self.opConfObjList = [] |
|
579 | 579 | |
|
580 | 580 | self.addOperation(name='run', optype='self') |
|
581 | 581 | |
|
582 | 582 | def removeOperations(self): |
|
583 | 583 | |
|
584 | 584 | for obj in self.opConfObjList: |
|
585 | 585 | del obj |
|
586 | 586 | |
|
587 | 587 | self.opConfObjList = [] |
|
588 | 588 | self.addOperation(name='run') |
|
589 | 589 | |
|
590 | 590 | def addParameter(self, **kwargs): |
|
591 | 591 | ''' |
|
592 | 592 | Add parameters to "run" operation |
|
593 | 593 | ''' |
|
594 | 594 | opObj = self.opConfObjList[0] |
|
595 | 595 | |
|
596 | 596 | opObj.addParameter(**kwargs) |
|
597 | 597 | |
|
598 | 598 | return opObj |
|
599 | 599 | |
|
600 | 600 | def addOperation(self, name, optype='self'): |
|
601 | 601 | |
|
602 | 602 | id = self.__getNewId() |
|
603 | 603 | priority = self.__getPriority() |
|
604 | 604 | |
|
605 | 605 | opConfObj = OperationConf() |
|
606 | 606 | opConfObj.setup(id, name=name, priority=priority, type=optype) |
|
607 | 607 | |
|
608 | 608 | self.opConfObjList.append(opConfObj) |
|
609 | 609 | |
|
610 | 610 | return opConfObj |
|
611 | 611 | |
|
612 | 612 | def makeXml(self, projectElement): |
|
613 | 613 | |
|
614 | 614 | procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
615 | 615 | procUnitElement.set('id', str(self.id)) |
|
616 | 616 | procUnitElement.set('name', self.name) |
|
617 | 617 | procUnitElement.set('datatype', self.datatype) |
|
618 | 618 | procUnitElement.set('inputId', str(self.inputId)) |
|
619 | 619 | |
|
620 | 620 | for opConfObj in self.opConfObjList: |
|
621 | 621 | opConfObj.makeXml(procUnitElement) |
|
622 | 622 | |
|
623 | 623 | def readXml(self, upElement): |
|
624 | 624 | |
|
625 | 625 | self.id = upElement.get('id') |
|
626 | 626 | self.name = upElement.get('name') |
|
627 | 627 | self.datatype = upElement.get('datatype') |
|
628 | 628 | self.inputId = upElement.get('inputId') |
|
629 | 629 | |
|
630 | 630 | if self.ELEMENTNAME == "ReadUnit": |
|
631 | 631 | self.datatype = self.datatype.replace("Reader", "") |
|
632 | 632 | |
|
633 | 633 | if self.ELEMENTNAME == "ProcUnit": |
|
634 | 634 | self.datatype = self.datatype.replace("Proc", "") |
|
635 | 635 | |
|
636 | 636 | if self.inputId == 'None': |
|
637 | 637 | self.inputId = '0' |
|
638 | 638 | |
|
639 | 639 | self.opConfObjList = [] |
|
640 | 640 | |
|
641 | 641 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
642 | 642 | |
|
643 | 643 | for opElement in opElementList: |
|
644 | 644 | opConfObj = OperationConf() |
|
645 | 645 | opConfObj.readXml(opElement) |
|
646 | 646 | self.opConfObjList.append(opConfObj) |
|
647 | 647 | |
|
648 | 648 | def printattr(self): |
|
649 | 649 | |
|
650 | 650 | print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME, |
|
651 | 651 | self.id, |
|
652 | 652 | self.name, |
|
653 | 653 | self.datatype, |
|
654 | 654 | self.inputId) |
|
655 | 655 | |
|
656 | 656 | for opConfObj in self.opConfObjList: |
|
657 | 657 | opConfObj.printattr() |
|
658 | 658 | |
|
659 | 659 | |
|
660 | 660 | def getKwargs(self): |
|
661 | 661 | |
|
662 | 662 | opObj = self.opConfObjList[0] |
|
663 | 663 | kwargs = opObj.getKwargs() |
|
664 | 664 | |
|
665 | 665 | return kwargs |
|
666 | 666 | |
|
667 | 667 | def createObjects(self, plotter_queue=None): |
|
668 | 668 | |
|
669 | 669 | className = eval(self.name) |
|
670 | 670 | kwargs = self.getKwargs() |
|
671 | 671 | procUnitObj = className(**kwargs) |
|
672 | 672 | |
|
673 | 673 | for opConfObj in self.opConfObjList: |
|
674 | 674 | |
|
675 | 675 | if opConfObj.type=='self' and self.name=='run': |
|
676 | 676 | continue |
|
677 | 677 | elif opConfObj.type=='self': |
|
678 | 678 | procUnitObj.addOperationKwargs(opConfObj.id, **opConfObj.getKwargs()) |
|
679 | 679 | continue |
|
680 | 680 | |
|
681 | 681 | opObj = opConfObj.createObject(plotter_queue) |
|
682 | 682 | |
|
683 | 683 | self.opObjDict[opConfObj.id] = opObj |
|
684 | 684 | |
|
685 | 685 | procUnitObj.addOperation(opObj, opConfObj.id) |
|
686 | 686 | |
|
687 | 687 | self.procUnitObj = procUnitObj |
|
688 | 688 | |
|
689 | 689 | return procUnitObj |
|
690 | 690 | |
|
691 | 691 | def run(self): |
|
692 | 692 | |
|
693 | 693 | is_ok = False |
|
694 | 694 | |
|
695 | 695 | for opConfObj in self.opConfObjList: |
|
696 | 696 | |
|
697 | 697 | kwargs = {} |
|
698 | 698 | for parmConfObj in opConfObj.getParameterObjList(): |
|
699 | 699 | if opConfObj.name == 'run' and parmConfObj.name == 'datatype': |
|
700 | 700 | continue |
|
701 | 701 | |
|
702 | 702 | kwargs[parmConfObj.name] = parmConfObj.getValue() |
|
703 | 703 | |
|
704 | 704 | #ini = time.time() |
|
705 | 705 | |
|
706 | 706 | #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id) |
|
707 | 707 | sts = self.procUnitObj.call(opType = opConfObj.type, |
|
708 | 708 | opName = opConfObj.name, |
|
709 | 709 | opId = opConfObj.id, |
|
710 | 710 | ) |
|
711 | 711 | |
|
712 | 712 | # total_time = time.time() - ini |
|
713 | 713 | # |
|
714 | 714 | # if total_time > 0.002: |
|
715 | 715 | # print "%s::%s took %f seconds" %(self.name, opConfObj.name, total_time) |
|
716 | 716 | |
|
717 | 717 | is_ok = is_ok or sts |
|
718 | 718 | |
|
719 | 719 | return is_ok |
|
720 | 720 | |
|
721 | 721 | def close(self): |
|
722 | 722 | |
|
723 | 723 | for opConfObj in self.opConfObjList: |
|
724 | 724 | if opConfObj.type == 'self': |
|
725 | 725 | continue |
|
726 | 726 | |
|
727 | 727 | opObj = self.procUnitObj.getOperationObj(opConfObj.id) |
|
728 | 728 | opObj.close() |
|
729 | 729 | |
|
730 | 730 | self.procUnitObj.close() |
|
731 | 731 | |
|
732 | 732 | return |
|
733 | 733 | |
|
734 | 734 | class ReadUnitConf(ProcUnitConf): |
|
735 | 735 | |
|
736 | 736 | path = None |
|
737 | 737 | startDate = None |
|
738 | 738 | endDate = None |
|
739 | 739 | startTime = None |
|
740 | 740 | endTime = None |
|
741 | 741 | |
|
742 | 742 | ELEMENTNAME = 'ReadUnit' |
|
743 | 743 | |
|
744 | 744 | def __init__(self): |
|
745 | 745 | |
|
746 | 746 | self.id = None |
|
747 | 747 | self.datatype = None |
|
748 | 748 | self.name = None |
|
749 | 749 | self.inputId = None |
|
750 | 750 | |
|
751 | 751 | self.parentId = None |
|
752 | 752 | |
|
753 | 753 | self.opConfObjList = [] |
|
754 | 754 | self.opObjList = [] |
|
755 | 755 | |
|
756 | 756 | def getElementName(self): |
|
757 | 757 | |
|
758 | 758 | return self.ELEMENTNAME |
|
759 | 759 | |
|
760 | 760 | def setup(self, id, name, datatype, path, startDate="", endDate="", startTime="", endTime="", parentId=None, queue=None, **kwargs): |
|
761 | 761 | |
|
762 | 762 | #Compatible with old signal chain version |
|
763 | 763 | if datatype==None and name==None: |
|
764 | 764 | raise ValueError, "datatype or name should be defined" |
|
765 | 765 | |
|
766 | 766 | if name==None: |
|
767 | 767 | if 'Reader' in datatype: |
|
768 | 768 | name = datatype |
|
769 | 769 | else: |
|
770 | 770 | name = '%sReader' %(datatype) |
|
771 | 771 | |
|
772 | 772 | if datatype==None: |
|
773 | 773 | datatype = name.replace('Reader','') |
|
774 | 774 | |
|
775 | 775 | self.id = id |
|
776 | 776 | self.name = name |
|
777 | 777 | self.datatype = datatype |
|
778 | 778 | |
|
779 | 779 | self.path = os.path.abspath(path) |
|
780 | 780 | self.startDate = startDate |
|
781 | 781 | self.endDate = endDate |
|
782 | 782 | self.startTime = startTime |
|
783 | 783 | self.endTime = endTime |
|
784 | 784 | |
|
785 | 785 | self.inputId = '0' |
|
786 | 786 | self.parentId = parentId |
|
787 | 787 | self.queue = queue |
|
788 | 788 | self.addRunOperation(**kwargs) |
|
789 | 789 | |
|
790 | 790 | def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, name=None, **kwargs): |
|
791 | 791 | |
|
792 | 792 | #Compatible with old signal chain version |
|
793 | 793 | if datatype==None and name==None: |
|
794 | 794 | raise ValueError, "datatype or name should be defined" |
|
795 | 795 | |
|
796 | 796 | if name==None: |
|
797 | 797 | if 'Reader' in datatype: |
|
798 | 798 | name = datatype |
|
799 | 799 | else: |
|
800 | 800 | name = '%sReader' %(datatype) |
|
801 | 801 | |
|
802 | 802 | if datatype==None: |
|
803 | 803 | datatype = name.replace('Reader','') |
|
804 | 804 | |
|
805 | 805 | self.datatype = datatype |
|
806 | 806 | self.name = name |
|
807 | 807 | self.path = path |
|
808 | 808 | self.startDate = startDate |
|
809 | 809 | self.endDate = endDate |
|
810 | 810 | self.startTime = startTime |
|
811 | 811 | self.endTime = endTime |
|
812 | 812 | |
|
813 | 813 | self.inputId = '0' |
|
814 | 814 | self.parentId = parentId |
|
815 | 815 | |
|
816 | 816 | self.updateRunOperation(**kwargs) |
|
817 | 817 | |
|
818 | 818 | def removeOperations(self): |
|
819 | 819 | |
|
820 | 820 | for obj in self.opConfObjList: |
|
821 | 821 | del obj |
|
822 | 822 | |
|
823 | 823 | self.opConfObjList = [] |
|
824 | 824 | |
|
825 | 825 | def addRunOperation(self, **kwargs): |
|
826 | 826 | |
|
827 | 827 | opObj = self.addOperation(name = 'run', optype = 'self') |
|
828 | 828 | |
|
829 | 829 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
830 | 830 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
831 | 831 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
832 | 832 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
833 | 833 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
834 | 834 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
835 | 835 | opObj.addParameter(name='queue' , value=self.queue, format='obj') |
|
836 | 836 | |
|
837 | 837 | for key, value in kwargs.items(): |
|
838 | 838 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
839 | 839 | |
|
840 | 840 | return opObj |
|
841 | 841 | |
|
842 | 842 | def updateRunOperation(self, **kwargs): |
|
843 | 843 | |
|
844 | 844 | opObj = self.getOperationObj(name = 'run') |
|
845 | 845 | opObj.removeParameters() |
|
846 | 846 | |
|
847 | 847 | opObj.addParameter(name='datatype' , value=self.datatype, format='str') |
|
848 | 848 | opObj.addParameter(name='path' , value=self.path, format='str') |
|
849 | 849 | opObj.addParameter(name='startDate' , value=self.startDate, format='date') |
|
850 | 850 | opObj.addParameter(name='endDate' , value=self.endDate, format='date') |
|
851 | 851 | opObj.addParameter(name='startTime' , value=self.startTime, format='time') |
|
852 | 852 | opObj.addParameter(name='endTime' , value=self.endTime, format='time') |
|
853 | 853 | |
|
854 | 854 | for key, value in kwargs.items(): |
|
855 | 855 | opObj.addParameter(name=key, value=value, format=type(value).__name__) |
|
856 | 856 | |
|
857 | 857 | return opObj |
|
858 | 858 | |
|
859 | 859 | # def makeXml(self, projectElement): |
|
860 | 860 | # |
|
861 | 861 | # procUnitElement = SubElement(projectElement, self.ELEMENTNAME) |
|
862 | 862 | # procUnitElement.set('id', str(self.id)) |
|
863 | 863 | # procUnitElement.set('name', self.name) |
|
864 | 864 | # procUnitElement.set('datatype', self.datatype) |
|
865 | 865 | # procUnitElement.set('inputId', str(self.inputId)) |
|
866 | 866 | # |
|
867 | 867 | # for opConfObj in self.opConfObjList: |
|
868 | 868 | # opConfObj.makeXml(procUnitElement) |
|
869 | 869 | |
|
870 | 870 | def readXml(self, upElement): |
|
871 | 871 | |
|
872 | 872 | self.id = upElement.get('id') |
|
873 | 873 | self.name = upElement.get('name') |
|
874 | 874 | self.datatype = upElement.get('datatype') |
|
875 | 875 | self.inputId = upElement.get('inputId') |
|
876 | 876 | |
|
877 | 877 | if self.ELEMENTNAME == "ReadUnit": |
|
878 | 878 | self.datatype = self.datatype.replace("Reader", "") |
|
879 | 879 | |
|
880 | 880 | if self.inputId == 'None': |
|
881 | 881 | self.inputId = '0' |
|
882 | 882 | |
|
883 | 883 | self.opConfObjList = [] |
|
884 | 884 | |
|
885 | 885 | opElementList = upElement.iter(OperationConf().getElementName()) |
|
886 | 886 | |
|
887 | 887 | for opElement in opElementList: |
|
888 | 888 | opConfObj = OperationConf() |
|
889 | 889 | opConfObj.readXml(opElement) |
|
890 | 890 | self.opConfObjList.append(opConfObj) |
|
891 | 891 | |
|
892 | 892 | if opConfObj.name == 'run': |
|
893 | 893 | self.path = opConfObj.getParameterValue('path') |
|
894 | 894 | self.startDate = opConfObj.getParameterValue('startDate') |
|
895 | 895 | self.endDate = opConfObj.getParameterValue('endDate') |
|
896 | 896 | self.startTime = opConfObj.getParameterValue('startTime') |
|
897 | 897 | self.endTime = opConfObj.getParameterValue('endTime') |
|
898 | 898 | |
|
899 | 899 | class Project(): |
|
900 | 900 | |
|
901 | 901 | id = None |
|
902 | 902 | name = None |
|
903 | 903 | description = None |
|
904 | 904 | filename = None |
|
905 | 905 | |
|
906 | 906 | procUnitConfObjDict = None |
|
907 | 907 | |
|
908 | 908 | ELEMENTNAME = 'Project' |
|
909 | 909 | |
|
910 | 910 | plotterQueue = None |
|
911 | 911 | |
|
912 | 912 | def __init__(self, plotter_queue=None): |
|
913 | 913 | |
|
914 | 914 | self.id = None |
|
915 | 915 | self.name = None |
|
916 | 916 | self.description = None |
|
917 | 917 | |
|
918 | 918 | self.plotterQueue = plotter_queue |
|
919 | 919 | |
|
920 | 920 | self.procUnitConfObjDict = {} |
|
921 | 921 | |
|
922 | 922 | def __getNewId(self): |
|
923 | 923 | |
|
924 | 924 | idList = self.procUnitConfObjDict.keys() |
|
925 | 925 | |
|
926 | 926 | id = int(self.id)*10 |
|
927 | 927 | |
|
928 | 928 | while True: |
|
929 | 929 | id += 1 |
|
930 | 930 | |
|
931 | 931 | if str(id) in idList: |
|
932 | 932 | continue |
|
933 | 933 | |
|
934 | 934 | break |
|
935 | 935 | |
|
936 | 936 | return str(id) |
|
937 | 937 | |
|
938 | 938 | def getElementName(self): |
|
939 | 939 | |
|
940 | 940 | return self.ELEMENTNAME |
|
941 | 941 | |
|
942 | 942 | def getId(self): |
|
943 | 943 | |
|
944 | 944 | return self.id |
|
945 | 945 | |
|
946 | 946 | def updateId(self, new_id): |
|
947 | 947 | |
|
948 | 948 | self.id = str(new_id) |
|
949 | 949 | |
|
950 | 950 | keyList = self.procUnitConfObjDict.keys() |
|
951 | 951 | keyList.sort() |
|
952 | 952 | |
|
953 | 953 | n = 1 |
|
954 | 954 | newProcUnitConfObjDict = {} |
|
955 | 955 | |
|
956 | 956 | for procKey in keyList: |
|
957 | 957 | |
|
958 | 958 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
959 | 959 | idProcUnit = str(int(self.id)*10 + n) |
|
960 | 960 | procUnitConfObj.updateId(idProcUnit, parentId = self.id) |
|
961 | 961 | |
|
962 | 962 | newProcUnitConfObjDict[idProcUnit] = procUnitConfObj |
|
963 | 963 | n += 1 |
|
964 | 964 | |
|
965 | 965 | self.procUnitConfObjDict = newProcUnitConfObjDict |
|
966 | 966 | |
|
967 | 967 | def setup(self, id, name, description): |
|
968 | 968 | |
|
969 | 969 | self.id = str(id) |
|
970 | 970 | self.name = name |
|
971 | 971 | self.description = description |
|
972 | 972 | |
|
973 | 973 | def update(self, name, description): |
|
974 | 974 | |
|
975 | 975 | self.name = name |
|
976 | 976 | self.description = description |
|
977 | 977 | |
|
978 | 978 | def addReadUnit(self, id=None, datatype=None, name=None, **kwargs): |
|
979 | 979 | |
|
980 | 980 | if id is None: |
|
981 | 981 | idReadUnit = self.__getNewId() |
|
982 | 982 | else: |
|
983 | 983 | idReadUnit = str(id) |
|
984 | 984 | |
|
985 | 985 | readUnitConfObj = ReadUnitConf() |
|
986 | 986 | readUnitConfObj.setup(idReadUnit, name, datatype, parentId=self.id, **kwargs) |
|
987 | 987 | |
|
988 | 988 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
989 | 989 | |
|
990 | 990 | return readUnitConfObj |
|
991 | 991 | |
|
992 | 992 | def addProcUnit(self, inputId='0', datatype=None, name=None): |
|
993 | 993 | |
|
994 | 994 | idProcUnit = self.__getNewId() |
|
995 | 995 | |
|
996 | 996 | procUnitConfObj = ProcUnitConf() |
|
997 | 997 | procUnitConfObj.setup(idProcUnit, name, datatype, inputId, parentId=self.id) |
|
998 | 998 | |
|
999 | 999 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1000 | 1000 | |
|
1001 | 1001 | return procUnitConfObj |
|
1002 | 1002 | |
|
1003 | 1003 | def removeProcUnit(self, id): |
|
1004 | 1004 | |
|
1005 | 1005 | if id in self.procUnitConfObjDict.keys(): |
|
1006 | 1006 | self.procUnitConfObjDict.pop(id) |
|
1007 | 1007 | |
|
1008 | 1008 | def getReadUnitId(self): |
|
1009 | 1009 | |
|
1010 | 1010 | readUnitConfObj = self.getReadUnitObj() |
|
1011 | 1011 | |
|
1012 | 1012 | return readUnitConfObj.id |
|
1013 | 1013 | |
|
1014 | 1014 | def getReadUnitObj(self): |
|
1015 | 1015 | |
|
1016 | 1016 | for obj in self.procUnitConfObjDict.values(): |
|
1017 | 1017 | if obj.getElementName() == "ReadUnit": |
|
1018 | 1018 | return obj |
|
1019 | 1019 | |
|
1020 | 1020 | return None |
|
1021 | 1021 | |
|
1022 | 1022 | def getProcUnitObj(self, id=None, name=None): |
|
1023 | 1023 | |
|
1024 | 1024 | if id != None: |
|
1025 | 1025 | return self.procUnitConfObjDict[id] |
|
1026 | 1026 | |
|
1027 | 1027 | if name != None: |
|
1028 | 1028 | return self.getProcUnitObjByName(name) |
|
1029 | 1029 | |
|
1030 | 1030 | return None |
|
1031 | 1031 | |
|
1032 | 1032 | def getProcUnitObjByName(self, name): |
|
1033 | 1033 | |
|
1034 | 1034 | for obj in self.procUnitConfObjDict.values(): |
|
1035 | 1035 | if obj.name == name: |
|
1036 | 1036 | return obj |
|
1037 | 1037 | |
|
1038 | 1038 | return None |
|
1039 | 1039 | |
|
1040 | 1040 | def procUnitItems(self): |
|
1041 | 1041 | |
|
1042 | 1042 | return self.procUnitConfObjDict.items() |
|
1043 | 1043 | |
|
1044 | 1044 | def makeXml(self): |
|
1045 | 1045 | |
|
1046 | 1046 | projectElement = Element('Project') |
|
1047 | 1047 | projectElement.set('id', str(self.id)) |
|
1048 | 1048 | projectElement.set('name', self.name) |
|
1049 | 1049 | projectElement.set('description', self.description) |
|
1050 | 1050 | |
|
1051 | 1051 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1052 | 1052 | procUnitConfObj.makeXml(projectElement) |
|
1053 | 1053 | |
|
1054 | 1054 | self.projectElement = projectElement |
|
1055 | 1055 | |
|
1056 | 1056 | def writeXml(self, filename=None): |
|
1057 | 1057 | |
|
1058 | 1058 | if filename == None: |
|
1059 | 1059 | if self.filename: |
|
1060 | 1060 | filename = self.filename |
|
1061 | 1061 | else: |
|
1062 | 1062 | filename = "schain.xml" |
|
1063 | 1063 | |
|
1064 | 1064 | if not filename: |
|
1065 | 1065 | print "filename has not been defined. Use setFilename(filename) for do it." |
|
1066 | 1066 | return 0 |
|
1067 | 1067 | |
|
1068 | 1068 | abs_file = os.path.abspath(filename) |
|
1069 | 1069 | |
|
1070 | 1070 | if not os.access(os.path.dirname(abs_file), os.W_OK): |
|
1071 | 1071 | print "No write permission on %s" %os.path.dirname(abs_file) |
|
1072 | 1072 | return 0 |
|
1073 | 1073 | |
|
1074 | 1074 | if os.path.isfile(abs_file) and not(os.access(abs_file, os.W_OK)): |
|
1075 | 1075 | print "File %s already exists and it could not be overwriten" %abs_file |
|
1076 | 1076 | return 0 |
|
1077 | 1077 | |
|
1078 | 1078 | self.makeXml() |
|
1079 | 1079 | |
|
1080 | 1080 | ElementTree(self.projectElement).write(abs_file, method='xml') |
|
1081 | 1081 | |
|
1082 | 1082 | self.filename = abs_file |
|
1083 | 1083 | |
|
1084 | 1084 | return 1 |
|
1085 | 1085 | |
|
1086 | 1086 | def readXml(self, filename = None): |
|
1087 | 1087 | |
|
1088 | 1088 | if not filename: |
|
1089 | 1089 | print "filename is not defined" |
|
1090 | 1090 | return 0 |
|
1091 | 1091 | |
|
1092 | 1092 | abs_file = os.path.abspath(filename) |
|
1093 | 1093 | |
|
1094 | 1094 | if not os.path.isfile(abs_file): |
|
1095 | 1095 | print "%s file does not exist" %abs_file |
|
1096 | 1096 | return 0 |
|
1097 | 1097 | |
|
1098 | 1098 | self.projectElement = None |
|
1099 | 1099 | self.procUnitConfObjDict = {} |
|
1100 | 1100 | |
|
1101 | 1101 | try: |
|
1102 | 1102 | self.projectElement = ElementTree().parse(abs_file) |
|
1103 | 1103 | except: |
|
1104 | 1104 | print "Error reading %s, verify file format" %filename |
|
1105 | 1105 | return 0 |
|
1106 | 1106 | |
|
1107 | 1107 | self.project = self.projectElement.tag |
|
1108 | 1108 | |
|
1109 | 1109 | self.id = self.projectElement.get('id') |
|
1110 | 1110 | self.name = self.projectElement.get('name') |
|
1111 | 1111 | self.description = self.projectElement.get('description') |
|
1112 | 1112 | |
|
1113 | 1113 | readUnitElementList = self.projectElement.iter(ReadUnitConf().getElementName()) |
|
1114 | 1114 | |
|
1115 | 1115 | for readUnitElement in readUnitElementList: |
|
1116 | 1116 | readUnitConfObj = ReadUnitConf() |
|
1117 | 1117 | readUnitConfObj.readXml(readUnitElement) |
|
1118 | 1118 | |
|
1119 | 1119 | if readUnitConfObj.parentId == None: |
|
1120 | 1120 | readUnitConfObj.parentId = self.id |
|
1121 | 1121 | |
|
1122 | 1122 | self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj |
|
1123 | 1123 | |
|
1124 | 1124 | procUnitElementList = self.projectElement.iter(ProcUnitConf().getElementName()) |
|
1125 | 1125 | |
|
1126 | 1126 | for procUnitElement in procUnitElementList: |
|
1127 | 1127 | procUnitConfObj = ProcUnitConf() |
|
1128 | 1128 | procUnitConfObj.readXml(procUnitElement) |
|
1129 | 1129 | |
|
1130 | 1130 | if procUnitConfObj.parentId == None: |
|
1131 | 1131 | procUnitConfObj.parentId = self.id |
|
1132 | 1132 | |
|
1133 | 1133 | self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj |
|
1134 | 1134 | |
|
1135 | 1135 | self.filename = abs_file |
|
1136 | 1136 | |
|
1137 | 1137 | return 1 |
|
1138 | 1138 | |
|
1139 | 1139 | def printattr(self): |
|
1140 | 1140 | |
|
1141 | 1141 | print "Project[%s]: name = %s, description = %s" %(self.id, |
|
1142 | 1142 | self.name, |
|
1143 | 1143 | self.description) |
|
1144 | 1144 | |
|
1145 | 1145 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1146 | 1146 | procUnitConfObj.printattr() |
|
1147 | 1147 | |
|
1148 | 1148 | def createObjects(self): |
|
1149 | 1149 | |
|
1150 | 1150 | for procUnitConfObj in self.procUnitConfObjDict.values(): |
|
1151 | 1151 | procUnitConfObj.createObjects(self.plotterQueue) |
|
1152 | 1152 | |
|
1153 | 1153 | def __connect(self, objIN, thisObj): |
|
1154 | 1154 | |
|
1155 | 1155 | thisObj.setInput(objIN.getOutputObj()) |
|
1156 | 1156 | |
|
1157 | 1157 | def connectObjects(self): |
|
1158 | 1158 | |
|
1159 | 1159 | for thisPUConfObj in self.procUnitConfObjDict.values(): |
|
1160 | 1160 | |
|
1161 | 1161 | inputId = thisPUConfObj.getInputId() |
|
1162 | 1162 | |
|
1163 | 1163 | if int(inputId) == 0: |
|
1164 | 1164 | continue |
|
1165 | 1165 | |
|
1166 | 1166 | #Get input object |
|
1167 | 1167 | puConfINObj = self.procUnitConfObjDict[inputId] |
|
1168 | 1168 | puObjIN = puConfINObj.getProcUnitObj() |
|
1169 | 1169 | |
|
1170 | 1170 | #Get current object |
|
1171 | 1171 | thisPUObj = thisPUConfObj.getProcUnitObj() |
|
1172 | 1172 | |
|
1173 | 1173 | self.__connect(puObjIN, thisPUObj) |
|
1174 | 1174 | |
|
1175 | 1175 | def __handleError(self, procUnitConfObj, send_email=True): |
|
1176 | 1176 | |
|
1177 | 1177 | import socket |
|
1178 | 1178 | |
|
1179 | 1179 | err = traceback.format_exception(sys.exc_info()[0], |
|
1180 | 1180 | sys.exc_info()[1], |
|
1181 | 1181 | sys.exc_info()[2]) |
|
1182 | 1182 | |
|
1183 | 1183 | print "***** Error occurred in %s *****" %(procUnitConfObj.name) |
|
1184 | 1184 | print "***** %s" %err[-1] |
|
1185 | 1185 | |
|
1186 | 1186 | message = "".join(err) |
|
1187 | 1187 | |
|
1188 | 1188 | sys.stderr.write(message) |
|
1189 | 1189 | |
|
1190 | 1190 | if not send_email: |
|
1191 | 1191 | return |
|
1192 | 1192 | |
|
1193 | 1193 | subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, procUnitConfObj.name) |
|
1194 | 1194 | |
|
1195 | 1195 | subtitle = "%s: %s\n" %(procUnitConfObj.getElementName() ,procUnitConfObj.name) |
|
1196 | 1196 | subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname()) |
|
1197 | 1197 | subtitle += "Working directory: %s\n" %os.path.abspath("./") |
|
1198 | 1198 | subtitle += "Configuration file: %s\n" %self.filename |
|
1199 | 1199 | subtitle += "Time: %s\n" %str(datetime.datetime.now()) |
|
1200 | 1200 | |
|
1201 | 1201 | readUnitConfObj = self.getReadUnitObj() |
|
1202 | 1202 | if readUnitConfObj: |
|
1203 | 1203 | subtitle += "\nInput parameters:\n" |
|
1204 | 1204 | subtitle += "[Data path = %s]\n" %readUnitConfObj.path |
|
1205 | 1205 | subtitle += "[Data type = %s]\n" %readUnitConfObj.datatype |
|
1206 | 1206 | subtitle += "[Start date = %s]\n" %readUnitConfObj.startDate |
|
1207 | 1207 | subtitle += "[End date = %s]\n" %readUnitConfObj.endDate |
|
1208 | 1208 | subtitle += "[Start time = %s]\n" %readUnitConfObj.startTime |
|
1209 | 1209 | subtitle += "[End time = %s]\n" %readUnitConfObj.endTime |
|
1210 | 1210 | |
|
1211 | 1211 | adminObj = schainpy.admin.SchainNotify() |
|
1212 | 1212 | adminObj.sendAlert(message=message, |
|
1213 | 1213 | subject=subject, |
|
1214 | 1214 | subtitle=subtitle, |
|
1215 | 1215 | filename=self.filename) |
|
1216 | 1216 | |
|
1217 | 1217 | def isPaused(self): |
|
1218 | 1218 | return 0 |
|
1219 | 1219 | |
|
1220 | 1220 | def isStopped(self): |
|
1221 | 1221 | return 0 |
|
1222 | 1222 | |
|
1223 | 1223 | def runController(self): |
|
1224 | 1224 | """ |
|
1225 | 1225 | returns 0 when this process has been stopped, 1 otherwise |
|
1226 | 1226 | """ |
|
1227 | 1227 | |
|
1228 | 1228 | if self.isPaused(): |
|
1229 | 1229 | print "Process suspended" |
|
1230 | 1230 | |
|
1231 | 1231 | while True: |
|
1232 | 1232 | sleep(0.1) |
|
1233 | 1233 | |
|
1234 | 1234 | if not self.isPaused(): |
|
1235 | 1235 | break |
|
1236 | 1236 | |
|
1237 | 1237 | if self.isStopped(): |
|
1238 | 1238 | break |
|
1239 | 1239 | |
|
1240 | 1240 | print "Process reinitialized" |
|
1241 | 1241 | |
|
1242 | 1242 | if self.isStopped(): |
|
1243 | 1243 | print "Process stopped" |
|
1244 | 1244 | return 0 |
|
1245 | 1245 | |
|
1246 | 1246 | return 1 |
|
1247 | 1247 | |
|
1248 | 1248 | def setFilename(self, filename): |
|
1249 | 1249 | |
|
1250 | 1250 | self.filename = filename |
|
1251 | 1251 | |
|
1252 | 1252 | def setPlotterQueue(self, plotter_queue): |
|
1253 | 1253 | |
|
1254 | 1254 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1255 | 1255 | |
|
1256 | 1256 | def getPlotterQueue(self): |
|
1257 | 1257 | |
|
1258 | 1258 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1259 | 1259 | |
|
1260 | 1260 | def useExternalPlotter(self): |
|
1261 | 1261 | |
|
1262 | 1262 | raise NotImplementedError, "Use schainpy.controller_api.ControllerThread instead Project class" |
|
1263 | 1263 | |
|
1264 | 1264 | def run(self): |
|
1265 | 1265 | |
|
1266 | 1266 | |
|
1267 | 1267 | print "*"*60 |
|
1268 | 1268 | print " Starting SIGNAL CHAIN PROCESSING v%s " %schainpy.__version__ |
|
1269 | 1269 | print "*"*60 |
|
1270 | 1270 | |
|
1271 | 1271 | |
|
1272 | 1272 | keyList = self.procUnitConfObjDict.keys() |
|
1273 | 1273 | keyList.sort() |
|
1274 | 1274 | |
|
1275 | 1275 | while(True): |
|
1276 | 1276 | |
|
1277 | 1277 | is_ok = False |
|
1278 | 1278 | |
|
1279 | 1279 | for procKey in keyList: |
|
1280 | 1280 | # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id) |
|
1281 | 1281 | |
|
1282 | 1282 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1283 | 1283 | |
|
1284 | 1284 | try: |
|
1285 | 1285 | sts = procUnitConfObj.run() |
|
1286 | 1286 | is_ok = is_ok or sts |
|
1287 | 1287 | except KeyboardInterrupt: |
|
1288 | 1288 | is_ok = False |
|
1289 | 1289 | break |
|
1290 | 1290 | except ValueError, e: |
|
1291 | 1291 | sleep(0.5) |
|
1292 | 1292 | self.__handleError(procUnitConfObj, send_email=True) |
|
1293 | 1293 | is_ok = False |
|
1294 | 1294 | break |
|
1295 | 1295 | except: |
|
1296 | 1296 | sleep(0.5) |
|
1297 | 1297 | self.__handleError(procUnitConfObj) |
|
1298 | 1298 | is_ok = False |
|
1299 | 1299 | break |
|
1300 | 1300 | |
|
1301 | 1301 | #If every process unit finished so end process |
|
1302 | 1302 | if not(is_ok): |
|
1303 | 1303 | # print "Every process unit have finished" |
|
1304 | 1304 | break |
|
1305 | 1305 | |
|
1306 | 1306 | if not self.runController(): |
|
1307 | 1307 | break |
|
1308 | 1308 | |
|
1309 | 1309 | #Closing every process |
|
1310 | 1310 | for procKey in keyList: |
|
1311 | 1311 | procUnitConfObj = self.procUnitConfObjDict[procKey] |
|
1312 | 1312 | procUnitConfObj.close() |
|
1313 | 1313 | |
|
1314 | 1314 | print "Process finished" |
|
1315 | 1315 | |
|
1316 | 1316 | def start(self, filename=None): |
|
1317 | 1317 | |
|
1318 | 1318 | self.writeXml(filename) |
|
1319 | 1319 | self.createObjects() |
|
1320 | 1320 | self.connectObjects() |
|
1321 | 1321 | self.run() |
@@ -1,1228 +1,1220 | |||
|
1 | 1 | ''' |
|
2 | 2 | |
|
3 | 3 | $Author: murco $ |
|
4 | 4 | $Id: JROData.py 173 2012-11-20 15:06:21Z murco $ |
|
5 | 5 | ''' |
|
6 | 6 | |
|
7 | 7 | import copy |
|
8 | 8 | import numpy |
|
9 | 9 | import datetime |
|
10 | 10 | |
|
11 | 11 | from jroheaderIO import SystemHeader, RadarControllerHeader |
|
12 | 12 | from schainpy import cSchain |
|
13 | 13 | |
|
14 | 14 | |
|
15 | 15 | def getNumpyDtype(dataTypeCode): |
|
16 | 16 | |
|
17 | 17 | if dataTypeCode == 0: |
|
18 | 18 | numpyDtype = numpy.dtype([('real','<i1'),('imag','<i1')]) |
|
19 | 19 | elif dataTypeCode == 1: |
|
20 | 20 | numpyDtype = numpy.dtype([('real','<i2'),('imag','<i2')]) |
|
21 | 21 | elif dataTypeCode == 2: |
|
22 | 22 | numpyDtype = numpy.dtype([('real','<i4'),('imag','<i4')]) |
|
23 | 23 | elif dataTypeCode == 3: |
|
24 | 24 | numpyDtype = numpy.dtype([('real','<i8'),('imag','<i8')]) |
|
25 | 25 | elif dataTypeCode == 4: |
|
26 | 26 | numpyDtype = numpy.dtype([('real','<f4'),('imag','<f4')]) |
|
27 | 27 | elif dataTypeCode == 5: |
|
28 | 28 | numpyDtype = numpy.dtype([('real','<f8'),('imag','<f8')]) |
|
29 | 29 | else: |
|
30 | 30 | raise ValueError, 'dataTypeCode was not defined' |
|
31 | 31 | |
|
32 | 32 | return numpyDtype |
|
33 | 33 | |
|
34 | 34 | def getDataTypeCode(numpyDtype): |
|
35 | 35 | |
|
36 | 36 | if numpyDtype == numpy.dtype([('real','<i1'),('imag','<i1')]): |
|
37 | 37 | datatype = 0 |
|
38 | 38 | elif numpyDtype == numpy.dtype([('real','<i2'),('imag','<i2')]): |
|
39 | 39 | datatype = 1 |
|
40 | 40 | elif numpyDtype == numpy.dtype([('real','<i4'),('imag','<i4')]): |
|
41 | 41 | datatype = 2 |
|
42 | 42 | elif numpyDtype == numpy.dtype([('real','<i8'),('imag','<i8')]): |
|
43 | 43 | datatype = 3 |
|
44 | 44 | elif numpyDtype == numpy.dtype([('real','<f4'),('imag','<f4')]): |
|
45 | 45 | datatype = 4 |
|
46 | 46 | elif numpyDtype == numpy.dtype([('real','<f8'),('imag','<f8')]): |
|
47 | 47 | datatype = 5 |
|
48 | 48 | else: |
|
49 | 49 | datatype = None |
|
50 | 50 | |
|
51 | 51 | return datatype |
|
52 | 52 | |
|
53 | 53 | def hildebrand_sekhon(data, navg): |
|
54 | 54 | """ |
|
55 | 55 | This method is for the objective determination of the noise level in Doppler spectra. This |
|
56 | 56 | implementation technique is based on the fact that the standard deviation of the spectral |
|
57 | 57 | densities is equal to the mean spectral density for white Gaussian noise |
|
58 | 58 | |
|
59 | 59 | Inputs: |
|
60 | 60 | Data : heights |
|
61 | 61 | navg : numbers of averages |
|
62 | 62 | |
|
63 | 63 | Return: |
|
64 | 64 | -1 : any error |
|
65 | 65 | anoise : noise's level |
|
66 | 66 | """ |
|
67 | 67 | |
|
68 | 68 | sortdata = numpy.sort(data, axis=None) |
|
69 | 69 | # lenOfData = len(sortdata) |
|
70 | 70 | # nums_min = lenOfData*0.2 |
|
71 | 71 | # |
|
72 | 72 | # if nums_min <= 5: |
|
73 | 73 | # nums_min = 5 |
|
74 | 74 | # |
|
75 | 75 | # sump = 0. |
|
76 | 76 | # |
|
77 | 77 | # sumq = 0. |
|
78 | 78 | # |
|
79 | 79 | # j = 0 |
|
80 | 80 | # |
|
81 | 81 | # cont = 1 |
|
82 | 82 | # |
|
83 | 83 | # while((cont==1)and(j<lenOfData)): |
|
84 | 84 | # |
|
85 | 85 | # sump += sortdata[j] |
|
86 | 86 | # |
|
87 | 87 | # sumq += sortdata[j]**2 |
|
88 | 88 | # |
|
89 | 89 | # if j > nums_min: |
|
90 | 90 | # rtest = float(j)/(j-1) + 1.0/navg |
|
91 | 91 | # if ((sumq*j) > (rtest*sump**2)): |
|
92 | 92 | # j = j - 1 |
|
93 | 93 | # sump = sump - sortdata[j] |
|
94 | 94 | # sumq = sumq - sortdata[j]**2 |
|
95 | 95 | # cont = 0 |
|
96 | 96 | # |
|
97 | 97 | # j += 1 |
|
98 | 98 | # |
|
99 | 99 | # lnoise = sump /j |
|
100 | 100 | # |
|
101 | 101 | # return lnoise |
|
102 | 102 | |
|
103 | 103 | return cSchain.hildebrand_sekhon(sortdata, navg) |
|
104 | 104 | |
|
105 | 105 | |
|
106 | 106 | class Beam: |
|
107 | 107 | |
|
108 | 108 | def __init__(self): |
|
109 | 109 | self.codeList = [] |
|
110 | 110 | self.azimuthList = [] |
|
111 | 111 | self.zenithList = [] |
|
112 | 112 | |
|
113 | 113 | class GenericData(object): |
|
114 | 114 | |
|
115 | 115 | flagNoData = True |
|
116 | 116 | |
|
117 | def __init__(self): | |
|
118 | ||
|
119 | raise NotImplementedError | |
|
120 | ||
|
121 | 117 | def copy(self, inputObj=None): |
|
122 | 118 | |
|
123 | 119 | if inputObj == None: |
|
124 | 120 | return copy.deepcopy(self) |
|
125 | 121 | |
|
126 | 122 | for key in inputObj.__dict__.keys(): |
|
127 | 123 | |
|
128 | 124 | attribute = inputObj.__dict__[key] |
|
129 | 125 | |
|
130 | 126 | #If this attribute is a tuple or list |
|
131 | 127 | if type(inputObj.__dict__[key]) in (tuple, list): |
|
132 | 128 | self.__dict__[key] = attribute[:] |
|
133 | 129 | continue |
|
134 | 130 | |
|
135 | 131 | #If this attribute is another object or instance |
|
136 | 132 | if hasattr(attribute, '__dict__'): |
|
137 | 133 | self.__dict__[key] = attribute.copy() |
|
138 | 134 | continue |
|
139 | 135 | |
|
140 | 136 | self.__dict__[key] = inputObj.__dict__[key] |
|
141 | 137 | |
|
142 | 138 | def deepcopy(self): |
|
143 | 139 | |
|
144 | 140 | return copy.deepcopy(self) |
|
145 | 141 | |
|
146 | 142 | def isEmpty(self): |
|
147 | 143 | |
|
148 | 144 | return self.flagNoData |
|
149 | 145 | |
|
150 | 146 | class JROData(GenericData): |
|
151 | 147 | |
|
152 | 148 | # m_BasicHeader = BasicHeader() |
|
153 | 149 | # m_ProcessingHeader = ProcessingHeader() |
|
154 | 150 | |
|
155 | 151 | systemHeaderObj = SystemHeader() |
|
156 | 152 | |
|
157 | 153 | radarControllerHeaderObj = RadarControllerHeader() |
|
158 | 154 | |
|
159 | 155 | # data = None |
|
160 | 156 | |
|
161 | 157 | type = None |
|
162 | 158 | |
|
163 | 159 | datatype = None #dtype but in string |
|
164 | 160 | |
|
165 | 161 | # dtype = None |
|
166 | 162 | |
|
167 | 163 | # nChannels = None |
|
168 | 164 | |
|
169 | 165 | # nHeights = None |
|
170 | 166 | |
|
171 | 167 | nProfiles = None |
|
172 | 168 | |
|
173 | 169 | heightList = None |
|
174 | 170 | |
|
175 | 171 | channelList = None |
|
176 | 172 | |
|
177 | 173 | flagDiscontinuousBlock = False |
|
178 | 174 | |
|
179 | 175 | useLocalTime = False |
|
180 | 176 | |
|
181 | 177 | utctime = None |
|
182 | 178 | |
|
183 | 179 | timeZone = None |
|
184 | 180 | |
|
185 | 181 | dstFlag = None |
|
186 | 182 | |
|
187 | 183 | errorCount = None |
|
188 | 184 | |
|
189 | 185 | blocksize = None |
|
190 | 186 | |
|
191 | 187 | # nCode = None |
|
192 | 188 | # |
|
193 | 189 | # nBaud = None |
|
194 | 190 | # |
|
195 | 191 | # code = None |
|
196 | 192 | |
|
197 | 193 | flagDecodeData = False #asumo q la data no esta decodificada |
|
198 | 194 | |
|
199 | 195 | flagDeflipData = False #asumo q la data no esta sin flip |
|
200 | 196 | |
|
201 | 197 | flagShiftFFT = False |
|
202 | 198 | |
|
203 | 199 | # ippSeconds = None |
|
204 | 200 | |
|
205 | 201 | # timeInterval = None |
|
206 | 202 | |
|
207 | 203 | nCohInt = None |
|
208 | 204 | |
|
209 | 205 | # noise = None |
|
210 | 206 | |
|
211 | 207 | windowOfFilter = 1 |
|
212 | 208 | |
|
213 | 209 | #Speed of ligth |
|
214 | 210 | C = 3e8 |
|
215 | 211 | |
|
216 | 212 | frequency = 49.92e6 |
|
217 | 213 | |
|
218 | 214 | realtime = False |
|
219 | 215 | |
|
220 | 216 | beacon_heiIndexList = None |
|
221 | 217 | |
|
222 | 218 | last_block = None |
|
223 | 219 | |
|
224 | 220 | blocknow = None |
|
225 | 221 | |
|
226 | 222 | azimuth = None |
|
227 | 223 | |
|
228 | 224 | zenith = None |
|
229 | 225 | |
|
230 | 226 | beam = Beam() |
|
231 | 227 | |
|
232 | 228 | profileIndex = None |
|
233 | 229 | |
|
234 | def __init__(self): | |
|
235 | ||
|
236 | raise NotImplementedError | |
|
237 | ||
|
238 | 230 | def getNoise(self): |
|
239 | 231 | |
|
240 | 232 | raise NotImplementedError |
|
241 | 233 | |
|
242 | 234 | def getNChannels(self): |
|
243 | 235 | |
|
244 | 236 | return len(self.channelList) |
|
245 | 237 | |
|
246 | 238 | def getChannelIndexList(self): |
|
247 | 239 | |
|
248 | 240 | return range(self.nChannels) |
|
249 | 241 | |
|
250 | 242 | def getNHeights(self): |
|
251 | 243 | |
|
252 | 244 | return len(self.heightList) |
|
253 | 245 | |
|
254 | 246 | def getHeiRange(self, extrapoints=0): |
|
255 | 247 | |
|
256 | 248 | heis = self.heightList |
|
257 | 249 | # deltah = self.heightList[1] - self.heightList[0] |
|
258 | 250 | # |
|
259 | 251 | # heis.append(self.heightList[-1]) |
|
260 | 252 | |
|
261 | 253 | return heis |
|
262 | 254 | |
|
263 | 255 | def getDeltaH(self): |
|
264 | 256 | |
|
265 | 257 | delta = self.heightList[1] - self.heightList[0] |
|
266 | 258 | |
|
267 | 259 | return delta |
|
268 | 260 | |
|
269 | 261 | def getltctime(self): |
|
270 | 262 | |
|
271 | 263 | if self.useLocalTime: |
|
272 | 264 | return self.utctime - self.timeZone*60 |
|
273 | 265 | |
|
274 | 266 | return self.utctime |
|
275 | 267 | |
|
276 | 268 | def getDatatime(self): |
|
277 | 269 | |
|
278 | 270 | datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime) |
|
279 | 271 | return datatimeValue |
|
280 | 272 | |
|
281 | 273 | def getTimeRange(self): |
|
282 | 274 | |
|
283 | 275 | datatime = [] |
|
284 | 276 | |
|
285 | 277 | datatime.append(self.ltctime) |
|
286 | 278 | datatime.append(self.ltctime + self.timeInterval+1) |
|
287 | 279 | |
|
288 | 280 | datatime = numpy.array(datatime) |
|
289 | 281 | |
|
290 | 282 | return datatime |
|
291 | 283 | |
|
292 | 284 | def getFmaxTimeResponse(self): |
|
293 | 285 | |
|
294 | 286 | period = (10**-6)*self.getDeltaH()/(0.15) |
|
295 | 287 | |
|
296 | 288 | PRF = 1./(period * self.nCohInt) |
|
297 | 289 | |
|
298 | 290 | fmax = PRF |
|
299 | 291 | |
|
300 | 292 | return fmax |
|
301 | 293 | |
|
302 | 294 | def getFmax(self): |
|
303 | 295 | |
|
304 | 296 | PRF = 1./(self.ippSeconds * self.nCohInt) |
|
305 | 297 | |
|
306 | 298 | fmax = PRF |
|
307 | 299 | |
|
308 | 300 | return fmax |
|
309 | 301 | |
|
310 | 302 | def getVmax(self): |
|
311 | 303 | |
|
312 | 304 | _lambda = self.C/self.frequency |
|
313 | 305 | |
|
314 | 306 | vmax = self.getFmax() * _lambda/2 |
|
315 | 307 | |
|
316 | 308 | return vmax |
|
317 | 309 | |
|
318 | 310 | def get_ippSeconds(self): |
|
319 | 311 | ''' |
|
320 | 312 | ''' |
|
321 | 313 | return self.radarControllerHeaderObj.ippSeconds |
|
322 | 314 | |
|
323 | 315 | def set_ippSeconds(self, ippSeconds): |
|
324 | 316 | ''' |
|
325 | 317 | ''' |
|
326 | 318 | |
|
327 | 319 | self.radarControllerHeaderObj.ippSeconds = ippSeconds |
|
328 | 320 | |
|
329 | 321 | return |
|
330 | 322 | |
|
331 | 323 | def get_dtype(self): |
|
332 | 324 | ''' |
|
333 | 325 | ''' |
|
334 | 326 | return getNumpyDtype(self.datatype) |
|
335 | 327 | |
|
336 | 328 | def set_dtype(self, numpyDtype): |
|
337 | 329 | ''' |
|
338 | 330 | ''' |
|
339 | 331 | |
|
340 | 332 | self.datatype = getDataTypeCode(numpyDtype) |
|
341 | 333 | |
|
342 | 334 | def get_code(self): |
|
343 | 335 | ''' |
|
344 | 336 | ''' |
|
345 | 337 | return self.radarControllerHeaderObj.code |
|
346 | 338 | |
|
347 | 339 | def set_code(self, code): |
|
348 | 340 | ''' |
|
349 | 341 | ''' |
|
350 | 342 | self.radarControllerHeaderObj.code = code |
|
351 | 343 | |
|
352 | 344 | return |
|
353 | 345 | |
|
354 | 346 | def get_ncode(self): |
|
355 | 347 | ''' |
|
356 | 348 | ''' |
|
357 | 349 | return self.radarControllerHeaderObj.nCode |
|
358 | 350 | |
|
359 | 351 | def set_ncode(self, nCode): |
|
360 | 352 | ''' |
|
361 | 353 | ''' |
|
362 | 354 | self.radarControllerHeaderObj.nCode = nCode |
|
363 | 355 | |
|
364 | 356 | return |
|
365 | 357 | |
|
366 | 358 | def get_nbaud(self): |
|
367 | 359 | ''' |
|
368 | 360 | ''' |
|
369 | 361 | return self.radarControllerHeaderObj.nBaud |
|
370 | 362 | |
|
371 | 363 | def set_nbaud(self, nBaud): |
|
372 | 364 | ''' |
|
373 | 365 | ''' |
|
374 | 366 | self.radarControllerHeaderObj.nBaud = nBaud |
|
375 | 367 | |
|
376 | 368 | return |
|
377 | 369 | |
|
378 | 370 | nChannels = property(getNChannels, "I'm the 'nChannel' property.") |
|
379 | 371 | channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.") |
|
380 | 372 | nHeights = property(getNHeights, "I'm the 'nHeights' property.") |
|
381 | 373 | #noise = property(getNoise, "I'm the 'nHeights' property.") |
|
382 | 374 | datatime = property(getDatatime, "I'm the 'datatime' property") |
|
383 | 375 | ltctime = property(getltctime, "I'm the 'ltctime' property") |
|
384 | 376 | ippSeconds = property(get_ippSeconds, set_ippSeconds) |
|
385 | 377 | dtype = property(get_dtype, set_dtype) |
|
386 | 378 | # timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
387 | 379 | code = property(get_code, set_code) |
|
388 | 380 | nCode = property(get_ncode, set_ncode) |
|
389 | 381 | nBaud = property(get_nbaud, set_nbaud) |
|
390 | 382 | |
|
391 | 383 | class Voltage(JROData): |
|
392 | 384 | |
|
393 | 385 | #data es un numpy array de 2 dmensiones (canales, alturas) |
|
394 | 386 | data = None |
|
395 | 387 | |
|
396 | 388 | def __init__(self): |
|
397 | 389 | ''' |
|
398 | 390 | Constructor |
|
399 | 391 | ''' |
|
400 | 392 | |
|
401 | 393 | self.useLocalTime = True |
|
402 | 394 | |
|
403 | 395 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
404 | 396 | |
|
405 | 397 | self.systemHeaderObj = SystemHeader() |
|
406 | 398 | |
|
407 | 399 | self.type = "Voltage" |
|
408 | 400 | |
|
409 | 401 | self.data = None |
|
410 | 402 | |
|
411 | 403 | # self.dtype = None |
|
412 | 404 | |
|
413 | 405 | # self.nChannels = 0 |
|
414 | 406 | |
|
415 | 407 | # self.nHeights = 0 |
|
416 | 408 | |
|
417 | 409 | self.nProfiles = None |
|
418 | 410 | |
|
419 | 411 | self.heightList = None |
|
420 | 412 | |
|
421 | 413 | self.channelList = None |
|
422 | 414 | |
|
423 | 415 | # self.channelIndexList = None |
|
424 | 416 | |
|
425 | 417 | self.flagNoData = True |
|
426 | 418 | |
|
427 | 419 | self.flagDiscontinuousBlock = False |
|
428 | 420 | |
|
429 | 421 | self.utctime = None |
|
430 | 422 | |
|
431 | 423 | self.timeZone = None |
|
432 | 424 | |
|
433 | 425 | self.dstFlag = None |
|
434 | 426 | |
|
435 | 427 | self.errorCount = None |
|
436 | 428 | |
|
437 | 429 | self.nCohInt = None |
|
438 | 430 | |
|
439 | 431 | self.blocksize = None |
|
440 | 432 | |
|
441 | 433 | self.flagDecodeData = False #asumo q la data no esta decodificada |
|
442 | 434 | |
|
443 | 435 | self.flagDeflipData = False #asumo q la data no esta sin flip |
|
444 | 436 | |
|
445 | 437 | self.flagShiftFFT = False |
|
446 | 438 | |
|
447 | 439 | self.flagDataAsBlock = False #Asumo que la data es leida perfil a perfil |
|
448 | 440 | |
|
449 | 441 | self.profileIndex = 0 |
|
450 | 442 | |
|
451 | 443 | def getNoisebyHildebrand(self, channel = None): |
|
452 | 444 | """ |
|
453 | 445 | Determino el nivel de ruido usando el metodo Hildebrand-Sekhon |
|
454 | 446 | |
|
455 | 447 | Return: |
|
456 | 448 | noiselevel |
|
457 | 449 | """ |
|
458 | 450 | |
|
459 | 451 | if channel != None: |
|
460 | 452 | data = self.data[channel] |
|
461 | 453 | nChannels = 1 |
|
462 | 454 | else: |
|
463 | 455 | data = self.data |
|
464 | 456 | nChannels = self.nChannels |
|
465 | 457 | |
|
466 | 458 | noise = numpy.zeros(nChannels) |
|
467 | 459 | power = data * numpy.conjugate(data) |
|
468 | 460 | |
|
469 | 461 | for thisChannel in range(nChannels): |
|
470 | 462 | if nChannels == 1: |
|
471 | 463 | daux = power[:].real |
|
472 | 464 | else: |
|
473 | 465 | daux = power[thisChannel,:].real |
|
474 | 466 | noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt) |
|
475 | 467 | |
|
476 | 468 | return noise |
|
477 | 469 | |
|
478 | 470 | def getNoise(self, type = 1, channel = None): |
|
479 | 471 | |
|
480 | 472 | if type == 1: |
|
481 | 473 | noise = self.getNoisebyHildebrand(channel) |
|
482 | 474 | |
|
483 | 475 | return noise |
|
484 | 476 | |
|
485 | 477 | def getPower(self, channel = None): |
|
486 | 478 | |
|
487 | 479 | if channel != None: |
|
488 | 480 | data = self.data[channel] |
|
489 | 481 | else: |
|
490 | 482 | data = self.data |
|
491 | 483 | |
|
492 | 484 | power = data * numpy.conjugate(data) |
|
493 | 485 | powerdB = 10*numpy.log10(power.real) |
|
494 | 486 | powerdB = numpy.squeeze(powerdB) |
|
495 | 487 | |
|
496 | 488 | return powerdB |
|
497 | 489 | |
|
498 | 490 | def getTimeInterval(self): |
|
499 | 491 | |
|
500 | 492 | timeInterval = self.ippSeconds * self.nCohInt |
|
501 | 493 | |
|
502 | 494 | return timeInterval |
|
503 | 495 | |
|
504 | 496 | noise = property(getNoise, "I'm the 'nHeights' property.") |
|
505 | 497 | timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
506 | 498 | |
|
507 | 499 | class Spectra(JROData): |
|
508 | 500 | |
|
509 | 501 | #data spc es un numpy array de 2 dmensiones (canales, perfiles, alturas) |
|
510 | 502 | data_spc = None |
|
511 | 503 | |
|
512 | 504 | #data cspc es un numpy array de 2 dmensiones (canales, pares, alturas) |
|
513 | 505 | data_cspc = None |
|
514 | 506 | |
|
515 | 507 | #data dc es un numpy array de 2 dmensiones (canales, alturas) |
|
516 | 508 | data_dc = None |
|
517 | 509 | |
|
518 | 510 | #data power |
|
519 | 511 | data_pwr = None |
|
520 | 512 | |
|
521 | 513 | nFFTPoints = None |
|
522 | 514 | |
|
523 | 515 | # nPairs = None |
|
524 | 516 | |
|
525 | 517 | pairsList = None |
|
526 | 518 | |
|
527 | 519 | nIncohInt = None |
|
528 | 520 | |
|
529 | 521 | wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia |
|
530 | 522 | |
|
531 | 523 | nCohInt = None #se requiere para determinar el valor de timeInterval |
|
532 | 524 | |
|
533 | 525 | ippFactor = None |
|
534 | 526 | |
|
535 | 527 | profileIndex = 0 |
|
536 | 528 | |
|
537 | 529 | plotting = "spectra" |
|
538 | 530 | |
|
539 | 531 | def __init__(self): |
|
540 | 532 | ''' |
|
541 | 533 | Constructor |
|
542 | 534 | ''' |
|
543 | 535 | |
|
544 | 536 | self.useLocalTime = True |
|
545 | 537 | |
|
546 | 538 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
547 | 539 | |
|
548 | 540 | self.systemHeaderObj = SystemHeader() |
|
549 | 541 | |
|
550 | 542 | self.type = "Spectra" |
|
551 | 543 | |
|
552 | 544 | # self.data = None |
|
553 | 545 | |
|
554 | 546 | # self.dtype = None |
|
555 | 547 | |
|
556 | 548 | # self.nChannels = 0 |
|
557 | 549 | |
|
558 | 550 | # self.nHeights = 0 |
|
559 | 551 | |
|
560 | 552 | self.nProfiles = None |
|
561 | 553 | |
|
562 | 554 | self.heightList = None |
|
563 | 555 | |
|
564 | 556 | self.channelList = None |
|
565 | 557 | |
|
566 | 558 | # self.channelIndexList = None |
|
567 | 559 | |
|
568 | 560 | self.pairsList = None |
|
569 | 561 | |
|
570 | 562 | self.flagNoData = True |
|
571 | 563 | |
|
572 | 564 | self.flagDiscontinuousBlock = False |
|
573 | 565 | |
|
574 | 566 | self.utctime = None |
|
575 | 567 | |
|
576 | 568 | self.nCohInt = None |
|
577 | 569 | |
|
578 | 570 | self.nIncohInt = None |
|
579 | 571 | |
|
580 | 572 | self.blocksize = None |
|
581 | 573 | |
|
582 | 574 | self.nFFTPoints = None |
|
583 | 575 | |
|
584 | 576 | self.wavelength = None |
|
585 | 577 | |
|
586 | 578 | self.flagDecodeData = False #asumo q la data no esta decodificada |
|
587 | 579 | |
|
588 | 580 | self.flagDeflipData = False #asumo q la data no esta sin flip |
|
589 | 581 | |
|
590 | 582 | self.flagShiftFFT = False |
|
591 | 583 | |
|
592 | 584 | self.ippFactor = 1 |
|
593 | 585 | |
|
594 | 586 | #self.noise = None |
|
595 | 587 | |
|
596 | 588 | self.beacon_heiIndexList = [] |
|
597 | 589 | |
|
598 | 590 | self.noise_estimation = None |
|
599 | 591 | |
|
600 | 592 | |
|
601 | 593 | def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None): |
|
602 | 594 | """ |
|
603 | 595 | Determino el nivel de ruido usando el metodo Hildebrand-Sekhon |
|
604 | 596 | |
|
605 | 597 | Return: |
|
606 | 598 | noiselevel |
|
607 | 599 | """ |
|
608 | 600 | |
|
609 | 601 | noise = numpy.zeros(self.nChannels) |
|
610 | 602 | |
|
611 | 603 | for channel in range(self.nChannels): |
|
612 | 604 | daux = self.data_spc[channel,xmin_index:xmax_index,ymin_index:ymax_index] |
|
613 | 605 | noise[channel] = hildebrand_sekhon(daux, self.nIncohInt) |
|
614 | 606 | |
|
615 | 607 | return noise |
|
616 | 608 | |
|
617 | 609 | def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None): |
|
618 | 610 | |
|
619 | 611 | if self.noise_estimation is not None: |
|
620 | 612 | return self.noise_estimation #this was estimated by getNoise Operation defined in jroproc_spectra.py |
|
621 | 613 | else: |
|
622 | 614 | noise = self.getNoisebyHildebrand(xmin_index, xmax_index, ymin_index, ymax_index) |
|
623 | 615 | return noise |
|
624 | 616 | |
|
625 | 617 | def getFreqRangeTimeResponse(self, extrapoints=0): |
|
626 | 618 | |
|
627 | 619 | deltafreq = self.getFmaxTimeResponse() / (self.nFFTPoints*self.ippFactor) |
|
628 | 620 | freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2 |
|
629 | 621 | |
|
630 | 622 | return freqrange |
|
631 | 623 | |
|
632 | 624 | def getAcfRange(self, extrapoints=0): |
|
633 | 625 | |
|
634 | 626 | deltafreq = 10./(self.getFmax() / (self.nFFTPoints*self.ippFactor)) |
|
635 | 627 | freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2 |
|
636 | 628 | |
|
637 | 629 | return freqrange |
|
638 | 630 | |
|
639 | 631 | def getFreqRange(self, extrapoints=0): |
|
640 | 632 | |
|
641 | 633 | deltafreq = self.getFmax() / (self.nFFTPoints*self.ippFactor) |
|
642 | 634 | freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2 |
|
643 | 635 | |
|
644 | 636 | return freqrange |
|
645 | 637 | |
|
646 | 638 | def getVelRange(self, extrapoints=0): |
|
647 | 639 | |
|
648 | 640 | deltav = self.getVmax() / (self.nFFTPoints*self.ippFactor) |
|
649 | 641 | velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) #- deltav/2 |
|
650 | 642 | |
|
651 | 643 | return velrange |
|
652 | 644 | |
|
653 | 645 | def getNPairs(self): |
|
654 | 646 | |
|
655 | 647 | return len(self.pairsList) |
|
656 | 648 | |
|
657 | 649 | def getPairsIndexList(self): |
|
658 | 650 | |
|
659 | 651 | return range(self.nPairs) |
|
660 | 652 | |
|
661 | 653 | def getNormFactor(self): |
|
662 | 654 | |
|
663 | 655 | pwcode = 1 |
|
664 | 656 | |
|
665 | 657 | if self.flagDecodeData: |
|
666 | 658 | pwcode = numpy.sum(self.code[0]**2) |
|
667 | 659 | #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter |
|
668 | 660 | normFactor = self.nProfiles*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter |
|
669 | 661 | |
|
670 | 662 | return normFactor |
|
671 | 663 | |
|
672 | 664 | def getFlagCspc(self): |
|
673 | 665 | |
|
674 | 666 | if self.data_cspc is None: |
|
675 | 667 | return True |
|
676 | 668 | |
|
677 | 669 | return False |
|
678 | 670 | |
|
679 | 671 | def getFlagDc(self): |
|
680 | 672 | |
|
681 | 673 | if self.data_dc is None: |
|
682 | 674 | return True |
|
683 | 675 | |
|
684 | 676 | return False |
|
685 | 677 | |
|
686 | 678 | def getTimeInterval(self): |
|
687 | 679 | |
|
688 | 680 | timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles |
|
689 | 681 | |
|
690 | 682 | return timeInterval |
|
691 | 683 | |
|
692 | 684 | def getPower(self): |
|
693 | 685 | |
|
694 | 686 | factor = self.normFactor |
|
695 | 687 | z = self.data_spc/factor |
|
696 | 688 | z = numpy.where(numpy.isfinite(z), z, numpy.NAN) |
|
697 | 689 | avg = numpy.average(z, axis=1) |
|
698 | 690 | |
|
699 | 691 | return 10*numpy.log10(avg) |
|
700 | 692 | |
|
701 | 693 | def getCoherence(self, pairsList=None, phase=False): |
|
702 | 694 | |
|
703 | 695 | z = [] |
|
704 | 696 | if pairsList is None: |
|
705 | 697 | pairsIndexList = self.pairsIndexList |
|
706 | 698 | else: |
|
707 | 699 | pairsIndexList = [] |
|
708 | 700 | for pair in pairsList: |
|
709 | 701 | if pair not in self.pairsList: |
|
710 | 702 | raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair) |
|
711 | 703 | pairsIndexList.append(self.pairsList.index(pair)) |
|
712 | 704 | for i in range(len(pairsIndexList)): |
|
713 | 705 | pair = self.pairsList[pairsIndexList[i]] |
|
714 | 706 | ccf = numpy.average(self.data_cspc[pairsIndexList[i], :, :], axis=0) |
|
715 | 707 | powa = numpy.average(self.data_spc[pair[0], :, :], axis=0) |
|
716 | 708 | powb = numpy.average(self.data_spc[pair[1], :, :], axis=0) |
|
717 | 709 | avgcoherenceComplex = ccf/numpy.sqrt(powa*powb) |
|
718 | 710 | if phase: |
|
719 | 711 | data = numpy.arctan2(avgcoherenceComplex.imag, |
|
720 | 712 | avgcoherenceComplex.real)*180/numpy.pi |
|
721 | 713 | else: |
|
722 | 714 | data = numpy.abs(avgcoherenceComplex) |
|
723 | 715 | |
|
724 | 716 | z.append(data) |
|
725 | 717 | |
|
726 | 718 | return numpy.array(z) |
|
727 | 719 | |
|
728 | 720 | def setValue(self, value): |
|
729 | 721 | |
|
730 | 722 | print "This property should not be initialized" |
|
731 | 723 | |
|
732 | 724 | return |
|
733 | 725 | |
|
734 | 726 | nPairs = property(getNPairs, setValue, "I'm the 'nPairs' property.") |
|
735 | 727 | pairsIndexList = property(getPairsIndexList, setValue, "I'm the 'pairsIndexList' property.") |
|
736 | 728 | normFactor = property(getNormFactor, setValue, "I'm the 'getNormFactor' property.") |
|
737 | 729 | flag_cspc = property(getFlagCspc, setValue) |
|
738 | 730 | flag_dc = property(getFlagDc, setValue) |
|
739 | 731 | noise = property(getNoise, setValue, "I'm the 'nHeights' property.") |
|
740 | 732 | timeInterval = property(getTimeInterval, setValue, "I'm the 'timeInterval' property") |
|
741 | 733 | |
|
742 | 734 | class SpectraHeis(Spectra): |
|
743 | 735 | |
|
744 | 736 | data_spc = None |
|
745 | 737 | |
|
746 | 738 | data_cspc = None |
|
747 | 739 | |
|
748 | 740 | data_dc = None |
|
749 | 741 | |
|
750 | 742 | nFFTPoints = None |
|
751 | 743 | |
|
752 | 744 | # nPairs = None |
|
753 | 745 | |
|
754 | 746 | pairsList = None |
|
755 | 747 | |
|
756 | 748 | nCohInt = None |
|
757 | 749 | |
|
758 | 750 | nIncohInt = None |
|
759 | 751 | |
|
760 | 752 | def __init__(self): |
|
761 | 753 | |
|
762 | 754 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
763 | 755 | |
|
764 | 756 | self.systemHeaderObj = SystemHeader() |
|
765 | 757 | |
|
766 | 758 | self.type = "SpectraHeis" |
|
767 | 759 | |
|
768 | 760 | # self.dtype = None |
|
769 | 761 | |
|
770 | 762 | # self.nChannels = 0 |
|
771 | 763 | |
|
772 | 764 | # self.nHeights = 0 |
|
773 | 765 | |
|
774 | 766 | self.nProfiles = None |
|
775 | 767 | |
|
776 | 768 | self.heightList = None |
|
777 | 769 | |
|
778 | 770 | self.channelList = None |
|
779 | 771 | |
|
780 | 772 | # self.channelIndexList = None |
|
781 | 773 | |
|
782 | 774 | self.flagNoData = True |
|
783 | 775 | |
|
784 | 776 | self.flagDiscontinuousBlock = False |
|
785 | 777 | |
|
786 | 778 | # self.nPairs = 0 |
|
787 | 779 | |
|
788 | 780 | self.utctime = None |
|
789 | 781 | |
|
790 | 782 | self.blocksize = None |
|
791 | 783 | |
|
792 | 784 | self.profileIndex = 0 |
|
793 | 785 | |
|
794 | 786 | self.nCohInt = 1 |
|
795 | 787 | |
|
796 | 788 | self.nIncohInt = 1 |
|
797 | 789 | |
|
798 | 790 | def getNormFactor(self): |
|
799 | 791 | pwcode = 1 |
|
800 | 792 | if self.flagDecodeData: |
|
801 | 793 | pwcode = numpy.sum(self.code[0]**2) |
|
802 | 794 | |
|
803 | 795 | normFactor = self.nIncohInt*self.nCohInt*pwcode |
|
804 | 796 | |
|
805 | 797 | return normFactor |
|
806 | 798 | |
|
807 | 799 | def getTimeInterval(self): |
|
808 | 800 | |
|
809 | 801 | timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt |
|
810 | 802 | |
|
811 | 803 | return timeInterval |
|
812 | 804 | |
|
813 | 805 | normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.") |
|
814 | 806 | timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
815 | 807 | |
|
816 | 808 | class Fits(JROData): |
|
817 | 809 | |
|
818 | 810 | heightList = None |
|
819 | 811 | |
|
820 | 812 | channelList = None |
|
821 | 813 | |
|
822 | 814 | flagNoData = True |
|
823 | 815 | |
|
824 | 816 | flagDiscontinuousBlock = False |
|
825 | 817 | |
|
826 | 818 | useLocalTime = False |
|
827 | 819 | |
|
828 | 820 | utctime = None |
|
829 | 821 | |
|
830 | 822 | timeZone = None |
|
831 | 823 | |
|
832 | 824 | # ippSeconds = None |
|
833 | 825 | |
|
834 | 826 | # timeInterval = None |
|
835 | 827 | |
|
836 | 828 | nCohInt = None |
|
837 | 829 | |
|
838 | 830 | nIncohInt = None |
|
839 | 831 | |
|
840 | 832 | noise = None |
|
841 | 833 | |
|
842 | 834 | windowOfFilter = 1 |
|
843 | 835 | |
|
844 | 836 | #Speed of ligth |
|
845 | 837 | C = 3e8 |
|
846 | 838 | |
|
847 | 839 | frequency = 49.92e6 |
|
848 | 840 | |
|
849 | 841 | realtime = False |
|
850 | 842 | |
|
851 | 843 | |
|
852 | 844 | def __init__(self): |
|
853 | 845 | |
|
854 | 846 | self.type = "Fits" |
|
855 | 847 | |
|
856 | 848 | self.nProfiles = None |
|
857 | 849 | |
|
858 | 850 | self.heightList = None |
|
859 | 851 | |
|
860 | 852 | self.channelList = None |
|
861 | 853 | |
|
862 | 854 | # self.channelIndexList = None |
|
863 | 855 | |
|
864 | 856 | self.flagNoData = True |
|
865 | 857 | |
|
866 | 858 | self.utctime = None |
|
867 | 859 | |
|
868 | 860 | self.nCohInt = 1 |
|
869 | 861 | |
|
870 | 862 | self.nIncohInt = 1 |
|
871 | 863 | |
|
872 | 864 | self.useLocalTime = True |
|
873 | 865 | |
|
874 | 866 | self.profileIndex = 0 |
|
875 | 867 | |
|
876 | 868 | # self.utctime = None |
|
877 | 869 | # self.timeZone = None |
|
878 | 870 | # self.ltctime = None |
|
879 | 871 | # self.timeInterval = None |
|
880 | 872 | # self.header = None |
|
881 | 873 | # self.data_header = None |
|
882 | 874 | # self.data = None |
|
883 | 875 | # self.datatime = None |
|
884 | 876 | # self.flagNoData = False |
|
885 | 877 | # self.expName = '' |
|
886 | 878 | # self.nChannels = None |
|
887 | 879 | # self.nSamples = None |
|
888 | 880 | # self.dataBlocksPerFile = None |
|
889 | 881 | # self.comments = '' |
|
890 | 882 | # |
|
891 | 883 | |
|
892 | 884 | |
|
893 | 885 | def getltctime(self): |
|
894 | 886 | |
|
895 | 887 | if self.useLocalTime: |
|
896 | 888 | return self.utctime - self.timeZone*60 |
|
897 | 889 | |
|
898 | 890 | return self.utctime |
|
899 | 891 | |
|
900 | 892 | def getDatatime(self): |
|
901 | 893 | |
|
902 | 894 | datatime = datetime.datetime.utcfromtimestamp(self.ltctime) |
|
903 | 895 | return datatime |
|
904 | 896 | |
|
905 | 897 | def getTimeRange(self): |
|
906 | 898 | |
|
907 | 899 | datatime = [] |
|
908 | 900 | |
|
909 | 901 | datatime.append(self.ltctime) |
|
910 | 902 | datatime.append(self.ltctime + self.timeInterval) |
|
911 | 903 | |
|
912 | 904 | datatime = numpy.array(datatime) |
|
913 | 905 | |
|
914 | 906 | return datatime |
|
915 | 907 | |
|
916 | 908 | def getHeiRange(self): |
|
917 | 909 | |
|
918 | 910 | heis = self.heightList |
|
919 | 911 | |
|
920 | 912 | return heis |
|
921 | 913 | |
|
922 | 914 | def getNHeights(self): |
|
923 | 915 | |
|
924 | 916 | return len(self.heightList) |
|
925 | 917 | |
|
926 | 918 | def getNChannels(self): |
|
927 | 919 | |
|
928 | 920 | return len(self.channelList) |
|
929 | 921 | |
|
930 | 922 | def getChannelIndexList(self): |
|
931 | 923 | |
|
932 | 924 | return range(self.nChannels) |
|
933 | 925 | |
|
934 | 926 | def getNoise(self, type = 1): |
|
935 | 927 | |
|
936 | 928 | #noise = numpy.zeros(self.nChannels) |
|
937 | 929 | |
|
938 | 930 | if type == 1: |
|
939 | 931 | noise = self.getNoisebyHildebrand() |
|
940 | 932 | |
|
941 | 933 | if type == 2: |
|
942 | 934 | noise = self.getNoisebySort() |
|
943 | 935 | |
|
944 | 936 | if type == 3: |
|
945 | 937 | noise = self.getNoisebyWindow() |
|
946 | 938 | |
|
947 | 939 | return noise |
|
948 | 940 | |
|
949 | 941 | def getTimeInterval(self): |
|
950 | 942 | |
|
951 | 943 | timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt |
|
952 | 944 | |
|
953 | 945 | return timeInterval |
|
954 | 946 | |
|
955 | 947 | datatime = property(getDatatime, "I'm the 'datatime' property") |
|
956 | 948 | nHeights = property(getNHeights, "I'm the 'nHeights' property.") |
|
957 | 949 | nChannels = property(getNChannels, "I'm the 'nChannel' property.") |
|
958 | 950 | channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.") |
|
959 | 951 | noise = property(getNoise, "I'm the 'nHeights' property.") |
|
960 | 952 | |
|
961 | 953 | ltctime = property(getltctime, "I'm the 'ltctime' property") |
|
962 | 954 | timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
963 | 955 | |
|
964 | 956 | |
|
965 | 957 | class Correlation(JROData): |
|
966 | 958 | |
|
967 | 959 | noise = None |
|
968 | 960 | |
|
969 | 961 | SNR = None |
|
970 | 962 | |
|
971 | 963 | #-------------------------------------------------- |
|
972 | 964 | |
|
973 | 965 | mode = None |
|
974 | 966 | |
|
975 | 967 | split = False |
|
976 | 968 | |
|
977 | 969 | data_cf = None |
|
978 | 970 | |
|
979 | 971 | lags = None |
|
980 | 972 | |
|
981 | 973 | lagRange = None |
|
982 | 974 | |
|
983 | 975 | pairsList = None |
|
984 | 976 | |
|
985 | 977 | normFactor = None |
|
986 | 978 | |
|
987 | 979 | #-------------------------------------------------- |
|
988 | 980 | |
|
989 | 981 | # calculateVelocity = None |
|
990 | 982 | |
|
991 | 983 | nLags = None |
|
992 | 984 | |
|
993 | 985 | nPairs = None |
|
994 | 986 | |
|
995 | 987 | nAvg = None |
|
996 | 988 | |
|
997 | 989 | |
|
998 | 990 | def __init__(self): |
|
999 | 991 | ''' |
|
1000 | 992 | Constructor |
|
1001 | 993 | ''' |
|
1002 | 994 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
1003 | 995 | |
|
1004 | 996 | self.systemHeaderObj = SystemHeader() |
|
1005 | 997 | |
|
1006 | 998 | self.type = "Correlation" |
|
1007 | 999 | |
|
1008 | 1000 | self.data = None |
|
1009 | 1001 | |
|
1010 | 1002 | self.dtype = None |
|
1011 | 1003 | |
|
1012 | 1004 | self.nProfiles = None |
|
1013 | 1005 | |
|
1014 | 1006 | self.heightList = None |
|
1015 | 1007 | |
|
1016 | 1008 | self.channelList = None |
|
1017 | 1009 | |
|
1018 | 1010 | self.flagNoData = True |
|
1019 | 1011 | |
|
1020 | 1012 | self.flagDiscontinuousBlock = False |
|
1021 | 1013 | |
|
1022 | 1014 | self.utctime = None |
|
1023 | 1015 | |
|
1024 | 1016 | self.timeZone = None |
|
1025 | 1017 | |
|
1026 | 1018 | self.dstFlag = None |
|
1027 | 1019 | |
|
1028 | 1020 | self.errorCount = None |
|
1029 | 1021 | |
|
1030 | 1022 | self.blocksize = None |
|
1031 | 1023 | |
|
1032 | 1024 | self.flagDecodeData = False #asumo q la data no esta decodificada |
|
1033 | 1025 | |
|
1034 | 1026 | self.flagDeflipData = False #asumo q la data no esta sin flip |
|
1035 | 1027 | |
|
1036 | 1028 | self.pairsList = None |
|
1037 | 1029 | |
|
1038 | 1030 | self.nPoints = None |
|
1039 | 1031 | |
|
1040 | 1032 | def getPairsList(self): |
|
1041 | 1033 | |
|
1042 | 1034 | return self.pairsList |
|
1043 | 1035 | |
|
1044 | 1036 | def getNoise(self, mode = 2): |
|
1045 | 1037 | |
|
1046 | 1038 | indR = numpy.where(self.lagR == 0)[0][0] |
|
1047 | 1039 | indT = numpy.where(self.lagT == 0)[0][0] |
|
1048 | 1040 | |
|
1049 | 1041 | jspectra0 = self.data_corr[:,:,indR,:] |
|
1050 | 1042 | jspectra = copy.copy(jspectra0) |
|
1051 | 1043 | |
|
1052 | 1044 | num_chan = jspectra.shape[0] |
|
1053 | 1045 | num_hei = jspectra.shape[2] |
|
1054 | 1046 | |
|
1055 | 1047 | freq_dc = jspectra.shape[1]/2 |
|
1056 | 1048 | ind_vel = numpy.array([-2,-1,1,2]) + freq_dc |
|
1057 | 1049 | |
|
1058 | 1050 | if ind_vel[0]<0: |
|
1059 | 1051 | ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof |
|
1060 | 1052 | |
|
1061 | 1053 | if mode == 1: |
|
1062 | 1054 | jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION |
|
1063 | 1055 | |
|
1064 | 1056 | if mode == 2: |
|
1065 | 1057 | |
|
1066 | 1058 | vel = numpy.array([-2,-1,1,2]) |
|
1067 | 1059 | xx = numpy.zeros([4,4]) |
|
1068 | 1060 | |
|
1069 | 1061 | for fil in range(4): |
|
1070 | 1062 | xx[fil,:] = vel[fil]**numpy.asarray(range(4)) |
|
1071 | 1063 | |
|
1072 | 1064 | xx_inv = numpy.linalg.inv(xx) |
|
1073 | 1065 | xx_aux = xx_inv[0,:] |
|
1074 | 1066 | |
|
1075 | 1067 | for ich in range(num_chan): |
|
1076 | 1068 | yy = jspectra[ich,ind_vel,:] |
|
1077 | 1069 | jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy) |
|
1078 | 1070 | |
|
1079 | 1071 | junkid = jspectra[ich,freq_dc,:]<=0 |
|
1080 | 1072 | cjunkid = sum(junkid) |
|
1081 | 1073 | |
|
1082 | 1074 | if cjunkid.any(): |
|
1083 | 1075 | jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2 |
|
1084 | 1076 | |
|
1085 | 1077 | noise = jspectra0[:,freq_dc,:] - jspectra[:,freq_dc,:] |
|
1086 | 1078 | |
|
1087 | 1079 | return noise |
|
1088 | 1080 | |
|
1089 | 1081 | def getTimeInterval(self): |
|
1090 | 1082 | |
|
1091 | 1083 | timeInterval = self.ippSeconds * self.nCohInt * self.nProfiles |
|
1092 | 1084 | |
|
1093 | 1085 | return timeInterval |
|
1094 | 1086 | |
|
1095 | 1087 | def splitFunctions(self): |
|
1096 | 1088 | |
|
1097 | 1089 | pairsList = self.pairsList |
|
1098 | 1090 | ccf_pairs = [] |
|
1099 | 1091 | acf_pairs = [] |
|
1100 | 1092 | ccf_ind = [] |
|
1101 | 1093 | acf_ind = [] |
|
1102 | 1094 | for l in range(len(pairsList)): |
|
1103 | 1095 | chan0 = pairsList[l][0] |
|
1104 | 1096 | chan1 = pairsList[l][1] |
|
1105 | 1097 | |
|
1106 | 1098 | #Obteniendo pares de Autocorrelacion |
|
1107 | 1099 | if chan0 == chan1: |
|
1108 | 1100 | acf_pairs.append(chan0) |
|
1109 | 1101 | acf_ind.append(l) |
|
1110 | 1102 | else: |
|
1111 | 1103 | ccf_pairs.append(pairsList[l]) |
|
1112 | 1104 | ccf_ind.append(l) |
|
1113 | 1105 | |
|
1114 | 1106 | data_acf = self.data_cf[acf_ind] |
|
1115 | 1107 | data_ccf = self.data_cf[ccf_ind] |
|
1116 | 1108 | |
|
1117 | 1109 | return acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf |
|
1118 | 1110 | |
|
1119 | 1111 | def getNormFactor(self): |
|
1120 | 1112 | acf_ind, ccf_ind, acf_pairs, ccf_pairs, data_acf, data_ccf = self.splitFunctions() |
|
1121 | 1113 | acf_pairs = numpy.array(acf_pairs) |
|
1122 | 1114 | normFactor = numpy.zeros((self.nPairs,self.nHeights)) |
|
1123 | 1115 | |
|
1124 | 1116 | for p in range(self.nPairs): |
|
1125 | 1117 | pair = self.pairsList[p] |
|
1126 | 1118 | |
|
1127 | 1119 | ch0 = pair[0] |
|
1128 | 1120 | ch1 = pair[1] |
|
1129 | 1121 | |
|
1130 | 1122 | ch0_max = numpy.max(data_acf[acf_pairs==ch0,:,:], axis=1) |
|
1131 | 1123 | ch1_max = numpy.max(data_acf[acf_pairs==ch1,:,:], axis=1) |
|
1132 | 1124 | normFactor[p,:] = numpy.sqrt(ch0_max*ch1_max) |
|
1133 | 1125 | |
|
1134 | 1126 | return normFactor |
|
1135 | 1127 | |
|
1136 | 1128 | timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property") |
|
1137 | 1129 | normFactor = property(getNormFactor, "I'm the 'normFactor property'") |
|
1138 | 1130 | |
|
1139 | 1131 | class Parameters(Spectra): |
|
1140 | 1132 | |
|
1141 | 1133 | experimentInfo = None #Information about the experiment |
|
1142 | 1134 | |
|
1143 | 1135 | #Information from previous data |
|
1144 | 1136 | |
|
1145 | 1137 | inputUnit = None #Type of data to be processed |
|
1146 | 1138 | |
|
1147 | 1139 | operation = None #Type of operation to parametrize |
|
1148 | 1140 | |
|
1149 | 1141 | #normFactor = None #Normalization Factor |
|
1150 | 1142 | |
|
1151 | 1143 | groupList = None #List of Pairs, Groups, etc |
|
1152 | 1144 | |
|
1153 | 1145 | #Parameters |
|
1154 | 1146 | |
|
1155 | 1147 | data_param = None #Parameters obtained |
|
1156 | 1148 | |
|
1157 | 1149 | data_pre = None #Data Pre Parametrization |
|
1158 | 1150 | |
|
1159 | 1151 | data_SNR = None #Signal to Noise Ratio |
|
1160 | 1152 | |
|
1161 | 1153 | # heightRange = None #Heights |
|
1162 | 1154 | |
|
1163 | 1155 | abscissaList = None #Abscissa, can be velocities, lags or time |
|
1164 | 1156 | |
|
1165 | 1157 | # noise = None #Noise Potency |
|
1166 | 1158 | |
|
1167 | 1159 | utctimeInit = None #Initial UTC time |
|
1168 | 1160 | |
|
1169 | 1161 | paramInterval = None #Time interval to calculate Parameters in seconds |
|
1170 | 1162 | |
|
1171 | 1163 | useLocalTime = True |
|
1172 | 1164 | |
|
1173 | 1165 | #Fitting |
|
1174 | 1166 | |
|
1175 | 1167 | data_error = None #Error of the estimation |
|
1176 | 1168 | |
|
1177 | 1169 | constants = None |
|
1178 | 1170 | |
|
1179 | 1171 | library = None |
|
1180 | 1172 | |
|
1181 | 1173 | #Output signal |
|
1182 | 1174 | |
|
1183 | 1175 | outputInterval = None #Time interval to calculate output signal in seconds |
|
1184 | 1176 | |
|
1185 | 1177 | data_output = None #Out signal |
|
1186 | 1178 | |
|
1187 | 1179 | nAvg = None |
|
1188 | 1180 | |
|
1189 | 1181 | noise_estimation = None |
|
1190 | 1182 | |
|
1191 | 1183 | |
|
1192 | 1184 | def __init__(self): |
|
1193 | 1185 | ''' |
|
1194 | 1186 | Constructor |
|
1195 | 1187 | ''' |
|
1196 | 1188 | self.radarControllerHeaderObj = RadarControllerHeader() |
|
1197 | 1189 | |
|
1198 | 1190 | self.systemHeaderObj = SystemHeader() |
|
1199 | 1191 | |
|
1200 | 1192 | self.type = "Parameters" |
|
1201 | 1193 | |
|
1202 | 1194 | def getTimeRange1(self, interval): |
|
1203 | 1195 | |
|
1204 | 1196 | datatime = [] |
|
1205 | 1197 | |
|
1206 | 1198 | if self.useLocalTime: |
|
1207 | 1199 | time1 = self.utctimeInit - self.timeZone*60 |
|
1208 | 1200 | else: |
|
1209 | 1201 | time1 = self.utctimeInit |
|
1210 | 1202 | |
|
1211 | 1203 | datatime.append(time1) |
|
1212 | 1204 | datatime.append(time1 + interval) |
|
1213 | 1205 | datatime = numpy.array(datatime) |
|
1214 | 1206 | |
|
1215 | 1207 | return datatime |
|
1216 | 1208 | |
|
1217 | 1209 | def getTimeInterval(self): |
|
1218 | 1210 | |
|
1219 | 1211 | if hasattr(self, 'timeInterval1'): |
|
1220 | 1212 | return self.timeInterval1 |
|
1221 | 1213 | else: |
|
1222 | 1214 | return self.paramInterval |
|
1223 | 1215 | |
|
1224 | 1216 | def getNoise(self): |
|
1225 | 1217 | |
|
1226 | 1218 | return self.spc_noise |
|
1227 | 1219 | |
|
1228 | 1220 | timeInterval = property(getTimeInterval) |
@@ -1,464 +1,502 | |||
|
1 | 1 | ''' |
|
2 | 2 | @author: Juan C. Espinoza |
|
3 | 3 | ''' |
|
4 | 4 | |
|
5 | 5 | import time |
|
6 | 6 | import json |
|
7 | 7 | import numpy |
|
8 | 8 | import paho.mqtt.client as mqtt |
|
9 | 9 | import zmq |
|
10 | 10 | from profilehooks import profile |
|
11 | 11 | import datetime |
|
12 | 12 | from zmq.utils.monitor import recv_monitor_message |
|
13 | 13 | from functools import wraps |
|
14 | 14 | from threading import Thread |
|
15 | 15 | from multiprocessing import Process |
|
16 | 16 | |
|
17 | 17 | from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit |
|
18 | from schainpy.model.data.jrodata import JROData | |
|
18 | 19 | |
|
19 | 20 | MAXNUMX = 100 |
|
20 | 21 | MAXNUMY = 100 |
|
21 | 22 | |
|
22 | 23 | class PrettyFloat(float): |
|
23 | 24 | def __repr__(self): |
|
24 | 25 | return '%.2f' % self |
|
25 | 26 | |
|
26 | 27 | def roundFloats(obj): |
|
27 | 28 | if isinstance(obj, list): |
|
28 | 29 | return map(roundFloats, obj) |
|
29 | 30 | elif isinstance(obj, float): |
|
30 | 31 | return round(obj, 2) |
|
31 | 32 | |
|
32 | 33 | def decimate(z, MAXNUMY): |
|
33 | 34 | # dx = int(len(self.x)/self.__MAXNUMX) + 1 |
|
34 | 35 | |
|
35 | 36 | dy = int(len(z[0])/MAXNUMY) + 1 |
|
36 | 37 | |
|
37 | 38 | return z[::, ::dy] |
|
38 | 39 | |
|
39 | 40 | class throttle(object): |
|
40 | 41 | """Decorator that prevents a function from being called more than once every |
|
41 | 42 | time period. |
|
42 | 43 | To create a function that cannot be called more than once a minute, but |
|
43 | 44 | will sleep until it can be called: |
|
44 | 45 | @throttle(minutes=1) |
|
45 | 46 | def foo(): |
|
46 | 47 | pass |
|
47 | 48 | |
|
48 | 49 | for i in range(10): |
|
49 | 50 | foo() |
|
50 | 51 | print "This function has run %s times." % i |
|
51 | 52 | """ |
|
52 | 53 | |
|
53 | 54 | def __init__(self, seconds=0, minutes=0, hours=0): |
|
54 | 55 | self.throttle_period = datetime.timedelta( |
|
55 | 56 | seconds=seconds, minutes=minutes, hours=hours |
|
56 | 57 | ) |
|
57 | 58 | |
|
58 | 59 | self.time_of_last_call = datetime.datetime.min |
|
59 | 60 | |
|
60 | 61 | def __call__(self, fn): |
|
61 | 62 | @wraps(fn) |
|
62 | 63 | def wrapper(*args, **kwargs): |
|
63 | 64 | now = datetime.datetime.now() |
|
64 | 65 | time_since_last_call = now - self.time_of_last_call |
|
65 | 66 | time_left = self.throttle_period - time_since_last_call |
|
66 | 67 | |
|
67 | 68 | if time_left > datetime.timedelta(seconds=0): |
|
68 | 69 | return |
|
69 | 70 | |
|
70 | 71 | self.time_of_last_call = datetime.datetime.now() |
|
71 | 72 | return fn(*args, **kwargs) |
|
72 | 73 | |
|
73 | 74 | return wrapper |
|
74 | 75 | |
|
75 | 76 | |
|
76 | 77 | class PublishData(Operation): |
|
77 | 78 | """Clase publish.""" |
|
78 | 79 | |
|
79 | 80 | def __init__(self, **kwargs): |
|
80 | 81 | """Inicio.""" |
|
81 | 82 | Operation.__init__(self, **kwargs) |
|
82 | 83 | self.isConfig = False |
|
83 | 84 | self.client = None |
|
84 | 85 | self.zeromq = None |
|
85 | 86 | self.mqtt = None |
|
86 | 87 | |
|
87 | 88 | def on_disconnect(self, client, userdata, rc): |
|
88 | 89 | if rc != 0: |
|
89 | 90 | print("Unexpected disconnection.") |
|
90 | 91 | self.connect() |
|
91 | 92 | |
|
92 | 93 | def connect(self): |
|
93 | 94 | print 'trying to connect' |
|
94 | 95 | try: |
|
95 | 96 | self.client.connect( |
|
96 | 97 | host=self.host, |
|
97 | 98 | port=self.port, |
|
98 | 99 | keepalive=60*10, |
|
99 | 100 | bind_address='') |
|
100 | 101 | self.client.loop_start() |
|
101 | 102 | # self.client.publish( |
|
102 | 103 | # self.topic + 'SETUP', |
|
103 | 104 | # json.dumps(setup), |
|
104 | 105 | # retain=True |
|
105 | 106 | # ) |
|
106 | 107 | except: |
|
107 | 108 | print "MQTT Conection error." |
|
108 | 109 | self.client = False |
|
109 | 110 | |
|
110 | 111 | def setup(self, port=1883, username=None, password=None, clientId="user", zeromq=1, verbose=True, **kwargs): |
|
111 | 112 | self.counter = 0 |
|
112 | 113 | self.topic = kwargs.get('topic', 'schain') |
|
113 | 114 | self.delay = kwargs.get('delay', 0) |
|
114 | 115 | self.plottype = kwargs.get('plottype', 'spectra') |
|
115 | 116 | self.host = kwargs.get('host', "10.10.10.82") |
|
116 | 117 | self.port = kwargs.get('port', 3000) |
|
117 | 118 | self.clientId = clientId |
|
118 | 119 | self.cnt = 0 |
|
119 | 120 | self.zeromq = zeromq |
|
120 | 121 | self.mqtt = kwargs.get('plottype', 0) |
|
121 | 122 | self.client = None |
|
122 | 123 | self.verbose = verbose |
|
123 | 124 | self.dataOut.firstdata = True |
|
124 | 125 | setup = [] |
|
125 | 126 | if mqtt is 1: |
|
126 | 127 | self.client = mqtt.Client( |
|
127 | 128 | client_id=self.clientId + self.topic + 'SCHAIN', |
|
128 | 129 | clean_session=True) |
|
129 | 130 | self.client.on_disconnect = self.on_disconnect |
|
130 | 131 | self.connect() |
|
131 | 132 | for plot in self.plottype: |
|
132 | 133 | setup.append({ |
|
133 | 134 | 'plot': plot, |
|
134 | 135 | 'topic': self.topic + plot, |
|
135 | 136 | 'title': getattr(self, plot + '_' + 'title', False), |
|
136 | 137 | 'xlabel': getattr(self, plot + '_' + 'xlabel', False), |
|
137 | 138 | 'ylabel': getattr(self, plot + '_' + 'ylabel', False), |
|
138 | 139 | 'xrange': getattr(self, plot + '_' + 'xrange', False), |
|
139 | 140 | 'yrange': getattr(self, plot + '_' + 'yrange', False), |
|
140 | 141 | 'zrange': getattr(self, plot + '_' + 'zrange', False), |
|
141 | 142 | }) |
|
142 | 143 | if zeromq is 1: |
|
143 | 144 | context = zmq.Context() |
|
144 | 145 | self.zmq_socket = context.socket(zmq.PUSH) |
|
145 | 146 | server = kwargs.get('server', 'zmq.pipe') |
|
146 | 147 | |
|
147 | 148 | if 'tcp://' in server: |
|
148 | 149 | address = server |
|
149 | 150 | else: |
|
150 | 151 | address = 'ipc:///tmp/%s' % server |
|
151 | 152 | |
|
152 | 153 | self.zmq_socket.connect(address) |
|
153 | 154 | time.sleep(1) |
|
154 | 155 | |
|
155 | 156 | |
|
156 | 157 | def publish_data(self): |
|
157 | 158 | self.dataOut.finished = False |
|
158 | 159 | if self.mqtt is 1: |
|
159 | 160 | yData = self.dataOut.heightList[:2].tolist() |
|
160 | 161 | if self.plottype == 'spectra': |
|
161 | 162 | data = getattr(self.dataOut, 'data_spc') |
|
162 | 163 | z = data/self.dataOut.normFactor |
|
163 | 164 | zdB = 10*numpy.log10(z) |
|
164 | 165 | xlen, ylen = zdB[0].shape |
|
165 | 166 | dx = int(xlen/MAXNUMX) + 1 |
|
166 | 167 | dy = int(ylen/MAXNUMY) + 1 |
|
167 | 168 | Z = [0 for i in self.dataOut.channelList] |
|
168 | 169 | for i in self.dataOut.channelList: |
|
169 | 170 | Z[i] = zdB[i][::dx, ::dy].tolist() |
|
170 | 171 | payload = { |
|
171 | 172 | 'timestamp': self.dataOut.utctime, |
|
172 | 173 | 'data': roundFloats(Z), |
|
173 | 174 | 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList], |
|
174 | 175 | 'interval': self.dataOut.getTimeInterval(), |
|
175 | 176 | 'type': self.plottype, |
|
176 | 177 | 'yData': yData |
|
177 | 178 | } |
|
178 | 179 | # print payload |
|
179 | 180 | |
|
180 | 181 | elif self.plottype in ('rti', 'power'): |
|
181 | 182 | data = getattr(self.dataOut, 'data_spc') |
|
182 | 183 | z = data/self.dataOut.normFactor |
|
183 | 184 | avg = numpy.average(z, axis=1) |
|
184 | 185 | avgdB = 10*numpy.log10(avg) |
|
185 | 186 | xlen, ylen = z[0].shape |
|
186 | 187 | dy = numpy.floor(ylen/self.__MAXNUMY) + 1 |
|
187 | 188 | AVG = [0 for i in self.dataOut.channelList] |
|
188 | 189 | for i in self.dataOut.channelList: |
|
189 | 190 | AVG[i] = avgdB[i][::dy].tolist() |
|
190 | 191 | payload = { |
|
191 | 192 | 'timestamp': self.dataOut.utctime, |
|
192 | 193 | 'data': roundFloats(AVG), |
|
193 | 194 | 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList], |
|
194 | 195 | 'interval': self.dataOut.getTimeInterval(), |
|
195 | 196 | 'type': self.plottype, |
|
196 | 197 | 'yData': yData |
|
197 | 198 | } |
|
198 | 199 | elif self.plottype == 'noise': |
|
199 | 200 | noise = self.dataOut.getNoise()/self.dataOut.normFactor |
|
200 | 201 | noisedB = 10*numpy.log10(noise) |
|
201 | 202 | payload = { |
|
202 | 203 | 'timestamp': self.dataOut.utctime, |
|
203 | 204 | 'data': roundFloats(noisedB.reshape(-1, 1).tolist()), |
|
204 | 205 | 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList], |
|
205 | 206 | 'interval': self.dataOut.getTimeInterval(), |
|
206 | 207 | 'type': self.plottype, |
|
207 | 208 | 'yData': yData |
|
208 | 209 | } |
|
209 | 210 | elif self.plottype == 'snr': |
|
210 | 211 | data = getattr(self.dataOut, 'data_SNR') |
|
211 | 212 | avgdB = 10*numpy.log10(data) |
|
212 | 213 | |
|
213 | 214 | ylen = data[0].size |
|
214 | 215 | dy = numpy.floor(ylen/self.__MAXNUMY) + 1 |
|
215 | 216 | AVG = [0 for i in self.dataOut.channelList] |
|
216 | 217 | for i in self.dataOut.channelList: |
|
217 | 218 | AVG[i] = avgdB[i][::dy].tolist() |
|
218 | 219 | payload = { |
|
219 | 220 | 'timestamp': self.dataOut.utctime, |
|
220 | 221 | 'data': roundFloats(AVG), |
|
221 | 222 | 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList], |
|
222 | 223 | 'type': self.plottype, |
|
223 | 224 | 'yData': yData |
|
224 | 225 | } |
|
225 | 226 | else: |
|
226 | 227 | print "Tipo de grafico invalido" |
|
227 | 228 | payload = { |
|
228 | 229 | 'data': 'None', |
|
229 | 230 | 'timestamp': 'None', |
|
230 | 231 | 'type': None |
|
231 | 232 | } |
|
232 | 233 | # print 'Publishing data to {}'.format(self.host) |
|
233 | 234 | self.client.publish(self.topic + self.plottype, json.dumps(payload), qos=0) |
|
234 | 235 | |
|
235 | 236 | if self.zeromq is 1: |
|
236 | 237 | if self.verbose: |
|
237 | 238 | print '[Sending] {} - {}'.format(self.dataOut.type, self.dataOut.datatime) |
|
238 | 239 | self.zmq_socket.send_pyobj(self.dataOut) |
|
239 | 240 | self.dataOut.firstdata = False |
|
240 | 241 | |
|
241 | 242 | |
|
242 | 243 | def run(self, dataOut, **kwargs): |
|
243 | 244 | self.dataOut = dataOut |
|
244 | 245 | if not self.isConfig: |
|
245 | 246 | self.setup(**kwargs) |
|
246 | 247 | self.isConfig = True |
|
247 | 248 | |
|
248 | 249 | self.publish_data() |
|
249 | 250 | time.sleep(self.delay) |
|
250 | 251 | |
|
251 | 252 | def close(self): |
|
252 | 253 | if self.zeromq is 1: |
|
253 | 254 | self.dataOut.finished = True |
|
254 | 255 | self.zmq_socket.send_pyobj(self.dataOut) |
|
255 | 256 | self.zmq_socket.close() |
|
256 | 257 | if self.client: |
|
257 | 258 | self.client.loop_stop() |
|
258 | 259 | self.client.disconnect() |
|
259 | 260 | |
|
260 | class ReceiverData(ProcessingUnit, Process): | |
|
261 | ||
|
262 | class ReceiverData(ProcessingUnit): | |
|
263 | ||
|
264 | def __init__(self, **kwargs): | |
|
265 | ||
|
266 | ProcessingUnit.__init__(self, **kwargs) | |
|
267 | ||
|
268 | self.isConfig = False | |
|
269 | server = kwargs.get('server', 'zmq.pipe') | |
|
270 | if 'tcp://' in server: | |
|
271 | address = server | |
|
272 | else: | |
|
273 | address = 'ipc:///tmp/%s' % server | |
|
274 | ||
|
275 | self.address = address | |
|
276 | self.dataOut = JROData() | |
|
277 | ||
|
278 | def setup(self): | |
|
279 | ||
|
280 | self.context = zmq.Context() | |
|
281 | self.receiver = self.context.socket(zmq.PULL) | |
|
282 | self.receiver.bind(self.address) | |
|
283 | time.sleep(0.5) | |
|
284 | print '[Starting] ReceiverData from {}'.format(self.address) | |
|
285 | ||
|
286 | ||
|
287 | def run(self): | |
|
288 | ||
|
289 | if not self.isConfig: | |
|
290 | self.setup() | |
|
291 | self.isConfig = True | |
|
292 | ||
|
293 | self.dataOut = self.receiver.recv_pyobj() | |
|
294 | print '[Receiving] {} - {}'.format(self.dataOut.type, | |
|
295 | self.dataOut.datatime.ctime()) | |
|
296 | ||
|
297 | ||
|
298 | class PlotterReceiver(ProcessingUnit, Process): | |
|
261 | 299 | |
|
262 | 300 | throttle_value = 5 |
|
263 | 301 | |
|
264 | 302 | def __init__(self, **kwargs): |
|
265 | 303 | |
|
266 | 304 | ProcessingUnit.__init__(self, **kwargs) |
|
267 | 305 | Process.__init__(self) |
|
268 | 306 | self.mp = False |
|
269 | 307 | self.isConfig = False |
|
270 | 308 | self.isWebConfig = False |
|
271 | self.plottypes =[] | |
|
309 | self.plottypes = [] | |
|
272 | 310 | self.connections = 0 |
|
273 | 311 | server = kwargs.get('server', 'zmq.pipe') |
|
274 | 312 | plot_server = kwargs.get('plot_server', 'zmq.web') |
|
275 | 313 | if 'tcp://' in server: |
|
276 | 314 | address = server |
|
277 | 315 | else: |
|
278 | 316 | address = 'ipc:///tmp/%s' % server |
|
279 | 317 | |
|
280 | 318 | if 'tcp://' in plot_server: |
|
281 | 319 | plot_address = plot_server |
|
282 | 320 | else: |
|
283 | 321 | plot_address = 'ipc:///tmp/%s' % plot_server |
|
284 | 322 | |
|
285 | 323 | self.address = address |
|
286 | 324 | self.plot_address = plot_address |
|
287 | 325 | self.plottypes = [s.strip() for s in kwargs.get('plottypes', 'rti').split(',')] |
|
288 | 326 | self.realtime = kwargs.get('realtime', False) |
|
289 | 327 | self.throttle_value = kwargs.get('throttle', 5) |
|
290 | 328 | self.sendData = self.initThrottle(self.throttle_value) |
|
291 | 329 | self.setup() |
|
292 | 330 | |
|
293 | 331 | def setup(self): |
|
294 | 332 | |
|
295 | 333 | self.data = {} |
|
296 | 334 | self.data['times'] = [] |
|
297 | 335 | for plottype in self.plottypes: |
|
298 | 336 | self.data[plottype] = {} |
|
299 | 337 | self.data['noise'] = {} |
|
300 | 338 | self.data['throttle'] = self.throttle_value |
|
301 | 339 | self.data['ENDED'] = False |
|
302 | 340 | self.isConfig = True |
|
303 | 341 | self.data_web = {} |
|
304 | 342 | |
|
305 | 343 | def event_monitor(self, monitor): |
|
306 | 344 | |
|
307 | 345 | events = {} |
|
308 | 346 | |
|
309 | 347 | for name in dir(zmq): |
|
310 | 348 | if name.startswith('EVENT_'): |
|
311 | 349 | value = getattr(zmq, name) |
|
312 | 350 | events[value] = name |
|
313 | 351 | |
|
314 | 352 | while monitor.poll(): |
|
315 | 353 | evt = recv_monitor_message(monitor) |
|
316 | 354 | if evt['event'] == 32: |
|
317 | 355 | self.connections += 1 |
|
318 | 356 | if evt['event'] == 512: |
|
319 | 357 | pass |
|
320 | 358 | if self.connections == 0 and self.started is True: |
|
321 | 359 | self.ended = True |
|
322 | 360 | |
|
323 | 361 | evt.update({'description': events[evt['event']]}) |
|
324 | 362 | |
|
325 | 363 | if evt['event'] == zmq.EVENT_MONITOR_STOPPED: |
|
326 | 364 | break |
|
327 | 365 | monitor.close() |
|
328 | 366 | print("event monitor thread done!") |
|
329 | 367 | |
|
330 | 368 | def initThrottle(self, throttle_value): |
|
331 | 369 | |
|
332 | 370 | @throttle(seconds=throttle_value) |
|
333 | 371 | def sendDataThrottled(fn_sender, data): |
|
334 | 372 | fn_sender(data) |
|
335 | 373 | |
|
336 | 374 | return sendDataThrottled |
|
337 | 375 | |
|
338 | 376 | |
|
339 | 377 | def send(self, data): |
|
340 | 378 | # print '[sending] data=%s size=%s' % (data.keys(), len(data['times'])) |
|
341 | 379 | self.sender.send_pyobj(data) |
|
342 | 380 | |
|
343 | 381 | |
|
344 | 382 | def update(self): |
|
345 | 383 | t = self.dataOut.utctime |
|
346 | 384 | |
|
347 | 385 | if t in self.data['times']: |
|
348 | 386 | return |
|
349 | 387 | |
|
350 | 388 | self.data['times'].append(t) |
|
351 | 389 | self.data['dataOut'] = self.dataOut |
|
352 | 390 | |
|
353 | 391 | for plottype in self.plottypes: |
|
354 | 392 | if plottype == 'spc': |
|
355 | 393 | z = self.dataOut.data_spc/self.dataOut.normFactor |
|
356 | 394 | self.data[plottype] = 10*numpy.log10(z) |
|
357 | 395 | self.data['noise'][t] = 10*numpy.log10(self.dataOut.getNoise()/self.dataOut.normFactor) |
|
358 | 396 | if plottype == 'cspc': |
|
359 | 397 | jcoherence = self.dataOut.data_cspc/numpy.sqrt(self.dataOut.data_spc*self.dataOut.data_spc) |
|
360 | 398 | self.data['cspc_coh'] = numpy.abs(jcoherence) |
|
361 | 399 | self.data['cspc_phase'] = numpy.arctan2(jcoherence.imag, jcoherence.real)*180/numpy.pi |
|
362 | 400 | if plottype == 'rti': |
|
363 | 401 | self.data[plottype][t] = self.dataOut.getPower() |
|
364 | 402 | if plottype == 'snr': |
|
365 | 403 | self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_SNR) |
|
366 | 404 | if plottype == 'dop': |
|
367 | 405 | self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_DOP) |
|
368 | 406 | if plottype == 'mean': |
|
369 | 407 | self.data[plottype][t] = self.dataOut.data_MEAN |
|
370 | 408 | if plottype == 'std': |
|
371 | 409 | self.data[plottype][t] = self.dataOut.data_STD |
|
372 | 410 | if plottype == 'coh': |
|
373 | 411 | self.data[plottype][t] = self.dataOut.getCoherence() |
|
374 | 412 | if plottype == 'phase': |
|
375 | 413 | self.data[plottype][t] = self.dataOut.getCoherence(phase=True) |
|
376 | 414 | if plottype == 'output': |
|
377 | 415 | self.data[plottype][t] = self.dataOut.data_output |
|
378 | 416 | if plottype == 'param': |
|
379 | 417 | self.data[plottype][t] = self.dataOut.data_param |
|
380 | 418 | if self.realtime: |
|
381 | 419 | self.data_web['timestamp'] = t |
|
382 | 420 | if plottype == 'spc': |
|
383 | 421 | self.data_web[plottype] = roundFloats(decimate(self.data[plottype]).tolist()) |
|
384 | 422 | elif plottype == 'cspc': |
|
385 | 423 | self.data_web['cspc_coh'] = roundFloats(decimate(self.data['cspc_coh']).tolist()) |
|
386 | 424 | self.data_web['cspc_phase'] = roundFloats(decimate(self.data['cspc_phase']).tolist()) |
|
387 | 425 | elif plottype == 'noise': |
|
388 | 426 | self.data_web['noise'] = roundFloats(self.data['noise'][t].tolist()) |
|
389 | 427 | else: |
|
390 | 428 | self.data_web[plottype] = roundFloats(decimate(self.data[plottype][t]).tolist()) |
|
391 | 429 | self.data_web['interval'] = self.dataOut.getTimeInterval() |
|
392 | 430 | self.data_web['type'] = plottype |
|
393 | 431 | |
|
394 | 432 | def run(self): |
|
395 | 433 | |
|
396 | 434 | print '[Starting] {} from {}'.format(self.name, self.address) |
|
397 | 435 | |
|
398 | 436 | self.context = zmq.Context() |
|
399 | 437 | self.receiver = self.context.socket(zmq.PULL) |
|
400 | 438 | self.receiver.bind(self.address) |
|
401 | 439 | monitor = self.receiver.get_monitor_socket() |
|
402 | 440 | self.sender = self.context.socket(zmq.PUB) |
|
403 | 441 | if self.realtime: |
|
404 | 442 | self.sender_web = self.context.socket(zmq.PUB) |
|
405 | 443 | self.sender_web.connect(self.plot_address) |
|
406 | 444 | time.sleep(1) |
|
407 | 445 | |
|
408 | 446 | if 'server' in self.kwargs: |
|
409 | 447 | self.sender.bind("ipc:///tmp/{}.plots".format(self.kwargs['server'])) |
|
410 | 448 | else: |
|
411 | 449 | self.sender.bind("ipc:///tmp/zmq.plots") |
|
412 | 450 | |
|
413 | 451 | time.sleep(3) |
|
414 | 452 | |
|
415 | 453 | t = Thread(target=self.event_monitor, args=(monitor,)) |
|
416 | 454 | t.start() |
|
417 | 455 | |
|
418 | 456 | while True: |
|
419 | 457 | self.dataOut = self.receiver.recv_pyobj() |
|
420 | 458 | # print '[Receiving] {} - {}'.format(self.dataOut.type, |
|
421 | 459 | # self.dataOut.datatime.ctime()) |
|
422 | 460 | |
|
423 | 461 | self.update() |
|
424 | 462 | |
|
425 | 463 | if self.dataOut.firstdata is True: |
|
426 | 464 | self.data['STARTED'] = True |
|
427 | 465 | |
|
428 | 466 | if self.dataOut.finished is True: |
|
429 | 467 | self.send(self.data) |
|
430 | 468 | self.connections -= 1 |
|
431 | 469 | if self.connections == 0 and self.started: |
|
432 | 470 | self.ended = True |
|
433 | 471 | self.data['ENDED'] = True |
|
434 | 472 | self.send(self.data) |
|
435 | 473 | self.setup() |
|
436 | 474 | self.started = False |
|
437 | 475 | else: |
|
438 | 476 | if self.realtime: |
|
439 | 477 | self.send(self.data) |
|
440 | 478 | self.sender_web.send_string(json.dumps(self.data_web)) |
|
441 | 479 | else: |
|
442 | 480 | self.sendData(self.send, self.data) |
|
443 | 481 | self.started = True |
|
444 | 482 | |
|
445 | 483 | self.data['STARTED'] = False |
|
446 | 484 | return |
|
447 | 485 | |
|
448 | 486 | def sendToWeb(self): |
|
449 | 487 | |
|
450 | 488 | if not self.isWebConfig: |
|
451 | 489 | context = zmq.Context() |
|
452 | 490 | sender_web_config = context.socket(zmq.PUB) |
|
453 | 491 | if 'tcp://' in self.plot_address: |
|
454 | 492 | dum, address, port = self.plot_address.split(':') |
|
455 | 493 | conf_address = '{}:{}:{}'.format(dum, address, int(port)+1) |
|
456 | 494 | else: |
|
457 | 495 | conf_address = self.plot_address + '.config' |
|
458 | 496 | sender_web_config.bind(conf_address) |
|
459 | 497 | time.sleep(1) |
|
460 | 498 | for kwargs in self.operationKwargs.values(): |
|
461 | 499 | if 'plot' in kwargs: |
|
462 | 500 | print '[Sending] Config data to web for {}'.format(kwargs['code'].upper()) |
|
463 | 501 | sender_web_config.send_string(json.dumps(kwargs)) |
|
464 | 502 | self.isWebConfig = True |
General Comments 0
You need to be logged in to leave comments.
Login now