Salome HOME
Windows support
[modules/med.git] / doc / tut / medloader / explore.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2012-2016  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 from MEDLoader import MEDLoader
22
23 import os
24 #filename = "madnex_field.med"
25 filename = "timeseries.med"
26 filepath = os.path.join(os.path.abspath(os.path.dirname(__file__)),filename)
27
28 # Read the source meshes
29 meshNames = MEDLoader.GetMeshNames(filepath)
30
31 # Set to True if the meshes and fields data must be loaded. Otherwise,
32 # only theire descriptions will be loaded.
33 READ_PHYSICAL_DATA=False
34
35 for meshName in meshNames:
36
37     print("%s"%meshName)
38
39     # At this step, one can load the mesh of name meshName (but it is
40     # not an obligation to continue to explore the metadata)
41     meshDimRelToMax = 0 # 0 = no restriction
42     if READ_PHYSICAL_DATA:
43         mesh = MEDLoader.ReadUMeshFromFile(filepath,meshName,meshDimRelToMax)
44     # Note that the read function required the parameter
45     # meshDimRelToMax. This parameter discreminates the meshdim you
46     # are interested to relatively to the maximal dimension of cells
47     # contained in the mesh in file (then its value could be 0, -1, -2
48     # or -3 depending on the max dimension of the mesh. 0 means "no
49     # restriction".
50
51     # Read the names of the fields that rely on this mesh
52     fieldNames = MEDLoader.GetAllFieldNamesOnMesh(filepath,meshName)
53
54     for fieldName in fieldNames:
55
56         print("  %s"%fieldName)
57         
58         # A field name could identify several MEDCoupling fields, that
59         # differ by their spatial discretization on the mesh (values on
60         # cells, values on nodes, ...). This spatial discretization is
61         # specified by the TypeOfField that is an integer value in this
62         # list:
63         # 0 = ON_CELLS  
64         # 1 = ON_NODES  
65         # 2 = ON_GAUSS_PT       
66         # 3 = ON_GAUSS_NE
67         #
68         # As a consequence, before loading values of a field, we have
69         # to determine the types of spatial discretization defined for
70         # this field and to choose one.
71
72         listOfTypes = MEDLoader.GetTypesOfField(filepath,meshName,fieldName)
73         for typeOfDiscretization in listOfTypes:
74             print("    %s"%typeOfDiscretization)
75
76             # Then, we can get the iterations associated to this field on
77             # this type of spatial discretization:
78             fieldIterations = MEDLoader.GetFieldIterations(typeOfDiscretization,
79                                                            filepath,
80                                                            meshName,
81                                                            fieldName)
82
83             # Then, we can access to the physical data for each
84             # iteration of this field
85             for fieldIteration in fieldIterations:
86                 itNumber = fieldIteration[0]
87                 itOrder  = fieldIteration[1]
88                 print("      (%s,%s)"%(itNumber,itOrder))
89                 
90                 if READ_PHYSICAL_DATA:
91                     medCouplingField = MEDLoader.ReadField(typeOfDiscretization,
92                                                            filepath,
93                                                            meshName,
94                                                            meshDimRelToMax,
95                                                            fieldName,
96                                                            itNumber,
97                                                            itOrder)
98                     print(medCouplingField)