Salome HOME
Mentioning licence terms
[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                                          TEMPLATE_DATA =  BLOC (condition = " FROM in ( 'Template', ) ",
75                                              Template = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "AnalysisPrinter", into=("AnalysisPrinter", "AnalysisSaver", "AnalysisPrinterAndSaver")),
76                                              AnalysisPrinter = BLOC (condition = " Template == 'AnalysisPrinter' ",
77                                                  ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy\\nxa=numpy.ravel(ADD.get('Analysis')[-1])\\nprint 'Analysis:',xa" ),
78                                              ),
79                                              AnalysisSaver = BLOC (condition = " Template == 'AnalysisSaver' ",
80                                                  ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy\\nxa=numpy.ravel(ADD.get('Analysis')[-1])\\nf='/tmp/analysis.txt'\\nprint 'Analysis saved in \\"%s\\"'%f\\nnumpy.savetxt(f,xa)" ),
81                                              ),
82                                              AnalysisPrinterAndSaver = BLOC (condition = " Template == 'AnalysisPrinterAndSaver' ",
83                                                  ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy\\nxa=numpy.ravel(ADD.get('Analysis')[-1])\\nprint 'Analysis:',xa\\nf='/tmp/analysis.txt'\\nprint 'Analysis saved in \\"%s\\"'%f\\nnumpy.savetxt(f,xa)" ),
84                                              ),
85                                          ),
86                                     )
87 """
88
89 init_method = """
90 def F_InitChoice() : return  ("Background",
91                               "BackgroundError",
92                               "Observation",
93                               "ObservationError",
94                               "ObservationOperator",
95                               "EvolutionModel",
96                               "EvolutionError",
97                               "AlgorithmParameters",
98                               "UserPostAnalysis",
99                              )
100
101 def F_Init(statut) : return FACT(statut = statut,
102                                  INIT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
103                                  TARGET_LIST = SIMP(statut = "o", typ = "TXM", min=1, max="**", into=F_InitChoice(),  validators=(VerifExiste(2))),
104                                 )
105 """
106
107 assim_data_method = """
108 def F_${assim_name}(statut) : return FACT(statut=statut,
109 ${storage}
110                                           INPUT_TYPE = SIMP(statut="o", typ = "TXM", into=(${choices}), defaut=${default_choice}),
111 ${decl_choices}
112                                                 )
113 """
114
115 assim_data_choice = """
116                                                  ${choice_name} = BLOC ( condition = " INPUT_TYPE in ( '${choice_name}', ) ",
117                                                  data = F_${choice_name}("o"),
118                                                  ),
119 """
120
121 observers_choice = """
122                                        ${var_name} = BLOC (condition=" '${var_name}' in set(SELECTION) ",
123                                            ${var_name}_data = FACT(statut = "o",
124                                                Scheduler    = SIMP(statut = "f", typ = "TXM"),
125                                                Info         = SIMP(statut = "o", typ = "TXM", defaut = "${var_name}"),
126                                                NodeType     = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "Template", into=("String", "Script", "Template")),
127                                                PythonScript = BLOC (condition = " NodeType == 'String' ",
128                                                    Value = SIMP(statut = "o", typ = "TXM")
129                                                ),
130                                                UserFile = BLOC (condition = " NodeType == 'Script' ",
131                                                    Value = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()))
132                                                ),
133                                                ObserverTemplate =  BLOC (condition = " NodeType == 'Template' ",
134                                                    Template = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "ValuePrinter", into=("ValuePrinter", "ValueSeriePrinter", "ValueSaver", "ValueSerieSaver", "ValuePrinterAndSaver", "ValueSeriePrinterAndSaver", "ValueGnuPlotter", "ValueSerieGnuPlotter")),
135                                                    ValuePrinter = BLOC (condition = " Template == 'ValuePrinter' ",
136                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "print info,var[-1]" ),
137                                                    ),
138                                                    ValueSeriePrinter = BLOC (condition = " Template == 'ValueSeriePrinter' ",
139                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "print info,var[:]" ),
140                                                    ),
141                                                    ValueSaver = BLOC (condition = " Template == 'ValueSaver' ",
142                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy,re\\nv=numpy.array((var[-1]))\\nglobal istep\\ntry:\\n    istep += 1\\nexcept:\\n    istep = 0\\nf='/tmp/value_%s_%05i.txt'%(info,istep)\\nf=re.sub('\\s','_',f)\\nprint 'Value saved in \\"%s\\"'%f\\nnumpy.savetxt(f,v)" ),
143                                                    ),
144                                                    ValueSerieSaver = BLOC (condition = " Template == 'ValueSerieSaver' ",
145                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy,re\\nv=numpy.array((var[:])) \\nglobal istep\\ntry:\\n    istep += 1\\nexcept:\\n    istep = 0\\nf='/tmp/value_%s_%05i.txt'%(info,istep)\\nf=re.sub('\\s','_',f)\\nprint 'Value saved in \\"%s\\"'%f\\nnumpy.savetxt(f,v)" ),
146                                                    ),
147                                                    ValuePrinterAndSaver = BLOC (condition = " Template == 'ValuePrinterAndSaver' ",
148                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy,re\\nv=numpy.array((var[-1]))\\nprint info,v\\nglobal istep\\ntry:\\n    istep += 1\\nexcept:\\n    istep = 0\\nf='/tmp/value_%s_%05i.txt'%(info,istep)\\nf=re.sub('\\s','_',f)\\nprint 'Value saved in \\"%s\\"'%f\\nnumpy.savetxt(f,v)" ),
149                                                    ),
150                                                    ValueSeriePrinterAndSaver = BLOC (condition = " Template == 'ValueSeriePrinterAndSaver' ",
151                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy,re\\nv=numpy.array((var[:])) \\nprint info,v\\nglobal istep\\ntry:\\n    istep += 1\\nexcept:\\n    istep = 0\\nf='/tmp/value_%s_%05i.txt'%(info,istep)\\nf=re.sub('\\s','_',f)\\nprint 'Value saved in \\"%s\\"'%f\\nnumpy.savetxt(f,v)" ),
152                                                    ),
153                                                    ValueGnuPlotter = BLOC (condition = " Template == 'ValueGnuPlotter' ",
154                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import Gnuplot\\nglobal ifig,gp\\ntry:\\n    ifig += 1\\n    gp('set style data lines')\\nexcept:\\n    ifig = 0\\n    gp = Gnuplot.Gnuplot(persist=1)\\n    gp('set style data lines')\\ngp('set title  \\"%s (Figure %i)\\"'%(info,ifig))\\ngp.plot( Gnuplot.Data( var[-1], with_='lines lw 2' ) )" ),
155                                                    ),
156                                                    ValueSerieGnuPlotter = BLOC (condition = " Template == 'ValueSerieGnuPlotter' ",
157                                                        ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import Gnuplot\\nglobal ifig,gp\\ntry:\\n    ifig += 1\\n    gp('set style data lines')\\nexcept:\\n    ifig = 0\\n    gp = Gnuplot.Gnuplot(persist=1)\\n    gp('set style data lines')\\ngp('set title  \\"%s (Figure %i)\\"'%(info,ifig))\\ngp.plot( Gnuplot.Data( var[:], with_='lines lw 2' ) )" ),
158                                                    ),
159                                                ),
160                                            ),
161                                        ),
162 """
163
164 observers_method = """
165 def F_Observers(statut) : return FACT(statut=statut,
166                                       SELECTION = SIMP(statut="o", defaut=[], typ="TXM", min=0, max="**", validators=NoRepeat(), into=(${choices})),
167 ${decl_choices}
168                                      )
169 """
170
171 assim_study = """
172
173 def F_variables(statut) : return FACT(statut=statut,
174                                       regles = ( MEME_NOMBRE ('NAMES', 'SIZES')),
175                                       NAMES = SIMP(statut="o", typ="TXM", max="**", validators=NoRepeat()),
176                                       SIZES = SIMP(statut="o", typ="I", val_min=1, max="**")
177                                       )
178
179 ASSIMILATION_STUDY = PROC(nom="ASSIMILATION_STUDY",
180                           op=None,
181                           repetable           = "n",
182                           Study_name          = SIMP(statut="o", typ = "TXM", defaut="ADAO Calculation Case"),
183                           Study_repertory     = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
184                           Debug               = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
185                           Algorithm           = SIMP(statut="o", typ = "TXM", into=(${algos_names})),
186                           Background          = F_Background("o"),
187                           BackgroundError     = F_BackgroundError("o"),
188                           Observation         = F_Observation("o"),
189                           ObservationError    = F_ObservationError("o"),
190                           ObservationOperator = F_ObservationOperator("o"),
191                           EvolutionModel      = F_EvolutionModel("f"),
192                           EvolutionError      = F_EvolutionError("f"),
193                           ControlInput        = F_ControlInput("f"),
194                           AlgorithmParameters = F_AlgorithmParameters("f"),
195                           UserDataInit        = F_Init("f"),
196                           UserPostAnalysis    = F_UserPostAnalysis("f"),
197                           InputVariables      = F_variables("f"),
198                           OutputVariables     = F_variables("f"),
199                           Observers           = F_Observers("f")
200                          )
201
202 CHECKING_STUDY = PROC(nom="CHECKING_STUDY",
203                           op=None,
204                           repetable           = "n",
205                           Study_name          = SIMP(statut="o", typ = "TXM", defaut="ADAO Checking Case"),
206                           Study_repertory     = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
207                           Debug               = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
208                           Algorithm           = SIMP(statut="o", typ = "TXM", into=(${check_names})),
209                           CheckingPoint       = F_CheckingPoint("o"),
210                           ObservationOperator = F_ObservationOperator("o"),
211                           AlgorithmParameters = F_AlgorithmParameters("f"),
212                           UserDataInit        = F_Init("f"),
213                          )
214 """
215
216 begin_catalog_file = string.Template(begin_catalog_file)
217 data_method = string.Template(data_method)
218 assim_data_method = string.Template(assim_data_method)
219 assim_data_choice = string.Template(assim_data_choice)
220 assim_study = string.Template(assim_study)
221 observers_method = string.Template(observers_method)
222 observers_choice = string.Template(observers_choice)
223
224 #----------- End of Templates Part ---------------#
225
226
227
228 #----------- Begin generation script -----------#
229 print "-- Starting AdaoCalatogGenerator.py --"
230
231 try:
232   import daEficas
233   import daYacsSchemaCreator
234   import daCore.AssimilationStudy
235   import daYacsSchemaCreator.infos_daComposant as infos
236 except:
237   logging.fatal("Import of ADAO python modules failed !" +
238                 "\n add ADAO python installation directory in your PYTHONPATH")
239   traceback.print_exc()
240   sys.exit(1)
241
242 def check_args(args):
243   logging.debug("Arguments are :" + str(args))
244   if len(args) != 2:
245     logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
246     sys.exit(1)
247
248 # Parse arguments
249 from optparse import OptionParser
250 usage = "usage: %prog [options] catalog_path catalog_name"
251 version="%prog 0.1"
252 my_parser = OptionParser(usage=usage, version=version)
253 (options, args) = my_parser.parse_args()
254 check_args(args)
255
256 catalog_path =  args[0]
257 catalog_name =  args[1]
258
259 # Generates into a string
260 mem_file = StringIO.StringIO()
261
262 # Start file
263 from time import strftime
264 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
265
266 # Step 1: A partir des infos, on cree les fonctions qui vont permettre
267 # d'entrer les donnees utilisateur
268 for data_input_name in infos.DataTypeDict.keys():
269   logging.debug('A data input Type is found: ' + data_input_name)
270   data_name = data_input_name
271   data_into = ""
272   data_default = ""
273
274   # On recupere les differentes facon d'entrer les donnees
275   for basic_type in infos.DataTypeDict[data_input_name]:
276     data_into += "\"" + basic_type + "\", "
277
278   # On choisit le default
279   data_default = "\"" + infos.DataTypeDefaultDict[data_input_name] + "\""
280
281   mem_file.write(data_method.substitute(data_name    = data_name,
282                                         data_into    = data_into,
283                                         data_default = data_default))
284
285 # Step 2: On cree les fonctions qui permettent de rentrer les donnees des algorithmes
286 for assim_data_input_name in infos.AssimDataDict.keys():
287   logging.debug("An assimilation algorithm data input is found: " + assim_data_input_name)
288   assim_name = assim_data_input_name
289   storage = ""
290   choices = ""
291   default_choice = ""
292   decl_choices = ""
293   decl_opts = ""
294   if infos.AssimDataDefaultDict[assim_data_input_name] in infos.StoredAssimData:
295     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\"),"
296   for choice in infos.AssimDataDict[assim_data_input_name]:
297     choices += "\"" + choice + "\", "
298     decl_choices += assim_data_choice.substitute(choice_name = choice)
299     if choice in infos.StoredAssimData:
300       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\"),"
301   default_choice = "\"" + infos.AssimDataDefaultDict[assim_data_input_name] + "\""
302
303   mem_file.write(assim_data_method.substitute(assim_name = assim_name,
304                                               storage = storage,
305                                               choices = choices,
306                                               decl_choices = decl_choices,
307                                               default_choice=default_choice))
308
309 # Step 3: On ajoute les fonctions representant les options possibles
310 for opt_name in infos.OptDict.keys():
311   logging.debug("An optional node is found: " + opt_name)
312   data_name = opt_name
313   data_into = ""
314   data_default = ""
315
316   for choice in infos.OptDict[opt_name]:
317     data_into += "\"" + choice + "\", "
318   data_default = "\"" + infos.OptDefaultDict[opt_name] + "\""
319
320   mem_file.write(data_method.substitute(data_name = data_name,
321                                         data_into = data_into,
322                                         data_default = data_default))
323
324 # Step 4: On ajoute la methode optionnelle init
325 # TODO uniformiser avec le step 3
326 mem_file.write(init_method)
327
328 # Step 5: Add observers
329 decl_choices = ""
330 for obs_var in infos.ObserversList:
331   decl_choices += observers_choice.substitute(var_name=obs_var)
332 mem_file.write(observers_method.substitute(choices = infos.ObserversList,
333                                            decl_choices = decl_choices))
334
335 # Final step: Add algorithm and assim_study
336 algos_names = ""
337 check_names = ""
338 decl_algos  = ""
339
340 assim_study_object = daCore.AssimilationStudy.AssimilationStudy()
341 algos_list = assim_study_object.get_available_algorithms()
342 for algo_name in algos_list:
343   if algo_name in infos.AssimAlgos:
344     logging.debug("An assimilation algorithm is found: " + algo_name)
345     algos_names += "\"" + algo_name + "\", "
346   elif algo_name in infos.CheckAlgos:
347     logging.debug("A checking algorithm is found: " + algo_name)
348     check_names += "\"" + algo_name + "\", "
349   else:
350     logging.debug("This algorithm is not considered: " + algo_name)
351
352 mem_file.write(assim_study.substitute(algos_names=algos_names,
353                                       check_names=check_names,
354                                       decl_algos=decl_algos))
355 # Write file
356 final_file = open(catalog_path + "/" + catalog_name, "wr")
357 final_file.write(mem_file.getvalue())
358 mem_file.close()
359 final_file.close()
360