3 # Copyright (C) 2008-2009 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
20 # Author : André RIBES (EDF R&D)
29 logging.basicConfig(level=logging.DEBUG)
31 #----------- Templates Part ---------------#
32 begin_catalog_file = """
33 # -*- coding: utf-8 -*-
35 # --------------------------------------------------------
36 # generated by AdaoCatalogGenerator at ${date}
37 # --------------------------------------------------------
42 JdC = JDC_CATA (code = 'ADAO',
44 regles = ( AU_MOINS_UN ('ASSIM_STUDY')),
48 String_data_bloc = """
49 STRING_DATA = BLOC ( condition = " FROM in ( 'String', ) ",
51 STRING = SIMP(statut = "o", typ = "TXM"),
55 Script_data_bloc = """
56 SCRIPT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
58 SCRIPT_FILE = SIMP(statut = "o", typ = "Fichier"),
63 DICT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
65 SCRIPT_FILE = SIMP(statut = "o", typ = "Fichier"),
69 # Pour l'instant on ne gère qu'un seul script pour toutes les functions
70 FunctionDict_data_bloc = """
71 FUNCTIONDICT_DATA = BLOC ( condition = " FROM in ( 'FunctionDict', ) ",
73 FUNCTIONDICT_FILE = SIMP(statut = "o", typ = "Fichier"),
78 def F_${data_name}(statut) : return FACT(statut = statut,
79 FROM = SIMP(statut = "o", typ = "TXM", into=(${data_into})),
85 def F_InitChoice() : return ("Background",
89 "ObservationOperator",
90 "AlgorithmParameters",
93 def F_Init(statut) : return FACT(statut = statut,
94 INIT_FILE = SIMP(statut = "o", typ = "Fichier"),
95 TARGET_LIST = SIMP(statut = "o", typ = "TXM", min=1, max="**", into=F_InitChoice(), validators=(VerifExiste(2))),
98 assim_data_method = """
99 def F_${assim_name}(statut) : return FACT(statut=statut,
100 regles = ( UN_PARMI (${choices})),
105 assim_data_choice = """
106 ${choice_name} = F_${choice_name}("f"),
109 assim_opt_choice = """
110 ${choice_name} = F_${choice_name}("f"),
114 ${name} = FACT(regles = ( ENSEMBLE ("Background", "BackgroundError",
115 "Observation", "ObservationError",
116 "ObservationOperator")),
117 Background = F_Background("o"),
118 BackgroundError = F_BackgroundError("o"),
119 Observation = F_Observation("o"),
120 ObservationError = F_ObservationError("o"),
121 ObservationOperator = F_ObservationOperator("o"),
122 AlgorithmParameters = F_AlgorithmParameters("f"),
128 ASSIM_STUDY = PROC(nom="ASSIM_STUDY",
131 STUDY_NAME = SIMP(statut="o", typ = "TXM"),
132 ALGORITHM = FACT(statut='o',
133 regles = ( UN_PARMI (${algos}),),
139 begin_catalog_file = string.Template(begin_catalog_file)
140 data_method = string.Template(data_method)
141 assim_data_method = string.Template(assim_data_method)
142 assim_data_choice = string.Template(assim_data_choice)
143 assim_opt_choice = string.Template(assim_opt_choice)
144 assim_algo = string.Template(assim_algo)
145 assim_study = string.Template(assim_study)
147 #----------- End of Templates Part ---------------#
151 #----------- Begin generation script -----------#
152 print "-- Starting AdaoCalatogGenerator.py --"
156 import daYacsSchemaCreator
157 import daCore.AssimilationStudy
159 logging.fatal("Import of ADAO python modules failed !" +
160 "\n add ADAO python installation directory in your PYTHONPATH")
161 traceback.print_exc()
164 def check_args(args):
165 logging.debug("Arguments are :" + str(args))
167 logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
171 from optparse import OptionParser
172 usage = "usage: %prog [options] catalog_path catalog_name"
174 my_parser = OptionParser(usage=usage, version=version)
175 (options, args) = my_parser.parse_args()
178 catalog_path = args[0]
179 catalog_name = args[1]
181 # Generates into a string
182 mem_file = StringIO.StringIO()
185 from time import strftime
186 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
188 # Step 1: Check basic data input types
189 import daYacsSchemaCreator.infos_daComposant as infos
190 for basic_type in infos.BasicDataInputs:
191 logging.debug('A basic data input type is found: ' + basic_type)
192 if basic_type + '_data_bloc' not in locals().keys():
193 logging.fatal("Basic data input type not found: " + basic_type)
196 # Step 2: Add data input dict
197 for data_input_name in infos.DataTypeDict.keys():
198 logging.debug('A data input is found: ' + data_input_name)
199 data_name = data_input_name
203 for basic_type in infos.DataTypeDict[data_input_name]:
204 data_into += "\"" + basic_type + "\", "
205 data_bloc += locals()[basic_type + '_data_bloc'] + "\n"
207 mem_file.write(data_method.substitute(data_name = data_name,
208 data_into = data_into,
209 data_bloc = data_bloc))
211 # Step 3: Add assimilation algorithm data input
212 for assim_data_input_name in infos.AssimDataDict.keys():
213 logging.debug("An assimilation algorithm data input is found: " + assim_data_input_name)
214 assim_name = assim_data_input_name
218 for choice in infos.AssimDataDict[assim_data_input_name]:
219 choices += "\"" + choice + "\", "
220 decl_choices += assim_data_choice.substitute(choice_name = choice)
222 mem_file.write(assim_data_method.substitute(assim_name = assim_name,
224 decl_choices = decl_choices))
226 # Step 4: Add optional nodes
228 for opt_name in infos.OptDict.keys():
229 logging.debug("An optional node is found: " + opt_name)
234 for choice in infos.OptDict[opt_name]:
235 data_into += "\"" + choice + "\", "
236 data_bloc += locals()[choice + '_data_bloc'] + "\n"
238 mem_file.write(data_method.substitute(data_name = data_name,
239 data_into = data_into,
240 data_bloc = data_bloc))
242 opt_names.append(opt_name)
244 # Step 5: Add init node
245 mem_file.write(init_method)
247 # Final step: Add algorithm and assim_study
251 for opt_name in opt_names:
252 decl_opts += assim_opt_choice.substitute(choice_name = opt_name)
254 assim_study_object = daCore.AssimilationStudy.AssimilationStudy()
255 algos_list = assim_study_object.get_available_algorithms()
256 for algo_name in algos_list:
257 logging.debug("An assimilation algorithm is found: " + algo_name)
258 if algo_name == "3DVAR":
259 algo_name = "ThreeDVAR"
260 algos += "\"" + algo_name + "\", "
261 decl_algos += assim_algo.substitute(name = algo_name, decl_opts=decl_opts) + "\n"
263 mem_file.write(assim_study.substitute(algos=algos,
264 decl_algos=decl_algos))
266 final_file = open(catalog_path + "/" + catalog_name, "wr")
267 final_file.write(mem_file.getvalue())