1 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2009-2013 CEA/DEN, EDF R&D, OPEN CASCADE
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser 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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author : Erwan ADAM (CEA)
24 from libMEDMEM_Swig import *
38 convert(arg, "GIBI", "MED")
47 if arg.find('--format') == 0:
50 value = arg.split("=")[1]
63 convert(arg, "MED", "GIBI", format)
67 def convert(file_in, driver_in, driver_out, format=1, file_out=None):
73 ## if file_out.find('.') != -1:
74 ## suffix = file_in.split('.')[-1]
75 ## if driver_in == "GIBI":
80 ## if len(suffix) >= len(test):
81 ## suffix = suffix[:len(test)]
82 ## suffix = suffix.lower()
84 ## file_out = '.'.join(file_in.split('.')[:-1])
88 if driver_out == "GIBI":
90 elif driver_out == "MED":
93 msg = "Driver out %s is unknown"%(driver_out)
94 raise NotImplementedError(msg)
100 if driver_in == "GIBI":
101 driver = GIBI_MED_RDONLY_DRIVER(file_in)
102 fields = driver.read()
103 mesh = driver.getMesh()
105 meshes.append( mesh )
106 elif driver_in == "MED":
107 med = MEDFILEBROWSER(file_in)
108 for mesh_name in med.getMeshNames():
109 if med.isStructuredMesh( mesh_name ):
110 mesh = GRID( MED_DRIVER, file_in, mesh_name )
112 mesh = MESH( MED_DRIVER, file_in, mesh_name )
113 meshes.append( mesh )
114 for field_name in med.getFieldNames():
115 mesh_name = med.getMeshName( field_name )
118 if m.getName() == mesh_name:
120 for dtit in med.getFieldIteration( field_name ):
121 if med.getFieldType( field_name ) == MED_REEL64:
122 field = FIELDDOUBLE(MED_DRIVER, file_in, field_name, dtit.dt, dtit.it, mesh )
124 field = FIELDINT(MED_DRIVER, file_in, field_name, dtit.dt, dtit.it, mesh )
125 fields.append( field )
127 msg = "Driver in %s is unknown"%(driver_in)
128 raise NotImplementedError(msg)
132 if driver_out == "GIBI":
134 mesh_dim = mesh.getSpaceDimension()
136 file_out = file_out+'__format__'
140 driver = GIBI_MED_WRONLY_DRIVER(file_out, fields, mesh)
142 driver = GIBI_MESH_WRONLY_DRIVER(file_out, mesh)
148 from sys import platform
149 if platform in ["win32"]:
153 content = content.replace("IFOUR -1", "IFOUR 2")
154 content = content.replace("IFOMOD -1", "IFOMOD 2")
155 f = open(file_out, "w")
160 cmd += ' -e "s/IFOUR -1/IFOUR 2/g"'
161 cmd += ' -e "s/IFOMOD -1/IFOMOD 2/g"'
162 # cmd += ' -e "s/IECHO 1/IECHO 0/g"'
163 cmd += ' %s > .dummy'%(file_out)
165 cmd += ' mv -f .dummy %s'%(file_out)
166 from os import system
172 from castemlauncher import CastemLauncher
174 dgibi_stream += "OPTI REST FORMAT '%s' ;\n"%(file_out)
175 dgibi_stream += "REST FORMAT;\n"
176 file_out = file_out.replace('__format__', '')
177 dgibi_stream += "OPTI SAUV '%s' ;\n"%(file_out)
178 dgibi_stream += "SAUV ;\n"
179 cl = CastemLauncher(dgibi_stream)
180 cl.addTmpFiles(file_out+'__format__', "UTILNOTI", "UTILPROC")
186 mesh.write(MED_DRIVER, file_out)
188 typedField = field.castToTypedField();
189 typedField.write(MED_DRIVER, file_out)
193 def avs2med_one_file(file_in, file_out, mesh_name, field_name):
195 Convert an ucd avs inp file into a med file
196 inp Specifications can be found at :
197 http://people.scs.fsu.edu/~burkardt/data/ucd/ucd.html
198 http://help.avs.com/Express/doc/help/reference/dvmac/UCD_Form.htm
204 meshing.setName(mesh_name)
207 lines = f.readlines()
209 nb_lines = len(lines)
220 headers = [ int(i) for i in lines[0].split() ]
222 number_of_nodes = headers[0]
223 number_of_cells = headers[1]
224 number_of_nodes_data = headers[2]
225 number_of_cells_data = headers[3]
226 number_of_whole_data = headers[3]
230 nodes = lines[:number_of_nodes]
231 lines = lines[number_of_nodes:]
232 nodes = [ " ".join(l.split()[1:]) for l in nodes ]
233 nodes = " ".join(nodes)
234 nodes = [ float(v) for v in nodes.split() ]
238 nodes_min = min(nodes)
239 nodes_max = max(nodes)
240 epsilon = 1.0e-5 * (nodes_max - nodes_min)
241 nodes_z = [ nodes[i] for i in range(2, 3 * number_of_nodes, 3) ]
242 nodes_z_min = min(nodes_z)
243 nodes_z_max = max(nodes_z)
245 if abs(nodes_z_max) > epsilon:
248 if abs(nodes_z_min) > epsilon:
252 if space_dimension == 2:
253 l = range(0, 3 * number_of_nodes, 3) + range(1, 3 * number_of_nodes, 3)
255 nodes = [ nodes[i] for i in l ]
258 meshing.setCoordinates(space_dimension, number_of_nodes, nodes, "CARTESIAN", MED_FULL_INTERLACE)
262 cells = lines[:number_of_cells]
263 lines = lines[number_of_cells:]
264 cells = [ c.split() for c in cells ]
266 type2connectivity = {}
271 cell_connectivity = c[3:]
276 elif avs_type == 'tri':
281 elif avs_type == 'quad':
287 elif avs_type == 'tet':
292 elif avs_type == 'hex':
297 elif avs_type == 'pyr':
302 elif avs_type == 'prism':
308 raise Exception("unknown avs_type : %s"%(avs_type))
310 med_type = nb2medtype[len(cell_connectivity)]
313 d = type2connectivity[entity]
315 type2connectivity[entity] = {}
316 d = type2connectivity[entity]
326 cell_connectivity = ' '.join(cell_connectivity)
327 l.append(cell_connectivity)
331 mesh_dimension = space_dimension
333 for entity, d in type2connectivity.items():
334 meshing.setNumberOfTypes(len(d.keys()), entity)
335 meshing.setTypes(d.keys(), entity)
336 meshing.setNumberOfElements([len(v) for v in d.values()], entity)
337 for key, value in d.items():
339 connectivity = " ".join(connectivity)
340 connectivity = [ int(v) for v in connectivity.split() ]
341 meshing.setConnectivity(connectivity, entity, key)
345 meshing.write(meshing.addDriver(MED_DRIVER,file_out,meshing.getName()))
348 for n in [number_of_nodes_data, number_of_cells_data]:
353 header = [ int(v) for v in lines[0].split() ]
355 nb_of_components = header[0]
356 nb_of_values_by_component = header[1:]
357 if len(nb_of_values_by_component) != nb_of_components:
358 msg = "Error at line %d"%(nb_lines - len(lines))
361 titles_by_component = []
362 for i in range(nb_of_components):
365 label = l.split(',')[0]
366 unit = l[len(label)+1:] # The +1 is for the comma
367 label = label.strip()
369 labels_by_value = nb_of_values_by_component[i]*['']
370 l = [label, unit, labels_by_value]
371 titles_by_component.append(l)
373 if nb_of_components > 1:
374 if nb_of_values_by_component == nb_of_components * [1]:
375 nb_of_values_by_component = [ nb_of_components ]
378 if number_of_cells_data:
379 name = field_name + "_on_nodes"
383 if number_of_nodes_data:
384 name = field_name + "_on_cells"
387 titles_by_component = [ [name, "", [t[0] for t in titles_by_component]] ]
399 for i in range(nb_of_components):
400 imax = imin + nb_of_values_by_component[i]
401 vals.append([ l.split()[imin:imax] for l in values ])
407 support = SUPPORT(meshing, "support_on_nodes", MED_NODE)
409 support = SUPPORT(meshing, "support_on_cells", MED_CELL)
411 for i in range(nb_of_components):
412 nb = nb_of_values_by_component[i]
413 field = FIELDDOUBLE(support, nb)
414 field.setName(titles_by_component[i][0])
415 # field.setIterationNumber(-1)
416 # field.setOrderNumber(-1)
419 name = titles_by_component[i][2][n]
421 field.setComponentName(n+1,name)
426 value = [ " ".join(l) for l in value ]
427 value = " ".join(value)
428 value = [ float(v) for v in value.split() ]
429 field.setValue(value)
431 field.write(field.addDriver(MED_DRIVER,file_out,field.getName()))
445 if ( arg.find('--mesh_name') == 0 ) or ( arg.find('--mesh-name') == 0 ) :
448 mesh_name = arg.split("=")[1]
453 if ( arg.find('--field_name') == 0 ) or ( arg.find('--field-name') == 0 ) :
456 field_name = arg.split("=")[1]
464 avs2med_one_file(arg, arg + ".med", mesh_name, field_name)