##// END OF EJS Templates
Verificacion de la existencia de datos cspc y dc al realizar la integracion incoherente.
Miguel Valdez -
r222:6159533d3812
parent child
Show More
@@ -1,698 +1,710
1 1 '''
2 2 Created on September , 2012
3 3 @author:
4 4 '''
5 5 from xml.etree.ElementTree import Element, SubElement, ElementTree
6 6 from xml.etree import ElementTree as ET
7 7 from xml.dom import minidom
8 8
9 9 import sys
10 10 import datetime
11 11 from model.jrodataIO import *
12 12 from model.jroprocessing import *
13 13 from model.jroplot import *
14 14
15 15 def prettify(elem):
16 16 """Return a pretty-printed XML string for the Element.
17 17 """
18 18 rough_string = ET.tostring(elem, 'utf-8')
19 19 reparsed = minidom.parseString(rough_string)
20 20 return reparsed.toprettyxml(indent=" ")
21 21
22 22 class ParameterConf():
23 23
24 24 id = None
25 25 name = None
26 26 value = None
27 27 format = None
28 28
29 29 ELEMENTNAME = 'Parameter'
30 30
31 31 def __init__(self):
32 32
33 33 self.format = 'str'
34 34
35 35 def getElementName(self):
36 36
37 37 return self.ELEMENTNAME
38 38
39 39 def getValue(self):
40 40
41 41 if self.format == 'list':
42 42 strList = self.value.split(',')
43 43 return strList
44 44
45 45 if self.format == 'intlist':
46 46 strList = self.value.split(',')
47 47 intList = [int(x) for x in strList]
48 48 return intList
49 49
50 50 if self.format == 'floatlist':
51 51 strList = self.value.split(',')
52 52 floatList = [float(x) for x in strList]
53 53 return floatList
54 54
55 55 if self.format == 'date':
56 56 strList = self.value.split('/')
57 57 intList = [int(x) for x in strList]
58 58 date = datetime.date(intList[0], intList[1], intList[2])
59 59 return date
60 60
61 61 if self.format == 'time':
62 62 strList = self.value.split(':')
63 63 intList = [int(x) for x in strList]
64 64 time = datetime.time(intList[0], intList[1], intList[2])
65 65 return time
66 66
67 67 func = eval(self.format)
68 68
69 69 return func(self.value)
70 70
71 71 def setup(self, id, name, value, format='str'):
72 72
73 73 self.id = id
74 74 self.name = name
75 75 self.value = str(value)
76 76 self.format = format
77 77
78 78 def makeXml(self, opElement):
79 79
80 80 parmElement = SubElement(opElement, self.ELEMENTNAME)
81 81 parmElement.set('id', str(self.id))
82 82 parmElement.set('name', self.name)
83 83 parmElement.set('value', self.value)
84 84 parmElement.set('format', self.format)
85 85
86 86 def readXml(self, parmElement):
87 87
88 88 self.id = parmElement.get('id')
89 89 self.name = parmElement.get('name')
90 90 self.value = parmElement.get('value')
91 91 self.format = parmElement.get('format')
92 92
93 93 def printattr(self):
94 94
95 95 print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format)
96 96
97 97 class OperationConf():
98 98
99 99 id = None
100 100 name = None
101 101 priority = None
102 102 type = None
103 103
104 104 parmConfObjList = []
105 105
106 106 ELEMENTNAME = 'Operation'
107 107
108 108 def __init__(self):
109 109
110 110 id = 0
111 111 name = None
112 112 priority = None
113 113 type = 'self'
114 114
115 115
116 116 def __getNewId(self):
117 117
118 118 return int(self.id)*10 + len(self.parmConfObjList) + 1
119 119
120 120 def getElementName(self):
121 121
122 122 return self.ELEMENTNAME
123 123
124 124 def getParameterObjList(self):
125 125
126 126 return self.parmConfObjList
127 127
128 128 def setup(self, id, name, priority, type):
129 129
130 130 self.id = id
131 131 self.name = name
132 132 self.type = type
133 133 self.priority = priority
134 134
135 135 self.parmConfObjList = []
136 136
137 137 def addParameter(self, name, value, format='str'):
138 138
139 139 id = self.__getNewId()
140 140
141 141 parmConfObj = ParameterConf()
142 142 parmConfObj.setup(id, name, value, format)
143 143
144 144 self.parmConfObjList.append(parmConfObj)
145 145
146 146 return parmConfObj
147 147
148 148 def makeXml(self, upElement):
149 149
150 150 opElement = SubElement(upElement, self.ELEMENTNAME)
151 151 opElement.set('id', str(self.id))
152 152 opElement.set('name', self.name)
153 153 opElement.set('type', self.type)
154 154 opElement.set('priority', str(self.priority))
155 155
156 156 for parmConfObj in self.parmConfObjList:
157 157 parmConfObj.makeXml(opElement)
158 158
159 159 def readXml(self, opElement):
160 160
161 161 self.id = opElement.get('id')
162 162 self.name = opElement.get('name')
163 163 self.type = opElement.get('type')
164 164 self.priority = opElement.get('priority')
165 165
166 166 self.parmConfObjList = []
167 167
168 168 parmElementList = opElement.getiterator(ParameterConf().getElementName())
169 169
170 170 for parmElement in parmElementList:
171 171 parmConfObj = ParameterConf()
172 172 parmConfObj.readXml(parmElement)
173 173 self.parmConfObjList.append(parmConfObj)
174 174
175 175 def printattr(self):
176 176
177 177 print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME,
178 178 self.id,
179 179 self.name,
180 180 self.type,
181 181 self.priority)
182 182
183 183 for parmConfObj in self.parmConfObjList:
184 184 parmConfObj.printattr()
185 185
186 186 def createObject(self):
187 187
188 188 if self.type == 'self':
189 189 raise ValueError, "This operation type cannot be created"
190 190
191 191 if self.type == 'other':
192 192 className = eval(self.name)
193 193 opObj = className()
194 194
195 195 return opObj
196 196
197 197 class ProcUnitConf():
198 198
199 199 id = None
200 200 name = None
201 201 datatype = None
202 202 inputId = None
203 203
204 204 opConfObjList = []
205 205
206 206 procUnitObj = None
207 207 opObjList = []
208 208
209 209 ELEMENTNAME = 'ProcUnit'
210 210
211 211 def __init__(self):
212 212
213 213 self.id = None
214 214 self.datatype = None
215 215 self.name = None
216 216 self.inputId = None
217 217
218 218 self.opConfObjList = []
219 219
220 220 self.procUnitObj = None
221 221 self.opObjDict = {}
222 222
223 223 def __getPriority(self):
224 224
225 225 return len(self.opConfObjList)+1
226 226
227 227 def __getNewId(self):
228 228
229 229 return int(self.id)*10 + len(self.opConfObjList) + 1
230 230
231 231 def getElementName(self):
232 232
233 233 return self.ELEMENTNAME
234 234
235 235 def getId(self):
236 236
237 237 return str(self.id)
238 238
239 239 def getInputId(self):
240 240
241 241 return str(self.inputId)
242 242
243 243 def getOperationObjList(self):
244 244
245 245 return self.opConfObjList
246 246
247 247 def getProcUnitObj(self):
248 248
249 249 return self.procUnitObj
250 250
251 251 def setup(self, id, name, datatype, inputId):
252 252
253 253 self.id = id
254 254 self.name = name
255 255 self.datatype = datatype
256 256 self.inputId = inputId
257 257
258 258 self.opConfObjList = []
259 259
260 260 self.addOperation(name='init', optype='self')
261 261
262 262 def addParameter(self, **kwargs):
263 263
264 264 opObj = self.opConfObjList[0]
265 265
266 266 opObj.addParameter(**kwargs)
267 267
268 268 return opObj
269 269
270 270 def addOperation(self, name, optype='self'):
271 271
272 272 id = self.__getNewId()
273 273 priority = self.__getPriority()
274 274
275 275 opConfObj = OperationConf()
276 276 opConfObj.setup(id, name=name, priority=priority, type=optype)
277 277
278 278 self.opConfObjList.append(opConfObj)
279 279
280 280 return opConfObj
281 281
282 282 def makeXml(self, procUnitElement):
283 283
284 284 upElement = SubElement(procUnitElement, self.ELEMENTNAME)
285 285 upElement.set('id', str(self.id))
286 286 upElement.set('name', self.name)
287 287 upElement.set('datatype', self.datatype)
288 288 upElement.set('inputId', str(self.inputId))
289 289
290 290 for opConfObj in self.opConfObjList:
291 291 opConfObj.makeXml(upElement)
292 292
293 293 def readXml(self, upElement):
294 294
295 295 self.id = upElement.get('id')
296 296 self.name = upElement.get('name')
297 297 self.datatype = upElement.get('datatype')
298 298 self.inputId = upElement.get('inputId')
299 299
300 300 self.opConfObjList = []
301 301
302 302 opElementList = upElement.getiterator(OperationConf().getElementName())
303 303
304 304 for opElement in opElementList:
305 305 opConfObj = OperationConf()
306 306 opConfObj.readXml(opElement)
307 307 self.opConfObjList.append(opConfObj)
308 308
309 309 def printattr(self):
310 310
311 311 print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME,
312 312 self.id,
313 313 self.name,
314 314 self.datatype,
315 315 self.inputId)
316 316
317 317 for opConfObj in self.opConfObjList:
318 318 opConfObj.printattr()
319 319
320 320 def createObjects(self):
321 321
322 322 className = eval(self.name)
323 323 procUnitObj = className()
324 324
325 325 for opConfObj in self.opConfObjList:
326 326
327 327 if opConfObj.type == 'self':
328 328 continue
329 329
330 330 opObj = opConfObj.createObject()
331 331
332 332 self.opObjDict[opConfObj.id] = opObj
333 333 procUnitObj.addOperation(opObj, opConfObj.id)
334 334
335 335 self.procUnitObj = procUnitObj
336 336
337 337 return procUnitObj
338 338
339 339 def run(self):
340 340
341 341 finalSts = False
342 342
343 343 for opConfObj in self.opConfObjList:
344 344
345 345 kwargs = {}
346 346 for parmConfObj in opConfObj.getParameterObjList():
347 347 kwargs[parmConfObj.name] = parmConfObj.getValue()
348 348
349 349 #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id)
350 350 sts = self.procUnitObj.call(opConfObj, **kwargs)
351 351 finalSts = finalSts or sts
352 352
353 353 return finalSts
354 354
355 355 class ReadUnitConf(ProcUnitConf):
356 356
357 357 path = None
358 358 startDate = None
359 359 endDate = None
360 360 startTime = None
361 361 endTime = None
362 362 online = None
363 363 expLabel = None
364 364 delay = None
365 365
366 366 ELEMENTNAME = 'ReadUnit'
367 367
368 368 def __init__(self):
369 369
370 370 self.id = None
371 371 self.datatype = None
372 372 self.name = None
373 373 self.inputId = 0
374 374
375 375 self.opConfObjList = []
376 376 self.opObjList = []
377 377
378 378 def getElementName(self):
379 379
380 380 return self.ELEMENTNAME
381 381
382 382 def setup(self, id, name, datatype, path, startDate, endDate, startTime, endTime, online=0, expLabel='', delay=60):
383 383
384 384 self.id = id
385 385 self.name = name
386 386 self.datatype = datatype
387 387
388 388 self.path = path
389 389 self.startDate = startDate
390 390 self.endDate = endDate
391 391 self.startTime = startTime
392 392 self.endTime = endTime
393 393 self.online = online
394 394 self.expLabel = expLabel
395 395 self.delay = delay
396 396
397 397 self.addRunOperation()
398 398
399 399 def addRunOperation(self):
400 400
401 401 opObj = self.addOperation(name = 'run', optype = 'self')
402 402
403 403 opObj.addParameter(name='path' , value=self.path, format='str')
404 404 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
405 405 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
406 406 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
407 407 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
408 408 opObj.addParameter(name='expLabel' , value=self.expLabel, format='str')
409 409 opObj.addParameter(name='online' , value=self.online, format='int')
410 410 opObj.addParameter(name='delay' , value=self.delay, format='float')
411 411
412 412 return opObj
413 413
414 414
415 415 class Controller():
416 416
417 417 id = None
418 418 name = None
419 419 description = None
420 420 # readUnitConfObjList = None
421 421 procUnitConfObjDict = None
422 422
423 423 ELEMENTNAME = 'Controller'
424 424
425 425 def __init__(self):
426 426
427 427 self.id = None
428 428 self.name = None
429 429 self.description = None
430 430
431 431 # self.readUnitConfObjList = []
432 432 self.procUnitConfObjDict = {}
433 433
434 434 def __getNewId(self):
435 435
436 436 id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1
437 437
438 438 return str(id)
439 439
440 440 def getElementName(self):
441 441
442 442 return self.ELEMENTNAME
443 443
444 444 def setup(self, id, name, description):
445 445
446 446 self.id = id
447 447 self.name = name
448 448 self.description = description
449 449
450 450 def addReadUnit(self, datatype, path, startDate='', endDate='', startTime='', endTime='', online=0, expLabel='', delay=60):
451 451
452 452 id = self.__getNewId()
453 453 name = '%sReader' %(datatype)
454 454
455 455 readUnitConfObj = ReadUnitConf()
456 456 readUnitConfObj.setup(id, name, datatype, path, startDate, endDate, startTime, endTime, online, expLabel, delay)
457 457
458 458 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
459 459
460 460 return readUnitConfObj
461 461
462 462 def addProcUnit(self, datatype, inputId):
463 463
464 464 id = self.__getNewId()
465 465 name = '%sProc' %(datatype)
466 466
467 467 procUnitConfObj = ProcUnitConf()
468 468 procUnitConfObj.setup(id, name, datatype, inputId)
469 469
470 470 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
471 471
472 472 return procUnitConfObj
473 473
474 474 def makeXml(self):
475 475
476 476 projectElement = Element('Controller')
477 477 projectElement.set('id', str(self.id))
478 478 projectElement.set('name', self.name)
479 479 projectElement.set('description', self.description)
480 480
481 481 # for readUnitConfObj in self.readUnitConfObjList:
482 482 # readUnitConfObj.makeXml(projectElement)
483 483
484 484 for procUnitConfObj in self.procUnitConfObjDict.values():
485 485 procUnitConfObj.makeXml(projectElement)
486 486
487 487 self.projectElement = projectElement
488 488
489 489 def writeXml(self, filename):
490 490
491 491 self.makeXml()
492 492
493 493 print prettify(self.projectElement)
494 494
495 495 ElementTree(self.projectElement).write(filename, method='xml')
496 496
497 497 def readXml(self, filename):
498 498
499 499 #tree = ET.parse(filename)
500 500 self.projectElement = None
501 501 # self.readUnitConfObjList = []
502 502 self.procUnitConfObjDict = {}
503 503
504 504 self.projectElement = ElementTree().parse(filename)
505 505
506 506 self.project = self.projectElement.tag
507 507
508 508 self.id = self.projectElement.get('id')
509 509 self.name = self.projectElement.get('name')
510 510 self.description = self.projectElement.get('description')
511 511
512 512 readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName())
513 513
514 514 for readUnitElement in readUnitElementList:
515 515 readUnitConfObj = ReadUnitConf()
516 516 readUnitConfObj.readXml(readUnitElement)
517 517
518 518 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
519 519
520 520 procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName())
521 521
522 522 for procUnitElement in procUnitElementList:
523 523 procUnitConfObj = ProcUnitConf()
524 524 procUnitConfObj.readXml(procUnitElement)
525 525
526 526 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
527 527
528 528 def printattr(self):
529 529
530 530 print "Controller[%s]: name = %s, description = %s" %(self.id,
531 531 self.name,
532 532 self.description)
533 533
534 534 # for readUnitConfObj in self.readUnitConfObjList:
535 535 # readUnitConfObj.printattr()
536 536
537 537 for procUnitConfObj in self.procUnitConfObjDict.values():
538 538 procUnitConfObj.printattr()
539 539
540 540 def createObjects(self):
541 541
542 542 # for readUnitConfObj in self.readUnitConfObjList:
543 543 # readUnitConfObj.createObjects()
544 544
545 545 for procUnitConfObj in self.procUnitConfObjDict.values():
546 546 procUnitConfObj.createObjects()
547 547
548 548 def __connect(self, objIN, obj):
549 549
550 550 obj.setInput(objIN.getOutput())
551 551
552 552 def connectObjects(self):
553 553
554 554 for puConfObj in self.procUnitConfObjDict.values():
555 555
556 556 inputId = puConfObj.getInputId()
557 557
558 558 if int(inputId) == 0:
559 559 continue
560 560
561 561 puConfINObj = self.procUnitConfObjDict[inputId]
562 562
563 563 puObj = puConfObj.getProcUnitObj()
564 564 puINObj = puConfINObj.getProcUnitObj()
565 565
566 566 self.__connect(puINObj, puObj)
567 567
568 568 def run(self):
569 569
570 570 # for readUnitConfObj in self.readUnitConfObjList:
571 571 # readUnitConfObj.run()
572 572
573 573 while(True):
574 574
575 575 finalSts = False
576 576
577 577 for procUnitConfObj in self.procUnitConfObjDict.values():
578 578 #print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id)
579 579 sts = procUnitConfObj.run()
580 580 finalSts = finalSts or sts
581 581
582 582 #If every process unit finished so end process
583 583 if not(finalSts):
584 584 print "Every process units have finished"
585 585 break
586 586
587 587 if __name__ == '__main__':
588 588
589 589 desc = "Segundo Test"
590 590 filename = "schain.xml"
591 591
592 592 controllerObj = Controller()
593 593
594 594 controllerObj.setup(id = '191', name='test01', description=desc)
595 595
596 596 readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage',
597 597 path='data/rawdata/',
598 598 startDate='2011/01/01',
599 599 endDate='2012/12/31',
600 600 startTime='00:00:00',
601 601 endTime='23:59:59',
602 602 online=0)
603 603
604 604 # opObj00 = readUnitConfObj.addOperation(name='printInfo')
605 605
606 606 procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId())
607 607
608 608 opObj10 = procUnitConfObj0.addOperation(name='selectChannels')
609 609 opObj10.addParameter(name='channelList', value='3,4,5', format='intlist')
610 610
611 611 opObj10 = procUnitConfObj0.addOperation(name='selectHeights')
612 612 opObj10.addParameter(name='minHei', value='90', format='float')
613 opObj10.addParameter(name='maxHei', value='300', format='float')
613 opObj10.addParameter(name='maxHei', value='180', format='float')
614 614
615 615 opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='other')
616 616 opObj12.addParameter(name='n', value='10', format='int')
617 617
618 opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='other')
619 opObj12.addParameter(name='n', value='2', format='int')
620 opObj12.addParameter(name='overlapping', value='1', format='int')
621
622 618 procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId())
623 619 procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int')
624 620
625 621 opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='other')
626 622 opObj11.addParameter(name='idfigure', value='1', format='int')
627 opObj11.addParameter(name='wintitle', value='SpectraPlot', format='str')
623 opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str')
628 624 opObj11.addParameter(name='zmin', value='40', format='int')
629 625 opObj11.addParameter(name='zmax', value='90', format='int')
630 opObj11.addParameter(name='showprofile', value='1', format='int')
626 opObj11.addParameter(name='showprofile', value='1', format='int')
627
628 procUnitConfObj2 = controllerObj.addProcUnit(datatype='Voltage', inputId=procUnitConfObj0.getId())
629
630 opObj12 = procUnitConfObj2.addOperation(name='CohInt', optype='other')
631 opObj12.addParameter(name='n', value='2', format='int')
632 opObj12.addParameter(name='overlapping', value='1', format='int')
631 633
632 opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype='other')
633 opObj11.addParameter(name='idfigure', value='10', format='int')
634 opObj11.addParameter(name='wintitle', value='RTI', format='str')
635 # opObj11.addParameter(name='xmin', value='21', format='float')
636 # opObj11.addParameter(name='xmax', value='22', format='float')
634 procUnitConfObj3 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj2.getId())
635 procUnitConfObj3.addParameter(name='nFFTPoints', value='32', format='int')
636
637 opObj11 = procUnitConfObj3.addOperation(name='SpectraPlot', optype='other')
638 opObj11.addParameter(name='idfigure', value='2', format='int')
639 opObj11.addParameter(name='wintitle', value='SpectraPlot1', format='str')
637 640 opObj11.addParameter(name='zmin', value='40', format='int')
638 641 opObj11.addParameter(name='zmax', value='90', format='int')
639 opObj11.addParameter(name='showprofile', value='1', format='int')
640 opObj11.addParameter(name='timerange', value=str(60), format='int')
642 # opObj11.addParameter(name='showprofile', value='1', format='int')
643
644 # opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype='other')
645 # opObj11.addParameter(name='idfigure', value='10', format='int')
646 # opObj11.addParameter(name='wintitle', value='RTI', format='str')
647 ## opObj11.addParameter(name='xmin', value='21', format='float')
648 ## opObj11.addParameter(name='xmax', value='22', format='float')
649 # opObj11.addParameter(name='zmin', value='40', format='int')
650 # opObj11.addParameter(name='zmax', value='90', format='int')
651 # opObj11.addParameter(name='showprofile', value='1', format='int')
652 # opObj11.addParameter(name='timerange', value=str(60), format='int')
641 653
642 654 # opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
643 655 # opObj10.addParameter(name='channelList', value='0,2,4,6', format='intlist')
644 656 #
645 657 # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='other')
646 658 # opObj12.addParameter(name='n', value='2', format='int')
647 659 #
648 660 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='other')
649 661 # opObj11.addParameter(name='idfigure', value='2', format='int')
650 662 # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str')
651 663 # opObj11.addParameter(name='zmin', value='70', format='int')
652 664 # opObj11.addParameter(name='zmax', value='90', format='int')
653 665 #
654 666 # opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
655 667 # opObj10.addParameter(name='channelList', value='2,6', format='intlist')
656 668 #
657 669 # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='other')
658 670 # opObj12.addParameter(name='n', value='2', format='int')
659 671 #
660 672 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='other')
661 673 # opObj11.addParameter(name='idfigure', value='3', format='int')
662 674 # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str')
663 675 # opObj11.addParameter(name='zmin', value='70', format='int')
664 676 # opObj11.addParameter(name='zmax', value='90', format='int')
665 677
666 678
667 679 # opObj12 = procUnitConfObj1.addOperation(name='decoder')
668 680 # opObj12.addParameter(name='ncode', value='2', format='int')
669 681 # opObj12.addParameter(name='nbauds', value='8', format='int')
670 682 # opObj12.addParameter(name='code0', value='001110011', format='int')
671 683 # opObj12.addParameter(name='code1', value='001110011', format='int')
672 684
673 685
674 686
675 687 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj1.getId())
676 688 #
677 689 # opObj21 = procUnitConfObj2.addOperation(name='IncohInt', optype='other')
678 690 # opObj21.addParameter(name='n', value='2', format='int')
679 691 #
680 692 # opObj11 = procUnitConfObj2.addOperation(name='SpectraPlot', optype='other')
681 693 # opObj11.addParameter(name='idfigure', value='4', format='int')
682 694 # opObj11.addParameter(name='wintitle', value='SpectraPlot OBJ 2', format='str')
683 695 # opObj11.addParameter(name='zmin', value='70', format='int')
684 696 # opObj11.addParameter(name='zmax', value='90', format='int')
685 697
686 698 print "Escribiendo el archivo XML"
687 699
688 700 controllerObj.writeXml(filename)
689 701
690 702 print "Leyendo el archivo XML"
691 703 controllerObj.readXml(filename)
692 704 #controllerObj.printattr()
693 705
694 706 controllerObj.createObjects()
695 707 controllerObj.connectObjects()
696 708 controllerObj.run()
697 709
698 710 No newline at end of file
@@ -1,971 +1,975
1 1 '''
2 2
3 3 $Author: dsuarez $
4 4 $Id: Processor.py 1 2012-11-12 18:56:07Z dsuarez $
5 5 '''
6 6 import os
7 7 import numpy
8 8 import datetime
9 9 import time
10 10
11 11 from jrodata import *
12 12 from jrodataIO import *
13 13 from jroplot import *
14 14
15 15 class ProcessingUnit:
16 16
17 17 """
18 18 Esta es la clase base para el procesamiento de datos.
19 19
20 20 Contiene el metodo "call" para llamar operaciones. Las operaciones pueden ser:
21 21 - Metodos internos (callMethod)
22 22 - Objetos del tipo Operation (callObject). Antes de ser llamados, estos objetos
23 23 tienen que ser agreagados con el metodo "add".
24 24
25 25 """
26 26 # objeto de datos de entrada (Voltage, Spectra o Correlation)
27 27 dataIn = None
28 28
29 29 # objeto de datos de entrada (Voltage, Spectra o Correlation)
30 30 dataOut = None
31 31
32 32
33 33 objectDict = None
34 34
35 35 def __init__(self):
36 36
37 37 self.objectDict = {}
38 38
39 39 def init(self):
40 40
41 41 raise ValueError, "Not implemented"
42 42
43 43 def addOperation(self, object, objId):
44 44
45 45 """
46 46 Agrega el objeto "object" a la lista de objetos "self.objectList" y retorna el
47 47 identificador asociado a este objeto.
48 48
49 49 Input:
50 50
51 51 object : objeto de la clase "Operation"
52 52
53 53 Return:
54 54
55 55 objId : identificador del objeto, necesario para ejecutar la operacion
56 56 """
57 57
58 58 self.objectDict[objId] = object
59 59
60 60 return objId
61 61
62 62 def operation(self, **kwargs):
63 63
64 64 """
65 65 Operacion directa sobre la data (dataout.data). Es necesario actualizar los valores de los
66 66 atributos del objeto dataOut
67 67
68 68 Input:
69 69
70 70 **kwargs : Diccionario de argumentos de la funcion a ejecutar
71 71 """
72 72
73 73 raise ValueError, "ImplementedError"
74 74
75 75 def callMethod(self, name, **kwargs):
76 76
77 77 """
78 78 Ejecuta el metodo con el nombre "name" y con argumentos **kwargs de la propia clase.
79 79
80 80 Input:
81 81 name : nombre del metodo a ejecutar
82 82
83 83 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
84 84
85 85 """
86 86 if name != 'run':
87 87
88 88 if name == 'init' and self.dataIn.isEmpty():
89 89 self.dataOut.flagNoData = True
90 90 return False
91 91
92 92 if name != 'init' and self.dataOut.isEmpty():
93 93 return False
94 94
95 95 methodToCall = getattr(self, name)
96 96
97 97 methodToCall(**kwargs)
98 98
99 99 if name != 'run':
100 100 return True
101 101
102 102 if self.dataOut.isEmpty():
103 103 return False
104 104
105 105 return True
106 106
107 107 def callObject(self, objId, **kwargs):
108 108
109 109 """
110 110 Ejecuta la operacion asociada al identificador del objeto "objId"
111 111
112 112 Input:
113 113
114 114 objId : identificador del objeto a ejecutar
115 115
116 116 **kwargs : diccionario con los nombres y valores de la funcion a ejecutar.
117 117
118 118 Return:
119 119
120 120 None
121 121 """
122 122
123 123 if self.dataOut.isEmpty():
124 124 return False
125 125
126 126 object = self.objectDict[objId]
127 127
128 128 object.run(self.dataOut, **kwargs)
129 129
130 130 return True
131 131
132 132 def call(self, operationConf, **kwargs):
133 133
134 134 """
135 135 Return True si ejecuta la operacion "operationConf.name" con los
136 136 argumentos "**kwargs". False si la operacion no se ha ejecutado.
137 137 La operacion puede ser de dos tipos:
138 138
139 139 1. Un metodo propio de esta clase:
140 140
141 141 operation.type = "self"
142 142
143 143 2. El metodo "run" de un objeto del tipo Operation o de un derivado de ella:
144 144 operation.type = "other".
145 145
146 146 Este objeto de tipo Operation debe de haber sido agregado antes con el metodo:
147 147 "addOperation" e identificado con el operation.id
148 148
149 149
150 150 con el id de la operacion.
151 151
152 152 Input:
153 153
154 154 Operation : Objeto del tipo operacion con los atributos: name, type y id.
155 155
156 156 """
157 157
158 158 if operationConf.type == 'self':
159 159 sts = self.callMethod(operationConf.name, **kwargs)
160 160
161 161 if operationConf.type == 'other':
162 162 sts = self.callObject(operationConf.id, **kwargs)
163 163
164 164 return sts
165 165
166 166 def setInput(self, dataIn):
167 167
168 168 self.dataIn = dataIn
169 169
170 170 def getOutput(self):
171 171
172 172 return self.dataOut
173 173
174 174 class Operation():
175 175
176 176 """
177 177 Clase base para definir las operaciones adicionales que se pueden agregar a la clase ProcessingUnit
178 178 y necesiten acumular informacion previa de los datos a procesar. De preferencia usar un buffer de
179 179 acumulacion dentro de esta clase
180 180
181 181 Ejemplo: Integraciones coherentes, necesita la informacion previa de los n perfiles anteriores (bufffer)
182 182
183 183 """
184 184
185 185 __buffer = None
186 186 __isConfig = False
187 187
188 188 def __init__(self):
189 189
190 190 pass
191 191
192 192 def run(self, dataIn, **kwargs):
193 193
194 194 """
195 195 Realiza las operaciones necesarias sobre la dataIn.data y actualiza los atributos del objeto dataIn.
196 196
197 197 Input:
198 198
199 199 dataIn : objeto del tipo JROData
200 200
201 201 Return:
202 202
203 203 None
204 204
205 205 Affected:
206 206 __buffer : buffer de recepcion de datos.
207 207
208 208 """
209 209
210 210 raise ValueError, "ImplementedError"
211 211
212 212 class VoltageProc(ProcessingUnit):
213 213
214 214
215 215 def __init__(self):
216 216
217 217 self.objectDict = {}
218 218 self.dataOut = Voltage()
219 219
220 220 def init(self):
221 221
222 222 self.dataOut.copy(self.dataIn)
223 223 # No necesita copiar en cada init() los atributos de dataIn
224 224 # la copia deberia hacerse por cada nuevo bloque de datos
225 225
226 226 def selectChannels(self, channelList):
227 227
228 228 channelIndexList = []
229 229
230 230 for channel in channelList:
231 231 index = self.dataOut.channelList.index(channel)
232 232 channelIndexList.append(index)
233 233
234 234 self.selectChannelsByIndex(channelIndexList)
235 235
236 236 def selectChannelsByIndex(self, channelIndexList):
237 237 """
238 238 Selecciona un bloque de datos en base a canales segun el channelIndexList
239 239
240 240 Input:
241 241 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
242 242
243 243 Affected:
244 244 self.dataOut.data
245 245 self.dataOut.channelIndexList
246 246 self.dataOut.nChannels
247 247 self.dataOut.m_ProcessingHeader.totalSpectra
248 248 self.dataOut.systemHeaderObj.numChannels
249 249 self.dataOut.m_ProcessingHeader.blockSize
250 250
251 251 Return:
252 252 None
253 253 """
254 254
255 255 for channelIndex in channelIndexList:
256 256 if channelIndex not in self.dataOut.channelIndexList:
257 257 print channelIndexList
258 258 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
259 259
260 260 nChannels = len(channelIndexList)
261 261
262 262 data = self.dataOut.data[channelIndexList,:]
263 263
264 264 self.dataOut.data = data
265 265 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
266 266 # self.dataOut.nChannels = nChannels
267 267
268 268 return 1
269 269
270 270 def selectHeights(self, minHei, maxHei):
271 271 """
272 272 Selecciona un bloque de datos en base a un grupo de valores de alturas segun el rango
273 273 minHei <= height <= maxHei
274 274
275 275 Input:
276 276 minHei : valor minimo de altura a considerar
277 277 maxHei : valor maximo de altura a considerar
278 278
279 279 Affected:
280 280 Indirectamente son cambiados varios valores a travez del metodo selectHeightsByIndex
281 281
282 282 Return:
283 283 1 si el metodo se ejecuto con exito caso contrario devuelve 0
284 284 """
285 285 if (minHei < self.dataOut.heightList[0]) or (minHei > maxHei):
286 286 raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
287 287
288 288 if (maxHei > self.dataOut.heightList[-1]):
289 289 maxHei = self.dataOut.heightList[-1]
290 290 # raise ValueError, "some value in (%d,%d) is not valid" % (minHei, maxHei)
291 291
292 292 minIndex = 0
293 293 maxIndex = 0
294 294 data = self.dataOut.heightList
295 295
296 296 for i,val in enumerate(data):
297 297 if val < minHei:
298 298 continue
299 299 else:
300 300 minIndex = i;
301 301 break
302 302
303 303 for i,val in enumerate(data):
304 304 if val <= maxHei:
305 305 maxIndex = i;
306 306 else:
307 307 break
308 308
309 309 self.selectHeightsByIndex(minIndex, maxIndex)
310 310
311 311 return 1
312 312
313 313
314 314 def selectHeightsByIndex(self, minIndex, maxIndex):
315 315 """
316 316 Selecciona un bloque de datos en base a un grupo indices de alturas segun el rango
317 317 minIndex <= index <= maxIndex
318 318
319 319 Input:
320 320 minIndex : valor de indice minimo de altura a considerar
321 321 maxIndex : valor de indice maximo de altura a considerar
322 322
323 323 Affected:
324 324 self.dataOut.data
325 325 self.dataOut.heightList
326 326
327 327 Return:
328 328 1 si el metodo se ejecuto con exito caso contrario devuelve 0
329 329 """
330 330
331 331 if (minIndex < 0) or (minIndex > maxIndex):
332 332 raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
333 333
334 334 if (maxIndex >= self.dataOut.nHeights):
335 335 maxIndex = self.dataOut.nHeights-1
336 336 # raise ValueError, "some value in (%d,%d) is not valid" % (minIndex, maxIndex)
337 337
338 338 nHeights = maxIndex - minIndex + 1
339 339
340 340 #voltage
341 341 data = self.dataOut.data[:,minIndex:maxIndex+1]
342 342
343 343 firstHeight = self.dataOut.heightList[minIndex]
344 344
345 345 self.dataOut.data = data
346 346 self.dataOut.heightList = self.dataOut.heightList[minIndex:maxIndex+1]
347 347
348 348 return 1
349 349
350 350
351 351 class CohInt(Operation):
352 352
353 353 __profIndex = 0
354 354 __withOverapping = False
355 355
356 356 __byTime = False
357 357 __initime = None
358 358 __lastdatatime = None
359 359 __integrationtime = None
360 360
361 361 __buffer = None
362 362
363 363 __dataReady = False
364 364
365 365 n = None
366 366
367 367
368 368 def __init__(self):
369 369
370 370 self.__isConfig = False
371 371
372 372 def setup(self, n=None, timeInterval=None, overlapping=False):
373 373 """
374 374 Set the parameters of the integration class.
375 375
376 376 Inputs:
377 377
378 378 n : Number of coherent integrations
379 379 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
380 380 overlapping :
381 381
382 382 """
383 383
384 384 self.__initime = None
385 385 self.__lastdatatime = 0
386 386 self.__buffer = None
387 387 self.__dataReady = False
388 388
389 389
390 390 if n == None and timeInterval == None:
391 391 raise ValueError, "n or timeInterval should be specified ..."
392 392
393 393 if n != None:
394 394 self.n = n
395 395 self.__byTime = False
396 396 else:
397 397 self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line
398 398 self.n = 9999
399 399 self.__byTime = True
400 400
401 401 if overlapping:
402 402 self.__withOverapping = True
403 403 self.__buffer = None
404 404 else:
405 405 self.__withOverapping = False
406 406 self.__buffer = 0
407 407
408 408 self.__profIndex = 0
409 409
410 410 def putData(self, data):
411 411
412 412 """
413 413 Add a profile to the __buffer and increase in one the __profileIndex
414 414
415 415 """
416 416
417 417 if not self.__withOverapping:
418 418 self.__buffer += data.copy()
419 419 self.__profIndex += 1
420 420 return
421 421
422 422 #Overlapping data
423 423 nChannels, nHeis = data.shape
424 424 data = numpy.reshape(data, (1, nChannels, nHeis))
425 425
426 426 #If the buffer is empty then it takes the data value
427 427 if self.__buffer == None:
428 428 self.__buffer = data
429 429 self.__profIndex += 1
430 430 return
431 431
432 432 #If the buffer length is lower than n then stakcing the data value
433 433 if self.__profIndex < self.n:
434 434 self.__buffer = numpy.vstack((self.__buffer, data))
435 435 self.__profIndex += 1
436 436 return
437 437
438 438 #If the buffer length is equal to n then replacing the last buffer value with the data value
439 439 self.__buffer = numpy.roll(self.__buffer, -1, axis=0)
440 440 self.__buffer[self.n-1] = data
441 441 self.__profIndex = self.n
442 442 return
443 443
444 444
445 445 def pushData(self):
446 446 """
447 447 Return the sum of the last profiles and the profiles used in the sum.
448 448
449 449 Affected:
450 450
451 451 self.__profileIndex
452 452
453 453 """
454 454
455 455 if not self.__withOverapping:
456 456 data = self.__buffer
457 457 n = self.__profIndex
458 458
459 459 self.__buffer = 0
460 460 self.__profIndex = 0
461 461
462 462 return data, n
463 463
464 464 #Integration with Overlapping
465 465 data = numpy.sum(self.__buffer, axis=0)
466 466 n = self.__profIndex
467 467
468 468 return data, n
469 469
470 470 def byProfiles(self, data):
471 471
472 472 self.__dataReady = False
473 473 avgdata = None
474 474 n = None
475 475
476 476 self.putData(data)
477 477
478 478 if self.__profIndex == self.n:
479 479
480 480 avgdata, n = self.pushData()
481 481 self.__dataReady = True
482 482
483 483 return avgdata
484 484
485 485 def byTime(self, data, datatime):
486 486
487 487 self.__dataReady = False
488 488 avgdata = None
489 489 n = None
490 490
491 491 self.putData(data)
492 492
493 493 if (datatime - self.__initime) >= self.__integrationtime:
494 494 avgdata, n = self.pushData()
495 495 self.n = n
496 496 self.__dataReady = True
497 497
498 498 return avgdata
499 499
500 500 def integrate(self, data, datatime=None):
501 501
502 502 if self.__initime == None:
503 503 self.__initime = datatime
504 504
505 505 if self.__byTime:
506 506 avgdata = self.byTime(data, datatime)
507 507 else:
508 508 avgdata = self.byProfiles(data)
509 509
510 510
511 511 self.__lastdatatime = datatime
512 512
513 513 if avgdata == None:
514 514 return None, None
515 515
516 516 avgdatatime = self.__initime
517 517
518 518 deltatime = datatime -self.__lastdatatime
519 519
520 520 if not self.__withOverapping:
521 521 self.__initime = datatime
522 522 else:
523 523 self.__initime += deltatime
524 524
525 525 return avgdata, avgdatatime
526 526
527 527 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
528 528
529 529 if not self.__isConfig:
530 530 self.setup(n, timeInterval, overlapping)
531 531 self.__isConfig = True
532 532
533 533 avgdata, avgdatatime = self.integrate(dataOut.data, dataOut.utctime)
534 534
535 535 # dataOut.timeInterval *= n
536 536 dataOut.flagNoData = True
537 537
538 538 if self.__dataReady:
539 539 dataOut.data = avgdata
540 540 dataOut.nCohInt *= self.n
541 541 dataOut.utctime = avgdatatime
542 542 dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt
543 543 dataOut.flagNoData = False
544 544
545 545
546 546 class SpectraProc(ProcessingUnit):
547 547
548 548 def __init__(self):
549 549
550 550 self.objectDict = {}
551 551 self.buffer = None
552 552 self.firstdatatime = None
553 553 self.profIndex = 0
554 554 self.dataOut = Spectra()
555 555
556 556 def __updateObjFromInput(self):
557 557
558 558 self.dataOut.radarControllerHeaderObj = self.dataIn.radarControllerHeaderObj.copy()
559 559 self.dataOut.systemHeaderObj = self.dataIn.systemHeaderObj.copy()
560 560 self.dataOut.channelList = self.dataIn.channelList
561 561 self.dataOut.heightList = self.dataIn.heightList
562 562 self.dataOut.dtype = self.dataIn.dtype
563 563 # self.dataOut.nHeights = self.dataIn.nHeights
564 564 # self.dataOut.nChannels = self.dataIn.nChannels
565 565 self.dataOut.nBaud = self.dataIn.nBaud
566 566 self.dataOut.nCode = self.dataIn.nCode
567 567 self.dataOut.code = self.dataIn.code
568 568 self.dataOut.nProfiles = self.dataOut.nFFTPoints
569 569 # self.dataOut.channelIndexList = self.dataIn.channelIndexList
570 570 self.dataOut.flagTimeBlock = self.dataIn.flagTimeBlock
571 571 self.dataOut.utctime = self.firstdatatime
572 572 self.dataOut.flagDecodeData = self.dataIn.flagDecodeData #asumo q la data esta decodificada
573 573 self.dataOut.flagDeflipData = self.dataIn.flagDeflipData #asumo q la data esta sin flip
574 574 self.dataOut.flagShiftFFT = self.dataIn.flagShiftFFT
575 575 self.dataOut.nCohInt = self.dataIn.nCohInt
576 576 self.dataOut.nIncohInt = 1
577 577 self.dataOut.ippSeconds = self.dataIn.ippSeconds
578 578
579 579 self.dataOut.timeInterval = self.dataIn.timeInterval*self.dataOut.nFFTPoints*self.dataOut.nIncohInt
580 580
581 581 def __getFft(self):
582 582 """
583 583 Convierte valores de Voltaje a Spectra
584 584
585 585 Affected:
586 586 self.dataOut.data_spc
587 587 self.dataOut.data_cspc
588 588 self.dataOut.data_dc
589 589 self.dataOut.heightList
590 590 self.profIndex
591 591 self.buffer
592 592 self.dataOut.flagNoData
593 593 """
594 594 fft_volt = numpy.fft.fft(self.buffer,axis=1)
595 595 dc = fft_volt[:,0,:]
596 596
597 597 #calculo de self-spectra
598 598 fft_volt = numpy.fft.fftshift(fft_volt,axes=(1,))
599 599 spc = fft_volt * numpy.conjugate(fft_volt)
600 600 spc = spc.real
601 601
602 602 blocksize = 0
603 603 blocksize += dc.size
604 604 blocksize += spc.size
605 605
606 606 cspc = None
607 607 pairIndex = 0
608 608 if self.dataOut.pairsList != None:
609 609 #calculo de cross-spectra
610 610 cspc = numpy.zeros((self.dataOut.nPairs, self.dataOut.nFFTPoints, self.dataOut.nHeights), dtype='complex')
611 611 for pair in self.dataOut.pairsList:
612 612 cspc[pairIndex,:,:] = numpy.abs(fft_volt[pair[0],:,:] * numpy.conjugate(fft_volt[pair[1],:,:]))
613 613 pairIndex += 1
614 614 blocksize += cspc.size
615 615
616 616 self.dataOut.data_spc = spc
617 617 self.dataOut.data_cspc = cspc
618 618 self.dataOut.data_dc = dc
619 619 self.dataOut.blockSize = blocksize
620 620
621 621 def init(self, nFFTPoints=None, pairsList=None):
622 622
623 623 if self.dataIn.type == "Spectra":
624 624 self.dataOut.copy(self.dataIn)
625 625 return
626 626
627 627 if self.dataIn.type == "Voltage":
628 628
629 629 if nFFTPoints == None:
630 630 raise ValueError, "This SpectraProc.init() need nFFTPoints input variable"
631 631
632 632 if pairsList == None:
633 633 nPairs = 0
634 634 else:
635 635 nPairs = len(pairsList)
636 636
637 637 self.dataOut.nFFTPoints = nFFTPoints
638 638 self.dataOut.pairsList = pairsList
639 639 self.dataOut.nPairs = nPairs
640 640
641 641 if self.buffer == None:
642 642 self.buffer = numpy.zeros((self.dataIn.nChannels,
643 643 self.dataOut.nFFTPoints,
644 644 self.dataIn.nHeights),
645 645 dtype='complex')
646 646
647 647
648 648 self.buffer[:,self.profIndex,:] = self.dataIn.data
649 649 self.profIndex += 1
650 650
651 651 if self.firstdatatime == None:
652 652 self.firstdatatime = self.dataIn.utctime
653 653
654 654 if self.profIndex == self.dataOut.nFFTPoints:
655 655 self.__updateObjFromInput()
656 656 self.__getFft()
657 657
658 658 self.dataOut.flagNoData = False
659 659
660 660 self.buffer = None
661 661 self.firstdatatime = None
662 662 self.profIndex = 0
663 663
664 664 return
665 665
666 666 raise ValuError, "The type object %s is not valid"%(self.dataIn.type)
667 667
668 668 def selectChannels(self, channelList):
669 669
670 670 channelIndexList = []
671 671
672 672 for channel in channelList:
673 673 index = self.dataOut.channelList.index(channel)
674 674 channelIndexList.append(index)
675 675
676 676 self.selectChannelsByIndex(channelIndexList)
677 677
678 678 def selectChannelsByIndex(self, channelIndexList):
679 679 """
680 680 Selecciona un bloque de datos en base a canales segun el channelIndexList
681 681
682 682 Input:
683 683 channelIndexList : lista sencilla de canales a seleccionar por ej. [2,3,7]
684 684
685 685 Affected:
686 686 self.dataOut.data_spc
687 687 self.dataOut.channelIndexList
688 688 self.dataOut.nChannels
689 689
690 690 Return:
691 691 None
692 692 """
693 693
694 694 for channelIndex in channelIndexList:
695 695 if channelIndex not in self.dataOut.channelIndexList:
696 696 print channelIndexList
697 697 raise ValueError, "The value %d in channelIndexList is not valid" %channelIndex
698 698
699 699 nChannels = len(channelIndexList)
700 700
701 701 data_spc = self.dataOut.data_spc[channelIndexList,:]
702 702
703 703 self.dataOut.data_spc = data_spc
704 704 self.dataOut.channelList = [self.dataOut.channelList[i] for i in channelIndexList]
705 705 # self.dataOut.nChannels = nChannels
706 706
707 707 return 1
708 708
709 709
710 710 class IncohInt(Operation):
711 711
712 712
713 713 __profIndex = 0
714 714 __withOverapping = False
715 715
716 716 __byTime = False
717 717 __initime = None
718 718 __lastdatatime = None
719 719 __integrationtime = None
720 720
721 721 __buffer_spc = None
722 722 __buffer_cspc = None
723 723 __buffer_dc = None
724 724
725 725 __dataReady = False
726 726
727 727 n = None
728 728
729 729
730 730 def __init__(self):
731 731
732 732 self.__isConfig = False
733 733
734 734 def setup(self, n=None, timeInterval=None, overlapping=False):
735 735 """
736 736 Set the parameters of the integration class.
737 737
738 738 Inputs:
739 739
740 740 n : Number of coherent integrations
741 741 timeInterval : Time of integration. If the parameter "n" is selected this one does not work
742 742 overlapping :
743 743
744 744 """
745 745
746 746 self.__initime = None
747 747 self.__lastdatatime = 0
748 748 self.__buffer_spc = None
749 749 self.__buffer_cspc = None
750 750 self.__buffer_dc = None
751 751 self.__dataReady = False
752 752
753 753
754 754 if n == None and timeInterval == None:
755 755 raise ValueError, "n or timeInterval should be specified ..."
756 756
757 757 if n != None:
758 758 self.n = n
759 759 self.__byTime = False
760 760 else:
761 761 self.__integrationtime = timeInterval * 60. #if (type(timeInterval)!=integer) -> change this line
762 762 self.n = 9999
763 763 self.__byTime = True
764 764
765 765 if overlapping:
766 766 self.__withOverapping = True
767 767 else:
768 768 self.__withOverapping = False
769 769 self.__buffer_spc = 0
770 770 self.__buffer_cspc = 0
771 771 self.__buffer_dc = 0
772 772
773 773 self.__profIndex = 0
774 774
775 775 def putData(self, data_spc, data_cspc, data_dc):
776 776
777 777 """
778 778 Add a profile to the __buffer_spc and increase in one the __profileIndex
779 779
780 780 """
781 781
782 782 if not self.__withOverapping:
783 783 self.__buffer_spc += data_spc
784 784
785 785 if data_cspc == None:
786 786 self.__buffer_cspc = None
787 787 else:
788 788 self.__buffer_cspc += data_cspc
789 789
790 790 if data_dc == None:
791 791 self.__buffer_dc = None
792 792 else:
793 793 self.__buffer_dc += data_dc
794 794
795 795 self.__profIndex += 1
796 796 return
797 797
798 798 #Overlapping data
799 799 nChannels, nFFTPoints, nHeis = data_spc.shape
800 800 data_spc = numpy.reshape(data_spc, (1, nChannels, nFFTPoints, nHeis))
801 data_cspc = numpy.reshape(data_cspc, (1, -1, nFFTPoints, nHeis))
802 data_dc = numpy.reshape(data_dc, (1, -1, nHeis))
801 if data_cspc != None:
802 data_cspc = numpy.reshape(data_cspc, (1, -1, nFFTPoints, nHeis))
803 if data_dc != None:
804 data_dc = numpy.reshape(data_dc, (1, -1, nHeis))
803 805
804 806 #If the buffer is empty then it takes the data value
805 807 if self.__buffer_spc == None:
806 self.__buffer_spc = data_spc.copy()
808 self.__buffer_spc = data_spc
807 809
808 810 if data_cspc == None:
809 811 self.__buffer_cspc = None
810 812 else:
811 self.__buffer_cspc += data_cspc.copy()
813 self.__buffer_cspc += data_cspc
812 814
813 815 if data_dc == None:
814 816 self.__buffer_dc = None
815 817 else:
816 self.__buffer_dc += data_dc.copy()
818 self.__buffer_dc += data_dc
817 819
818 820 self.__profIndex += 1
819 821 return
820 822
821 823 #If the buffer length is lower than n then stakcing the data value
822 824 if self.__profIndex < self.n:
823 825 self.__buffer_spc = numpy.vstack((self.__buffer_spc, data_spc))
824 826
825 if self.__buffer_cspc != None:
827 if data_cspc != None:
826 828 self.__buffer_cspc = numpy.vstack((self.__buffer_cspc, data_cspc))
827 829
828 if self.__buffer_dc != None:
830 if data_dc != None:
829 831 self.__buffer_dc = numpy.vstack((self.__buffer_dc, data_dc))
830 832
831 833 self.__profIndex += 1
832 834 return
833 835
834 836 #If the buffer length is equal to n then replacing the last buffer value with the data value
835 837 self.__buffer_spc = numpy.roll(self.__buffer_spc, -1, axis=0)
836 838 self.__buffer_spc[self.n-1] = data_spc
837 839
838 self.__buffer_cspc = numpy.roll(self.__buffer_cspc, -1, axis=0)
839 self.__buffer_cspc[self.n-1] = data_cspc
840 if data_cspc != None:
841 self.__buffer_cspc = numpy.roll(self.__buffer_cspc, -1, axis=0)
842 self.__buffer_cspc[self.n-1] = data_cspc
840 843
841 self.__buffer_dc = numpy.roll(self.__buffer_dc, -1, axis=0)
842 self.__buffer_dc[self.n-1] = data_dc
844 if data_dc != None:
845 self.__buffer_dc = numpy.roll(self.__buffer_dc, -1, axis=0)
846 self.__buffer_dc[self.n-1] = data_dc
843 847
844 848 self.__profIndex = self.n
845 849 return
846 850
847 851
848 852 def pushData(self):
849 853 """
850 854 Return the sum of the last profiles and the profiles used in the sum.
851 855
852 856 Affected:
853 857
854 858 self.__profileIndex
855 859
856 860 """
857 861 data_spc = None
858 862 data_cspc = None
859 863 data_dc = None
860 864
861 865 if not self.__withOverapping:
862 866 data_spc = self.__buffer_spc
863 867 data_cspc = self.__buffer_cspc
864 868 data_dc = self.__buffer_dc
865 869
866 870 n = self.__profIndex
867 871
868 872 self.__buffer_spc = 0
869 873 self.__buffer_cspc = 0
870 874 self.__buffer_dc = 0
871 875 self.__profIndex = 0
872 876
873 877 return data_spc, data_cspc, data_dc, n
874 878
875 879 #Integration with Overlapping
876 880 data_spc = numpy.sum(self.__buffer_spc, axis=0)
877 881
878 882 if self.__buffer_cspc != None:
879 883 data_cspc = numpy.sum(self.__buffer_cspc, axis=0)
880 884
881 885 if self.__buffer_dc != None:
882 886 data_dc = numpy.sum(self.__buffer_dc, axis=0)
883 887
884 888 n = self.__profIndex
885 889
886 890 return data_spc, data_cspc, data_dc, n
887 891
888 892 def byProfiles(self, *args):
889 893
890 894 self.__dataReady = False
891 895 avgdata_spc = None
892 896 avgdata_cspc = None
893 897 avgdata_dc = None
894 898 n = None
895 899
896 900 self.putData(*args)
897 901
898 902 if self.__profIndex == self.n:
899 903
900 904 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
901 905 self.__dataReady = True
902 906
903 907 return avgdata_spc, avgdata_cspc, avgdata_dc
904 908
905 909 def byTime(self, datatime, *args):
906 910
907 911 self.__dataReady = False
908 912 avgdata_spc = None
909 913 avgdata_cspc = None
910 914 avgdata_dc = None
911 915 n = None
912 916
913 917 self.putData(*args)
914 918
915 919 if (datatime - self.__initime) >= self.__integrationtime:
916 920 avgdata_spc, avgdata_cspc, avgdata_dc, n = self.pushData()
917 921 self.n = n
918 922 self.__dataReady = True
919 923
920 924 return avgdata_spc, avgdata_cspc, avgdata_dc
921 925
922 926 def integrate(self, datatime, *args):
923 927
924 928 if self.__initime == None:
925 929 self.__initime = datatime
926 930
927 931 if self.__byTime:
928 932 avgdata_spc, avgdata_cspc, avgdata_dc = self.byTime(datatime, *args)
929 933 else:
930 934 avgdata_spc, avgdata_cspc, avgdata_dc = self.byProfiles(*args)
931 935
932 936 self.__lastdatatime = datatime
933 937
934 938 if avgdata_spc == None:
935 939 return None, None, None, None
936 940
937 941 avgdatatime = self.__initime
938 942
939 943 deltatime = datatime -self.__lastdatatime
940 944
941 945 if not self.__withOverapping:
942 946 self.__initime = datatime
943 947 else:
944 948 self.__initime += deltatime
945 949
946 950 return avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc
947 951
948 952 def run(self, dataOut, n=None, timeInterval=None, overlapping=False):
949 953
950 954 if not self.__isConfig:
951 955 self.setup(n, timeInterval, overlapping)
952 956 self.__isConfig = True
953 957
954 958 avgdatatime, avgdata_spc, avgdata_cspc, avgdata_dc = self.integrate(dataOut.utctime,
955 959 dataOut.data_spc,
956 960 dataOut.data_cspc,
957 961 dataOut.data_dc)
958 962
959 963 # dataOut.timeInterval *= n
960 964 dataOut.flagNoData = True
961 965
962 966 if self.__dataReady:
963 967 dataOut.data_spc = avgdata_spc
964 968 dataOut.data_cspc = avgdata_cspc
965 969 dataOut.data_dc = avgdata_dc
966 970
967 971 dataOut.nIncohInt *= self.n
968 972 dataOut.utctime = avgdatatime
969 973 dataOut.timeInterval = dataOut.ippSeconds * dataOut.nCohInt * dataOut.nIncohInt * dataOut.nFFTPoints
970 974 dataOut.flagNoData = False
971 975 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now