From 77188df611db35ef46ad7e4a3dbcca01baa7ad87 2012-11-29 22:53:10 From: Miguel Valdez Date: 2012-11-29 22:53:10 Subject: [PATCH] Controlador de Signal Chain finalizado. -Lee el archivo XML -Crea los objetos de lectura y procesmaiento -Conecta los objetos de lectura y procesmaiento -Ejecuta los objetos de lectura y procesmaiento --- diff --git a/schainpy/graphics/__init__.py b/schainpy/model/graphics/__init__.py similarity index 100% rename from schainpy/graphics/__init__.py rename to schainpy/model/graphics/__init__.py diff --git a/schainpy/graphics/figure.py b/schainpy/model/graphics/figure.py similarity index 100% rename from schainpy/graphics/figure.py rename to schainpy/model/graphics/figure.py diff --git a/schainpy/graphics/mpldriver.py b/schainpy/model/graphics/mpldriver.py similarity index 97% rename from schainpy/graphics/mpldriver.py rename to schainpy/model/graphics/mpldriver.py index 0da5624..5f9c895 100644 --- a/schainpy/graphics/mpldriver.py +++ b/schainpy/model/graphics/mpldriver.py @@ -1,7 +1,7 @@ import matplotlib -matplotlib.use("TKAgg") +matplotlib.use("Agg") import matplotlib.pyplot -import scitools.numpyutils +#import scitools.numpyutils from mpl_toolkits.axes_grid1 import make_axes_locatable def init(idfigure, wintitle, width, height): diff --git a/schainpy/model/jrodataIO.py b/schainpy/model/jrodataIO.py index f3ed588..e3e46c7 100644 --- a/schainpy/model/jrodataIO.py +++ b/schainpy/model/jrodataIO.py @@ -13,6 +13,7 @@ import time, datetime from jrodata import * from jroheaderIO import * +from jroprocessing import * def isNumber(str): """ @@ -236,7 +237,7 @@ class JRODataIO: return self.dataOut -class JRODataReader(JRODataIO): +class JRODataReader(JRODataIO, ProcessingUnit): nReadBlocks = 0 @@ -420,79 +421,7 @@ class JRODataReader(JRODataIO): return directory, filename, year, doy, set - def setup(self, - path=None, - startDate=None, - endDate=None, - startTime=datetime.time(0,0,0), - endTime=datetime.time(23,59,59), - set=0, - expLabel = "", - ext = None, - online = False, - delay = 60): - if path == None: - raise ValueError, "The path is not valid" - - if ext == None: - ext = self.ext - - if online: - print "Searching files in online mode..." - doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext) - - if not(doypath): - for nTries in range( self.nTries ): - print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1) - time.sleep( self.delay ) - doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=exp) - if doypath: - break - - if not(doypath): - print "There 'isn't valied files in %s" % path - return None - - self.year = year - self.doy = doy - self.set = set - 1 - self.path = path - - else: - print "Searching files in offline mode ..." - pathList, filenameList = self.__searchFilesOffLine(path, startDate, endDate, startTime, endTime, set, expLabel, ext) - - if not(pathList): - print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path, - datetime.datetime.combine(startDate,startTime).ctime(), - datetime.datetime.combine(endDate,endTime).ctime()) - - sys.exit(-1) - - - self.fileIndex = -1 - self.pathList = pathList - self.filenameList = filenameList - - self.online = online - self.delay = delay - ext = ext.lower() - self.ext = ext - - if not(self.setNextFile()): - if (startDate!=None) and (endDate!=None): - print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) - elif startDate != None: - print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime()) - else: - print "No files" - - sys.exit(-1) - -# self.updateDataHeader() - - return self.dataOut def __setNextFileOffline(self): @@ -766,6 +695,80 @@ class JRODataReader(JRODataIO): return True + def setup(self, + path=None, + startDate=None, + endDate=None, + startTime=datetime.time(0,0,0), + endTime=datetime.time(23,59,59), + set=0, + expLabel = "", + ext = None, + online = False, + delay = 60): + + if path == None: + raise ValueError, "The path is not valid" + + if ext == None: + ext = self.ext + + if online: + print "Searching files in online mode..." + doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=ext) + + if not(doypath): + for nTries in range( self.nTries ): + print '\tWaiting %0.2f sec for an valid file in %s: try %02d ...' % (self.delay, path, nTries+1) + time.sleep( self.delay ) + doypath, file, year, doy, set = self.__searchFilesOnLine(path=path, expLabel=expLabel, ext=exp) + if doypath: + break + + if not(doypath): + print "There 'isn't valied files in %s" % path + return None + + self.year = year + self.doy = doy + self.set = set - 1 + self.path = path + + else: + print "Searching files in offline mode ..." + pathList, filenameList = self.__searchFilesOffLine(path, startDate, endDate, startTime, endTime, set, expLabel, ext) + + if not(pathList): + print "No *%s files into the folder %s \nfor the range: %s - %s"%(ext, path, + datetime.datetime.combine(startDate,startTime).ctime(), + datetime.datetime.combine(endDate,endTime).ctime()) + + sys.exit(-1) + + + self.fileIndex = -1 + self.pathList = pathList + self.filenameList = filenameList + + self.online = online + self.delay = delay + ext = ext.lower() + self.ext = ext + + if not(self.setNextFile()): + if (startDate!=None) and (endDate!=None): + print "No files in range: %s - %s" %(datetime.datetime.combine(startDate,startTime).ctime(), datetime.datetime.combine(endDate,endTime).ctime()) + elif startDate != None: + print "No files in range: %s" %(datetime.datetime.combine(startDate,startTime).ctime()) + else: + print "No files" + + sys.exit(-1) + +# self.updateDataHeader() + + return self.dataOut + def getData(): pass @@ -785,7 +788,7 @@ class JRODataReader(JRODataIO): self.getData() -class JRODataWriter(JRODataIO): +class JRODataWriter(JRODataIO, Operation): """ Esta clase permite escribir datos a archivos procesados (.r o ,pdata). La escritura diff --git a/schainpy/model/jroprocessing.py b/schainpy/model/jroprocessing.py index 2013381..1760fa0 100644 --- a/schainpy/model/jroprocessing.py +++ b/schainpy/model/jroprocessing.py @@ -36,6 +36,10 @@ class ProcessingUnit: self.objectDict = {} + def init(self): + + raise ValueError, "Not implemented" + def addOperation(self, object, objId): """ @@ -79,6 +83,13 @@ class ProcessingUnit: **kwargs : diccionario con los nombres y valores de la funcion a ejecutar. """ + if name != 'run': + + if name == 'init' and self.dataIn.isEmpty(): + return + + if name != 'init' and self.dataOut.isEmpty(): + return methodToCall = getattr(self, name) @@ -100,6 +111,9 @@ class ProcessingUnit: None """ + if self.dataOut.isEmpty(): + return + object = self.objectDict[objId] object.run(self.dataOut, **kwargs) @@ -128,8 +142,6 @@ class ProcessingUnit: Operation : Objeto del tipo operacion con los atributos: name, type y id. """ - if self.dataIn.isEmpty(): - return None if operationConf.type == 'self': self.callMethod(operationConf.name, **kwargs) @@ -227,6 +239,7 @@ class VoltageProc(ProcessingUnit): for channel in channelIndexList: if channel not in self.dataOut.channelIndexList: + print channelIndexList raise ValueError, "The value %d in channelIndexList is not valid" %channel nChannels = len(channelIndexList) diff --git a/schainpy/schain.py b/schainpy/schain.py new file mode 100644 index 0000000..91ba7a1 --- /dev/null +++ b/schainpy/schain.py @@ -0,0 +1,609 @@ +''' +Created on September , 2012 +@author: +''' +from xml.etree.ElementTree import Element, SubElement, ElementTree +from xml.etree import ElementTree as ET +from xml.dom import minidom + +import sys +import datetime +from model.jrodataIO import * +from model.jroprocessing import * + +def prettify(elem): + """Return a pretty-printed XML string for the Element. + """ + rough_string = ET.tostring(elem, 'utf-8') + reparsed = minidom.parseString(rough_string) + return reparsed.toprettyxml(indent=" ") + +class ParameterConf(): + + id = None + name = None + value = None + type = None + + ELEMENTNAME = 'Parameter' + + def __init__(self): + + self.type = 'str' + + def getElementName(self): + + return self.ELEMENTNAME + + def getValue(self): + + if self.type == 'list': + strList = self.value.split(',') + return strList + + if self.type == 'intlist': + strList = self.value.split(',') + intList = [int(x) for x in strList] + return intList + + if self.type == 'floatlist': + strList = self.value.split(',') + floatList = [float(x) for x in strList] + return floatList + + if self.type == 'date': + strList = self.value.split('/') + intList = [int(x) for x in strList] + date = datetime.date(intList[0], intList[1], intList[2]) + return date + + if self.type == 'time': + strList = self.value.split(':') + intList = [int(x) for x in strList] + time = datetime.time(intList[0], intList[1], intList[2]) + return time + + func = eval(self.type) + + return func(self.value) + + def setup(self, id, name, value, type='str'): + + self.id = id + self.name = name + self.value = str(value) + self.type = type + + def makeXml(self, opElement): + + parmElement = SubElement(opElement, self.ELEMENTNAME) + parmElement.set('id', str(self.id)) + parmElement.set('name', self.name) + parmElement.set('value', self.value) + parmElement.set('type', self.type) + + def readXml(self, parmElement): + + self.id = parmElement.get('id') + self.name = parmElement.get('name') + self.value = parmElement.get('value') + self.type = parmElement.get('type') + + def printattr(self): + + print "Parameter[%s]: name = %s, value = %s, type = %s" %(self.id, self.name, self.value, self.type) + +class OperationConf(): + + id = None + name = None + priority = None + type = None + + parmConfObjList = [] + + ELEMENTNAME = 'Operation' + + def __init__(self): + + id = 0 + name = None + priority = None + type = 'self' + + + def __getNewId(self): + + return int(self.id)*10 + len(self.parmConfObjList) + 1 + + def getElementName(self): + + return self.ELEMENTNAME + + def getParameterObjList(self): + + return self.parmConfObjList + + def setup(self, id, name, priority, type): + + self.id = id + self.name = name + self.type = type + self.priority = priority + + self.parmConfObjList = [] + + def addParameter(self, name, value, type='str'): + + id = self.__getNewId() + + parmConfObj = ParameterConf() + parmConfObj.setup(id, name, value, type) + + self.parmConfObjList.append(parmConfObj) + + return parmConfObj + + def makeXml(self, upElement): + + opElement = SubElement(upElement, self.ELEMENTNAME) + opElement.set('id', str(self.id)) + opElement.set('name', self.name) + opElement.set('type', self.type) + opElement.set('priority', str(self.priority)) + + for parmConfObj in self.parmConfObjList: + parmConfObj.makeXml(opElement) + + def readXml(self, opElement): + + self.id = opElement.get('id') + self.name = opElement.get('name') + self.type = opElement.get('type') + self.priority = opElement.get('priority') + + self.parmConfObjList = [] + + parmElementList = opElement.getiterator(ParameterConf().getElementName()) + + for parmElement in parmElementList: + parmConfObj = ParameterConf() + parmConfObj.readXml(parmElement) + self.parmConfObjList.append(parmConfObj) + + def printattr(self): + + print "%s[%s]: name = %s, type = %s, priority = %s" %(self.ELEMENTNAME, + self.id, + self.name, + self.type, + self.priority) + + for parmConfObj in self.parmConfObjList: + parmConfObj.printattr() + + def createObject(self): + + if self.type == 'self': + raise ValueError, "This operation type cannot be created" + + if self.type == 'other': + className = eval(self.name) + opObj = className() + + return opObj + +class ProcUnitConf(): + + id = None + name = None + type = None + inputId = None + + opConfObjList = [] + + procUnitObj = None + opObjList = [] + + ELEMENTNAME = 'ProcUnit' + + def __init__(self): + + self.id = None + self.type = None + self.name = None + self.inputId = None + + self.opConfObjList = [] + + self.procUnitObj = None + self.opObjDict = {} + + def __getPriority(self): + + return len(self.opConfObjList)+1 + + def __getNewId(self): + + return int(self.id)*10 + len(self.opConfObjList) + 1 + + def getElementName(self): + + return self.ELEMENTNAME + + def getId(self): + + return str(self.id) + + def getInputId(self): + + return str(self.inputId) + + def getOperationObjList(self): + + return self.opConfObjList + + def getProcUnitObj(self): + + return self.procUnitObj + + def setup(self, id, name, type, inputId): + + self.id = id + self.name = name + self.type = type + self.inputId = inputId + + self.opConfObjList = [] + + self.addOperation(name='init', optype='self') + + def addOperation(self, name, optype='self'): + + id = self.__getNewId() + priority = self.__getPriority() + + opConfObj = OperationConf() + opConfObj.setup(id, name=name, priority=priority, type=optype) + + self.opConfObjList.append(opConfObj) + + return opConfObj + + def makeXml(self, procUnitElement): + + upElement = SubElement(procUnitElement, self.ELEMENTNAME) + upElement.set('id', str(self.id)) + upElement.set('name', self.name) + upElement.set('type', self.type) + upElement.set('inputId', str(self.inputId)) + + for opConfObj in self.opConfObjList: + opConfObj.makeXml(upElement) + + def readXml(self, upElement): + + self.id = upElement.get('id') + self.name = upElement.get('name') + self.type = upElement.get('type') + self.inputId = upElement.get('inputId') + + self.opConfObjList = [] + + opElementList = upElement.getiterator(OperationConf().getElementName()) + + for opElement in opElementList: + opConfObj = OperationConf() + opConfObj.readXml(opElement) + self.opConfObjList.append(opConfObj) + + def printattr(self): + + print "%s[%s]: name = %s, type = %s, inputId = %s" %(self.ELEMENTNAME, + self.id, + self.name, + self.type, + self.inputId) + + for opConfObj in self.opConfObjList: + opConfObj.printattr() + + def createObjects(self): + + className = eval(self.name) + procUnitObj = className() + + for opConfObj in self.opConfObjList: + + if opConfObj.type == 'self': + continue + + opObj = opConfObj.createObject() + + self.opObjDict[opConfObj.id] = opObj + procUnitObj.addOperation(opObj, opConfObj.id) + + self.procUnitObj = procUnitObj + + return procUnitObj + + def run(self): + + for opConfObj in self.opConfObjList: + kwargs = {} + for parmConfObj in opConfObj.getParameterObjList(): + kwargs[parmConfObj.name] = parmConfObj.getValue() + + self.procUnitObj.call(opConfObj, **kwargs) + + + +class ReadUnitConf(ProcUnitConf): + + + path = None + startDate = None + endDate = None + startTime = None + endTime = None + online = None + expLabel = None + delay = None + + ELEMENTNAME = 'ReadUnit' + + def __init__(self): + + self.id = None + self.type = None + self.name = None + self.inputId = 0 + + self.opConfObjList = [] + self.opObjList = [] + + def getElementName(self): + + return self.ELEMENTNAME + + def setup(self, id, name, type, path, startDate, endDate, startTime, endTime, online=0, expLabel='', delay=60): + + self.id = id + self.name = name + self.type = type + + self.path = path + self.startDate = startDate + self.endDate = endDate + self.startTime = startTime + self.endTime = endTime + self.online = online + self.expLabel = expLabel + self.delay = delay + + self.addRunOperation() + + def addRunOperation(self): + + opObj = self.addOperation(name = 'run', optype = 'self') + + opObj.addParameter(name='path' , value=self.path, type='str') + opObj.addParameter(name='startDate' , value=self.startDate, type='date') + opObj.addParameter(name='endDate' , value=self.endDate, type='date') + opObj.addParameter(name='startTime' , value=self.startTime, type='time') + opObj.addParameter(name='endTime' , value=self.endTime, type='time') + opObj.addParameter(name='expLabel' , value=self.expLabel, type='str') + opObj.addParameter(name='online' , value=self.online, type='bool') + opObj.addParameter(name='delay' , value=self.delay, type='float') + + return opObj + + +class Controller(): + + id = None + name = None + description = None +# readUnitConfObjList = None + procUnitConfObjDict = None + + ELEMENTNAME = 'Controller' + + def __init__(self): + + self.id = None + self.name = None + self.description = None + +# self.readUnitConfObjList = [] + self.procUnitConfObjDict = {} + + def __getNewId(self): + + id = int(self.id)*10 + len(self.procUnitConfObjDict) + 1 + + return str(id) + + def getElementName(self): + + return self.ELEMENTNAME + + def setup(self, id, name, description): + + self.id = id + self.name = name + self.description = description + + def addReadUnit(self, type, path, startDate='', endDate='', startTime='', endTime='', online=0, expLabel='', delay=60): + + id = self.__getNewId() + name = '%sReader' %(type) + + readUnitConfObj = ReadUnitConf() + readUnitConfObj.setup(id, name, type, path, startDate, endDate, startTime, endTime, online, expLabel, delay) + + self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj + + return readUnitConfObj + + def addProcUnit(self, type, inputId): + + id = self.__getNewId() + name = '%sProc' %(type) + + procUnitConfObj = ProcUnitConf() + procUnitConfObj.setup(id, name, type, inputId) + + self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj + + return procUnitConfObj + + def makeXml(self): + + projectElement = Element('Controller') + projectElement.set('id', str(self.id)) + projectElement.set('name', self.name) + projectElement.set('description', self.description) + +# for readUnitConfObj in self.readUnitConfObjList: +# readUnitConfObj.makeXml(projectElement) + + for procUnitConfObj in self.procUnitConfObjDict.values(): + procUnitConfObj.makeXml(projectElement) + + self.projectElement = projectElement + + def writeXml(self, filename): + + self.makeXml() + + print prettify(self.projectElement) + + ElementTree(self.projectElement).write(filename, method='xml') + + def readXml(self, filename): + + #tree = ET.parse(filename) + self.projectElement = None +# self.readUnitConfObjList = [] + self.procUnitConfObjDict = {} + + self.projectElement = ElementTree().parse(filename) + + self.project = self.projectElement.tag + + self.id = self.projectElement.get('id') + self.name = self.projectElement.get('name') + self.description = self.projectElement.get('description') + + readUnitElementList = self.projectElement.getiterator(ReadUnitConf().getElementName()) + + for readUnitElement in readUnitElementList: + readUnitConfObj = ReadUnitConf() + readUnitConfObj.readXml(readUnitElement) + + self.procUnitConfObjDict[readUnitConfObj.getId()] = readUnitConfObj + + procUnitElementList = self.projectElement.getiterator(ProcUnitConf().getElementName()) + + for procUnitElement in procUnitElementList: + procUnitConfObj = ProcUnitConf() + procUnitConfObj.readXml(procUnitElement) + + self.procUnitConfObjDict[procUnitConfObj.getId()] = procUnitConfObj + + def printattr(self): + + print "Controller[%s]: name = %s, description = %s" %(self.id, + self.name, + self.description) + +# for readUnitConfObj in self.readUnitConfObjList: +# readUnitConfObj.printattr() + + for procUnitConfObj in self.procUnitConfObjDict.values(): + procUnitConfObj.printattr() + + def createObjects(self): + +# for readUnitConfObj in self.readUnitConfObjList: +# readUnitConfObj.createObjects() + + for procUnitConfObj in self.procUnitConfObjDict.values(): + procUnitConfObj.createObjects() + + def __connect(self, objIN, obj): + + obj.setInput(objIN.getOutput()) + + def connectObjects(self): + + for puConfObj in self.procUnitConfObjDict.values(): + + inputId = puConfObj.getInputId() + + if int(inputId) == 0: + continue + + puConfINObj = self.procUnitConfObjDict[inputId] + + puObj = puConfObj.getProcUnitObj() + puINObj = puConfINObj.getProcUnitObj() + + self.__connect(puINObj, puObj) + + def run(self): + +# for readUnitConfObj in self.readUnitConfObjList: +# readUnitConfObj.run() + while(True): + for procUnitConfObj in self.procUnitConfObjDict.values(): + procUnitConfObj.run() + +if __name__ == '__main__': + + desc = "Segundo Test" + filename = "schain.xml" + + controllerObj = Controller() + + controllerObj.setup(id = '191', name='test01', description=desc) + + readUnitConfObj = controllerObj.addReadUnit(type='Voltage', + path='/home/roj-idl71/Data/RAWDATA/Meteors', + startDate='2012/01/01', + endDate='2012/12/31', + startTime='00:00:00', + endTime='23:59:59', + online=0) + + procUnitConfObj1 = controllerObj.addProcUnit(type='Voltage', inputId=readUnitConfObj.getId()) + + procUnitConfObj2 = controllerObj.addProcUnit(type='Voltage', inputId=procUnitConfObj1.getId()) + + opObj11 = procUnitConfObj1.addOperation(name='selectChannels') + opObj11.addParameter(name='channelList', value='1,2', type='intlist') + +# opObj12 = procUnitConfObj1.addOperation(name='decoder') +# opObj12.addParameter(name='ncode', value='2', type='int') +# opObj12.addParameter(name='nbauds', value='8', type='int') +# opObj12.addParameter(name='code0', value='001110011', type='int') +# opObj12.addParameter(name='code1', value='001110011', type='int') + + opObj21 = procUnitConfObj2.addOperation(name='CohInt', optype='other') + opObj21.addParameter(name='nCohInt', value='10', type='int') + + + print "Escribiendo el archivo XML" + + controllerObj.writeXml(filename) + + print "Leyendo el archivo XML" + controllerObj.readXml(filename) + #controllerObj.printattr() + + controllerObj.createObjects() + controllerObj.connectObjects() + controllerObj.run() + + \ No newline at end of file diff --git a/schainpy/schain.xml b/schainpy/schain.xml new file mode 100644 index 0000000..9a78729 --- /dev/null +++ b/schainpy/schain.xml @@ -0,0 +1 @@ + \ No newline at end of file