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