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.DEBUG)
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",
71 "AlgorithmParameters",
75 def F_Init(statut) : return FACT(statut = statut,
76 INIT_FILE = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr())),
77 TARGET_LIST = SIMP(statut = "o", typ = "TXM", min=1, max="**", into=F_InitChoice(), validators=(VerifExiste(2))),
81 assim_data_method = """
82 def F_${assim_name}(statut) : return FACT(statut=statut,
83 INPUT_TYPE = SIMP(statut="o", typ = "TXM", into=(${choices}), defaut=${default_choice}),
88 assim_data_choice = """
89 ${choice_name} = BLOC ( condition = " INPUT_TYPE in ( '${choice_name}', ) ",
90 data = F_${choice_name}("o"),
94 observers_choice = """
95 ${var_name} = BLOC (condition=" '${var_name}' in set(SELECTION) ",
96 ${var_name}_data = FACT(statut = "o",
97 Scheduler = SIMP(statut = "f", typ = "TXM"),
98 Info = SIMP(statut = "f", typ = "TXM"),
99 NodeType = SIMP(statut = "o", typ = "TXM", min=1, max=1, defaut = "", into=("String", "Script")),
100 PythonScript = BLOC (condition = " NodeType == 'String' ",
101 Value = SIMP(statut = "o", typ = "TXM")
103 UserFile = BLOC (condition = " NodeType == 'Script' ",
104 Value = SIMP(statut = "o", typ = "FichierNoAbs", validators=(OnlyStr()))
110 observers_method = """
111 def F_Observers(statut) : return FACT(statut=statut,
112 SELECTION = SIMP(statut="o", defaut=[], typ="TXM", min=0, max="**", validators=NoRepeat(), into=(${choices})),
119 def F_variables(statut) : return FACT(statut=statut,
120 regles = ( MEME_NOMBRE ('NAMES', 'SIZES')),
121 NAMES = SIMP(statut="o", typ="TXM", max="**", validators=NoRepeat()),
122 SIZES = SIMP(statut="o", typ="I", val_min=1, max="**")
125 ASSIMILATION_STUDY = PROC(nom="ASSIMILATION_STUDY",
128 Study_name = SIMP(statut="o", typ = "TXM"),
129 Study_repertory = SIMP(statut="f", typ = "TXM"),
130 Debug = SIMP(statut="o", typ = "I", into=(0, 1), defaut=0),
131 Algorithm = SIMP(statut="o", typ = "TXM", into=(${algos_names})),
132 Background = F_Background("o"),
133 BackgroundError = F_BackgroundError("o"),
134 Observation = F_Observation("o"),
135 ObservationError = F_ObservationError("o"),
136 ObservationOperator = F_ObservationOperator("o"),
137 AlgorithmParameters = F_AlgorithmParameters("f"),
138 UserDataInit = F_Init("f"),
139 UserPostAnalysis = F_UserPostAnalysis("f"),
140 InputVariables = F_variables("f"),
141 OutputVariables = F_variables("f"),
142 Observers = F_Observers("f")
146 begin_catalog_file = string.Template(begin_catalog_file)
147 data_method = string.Template(data_method)
148 assim_data_method = string.Template(assim_data_method)
149 assim_data_choice = string.Template(assim_data_choice)
150 assim_study = string.Template(assim_study)
151 observers_method = string.Template(observers_method)
152 observers_choice = string.Template(observers_choice)
154 #----------- End of Templates Part ---------------#
158 #----------- Begin generation script -----------#
159 print "-- Starting AdaoCalatogGenerator.py --"
163 import daYacsSchemaCreator
164 import daCore.AssimilationStudy
165 import daYacsSchemaCreator.infos_daComposant as infos
167 logging.fatal("Import of ADAO python modules failed !" +
168 "\n add ADAO python installation directory in your PYTHONPATH")
169 traceback.print_exc()
172 def check_args(args):
173 logging.debug("Arguments are :" + str(args))
175 logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
179 from optparse import OptionParser
180 usage = "usage: %prog [options] catalog_path catalog_name"
182 my_parser = OptionParser(usage=usage, version=version)
183 (options, args) = my_parser.parse_args()
186 catalog_path = args[0]
187 catalog_name = args[1]
189 # Generates into a string
190 mem_file = StringIO.StringIO()
193 from time import strftime
194 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
196 # Step 1: A partir des infos, on crée les fonctions qui vont permettre
197 # d'entrer les données utilisateur
198 for data_input_name in infos.DataTypeDict.keys():
199 logging.debug('A data input Type is found: ' + data_input_name)
200 data_name = data_input_name
204 # On récupère les différentes façon d'entrer les données
205 for basic_type in infos.DataTypeDict[data_input_name]:
206 data_into += "\"" + basic_type + "\", "
208 # On choisit le défault
209 data_default = "\"" + infos.DataTypeDefaultDict[data_input_name] + "\""
211 mem_file.write(data_method.substitute(data_name = data_name,
212 data_into = data_into,
213 data_default = data_default))
215 # Step 2: On crée les fonctions qui permettent de rentrer les données des algorithmes
216 for assim_data_input_name in infos.AssimDataDict.keys():
217 logging.debug("An assimilation algorithm data input is found: " + assim_data_input_name)
218 assim_name = assim_data_input_name
223 for choice in infos.AssimDataDict[assim_data_input_name]:
224 choices += "\"" + choice + "\", "
225 decl_choices += assim_data_choice.substitute(choice_name = choice)
226 default_choice = "\"" + infos.AssimDataDefaultDict[assim_data_input_name] + "\""
228 mem_file.write(assim_data_method.substitute(assim_name = assim_name,
230 decl_choices = decl_choices,
231 default_choice=default_choice))
233 # Step 3: On ajoute les fonctions représentant les options possibles
234 for opt_name in infos.OptDict.keys():
235 logging.debug("An optional node is found: " + opt_name)
240 for choice in infos.OptDict[opt_name]:
241 data_into += "\"" + choice + "\", "
242 data_default = "\"" + infos.OptDefaultDict[opt_name] + "\""
244 mem_file.write(data_method.substitute(data_name = data_name,
245 data_into = data_into,
246 data_default = data_default))
248 # Step 4: On ajoute la méthode optionnelle init
249 # TODO uniformiser avec le step 3
250 mem_file.write(init_method)
252 # Step 5: Add observers
254 for obs_var in infos.ObserversList:
255 decl_choices += observers_choice.substitute(var_name=obs_var)
256 mem_file.write(observers_method.substitute(choices = infos.ObserversList,
257 decl_choices = decl_choices))
259 # Final step: Add algorithm and assim_study
263 assim_study_object = daCore.AssimilationStudy.AssimilationStudy()
264 algos_list = assim_study_object.get_available_algorithms()
265 for algo_name in algos_list:
266 logging.debug("An assimilation algorithm is found: " + algo_name)
267 algos_names += "\"" + algo_name + "\", "
269 mem_file.write(assim_study.substitute(algos_names=algos_names,
270 decl_algos=decl_algos))
272 final_file = open(catalog_path + "/" + catalog_name, "wr")
273 final_file.write(mem_file.getvalue())