2 # Copyright (C) 2008-2011 EDF R&D
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.
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.
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
19 # Author : André RIBES (EDF R&D)
28 logging.basicConfig(level=logging.INFO)
30 #----------- Templates Part ---------------#
31 begin_catalog_file = """
32 # -*- coding: utf-8 -*-
34 # --------------------------------------------------------
35 # generated by AdaoCatalogGenerator at ${date}
36 # --------------------------------------------------------
41 JdC = JDC_CATA (code = 'ADAO',
43 regles = ( AU_MOINS_UN ('ASSIMILATION_STUDY'), AU_PLUS_UN ('ASSIMILATION_STUDY')),
48 def F_${data_name}(statut) : return FACT(statut = statut,
49 FROM = SIMP(statut = "o", typ = "TXM", into=(${data_into}), defaut=${data_default}),
50 SCRIPT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
52 SCRIPT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
54 STRING_DATA = BLOC ( condition = " FROM in ( 'String', ) ",
56 STRING = SIMP(statut = "o", typ = "TXM"),
58 FUNCTIONDICT_DATA = BLOC ( condition = " FROM in ( 'FunctionDict', ) ",
60 FUNCTIONDICT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
66 def F_InitChoice() : return ("Background",
70 "ObservationOperator",
73 "AlgorithmParameters",
77 def F_Init(statut) : return FACT(statut = statut,
78 INIT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
79 TARGET_LIST = SIMP(statut = "o", typ = "TXM", min=1, max="**", into=F_InitChoice(), validators=(VerifExiste(2))),
83 assim_data_method = """
84 def F_${assim_name}(statut) : return FACT(statut=statut,
85 INPUT_TYPE = SIMP(statut="o", typ = "TXM", into=(${choices}), defaut=${default_choice}),
90 assim_data_choice = """
91 ${choice_name} = BLOC ( condition = " INPUT_TYPE in ( '${choice_name}', ) ",
92 data = F_${choice_name}("o"),
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")
105 UserFile = BLOC (condition = " NodeType == 'Script' ",
106 Value = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()))
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})),
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="**")
127 ASSIMILATION_STUDY = PROC(nom="ASSIMILATION_STUDY",
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")
150 begin_catalog_file = string.Template(begin_catalog_file)
151 data_method = string.Template(data_method)
152 assim_data_method = string.Template(assim_data_method)
153 assim_data_choice = string.Template(assim_data_choice)
154 assim_study = string.Template(assim_study)
155 observers_method = string.Template(observers_method)
156 observers_choice = string.Template(observers_choice)
158 #----------- End of Templates Part ---------------#
162 #----------- Begin generation script -----------#
163 print "-- Starting AdaoCalatogGenerator.py --"
167 import daYacsSchemaCreator
168 import daCore.AssimilationStudy
169 import daYacsSchemaCreator.infos_daComposant as infos
171 logging.fatal("Import of ADAO python modules failed !" +
172 "\n add ADAO python installation directory in your PYTHONPATH")
173 traceback.print_exc()
176 def check_args(args):
177 logging.debug("Arguments are :" + str(args))
179 logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
183 from optparse import OptionParser
184 usage = "usage: %prog [options] catalog_path catalog_name"
186 my_parser = OptionParser(usage=usage, version=version)
187 (options, args) = my_parser.parse_args()
190 catalog_path = args[0]
191 catalog_name = args[1]
193 # Generates into a string
194 mem_file = StringIO.StringIO()
197 from time import strftime
198 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
200 # Step 1: A partir des infos, on crée les fonctions qui vont permettre
201 # d'entrer les données utilisateur
202 for data_input_name in infos.DataTypeDict.keys():
203 logging.debug('A data input Type is found: ' + data_input_name)
204 data_name = data_input_name
208 # On récupère les différentes façon d'entrer les données
209 for basic_type in infos.DataTypeDict[data_input_name]:
210 data_into += "\"" + basic_type + "\", "
212 # On choisit le défault
213 data_default = "\"" + infos.DataTypeDefaultDict[data_input_name] + "\""
215 mem_file.write(data_method.substitute(data_name = data_name,
216 data_into = data_into,
217 data_default = data_default))
219 # Step 2: On crée les fonctions qui permettent de rentrer les données des algorithmes
220 for assim_data_input_name in infos.AssimDataDict.keys():
221 logging.debug("An assimilation algorithm data input is found: " + assim_data_input_name)
222 assim_name = assim_data_input_name
227 for choice in infos.AssimDataDict[assim_data_input_name]:
228 choices += "\"" + choice + "\", "
229 decl_choices += assim_data_choice.substitute(choice_name = choice)
230 default_choice = "\"" + infos.AssimDataDefaultDict[assim_data_input_name] + "\""
232 mem_file.write(assim_data_method.substitute(assim_name = assim_name,
234 decl_choices = decl_choices,
235 default_choice=default_choice))
237 # Step 3: On ajoute les fonctions représentant les options possibles
238 for opt_name in infos.OptDict.keys():
239 logging.debug("An optional node is found: " + opt_name)
244 for choice in infos.OptDict[opt_name]:
245 data_into += "\"" + choice + "\", "
246 data_default = "\"" + infos.OptDefaultDict[opt_name] + "\""
248 mem_file.write(data_method.substitute(data_name = data_name,
249 data_into = data_into,
250 data_default = data_default))
252 # Step 4: On ajoute la méthode optionnelle init
253 # TODO uniformiser avec le step 3
254 mem_file.write(init_method)
256 # Step 5: Add observers
258 for obs_var in infos.ObserversList:
259 decl_choices += observers_choice.substitute(var_name=obs_var)
260 mem_file.write(observers_method.substitute(choices = infos.ObserversList,
261 decl_choices = decl_choices))
263 # Final step: Add algorithm and assim_study
267 assim_study_object = daCore.AssimilationStudy.AssimilationStudy()
268 algos_list = assim_study_object.get_available_algorithms()
269 for algo_name in algos_list:
270 logging.debug("An assimilation algorithm is found: " + algo_name)
271 algos_names += "\"" + algo_name + "\", "
273 mem_file.write(assim_study.substitute(algos_names=algos_names,
274 decl_algos=decl_algos))
276 final_file = open(catalog_path + "/" + catalog_name, "wr")
277 final_file.write(mem_file.getvalue())