Salome HOME
Move medtool folder to MED base repository
[modules/med.git] / doc / tutorial / medloader_basicAPI1_en.rst
1
2 Reading, Writing a MED file using MEDLoader's basic API
3 -------------------------------------------------------
4
5 The basic API is incarnated by the MEDLoader class.
6 All methods in that class are static.
7 The sessions of read/write are done on each call of a method.
8
9 Objective
10 ~~~~~~~~~
11
12 Write a mesh and a field from scratch, re-read them and compare the result.
13
14 Topics covered:
15 * Write using MEDLoader's basic API
16 * Read using MEDLoader's basic API
17
18 Implementation start
19 ~~~~~~~~~~~~~~~~~~~~
20
21 To implement this exercise we use the Python scripting language and import the MEDLoader Python module.
22 The whole MEDCoupling module is fully included in MEDLoader. No need to import MEDCoupling when MEDLoader has been loaded. ::
23
24         from MEDLoader import *
25
26 Writing/Reading a mesh
27 ~~~~~~~~~~~~~~~~~~~~~~
28
29 First of all, creation of a mesh "targetMesh". ::
30
31         targetCoords=[-0.3,-0.3, 0.2,-0.3, 0.7,-0.3, -0.3,0.2, 0.2,0.2, 0.7,0.2, -0.3,0.7, 0.2,0.7, 0.7,0.7 ]
32         targetConn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
33         targetMesh=MEDCouplingUMesh.New("MyMesh",2)
34         targetMesh.allocateCells(5)
35         targetMesh.insertNextCell(NORM_TRI3,3,targetConn[4:7])
36         targetMesh.insertNextCell(NORM_TRI3,3,targetConn[7:10])
37         targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[0:4])
38         targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[10:14])
39         targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[14:18])
40         myCoords=DataArrayDouble.New(targetCoords,9,2)
41         myCoords.setInfoOnComponents(["X [km]","YY [mm]"])
42         targetMesh.setCoords(myCoords)
43         
44 .. note:: targetMesh is ordered by geometric type.
45
46 We are then ready to write it. ::
47
48         MEDLoader.WriteUMesh("TargetMesh.med",targetMesh,True)
49
50 Then trying to read it. ::
51
52         meshRead=MEDLoader.ReadUMeshFromFile("TargetMesh.med",targetMesh.getName(),0)
53         print "Is the mesh read in file equals targetMesh? %s"%(meshRead.isEqual(targetMesh,1e-12))
54
55 Writing/Reading a field on one time step at once
56 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57 Creation of a vector field "f" on cell supported by "targetMesh". ::
58
59         f=MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
60         f.setTime(5.6,7,8)
61         f.setArray(targetMesh.getBarycenterAndOwner())
62         f.setMesh(targetMesh)
63         f.setName("AFieldName")
64         MEDLoader.WriteField("MyFirstField.med",f,True)
65
66 .. note:: Mesh AND Field is written at once into MyFirstField.
67
68 Reading into MyFirstField.med ::
69
70         f2=MEDLoader.ReadFieldCell("MyFirstField.med",f.getMesh().getName(),0,f.getName(),7,8)
71         print "Is the field read in file equals f ? %s"%(f2.isEqual(f,1e-12,1e-12))
72
73 Writing/Reading a field on one or many times steps in "multi-session mode"
74 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75
76 Here contrary to the previous steps, we are going to write in a multi-session mode on the same MED file.
77 First dealing with the mesh. ::
78
79         MEDLoader.WriteUMesh("MySecondField.med",f.getMesh(),True)
80         
81 Then writing only array part of field. ::
82
83         MEDLoader.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f)
84         
85 Then put a another time step. ::
86
87         f2=f.clone(True)
88         f2.getArray()[:]=2.0
89         f2.setTime(7.8,9,10)
90         MEDLoader.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f2)
91
92 Now "MySecondField.med" file contains 2 time steps.
93
94 Solution
95 ~~~~~~~~
96
97 :ref:`python_testMEDLoaderBasicAPI1_solution`