| 1 | import os.path
|
|---|
| 2 | import numpy as np
|
|---|
| 3 | from collections import OrderedDict
|
|---|
| 4 | import MatlabFuncs as m
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 | def expread(filename):
|
|---|
| 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') and m.strcmp(A[3], 'Y') and m.strcmp(A[4], 'pos')):
|
|---|
| 73 | break
|
|---|
| 74 | #Get Coordinates
|
|---|
| 75 | contour['x'] = np.empty(contour['nods'])
|
|---|
| 76 | contour['y'] = np.empty(contour['nods'])
|
|---|
| 77 | for i in range(int(contour['nods'])):
|
|---|
| 78 | A = fid.readline().split()
|
|---|
| 79 | contour['x'][i] = float(A[0])
|
|---|
| 80 | contour['y'][i] = float(A[1])
|
|---|
| 81 |
|
|---|
| 82 | #Check if closed
|
|---|
| 83 | if (contour['nods'] > 1) and (contour['x'][-1] == contour['x'][0]) and (contour['y'][-1] == contour['y'][0]):
|
|---|
| 84 | contour['closed'] = True
|
|---|
| 85 | else:
|
|---|
| 86 | contour['closed'] = False
|
|---|
| 87 |
|
|---|
| 88 | contours.append(contour)
|
|---|
| 89 | #close file
|
|---|
| 90 | fid.close()
|
|---|
| 91 | return contours
|
|---|