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