Salome HOME
Removing stand alone mascaret now within telemac
[modules/hydrosolver.git] / src / salome_hydro / study.py
1 #  Copyright (C) 2012-2013 EDF
2 #
3 #  This file is part of SALOME HYDRO module.
4 #
5 #  SALOME HYDRO module is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  SALOME HYDRO module 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
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with SALOME HYDRO module.  If not, see <http://www.gnu.org/licenses/>.
17
18 import os
19 import types
20
21 from salome.kernel.studyedit import getStudyEditor
22 from salome.kernel.parametric import study_exchange_vars
23
24 import HYDROSOLVER_ORB
25
26 # module constants
27 MODULE_NAME = "HYDROSOLVER"
28
29 COMPONENT_NAME = "HydroSolver"
30 COMPONENT_ICON = "HYDRO_small.png"
31
32 MASCARET_FILE_TYPE = "MASCARET_EFICAS_FILE"
33 MASCARET_ICON = "case1d.png"
34 MASCARET_CASE_TYPE_ID = 1
35
36 VARS_ICON = "icon_variables.png"
37
38 LOG_ICON = "log.png"
39 LOG_TYPE_ID = 2
40
41 TELEMAC2D_FILE_TYPE = "TELEMAC2D_EFICAS_FILE"
42 TELEMAC2D_ICON = "case2d.png"
43 TELEMAC2D_CASE_TYPE_ID = 3
44
45 PYTEL_FILE_TYPE = "PYTEL_EFICAS_FILE"
46 PYTEL_ICON = "case_pytel.png"
47 PYTEL_CASE_TYPE_ID = 5
48
49 # Dictionary used to map Eficas parameters to Mascaret file types
50 mascaretFileTypeDict = {"FICHIER_ABAQUES" : "abaques",
51                         "FICHIER_DICO" : "dico",
52                         "FICHIER_CASIER" : "casier",
53                         "FICHIER_GEOMETRIE" : "geo",
54                         "FICHIER_LOI" : "loi",
55                         "FICHIER_MOT_CLE" : "cas",
56                         "LISTING" : "listing",
57                         "FICHIER_DAMOCLE" : "damocle",
58                         "RESULTAT" : "res",
59                         "LISTING_CASIER" : "listing_casier",
60                         "LISTING_LIAISON" : "listing_liaison",
61                         "RESULTAT_CASIER" : "res_casier",
62                         "RESULTAT_LIAISON" : "res_liaison"
63                        }
64
65 def jdc_to_dict(jdc, command_list):
66     """
67     This tricky function transforms a JdC with a single command into a
68     dictionary that can be used more easily from a Python context (thanks to
69     M. Courtois and G. Boulant).
70     """
71     context = {}
72     for command in command_list:
73         context[command] = _args_to_dict
74     exec "parameters = " + jdc.strip() in context
75     return context['parameters']
76
77 def _args_to_dict(**kwargs):
78     return kwargs
79
80 def get_jdc_dict_var_as_tuple(jdc_dict, varname):
81     if not jdc_dict.has_key(varname):
82         return tuple()
83     elif not isinstance(jdc_dict[varname], types.TupleType):
84         return (jdc_dict[varname],)
85     else:
86         return jdc_dict[varname]
87
88 class HydroStudyEditor:
89     """
90     This class provides utility methods to edit the component "Hydro" in
91     the study. The parameter `studyId` defines the ID of the study to edit. If
92     it is :const:`None`, the edited study will be the current study.
93     """
94     def __init__(self, studyId = None):
95         self.editor = getStudyEditor(studyId)
96         self.hydroComp = None
97
98     def find_or_create_hydro_component(self):
99         """
100         Find the component "Hydro" or create it if none is found
101         :return: the SComponent found or created.
102         """
103         if self.hydroComp is None:
104             self.hydroComp = self.editor.findOrCreateComponent(MODULE_NAME,
105                                                                COMPONENT_NAME,
106                                                                COMPONENT_ICON)
107         return self.hydroComp
108
109     def find_or_create_mascaret_case(self, filePath):
110         self.find_or_create_hydro_component()
111         itemName = os.path.splitext(os.path.basename(filePath))[0]
112         sobj = self.editor.findOrCreateItem(self.hydroComp,
113                                             name = itemName,
114                                             fileType = MASCARET_FILE_TYPE,
115                                             fileName = filePath,
116                                             icon = MASCARET_ICON,
117                                             comment = str(filePath),
118                                             typeId = MASCARET_CASE_TYPE_ID)
119
120     def find_or_create_telemac2d_case(self, filePath):
121         self.find_or_create_hydro_component()
122         itemName = os.path.splitext(os.path.basename(filePath))[0]
123         sobj = self.editor.findOrCreateItem(self.hydroComp,
124                                             name = itemName,
125                                             fileType = TELEMAC2D_FILE_TYPE,
126                                             fileName = filePath,
127                                             icon = TELEMAC2D_ICON,
128                                             comment = str(filePath),
129                                             typeId = TELEMAC2D_CASE_TYPE_ID)
130
131     def generate_study_script(self, filePath):
132         """
133         Generating a python script from the eficas info
134         """
135         # Create "Python script" item
136         from TelApy.api.generate_study import generate_study_script
137         with open(filePath) as jdcfile:
138             jdc = jdcfile.read()
139         params = jdc_to_dict(jdc, ["TELEMAC2D", "_F"])
140
141         # Generation script string
142         python_script = generate_study_script(params)
143
144         # Computing name of the file (same as filePath)
145         file_dir, filename = os.path.split(filePath)
146         root, sfx = os.path.splitext(filename)
147         python_file = os.path.join(file_dir,root+".py")
148
149         # Writting to file
150         with open(python_file,'w') as pfile:
151             pfile.write(python_script)
152
153     def generate_study_yacs(self, filePath):
154         """
155         Generating a yacs file from the eficas info
156         """
157         # Create "Python script" item
158         from TelApy.api.generate_study import generate_study_yacs
159         with open(filePath) as jdcfile:
160             jdc = jdcfile.read()
161         params = jdc_to_dict(jdc, ["TELEMAC2D", "_F"])
162
163         # Generation YACS scheme
164         yacs_scheme = generate_study_yacs(params)
165
166         # Computing name of the file (same as filePath)
167         file_dir, filename = os.path.split(filePath)
168         root, sfx = os.path.splitext(filename)
169         yacs_file = os.path.join(file_dir,root+".xml")
170
171         # Writting to file
172         yacs_scheme.saveSchema(yacs_file)
173
174     def find_or_create_pytel_case(self, filePath):
175         self.find_or_create_hydro_component()
176         itemName = os.path.splitext(os.path.basename(filePath))[0]
177         sobj = self.editor.findOrCreateItem(self.hydroComp,
178                                             name = itemName,
179                                             fileType = PYTEL_FILE_TYPE,
180                                             fileName = filePath,
181                                             icon = PYTEL_ICON,
182                                             comment = str(filePath),
183                                             typeId = PYTEL_CASE_TYPE_ID)