Salome HOME
Documentation reorganization
[tools/medcoupling.git] / doc / tutorial / atestMEDLoaderBasicAPI1.rst
1
2 .. _python_testMEDLoaderBasicAPI1_solution:
3
4 Reading, Writing a MED file using MEDLoader basic API
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 ::
8
9         import MEDLoader as ml
10         from MEDLoader import MEDLoader
11         # Mesh creation
12         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 ]
13         targetConn = [0,3,4,1, 1,4,2, 4,5,2, 6,7,4,3, 7,8,5,4]
14         targetMesh = ml.MEDCouplingUMesh("MyMesh",2)
15         targetMesh.allocateCells(5)
16         targetMesh.insertNextCell(ml.NORM_TRI3,3,targetConn[4:7])
17         targetMesh.insertNextCell(ml.NORM_TRI3,3,targetConn[7:10])
18         targetMesh.insertNextCell(ml.NORM_QUAD4,4,targetConn[0:4])
19         targetMesh.insertNextCell(ml.NORM_QUAD4,4,targetConn[10:14])
20         targetMesh.insertNextCell(ml.NORM_QUAD4,4,targetConn[14:18])
21         targetMesh.finishInsertingCells()
22         myCoords = ml.DataArrayDouble(targetCoords,9,2)
23         myCoords.setInfoOnComponents(["X [km]","YY [mm]"])
24         targetMesh.setCoords(myCoords)
25         # Writing mesh only
26         MEDLoader.WriteUMesh("TargetMesh.med",targetMesh,True)  # True means 'from scratch'
27         # Re-read it and test equality
28         meshRead = MEDLoader.ReadUMeshFromFile("TargetMesh.med",targetMesh.getName(),0)
29         print "Is the read mesh equal to 'targetMesh' ?", meshRead.isEqual(targetMesh,1e-12)
30         # Writing a field and its support mesh in one go
31         f = ml.MEDCouplingFieldDouble.New(ml.ON_CELLS, ml.ONE_TIME)
32         f.setTime(5.6,7,8)                              # Declare the timestep associated to the field 
33         f.setArray(targetMesh.getBarycenterAndOwner())
34         f.setMesh(targetMesh)
35         f.setName("AFieldName")
36         MEDLoader.WriteField("MyFirstField.med",f,True)
37         # Re-read it ans test equality
38         f2 = MEDLoader.ReadFieldCell("MyFirstField.med", f.getMesh().getName(), 0, f.getName(), 7, 8)
39         print "Is the read field identical to 'f' ?", f2.isEqual(f,1e-12,1e-12)
40         # Writing in several steps 
41         MEDLoader.WriteUMesh("MySecondField.med",f.getMesh(),True)
42         MEDLoader.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f)
43         # A second field to write
44         f2 = f.clone(True)         # 'True' means that we need a deep copy  
45         f2.getArray()[:] = 2.0
46         f2.setTime(7.8,9,10)
47         MEDLoader.WriteFieldUsingAlreadyWrittenMesh("MySecondField.med",f2)
48         # Re-read and test this two-timestep field
49         f3 = MEDLoader.ReadFieldCell("MySecondField.med",f.getMesh().getName(),0,f.getName(),7,8)
50         print "Is the field read in file equals to 'f' ?", f.isEqual(f3,1e-12,1e-12)
51         f4 = MEDLoader.ReadFieldCell("MySecondField.med",f.getMesh().getName(),0,f.getName(),9,10)
52         print "Is the field read in file equals to 'f2' ?", f2.isEqual(f4,1e-12,1e-12)
53