2 Reading, Writing a MED file using MEDLoader's basic API
3 -------------------------------------------------------
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.
12 Write a mesh and a field from scratch, re-read them and compare the result.
15 * Write using MEDLoader's basic API
16 * Read using MEDLoader's basic API
21 To implement this exercise we use the Python scripting language and import the `medcoupling` Python module. ::
23 import medcoupling as mc
25 Writing/Reading a mesh
26 ~~~~~~~~~~~~~~~~~~~~~~
28 First of all, creation of a mesh "targetMesh". ::
30 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 ]
31 targetConn=[0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
32 targetMesh=mc.MEDCouplingUMesh.New("MyMesh",2)
33 targetMesh.allocateCells(5)
34 targetMesh.insertNextCell(NORM_TRI3,3,targetConn[4:7])
35 targetMesh.insertNextCell(NORM_TRI3,3,targetConn[7:10])
36 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[0:4])
37 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[10:14])
38 targetMesh.insertNextCell(NORM_QUAD4,4,targetConn[14:18])
39 myCoords=mc.DataArrayDouble.New(targetCoords,9,2)
40 myCoords.setInfoOnComponents(["X [km]","YY [mm]"])
41 targetMesh.setCoords(myCoords)
43 .. note:: targetMesh is ordered by geometric type.
45 We are then ready to write it. ::
47 mc.WriteUMesh("TargetMesh.med",targetMesh,True)
49 Then trying to read it. ::
51 meshRead=mc.ReadUMeshFromFile("TargetMesh.med",targetMesh.getName(),0)
52 print("Is the mesh read in file equals targetMesh? %s"%(meshRead.isEqual(targetMesh,1e-12)))
54 Writing/Reading a field on one time step at once
55 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56 Creation of a vector field "f" on cell supported by "targetMesh". ::
58 f=mc.MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
60 f.setArray(targetMesh.computeCellCenterOfMass())
62 f.setName("AFieldName")
63 mc.WriteField("MyFirstField.med",f,True)
65 .. note:: Mesh AND Field is written at once into MyFirstField.
67 Reading into MyFirstField.med ::
69 f2=mc.ReadFieldCell("MyFirstField.med",f.getMesh().getName(),0,f.getName(),7,8)
70 print("Is the field read in file equals f ? %s"%(f2.isEqual(f,1e-12,1e-12)))
72 Writing/Reading a field on one or many times steps in "multi-session mode"
73 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75 Here contrary to the previous steps, we are going to write in a multi-session mode on the same MED file.
76 First dealing with the mesh. ::
78 mc.WriteUMesh("MySecondField.med",f.getMesh(),True)
80 Then writing only array part of field. ::
82 mc.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f)
84 Then put a another time step. ::
89 mc.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f2)
91 Now "MySecondField.med" file contains 2 time steps.
96 :ref:`python_testMEDLoaderBasicAPI1_solution`