[16098] | 1 | import os.path
|
---|
[24313] | 2 | import numpy as np
|
---|
[16098] | 3 | from collections import OrderedDict
|
---|
| 4 | from expread import expread
|
---|
| 5 | from expwrite import expwrite
|
---|
| 6 |
|
---|
| 7 |
|
---|
[24313] | 8 | def expcoarsen(newfile, oldfile, resolution):
|
---|
| 9 | """
|
---|
| 10 | EXPCOARSEN - coarsen an exp contour
|
---|
[16098] | 11 |
|
---|
[24313] | 12 | This routine read an Argus file and remove points with respect to
|
---|
| 13 | the resolution (in meters) given in input.
|
---|
[16098] | 14 |
|
---|
[24313] | 15 | Usage:
|
---|
| 16 | expcoarsen(newfile, oldfile, resolution)
|
---|
[16098] | 17 |
|
---|
[24313] | 18 | Example:
|
---|
| 19 | expcoarsen('DomainOutline.exp', 'Antarctica.exp', 4000)
|
---|
| 20 | """
|
---|
[16098] | 21 |
|
---|
[24313] | 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
|
---|
[16098] | 30 |
|
---|
[24313] | 31 | #Get exp oldfile
|
---|
| 32 | contours = expread(oldfile)
|
---|
| 33 | newcontours = []
|
---|
[16098] | 34 |
|
---|
[24313] | 35 | for contour in contours:
|
---|
| 36 | numpoints = np.size(contour['x'])
|
---|
[16098] | 37 |
|
---|
[24313] | 38 | j = 0
|
---|
| 39 | x = contour['x']
|
---|
| 40 | y = contour['y']
|
---|
[16098] | 41 |
|
---|
[24313] | 42 | #stop if we have reached end of profile (always keep the last point)
|
---|
| 43 | while j < numpoints - 1:
|
---|
[16098] | 44 |
|
---|
[24313] | 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)
|
---|
[16098] | 56 |
|
---|
[24313] | 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)
|
---|