1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2013 EDF R&D
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.
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.
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
19 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
29 logging.basicConfig(level=logging.WARNING)
31 #----------- Templates Part ---------------#
32 begin_catalog_file = """# -*- coding: utf-8 -*-
34 # --------------------------------------------------------
35 # generated by AdaoCatalogGenerator at ${date}
36 # --------------------------------------------------------
41 JdC = JDC_CATA (code = '%s',
43 regles = ( AU_MOINS_UN ('ASSIMILATION_STUDY','CHECKING_STUDY'), AU_PLUS_UN ('ASSIMILATION_STUDY','CHECKING_STUDY')),
45 VERSION_CATALOGUE='%s'
46 """%(module_version.name,module_version.version)
49 def F_${data_name}(statut) : return FACT(statut = statut,
50 FROM = SIMP(statut = "o", typ = "TXM", into=(${data_into}), defaut=${data_default}),
51 SCRIPT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
53 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"),
55 STRING_DATA = BLOC ( condition = " FROM in ( 'String', ) ",
57 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"),
59 SCRIPTWITHFUNCTIONS_DATA = BLOC ( condition = " FROM in ( 'ScriptWithFunctions', ) ",
61 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"),
63 SCRIPTWITHONEFUNCTION_DATA = BLOC ( condition = " FROM in ( 'ScriptWithOneFunction', ) ",
65 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"),
66 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"),
67 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"),
69 SCRIPTWITHSWITCH_DATA = BLOC ( condition = " FROM in ( 'ScriptWithSwitch', ) ",
71 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"),
73 FUNCTIONDICT_DATA = BLOC ( condition = " FROM in ( 'FunctionDict', ) ",
75 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"),
77 TEMPLATE_DATA = BLOC (condition = " FROM in ( 'Template', ) ",
78 Template = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "AnalysisPrinter", into=("AnalysisPrinter", "AnalysisSaver", "AnalysisPrinterAndSaver")),
79 AnalysisPrinter = BLOC (condition = " Template == 'AnalysisPrinter' ",
80 ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "import numpy\\nxa=numpy.ravel(ADD.get('Analysis')[-1])\\nprint 'Analysis:',xa" ),
82 AnalysisSaver = BLOC (condition = " Template == 'AnalysisSaver' ",
83 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)" ),
85 AnalysisPrinterAndSaver = BLOC (condition = " Template == 'AnalysisPrinterAndSaver' ",
86 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)" ),
93 def F_InitChoice() : return ("Background",
97 "ObservationOperator",
100 "AlgorithmParameters",
104 def F_Init(statut) : return FACT(statut = statut,
105 INIT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
106 TARGET_LIST = SIMP(statut = "o", typ = "TXM", min=1, max="**", into=F_InitChoice(), validators=(VerifExiste(2))),
110 assim_data_method = """
111 def F_${assim_name}(statut) : return FACT(statut=statut,
113 INPUT_TYPE = SIMP(statut="o", typ = "TXM", into=(${choices}), defaut=${default_choice}),
118 assim_data_choice = """
119 ${choice_name} = BLOC ( condition = " INPUT_TYPE in ( '${choice_name}', ) ",
120 data = F_${choice_name}("o"),
124 observers_choice = """
125 ${var_name} = BLOC (condition=" '${var_name}' in set(SELECTION) ",
126 ${var_name}_data = FACT(statut = "o",
127 Scheduler = SIMP(statut = "f", typ = "TXM"),
128 Info = SIMP(statut = "o", typ = "TXM", defaut = "${var_name}"),
129 NodeType = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "Template", into=("String", "Script", "Template")),
130 PythonScript = BLOC (condition = " NodeType == 'String' ",
131 Value = SIMP(statut = "o", typ = "TXM")
133 UserFile = BLOC (condition = " NodeType == 'Script' ",
134 Value = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()))
136 ObserverTemplate = BLOC (condition = " NodeType == 'Template' ",
137 Template = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "ValuePrinter", into=("ValuePrinter", "ValueSeriePrinter", "ValueSaver", "ValueSerieSaver", "ValuePrinterAndSaver", "ValueSeriePrinterAndSaver", "ValueGnuPlotter", "ValueSerieGnuPlotter")),
138 ValuePrinter = BLOC (condition = " Template == 'ValuePrinter' ",
139 ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "print info,var[-1]" ),
141 ValueSeriePrinter = BLOC (condition = " Template == 'ValueSeriePrinter' ",
142 ValueTemplate = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "print info,var[:]" ),
144 ValueSaver = BLOC (condition = " Template == 'ValueSaver' ",
145 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)" ),
147 ValueSerieSaver = BLOC (condition = " Template == 'ValueSerieSaver' ",
148 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)" ),
150 ValuePrinterAndSaver = BLOC (condition = " Template == 'ValuePrinterAndSaver' ",
151 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)" ),
153 ValueSeriePrinterAndSaver = BLOC (condition = " Template == 'ValueSeriePrinterAndSaver' ",
154 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)" ),
156 ValueGnuPlotter = BLOC (condition = " Template == 'ValueGnuPlotter' ",
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[-1], with_='lines lw 2' ) )" ),
159 ValueSerieGnuPlotter = BLOC (condition = " Template == 'ValueSerieGnuPlotter' ",
160 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' ) )" ),
167 observers_method = """
168 def F_Observers(statut) : return FACT(statut=statut,
169 SELECTION = SIMP(statut="o", defaut=[], typ="TXM", min=0, max="**", validators=NoRepeat(), into=(${choices})),
176 def F_variables(statut) : return FACT(statut=statut,
177 regles = ( MEME_NOMBRE ('NAMES', 'SIZES')),
178 NAMES = SIMP(statut="o", typ="TXM", max="**", validators=NoRepeat()),
179 SIZES = SIMP(statut="o", typ="I", val_min=1, max="**")
182 ASSIMILATION_STUDY = PROC(nom="ASSIMILATION_STUDY",
185 Study_name = SIMP(statut="o", typ = "TXM", defaut="ADAO Calculation Case"),
186 Study_repertory = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
187 Debug = SIMP(statut="f", typ = "I", into=(0, 1), defaut=0),
188 Algorithm = SIMP(statut="o", typ = "TXM", into=(${algos_names})),
189 Background = F_Background("o"),
190 BackgroundError = F_BackgroundError("o"),
191 Observation = F_Observation("o"),
192 ObservationError = F_ObservationError("o"),
193 ObservationOperator = F_ObservationOperator("o"),
194 EvolutionModel = F_EvolutionModel("f"),
195 EvolutionError = F_EvolutionError("f"),
196 ControlInput = F_ControlInput("f"),
197 AlgorithmParameters = F_AlgorithmParameters("f"),
198 UserDataInit = F_Init("f"),
199 UserPostAnalysis = F_UserPostAnalysis("f"),
200 InputVariables = F_variables("f"),
201 OutputVariables = F_variables("f"),
202 Observers = F_Observers("f")
205 CHECKING_STUDY = PROC(nom="CHECKING_STUDY",
208 Study_name = SIMP(statut="o", typ = "TXM", defaut="ADAO Checking Case"),
209 Study_repertory = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
210 Debug = SIMP(statut="f", typ = "I", into=(0, 1), defaut=0),
211 Algorithm = SIMP(statut="o", typ = "TXM", into=(${check_names})),
212 CheckingPoint = F_CheckingPoint("o"),
213 ObservationOperator = F_ObservationOperator("o"),
214 AlgorithmParameters = F_AlgorithmParameters("f"),
215 UserDataInit = F_Init("f"),
219 begin_catalog_file = string.Template(begin_catalog_file)
220 data_method = string.Template(data_method)
221 assim_data_method = string.Template(assim_data_method)
222 assim_data_choice = string.Template(assim_data_choice)
223 assim_study = string.Template(assim_study)
224 observers_method = string.Template(observers_method)
225 observers_choice = string.Template(observers_choice)
227 #----------- End of Templates Part ---------------#
231 #----------- Begin generation script -----------#
232 print "-- Starting AdaoCalatogGenerator.py --"
236 import daYacsSchemaCreator
237 import daCore.AssimilationStudy
238 import daYacsSchemaCreator.infos_daComposant as infos
240 logging.fatal("Import of ADAO python modules failed !" +
241 "\n add ADAO python installation directory in your PYTHONPATH")
242 traceback.print_exc()
245 def check_args(args):
246 logging.debug("Arguments are :" + str(args))
248 logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
252 from optparse import OptionParser
253 usage = "usage: %prog [options] catalog_path catalog_name"
255 my_parser = OptionParser(usage=usage, version=version)
256 (options, args) = my_parser.parse_args()
259 catalog_path = args[0]
260 catalog_name = args[1]
262 # Generates into a string
263 mem_file = StringIO.StringIO()
266 from time import strftime
267 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
269 # Step 1: A partir des infos, on cree les fonctions qui vont permettre
270 # d'entrer les donnees utilisateur
271 for data_input_name in infos.DataTypeDict.keys():
272 logging.debug('A data input Type is found: ' + data_input_name)
273 data_name = data_input_name
277 # On recupere les differentes facon d'entrer les donnees
278 for basic_type in infos.DataTypeDict[data_input_name]:
279 data_into += "\"" + basic_type + "\", "
281 # On choisit le default
282 data_default = "\"" + infos.DataTypeDefaultDict[data_input_name] + "\""
284 mem_file.write(data_method.substitute(data_name = data_name,
285 data_into = data_into,
286 data_default = data_default))
288 # Step 2: On cree les fonctions qui permettent de rentrer les donnees des algorithmes
289 for assim_data_input_name in infos.AssimDataDict.keys():
290 logging.debug("An assimilation algorithm data input is found: " + assim_data_input_name)
291 assim_name = assim_data_input_name
297 if infos.AssimDataDefaultDict[assim_data_input_name] in infos.StoredAssimData:
298 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\"),"
299 for choice in infos.AssimDataDict[assim_data_input_name]:
300 choices += "\"" + choice + "\", "
301 decl_choices += assim_data_choice.substitute(choice_name = choice)
302 if choice in infos.StoredAssimData:
303 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\"),"
304 default_choice = "\"" + infos.AssimDataDefaultDict[assim_data_input_name] + "\""
306 mem_file.write(assim_data_method.substitute(assim_name = assim_name,
309 decl_choices = decl_choices,
310 default_choice=default_choice))
312 # Step 3: On ajoute les fonctions representant les options possibles
313 for opt_name in infos.OptDict.keys():
314 logging.debug("An optional node is found: " + opt_name)
319 for choice in infos.OptDict[opt_name]:
320 data_into += "\"" + choice + "\", "
321 data_default = "\"" + infos.OptDefaultDict[opt_name] + "\""
323 mem_file.write(data_method.substitute(data_name = data_name,
324 data_into = data_into,
325 data_default = data_default))
327 # Step 4: On ajoute la methode optionnelle init
328 # TODO uniformiser avec le step 3
329 mem_file.write(init_method)
331 # Step 5: Add observers
333 for obs_var in infos.ObserversList:
334 decl_choices += observers_choice.substitute(var_name=obs_var)
335 mem_file.write(observers_method.substitute(choices = infos.ObserversList,
336 decl_choices = decl_choices))
338 # Final step: Add algorithm and assim_study
343 assim_study_object = daCore.AssimilationStudy.AssimilationStudy()
344 algos_list = assim_study_object.get_available_algorithms()
345 for algo_name in algos_list:
346 if algo_name in infos.AssimAlgos:
347 logging.debug("An assimilation algorithm is found: " + algo_name)
348 algos_names += "\"" + algo_name + "\", "
349 elif algo_name in infos.CheckAlgos:
350 logging.debug("A checking algorithm is found: " + algo_name)
351 check_names += "\"" + algo_name + "\", "
353 logging.debug("This algorithm is not considered: " + algo_name)
355 mem_file.write(assim_study.substitute(algos_names=algos_names,
356 check_names=check_names,
357 decl_algos=decl_algos))
359 final_file = open(catalog_path + "/" + catalog_name, "wr")
360 final_file.write(mem_file.getvalue())