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 MEDLoader Python module.
22 The whole MEDCoupling module is fully included in MEDLoader. No need to import MEDCoupling when MEDLoader has been loaded. ::
24 import MEDLoader as ml
26 Writing/Reading a mesh
27 ~~~~~~~~~~~~~~~~~~~~~~
29 First of all, creation of a mesh "targetMesh". ::
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=ml.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=ml.DataArrayDouble.New(targetCoords,9,2)
41 myCoords.setInfoOnComponents(["X [km]","YY [mm]"])
42 targetMesh.setCoords(myCoords)
44 .. note:: targetMesh is ordered by geometric type.
46 We are then ready to write it. ::
48 ml.WriteUMesh("TargetMesh.med",targetMesh,True)
50 Then trying to read it. ::
52 meshRead=ml.ReadUMeshFromFile("TargetMesh.med",targetMesh.getName(),0)
53 print "Is the mesh read in file equals targetMesh? %s"%(meshRead.isEqual(targetMesh,1e-12))
55 Writing/Reading a field on one time step at once
56 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57 Creation of a vector field "f" on cell supported by "targetMesh". ::
59 f=ml.MEDCouplingFieldDouble.New(ON_CELLS,ONE_TIME)
61 f.setArray(targetMesh.computeCellCenterOfMass())
63 f.setName("AFieldName")
64 ml.WriteField("MyFirstField.med",f,True)
66 .. note:: Mesh AND Field is written at once into MyFirstField.
68 Reading into MyFirstField.med ::
70 f2=ml.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))
73 Writing/Reading a field on one or many times steps in "multi-session mode"
74 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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. ::
79 ml.WriteUMesh("MySecondField.med",f.getMesh(),True)
81 Then writing only array part of field. ::
83 ml.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f)
85 Then put a another time step. ::
90 ml.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f2)
92 Now "MySecondField.med" file contains 2 time steps.
97 :ref:`python_testMEDLoaderBasicAPI1_solution`