Salome HOME
Ajout information manquante ordre
[modules/adao.git] / src / daEficas / generator_adao.py
1 # -*- coding: utf-8 -*-
2 print "import generator_adao"
3
4 from generator.generator_python import PythonGenerator
5 import traceback
6 import logging
7
8 def entryPoint():
9    """
10       Retourne les informations necessaires pour le chargeur de plugins
11
12       Ces informations sont retournees dans un dictionnaire
13    """
14    return {
15         # Le nom du plugin
16         'name' : 'adao',
17         # La factory pour creer une instance du plugin
18           'factory' : AdaoGenerator,
19           }
20
21 class AdaoGenerator(PythonGenerator):
22
23   def __init__(self,cr=None):
24     PythonGenerator.__init__(self, cr)
25     self.dictMCVal={}
26     self.text_comm = ""
27     self.text_da = ""
28     self.text_da_status = False
29     self.logger = logging.getLogger('ADAO EFICAS GENERATOR')
30     self.logger.setLevel(logging.DEBUG)
31     ch = logging.StreamHandler()
32     ch.setLevel(logging.DEBUG)
33     formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
34     ch.setFormatter(formatter)
35     self.logger.addHandler(ch)
36
37   def gener(self,obj,format='brut',config=None):
38     self.logger.debug("method gener called")
39     self.text_comm = PythonGenerator.gener(self, obj, format, config)
40     for key, value in self.dictMCVal.iteritems():
41       self.logger.debug("dictMCVAl %s %s" % (key,value))
42
43     try :
44       self.text_da_status = False
45       self.generate_da()
46       self.text_da_status = True
47     except:
48       self.logger.info("Case is not correct, python command file for YACS schema generation cannot be created")
49       self.logger.debug(self.text_da)
50       self.dictMCVal = {}
51       traceback.print_exc()
52     return self.text_comm
53
54   def writeDefault(self, fn):
55     if self.text_da_status:
56       print "write adao python command file"
57       filename = fn[:fn.rfind(".")] + '.py'
58       f = open( str(filename), 'wb')
59       f.write( self.text_da )
60       f.close()
61
62   def generMCSIMP(self,obj) :
63     """
64     Convertit un objet MCSIMP en texte python
65     """
66     clef=""
67     for i in obj.get_genealogie() :
68       print obj
69       print obj.get_genealogie()
70       clef=clef+"__"+i
71     self.dictMCVal[clef]=obj.valeur
72
73     s=PythonGenerator.generMCSIMP(self,obj)
74     return s
75
76   def generate_da(self):
77
78     self.text_da += "#-*-coding:iso-8859-1-*- \n"
79     self.text_da += "study_config = {} \n"
80
81     # Extraction de Study_name
82     self.text_da += "study_config[\"Name\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Study_name"] + "\"\n"
83     # Extraction de Debug
84     self.text_da += "study_config[\"Debug\"] = \"" + str(self.dictMCVal["__ASSIMILATION_STUDY__Debug"]) + "\"\n"
85     # Extraction de Algorithm
86     self.text_da += "study_config[\"Algorithm\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Algorithm"] + "\"\n"
87
88     self.add_data("Background")
89     self.add_data("BackgroundError")
90     self.add_data("Observation")
91     self.add_data("ObservationError")
92     self.add_data("ObservationOperator")
93
94     self.add_variables()
95     # Parametres optionnels
96
97     # Extraction du Study_repertory
98     if "__ASSIMILATION_STUDY__Study_repertory" in self.dictMCVal.keys():
99       self.text_da += "study_config[\"Repertory\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Study_repertory"] + "\"\n"
100     # Extraction de AlgorithmParameters
101     if "__ASSIMILATION_STUDY__AlgorithmParameters__INPUT_TYPE" in self.dictMCVal.keys():
102       self.add_algorithm_parameters()
103     # Extraction de UserPostAnalysis
104     if "__ASSIMILATION_STUDY__UserPostAnalysis__FROM" in self.dictMCVal.keys():
105       self.add_UserPostAnalysis()
106     if "__ASSIMILATION_STUDY__UserDataInit__INIT_FILE" in self.dictMCVal.keys():
107       self.add_init()
108
109   def add_data(self, data_name):
110
111     # Extraction des donnĂ©es
112     search_text = "__ASSIMILATION_STUDY__" + data_name + "__"
113     data_type = self.dictMCVal[search_text + "INPUT_TYPE"]
114     search_type = search_text + data_type + "__data__"
115     from_type = self.dictMCVal[search_type + "FROM"]
116     data = ""
117     if from_type == "String":
118       data = self.dictMCVal[search_type + "STRING_DATA__STRING"]
119     elif from_type == "Script":
120       data = self.dictMCVal[search_type + "SCRIPT_DATA__SCRIPT_FILE"]
121     elif from_type == "FunctionDict":
122       data = self.dictMCVal[search_type + "FUNCTIONDICT_DATA__FUNCTIONDICT_FILE"]
123     else:
124       raise Exception('From Type unknown', from_type)
125
126     if from_type == "String" or from_type == "Script":
127       self.text_da += data_name + "_config = {} \n"
128       self.text_da += data_name + "_config[\"Type\"] = \"" + data_type + "\" \n"
129       self.text_da += data_name + "_config[\"From\"] = \"" + from_type + "\" \n"
130       self.text_da += data_name + "_config[\"Data\"] = \"" + data      + "\" \n"
131       self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
132
133     if from_type == "FunctionDict":
134       self.text_da += data_name + "_FunctionDict = {} \n"
135       self.text_da += data_name + "_FunctionDict[\"Function\"] = [\"Direct\", \"Tangent\", \"Adjoint\"] \n"
136       self.text_da += data_name + "_FunctionDict[\"Script\"] = {} \n"
137       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Direct\"] = \""  + data + "\" \n"
138       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Tangent\"] = \"" + data + "\" \n"
139       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Adjoint\"] = \"" + data + "\" \n"
140       self.text_da += data_name + "_config = {} \n"
141       self.text_da += data_name + "_config[\"Type\"] = \"Function\" \n"
142       self.text_da += data_name + "_config[\"From\"] = \"FunctionDict\" \n"
143       self.text_da += data_name + "_config[\"Data\"] = " + data_name + "_FunctionDict \n"
144       self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
145
146   def add_algorithm_parameters(self):
147
148     data_name = "AlgorithmParameters"
149     data_type = "Dict"
150     from_type = "Script"
151     data = self.dictMCVal["__ASSIMILATION_STUDY__AlgorithmParameters__Dict__data__SCRIPT_DATA__SCRIPT_FILE"]
152
153     self.text_da += data_name + "_config = {} \n"
154     self.text_da += data_name + "_config[\"Type\"] = \"" + data_type + "\" \n"
155     self.text_da += data_name + "_config[\"From\"] = \"" + from_type + "\" \n"
156     self.text_da += data_name + "_config[\"Data\"] = \"" + data + "\" \n"
157     self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
158
159   def add_init(self):
160
161       init_file_data = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__INIT_FILE"]
162       init_target_list = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__TARGET_LIST"]
163
164       self.text_da += "Init_config = {} \n"
165       self.text_da += "Init_config[\"Type\"] = \"Dict\" \n"
166       self.text_da += "Init_config[\"From\"] = \"Script\" \n"
167       self.text_da += "Init_config[\"Data\"] = \"" + init_file_data + "\"\n"
168       self.text_da += "Init_config[\"Target\"] = ["
169       for target in init_target_list:
170         self.text_da += "\"" + target + "\","
171       self.text_da += "] \n"
172       self.text_da += "study_config[\"UserDataInit\"] = Init_config \n"
173
174   def add_UserPostAnalysis(self):
175
176     from_type = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__FROM"]
177     data = ""
178     if from_type == "String":
179       data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__STRING_DATA__STRING"]
180       self.text_da += "Analysis_config = {} \n"
181       self.text_da += "Analysis_config[\"From\"] = \"String\" \n"
182       self.text_da += "Analysis_config[\"Data\"] = \"\"\"" + data + "\"\"\" \n"
183       self.text_da += "study_config[\"UserPostAnalysis\"] = Analysis_config \n"
184     elif from_type == "Script":
185       data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__SCRIPT_DATA__SCRIPT_FILE"]
186       self.text_da += "Analysis_config = {} \n"
187       self.text_da += "Analysis_config[\"From\"] = \"Script\" \n"
188       self.text_da += "Analysis_config[\"Data\"] = \"" + data + "\" \n"
189       self.text_da += "study_config[\"UserPostAnalysis\"] = Analysis_config \n"
190     else:
191       raise Exception('From Type unknown', from_type)
192
193   def add_variables(self):
194
195     # Input variables
196     if "__ASSIMILATION_STUDY__InputVariables__NAMES" in self.dictMCVal.keys():
197       names = []
198       sizes = []
199       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"], type("")):
200         names.append(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"])
201       else:
202         names = self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"]
203       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"], type(1)):
204         sizes.append(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"])
205       else:
206         sizes = self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"]
207
208       self.text_da += "inputvariables_config = {} \n"
209       self.text_da += "inputvariables_config[\"Order\"] = %s \n" % list(names)
210       for name, size in zip(names, sizes):
211         self.text_da += "inputvariables_config[\"%s\"] = %s \n" % (name,size)
212       self.text_da += "study_config[\"InputVariables\"] = inputvariables_config \n"
213     else:
214       self.text_da += "inputvariables_config = {} \n"
215       self.text_da += "inputvariables_config[\"adao_default\"] = -1 \n"
216       self.text_da += "study_config[\"InputVariables\"] = inputvariables_config \n"
217
218     # Output variables
219     if "__ASSIMILATION_STUDY__OutputVariables__NAMES" in self.dictMCVal.keys():
220       names = []
221       sizes = []
222       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"], type("")):
223         names.append(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"])
224       else:
225         names = self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"]
226       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"], type(1)):
227         sizes.append(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"])
228       else:
229         sizes = self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"]
230
231       self.text_da += "outputvariables_config = {} \n"
232       self.text_da += "outputvariables_config[\"Order\"] = %s \n" % list(names)
233       for name, size in zip(names, sizes):
234         self.text_da += "outputvariables_config[\"%s\"] = %s \n" % (name,size)
235       self.text_da += "study_config[\"OutputVariables\"] = outputvariables_config \n"
236     else:
237       self.text_da += "outputvariables_config = {} \n"
238       self.text_da += "outputvariables_config[\"adao_default\"] = -1 \n"
239       self.text_da += "study_config[\"OutputVariables\"] = outputvariables_config \n"