[16098] | 1 | import os.path
|
---|
| 2 | import numpy as npy
|
---|
| 3 | from collections import OrderedDict
|
---|
| 4 | from expread import expread
|
---|
| 5 | from expwrite import expwrite
|
---|
| 6 |
|
---|
| 7 | def 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=npy.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=npy.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=npy.delete(x,j+1,0)
|
---|
| 49 | y=npy.delete(y,j+1,0)
|
---|
| 50 | numpoints=numpoints-1
|
---|
| 51 | else:
|
---|
| 52 | division=int(npy.floor(distance/resolution)+1)
|
---|
| 53 | if division>=2:
|
---|
| 54 | xi=npy.linspace(x[j],x[j+1],division)
|
---|
| 55 | yi=npy.linspace(y[j],y[j+1],division)
|
---|
| 56 |
|
---|
| 57 | x=npy.hstack((x[0:j+1],xi[1:-1],x[j+1:]))
|
---|
| 58 | y=npy.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 npy.size(x)>1:
|
---|
| 68 | #keep the (x,y) contour arond
|
---|
| 69 | newcontour=OrderedDict()
|
---|
| 70 | newcontour['nods']=npy.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)
|
---|