Salome HOME
Copyright update 2022
[modules/paravis.git] / examples / command_line / medio.py
1 # Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 def __select_data(reader, dataname=None):
20   if dataname:
21     keys = reader.GetProperty("FieldsTreeInfo")[::2]
22     # list all the names of arrays that can be seen (including their spatial discretization)
23     arr_name_with_dis = [elt.split("/")[-1] for elt in keys]
24     # list all the names of arrays (Equal to those in the MED File)
25     separator = reader.GetProperty("Separator").GetData()
26     arr_name = [elt.split(separator)[0] for elt in arr_name_with_dis]
27     for idx in range(len(keys)):
28       if arr_name[idx] == dataname:
29         reader.AllArrays = keys[idx]
30         break
31
32   return reader
33 #
34
35 def get_element_type(reader):
36   # Return 'P0', 'P1'...
37   separator = reader.GetProperty("Separator").GetData()
38   return reader.AllArrays[0].split(separator)[1]
39 #
40
41 def get_element_name(reader):
42   separator = reader.GetProperty("Separator").GetData()
43   return reader.AllArrays[0].split(separator)[0].split("/")[-1]
44 #
45
46 def load_mesh(med_filename, mesh_name=None):
47   import pvsimple
48   reader = pvsimple.MEDReader(FileName=med_filename)
49   return __select_data(reader, mesh_name)
50 #
51
52 def load_field(med_filename, field_name=None):
53   import pvsimple
54   reader = pvsimple.MEDReader(FileName=med_filename)
55   return __select_data(reader, field_name)
56 #