1 | import os.path
|
---|
2 | import numpy
|
---|
3 | from collections import OrderedDict
|
---|
4 | import MatlabFuncs as m
|
---|
5 |
|
---|
6 | def expread(filename):
|
---|
7 | """
|
---|
8 | EXPREAD - read a file exp and build a Structure
|
---|
9 |
|
---|
10 | This routine reads a file .exp and builds a list of dicts containing the
|
---|
11 | fields x and y corresponding to the coordinates, one for the filename of
|
---|
12 | the exp file, for the density, for the nodes, and a field closed to
|
---|
13 | indicate if the domain is closed.
|
---|
14 | The first argument is the .exp file to be read and the second one (optional)
|
---|
15 | indicate if the last point shall be read (1 to read it, 0 not to).
|
---|
16 |
|
---|
17 | Usage:
|
---|
18 | contours=expread(filename)
|
---|
19 |
|
---|
20 | Example:
|
---|
21 | contours=expread('domainoutline.exp')
|
---|
22 | contours=expread('domainoutline.exp')
|
---|
23 |
|
---|
24 | See also EXPDOC, EXPWRITEASVERTICES
|
---|
25 | """
|
---|
26 |
|
---|
27 | #some checks
|
---|
28 | if not os.path.exists(filename):
|
---|
29 | raise OSError("expread error message: file '%s' not found!" % filename)
|
---|
30 |
|
---|
31 | #initialize number of profile
|
---|
32 | contours=[]
|
---|
33 |
|
---|
34 | #open file
|
---|
35 | fid=open(filename,'r')
|
---|
36 |
|
---|
37 | #loop over the number of profiles
|
---|
38 | while True:
|
---|
39 |
|
---|
40 | #update number of profiles
|
---|
41 | contour=OrderedDict()
|
---|
42 |
|
---|
43 | #Get file name
|
---|
44 | A=fid.readline()
|
---|
45 | while A=='\n':
|
---|
46 | A=fid.readline()
|
---|
47 | if not A:
|
---|
48 | break
|
---|
49 | A=A.split(None,1)
|
---|
50 | if not (len(A) == 2 and m.strcmp(A[0],'##') and m.strncmp(A[1],'Name:',5)):
|
---|
51 | break
|
---|
52 | if len(A[1])>5:
|
---|
53 | contour['name']=A[1][5:-1]
|
---|
54 | else:
|
---|
55 | contour['name']=''
|
---|
56 |
|
---|
57 | #Get Icon
|
---|
58 | A=fid.readline().split(None,1)
|
---|
59 | if not (len(A) == 2 and m.strcmp(A[0],'##') and m.strncmp(A[1],'Icon:',5)):
|
---|
60 | break
|
---|
61 |
|
---|
62 | #Get Info
|
---|
63 | A=fid.readline().split()
|
---|
64 | if not (len(A) == 4 and m.strcmp(A[0],'#') and m.strcmp(A[1],'Points')):
|
---|
65 | break
|
---|
66 |
|
---|
67 | #Get number of nodes and density
|
---|
68 | A=fid.readline().split()
|
---|
69 | contour['nods']=int(A[0])
|
---|
70 | contour['density']=float(A[1])
|
---|
71 |
|
---|
72 | #Get Info
|
---|
73 | A=fid.readline().split()
|
---|
74 | if not (len(A) == 5 and m.strcmp(A[0],'#') and m.strcmp(A[1],'X') and m.strcmp(A[2],'pos') \
|
---|
75 | and m.strcmp(A[3],'Y') and m.strcmp(A[4],'pos')):
|
---|
76 | break
|
---|
77 |
|
---|
78 | #Get Coordinates
|
---|
79 | contour['x']=numpy.empty(contour['nods'])
|
---|
80 | contour['y']=numpy.empty(contour['nods'])
|
---|
81 | for i in range(int(contour['nods'])):
|
---|
82 | A=fid.readline().split()
|
---|
83 | contour['x'][i]=float(A[0])
|
---|
84 | contour['y'][i]=float(A[1])
|
---|
85 |
|
---|
86 | #Check if closed
|
---|
87 | if (contour['nods'] > 1) and \
|
---|
88 | (contour['x'][-1] == contour['x'][0]) and \
|
---|
89 | (contour['y'][-1] == contour['y'][0]):
|
---|
90 | contour['closed']=True
|
---|
91 | else:
|
---|
92 | contour['closed']=False
|
---|
93 |
|
---|
94 | contours.append(contour)
|
---|
95 |
|
---|
96 | #close file
|
---|
97 | fid.close()
|
---|
98 |
|
---|
99 | return contours
|
---|
100 |
|
---|