1 #-*-coding:iso-8859-1-*-
3 # Copyright (C) 2008-2012 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
27 logging.basicConfig(level=logging.INFO)
29 #----------- Templates Part ---------------#
30 begin_catalog_file = """# -*- coding: utf-8 -*-
32 # --------------------------------------------------------
33 # generated by AdaoCatalogGenerator at ${date}
34 # --------------------------------------------------------
39 JdC = JDC_CATA (code = 'ADAO',
41 regles = ( AU_MOINS_UN ('ASSIMILATION_STUDY','CHECKING_STUDY'), AU_PLUS_UN ('ASSIMILATION_STUDY','CHECKING_STUDY')),
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', ) ",
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"),
52 STRING_DATA = BLOC ( condition = " FROM in ( 'String', ) ",
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"),
56 SCRIPTWITHFUNCTIONS_DATA = BLOC ( condition = " FROM in ( 'ScriptWithFunctions', ) ",
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"),
60 SCRIPTWITHONEFUNCTION_DATA = BLOC ( condition = " FROM in ( 'ScriptWithOneFunction', ) ",
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"),
65 SCRIPTWITHSWITCH_DATA = BLOC ( condition = " FROM in ( 'ScriptWithSwitch', ) ",
67 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 FUNCTIONDICT_DATA = BLOC ( condition = " FROM in ( 'FunctionDict', ) ",
71 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 def F_InitChoice() : return ("Background",
81 "ObservationOperator",
84 "AlgorithmParameters",
88 def F_Init(statut) : return FACT(statut = statut,
89 INIT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
90 TARGET_LIST = SIMP(statut = "o", typ = "TXM", min=1, max="**", into=F_InitChoice(), validators=(VerifExiste(2))),
94 assim_data_method = """
95 def F_${assim_name}(statut) : return FACT(statut=statut,
97 INPUT_TYPE = SIMP(statut="o", typ = "TXM", into=(${choices}), defaut=${default_choice}),
102 assim_data_choice = """
103 ${choice_name} = BLOC ( condition = " INPUT_TYPE in ( '${choice_name}', ) ",
104 data = F_${choice_name}("o"),
108 observers_choice = """
109 ${var_name} = BLOC (condition=" '${var_name}' in set(SELECTION) ",
110 ${var_name}_data = FACT(statut = "o",
111 Scheduler = SIMP(statut = "f", typ = "TXM"),
112 Info = SIMP(statut = "f", typ = "TXM"),
113 NodeType = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "", into=("String", "Script")),
114 PythonScript = BLOC (condition = " NodeType == 'String' ",
115 Value = SIMP(statut = "o", typ = "TXM")
117 UserFile = BLOC (condition = " NodeType == 'Script' ",
118 Value = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()))
124 observers_method = """
125 def F_Observers(statut) : return FACT(statut=statut,
126 SELECTION = SIMP(statut="o", defaut=[], typ="TXM", min=0, max="**", validators=NoRepeat(), into=(${choices})),
133 def F_variables(statut) : return FACT(statut=statut,
134 regles = ( MEME_NOMBRE ('NAMES', 'SIZES')),
135 NAMES = SIMP(statut="o", typ="TXM", max="**", validators=NoRepeat()),
136 SIZES = SIMP(statut="o", typ="I", val_min=1, max="**")
139 ASSIMILATION_STUDY = PROC(nom="ASSIMILATION_STUDY",
142 Study_name = SIMP(statut="o", typ = "TXM"),
143 Study_repertory = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
144 Debug = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
145 Algorithm = SIMP(statut="o", typ = "TXM", into=(${algos_names})),
146 Background = F_Background("o"),
147 BackgroundError = F_BackgroundError("o"),
148 Observation = F_Observation("o"),
149 ObservationError = F_ObservationError("o"),
150 ObservationOperator = F_ObservationOperator("o"),
151 EvolutionModel = F_EvolutionModel("f"),
152 EvolutionError = F_EvolutionError("f"),
153 ControlInput = F_ControlInput("f"),
154 AlgorithmParameters = F_AlgorithmParameters("f"),
155 UserDataInit = F_Init("f"),
156 UserPostAnalysis = F_UserPostAnalysis("f"),
157 InputVariables = F_variables("f"),
158 OutputVariables = F_variables("f"),
159 Observers = F_Observers("f")
162 CHECKING_STUDY = PROC(nom="CHECKING_STUDY",
165 Study_name = SIMP(statut="o", typ = "TXM"),
166 Study_repertory = SIMP(statut="f", typ = "Repertoire", min=1, max=1),
167 Debug = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
168 Algorithm = SIMP(statut="o", typ = "TXM", into=(${check_names})),
169 CheckingPoint = F_CheckingPoint("o"),
170 ObservationOperator = F_ObservationOperator("o"),
171 AlgorithmParameters = F_AlgorithmParameters("f"),
172 UserDataInit = F_Init("f"),
176 begin_catalog_file = string.Template(begin_catalog_file)
177 data_method = string.Template(data_method)
178 assim_data_method = string.Template(assim_data_method)
179 assim_data_choice = string.Template(assim_data_choice)
180 assim_study = string.Template(assim_study)
181 observers_method = string.Template(observers_method)
182 observers_choice = string.Template(observers_choice)
184 #----------- End of Templates Part ---------------#
188 #----------- Begin generation script -----------#
189 print "-- Starting AdaoCalatogGenerator.py --"
193 import daYacsSchemaCreator
194 import daCore.AssimilationStudy
195 import daYacsSchemaCreator.infos_daComposant as infos
197 logging.fatal("Import of ADAO python modules failed !" +
198 "\n add ADAO python installation directory in your PYTHONPATH")
199 traceback.print_exc()
202 def check_args(args):
203 logging.debug("Arguments are :" + str(args))
205 logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
209 from optparse import OptionParser
210 usage = "usage: %prog [options] catalog_path catalog_name"
212 my_parser = OptionParser(usage=usage, version=version)
213 (options, args) = my_parser.parse_args()
216 catalog_path = args[0]
217 catalog_name = args[1]
219 # Generates into a string
220 mem_file = StringIO.StringIO()
223 from time import strftime
224 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
226 # Step 1: A partir des infos, on cree les fonctions qui vont permettre
227 # d'entrer les donnees utilisateur
228 for data_input_name in infos.DataTypeDict.keys():
229 logging.debug('A data input Type is found: ' + data_input_name)
230 data_name = data_input_name
234 # On recupere les differentes facon d'entrer les donnees
235 for basic_type in infos.DataTypeDict[data_input_name]:
236 data_into += "\"" + basic_type + "\", "
238 # On choisit le default
239 data_default = "\"" + infos.DataTypeDefaultDict[data_input_name] + "\""
241 mem_file.write(data_method.substitute(data_name = data_name,
242 data_into = data_into,
243 data_default = data_default))
245 # Step 2: On cree les fonctions qui permettent de rentrer les donnees des algorithmes
246 for assim_data_input_name in infos.AssimDataDict.keys():
247 logging.debug("An assimilation algorithm data input is found: " + assim_data_input_name)
248 assim_name = assim_data_input_name
254 if infos.AssimDataDefaultDict[assim_data_input_name] in infos.StoredAssimData:
255 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\"),"
256 for choice in infos.AssimDataDict[assim_data_input_name]:
257 choices += "\"" + choice + "\", "
258 decl_choices += assim_data_choice.substitute(choice_name = choice)
259 if choice in infos.StoredAssimData:
260 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\"),"
261 default_choice = "\"" + infos.AssimDataDefaultDict[assim_data_input_name] + "\""
263 mem_file.write(assim_data_method.substitute(assim_name = assim_name,
266 decl_choices = decl_choices,
267 default_choice=default_choice))
269 # Step 3: On ajoute les fonctions representant les options possibles
270 for opt_name in infos.OptDict.keys():
271 logging.debug("An optional node is found: " + opt_name)
276 for choice in infos.OptDict[opt_name]:
277 data_into += "\"" + choice + "\", "
278 data_default = "\"" + infos.OptDefaultDict[opt_name] + "\""
280 mem_file.write(data_method.substitute(data_name = data_name,
281 data_into = data_into,
282 data_default = data_default))
284 # Step 4: On ajoute la methode optionnelle init
285 # TODO uniformiser avec le step 3
286 mem_file.write(init_method)
288 # Step 5: Add observers
290 for obs_var in infos.ObserversList:
291 decl_choices += observers_choice.substitute(var_name=obs_var)
292 mem_file.write(observers_method.substitute(choices = infos.ObserversList,
293 decl_choices = decl_choices))
295 # Final step: Add algorithm and assim_study
300 assim_study_object = daCore.AssimilationStudy.AssimilationStudy()
301 algos_list = assim_study_object.get_available_algorithms()
302 for algo_name in algos_list:
303 if algo_name in infos.AssimAlgos:
304 logging.debug("An assimilation algorithm is found: " + algo_name)
305 algos_names += "\"" + algo_name + "\", "
306 elif algo_name in infos.CheckAlgos:
307 logging.debug("A checking algorithm is found: " + algo_name)
308 check_names += "\"" + algo_name + "\", "
310 logging.debug("This algorithm is not considered: " + algo_name)
312 mem_file.write(assim_study.substitute(algos_names=algos_names,
313 check_names=check_names,
314 decl_algos=decl_algos))
316 final_file = open(catalog_path + "/" + catalog_name, "wr")
317 final_file.write(mem_file.getvalue())