Salome HOME
d5425dfe3a4c7cd58293ec6d69a26b30295bca76
[modules/adao.git] / bin / AdaoCatalogGenerator.py
1 #-*-coding:iso-8859-1-*-
2 #
3 # Copyright (C) 2008-2013 EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
20
21 import logging
22 import traceback
23 import sys
24 import string
25 import StringIO
26
27 logging.basicConfig(level=logging.WARNING)
28
29 #----------- Templates Part ---------------#
30 begin_catalog_file = """# -*- coding: utf-8 -*-
31
32 # --------------------------------------------------------
33 # generated by AdaoCatalogGenerator at ${date}
34 # --------------------------------------------------------
35
36 import Accas
37 from Accas import *
38
39 JdC = JDC_CATA (code = 'ADAO',
40                 execmodul = None,
41                 regles = ( AU_MOINS_UN ('ASSIMILATION_STUDY','CHECKING_STUDY'), AU_PLUS_UN ('ASSIMILATION_STUDY','CHECKING_STUDY')),
42                )
43 """
44
45 data_method = """
46 def F_${data_name}(statut) : return FACT(statut = statut,
47                                          FROM = SIMP(statut = "o", typ = "TXM", into=(${data_into}), defaut=${data_default}),
48                                          SCRIPT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
49
50                                                       SCRIPT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()), fr="En attente d'un nom de fichier script, avec ou sans le chemin complet pour le trouver, contenant si nécessaire la définition d'une variable interne de même nom que le concept parent", ang="Waiting for a script file name, with or without the full path to find it, containing if necessary the definition of an internal variable of the same name as the parent concept"),
51                                                      ),
52                                          STRING_DATA = BLOC ( condition = " FROM in ( 'String', ) ",
53
54                                                       STRING = SIMP(statut = "o", typ = "TXM", fr="En attente d'une chaine de caractères entre guillements. Pour construire un vecteur ou une matrice, ce doit être une suite de nombres, utilisant un espace ou une virgule pour séparer deux éléments et un point-virgule pour séparer deux lignes", ang="Waiting for a string in quotes. To build a vector or a matrix, it has to be a float serie, using a space or comma to separate two elements in a line, a semi-colon to separate rows"),
55                                                      ),
56                                          SCRIPTWITHFUNCTIONS_DATA = BLOC ( condition = " FROM in ( 'ScriptWithFunctions', ) ",
57
58                                                       SCRIPTWITHFUNCTIONS_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()), fr="En attente d'un nom de fichier script, avec ou sans le chemin complet pour le trouver, contenant en variables internes trois fonctions de calcul nommées DirectOperator, TangentOperator et AdjointOperator", ang="Waiting for a script file name, with or without the full path to find it, containing as internal variables three computation functions named DirectOperator, TangentOperator and AdjointOperator"),
59                                                      ),
60                                          SCRIPTWITHONEFUNCTION_DATA = BLOC ( condition = " FROM in ( 'ScriptWithOneFunction', ) ",
61
62                                                       SCRIPTWITHONEFUNCTION_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()), fr="En attente d'un nom de fichier script, avec ou sans le chemin complet pour le trouver, contenant en variable interne une seule fonction de calcul nommée DirectOperator", ang="Waiting for a script file name, with or without the full path to find it, containing as internal variable only one function named DirectOperator"),
63                                                       DifferentialIncrement = SIMP(statut="o", typ = "R", val_min=0, val_max=1, defaut=0.01, fr="Incrément de la perturbation dX pour calculer la dérivée, construite en multipliant X par l'incrément en évitant les valeurs nulles", ang="Increment of dX perturbation to calculate the derivative, build multiplying X by the increment avoiding null values"),
64                                                       CenteredFiniteDifference = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0, fr="Formulation centrée (1) ou décentrée (0) pour la méthode des différences finies", ang="Centered (1) or uncentered (0) formulation for the finite difference method"),
65                                                      ),
66                                          SCRIPTWITHSWITCH_DATA = BLOC ( condition = " FROM in ( 'ScriptWithSwitch', ) ",
67
68                                                       SCRIPTWITHSWITCH_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()), fr="En attente d'un nom de fichier script, avec ou sans le chemin complet pour le trouver, contenant un switch pour les calculs direct, tangent et adjoint", ang="Waiting for a script file name, with or without the full path to find it, containing a switch for direct, tangent and adjoint computations"),
69                                                      ),
70                                          FUNCTIONDICT_DATA = BLOC ( condition = " FROM in ( 'FunctionDict', ) ",
71
72                                                       FUNCTIONDICT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()), fr="OBSOLETE : conservé pour compatibilité avec la version 6.5, sera supprimé dans le futur", ang="OBSOLETE: keeped for compatibility with the 6.5 version, will be removed in the future"),
73                                                      ),
74                                     )
75 """
76
77 init_method = """
78 def F_InitChoice() : return  ("Background",
79                               "BackgroundError",
80                               "Observation",
81                               "ObservationError",
82                               "ObservationOperator",
83                               "EvolutionModel",
84                               "EvolutionError",
85                               "AlgorithmParameters",
86                               "UserPostAnalysis",
87                              )
88
89 def F_Init(statut) : return FACT(statut = statut,
90                                  INIT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
91                                  TARGET_LIST = SIMP(statut = "o", typ = "TXM", min=1, max="**", into=F_InitChoice(),  validators=(VerifExiste(2))),
92                                 )
93 """
94
95 assim_data_method = """
96 def F_${assim_name}(statut) : return FACT(statut=statut,
97 ${storage}
98                                           INPUT_TYPE = SIMP(statut="o", typ = "TXM", into=(${choices}), defaut=${default_choice}),
99 ${decl_choices}
100                                                 )
101 """
102
103 assim_data_choice = """
104                                                  ${choice_name} = BLOC ( condition = " INPUT_TYPE in ( '${choice_name}', ) ",
105                                                  data = F_${choice_name}("o"),
106                                                  ),
107 """
108
109 observers_choice = """
110                                        ${var_name} = BLOC (condition=" '${var_name}' in set(SELECTION) ",
111                                            ${var_name}_data = FACT(statut = "o",
112                                                Scheduler    = SIMP(statut = "f", typ = "TXM"),
113                                                Info         = SIMP(statut = "f", typ = "TXM", defaut = "${var_name}"),
114                                                NodeType     = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "", into=("String", "Script", "Template")),
115                                                PythonScript = BLOC (condition = " NodeType == 'String' ",
116                                                    Value = SIMP(statut = "o", typ = "TXM")
117                                                ),
118                                                UserFile = BLOC (condition = " NodeType == 'Script' ",
119                                                    Value = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()))
120                                                ),
121                                                ObserverTemplate =  BLOC (condition = " NodeType == 'Template' ",
122                                                    Template = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "", into=("ValuePrinter", "ValueSeriePrinter", "ValueGnuPlotter", "ValueSerieGnuPlotter")),
123                                                    ValuePrinter = BLOC (condition = " Template == 'ValuePrinter' ",
124                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "print info,var[-1]" ),
125                                                    ),
126                                                    ValueSeriePrinter = BLOC (condition = " Template == 'ValueSeriePrinter' ",
127                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "print info,var[:]" ),
128                                                    ),
129                                                    ValueGnuPlotter = BLOC (condition = " Template == 'ValueGnuPlotter' ",
130                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import Gnuplot\\nglobal nfig,gp\\ntry:\\n    nfig += 1\\n    gp('set style data lines')\\nexcept:\\n    nfig = 0\\n    gp = Gnuplot.Gnuplot(persist=1)\\n    gp('set style data lines')\\ngp('set title  \\"'+str(info)+' (Figure %i)\\"'%nfig)\\ngp.plot( Gnuplot.Data( var[-1], with_='lines lw 2' ) )" ),
131                                                    ),
132                                                    ValueSerieGnuPlotter = BLOC (condition = " Template == 'ValueSerieGnuPlotter' ",
133                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import Gnuplot\\nglobal nfig,gp\\ntry:\\n    nfig += 1\\n    gp('set style data lines')\\nexcept:\\n    nfig = 0\\n    gp = Gnuplot.Gnuplot(persist=1)\\n    gp('set style data lines')\\ngp('set title  \\"'+str(info)+' (Figure %i)\\"'%nfig)\\ngp.plot( Gnuplot.Data( var[:], with_='lines lw 2' ) )" ),
134                                                    ),
135                                                ),
136                                            ),
137                                        ),
138 """
139
140 observers_method = """
141 def F_Observers(statut) : return FACT(statut=statut,
142                                       SELECTION = SIMP(statut="o", defaut=[], typ="TXM", min=0, max="**", validators=NoRepeat(), into=(${choices})),
143 ${decl_choices}
144                                      )
145 """
146
147 assim_study = """
148
149 def F_variables(statut) : return FACT(statut=statut,
150                                       regles = ( MEME_NOMBRE ('NAMES', 'SIZES')),
151                                       NAMES = SIMP(statut="o", typ="TXM", max="**", validators=NoRepeat()),
152                                       SIZES = SIMP(statut="o", typ="I", val_min=1, max="**")
153                                       )
154
155 ASSIMILATION_STUDY = PROC(nom="ASSIMILATION_STUDY",
156                           op=None,
157                           repetable           = "n",
158                           Study_name          = SIMP(statut="o", typ = "TXM", defaut="ADAO Calculation Case"),
159                           Study_repertory     = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
160                           Debug               = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
161                           Algorithm           = SIMP(statut="o", typ = "TXM", into=(${algos_names})),
162                           Background          = F_Background("o"),
163                           BackgroundError     = F_BackgroundError("o"),
164                           Observation         = F_Observation("o"),
165                           ObservationError    = F_ObservationError("o"),
166                           ObservationOperator = F_ObservationOperator("o"),
167                           EvolutionModel      = F_EvolutionModel("f"),
168                           EvolutionError      = F_EvolutionError("f"),
169                           ControlInput        = F_ControlInput("f"),
170                           AlgorithmParameters = F_AlgorithmParameters("f"),
171                           UserDataInit        = F_Init("f"),
172                           UserPostAnalysis    = F_UserPostAnalysis("f"),
173                           InputVariables      = F_variables("f"),
174                           OutputVariables     = F_variables("f"),
175                           Observers           = F_Observers("f")
176                          )
177
178 CHECKING_STUDY = PROC(nom="CHECKING_STUDY",
179                           op=None,
180                           repetable           = "n",
181                           Study_name          = SIMP(statut="o", typ = "TXM", defaut="ADAO Checking Case"),
182                           Study_repertory     = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
183                           Debug               = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
184                           Algorithm           = SIMP(statut="o", typ = "TXM", into=(${check_names})),
185                           CheckingPoint       = F_CheckingPoint("o"),
186                           ObservationOperator = F_ObservationOperator("o"),
187                           AlgorithmParameters = F_AlgorithmParameters("f"),
188                           UserDataInit        = F_Init("f"),
189                          )
190 """
191
192 begin_catalog_file = string.Template(begin_catalog_file)
193 data_method = string.Template(data_method)
194 assim_data_method = string.Template(assim_data_method)
195 assim_data_choice = string.Template(assim_data_choice)
196 assim_study = string.Template(assim_study)
197 observers_method = string.Template(observers_method)
198 observers_choice = string.Template(observers_choice)
199
200 #----------- End of Templates Part ---------------#
201
202
203
204 #----------- Begin generation script -----------#
205 print "-- Starting AdaoCalatogGenerator.py --"
206
207 try:
208   import daEficas
209   import daYacsSchemaCreator
210   import daCore.AssimilationStudy
211   import daYacsSchemaCreator.infos_daComposant as infos
212 except:
213   logging.fatal("Import of ADAO python modules failed !" +
214                 "\n add ADAO python installation directory in your PYTHONPATH")
215   traceback.print_exc()
216   sys.exit(1)
217
218 def check_args(args):
219   logging.debug("Arguments are :" + str(args))
220   if len(args) != 2:
221     logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
222     sys.exit(1)
223
224 # Parse arguments
225 from optparse import OptionParser
226 usage = "usage: %prog [options] catalog_path catalog_name"
227 version="%prog 0.1"
228 my_parser = OptionParser(usage=usage, version=version)
229 (options, args) = my_parser.parse_args()
230 check_args(args)
231
232 catalog_path =  args[0]
233 catalog_name =  args[1]
234
235 # Generates into a string
236 mem_file = StringIO.StringIO()
237
238 # Start file
239 from time import strftime
240 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
241
242 # Step 1: A partir des infos, on cree les fonctions qui vont permettre
243 # d'entrer les donnees utilisateur
244 for data_input_name in infos.DataTypeDict.keys():
245   logging.debug('A data input Type is found: ' + data_input_name)
246   data_name = data_input_name
247   data_into = ""
248   data_default = ""
249
250   # On recupere les differentes facon d'entrer les donnees
251   for basic_type in infos.DataTypeDict[data_input_name]:
252     data_into += "\"" + basic_type + "\", "
253
254   # On choisit le default
255   data_default = "\"" + infos.DataTypeDefaultDict[data_input_name] + "\""
256
257   mem_file.write(data_method.substitute(data_name    = data_name,
258                                         data_into    = data_into,
259                                         data_default = data_default))
260
261 # Step 2: On cree les fonctions qui permettent de rentrer les donnees des algorithmes
262 for assim_data_input_name in infos.AssimDataDict.keys():
263   logging.debug("An assimilation algorithm data input is found: " + assim_data_input_name)
264   assim_name = assim_data_input_name
265   storage = ""
266   choices = ""
267   default_choice = ""
268   decl_choices = ""
269   decl_opts = ""
270   if infos.AssimDataDefaultDict[assim_data_input_name] in infos.StoredAssimData:
271     storage = "                                          Stored = SIMP(statut=\"o\", typ = \"I\", into=(0, 1), defaut=0, fr=\"Choix de stockage interne ou non du concept parent\", ang=\"Choice of the storage or not of the parent concept\"),"
272   for choice in infos.AssimDataDict[assim_data_input_name]:
273     choices += "\"" + choice + "\", "
274     decl_choices += assim_data_choice.substitute(choice_name = choice)
275     if choice in infos.StoredAssimData:
276       storage = "                                          Stored = SIMP(statut=\"o\", typ = \"I\", into=(0, 1), defaut=0, fr=\"Choix de stockage interne ou non du concept parent\", ang=\"Choice of the storage or not of the parent concept\"),"
277   default_choice = "\"" + infos.AssimDataDefaultDict[assim_data_input_name] + "\""
278
279   mem_file.write(assim_data_method.substitute(assim_name = assim_name,
280                                               storage = storage,
281                                               choices = choices,
282                                               decl_choices = decl_choices,
283                                               default_choice=default_choice))
284
285 # Step 3: On ajoute les fonctions representant les options possibles
286 for opt_name in infos.OptDict.keys():
287   logging.debug("An optional node is found: " + opt_name)
288   data_name = opt_name
289   data_into = ""
290   data_default = ""
291
292   for choice in infos.OptDict[opt_name]:
293     data_into += "\"" + choice + "\", "
294   data_default = "\"" + infos.OptDefaultDict[opt_name] + "\""
295
296   mem_file.write(data_method.substitute(data_name = data_name,
297                                         data_into = data_into,
298                                         data_default = data_default))
299
300 # Step 4: On ajoute la methode optionnelle init
301 # TODO uniformiser avec le step 3
302 mem_file.write(init_method)
303
304 # Step 5: Add observers
305 decl_choices = ""
306 for obs_var in infos.ObserversList:
307   decl_choices += observers_choice.substitute(var_name=obs_var)
308 mem_file.write(observers_method.substitute(choices = infos.ObserversList,
309                                            decl_choices = decl_choices))
310
311 # Final step: Add algorithm and assim_study
312 algos_names = ""
313 check_names = ""
314 decl_algos  = ""
315
316 assim_study_object = daCore.AssimilationStudy.AssimilationStudy()
317 algos_list = assim_study_object.get_available_algorithms()
318 for algo_name in algos_list:
319   if algo_name in infos.AssimAlgos:
320     logging.debug("An assimilation algorithm is found: " + algo_name)
321     algos_names += "\"" + algo_name + "\", "
322   elif algo_name in infos.CheckAlgos:
323     logging.debug("A checking algorithm is found: " + algo_name)
324     check_names += "\"" + algo_name + "\", "
325   else:
326     logging.debug("This algorithm is not considered: " + algo_name)
327
328 mem_file.write(assim_study.substitute(algos_names=algos_names,
329                                       check_names=check_names,
330                                       decl_algos=decl_algos))
331 # Write file
332 final_file = open(catalog_path + "/" + catalog_name, "wr")
333 final_file.write(mem_file.getvalue())
334 mem_file.close()
335 final_file.close()
336