2 # Copyright (C) 2008-2009 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 ('ASSIM_STUDY')),
47 String_data_bloc = """
48 STRING_DATA = BLOC ( condition = " FROM in ( 'String', ) ",
50 STRING = SIMP(statut = "o", typ = "TXM"),
54 Script_data_bloc = """
55 SCRIPT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
57 SCRIPT_FILE = SIMP(statut = "o", typ = "Fichier"),
62 DICT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
64 SCRIPT_FILE = SIMP(statut = "o", typ = "Fichier"),
68 # Pour l'instant on ne gère qu'un seul script pour toutes les functions
69 FunctionDict_data_bloc = """
70 FUNCTIONDICT_DATA = BLOC ( condition = " FROM in ( 'FunctionDict', ) ",
72 FUNCTIONDICT_FILE = SIMP(statut = "o", typ = "Fichier"),
77 def F_${data_name}(statut) : return FACT(statut = statut,
78 FROM = SIMP(statut = "o", typ = "TXM", into=(${data_into})),
84 def F_InitChoice() : return ("Background",
88 "ObservationOperator",
89 "AlgorithmParameters",
92 def F_Init(statut) : return FACT(statut = statut,
93 INIT_FILE = SIMP(statut = "o", typ = "Fichier"),
94 TARGET_LIST = SIMP(statut = "o", typ = "TXM", min=1, max="**", into=F_InitChoice(), validators=(VerifExiste(2))),
97 assim_data_method = """
98 def F_${assim_name}(statut) : return FACT(statut=statut,
99 regles = ( UN_PARMI (${choices})),
104 assim_data_choice = """
105 ${choice_name} = F_${choice_name}("f"),
108 assim_opt_choice = """
109 ${choice_name} = F_${choice_name}("f"),
113 ${name} = FACT(regles = ( ENSEMBLE ("Background", "BackgroundError",
114 "Observation", "ObservationError",
115 "ObservationOperator")),
116 Background = F_Background("o"),
117 BackgroundError = F_BackgroundError("o"),
118 Observation = F_Observation("o"),
119 ObservationError = F_ObservationError("o"),
120 ObservationOperator = F_ObservationOperator("o"),
121 AlgorithmParameters = F_AlgorithmParameters("f"),
127 ASSIM_STUDY = PROC(nom="ASSIM_STUDY",
130 STUDY_NAME = SIMP(statut="o", typ = "TXM"),
131 ALGORITHM = FACT(statut='o',
132 regles = ( UN_PARMI (${algos}),),
138 begin_catalog_file = string.Template(begin_catalog_file)
139 data_method = string.Template(data_method)
140 assim_data_method = string.Template(assim_data_method)
141 assim_data_choice = string.Template(assim_data_choice)
142 assim_opt_choice = string.Template(assim_opt_choice)
143 assim_algo = string.Template(assim_algo)
144 assim_study = string.Template(assim_study)
146 #----------- End of Templates Part ---------------#
150 #----------- Begin generation script -----------#
151 print "-- Starting AdaoCalatogGenerator.py --"
155 import daYacsSchemaCreator
156 import daCore.AssimilationStudy
158 logging.fatal("Import of ADAO python modules failed !" +
159 "\n add ADAO python installation directory in your PYTHONPATH")
160 traceback.print_exc()
163 def check_args(args):
164 logging.debug("Arguments are :" + str(args))
166 logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
170 from optparse import OptionParser
171 usage = "usage: %prog [options] catalog_path catalog_name"
173 my_parser = OptionParser(usage=usage, version=version)
174 (options, args) = my_parser.parse_args()
177 catalog_path = args[0]
178 catalog_name = args[1]
180 # Generates into a string
181 mem_file = StringIO.StringIO()
184 from time import strftime
185 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
187 # Step 1: Check basic data input types
188 import daYacsSchemaCreator.infos_daComposant as infos
189 for basic_type in infos.BasicDataInputs:
190 logging.debug('A basic data input type is found: ' + basic_type)
191 if basic_type + '_data_bloc' not in locals().keys():
192 logging.fatal("Basic data input type not found: " + basic_type)
195 # Step 2: Add data input dict
196 for data_input_name in infos.DataTypeDict.keys():
197 logging.debug('A data input is found: ' + data_input_name)
198 data_name = data_input_name
202 for basic_type in infos.DataTypeDict[data_input_name]:
203 data_into += "\"" + basic_type + "\", "
204 data_bloc += locals()[basic_type + '_data_bloc'] + "\n"
206 mem_file.write(data_method.substitute(data_name = data_name,
207 data_into = data_into,
208 data_bloc = data_bloc))
210 # Step 3: Add assimilation algorithm data input
211 for assim_data_input_name in infos.AssimDataDict.keys():
212 logging.debug("An assimilation algorithm data input is found: " + assim_data_input_name)
213 assim_name = assim_data_input_name
217 for choice in infos.AssimDataDict[assim_data_input_name]:
218 choices += "\"" + choice + "\", "
219 decl_choices += assim_data_choice.substitute(choice_name = choice)
221 mem_file.write(assim_data_method.substitute(assim_name = assim_name,
223 decl_choices = decl_choices))
225 # Step 4: Add optional nodes
227 for opt_name in infos.OptDict.keys():
228 logging.debug("An optional node is found: " + opt_name)
233 for choice in infos.OptDict[opt_name]:
234 data_into += "\"" + choice + "\", "
235 data_bloc += locals()[choice + '_data_bloc'] + "\n"
237 mem_file.write(data_method.substitute(data_name = data_name,
238 data_into = data_into,
239 data_bloc = data_bloc))
241 opt_names.append(opt_name)
243 # Step 5: Add init node
244 mem_file.write(init_method)
246 # Final step: Add algorithm and assim_study
250 for opt_name in opt_names:
251 decl_opts += assim_opt_choice.substitute(choice_name = opt_name)
253 assim_study_object = daCore.AssimilationStudy.AssimilationStudy()
254 algos_list = assim_study_object.get_available_algorithms()
255 for algo_name in algos_list:
256 logging.debug("An assimilation algorithm is found: " + algo_name)
257 if algo_name == "3DVAR":
258 algo_name = "ThreeDVAR"
259 algos += "\"" + algo_name + "\", "
260 decl_algos += assim_algo.substitute(name = algo_name, decl_opts=decl_opts) + "\n"
262 mem_file.write(assim_study.substitute(algos=algos,
263 decl_algos=decl_algos))
265 final_file = open(catalog_path + "/" + catalog_name, "wr")
266 final_file.write(mem_file.getvalue())