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

Last change on this file since 16137 was 16137, checked in by Mathieu Morlighem, 12 years ago

merged trunk-jpl and trunk for revision 16135

File size: 2.0 KB
RevLine 
[16098]1import os.path
2import numpy as npy
3from collections import OrderedDict
4from MatlabFuncs import *
5from expread import expread
6from expwrite import expwrite
7
8def 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=raw_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
37 numpoints=npy.size(contour['x'])
38
39 j=0
40 x=contour['x']
41 y=contour['y']
42
43 #stop if we have reached end of profile (always keep the last point)
44 while j<numpoints-1:
45
46 #see whether we keep this point or not
47 distance=npy.sqrt((x[j]-x[j+1])**2+(y[j]-y[j+1])**2)
48 if distance<resolution and j<numpoints-2: #do not remove last point
49 x=npy.delete(x,j+1,0)
50 y=npy.delete(y,j+1,0)
51 numpoints=numpoints-1
52 else:
53 division=int(npy.floor(distance/resolution)+1)
54 if division>=2:
55 xi=npy.linspace(x[j],x[j+1],division)
56 yi=npy.linspace(y[j],y[j+1],division)
57
58 x=npy.hstack((x[0:j+1],xi[1:-1],x[j+1:]))
59 y=npy.hstack((y[0:j+1],yi[1:-1],y[j+1:]))
60
61 #update current point
62 j=j+1+division-2
63 numpoints=numpoints+division-2
64 else:
65 #update current point
66 j=j+1
67
68 if npy.size(x)>1:
69 #keep the (x,y) contour arond
70 newcontour=OrderedDict()
71 newcontour['nods']=npy.size(x)
72 newcontour['density']=contour['density']
73 newcontour['x']=x
74 newcontour['y']=y
75 newcontours.append(newcontour)
76
77 #write output
78 expwrite(newcontours,newfile)
Note: See TracBrowser for help on using the repository browser.