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