source: issm/trunk/src/m/exp/expcoarsen.py@ 21341

Last change on this file since 21341 was 21341, checked in by Mathieu Morlighem, 8 years ago

merged trunk-jpl and trunk for revision 21337

File size: 1.9 KB
Line 
1import os.path
2import numpy as np
3from collections import OrderedDict
4from expread import expread
5from expwrite import expwrite
6
7def expcoarsen(newfile,oldfile,resolution):
8 """
9 EXPCOARSEN - coarsen an exp contour
10
11 This routine read an Argus file and remove points with respect to
12 the resolution (in meters) given in input.
13
14 Usage:
15 expcoarsen(newfile,oldfile,resolution)
16
17 Example:
18 expcoarsen('DomainOutline.exp','Antarctica.exp',4000)
19 """
20
21 #Some checks
22 if not os.path.exists(oldfile):
23 raise OSError("expcoarsen error message: file '%s' not found!" % oldfile)
24 if os.path.exists(newfile):
25 choice=raw_input('A file ' + newfile + ' already exists, do you want to modify it? (y/n)')
26 if choice not in 'y':
27 print('no modification done ... exiting')
28 return 0
29
30 #Get exp oldfile
31 contours=expread(oldfile)
32 newcontours=[]
33
34 for contour in contours:
35
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)
Note: See TracBrowser for help on using the repository browser.