Salome HOME
AdaoCalatogGenerator.py -> AdaoCatalogGenerator.py
[modules/adao.git] / bin / AdaoCatalogGenerator.py
1 #-*- coding:utf-8 -*-
2 #  Copyright (C) 2008-2009  EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # --
19 # Author : André RIBES (EDF R&D)
20 # --
21
22 import logging
23 import traceback
24 import sys
25 import string
26 import StringIO
27
28 logging.basicConfig(level=logging.DEBUG)
29
30 #----------- Templates Part ---------------#
31 begin_catalog_file = """
32 # -*- coding: utf-8 -*-
33
34 # --------------------------------------------------------
35 # generated by AdaoCatalogGenerator at ${date}
36 # --------------------------------------------------------
37
38 import Accas
39 from Accas import *
40
41 JdC = JDC_CATA (code = 'ADAO',
42                 execmodul = None,
43                 regles = ( AU_MOINS_UN ('ASSIM_STUDY')),
44                )
45 """
46
47 String_data_bloc = """
48                                      STRING_DATA = BLOC ( condition = " FROM in ( 'String', ) ",
49
50                                                   STRING = SIMP(statut = "o", typ = "TXM"),
51                                                  ),
52 """
53
54 Script_data_bloc = """
55                                      SCRIPT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
56
57                                                   SCRIPT_FILE = SIMP(statut = "o", typ = "Fichier"),
58                                                  ),
59 """
60
61 Dict_data_bloc = """
62                                      DICT_DATA = BLOC ( condition = " FROM in ( 'Script', ) ",
63
64                                                   SCRIPT_FILE = SIMP(statut = "o", typ = "Fichier"),
65                                                  ),
66 """
67
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', ) ",
71
72                                                   FUNCTIONDICT_FILE = SIMP(statut = "o", typ = "Fichier"),
73                                                  ),
74 """
75
76 data_method = """
77 def F_${data_name}(statut) : return FACT(statut = statut,
78                                          FROM = SIMP(statut = "o", typ = "TXM", into=(${data_into})),
79 ${data_bloc}
80                                     )
81 """
82
83 init_method = """
84 def F_InitChoice() : return  ("Background",
85                               "BackgroundError",
86                               "Observation",
87                               "ObservationError",
88                               "ObservationOperator",
89                               "AlgorithmParameters",
90                               "Analysis",
91                              )
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))),
95                                 )
96 """
97 assim_data_method = """
98 def F_${assim_name}(statut) : return FACT(statut=statut,
99                                           regles = ( UN_PARMI (${choices})),
100 ${decl_choices}
101                                                 )
102 """
103
104 assim_data_choice = """
105                                                  ${choice_name} = F_${choice_name}("f"),
106 """
107
108 assim_opt_choice = """
109                                                  ${choice_name} = F_${choice_name}("f"),
110 """
111
112 assim_algo = """
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"),
122                                                  Init = F_Init("f"),
123 ${decl_opts}
124                                                 ),
125 """
126 assim_study = """
127 ASSIM_STUDY = PROC(nom="ASSIM_STUDY",
128                    op=None,
129                    repetable = "n",
130                    STUDY_NAME = SIMP(statut="o", typ = "TXM"),
131                    ALGORITHM  = FACT(statut='o',
132                                      regles = ( UN_PARMI (${algos}),),
133 ${decl_algos}
134                                     ),
135                   )
136 """
137
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)
145
146 #----------- End of Templates Part ---------------#
147
148
149
150 #----------- Begin generation script -----------#
151 print "-- Starting AdaoCalatogGenerator.py --"
152
153 try:
154   import daEficas
155   import daYacsSchemaCreator
156   import daCore.AssimilationStudy
157 except:
158   logging.fatal("Import of ADAO python modules failed !" +
159                 "\n add ADAO python installation directory in your PYTHONPATH")
160   traceback.print_exc()
161   sys.exit(1)
162
163 def check_args(args):
164   logging.debug("Arguments are :" + str(args))
165   if len(args) != 2:
166     logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
167     sys.exit(1)
168
169 # Parse arguments
170 from optparse import OptionParser
171 usage = "usage: %prog [options] catalog_path catalog_name"
172 version="%prog 0.1"
173 my_parser = OptionParser(usage=usage, version=version)
174 (options, args) = my_parser.parse_args()
175 check_args(args)
176
177 catalog_path =  args[0]
178 catalog_name =  args[1]
179
180 # Generates into a string
181 mem_file = StringIO.StringIO()
182
183 # Start file
184 from time import strftime
185 mem_file.write(begin_catalog_file.substitute(date=strftime("%Y-%m-%d %H:%M:%S")))
186
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)
193     sys.exit(1)
194
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
199   data_into = ""
200   data_bloc = ""
201
202   for basic_type in infos.DataTypeDict[data_input_name]:
203     data_into += "\"" + basic_type + "\", "
204     data_bloc += locals()[basic_type + '_data_bloc'] + "\n"
205
206   mem_file.write(data_method.substitute(data_name = data_name,
207                                         data_into = data_into,
208                                         data_bloc = data_bloc))
209
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
214   choices = ""
215   decl_choices = ""
216   decl_opts = ""
217   for choice in infos.AssimDataDict[assim_data_input_name]:
218     choices += "\"" + choice + "\", "
219     decl_choices += assim_data_choice.substitute(choice_name = choice)
220
221   mem_file.write(assim_data_method.substitute(assim_name = assim_name,
222                                               choices = choices,
223                                               decl_choices = decl_choices))
224
225 # Step 4: Add optional nodes
226 opt_names = []
227 for opt_name in infos.OptDict.keys():
228   logging.debug("An optional node is found: " + opt_name)
229   data_name = opt_name
230   data_into = ""
231   data_bloc = ""
232
233   for choice in infos.OptDict[opt_name]:
234     data_into += "\"" + choice + "\", "
235     data_bloc += locals()[choice + '_data_bloc'] + "\n"
236
237   mem_file.write(data_method.substitute(data_name = data_name,
238                                         data_into = data_into,
239                                         data_bloc = data_bloc))
240
241   opt_names.append(opt_name)
242
243 # Step 5: Add init node
244 mem_file.write(init_method)
245
246 # Final step: Add algorithm and assim_study
247 algos = ""
248 decl_algos = ""
249 decl_opts = ""
250 for opt_name in opt_names:
251   decl_opts += assim_opt_choice.substitute(choice_name = opt_name)
252
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"
261
262 mem_file.write(assim_study.substitute(algos=algos,
263                                       decl_algos=decl_algos))
264 # Write file
265 final_file = open(catalog_path + "/" + catalog_name, "wr")
266 final_file.write(mem_file.getvalue())
267 mem_file.close()
268 final_file.close()
269