Salome HOME
Add documentation for users and basic tutotial script examples (associated to the...
[modules/med.git] / src / MEDOP / tut / addfields / operations.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3
4 # This script illustrates the basic usage of MEDCoupling and MEDLoader
5 # to read fields in a med file and make basics operation on the values
6 # (gboulant - 11/07/2011)
7
8
9 # _T1A
10 medfilename = "timeseries.med" # med source filename
11 meshName = "Grid_80x80"        # name of the support mesh
12 dimrestriction = 0             # 0=no restriction
13 fieldName = "Pulse"            # name of the field series
14 # _T1B
15
16 # ==============================================================
17 # Make a scaling. This does not require the loading of the mesh
18 # _T2A
19 from MEDLoader import MEDLoader, ON_NODES
20
21 iteration, order = (3,-1)      # timestamps to consider
22 field=MEDLoader.ReadField(ON_NODES,
23                           medfilename, meshName, dimrestriction,
24                           fieldName, iteration, order)
25
26 field.applyFunc("f*3")
27 outfilename = "scaling.med"
28 MEDLoader.WriteField(outfilename,field,True)
29 # _T2B
30
31
32 # ==============================================================
33 # Make an addition. This requires to load first the mesh
34
35 # _T3A
36 # Load the support mesh
37 mesh = MEDLoader.ReadUMeshFromFile(medfilename, meshName, dimrestriction)
38
39 # Load the field at timestamps 3
40 iteration, order = (3,-1)
41 p3=MEDLoader.ReadField(ON_NODES,
42                        medfilename,meshName,dimrestriction,
43                        fieldName,iteration,order)
44 # Associate the mesh
45 p3.setMesh(mesh)
46
47 # Load the field at timestamps 4 
48 iteration, order = (4,-1)
49 p4=MEDLoader.ReadField(ON_NODES,
50                        medfilename, meshName, dimrestriction,
51                        fieldName, iteration, order)
52 # Associate the mesh
53 p4.setMesh(mesh)
54
55 # Make the addition
56 result = p3+p4
57 result.setName("p3+p4")
58
59 # We can finally save the result together with the operandes fields
60 outfilename = "addition.med"
61 MEDLoader.WriteField(outfilename,result,True)
62 MEDLoader.WriteField(outfilename,p3,False)
63 MEDLoader.WriteField(outfilename,p4,False)
64 # _T3B
65
66 # ==============================================================
67 # Make an addition. A different way that loads fields all at once
68 mesh = MEDLoader.ReadUMeshFromFile(medfilename, meshName, dimrestriction)
69 timestamps = [(3,-1),(4,-1)]
70 p3,p4 = MEDLoader.ReadFieldsOnSameMesh(ON_NODES,medfilename,
71                                        meshName,dimrestriction,
72                                        fieldName,timestamps)
73 p3.setMesh(mesh)
74 p4.setMesh(mesh)
75 result = p3+p4
76 result.setName("p3+p4")
77
78 outfilename = "addition_01.med"
79 MEDLoader.WriteField(outfilename,result,True)