]> SALOME platform Git repositories - modules/adao.git/blob - bin/AdaoCatalogGenerator.py
Salome HOME
Adding storage capacity of some input variables
[modules/adao.git] / bin / AdaoCatalogGenerator.py
1 #-*- coding:utf-8 -*-
2 #  Copyright (C) 2008-2011  EDF R&D
3 #
4 #  This library is free software; you can redistribute it and/or
5 #  modify it under the terms of the GNU General Public
6 #  License as published by the Free Software Foundation; either
7 #  version 2.1 of the License.
8 #
9 #  This library is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 #  Lesser General Public License for more details.
13 #
14 #  You should have received a copy of the GNU Lesser General Public
15 #  License along with this library; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # --
19 # Author : André RIBES (EDF R&D)
20 # --
21
22 import logging
23 import traceback
24 import sys
25 import string
26 import StringIO
27
28 logging.basicConfig(level=logging.INFO)
29
30 #----------- Templates Part ---------------#
31 begin_catalog_file = """# -*- coding: utf-8 -*-
32
33 # --------------------------------------------------------
34 # generated by AdaoCatalogGenerator at ${date}
35 # --------------------------------------------------------
36
37 import Accas
38 from Accas import *
39
40 JdC = JDC_CATA (code = 'ADAO',
41                 execmodul = None,
42                 regles = ( AU_MOINS_UN ('ASSIMILATION_STUDY','CHECKING_STUDY'), AU_PLUS_UN ('ASSIMILATION_STUDY','CHECKING_STUDY')),
43                )
44 """
45
46 data_method = """
47 def F_${data_name}(statut) : return FACT(statut = statut,
48                                          FROM = SIMP(statut = "o", typ = "TXM", into=(${data_into}), defaut=${data_default}),
49                                          SCRIPT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
50
51                                                       SCRIPT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
52                                                      ),
53                                          STRING_DATA = BLOC ( condition = " FROM in ( 'String', ) ",
54
55                                                       STRING = SIMP(statut = "o", typ = "TXM"),
56                                                      ),
57                                          FUNCTIONDICT_DATA = BLOC ( condition = " FROM in ( 'FunctionDict', ) ",
58
59                                                       FUNCTIONDICT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
60                                                      ),
61                                     )
62 """
63
64 init_method = """
65 def F_InitChoice() : return  ("Background",
66                               "BackgroundError",
67                               "Observation",
68                               "ObservationError",
69                               "ObservationOperator",
70                               "EvolutionModel",
71                               "EvolutionError",
72                               "AlgorithmParameters",
73                               "UserPostAnalysis",
74                              )
75
76 def F_Init(statut) : return FACT(statut = statut,
77                                  INIT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
78                                  TARGET_LIST = SIMP(statut = "o", typ = "TXM", min=1, max="**", into=F_InitChoice(),  validators=(VerifExiste(2))),
79                                 )
80 """
81
82 assim_data_method = """
83 def F_${assim_name}(statut) : return FACT(statut=statut,
84 ${storage}
85                                           INPUT_TYPE = SIMP(statut="o", typ = "TXM", into=(${choices}), defaut=${default_choice}),
86 ${decl_choices}
87                                                 )
88 """
89
90 assim_data_choice = """
91                                                  ${choice_name} = BLOC ( condition = " INPUT_TYPE in ( '${choice_name}', ) ",
92                                                  data = F_${choice_name}("o"),
93                                                  ),
94 """
95
96 observers_choice = """
97                                        ${var_name} = BLOC (condition=" '${var_name}' in set(SELECTION) ",
98                                                   ${var_name}_data = FACT(statut = "o",
99                                                              Scheduler = SIMP(statut = "f", typ = "TXM"),
100                                                              Info      = SIMP(statut = "f", typ = "TXM"),
101                                                              NodeType  = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "", into=("String", "Script")),
102                                                              PythonScript = BLOC (condition = " NodeType == 'String' ",
103                                                                                   Value = SIMP(statut = "o", typ = "TXM")
104                                                                                  ),
105                                                              UserFile = BLOC (condition = " NodeType == 'Script' ",
106                                                                               Value = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()))
107                                                                              )
108                                                                       ),
109                                                           ),
110 """
111
112 observers_method = """
113 def F_Observers(statut) : return FACT(statut=statut,
114                                       SELECTION = SIMP(statut="o", defaut=[], typ="TXM", min=0, max="**", validators=NoRepeat(), into=(${choices})),
115 ${decl_choices}
116                                      )
117 """
118
119 assim_study = """
120
121 def F_variables(statut) : return FACT(statut=statut,
122                                       regles = ( MEME_NOMBRE ('NAMES', 'SIZES')),
123                                       NAMES = SIMP(statut="o", typ="TXM", max="**", validators=NoRepeat()),
124                                       SIZES = SIMP(statut="o", typ="I", val_min=1, max="**")
125                                       )
126
127 ASSIMILATION_STUDY = PROC(nom="ASSIMILATION_STUDY",
128                           op=None,
129                           repetable           = "n",
130                           Study_name          = SIMP(statut="o", typ = "TXM"),
131                           Study_repertory     = SIMP(statut="f", typ = "TXM"),
132                           Debug               = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
133                           Algorithm           = SIMP(statut="o", typ = "TXM", into=(${algos_names})),
134                           Background          = F_Background("o"),
135                           BackgroundError     = F_BackgroundError("o"),
136                           Observation         = F_Observation("o"),
137                           ObservationError    = F_ObservationError("o"),
138                           ObservationOperator = F_ObservationOperator("o"),
139                           EvolutionModel      = F_EvolutionModel("f"),
140                           EvolutionError      = F_EvolutionError("f"),
141                           AlgorithmParameters = F_AlgorithmParameters("f"),
142                           UserDataInit        = F_Init("f"),
143                           UserPostAnalysis    = F_UserPostAnalysis("f"),
144                           InputVariables      = F_variables("f"),
145                           OutputVariables     = F_variables("f"),
146                           Observers           = F_Observers("f")
147                          )
148
149 CHECKING_STUDY = PROC(nom="CHECKING_STUDY",
150                           op=None,
151                           repetable           = "n",
152                           Study_name          = SIMP(statut="o", typ = "TXM"),
153                           Study_repertory     = SIMP(statut="f", typ = "TXM"),
154                           Debug               = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
155                           Algorithm           = SIMP(statut="o", typ = "TXM", into=(${check_names})),
156                           CheckingPoint       = F_CheckingPoint("o"),
157                           ObservationOperator = F_ObservationOperator("o"),
158                           AlgorithmParameters = F_AlgorithmParameters("f"),
159                           UserDataInit        = F_Init("f"),
160                          )
161 """
162
163 begin_catalog_file = string.Template(begin_catalog_file)
164 data_method = string.Template(data_method)
165 assim_data_method = string.Template(assim_data_method)
166 assim_data_choice = string.Template(assim_data_choice)
167 assim_study = string.Template(assim_study)
168 observers_method = string.Template(observers_method)
169 observers_choice = string.Template(observers_choice)
170
171 #----------- End of Templates Part ---------------#
172
173
174
175 #----------- Begin generation script -----------#
176 print "-- Starting AdaoCalatogGenerator.py --"
177
178 try:
179   import daEficas
180   import daYacsSchemaCreator
181   import daCore.AssimilationStudy
182   import daYacsSchemaCreator.infos_daComposant as infos
183 except:
184   logging.fatal("Import of ADAO python modules failed !" +
185                 "\n add ADAO python installation directory in your PYTHONPATH")
186   traceback.print_exc()
187   sys.exit(1)
188
189 def check_args(args):
190   logging.debug("Arguments are :" + str(args))
191   if len(args) != 2:
192     logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
193     sys.exit(1)
194
195 # Parse arguments
196 from optparse import OptionParser
197 usage = "usage: %prog [options] catalog_path catalog_name"
198 version="%prog 0.1"
199 my_parser = OptionParser(usage=usage, version=version)
200 (options, args) = my_parser.parse_args()
201 check_args(args)
202
203 catalog_path =  args[0]
204 catalog_name =  args[1]
205
206 # Generates into a string
207 mem_file = StringIO.StringIO()
208
209 # Start file
210 from time import strftime
211 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
212
213 # Step 1: A partir des infos, on crée les fonctions qui vont permettre
214 # d'entrer les données utilisateur
215 for data_input_name in infos.DataTypeDict.keys():
216   logging.debug('A data input Type is found: ' + data_input_name)
217   data_name = data_input_name
218   data_into = ""
219   data_default = ""
220
221   # On récupère les différentes façon d'entrer les données
222   for basic_type in infos.DataTypeDict[data_input_name]:
223     data_into += "\"" + basic_type + "\", "
224
225   # On choisit le défault
226   data_default = "\"" + infos.DataTypeDefaultDict[data_input_name] + "\""
227
228   mem_file.write(data_method.substitute(data_name    = data_name,
229                                         data_into    = data_into,
230                                         data_default = data_default))
231
232 # Step 2: On crée les fonctions qui permettent de rentrer les données des algorithmes
233 for assim_data_input_name in infos.AssimDataDict.keys():
234   logging.debug("An assimilation algorithm data input is found: " + assim_data_input_name)
235   assim_name = assim_data_input_name
236   storage = ""
237   choices = ""
238   default_choice = ""
239   decl_choices = ""
240   decl_opts = ""
241   if infos.AssimDataDefaultDict[assim_data_input_name] in infos.StoredAssimData:
242     storage = "                                          Stored = SIMP(statut=\"o\", typ = \"I\", into=(0, 1), defaut=0),"
243   for choice in infos.AssimDataDict[assim_data_input_name]:
244     choices += "\"" + choice + "\", "
245     decl_choices += assim_data_choice.substitute(choice_name = choice)
246     if choice in infos.StoredAssimData:
247       storage = "                                          Stored = SIMP(statut=\"o\", typ = \"I\", into=(0, 1), defaut=0),"
248   default_choice = "\"" + infos.AssimDataDefaultDict[assim_data_input_name] + "\""
249
250   mem_file.write(assim_data_method.substitute(assim_name = assim_name,
251                                               storage = storage,
252                                               choices = choices,
253                                               decl_choices = decl_choices,
254                                               default_choice=default_choice))
255
256 # Step 3: On ajoute les fonctions représentant les options possibles
257 for opt_name in infos.OptDict.keys():
258   logging.debug("An optional node is found: " + opt_name)
259   data_name = opt_name
260   data_into = ""
261   data_default = ""
262
263   for choice in infos.OptDict[opt_name]:
264     data_into += "\"" + choice + "\", "
265   data_default = "\"" + infos.OptDefaultDict[opt_name] + "\""
266
267   mem_file.write(data_method.substitute(data_name = data_name,
268                                         data_into = data_into,
269                                         data_default = data_default))
270
271 # Step 4: On ajoute la méthode optionnelle init
272 # TODO uniformiser avec le step 3
273 mem_file.write(init_method)
274
275 # Step 5: Add observers
276 decl_choices = ""
277 for obs_var in infos.ObserversList:
278   decl_choices += observers_choice.substitute(var_name=obs_var)
279 mem_file.write(observers_method.substitute(choices = infos.ObserversList,
280                                            decl_choices = decl_choices))
281
282 # Final step: Add algorithm and assim_study
283 algos_names = ""
284 check_names = ""
285 decl_algos  = ""
286
287 assim_study_object = daCore.AssimilationStudy.AssimilationStudy()
288 algos_list = assim_study_object.get_available_algorithms()
289 for algo_name in algos_list:
290   if algo_name in infos.AssimAlgos:
291     logging.debug("An assimilation algorithm is found: " + algo_name)
292     algos_names += "\"" + algo_name + "\", "
293   elif algo_name in infos.CheckAlgos:
294     logging.debug("A checking algorithm is found: " + algo_name)
295     check_names += "\"" + algo_name + "\", "
296   else:
297     logging.debug("This algorithm is not considered: " + algo_name)
298
299 mem_file.write(assim_study.substitute(algos_names=algos_names,
300                                       check_names=check_names,
301                                       decl_algos=decl_algos))
302 # Write file
303 final_file = open(catalog_path + "/" + catalog_name, "wr")
304 final_file.write(mem_file.getvalue())
305 mem_file.close()
306 final_file.close()
307