##// END OF EJS Templates
Signal Chain GUI updated:...
Miguel Valdez -
r587:d717921bf643
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,1051 +1,1061
1 1 '''
2 2 Created on September , 2012
3 3 @author:
4 4 '''
5 5 from xml.etree.ElementTree import Element, SubElement
6 6 from xml.etree import ElementTree as ET
7 7 from xml.dom import minidom
8 8
9 9 #import datetime
10 10 from model import *
11 11
12 try:
13 from gevent import sleep
14 except:
15 from time import sleep
16
12 17 import ast
13 18
14 19 def prettify(elem):
15 20 """Return a pretty-printed XML string for the Element.
16 21 """
17 22 rough_string = ET.tostring(elem, 'utf-8')
18 23 reparsed = minidom.parseString(rough_string)
19 24 return reparsed.toprettyxml(indent=" ")
20 25
21 26 class ParameterConf():
22 27
23 28 id = None
24 29 name = None
25 30 value = None
26 31 format = None
27 32
28 33 __formated_value = None
29 34
30 35 ELEMENTNAME = 'Parameter'
31 36
32 37 def __init__(self):
33 38
34 39 self.format = 'str'
35 40
36 41 def getElementName(self):
37 42
38 43 return self.ELEMENTNAME
39 44
40 45 def getValue(self):
41 46
42 47 if self.__formated_value != None:
43 48
44 49 return self.__formated_value
45
50
46 51 value = self.value
47 52
48 53 if self.format == 'bool':
49 54 value = int(value)
50 55
51 56 if self.format == 'list':
52 57 strList = value.split(',')
53 58
54 59 self.__formated_value = strList
55 60
56 61 return self.__formated_value
57 62
58 63 if self.format == 'intlist':
59 64 """
60 65 Example:
61 66 value = (0,1,2)
62 67 """
63 68 value = value.replace('(', '')
64 69 value = value.replace(')', '')
65 70
66 71 value = value.replace('[', '')
67 72 value = value.replace(']', '')
68 73
69 74 strList = value.split(',')
70 75 intList = [int(x) for x in strList]
71 76
72 77 self.__formated_value = intList
73 78
74 79 return self.__formated_value
75 80
76 81 if self.format == 'floatlist':
77 82 """
78 83 Example:
79 84 value = (0.5, 1.4, 2.7)
80 85 """
81 86
82 87 value = value.replace('(', '')
83 88 value = value.replace(')', '')
84 89
85 90 value = value.replace('[', '')
86 91 value = value.replace(']', '')
87 92
88 93 strList = value.split(',')
89 94 floatList = [float(x) for x in strList]
90 95
91 96 self.__formated_value = floatList
92 97
93 98 return self.__formated_value
94 99
95 100 if self.format == 'date':
96 101 strList = value.split('/')
97 102 intList = [int(x) for x in strList]
98 103 date = datetime.date(intList[0], intList[1], intList[2])
99 104
100 105 self.__formated_value = date
101 106
102 107 return self.__formated_value
103 108
104 109 if self.format == 'time':
105 110 strList = value.split(':')
106 111 intList = [int(x) for x in strList]
107 112 time = datetime.time(intList[0], intList[1], intList[2])
108 113
109 114 self.__formated_value = time
110 115
111 116 return self.__formated_value
112 117
113 118 if self.format == 'pairslist':
114 119 """
115 120 Example:
116 121 value = (0,1),(1,2)
117 122 """
118 123
119 124 value = value.replace('(', '')
120 125 value = value.replace(')', '')
121 126
122 127 value = value.replace('[', '')
123 128 value = value.replace(']', '')
124 129
125 130 strList = value.split(',')
126 131 intList = [int(item) for item in strList]
127 132 pairList = []
128 133 for i in range(len(intList)/2):
129 134 pairList.append((intList[i*2], intList[i*2 + 1]))
130 135
131 136 self.__formated_value = pairList
132 137
133 138 return self.__formated_value
134 139
135 140 if self.format == 'multilist':
136 141 """
137 142 Example:
138 143 value = (0,1,2),(3,4,5)
139 144 """
140 145 multiList = ast.literal_eval(value)
141 146
142 147 self.__formated_value = multiList
143 148
144 149 return self.__formated_value
145 150
146 151 format_func = eval(self.format)
147 152
148 153 self.__formated_value = format_func(value)
149 154
150 155 return self.__formated_value
151 156
152 157 def setup(self, id, name, value, format='str'):
153 158
154 159 self.id = id
155 160 self.name = name
156 161 self.value = str(value)
157 162 self.format = str.lower(format)
158 163
159 164 def update(self, name, value, format='str'):
160 165
161 166 self.name = name
162 167 self.value = str(value)
163 168 self.format = format
164 169
165 170 def makeXml(self, opElement):
166 171
167 172 parmElement = SubElement(opElement, self.ELEMENTNAME)
168 173 parmElement.set('id', str(self.id))
169 174 parmElement.set('name', self.name)
170 175 parmElement.set('value', self.value)
171 176 parmElement.set('format', self.format)
172 177
173 178 def readXml(self, parmElement):
174 179
175 180 self.id = parmElement.get('id')
176 181 self.name = parmElement.get('name')
177 182 self.value = parmElement.get('value')
178 183 self.format = str.lower(parmElement.get('format'))
179 184
180 185 #Compatible with old signal chain version
181 186 if self.format == 'int' and self.name == 'idfigure':
182 187 self.name = 'id'
183 188
184 189 def printattr(self):
185 190
186 191 print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format)
187 192
188 193 class OperationConf():
189 194
190 195 id = None
191 196 name = None
192 197 priority = None
193 198 type = None
194 199
195 200 parmConfObjList = []
196 201
197 202 ELEMENTNAME = 'Operation'
198 203
199 204 def __init__(self):
200 205
201 206 self.id = 0
202 207 self.name = None
203 208 self.priority = None
204 209 self.type = 'self'
205 210
206 211
207 212 def __getNewId(self):
208 213
209 214 return int(self.id)*10 + len(self.parmConfObjList) + 1
210 215
211 216 def getElementName(self):
212 217
213 218 return self.ELEMENTNAME
214 219
215 220 def getParameterObjList(self):
216 221
217 222 return self.parmConfObjList
218 223
219 224 def getParameterObj(self, parameterName):
220 225
221 226 for parmConfObj in self.parmConfObjList:
222 227
223 228 if parmConfObj.name != parameterName:
224 229 continue
225 230
226 231 return parmConfObj
227 232
228 233 return None
229 234
230 235 def getParameterObjfromValue(self,parameterValue):
231 236 for parmConfObj in self.parmConfObjList:
232 237
233 238 if parmConfObj.getValue() != parameterValue:
234 239 continue
235 240
236 241 return parmConfObj.getValue()
237 242
238 243 return None
239 244
240 245 def getParameterValue(self, parameterName):
241 246
242 247 parameterObj = self.getParameterObj(parameterName)
243 248 value = parameterObj.getValue()
244 249
245 250 return value
246 251
247 252 def setup(self, id, name, priority, type):
248 253
249 254 self.id = id
250 255 self.name = name
251 256 self.type = type
252 257 self.priority = priority
253 258
254 259 self.parmConfObjList = []
255 260
256 261 def removeParameters(self):
257 262
258 263 for obj in self.parmConfObjList:
259 264 del obj
260 265
261 266 self.parmConfObjList = []
262 267
263 268 def addParameter(self, name, value, format='str'):
264 269
265 270 id = self.__getNewId()
266 271
267 272 parmConfObj = ParameterConf()
268 273 parmConfObj.setup(id, name, value, format)
269 274
270 275 self.parmConfObjList.append(parmConfObj)
271 276
272 277 return parmConfObj
273 278
274 279 def changeParameter(self, name, value, format='str'):
275 280
276 281 parmConfObj = self.getParameterObj(name)
277 282 parmConfObj.update(name, value, format)
278 283
279 284 return parmConfObj
280 285
281 286 def makeXml(self, upElement):
282 287
283 288 opElement = SubElement(upElement, self.ELEMENTNAME)
284 289 opElement.set('id', str(self.id))
285 290 opElement.set('name', self.name)
286 291 opElement.set('type', self.type)
287 292 opElement.set('priority', str(self.priority))
288 293
289 294 for parmConfObj in self.parmConfObjList:
290 295 parmConfObj.makeXml(opElement)
291 296
292 297 def readXml(self, opElement):
293 298
294 299 self.id = opElement.get('id')
295 300 self.name = opElement.get('name')
296 301 self.type = opElement.get('type')
297 302 self.priority = opElement.get('priority')
298 303
299 304 #Compatible with old signal chain version
300 305 #Use of 'run' method instead 'init'
301 306 if self.type == 'self' and self.name == 'init':
302 307 self.name = 'run'
303 308
304 309 self.parmConfObjList = []
305 310
306 311 parmElementList = opElement.getiterator(ParameterConf().getElementName())
307 312
308 313 for parmElement in parmElementList:
309 314 parmConfObj = ParameterConf()
310 315 parmConfObj.readXml(parmElement)
311 316
312 317 #Compatible with old signal chain version
313 318 #If an 'plot' OPERATION is found, changes name operation by the value of its type PARAMETER
314 319 if self.type != 'self' and self.name == 'Plot':
315 320 if parmConfObj.format == 'str' and parmConfObj.name == 'type':
316 321 self.name = parmConfObj.value
317 322 continue
318 323
319 324 self.parmConfObjList.append(parmConfObj)
320 325
321 326 def printattr(self):
322 327
323 328 print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME,
324 329 self.id,
325 330 self.name,
326 331 self.type,
327 332 self.priority)
328 333
329 334 for parmConfObj in self.parmConfObjList:
330 335 parmConfObj.printattr()
331 336
332 337 def createObject(self):
333 338
334 339 if self.type == 'self':
335 340 raise ValueError, "This operation type cannot be created"
336 341
337 342 if self.type == 'external' or self.type == 'other':
338 343 className = eval(self.name)
339 344 opObj = className()
340 345
341 346 return opObj
342 347
343 348 class ProcUnitConf():
344 349
345 350 id = None
346 351 name = None
347 352 datatype = None
348 353 inputId = None
349 354 parentId = None
350 355
351 356 opConfObjList = []
352 357
353 358 procUnitObj = None
354 359 opObjList = []
355 360
356 361 ELEMENTNAME = 'ProcUnit'
357 362
358 363 def __init__(self):
359 364
360 365 self.id = None
361 366 self.datatype = None
362 367 self.name = None
363 368 self.inputId = None
364 369
365 370 self.opConfObjList = []
366 371
367 372 self.procUnitObj = None
368 373 self.opObjDict = {}
369 374
370 375 def __getPriority(self):
371 376
372 377 return len(self.opConfObjList)+1
373 378
374 379 def __getNewId(self):
375 380
376 381 return int(self.id)*10 + len(self.opConfObjList) + 1
377 382
378 383 def getElementName(self):
379 384
380 385 return self.ELEMENTNAME
381 386
382 387 def getId(self):
383 388
384 389 return str(self.id)
385 390
386 391 def getInputId(self):
387 392
388 393 return str(self.inputId)
389 394
390 395 def getOperationObjList(self):
391 396
392 397 return self.opConfObjList
393 398
394 399 def getOperationObj(self, name=None):
395 400
396 401 for opConfObj in self.opConfObjList:
397 402
398 403 if opConfObj.name != name:
399 404 continue
400 405
401 406 return opConfObj
402 407
403 408 return None
404 409
405 410 def getOpObjfromParamValue(self,value=None):
406 411
407 412 for opConfObj in self.opConfObjList:
408 413 if opConfObj.getParameterObjfromValue(parameterValue=value) != value:
409 414 continue
410 415 return opConfObj
411 416 return None
412 417
413 418 def getProcUnitObj(self):
414 419
415 420 return self.procUnitObj
416 421
417 422 def setup(self, id, name, datatype, inputId, parentId=None):
418 423
419 424 self.id = id
420 425 self.name = name
421 426 self.datatype = datatype
422 427 self.inputId = inputId
423 428 self.parentId = parentId
424 429
425 430 self.opConfObjList = []
426 431
427 432 self.addOperation(name='run', optype='self')
428 433
429 434 def removeOperations(self):
430 435
431 436 for obj in self.opConfObjList:
432 437 del obj
433 438
434 439 self.opConfObjList = []
435 440 self.addOperation(name='run')
436 441
437 442 def addParameter(self, **kwargs):
438 443 '''
439 444 Add parameters to "run" operation
440 445 '''
441 446 opObj = self.opConfObjList[0]
442 447
443 448 opObj.addParameter(**kwargs)
444 449
445 450 return opObj
446 451
447 452 def addOperation(self, name, optype='self'):
448 453
449 454 id = self.__getNewId()
450 455 priority = self.__getPriority()
451 456
452 457 opConfObj = OperationConf()
453 458 opConfObj.setup(id, name=name, priority=priority, type=optype)
454 459
455 460 self.opConfObjList.append(opConfObj)
456 461
457 462 return opConfObj
458 463
459 464 def makeXml(self, procUnitElement):
460 465
461 466 upElement = SubElement(procUnitElement, self.ELEMENTNAME)
462 467 upElement.set('id', str(self.id))
463 468 upElement.set('name', self.name)
464 469 upElement.set('datatype', self.datatype)
465 470 upElement.set('inputId', str(self.inputId))
466 471
467 472 for opConfObj in self.opConfObjList:
468 473 opConfObj.makeXml(upElement)
469 474
470 475 def readXml(self, upElement):
471 476
472 477 self.id = upElement.get('id')
473 478 self.name = upElement.get('name')
474 479 self.datatype = upElement.get('datatype')
475 480 self.inputId = upElement.get('inputId')
476 481
477 482 self.opConfObjList = []
478 483
479 484 opElementList = upElement.getiterator(OperationConf().getElementName())
480 485
481 486 for opElement in opElementList:
482 487 opConfObj = OperationConf()
483 488 opConfObj.readXml(opElement)
484 489 self.opConfObjList.append(opConfObj)
485 490
486 491 def printattr(self):
487 492
488 493 print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME,
489 494 self.id,
490 495 self.name,
491 496 self.datatype,
492 497 self.inputId)
493 498
494 499 for opConfObj in self.opConfObjList:
495 500 opConfObj.printattr()
496 501
497 502 def createObjects(self):
498 503
499 504 className = eval(self.name)
500 505 procUnitObj = className()
501 506
502 507 for opConfObj in self.opConfObjList:
503 508
504 509 if opConfObj.type == 'self':
505 510 continue
506 511
507 512 opObj = opConfObj.createObject()
508 513
509 514 self.opObjDict[opConfObj.id] = opObj
510 515 procUnitObj.addOperation(opObj, opConfObj.id)
511 516
512 517 self.procUnitObj = procUnitObj
513 518
514 519 return procUnitObj
515 520
516 521 def run(self):
517 522
518 523 finalSts = False
519 524
520 525 for opConfObj in self.opConfObjList:
521 526
522 527 kwargs = {}
523 528 for parmConfObj in opConfObj.getParameterObjList():
524 529 if opConfObj.name == 'run' and parmConfObj.name == 'datatype':
525 530 continue
526 531
527 532 kwargs[parmConfObj.name] = parmConfObj.getValue()
528 533
529 534 #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id)
530 535 sts = self.procUnitObj.call(opType = opConfObj.type,
531 536 opName = opConfObj.name,
532 537 opId = opConfObj.id,
533 538 **kwargs)
534 539 finalSts = finalSts or sts
535 540
536 541 return finalSts
537 542
538 543 def close(self):
539 544
540 545 for opConfObj in self.opConfObjList:
541 546 if opConfObj.type == 'self':
542 547 continue
543 548
544 549 opObj = self.procUnitObj.getOperationObj(opConfObj.id)
545 550 opObj.close()
546 551
547 552 self.procUnitObj.close()
548 553
549 554 return
550 555
551 556 class ReadUnitConf(ProcUnitConf):
552 557
553 558 path = None
554 559 startDate = None
555 560 endDate = None
556 561 startTime = None
557 562 endTime = None
558 563
559 564 ELEMENTNAME = 'ReadUnit'
560 565
561 566 def __init__(self):
562 567
563 568 self.id = None
564 569 self.datatype = None
565 570 self.name = None
566 571 self.inputId = 0
567 572
568 573 self.opConfObjList = []
569 574 self.opObjList = []
570 575
571 576 def getElementName(self):
572 577
573 578 return self.ELEMENTNAME
574 579
575 580 def setup(self, id, name, datatype, path, startDate="", endDate="", startTime="", endTime="", parentId=None, **kwargs):
576 581
577 582 self.id = id
578 583 self.name = name
579 584 self.datatype = datatype
580 585
581 586 self.path = path
582 587 self.startDate = startDate
583 588 self.endDate = endDate
584 589 self.startTime = startTime
585 590 self.endTime = endTime
586 591
587 592 self.addRunOperation(**kwargs)
588 593
589 594 def update(self, datatype, path, startDate, endDate, startTime, endTime, parentId=None, **kwargs):
590 595
591 596 self.datatype = datatype
592 597 self.path = path
593 598 self.startDate = startDate
594 599 self.endDate = endDate
595 600 self.startTime = startTime
596 601 self.endTime = endTime
597 602
598 603 self.updateRunOperation(**kwargs)
599 604
600 605 def addRunOperation(self, **kwargs):
601 606
602 607 opObj = self.addOperation(name = 'run', optype = 'self')
603 608
604 609 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
605 610 opObj.addParameter(name='path' , value=self.path, format='str')
606 611 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
607 612 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
608 613 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
609 614 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
610 615
611 616 for key, value in kwargs.items():
612 617 opObj.addParameter(name=key, value=value, format=type(value).__name__)
613 618
614 619 return opObj
615 620
616 621 def updateRunOperation(self, **kwargs):
617 622
618 623 opObj = self.getOperationObj(name = 'run')
619 624 opObj.removeParameters()
620 625
621 626 opObj.addParameter(name='datatype' , value=self.datatype, format='str')
622 627 opObj.addParameter(name='path' , value=self.path, format='str')
623 628 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
624 629 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
625 630 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
626 631 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
627 632
628 633 for key, value in kwargs.items():
629 634 opObj.addParameter(name=key, value=value, format=type(value).__name__)
630 635
631 636 return opObj
632 637
633 638 class Project():
634 639
635 640 id = None
636 641 name = None
637 642 description = None
638 643 # readUnitConfObjList = None
639 644 procUnitConfObjDict = None
640 645
641 646 ELEMENTNAME = 'Project'
642 647
643 648 def __init__(self, control=None, dataq=None):
644 649
645 650 self.id = None
646 651 self.name = None
647 652 self.description = None
648 653
649 654 self.procUnitConfObjDict = {}
650 655
651 656 #global data_q
652 657 #data_q = dataq
653 658
654 659 if control==None:
655 660 control = {}
656 661 control['stop'] = False
657 662 control['pause'] = False
658 663
659 664 self.control = control
660 665
661 666 def __getNewId(self):
662 667
663 668 id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1
664 669
665 670 return str(id)
666 671
667 672 def getElementName(self):
668 673
669 674 return self.ELEMENTNAME
670 675
671 676 def getId(self):
672 677
673 678 return self.id
674 679
675 680 def setup(self, id, name, description):
676 681
677 682 self.id = id
678 683 self.name = name
679 684 self.description = description
680 685
681 686 def update(self, name, description):
682 687
683 688 self.name = name
684 689 self.description = description
685 690
686 691 def addReadUnit(self, datatype=None, name=None, **kwargs):
687 692
688 693 #Compatible with old signal chain version
689 694 if datatype==None and name==None:
690 695 raise ValueError, "datatype or name should be defined"
691 696
692 697 if name==None:
693 698 if 'Reader' in datatype:
694 699 name = datatype
695 700 else:
696 701 name = '%sReader' %(datatype)
697 702
698 703 if datatype==None:
699 704 datatype = name.replace('Reader','')
700 705
701 706 id = self.__getNewId()
702 707
703 708 readUnitConfObj = ReadUnitConf()
704 709 readUnitConfObj.setup(id, name, datatype, parentId=self.id, **kwargs)
705 710
706 711 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
707 712
708 713 return readUnitConfObj
709 714
710 715 def addProcUnit(self, inputId=0, datatype=None, name=None):
711 716
712 717 #Compatible with old signal chain version
713 718 if datatype==None and name==None:
714 719 raise ValueError, "datatype or name should be defined"
715 720
716 721 if name==None:
717 722 if 'Proc' in datatype:
718 723 name = datatype
719 724 else:
720 725 name = '%sProc' %(datatype)
721 726
722 727 if datatype==None:
723 728 datatype = name.replace('Proc','')
724 729
725 730 id = self.__getNewId()
726 731
727 732 procUnitConfObj = ProcUnitConf()
728 733 procUnitConfObj.setup(id, name, datatype, inputId, parentId=self.id)
729 734
730 735 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
731 736
732 737 return procUnitConfObj
733 738
739 def removeProcUnit(self, id):
740
741 if id in self.procUnitConfObjDict.keys():
742 self.procUnitConfObjDict.pop(id)
743
734 744 def getReadUnitId(self):
735 745
736 746 readUnitConfObj = self.getReadUnitObj()
737 747
738 748 return readUnitConfObj.id
739 749
740 750 def getReadUnitObj(self):
741 751
742 752 for obj in self.procUnitConfObjDict.values():
743 753 if obj.getElementName() == "ReadUnit":
744 754 return obj
745 755
746 756 return None
747 757
748 758 def getProcUnitObj(self, id):
749 759
750 760 return self.procUnitConfObjDict[id]
751 761
752 762 def getProcUnitObjByName(self, name):
753 763
754 764 for obj in self.procUnitConfObjDict.values():
755 765 if obj.name == name:
756 766 return obj
757 767
758 768 return None
759 769
760 770 def makeXml(self):
761 771
762 772 projectElement = Element('Project')
763 773 projectElement.set('id', str(self.id))
764 774 projectElement.set('name', self.name)
765 775 projectElement.set('description', self.description)
766 776
767 777 # for readUnitConfObj in self.readUnitConfObjList:
768 778 # readUnitConfObj.makeXml(projectElement)
769 779
770 780 for procUnitConfObj in self.procUnitConfObjDict.values():
771 781 procUnitConfObj.makeXml(projectElement)
772 782
773 783 self.projectElement = projectElement
774 784
775 785 def writeXml(self, filename):
776 786
777 787 self.makeXml()
778 788
779 789 #print prettify(self.projectElement)
780 790
781 791 ElementTree(self.projectElement).write(filename, method='xml')
782 792
783 793 def readXml(self, filename):
784 794
785 795 #tree = ET.parse(filename)
786 796 self.projectElement = None
787 797 # self.readUnitConfObjList = []
788 798 self.procUnitConfObjDict = {}
789 799
790 800 self.projectElement = ElementTree().parse(filename)
791 801
792 802 self.project = self.projectElement.tag
793 803
794 804 self.id = self.projectElement.get('id')
795 805 self.name = self.projectElement.get('name')
796 806 self.description = self.projectElement.get('description')
797 807
798 808 readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName())
799 809
800 810 for readUnitElement in readUnitElementList:
801 811 readUnitConfObj = ReadUnitConf()
802 812 readUnitConfObj.readXml(readUnitElement)
803 813
804 814 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
805 815
806 816 procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName())
807 817
808 818 for procUnitElement in procUnitElementList:
809 819 procUnitConfObj = ProcUnitConf()
810 820 procUnitConfObj.readXml(procUnitElement)
811 821
812 822 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
813 823
814 824 def printattr(self):
815 825
816 826 print "Project[%s]: name = %s, description = %s" %(self.id,
817 827 self.name,
818 828 self.description)
819 829
820 830 # for readUnitConfObj in self.readUnitConfObjList:
821 831 # readUnitConfObj.printattr()
822 832
823 833 for procUnitConfObj in self.procUnitConfObjDict.values():
824 834 procUnitConfObj.printattr()
825 835
826 836 def createObjects(self):
827 837
828 838 # for readUnitConfObj in self.readUnitConfObjList:
829 839 # readUnitConfObj.createObjects()
830 840
831 841 for procUnitConfObj in self.procUnitConfObjDict.values():
832 842 procUnitConfObj.createObjects()
833 843
834 844 def __connect(self, objIN, thisObj):
835 845
836 846 thisObj.setInput(objIN.getOutputObj())
837 847
838 848 def connectObjects(self):
839 849
840 850 for thisPUConfObj in self.procUnitConfObjDict.values():
841 851
842 852 inputId = thisPUConfObj.getInputId()
843 853
844 854 if int(inputId) == 0:
845 855 continue
846 856
847 857 #Get input object
848 858 puConfINObj = self.procUnitConfObjDict[inputId]
849 859 puObjIN = puConfINObj.getProcUnitObj()
850 860
851 861 #Get current object
852 862 thisPUObj = thisPUConfObj.getProcUnitObj()
853 863
854 864 self.__connect(puObjIN, thisPUObj)
855 865
856 866 def run(self):
857 867
858 868 # for readUnitConfObj in self.readUnitConfObjList:
859 869 # readUnitConfObj.run()
860 870 print
861 871 print "*"*40
862 872 print " Starting SIGNAL CHAIN PROCESSING "
863 873 print "*"*40
864 874 print
865 875
866 876 keyList = self.procUnitConfObjDict.keys()
867 877 keyList.sort()
868 878
869 879 while(True):
870 880
871 881 finalSts = False
872 882
873 883 for procKey in keyList:
874 884 # print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id)
875 885
876 886 procUnitConfObj = self.procUnitConfObjDict[procKey]
877 887 sts = procUnitConfObj.run()
878 888 finalSts = finalSts or sts
879 889
880 890 #If every process unit finished so end process
881 891 if not(finalSts):
882 892 print "Every process unit have finished"
883 893 break
884 894
885 895 if self.control['pause']:
886 896 print "Process suspended"
887 897
888 898 while True:
889 time.sleep(0.1)
899 sleep(0.1)
890 900
891 901 if not self.control['pause']:
892 902 break
893 903
894 904 if self.control['stop']:
895 905 break
896 906 print "Process reinitialized"
897 907
898 908 if self.control['stop']:
899 print "Stopping process"
909 print "Process stopped"
900 910 break
901 911
902 912 #Closing every process
903 913 for procKey in keyList:
904 914 procUnitConfObj = self.procUnitConfObjDict[procKey]
905 915 procUnitConfObj.close()
906 916
907 print "Process stopped"
917 print "Process finished"
908 918
909 919 def start(self, filename):
910 920
911 921 self.writeXml(filename)
912 922 self.readXml(filename)
913 923
914 924 self.createObjects()
915 925 self.connectObjects()
916 926 self.run()
917 927
918 928 if __name__ == '__main__':
919 929
920 930 desc = "Segundo Test"
921 931 filename = "schain.xml"
922 932
923 933 controllerObj = Project()
924 934
925 935 controllerObj.setup(id = '191', name='test01', description=desc)
926 936
927 937 readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage',
928 938 path='data/rawdata/',
929 939 startDate='2011/01/01',
930 940 endDate='2012/12/31',
931 941 startTime='00:00:00',
932 942 endTime='23:59:59',
933 943 online=1,
934 944 walk=1)
935 945
936 946 # opObj00 = readUnitConfObj.addOperation(name='printInfo')
937 947
938 948 procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId())
939 949
940 950 opObj10 = procUnitConfObj0.addOperation(name='selectChannels')
941 951 opObj10.addParameter(name='channelList', value='3,4,5', format='intlist')
942 952
943 953 opObj10 = procUnitConfObj0.addOperation(name='selectHeights')
944 954 opObj10.addParameter(name='minHei', value='90', format='float')
945 955 opObj10.addParameter(name='maxHei', value='180', format='float')
946 956
947 957 opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='external')
948 958 opObj12.addParameter(name='n', value='10', format='int')
949 959
950 960 procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId())
951 961 procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int')
952 962 # procUnitConfObj1.addParameter(name='pairList', value='(0,1),(0,2),(1,2)', format='')
953 963
954 964
955 965 opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external')
956 966 opObj11.addParameter(name='idfigure', value='1', format='int')
957 967 opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str')
958 968 opObj11.addParameter(name='zmin', value='40', format='int')
959 969 opObj11.addParameter(name='zmax', value='90', format='int')
960 970 opObj11.addParameter(name='showprofile', value='1', format='int')
961 971
962 972 # opObj11 = procUnitConfObj1.addOperation(name='CrossSpectraPlot', optype='external')
963 973 # opObj11.addParameter(name='idfigure', value='2', format='int')
964 974 # opObj11.addParameter(name='wintitle', value='CrossSpectraPlot', format='str')
965 975 # opObj11.addParameter(name='zmin', value='40', format='int')
966 976 # opObj11.addParameter(name='zmax', value='90', format='int')
967 977
968 978
969 979 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Voltage', inputId=procUnitConfObj0.getId())
970 980 #
971 981 # opObj12 = procUnitConfObj2.addOperation(name='CohInt', optype='external')
972 982 # opObj12.addParameter(name='n', value='2', format='int')
973 983 # opObj12.addParameter(name='overlapping', value='1', format='int')
974 984 #
975 985 # procUnitConfObj3 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj2.getId())
976 986 # procUnitConfObj3.addParameter(name='nFFTPoints', value='32', format='int')
977 987 #
978 988 # opObj11 = procUnitConfObj3.addOperation(name='SpectraPlot', optype='external')
979 989 # opObj11.addParameter(name='idfigure', value='2', format='int')
980 990 # opObj11.addParameter(name='wintitle', value='SpectraPlot1', format='str')
981 991 # opObj11.addParameter(name='zmin', value='40', format='int')
982 992 # opObj11.addParameter(name='zmax', value='90', format='int')
983 993 # opObj11.addParameter(name='showprofile', value='1', format='int')
984 994
985 995 # opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype='external')
986 996 # opObj11.addParameter(name='idfigure', value='10', format='int')
987 997 # opObj11.addParameter(name='wintitle', value='RTI', format='str')
988 998 ## opObj11.addParameter(name='xmin', value='21', format='float')
989 999 ## opObj11.addParameter(name='xmax', value='22', format='float')
990 1000 # opObj11.addParameter(name='zmin', value='40', format='int')
991 1001 # opObj11.addParameter(name='zmax', value='90', format='int')
992 1002 # opObj11.addParameter(name='showprofile', value='1', format='int')
993 1003 # opObj11.addParameter(name='timerange', value=str(60), format='int')
994 1004
995 1005 # opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
996 1006 # opObj10.addParameter(name='channelList', value='0,2,4,6', format='intlist')
997 1007 #
998 1008 # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='external')
999 1009 # opObj12.addParameter(name='n', value='2', format='int')
1000 1010 #
1001 1011 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external')
1002 1012 # opObj11.addParameter(name='idfigure', value='2', format='int')
1003 1013 # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str')
1004 1014 # opObj11.addParameter(name='zmin', value='70', format='int')
1005 1015 # opObj11.addParameter(name='zmax', value='90', format='int')
1006 1016 #
1007 1017 # opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
1008 1018 # opObj10.addParameter(name='channelList', value='2,6', format='intlist')
1009 1019 #
1010 1020 # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='external')
1011 1021 # opObj12.addParameter(name='n', value='2', format='int')
1012 1022 #
1013 1023 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='external')
1014 1024 # opObj11.addParameter(name='idfigure', value='3', format='int')
1015 1025 # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str')
1016 1026 # opObj11.addParameter(name='zmin', value='70', format='int')
1017 1027 # opObj11.addParameter(name='zmax', value='90', format='int')
1018 1028
1019 1029
1020 1030 # opObj12 = procUnitConfObj1.addOperation(name='decoder')
1021 1031 # opObj12.addParameter(name='ncode', value='2', format='int')
1022 1032 # opObj12.addParameter(name='nbauds', value='8', format='int')
1023 1033 # opObj12.addParameter(name='code0', value='001110011', format='int')
1024 1034 # opObj12.addParameter(name='code1', value='001110011', format='int')
1025 1035
1026 1036
1027 1037
1028 1038 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj1.getId())
1029 1039 #
1030 1040 # opObj21 = procUnitConfObj2.addOperation(name='IncohInt', optype='external')
1031 1041 # opObj21.addParameter(name='n', value='2', format='int')
1032 1042 #
1033 1043 # opObj11 = procUnitConfObj2.addOperation(name='SpectraPlot', optype='external')
1034 1044 # opObj11.addParameter(name='idfigure', value='4', format='int')
1035 1045 # opObj11.addParameter(name='wintitle', value='SpectraPlot OBJ 2', format='str')
1036 1046 # opObj11.addParameter(name='zmin', value='70', format='int')
1037 1047 # opObj11.addParameter(name='zmax', value='90', format='int')
1038 1048
1039 1049 print "Escribiendo el archivo XML"
1040 1050
1041 1051 controllerObj.writeXml(filename)
1042 1052
1043 1053 print "Leyendo el archivo XML"
1044 1054 controllerObj.readXml(filename)
1045 1055 #controllerObj.printattr()
1046 1056
1047 1057 controllerObj.createObjects()
1048 1058 controllerObj.connectObjects()
1049 1059 controllerObj.run()
1050 1060
1051 1061 No newline at end of file
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,100 +1,115
1 1 import threading
2 2 import Queue
3 from time import sleep
3 try:
4 from gevent import sleep
5 except:
6 from time import sleep
4 7
5 8 from schainpy.controller import Project
6 9 from command import *
7 10
8 11 class ControllerThread(threading.Thread):
9 12 def __init__(self, filename, data_q=None):
10 13 super(ControllerThread, self).__init__()
11 14 self.filename = filename
12 15 self.data_q = data_q
13 16 self.control = {'stop':False,'pause':False}
14 17
15 18 def stop(self):
16 19 self.control['stop'] = True
17 20
18 21 def pause(self):
19 22 self.control['pause'] = not(self.control['pause'])
20 23
21 24 def run(self):
22 25 self.control['stop'] = False
23 26 self.control['pause'] = False
24 27 self.controllerObj = Project(self.control, self.data_q)
25 28 self.controllerObj.readXml(self.filename)
26 29 self.controllerObj.createObjects()
27 30 self.controllerObj.connectObjects()
28 31 self.controllerObj.run()
29 32
30 33 class CommCtrlProcessThread(threading.Thread):
31 34 """ Implements the threading.Thread interface (start, join, etc.) and
32 35 can be controlled via the cmd_q Queue attribute. Replies are placed in
33 36 the reply_q Queue attribute.
34 37 """
35 38 def __init__(self, cmd_q=Queue.Queue(), reply_q=Queue.Queue()):
36 39 super(CommCtrlProcessThread, self).__init__()
37 40 self.cmd_q = cmd_q
38 41 # self.reply_q = reply_q
39 42
40 43 # self.print_q = Queue.Queue()
41 44 # self.data_q = Queue.Queue()
42 45
43 46
44 47 self.alive = threading.Event()
45 48 self.setDaemon(True)
46 49 self.alive.set()
47 50 self.socket = None
48 51
49 52 self.socketIO = None
50 53 self.mySocket = None
51 54
55 self.controllerObj = None
52 56
53 57 self.handlers = {
54 58 ProcessCommand.PROCESS: self._handle_ioPROCESSTHREAD,
55 59 ProcessCommand.MESSAGE: self._handle_ioMESSAGE,
56 60 ProcessCommand.DATA: self._handle_ioDATA,
57 61 ProcessCommand.STOP: self._handle_ioSTOP,
58 62 ProcessCommand.PAUSE: self._handle_ioPAUSE
59 63 }
60 64
61 65 def run(self):
62 66
63 67 while self.alive.isSet():
64 68 try:
65 69 cmd = self.cmd_q.get(True, 0.1)
66 70 self.handlers[cmd.type](cmd)
67 71 except Queue.Empty as e:
68 72 sleep(0.1)
69 73 continue
70 74
71
75 def isRunning(self):
76
77 if self.controllerObj == None:
78 return False
79
80 if self.controllerObj.isAlive():
81 return True
82
83 return False
84
72 85 def _handle_ioPROCESSTHREAD(self, cmd):
73 86 filename = cmd.data
74 87 self.controllerObj = ControllerThread(filename=filename)
75 88 self.controllerObj.start()
76 89
77 90 def _handle_ioPAUSE(self, cmd):
78 91 self.controllerObj.pause()
79 92
80 93 def _handle_ioSTOP(self, cmd):
81 94 self.controllerObj.stop()
95 self.controllerObj.join()
96 # print "Process thread finished"
82 97
83 98 def _handle_ioDATA(self, cmd):
84 99 self.reply_q.put(self._success_reply_data(data=cmd.data))
85 100
86 101 def _handle_ioMESSAGE(self, cmd):
87 102 self.reply_q.put(self._success_reply_message(data=cmd.data))
88 103
89 104 def _success_reply_data(self, data=None):
90 105 return ClientReply(ClientReply.DATA, data)
91 106
92 107 def _success_reply_message(self, data=None):
93 108 return ClientReply(ClientReply.MESSAGE, data)
94 109
95 110 def join(self, timeout=None):
96 111 self.alive.clear()
97 112 threading.Thread.join(self, timeout)
98 113
99 114
100 115 No newline at end of file
@@ -1,83 +1,90
1 1 import numpy
2 2 import copy
3 3
4 4 class Beam:
5 5 def __init__(self):
6 6 self.codeList = []
7 7 self.azimuthList = []
8 8 self.zenithList = []
9 9
10 10
11 11 class AMISR:
12 12 def __init__(self):
13 13 self.flagNoData = True
14 14 self.data = None
15 15 self.utctime = None
16 16 self.type = "AMISR"
17 17
18 18 #propiedades para compatibilidad con Voltages
19 19 self.timeZone = 0#timezone like jroheader, difference in minutes between UTC and localtime
20 20 self.dstFlag = 0#self.dataIn.dstFlag
21 21 self.errorCount = 0#self.dataIn.errorCount
22 22 self.useLocalTime = True#self.dataIn.useLocalTime
23 23
24 24 self.radarControllerHeaderObj = None#self.dataIn.radarControllerHeaderObj.copy()
25 25 self.systemHeaderObj = None#self.dataIn.systemHeaderObj.copy()
26 26 self.channelList = [0]#self.dataIn.channelList esto solo aplica para el caso de AMISR
27 27 self.dtype = numpy.dtype([('real','<f4'),('imag','<f4')])
28 28
29 29 self.flagDiscontinuousBlock = None#self.dataIn.flagDiscontinuousBlock
30 30 #self.utctime = #self.firstdatatime
31 31 self.flagDecodeData = None#self.dataIn.flagDecodeData #asumo q la data esta decodificada
32 32 self.flagDeflipData = None#self.dataIn.flagDeflipData #asumo q la data esta sin flip
33 33
34 34 self.nCohInt = 1#self.dataIn.nCohInt
35 35 self.nIncohInt = 1
36 36 self.ippSeconds = None#self.dataIn.ippSeconds, segun el filename/Setup/Tufile
37 37 self.windowOfFilter = None#self.dataIn.windowOfFilter
38 38
39 39 self.timeInterval = None#self.dataIn.timeInterval*self.dataOut.nFFTPoints*self.dataOut.nIncohInt
40 40 self.frequency = None#self.dataIn.frequency
41 41 self.realtime = 0#self.dataIn.realtime
42 42
43 43 #actualizar en la lectura de datos
44 44 self.heightList = None#self.dataIn.heightList
45 45 self.nProfiles = None#Number of samples or nFFTPoints
46 46 self.nRecords = None
47 47 self.nBeams = None
48 48 self.nBaud = None#self.dataIn.nBaud
49 49 self.nCode = None#self.dataIn.nCode
50 50 self.code = None#self.dataIn.code
51 51
52 52 #consideracion para los Beams
53 53 self.beamCodeDict = None
54 54 self.beamRangeDict = None
55 55 self.beamcode = None
56 56 self.azimuth = None
57 57 self.zenith = None
58 58 self.gain = None
59 59
60 60 self.npulseByFrame = None
61 61
62 62 self.profileIndex = None
63 63
64 64 self.beam = Beam()
65 65
66 66 def copy(self, inputObj=None):
67 67
68 68 if inputObj == None:
69 69 return copy.deepcopy(self)
70 70
71 71 for key in inputObj.__dict__.keys():
72 72 self.__dict__[key] = inputObj.__dict__[key]
73 73
74 74 def getNHeights(self):
75 75
76 76 return len(self.heightList)
77 77
78 78
79 79 def isEmpty(self):
80 80
81 81 return self.flagNoData
82
83 def getTimeInterval(self):
84
85 timeInterval = self.ippSeconds * self.nCohInt
86
87 return timeInterval
82 88
89 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
83 90 nHeights = property(getNHeights, "I'm the 'nHeights' property.") No newline at end of file
@@ -1,1102 +1,1115
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
13 13 def getNumpyDtype(dataTypeCode):
14 14
15 15 if dataTypeCode == 0:
16 16 numpyDtype = numpy.dtype([('real','<i1'),('imag','<i1')])
17 17 elif dataTypeCode == 1:
18 18 numpyDtype = numpy.dtype([('real','<i2'),('imag','<i2')])
19 19 elif dataTypeCode == 2:
20 20 numpyDtype = numpy.dtype([('real','<i4'),('imag','<i4')])
21 21 elif dataTypeCode == 3:
22 22 numpyDtype = numpy.dtype([('real','<i8'),('imag','<i8')])
23 23 elif dataTypeCode == 4:
24 24 numpyDtype = numpy.dtype([('real','<f4'),('imag','<f4')])
25 25 elif dataTypeCode == 5:
26 26 numpyDtype = numpy.dtype([('real','<f8'),('imag','<f8')])
27 27 else:
28 28 raise ValueError, 'dataTypeCode was not defined'
29 29
30 30 return numpyDtype
31 31
32 32 def getDataTypeCode(numpyDtype):
33 33
34 34 if numpyDtype == numpy.dtype([('real','<i1'),('imag','<i1')]):
35 35 datatype = 0
36 36 elif numpyDtype == numpy.dtype([('real','<i2'),('imag','<i2')]):
37 37 datatype = 1
38 38 elif numpyDtype == numpy.dtype([('real','<i4'),('imag','<i4')]):
39 39 datatype = 2
40 40 elif numpyDtype == numpy.dtype([('real','<i8'),('imag','<i8')]):
41 41 datatype = 3
42 42 elif numpyDtype == numpy.dtype([('real','<f4'),('imag','<f4')]):
43 43 datatype = 4
44 44 elif numpyDtype == numpy.dtype([('real','<f8'),('imag','<f8')]):
45 45 datatype = 5
46 46 else:
47 47 datatype = None
48 48
49 49 return datatype
50 50
51 51 def hildebrand_sekhon(data, navg):
52 52 """
53 53 This method is for the objective determination of the noise level in Doppler spectra. This
54 54 implementation technique is based on the fact that the standard deviation of the spectral
55 55 densities is equal to the mean spectral density for white Gaussian noise
56 56
57 57 Inputs:
58 58 Data : heights
59 59 navg : numbers of averages
60 60
61 61 Return:
62 62 -1 : any error
63 63 anoise : noise's level
64 64 """
65 65
66 66 sortdata = numpy.sort(data,axis=None)
67 67 lenOfData = len(sortdata)
68 68 nums_min = lenOfData/10
69 69
70 70 if (lenOfData/10) > 2:
71 71 nums_min = lenOfData/10
72 72 else:
73 73 nums_min = 2
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 j += 1
90 90
91 91 if j > nums_min:
92 92 rtest = float(j)/(j-1) + 1.0/navg
93 93 if ((sumq*j) > (rtest*sump**2)):
94 94 j = j - 1
95 95 sump = sump - sortdata[j]
96 96 sumq = sumq - sortdata[j]**2
97 97 cont = 0
98 98
99 99 lnoise = sump /j
100 100 stdv = numpy.sqrt((sumq - lnoise**2)/(j - 1))
101 101 return lnoise
102 102
103 103 class Beam:
104 104 def __init__(self):
105 105 self.codeList = []
106 106 self.azimuthList = []
107 107 self.zenithList = []
108 108
109 109 class GenericData(object):
110 110
111 111 flagNoData = True
112 112
113 113 def __init__(self):
114 114
115 115 raise ValueError, "This class has not been implemented"
116 116
117 117 def copy(self, inputObj=None):
118 118
119 119 if inputObj == None:
120 120 return copy.deepcopy(self)
121 121
122 122 for key in inputObj.__dict__.keys():
123 123 self.__dict__[key] = inputObj.__dict__[key]
124 124
125 125 def deepcopy(self):
126 126
127 127 return copy.deepcopy(self)
128 128
129 129 def isEmpty(self):
130 130
131 131 return self.flagNoData
132 132
133 133 class JROData(GenericData):
134 134
135 135 # m_BasicHeader = BasicHeader()
136 136 # m_ProcessingHeader = ProcessingHeader()
137 137
138 138 systemHeaderObj = SystemHeader()
139 139
140 140 radarControllerHeaderObj = RadarControllerHeader()
141 141
142 142 # data = None
143 143
144 144 type = None
145 145
146 146 datatype = None #dtype but in string
147 147
148 148 # dtype = None
149 149
150 150 # nChannels = None
151 151
152 152 # nHeights = None
153 153
154 154 nProfiles = None
155 155
156 156 heightList = None
157 157
158 158 channelList = None
159 159
160 160 flagDiscontinuousBlock = False
161 161
162 162 useLocalTime = False
163 163
164 164 utctime = None
165 165
166 166 timeZone = None
167 167
168 168 dstFlag = None
169 169
170 170 errorCount = None
171 171
172 172 blocksize = None
173 173
174 174 # nCode = None
175 175 #
176 176 # nBaud = None
177 177 #
178 178 # code = None
179 179
180 180 flagDecodeData = False #asumo q la data no esta decodificada
181 181
182 182 flagDeflipData = False #asumo q la data no esta sin flip
183 183
184 184 flagShiftFFT = False
185 185
186 186 # ippSeconds = None
187 187
188 188 # timeInterval = None
189 189
190 190 nCohInt = None
191 191
192 192 # noise = None
193 193
194 194 windowOfFilter = 1
195 195
196 196 #Speed of ligth
197 197 C = 3e8
198 198
199 199 frequency = 49.92e6
200 200
201 201 realtime = False
202 202
203 203 beacon_heiIndexList = None
204 204
205 205 last_block = None
206 206
207 207 blocknow = None
208 208
209 209 azimuth = None
210 210
211 211 zenith = None
212 212
213 213 beam = Beam()
214 214
215 215 profileIndex = None
216 216
217 217 def __init__(self):
218 218
219 219 raise ValueError, "This class has not been implemented"
220 220
221 221 def getNoise(self):
222 222
223 223 raise ValueError, "Not implemented"
224 224
225 225 def getNChannels(self):
226 226
227 227 return len(self.channelList)
228 228
229 229 def getChannelIndexList(self):
230 230
231 231 return range(self.nChannels)
232 232
233 233 def getNHeights(self):
234 234
235 235 return len(self.heightList)
236 236
237 237 def getHeiRange(self, extrapoints=0):
238 238
239 239 heis = self.heightList
240 240 # deltah = self.heightList[1] - self.heightList[0]
241 241 #
242 242 # heis.append(self.heightList[-1])
243 243
244 244 return heis
245 245
246 246 def getltctime(self):
247 247
248 248 if self.useLocalTime:
249 249 return self.utctime - self.timeZone*60
250 250
251 251 return self.utctime
252 252
253 253 def getDatatime(self):
254 254
255 255 datatimeValue = datetime.datetime.utcfromtimestamp(self.ltctime)
256 256 return datatimeValue
257 257
258 258 def getTimeRange(self):
259 259
260 260 datatime = []
261 261
262 262 datatime.append(self.ltctime)
263 263 datatime.append(self.ltctime + self.timeInterval+60)
264 264
265 265 datatime = numpy.array(datatime)
266 266
267 267 return datatime
268 268
269 269 def getFmax(self):
270 270
271 271 PRF = 1./(self.ippSeconds * self.nCohInt)
272 272
273 273 fmax = PRF/2.
274 274
275 275 return fmax
276 276
277 277 def getVmax(self):
278 278
279 279 _lambda = self.C/self.frequency
280 280
281 281 vmax = self.getFmax() * _lambda
282 282
283 283 return vmax
284 284
285 285 def get_ippSeconds(self):
286 286 '''
287 287 '''
288 288 return self.radarControllerHeaderObj.ippSeconds
289 289
290 290 def set_ippSeconds(self, ippSeconds):
291 291 '''
292 292 '''
293 293
294 294 self.radarControllerHeaderObj.ippSeconds = ippSeconds
295 295
296 296 return
297 297
298 298 def get_dtype(self):
299 299 '''
300 300 '''
301 301 return getNumpyDtype(self.datatype)
302 302
303 303 def set_dtype(self, numpyDtype):
304 304 '''
305 305 '''
306 306
307 307 self.datatype = getDataTypeCode(numpyDtype)
308 308
309 309 def get_code(self):
310 310 '''
311 311 '''
312 312 return self.radarControllerHeaderObj.code
313 313
314 314 def set_code(self, code):
315 315 '''
316 316 '''
317 317 self.radarControllerHeaderObj.code = code
318 318
319 319 return
320 320
321 321 def get_ncode(self):
322 322 '''
323 323 '''
324 324 return self.radarControllerHeaderObj.nCode
325 325
326 326 def set_ncode(self, nCode):
327 327 '''
328 328 '''
329 329 self.radarControllerHeaderObj.nCode = nCode
330 330
331 331 return
332 332
333 333 def get_nbaud(self):
334 334 '''
335 335 '''
336 336 return self.radarControllerHeaderObj.nBaud
337 337
338 338 def set_nbaud(self, nBaud):
339 339 '''
340 340 '''
341 341 self.radarControllerHeaderObj.nBaud = nBaud
342 342
343 343 return
344 344 # def getTimeInterval(self):
345 345 #
346 346 # raise IOError, "This method should be implemented inside each Class"
347 347
348 348 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
349 349 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
350 350 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
351 351 #noise = property(getNoise, "I'm the 'nHeights' property.")
352 352 datatime = property(getDatatime, "I'm the 'datatime' property")
353 353 ltctime = property(getltctime, "I'm the 'ltctime' property")
354 354 ippSeconds = property(get_ippSeconds, set_ippSeconds)
355 355 dtype = property(get_dtype, set_dtype)
356 356 # timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
357 357 code = property(get_code, set_code)
358 358 nCode = property(get_ncode, set_ncode)
359 359 nBaud = property(get_nbaud, set_nbaud)
360 360
361 361 class Voltage(JROData):
362 362
363 363 #data es un numpy array de 2 dmensiones (canales, alturas)
364 364 data = None
365 365
366 366 def __init__(self):
367 367 '''
368 368 Constructor
369 369 '''
370 370
371 371 self.useLocalTime = True
372 372
373 373 self.radarControllerHeaderObj = RadarControllerHeader()
374 374
375 375 self.systemHeaderObj = SystemHeader()
376 376
377 377 self.type = "Voltage"
378 378
379 379 self.data = None
380 380
381 381 # self.dtype = None
382 382
383 383 # self.nChannels = 0
384 384
385 385 # self.nHeights = 0
386 386
387 387 self.nProfiles = None
388 388
389 389 self.heightList = None
390 390
391 391 self.channelList = None
392 392
393 393 # self.channelIndexList = None
394 394
395 395 self.flagNoData = True
396 396
397 397 self.flagDiscontinuousBlock = False
398 398
399 399 self.utctime = None
400 400
401 401 self.timeZone = None
402 402
403 403 self.dstFlag = None
404 404
405 405 self.errorCount = None
406 406
407 407 self.nCohInt = None
408 408
409 409 self.blocksize = None
410 410
411 411 self.flagDecodeData = False #asumo q la data no esta decodificada
412 412
413 413 self.flagDeflipData = False #asumo q la data no esta sin flip
414 414
415 415 self.flagShiftFFT = False
416 416
417 417 self.flagDataAsBlock = False #Asumo que la data es leida perfil a perfil
418 418
419 419 self.profileIndex = 0
420 420
421 421 def getNoisebyHildebrand(self, channel = None):
422 422 """
423 423 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
424 424
425 425 Return:
426 426 noiselevel
427 427 """
428 428
429 429 if channel != None:
430 430 data = self.data[channel]
431 431 nChannels = 1
432 432 else:
433 433 data = self.data
434 434 nChannels = self.nChannels
435 435
436 436 noise = numpy.zeros(nChannels)
437 437 power = data * numpy.conjugate(data)
438 438
439 439 for thisChannel in range(nChannels):
440 440 if nChannels == 1:
441 441 daux = power[:].real
442 442 else:
443 443 daux = power[thisChannel,:].real
444 444 noise[thisChannel] = hildebrand_sekhon(daux, self.nCohInt)
445 445
446 446 return noise
447 447
448 448 def getNoise(self, type = 1, channel = None):
449 449
450 450 if type == 1:
451 451 noise = self.getNoisebyHildebrand(channel)
452 452
453 453 return 10*numpy.log10(noise)
454 454
455 455 def getPower(self, channel = None):
456 456
457 457 if channel != None:
458 458 data = self.data[channel]
459 459 else:
460 460 data = self.data
461 461
462 462 power = data * numpy.conjugate(data)
463 463
464 464 return 10*numpy.log10(power.real)
465 465
466 466 def getTimeInterval(self):
467 467
468 468 timeInterval = self.ippSeconds * self.nCohInt
469 469
470 470 return timeInterval
471 471
472 472 noise = property(getNoise, "I'm the 'nHeights' property.")
473 473 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
474 474
475 475 class Spectra(JROData):
476 476
477 477 #data es un numpy array de 2 dmensiones (canales, perfiles, alturas)
478 478 data_spc = None
479 479
480 480 #data es un numpy array de 2 dmensiones (canales, pares, alturas)
481 481 data_cspc = None
482 482
483 483 #data es un numpy array de 2 dmensiones (canales, alturas)
484 484 data_dc = None
485 485
486 486 nFFTPoints = None
487 487
488 488 # nPairs = None
489 489
490 490 pairsList = None
491 491
492 492 nIncohInt = None
493 493
494 494 wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia
495 495
496 496 nCohInt = None #se requiere para determinar el valor de timeInterval
497 497
498 498 ippFactor = None
499 499
500 500 profileIndex = 0
501 501
502 502 def __init__(self):
503 503 '''
504 504 Constructor
505 505 '''
506 506
507 507 self.useLocalTime = True
508 508
509 509 self.radarControllerHeaderObj = RadarControllerHeader()
510 510
511 511 self.systemHeaderObj = SystemHeader()
512 512
513 513 self.type = "Spectra"
514 514
515 515 # self.data = None
516 516
517 517 # self.dtype = None
518 518
519 519 # self.nChannels = 0
520 520
521 521 # self.nHeights = 0
522 522
523 523 self.nProfiles = None
524 524
525 525 self.heightList = None
526 526
527 527 self.channelList = None
528 528
529 529 # self.channelIndexList = None
530 530
531 531 self.pairsList = None
532 532
533 533 self.flagNoData = True
534 534
535 535 self.flagDiscontinuousBlock = False
536 536
537 537 self.utctime = None
538 538
539 539 self.nCohInt = None
540 540
541 541 self.nIncohInt = None
542 542
543 543 self.blocksize = None
544 544
545 545 self.nFFTPoints = None
546 546
547 547 self.wavelength = None
548 548
549 549 self.flagDecodeData = False #asumo q la data no esta decodificada
550 550
551 551 self.flagDeflipData = False #asumo q la data no esta sin flip
552 552
553 553 self.flagShiftFFT = False
554 554
555 555 self.ippFactor = 1
556 556
557 557 #self.noise = None
558 558
559 559 self.beacon_heiIndexList = []
560 560
561 561 self.noise_estimation = None
562 562
563 563
564 564 def getNoisebyHildebrand(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
565 565 """
566 566 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
567 567
568 568 Return:
569 569 noiselevel
570 570 """
571 571
572 572 noise = numpy.zeros(self.nChannels)
573 573
574 574 for channel in range(self.nChannels):
575 575 daux = self.data_spc[channel,xmin_index:xmax_index,ymin_index:ymax_index]
576 576 noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
577 577
578 578 return noise
579 579
580 580 def getNoise(self, xmin_index=None, xmax_index=None, ymin_index=None, ymax_index=None):
581 581
582 582 if self.noise_estimation != None:
583 583 return self.noise_estimation #this was estimated by getNoise Operation defined in jroproc_spectra.py
584 584 else:
585 585 noise = self.getNoisebyHildebrand(xmin_index, xmax_index, ymin_index, ymax_index)
586 586 return noise
587 587
588 588
589 589 def getFreqRange(self, extrapoints=0):
590 590
591 591 deltafreq = self.getFmax() / (self.nFFTPoints*self.ippFactor)
592 592 freqrange = deltafreq*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
593 593
594 594 return freqrange
595 595
596 596 def getVelRange(self, extrapoints=0):
597 597
598 598 deltav = self.getVmax() / (self.nFFTPoints*self.ippFactor)
599 599 velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltav/2
600 600
601 601 return velrange
602 602
603 603 def getNPairs(self):
604 604
605 605 return len(self.pairsList)
606 606
607 607 def getPairsIndexList(self):
608 608
609 609 return range(self.nPairs)
610 610
611 611 def getNormFactor(self):
612 612 pwcode = 1
613 613 if self.flagDecodeData:
614 614 pwcode = numpy.sum(self.code[0]**2)
615 615 #normFactor = min(self.nFFTPoints,self.nProfiles)*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
616 616 normFactor = self.nProfiles*self.nIncohInt*self.nCohInt*pwcode*self.windowOfFilter
617 617
618 618 return normFactor
619 619
620 620 def getFlagCspc(self):
621 621
622 622 if self.data_cspc == None:
623 623 return True
624 624
625 625 return False
626 626
627 627 def getFlagDc(self):
628 628
629 629 if self.data_dc == None:
630 630 return True
631 631
632 632 return False
633 633
634 634 def getTimeInterval(self):
635 635
636 636 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt * self.nProfiles
637 637
638 638 return timeInterval
639 639
640 640 nPairs = property(getNPairs, "I'm the 'nPairs' property.")
641 641 pairsIndexList = property(getPairsIndexList, "I'm the 'pairsIndexList' property.")
642 642 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
643 643 flag_cspc = property(getFlagCspc)
644 644 flag_dc = property(getFlagDc)
645 645 noise = property(getNoise, "I'm the 'nHeights' property.")
646 646 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
647 647
648 648 class SpectraHeis(Spectra):
649 649
650 650 data_spc = None
651 651
652 652 data_cspc = None
653 653
654 654 data_dc = None
655 655
656 656 nFFTPoints = None
657 657
658 658 # nPairs = None
659 659
660 660 pairsList = None
661 661
662 nCohInt = None
663
662 664 nIncohInt = None
663 665
664 666 def __init__(self):
665 667
666 668 self.radarControllerHeaderObj = RadarControllerHeader()
667 669
668 670 self.systemHeaderObj = SystemHeader()
669 671
670 672 self.type = "SpectraHeis"
671 673
672 674 # self.dtype = None
673 675
674 676 # self.nChannels = 0
675 677
676 678 # self.nHeights = 0
677 679
678 680 self.nProfiles = None
679 681
680 682 self.heightList = None
681 683
682 684 self.channelList = None
683 685
684 686 # self.channelIndexList = None
685 687
686 688 self.flagNoData = True
687 689
688 690 self.flagDiscontinuousBlock = False
689 691
690 692 # self.nPairs = 0
691 693
692 694 self.utctime = None
693 695
694 696 self.blocksize = None
695 697
696 698 self.profileIndex = 0
699
700 self.nCohInt = 1
701
702 self.nIncohInt = 1
697 703
698 704 def getNormFactor(self):
699 705 pwcode = 1
700 706 if self.flagDecodeData:
701 707 pwcode = numpy.sum(self.code[0]**2)
702 708
703 709 normFactor = self.nIncohInt*self.nCohInt*pwcode
704 710
705 711 return normFactor
706 712
707 713 def getTimeInterval(self):
708 714
709 715 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
710 716
711 717 return timeInterval
712 718
713 719 normFactor = property(getNormFactor, "I'm the 'getNormFactor' property.")
714 720 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
715 721
716 class Fits:
722 class Fits(JROData):
717 723
718 724 heightList = None
719 725
720 726 channelList = None
721 727
722 728 flagNoData = True
723 729
724 730 flagDiscontinuousBlock = False
725 731
726 732 useLocalTime = False
727 733
728 734 utctime = None
729 735
730 736 timeZone = None
731 737
732 738 # ippSeconds = None
733 739
734 740 # timeInterval = None
735 741
736 742 nCohInt = None
737 743
738 744 nIncohInt = None
739 745
740 746 noise = None
741 747
742 748 windowOfFilter = 1
743 749
744 750 #Speed of ligth
745 751 C = 3e8
746 752
747 753 frequency = 49.92e6
748 754
749 755 realtime = False
750 756
751 757
752 758 def __init__(self):
753 759
754 760 self.type = "Fits"
755 761
756 762 self.nProfiles = None
757 763
758 764 self.heightList = None
759 765
760 766 self.channelList = None
761 767
762 768 # self.channelIndexList = None
763 769
764 770 self.flagNoData = True
765 771
766 772 self.utctime = None
767 773
768 self.nCohInt = None
774 self.nCohInt = 1
769 775
770 self.nIncohInt = None
776 self.nIncohInt = 1
771 777
772 778 self.useLocalTime = True
773 779
774 780 self.profileIndex = 0
775 781
776 782 # self.utctime = None
777 783 # self.timeZone = None
778 784 # self.ltctime = None
779 785 # self.timeInterval = None
780 786 # self.header = None
781 787 # self.data_header = None
782 788 # self.data = None
783 789 # self.datatime = None
784 790 # self.flagNoData = False
785 791 # self.expName = ''
786 792 # self.nChannels = None
787 793 # self.nSamples = None
788 794 # self.dataBlocksPerFile = None
789 795 # self.comments = ''
790 796 #
791 797
792 798
793 799 def getltctime(self):
794 800
795 801 if self.useLocalTime:
796 802 return self.utctime - self.timeZone*60
797 803
798 804 return self.utctime
799 805
800 806 def getDatatime(self):
801 807
802 808 datatime = datetime.datetime.utcfromtimestamp(self.ltctime)
803 809 return datatime
804 810
805 811 def getTimeRange(self):
806 812
807 813 datatime = []
808 814
809 815 datatime.append(self.ltctime)
810 816 datatime.append(self.ltctime + self.timeInterval)
811 817
812 818 datatime = numpy.array(datatime)
813 819
814 820 return datatime
815 821
816 822 def getHeiRange(self):
817 823
818 824 heis = self.heightList
819 825
820 826 return heis
821 827
822 828 def isEmpty(self):
823 829
824 830 return self.flagNoData
825 831
826 832 def getNHeights(self):
827 833
828 834 return len(self.heightList)
829 835
830 836 def getNChannels(self):
831 837
832 838 return len(self.channelList)
833 839
834 840 def getChannelIndexList(self):
835 841
836 842 return range(self.nChannels)
837 843
838 844 def getNoise(self, type = 1):
839 845
840 846 #noise = numpy.zeros(self.nChannels)
841 847
842 848 if type == 1:
843 849 noise = self.getNoisebyHildebrand()
844 850
845 851 if type == 2:
846 852 noise = self.getNoisebySort()
847 853
848 854 if type == 3:
849 855 noise = self.getNoisebyWindow()
850 856
851 857 return noise
852 858
853 859 def getTimeInterval(self):
854 860
855 raise ValueError, "This method is not implemented yet"
861 timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
862
863 return timeInterval
856 864
857 865 datatime = property(getDatatime, "I'm the 'datatime' property")
858 866 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
859 867 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
860 868 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
861 869 noise = property(getNoise, "I'm the 'nHeights' property.")
862 870 datatime = property(getDatatime, "I'm the 'datatime' property")
863 871 ltctime = property(getltctime, "I'm the 'ltctime' property")
864 872 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
865 873
866 874 class Correlation(JROData):
867 875
868 876 noise = None
869 877
870 878 SNR = None
871 879
872 880 pairsAutoCorr = None #Pairs of Autocorrelation
873 881
874 882 #--------------------------------------------------
875 883
876 884 data_corr = None
877 885
878 886 data_volt = None
879 887
880 888 lagT = None # each element value is a profileIndex
881 889
882 890 lagR = None # each element value is in km
883 891
884 892 pairsList = None
885 893
886 894 calculateVelocity = None
887 895
888 896 nPoints = None
889 897
890 898 nAvg = None
891 899
892 900 bufferSize = None
893 901
894 902 def __init__(self):
895 903 '''
896 904 Constructor
897 905 '''
898 906 self.radarControllerHeaderObj = RadarControllerHeader()
899 907
900 908 self.systemHeaderObj = SystemHeader()
901 909
902 910 self.type = "Correlation"
903 911
904 912 self.data = None
905 913
906 914 self.dtype = None
907 915
908 916 self.nProfiles = None
909 917
910 918 self.heightList = None
911 919
912 920 self.channelList = None
913 921
914 922 self.flagNoData = True
915 923
916 924 self.flagDiscontinuousBlock = False
917 925
918 926 self.utctime = None
919 927
920 928 self.timeZone = None
921 929
922 930 self.dstFlag = None
923 931
924 932 self.errorCount = None
925 933
926 934 self.blocksize = None
927 935
928 936 self.flagDecodeData = False #asumo q la data no esta decodificada
929 937
930 938 self.flagDeflipData = False #asumo q la data no esta sin flip
931 939
932 940 self.pairsList = None
933 941
934 942 self.nPoints = None
935 943
936 944 def getLagTRange(self, extrapoints=0):
937 945
938 946 lagTRange = self.lagT
939 947 diff = lagTRange[1] - lagTRange[0]
940 948 extra = numpy.arange(1,extrapoints + 1)*diff + lagTRange[-1]
941 949 lagTRange = numpy.hstack((lagTRange, extra))
942 950
943 951 return lagTRange
944 952
945 953 def getLagRRange(self, extrapoints=0):
946 954
947 955 return self.lagR
948 956
949 957 def getPairsList(self):
950 958
951 959 return self.pairsList
952 960
953 961 def getCalculateVelocity(self):
954 962
955 963 return self.calculateVelocity
956 964
957 965 def getNPoints(self):
958 966
959 967 return self.nPoints
960 968
961 969 def getNAvg(self):
962 970
963 971 return self.nAvg
964 972
965 973 def getBufferSize(self):
966 974
967 975 return self.bufferSize
968 976
969 977 def getPairsAutoCorr(self):
970 978 pairsList = self.pairsList
971 979 pairsAutoCorr = numpy.zeros(self.nChannels, dtype = 'int')*numpy.nan
972 980
973 981 for l in range(len(pairsList)):
974 982 firstChannel = pairsList[l][0]
975 983 secondChannel = pairsList[l][1]
976 984
977 985 #Obteniendo pares de Autocorrelacion
978 986 if firstChannel == secondChannel:
979 987 pairsAutoCorr[firstChannel] = int(l)
980 988
981 989 pairsAutoCorr = pairsAutoCorr.astype(int)
982 990
983 991 return pairsAutoCorr
984 992
985 993 def getNoise(self, mode = 2):
986 994
987 995 indR = numpy.where(self.lagR == 0)[0][0]
988 996 indT = numpy.where(self.lagT == 0)[0][0]
989 997
990 998 jspectra0 = self.data_corr[:,:,indR,:]
991 999 jspectra = copy.copy(jspectra0)
992 1000
993 1001 num_chan = jspectra.shape[0]
994 1002 num_hei = jspectra.shape[2]
995 1003
996 1004 freq_dc = jspectra.shape[1]/2
997 1005 ind_vel = numpy.array([-2,-1,1,2]) + freq_dc
998 1006
999 1007 if ind_vel[0]<0:
1000 1008 ind_vel[range(0,1)] = ind_vel[range(0,1)] + self.num_prof
1001 1009
1002 1010 if mode == 1:
1003 1011 jspectra[:,freq_dc,:] = (jspectra[:,ind_vel[1],:] + jspectra[:,ind_vel[2],:])/2 #CORRECCION
1004 1012
1005 1013 if mode == 2:
1006 1014
1007 1015 vel = numpy.array([-2,-1,1,2])
1008 1016 xx = numpy.zeros([4,4])
1009 1017
1010 1018 for fil in range(4):
1011 1019 xx[fil,:] = vel[fil]**numpy.asarray(range(4))
1012 1020
1013 1021 xx_inv = numpy.linalg.inv(xx)
1014 1022 xx_aux = xx_inv[0,:]
1015 1023
1016 1024 for ich in range(num_chan):
1017 1025 yy = jspectra[ich,ind_vel,:]
1018 1026 jspectra[ich,freq_dc,:] = numpy.dot(xx_aux,yy)
1019 1027
1020 1028 junkid = jspectra[ich,freq_dc,:]<=0
1021 1029 cjunkid = sum(junkid)
1022 1030
1023 1031 if cjunkid.any():
1024 1032 jspectra[ich,freq_dc,junkid.nonzero()] = (jspectra[ich,ind_vel[1],junkid] + jspectra[ich,ind_vel[2],junkid])/2
1025 1033
1026 1034 noise = jspectra0[:,freq_dc,:] - jspectra[:,freq_dc,:]
1027 1035
1028 1036 return noise
1037
1038 def getTimeInterval(self):
1039
1040 timeInterval = self.ippSeconds * self.nCohInt * self.nPoints
1041
1042 return timeInterval
1029 1043
1044 timeInterval = property(getTimeInterval, "I'm the 'timeInterval' property")
1030 1045 # pairsList = property(getPairsList, "I'm the 'pairsList' property.")
1031 1046 # nPoints = property(getNPoints, "I'm the 'nPoints' property.")
1032 1047 calculateVelocity = property(getCalculateVelocity, "I'm the 'calculateVelocity' property.")
1033 1048 nAvg = property(getNAvg, "I'm the 'nAvg' property.")
1034 1049 bufferSize = property(getBufferSize, "I'm the 'bufferSize' property.")
1035 1050
1036 1051
1037 1052 class Parameters(JROData):
1038 1053
1039 1054 #Information from previous data
1040 1055
1041 1056 inputUnit = None #Type of data to be processed
1042 1057
1043 1058 operation = None #Type of operation to parametrize
1044 1059
1045 1060 normFactor = None #Normalization Factor
1046 1061
1047 1062 groupList = None #List of Pairs, Groups, etc
1048 1063
1049 1064 #Parameters
1050 1065
1051 1066 data_param = None #Parameters obtained
1052 1067
1053 1068 data_pre = None #Data Pre Parametrization
1054 1069
1055 1070 data_SNR = None #Signal to Noise Ratio
1056 1071
1057 1072 heightRange = None #Heights
1058 1073
1059 1074 abscissaRange = None #Abscissa, can be velocities, lags or time
1060 1075
1061 1076 noise = None #Noise Potency
1062 1077
1063 initUtcTime = None #Initial UTC time
1078 # initUtcTime = None #Initial UTC time
1064 1079
1065 1080 paramInterval = None #Time interval to calculate Parameters in seconds
1066 1081
1067 1082 #Fitting
1068 1083
1069 1084 data_error = None #Error of the estimation
1070 1085
1071 1086 constants = None
1072 1087
1073 1088 library = None
1074 1089
1075 1090 #Output signal
1076 1091
1077 1092 outputInterval = None #Time interval to calculate output signal in seconds
1078 1093
1079 1094 data_output = None #Out signal
1080 1095
1081
1082
1083 1096 def __init__(self):
1084 1097 '''
1085 1098 Constructor
1086 1099 '''
1087 1100 self.radarControllerHeaderObj = RadarControllerHeader()
1088 1101
1089 1102 self.systemHeaderObj = SystemHeader()
1090 1103
1091 1104 self.type = "Parameters"
1092
1105
1093 1106 def getTimeRange1(self):
1094 1107
1095 1108 datatime = []
1096 1109
1097 datatime.append(self.initUtcTime)
1098 datatime.append(self.initUtcTime + self.outputInterval - 1)
1110 datatime.append(self.ltctime)
1111 datatime.append(self.ltctime + self.outputInterval - 1)
1099 1112
1100 1113 datatime = numpy.array(datatime)
1101 1114
1102 1115 return datatime
@@ -1,646 +1,610
1 1 import os
2 2 import numpy
3 3 import time, datetime
4 4 import mpldriver
5 5
6 6 from schainpy.model.proc.jroproc_base import Operation
7 7
8 8 def isRealtime(utcdatatime):
9 9 utcnow = time.mktime(time.localtime())
10 10 delta = abs(utcnow - utcdatatime) # abs
11 11 if delta >= 30.:
12 12 return False
13 13 return True
14 14
15 15 class Figure(Operation):
16 16
17 17 __driver = mpldriver
18 18 __isConfigThread = False
19 19 fig = None
20 20
21 21 id = None
22 22 wintitle = None
23 23 width = None
24 24 height = None
25 25 nplots = None
26 26 timerange = None
27 27
28 28 axesObjList = []
29 29
30 30 WIDTH = None
31 31 HEIGHT = None
32 32 PREFIX = 'fig'
33 33
34 34 xmin = None
35 35 xmax = None
36 36
37 37 counter_imagwr = 0
38 38
39 39 figfile = None
40 40
41 41 def __init__(self):
42 42
43 43 raise ValueError, "This method is not implemented"
44 44
45 45 def __del__(self):
46 46
47 47 self.__driver.closeFigure()
48 48
49 49 def getFilename(self, name, ext='.png'):
50 50
51 51 path = '%s%03d' %(self.PREFIX, self.id)
52 52 filename = '%s_%s%s' %(self.PREFIX, name, ext)
53 53 return os.path.join(path, filename)
54 54
55 55 def getAxesObjList(self):
56 56
57 57 return self.axesObjList
58 58
59 59 def getSubplots(self):
60 60
61 61 raise ValueError, "Abstract method: This method should be defined"
62 62
63 63 def getScreenDim(self, widthplot, heightplot):
64 64
65 65 nrow, ncol = self.getSubplots()
66 66
67 67 widthscreen = widthplot*ncol
68 68 heightscreen = heightplot*nrow
69 69
70 70 return widthscreen, heightscreen
71 71
72 72 def getTimeLim(self, x, xmin=None, xmax=None, timerange=None):
73 73
74 74 if self.xmin != None and self.xmax != None:
75 75 if timerange == None:
76 76 timerange = self.xmax - self.xmin
77 77 xmin = self.xmin + timerange
78 78 xmax = self.xmax + timerange
79 79
80 80 return xmin, xmax
81 81
82 82 if timerange == None and (xmin==None or xmax==None):
83 83 timerange = 14400 #seconds
84 84 #raise ValueError, "(timerange) or (xmin & xmax) should be defined"
85 85
86 86 if timerange != None:
87 87 txmin = x[0] - x[0] % min(timerange/10, 10*60)
88 88 else:
89 89 txmin = x[0] - x[0] % 10*60
90 90
91 91 thisdatetime = datetime.datetime.utcfromtimestamp(txmin)
92 92 thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0))
93 93
94 94 if timerange != None:
95 95 xmin = (thisdatetime - thisdate).seconds/(60*60.)
96 96 xmax = xmin + timerange/(60*60.)
97 97
98 98 mindt = thisdate + datetime.timedelta(hours=xmin) - datetime.timedelta(seconds=time.timezone)
99 99 xmin_sec = time.mktime(mindt.timetuple())
100 100
101 101 maxdt = thisdate + datetime.timedelta(hours=xmax) - datetime.timedelta(seconds=time.timezone)
102 102 xmax_sec = time.mktime(maxdt.timetuple())
103 103
104 104 return xmin_sec, xmax_sec
105
106
107
108
109
110 # if timerange != None:
111 # txmin = x[0] - x[0]%timerange
112 # else:
113 # txmin = numpy.min(x)
114 #
115 # thisdatetime = datetime.datetime.utcfromtimestamp(txmin)
116 # thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0))
117 #
118 # ####################################################
119 # #If the x is out of xrange
120 # if xmax != None:
121 # if xmax < (thisdatetime - thisdate).seconds/(60*60.):
122 # xmin = None
123 # xmax = None
124 #
125 # if xmin == None:
126 # td = thisdatetime - thisdate
127 # xmin = td.seconds/(60*60.)
128 #
129 # if xmax == None:
130 # xmax = xmin + self.timerange/(60*60.)
131 #
132 # mindt = thisdate + datetime.timedelta(hours=xmin) - datetime.timedelta(seconds=time.timezone)
133 # tmin = time.mktime(mindt.timetuple())
134 #
135 # maxdt = thisdate + datetime.timedelta(hours=xmax) - datetime.timedelta(seconds=time.timezone)
136 # tmax = time.mktime(maxdt.timetuple())
137 #
138 # #self.timerange = tmax - tmin
139 #
140 # return tmin, tmax
141 105
142 106 def init(self, id, nplots, wintitle):
143 107
144 108 raise ValueError, "This method has been replaced with createFigure"
145 109
146 110 def createFigure(self, id, wintitle, widthplot=None, heightplot=None, show=True):
147 111
148 112 """
149 113 Crea la figura de acuerdo al driver y parametros seleccionados seleccionados.
150 114 Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH
151 115 y self.HEIGHT y el numero de subplots (nrow, ncol)
152 116
153 117 Input:
154 118 id : Los parametros necesarios son
155 119 wintitle :
156 120
157 121 """
158 122
159 123 if widthplot == None:
160 124 widthplot = self.WIDTH
161 125
162 126 if heightplot == None:
163 127 heightplot = self.HEIGHT
164 128
165 129 self.id = id
166 130
167 131 self.wintitle = wintitle
168 132
169 133 self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot)
170 134
171 135 self.fig = self.__driver.createFigure(id=self.id,
172 136 wintitle=self.wintitle,
173 137 width=self.widthscreen,
174 138 height=self.heightscreen,
175 139 show=show)
176 140
177 141 self.axesObjList = []
178 142 self.counter_imagwr = 0
179 143
180 144
181 145 def setDriver(self, driver=mpldriver):
182 146
183 147 self.__driver = driver
184 148
185 149 def setTitle(self, title):
186 150
187 151 self.__driver.setTitle(self.fig, title)
188 152
189 153 def setWinTitle(self, title):
190 154
191 155 self.__driver.setWinTitle(self.fig, title=title)
192 156
193 157 def setTextFromAxes(self, text):
194 158
195 159 raise ValueError, "Este metodo ha sido reemplazaado con el metodo setText de la clase Axes"
196 160
197 161 def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan):
198 162
199 163 raise ValueError, "Este metodo ha sido reemplazaado con el metodo addAxes"
200 164
201 165 def addAxes(self, *args):
202 166 """
203 167
204 168 Input:
205 169 *args : Los parametros necesarios son
206 170 nrow, ncol, xpos, ypos, colspan, rowspan
207 171 """
208 172
209 173 axesObj = Axes(self.fig, *args)
210 174 self.axesObjList.append(axesObj)
211 175
212 176 def saveFigure(self, figpath, figfile, *args):
213 177
214 178 filename = os.path.join(figpath, figfile)
215 179
216 180 fullpath = os.path.split(filename)[0]
217 181
218 182 if not os.path.exists(fullpath):
219 183 subpath = os.path.split(fullpath)[0]
220 184
221 185 if not os.path.exists(subpath):
222 186 os.mkdir(subpath)
223 187
224 188 os.mkdir(fullpath)
225 189
226 190 self.__driver.saveFigure(self.fig, filename, *args)
227 191
228 192 def save(self, figpath, figfile=None, save=True, ftp=False, wr_period=1, thisDatetime=None, update_figfile=True):
229 193
230 194 self.counter_imagwr += 1
231 195 if self.counter_imagwr < wr_period:
232 196 return
233 197
234 198 self.counter_imagwr = 0
235 199
236 200 if save:
237 201
238 202 if figfile == None:
239 203
240 204 if not thisDatetime:
241 205 raise ValueError, "Saving figure: figfile or thisDatetime should be defined"
242 206 return
243 207
244 208 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
245 209 figfile = self.getFilename(name = str_datetime)
246 210
247 211 if self.figfile == None:
248 212 self.figfile = figfile
249 213
250 214 if update_figfile:
251 215 self.figfile = figfile
252 216
253 217 # store png plot to local folder
254 218 self.saveFigure(figpath, self.figfile)
255 219
256 220
257 221 if not ftp:
258 222 return
259 223
260 224 if not thisDatetime:
261 225 return
262 226
263 227 # store png plot to FTP server according to RT-Web format
264 228 name = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
265 229 ftp_filename = os.path.join(figpath, name)
266 230 self.saveFigure(figpath, ftp_filename)
267 231
268 232 def getNameToFtp(self, thisDatetime, FTP_WEI, EXP_CODE, SUB_EXP_CODE, PLOT_CODE, PLOT_POS):
269 233 YEAR_STR = '%4.4d'%thisDatetime.timetuple().tm_year
270 234 DOY_STR = '%3.3d'%thisDatetime.timetuple().tm_yday
271 235 FTP_WEI = '%2.2d'%FTP_WEI
272 236 EXP_CODE = '%3.3d'%EXP_CODE
273 237 SUB_EXP_CODE = '%2.2d'%SUB_EXP_CODE
274 238 PLOT_CODE = '%2.2d'%PLOT_CODE
275 239 PLOT_POS = '%2.2d'%PLOT_POS
276 240 name = YEAR_STR + DOY_STR + FTP_WEI + EXP_CODE + SUB_EXP_CODE + PLOT_CODE + PLOT_POS
277 241 return name
278 242
279 243 def draw(self):
280 244
281 245 self.__driver.draw(self.fig)
282 246
283 247 def run(self):
284 248
285 249 raise ValueError, "This method is not implemented"
286 250
287 def close(self):
251 def close(self, show=False):
288 252
289 self.__driver.closeFigure()
253 self.__driver.closeFigure(show=show, fig=self.fig)
290 254
291 255 axesList = property(getAxesObjList)
292 256
293 257
294 258 class Axes:
295 259
296 260 __driver = mpldriver
297 261 fig = None
298 262 ax = None
299 263 plot = None
300 264 __missing = 1E30
301 265 __firsttime = None
302 266
303 267 __showprofile = False
304 268
305 269 xmin = None
306 270 xmax = None
307 271 ymin = None
308 272 ymax = None
309 273 zmin = None
310 274 zmax = None
311 275
312 276 x_buffer = None
313 277 z_buffer = None
314 278
315 279 decimationx = None
316 280 decimationy = None
317 281
318 282 __MAXNUMX = 300
319 283 __MAXNUMY = 150
320 284
321 285 def __init__(self, *args):
322 286
323 287 """
324 288
325 289 Input:
326 290 *args : Los parametros necesarios son
327 291 fig, nrow, ncol, xpos, ypos, colspan, rowspan
328 292 """
329 293
330 294 ax = self.__driver.createAxes(*args)
331 295 self.fig = args[0]
332 296 self.ax = ax
333 297 self.plot = None
334 298
335 299 self.__firsttime = True
336 300 self.idlineList = []
337 301
338 302 self.x_buffer = numpy.array([])
339 303 self.z_buffer = numpy.array([])
340 304
341 305 def setText(self, text):
342 306
343 307 self.__driver.setAxesText(self.ax, text)
344 308
345 309 def setXAxisAsTime(self):
346 310 pass
347 311
348 312 def pline(self, x, y,
349 313 xmin=None, xmax=None,
350 314 ymin=None, ymax=None,
351 315 xlabel='', ylabel='',
352 316 title='',
353 317 **kwargs):
354 318
355 319 """
356 320
357 321 Input:
358 322 x :
359 323 y :
360 324 xmin :
361 325 xmax :
362 326 ymin :
363 327 ymax :
364 328 xlabel :
365 329 ylabel :
366 330 title :
367 331 **kwargs : Los parametros aceptados son
368 332
369 333 ticksize
370 334 ytick_visible
371 335 """
372 336
373 337 if self.__firsttime:
374 338
375 339 if xmin == None: xmin = numpy.nanmin(x)
376 340 if xmax == None: xmax = numpy.nanmax(x)
377 341 if ymin == None: ymin = numpy.nanmin(y)
378 342 if ymax == None: ymax = numpy.nanmax(y)
379 343
380 344 self.plot = self.__driver.createPline(self.ax, x, y,
381 345 xmin, xmax,
382 346 ymin, ymax,
383 347 xlabel=xlabel,
384 348 ylabel=ylabel,
385 349 title=title,
386 350 **kwargs)
387 351
388 352 self.idlineList.append(0)
389 353 self.__firsttime = False
390 354 return
391 355
392 356 self.__driver.pline(self.plot, x, y, xlabel=xlabel,
393 357 ylabel=ylabel,
394 358 title=title)
395 359
396 360 def addpline(self, x, y, idline, **kwargs):
397 361 lines = self.ax.lines
398 362
399 363 if idline in self.idlineList:
400 364 self.__driver.set_linedata(self.ax, x, y, idline)
401 365
402 366 if idline not in(self.idlineList):
403 367 self.__driver.addpline(self.ax, x, y, **kwargs)
404 368 self.idlineList.append(idline)
405 369
406 370 return
407 371
408 372 def pmultiline(self, x, y,
409 373 xmin=None, xmax=None,
410 374 ymin=None, ymax=None,
411 375 xlabel='', ylabel='',
412 376 title='',
413 377 **kwargs):
414 378
415 379 if self.__firsttime:
416 380
417 381 if xmin == None: xmin = numpy.nanmin(x)
418 382 if xmax == None: xmax = numpy.nanmax(x)
419 383 if ymin == None: ymin = numpy.nanmin(y)
420 384 if ymax == None: ymax = numpy.nanmax(y)
421 385
422 386 self.plot = self.__driver.createPmultiline(self.ax, x, y,
423 387 xmin, xmax,
424 388 ymin, ymax,
425 389 xlabel=xlabel,
426 390 ylabel=ylabel,
427 391 title=title,
428 392 **kwargs)
429 393 self.__firsttime = False
430 394 return
431 395
432 396 self.__driver.pmultiline(self.plot, x, y, xlabel=xlabel,
433 397 ylabel=ylabel,
434 398 title=title)
435 399
436 400 def pmultilineyaxis(self, x, y,
437 401 xmin=None, xmax=None,
438 402 ymin=None, ymax=None,
439 403 xlabel='', ylabel='',
440 404 title='',
441 405 **kwargs):
442 406
443 407 if self.__firsttime:
444 408
445 409 if xmin == None: xmin = numpy.nanmin(x)
446 410 if xmax == None: xmax = numpy.nanmax(x)
447 411 if ymin == None: ymin = numpy.nanmin(y)
448 412 if ymax == None: ymax = numpy.nanmax(y)
449 413
450 414 self.plot = self.__driver.createPmultilineYAxis(self.ax, x, y,
451 415 xmin, xmax,
452 416 ymin, ymax,
453 417 xlabel=xlabel,
454 418 ylabel=ylabel,
455 419 title=title,
456 420 **kwargs)
457 421 if self.xmin == None: self.xmin = xmin
458 422 if self.xmax == None: self.xmax = xmax
459 423 if self.ymin == None: self.ymin = ymin
460 424 if self.ymax == None: self.ymax = ymax
461 425
462 426 self.__firsttime = False
463 427 return
464 428
465 429 self.__driver.pmultilineyaxis(self.plot, x, y, xlabel=xlabel,
466 430 ylabel=ylabel,
467 431 title=title)
468 432
469 433 def pcolor(self, x, y, z,
470 434 xmin=None, xmax=None,
471 435 ymin=None, ymax=None,
472 436 zmin=None, zmax=None,
473 437 xlabel='', ylabel='',
474 438 title='', rti = False, colormap='jet',
475 439 **kwargs):
476 440
477 441 """
478 442 Input:
479 443 x :
480 444 y :
481 445 x :
482 446 xmin :
483 447 xmax :
484 448 ymin :
485 449 ymax :
486 450 zmin :
487 451 zmax :
488 452 xlabel :
489 453 ylabel :
490 454 title :
491 455 **kwargs : Los parametros aceptados son
492 456 ticksize=9,
493 457 cblabel=''
494 458 rti = True or False
495 459 """
496 460
497 461 if self.__firsttime:
498 462
499 463 if xmin == None: xmin = numpy.nanmin(x)
500 464 if xmax == None: xmax = numpy.nanmax(x)
501 465 if ymin == None: ymin = numpy.nanmin(y)
502 466 if ymax == None: ymax = numpy.nanmax(y)
503 467 if zmin == None: zmin = numpy.nanmin(z)
504 468 if zmax == None: zmax = numpy.nanmax(z)
505 469
506 470
507 471 self.plot = self.__driver.createPcolor(self.ax, x, y, z,
508 472 xmin, xmax,
509 473 ymin, ymax,
510 474 zmin, zmax,
511 475 xlabel=xlabel,
512 476 ylabel=ylabel,
513 477 title=title,
514 478 colormap=colormap,
515 479 **kwargs)
516 480
517 481 if self.xmin == None: self.xmin = xmin
518 482 if self.xmax == None: self.xmax = xmax
519 483 if self.ymin == None: self.ymin = ymin
520 484 if self.ymax == None: self.ymax = ymax
521 485 if self.zmin == None: self.zmin = zmin
522 486 if self.zmax == None: self.zmax = zmax
523 487
524 488 self.__firsttime = False
525 489 return
526 490
527 491 if rti:
528 492 self.__driver.addpcolor(self.ax, x, y, z, self.zmin, self.zmax,
529 493 xlabel=xlabel,
530 494 ylabel=ylabel,
531 495 title=title,
532 496 colormap=colormap)
533 497 return
534 498
535 499 self.__driver.pcolor(self.plot, z,
536 500 xlabel=xlabel,
537 501 ylabel=ylabel,
538 502 title=title)
539 503
540 504 def pcolorbuffer(self, x, y, z,
541 505 xmin=None, xmax=None,
542 506 ymin=None, ymax=None,
543 507 zmin=None, zmax=None,
544 508 xlabel='', ylabel='',
545 509 title='', rti = True, colormap='jet',
546 510 maxNumX = None, maxNumY = None,
547 511 **kwargs):
548 512
549 513 if maxNumX == None:
550 514 maxNumX = self.__MAXNUMX
551 515
552 516 if maxNumY == None:
553 517 maxNumY = self.__MAXNUMY
554 518
555 519 if self.__firsttime:
556 520 self.z_buffer = z
557 521 self.x_buffer = numpy.hstack((self.x_buffer, x))
558 522
559 523 if xmin == None: xmin = numpy.nanmin(x)
560 524 if xmax == None: xmax = numpy.nanmax(x)
561 525 if ymin == None: ymin = numpy.nanmin(y)
562 526 if ymax == None: ymax = numpy.nanmax(y)
563 527 if zmin == None: zmin = numpy.nanmin(z)
564 528 if zmax == None: zmax = numpy.nanmax(z)
565 529
566 530
567 531 self.plot = self.__driver.createPcolor(self.ax, self.x_buffer, y, z,
568 532 xmin, xmax,
569 533 ymin, ymax,
570 534 zmin, zmax,
571 535 xlabel=xlabel,
572 536 ylabel=ylabel,
573 537 title=title,
574 538 colormap=colormap,
575 539 **kwargs)
576 540
577 541 if self.xmin == None: self.xmin = xmin
578 542 if self.xmax == None: self.xmax = xmax
579 543 if self.ymin == None: self.ymin = ymin
580 544 if self.ymax == None: self.ymax = ymax
581 545 if self.zmin == None: self.zmin = zmin
582 546 if self.zmax == None: self.zmax = zmax
583 547
584 548 self.__firsttime = False
585 549 return
586 550
587 551 self.x_buffer = numpy.hstack((self.x_buffer, x[-1]))
588 552 self.z_buffer = numpy.hstack((self.z_buffer, z))
589 553
590 554 if self.decimationx == None:
591 555 deltax = float(self.xmax - self.xmin)/maxNumX
592 556 deltay = float(self.ymax - self.ymin)/maxNumY
593 557
594 558 resolutionx = self.x_buffer[2]-self.x_buffer[0]
595 559 resolutiony = y[1]-y[0]
596 560
597 561 self.decimationx = numpy.ceil(deltax / resolutionx)
598 562 self.decimationy = numpy.ceil(deltay / resolutiony)
599 563
600 564 z_buffer = self.z_buffer.reshape(-1,len(y))
601 565
602 566 x_buffer = self.x_buffer[::self.decimationx]
603 567 y_buffer = y[::self.decimationy]
604 568 z_buffer = z_buffer[::self.decimationx, ::self.decimationy]
605 569 #===================================================
606 570
607 571 x_buffer, y_buffer, z_buffer = self.__fillGaps(x_buffer, y_buffer, z_buffer)
608 572
609 573 self.__driver.addpcolorbuffer(self.ax, x_buffer, y_buffer, z_buffer, self.zmin, self.zmax,
610 574 xlabel=xlabel,
611 575 ylabel=ylabel,
612 576 title=title,
613 577 colormap=colormap)
614 578
615 579 def polar(self, x, y,
616 580 title='', xlabel='',ylabel='',**kwargs):
617 581
618 582 if self.__firsttime:
619 583 self.plot = self.__driver.createPolar(self.ax, x, y, title = title, xlabel = xlabel, ylabel = ylabel)
620 584 self.__firsttime = False
621 585 self.x_buffer = x
622 586 self.y_buffer = y
623 587 return
624 588
625 589 self.x_buffer = numpy.hstack((self.x_buffer,x))
626 590 self.y_buffer = numpy.hstack((self.y_buffer,y))
627 591 self.__driver.polar(self.plot, self.x_buffer, self.y_buffer, xlabel=xlabel,
628 592 ylabel=ylabel,
629 593 title=title)
630 594
631 595 def __fillGaps(self, x_buffer, y_buffer, z_buffer):
632 596
633 597 deltas = x_buffer[1:] - x_buffer[0:-1]
634 598 x_median = numpy.median(deltas)
635 599
636 600 index = numpy.where(deltas >= 2*x_median)
637 601
638 602 if len(index[0]) != 0:
639 603 z_buffer[index[0],::] = self.__missing
640 604 z_buffer = numpy.ma.masked_inside(z_buffer,0.99*self.__missing,1.01*self.__missing)
641 605
642 606 return x_buffer, y_buffer, z_buffer
643 607
644 608
645 609
646 610 No newline at end of file
@@ -1,1326 +1,1327
1 1 '''
2 2 Created on Jul 9, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6 import os
7 7 import datetime
8 8 import numpy
9 9
10 10 from figure import Figure, isRealtime
11 11 from plotting_codes import *
12 12
13 13 class SpectraPlot(Figure):
14 14
15 15 isConfig = None
16 16 __nsubplots = None
17 17
18 18 WIDTHPROF = None
19 19 HEIGHTPROF = None
20 20 PREFIX = 'spc'
21 21
22 22 def __init__(self):
23 23
24 24 self.isConfig = False
25 25 self.__nsubplots = 1
26 26
27 27 self.WIDTH = 280
28 28 self.HEIGHT = 250
29 29 self.WIDTHPROF = 120
30 30 self.HEIGHTPROF = 0
31 31 self.counter_imagwr = 0
32 32
33 33 self.PLOT_CODE = SPEC_CODE
34 34
35 35 self.FTP_WEI = None
36 36 self.EXP_CODE = None
37 37 self.SUB_EXP_CODE = None
38 38 self.PLOT_POS = None
39 39
40 40 self.__xfilter_ena = False
41 41 self.__yfilter_ena = False
42 42
43 43 def getSubplots(self):
44 44
45 45 ncol = int(numpy.sqrt(self.nplots)+0.9)
46 46 nrow = int(self.nplots*1./ncol + 0.9)
47 47
48 48 return nrow, ncol
49 49
50 50 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
51 51
52 52 self.__showprofile = showprofile
53 53 self.nplots = nplots
54 54
55 55 ncolspan = 1
56 56 colspan = 1
57 57 if showprofile:
58 58 ncolspan = 3
59 59 colspan = 2
60 60 self.__nsubplots = 2
61 61
62 62 self.createFigure(id = id,
63 63 wintitle = wintitle,
64 64 widthplot = self.WIDTH + self.WIDTHPROF,
65 65 heightplot = self.HEIGHT + self.HEIGHTPROF,
66 66 show=show)
67 67
68 68 nrow, ncol = self.getSubplots()
69 69
70 70 counter = 0
71 71 for y in range(nrow):
72 72 for x in range(ncol):
73 73
74 74 if counter >= self.nplots:
75 75 break
76 76
77 77 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
78 78
79 79 if showprofile:
80 80 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
81 81
82 82 counter += 1
83 83
84 84 def run(self, dataOut, id, wintitle="", channelList=None, showprofile=True,
85 85 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
86 86 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
87 87 server=None, folder=None, username=None, password=None,
88 88 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0, realtime=False):
89 89
90 90 """
91 91
92 92 Input:
93 93 dataOut :
94 94 id :
95 95 wintitle :
96 96 channelList :
97 97 showProfile :
98 98 xmin : None,
99 99 xmax : None,
100 100 ymin : None,
101 101 ymax : None,
102 102 zmin : None,
103 103 zmax : None
104 104 """
105 105
106 106 if realtime:
107 107 if not(isRealtime(utcdatatime = dataOut.utctime)):
108 108 print 'Skipping this plot function'
109 109 return
110 110
111 111 if channelList == None:
112 112 channelIndexList = dataOut.channelIndexList
113 113 else:
114 114 channelIndexList = []
115 115 for channel in channelList:
116 116 if channel not in dataOut.channelList:
117 117 raise ValueError, "Channel %d is not in dataOut.channelList"
118 118 channelIndexList.append(dataOut.channelList.index(channel))
119 119
120 120 factor = dataOut.normFactor
121 121
122 122 x = dataOut.getVelRange(1)
123 123 y = dataOut.getHeiRange()
124 124
125 125 z = dataOut.data_spc[channelIndexList,:,:]/factor
126 126 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
127 127 zdB = 10*numpy.log10(z)
128 128
129 129 avg = numpy.average(z, axis=1)
130 130 avgdB = 10*numpy.log10(avg)
131 131
132 132 noise = dataOut.getNoise()/factor
133 133 noisedB = 10*numpy.log10(noise)
134 134
135 135 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
136 136 title = wintitle + " Spectra"
137 137 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
138 138 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
139 139
140 140 xlabel = "Velocity (m/s)"
141 141 ylabel = "Range (Km)"
142 142
143 143 if not self.isConfig:
144 144
145 145 nplots = len(channelIndexList)
146 146
147 147 self.setup(id=id,
148 148 nplots=nplots,
149 149 wintitle=wintitle,
150 150 showprofile=showprofile,
151 151 show=show)
152 152
153 153 if xmin == None: xmin = numpy.nanmin(x)
154 154 if xmax == None: xmax = numpy.nanmax(x)
155 155 if ymin == None: ymin = numpy.nanmin(y)
156 156 if ymax == None: ymax = numpy.nanmax(y)
157 157 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
158 158 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
159 159
160 160 self.FTP_WEI = ftp_wei
161 161 self.EXP_CODE = exp_code
162 162 self.SUB_EXP_CODE = sub_exp_code
163 163 self.PLOT_POS = plot_pos
164 164
165 165 self.isConfig = True
166 166
167 167 self.setWinTitle(title)
168 168
169 169 for i in range(self.nplots):
170 170 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
171 171 title = "Channel %d: %4.2fdB: %s" %(dataOut.channelList[i]+1, noisedB[i], str_datetime)
172 172 if len(dataOut.beam.codeList) != 0:
173 173 title = "Ch%d:%4.2fdB,%2.2f,%2.2f:%s" %(dataOut.channelList[i]+1, noisedB[i], dataOut.beam.azimuthList[i], dataOut.beam.zenithList[i], str_datetime)
174 174
175 175 axes = self.axesList[i*self.__nsubplots]
176 176 axes.pcolor(x, y, zdB[i,:,:],
177 177 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
178 178 xlabel=xlabel, ylabel=ylabel, title=title,
179 179 ticksize=9, cblabel='')
180 180
181 181 if self.__showprofile:
182 182 axes = self.axesList[i*self.__nsubplots +1]
183 183 axes.pline(avgdB[i,:], y,
184 184 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
185 185 xlabel='dB', ylabel='', title='',
186 186 ytick_visible=False,
187 187 grid='x')
188 188
189 189 noiseline = numpy.repeat(noisedB[i], len(y))
190 190 axes.addpline(noiseline, y, idline=1, color="black", linestyle="dashed", lw=2)
191 191
192 192 self.draw()
193 193
194 194 if figfile == None:
195 195 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
196 196 name = str_datetime
197 197 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
198 198 name = name + '_az' + '_%2.2f'%(dataOut.azimuth) + '_zn' + '_%2.2f'%(dataOut.zenith)
199 199 figfile = self.getFilename(name)
200 200
201 201 self.save(figpath=figpath,
202 202 figfile=figfile,
203 203 save=save,
204 204 ftp=ftp,
205 205 wr_period=wr_period,
206 206 thisDatetime=thisDatetime)
207 207
208 208 class CrossSpectraPlot(Figure):
209 209
210 210 isConfig = None
211 211 __nsubplots = None
212 212
213 213 WIDTH = None
214 214 HEIGHT = None
215 215 WIDTHPROF = None
216 216 HEIGHTPROF = None
217 217 PREFIX = 'cspc'
218 218
219 219 def __init__(self):
220 220
221 221 self.isConfig = False
222 222 self.__nsubplots = 4
223 223 self.counter_imagwr = 0
224 224 self.WIDTH = 250
225 225 self.HEIGHT = 250
226 226 self.WIDTHPROF = 0
227 227 self.HEIGHTPROF = 0
228 228
229 229 self.PLOT_CODE = CROSS_CODE
230 230 self.FTP_WEI = None
231 231 self.EXP_CODE = None
232 232 self.SUB_EXP_CODE = None
233 233 self.PLOT_POS = None
234 234
235 235 def getSubplots(self):
236 236
237 237 ncol = 4
238 238 nrow = self.nplots
239 239
240 240 return nrow, ncol
241 241
242 242 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
243 243
244 244 self.__showprofile = showprofile
245 245 self.nplots = nplots
246 246
247 247 ncolspan = 1
248 248 colspan = 1
249 249
250 250 self.createFigure(id = id,
251 251 wintitle = wintitle,
252 252 widthplot = self.WIDTH + self.WIDTHPROF,
253 253 heightplot = self.HEIGHT + self.HEIGHTPROF,
254 254 show=True)
255 255
256 256 nrow, ncol = self.getSubplots()
257 257
258 258 counter = 0
259 259 for y in range(nrow):
260 260 for x in range(ncol):
261 261 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
262 262
263 263 counter += 1
264 264
265 265 def run(self, dataOut, id, wintitle="", pairsList=None,
266 266 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
267 267 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
268 268 power_cmap='jet', coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
269 269 server=None, folder=None, username=None, password=None,
270 270 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
271 271
272 272 """
273 273
274 274 Input:
275 275 dataOut :
276 276 id :
277 277 wintitle :
278 278 channelList :
279 279 showProfile :
280 280 xmin : None,
281 281 xmax : None,
282 282 ymin : None,
283 283 ymax : None,
284 284 zmin : None,
285 285 zmax : None
286 286 """
287 287
288 288 if pairsList == None:
289 289 pairsIndexList = dataOut.pairsIndexList
290 290 else:
291 291 pairsIndexList = []
292 292 for pair in pairsList:
293 293 if pair not in dataOut.pairsList:
294 294 raise ValueError, "Pair %s is not in dataOut.pairsList" %str(pair)
295 295 pairsIndexList.append(dataOut.pairsList.index(pair))
296 296
297 if pairsIndexList == []:
297 if not pairsIndexList:
298 298 return
299 299
300 300 if len(pairsIndexList) > 4:
301 301 pairsIndexList = pairsIndexList[0:4]
302
302 303 factor = dataOut.normFactor
303 304 x = dataOut.getVelRange(1)
304 305 y = dataOut.getHeiRange()
305 306 z = dataOut.data_spc[:,:,:]/factor
306 307 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
307 308
308 309 noise = dataOut.noise/factor
309 310
310 311 zdB = 10*numpy.log10(z)
311 312 noisedB = 10*numpy.log10(noise)
312 313
313 314
314 315 #thisDatetime = dataOut.datatime
315 316 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
316 317 title = wintitle + " Cross-Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
317 318 xlabel = "Velocity (m/s)"
318 319 ylabel = "Range (Km)"
319 320
320 321 if not self.isConfig:
321 322
322 323 nplots = len(pairsIndexList)
323 324
324 325 self.setup(id=id,
325 326 nplots=nplots,
326 327 wintitle=wintitle,
327 328 showprofile=False,
328 329 show=show)
329 330
330 331 avg = numpy.abs(numpy.average(z, axis=1))
331 332 avgdB = 10*numpy.log10(avg)
332 333
333 334 if xmin == None: xmin = numpy.nanmin(x)
334 335 if xmax == None: xmax = numpy.nanmax(x)
335 336 if ymin == None: ymin = numpy.nanmin(y)
336 337 if ymax == None: ymax = numpy.nanmax(y)
337 338 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
338 339 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
339 340
340 341 self.FTP_WEI = ftp_wei
341 342 self.EXP_CODE = exp_code
342 343 self.SUB_EXP_CODE = sub_exp_code
343 344 self.PLOT_POS = plot_pos
344 345
345 346 self.isConfig = True
346 347
347 348 self.setWinTitle(title)
348 349
349 350 for i in range(self.nplots):
350 351 pair = dataOut.pairsList[pairsIndexList[i]]
351 352 str_datetime = '%s %s'%(thisDatetime.strftime("%Y/%m/%d"),thisDatetime.strftime("%H:%M:%S"))
352 353 title = "Ch%d: %4.2fdB: %s" %(pair[0], noisedB[pair[0]], str_datetime)
353 354 zdB = 10.*numpy.log10(dataOut.data_spc[pair[0],:,:]/factor)
354 355 axes0 = self.axesList[i*self.__nsubplots]
355 356 axes0.pcolor(x, y, zdB,
356 357 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
357 358 xlabel=xlabel, ylabel=ylabel, title=title,
358 359 ticksize=9, colormap=power_cmap, cblabel='')
359 360
360 361 title = "Ch%d: %4.2fdB: %s" %(pair[1], noisedB[pair[1]], str_datetime)
361 362 zdB = 10.*numpy.log10(dataOut.data_spc[pair[1],:,:]/factor)
362 363 axes0 = self.axesList[i*self.__nsubplots+1]
363 364 axes0.pcolor(x, y, zdB,
364 365 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
365 366 xlabel=xlabel, ylabel=ylabel, title=title,
366 367 ticksize=9, colormap=power_cmap, cblabel='')
367 368
368 369 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:])
369 370 coherence = numpy.abs(coherenceComplex)
370 371 # phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
371 372 phase = numpy.arctan2(coherenceComplex.imag, coherenceComplex.real)*180/numpy.pi
372 373
373 374 title = "Coherence %d%d" %(pair[0], pair[1])
374 375 axes0 = self.axesList[i*self.__nsubplots+2]
375 376 axes0.pcolor(x, y, coherence,
376 377 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=0, zmax=1,
377 378 xlabel=xlabel, ylabel=ylabel, title=title,
378 379 ticksize=9, colormap=coherence_cmap, cblabel='')
379 380
380 381 title = "Phase %d%d" %(pair[0], pair[1])
381 382 axes0 = self.axesList[i*self.__nsubplots+3]
382 383 axes0.pcolor(x, y, phase,
383 384 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180,
384 385 xlabel=xlabel, ylabel=ylabel, title=title,
385 386 ticksize=9, colormap=phase_cmap, cblabel='')
386 387
387 388
388 389
389 390 self.draw()
390 391
391 392 self.save(figpath=figpath,
392 393 figfile=figfile,
393 394 save=save,
394 395 ftp=ftp,
395 396 wr_period=wr_period,
396 397 thisDatetime=thisDatetime)
397 398
398 399
399 400 class RTIPlot(Figure):
400 401
401 402 __isConfig = None
402 403 __nsubplots = None
403 404
404 405 WIDTHPROF = None
405 406 HEIGHTPROF = None
406 407 PREFIX = 'rti'
407 408
408 409 def __init__(self):
409 410
410 411 self.timerange = None
411 412 self.__isConfig = False
412 413 self.__nsubplots = 1
413 414
414 415 self.WIDTH = 800
415 416 self.HEIGHT = 150
416 417 self.WIDTHPROF = 120
417 418 self.HEIGHTPROF = 0
418 419 self.counter_imagwr = 0
419 420
420 421 self.PLOT_CODE = RTI_CODE
421 422
422 423 self.FTP_WEI = None
423 424 self.EXP_CODE = None
424 425 self.SUB_EXP_CODE = None
425 426 self.PLOT_POS = None
426 427 self.tmin = None
427 428 self.tmax = None
428 429
429 430 self.xmin = None
430 431 self.xmax = None
431 432
432 433 self.figfile = None
433 434
434 435 def getSubplots(self):
435 436
436 437 ncol = 1
437 438 nrow = self.nplots
438 439
439 440 return nrow, ncol
440 441
441 442 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
442 443
443 444 self.__showprofile = showprofile
444 445 self.nplots = nplots
445 446
446 447 ncolspan = 1
447 448 colspan = 1
448 449 if showprofile:
449 450 ncolspan = 7
450 451 colspan = 6
451 452 self.__nsubplots = 2
452 453
453 454 self.createFigure(id = id,
454 455 wintitle = wintitle,
455 456 widthplot = self.WIDTH + self.WIDTHPROF,
456 457 heightplot = self.HEIGHT + self.HEIGHTPROF,
457 458 show=show)
458 459
459 460 nrow, ncol = self.getSubplots()
460 461
461 462 counter = 0
462 463 for y in range(nrow):
463 464 for x in range(ncol):
464 465
465 466 if counter >= self.nplots:
466 467 break
467 468
468 469 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
469 470
470 471 if showprofile:
471 472 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
472 473
473 474 counter += 1
474 475
475 476 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
476 477 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
477 478 timerange=None,
478 479 save=False, figpath='./', lastone=0,figfile=None, ftp=False, wr_period=1, show=True,
479 480 server=None, folder=None, username=None, password=None,
480 481 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
481 482
482 483 """
483 484
484 485 Input:
485 486 dataOut :
486 487 id :
487 488 wintitle :
488 489 channelList :
489 490 showProfile :
490 491 xmin : None,
491 492 xmax : None,
492 493 ymin : None,
493 494 ymax : None,
494 495 zmin : None,
495 496 zmax : None
496 497 """
497 498
498 499 if channelList == None:
499 500 channelIndexList = dataOut.channelIndexList
500 501 else:
501 502 channelIndexList = []
502 503 for channel in channelList:
503 504 if channel not in dataOut.channelList:
504 505 raise ValueError, "Channel %d is not in dataOut.channelList"
505 506 channelIndexList.append(dataOut.channelList.index(channel))
506 507
507 508 # if timerange != None:
508 509 # self.timerange = timerange
509 510
510 511 #tmin = None
511 512 #tmax = None
512 513 factor = dataOut.normFactor
513 514 x = dataOut.getTimeRange()
514 515 y = dataOut.getHeiRange()
515 516
516 517 z = dataOut.data_spc[channelIndexList,:,:]/factor
517 518 z = numpy.where(numpy.isfinite(z), z, numpy.NAN)
518 519 avg = numpy.average(z, axis=1)
519 520
520 521 avgdB = 10.*numpy.log10(avg)
521 522
522 523
523 524 # thisDatetime = dataOut.datatime
524 525 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
525 526 title = wintitle + " RTI" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
526 527 xlabel = ""
527 528 ylabel = "Range (Km)"
528 529
529 530 if not self.__isConfig:
530 531
531 532 nplots = len(channelIndexList)
532 533
533 534 self.setup(id=id,
534 535 nplots=nplots,
535 536 wintitle=wintitle,
536 537 showprofile=showprofile,
537 538 show=show)
538 539
539 540 if timerange != None:
540 541 self.timerange = timerange
541 542
542 543 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
543 544
544 545 noise = dataOut.noise/factor
545 546 noisedB = 10*numpy.log10(noise)
546 547
547 548 if ymin == None: ymin = numpy.nanmin(y)
548 549 if ymax == None: ymax = numpy.nanmax(y)
549 550 if zmin == None: zmin = numpy.floor(numpy.nanmin(noisedB)) - 3
550 551 if zmax == None: zmax = numpy.ceil(numpy.nanmax(avgdB)) + 3
551 552
552 553 self.FTP_WEI = ftp_wei
553 554 self.EXP_CODE = exp_code
554 555 self.SUB_EXP_CODE = sub_exp_code
555 556 self.PLOT_POS = plot_pos
556 557
557 558 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
558 559 self.__isConfig = True
559 560 self.figfile = figfile
560 561
561 562 self.setWinTitle(title)
562 563
563 564 if ((self.xmax - x[1]) < (x[1]-x[0])):
564 565 x[1] = self.xmax
565 566
566 567 for i in range(self.nplots):
567 568 title = "Channel %d: %s" %(dataOut.channelList[i]+1, thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
568 569 if ((dataOut.azimuth!=None) and (dataOut.zenith!=None)):
569 570 title = title + '_' + 'azimuth,zenith=%2.2f,%2.2f'%(dataOut.azimuth, dataOut.zenith)
570 571 axes = self.axesList[i*self.__nsubplots]
571 572 zdB = avgdB[i].reshape((1,-1))
572 573 axes.pcolorbuffer(x, y, zdB,
573 574 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
574 575 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
575 576 ticksize=9, cblabel='', cbsize="1%")
576 577
577 578 if self.__showprofile:
578 579 axes = self.axesList[i*self.__nsubplots +1]
579 580 axes.pline(avgdB[i], y,
580 581 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
581 582 xlabel='dB', ylabel='', title='',
582 583 ytick_visible=False,
583 584 grid='x')
584 585
585 586 self.draw()
586 587
587 588 if x[1] >= self.axesList[0].xmax:
588 589 self.counter_imagwr = wr_period
589 590 self.__isConfig = False
590 591 self.figfile = None
591 592
592 593 self.save(figpath=figpath,
593 594 figfile=figfile,
594 595 save=save,
595 596 ftp=ftp,
596 597 wr_period=wr_period,
597 598 thisDatetime=thisDatetime,
598 599 update_figfile=False)
599 600
600 601 class CoherenceMap(Figure):
601 602 isConfig = None
602 603 __nsubplots = None
603 604
604 605 WIDTHPROF = None
605 606 HEIGHTPROF = None
606 607 PREFIX = 'cmap'
607 608
608 609 def __init__(self):
609 610 self.timerange = 2*60*60
610 611 self.isConfig = False
611 612 self.__nsubplots = 1
612 613
613 614 self.WIDTH = 800
614 615 self.HEIGHT = 150
615 616 self.WIDTHPROF = 120
616 617 self.HEIGHTPROF = 0
617 618 self.counter_imagwr = 0
618 619
619 620 self.PLOT_CODE = COH_CODE
620 621
621 622 self.FTP_WEI = None
622 623 self.EXP_CODE = None
623 624 self.SUB_EXP_CODE = None
624 625 self.PLOT_POS = None
625 626 self.counter_imagwr = 0
626 627
627 628 self.xmin = None
628 629 self.xmax = None
629 630
630 631 def getSubplots(self):
631 632 ncol = 1
632 633 nrow = self.nplots*2
633 634
634 635 return nrow, ncol
635 636
636 637 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
637 638 self.__showprofile = showprofile
638 639 self.nplots = nplots
639 640
640 641 ncolspan = 1
641 642 colspan = 1
642 643 if showprofile:
643 644 ncolspan = 7
644 645 colspan = 6
645 646 self.__nsubplots = 2
646 647
647 648 self.createFigure(id = id,
648 649 wintitle = wintitle,
649 650 widthplot = self.WIDTH + self.WIDTHPROF,
650 651 heightplot = self.HEIGHT + self.HEIGHTPROF,
651 652 show=True)
652 653
653 654 nrow, ncol = self.getSubplots()
654 655
655 656 for y in range(nrow):
656 657 for x in range(ncol):
657 658
658 659 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
659 660
660 661 if showprofile:
661 662 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
662 663
663 664 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
664 665 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
665 666 timerange=None,
666 667 save=False, figpath='./', figfile=None, ftp=False, wr_period=1,
667 668 coherence_cmap='jet', phase_cmap='RdBu_r', show=True,
668 669 server=None, folder=None, username=None, password=None,
669 670 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
670 671
671 672 if pairsList == None:
672 673 pairsIndexList = dataOut.pairsIndexList
673 674 else:
674 675 pairsIndexList = []
675 676 for pair in pairsList:
676 677 if pair not in dataOut.pairsList:
677 678 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
678 679 pairsIndexList.append(dataOut.pairsList.index(pair))
679 680
680 681 if pairsIndexList == []:
681 682 return
682 683
683 684 if len(pairsIndexList) > 4:
684 685 pairsIndexList = pairsIndexList[0:4]
685 686
686 687 # tmin = None
687 688 # tmax = None
688 689 x = dataOut.getTimeRange()
689 690 y = dataOut.getHeiRange()
690 691
691 692 #thisDatetime = dataOut.datatime
692 693 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
693 694 title = wintitle + " CoherenceMap" #: %s" %(thisDatetime.strftime("%d-%b-%Y"))
694 695 xlabel = ""
695 696 ylabel = "Range (Km)"
696 697
697 698 if not self.isConfig:
698 699 nplots = len(pairsIndexList)
699 700 self.setup(id=id,
700 701 nplots=nplots,
701 702 wintitle=wintitle,
702 703 showprofile=showprofile,
703 704 show=show)
704 705
705 706 if timerange != None:
706 707 self.timerange = timerange
707 708
708 709 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
709 710
710 711 if ymin == None: ymin = numpy.nanmin(y)
711 712 if ymax == None: ymax = numpy.nanmax(y)
712 713 if zmin == None: zmin = 0.
713 714 if zmax == None: zmax = 1.
714 715
715 716 self.FTP_WEI = ftp_wei
716 717 self.EXP_CODE = exp_code
717 718 self.SUB_EXP_CODE = sub_exp_code
718 719 self.PLOT_POS = plot_pos
719 720
720 721 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
721 722
722 723 self.isConfig = True
723 724
724 725 self.setWinTitle(title)
725 726
726 727 if ((self.xmax - x[1]) < (x[1]-x[0])):
727 728 x[1] = self.xmax
728 729
729 730 for i in range(self.nplots):
730 731
731 732 pair = dataOut.pairsList[pairsIndexList[i]]
732 733
733 734 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
734 735 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
735 736 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
736 737
737 738
738 739 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
739 740 coherence = numpy.abs(avgcoherenceComplex)
740 741
741 742 z = coherence.reshape((1,-1))
742 743
743 744 counter = 0
744 745
745 746 title = "Coherence %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
746 747 axes = self.axesList[i*self.__nsubplots*2]
747 748 axes.pcolorbuffer(x, y, z,
748 749 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
749 750 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
750 751 ticksize=9, cblabel='', colormap=coherence_cmap, cbsize="1%")
751 752
752 753 if self.__showprofile:
753 754 counter += 1
754 755 axes = self.axesList[i*self.__nsubplots*2 + counter]
755 756 axes.pline(coherence, y,
756 757 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
757 758 xlabel='', ylabel='', title='', ticksize=7,
758 759 ytick_visible=False, nxticks=5,
759 760 grid='x')
760 761
761 762 counter += 1
762 763
763 764 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
764 765
765 766 z = phase.reshape((1,-1))
766 767
767 768 title = "Phase %d%d: %s" %(pair[0], pair[1], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
768 769 axes = self.axesList[i*self.__nsubplots*2 + counter]
769 770 axes.pcolorbuffer(x, y, z,
770 771 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180,
771 772 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
772 773 ticksize=9, cblabel='', colormap=phase_cmap, cbsize="1%")
773 774
774 775 if self.__showprofile:
775 776 counter += 1
776 777 axes = self.axesList[i*self.__nsubplots*2 + counter]
777 778 axes.pline(phase, y,
778 779 xmin=-180, xmax=180, ymin=ymin, ymax=ymax,
779 780 xlabel='', ylabel='', title='', ticksize=7,
780 781 ytick_visible=False, nxticks=4,
781 782 grid='x')
782 783
783 784 self.draw()
784 785
785 786 if x[1] >= self.axesList[0].xmax:
786 787 self.counter_imagwr = wr_period
787 788 self.__isConfig = False
788 789 self.figfile = None
789 790
790 791 self.save(figpath=figpath,
791 792 figfile=figfile,
792 793 save=save,
793 794 ftp=ftp,
794 795 wr_period=wr_period,
795 796 thisDatetime=thisDatetime,
796 797 update_figfile=False)
797 798
798 799 class PowerProfilePlot(Figure):
799 800
800 801 isConfig = None
801 802 __nsubplots = None
802 803
803 804 WIDTHPROF = None
804 805 HEIGHTPROF = None
805 806 PREFIX = 'spcprofile'
806 807
807 808 def __init__(self):
808 809 self.isConfig = False
809 810 self.__nsubplots = 1
810 811
811 812 self.PLOT_CODE = POWER_CODE
812 813
813 814 self.WIDTH = 300
814 815 self.HEIGHT = 500
815 816 self.counter_imagwr = 0
816 817
817 818 def getSubplots(self):
818 819 ncol = 1
819 820 nrow = 1
820 821
821 822 return nrow, ncol
822 823
823 824 def setup(self, id, nplots, wintitle, show):
824 825
825 826 self.nplots = nplots
826 827
827 828 ncolspan = 1
828 829 colspan = 1
829 830
830 831 self.createFigure(id = id,
831 832 wintitle = wintitle,
832 833 widthplot = self.WIDTH,
833 834 heightplot = self.HEIGHT,
834 835 show=show)
835 836
836 837 nrow, ncol = self.getSubplots()
837 838
838 839 counter = 0
839 840 for y in range(nrow):
840 841 for x in range(ncol):
841 842 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
842 843
843 844 def run(self, dataOut, id, wintitle="", channelList=None,
844 845 xmin=None, xmax=None, ymin=None, ymax=None,
845 846 save=False, figpath='./', figfile=None, show=True,
846 847 ftp=False, wr_period=1, server=None,
847 848 folder=None, username=None, password=None):
848 849
849 850
850 851 if channelList == None:
851 852 channelIndexList = dataOut.channelIndexList
852 853 channelList = dataOut.channelList
853 854 else:
854 855 channelIndexList = []
855 856 for channel in channelList:
856 857 if channel not in dataOut.channelList:
857 858 raise ValueError, "Channel %d is not in dataOut.channelList"
858 859 channelIndexList.append(dataOut.channelList.index(channel))
859 860
860 861 factor = dataOut.normFactor
861 862
862 863 y = dataOut.getHeiRange()
863 864
864 865 #for voltage
865 866 if dataOut.type == 'Voltage':
866 867 x = dataOut.data[channelIndexList,:] * numpy.conjugate(dataOut.data[channelIndexList,:])
867 868 x = x.real
868 869 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
869 870
870 871 #for spectra
871 872 if dataOut.type == 'Spectra':
872 873 x = dataOut.data_spc[channelIndexList,:,:]/factor
873 874 x = numpy.where(numpy.isfinite(x), x, numpy.NAN)
874 875 x = numpy.average(x, axis=1)
875 876
876 877
877 878 xdB = 10*numpy.log10(x)
878 879
879 880 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
880 881 title = wintitle + " Power Profile %s" %(thisDatetime.strftime("%d-%b-%Y"))
881 882 xlabel = "dB"
882 883 ylabel = "Range (Km)"
883 884
884 885 if not self.isConfig:
885 886
886 887 nplots = 1
887 888
888 889 self.setup(id=id,
889 890 nplots=nplots,
890 891 wintitle=wintitle,
891 892 show=show)
892 893
893 894 if ymin == None: ymin = numpy.nanmin(y)
894 895 if ymax == None: ymax = numpy.nanmax(y)
895 896 if xmin == None: xmin = numpy.nanmin(xdB)*0.9
896 897 if xmax == None: xmax = numpy.nanmax(xdB)*1.1
897 898
898 899 self.__isConfig = True
899 900
900 901 self.setWinTitle(title)
901 902
902 903 title = "Power Profile: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
903 904 axes = self.axesList[0]
904 905
905 906 legendlabels = ["channel %d"%x for x in channelList]
906 907 axes.pmultiline(xdB, y,
907 908 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
908 909 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels,
909 910 ytick_visible=True, nxticks=5,
910 911 grid='x')
911 912
912 913 self.draw()
913 914
914 915 self.save(figpath=figpath,
915 916 figfile=figfile,
916 917 save=save,
917 918 ftp=ftp,
918 919 wr_period=wr_period,
919 920 thisDatetime=thisDatetime)
920 921
921 922 class Noise(Figure):
922 923
923 924 isConfig = None
924 925 __nsubplots = None
925 926
926 927 PREFIX = 'noise'
927 928
928 929 def __init__(self):
929 930
930 931 self.timerange = 24*60*60
931 932 self.isConfig = False
932 933 self.__nsubplots = 1
933 934 self.counter_imagwr = 0
934 935 self.WIDTH = 600
935 936 self.HEIGHT = 300
936 937 self.WIDTHPROF = 120
937 938 self.HEIGHTPROF = 0
938 939 self.xdata = None
939 940 self.ydata = None
940 941
941 942 self.PLOT_CODE = NOISE_CODE
942 943
943 944 self.FTP_WEI = None
944 945 self.EXP_CODE = None
945 946 self.SUB_EXP_CODE = None
946 947 self.PLOT_POS = None
947 948 self.figfile = None
948 949
949 950 self.xmin = None
950 951 self.xmax = None
951 952
952 953 def getSubplots(self):
953 954
954 955 ncol = 1
955 956 nrow = 1
956 957
957 958 return nrow, ncol
958 959
959 960 def openfile(self, filename):
960 961 dirname = os.path.dirname(filename)
961 962
962 963 if not os.path.exists(dirname):
963 964 os.mkdir(dirname)
964 965
965 966 f = open(filename,'w+')
966 967 f.write('\n\n')
967 968 f.write('JICAMARCA RADIO OBSERVATORY - Noise \n')
968 969 f.write('DD MM YYYY HH MM SS Channel0 Channel1 Channel2 Channel3\n\n' )
969 970 f.close()
970 971
971 972 def save_data(self, filename_phase, data, data_datetime):
972 973 f=open(filename_phase,'a')
973 974 timetuple_data = data_datetime.timetuple()
974 975 day = str(timetuple_data.tm_mday)
975 976 month = str(timetuple_data.tm_mon)
976 977 year = str(timetuple_data.tm_year)
977 978 hour = str(timetuple_data.tm_hour)
978 979 minute = str(timetuple_data.tm_min)
979 980 second = str(timetuple_data.tm_sec)
980 981
981 982 data_msg = ''
982 983 for i in range(len(data)):
983 984 data_msg += str(data[i]) + ' '
984 985
985 986 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' ' + data_msg + '\n')
986 987 f.close()
987 988
988 989
989 990 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
990 991
991 992 self.__showprofile = showprofile
992 993 self.nplots = nplots
993 994
994 995 ncolspan = 7
995 996 colspan = 6
996 997 self.__nsubplots = 2
997 998
998 999 self.createFigure(id = id,
999 1000 wintitle = wintitle,
1000 1001 widthplot = self.WIDTH+self.WIDTHPROF,
1001 1002 heightplot = self.HEIGHT+self.HEIGHTPROF,
1002 1003 show=show)
1003 1004
1004 1005 nrow, ncol = self.getSubplots()
1005 1006
1006 1007 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1007 1008
1008 1009
1009 1010 def run(self, dataOut, id, wintitle="", channelList=None, showprofile='True',
1010 1011 xmin=None, xmax=None, ymin=None, ymax=None,
1011 1012 timerange=None,
1012 1013 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1013 1014 server=None, folder=None, username=None, password=None,
1014 1015 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1015 1016
1016 1017 if channelList == None:
1017 1018 channelIndexList = dataOut.channelIndexList
1018 1019 channelList = dataOut.channelList
1019 1020 else:
1020 1021 channelIndexList = []
1021 1022 for channel in channelList:
1022 1023 if channel not in dataOut.channelList:
1023 1024 raise ValueError, "Channel %d is not in dataOut.channelList"
1024 1025 channelIndexList.append(dataOut.channelList.index(channel))
1025 1026
1026 1027 x = dataOut.getTimeRange()
1027 1028 #y = dataOut.getHeiRange()
1028 1029 factor = dataOut.normFactor
1029 1030 noise = dataOut.noise/factor
1030 1031 noisedB = 10*numpy.log10(noise)
1031 1032
1032 1033 #thisDatetime = dataOut.datatime
1033 1034 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1034 1035 title = wintitle + " Noise" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1035 1036 xlabel = ""
1036 1037 ylabel = "Intensity (dB)"
1037 1038
1038 1039 if not self.isConfig:
1039 1040
1040 1041 nplots = 1
1041 1042
1042 1043 self.setup(id=id,
1043 1044 nplots=nplots,
1044 1045 wintitle=wintitle,
1045 1046 showprofile=showprofile,
1046 1047 show=show)
1047 1048
1048 1049 if timerange != None:
1049 1050 self.timerange = timerange
1050 1051
1051 1052 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1052 1053
1053 1054 if ymin == None: ymin = numpy.floor(numpy.nanmin(noisedB)) - 10.0
1054 1055 if ymax == None: ymax = numpy.nanmax(noisedB) + 10.0
1055 1056
1056 1057 self.FTP_WEI = ftp_wei
1057 1058 self.EXP_CODE = exp_code
1058 1059 self.SUB_EXP_CODE = sub_exp_code
1059 1060 self.PLOT_POS = plot_pos
1060 1061
1061 1062
1062 1063 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1063 1064 self.isConfig = True
1064 1065 self.figfile = figfile
1065 1066 self.xdata = numpy.array([])
1066 1067 self.ydata = numpy.array([])
1067 1068
1068 1069 #open file beacon phase
1069 1070 path = '%s%03d' %(self.PREFIX, self.id)
1070 1071 noise_file = os.path.join(path,'%s.txt'%self.name)
1071 1072 self.filename_noise = os.path.join(figpath,noise_file)
1072 1073 if save:
1073 1074 self.openfile(self.filename_noise)
1074 1075
1075 1076
1076 1077 #store data beacon phase
1077 1078 if save:
1078 1079 self.save_data(self.filename_noise, noisedB, thisDatetime)
1079 1080
1080 1081
1081 1082 self.setWinTitle(title)
1082 1083
1083 1084
1084 1085 title = "Noise %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1085 1086
1086 1087 legendlabels = ["channel %d"%(idchannel+1) for idchannel in channelList]
1087 1088 axes = self.axesList[0]
1088 1089
1089 1090 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1090 1091
1091 1092 if len(self.ydata)==0:
1092 1093 self.ydata = noisedB[channelIndexList].reshape(-1,1)
1093 1094 else:
1094 1095 self.ydata = numpy.hstack((self.ydata, noisedB[channelIndexList].reshape(-1,1)))
1095 1096
1096 1097
1097 1098 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1098 1099 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1099 1100 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1100 1101 XAxisAsTime=True, grid='both'
1101 1102 )
1102 1103
1103 1104 self.draw()
1104 1105
1105 1106 if x[1] >= self.axesList[0].xmax:
1106 1107 self.counter_imagwr = wr_period
1107 1108 del self.xdata
1108 1109 del self.ydata
1109 1110 self.__isConfig = False
1110 1111 self.figfile = None
1111 1112
1112 1113 self.save(figpath=figpath,
1113 1114 figfile=figfile,
1114 1115 save=save,
1115 1116 ftp=ftp,
1116 1117 wr_period=wr_period,
1117 1118 thisDatetime=thisDatetime,
1118 1119 update_figfile=False)
1119 1120
1120 1121
1121 1122 class BeaconPhase(Figure):
1122 1123
1123 1124 __isConfig = None
1124 1125 __nsubplots = None
1125 1126
1126 1127 PREFIX = 'beacon_phase'
1127 1128
1128 1129 def __init__(self):
1129 1130
1130 1131 self.timerange = 24*60*60
1131 1132 self.__isConfig = False
1132 1133 self.__nsubplots = 1
1133 1134 self.counter_imagwr = 0
1134 1135 self.WIDTH = 600
1135 1136 self.HEIGHT = 300
1136 1137 self.WIDTHPROF = 120
1137 1138 self.HEIGHTPROF = 0
1138 1139 self.xdata = None
1139 1140 self.ydata = None
1140 1141
1141 1142 self.PLOT_CODE = BEACON_CODE
1142 1143
1143 1144 self.FTP_WEI = None
1144 1145 self.EXP_CODE = None
1145 1146 self.SUB_EXP_CODE = None
1146 1147 self.PLOT_POS = None
1147 1148
1148 1149 self.filename_phase = None
1149 1150
1150 1151 self.figfile = None
1151 1152
1152 1153 self.xmin = None
1153 1154 self.xmax = None
1154 1155
1155 1156 def getSubplots(self):
1156 1157
1157 1158 ncol = 1
1158 1159 nrow = 1
1159 1160
1160 1161 return nrow, ncol
1161 1162
1162 1163 def setup(self, id, nplots, wintitle, showprofile=True, show=True):
1163 1164
1164 1165 self.__showprofile = showprofile
1165 1166 self.nplots = nplots
1166 1167
1167 1168 ncolspan = 7
1168 1169 colspan = 6
1169 1170 self.__nsubplots = 2
1170 1171
1171 1172 self.createFigure(id = id,
1172 1173 wintitle = wintitle,
1173 1174 widthplot = self.WIDTH+self.WIDTHPROF,
1174 1175 heightplot = self.HEIGHT+self.HEIGHTPROF,
1175 1176 show=show)
1176 1177
1177 1178 nrow, ncol = self.getSubplots()
1178 1179
1179 1180 self.addAxes(nrow, ncol*ncolspan, 0, 0, colspan, 1)
1180 1181
1181 1182 def save_phase(self, filename_phase):
1182 1183 f = open(filename_phase,'w+')
1183 1184 f.write('\n\n')
1184 1185 f.write('JICAMARCA RADIO OBSERVATORY - Beacon Phase \n')
1185 1186 f.write('DD MM YYYY HH MM SS pair(2,0) pair(2,1) pair(2,3) pair(2,4)\n\n' )
1186 1187 f.close()
1187 1188
1188 1189 def save_data(self, filename_phase, data, data_datetime):
1189 1190 f=open(filename_phase,'a')
1190 1191 timetuple_data = data_datetime.timetuple()
1191 1192 day = str(timetuple_data.tm_mday)
1192 1193 month = str(timetuple_data.tm_mon)
1193 1194 year = str(timetuple_data.tm_year)
1194 1195 hour = str(timetuple_data.tm_hour)
1195 1196 minute = str(timetuple_data.tm_min)
1196 1197 second = str(timetuple_data.tm_sec)
1197 1198 f.write(day+' '+month+' '+year+' '+hour+' '+minute+' '+second+' '+str(data[0])+' '+str(data[1])+' '+str(data[2])+' '+str(data[3])+'\n')
1198 1199 f.close()
1199 1200
1200 1201
1201 1202 def run(self, dataOut, id, wintitle="", pairsList=None, showprofile='True',
1202 1203 xmin=None, xmax=None, ymin=None, ymax=None,
1203 1204 timerange=None,
1204 1205 save=False, figpath='./', figfile=None, show=True, ftp=False, wr_period=1,
1205 1206 server=None, folder=None, username=None, password=None,
1206 1207 ftp_wei=0, exp_code=0, sub_exp_code=0, plot_pos=0):
1207 1208
1208 1209 if pairsList == None:
1209 1210 pairsIndexList = dataOut.pairsIndexList
1210 1211 else:
1211 1212 pairsIndexList = []
1212 1213 for pair in pairsList:
1213 1214 if pair not in dataOut.pairsList:
1214 1215 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
1215 1216 pairsIndexList.append(dataOut.pairsList.index(pair))
1216 1217
1217 1218 if pairsIndexList == []:
1218 1219 return
1219 1220
1220 1221 # if len(pairsIndexList) > 4:
1221 1222 # pairsIndexList = pairsIndexList[0:4]
1222 1223
1223 1224 x = dataOut.getTimeRange()
1224 1225 #y = dataOut.getHeiRange()
1225 1226
1226 1227
1227 1228 #thisDatetime = dataOut.datatime
1228 1229 thisDatetime = datetime.datetime.utcfromtimestamp(dataOut.getTimeRange()[0])
1229 1230 title = wintitle + " Phase of Beacon Signal" # : %s" %(thisDatetime.strftime("%d-%b-%Y"))
1230 1231 xlabel = "Local Time"
1231 1232 ylabel = "Phase"
1232 1233
1233 1234 nplots = len(pairsIndexList)
1234 1235 #phase = numpy.zeros((len(pairsIndexList),len(dataOut.beacon_heiIndexList)))
1235 1236 phase_beacon = numpy.zeros(len(pairsIndexList))
1236 1237 for i in range(nplots):
1237 1238 pair = dataOut.pairsList[pairsIndexList[i]]
1238 1239 ccf = numpy.average(dataOut.data_cspc[pairsIndexList[i],:,:],axis=0)
1239 1240 powa = numpy.average(dataOut.data_spc[pair[0],:,:],axis=0)
1240 1241 powb = numpy.average(dataOut.data_spc[pair[1],:,:],axis=0)
1241 1242 avgcoherenceComplex = ccf/numpy.sqrt(powa*powb)
1242 1243 phase = numpy.arctan2(avgcoherenceComplex.imag, avgcoherenceComplex.real)*180/numpy.pi
1243 1244
1244 1245 #print "Phase %d%d" %(pair[0], pair[1])
1245 1246 #print phase[dataOut.beacon_heiIndexList]
1246 1247
1247 1248 phase_beacon[i] = numpy.average(phase[dataOut.beacon_heiIndexList])
1248 1249
1249 1250 if not self.__isConfig:
1250 1251
1251 1252 nplots = len(pairsIndexList)
1252 1253
1253 1254 self.setup(id=id,
1254 1255 nplots=nplots,
1255 1256 wintitle=wintitle,
1256 1257 showprofile=showprofile,
1257 1258 show=show)
1258 1259
1259 1260 if timerange != None:
1260 1261 self.timerange = timerange
1261 1262
1262 1263 self.xmin, self.xmax = self.getTimeLim(x, xmin, xmax, timerange)
1263 1264
1264 1265 if ymin == None: ymin = numpy.nanmin(phase_beacon) - 10.0
1265 1266 if ymax == None: ymax = numpy.nanmax(phase_beacon) + 10.0
1266 1267
1267 1268 self.FTP_WEI = ftp_wei
1268 1269 self.EXP_CODE = exp_code
1269 1270 self.SUB_EXP_CODE = sub_exp_code
1270 1271 self.PLOT_POS = plot_pos
1271 1272
1272 1273 self.name = thisDatetime.strftime("%Y%m%d_%H%M%S")
1273 1274 self.__isConfig = True
1274 1275 self.figfile = figfile
1275 1276 self.xdata = numpy.array([])
1276 1277 self.ydata = numpy.array([])
1277 1278
1278 1279 #open file beacon phase
1279 1280 path = '%s%03d' %(self.PREFIX, self.id)
1280 1281 beacon_file = os.path.join(path,'%s.txt'%self.name)
1281 1282 self.filename_phase = os.path.join(figpath,beacon_file)
1282 1283 #self.save_phase(self.filename_phase)
1283 1284
1284 1285
1285 1286 #store data beacon phase
1286 1287 #self.save_data(self.filename_phase, phase_beacon, thisDatetime)
1287 1288
1288 1289 self.setWinTitle(title)
1289 1290
1290 1291
1291 1292 title = "Beacon Signal %s" %(thisDatetime.strftime("%Y/%m/%d %H:%M:%S"))
1292 1293
1293 1294 legendlabels = ["pairs %d%d"%(pair[0], pair[1]) for pair in dataOut.pairsList]
1294 1295
1295 1296 axes = self.axesList[0]
1296 1297
1297 1298 self.xdata = numpy.hstack((self.xdata, x[0:1]))
1298 1299
1299 1300 if len(self.ydata)==0:
1300 1301 self.ydata = phase_beacon.reshape(-1,1)
1301 1302 else:
1302 1303 self.ydata = numpy.hstack((self.ydata, phase_beacon.reshape(-1,1)))
1303 1304
1304 1305
1305 1306 axes.pmultilineyaxis(x=self.xdata, y=self.ydata,
1306 1307 xmin=self.xmin, xmax=self.xmax, ymin=ymin, ymax=ymax,
1307 1308 xlabel=xlabel, ylabel=ylabel, title=title, legendlabels=legendlabels, marker='x', markersize=8, linestyle="solid",
1308 1309 XAxisAsTime=True, grid='both'
1309 1310 )
1310 1311
1311 1312 self.draw()
1312 1313
1313 1314 if x[1] >= self.axesList[0].xmax:
1314 1315 self.counter_imagwr = wr_period
1315 1316 del self.xdata
1316 1317 del self.ydata
1317 1318 self.__isConfig = False
1318 1319 self.figfile = None
1319 1320
1320 1321 self.save(figpath=figpath,
1321 1322 figfile=figfile,
1322 1323 save=save,
1323 1324 ftp=ftp,
1324 1325 wr_period=wr_period,
1325 1326 thisDatetime=thisDatetime,
1326 1327 update_figfile=False)
@@ -1,425 +1,435
1 1 import numpy
2 2 import datetime
3 3 import sys
4 4 import matplotlib
5 5
6 6 if 'linux' in sys.platform:
7 7 matplotlib.use("TKAgg")
8 8
9 9 if 'darwin' in sys.platform:
10 10 matplotlib.use("WXAgg")
11 11 #Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'
12 12 import matplotlib.pyplot
13 13
14 14 from mpl_toolkits.axes_grid1 import make_axes_locatable
15 15 from matplotlib.ticker import *
16 16
17 17 ###########################################
18 18 #Actualizacion de las funciones del driver
19 19 ###########################################
20 20
21 21 def createFigure(id, wintitle, width, height, facecolor="w", show=True):
22 22
23 23 matplotlib.pyplot.ioff()
24 24 fig = matplotlib.pyplot.figure(num=id, facecolor=facecolor)
25 25 fig.canvas.manager.set_window_title(wintitle)
26 26 fig.canvas.manager.resize(width, height)
27 27 matplotlib.pyplot.ion()
28 28 if show:
29 29 matplotlib.pyplot.show()
30 30
31 31 return fig
32 32
33 def closeFigure(show=False):
33 def closeFigure(show=False, fig=None):
34 34
35 35 matplotlib.pyplot.ioff()
36 matplotlib.pyplot.pause(0.1)
37
36 38 if show:
37 39 matplotlib.pyplot.show()
38
39 matplotlib.pyplot.close()
40
41 if fig != None:
42 matplotlib.pyplot.close(fig)
43 matplotlib.pyplot.pause(0.1)
44 matplotlib.pyplot.ion()
45 return
46
47 matplotlib.pyplot.close("all")
48 matplotlib.pyplot.pause(0.1)
49 matplotlib.pyplot.ion()
40 50 return
41 51
42 52 def saveFigure(fig, filename):
43 53
44 54 matplotlib.pyplot.ioff()
45 55 fig.savefig(filename)
46 56 matplotlib.pyplot.ion()
47 57
48 58 def setWinTitle(fig, title):
49 59
50 60 fig.canvas.manager.set_window_title(title)
51 61
52 62 def setTitle(fig, title):
53 63
54 64 fig.suptitle(title)
55 65
56 66 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan, polar=False):
57 67
58 68 matplotlib.pyplot.ioff()
59 69 matplotlib.pyplot.figure(fig.number)
60 70 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
61 71 (xpos, ypos),
62 72 colspan=colspan,
63 73 rowspan=rowspan,
64 74 polar=polar)
65 75
66 76 matplotlib.pyplot.ion()
67 77 return axes
68 78
69 79 def setAxesText(ax, text):
70 80
71 81 ax.annotate(text,
72 82 xy = (.1, .99),
73 83 xycoords = 'figure fraction',
74 84 horizontalalignment = 'left',
75 85 verticalalignment = 'top',
76 86 fontsize = 10)
77 87
78 88 def printLabels(ax, xlabel, ylabel, title):
79 89
80 90 ax.set_xlabel(xlabel, size=11)
81 91 ax.set_ylabel(ylabel, size=11)
82 92 ax.set_title(title, size=8)
83 93
84 94 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='',
85 95 ticksize=9, xtick_visible=True, ytick_visible=True,
86 96 nxticks=4, nyticks=10,
87 97 grid=None,color='blue'):
88 98
89 99 """
90 100
91 101 Input:
92 102 grid : None, 'both', 'x', 'y'
93 103 """
94 104
95 105 matplotlib.pyplot.ioff()
96 106
97 107 ax.set_xlim([xmin,xmax])
98 108 ax.set_ylim([ymin,ymax])
99 109
100 110 printLabels(ax, xlabel, ylabel, title)
101 111
102 112 ######################################################
103 113 if (xmax-xmin)<=1:
104 114 xtickspos = numpy.linspace(xmin,xmax,nxticks)
105 115 xtickspos = numpy.array([float("%.1f"%i) for i in xtickspos])
106 116 ax.set_xticks(xtickspos)
107 117 else:
108 118 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
109 119 # xtickspos = numpy.arange(nxticks)*float(xmax-xmin)/float(nxticks) + int(xmin)
110 120 ax.set_xticks(xtickspos)
111 121
112 122 for tick in ax.get_xticklabels():
113 123 tick.set_visible(xtick_visible)
114 124
115 125 for tick in ax.xaxis.get_major_ticks():
116 126 tick.label.set_fontsize(ticksize)
117 127
118 128 ######################################################
119 129 for tick in ax.get_yticklabels():
120 130 tick.set_visible(ytick_visible)
121 131
122 132 for tick in ax.yaxis.get_major_ticks():
123 133 tick.label.set_fontsize(ticksize)
124 134
125 135 ax.plot(x, y, color=color)
126 136 iplot = ax.lines[-1]
127 137
128 138 ######################################################
129 139 if '0.' in matplotlib.__version__[0:2]:
130 140 print "The matplotlib version has to be updated to 1.1 or newer"
131 141 return iplot
132 142
133 143 if '1.0.' in matplotlib.__version__[0:4]:
134 144 print "The matplotlib version has to be updated to 1.1 or newer"
135 145 return iplot
136 146
137 147 if grid != None:
138 148 ax.grid(b=True, which='major', axis=grid)
139 149
140 150 matplotlib.pyplot.tight_layout()
141 151
142 152 matplotlib.pyplot.ion()
143 153
144 154 return iplot
145 155
146 156 def set_linedata(ax, x, y, idline):
147 157
148 158 ax.lines[idline].set_data(x,y)
149 159
150 160 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
151 161
152 162 ax = iplot.get_axes()
153 163
154 164 printLabels(ax, xlabel, ylabel, title)
155 165
156 166 set_linedata(ax, x, y, idline=0)
157 167
158 168 def addpline(ax, x, y, color, linestyle, lw):
159 169
160 170 ax.plot(x,y,color=color,linestyle=linestyle,lw=lw)
161 171
162 172
163 173 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
164 174 xlabel='', ylabel='', title='', ticksize = 9,
165 175 colormap='jet',cblabel='', cbsize="5%",
166 176 XAxisAsTime=False):
167 177
168 178 matplotlib.pyplot.ioff()
169 179
170 180 divider = make_axes_locatable(ax)
171 181 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
172 182 fig = ax.get_figure()
173 183 fig.add_axes(ax_cb)
174 184
175 185 ax.set_xlim([xmin,xmax])
176 186 ax.set_ylim([ymin,ymax])
177 187
178 188 printLabels(ax, xlabel, ylabel, title)
179 189
180 190 imesh = ax.pcolormesh(x,y,z.T, vmin=zmin, vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
181 191 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
182 192 cb.set_label(cblabel)
183 193
184 194 # for tl in ax_cb.get_yticklabels():
185 195 # tl.set_visible(True)
186 196
187 197 for tick in ax.yaxis.get_major_ticks():
188 198 tick.label.set_fontsize(ticksize)
189 199
190 200 for tick in ax.xaxis.get_major_ticks():
191 201 tick.label.set_fontsize(ticksize)
192 202
193 203 for tick in cb.ax.get_yticklabels():
194 204 tick.set_fontsize(ticksize)
195 205
196 206 ax_cb.yaxis.tick_right()
197 207
198 208 if '0.' in matplotlib.__version__[0:2]:
199 209 print "The matplotlib version has to be updated to 1.1 or newer"
200 210 return imesh
201 211
202 212 if '1.0.' in matplotlib.__version__[0:4]:
203 213 print "The matplotlib version has to be updated to 1.1 or newer"
204 214 return imesh
205 215
206 216 matplotlib.pyplot.tight_layout()
207 217
208 218 if XAxisAsTime:
209 219
210 220 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
211 221 ax.xaxis.set_major_formatter(FuncFormatter(func))
212 222 ax.xaxis.set_major_locator(LinearLocator(7))
213 223
214 224 matplotlib.pyplot.ion()
215 225 return imesh
216 226
217 227 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
218 228
219 229 z = z.T
220 230 ax = imesh.get_axes()
221 231 printLabels(ax, xlabel, ylabel, title)
222 232 imesh.set_array(z.ravel())
223 233
224 234 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
225 235
226 236 printLabels(ax, xlabel, ylabel, title)
227 237
228 238 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
229 239
230 240 def addpcolorbuffer(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title='', colormap='jet'):
231 241
232 242 printLabels(ax, xlabel, ylabel, title)
233 243
234 244 ax.collections.remove(ax.collections[0])
235 245
236 246 ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax, cmap=matplotlib.pyplot.get_cmap(colormap))
237 247
238 248 def createPmultiline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
239 249 ticksize=9, xtick_visible=True, ytick_visible=True,
240 250 nxticks=4, nyticks=10,
241 251 grid=None):
242 252
243 253 """
244 254
245 255 Input:
246 256 grid : None, 'both', 'x', 'y'
247 257 """
248 258
249 259 matplotlib.pyplot.ioff()
250 260
251 261 lines = ax.plot(x.T, y)
252 262 leg = ax.legend(lines, legendlabels, loc='upper right')
253 263 leg.get_frame().set_alpha(0.5)
254 264 ax.set_xlim([xmin,xmax])
255 265 ax.set_ylim([ymin,ymax])
256 266 printLabels(ax, xlabel, ylabel, title)
257 267
258 268 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
259 269 ax.set_xticks(xtickspos)
260 270
261 271 for tick in ax.get_xticklabels():
262 272 tick.set_visible(xtick_visible)
263 273
264 274 for tick in ax.xaxis.get_major_ticks():
265 275 tick.label.set_fontsize(ticksize)
266 276
267 277 for tick in ax.get_yticklabels():
268 278 tick.set_visible(ytick_visible)
269 279
270 280 for tick in ax.yaxis.get_major_ticks():
271 281 tick.label.set_fontsize(ticksize)
272 282
273 283 iplot = ax.lines[-1]
274 284
275 285 if '0.' in matplotlib.__version__[0:2]:
276 286 print "The matplotlib version has to be updated to 1.1 or newer"
277 287 return iplot
278 288
279 289 if '1.0.' in matplotlib.__version__[0:4]:
280 290 print "The matplotlib version has to be updated to 1.1 or newer"
281 291 return iplot
282 292
283 293 if grid != None:
284 294 ax.grid(b=True, which='major', axis=grid)
285 295
286 296 matplotlib.pyplot.tight_layout()
287 297
288 298 matplotlib.pyplot.ion()
289 299
290 300 return iplot
291 301
292 302
293 303 def pmultiline(iplot, x, y, xlabel='', ylabel='', title=''):
294 304
295 305 ax = iplot.get_axes()
296 306
297 307 printLabels(ax, xlabel, ylabel, title)
298 308
299 309 for i in range(len(ax.lines)):
300 310 line = ax.lines[i]
301 311 line.set_data(x[i,:],y)
302 312
303 313 def createPmultilineYAxis(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='', legendlabels=None,
304 314 ticksize=9, xtick_visible=True, ytick_visible=True,
305 315 nxticks=4, nyticks=10, marker='.', markersize=10, linestyle="None",
306 316 grid=None, XAxisAsTime=False):
307 317
308 318 """
309 319
310 320 Input:
311 321 grid : None, 'both', 'x', 'y'
312 322 """
313 323
314 324 matplotlib.pyplot.ioff()
315 325
316 326 # lines = ax.plot(x, y.T, marker=marker,markersize=markersize,linestyle=linestyle)
317 327 lines = ax.plot(x, y.T, linestyle=linestyle, marker=marker, markersize=markersize)
318 328 leg = ax.legend(lines, legendlabels, loc='upper left', bbox_to_anchor=(1.01, 1.00), numpoints=1, handlelength=1.5, \
319 329 handletextpad=0.5, borderpad=0.5, labelspacing=0.5, borderaxespad=0.)
320 330
321 331 for label in leg.get_texts(): label.set_fontsize(9)
322 332
323 333 ax.set_xlim([xmin,xmax])
324 334 ax.set_ylim([ymin,ymax])
325 335 printLabels(ax, xlabel, ylabel, title)
326 336
327 337 # xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/(nxticks)) + int(xmin)
328 338 # ax.set_xticks(xtickspos)
329 339
330 340 for tick in ax.get_xticklabels():
331 341 tick.set_visible(xtick_visible)
332 342
333 343 for tick in ax.xaxis.get_major_ticks():
334 344 tick.label.set_fontsize(ticksize)
335 345
336 346 for tick in ax.get_yticklabels():
337 347 tick.set_visible(ytick_visible)
338 348
339 349 for tick in ax.yaxis.get_major_ticks():
340 350 tick.label.set_fontsize(ticksize)
341 351
342 352 iplot = ax.lines[-1]
343 353
344 354 if '0.' in matplotlib.__version__[0:2]:
345 355 print "The matplotlib version has to be updated to 1.1 or newer"
346 356 return iplot
347 357
348 358 if '1.0.' in matplotlib.__version__[0:4]:
349 359 print "The matplotlib version has to be updated to 1.1 or newer"
350 360 return iplot
351 361
352 362 if grid != None:
353 363 ax.grid(b=True, which='major', axis=grid)
354 364
355 365 matplotlib.pyplot.tight_layout()
356 366
357 367 if XAxisAsTime:
358 368
359 369 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
360 370 ax.xaxis.set_major_formatter(FuncFormatter(func))
361 371 ax.xaxis.set_major_locator(LinearLocator(7))
362 372
363 373 matplotlib.pyplot.ion()
364 374
365 375 return iplot
366 376
367 377 def pmultilineyaxis(iplot, x, y, xlabel='', ylabel='', title=''):
368 378
369 379 ax = iplot.get_axes()
370 380
371 381 printLabels(ax, xlabel, ylabel, title)
372 382
373 383 for i in range(len(ax.lines)):
374 384 line = ax.lines[i]
375 385 line.set_data(x,y[i,:])
376 386
377 387 def createPolar(ax, x, y,
378 388 xlabel='', ylabel='', title='', ticksize = 9,
379 389 colormap='jet',cblabel='', cbsize="5%",
380 390 XAxisAsTime=False):
381 391
382 392 matplotlib.pyplot.ioff()
383 393
384 394 ax.plot(x,y,'bo', markersize=5)
385 395 # ax.set_rmax(90)
386 396 ax.set_ylim(0,90)
387 397 ax.set_yticks(numpy.arange(0,90,20))
388 398 ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center' ,size='11')
389 399 # ax.text(100, 100, 'example', ha='left', va='center', rotation='vertical')
390 400 printLabels(ax, xlabel, '', title)
391 401 iplot = ax.lines[-1]
392 402
393 403 if '0.' in matplotlib.__version__[0:2]:
394 404 print "The matplotlib version has to be updated to 1.1 or newer"
395 405 return iplot
396 406
397 407 if '1.0.' in matplotlib.__version__[0:4]:
398 408 print "The matplotlib version has to be updated to 1.1 or newer"
399 409 return iplot
400 410
401 411 # if grid != None:
402 412 # ax.grid(b=True, which='major', axis=grid)
403 413
404 414 matplotlib.pyplot.tight_layout()
405 415
406 416 matplotlib.pyplot.ion()
407 417
408 418
409 419 return iplot
410 420
411 421 def polar(iplot, x, y, xlabel='', ylabel='', title=''):
412 422
413 423 ax = iplot.get_axes()
414 424
415 425 # ax.text(0, -110, ylabel, rotation='vertical', va ='center', ha = 'center',size='11')
416 426 printLabels(ax, xlabel, '', title)
417 427
418 428 set_linedata(ax, x, y, idline=0)
419 429
420 430 def draw(fig):
421 431
422 432 if type(fig) == 'int':
423 raise ValueError, "This parameter should be of tpye matplotlib figure"
433 raise ValueError, "Error drawing: Fig parameter should be a matplotlib figure object figure"
424 434
425 435 fig.canvas.draw()
@@ -1,687 +1,692
1 1 '''
2 2 @author: Daniel Suarez
3 3 '''
4 4
5 5 import os
6 6 import sys
7 7 import glob
8 8 import fnmatch
9 9 import datetime
10 10 import time
11 11 import re
12 12 import h5py
13 13 import numpy
14 14
15 15 from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation
16 16 from schainpy.model.data.jroamisr import AMISR
17 17
18 try:
19 from gevent import sleep
20 except:
21 from time import sleep
22
18 23 class RadacHeader():
19 24 def __init__(self, fp):
20 25 header = 'Raw11/Data/RadacHeader'
21 26 self.beamCodeByPulse = fp.get(header+'/BeamCode')
22 27 self.beamCode = fp.get('Raw11/Data/Beamcodes')
23 28 self.code = fp.get(header+'/Code')
24 29 self.frameCount = fp.get(header+'/FrameCount')
25 30 self.modeGroup = fp.get(header+'/ModeGroup')
26 31 self.nsamplesPulse = fp.get(header+'/NSamplesPulse')
27 32 self.pulseCount = fp.get(header+'/PulseCount')
28 33 self.radacTime = fp.get(header+'/RadacTime')
29 34 self.timeCount = fp.get(header+'/TimeCount')
30 35 self.timeStatus = fp.get(header+'/TimeStatus')
31 36
32 37 self.nrecords = self.pulseCount.shape[0] #nblocks
33 38 self.npulses = self.pulseCount.shape[1] #nprofile
34 39 self.nsamples = self.nsamplesPulse[0,0] #ngates
35 40 self.nbeams = self.beamCode.shape[1]
36 41
37 42
38 43 def getIndexRangeToPulse(self, idrecord=0):
39 44 #indexToZero = numpy.where(self.pulseCount.value[idrecord,:]==0)
40 45 #startPulseCountId = indexToZero[0][0]
41 46 #endPulseCountId = startPulseCountId - 1
42 47 #range1 = numpy.arange(startPulseCountId,self.npulses,1)
43 48 #range2 = numpy.arange(0,startPulseCountId,1)
44 49 #return range1, range2
45 50 zero = 0
46 51 npulse = max(self.pulseCount[0,:]+1)-1
47 52 looking_index = numpy.where(self.pulseCount.value[idrecord,:]==npulse)[0]
48 53 getLastIndex = looking_index[-1]
49 54 index_data = numpy.arange(0,getLastIndex+1,1)
50 55 index_buffer = numpy.arange(getLastIndex+1,self.npulses,1)
51 56 return index_data, index_buffer
52 57
53 58 class AMISRReader(ProcessingUnit):
54 59
55 60 path = None
56 61 startDate = None
57 62 endDate = None
58 63 startTime = None
59 64 endTime = None
60 65 walk = None
61 66 isConfig = False
62 67
63 68 def __init__(self):
64 69 self.set = None
65 70 self.subset = None
66 71 self.extension_file = '.h5'
67 72 self.dtc_str = 'dtc'
68 73 self.dtc_id = 0
69 74 self.status = True
70 75 self.isConfig = False
71 76 self.dirnameList = []
72 77 self.filenameList = []
73 78 self.fileIndex = None
74 79 self.flagNoMoreFiles = False
75 80 self.flagIsNewFile = 0
76 81 self.filename = ''
77 82 self.amisrFilePointer = None
78 83 self.radacHeaderObj = None
79 84 self.dataOut = self.__createObjByDefault()
80 85 self.datablock = None
81 86 self.rest_datablock = None
82 87 self.range = None
83 88 self.idrecord_count = 0
84 89 self.profileIndex = 0
85 90 self.index_amisr_sample = None
86 91 self.index_amisr_buffer = None
87 92 self.beamCodeByFrame = None
88 93 self.radacTimeByFrame = None
89 94 #atributos originales tal y como esta en el archivo de datos
90 95 self.beamCodesFromFile = None
91 96 self.radacTimeFromFile = None
92 97 self.rangeFromFile = None
93 98 self.dataByFrame = None
94 99 self.dataset = None
95 100
96 101 self.beamCodeDict = {}
97 102 self.beamRangeDict = {}
98 103
99 104 #experiment cgf file
100 105 self.npulsesint_fromfile = None
101 106 self.recordsperfile_fromfile = None
102 107 self.nbeamcodes_fromfile = None
103 108 self.ngates_fromfile = None
104 109 self.ippSeconds_fromfile = None
105 110 self.frequency_h5file = None
106 111
107 112
108 113 self.__firstFile = True
109 114 self.buffer_radactime = None
110 115
111 116 self.index4_schain_datablock = None
112 117 self.index4_buffer = None
113 118 self.schain_datablock = None
114 119 self.buffer = None
115 120 self.linear_pulseCount = None
116 121 self.npulseByFrame = None
117 122 self.profileIndex_offset = None
118 123 self.timezone = 'ut'
119 124
120 125 self.__waitForNewFile = 20
121 126 self.__filename_online = None
122 127
123 128 def __createObjByDefault(self):
124 129
125 130 dataObj = AMISR()
126 131
127 132 return dataObj
128 133
129 134 def __setParameters(self,path='', startDate='',endDate='',startTime='', endTime='', walk=''):
130 135 self.path = path
131 136 self.startDate = startDate
132 137 self.endDate = endDate
133 138 self.startTime = startTime
134 139 self.endTime = endTime
135 140 self.walk = walk
136 141
137 142 def __checkPath(self):
138 143 if os.path.exists(self.path):
139 144 self.status = 1
140 145 else:
141 146 self.status = 0
142 147 print 'Path:%s does not exists'%self.path
143 148
144 149 return
145 150
146 151 def __selDates(self, amisr_dirname_format):
147 152 try:
148 153 year = int(amisr_dirname_format[0:4])
149 154 month = int(amisr_dirname_format[4:6])
150 155 dom = int(amisr_dirname_format[6:8])
151 156 thisDate = datetime.date(year,month,dom)
152 157
153 158 if (thisDate>=self.startDate and thisDate <= self.endDate):
154 159 return amisr_dirname_format
155 160 except:
156 161 return None
157 162
158 163 def __findDataForDates(self,online=False):
159 164
160 165
161 166
162 167 if not(self.status):
163 168 return None
164 169
165 170 pat = '\d+.\d+'
166 171 dirnameList = [re.search(pat,x) for x in os.listdir(self.path)]
167 172 dirnameList = filter(lambda x:x!=None,dirnameList)
168 173 dirnameList = [x.string for x in dirnameList]
169 174 if not(online):
170 175 dirnameList = [self.__selDates(x) for x in dirnameList]
171 176 dirnameList = filter(lambda x:x!=None,dirnameList)
172 177 if len(dirnameList)>0:
173 178 self.status = 1
174 179 self.dirnameList = dirnameList
175 180 self.dirnameList.sort()
176 181 else:
177 182 self.status = 0
178 183 return None
179 184
180 185 def __getTimeFromData(self):
181 186 startDateTime_Reader = datetime.datetime.combine(self.startDate,self.startTime)
182 187 endDateTime_Reader = datetime.datetime.combine(self.endDate,self.endTime)
183 188
184 189 print 'Filtering Files from %s to %s'%(startDateTime_Reader, endDateTime_Reader)
185 190 print '........................................'
186 191 filter_filenameList = []
187 192 self.filenameList.sort()
188 193 for i in range(len(self.filenameList)-1):
189 194 filename = self.filenameList[i]
190 195 fp = h5py.File(filename,'r')
191 196 time_str = fp.get('Time/RadacTimeString')
192 197
193 198 startDateTimeStr_File = time_str[0][0].split('.')[0]
194 199 junk = time.strptime(startDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
195 200 startDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
196 201
197 202 endDateTimeStr_File = time_str[-1][-1].split('.')[0]
198 203 junk = time.strptime(endDateTimeStr_File, '%Y-%m-%d %H:%M:%S')
199 204 endDateTime_File = datetime.datetime(junk.tm_year,junk.tm_mon,junk.tm_mday,junk.tm_hour, junk.tm_min, junk.tm_sec)
200 205
201 206 fp.close()
202 207
203 208 if self.timezone == 'lt':
204 209 startDateTime_File = startDateTime_File - datetime.timedelta(minutes = 300)
205 210 endDateTime_File = endDateTime_File - datetime.timedelta(minutes = 300)
206 211
207 212 if (endDateTime_File>=startDateTime_Reader and endDateTime_File<endDateTime_Reader):
208 213 #self.filenameList.remove(filename)
209 214 filter_filenameList.append(filename)
210 215
211 216 filter_filenameList.sort()
212 217 self.filenameList = filter_filenameList
213 218 return 1
214 219
215 220 def __filterByGlob1(self, dirName):
216 221 filter_files = glob.glob1(dirName, '*.*%s'%self.extension_file)
217 222 filterDict = {}
218 223 filterDict.setdefault(dirName)
219 224 filterDict[dirName] = filter_files
220 225 return filterDict
221 226
222 227 def __getFilenameList(self, fileListInKeys, dirList):
223 228 for value in fileListInKeys:
224 229 dirName = value.keys()[0]
225 230 for file in value[dirName]:
226 231 filename = os.path.join(dirName, file)
227 232 self.filenameList.append(filename)
228 233
229 234
230 235 def __selectDataForTimes(self, online=False):
231 236 #aun no esta implementado el filtro for tiempo
232 237 if not(self.status):
233 238 return None
234 239
235 240 dirList = [os.path.join(self.path,x) for x in self.dirnameList]
236 241
237 242 fileListInKeys = [self.__filterByGlob1(x) for x in dirList]
238 243
239 244 self.__getFilenameList(fileListInKeys, dirList)
240 245 if not(online):
241 246 #filtro por tiempo
242 247 if not(self.all):
243 248 self.__getTimeFromData()
244 249
245 250 if len(self.filenameList)>0:
246 251 self.status = 1
247 252 self.filenameList.sort()
248 253 else:
249 254 self.status = 0
250 255 return None
251 256
252 257 else:
253 258 #get the last file - 1
254 259 self.filenameList = [self.filenameList[-2]]
255 260
256 261 new_dirnameList = []
257 262 for dirname in self.dirnameList:
258 263 junk = numpy.array([dirname in x for x in self.filenameList])
259 264 junk_sum = junk.sum()
260 265 if junk_sum > 0:
261 266 new_dirnameList.append(dirname)
262 267 self.dirnameList = new_dirnameList
263 268 return 1
264 269
265 270 def __searchFilesOnline(self,
266 271 path,
267 272 walk=True):
268 273
269 274 startDate = datetime.datetime.utcnow().date()
270 275 endDate = datetime.datetime.utcnow().date()
271 276
272 277 self.__setParameters(path=path, startDate=startDate, endDate=endDate, walk=walk)
273 278
274 279 self.__checkPath()
275 280
276 281 self.__findDataForDates(online=True)
277 282
278 283 self.dirnameList = [self.dirnameList[-1]]
279 284
280 285 self.__selectDataForTimes(online=True)
281 286
282 287 return
283 288
284 289
285 290 def __searchFilesOffline(self,
286 291 path,
287 292 startDate,
288 293 endDate,
289 294 startTime=datetime.time(0,0,0),
290 295 endTime=datetime.time(23,59,59),
291 296 walk=True):
292 297
293 298 self.__setParameters(path, startDate, endDate, startTime, endTime, walk)
294 299
295 300 self.__checkPath()
296 301
297 302 self.__findDataForDates()
298 303
299 304 self.__selectDataForTimes()
300 305
301 306 for i in range(len(self.filenameList)):
302 307 print "%s" %(self.filenameList[i])
303 308
304 309 return
305 310
306 311 def __setNextFileOffline(self):
307 312 idFile = self.fileIndex
308 313
309 314 while (True):
310 315 idFile += 1
311 316 if not(idFile < len(self.filenameList)):
312 317 self.flagNoMoreFiles = 1
313 318 print "No more Files"
314 319 return 0
315 320
316 321 filename = self.filenameList[idFile]
317 322
318 323 amisrFilePointer = h5py.File(filename,'r')
319 324
320 325 break
321 326
322 327 self.flagIsNewFile = 1
323 328 self.fileIndex = idFile
324 329 self.filename = filename
325 330
326 331 self.amisrFilePointer = amisrFilePointer
327 332
328 333 print "Setting the file: %s"%self.filename
329 334
330 335 return 1
331 336
332 337
333 338 def __setNextFileOnline(self):
334 339 filename = self.filenameList[0]
335 340 if self.__filename_online != None:
336 341 self.__selectDataForTimes(online=True)
337 342 filename = self.filenameList[0]
338 343 while self.__filename_online == filename:
339 344 print 'waiting %d seconds to get a new file...'%(self.__waitForNewFile)
340 time.sleep(self.__waitForNewFile)
345 sleep(self.__waitForNewFile)
341 346 self.__selectDataForTimes(online=True)
342 347 filename = self.filenameList[0]
343 348
344 349 self.__filename_online = filename
345 350
346 351 self.amisrFilePointer = h5py.File(filename,'r')
347 352 self.flagIsNewFile = 1
348 353 self.filename = filename
349 354 print "Setting the file: %s"%self.filename
350 355 return 1
351 356
352 357
353 358 def __readHeader(self):
354 359 self.radacHeaderObj = RadacHeader(self.amisrFilePointer)
355 360
356 361 #update values from experiment cfg file
357 362 if self.radacHeaderObj.nrecords == self.recordsperfile_fromfile:
358 363 self.radacHeaderObj.nrecords = self.recordsperfile_fromfile
359 364 self.radacHeaderObj.nbeams = self.nbeamcodes_fromfile
360 365 self.radacHeaderObj.npulses = self.npulsesint_fromfile
361 366 self.radacHeaderObj.nsamples = self.ngates_fromfile
362 367
363 368 #looking index list for data
364 369 start_index = self.radacHeaderObj.pulseCount[0,:][0]
365 370 end_index = self.radacHeaderObj.npulses
366 371 range4data = range(start_index, end_index)
367 372 self.index4_schain_datablock = numpy.array(range4data)
368 373
369 374 buffer_start_index = 0
370 375 buffer_end_index = self.radacHeaderObj.pulseCount[0,:][0]
371 376 range4buffer = range(buffer_start_index, buffer_end_index)
372 377 self.index4_buffer = numpy.array(range4buffer)
373 378
374 379 self.linear_pulseCount = numpy.array(range4data + range4buffer)
375 380 self.npulseByFrame = max(self.radacHeaderObj.pulseCount[0,:]+1)
376 381
377 382 #get tuning frequency
378 383 frequency_h5file_dataset = self.amisrFilePointer.get('Rx'+'/TuningFrequency')
379 384 self.frequency_h5file = frequency_h5file_dataset[0,0]
380 385
381 386 self.flagIsNewFile = 1
382 387
383 388 def __getBeamCode(self):
384 389 self.beamCodeDict = {}
385 390 self.beamRangeDict = {}
386 391
387 392 beamCodeMap = self.amisrFilePointer.get('Setup/BeamcodeMap')
388 393
389 394 for i in range(len(self.radacHeaderObj.beamCode[0,:])):
390 395 self.beamCodeDict.setdefault(i)
391 396 self.beamRangeDict.setdefault(i)
392 397 beamcodeValue = self.radacHeaderObj.beamCode[0,i]
393 398 beamcodeIndex = numpy.where(beamCodeMap[:,0] == beamcodeValue)[0][0]
394 399 x = beamCodeMap[beamcodeIndex][1]
395 400 y = beamCodeMap[beamcodeIndex][2]
396 401 z = beamCodeMap[beamcodeIndex][3]
397 402 self.beamCodeDict[i] = [beamcodeValue, x, y, z]
398 403
399 404 just4record0 = self.radacHeaderObj.beamCodeByPulse[0,:]
400 405
401 406 for i in range(len(self.beamCodeDict.values())):
402 407 xx = numpy.where(just4record0==self.beamCodeDict.values()[i][0])
403 408 indexPulseByBeam = self.linear_pulseCount[xx[0]]
404 409 self.beamRangeDict[i] = indexPulseByBeam
405 410
406 411 def __getExpParameters(self):
407 412 if not(self.status):
408 413 return None
409 414
410 415 experimentCfgPath = os.path.join(self.path, self.dirnameList[0], 'Setup')
411 416
412 417 expFinder = glob.glob1(experimentCfgPath,'*.exp')
413 418 if len(expFinder)== 0:
414 419 self.status = 0
415 420 return None
416 421
417 422 experimentFilename = os.path.join(experimentCfgPath,expFinder[0])
418 423
419 424 f = open(experimentFilename)
420 425 lines = f.readlines()
421 426 f.close()
422 427
423 428 parmsList = ['npulsesint*','recordsperfile*','nbeamcodes*','ngates*']
424 429 filterList = [fnmatch.filter(lines, x) for x in parmsList]
425 430
426 431
427 432 values = [re.sub(r'\D',"",x[0]) for x in filterList]
428 433
429 434 self.npulsesint_fromfile = int(values[0])
430 435 self.recordsperfile_fromfile = int(values[1])
431 436 self.nbeamcodes_fromfile = int(values[2])
432 437 self.ngates_fromfile = int(values[3])
433 438
434 439 tufileFinder = fnmatch.filter(lines, 'tufile=*')
435 440 tufile = tufileFinder[0].split('=')[1].split('\n')[0]
436 441 tufile = tufile.split('\r')[0]
437 442 tufilename = os.path.join(experimentCfgPath,tufile)
438 443
439 444 f = open(tufilename)
440 445 lines = f.readlines()
441 446 f.close()
442 447 self.ippSeconds_fromfile = float(lines[1].split()[2])/1E6
443 448
444 449
445 450 self.status = 1
446 451
447 452 def __setIdsAndArrays(self):
448 453 self.dataByFrame = self.__setDataByFrame()
449 454 self.beamCodeByFrame = self.amisrFilePointer.get('Raw11/Data/RadacHeader/BeamCode').value[0, :]
450 455 self.readRanges()
451 456 self.index_amisr_sample, self.index_amisr_buffer = self.radacHeaderObj.getIndexRangeToPulse(0)
452 457 self.radacTimeByFrame = numpy.zeros(self.radacHeaderObj.npulses)
453 458 if len(self.index_amisr_buffer) > 0:
454 459 self.buffer_radactime = numpy.zeros_like(self.radacTimeByFrame)
455 460
456 461
457 462 def __setNextFile(self,online=False):
458 463
459 464 if not(online):
460 465 newFile = self.__setNextFileOffline()
461 466 else:
462 467 newFile = self.__setNextFileOnline()
463 468
464 469 if not(newFile):
465 470 return 0
466 471
467 472 self.__readHeader()
468 473
469 474 if self.__firstFile:
470 475 self.__setIdsAndArrays()
471 476 self.__firstFile = False
472 477
473 478 self.__getBeamCode()
474 479 self.readDataBlock()
475 480
476 481
477 482 def setup(self,path=None,
478 483 startDate=None,
479 484 endDate=None,
480 485 startTime=datetime.time(0,0,0),
481 486 endTime=datetime.time(23,59,59),
482 487 walk=True,
483 488 timezone='ut',
484 489 all=0,
485 490 online=False):
486 491
487 492 self.timezone = timezone
488 493 self.all = all
489 494 self.online = online
490 495 if not(online):
491 496 #Busqueda de archivos offline
492 497 self.__searchFilesOffline(path, startDate, endDate, startTime, endTime, walk)
493 498 else:
494 499 self.__searchFilesOnline(path, walk)
495 500
496 501 if not(self.filenameList):
497 502 print "There is no files into the folder: %s"%(path)
498 503
499 504 sys.exit(-1)
500 505
501 506 self.__getExpParameters()
502 507
503 508 self.fileIndex = -1
504 509
505 510 self.__setNextFile(online)
506 511
507 512 # first_beamcode = self.radacHeaderObj.beamCodeByPulse[0,0]
508 513 # index = numpy.where(self.radacHeaderObj.beamCodeByPulse[0,:]!=first_beamcode)[0][0]
509 514 self.profileIndex_offset = self.radacHeaderObj.pulseCount[0,:][0]
510 515 self.profileIndex = self.profileIndex_offset
511 516
512 517 def readRanges(self):
513 518 dataset = self.amisrFilePointer.get('Raw11/Data/Samples/Range')
514 519
515 520 self.rangeFromFile = numpy.reshape(dataset.value,(-1))
516 521 return self.rangeFromFile
517 522
518 523
519 524 def readRadacTime(self,idrecord, range1, range2):
520 525 self.radacTimeFromFile = self.radacHeaderObj.radacTime.value
521 526
522 527 radacTimeByFrame = numpy.zeros((self.radacHeaderObj.npulses))
523 528 #radacTimeByFrame = dataset[idrecord - 1,range1]
524 529 #radacTimeByFrame = dataset[idrecord,range2]
525 530
526 531 return radacTimeByFrame
527 532
528 533 def readBeamCode(self, idrecord, range1, range2):
529 534 dataset = self.amisrFilePointer.get('Raw11/Data/RadacHeader/BeamCode')
530 535 beamcodeByFrame = numpy.zeros((self.radacHeaderObj.npulses))
531 536 self.beamCodesFromFile = dataset.value
532 537
533 538 #beamcodeByFrame[range1] = dataset[idrecord - 1, range1]
534 539 #beamcodeByFrame[range2] = dataset[idrecord, range2]
535 540 beamcodeByFrame[range1] = dataset[idrecord, range1]
536 541 beamcodeByFrame[range2] = dataset[idrecord, range2]
537 542
538 543 return beamcodeByFrame
539 544
540 545
541 546 def __setDataByFrame(self):
542 547 ndata = 2 # porque es complejo
543 548 dataByFrame = numpy.zeros((self.radacHeaderObj.npulses, self.radacHeaderObj.nsamples, ndata))
544 549 return dataByFrame
545 550
546 551 def __readDataSet(self):
547 552 dataset = self.amisrFilePointer.get('Raw11/Data/Samples/Data')
548 553 return dataset
549 554
550 555 def __setDataBlock(self,):
551 556 real = self.dataByFrame[:,:,0] #asumo que 0 es real
552 557 imag = self.dataByFrame[:,:,1] #asumo que 1 es imaginario
553 558 datablock = real + imag*1j #armo el complejo
554 559 return datablock
555 560
556 561 def readSamples_version1(self,idrecord):
557 562 #estas tres primeras lineas solo se deben ejecutar una vez
558 563 if self.flagIsNewFile:
559 564 #reading dataset
560 565 self.dataset = self.__readDataSet()
561 566 self.flagIsNewFile = 0
562 567
563 568 if idrecord == 0:
564 569 self.dataByFrame[self.index4_schain_datablock, : ,:] = self.dataset[0, self.index_amisr_sample,:,:]
565 570 self.radacTimeByFrame[self.index4_schain_datablock] = self.radacHeaderObj.radacTime[0, self.index_amisr_sample]
566 571 datablock = self.__setDataBlock()
567 572 if len(self.index_amisr_buffer) > 0:
568 573 self.buffer = self.dataset[0, self.index_amisr_buffer,:,:]
569 574 self.buffer_radactime = self.radacHeaderObj.radacTime[0, self.index_amisr_buffer]
570 575
571 576 return datablock
572 577 if len(self.index_amisr_buffer) > 0:
573 578 self.dataByFrame[self.index4_buffer,:,:] = self.buffer.copy()
574 579 self.radacTimeByFrame[self.index4_buffer] = self.buffer_radactime.copy()
575 580 self.dataByFrame[self.index4_schain_datablock,:,:] = self.dataset[idrecord, self.index_amisr_sample,:,:]
576 581 self.radacTimeByFrame[self.index4_schain_datablock] = self.radacHeaderObj.radacTime[idrecord, self.index_amisr_sample]
577 582 datablock = self.__setDataBlock()
578 583 if len(self.index_amisr_buffer) > 0:
579 584 self.buffer = self.dataset[idrecord, self.index_amisr_buffer, :, :]
580 585 self.buffer_radactime = self.radacHeaderObj.radacTime[idrecord, self.index_amisr_buffer]
581 586
582 587 return datablock
583 588
584 589
585 590 def readSamples(self,idrecord):
586 591 if self.flagIsNewFile:
587 592 self.dataByFrame = self.__setDataByFrame()
588 593 self.beamCodeByFrame = self.amisrFilePointer.get('Raw11/Data/RadacHeader/BeamCode').value[idrecord, :]
589 594
590 595 #reading ranges
591 596 self.readRanges()
592 597 #reading dataset
593 598 self.dataset = self.__readDataSet()
594 599
595 600 self.flagIsNewFile = 0
596 601 self.radacTimeByFrame = self.radacHeaderObj.radacTime.value[idrecord, :]
597 602 self.dataByFrame = self.dataset[idrecord, :, :, :]
598 603 datablock = self.__setDataBlock()
599 604 return datablock
600 605
601 606
602 607 def readDataBlock(self):
603 608
604 609 self.datablock = self.readSamples_version1(self.idrecord_count)
605 610 #self.datablock = self.readSamples(self.idrecord_count)
606 611 #print 'record:', self.idrecord_count
607 612
608 613 self.idrecord_count += 1
609 614 self.profileIndex = 0
610 615
611 616 if self.idrecord_count >= self.radacHeaderObj.nrecords:
612 617 self.idrecord_count = 0
613 618 self.flagIsNewFile = 1
614 619
615 620 def readNextBlock(self):
616 621
617 622 self.readDataBlock()
618 623
619 624 if self.flagIsNewFile:
620 625 self.__setNextFile(self.online)
621 626 pass
622 627
623 628 def __hasNotDataInBuffer(self):
624 629 #self.radacHeaderObj.npulses debe ser otra variable para considerar el numero de pulsos a tomar en el primer y ultimo record
625 630 if self.profileIndex >= self.radacHeaderObj.npulses:
626 631 return 1
627 632 return 0
628 633
629 634 def printUTC(self):
630 635 print self.dataOut.utctime
631 636 print ''
632 637
633 638 def setObjProperties(self):
634 639
635 640 self.dataOut.heightList = self.rangeFromFile/1000.0 #km
636 641 self.dataOut.nProfiles = self.radacHeaderObj.npulses
637 642 self.dataOut.nRecords = self.radacHeaderObj.nrecords
638 643 self.dataOut.nBeams = self.radacHeaderObj.nbeams
639 644 self.dataOut.ippSeconds = self.ippSeconds_fromfile
640 645 # self.dataOut.timeInterval = self.dataOut.ippSeconds * self.dataOut.nCohInt
641 646 self.dataOut.frequency = self.frequency_h5file
642 647 self.dataOut.npulseByFrame = self.npulseByFrame
643 648 self.dataOut.nBaud = None
644 649 self.dataOut.nCode = None
645 650 self.dataOut.code = None
646 651
647 652 self.dataOut.beamCodeDict = self.beamCodeDict
648 653 self.dataOut.beamRangeDict = self.beamRangeDict
649 654
650 655 if self.timezone == 'lt':
651 656 self.dataOut.timeZone = time.timezone / 60. #get the timezone in minutes
652 657 else:
653 658 self.dataOut.timeZone = 0 #by default time is UTC
654 659
655 660 def getData(self):
656 661
657 662 if self.flagNoMoreFiles:
658 663 self.dataOut.flagNoData = True
659 664 print 'Process finished'
660 665 return 0
661 666
662 667 if self.__hasNotDataInBuffer():
663 668 self.readNextBlock()
664 669
665 670
666 671 if self.datablock == None: # setear esta condicion cuando no hayan datos por leers
667 672 self.dataOut.flagNoData = True
668 673 return 0
669 674
670 675 self.dataOut.data = numpy.reshape(self.datablock[self.profileIndex,:],(1,-1))
671 676
672 677 self.dataOut.utctime = self.radacTimeByFrame[self.profileIndex]
673 678 self.dataOut.profileIndex = self.profileIndex
674 679 self.dataOut.flagNoData = False
675 680
676 681 self.profileIndex += 1
677 682
678 683 return self.dataOut.data
679 684
680 685
681 686 def run(self, **kwargs):
682 687 if not(self.isConfig):
683 688 self.setup(**kwargs)
684 689 self.setObjProperties()
685 690 self.isConfig = True
686 691
687 692 self.getData()
@@ -1,834 +1,849
1 1 '''
2 2 Created on Jul 3, 2014
3 3
4 4 @author: roj-idl71
5 5 '''
6 6
7 7 import os, sys
8 8 import time, datetime
9 9 import numpy
10 10 import fnmatch
11 11 import glob
12 12
13 13 try:
14 14 from gevent import sleep
15 15 except:
16 16 from time import sleep
17 17
18 18 try:
19 19 import pyfits
20 20 except:
21 21 """
22 22 """
23 23
24 24 from xml.etree.ElementTree import ElementTree
25 25
26 26 from jroIO_base import isDoyFolder, isNumber
27 27 from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit
28 28
29 29 class Fits:
30 30 name=None
31 31 format=None
32 32 array =None
33 33 data =None
34 34 thdulist=None
35 35 prihdr=None
36 36 hdu=None
37 37
38 38 def __init__(self):
39 39
40 40 pass
41 41
42 42 def setColF(self,name,format,array):
43 43 self.name=name
44 44 self.format=format
45 45 self.array=array
46 46 a1=numpy.array([self.array],dtype=numpy.float32)
47 47 self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1)
48 48 return self.col1
49 49
50 50 # def setColP(self,name,format,data):
51 51 # self.name=name
52 52 # self.format=format
53 53 # self.data=data
54 54 # a2=numpy.array([self.data],dtype=numpy.float32)
55 55 # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
56 56 # return self.col2
57 57
58 58
59 59 def writeData(self,name,format,data):
60 60 self.name=name
61 61 self.format=format
62 62 self.data=data
63 63 a2=numpy.array([self.data],dtype=numpy.float32)
64 64 self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
65 65 return self.col2
66 66
67 67 def cFImage(self,idblock,year,month,day,hour,minute,second):
68 68 self.hdu= pyfits.PrimaryHDU(idblock)
69 69 self.hdu.header.set("Year",year)
70 70 self.hdu.header.set("Month",month)
71 71 self.hdu.header.set("Day",day)
72 72 self.hdu.header.set("Hour",hour)
73 73 self.hdu.header.set("Minute",minute)
74 74 self.hdu.header.set("Second",second)
75 75 return self.hdu
76 76
77 77
78 78 def Ctable(self,colList):
79 79 self.cols=pyfits.ColDefs(colList)
80 80 self.tbhdu = pyfits.new_table(self.cols)
81 81 return self.tbhdu
82 82
83 83
84 84 def CFile(self,hdu,tbhdu):
85 85 self.thdulist=pyfits.HDUList([hdu,tbhdu])
86 86
87 87 def wFile(self,filename):
88 88 if os.path.isfile(filename):
89 89 os.remove(filename)
90 90 self.thdulist.writeto(filename)
91 91
92 92
93 93 class ParameterConf:
94 94 ELEMENTNAME = 'Parameter'
95 95 def __init__(self):
96 96 self.name = ''
97 97 self.value = ''
98 98
99 99 def readXml(self, parmElement):
100 100 self.name = parmElement.get('name')
101 101 self.value = parmElement.get('value')
102 102
103 103 def getElementName(self):
104 104 return self.ELEMENTNAME
105 105
106 106 class Metadata:
107 107
108 108 def __init__(self, filename):
109 109 self.parmConfObjList = []
110 110 self.readXml(filename)
111 111
112 112 def readXml(self, filename):
113 113 self.projectElement = None
114 114 self.procUnitConfObjDict = {}
115 115 self.projectElement = ElementTree().parse(filename)
116 116 self.project = self.projectElement.tag
117 117
118 118 parmElementList = self.projectElement.getiterator(ParameterConf().getElementName())
119 119
120 120 for parmElement in parmElementList:
121 121 parmConfObj = ParameterConf()
122 122 parmConfObj.readXml(parmElement)
123 123 self.parmConfObjList.append(parmConfObj)
124 124
125 125 class FitsWriter(Operation):
126 126
127 127 def __init__(self):
128 128 self.isConfig = False
129 129 self.dataBlocksPerFile = None
130 130 self.blockIndex = 0
131 131 self.flagIsNewFile = 1
132 132 self.fitsObj = None
133 133 self.optchar = 'P'
134 134 self.ext = '.fits'
135 135 self.setFile = 0
136 136
137 137 def setFitsHeader(self, dataOut, metadatafile):
138 138
139 139 header_data = pyfits.PrimaryHDU()
140 140
141 141 metadata4fits = Metadata(metadatafile)
142 142 for parameter in metadata4fits.parmConfObjList:
143 143 parm_name = parameter.name
144 144 parm_value = parameter.value
145 145
146 146 # if parm_value == 'fromdatadatetime':
147 147 # value = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple())
148 148 # elif parm_value == 'fromdataheights':
149 149 # value = dataOut.nHeights
150 150 # elif parm_value == 'fromdatachannel':
151 151 # value = dataOut.nChannels
152 152 # elif parm_value == 'fromdatasamples':
153 153 # value = dataOut.nFFTPoints
154 154 # else:
155 155 # value = parm_value
156 156
157 157 header_data.header[parm_name] = parm_value
158 158
159 159
160 160 header_data.header['DATETIME'] = time.strftime("%b %d %Y %H:%M:%S", dataOut.datatime.timetuple())
161 161 header_data.header['CHANNELLIST'] = str(dataOut.channelList)
162 162 header_data.header['NCHANNELS'] = dataOut.nChannels
163 163 #header_data.header['HEIGHTS'] = dataOut.heightList
164 164 header_data.header['NHEIGHTS'] = dataOut.nHeights
165 165
166 166 header_data.header['IPPSECONDS'] = dataOut.ippSeconds
167 167 header_data.header['NCOHINT'] = dataOut.nCohInt
168 168 header_data.header['NINCOHINT'] = dataOut.nIncohInt
169 169 header_data.header['TIMEZONE'] = dataOut.timeZone
170 170 header_data.header['NBLOCK'] = self.blockIndex
171 171
172 172 header_data.writeto(self.filename)
173 173
174 174 self.addExtension(dataOut.heightList,'HEIGHTLIST')
175 175
176 176
177 177 def setup(self, dataOut, path, dataBlocksPerFile, metadatafile):
178 178
179 179 self.path = path
180 180 self.dataOut = dataOut
181 181 self.metadatafile = metadatafile
182 182 self.dataBlocksPerFile = dataBlocksPerFile
183 183
184 184 def open(self):
185 185 self.fitsObj = pyfits.open(self.filename, mode='update')
186 186
187 187
188 188 def addExtension(self, data, tagname):
189 189 self.open()
190 190 extension = pyfits.ImageHDU(data=data, name=tagname)
191 191 #extension.header['TAG'] = tagname
192 192 self.fitsObj.append(extension)
193 193 self.write()
194 194
195 195 def addData(self, data):
196 196 self.open()
197 197 extension = pyfits.ImageHDU(data=data, name=self.fitsObj[0].header['DATATYPE'])
198 198 extension.header['UTCTIME'] = self.dataOut.utctime
199 199 self.fitsObj.append(extension)
200 200 self.blockIndex += 1
201 201 self.fitsObj[0].header['NBLOCK'] = self.blockIndex
202 202
203 203 self.write()
204 204
205 205 def write(self):
206 206
207 207 self.fitsObj.flush(verbose=True)
208 208 self.fitsObj.close()
209 209
210 210
211 211 def setNextFile(self):
212 212
213 213 ext = self.ext
214 214 path = self.path
215 215
216 216 timeTuple = time.localtime( self.dataOut.utctime)
217 217 subfolder = 'd%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
218 218
219 219 fullpath = os.path.join( path, subfolder )
220 220 if not( os.path.exists(fullpath) ):
221 221 os.mkdir(fullpath)
222 222 self.setFile = -1 #inicializo mi contador de seteo
223 223 else:
224 224 filesList = os.listdir( fullpath )
225 225 if len( filesList ) > 0:
226 226 filesList = sorted( filesList, key=str.lower )
227 227 filen = filesList[-1]
228 228
229 229 if isNumber( filen[8:11] ):
230 230 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
231 231 else:
232 232 self.setFile = -1
233 233 else:
234 234 self.setFile = -1 #inicializo mi contador de seteo
235 235
236 236 setFile = self.setFile
237 237 setFile += 1
238 238
239 239 thisFile = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
240 240 timeTuple.tm_year,
241 241 timeTuple.tm_yday,
242 242 setFile,
243 243 ext )
244 244
245 245 filename = os.path.join( path, subfolder, thisFile )
246 246
247 247 self.blockIndex = 0
248 248 self.filename = filename
249 249 self.setFile = setFile
250 250 self.flagIsNewFile = 1
251 251
252 252 print 'Writing the file: %s'%self.filename
253 253
254 254 self.setFitsHeader(self.dataOut, self.metadatafile)
255 255
256 256 return 1
257 257
258 258 def writeBlock(self):
259 259 self.addData(self.dataOut.data_spc)
260 260 self.flagIsNewFile = 0
261 261
262 262
263 263 def __setNewBlock(self):
264 264
265 265 if self.flagIsNewFile:
266 266 return 1
267 267
268 268 if self.blockIndex < self.dataBlocksPerFile:
269 269 return 1
270 270
271 271 if not( self.setNextFile() ):
272 272 return 0
273 273
274 274 return 1
275 275
276 276 def writeNextBlock(self):
277 277 if not( self.__setNewBlock() ):
278 278 return 0
279 279 self.writeBlock()
280 280 return 1
281 281
282 282 def putData(self):
283 283 if self.flagIsNewFile:
284 284 self.setNextFile()
285 285 self.writeNextBlock()
286 286
287 287 def run(self, dataOut, **kwargs):
288 288 if not(self.isConfig):
289 289 self.setup(dataOut, **kwargs)
290 290 self.isConfig = True
291 291 self.putData()
292 292
293 293
294 294 class FitsReader(ProcessingUnit):
295 295
296 296 # __TIMEZONE = time.timezone
297 297
298 298 expName = None
299 299 datetimestr = None
300 300 utc = None
301 301 nChannels = None
302 302 nSamples = None
303 303 dataBlocksPerFile = None
304 304 comments = None
305 305 lastUTTime = None
306 306 header_dict = None
307 307 data = None
308 308 data_header_dict = None
309 309
310 310 def __init__(self):
311 311 self.isConfig = False
312 312 self.ext = '.fits'
313 313 self.setFile = 0
314 314 self.flagNoMoreFiles = 0
315 315 self.flagIsNewFile = 1
316 316 self.flagDiscontinuousBlock = None
317 317 self.fileIndex = None
318 318 self.filename = None
319 319 self.fileSize = None
320 320 self.fitsObj = None
321 321 self.timeZone = None
322 322 self.nReadBlocks = 0
323 323 self.nTotalBlocks = 0
324 324 self.dataOut = self.createObjByDefault()
325 325 self.maxTimeStep = 10# deberia ser definido por el usuario usando el metodo setup()
326 326 self.blockIndex = 1
327 327
328 328 def createObjByDefault(self):
329 329
330 330 dataObj = Fits()
331 331
332 332 return dataObj
333 333
334 334 def isFileinThisTime(self, filename, startTime, endTime, useLocalTime=False):
335 335 try:
336 336 fitsObj = pyfits.open(filename,'readonly')
337 337 except:
338 338 raise IOError, "The file %s can't be opened" %(filename)
339 339
340 340 header = fitsObj[0].header
341 341 struct_time = time.strptime(header['DATETIME'], "%b %d %Y %H:%M:%S")
342 342 utc = time.mktime(struct_time) - time.timezone #TIMEZONE debe ser un parametro del header FITS
343 343
344 344 ltc = utc
345 345 if useLocalTime:
346 346 ltc -= time.timezone
347 347 thisDatetime = datetime.datetime.utcfromtimestamp(ltc)
348 348 thisTime = thisDatetime.time()
349 349
350 350 if not ((startTime <= thisTime) and (endTime > thisTime)):
351 351 return None
352 352
353 353 return thisDatetime
354 354
355 355 def __setNextFileOnline(self):
356 356 raise ValueError, "No implemented"
357 357
358 358 def __setNextFileOffline(self):
359 359 idFile = self.fileIndex
360 360
361 361 while (True):
362 362 idFile += 1
363 363 if not(idFile < len(self.filenameList)):
364 364 self.flagNoMoreFiles = 1
365 365 print "No more Files"
366 366 return 0
367 367
368 368 filename = self.filenameList[idFile]
369 369
370 370 # if not(self.__verifyFile(filename)):
371 371 # continue
372 372
373 373 fileSize = os.path.getsize(filename)
374 374 fitsObj = pyfits.open(filename,'readonly')
375 375 break
376 376
377 377 self.flagIsNewFile = 1
378 378 self.fileIndex = idFile
379 379 self.filename = filename
380 380 self.fileSize = fileSize
381 381 self.fitsObj = fitsObj
382 382 self.blockIndex = 0
383 383 print "Setting the file: %s"%self.filename
384 384
385 385 return 1
386 386
387 def __setValuesFromHeader(self):
388
389 self.dataOut.header = self.header_dict
390 self.dataOut.expName = self.expName
391 self.dataOut.nChannels = self.nChannels
392 self.dataOut.timeZone = self.timeZone
393 self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile
394 self.dataOut.comments = self.comments
395 # self.dataOut.timeInterval = self.timeInterval
396 self.dataOut.channelList = self.channelList
397 self.dataOut.heightList = self.heightList
398
399 self.dataOut.nCohInt = self.nCohInt
400 self.dataOut.nIncohInt = self.nIncohInt
401
387 402 def readHeader(self):
388 403 headerObj = self.fitsObj[0]
389 404
390 405 self.header_dict = headerObj.header
391 406 if 'EXPNAME' in headerObj.header.keys():
392 407 self.expName = headerObj.header['EXPNAME']
393 408
394 409 if 'DATATYPE' in headerObj.header.keys():
395 410 self.dataType = headerObj.header['DATATYPE']
396 411
397 412 self.datetimestr = headerObj.header['DATETIME']
398 413 channelList = headerObj.header['CHANNELLIST']
399 414 channelList = channelList.split('[')
400 415 channelList = channelList[1].split(']')
401 416 channelList = channelList[0].split(',')
402 417 channelList = [int(ch) for ch in channelList]
403 418 self.channelList = channelList
404 419 self.nChannels = headerObj.header['NCHANNELS']
405 420 self.nHeights = headerObj.header['NHEIGHTS']
406 421 self.ippSeconds = headerObj.header['IPPSECONDS']
407 422 self.nCohInt = headerObj.header['NCOHINT']
408 423 self.nIncohInt = headerObj.header['NINCOHINT']
409 424 self.dataBlocksPerFile = headerObj.header['NBLOCK']
410 425 self.timeZone = headerObj.header['TIMEZONE']
411 426
412 427 # self.timeInterval = self.ippSeconds * self.nCohInt * self.nIncohInt
413 428
414 429 if 'COMMENT' in headerObj.header.keys():
415 430 self.comments = headerObj.header['COMMENT']
416 431
417 432 self.readHeightList()
418 433
419 434 def readHeightList(self):
420 435 self.blockIndex = self.blockIndex + 1
421 436 obj = self.fitsObj[self.blockIndex]
422 437 self.heightList = obj.data
423 438 self.blockIndex = self.blockIndex + 1
424 439
425 440 def readExtension(self):
426 441 obj = self.fitsObj[self.blockIndex]
427 442 self.heightList = obj.data
428 443 self.blockIndex = self.blockIndex + 1
429 444
430 445 def setNextFile(self):
431 446
432 447 if self.online:
433 448 newFile = self.__setNextFileOnline()
434 449 else:
435 450 newFile = self.__setNextFileOffline()
436 451
437 452 if not(newFile):
438 453 return 0
439 454
440 455 self.readHeader()
441
456 self.__setValuesFromHeader()
442 457 self.nReadBlocks = 0
443 458 # self.blockIndex = 1
444 459 return 1
445 460
446 461 def __searchFilesOffLine(self,
447 462 path,
448 463 startDate,
449 464 endDate,
450 465 startTime=datetime.time(0,0,0),
451 466 endTime=datetime.time(23,59,59),
452 467 set=None,
453 468 expLabel='',
454 469 ext='.fits',
455 470 walk=True):
456 471
457 472 pathList = []
458 473
459 474 if not walk:
460 475 pathList.append(path)
461 476
462 477 else:
463 478 dirList = []
464 479 for thisPath in os.listdir(path):
465 480 if not os.path.isdir(os.path.join(path,thisPath)):
466 481 continue
467 482 if not isDoyFolder(thisPath):
468 483 continue
469 484
470 485 dirList.append(thisPath)
471 486
472 487 if not(dirList):
473 488 return None, None
474 489
475 490 thisDate = startDate
476 491
477 492 while(thisDate <= endDate):
478 493 year = thisDate.timetuple().tm_year
479 494 doy = thisDate.timetuple().tm_yday
480 495
481 496 matchlist = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy) + '*')
482 497 if len(matchlist) == 0:
483 498 thisDate += datetime.timedelta(1)
484 499 continue
485 500 for match in matchlist:
486 501 pathList.append(os.path.join(path,match,expLabel))
487 502
488 503 thisDate += datetime.timedelta(1)
489 504
490 505 if pathList == []:
491 506 print "Any folder was found for the date range: %s-%s" %(startDate, endDate)
492 507 return None, None
493 508
494 509 print "%d folder(s) was(were) found for the date range: %s - %s" %(len(pathList), startDate, endDate)
495 510
496 511 filenameList = []
497 512 datetimeList = []
498 513
499 514 for i in range(len(pathList)):
500 515
501 516 thisPath = pathList[i]
502 517
503 518 fileList = glob.glob1(thisPath, "*%s" %ext)
504 519 fileList.sort()
505 520
506 521 for thisFile in fileList:
507 522
508 523 filename = os.path.join(thisPath,thisFile)
509 524 thisDatetime = self.isFileinThisTime(filename, startTime, endTime)
510 525
511 526 if not(thisDatetime):
512 527 continue
513 528
514 529 filenameList.append(filename)
515 530 datetimeList.append(thisDatetime)
516 531
517 532 if not(filenameList):
518 533 print "Any file was found for the time range %s - %s" %(startTime, endTime)
519 534 return None, None
520 535
521 536 print "%d file(s) was(were) found for the time range: %s - %s" %(len(filenameList), startTime, endTime)
522 537 print
523 538
524 539 for i in range(len(filenameList)):
525 540 print "%s -> [%s]" %(filenameList[i], datetimeList[i].ctime())
526 541
527 542 self.filenameList = filenameList
528 543 self.datetimeList = datetimeList
529 544
530 545 return pathList, filenameList
531 546
532 547 def setup(self, path=None,
533 548 startDate=None,
534 549 endDate=None,
535 550 startTime=datetime.time(0,0,0),
536 551 endTime=datetime.time(23,59,59),
537 552 set=0,
538 553 expLabel = "",
539 554 ext = None,
540 555 online = False,
541 556 delay = 60,
542 557 walk = True):
543 558
544 559 if path == None:
545 560 raise ValueError, "The path is not valid"
546 561
547 562 if ext == None:
548 563 ext = self.ext
549 564
550 565 if not(online):
551 566 print "Searching files in offline mode ..."
552 567 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
553 568 startTime=startTime, endTime=endTime,
554 569 set=set, expLabel=expLabel, ext=ext,
555 570 walk=walk)
556 571
557 572 if not(pathList):
558 573 print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path,
559 574 datetime.datetime.combine(startDate,startTime).ctime(),
560 575 datetime.datetime.combine(endDate,endTime).ctime())
561 576
562 577 sys.exit(-1)
563 578
564 579 self.fileIndex = -1
565 580 self.pathList = pathList
566 581 self.filenameList = filenameList
567 582
568 583 self.online = online
569 584 self.delay = delay
570 585 ext = ext.lower()
571 586 self.ext = ext
572 587
573 588 if not(self.setNextFile()):
574 589 if (startDate!=None) and (endDate!=None):
575 590 print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
576 591 elif startDate != None:
577 592 print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
578 593 else:
579 594 print "No files"
580 595
581 596 sys.exit(-1)
582 597
583 598
584 599
585 600 def readBlock(self):
586 601 dataObj = self.fitsObj[self.blockIndex]
587 602
588 603 self.data = dataObj.data
589 604 self.data_header_dict = dataObj.header
590 605 self.utc = self.data_header_dict['UTCTIME']
591 606
592 607 self.flagIsNewFile = 0
593 608 self.blockIndex += 1
594 609 self.nTotalBlocks += 1
595 610 self.nReadBlocks += 1
596 611
597 612 return 1
598 613
599 614 def __jumpToLastBlock(self):
600 615 raise ValueError, "No implemented"
601 616
602 617 def __waitNewBlock(self):
603 618 """
604 619 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
605 620
606 621 Si el modo de lectura es OffLine siempre retorn 0
607 622 """
608 623 if not self.online:
609 624 return 0
610 625
611 626 if (self.nReadBlocks >= self.dataBlocksPerFile):
612 627 return 0
613 628
614 629 currentPointer = self.fp.tell()
615 630
616 631 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
617 632
618 633 for nTries in range( self.nTries ):
619 634
620 635 self.fp.close()
621 636 self.fp = open( self.filename, 'rb' )
622 637 self.fp.seek( currentPointer )
623 638
624 639 self.fileSize = os.path.getsize( self.filename )
625 640 currentSize = self.fileSize - currentPointer
626 641
627 642 if ( currentSize >= neededSize ):
628 643 self.__rdBasicHeader()
629 644 return 1
630 645
631 646 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
632 647 sleep( self.delay )
633 648
634 649
635 650 return 0
636 651
637 652 def __setNewBlock(self):
638 653
639 654 if self.online:
640 655 self.__jumpToLastBlock()
641 656
642 657 if self.flagIsNewFile:
643 658 return 1
644 659
645 660 self.lastUTTime = self.utc
646 661
647 662 if self.online:
648 663 if self.__waitNewBlock():
649 664 return 1
650 665
651 666 if self.nReadBlocks < self.dataBlocksPerFile:
652 667 return 1
653 668
654 669 if not(self.setNextFile()):
655 670 return 0
656 671
657 672 deltaTime = self.utc - self.lastUTTime
658 673
659 674 self.flagDiscontinuousBlock = 0
660 675
661 676 if deltaTime > self.maxTimeStep:
662 677 self.flagDiscontinuousBlock = 1
663 678
664 679 return 1
665 680
666 681
667 682 def readNextBlock(self):
668 683 if not(self.__setNewBlock()):
669 684 return 0
670 685
671 686 if not(self.readBlock()):
672 687 return 0
673 688
674 689 return 1
675 690
676 691
677 692 def getData(self):
678 693
679 694 if self.flagNoMoreFiles:
680 695 self.dataOut.flagNoData = True
681 696 print 'Process finished'
682 697 return 0
683 698
684 699 self.flagDiscontinuousBlock = 0
685 700 self.flagIsNewBlock = 0
686 701
687 702 if not(self.readNextBlock()):
688 703 return 0
689 704
690 705 if self.data == None:
691 706 self.dataOut.flagNoData = True
692 707 return 0
693 708
694 709 self.dataOut.data = self.data
695 710 self.dataOut.data_header = self.data_header_dict
696 711 self.dataOut.utctime = self.utc
697 712
698 self.dataOut.header = self.header_dict
699 self.dataOut.expName = self.expName
700 self.dataOut.nChannels = self.nChannels
701 self.dataOut.timeZone = self.timeZone
702 self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile
703 self.dataOut.comments = self.comments
704 self.dataOut.timeInterval = self.timeInterval
705 self.dataOut.channelList = self.channelList
706 self.dataOut.heightList = self.heightList
713 # self.dataOut.header = self.header_dict
714 # self.dataOut.expName = self.expName
715 # self.dataOut.nChannels = self.nChannels
716 # self.dataOut.timeZone = self.timeZone
717 # self.dataOut.dataBlocksPerFile = self.dataBlocksPerFile
718 # self.dataOut.comments = self.comments
719 # # self.dataOut.timeInterval = self.timeInterval
720 # self.dataOut.channelList = self.channelList
721 # self.dataOut.heightList = self.heightList
707 722 self.dataOut.flagNoData = False
708 723
709 724 return self.dataOut.data
710 725
711 726 def run(self, **kwargs):
712 727
713 728 if not(self.isConfig):
714 729 self.setup(**kwargs)
715 730 self.isConfig = True
716 731
717 732 self.getData()
718 733
719 734 class SpectraHeisWriter(Operation):
720 735 # set = None
721 736 setFile = None
722 737 idblock = None
723 738 doypath = None
724 739 subfolder = None
725 740
726 741 def __init__(self):
727 742 self.wrObj = Fits()
728 743 # self.dataOut = dataOut
729 744 self.nTotalBlocks=0
730 745 # self.set = None
731 746 self.setFile = None
732 747 self.idblock = 0
733 748 self.wrpath = None
734 749 self.doypath = None
735 750 self.subfolder = None
736 751 self.isConfig = False
737 752
738 753 def isNumber(str):
739 754 """
740 755 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
741 756
742 757 Excepciones:
743 758 Si un determinado string no puede ser convertido a numero
744 759 Input:
745 760 str, string al cual se le analiza para determinar si convertible a un numero o no
746 761
747 762 Return:
748 763 True : si el string es uno numerico
749 764 False : no es un string numerico
750 765 """
751 766 try:
752 767 float( str )
753 768 return True
754 769 except:
755 770 return False
756 771
757 772 def setup(self, dataOut, wrpath):
758 773
759 774 if not(os.path.exists(wrpath)):
760 775 os.mkdir(wrpath)
761 776
762 777 self.wrpath = wrpath
763 778 # self.setFile = 0
764 779 self.dataOut = dataOut
765 780
766 781 def putData(self):
767 782 name= time.localtime( self.dataOut.utctime)
768 783 ext=".fits"
769 784
770 785 if self.doypath == None:
771 786 self.subfolder = 'F%4.4d%3.3d_%d' % (name.tm_year,name.tm_yday,time.mktime(datetime.datetime.now().timetuple()))
772 787 self.doypath = os.path.join( self.wrpath, self.subfolder )
773 788 os.mkdir(self.doypath)
774 789
775 790 if self.setFile == None:
776 791 # self.set = self.dataOut.set
777 792 self.setFile = 0
778 793 # if self.set != self.dataOut.set:
779 794 ## self.set = self.dataOut.set
780 795 # self.setFile = 0
781 796
782 797 #make the filename
783 798 thisFile = 'D%4.4d%3.3d_%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext)
784 799
785 800 filename = os.path.join(self.wrpath,self.subfolder, thisFile)
786 801
787 802 idblock = numpy.array([self.idblock],dtype="int64")
788 803 header=self.wrObj.cFImage(idblock=idblock,
789 804 year=time.gmtime(self.dataOut.utctime).tm_year,
790 805 month=time.gmtime(self.dataOut.utctime).tm_mon,
791 806 day=time.gmtime(self.dataOut.utctime).tm_mday,
792 807 hour=time.gmtime(self.dataOut.utctime).tm_hour,
793 808 minute=time.gmtime(self.dataOut.utctime).tm_min,
794 809 second=time.gmtime(self.dataOut.utctime).tm_sec)
795 810
796 811 c=3E8
797 812 deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
798 813 freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)*(c/(2*deltaHeight*1000))
799 814
800 815 colList = []
801 816
802 817 colFreq=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq)
803 818
804 819 colList.append(colFreq)
805 820
806 821 nchannel=self.dataOut.nChannels
807 822
808 823 for i in range(nchannel):
809 824 col = self.wrObj.writeData(name="PCh"+str(i+1),
810 825 format=str(self.dataOut.nFFTPoints)+'E',
811 826 data=10*numpy.log10(self.dataOut.data_spc[i,:]))
812 827
813 828 colList.append(col)
814 829
815 830 data=self.wrObj.Ctable(colList=colList)
816 831
817 832 self.wrObj.CFile(header,data)
818 833
819 834 self.wrObj.wFile(filename)
820 835
821 836 #update the setFile
822 837 self.setFile += 1
823 838 self.idblock += 1
824 839
825 840 return 1
826 841
827 842 def run(self, dataOut, **kwargs):
828 843
829 844 if not(self.isConfig):
830 845
831 846 self.setup(dataOut, **kwargs)
832 847 self.isConfig = True
833 848
834 849 self.putData() No newline at end of file
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now