source: storm_surge/pyvolution/load_mesh/Copy of loadASCII.py @ 1

Last change on this file since 1 was 1, checked in by duncan, 20 years ago

initial import

File size: 8.7 KB
Line 
1
2from string import  find, rfind
3
4def mesh_file_to_mesh_dictionary(fileName):
5    """Load a pmesh file.  Returning the mesh dictionary.
6    """
7    try:
8        meshdic = import_trianglulation(fileName)
9    except IOError, e:       
10        msg = 'Could not open file %s ' %fileName
11        raise IOError, msg
12    return meshdic
13
14def import_trianglulation(ofile):
15    """
16    import a file, ofile, with the format
17   
18    First line:  <# of vertices> <# of attributes>
19    Following lines:  <vertex #> <x> <y> [attributes]
20    One line:  <# of triangles>
21    Following lines:  <triangle #> <vertex #>  <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]
22    One line:  <# of segments>
23    Following lines:  <segment #> <vertex #>  <vertex #> [boundary marker]
24    Note: This might throw a can't load file error
25    """
26    fd = open(ofile,'r')
27    dict = read_trianglulation(fd)
28    dict_mesh = read_mesh(fd)
29    for element in dict_mesh.keys():
30        dict[element] = dict_mesh[element]
31       
32    fd.close()
33    return dict
34
35
36def read_trianglulation(fd):
37    delimiter = " "
38    ######### loading the point info
39    line = fd.readline()
40    #print line
41    fragments = line.split()
42    #for fragment in fragments:
43    #    print fragment
44    NumOfVertices = fragments[0]
45    NumOfVertAttributes = fragments[1]
46    points = []
47    pointattributes = []
48    for index in range(int(NumOfVertices)):       
49        #print index
50        fragments = fd.readline().split()
51        #print fragments
52        fragments.pop(0) #pop off the index
53       
54        # pop the x y off so we're left with a list of attributes       
55        vert = [float(fragments.pop(0)),float(fragments.pop(0))]
56        points.append(vert)
57        apointattributes  = []
58        #print fragments
59        for fragment in fragments:
60            apointattributes.append(float(fragment))
61        pointattributes.append(apointattributes)
62       
63    ######### loading the triangle info
64    line = fd.readline()
65    #print line
66    fragments = line.split()
67    #for fragment in fragments:
68    #    print fragment
69    NumOfTriangles = fragments[0]
70    triangles = []
71    triangleattributes = []
72    triangleneighbors = []
73    for index in range(int(NumOfTriangles)):       
74        #print index
75        line = fd.readline()
76        line.strip() # so we can get the region string
77        fragments = line.split()
78        #print fragments
79        fragments.pop(0) #pop off the index
80       
81        tri = [int(fragments[0]),int(fragments[1]),int(fragments[2])]
82        triangles.append(tri)
83        neighbors = [int(fragments[3]),int(fragments[4]),int(fragments[5])]
84        triangleneighbors.append(neighbors)
85        for x in range(7): # remove index [<vertex #>] [<neigbouring tri #>]
86            line = line[find(line,delimiter):] # remove index
87            line = line.lstrip()
88        stringmarker = line.strip()
89        triangleattributes.append([stringmarker])
90       
91    ######### loading the segment info
92    line = fd.readline()
93    #print line
94    fragments = line.split()
95    #for fragment in fragments:
96    #    print fragment
97    NumOfSegments = fragments[0]
98    segments = []
99    segmentmarkers = []
100    for index in range(int(NumOfSegments)):       
101        #print index
102        line = fd.readline()
103        line.strip() # to get the segment string
104        fragments = line.split()
105        #print fragments
106        fragments.pop(0) #pop off the index
107        seg = [int(fragments[0]),int(fragments[1])]
108        segments.append(seg)
109        line = line[find(line,delimiter):] # remove index
110        line = line.lstrip()
111        line = line[find(line,delimiter):] # remove x
112        line = line.lstrip()
113        line = line[find(line,delimiter):] # remove y
114        stringmarker = line.strip()
115        segmentmarkers.append(stringmarker)
116    meshDict = {}
117    meshDict['generatedpointlist'] = points
118    meshDict['generatedpointattributelist'] = pointattributes
119    meshDict['generatedtrianglelist'] = triangles
120    meshDict['generatedtriangleattributelist'] = triangleattributes
121    meshDict['generatedtriangleneighborlist'] = triangleneighbors
122    meshDict['generatedsegmentlist'] = segments
123    meshDict['generatedsegmentmarkerlist'] = segmentmarkers
124    return meshDict
125   
126def import_mesh(ofile):
127    """
128    import a file, ofile, with the format
129   
130    First line:  <# of vertices> <# of attributes>
131    Following lines: <x> <y> [attributes]
132    One line:  <# of segments>
133    Following lines:  <vertex #>  <vertex #> [boundary marker]
134    Note: This might throw a can't load file error
135    """
136    fd = open(ofile,'r')
137    meshDict = read_mesh(fd)
138    fd.close()
139    return meshDict
140
141def read_mesh(fd):
142    delimiter = " " # warning: split() calls are using default whitespace
143   
144    ######### loading the point info
145    line = fd.readline()
146    #print line
147    fragments = line.split()
148    #for fragment in fragments:
149    #    print fragment
150    NumOfVertices = fragments[0]
151    NumOfVertAttributes = fragments[1]
152    points = []
153    pointattributes = []
154    for index in range(int(NumOfVertices)):       
155        #print index
156        fragments = fd.readline().split() 
157        #print fragments
158        fragments.pop(0) #pop off the index
159        # pop the x y off so we're left with a list of attributes
160        vert = [float(fragments.pop(0)),float(fragments.pop(0))]
161        points.append(vert)
162        apointattributes  = []
163        #print fragments
164        for fragment in fragments:
165            apointattributes.append(float(fragment))
166        pointattributes.append(apointattributes)
167       
168
169    ######### loading the segment info
170    line = fd.readline()
171    #print line
172    fragments = line.split()
173    #for fragment in fragments:
174    #    print fragment
175    NumOfSegments = fragments[0]
176    segments = []
177    segmentmarkers = []
178    for index in range(int(NumOfSegments)): 
179        #print index
180        line = fd.readline()
181        fragments = line.split()
182        #print fragments
183        fragments.pop(0) #pop off the index
184       
185        seg = [int(fragments[0]),int(fragments[1])]
186        segments.append(seg)
187        line = line[find(line,delimiter):] # remove index
188        line = line.lstrip()
189        line = line[find(line,delimiter):] # remove x
190        line = line.lstrip()
191        line = line[find(line,delimiter):] # remove y
192        stringmarker = line.strip()
193        segmentmarkers.append(stringmarker) 
194
195    ######### loading the hole info
196    line = fd.readline()
197    #print line
198    fragments = line.split()
199    #for fragment in fragments:
200    #    print fragment
201    numOfHoles = fragments[0]
202    holes = []
203    for index in range(int(numOfHoles)):       
204        #print index
205        fragments = fd.readline().split()
206        #print fragments
207        fragments.pop(0) #pop off the index
208        hole = [float(fragments[0]),float(fragments[1])]
209        holes.append(hole)
210
211   
212    ######### loading the region info
213    line = fd.readline()
214    #print line
215    fragments = line.split()
216    #for fragment in fragments:
217    #    print fragment
218    numOfRegions = fragments[0]
219    regions = []
220    regionattributes = []
221    for index in range(int(numOfRegions)):
222        line = fd.readline()
223        fragments = line.split()
224        #print fragments
225        fragments.pop(0) #pop off the index
226        region = [float(fragments[0]),float(fragments[1])]
227        regions.append(region)
228
229        line = line[find(line,delimiter):] # remove index
230        line = line.lstrip()
231        line = line[find(line,delimiter):] # remove x
232        line = line.lstrip()
233        line = line[find(line,delimiter):] # remove y
234        stringmarker = line.strip()
235        regionattributes.append(stringmarker)
236    regionmaxareas = []
237    for index in range(int(numOfRegions)): # Read in the Max area info
238        line = fd.readline()
239        fragments = line.split()
240        #print fragments
241        fragments.pop(0) #pop off the index
242        if len(fragments) == 0: #no max area
243            regionmaxareas.append(None)
244        else:
245            regionmaxareas.append(float(fragments[0]))
246
247       
248    meshDict = {}
249    meshDict['pointlist'] = points
250    meshDict['pointattributelist'] = pointattributes
251    meshDict['segmentlist'] = segments
252    meshDict['segmentmarkerlist'] = segmentmarkers
253    meshDict['holelist'] = holes
254    meshDict['regionlist'] = regions
255    meshDict['regionattributelist'] = regionattributes
256    meshDict['regionmaxarealist'] = regionmaxareas
257
258    return meshDict
259
260if __name__ == "__main__":
261    m = import_mesh("tee.txt")
262    print m
Note: See TracBrowser for help on using the repository browser.