##// END OF EJS Templates
Busqueda de archivos dentro del directorio indicado (sin busqueda de subdirectorios) activando el flag de lectura 'walk'
Miguel Valdez -
r224:b8cf5fb6064c
parent child
Show More
@@ -1,710 +1,719
1 1 '''
2 2 Created on September , 2012
3 3 @author:
4 4 '''
5 5 from xml.etree.ElementTree import Element, SubElement, ElementTree
6 6 from xml.etree import ElementTree as ET
7 7 from xml.dom import minidom
8 8
9 9 import sys
10 10 import datetime
11 11 from model.jrodataIO import *
12 12 from model.jroprocessing import *
13 13 from model.jroplot import *
14 14
15 15 def prettify(elem):
16 16 """Return a pretty-printed XML string for the Element.
17 17 """
18 18 rough_string = ET.tostring(elem, 'utf-8')
19 19 reparsed = minidom.parseString(rough_string)
20 20 return reparsed.toprettyxml(indent=" ")
21 21
22 22 class ParameterConf():
23 23
24 24 id = None
25 25 name = None
26 26 value = None
27 27 format = None
28 28
29 29 ELEMENTNAME = 'Parameter'
30 30
31 31 def __init__(self):
32 32
33 33 self.format = 'str'
34 34
35 35 def getElementName(self):
36 36
37 37 return self.ELEMENTNAME
38 38
39 39 def getValue(self):
40 40
41 value = self.value
42
41 43 if self.format == 'list':
42 strList = self.value.split(',')
44 strList = value.split(',')
43 45 return strList
44 46
45 47 if self.format == 'intlist':
46 strList = self.value.split(',')
48 strList = value.split(',')
47 49 intList = [int(x) for x in strList]
48 50 return intList
49 51
50 52 if self.format == 'floatlist':
51 strList = self.value.split(',')
53 strList = value.split(',')
52 54 floatList = [float(x) for x in strList]
53 55 return floatList
54 56
55 57 if self.format == 'date':
56 strList = self.value.split('/')
58 strList = value.split('/')
57 59 intList = [int(x) for x in strList]
58 60 date = datetime.date(intList[0], intList[1], intList[2])
59 61 return date
60 62
61 63 if self.format == 'time':
62 strList = self.value.split(':')
64 strList = value.split(':')
63 65 intList = [int(x) for x in strList]
64 66 time = datetime.time(intList[0], intList[1], intList[2])
65 67 return time
66
68
69 if self.format == 'bool':
70 value = int(value)
71
67 72 func = eval(self.format)
68
69 return func(self.value)
73
74 return func(value)
70 75
71 76 def setup(self, id, name, value, format='str'):
72 77
73 78 self.id = id
74 79 self.name = name
75 80 self.value = str(value)
76 81 self.format = format
77 82
78 83 def makeXml(self, opElement):
79 84
80 85 parmElement = SubElement(opElement, self.ELEMENTNAME)
81 86 parmElement.set('id', str(self.id))
82 87 parmElement.set('name', self.name)
83 88 parmElement.set('value', self.value)
84 89 parmElement.set('format', self.format)
85 90
86 91 def readXml(self, parmElement):
87 92
88 93 self.id = parmElement.get('id')
89 94 self.name = parmElement.get('name')
90 95 self.value = parmElement.get('value')
91 96 self.format = parmElement.get('format')
92 97
93 98 def printattr(self):
94 99
95 100 print "Parameter[%s]: name = %s, value = %s, format = %s" %(self.id, self.name, self.value, self.format)
96 101
97 102 class OperationConf():
98 103
99 104 id = None
100 105 name = None
101 106 priority = None
102 107 type = None
103 108
104 109 parmConfObjList = []
105 110
106 111 ELEMENTNAME = 'Operation'
107 112
108 113 def __init__(self):
109 114
110 115 id = 0
111 116 name = None
112 117 priority = None
113 118 type = 'self'
114 119
115 120
116 121 def __getNewId(self):
117 122
118 123 return int(self.id)*10 + len(self.parmConfObjList) + 1
119 124
120 125 def getElementName(self):
121 126
122 127 return self.ELEMENTNAME
123 128
124 129 def getParameterObjList(self):
125 130
126 131 return self.parmConfObjList
127 132
128 133 def setup(self, id, name, priority, type):
129 134
130 135 self.id = id
131 136 self.name = name
132 137 self.type = type
133 138 self.priority = priority
134 139
135 140 self.parmConfObjList = []
136 141
137 142 def addParameter(self, name, value, format='str'):
138 143
139 144 id = self.__getNewId()
140 145
141 146 parmConfObj = ParameterConf()
142 147 parmConfObj.setup(id, name, value, format)
143 148
144 149 self.parmConfObjList.append(parmConfObj)
145 150
146 151 return parmConfObj
147 152
148 153 def makeXml(self, upElement):
149 154
150 155 opElement = SubElement(upElement, self.ELEMENTNAME)
151 156 opElement.set('id', str(self.id))
152 157 opElement.set('name', self.name)
153 158 opElement.set('type', self.type)
154 159 opElement.set('priority', str(self.priority))
155 160
156 161 for parmConfObj in self.parmConfObjList:
157 162 parmConfObj.makeXml(opElement)
158 163
159 164 def readXml(self, opElement):
160 165
161 166 self.id = opElement.get('id')
162 167 self.name = opElement.get('name')
163 168 self.type = opElement.get('type')
164 169 self.priority = opElement.get('priority')
165 170
166 171 self.parmConfObjList = []
167 172
168 173 parmElementList = opElement.getiterator(ParameterConf().getElementName())
169 174
170 175 for parmElement in parmElementList:
171 176 parmConfObj = ParameterConf()
172 177 parmConfObj.readXml(parmElement)
173 178 self.parmConfObjList.append(parmConfObj)
174 179
175 180 def printattr(self):
176 181
177 182 print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME,
178 183 self.id,
179 184 self.name,
180 185 self.type,
181 186 self.priority)
182 187
183 188 for parmConfObj in self.parmConfObjList:
184 189 parmConfObj.printattr()
185 190
186 191 def createObject(self):
187 192
188 193 if self.type == 'self':
189 194 raise ValueError, "This operation type cannot be created"
190 195
191 196 if self.type == 'other':
192 197 className = eval(self.name)
193 198 opObj = className()
194 199
195 200 return opObj
196 201
197 202 class ProcUnitConf():
198 203
199 204 id = None
200 205 name = None
201 206 datatype = None
202 207 inputId = None
203 208
204 209 opConfObjList = []
205 210
206 211 procUnitObj = None
207 212 opObjList = []
208 213
209 214 ELEMENTNAME = 'ProcUnit'
210 215
211 216 def __init__(self):
212 217
213 218 self.id = None
214 219 self.datatype = None
215 220 self.name = None
216 221 self.inputId = None
217 222
218 223 self.opConfObjList = []
219 224
220 225 self.procUnitObj = None
221 226 self.opObjDict = {}
222 227
223 228 def __getPriority(self):
224 229
225 230 return len(self.opConfObjList)+1
226 231
227 232 def __getNewId(self):
228 233
229 234 return int(self.id)*10 + len(self.opConfObjList) + 1
230 235
231 236 def getElementName(self):
232 237
233 238 return self.ELEMENTNAME
234 239
235 240 def getId(self):
236 241
237 242 return str(self.id)
238 243
239 244 def getInputId(self):
240 245
241 246 return str(self.inputId)
242 247
243 248 def getOperationObjList(self):
244 249
245 250 return self.opConfObjList
246 251
247 252 def getProcUnitObj(self):
248 253
249 254 return self.procUnitObj
250 255
251 256 def setup(self, id, name, datatype, inputId):
252 257
253 258 self.id = id
254 259 self.name = name
255 260 self.datatype = datatype
256 261 self.inputId = inputId
257 262
258 263 self.opConfObjList = []
259 264
260 265 self.addOperation(name='init', optype='self')
261 266
262 267 def addParameter(self, **kwargs):
263 268
264 269 opObj = self.opConfObjList[0]
265 270
266 271 opObj.addParameter(**kwargs)
267 272
268 273 return opObj
269 274
270 275 def addOperation(self, name, optype='self'):
271 276
272 277 id = self.__getNewId()
273 278 priority = self.__getPriority()
274 279
275 280 opConfObj = OperationConf()
276 281 opConfObj.setup(id, name=name, priority=priority, type=optype)
277 282
278 283 self.opConfObjList.append(opConfObj)
279 284
280 285 return opConfObj
281 286
282 287 def makeXml(self, procUnitElement):
283 288
284 289 upElement = SubElement(procUnitElement, self.ELEMENTNAME)
285 290 upElement.set('id', str(self.id))
286 291 upElement.set('name', self.name)
287 292 upElement.set('datatype', self.datatype)
288 293 upElement.set('inputId', str(self.inputId))
289 294
290 295 for opConfObj in self.opConfObjList:
291 296 opConfObj.makeXml(upElement)
292 297
293 298 def readXml(self, upElement):
294 299
295 300 self.id = upElement.get('id')
296 301 self.name = upElement.get('name')
297 302 self.datatype = upElement.get('datatype')
298 303 self.inputId = upElement.get('inputId')
299 304
300 305 self.opConfObjList = []
301 306
302 307 opElementList = upElement.getiterator(OperationConf().getElementName())
303 308
304 309 for opElement in opElementList:
305 310 opConfObj = OperationConf()
306 311 opConfObj.readXml(opElement)
307 312 self.opConfObjList.append(opConfObj)
308 313
309 314 def printattr(self):
310 315
311 316 print "%s[%s]: name = %s, datatype = %s, inputId = %s" %(self.ELEMENTNAME,
312 317 self.id,
313 318 self.name,
314 319 self.datatype,
315 320 self.inputId)
316 321
317 322 for opConfObj in self.opConfObjList:
318 323 opConfObj.printattr()
319 324
320 325 def createObjects(self):
321 326
322 327 className = eval(self.name)
323 328 procUnitObj = className()
324 329
325 330 for opConfObj in self.opConfObjList:
326 331
327 332 if opConfObj.type == 'self':
328 333 continue
329 334
330 335 opObj = opConfObj.createObject()
331 336
332 337 self.opObjDict[opConfObj.id] = opObj
333 338 procUnitObj.addOperation(opObj, opConfObj.id)
334 339
335 340 self.procUnitObj = procUnitObj
336 341
337 342 return procUnitObj
338 343
339 344 def run(self):
340 345
341 346 finalSts = False
342 347
343 348 for opConfObj in self.opConfObjList:
344 349
345 350 kwargs = {}
346 351 for parmConfObj in opConfObj.getParameterObjList():
347 352 kwargs[parmConfObj.name] = parmConfObj.getValue()
348 353
349 354 #print "\tRunning the '%s' operation with %s" %(opConfObj.name, opConfObj.id)
350 355 sts = self.procUnitObj.call(opConfObj, **kwargs)
351 356 finalSts = finalSts or sts
352 357
353 358 return finalSts
354 359
355 360 class ReadUnitConf(ProcUnitConf):
356 361
357 362 path = None
358 363 startDate = None
359 364 endDate = None
360 365 startTime = None
361 366 endTime = None
362 online = None
363 expLabel = None
364 delay = None
365 367
366 368 ELEMENTNAME = 'ReadUnit'
367 369
368 370 def __init__(self):
369 371
370 372 self.id = None
371 373 self.datatype = None
372 374 self.name = None
373 375 self.inputId = 0
374 376
375 377 self.opConfObjList = []
376 378 self.opObjList = []
377 379
378 380 def getElementName(self):
379 381
380 382 return self.ELEMENTNAME
381 383
382 def setup(self, id, name, datatype, path, startDate, endDate, startTime, endTime, online=0, expLabel='', delay=60):
384 def setup(self, id, name, datatype, path, startDate, endDate, startTime, endTime, **kwargs):
383 385
384 386 self.id = id
385 387 self.name = name
386 388 self.datatype = datatype
387 389
388 390 self.path = path
389 391 self.startDate = startDate
390 392 self.endDate = endDate
391 393 self.startTime = startTime
392 394 self.endTime = endTime
393 self.online = online
394 self.expLabel = expLabel
395 self.delay = delay
396 395
397 self.addRunOperation()
396 self.addRunOperation(**kwargs)
398 397
399 def addRunOperation(self):
398 def addRunOperation(self, **kwargs):
400 399
401 400 opObj = self.addOperation(name = 'run', optype = 'self')
402 401
403 402 opObj.addParameter(name='path' , value=self.path, format='str')
404 403 opObj.addParameter(name='startDate' , value=self.startDate, format='date')
405 404 opObj.addParameter(name='endDate' , value=self.endDate, format='date')
406 405 opObj.addParameter(name='startTime' , value=self.startTime, format='time')
407 406 opObj.addParameter(name='endTime' , value=self.endTime, format='time')
408 opObj.addParameter(name='expLabel' , value=self.expLabel, format='str')
409 opObj.addParameter(name='online' , value=self.online, format='int')
410 opObj.addParameter(name='delay' , value=self.delay, format='float')
411
407
408 for key, value in kwargs.items():
409 opObj.addParameter(name=key, value=value, format=type(value).__name__)
410
412 411 return opObj
413 412
414 413
415 class Controller():
414 class Project():
416 415
417 416 id = None
418 417 name = None
419 418 description = None
420 419 # readUnitConfObjList = None
421 420 procUnitConfObjDict = None
422 421
423 ELEMENTNAME = 'Controller'
422 ELEMENTNAME = 'Project'
424 423
425 424 def __init__(self):
426 425
427 426 self.id = None
428 427 self.name = None
429 428 self.description = None
430 429
431 430 # self.readUnitConfObjList = []
432 431 self.procUnitConfObjDict = {}
433 432
434 433 def __getNewId(self):
435 434
436 435 id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1
437 436
438 437 return str(id)
439 438
440 439 def getElementName(self):
441 440
442 441 return self.ELEMENTNAME
443 442
444 443 def setup(self, id, name, description):
445 444
446 445 self.id = id
447 446 self.name = name
448 447 self.description = description
449 448
450 def addReadUnit(self, datatype, path, startDate='', endDate='', startTime='', endTime='', online=0, expLabel='', delay=60):
449 def addReadUnit(self, datatype, path, startDate='', endDate='', startTime='', endTime='', **kwargs):
451 450
452 451 id = self.__getNewId()
453 452 name = '%sReader' %(datatype)
454 453
455 454 readUnitConfObj = ReadUnitConf()
456 readUnitConfObj.setup(id, name, datatype, path, startDate, endDate, startTime, endTime, online, expLabel, delay)
455 readUnitConfObj.setup(id, name, datatype, path, startDate, endDate, startTime, endTime, **kwargs)
457 456
458 457 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
459 458
460 459 return readUnitConfObj
461 460
462 461 def addProcUnit(self, datatype, inputId):
463 462
464 463 id = self.__getNewId()
465 464 name = '%sProc' %(datatype)
466 465
467 466 procUnitConfObj = ProcUnitConf()
468 467 procUnitConfObj.setup(id, name, datatype, inputId)
469 468
470 469 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
471 470
472 471 return procUnitConfObj
473 472
474 473 def makeXml(self):
475 474
476 projectElement = Element('Controller')
475 projectElement = Element('Project')
477 476 projectElement.set('id', str(self.id))
478 477 projectElement.set('name', self.name)
479 478 projectElement.set('description', self.description)
480 479
481 480 # for readUnitConfObj in self.readUnitConfObjList:
482 481 # readUnitConfObj.makeXml(projectElement)
483 482
484 483 for procUnitConfObj in self.procUnitConfObjDict.values():
485 484 procUnitConfObj.makeXml(projectElement)
486 485
487 486 self.projectElement = projectElement
488 487
489 488 def writeXml(self, filename):
490 489
491 490 self.makeXml()
492 491
493 492 print prettify(self.projectElement)
494 493
495 494 ElementTree(self.projectElement).write(filename, method='xml')
496 495
497 496 def readXml(self, filename):
498 497
499 498 #tree = ET.parse(filename)
500 499 self.projectElement = None
501 500 # self.readUnitConfObjList = []
502 501 self.procUnitConfObjDict = {}
503 502
504 503 self.projectElement = ElementTree().parse(filename)
505 504
506 505 self.project = self.projectElement.tag
507 506
508 507 self.id = self.projectElement.get('id')
509 508 self.name = self.projectElement.get('name')
510 509 self.description = self.projectElement.get('description')
511 510
512 511 readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName())
513 512
514 513 for readUnitElement in readUnitElementList:
515 514 readUnitConfObj = ReadUnitConf()
516 515 readUnitConfObj.readXml(readUnitElement)
517 516
518 517 self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj
519 518
520 519 procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName())
521 520
522 521 for procUnitElement in procUnitElementList:
523 522 procUnitConfObj = ProcUnitConf()
524 523 procUnitConfObj.readXml(procUnitElement)
525 524
526 525 self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj
527 526
528 527 def printattr(self):
529 528
530 print "Controller[%s]: name = %s, description = %s" %(self.id,
529 print "Project[%s]: name = %s, description = %s" %(self.id,
531 530 self.name,
532 531 self.description)
533 532
534 533 # for readUnitConfObj in self.readUnitConfObjList:
535 534 # readUnitConfObj.printattr()
536 535
537 536 for procUnitConfObj in self.procUnitConfObjDict.values():
538 537 procUnitConfObj.printattr()
539 538
540 539 def createObjects(self):
541 540
542 541 # for readUnitConfObj in self.readUnitConfObjList:
543 542 # readUnitConfObj.createObjects()
544 543
545 544 for procUnitConfObj in self.procUnitConfObjDict.values():
546 545 procUnitConfObj.createObjects()
547 546
548 547 def __connect(self, objIN, obj):
549 548
550 549 obj.setInput(objIN.getOutput())
551 550
552 551 def connectObjects(self):
553 552
554 553 for puConfObj in self.procUnitConfObjDict.values():
555 554
556 555 inputId = puConfObj.getInputId()
557 556
558 557 if int(inputId) == 0:
559 558 continue
560 559
561 560 puConfINObj = self.procUnitConfObjDict[inputId]
562 561
563 562 puObj = puConfObj.getProcUnitObj()
564 563 puINObj = puConfINObj.getProcUnitObj()
565 564
566 565 self.__connect(puINObj, puObj)
567 566
568 567 def run(self):
569 568
570 569 # for readUnitConfObj in self.readUnitConfObjList:
571 570 # readUnitConfObj.run()
572 571
573 572 while(True):
574 573
575 574 finalSts = False
576 575
577 576 for procUnitConfObj in self.procUnitConfObjDict.values():
578 577 #print "Running the '%s' process with %s" %(procUnitConfObj.name, procUnitConfObj.id)
579 578 sts = procUnitConfObj.run()
580 579 finalSts = finalSts or sts
581 580
582 581 #If every process unit finished so end process
583 582 if not(finalSts):
584 583 print "Every process units have finished"
585 584 break
586 585
587 586 if __name__ == '__main__':
588 587
589 588 desc = "Segundo Test"
590 589 filename = "schain.xml"
591 590
592 controllerObj = Controller()
591 controllerObj = Project()
593 592
594 593 controllerObj.setup(id = '191', name='test01', description=desc)
595 594
596 595 readUnitConfObj = controllerObj.addReadUnit(datatype='Voltage',
597 596 path='data/rawdata/',
598 597 startDate='2011/01/01',
599 598 endDate='2012/12/31',
600 599 startTime='00:00:00',
601 600 endTime='23:59:59',
602 online=0)
601 online=1,
602 walk=1)
603 603
604 604 # opObj00 = readUnitConfObj.addOperation(name='printInfo')
605 605
606 606 procUnitConfObj0 = controllerObj.addProcUnit(datatype='Voltage', inputId=readUnitConfObj.getId())
607 607
608 608 opObj10 = procUnitConfObj0.addOperation(name='selectChannels')
609 609 opObj10.addParameter(name='channelList', value='3,4,5', format='intlist')
610 610
611 611 opObj10 = procUnitConfObj0.addOperation(name='selectHeights')
612 612 opObj10.addParameter(name='minHei', value='90', format='float')
613 613 opObj10.addParameter(name='maxHei', value='180', format='float')
614 614
615 615 opObj12 = procUnitConfObj0.addOperation(name='CohInt', optype='other')
616 616 opObj12.addParameter(name='n', value='10', format='int')
617 617
618 618 procUnitConfObj1 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj0.getId())
619 619 procUnitConfObj1.addParameter(name='nFFTPoints', value='32', format='int')
620 # procUnitConfObj1.addParameter(name='pairList', value='(0,1),(0,2),(1,2)', format='')
621
620 622
621 623 opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='other')
622 624 opObj11.addParameter(name='idfigure', value='1', format='int')
623 625 opObj11.addParameter(name='wintitle', value='SpectraPlot0', format='str')
624 626 opObj11.addParameter(name='zmin', value='40', format='int')
625 627 opObj11.addParameter(name='zmax', value='90', format='int')
626 628 opObj11.addParameter(name='showprofile', value='1', format='int')
627
628 procUnitConfObj2 = controllerObj.addProcUnit(datatype='Voltage', inputId=procUnitConfObj0.getId())
629
630 opObj12 = procUnitConfObj2.addOperation(name='CohInt', optype='other')
631 opObj12.addParameter(name='n', value='2', format='int')
632 opObj12.addParameter(name='overlapping', value='1', format='int')
633 629
634 procUnitConfObj3 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj2.getId())
635 procUnitConfObj3.addParameter(name='nFFTPoints', value='32', format='int')
636
637 opObj11 = procUnitConfObj3.addOperation(name='SpectraPlot', optype='other')
638 opObj11.addParameter(name='idfigure', value='2', format='int')
639 opObj11.addParameter(name='wintitle', value='SpectraPlot1', format='str')
640 opObj11.addParameter(name='zmin', value='40', format='int')
641 opObj11.addParameter(name='zmax', value='90', format='int')
630 # opObj11 = procUnitConfObj1.addOperation(name='CrossSpectraPlot', optype='other')
631 # opObj11.addParameter(name='idfigure', value='2', format='int')
632 # opObj11.addParameter(name='wintitle', value='CrossSpectraPlot', format='str')
633 # opObj11.addParameter(name='zmin', value='40', format='int')
634 # opObj11.addParameter(name='zmax', value='90', format='int')
635
636
637 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Voltage', inputId=procUnitConfObj0.getId())
638 #
639 # opObj12 = procUnitConfObj2.addOperation(name='CohInt', optype='other')
640 # opObj12.addParameter(name='n', value='2', format='int')
641 # opObj12.addParameter(name='overlapping', value='1', format='int')
642 #
643 # procUnitConfObj3 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj2.getId())
644 # procUnitConfObj3.addParameter(name='nFFTPoints', value='32', format='int')
645 #
646 # opObj11 = procUnitConfObj3.addOperation(name='SpectraPlot', optype='other')
647 # opObj11.addParameter(name='idfigure', value='2', format='int')
648 # opObj11.addParameter(name='wintitle', value='SpectraPlot1', format='str')
649 # opObj11.addParameter(name='zmin', value='40', format='int')
650 # opObj11.addParameter(name='zmax', value='90', format='int')
642 651 # opObj11.addParameter(name='showprofile', value='1', format='int')
643 652
644 653 # opObj11 = procUnitConfObj1.addOperation(name='RTIPlot', optype='other')
645 654 # opObj11.addParameter(name='idfigure', value='10', format='int')
646 655 # opObj11.addParameter(name='wintitle', value='RTI', format='str')
647 656 ## opObj11.addParameter(name='xmin', value='21', format='float')
648 657 ## opObj11.addParameter(name='xmax', value='22', format='float')
649 658 # opObj11.addParameter(name='zmin', value='40', format='int')
650 659 # opObj11.addParameter(name='zmax', value='90', format='int')
651 660 # opObj11.addParameter(name='showprofile', value='1', format='int')
652 661 # opObj11.addParameter(name='timerange', value=str(60), format='int')
653 662
654 663 # opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
655 664 # opObj10.addParameter(name='channelList', value='0,2,4,6', format='intlist')
656 665 #
657 666 # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='other')
658 667 # opObj12.addParameter(name='n', value='2', format='int')
659 668 #
660 669 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='other')
661 670 # opObj11.addParameter(name='idfigure', value='2', format='int')
662 671 # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str')
663 672 # opObj11.addParameter(name='zmin', value='70', format='int')
664 673 # opObj11.addParameter(name='zmax', value='90', format='int')
665 674 #
666 675 # opObj10 = procUnitConfObj1.addOperation(name='selectChannels')
667 676 # opObj10.addParameter(name='channelList', value='2,6', format='intlist')
668 677 #
669 678 # opObj12 = procUnitConfObj1.addOperation(name='IncohInt', optype='other')
670 679 # opObj12.addParameter(name='n', value='2', format='int')
671 680 #
672 681 # opObj11 = procUnitConfObj1.addOperation(name='SpectraPlot', optype='other')
673 682 # opObj11.addParameter(name='idfigure', value='3', format='int')
674 683 # opObj11.addParameter(name='wintitle', value='SpectraPlot10', format='str')
675 684 # opObj11.addParameter(name='zmin', value='70', format='int')
676 685 # opObj11.addParameter(name='zmax', value='90', format='int')
677 686
678 687
679 688 # opObj12 = procUnitConfObj1.addOperation(name='decoder')
680 689 # opObj12.addParameter(name='ncode', value='2', format='int')
681 690 # opObj12.addParameter(name='nbauds', value='8', format='int')
682 691 # opObj12.addParameter(name='code0', value='001110011', format='int')
683 692 # opObj12.addParameter(name='code1', value='001110011', format='int')
684 693
685 694
686 695
687 696 # procUnitConfObj2 = controllerObj.addProcUnit(datatype='Spectra', inputId=procUnitConfObj1.getId())
688 697 #
689 698 # opObj21 = procUnitConfObj2.addOperation(name='IncohInt', optype='other')
690 699 # opObj21.addParameter(name='n', value='2', format='int')
691 700 #
692 701 # opObj11 = procUnitConfObj2.addOperation(name='SpectraPlot', optype='other')
693 702 # opObj11.addParameter(name='idfigure', value='4', format='int')
694 703 # opObj11.addParameter(name='wintitle', value='SpectraPlot OBJ 2', format='str')
695 704 # opObj11.addParameter(name='zmin', value='70', format='int')
696 705 # opObj11.addParameter(name='zmax', value='90', format='int')
697 706
698 707 print "Escribiendo el archivo XML"
699 708
700 709 controllerObj.writeXml(filename)
701 710
702 711 print "Leyendo el archivo XML"
703 712 controllerObj.readXml(filename)
704 713 #controllerObj.printattr()
705 714
706 715 controllerObj.createObjects()
707 716 controllerObj.connectObjects()
708 717 controllerObj.run()
709 718
710 719 No newline at end of file
@@ -1,305 +1,285
1 1 import numpy
2 2 import datetime
3 3 import matplotlib
4 4 matplotlib.use("TKAgg")
5 5 import matplotlib.pyplot
6 6 import matplotlib.dates
7 7 #import scitools.numpyutils
8 8 from mpl_toolkits.axes_grid1 import make_axes_locatable
9 9
10 10 from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, SecondLocator, DateFormatter
11 11 from matplotlib.ticker import FuncFormatter
12 12 from matplotlib.ticker import *
13 13
14 14 #def init(idfigure, wintitle, width, height, facecolor="w"):
15 15 #
16 16 # matplotlib.pyplot.ioff()
17 17 # fig = matplotlib.pyplot.matplotlib.pyplot.figure(num=idfigure, facecolor=facecolor)
18 18 # fig.canvas.manager.set_window_title(wintitle)
19 19 # fig.canvas.manager.resize(width, height)
20 20 # matplotlib.pyplot.ion()
21 21 #
22 22 # return fig
23 23 #
24 24 #def setWinTitle(fig, title):
25 25 #
26 26 # fig.canvas.manager.set_window_title(title)
27 27 #
28 28 #def setTitle(idfigure, title):
29 29 # fig = matplotlib.pyplot.figure(idfigure)
30 30 # fig.suptitle(title)
31 31 #
32 32 #def makeAxes(idfigure, nrow, ncol, xpos, ypos, colspan, rowspan):
33 33 # fig = matplotlib.pyplot.figure(idfigure)
34 34 # ax = matplotlib.pyplot.subplot2grid((nrow, ncol), (xpos, ypos), colspan=colspan, rowspan=rowspan)
35 35 # return ax
36 36 #
37 37 #def setTextFromAxes(idfigure, ax, title):
38 38 # fig = matplotlib.pyplot.figure(idfigure)
39 39 # ax.annotate(title, xy=(.1, .99),
40 40 # xycoords='figure fraction',
41 41 # horizontalalignment='left', verticalalignment='top',
42 42 # fontsize=10)
43 43 #
44 44 #def pline(ax, x, y, xmin, xmax, ymin, ymax, xlabel, ylabel, title, firsttime):
45 45 #
46 46 # if firsttime:
47 47 # ax.plot(x, y)
48 48 # ax.set_xlim([xmin,xmax])
49 49 # ax.set_ylim([ymin,ymax])
50 50 # ax.set_xlabel(xlabel, size=8)
51 51 # ax.set_ylabel(ylabel, size=8)
52 52 # ax.set_title(title, size=10)
53 53 # matplotlib.pyplot.tight_layout()
54 54 # else:
55 55 # ax.lines[0].set_data(x,y)
56 56 #
57 57 #def draw(idfigure):
58 58 #
59 59 # fig = matplotlib.pyplot.figure(idfigure)
60 60 # fig.canvas.draw()
61 61 #
62 62 #def pcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax, xlabel, ylabel, title, firsttime, mesh):
63 63 #
64 64 # if firsttime:
65 65 # divider = make_axes_locatable(ax)
66 66 # ax_cb = divider.new_horizontal(size="4%", pad=0.05)
67 67 # fig1 = ax.get_figure()
68 68 # fig1.add_axes(ax_cb)
69 69 #
70 70 # ax.set_xlim([xmin,xmax])
71 71 # ax.set_ylim([ymin,ymax])
72 72 # ax.set_xlabel(xlabel)
73 73 # ax.set_ylabel(ylabel)
74 74 # ax.set_title(title)
75 75 # print x
76 76 # imesh=ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax)
77 77 # matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
78 78 # ax_cb.yaxis.tick_right()
79 79 # for tl in ax_cb.get_yticklabels():
80 80 # tl.set_visible(True)
81 81 # ax_cb.yaxis.tick_right()
82 82 # matplotlib.pyplot.tight_layout()
83 83 # return imesh
84 84 # else:
85 85 ## ax.set_xlim([xmin,xmax])
86 86 ## ax.set_ylim([ymin,ymax])
87 87 # ax.set_xlabel(xlabel)
88 88 # ax.set_ylabel(ylabel)
89 89 # ax.set_title(title)
90 90 #
91 91 # z = z.T
92 92 ## z = z[0:-1,0:-1]
93 93 # mesh.set_array(z.ravel())
94 94 #
95 95 # return mesh
96 96
97 97 ###########################################
98 98 #Actualizacion de las funciones del driver
99 99 ###########################################
100 100
101 101 def createFigure(idfigure, wintitle, width, height, facecolor="w"):
102 102
103 103 matplotlib.pyplot.ioff()
104 104 fig = matplotlib.pyplot.matplotlib.pyplot.figure(num=idfigure, facecolor=facecolor)
105 105 fig.canvas.manager.set_window_title(wintitle)
106 106 fig.canvas.manager.resize(width, height)
107 107 matplotlib.pyplot.ion()
108 108
109 109 return fig
110 110
111 111 def closeFigure():
112 112
113 113 matplotlib.pyplot.ioff()
114 114 matplotlib.pyplot.show()
115 115
116 116 return
117 117
118 118 def saveFigure(fig, filename):
119 119 fig.savefig(filename)
120 120
121 121 def setWinTitle(fig, title):
122 122
123 123 fig.canvas.manager.set_window_title(title)
124 124
125 125 def setTitle(fig, title):
126 126
127 127 fig.suptitle(title)
128 128
129 129 def createAxes(fig, nrow, ncol, xpos, ypos, colspan, rowspan):
130 130
131 131 matplotlib.pyplot.figure(fig.number)
132 132 axes = matplotlib.pyplot.subplot2grid((nrow, ncol),
133 133 (xpos, ypos),
134 134 colspan=colspan,
135 135 rowspan=rowspan)
136 136 return axes
137 137
138 138 def setAxesText(ax, text):
139 139
140 140 ax.annotate(text,
141 141 xy = (.1, .99),
142 142 xycoords = 'figure fraction',
143 143 horizontalalignment = 'left',
144 144 verticalalignment = 'top',
145 145 fontsize = 10)
146 146
147 147 def printLabels(ax, xlabel, ylabel, title):
148 148
149 149 ax.set_xlabel(xlabel, size=11)
150 150 ax.set_ylabel(ylabel, size=11)
151 151 ax.set_title(title, size=12)
152 152
153 153 def createPline(ax, x, y, xmin, xmax, ymin, ymax, xlabel='', ylabel='', title='',
154 154 ticksize=9, xtick_visible=True, ytick_visible=True,
155 155 nxticks=4, nyticks=10,
156 156 grid=None):
157 157
158 158 """
159 159
160 160 Input:
161 161 grid : None, 'both', 'x', 'y'
162 162 """
163 163
164 164 ax.plot(x, y)
165 165 ax.set_xlim([xmin,xmax])
166 166 ax.set_ylim([ymin,ymax])
167 167
168 168 printLabels(ax, xlabel, ylabel, title)
169 169
170 170 ######################################################
171 171 xtickspos = numpy.arange(nxticks)*int((xmax-xmin)/nxticks) + int(xmin)
172 172 ax.set_xticks(xtickspos)
173 173
174 174 for tick in ax.get_xticklabels():
175 175 tick.set_visible(xtick_visible)
176 176
177 177 for tick in ax.xaxis.get_major_ticks():
178 178 tick.label.set_fontsize(ticksize)
179 179
180 180 ######################################################
181 181 for tick in ax.get_yticklabels():
182 182 tick.set_visible(ytick_visible)
183 183
184 184 for tick in ax.yaxis.get_major_ticks():
185 185 tick.label.set_fontsize(ticksize)
186 186
187 187 iplot = ax.lines[-1]
188 188
189 189 ######################################################
190 190 if '0.' in matplotlib.__version__[0:2]:
191 191 print "The matplotlib version has to be updated to 1.1 or newer"
192 192 return iplot
193 193
194 194 if '1.0.' in matplotlib.__version__[0:4]:
195 195 print "The matplotlib version has to be updated to 1.1 or newer"
196 196 return iplot
197 197
198 198 if grid != None:
199 199 ax.grid(b=True, which='major', axis=grid)
200 200
201 201 matplotlib.pyplot.tight_layout()
202 202
203 203 return iplot
204 204
205 205 def pline(iplot, x, y, xlabel='', ylabel='', title=''):
206 206
207 207 ax = iplot.get_axes()
208 208
209 209 printLabels(ax, xlabel, ylabel, title)
210 210
211 211 iplot.set_data(x, y)
212 212
213 213 def createPcolor(ax, x, y, z, xmin, xmax, ymin, ymax, zmin, zmax,
214 214 xlabel='', ylabel='', title='', ticksize = 9,
215 215 cblabel='', cbsize="5%",
216 216 XAxisAsTime=False):
217 217
218 218 divider = make_axes_locatable(ax)
219 219 ax_cb = divider.new_horizontal(size=cbsize, pad=0.05)
220 220 fig = ax.get_figure()
221 221 fig.add_axes(ax_cb)
222 222
223 223 ax.set_xlim([xmin,xmax])
224 224 ax.set_ylim([ymin,ymax])
225 225
226 226 printLabels(ax, xlabel, ylabel, title)
227 227
228 228 imesh = ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax)
229 229 cb = matplotlib.pyplot.colorbar(imesh, cax=ax_cb)
230 230 cb.set_label(cblabel)
231 231
232 232 # for tl in ax_cb.get_yticklabels():
233 233 # tl.set_visible(True)
234 234
235 235 for tick in ax.yaxis.get_major_ticks():
236 236 tick.label.set_fontsize(ticksize)
237 237
238 238 for tick in ax.xaxis.get_major_ticks():
239 239 tick.label.set_fontsize(ticksize)
240 240
241 241 for tick in cb.ax.get_yticklabels():
242 242 tick.set_fontsize(ticksize)
243 243
244 244 ax_cb.yaxis.tick_right()
245 245
246 246 if '0.' in matplotlib.__version__[0:2]:
247 247 print "The matplotlib version has to be updated to 1.1 or newer"
248 248 return imesh
249 249
250 250 if '1.0.' in matplotlib.__version__[0:4]:
251 251 print "The matplotlib version has to be updated to 1.1 or newer"
252 252 return imesh
253 253
254 254 matplotlib.pyplot.tight_layout()
255 255
256 256 if XAxisAsTime:
257 257
258 func = lambda x, pos: ('%s') %(datetime.datetime.fromtimestamp(x).strftime("%H:%M:%S"))
258 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime("%H:%M:%S"))
259 259 ax.xaxis.set_major_formatter(FuncFormatter(func))
260 260 ax.xaxis.set_major_locator(LinearLocator(7))
261 261
262 # seconds = numpy.array([xmin, xmax])
263 # datesList = map(datetime.datetime.fromtimestamp, seconds)
264 # ax.set_xlim([datesList[0],datesList[-1]])
265 # ax.xaxis.set_major_locator(MinuteLocator(numpy.arange(0,61,10)))
266 # ax.xaxis.set_minor_locator(SecondLocator(numpy.arange(0,61,60)))
267 # ax.xaxis.set_major_formatter(DateFormatter("%H:%M:%S"))
268 # xdateList = map(datetime.datetime.fromtimestamp, x)
269 # xdate = matplotlib.dates.date2num(xdateList)
270 # x = xdate
271
272 # labels = []
273 # for item in ax.xaxis.get_ticklabels():
274 # stri = item.get_text()
275 # text = datetime.datetime.fromtimestamp(float(stri))
276 # labels.append(text)
277 #
278 # ax.xaxis.set_ticklabels(labels)
279 262 return imesh
280 263
281 264 def pcolor(imesh, z, xlabel='', ylabel='', title=''):
282 265
283 266 z = z.T
284 267
285 268 ax = imesh.get_axes()
286 269
287 270 printLabels(ax, xlabel, ylabel, title)
288 271
289 272 imesh.set_array(z.ravel())
290 273
291 274 def addpcolor(ax, x, y, z, zmin, zmax, xlabel='', ylabel='', title=''):
292 275
293 # xdateList = map(datetime.datetime.fromtimestamp, x)
294 # xdate = matplotlib.dates.date2num(xdateList)
295
296 276 printLabels(ax, xlabel, ylabel, title)
297 277
298 278 imesh = ax.pcolormesh(x,y,z.T,vmin=zmin,vmax=zmax)
299 279
300 280 def draw(fig):
301 281
302 282 if type(fig) == 'int':
303 283 raise ValueError, "This parameter should be of tpye matplotlib figure"
304 284
305 285 fig.canvas.draw() No newline at end of file
@@ -1,506 +1,511
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 os, sys
8 8 import copy
9 9 import numpy
10 import datetime
10 11
11 12 from jroheaderIO import SystemHeader, RadarControllerHeader
12 13
13 14 def hildebrand_sekhon(data, navg):
14 15 """
15 16 This method is for the objective determination of de noise level in Doppler spectra. This
16 17 implementation technique is based on the fact that the standard deviation of the spectral
17 18 densities is equal to the mean spectral density for white Gaussian noise
18 19
19 20 Inputs:
20 21 Data : heights
21 22 navg : numbers of averages
22 23
23 24 Return:
24 25 -1 : any error
25 26 anoise : noise's level
26 27 """
27 28
28 29 dataflat = data.copy().reshape(-1)
29 30 dataflat.sort()
30 31 npts = dataflat.size #numbers of points of the data
31 32
32 33 if npts < 32:
33 34 print "error in noise - requires at least 32 points"
34 35 return -1.0
35 36
36 37 dataflat2 = numpy.power(dataflat,2)
37 38
38 39 cs = numpy.cumsum(dataflat)
39 40 cs2 = numpy.cumsum(dataflat2)
40 41
41 42 # data sorted in ascending order
42 43 nmin = int((npts + 7.)/8)
43 44
44 45 for i in range(nmin, npts):
45 46 s = cs[i]
46 47 s2 = cs2[i]
47 48 p = s / float(i);
48 49 p2 = p**2;
49 50 q = s2 / float(i) - p2;
50 51 leftc = p2;
51 52 rightc = q * float(navg);
52 53 R2 = leftc/rightc
53 54
54 55 # Signal detect: R2 < 1 (R2 = leftc/rightc)
55 56 if R2 < 1:
56 57 npts_noise = i
57 58 break
58 59
59 60
60 61 anoise = numpy.average(dataflat[0:npts_noise])
61 62
62 63 return anoise;
63 64
64 65 def sorting_bruce(data, navg):
65 66
66 67 data = data.copy()
67 68
68 69 sortdata = numpy.sort(data)
69 70 lenOfData = len(data)
70 71 nums_min = lenOfData/10
71 72
72 73 if (lenOfData/10) > 0:
73 74 nums_min = lenOfData/10
74 75 else:
75 76 nums_min = 0
76 77
77 78 rtest = 1.0 + 1.0/navg
78 79
79 80 sum = 0.
80 81
81 82 sumq = 0.
82 83
83 84 j = 0
84 85
85 86 cont = 1
86 87
87 88 while((cont==1)and(j<lenOfData)):
88 89
89 90 sum += sortdata[j]
90 91
91 92 sumq += sortdata[j]**2
92 93
93 94 j += 1
94 95
95 96 if j > nums_min:
96 97 if ((sumq*j) <= (rtest*sum**2)):
97 98 lnoise = sum / j
98 99 else:
99 100 j = j - 1
100 101 sum = sum - sordata[j]
101 102 sumq = sumq - sordata[j]**2
102 103 cont = 0
103 104
104 105 if j == nums_min:
105 106 lnoise = sum /j
106 107
107 108 return lnoise
108 109
109 110 class JROData:
110 111
111 112 # m_BasicHeader = BasicHeader()
112 113 # m_ProcessingHeader = ProcessingHeader()
113 114
114 115 systemHeaderObj = SystemHeader()
115 116
116 117 radarControllerHeaderObj = RadarControllerHeader()
117 118
118 119 # data = None
119 120
120 121 type = None
121 122
122 123 dtype = None
123 124
124 125 # nChannels = None
125 126
126 127 # nHeights = None
127 128
128 129 nProfiles = None
129 130
130 131 heightList = None
131 132
132 133 channelList = None
133 134
134 135 flagNoData = True
135 136
136 137 flagTimeBlock = False
137 138
138 139 utctime = None
139 140
140 141 blocksize = None
141 142
142 143 nCode = None
143 144
144 145 nBaud = None
145 146
146 147 code = None
147 148
148 149 flagDecodeData = True #asumo q la data esta decodificada
149 150
150 151 flagDeflipData = True #asumo q la data esta sin flip
151 152
152 153 flagShiftFFT = False
153 154
154 155 ippSeconds = None
155 156
156 157 timeInterval = None
157 158
158 159 nCohInt = None
159 160
160 161 noise = None
161 162
162 163 #Speed of ligth
163 164 C = 3e8
164 165
165 166 frequency = 49.92e6
166 167
167 168 def __init__(self):
168 169
169 170 raise ValueError, "This class has not been implemented"
170 171
171 172 def copy(self, inputObj=None):
172 173
173 174 if inputObj == None:
174 175 return copy.deepcopy(self)
175 176
176 177 for key in inputObj.__dict__.keys():
177 178 self.__dict__[key] = inputObj.__dict__[key]
178 179
179 180 def deepcopy(self):
180 181
181 182 return copy.deepcopy(self)
182 183
183 184 def isEmpty(self):
184 185
185 186 return self.flagNoData
186 187
187 188 def getNoise(self):
188 189
189 190 raise ValueError, "Not implemented"
190 191
191 192 def getNChannels(self):
192 193
193 194 return len(self.channelList)
194 195
195 196 def getChannelIndexList(self):
196 197
197 198 return range(self.nChannels)
198 199
199 200 def getNHeights(self):
200 201
201 202 return len(self.heightList)
202 203
203 204 def getHeiRange(self, extrapoints=0):
204 205
205 206 heis = self.heightList
206 207 # deltah = self.heightList[1] - self.heightList[0]
207 208 #
208 209 # heis.append(self.heightList[-1])
209 210
210 211 return heis
211 212
212 213 def getDatatime(self):
213 214
215 datatime = datetime.datetime.utcfromtimestamp(self.utctime)
216 return datatime
217
218 def getTimeRange(self):
219
214 220 datatime = []
215 221
216 222 datatime.append(self.utctime)
217 223 datatime.append(self.utctime + self.timeInterval)
218 224
219 225 datatime = numpy.array(datatime)
220 226
221 227 return datatime
222 228
223 229 def getFmax(self):
224 230
225 231 PRF = 1./(self.ippSeconds * self.nCohInt)
226 232
227 233 fmax = PRF/2.
228 234
229 235 return fmax
230 236
231 237 def getVmax(self):
232 238
233 239 _lambda = self.C/self.frequency
234 240
235 241 vmax = self.getFmax() * _lambda / 2.
236 242
237 243 return vmax
238 244
239 245 nChannels = property(getNChannels, "I'm the 'nChannel' property.")
240 246 channelIndexList = property(getChannelIndexList, "I'm the 'channelIndexList' property.")
241
242 247 nHeights = property(getNHeights, "I'm the 'nHeights' property.")
243
244 248 noise = property(getNoise, "I'm the 'nHeights' property.")
249 datatime = property(getDatatime, "I'm the 'datatime' property")
245 250
246 251 class Voltage(JROData):
247 252
248 253 #data es un numpy array de 2 dmensiones (canales, alturas)
249 254 data = None
250 255
251 256 def __init__(self):
252 257 '''
253 258 Constructor
254 259 '''
255 260
256 261 self.radarControllerHeaderObj = RadarControllerHeader()
257 262
258 263 self.systemHeaderObj = SystemHeader()
259 264
260 265 self.type = "Voltage"
261 266
262 267 self.data = None
263 268
264 269 self.dtype = None
265 270
266 271 # self.nChannels = 0
267 272
268 273 # self.nHeights = 0
269 274
270 275 self.nProfiles = None
271 276
272 277 self.heightList = None
273 278
274 279 self.channelList = None
275 280
276 281 # self.channelIndexList = None
277 282
278 283 self.flagNoData = True
279 284
280 285 self.flagTimeBlock = False
281 286
282 287 self.utctime = None
283 288
284 289 self.nCohInt = None
285 290
286 291 self.blocksize = None
287 292
288 293 def getNoisebyHildebrand(self):
289 294 """
290 295 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
291 296
292 297 Return:
293 298 noiselevel
294 299 """
295 300
296 301 for channel in range(self.nChannels):
297 302 daux = self.data_spc[channel,:,:]
298 303 self.noise[channel] = hildebrand_sekhon(daux, self.nCohInt)
299 304
300 305 return self.noise
301 306
302 307 def getNoise(self, type = 1):
303 308
304 309 self.noise = numpy.zeros(self.nChannels)
305 310
306 311 if type == 1:
307 312 noise = self.getNoisebyHildebrand()
308 313
309 314 return 10*numpy.log10(noise)
310 315
311 316 class Spectra(JROData):
312 317
313 318 #data es un numpy array de 2 dmensiones (canales, perfiles, alturas)
314 319 data_spc = None
315 320
316 321 #data es un numpy array de 2 dmensiones (canales, pares, alturas)
317 322 data_cspc = None
318 323
319 324 #data es un numpy array de 2 dmensiones (canales, alturas)
320 325 data_dc = None
321 326
322 327 nFFTPoints = None
323 328
324 329 nPairs = None
325 330
326 331 pairsList = None
327 332
328 333 nIncohInt = None
329 334
330 335 wavelength = None #Necesario para cacular el rango de velocidad desde la frecuencia
331 336
332 337 nCohInt = None #se requiere para determinar el valor de timeInterval
333 338
334 339 def __init__(self):
335 340 '''
336 341 Constructor
337 342 '''
338 343
339 344 self.radarControllerHeaderObj = RadarControllerHeader()
340 345
341 346 self.systemHeaderObj = SystemHeader()
342 347
343 348 self.type = "Spectra"
344 349
345 350 # self.data = None
346 351
347 352 self.dtype = None
348 353
349 354 # self.nChannels = 0
350 355
351 356 # self.nHeights = 0
352 357
353 358 self.nProfiles = None
354 359
355 360 self.heightList = None
356 361
357 362 self.channelList = None
358 363
359 364 # self.channelIndexList = None
360 365
361 366 self.flagNoData = True
362 367
363 368 self.flagTimeBlock = False
364 369
365 370 self.utctime = None
366 371
367 372 self.nCohInt = None
368 373
369 374 self.nIncohInt = None
370 375
371 376 self.blocksize = None
372 377
373 378 self.nFFTPoints = None
374 379
375 380 self.wavelength = None
376 381
377 382 def getNoisebyHildebrand(self):
378 383 """
379 384 Determino el nivel de ruido usando el metodo Hildebrand-Sekhon
380 385
381 386 Return:
382 387 noiselevel
383 388 """
384 389
385 390 for channel in range(self.nChannels):
386 391 daux = self.data_spc[channel,:,:]
387 392 self.noise[channel] = hildebrand_sekhon(daux, self.nIncohInt)
388 393
389 394 return self.noise
390 395
391 396 def getNoisebyWindow(self, heiIndexMin=0, heiIndexMax=-1, freqIndexMin=0, freqIndexMax=-1):
392 397 """
393 398 Determina el ruido del canal utilizando la ventana indicada con las coordenadas:
394 399 (heiIndexMIn, freqIndexMin) hasta (heiIndexMax, freqIndexMAx)
395 400
396 401 Inputs:
397 402 heiIndexMin: Limite inferior del eje de alturas
398 403 heiIndexMax: Limite superior del eje de alturas
399 404 freqIndexMin: Limite inferior del eje de frecuencia
400 405 freqIndexMax: Limite supoerior del eje de frecuencia
401 406 """
402 407
403 408 data = self.data_spc[:, heiIndexMin:heiIndexMax, freqIndexMin:freqIndexMax]
404 409
405 410 for channel in range(self.nChannels):
406 411 daux = data[channel,:,:]
407 412 self.noise[channel] = numpy.average(daux)
408 413
409 414 return self.noise
410 415
411 416 def getNoisebySort(self):
412 417
413 418 for channel in range(self.nChannels):
414 419 daux = self.data_spc[channel,:,:]
415 420 self.noise[channel] = sorting_bruce(daux, self.nIncohInt)
416 421
417 422 return self.noise
418 423
419 424 def getNoise(self, type = 1):
420 425
421 426 self.noise = numpy.zeros(self.nChannels)
422 427
423 428 if type == 1:
424 429 noise = self.getNoisebyHildebrand()
425 430
426 431 if type == 2:
427 432 noise = self.getNoisebySort()
428 433
429 434 if type == 3:
430 435 noise = self.getNoisebyWindow()
431 436
432 437 return 10*numpy.log10(noise)
433 438
434 439
435 440 def getFreqRange(self, extrapoints=0):
436 441
437 442 delfreq = 2 * self.getFmax() / self.nFFTPoints
438 443 freqrange = deltafreqs*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltafreq/2
439 444
440 445 return freqrange
441 446
442 447 def getVelRange(self, extrapoints=0):
443 448
444 449 deltav = 2 * self.getVmax() / self.nFFTPoints
445 450 velrange = deltav*(numpy.arange(self.nFFTPoints+extrapoints)-self.nFFTPoints/2.) - deltav/2
446 451
447 452 return velrange
448 453
449 454 def getNPairs(self):
450 455
451 456 return len(self.pairsList)
452 457
453 458 def getPairsIndexList(self):
454 459
455 460 return range(self.nPairs)
456 461
457 462 nPairs = property(getNPairs, "I'm the 'nPairs' property.")
458 463 pairsIndexList = property(getPairsIndexList, "I'm the 'pairsIndexList' property.")
459 464
460 465 class SpectraHeis(JROData):
461 466
462 467 data_spc = None
463 468
464 469 data_cspc = None
465 470
466 471 data_dc = None
467 472
468 473 nFFTPoints = None
469 474
470 475 nPairs = None
471 476
472 477 pairsList = None
473 478
474 479 nIncohInt = None
475 480
476 481 def __init__(self):
477 482
478 483 self.radarControllerHeaderObj = RadarControllerHeader()
479 484
480 485 self.systemHeaderObj = SystemHeader()
481 486
482 487 self.type = "SpectraHeis"
483 488
484 489 self.dtype = None
485 490
486 491 # self.nChannels = 0
487 492
488 493 # self.nHeights = 0
489 494
490 495 self.nProfiles = None
491 496
492 497 self.heightList = None
493 498
494 499 self.channelList = None
495 500
496 501 # self.channelIndexList = None
497 502
498 503 self.flagNoData = True
499 504
500 505 self.flagTimeBlock = False
501 506
502 507 self.nPairs = 0
503 508
504 509 self.utctime = None
505 510
506 511 self.blocksize = None
@@ -1,2516 +1,2541
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JRODataIO.py 169 2012-11-19 21:57:03Z murco $
5 5 '''
6 6
7 7 import os, sys
8 8 import glob
9 9 import time
10 10 import numpy
11 11 import fnmatch
12 12 import time, datetime
13 13
14 14 from jrodata import *
15 15 from jroheaderIO import *
16 16 from jroprocessing import *
17 17
18 18 def isNumber(str):
19 19 """
20 20 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
21 21
22 22 Excepciones:
23 23 Si un determinado string no puede ser convertido a numero
24 24 Input:
25 25 str, string al cual se le analiza para determinar si convertible a un numero o no
26 26
27 27 Return:
28 28 True : si el string es uno numerico
29 29 False : no es un string numerico
30 30 """
31 31 try:
32 32 float( str )
33 33 return True
34 34 except:
35 35 return False
36 36
37 37 def isThisFileinRange(filename, startUTSeconds, endUTSeconds):
38 38 """
39 39 Esta funcion determina si un archivo de datos se encuentra o no dentro del rango de fecha especificado.
40 40
41 41 Inputs:
42 42 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
43 43
44 44 startUTSeconds : fecha inicial del rango seleccionado. La fecha esta dada en
45 45 segundos contados desde 01/01/1970.
46 46 endUTSeconds : fecha final del rango seleccionado. La fecha esta dada en
47 47 segundos contados desde 01/01/1970.
48 48
49 49 Return:
50 50 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
51 51 fecha especificado, de lo contrario retorna False.
52 52
53 53 Excepciones:
54 54 Si el archivo no existe o no puede ser abierto
55 55 Si la cabecera no puede ser leida.
56 56
57 57 """
58 58 basicHeaderObj = BasicHeader()
59 59
60 60 try:
61 61 fp = open(filename,'rb')
62 62 except:
63 63 raise IOError, "The file %s can't be opened" %(filename)
64 64
65 65 sts = basicHeaderObj.read(fp)
66 66 fp.close()
67 67
68 68 if not(sts):
69 69 print "Skipping the file %s because it has not a valid header" %(filename)
70 70 return 0
71 71
72 72 if not ((startUTSeconds <= basicHeaderObj.utc) and (endUTSeconds > basicHeaderObj.utc)):
73 73 return 0
74 74
75 75 return 1
76 76
77 def isFileinThisTime(filename, startTime, endTime):
78 """
79 Retorna 1 si el archivo de datos se encuentra dentro del rango de horas especificado.
80
81 Inputs:
82 filename : nombre completo del archivo de datos en formato Jicamarca (.r)
83
84 startTime : tiempo inicial del rango seleccionado en formato datetime.time
85
86 endTime : tiempo final del rango seleccionado en formato datetime.time
87
88 Return:
89 Boolean : Retorna True si el archivo de datos contiene datos en el rango de
90 fecha especificado, de lo contrario retorna False.
91
92 Excepciones:
93 Si el archivo no existe o no puede ser abierto
94 Si la cabecera no puede ser leida.
95
96 """
97
98
99 try:
100 fp = open(filename,'rb')
101 except:
102 raise IOError, "The file %s can't be opened" %(filename)
103
104 basicHeaderObj = BasicHeader()
105 sts = basicHeaderObj.read(fp)
106 fp.close()
107
108 thisTime = basicHeaderObj.datatime.time()
109
110 if not(sts):
111 print "Skipping the file %s because it has not a valid header" %(filename)
112 return 0
113
114 if not ((startTime <= thisTime) and (endTime > thisTime)):
115 return 0
116
117 return 1
118
77 119 def getlastFileFromPath(path, ext):
78 120 """
79 121 Depura el fileList dejando solo los que cumplan el formato de "PYYYYDDDSSS.ext"
80 122 al final de la depuracion devuelve el ultimo file de la lista que quedo.
81 123
82 124 Input:
83 125 fileList : lista conteniendo todos los files (sin path) que componen una determinada carpeta
84 126 ext : extension de los files contenidos en una carpeta
85 127
86 128 Return:
87 129 El ultimo file de una determinada carpeta, no se considera el path.
88 130 """
89 131 validFilelist = []
90 132 fileList = os.listdir(path)
91 133
92 134 # 0 1234 567 89A BCDE
93 135 # H YYYY DDD SSS .ext
94 136
95 137 for file in fileList:
96 138 try:
97 139 year = int(file[1:5])
98 140 doy = int(file[5:8])
99 141
100 if (os.path.splitext(file)[-1].upper() != ext.upper()) : continue
142
101 143 except:
102 144 continue
103
145
146 if (os.path.splitext(file)[-1].lower() != ext.lower()):
147 continue
148
104 149 validFilelist.append(file)
105 150
106 151 if validFilelist:
107 152 validFilelist = sorted( validFilelist, key=str.lower )
108 153 return validFilelist[-1]
109 154
110 155 return None
111 156
112 157 def checkForRealPath(path, year, doy, set, ext):
113 158 """
114 159 Por ser Linux Case Sensitive entonces checkForRealPath encuentra el nombre correcto de un path,
115 160 Prueba por varias combinaciones de nombres entre mayusculas y minusculas para determinar
116 161 el path exacto de un determinado file.
117 162
118 163 Example :
119 164 nombre correcto del file es .../.../D2009307/P2009307367.ext
120 165
121 166 Entonces la funcion prueba con las siguientes combinaciones
167 .../.../y2009307367.ext
168 .../.../Y2009307367.ext
122 169 .../.../x2009307/y2009307367.ext
123 170 .../.../x2009307/Y2009307367.ext
124 171 .../.../X2009307/y2009307367.ext
125 172 .../.../X2009307/Y2009307367.ext
126 173 siendo para este caso, la ultima combinacion de letras, identica al file buscado
127 174
128 175 Return:
129 176 Si encuentra la cobinacion adecuada devuelve el path completo y el nombre del file
130 177 caso contrario devuelve None como path y el la ultima combinacion de nombre en mayusculas
131 178 para el filename
132 179 """
133 filepath = None
180 fullfilename = None
134 181 find_flag = False
135 182 filename = None
136
183
184 prefixDirList = [None,'d','D']
137 185 if ext.lower() == ".r": #voltage
138 header1 = "dD"
139 header2 = "dD"
186 prefixFileList = ['d','D']
140 187 elif ext.lower() == ".pdata": #spectra
141 header1 = "dD"
142 header2 = "pP"
188 prefixFileList = ['p','P']
143 189 else:
144 190 return None, filename
191
192 #barrido por las combinaciones posibles
193 for prefixDir in prefixDirList:
194 thispath = path
195 if prefixDir != None:
196 #formo el nombre del directorio xYYYYDDD (x=d o x=D)
197 thispath = os.path.join(path, "%s%04d%03d" % ( prefixDir, year, doy ))
198
199 for prefixFile in prefixFileList: #barrido por las dos combinaciones posibles de "D"
200 filename = "%s%04d%03d%03d%s" % ( prefixFile, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
201 fullfilename = os.path.join( thispath, filename ) #formo el path completo
145 202
146 for dir in header1: #barrido por las dos combinaciones posibles de "D"
147 for fil in header2: #barrido por las dos combinaciones posibles de "D"
148 doypath = "%s%04d%03d" % ( dir, year, doy ) #formo el nombre del directorio xYYYYDDD (x=d o x=D)
149 filename = "%s%04d%03d%03d%s" % ( fil, year, doy, set, ext ) #formo el nombre del file xYYYYDDDSSS.ext
150 filepath = os.path.join( path, doypath, filename ) #formo el path completo
151 if os.path.exists( filepath ): #verifico que exista
203 if os.path.exists( fullfilename ): #verifico que exista
152 204 find_flag = True
153 205 break
154 206 if find_flag:
155 207 break
156 208
157 209 if not(find_flag):
158 210 return None, filename
159 211
160 return filepath, filename
212 return fullfilename, filename
161 213
162 214 class JRODataIO:
163 215
164 216 c = 3E8
165 217
166 218 isConfig = False
167 219
168 220 basicHeaderObj = BasicHeader()
169 221
170 222 systemHeaderObj = SystemHeader()
171 223
172 224 radarControllerHeaderObj = RadarControllerHeader()
173 225
174 226 processingHeaderObj = ProcessingHeader()
175 227
176 228 online = 0
177 229
178 230 dtype = None
179 231
180 232 pathList = []
181 233
182 234 filenameList = []
183 235
184 236 filename = None
185 237
186 238 ext = None
187 239
188 240 flagIsNewFile = 1
189 241
190 242 flagTimeBlock = 0
191 243
192 244 flagIsNewBlock = 0
193 245
194 246 fp = None
195 247
196 248 firstHeaderSize = 0
197 249
198 250 basicHeaderSize = 24
199 251
200 252 versionFile = 1103
201 253
202 254 fileSize = None
203 255
204 256 ippSeconds = None
205 257
206 258 fileSizeByHeader = None
207 259
208 260 fileIndex = None
209 261
210 262 profileIndex = None
211 263
212 264 blockIndex = None
213 265
214 266 nTotalBlocks = None
215 267
216 268 maxTimeStep = 30
217 269
218 270 lastUTTime = None
219 271
220 272 datablock = None
221 273
222 274 dataOut = None
223 275
224 276 blocksize = None
225 277
226 278 def __init__(self):
227 279
228 280 raise ValueError, "Not implemented"
229 281
230 282 def run(self):
231 283
232 284 raise ValueError, "Not implemented"
233 285
234 286 def getOutput(self):
235 287
236 288 return self.dataOut
237 289
238 290 class JRODataReader(JRODataIO, ProcessingUnit):
239 291
240 292 nReadBlocks = 0
241 293
242 294 delay = 10 #number of seconds waiting a new file
243 295
244 296 nTries = 3 #quantity tries
245 297
246 298 nFiles = 3 #number of files for searching
247 299
248 300 flagNoMoreFiles = 0
249 301
250 302 def __init__(self):
251 303
252 304 """
253 305
254 306 """
255 307
256 308 raise ValueError, "This method has not been implemented"
257 309
258 310
259 311 def createObjByDefault(self):
260 312 """
261 313
262 314 """
263 315 raise ValueError, "This method has not been implemented"
264 316
265 317 def getBlockDimension(self):
266 318
267 319 raise ValueError, "No implemented"
268 320
269 321 def __searchFilesOffLine(self,
270 322 path,
271 323 startDate,
272 324 endDate,
273 325 startTime=datetime.time(0,0,0),
274 326 endTime=datetime.time(23,59,59),
275 327 set=None,
276 expLabel="",
277 ext=".r"):
278 dirList = []
279 for thisPath in os.listdir(path):
280 if os.path.isdir(os.path.join(path,thisPath)):
281 dirList.append(thisPath)
282
283 if not(dirList):
284 return None, None
285
286 pathList = []
287 dateList = []
328 expLabel='',
329 ext='.r',
330 walk=True):
288 331
289 thisDate = startDate
332 pathList = []
290 333
291 while(thisDate <= endDate):
292 year = thisDate.timetuple().tm_year
293 doy = thisDate.timetuple().tm_yday
334 if not walk:
335 pathList.append(path)
294 336
295 match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy))
296 if len(match) == 0:
297 thisDate += datetime.timedelta(1)
298 continue
337 else:
338 dirList = []
339 for thisPath in os.listdir(path):
340 if os.path.isdir(os.path.join(path,thisPath)):
341 dirList.append(thisPath)
342
343 if not(dirList):
344 return None, None
345
346 thisDate = startDate
299 347
300 pathList.append(os.path.join(path,match[0],expLabel))
301 dateList.append(thisDate)
302 thisDate += datetime.timedelta(1)
348 while(thisDate <= endDate):
349 year = thisDate.timetuple().tm_year
350 doy = thisDate.timetuple().tm_yday
351
352 match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy))
353 if len(match) == 0:
354 thisDate += datetime.timedelta(1)
355 continue
356
357 pathList.append(os.path.join(path,match[0],expLabel))
358 thisDate += datetime.timedelta(1)
359
303 360
304 361 filenameList = []
305 for index in range(len(pathList)):
362 for thisPath in pathList:
306 363
307 thisPath = pathList[index]
308 364 fileList = glob.glob1(thisPath, "*%s" %ext)
309 365 fileList.sort()
310 366
311 #Busqueda de datos en el rango de horas indicados
312 thisDate = dateList[index]
313 startDT = datetime.datetime.combine(thisDate, startTime)
314 endDT = datetime.datetime.combine(thisDate, endTime)
315
316 startUtSeconds = time.mktime(startDT.timetuple())
317 endUtSeconds = time.mktime(endDT.timetuple())
318
319 367 for file in fileList:
320 368
321 369 filename = os.path.join(thisPath,file)
322 370
323 if isThisFileinRange(filename, startUtSeconds, endUtSeconds):
371 if isFileinThisTime(filename, startTime, endTime):
324 372 filenameList.append(filename)
325 373
326 374 if not(filenameList):
327 375 return None, None
328 376
329 377 self.filenameList = filenameList
330 378
331 379 return pathList, filenameList
332 380
333 def __searchFilesOnLine(self, path, startDate=None, endDate=None, startTime=None, endTime=None, expLabel = "", ext = None):
381 def __searchFilesOnLine(self, path, expLabel = "", ext = None, walk=True):
334 382
335 383 """
336 384 Busca el ultimo archivo de la ultima carpeta (determinada o no por startDateTime) y
337 385 devuelve el archivo encontrado ademas de otros datos.
338 386
339 387 Input:
340 388 path : carpeta donde estan contenidos los files que contiene data
341 389
342 startDate : Fecha inicial. Rechaza todos los directorios donde
343 file end time < startDate (obejto datetime.date)
344
345 endDate : Fecha final. Rechaza todos los directorios donde
346 file start time > endDate (obejto datetime.date)
347
348 startTime : Tiempo inicial. Rechaza todos los archivos donde
349 file end time < startTime (obejto datetime.time)
350
351 endTime : Tiempo final. Rechaza todos los archivos donde
352 file start time > endTime (obejto datetime.time)
353
354 390 expLabel : Nombre del subexperimento (subfolder)
355 391
356 ext : extension de los files
392 ext : extension de los files
393
394 walk : Si es habilitado no realiza busquedas dentro de los ubdirectorios (doypath)
357 395
358 396 Return:
359 397 directory : eL directorio donde esta el file encontrado
360 398 filename : el ultimo file de una determinada carpeta
361 399 year : el anho
362 400 doy : el numero de dia del anho
363 401 set : el set del archivo
364 402
365 403
366 404 """
367 405 dirList = []
368 pathList = []
369 directory = None
370 406
371 #Filtra solo los directorios
372 for thisPath in os.listdir(path):
373 if os.path.isdir(os.path.join(path, thisPath)):
374 dirList.append(thisPath)
375
376 if not(dirList):
377 return None, None, None, None, None
378
379 dirList = sorted( dirList, key=str.lower )
380
381 if startDate:
382 startDateTime = datetime.datetime.combine(startDate, startTime)
383 thisDateTime = startDateTime
384 if endDate == None: endDateTime = startDateTime
385 else: endDateTime = datetime.datetime.combine(endDate, endTime)
407 if walk:
386 408
387 while(thisDateTime <= endDateTime):
388 year = thisDateTime.timetuple().tm_year
389 doy = thisDateTime.timetuple().tm_yday
390
391 match = fnmatch.filter(dirList, '?' + '%4.4d%3.3d' % (year,doy))
392 if len(match) == 0:
393 thisDateTime += datetime.timedelta(1)
394 continue
395
396 pathList.append(os.path.join(path,match[0], expLabel))
397 thisDateTime += datetime.timedelta(1)
398
399 if not(pathList):
400 print "\tNo files in range: %s - %s" %(startDateTime.ctime(), endDateTime.ctime())
409 #Filtra solo los directorios
410 for thisPath in os.listdir(path):
411 if os.path.isdir(os.path.join(path, thisPath)):
412 dirList.append(thisPath)
413
414 if not(dirList):
401 415 return None, None, None, None, None
402
403 directory = pathList[0]
404
416
417 dirList = sorted( dirList, key=str.lower )
418
419 doypath = dirList[-1]
420 fullpath = os.path.join(path, doypath, expLabel)
421
405 422 else:
406 directory = dirList[-1]
407 directory = os.path.join(path,directory)
423 fullpath = path
408 424
409 filename = getlastFileFromPath(directory, ext)
425 filename = getlastFileFromPath(fullpath, ext)
410 426
411 427 if not(filename):
412 428 return None, None, None, None, None
413 429
414 if not(self.__verifyFile(os.path.join(directory, filename))):
430 if not(self.__verifyFile(os.path.join(fullpath, filename))):
415 431 return None, None, None, None, None
416 432
417 433 year = int( filename[1:5] )
418 434 doy = int( filename[5:8] )
419 435 set = int( filename[8:11] )
420 436
421 return directory, filename, year, doy, set
437 return fullpath, filename, year, doy, set
422 438
423 439
424 440
425 441 def __setNextFileOffline(self):
426 442
427 443 idFile = self.fileIndex
428 444
429 445 while (True):
430 446 idFile += 1
431 447 if not(idFile < len(self.filenameList)):
432 448 self.flagNoMoreFiles = 1
433 449 print "No more Files"
434 450 return 0
435 451
436 452 filename = self.filenameList[idFile]
437 453
438 454 if not(self.__verifyFile(filename)):
439 455 continue
440 456
441 457 fileSize = os.path.getsize(filename)
442 458 fp = open(filename,'rb')
443 459 break
444 460
445 461 self.flagIsNewFile = 1
446 462 self.fileIndex = idFile
447 463 self.filename = filename
448 464 self.fileSize = fileSize
449 465 self.fp = fp
450 466
451 467 print "Setting the file: %s"%self.filename
452 468
453 469 return 1
454 470
455 471 def __setNextFileOnline(self):
456 472 """
457 473 Busca el siguiente file que tenga suficiente data para ser leida, dentro de un folder especifico, si
458 474 no encuentra un file valido espera un tiempo determinado y luego busca en los posibles n files
459 475 siguientes.
460 476
461 477 Affected:
462 478 self.flagIsNewFile
463 479 self.filename
464 480 self.fileSize
465 481 self.fp
466 482 self.set
467 483 self.flagNoMoreFiles
468 484
469 485 Return:
470 486 0 : si luego de una busqueda del siguiente file valido este no pudo ser encontrado
471 487 1 : si el file fue abierto con exito y esta listo a ser leido
472 488
473 489 Excepciones:
474 490 Si un determinado file no puede ser abierto
475 491 """
476 492 nFiles = 0
477 493 fileOk_flag = False
478 494 firstTime_flag = True
479 495
480 496 self.set += 1
481 497
482 498 #busca el 1er file disponible
483 file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
484 if file:
485 if self.__verifyFile(file, False):
499 fullfilename, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
500 if fullfilename:
501 if self.__verifyFile(fullfilename, False):
486 502 fileOk_flag = True
487 503
488 504 #si no encuentra un file entonces espera y vuelve a buscar
489 505 if not(fileOk_flag):
490 506 for nFiles in range(self.nFiles+1): #busco en los siguientes self.nFiles+1 files posibles
491 507
492 508 if firstTime_flag: #si es la 1era vez entonces hace el for self.nTries veces
493 509 tries = self.nTries
494 510 else:
495 511 tries = 1 #si no es la 1era vez entonces solo lo hace una vez
496 512
497 513 for nTries in range( tries ):
498 514 if firstTime_flag:
499 515 print "\tWaiting %0.2f sec for the file \"%s\" , try %03d ..." % ( self.delay, filename, nTries+1 )
500 516 time.sleep( self.delay )
501 517 else:
502 518 print "\tSearching next \"%s%04d%03d%03d%s\" file ..." % (self.optchar, self.year, self.doy, self.set, self.ext)
503 519
504 file, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
505 if file:
506 if self.__verifyFile(file):
520 fullfilename, filename = checkForRealPath( self.path, self.year, self.doy, self.set, self.ext )
521 if fullfilename:
522 if self.__verifyFile(fullfilename):
507 523 fileOk_flag = True
508 524 break
509 525
510 526 if fileOk_flag:
511 527 break
512 528
513 529 firstTime_flag = False
514 530
515 531 print "\tSkipping the file \"%s\" due to this file doesn't exist" % filename
516 532 self.set += 1
517 533
518 534 if nFiles == (self.nFiles-1): #si no encuentro el file buscado cambio de carpeta y busco en la siguiente carpeta
519 535 self.set = 0
520 536 self.doy += 1
521 537
522 538 if fileOk_flag:
523 self.fileSize = os.path.getsize( file )
524 self.filename = file
539 self.fileSize = os.path.getsize( fullfilename )
540 self.filename = fullfilename
525 541 self.flagIsNewFile = 1
526 542 if self.fp != None: self.fp.close()
527 self.fp = open(file, 'rb')
543 self.fp = open(fullfilename, 'rb')
528 544 self.flagNoMoreFiles = 0
529 print 'Setting the file: %s' % file
545 print 'Setting the file: %s' % fullfilename
530 546 else:
531 547 self.fileSize = 0
532 548 self.filename = None
533 549 self.flagIsNewFile = 0
534 550 self.fp = None
535 551 self.flagNoMoreFiles = 1
536 552 print 'No more Files'
537 553
538 554 return fileOk_flag
539 555
540 556
541 557 def setNextFile(self):
542 558 if self.fp != None:
543 559 self.fp.close()
544 560
545 561 if self.online:
546 562 newFile = self.__setNextFileOnline()
547 563 else:
548 564 newFile = self.__setNextFileOffline()
549 565
550 566 if not(newFile):
551 567 return 0
552 568
553 569 self.__readFirstHeader()
554 570 self.nReadBlocks = 0
555 571 return 1
556 572
557 573 def __waitNewBlock(self):
558 #si es OnLine y ademas aun no se han leido un bloque completo entonces se espera por uno valido
574 """
575 Return 1 si se encontro un nuevo bloque de datos, 0 de otra forma.
576
577 Si el modo de lectura es OffLine siempre retorn 0
578 """
559 579 if not self.online:
560 580 return 0
561 581
562 582 if (self.nReadBlocks >= self.processingHeaderObj.dataBlocksPerFile):
563 583 return 0
564 584
565 585 currentPointer = self.fp.tell()
566 586
567 587 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
568 588
569 589 for nTries in range( self.nTries ):
570 590
571 591 self.fp.close()
572 592 self.fp = open( self.filename, 'rb' )
573 593 self.fp.seek( currentPointer )
574 594
575 595 self.fileSize = os.path.getsize( self.filename )
576 596 currentSize = self.fileSize - currentPointer
577 597
578 598 if ( currentSize >= neededSize ):
579 599 self.__rdBasicHeader()
580 600 return 1
581 601
582 602 print "\tWaiting %0.2f seconds for the next block, try %03d ..." % (self.delay, nTries+1)
583 603 time.sleep( self.delay )
584 604
585 605
586 606 return 0
587 607
588 608 def __setNewBlock(self):
609
589 610 if self.fp == None:
590 611 return 0
591 612
592 613 if self.flagIsNewFile:
593 614 return 1
594 615
595 616 self.lastUTTime = self.basicHeaderObj.utc
596 617 currentSize = self.fileSize - self.fp.tell()
597 618 neededSize = self.processingHeaderObj.blockSize + self.basicHeaderSize
598 619
599 620 if (currentSize >= neededSize):
600 621 self.__rdBasicHeader()
601 622 return 1
602 623
603 624 if self.__waitNewBlock():
604 625 return 1
605 626
606 627 if not(self.setNextFile()):
607 628 return 0
608 629
609 630 deltaTime = self.basicHeaderObj.utc - self.lastUTTime #
610 631
611 632 self.flagTimeBlock = 0
612 633
613 634 if deltaTime > self.maxTimeStep:
614 635 self.flagTimeBlock = 1
615 636
616 637 return 1
617 638
618 639
619 640 def readNextBlock(self):
620 641 if not(self.__setNewBlock()):
621 642 return 0
622 643
623 644 if not(self.readBlock()):
624 645 return 0
625 646
626 647 return 1
627 648
628 649 def __rdProcessingHeader(self, fp=None):
629 650 if fp == None:
630 651 fp = self.fp
631 652
632 653 self.processingHeaderObj.read(fp)
633 654
634 655 def __rdRadarControllerHeader(self, fp=None):
635 656 if fp == None:
636 657 fp = self.fp
637 658
638 659 self.radarControllerHeaderObj.read(fp)
639 660
640 661 def __rdSystemHeader(self, fp=None):
641 662 if fp == None:
642 663 fp = self.fp
643 664
644 665 self.systemHeaderObj.read(fp)
645 666
646 667 def __rdBasicHeader(self, fp=None):
647 668 if fp == None:
648 669 fp = self.fp
649 670
650 671 self.basicHeaderObj.read(fp)
651 672
652 673
653 674 def __readFirstHeader(self):
654 675 self.__rdBasicHeader()
655 676 self.__rdSystemHeader()
656 677 self.__rdRadarControllerHeader()
657 678 self.__rdProcessingHeader()
658 679
659 680 self.firstHeaderSize = self.basicHeaderObj.size
660 681
661 682 datatype = int(numpy.log2((self.processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
662 683 if datatype == 0:
663 684 datatype_str = numpy.dtype([('real','<i1'),('imag','<i1')])
664 685 elif datatype == 1:
665 686 datatype_str = numpy.dtype([('real','<i2'),('imag','<i2')])
666 687 elif datatype == 2:
667 688 datatype_str = numpy.dtype([('real','<i4'),('imag','<i4')])
668 689 elif datatype == 3:
669 690 datatype_str = numpy.dtype([('real','<i8'),('imag','<i8')])
670 691 elif datatype == 4:
671 692 datatype_str = numpy.dtype([('real','<f4'),('imag','<f4')])
672 693 elif datatype == 5:
673 694 datatype_str = numpy.dtype([('real','<f8'),('imag','<f8')])
674 695 else:
675 696 raise ValueError, 'Data type was not defined'
676 697
677 698 self.dtype = datatype_str
678 699 self.ippSeconds = 2 * 1000 * self.radarControllerHeaderObj.ipp / self.c
679 700 self.fileSizeByHeader = self.processingHeaderObj.dataBlocksPerFile * self.processingHeaderObj.blockSize + self.firstHeaderSize + self.basicHeaderSize*(self.processingHeaderObj.dataBlocksPerFile - 1)
680 701 # self.dataOut.channelList = numpy.arange(self.systemHeaderObj.numChannels)
681 702 # self.dataOut.channelIndexList = numpy.arange(self.systemHeaderObj.numChannels)
682 703 self.getBlockDimension()
683 704
684 705
685 706 def __verifyFile(self, filename, msgFlag=True):
686 707 msg = None
687 708 try:
688 709 fp = open(filename, 'rb')
689 710 currentPosition = fp.tell()
690 711 except:
691 712 if msgFlag:
692 713 print "The file %s can't be opened" % (filename)
693 714 return False
694 715
695 716 neededSize = self.processingHeaderObj.blockSize + self.firstHeaderSize
696 717
697 718 if neededSize == 0:
698 719 basicHeaderObj = BasicHeader()
699 720 systemHeaderObj = SystemHeader()
700 721 radarControllerHeaderObj = RadarControllerHeader()
701 722 processingHeaderObj = ProcessingHeader()
702 723
703 724 try:
704 725 if not( basicHeaderObj.read(fp) ): raise IOError
705 726 if not( systemHeaderObj.read(fp) ): raise IOError
706 727 if not( radarControllerHeaderObj.read(fp) ): raise IOError
707 728 if not( processingHeaderObj.read(fp) ): raise IOError
708 729 data_type = int(numpy.log2((processingHeaderObj.processFlags & PROCFLAG.DATATYPE_MASK))-numpy.log2(PROCFLAG.DATATYPE_CHAR))
709 730
710 731 neededSize = processingHeaderObj.blockSize + basicHeaderObj.size
711 732
712 733 except:
713 734 if msgFlag:
714 735 print "\tThe file %s is empty or it hasn't enough data" % filename
715 736
716 737 fp.close()
717 738 return False
718 739 else:
719 740 msg = "\tSkipping the file %s due to it hasn't enough data" %filename
720 741
721 742 fp.close()
722 743 fileSize = os.path.getsize(filename)
723 744 currentSize = fileSize - currentPosition
724 745 if currentSize < neededSize:
725 746 if msgFlag and (msg != None):
726 747 print msg #print"\tSkipping the file %s due to it hasn't enough data" %filename
727 748 return False
728 749
729 750 return True
730 751
731 752 def setup(self,
732 753 path=None,
733 754 startDate=None,
734 755 endDate=None,
735 756 startTime=datetime.time(0,0,0),
736 757 endTime=datetime.time(23,59,59),
737 758 set=0,
738 759 expLabel = "",
739 760 ext = None,
740 761 online = False,
741 delay = 60):
762 delay = 60,
763 walk = True):
742 764
743 765 if path == None:
744 766 raise ValueError, "The path is not valid"
745 767
746 768 if ext == None:
747 769 ext = self.ext
748 770
749 771 if online:
750 772 print "Searching files in online mode..."
751 doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext)
752
753 if not(doypath):
754 for nTries in range( self.nTries ):
755 print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
756 time.sleep( self.delay )
757 doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext)
758 if doypath:
759 break
773
774 for nTries in range( self.nTries ):
775 fullpath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext, walk=walk)
776
777 if fullpath:
778 break
779
780 print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1)
781 time.sleep( self.delay )
760 782
761 if not(doypath):
783 if not(fullpath):
762 784 print "There 'isn't valied files in %s" % path
763 785 return None
764 786
765 787 self.year = year
766 788 self.doy = doy
767 789 self.set = set - 1
768 790 self.path = path
769 791
770 792 else:
771 793 print "Searching files in offline mode ..."
772 pathList, filenameList = self.__searchFilesOffLine(path, startDate, endDate, startTime, endTime, set, expLabel, ext)
794 pathList, filenameList = self.__searchFilesOffLine(path, startDate=startDate, endDate=endDate,
795 startTime=startTime, endTime=endTime,
796 set=set, expLabel=expLabel, ext=ext,
797 walk=walk)
773 798
774 799 if not(pathList):
775 800 print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path,
776 801 datetime.datetime.combine(startDate,startTime).ctime(),
777 802 datetime.datetime.combine(endDate,endTime).ctime())
778 803
779 804 sys.exit(-1)
780 805
781 806
782 807 self.fileIndex = -1
783 808 self.pathList = pathList
784 809 self.filenameList = filenameList
785 810
786 811 self.online = online
787 812 self.delay = delay
788 813 ext = ext.lower()
789 814 self.ext = ext
790 815
791 816 if not(self.setNextFile()):
792 817 if (startDate!=None) and (endDate!=None):
793 818 print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime())
794 819 elif startDate != None:
795 820 print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime())
796 821 else:
797 822 print "No files"
798 823
799 824 sys.exit(-1)
800 825
801 826 # self.updateDataHeader()
802 827
803 828 return self.dataOut
804 829
805 830 def getData():
806 831
807 832 raise ValueError, "This method has not been implemented"
808 833
809 834 def hasNotDataInBuffer():
810 835
811 836 raise ValueError, "This method has not been implemented"
812 837
813 838 def readBlock():
814 839
815 840 raise ValueError, "This method has not been implemented"
816 841
817 842 def isEndProcess(self):
818 843
819 844 return self.flagNoMoreFiles
820 845
821 846 def printReadBlocks(self):
822 847
823 848 print "Number of read blocks per file %04d" %self.nReadBlocks
824 849
825 850 def printTotalBlocks(self):
826 851
827 852 print "Number of read blocks %04d" %self.nTotalBlocks
828 853
829 854 def printInfo(self):
830 855
831 856 print self.basicHeaderObj.printInfo()
832 857 print self.systemHeaderObj.printInfo()
833 858 print self.radarControllerHeaderObj.printInfo()
834 859 print self.processingHeaderObj.printInfo()
835 860
836 861
837 862 def run(self, **kwargs):
838 863
839 864 if not(self.isConfig):
840 865
841 866 # self.dataOut = dataOut
842 867 self.setup(**kwargs)
843 868 self.isConfig = True
844 869
845 870 self.getData()
846 871
847 872 class JRODataWriter(JRODataIO, Operation):
848 873
849 874 """
850 875 Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura
851 876 de los datos siempre se realiza por bloques.
852 877 """
853 878
854 879 blockIndex = 0
855 880
856 881 path = None
857 882
858 883 setFile = None
859 884
860 885 profilesPerBlock = None
861 886
862 887 blocksPerFile = None
863 888
864 889 nWriteBlocks = 0
865 890
866 891 def __init__(self, dataOut=None):
867 892 raise ValueError, "Not implemented"
868 893
869 894
870 895 def hasAllDataInBuffer(self):
871 896 raise ValueError, "Not implemented"
872 897
873 898
874 899 def setBlockDimension(self):
875 900 raise ValueError, "Not implemented"
876 901
877 902
878 903 def writeBlock(self):
879 904 raise ValueError, "No implemented"
880 905
881 906
882 907 def putData(self):
883 908 raise ValueError, "No implemented"
884 909
885 910 def getDataHeader(self):
886 911 """
887 912 Obtiene una copia del First Header
888 913
889 914 Affected:
890 915
891 916 self.basicHeaderObj
892 917 self.systemHeaderObj
893 918 self.radarControllerHeaderObj
894 919 self.processingHeaderObj self.
895 920
896 921 Return:
897 922 None
898 923 """
899 924
900 925 raise ValueError, "No implemented"
901 926
902 927 def getBasicHeader(self):
903 928
904 929 self.basicHeaderObj.size = self.basicHeaderSize #bytes
905 930 self.basicHeaderObj.version = self.versionFile
906 931 self.basicHeaderObj.dataBlock = self.nTotalBlocks
907 932
908 933 utc = numpy.floor(self.dataOut.utctime)
909 934 milisecond = (self.dataOut.utctime - utc)* 1000.0
910 935
911 936 self.basicHeaderObj.utc = utc
912 937 self.basicHeaderObj.miliSecond = milisecond
913 938 self.basicHeaderObj.timeZone = 0
914 939 self.basicHeaderObj.dstFlag = 0
915 940 self.basicHeaderObj.errorCount = 0
916 941
917 942 def __writeFirstHeader(self):
918 943 """
919 944 Escribe el primer header del file es decir el Basic header y el Long header (SystemHeader, RadarControllerHeader, ProcessingHeader)
920 945
921 946 Affected:
922 947 __dataType
923 948
924 949 Return:
925 950 None
926 951 """
927 952
928 953 # CALCULAR PARAMETROS
929 954
930 955 sizeLongHeader = self.systemHeaderObj.size + self.radarControllerHeaderObj.size + self.processingHeaderObj.size
931 956 self.basicHeaderObj.size = self.basicHeaderSize + sizeLongHeader
932 957
933 958 self.basicHeaderObj.write(self.fp)
934 959 self.systemHeaderObj.write(self.fp)
935 960 self.radarControllerHeaderObj.write(self.fp)
936 961 self.processingHeaderObj.write(self.fp)
937 962
938 963 self.dtype = self.dataOut.dtype
939 964
940 965 def __setNewBlock(self):
941 966 """
942 967 Si es un nuevo file escribe el First Header caso contrario escribe solo el Basic Header
943 968
944 969 Return:
945 970 0 : si no pudo escribir nada
946 971 1 : Si escribio el Basic el First Header
947 972 """
948 973 if self.fp == None:
949 974 self.setNextFile()
950 975
951 976 if self.flagIsNewFile:
952 977 return 1
953 978
954 979 if self.blockIndex < self.processingHeaderObj.dataBlocksPerFile:
955 980 self.basicHeaderObj.write(self.fp)
956 981 return 1
957 982
958 983 if not( self.setNextFile() ):
959 984 return 0
960 985
961 986 return 1
962 987
963 988
964 989 def writeNextBlock(self):
965 990 """
966 991 Selecciona el bloque siguiente de datos y los escribe en un file
967 992
968 993 Return:
969 994 0 : Si no hizo pudo escribir el bloque de datos
970 995 1 : Si no pudo escribir el bloque de datos
971 996 """
972 997 if not( self.__setNewBlock() ):
973 998 return 0
974 999
975 1000 self.writeBlock()
976 1001
977 1002 return 1
978 1003
979 1004 def setNextFile(self):
980 1005 """
981 1006 Determina el siguiente file que sera escrito
982 1007
983 1008 Affected:
984 1009 self.filename
985 1010 self.subfolder
986 1011 self.fp
987 1012 self.setFile
988 1013 self.flagIsNewFile
989 1014
990 1015 Return:
991 1016 0 : Si el archivo no puede ser escrito
992 1017 1 : Si el archivo esta listo para ser escrito
993 1018 """
994 1019 ext = self.ext
995 1020 path = self.path
996 1021
997 1022 if self.fp != None:
998 1023 self.fp.close()
999 1024
1000 1025 timeTuple = time.localtime( self.dataOut.dataUtcTime)
1001 1026 subfolder = 'D%4.4d%3.3d' % (timeTuple.tm_year,timeTuple.tm_yday)
1002 1027
1003 doypath = os.path.join( path, subfolder )
1004 if not( os.path.exists(doypath) ):
1005 os.mkdir(doypath)
1028 fullpath = os.path.join( path, subfolder )
1029 if not( os.path.exists(fullpath) ):
1030 os.mkdir(fullpath)
1006 1031 self.setFile = -1 #inicializo mi contador de seteo
1007 1032 else:
1008 filesList = os.listdir( doypath )
1033 filesList = os.listdir( fullpath )
1009 1034 if len( filesList ) > 0:
1010 1035 filesList = sorted( filesList, key=str.lower )
1011 1036 filen = filesList[-1]
1012 1037 # el filename debera tener el siguiente formato
1013 1038 # 0 1234 567 89A BCDE (hex)
1014 1039 # x YYYY DDD SSS .ext
1015 1040 if isNumber( filen[8:11] ):
1016 1041 self.setFile = int( filen[8:11] ) #inicializo mi contador de seteo al seteo del ultimo file
1017 1042 else:
1018 1043 self.setFile = -1
1019 1044 else:
1020 1045 self.setFile = -1 #inicializo mi contador de seteo
1021 1046
1022 1047 setFile = self.setFile
1023 1048 setFile += 1
1024 1049
1025 1050 file = '%s%4.4d%3.3d%3.3d%s' % (self.optchar,
1026 1051 timeTuple.tm_year,
1027 1052 timeTuple.tm_yday,
1028 1053 setFile,
1029 1054 ext )
1030 1055
1031 1056 filename = os.path.join( path, subfolder, file )
1032 1057
1033 1058 fp = open( filename,'wb' )
1034 1059
1035 1060 self.blockIndex = 0
1036 1061
1037 1062 #guardando atributos
1038 1063 self.filename = filename
1039 1064 self.subfolder = subfolder
1040 1065 self.fp = fp
1041 1066 self.setFile = setFile
1042 1067 self.flagIsNewFile = 1
1043 1068
1044 1069 self.getDataHeader()
1045 1070
1046 1071 print 'Writing the file: %s'%self.filename
1047 1072
1048 1073 self.__writeFirstHeader()
1049 1074
1050 1075 return 1
1051 1076
1052 1077 def setup(self, dataOut, path, blocksPerFile, profilesPerBlock=None, set=0, ext=None):
1053 1078 """
1054 1079 Setea el tipo de formato en la cual sera guardada la data y escribe el First Header
1055 1080
1056 1081 Inputs:
1057 1082 path : el path destino en el cual se escribiran los files a crear
1058 1083 format : formato en el cual sera salvado un file
1059 1084 set : el setebo del file
1060 1085
1061 1086 Return:
1062 1087 0 : Si no realizo un buen seteo
1063 1088 1 : Si realizo un buen seteo
1064 1089 """
1065 1090
1066 1091 if ext == None:
1067 1092 ext = self.ext
1068 1093
1069 1094 ext = ext.lower()
1070 1095
1071 1096 self.ext = ext
1072 1097
1073 1098 self.path = path
1074 1099
1075 1100 self.setFile = set - 1
1076 1101
1077 1102 self.blocksPerFile = blocksPerFile
1078 1103
1079 1104 self.profilesPerBlock = profilesPerBlock
1080 1105
1081 1106 self.dataOut = dataOut
1082 1107
1083 1108 if not(self.setNextFile()):
1084 1109 print "There isn't a next file"
1085 1110 return 0
1086 1111
1087 1112 self.setBlockDimension()
1088 1113
1089 1114 return 1
1090 1115
1091 1116 def run(self, dataOut, **kwargs):
1092 1117
1093 1118 if not(self.isConfig):
1094 1119
1095 1120 self.setup(dataOut, **kwargs)
1096 1121 self.isConfig = True
1097 1122
1098 1123 self.putData()
1099 1124
1100 1125 class VoltageReader(JRODataReader):
1101 1126 """
1102 1127 Esta clase permite leer datos de voltage desde archivos en formato rawdata (.r). La lectura
1103 1128 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones:
1104 1129 perfiles*alturas*canales) son almacenados en la variable "buffer".
1105 1130
1106 1131 perfiles * alturas * canales
1107 1132
1108 1133 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1109 1134 RadarControllerHeader y Voltage. Los tres primeros se usan para almacenar informacion de la
1110 1135 cabecera de datos (metadata), y el cuarto (Voltage) para obtener y almacenar un perfil de
1111 1136 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1112 1137
1113 1138 Example:
1114 1139
1115 1140 dpath = "/home/myuser/data"
1116 1141
1117 1142 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1118 1143
1119 1144 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1120 1145
1121 1146 readerObj = VoltageReader()
1122 1147
1123 1148 readerObj.setup(dpath, startTime, endTime)
1124 1149
1125 1150 while(True):
1126 1151
1127 1152 #to get one profile
1128 1153 profile = readerObj.getData()
1129 1154
1130 1155 #print the profile
1131 1156 print profile
1132 1157
1133 1158 #If you want to see all datablock
1134 1159 print readerObj.datablock
1135 1160
1136 1161 if readerObj.flagNoMoreFiles:
1137 1162 break
1138 1163
1139 1164 """
1140 1165
1141 1166 ext = ".r"
1142 1167
1143 1168 optchar = "D"
1144 1169 dataOut = None
1145 1170
1146 1171
1147 1172 def __init__(self):
1148 1173 """
1149 1174 Inicializador de la clase VoltageReader para la lectura de datos de voltage.
1150 1175
1151 1176 Input:
1152 1177 dataOut : Objeto de la clase Voltage. Este objeto sera utilizado para
1153 1178 almacenar un perfil de datos cada vez que se haga un requerimiento
1154 1179 (getData). El perfil sera obtenido a partir del buffer de datos,
1155 1180 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1156 1181 bloque de datos.
1157 1182 Si este parametro no es pasado se creara uno internamente.
1158 1183
1159 1184 Variables afectadas:
1160 1185 self.dataOut
1161 1186
1162 1187 Return:
1163 1188 None
1164 1189 """
1165 1190
1166 1191 self.isConfig = False
1167 1192
1168 1193 self.datablock = None
1169 1194
1170 1195 self.utc = 0
1171 1196
1172 1197 self.ext = ".r"
1173 1198
1174 1199 self.optchar = "D"
1175 1200
1176 1201 self.basicHeaderObj = BasicHeader()
1177 1202
1178 1203 self.systemHeaderObj = SystemHeader()
1179 1204
1180 1205 self.radarControllerHeaderObj = RadarControllerHeader()
1181 1206
1182 1207 self.processingHeaderObj = ProcessingHeader()
1183 1208
1184 1209 self.online = 0
1185 1210
1186 1211 self.fp = None
1187 1212
1188 1213 self.idFile = None
1189 1214
1190 1215 self.dtype = None
1191 1216
1192 1217 self.fileSizeByHeader = None
1193 1218
1194 1219 self.filenameList = []
1195 1220
1196 1221 self.filename = None
1197 1222
1198 1223 self.fileSize = None
1199 1224
1200 1225 self.firstHeaderSize = 0
1201 1226
1202 1227 self.basicHeaderSize = 24
1203 1228
1204 1229 self.pathList = []
1205 1230
1206 1231 self.filenameList = []
1207 1232
1208 1233 self.lastUTTime = 0
1209 1234
1210 1235 self.maxTimeStep = 30
1211 1236
1212 1237 self.flagNoMoreFiles = 0
1213 1238
1214 1239 self.set = 0
1215 1240
1216 1241 self.path = None
1217 1242
1218 1243 self.profileIndex = 9999
1219 1244
1220 1245 self.delay = 3 #seconds
1221 1246
1222 1247 self.nTries = 3 #quantity tries
1223 1248
1224 1249 self.nFiles = 3 #number of files for searching
1225 1250
1226 1251 self.nReadBlocks = 0
1227 1252
1228 1253 self.flagIsNewFile = 1
1229 1254
1230 1255 self.ippSeconds = 0
1231 1256
1232 1257 self.flagTimeBlock = 0
1233 1258
1234 1259 self.flagIsNewBlock = 0
1235 1260
1236 1261 self.nTotalBlocks = 0
1237 1262
1238 1263 self.blocksize = 0
1239 1264
1240 1265 self.dataOut = self.createObjByDefault()
1241 1266
1242 1267 def createObjByDefault(self):
1243 1268
1244 1269 dataObj = Voltage()
1245 1270
1246 1271 return dataObj
1247 1272
1248 1273 def __hasNotDataInBuffer(self):
1249 1274 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1250 1275 return 1
1251 1276 return 0
1252 1277
1253 1278
1254 1279 def getBlockDimension(self):
1255 1280 """
1256 1281 Obtiene la cantidad de puntos a leer por cada bloque de datos
1257 1282
1258 1283 Affected:
1259 1284 self.blocksize
1260 1285
1261 1286 Return:
1262 1287 None
1263 1288 """
1264 1289 pts2read = self.processingHeaderObj.profilesPerBlock * self.processingHeaderObj.nHeights * self.systemHeaderObj.nChannels
1265 1290 self.blocksize = pts2read
1266 1291
1267 1292
1268 1293 def readBlock(self):
1269 1294 """
1270 1295 readBlock lee el bloque de datos desde la posicion actual del puntero del archivo
1271 1296 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
1272 1297 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
1273 1298 es seteado a 0
1274 1299
1275 1300 Inputs:
1276 1301 None
1277 1302
1278 1303 Return:
1279 1304 None
1280 1305
1281 1306 Affected:
1282 1307 self.profileIndex
1283 1308 self.datablock
1284 1309 self.flagIsNewFile
1285 1310 self.flagIsNewBlock
1286 1311 self.nTotalBlocks
1287 1312
1288 1313 Exceptions:
1289 1314 Si un bloque leido no es un bloque valido
1290 1315 """
1291 1316
1292 1317 junk = numpy.fromfile( self.fp, self.dtype, self.blocksize )
1293 1318
1294 1319 try:
1295 1320 junk = junk.reshape( (self.processingHeaderObj.profilesPerBlock, self.processingHeaderObj.nHeights, self.systemHeaderObj.nChannels) )
1296 1321 except:
1297 1322 print "The read block (%3d) has not enough data" %self.nReadBlocks
1298 1323 return 0
1299 1324
1300 1325 junk = numpy.transpose(junk, (2,0,1))
1301 1326 self.datablock = junk['real'] + junk['imag']*1j
1302 1327
1303 1328 self.profileIndex = 0
1304 1329
1305 1330 self.flagIsNewFile = 0
1306 1331 self.flagIsNewBlock = 1
1307 1332
1308 1333 self.nTotalBlocks += 1
1309 1334 self.nReadBlocks += 1
1310 1335
1311 1336 return 1
1312 1337
1313 1338
1314 1339 def getData(self):
1315 1340 """
1316 1341 getData obtiene una unidad de datos del buffer de lectura y la copia a la clase "Voltage"
1317 1342 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
1318 1343 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
1319 1344
1320 1345 Ademas incrementa el contador del buffer en 1.
1321 1346
1322 1347 Return:
1323 1348 data : retorna un perfil de voltages (alturas * canales) copiados desde el
1324 1349 buffer. Si no hay mas archivos a leer retorna None.
1325 1350
1326 1351 Variables afectadas:
1327 1352 self.dataOut
1328 1353 self.profileIndex
1329 1354
1330 1355 Affected:
1331 1356 self.dataOut
1332 1357 self.profileIndex
1333 1358 self.flagTimeBlock
1334 1359 self.flagIsNewBlock
1335 1360 """
1336 1361
1337 1362 if self.flagNoMoreFiles:
1338 1363 self.dataOut.flagNoData = True
1339 1364 print 'Process finished'
1340 1365 return 0
1341 1366
1342 1367 self.flagTimeBlock = 0
1343 1368 self.flagIsNewBlock = 0
1344 1369
1345 1370 if self.__hasNotDataInBuffer():
1346 1371
1347 1372 if not( self.readNextBlock() ):
1348 1373 return 0
1349 1374
1350 1375 self.dataOut.dtype = self.dtype
1351 1376
1352 1377 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
1353 1378
1354 1379 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
1355 1380
1356 1381 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
1357 1382
1358 1383 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
1359 1384
1360 1385 self.dataOut.flagTimeBlock = self.flagTimeBlock
1361 1386
1362 1387 self.dataOut.ippSeconds = self.ippSeconds
1363 1388
1364 1389 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt
1365 1390
1366 1391 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
1367 1392
1368 1393 self.dataOut.flagShiftFFT = False
1369 1394
1370 1395 if self.processingHeaderObj.code != None:
1371 1396 self.dataOut.nCode = self.processingHeaderObj.nCode
1372 1397
1373 1398 self.dataOut.nBaud = self.processingHeaderObj.nBaud
1374 1399
1375 1400 self.dataOut.code = self.processingHeaderObj.code
1376 1401
1377 1402 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
1378 1403
1379 1404 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
1380 1405
1381 1406 # self.updateDataHeader()
1382 1407
1383 1408 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
1384 1409
1385 1410 if self.datablock == None:
1386 1411 self.dataOut.flagNoData = True
1387 1412 return 0
1388 1413
1389 1414 self.dataOut.data = self.datablock[:,self.profileIndex,:]
1390 1415
1391 1416 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000. + self.profileIndex * self.ippSeconds
1392 1417
1393 1418 self.profileIndex += 1
1394 1419
1395 1420 self.dataOut.flagNoData = False
1396 1421
1397 1422 # print self.profileIndex, self.dataOut.utctime
1398 1423 # if self.profileIndex == 800:
1399 1424 # a=1
1400 1425
1401 1426
1402 1427 return self.dataOut.data
1403 1428
1404 1429
1405 1430 class VoltageWriter(JRODataWriter):
1406 1431 """
1407 1432 Esta clase permite escribir datos de voltajes a archivos procesados (.r). La escritura
1408 1433 de los datos siempre se realiza por bloques.
1409 1434 """
1410 1435
1411 1436 ext = ".r"
1412 1437
1413 1438 optchar = "D"
1414 1439
1415 1440 shapeBuffer = None
1416 1441
1417 1442
1418 1443 def __init__(self):
1419 1444 """
1420 1445 Inicializador de la clase VoltageWriter para la escritura de datos de espectros.
1421 1446
1422 1447 Affected:
1423 1448 self.dataOut
1424 1449
1425 1450 Return: None
1426 1451 """
1427 1452
1428 1453 self.nTotalBlocks = 0
1429 1454
1430 1455 self.profileIndex = 0
1431 1456
1432 1457 self.isConfig = False
1433 1458
1434 1459 self.fp = None
1435 1460
1436 1461 self.flagIsNewFile = 1
1437 1462
1438 1463 self.nTotalBlocks = 0
1439 1464
1440 1465 self.flagIsNewBlock = 0
1441 1466
1442 1467 self.setFile = None
1443 1468
1444 1469 self.dtype = None
1445 1470
1446 1471 self.path = None
1447 1472
1448 1473 self.filename = None
1449 1474
1450 1475 self.basicHeaderObj = BasicHeader()
1451 1476
1452 1477 self.systemHeaderObj = SystemHeader()
1453 1478
1454 1479 self.radarControllerHeaderObj = RadarControllerHeader()
1455 1480
1456 1481 self.processingHeaderObj = ProcessingHeader()
1457 1482
1458 1483 def hasAllDataInBuffer(self):
1459 1484 if self.profileIndex >= self.processingHeaderObj.profilesPerBlock:
1460 1485 return 1
1461 1486 return 0
1462 1487
1463 1488
1464 1489 def setBlockDimension(self):
1465 1490 """
1466 1491 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
1467 1492
1468 1493 Affected:
1469 1494 self.shape_spc_Buffer
1470 1495 self.shape_cspc_Buffer
1471 1496 self.shape_dc_Buffer
1472 1497
1473 1498 Return: None
1474 1499 """
1475 1500 self.shapeBuffer = (self.processingHeaderObj.profilesPerBlock,
1476 1501 self.processingHeaderObj.nHeights,
1477 1502 self.systemHeaderObj.nChannels)
1478 1503
1479 1504 self.datablock = numpy.zeros((self.systemHeaderObj.nChannels,
1480 1505 self.processingHeaderObj.profilesPerBlock,
1481 1506 self.processingHeaderObj.nHeights),
1482 1507 dtype=numpy.dtype('complex'))
1483 1508
1484 1509
1485 1510 def writeBlock(self):
1486 1511 """
1487 1512 Escribe el buffer en el file designado
1488 1513
1489 1514 Affected:
1490 1515 self.profileIndex
1491 1516 self.flagIsNewFile
1492 1517 self.flagIsNewBlock
1493 1518 self.nTotalBlocks
1494 1519 self.blockIndex
1495 1520
1496 1521 Return: None
1497 1522 """
1498 1523 data = numpy.zeros( self.shapeBuffer, self.dtype )
1499 1524
1500 1525 junk = numpy.transpose(self.datablock, (1,2,0))
1501 1526
1502 1527 data['real'] = junk.real
1503 1528 data['imag'] = junk.imag
1504 1529
1505 1530 data = data.reshape( (-1) )
1506 1531
1507 1532 data.tofile( self.fp )
1508 1533
1509 1534 self.datablock.fill(0)
1510 1535
1511 1536 self.profileIndex = 0
1512 1537 self.flagIsNewFile = 0
1513 1538 self.flagIsNewBlock = 1
1514 1539
1515 1540 self.blockIndex += 1
1516 1541 self.nTotalBlocks += 1
1517 1542
1518 1543 def putData(self):
1519 1544 """
1520 1545 Setea un bloque de datos y luego los escribe en un file
1521 1546
1522 1547 Affected:
1523 1548 self.flagIsNewBlock
1524 1549 self.profileIndex
1525 1550
1526 1551 Return:
1527 1552 0 : Si no hay data o no hay mas files que puedan escribirse
1528 1553 1 : Si se escribio la data de un bloque en un file
1529 1554 """
1530 1555 if self.dataOut.flagNoData:
1531 1556 return 0
1532 1557
1533 1558 self.flagIsNewBlock = 0
1534 1559
1535 1560 if self.dataOut.flagTimeBlock:
1536 1561
1537 1562 self.datablock.fill(0)
1538 1563 self.profileIndex = 0
1539 1564 self.setNextFile()
1540 1565
1541 1566 if self.profileIndex == 0:
1542 1567 self.getBasicHeader()
1543 1568
1544 1569 self.datablock[:,self.profileIndex,:] = self.dataOut.data
1545 1570
1546 1571 self.profileIndex += 1
1547 1572
1548 1573 if self.hasAllDataInBuffer():
1549 1574 #if self.flagIsNewFile:
1550 1575 self.writeNextBlock()
1551 1576 # self.getDataHeader()
1552 1577
1553 1578 return 1
1554 1579
1555 1580 def __getProcessFlags(self):
1556 1581
1557 1582 processFlags = 0
1558 1583
1559 1584 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1560 1585 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1561 1586 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1562 1587 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1563 1588 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1564 1589 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1565 1590
1566 1591 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1567 1592
1568 1593
1569 1594
1570 1595 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
1571 1596 PROCFLAG.DATATYPE_SHORT,
1572 1597 PROCFLAG.DATATYPE_LONG,
1573 1598 PROCFLAG.DATATYPE_INT64,
1574 1599 PROCFLAG.DATATYPE_FLOAT,
1575 1600 PROCFLAG.DATATYPE_DOUBLE]
1576 1601
1577 1602
1578 1603 for index in range(len(dtypeList)):
1579 1604 if self.dataOut.dtype == dtypeList[index]:
1580 1605 dtypeValue = datatypeValueList[index]
1581 1606 break
1582 1607
1583 1608 processFlags += dtypeValue
1584 1609
1585 1610 if self.dataOut.flagDecodeData:
1586 1611 processFlags += PROCFLAG.DECODE_DATA
1587 1612
1588 1613 if self.dataOut.flagDeflipData:
1589 1614 processFlags += PROCFLAG.DEFLIP_DATA
1590 1615
1591 1616 if self.dataOut.code != None:
1592 1617 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
1593 1618
1594 1619 if self.dataOut.nCohInt > 1:
1595 1620 processFlags += PROCFLAG.COHERENT_INTEGRATION
1596 1621
1597 1622 return processFlags
1598 1623
1599 1624
1600 1625 def __getBlockSize(self):
1601 1626 '''
1602 1627 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Voltage
1603 1628 '''
1604 1629
1605 1630 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
1606 1631 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
1607 1632 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
1608 1633 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
1609 1634 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
1610 1635 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
1611 1636
1612 1637 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
1613 1638 datatypeValueList = [1,2,4,8,4,8]
1614 1639 for index in range(len(dtypeList)):
1615 1640 if self.dataOut.dtype == dtypeList[index]:
1616 1641 datatypeValue = datatypeValueList[index]
1617 1642 break
1618 1643
1619 1644 blocksize = int(self.dataOut.nHeights * self.dataOut.nChannels * self.dataOut.nProfiles * datatypeValue * 2)
1620 1645
1621 1646 return blocksize
1622 1647
1623 1648 def getDataHeader(self):
1624 1649
1625 1650 """
1626 1651 Obtiene una copia del First Header
1627 1652
1628 1653 Affected:
1629 1654 self.systemHeaderObj
1630 1655 self.radarControllerHeaderObj
1631 1656 self.dtype
1632 1657
1633 1658 Return:
1634 1659 None
1635 1660 """
1636 1661
1637 1662 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
1638 1663 self.systemHeaderObj.nChannels = self.dataOut.nChannels
1639 1664 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
1640 1665
1641 1666 self.getBasicHeader()
1642 1667
1643 1668 processingHeaderSize = 40 # bytes
1644 1669 self.processingHeaderObj.dtype = 0 # Voltage
1645 1670 self.processingHeaderObj.blockSize = self.__getBlockSize()
1646 1671 self.processingHeaderObj.profilesPerBlock = self.profilesPerBlock
1647 1672 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
1648 1673 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
1649 1674 self.processingHeaderObj.processFlags = self.__getProcessFlags()
1650 1675 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt
1651 1676 self.processingHeaderObj.nIncohInt = 1 # Cuando la data de origen es de tipo Voltage
1652 1677 self.processingHeaderObj.totalSpectra = 0 # Cuando la data de origen es de tipo Voltage
1653 1678
1654 1679 if self.dataOut.code != None:
1655 1680 self.processingHeaderObj.code = self.dataOut.code
1656 1681 self.processingHeaderObj.nCode = self.dataOut.nCode
1657 1682 self.processingHeaderObj.nBaud = self.dataOut.nBaud
1658 1683 codesize = int(8 + 4 * self.dataOut.nCode * self.dataOut.nBaud)
1659 1684 processingHeaderSize += codesize
1660 1685
1661 1686 if self.processingHeaderObj.nWindows != 0:
1662 1687 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
1663 1688 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
1664 1689 self.processingHeaderObj.nHeights = self.dataOut.nHeights
1665 1690 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
1666 1691 processingHeaderSize += 12
1667 1692
1668 1693 self.processingHeaderObj.size = processingHeaderSize
1669 1694
1670 1695 class SpectraReader(JRODataReader):
1671 1696 """
1672 1697 Esta clase permite leer datos de espectros desde archivos procesados (.pdata). La lectura
1673 1698 de los datos siempre se realiza por bloques. Los datos leidos (array de 3 dimensiones)
1674 1699 son almacenados en tres buffer's para el Self Spectra, el Cross Spectra y el DC Channel.
1675 1700
1676 1701 paresCanalesIguales * alturas * perfiles (Self Spectra)
1677 1702 paresCanalesDiferentes * alturas * perfiles (Cross Spectra)
1678 1703 canales * alturas (DC Channels)
1679 1704
1680 1705 Esta clase contiene instancias (objetos) de las clases BasicHeader, SystemHeader,
1681 1706 RadarControllerHeader y Spectra. Los tres primeros se usan para almacenar informacion de la
1682 1707 cabecera de datos (metadata), y el cuarto (Spectra) para obtener y almacenar un bloque de
1683 1708 datos desde el "buffer" cada vez que se ejecute el metodo "getData".
1684 1709
1685 1710 Example:
1686 1711 dpath = "/home/myuser/data"
1687 1712
1688 1713 startTime = datetime.datetime(2010,1,20,0,0,0,0,0,0)
1689 1714
1690 1715 endTime = datetime.datetime(2010,1,21,23,59,59,0,0,0)
1691 1716
1692 1717 readerObj = SpectraReader()
1693 1718
1694 1719 readerObj.setup(dpath, startTime, endTime)
1695 1720
1696 1721 while(True):
1697 1722
1698 1723 readerObj.getData()
1699 1724
1700 1725 print readerObj.data_spc
1701 1726
1702 1727 print readerObj.data_cspc
1703 1728
1704 1729 print readerObj.data_dc
1705 1730
1706 1731 if readerObj.flagNoMoreFiles:
1707 1732 break
1708 1733
1709 1734 """
1710 1735
1711 1736 pts2read_SelfSpectra = 0
1712 1737
1713 1738 pts2read_CrossSpectra = 0
1714 1739
1715 1740 pts2read_DCchannels = 0
1716 1741
1717 1742 ext = ".pdata"
1718 1743
1719 1744 optchar = "P"
1720 1745
1721 1746 dataOut = None
1722 1747
1723 1748 nRdChannels = None
1724 1749
1725 1750 nRdPairs = None
1726 1751
1727 1752 rdPairList = []
1728 1753
1729 1754
1730 1755 def __init__(self):
1731 1756 """
1732 1757 Inicializador de la clase SpectraReader para la lectura de datos de espectros.
1733 1758
1734 1759 Inputs:
1735 1760 dataOut : Objeto de la clase Spectra. Este objeto sera utilizado para
1736 1761 almacenar un perfil de datos cada vez que se haga un requerimiento
1737 1762 (getData). El perfil sera obtenido a partir del buffer de datos,
1738 1763 si el buffer esta vacio se hara un nuevo proceso de lectura de un
1739 1764 bloque de datos.
1740 1765 Si este parametro no es pasado se creara uno internamente.
1741 1766
1742 1767 Affected:
1743 1768 self.dataOut
1744 1769
1745 1770 Return : None
1746 1771 """
1747 1772
1748 1773 self.isConfig = False
1749 1774
1750 1775 self.pts2read_SelfSpectra = 0
1751 1776
1752 1777 self.pts2read_CrossSpectra = 0
1753 1778
1754 1779 self.pts2read_DCchannels = 0
1755 1780
1756 1781 self.datablock = None
1757 1782
1758 1783 self.utc = None
1759 1784
1760 1785 self.ext = ".pdata"
1761 1786
1762 1787 self.optchar = "P"
1763 1788
1764 1789 self.basicHeaderObj = BasicHeader()
1765 1790
1766 1791 self.systemHeaderObj = SystemHeader()
1767 1792
1768 1793 self.radarControllerHeaderObj = RadarControllerHeader()
1769 1794
1770 1795 self.processingHeaderObj = ProcessingHeader()
1771 1796
1772 1797 self.online = 0
1773 1798
1774 1799 self.fp = None
1775 1800
1776 1801 self.idFile = None
1777 1802
1778 1803 self.dtype = None
1779 1804
1780 1805 self.fileSizeByHeader = None
1781 1806
1782 1807 self.filenameList = []
1783 1808
1784 1809 self.filename = None
1785 1810
1786 1811 self.fileSize = None
1787 1812
1788 1813 self.firstHeaderSize = 0
1789 1814
1790 1815 self.basicHeaderSize = 24
1791 1816
1792 1817 self.pathList = []
1793 1818
1794 1819 self.lastUTTime = 0
1795 1820
1796 1821 self.maxTimeStep = 30
1797 1822
1798 1823 self.flagNoMoreFiles = 0
1799 1824
1800 1825 self.set = 0
1801 1826
1802 1827 self.path = None
1803 1828
1804 1829 self.delay = 3 #seconds
1805 1830
1806 1831 self.nTries = 3 #quantity tries
1807 1832
1808 1833 self.nFiles = 3 #number of files for searching
1809 1834
1810 1835 self.nReadBlocks = 0
1811 1836
1812 1837 self.flagIsNewFile = 1
1813 1838
1814 1839 self.ippSeconds = 0
1815 1840
1816 1841 self.flagTimeBlock = 0
1817 1842
1818 1843 self.flagIsNewBlock = 0
1819 1844
1820 1845 self.nTotalBlocks = 0
1821 1846
1822 1847 self.blocksize = 0
1823 1848
1824 1849 self.dataOut = self.createObjByDefault()
1825 1850
1826 1851
1827 1852 def createObjByDefault(self):
1828 1853
1829 1854 dataObj = Spectra()
1830 1855
1831 1856 return dataObj
1832 1857
1833 1858 def __hasNotDataInBuffer(self):
1834 1859 return 1
1835 1860
1836 1861
1837 1862 def getBlockDimension(self):
1838 1863 """
1839 1864 Obtiene la cantidad de puntos a leer por cada bloque de datos
1840 1865
1841 1866 Affected:
1842 1867 self.nRdChannels
1843 1868 self.nRdPairs
1844 1869 self.pts2read_SelfSpectra
1845 1870 self.pts2read_CrossSpectra
1846 1871 self.pts2read_DCchannels
1847 1872 self.blocksize
1848 1873 self.dataOut.nChannels
1849 1874 self.dataOut.nPairs
1850 1875
1851 1876 Return:
1852 1877 None
1853 1878 """
1854 1879 self.nRdChannels = 0
1855 1880 self.nRdPairs = 0
1856 1881 self.rdPairList = []
1857 1882
1858 1883 for i in range(0, self.processingHeaderObj.totalSpectra*2, 2):
1859 1884 if self.processingHeaderObj.spectraComb[i] == self.processingHeaderObj.spectraComb[i+1]:
1860 1885 self.nRdChannels = self.nRdChannels + 1 #par de canales iguales
1861 1886 else:
1862 1887 self.nRdPairs = self.nRdPairs + 1 #par de canales diferentes
1863 1888 self.rdPairList.append((self.processingHeaderObj.spectraComb[i], self.processingHeaderObj.spectraComb[i+1]))
1864 1889
1865 1890 pts2read = self.processingHeaderObj.nHeights * self.processingHeaderObj.profilesPerBlock
1866 1891
1867 1892 self.pts2read_SelfSpectra = int(self.nRdChannels * pts2read)
1868 1893 self.blocksize = self.pts2read_SelfSpectra
1869 1894
1870 1895 if self.processingHeaderObj.flag_cspc:
1871 1896 self.pts2read_CrossSpectra = int(self.nRdPairs * pts2read)
1872 1897 self.blocksize += self.pts2read_CrossSpectra
1873 1898
1874 1899 if self.processingHeaderObj.flag_dc:
1875 1900 self.pts2read_DCchannels = int(self.systemHeaderObj.nChannels * self.processingHeaderObj.nHeights)
1876 1901 self.blocksize += self.pts2read_DCchannels
1877 1902
1878 1903 # self.blocksize = self.pts2read_SelfSpectra + self.pts2read_CrossSpectra + self.pts2read_DCchannels
1879 1904
1880 1905
1881 1906 def readBlock(self):
1882 1907 """
1883 1908 Lee el bloque de datos desde la posicion actual del puntero del archivo
1884 1909 (self.fp) y actualiza todos los parametros relacionados al bloque de datos
1885 1910 (metadata + data). La data leida es almacenada en el buffer y el contador del buffer
1886 1911 es seteado a 0
1887 1912
1888 1913 Return: None
1889 1914
1890 1915 Variables afectadas:
1891 1916
1892 1917 self.flagIsNewFile
1893 1918 self.flagIsNewBlock
1894 1919 self.nTotalBlocks
1895 1920 self.data_spc
1896 1921 self.data_cspc
1897 1922 self.data_dc
1898 1923
1899 1924 Exceptions:
1900 1925 Si un bloque leido no es un bloque valido
1901 1926 """
1902 1927 blockOk_flag = False
1903 1928 fpointer = self.fp.tell()
1904 1929
1905 1930 spc = numpy.fromfile( self.fp, self.dtype[0], self.pts2read_SelfSpectra )
1906 1931 spc = spc.reshape( (self.nRdChannels, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
1907 1932
1908 1933 if self.processingHeaderObj.flag_cspc:
1909 1934 cspc = numpy.fromfile( self.fp, self.dtype, self.pts2read_CrossSpectra )
1910 1935 cspc = cspc.reshape( (self.nRdPairs, self.processingHeaderObj.nHeights, self.processingHeaderObj.profilesPerBlock) ) #transforma a un arreglo 3D
1911 1936
1912 1937 if self.processingHeaderObj.flag_dc:
1913 1938 dc = numpy.fromfile( self.fp, self.dtype, self.pts2read_DCchannels ) #int(self.processingHeaderObj.nHeights*self.systemHeaderObj.nChannels) )
1914 1939 dc = dc.reshape( (self.systemHeaderObj.nChannels, self.processingHeaderObj.nHeights) ) #transforma a un arreglo 2D
1915 1940
1916 1941
1917 1942 if not(self.processingHeaderObj.shif_fft):
1918 1943 #desplaza a la derecha en el eje 2 determinadas posiciones
1919 1944 shift = int(self.processingHeaderObj.profilesPerBlock/2)
1920 1945 spc = numpy.roll( spc, shift , axis=2 )
1921 1946
1922 1947 if self.processingHeaderObj.flag_cspc:
1923 1948 #desplaza a la derecha en el eje 2 determinadas posiciones
1924 1949 cspc = numpy.roll( cspc, shift, axis=2 )
1925 1950
1926 1951
1927 1952 spc = numpy.transpose( spc, (0,2,1) )
1928 1953 self.data_spc = spc
1929 1954
1930 1955 if self.processingHeaderObj.flag_cspc:
1931 1956 cspc = numpy.transpose( cspc, (0,2,1) )
1932 1957 self.data_cspc = cspc['real'] + cspc['imag']*1j
1933 1958 else:
1934 1959 self.data_cspc = None
1935 1960
1936 1961 if self.processingHeaderObj.flag_dc:
1937 1962 self.data_dc = dc['real'] + dc['imag']*1j
1938 1963 else:
1939 1964 self.data_dc = None
1940 1965
1941 1966 self.flagIsNewFile = 0
1942 1967 self.flagIsNewBlock = 1
1943 1968
1944 1969 self.nTotalBlocks += 1
1945 1970 self.nReadBlocks += 1
1946 1971
1947 1972 return 1
1948 1973
1949 1974
1950 1975 def getData(self):
1951 1976 """
1952 1977 Copia el buffer de lectura a la clase "Spectra",
1953 1978 con todos los parametros asociados a este (metadata). cuando no hay datos en el buffer de
1954 1979 lectura es necesario hacer una nueva lectura de los bloques de datos usando "readNextBlock"
1955 1980
1956 1981 Return:
1957 1982 0 : Si no hay mas archivos disponibles
1958 1983 1 : Si hizo una buena copia del buffer
1959 1984
1960 1985 Affected:
1961 1986 self.dataOut
1962 1987
1963 1988 self.flagTimeBlock
1964 1989 self.flagIsNewBlock
1965 1990 """
1966 1991
1967 1992 if self.flagNoMoreFiles:
1968 1993 self.dataOut.flagNoData = True
1969 1994 print 'Process finished'
1970 1995 return 0
1971 1996
1972 1997 self.flagTimeBlock = 0
1973 1998 self.flagIsNewBlock = 0
1974 1999
1975 2000 if self.__hasNotDataInBuffer():
1976 2001
1977 2002 if not( self.readNextBlock() ):
1978 2003 self.dataOut.flagNoData = True
1979 2004 return 0
1980 2005
1981 2006 # self.updateDataHeader()
1982 2007
1983 2008 #data es un numpy array de 3 dmensiones (perfiles, alturas y canales)
1984 2009
1985 2010 if self.data_dc == None:
1986 2011 self.dataOut.flagNoData = True
1987 2012 return 0
1988 2013
1989 2014 self.dataOut.data_spc = self.data_spc
1990 2015
1991 2016 self.dataOut.data_cspc = self.data_cspc
1992 2017
1993 2018 self.dataOut.data_dc = self.data_dc
1994 2019
1995 2020 self.dataOut.flagTimeBlock = self.flagTimeBlock
1996 2021
1997 2022 self.dataOut.flagNoData = False
1998 2023
1999 2024 self.dataOut.dtype = self.dtype
2000 2025
2001 2026 # self.dataOut.nChannels = self.nRdChannels
2002 2027
2003 2028 self.dataOut.nPairs = self.nRdPairs
2004 2029
2005 2030 self.dataOut.pairsList = self.rdPairList
2006 2031
2007 2032 # self.dataOut.nHeights = self.processingHeaderObj.nHeights
2008 2033
2009 2034 self.dataOut.nProfiles = self.processingHeaderObj.profilesPerBlock
2010 2035
2011 2036 self.dataOut.nFFTPoints = self.processingHeaderObj.profilesPerBlock
2012 2037
2013 2038 self.dataOut.nCohInt = self.processingHeaderObj.nCohInt
2014 2039
2015 2040 self.dataOut.nIncohInt = self.processingHeaderObj.nIncohInt
2016 2041
2017 2042 xf = self.processingHeaderObj.firstHeight + self.processingHeaderObj.nHeights*self.processingHeaderObj.deltaHeight
2018 2043
2019 2044 self.dataOut.heightList = numpy.arange(self.processingHeaderObj.firstHeight, xf, self.processingHeaderObj.deltaHeight)
2020 2045
2021 2046 self.dataOut.channelList = range(self.systemHeaderObj.nChannels)
2022 2047
2023 2048 # self.dataOut.channelIndexList = range(self.systemHeaderObj.nChannels)
2024 2049
2025 2050 self.dataOut.utctime = self.basicHeaderObj.utc + self.basicHeaderObj.miliSecond/1000.#+ self.profileIndex * self.ippSeconds
2026 2051
2027 2052 self.dataOut.ippSeconds = self.ippSeconds
2028 2053
2029 2054 self.dataOut.timeInterval = self.ippSeconds * self.processingHeaderObj.nCohInt * self.processingHeaderObj.nIncohInt * self.dataOut.nFFTPoints
2030 2055
2031 2056 self.dataOut.flagShiftFFT = self.processingHeaderObj.shif_fft
2032 2057
2033 2058 # self.profileIndex += 1
2034 2059
2035 2060 self.dataOut.systemHeaderObj = self.systemHeaderObj.copy()
2036 2061
2037 2062 self.dataOut.radarControllerHeaderObj = self.radarControllerHeaderObj.copy()
2038 2063
2039 2064 return self.dataOut.data_spc
2040 2065
2041 2066
2042 2067 class SpectraWriter(JRODataWriter):
2043 2068
2044 2069 """
2045 2070 Esta clase permite escribir datos de espectros a archivos procesados (.pdata). La escritura
2046 2071 de los datos siempre se realiza por bloques.
2047 2072 """
2048 2073
2049 2074 ext = ".pdata"
2050 2075
2051 2076 optchar = "P"
2052 2077
2053 2078 shape_spc_Buffer = None
2054 2079
2055 2080 shape_cspc_Buffer = None
2056 2081
2057 2082 shape_dc_Buffer = None
2058 2083
2059 2084 data_spc = None
2060 2085
2061 2086 data_cspc = None
2062 2087
2063 2088 data_dc = None
2064 2089
2065 2090 # dataOut = None
2066 2091
2067 2092 def __init__(self):
2068 2093 """
2069 2094 Inicializador de la clase SpectraWriter para la escritura de datos de espectros.
2070 2095
2071 2096 Affected:
2072 2097 self.dataOut
2073 2098 self.basicHeaderObj
2074 2099 self.systemHeaderObj
2075 2100 self.radarControllerHeaderObj
2076 2101 self.processingHeaderObj
2077 2102
2078 2103 Return: None
2079 2104 """
2080 2105
2081 2106 self.isConfig = False
2082 2107
2083 2108 self.nTotalBlocks = 0
2084 2109
2085 2110 self.data_spc = None
2086 2111
2087 2112 self.data_cspc = None
2088 2113
2089 2114 self.data_dc = None
2090 2115
2091 2116 self.fp = None
2092 2117
2093 2118 self.flagIsNewFile = 1
2094 2119
2095 2120 self.nTotalBlocks = 0
2096 2121
2097 2122 self.flagIsNewBlock = 0
2098 2123
2099 2124 self.setFile = None
2100 2125
2101 2126 self.dtype = None
2102 2127
2103 2128 self.path = None
2104 2129
2105 2130 self.noMoreFiles = 0
2106 2131
2107 2132 self.filename = None
2108 2133
2109 2134 self.basicHeaderObj = BasicHeader()
2110 2135
2111 2136 self.systemHeaderObj = SystemHeader()
2112 2137
2113 2138 self.radarControllerHeaderObj = RadarControllerHeader()
2114 2139
2115 2140 self.processingHeaderObj = ProcessingHeader()
2116 2141
2117 2142
2118 2143 def hasAllDataInBuffer(self):
2119 2144 return 1
2120 2145
2121 2146
2122 2147 def setBlockDimension(self):
2123 2148 """
2124 2149 Obtiene las formas dimensionales del los subbloques de datos que componen un bloque
2125 2150
2126 2151 Affected:
2127 2152 self.shape_spc_Buffer
2128 2153 self.shape_cspc_Buffer
2129 2154 self.shape_dc_Buffer
2130 2155
2131 2156 Return: None
2132 2157 """
2133 2158 self.shape_spc_Buffer = (self.dataOut.nChannels,
2134 2159 self.processingHeaderObj.nHeights,
2135 2160 self.processingHeaderObj.profilesPerBlock)
2136 2161
2137 2162 self.shape_cspc_Buffer = (self.dataOut.nPairs,
2138 2163 self.processingHeaderObj.nHeights,
2139 2164 self.processingHeaderObj.profilesPerBlock)
2140 2165
2141 2166 self.shape_dc_Buffer = (self.dataOut.nChannels,
2142 2167 self.processingHeaderObj.nHeights)
2143 2168
2144 2169
2145 2170 def writeBlock(self):
2146 2171 """
2147 2172 Escribe el buffer en el file designado
2148 2173
2149 2174 Affected:
2150 2175 self.data_spc
2151 2176 self.data_cspc
2152 2177 self.data_dc
2153 2178 self.flagIsNewFile
2154 2179 self.flagIsNewBlock
2155 2180 self.nTotalBlocks
2156 2181 self.nWriteBlocks
2157 2182
2158 2183 Return: None
2159 2184 """
2160 2185
2161 2186 spc = numpy.transpose( self.data_spc, (0,2,1) )
2162 2187 if not( self.processingHeaderObj.shif_fft ):
2163 2188 spc = numpy.roll( spc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2164 2189 data = spc.reshape((-1))
2165 2190 data.tofile(self.fp)
2166 2191
2167 2192 if self.data_cspc != None:
2168 2193 data = numpy.zeros( self.shape_cspc_Buffer, self.dtype )
2169 2194 cspc = numpy.transpose( self.data_cspc, (0,2,1) )
2170 2195 if not( self.processingHeaderObj.shif_fft ):
2171 2196 cspc = numpy.roll( cspc, self.processingHeaderObj.profilesPerBlock/2, axis=2 ) #desplaza a la derecha en el eje 2 determinadas posiciones
2172 2197 data['real'] = cspc.real
2173 2198 data['imag'] = cspc.imag
2174 2199 data = data.reshape((-1))
2175 2200 data.tofile(self.fp)
2176 2201
2177 2202 if self.data_dc != None:
2178 2203 data = numpy.zeros( self.shape_dc_Buffer, self.dtype )
2179 2204 dc = self.data_dc
2180 2205 data['real'] = dc.real
2181 2206 data['imag'] = dc.imag
2182 2207 data = data.reshape((-1))
2183 2208 data.tofile(self.fp)
2184 2209
2185 2210 self.data_spc.fill(0)
2186 2211 self.data_dc.fill(0)
2187 2212 if self.data_cspc != None:
2188 2213 self.data_cspc.fill(0)
2189 2214
2190 2215 self.flagIsNewFile = 0
2191 2216 self.flagIsNewBlock = 1
2192 2217 self.nTotalBlocks += 1
2193 2218 self.nWriteBlocks += 1
2194 2219 self.blockIndex += 1
2195 2220
2196 2221
2197 2222 def putData(self):
2198 2223 """
2199 2224 Setea un bloque de datos y luego los escribe en un file
2200 2225
2201 2226 Affected:
2202 2227 self.data_spc
2203 2228 self.data_cspc
2204 2229 self.data_dc
2205 2230
2206 2231 Return:
2207 2232 0 : Si no hay data o no hay mas files que puedan escribirse
2208 2233 1 : Si se escribio la data de un bloque en un file
2209 2234 """
2210 2235
2211 2236 if self.dataOut.flagNoData:
2212 2237 return 0
2213 2238
2214 2239 self.flagIsNewBlock = 0
2215 2240
2216 2241 if self.dataOut.flagTimeBlock:
2217 2242 self.data_spc.fill(0)
2218 2243 self.data_cspc.fill(0)
2219 2244 self.data_dc.fill(0)
2220 2245 self.setNextFile()
2221 2246
2222 2247 if self.flagIsNewFile == 0:
2223 2248 self.getBasicHeader()
2224 2249
2225 2250 self.data_spc = self.dataOut.data_spc
2226 2251 self.data_cspc = self.dataOut.data_cspc
2227 2252 self.data_dc = self.dataOut.data_dc
2228 2253
2229 2254 # #self.processingHeaderObj.dataBlocksPerFile)
2230 2255 if self.hasAllDataInBuffer():
2231 2256 # self.getDataHeader()
2232 2257 self.writeNextBlock()
2233 2258
2234 2259 return 1
2235 2260
2236 2261
2237 2262 def __getProcessFlags(self):
2238 2263
2239 2264 processFlags = 0
2240 2265
2241 2266 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2242 2267 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2243 2268 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2244 2269 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2245 2270 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2246 2271 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2247 2272
2248 2273 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2249 2274
2250 2275
2251 2276
2252 2277 datatypeValueList = [PROCFLAG.DATATYPE_CHAR,
2253 2278 PROCFLAG.DATATYPE_SHORT,
2254 2279 PROCFLAG.DATATYPE_LONG,
2255 2280 PROCFLAG.DATATYPE_INT64,
2256 2281 PROCFLAG.DATATYPE_FLOAT,
2257 2282 PROCFLAG.DATATYPE_DOUBLE]
2258 2283
2259 2284
2260 2285 for index in range(len(dtypeList)):
2261 2286 if self.dataOut.dtype == dtypeList[index]:
2262 2287 dtypeValue = datatypeValueList[index]
2263 2288 break
2264 2289
2265 2290 processFlags += dtypeValue
2266 2291
2267 2292 if self.dataOut.flagDecodeData:
2268 2293 processFlags += PROCFLAG.DECODE_DATA
2269 2294
2270 2295 if self.dataOut.flagDeflipData:
2271 2296 processFlags += PROCFLAG.DEFLIP_DATA
2272 2297
2273 2298 if self.dataOut.code != None:
2274 2299 processFlags += PROCFLAG.DEFINE_PROCESS_CODE
2275 2300
2276 2301 if self.dataOut.nIncohInt > 1:
2277 2302 processFlags += PROCFLAG.INCOHERENT_INTEGRATION
2278 2303
2279 2304 if self.dataOut.data_dc != None:
2280 2305 processFlags += PROCFLAG.SAVE_CHANNELS_DC
2281 2306
2282 2307 return processFlags
2283 2308
2284 2309
2285 2310 def __getBlockSize(self):
2286 2311 '''
2287 2312 Este metodos determina el cantidad de bytes para un bloque de datos de tipo Spectra
2288 2313 '''
2289 2314
2290 2315 dtype0 = numpy.dtype([('real','<i1'),('imag','<i1')])
2291 2316 dtype1 = numpy.dtype([('real','<i2'),('imag','<i2')])
2292 2317 dtype2 = numpy.dtype([('real','<i4'),('imag','<i4')])
2293 2318 dtype3 = numpy.dtype([('real','<i8'),('imag','<i8')])
2294 2319 dtype4 = numpy.dtype([('real','<f4'),('imag','<f4')])
2295 2320 dtype5 = numpy.dtype([('real','<f8'),('imag','<f8')])
2296 2321
2297 2322 dtypeList = [dtype0, dtype1, dtype2, dtype3, dtype4, dtype5]
2298 2323 datatypeValueList = [1,2,4,8,4,8]
2299 2324 for index in range(len(dtypeList)):
2300 2325 if self.dataOut.dtype == dtypeList[index]:
2301 2326 datatypeValue = datatypeValueList[index]
2302 2327 break
2303 2328
2304 2329
2305 2330 pts2write = self.dataOut.nHeights * self.dataOut.nFFTPoints
2306 2331
2307 2332 pts2write_SelfSpectra = int(self.dataOut.nChannels * pts2write)
2308 2333 blocksize = (pts2write_SelfSpectra*datatypeValue)
2309 2334
2310 2335 if self.dataOut.data_cspc != None:
2311 2336 pts2write_CrossSpectra = int(self.dataOut.nPairs * pts2write)
2312 2337 blocksize += (pts2write_CrossSpectra*datatypeValue*2)
2313 2338
2314 2339 if self.dataOut.data_dc != None:
2315 2340 pts2write_DCchannels = int(self.dataOut.nChannels * self.dataOut.nHeights)
2316 2341 blocksize += (pts2write_DCchannels*datatypeValue*2)
2317 2342
2318 2343 blocksize = blocksize #* datatypeValue * 2 #CORREGIR ESTO
2319 2344
2320 2345 return blocksize
2321 2346
2322 2347 def getDataHeader(self):
2323 2348
2324 2349 """
2325 2350 Obtiene una copia del First Header
2326 2351
2327 2352 Affected:
2328 2353 self.systemHeaderObj
2329 2354 self.radarControllerHeaderObj
2330 2355 self.dtype
2331 2356
2332 2357 Return:
2333 2358 None
2334 2359 """
2335 2360
2336 2361 self.systemHeaderObj = self.dataOut.systemHeaderObj.copy()
2337 2362 self.systemHeaderObj.nChannels = self.dataOut.nChannels
2338 2363 self.radarControllerHeaderObj = self.dataOut.radarControllerHeaderObj.copy()
2339 2364
2340 2365 self.getBasicHeader()
2341 2366
2342 2367 processingHeaderSize = 40 # bytes
2343 2368 self.processingHeaderObj.dtype = 0 # Voltage
2344 2369 self.processingHeaderObj.blockSize = self.__getBlockSize()
2345 2370 self.processingHeaderObj.profilesPerBlock = self.dataOut.nFFTPoints
2346 2371 self.processingHeaderObj.dataBlocksPerFile = self.blocksPerFile
2347 2372 self.processingHeaderObj.nWindows = 1 #podria ser 1 o self.dataOut.processingHeaderObj.nWindows
2348 2373 self.processingHeaderObj.processFlags = self.__getProcessFlags()
2349 2374 self.processingHeaderObj.nCohInt = self.dataOut.nCohInt# Se requiere para determinar el valor de timeInterval
2350 2375 self.processingHeaderObj.nIncohInt = self.dataOut.nIncohInt
2351 2376 self.processingHeaderObj.totalSpectra = self.dataOut.nPairs + self.dataOut.nChannels
2352 2377
2353 2378 if self.processingHeaderObj.totalSpectra > 0:
2354 2379 channelList = []
2355 2380 for channel in range(self.dataOut.nChannels):
2356 2381 channelList.append(channel)
2357 2382 channelList.append(channel)
2358 2383
2359 2384 pairsList = []
2360 2385 for pair in self.dataOut.pairsList:
2361 2386 pairsList.append(pair[0])
2362 2387 pairsList.append(pair[1])
2363 2388 spectraComb = channelList + pairsList
2364 2389 spectraComb = numpy.array(spectraComb,dtype="u1")
2365 2390 self.processingHeaderObj.spectraComb = spectraComb
2366 2391 sizeOfSpcComb = len(spectraComb)
2367 2392 processingHeaderSize += sizeOfSpcComb
2368 2393
2369 2394 if self.dataOut.code != None:
2370 2395 self.processingHeaderObj.code = self.dataOut.code
2371 2396 self.processingHeaderObj.nCode = self.dataOut.nCode
2372 2397 self.processingHeaderObj.nBaud = self.dataOut.nBaud
2373 2398 nCodeSize = 4 # bytes
2374 2399 nBaudSize = 4 # bytes
2375 2400 codeSize = 4 # bytes
2376 2401 sizeOfCode = int(nCodeSize + nBaudSize + codeSize * self.dataOut.nCode * self.dataOut.nBaud)
2377 2402 processingHeaderSize += sizeOfCode
2378 2403
2379 2404 if self.processingHeaderObj.nWindows != 0:
2380 2405 self.processingHeaderObj.firstHeight = self.dataOut.heightList[0]
2381 2406 self.processingHeaderObj.deltaHeight = self.dataOut.heightList[1] - self.dataOut.heightList[0]
2382 2407 self.processingHeaderObj.nHeights = self.dataOut.nHeights
2383 2408 self.processingHeaderObj.samplesWin = self.dataOut.nHeights
2384 2409 sizeOfFirstHeight = 4
2385 2410 sizeOfdeltaHeight = 4
2386 2411 sizeOfnHeights = 4
2387 2412 sizeOfWindows = (sizeOfFirstHeight + sizeOfdeltaHeight + sizeOfnHeights)*self.processingHeaderObj.nWindows
2388 2413 processingHeaderSize += sizeOfWindows
2389 2414
2390 2415 self.processingHeaderObj.size = processingHeaderSize
2391 2416
2392 2417 class SpectraHeisWriter():
2393 2418
2394 2419 i=0
2395 2420
2396 2421 def __init__(self, dataOut):
2397 2422
2398 2423 self.wrObj = FITS()
2399 2424 self.dataOut = dataOut
2400 2425
2401 2426 def isNumber(str):
2402 2427 """
2403 2428 Chequea si el conjunto de caracteres que componen un string puede ser convertidos a un numero.
2404 2429
2405 2430 Excepciones:
2406 2431 Si un determinado string no puede ser convertido a numero
2407 2432 Input:
2408 2433 str, string al cual se le analiza para determinar si convertible a un numero o no
2409 2434
2410 2435 Return:
2411 2436 True : si el string es uno numerico
2412 2437 False : no es un string numerico
2413 2438 """
2414 2439 try:
2415 2440 float( str )
2416 2441 return True
2417 2442 except:
2418 2443 return False
2419 2444
2420 2445 def setup(self, wrpath,):
2421 2446
2422 2447 if not(os.path.exists(wrpath)):
2423 2448 os.mkdir(wrpath)
2424 2449
2425 2450 self.wrpath = wrpath
2426 2451 self.setFile = 0
2427 2452
2428 2453 def putData(self):
2429 2454 # self.wrObj.writeHeader(nChannels=self.dataOut.nChannels, nFFTPoints=self.dataOut.nFFTPoints)
2430 2455 #name = self.dataOut.utctime
2431 2456 name= time.localtime( self.dataOut.utctime)
2432 2457 ext=".fits"
2433 2458 #folder='D%4.4d%3.3d'%(name.tm_year,name.tm_yday)
2434 2459 subfolder = 'D%4.4d%3.3d' % (name.tm_year,name.tm_yday)
2435 2460
2436 doypath = os.path.join( self.wrpath, subfolder )
2437 if not( os.path.exists(doypath) ):
2438 os.mkdir(doypath)
2461 fullpath = os.path.join( self.wrpath, subfolder )
2462 if not( os.path.exists(fullpath) ):
2463 os.mkdir(fullpath)
2439 2464 self.setFile += 1
2440 2465 file = 'D%4.4d%3.3d%3.3d%s' % (name.tm_year,name.tm_yday,self.setFile,ext)
2441 2466
2442 2467 filename = os.path.join(self.wrpath,subfolder, file)
2443 2468
2444 2469 # print self.dataOut.ippSeconds
2445 2470 freq=numpy.arange(-1*self.dataOut.nHeights/2.,self.dataOut.nHeights/2.)/(2*self.dataOut.ippSeconds)
2446 2471
2447 2472 col1=self.wrObj.setColF(name="freq", format=str(self.dataOut.nFFTPoints)+'E', array=freq)
2448 2473 col2=self.wrObj.writeData(name="P_Ch1",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[0,:]))
2449 2474 col3=self.wrObj.writeData(name="P_Ch2",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[1,:]))
2450 2475 col4=self.wrObj.writeData(name="P_Ch3",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[2,:]))
2451 2476 col5=self.wrObj.writeData(name="P_Ch4",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[3,:]))
2452 2477 col6=self.wrObj.writeData(name="P_Ch5",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[4,:]))
2453 2478 col7=self.wrObj.writeData(name="P_Ch6",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[5,:]))
2454 2479 col8=self.wrObj.writeData(name="P_Ch7",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[6,:]))
2455 2480 col9=self.wrObj.writeData(name="P_Ch8",format=str(self.dataOut.nFFTPoints)+'E',data=10*numpy.log10(self.dataOut.data_spc[7,:]))
2456 2481 #n=numpy.arange((100))
2457 2482 n=self.dataOut.data_spc[6,:]
2458 2483 a=self.wrObj.cFImage(n)
2459 2484 b=self.wrObj.Ctable(col1,col2,col3,col4,col5,col6,col7,col8,col9)
2460 2485 self.wrObj.CFile(a,b)
2461 2486 self.wrObj.wFile(filename)
2462 2487 return 1
2463 2488
2464 2489 class FITS:
2465 2490
2466 2491 name=None
2467 2492 format=None
2468 2493 array =None
2469 2494 data =None
2470 2495 thdulist=None
2471 2496
2472 2497 def __init__(self):
2473 2498
2474 2499 pass
2475 2500
2476 2501 def setColF(self,name,format,array):
2477 2502 self.name=name
2478 2503 self.format=format
2479 2504 self.array=array
2480 2505 a1=numpy.array([self.array],dtype=numpy.float32)
2481 2506 self.col1 = pyfits.Column(name=self.name, format=self.format, array=a1)
2482 2507 return self.col1
2483 2508
2484 2509 # def setColP(self,name,format,data):
2485 2510 # self.name=name
2486 2511 # self.format=format
2487 2512 # self.data=data
2488 2513 # a2=numpy.array([self.data],dtype=numpy.float32)
2489 2514 # self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2490 2515 # return self.col2
2491 2516
2492 2517 def writeHeader(self,):
2493 2518 pass
2494 2519
2495 2520 def writeData(self,name,format,data):
2496 2521 self.name=name
2497 2522 self.format=format
2498 2523 self.data=data
2499 2524 a2=numpy.array([self.data],dtype=numpy.float32)
2500 2525 self.col2 = pyfits.Column(name=self.name, format=self.format, array=a2)
2501 2526 return self.col2
2502 2527
2503 2528 def cFImage(self,n):
2504 2529 self.hdu= pyfits.PrimaryHDU(n)
2505 2530 return self.hdu
2506 2531
2507 2532 def Ctable(self,col1,col2,col3,col4,col5,col6,col7,col8,col9):
2508 2533 self.cols=pyfits.ColDefs( [col1,col2,col3,col4,col5,col6,col7,col8,col9])
2509 2534 self.tbhdu = pyfits.new_table(self.cols)
2510 2535 return self.tbhdu
2511 2536
2512 2537 def CFile(self,hdu,tbhdu):
2513 2538 self.thdulist=pyfits.HDUList([hdu,tbhdu])
2514 2539
2515 2540 def wFile(self,filename):
2516 2541 self.thdulist.writeto(filename) No newline at end of file
@@ -1,516 +1,519
1 1 '''
2 2
3 3 $Author: murco $
4 4 $Id: JROHeaderIO.py 151 2012-10-31 19:00:51Z murco $
5 5 '''
6 6 import sys
7 7 import numpy
8 8 import copy
9 import datetime
9 10
10 11 class Header:
11 12
12 13 def __init__(self):
13 14 raise
14 15
15 16 def copy(self):
16 17 return copy.deepcopy(self)
17 18
18 19 def read():
19 20 pass
20 21
21 22 def write():
22 23 pass
23 24
24 25 def printInfo(self):
25 26
26 27 for key in self.__dict__.keys():
27 28 print "%s = %s" %(key, self.__dict__[key])
28 29
29 30 class BasicHeader(Header):
30 31
31 32 size = None
32 33 version = None
33 34 dataBlock = None
34 35 utc = None
35 36 miliSecond = None
36 37 timeZone = None
37 38 dstFlag = None
38 39 errorCount = None
39 40 struct = None
41 datatime = None
40 42
41 43 def __init__(self):
42 44
43 45 self.size = 0
44 46 self.version = 0
45 47 self.dataBlock = 0
46 48 self.utc = 0
47 49 self.miliSecond = 0
48 50 self.timeZone = 0
49 51 self.dstFlag = 0
50 52 self.errorCount = 0
51 53 self.struct = numpy.dtype([
52 54 ('nSize','<u4'),
53 55 ('nVersion','<u2'),
54 56 ('nDataBlockId','<u4'),
55 57 ('nUtime','<u4'),
56 58 ('nMilsec','<u2'),
57 59 ('nTimezone','<i2'),
58 60 ('nDstflag','<i2'),
59 61 ('nErrorCount','<u4')
60 62 ])
61 63
62 64
63 65 def read(self, fp):
64 66 try:
65 67 header = numpy.fromfile(fp, self.struct,1)
66 68 self.size = int(header['nSize'][0])
67 69 self.version = int(header['nVersion'][0])
68 70 self.dataBlock = int(header['nDataBlockId'][0])
69 71 self.utc = int(header['nUtime'][0])
70 72 self.miliSecond = int(header['nMilsec'][0])
71 73 self.timeZone = int(header['nTimezone'][0])
72 74 self.dstFlag = int(header['nDstflag'][0])
73 75 self.errorCount = int(header['nErrorCount'][0])
74 76
77 self.datatime = datetime.datetime.utcfromtimestamp(self.utc)
75 78 except Exception, e:
76 79 print "BasicHeader: " + e
77 80 return 0
78 81
79 82 return 1
80 83
81 84 def write(self, fp):
82 85 headerTuple = (self.size,self.version,self.dataBlock,self.utc,self.miliSecond,self.timeZone,self.dstFlag,self.errorCount)
83 86 header = numpy.array(headerTuple,self.struct)
84 87 header.tofile(fp)
85 88
86 89 return 1
87 90
88 91 class SystemHeader(Header):
89 92
90 93 size = None
91 94 nSamples = None
92 95 nProfiles = None
93 96 nChannels = None
94 97 adcResolution = None
95 98 pciDioBusWidth = None
96 99 struct = None
97 100
98 101 def __init__(self):
99 102 self.size = 0
100 103 self.nSamples = 0
101 104 self.nProfiles = 0
102 105 self.nChannels = 0
103 106 self.adcResolution = 0
104 107 self.pciDioBusWidth = 0
105 108 self.struct = numpy.dtype([
106 109 ('nSize','<u4'),
107 110 ('nNumSamples','<u4'),
108 111 ('nNumProfiles','<u4'),
109 112 ('nNumChannels','<u4'),
110 113 ('nADCResolution','<u4'),
111 114 ('nPCDIOBusWidth','<u4'),
112 115 ])
113 116
114 117
115 118 def read(self, fp):
116 119 try:
117 120 header = numpy.fromfile(fp,self.struct,1)
118 121 self.size = header['nSize'][0]
119 122 self.nSamples = header['nNumSamples'][0]
120 123 self.nProfiles = header['nNumProfiles'][0]
121 124 self.nChannels = header['nNumChannels'][0]
122 125 self.adcResolution = header['nADCResolution'][0]
123 126 self.pciDioBusWidth = header['nPCDIOBusWidth'][0]
124 127
125 128 except Exception, e:
126 129 print "SystemHeader: " + e
127 130 return 0
128 131
129 132 return 1
130 133
131 134 def write(self, fp):
132 135 headerTuple = (self.size,self.nSamples,self.nProfiles,self.nChannels,self.adcResolution,self.pciDioBusWidth)
133 136 header = numpy.array(headerTuple,self.struct)
134 137 header.tofile(fp)
135 138
136 139 return 1
137 140
138 141 class RadarControllerHeader(Header):
139 142
140 143 size = None
141 144 expType = None
142 145 nTx = None
143 146 ipp = None
144 147 txA = None
145 148 txB = None
146 149 nWindows = None
147 150 numTaus = None
148 151 codeType = None
149 152 line6Function = None
150 153 line5Function = None
151 154 fClock = None
152 155 prePulseBefore = None
153 156 prePulserAfter = None
154 157 rangeIpp = None
155 158 rangeTxA = None
156 159 rangeTxB = None
157 160 struct = None
158 161
159 162 def __init__(self):
160 163 self.size = 0
161 164 self.expType = 0
162 165 self.nTx = 0
163 166 self.ipp = 0
164 167 self.txA = 0
165 168 self.txB = 0
166 169 self.nWindows = 0
167 170 self.numTaus = 0
168 171 self.codeType = 0
169 172 self.line6Function = 0
170 173 self.line5Function = 0
171 174 self.fClock = 0
172 175 self.prePulseBefore = 0
173 176 self.prePulserAfter = 0
174 177 self.rangeIpp = 0
175 178 self.rangeTxA = 0
176 179 self.rangeTxB = 0
177 180 self.struct = numpy.dtype([
178 181 ('nSize','<u4'),
179 182 ('nExpType','<u4'),
180 183 ('nNTx','<u4'),
181 184 ('fIpp','<f4'),
182 185 ('fTxA','<f4'),
183 186 ('fTxB','<f4'),
184 187 ('nNumWindows','<u4'),
185 188 ('nNumTaus','<u4'),
186 189 ('nCodeType','<u4'),
187 190 ('nLine6Function','<u4'),
188 191 ('nLine5Function','<u4'),
189 192 ('fClock','<f4'),
190 193 ('nPrePulseBefore','<u4'),
191 194 ('nPrePulseAfter','<u4'),
192 195 ('sRangeIPP','<a20'),
193 196 ('sRangeTxA','<a20'),
194 197 ('sRangeTxB','<a20'),
195 198 ])
196 199
197 200 self.samplingWindowStruct = numpy.dtype([('h0','<f4'),('dh','<f4'),('nsa','<u4')])
198 201
199 202 self.samplingWindow = None
200 203 self.nHeights = None
201 204 self.firstHeight = None
202 205 self.deltaHeight = None
203 206 self.samplesWin = None
204 207
205 208 self.nCode = None
206 209 self.nBaud = None
207 210 self.code = None
208 211 self.flip1 = None
209 212 self.flip2 = None
210 213
211 214 self.dynamic = numpy.array([],numpy.dtype('byte'))
212 215
213 216
214 217 def read(self, fp):
215 218 try:
216 219 startFp = fp.tell()
217 220 header = numpy.fromfile(fp,self.struct,1)
218 221 self.size = int(header['nSize'][0])
219 222 self.expType = int(header['nExpType'][0])
220 223 self.nTx = int(header['nNTx'][0])
221 224 self.ipp = float(header['fIpp'][0])
222 225 self.txA = float(header['fTxA'][0])
223 226 self.txB = float(header['fTxB'][0])
224 227 self.nWindows = int(header['nNumWindows'][0])
225 228 self.numTaus = int(header['nNumTaus'][0])
226 229 self.codeType = int(header['nCodeType'][0])
227 230 self.line6Function = int(header['nLine6Function'][0])
228 231 self.line5Function = int(header['nLine5Function'][0])
229 232 self.fClock = float(header['fClock'][0])
230 233 self.prePulseBefore = int(header['nPrePulseBefore'][0])
231 234 self.prePulserAfter = int(header['nPrePulseAfter'][0])
232 235 self.rangeIpp = header['sRangeIPP'][0]
233 236 self.rangeTxA = header['sRangeTxA'][0]
234 237 self.rangeTxB = header['sRangeTxB'][0]
235 238 # jump Dynamic Radar Controller Header
236 239 jumpFp = self.size - 116
237 240 self.dynamic = numpy.fromfile(fp,numpy.dtype('byte'),jumpFp)
238 241 #pointer backward to dynamic header and read
239 242 backFp = fp.tell() - jumpFp
240 243 fp.seek(backFp)
241 244
242 245 self.samplingWindow = numpy.fromfile(fp,self.samplingWindowStruct,self.nWindows)
243 246 self.nHeights = int(numpy.sum(self.samplingWindow['nsa']))
244 247 self.firstHeight = self.samplingWindow['h0']
245 248 self.deltaHeight = self.samplingWindow['dh']
246 249 self.samplesWin = self.samplingWindow['nsa']
247 250
248 251 self.Taus = numpy.fromfile(fp,'<f4',self.numTaus)
249 252
250 253 if self.codeType != 0:
251 254 self.nCode = int(numpy.fromfile(fp,'<u4',1))
252 255 self.nBaud = int(numpy.fromfile(fp,'<u4',1))
253 256 self.code = numpy.empty([self.nCode,self.nBaud],dtype='u1')
254 257 tempList = []
255 258 for ic in range(self.nCode):
256 259 temp = numpy.fromfile(fp,'u1',4*int(numpy.ceil(self.nBaud/32.)))
257 260 tempList.append(temp)
258 261 self.code[ic] = numpy.unpackbits(temp[::-1])[-1*self.nBaud:]
259 262 self.code = 2.0*self.code - 1.0
260 263
261 264 if self.line5Function == RCfunction.FLIP:
262 265 self.flip1 = numpy.fromfile(fp,'<u4',1)
263 266
264 267 if self.line6Function == RCfunction.FLIP:
265 268 self.flip2 = numpy.fromfile(fp,'<u4',1)
266 269
267 270 endFp = self.size + startFp
268 271 jumpFp = endFp - fp.tell()
269 272 if jumpFp > 0:
270 273 fp.seek(jumpFp)
271 274
272 275 except Exception, e:
273 276 print "RadarControllerHeader: " + e
274 277 return 0
275 278
276 279 return 1
277 280
278 281 def write(self, fp):
279 282 headerTuple = (self.size,
280 283 self.expType,
281 284 self.nTx,
282 285 self.ipp,
283 286 self.txA,
284 287 self.txB,
285 288 self.nWindows,
286 289 self.numTaus,
287 290 self.codeType,
288 291 self.line6Function,
289 292 self.line5Function,
290 293 self.fClock,
291 294 self.prePulseBefore,
292 295 self.prePulserAfter,
293 296 self.rangeIpp,
294 297 self.rangeTxA,
295 298 self.rangeTxB)
296 299
297 300 header = numpy.array(headerTuple,self.struct)
298 301 header.tofile(fp)
299 302
300 303 dynamic = self.dynamic
301 304 dynamic.tofile(fp)
302 305
303 306 return 1
304 307
305 308
306 309
307 310 class ProcessingHeader(Header):
308 311
309 312 size = None
310 313 dtype = None
311 314 blockSize = None
312 315 profilesPerBlock = None
313 316 dataBlocksPerFile = None
314 317 nWindows = None
315 318 processFlags = None
316 319 nCohInt = None
317 320 nIncohInt = None
318 321 totalSpectra = None
319 322 struct = None
320 323 flag_dc = None
321 324 flag_cspc = None
322 325
323 326 def __init__(self):
324 327 self.size = 0
325 328 self.dtype = 0
326 329 self.blockSize = 0
327 330 self.profilesPerBlock = 0
328 331 self.dataBlocksPerFile = 0
329 332 self.nWindows = 0
330 333 self.processFlags = 0
331 334 self.nCohInt = 0
332 335 self.nIncohInt = 0
333 336 self.totalSpectra = 0
334 337 self.struct = numpy.dtype([
335 338 ('nSize','<u4'),
336 339 ('nDataType','<u4'),
337 340 ('nSizeOfDataBlock','<u4'),
338 341 ('nProfilesperBlock','<u4'),
339 342 ('nDataBlocksperFile','<u4'),
340 343 ('nNumWindows','<u4'),
341 344 ('nProcessFlags','<u4'),
342 345 ('nCoherentIntegrations','<u4'),
343 346 ('nIncoherentIntegrations','<u4'),
344 347 ('nTotalSpectra','<u4')
345 348 ])
346 349 self.samplingWindow = 0
347 350 self.structSamplingWindow = numpy.dtype([('h0','<f4'),('dh','<f4'),('nsa','<u4')])
348 351 self.nHeights = 0
349 352 self.firstHeight = 0
350 353 self.deltaHeight = 0
351 354 self.samplesWin = 0
352 355 self.spectraComb = 0
353 356 self.nCode = None
354 357 self.code = None
355 358 self.nBaud = None
356 359 self.shif_fft = False
357 360 self.flag_dc = False
358 361 self.flag_cspc = False
359 362
360 363 def read(self, fp):
361 364 try:
362 365 header = numpy.fromfile(fp,self.struct,1)
363 366 self.size = int(header['nSize'][0])
364 367 self.dtype = int(header['nDataType'][0])
365 368 self.blockSize = int(header['nSizeOfDataBlock'][0])
366 369 self.profilesPerBlock = int(header['nProfilesperBlock'][0])
367 370 self.dataBlocksPerFile = int(header['nDataBlocksperFile'][0])
368 371 self.nWindows = int(header['nNumWindows'][0])
369 372 self.processFlags = int(header['nProcessFlags'])
370 373 self.nCohInt = int(header['nCoherentIntegrations'][0])
371 374 self.nIncohInt = int(header['nIncoherentIntegrations'][0])
372 375 self.totalSpectra = int(header['nTotalSpectra'][0])
373 376 self.samplingWindow = numpy.fromfile(fp,self.structSamplingWindow,self.nWindows)
374 377 self.nHeights = int(numpy.sum(self.samplingWindow['nsa']))
375 378 self.firstHeight = float(self.samplingWindow['h0'][0])
376 379 self.deltaHeight = float(self.samplingWindow['dh'][0])
377 380 self.samplesWin = self.samplingWindow['nsa']
378 381 self.spectraComb = numpy.fromfile(fp,'u1',2*self.totalSpectra)
379 382
380 383 if ((self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE) == PROCFLAG.DEFINE_PROCESS_CODE):
381 384 self.nCode = int(numpy.fromfile(fp,'<u4',1))
382 385 self.nBaud = int(numpy.fromfile(fp,'<u4',1))
383 386 self.code = numpy.fromfile(fp,'<f4',self.nCode*self.nBaud).reshape(self.nBaud,self.nCode)
384 387
385 388 if ((self.processFlags & PROCFLAG.SHIFT_FFT_DATA) == PROCFLAG.SHIFT_FFT_DATA):
386 389 self.shif_fft = True
387 390 else:
388 391 self.shif_fft = False
389 392
390 393 if ((self.processFlags & PROCFLAG.SAVE_CHANNELS_DC) == PROCFLAG.SAVE_CHANNELS_DC):
391 394 self.flag_dc = True
392 395
393 396 nChannels = 0
394 397 nPairs = 0
395 398 pairList = []
396 399
397 400 for i in range( 0, self.totalSpectra*2, 2 ):
398 401 if self.spectraComb[i] == self.spectraComb[i+1]:
399 402 nChannels = nChannels + 1 #par de canales iguales
400 403 else:
401 404 nPairs = nPairs + 1 #par de canales diferentes
402 405 pairList.append( (self.spectraComb[i], self.spectraComb[i+1]) )
403 406
404 407 self.flag_cspc = False
405 408 if nPairs > 0:
406 409 self.flag_cspc = True
407 410
408 411 except Exception, e:
409 412 print "ProcessingHeader: " + e
410 413 return 0
411 414
412 415 return 1
413 416
414 417 def write(self, fp):
415 418 headerTuple = (self.size,
416 419 self.dtype,
417 420 self.blockSize,
418 421 self.profilesPerBlock,
419 422 self.dataBlocksPerFile,
420 423 self.nWindows,
421 424 self.processFlags,
422 425 self.nCohInt,
423 426 self.nIncohInt,
424 427 self.totalSpectra)
425 428
426 429 header = numpy.array(headerTuple,self.struct)
427 430 header.tofile(fp)
428 431
429 432 if self.nWindows != 0:
430 433 sampleWindowTuple = (self.firstHeight,self.deltaHeight,self.samplesWin)
431 434 samplingWindow = numpy.array(sampleWindowTuple,self.structSamplingWindow)
432 435 samplingWindow.tofile(fp)
433 436
434 437
435 438 if self.totalSpectra != 0:
436 439 spectraComb = numpy.array([],numpy.dtype('u1'))
437 440 spectraComb = self.spectraComb
438 441 spectraComb.tofile(fp)
439 442
440 443
441 444 if self.processFlags & PROCFLAG.DEFINE_PROCESS_CODE == PROCFLAG.DEFINE_PROCESS_CODE:
442 445 nCode = self.nCode #Probar con un dato que almacene codigo, hasta el momento no se hizo la prueba
443 446 nCode.tofile(fp)
444 447
445 448 nBaud = self.nBaud
446 449 nBaud.tofile(fp)
447 450
448 451 code = self.code.reshape(nCode*nBaud)
449 452 code.tofile(fp)
450 453
451 454 return 1
452 455
453 456 class RCfunction:
454 457 NONE=0
455 458 FLIP=1
456 459 CODE=2
457 460 SAMPLING=3
458 461 LIN6DIV256=4
459 462 SYNCHRO=5
460 463
461 464 class nCodeType:
462 465 NONE=0
463 466 USERDEFINE=1
464 467 BARKER2=2
465 468 BARKER3=3
466 469 BARKER4=4
467 470 BARKER5=5
468 471 BARKER7=6
469 472 BARKER11=7
470 473 BARKER13=8
471 474 AC128=9
472 475 COMPLEMENTARYCODE2=10
473 476 COMPLEMENTARYCODE4=11
474 477 COMPLEMENTARYCODE8=12
475 478 COMPLEMENTARYCODE16=13
476 479 COMPLEMENTARYCODE32=14
477 480 COMPLEMENTARYCODE64=15
478 481 COMPLEMENTARYCODE128=16
479 482 CODE_BINARY28=17
480 483
481 484 class PROCFLAG:
482 485 COHERENT_INTEGRATION = numpy.uint32(0x00000001)
483 486 DECODE_DATA = numpy.uint32(0x00000002)
484 487 SPECTRA_CALC = numpy.uint32(0x00000004)
485 488 INCOHERENT_INTEGRATION = numpy.uint32(0x00000008)
486 489 POST_COHERENT_INTEGRATION = numpy.uint32(0x00000010)
487 490 SHIFT_FFT_DATA = numpy.uint32(0x00000020)
488 491
489 492 DATATYPE_CHAR = numpy.uint32(0x00000040)
490 493 DATATYPE_SHORT = numpy.uint32(0x00000080)
491 494 DATATYPE_LONG = numpy.uint32(0x00000100)
492 495 DATATYPE_INT64 = numpy.uint32(0x00000200)
493 496 DATATYPE_FLOAT = numpy.uint32(0x00000400)
494 497 DATATYPE_DOUBLE = numpy.uint32(0x00000800)
495 498
496 499 DATAARRANGE_CONTIGUOUS_CH = numpy.uint32(0x00001000)
497 500 DATAARRANGE_CONTIGUOUS_H = numpy.uint32(0x00002000)
498 501 DATAARRANGE_CONTIGUOUS_P = numpy.uint32(0x00004000)
499 502
500 503 SAVE_CHANNELS_DC = numpy.uint32(0x00008000)
501 504 DEFLIP_DATA = numpy.uint32(0x00010000)
502 505 DEFINE_PROCESS_CODE = numpy.uint32(0x00020000)
503 506
504 507 ACQ_SYS_NATALIA = numpy.uint32(0x00040000)
505 508 ACQ_SYS_ECHOTEK = numpy.uint32(0x00080000)
506 509 ACQ_SYS_ADRXD = numpy.uint32(0x000C0000)
507 510 ACQ_SYS_JULIA = numpy.uint32(0x00100000)
508 511 ACQ_SYS_XXXXXX = numpy.uint32(0x00140000)
509 512
510 513 EXP_NAME_ESP = numpy.uint32(0x00200000)
511 514 CHANNEL_NAMES_ESP = numpy.uint32(0x00400000)
512 515
513 516 OPERATION_MASK = numpy.uint32(0x0000003F)
514 517 DATATYPE_MASK = numpy.uint32(0x00000FC0)
515 518 DATAARRANGE_MASK = numpy.uint32(0x00007000)
516 519 ACQ_SYS_MASK = numpy.uint32(0x001C0000) No newline at end of file
@@ -1,586 +1,589
1 1 import numpy
2 2 import time, datetime
3 3 from graphics.figure import *
4 4
5 5 class CrossSpectraPlot(Figure):
6 6
7 7 __isConfig = None
8 8 __nsubplots = None
9 9
10 10 WIDTHPROF = None
11 11 HEIGHTPROF = None
12 12 PREFIX = 'spc'
13 13
14 14 def __init__(self):
15 15
16 16 self.__isConfig = False
17 17 self.__nsubplots = 4
18 18
19 19 self.WIDTH = 300
20 20 self.HEIGHT = 400
21 21 self.WIDTHPROF = 0
22 22 self.HEIGHTPROF = 0
23 23
24 24 def getSubplots(self):
25 25
26 26 ncol = 4
27 27 nrow = self.nplots
28 28
29 29 return nrow, ncol
30 30
31 31 def setup(self, idfigure, nplots, wintitle, showprofile=True):
32 32
33 33 self.__showprofile = showprofile
34 34 self.nplots = nplots
35 35
36 36 ncolspan = 1
37 37 colspan = 1
38 38
39 39 self.createFigure(idfigure = idfigure,
40 40 wintitle = wintitle,
41 41 widthplot = self.WIDTH + self.WIDTHPROF,
42 42 heightplot = self.HEIGHT + self.HEIGHTPROF)
43 43
44 44 nrow, ncol = self.getSubplots()
45 45
46 46 counter = 0
47 47 for y in range(nrow):
48 48 for x in range(ncol):
49 49 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
50 50
51 51 counter += 1
52 52
53 53 def run(self, dataOut, idfigure, wintitle="", pairsList=None, showprofile='True',
54 54 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
55 55 save=False, figpath='./', figfile=None):
56 56
57 57 """
58 58
59 59 Input:
60 60 dataOut :
61 61 idfigure :
62 62 wintitle :
63 63 channelList :
64 64 showProfile :
65 65 xmin : None,
66 66 xmax : None,
67 67 ymin : None,
68 68 ymax : None,
69 69 zmin : None,
70 70 zmax : None
71 71 """
72 72
73 73 if pairsList == None:
74 74 pairsIndexList = dataOut.pairsIndexList
75 75 else:
76 76 pairsIndexList = []
77 77 for pair in pairsList:
78 78 if pair not in dataOut.pairsList:
79 79 raise ValueError, "Pair %s is not in dataOut.pairsList" %(pair)
80 80 pairsIndexList.append(dataOut.pairsList.index(pair))
81 81
82 if pairIndexList == []:
83 return
84
82 85 x = dataOut.getVelRange(1)
83 86 y = dataOut.getHeiRange()
84 87 z = 10.*numpy.log10(dataOut.data_spc[:,:,:])
85 88 avg = numpy.average(numpy.abs(z), axis=1)
86 89
87 90 noise = dataOut.getNoise()
88 91
89 92 if not self.__isConfig:
90 93
91 94 nplots = len(pairsIndexList)
92 95
93 96 self.setup(idfigure=idfigure,
94 97 nplots=nplots,
95 98 wintitle=wintitle,
96 99 showprofile=showprofile)
97 100
98 101 if xmin == None: xmin = numpy.nanmin(x)
99 102 if xmax == None: xmax = numpy.nanmax(x)
100 103 if ymin == None: ymin = numpy.nanmin(y)
101 104 if ymax == None: ymax = numpy.nanmax(y)
102 105 if zmin == None: zmin = numpy.nanmin(avg)*0.9
103 106 if zmax == None: zmax = numpy.nanmax(avg)*0.9
104 107
105 108 self.__isConfig = True
106 109
107 thisDatetime = datetime.datetime.fromtimestamp(dataOut.utctime)
110 thisDatetime = dataOut.datatime
108 111 title = "Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
109 112 xlabel = "Velocity (m/s)"
110 113 ylabel = "Range (Km)"
111 114
112 115 self.setWinTitle(title)
113 116
114 117 for i in range(self.nplots):
115 118 pair = dataOut.pairsList[pairsIndexList[i]]
116 119
117 120 title = "Channel %d: %4.2fdB" %(pair[0], noise[pair[0]])
118 121 z = 10.*numpy.log10(dataOut.data_spc[pair[0],:,:])
119 122 axes0 = self.axesList[i*self.__nsubplots]
120 123 axes0.pcolor(x, y, z,
121 124 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
122 125 xlabel=xlabel, ylabel=ylabel, title=title,
123 126 ticksize=9, cblabel='')
124 127
125 128 title = "Channel %d: %4.2fdB" %(pair[1], noise[pair[1]])
126 129 z = 10.*numpy.log10(dataOut.data_spc[pair[1],:,:])
127 130 axes0 = self.axesList[i*self.__nsubplots+1]
128 131 axes0.pcolor(x, y, z,
129 132 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
130 133 xlabel=xlabel, ylabel=ylabel, title=title,
131 134 ticksize=9, cblabel='')
132 135
133 136 coherenceComplex = dataOut.data_cspc[pairsIndexList[i],:,:]/numpy.sqrt(dataOut.data_spc[pair[0],:,:]*dataOut.data_spc[pair[1],:,:])
134 137 coherence = numpy.abs(coherenceComplex)
135 138 phase = numpy.arctan(-1*coherenceComplex.imag/coherenceComplex.real)*180/numpy.pi
136 139
137 140
138 141 title = "Coherence %d%d" %(pair[0], pair[1])
139 142 axes0 = self.axesList[i*self.__nsubplots+2]
140 143 axes0.pcolor(x, y, coherence,
141 144 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=-1, zmax=1,
142 145 xlabel=xlabel, ylabel=ylabel, title=title,
143 146 ticksize=9, cblabel='')
144 147
145 148 title = "Phase %d%d" %(pair[0], pair[1])
146 149 axes0 = self.axesList[i*self.__nsubplots+3]
147 150 axes0.pcolor(x, y, phase,
148 151 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=-180, zmax=180,
149 152 xlabel=xlabel, ylabel=ylabel, title=title,
150 153 ticksize=9, cblabel='')
151 154
152 155
153 156
154 157 self.draw()
155 158
156 159 if save:
157 160 date = thisDatetime.strftime("%Y%m%d")
158 161 if figfile == None:
159 162 figfile = self.getFilename(name = date)
160 163
161 164 self.saveFigure(figpath, figfile)
162 165
163 166
164 167 class RTIPlot(Figure):
165 168
166 169 __isConfig = None
167 170 __nsubplots = None
168 171
169 172 WIDTHPROF = None
170 173 HEIGHTPROF = None
171 174 PREFIX = 'rti'
172 175
173 176 def __init__(self):
174 177
175 178 self.__timerange = 24*60*60
176 179 self.__isConfig = False
177 180 self.__nsubplots = 1
178 181
179 182 self.WIDTH = 800
180 183 self.HEIGHT = 200
181 184 self.WIDTHPROF = 120
182 185 self.HEIGHTPROF = 0
183 186
184 187 def getSubplots(self):
185 188
186 189 ncol = 1
187 190 nrow = self.nplots
188 191
189 192 return nrow, ncol
190 193
191 194 def setup(self, idfigure, nplots, wintitle, showprofile=True):
192 195
193 196 self.__showprofile = showprofile
194 197 self.nplots = nplots
195 198
196 199 ncolspan = 1
197 200 colspan = 1
198 201 if showprofile:
199 202 ncolspan = 7
200 203 colspan = 6
201 204 self.__nsubplots = 2
202 205
203 206 self.createFigure(idfigure = idfigure,
204 207 wintitle = wintitle,
205 208 widthplot = self.WIDTH + self.WIDTHPROF,
206 209 heightplot = self.HEIGHT + self.HEIGHTPROF)
207 210
208 211 nrow, ncol = self.getSubplots()
209 212
210 213 counter = 0
211 214 for y in range(nrow):
212 215 for x in range(ncol):
213 216
214 217 if counter >= self.nplots:
215 218 break
216 219
217 220 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
218 221
219 222 if showprofile:
220 223 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
221 224
222 225 counter += 1
223 226
224 227 def __getTimeLim(self, x, xmin, xmax):
225 228
226 thisdatetime = datetime.datetime.fromtimestamp(numpy.min(x))
229 thisdatetime = datetime.datetime.utcfromtimestamp(numpy.min(x))
227 230 thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0))
228 231
229 232 ####################################################
230 233 #If the x is out of xrange
231 234 if xmax < (thisdatetime - thisdate).seconds/(60*60.):
232 235 xmin = None
233 236 xmax = None
234 237
235 238 if xmin == None:
236 239 td = thisdatetime - thisdate
237 240 xmin = td.seconds/(60*60.)
238 241
239 242 if xmax == None:
240 243 xmax = xmin + self.__timerange/(60*60.)
241 244
242 245 mindt = thisdate + datetime.timedelta(0,0,0,0,0, xmin)
243 246 tmin = time.mktime(mindt.timetuple())
244 247
245 248 maxdt = thisdate + datetime.timedelta(0,0,0,0,0, xmax)
246 249 tmax = time.mktime(maxdt.timetuple())
247 250
248 251 self.__timerange = tmax - tmin
249 252
250 253 return tmin, tmax
251 254
252 255 def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True',
253 256 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
254 257 timerange=None,
255 258 save=False, figpath='./', figfile=None):
256 259
257 260 """
258 261
259 262 Input:
260 263 dataOut :
261 264 idfigure :
262 265 wintitle :
263 266 channelList :
264 267 showProfile :
265 268 xmin : None,
266 269 xmax : None,
267 270 ymin : None,
268 271 ymax : None,
269 272 zmin : None,
270 273 zmax : None
271 274 """
272 275
273 276 if channelList == None:
274 277 channelIndexList = dataOut.channelIndexList
275 278 else:
276 279 channelIndexList = []
277 280 for channel in channelList:
278 281 if channel not in dataOut.channelList:
279 282 raise ValueError, "Channel %d is not in dataOut.channelList"
280 283 channelIndexList.append(dataOut.channelList.index(channel))
281 284
282 285 if timerange != None:
283 286 self.__timerange = timerange
284 287
285 288 tmin = None
286 289 tmax = None
287 x = dataOut.getDatatime()
290 x = dataOut.getTimeRange()
288 291 y = dataOut.getHeiRange()
289 292 z = 10.*numpy.log10(dataOut.data_spc[channelIndexList,:,:])
290 293 avg = numpy.average(z, axis=1)
291 294
292 295 noise = dataOut.getNoise()
293 296
294 297 if not self.__isConfig:
295 298
296 299 nplots = len(channelIndexList)
297 300
298 301 self.setup(idfigure=idfigure,
299 302 nplots=nplots,
300 303 wintitle=wintitle,
301 304 showprofile=showprofile)
302 305
303 306 tmin, tmax = self.__getTimeLim(x, xmin, xmax)
304 307 if ymin == None: ymin = numpy.nanmin(y)
305 308 if ymax == None: ymax = numpy.nanmax(y)
306 309 if zmin == None: zmin = numpy.nanmin(avg)*0.9
307 310 if zmax == None: zmax = numpy.nanmax(avg)*0.9
308 311
309 312 self.__isConfig = True
310 313
311 thisDatetime = datetime.datetime.fromtimestamp(dataOut.utctime)
314 thisDatetime = dataOut.datatime
312 315 title = "RTI: %s" %(thisDatetime.strftime("%d-%b-%Y"))
313 316 xlabel = "Velocity (m/s)"
314 317 ylabel = "Range (Km)"
315 318
316 319 self.setWinTitle(title)
317 320
318 321 for i in range(self.nplots):
319 322 title = "Channel %d: %s" %(dataOut.channelList[i], thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
320 323 axes = self.axesList[i*self.__nsubplots]
321 324 z = avg[i].reshape((1,-1))
322 325 axes.pcolor(x, y, z,
323 326 xmin=tmin, xmax=tmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
324 327 xlabel=xlabel, ylabel=ylabel, title=title, rti=True, XAxisAsTime=True,
325 328 ticksize=9, cblabel='', cbsize="1%")
326 329
327 330 if self.__showprofile:
328 331 axes = self.axesList[i*self.__nsubplots +1]
329 332 axes.pline(avg[i], y,
330 333 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
331 334 xlabel='dB', ylabel='', title='',
332 335 ytick_visible=False,
333 336 grid='x')
334 337
335 338 self.draw()
336 339
337 340 if save:
338 341 date = thisDatetime.strftime("%Y%m%d")
339 342 if figfile == None:
340 343 figfile = self.getFilename(name = date)
341 344
342 345 self.saveFigure(figpath, figfile)
343 346
344 347 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
345 348 self.__isConfig = False
346 349
347 350 class SpectraPlot(Figure):
348 351
349 352 __isConfig = None
350 353 __nsubplots = None
351 354
352 355 WIDTHPROF = None
353 356 HEIGHTPROF = None
354 357 PREFIX = 'spc'
355 358
356 359 def __init__(self):
357 360
358 361 self.__isConfig = False
359 362 self.__nsubplots = 1
360 363
361 364 self.WIDTH = 300
362 365 self.HEIGHT = 400
363 366 self.WIDTHPROF = 120
364 367 self.HEIGHTPROF = 0
365 368
366 369 def getSubplots(self):
367 370
368 371 ncol = int(numpy.sqrt(self.nplots)+0.9)
369 372 nrow = int(self.nplots*1./ncol + 0.9)
370 373
371 374 return nrow, ncol
372 375
373 376 def setup(self, idfigure, nplots, wintitle, showprofile=True):
374 377
375 378 self.__showprofile = showprofile
376 379 self.nplots = nplots
377 380
378 381 ncolspan = 1
379 382 colspan = 1
380 383 if showprofile:
381 384 ncolspan = 3
382 385 colspan = 2
383 386 self.__nsubplots = 2
384 387
385 388 self.createFigure(idfigure = idfigure,
386 389 wintitle = wintitle,
387 390 widthplot = self.WIDTH + self.WIDTHPROF,
388 391 heightplot = self.HEIGHT + self.HEIGHTPROF)
389 392
390 393 nrow, ncol = self.getSubplots()
391 394
392 395 counter = 0
393 396 for y in range(nrow):
394 397 for x in range(ncol):
395 398
396 399 if counter >= self.nplots:
397 400 break
398 401
399 402 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan, colspan, 1)
400 403
401 404 if showprofile:
402 405 self.addAxes(nrow, ncol*ncolspan, y, x*ncolspan+colspan, 1, 1)
403 406
404 407 counter += 1
405 408
406 409 def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True',
407 410 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
408 411 save=False, figpath='./', figfile=None):
409 412
410 413 """
411 414
412 415 Input:
413 416 dataOut :
414 417 idfigure :
415 418 wintitle :
416 419 channelList :
417 420 showProfile :
418 421 xmin : None,
419 422 xmax : None,
420 423 ymin : None,
421 424 ymax : None,
422 425 zmin : None,
423 426 zmax : None
424 427 """
425 428
426 429 if channelList == None:
427 430 channelIndexList = dataOut.channelIndexList
428 431 else:
429 432 channelIndexList = []
430 433 for channel in channelList:
431 434 if channel not in dataOut.channelList:
432 435 raise ValueError, "Channel %d is not in dataOut.channelList"
433 436 channelIndexList.append(dataOut.channelList.index(channel))
434 437
435 438 x = dataOut.getVelRange(1)
436 439 y = dataOut.getHeiRange()
437 440 z = 10.*numpy.log10(dataOut.data_spc[channelIndexList,:,:])
438 441 avg = numpy.average(z, axis=1)
439 442
440 443 noise = dataOut.getNoise()
441 444
442 445 if not self.__isConfig:
443 446
444 447 nplots = len(channelIndexList)
445 448
446 449 self.setup(idfigure=idfigure,
447 450 nplots=nplots,
448 451 wintitle=wintitle,
449 452 showprofile=showprofile)
450 453
451 454 if xmin == None: xmin = numpy.nanmin(x)
452 455 if xmax == None: xmax = numpy.nanmax(x)
453 456 if ymin == None: ymin = numpy.nanmin(y)
454 457 if ymax == None: ymax = numpy.nanmax(y)
455 458 if zmin == None: zmin = numpy.nanmin(avg)*0.9
456 459 if zmax == None: zmax = numpy.nanmax(avg)*0.9
457 460
458 461 self.__isConfig = True
459 462
460 thisDatetime = datetime.datetime.fromtimestamp(dataOut.utctime)
463 thisDatetime = dataOut.datatime
461 464 title = "Spectra: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
462 465 xlabel = "Velocity (m/s)"
463 466 ylabel = "Range (Km)"
464 467
465 468 self.setWinTitle(title)
466 469
467 470 for i in range(self.nplots):
468 471 title = "Channel %d: %4.2fdB" %(dataOut.channelList[i], noise[i])
469 472 axes = self.axesList[i*self.__nsubplots]
470 473 axes.pcolor(x, y, z[i,:,:],
471 474 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, zmin=zmin, zmax=zmax,
472 475 xlabel=xlabel, ylabel=ylabel, title=title,
473 476 ticksize=9, cblabel='')
474 477
475 478 if self.__showprofile:
476 479 axes = self.axesList[i*self.__nsubplots +1]
477 480 axes.pline(avg[i], y,
478 481 xmin=zmin, xmax=zmax, ymin=ymin, ymax=ymax,
479 482 xlabel='dB', ylabel='', title='',
480 483 ytick_visible=False,
481 484 grid='x')
482 485
483 486 self.draw()
484 487
485 488 if save:
486 489 date = thisDatetime.strftime("%Y%m%d")
487 490 if figfile == None:
488 491 figfile = self.getFilename(name = date)
489 492
490 493 self.saveFigure(figpath, figfile)
491 494
492 495 class Scope(Figure):
493 496
494 497 __isConfig = None
495 498
496 499 def __init__(self):
497 500
498 501 self.__isConfig = False
499 502 self.WIDTH = 600
500 503 self.HEIGHT = 200
501 504
502 505 def getSubplots(self):
503 506
504 507 nrow = self.nplots
505 508 ncol = 3
506 509 return nrow, ncol
507 510
508 511 def setup(self, idfigure, nplots, wintitle):
509 512
510 513 self.createFigure(idfigure, wintitle)
511 514
512 515 nrow,ncol = self.getSubplots()
513 516 colspan = 3
514 517 rowspan = 1
515 518
516 519 for i in range(nplots):
517 520 self.addAxes(nrow, ncol, i, 0, colspan, rowspan)
518 521
519 522 self.nplots = nplots
520 523
521 524 def run(self, dataOut, idfigure, wintitle="", channelList=None,
522 525 xmin=None, xmax=None, ymin=None, ymax=None, save=False, filename=None):
523 526
524 527 """
525 528
526 529 Input:
527 530 dataOut :
528 531 idfigure :
529 532 wintitle :
530 533 channelList :
531 534 xmin : None,
532 535 xmax : None,
533 536 ymin : None,
534 537 ymax : None,
535 538 """
536 539
537 540 if channelList == None:
538 541 channelIndexList = dataOut.channelIndexList
539 542 else:
540 543 channelIndexList = []
541 544 for channel in channelList:
542 545 if channel not in dataOut.channelList:
543 546 raise ValueError, "Channel %d is not in dataOut.channelList"
544 547 channelIndexList.append(dataOut.channelList.index(channel))
545 548
546 549 x = dataOut.heightList
547 550 y = dataOut.data[channelList,:] * numpy.conjugate(dataOut.data[channelList,:])
548 551 y = y.real
549 552
550 553 noise = dataOut.getNoise()
551 554
552 555 if not self.__isConfig:
553 556 nplots = len(channelList)
554 557
555 558 self.setup(idfigure=idfigure,
556 559 nplots=nplots,
557 560 wintitle=wintitle)
558 561
559 562 if xmin == None: xmin = numpy.nanmin(x)
560 563 if xmax == None: xmax = numpy.nanmax(x)
561 564 if ymin == None: ymin = numpy.nanmin(y)
562 565 if ymax == None: ymax = numpy.nanmax(y)
563 566
564 567 self.__isConfig = True
565 568
566 569
567 thisDatetime = datetime.datetime.fromtimestamp(dataOut.utctime)
570 thisDatetime = dataOut.datatime
568 571 title = "Scope: %s" %(thisDatetime.strftime("%d-%b-%Y %H:%M:%S"))
569 572 xlabel = "Range (Km)"
570 573 ylabel = "Intensity"
571 574
572 575 self.setWinTitle(title)
573 576
574 577 for i in range(len(self.axesList)):
575 578 title = "Channel %d: %4.2fdB" %(i, noise[i])
576 579 axes = self.axesList[i]
577 580 ychannel = y[i,:]
578 581 axes.pline(x, ychannel,
579 582 xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
580 583 xlabel=xlabel, ylabel=ylabel, title=title)
581 584
582 585 self.draw()
583 586
584 587 if save:
585 588 self.saveFigure(filename)
586 589 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now