Salome HOME
samples -> examples + moins de debug
[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.INFO)
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       clef=clef+"__"+i
69     self.dictMCVal[clef]=obj.valeur
70
71     s=PythonGenerator.generMCSIMP(self,obj)
72     return s
73
74   def generate_da(self):
75
76     self.text_da += "#-*-coding:iso-8859-1-*- \n"
77     self.text_da += "study_config = {} \n"
78
79     # Extraction de Study_name
80     self.text_da += "study_config[\"Name\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Study_name"] + "\"\n"
81     # Extraction de Debug
82     self.text_da += "study_config[\"Debug\"] = \"" + str(self.dictMCVal["__ASSIMILATION_STUDY__Debug"]) + "\"\n"
83     # Extraction de Algorithm
84     self.text_da += "study_config[\"Algorithm\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Algorithm"] + "\"\n"
85
86     self.add_data("Background")
87     self.add_data("BackgroundError")
88     self.add_data("Observation")
89     self.add_data("ObservationError")
90     self.add_data("ObservationOperator")
91
92     self.add_variables()
93     # Parametres optionnels
94
95     # Extraction du Study_repertory
96     if "__ASSIMILATION_STUDY__Study_repertory" in self.dictMCVal.keys():
97       self.text_da += "study_config[\"Repertory\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Study_repertory"] + "\"\n"
98     # Extraction de AlgorithmParameters
99     if "__ASSIMILATION_STUDY__AlgorithmParameters__INPUT_TYPE" in self.dictMCVal.keys():
100       self.add_algorithm_parameters()
101     # Extraction de UserPostAnalysis
102     if "__ASSIMILATION_STUDY__UserPostAnalysis__FROM" in self.dictMCVal.keys():
103       self.add_UserPostAnalysis()
104     if "__ASSIMILATION_STUDY__UserDataInit__INIT_FILE" in self.dictMCVal.keys():
105       self.add_init()
106
107   def add_data(self, data_name):
108
109     # Extraction des donnĂ©es
110     search_text = "__ASSIMILATION_STUDY__" + data_name + "__"
111     data_type = self.dictMCVal[search_text + "INPUT_TYPE"]
112     search_type = search_text + data_type + "__data__"
113     from_type = self.dictMCVal[search_type + "FROM"]
114     data = ""
115     if from_type == "String":
116       data = self.dictMCVal[search_type + "STRING_DATA__STRING"]
117     elif from_type == "Script":
118       data = self.dictMCVal[search_type + "SCRIPT_DATA__SCRIPT_FILE"]
119     elif from_type == "FunctionDict":
120       data = self.dictMCVal[search_type + "FUNCTIONDICT_DATA__FUNCTIONDICT_FILE"]
121     else:
122       raise Exception('From Type unknown', from_type)
123
124     if from_type == "String" or from_type == "Script":
125       self.text_da += data_name + "_config = {} \n"
126       self.text_da += data_name + "_config[\"Type\"] = \"" + data_type + "\" \n"
127       self.text_da += data_name + "_config[\"From\"] = \"" + from_type + "\" \n"
128       self.text_da += data_name + "_config[\"Data\"] = \"" + data      + "\" \n"
129       self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
130
131     if from_type == "FunctionDict":
132       self.text_da += data_name + "_FunctionDict = {} \n"
133       self.text_da += data_name + "_FunctionDict[\"Function\"] = [\"Direct\", \"Tangent\", \"Adjoint\"] \n"
134       self.text_da += data_name + "_FunctionDict[\"Script\"] = {} \n"
135       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Direct\"] = \""  + data + "\" \n"
136       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Tangent\"] = \"" + data + "\" \n"
137       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Adjoint\"] = \"" + data + "\" \n"
138       self.text_da += data_name + "_config = {} \n"
139       self.text_da += data_name + "_config[\"Type\"] = \"Function\" \n"
140       self.text_da += data_name + "_config[\"From\"] = \"FunctionDict\" \n"
141       self.text_da += data_name + "_config[\"Data\"] = " + data_name + "_FunctionDict \n"
142       self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
143
144   def add_algorithm_parameters(self):
145
146     data_name = "AlgorithmParameters"
147     data_type = "Dict"
148     from_type = "Script"
149     data = self.dictMCVal["__ASSIMILATION_STUDY__AlgorithmParameters__Dict__data__SCRIPT_DATA__SCRIPT_FILE"]
150
151     self.text_da += data_name + "_config = {} \n"
152     self.text_da += data_name + "_config[\"Type\"] = \"" + data_type + "\" \n"
153     self.text_da += data_name + "_config[\"From\"] = \"" + from_type + "\" \n"
154     self.text_da += data_name + "_config[\"Data\"] = \"" + data + "\" \n"
155     self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
156
157   def add_init(self):
158
159       init_file_data = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__INIT_FILE"]
160       init_target_list = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__TARGET_LIST"]
161
162       self.text_da += "Init_config = {} \n"
163       self.text_da += "Init_config[\"Type\"] = \"Dict\" \n"
164       self.text_da += "Init_config[\"From\"] = \"Script\" \n"
165       self.text_da += "Init_config[\"Data\"] = \"" + init_file_data + "\"\n"
166       self.text_da += "Init_config[\"Target\"] = ["
167       for target in init_target_list:
168         self.text_da += "\"" + target + "\","
169       self.text_da += "] \n"
170       self.text_da += "study_config[\"UserDataInit\"] = Init_config \n"
171
172   def add_UserPostAnalysis(self):
173
174     from_type = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__FROM"]
175     data = ""
176     if from_type == "String":
177       data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__STRING_DATA__STRING"]
178       self.text_da += "Analysis_config = {} \n"
179       self.text_da += "Analysis_config[\"From\"] = \"String\" \n"
180       self.text_da += "Analysis_config[\"Data\"] = \"\"\"" + data + "\"\"\" \n"
181       self.text_da += "study_config[\"UserPostAnalysis\"] = Analysis_config \n"
182     elif from_type == "Script":
183       data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__SCRIPT_DATA__SCRIPT_FILE"]
184       self.text_da += "Analysis_config = {} \n"
185       self.text_da += "Analysis_config[\"From\"] = \"Script\" \n"
186       self.text_da += "Analysis_config[\"Data\"] = \"" + data + "\" \n"
187       self.text_da += "study_config[\"UserPostAnalysis\"] = Analysis_config \n"
188     else:
189       raise Exception('From Type unknown', from_type)
190
191   def add_variables(self):
192
193     # Input variables
194     if "__ASSIMILATION_STUDY__InputVariables__NAMES" in self.dictMCVal.keys():
195       names = []
196       sizes = []
197       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"], type("")):
198         names.append(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"])
199       else:
200         names = self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"]
201       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"], type(1)):
202         sizes.append(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"])
203       else:
204         sizes = self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"]
205
206       self.text_da += "inputvariables_config = {} \n"
207       self.text_da += "inputvariables_config[\"Order\"] = %s \n" % list(names)
208       for name, size in zip(names, sizes):
209         self.text_da += "inputvariables_config[\"%s\"] = %s \n" % (name,size)
210       self.text_da += "study_config[\"InputVariables\"] = inputvariables_config \n"
211     else:
212       self.text_da += "inputvariables_config = {} \n"
213       self.text_da += "inputvariables_config[\"Order\"] =[\"adao_default\"] \n"
214       self.text_da += "inputvariables_config[\"adao_default\"] = -1 \n"
215       self.text_da += "study_config[\"InputVariables\"] = inputvariables_config \n"
216
217     # Output variables
218     if "__ASSIMILATION_STUDY__OutputVariables__NAMES" in self.dictMCVal.keys():
219       names = []
220       sizes = []
221       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"], type("")):
222         names.append(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"])
223       else:
224         names = self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"]
225       if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"], type(1)):
226         sizes.append(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"])
227       else:
228         sizes = self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"]
229
230       self.text_da += "outputvariables_config = {} \n"
231       self.text_da += "outputvariables_config[\"Order\"] = %s \n" % list(names)
232       for name, size in zip(names, sizes):
233         self.text_da += "outputvariables_config[\"%s\"] = %s \n" % (name,size)
234       self.text_da += "study_config[\"OutputVariables\"] = outputvariables_config \n"
235     else:
236       self.text_da += "outputvariables_config = {} \n"
237       self.text_da += "outputvariables_config[\"Order\"] = [\"adao_default\"] \n"
238       self.text_da += "outputvariables_config[\"adao_default\"] = -1 \n"
239       self.text_da += "study_config[\"OutputVariables\"] = outputvariables_config \n"