1 | import os.path
|
---|
2 | import numpy as np
|
---|
3 | from collections import OrderedDict
|
---|
4 | from expread import expread
|
---|
5 | from expwrite import expwrite
|
---|
6 |
|
---|
7 |
|
---|
8 | def expcoarsen(newfile, oldfile, resolution):
|
---|
9 | """
|
---|
10 | EXPCOARSEN - coarsen an exp contour
|
---|
11 |
|
---|
12 | This routine read an Argus file and remove points with respect to
|
---|
13 | the resolution (in meters) given in input.
|
---|
14 |
|
---|
15 | Usage:
|
---|
16 | expcoarsen(newfile, oldfile, resolution)
|
---|
17 |
|
---|
18 | Example:
|
---|
19 | expcoarsen('DomainOutline.exp', 'Antarctica.exp', 4000)
|
---|
20 | """
|
---|
21 |
|
---|
22 | #Some checks
|
---|
23 | if not os.path.exists(oldfile):
|
---|
24 | raise OSError("expcoarsen error message: file '%s' not found!" % oldfile)
|
---|
25 | if os.path.exists(newfile):
|
---|
26 | choice = eval(input('A file ' + newfile + ' already exists, do you want to modify it? (y / n)'))
|
---|
27 | if choice not in 'y':
|
---|
28 | print('no modification done ... exiting')
|
---|
29 | return 0
|
---|
30 |
|
---|
31 | #Get exp oldfile
|
---|
32 | contours = expread(oldfile)
|
---|
33 | newcontours = []
|
---|
34 |
|
---|
35 | for contour in contours:
|
---|
36 | numpoints = np.size(contour['x'])
|
---|
37 |
|
---|
38 | j = 0
|
---|
39 | x = contour['x']
|
---|
40 | y = contour['y']
|
---|
41 |
|
---|
42 | #stop if we have reached end of profile (always keep the last point)
|
---|
43 | while j < numpoints - 1:
|
---|
44 |
|
---|
45 | #see whether we keep this point or not
|
---|
46 | distance = np.sqrt((x[j] - x[j + 1])**2 + (y[j] - y[j + 1])**2)
|
---|
47 | if distance < resolution and j < numpoints - 2: #do not remove last point
|
---|
48 | x = np.delete(x, j + 1, 0)
|
---|
49 | y = np.delete(y, j + 1, 0)
|
---|
50 | numpoints = numpoints - 1
|
---|
51 | else:
|
---|
52 | division = int(np.floor(distance / resolution) + 1)
|
---|
53 | if division >= 2:
|
---|
54 | xi = np.linspace(x[j], x[j + 1], division)
|
---|
55 | yi = np.linspace(y[j], y[j + 1], division)
|
---|
56 |
|
---|
57 | x = np.hstack((x[0:j + 1], xi[1:-1], x[j + 1:]))
|
---|
58 | y = np.hstack((y[0:j + 1], yi[1:-1], y[j + 1:]))
|
---|
59 |
|
---|
60 | #update current point
|
---|
61 | j = j + 1 + division - 2
|
---|
62 | numpoints = numpoints + division - 2
|
---|
63 | else:
|
---|
64 | #update current point
|
---|
65 | j = j + 1
|
---|
66 |
|
---|
67 | if np.size(x) > 1:
|
---|
68 | #keep the (x, y) contour arond
|
---|
69 | newcontour = OrderedDict()
|
---|
70 | newcontour['nods'] = np.size(x)
|
---|
71 | newcontour['density'] = contour['density']
|
---|
72 | newcontour['x'] = x
|
---|
73 | newcontour['y'] = y
|
---|
74 | newcontours.append(newcontour)
|
---|
75 |
|
---|
76 | #write output
|
---|
77 | expwrite(newcontours, newfile)
|
---|