]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/MEDLoader/Swig/medutilities.py
Salome HOME
adf51ce0bdc506dae21fc69912b5301249d2c17b
[tools/medcoupling.git] / src / MEDLoader / Swig / medutilities.py
1 # -*- coding: iso-8859-1 -*-
2 # --
3 # Copyright (C) 2009-2013  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
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.
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 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author : Erwan ADAM (CEA), Anthony GEAY (CEA)
22 # --
23
24 from MEDLoader import *
25
26 def convert(file_in, driver_in, driver_out, format=1, file_out=None):
27     #
28     print file_in
29     #
30     if file_out is None:
31         file_out = file_in
32         if driver_out == "GIBI":
33             file_out += ".sauv"
34         elif driver_out == "MED":
35             file_out += ".med"
36         else:
37             msg = "Driver out %s is unknown"%(driver_out)
38             raise NotImplementedError(msg)
39         pass
40     print file_out
41     #
42     if driver_in == "GIBI":
43         sr = SauvReader.New(file_in)
44         mfd= sr.loadInMEDFileDS()
45         pass
46     elif driver_in == "MED":
47         mfd = MEDFileData(file_in)
48         pass
49     else:
50         raise NotImplementedError("Driver in %s is unknown"%(driver_in))
51     #
52     my_remove(file_out)
53     #
54     if driver_out == "GIBI":
55         sw=SauvWriter.New()
56         sw.setMEDFileDS(mfd,0);#0 ?
57         sw.write(file_out)
58         #
59         if mesh_dim >= 3:
60             from sys import platform
61             if platform in ["win32"]:
62                 f = open(file_out)
63                 content = f.read()
64                 f.close()
65                 content = content.replace("IFOUR  -1", "IFOUR   2")
66                 content = content.replace("IFOMOD  -1", "IFOMOD   2")
67                 f = open(file_out, "w")
68                 f.write(content)
69                 f.close()
70             else:
71                 cmd  = "sed"
72                 cmd += ' -e "s/IFOUR  -1/IFOUR   2/g"'
73                 cmd += ' -e "s/IFOMOD  -1/IFOMOD   2/g"'
74                 # cmd += ' -e "s/IECHO   1/IECHO   0/g"'
75                 cmd += ' %s > .dummy'%(file_out)
76                 cmd += ' && '
77                 cmd += ' mv -f .dummy %s'%(file_out)
78                 from os import system
79                 system(cmd)
80                 pass
81             pass
82         #
83         if format == 0:
84             from castemlauncher import CastemLauncher
85             dgibi_stream  = "\n"
86             dgibi_stream += "OPTI REST FORMAT '%s' ;\n"%(file_out)
87             dgibi_stream += "REST FORMAT;\n"
88             file_out = file_out.replace('__format__', '')
89             dgibi_stream += "OPTI SAUV '%s' ;\n"%(file_out)
90             dgibi_stream += "SAUV ;\n"
91             cl = CastemLauncher(dgibi_stream)
92             cl.addTmpFiles(file_out+'__format__', "UTILNOTI", "UTILPROC")
93             cl.run()
94             pass
95         return
96     elif driver_out == "MED":
97         mfd.write(file_out,2)
98         return
99     else:
100         raise NotImplementedError("Driver in %s is unknown"%(driver_in))
101
102 def sauv2med(*argv):
103     argv = list(argv)
104     for arg in argv:
105         convert(arg, "GIBI", "MED")
106         pass
107     return
108
109 def med2sauv(*argv):
110     argv = list(argv)
111     format = 1
112     for arg in argv[:]:
113         if arg.find('--format') == 0:
114             argv.remove(arg)
115             try:
116                 value = arg.split("=")[1]
117             except IndexError:
118                 usage(1)
119                 pass
120             try:
121                 value = int(value)
122             except ValueError:
123                 usage(1)
124                 pass
125             format = value
126             pass
127         pass
128     for arg in argv:
129         convert(arg, "MED", "GIBI", format)
130         pass
131     return