Salome HOME
Synchronize adm files
[modules/med.git] / src / MEDOP / tut / medloader / manage.py
1 #!/usr/bin/env python
2 # Copyright (C) 2012-2014  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # _T1A
22 import collections
23 def tree():
24     return collections.defaultdict(tree)
25
26 fieldTree = tree()
27 meshDict = {}
28 # _T1B
29
30 import os
31 filename = "timeseries.med"
32 filepath = os.path.join(os.path.abspath(os.path.dirname(__file__)),filename)
33
34 # _T2A
35 from MEDLoader import MEDLoader
36 meshNames = MEDLoader.GetMeshNames(filepath)
37
38 meshDimRelToMax = 0 # 0 = no restriction
39
40 for meshName in meshNames:
41     mesh = MEDLoader.ReadUMeshFromFile(filepath,meshName,meshDimRelToMax)
42     meshDict[meshName] = mesh
43
44     fieldNames = MEDLoader.GetAllFieldNamesOnMesh(filepath,meshName)
45     for fieldName in fieldNames:
46         listOfTypes = MEDLoader.GetTypesOfField(filepath,meshName,fieldName)
47         for typeOfDiscretization in listOfTypes:
48             fieldIterations = MEDLoader.GetFieldIterations(typeOfDiscretization,
49                                                            filepath,
50                                                            meshName,
51                                                            fieldName)
52             for fieldIteration in fieldIterations:
53                 itNumber = fieldIteration[0]
54                 itOrder  = fieldIteration[1]
55
56                 field = MEDLoader.ReadField(typeOfDiscretization,
57                                             filepath,
58                                             meshName,
59                                             meshDimRelToMax,
60                                             fieldName,
61                                             itNumber,
62                                             itOrder)
63
64                 fieldTree\
65                            [meshName]\
66                            [fieldName]\
67                            [typeOfDiscretization]\
68                            [itNumber][itOrder] = field
69 # _T2B
70
71 # Q: use a list of structures whose an attribute could be a
72 # MEDCoupling field? Or a tree that you cross using attribute and
73 # whose leaves are the MEDCoupling fields?
74 # R: I think that the default structure should be a simple list that
75 # store objects whith properties that corresponds to the metadata (and
76 # if loaded the MEDCouplingField or Mesh). Then for specific request,
77 # a BTree could be create to organize the search (for example if we
78 # request all the fields for a given iteration step, then we should
79 # use the iteration step as a first classifaction switch of the tree
80
81 print fieldTree.keys()
82
83 # _T3A
84 for meshName in fieldTree.keys():
85     print "%s"%meshName
86     for fieldName in fieldTree[meshName].keys():
87         print "  %s"%fieldName
88         for fieldType in fieldTree[meshName][fieldName].keys():
89             print "    %s"%fieldType
90             for itNumber in fieldTree[meshName][fieldName][fieldType].keys():
91                 for itOrder in fieldTree[meshName][fieldName][fieldType][itNumber].keys():
92                     print "      (%s,%s)"%(itNumber,itOrder)
93                     print fieldTree[meshName][fieldName][fieldType][itNumber][itOrder]
94 # _T3B