Salome HOME
9a11d5c16b3b1d4fdfd66c1998875ea5d90db981
[modules/med.git] / doc / tut / addfields / operations.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2012-2023  CEA, EDF
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 # This script illustrates the basic usage of MEDCoupling and MEDLoader
22 # to read fields in a med file and make basics operation on the values
23 # (gboulant - 11/07/2011)
24
25
26 # _T1A
27 medfilename = "timeseries.med" # med source filename
28 meshName = "Grid_80x80"        # name of the support mesh
29 dimrestriction = 0             # 0=no restriction
30 fieldName = "Pulse"            # name of the field series
31 # _T1B
32
33 # ==============================================================
34 # Make a scaling. This does not require the loading of the mesh
35 # _T2A
36 from MEDLoader import MEDLoader, ON_NODES
37
38 iteration, order = (3,-1)      # timestamps to consider
39 field=MEDLoader.ReadField(ON_NODES,
40                           medfilename, meshName, dimrestriction,
41                           fieldName, iteration, order)
42
43 field.applyFunc("f*3")
44 outfilename = "scaling.med"
45 MEDLoader.WriteField(outfilename,field,True)
46 # _T2B
47
48
49 # ==============================================================
50 # Make an addition. This requires to load first the mesh
51
52 # _T3A
53 # Load the support mesh
54 mesh = MEDLoader.ReadUMeshFromFile(medfilename, meshName, dimrestriction)
55
56 # Load the field at timestamps 3
57 iteration, order = (3,-1)
58 p3=MEDLoader.ReadField(ON_NODES,
59                        medfilename,meshName,dimrestriction,
60                        fieldName,iteration,order)
61 # Associate the mesh
62 p3.setMesh(mesh)
63
64 # Load the field at timestamps 4 
65 iteration, order = (4,-1)
66 p4=MEDLoader.ReadField(ON_NODES,
67                        medfilename, meshName, dimrestriction,
68                        fieldName, iteration, order)
69 # Associate the mesh
70 p4.setMesh(mesh)
71
72 # Make the addition
73 result = p3+p4
74 result.setName("p3+p4")
75
76 # We can finally save the result together with the operands fields
77 outfilename = "addition.med"
78 MEDLoader.WriteField(outfilename,result,True)
79 MEDLoader.WriteField(outfilename,p3,False)
80 MEDLoader.WriteField(outfilename,p4,False)
81 # _T3B
82
83 # ==============================================================
84 # Make an addition. A different way that loads fields all at once
85 mesh = MEDLoader.ReadUMeshFromFile(medfilename, meshName, dimrestriction)
86 timestamps = [(3,-1),(4,-1)]
87 p3,p4 = MEDLoader.ReadFieldsOnSameMesh(ON_NODES,medfilename,
88                                        meshName,dimrestriction,
89                                        fieldName,timestamps)
90 p3.setMesh(mesh)
91 p4.setMesh(mesh)
92 result = p3+p4
93 result.setName("p3+p4")
94
95 outfilename = "addition_01.med"
96 MEDLoader.WriteField(outfilename,result,True)