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