source: issm/branches/trunk-larour-NatGeoScience2016/src/m/coordsystems/gmtmask.py@ 21759

Last change on this file since 21759 was 21759, checked in by Eric.Larour, 8 years ago

CHG: merged branch back to trunk-jpl 21754.

File size: 2.2 KB
Line 
1from MatlabFuncs import *
2from model import *
3import numpy as np
4from os import getenv, putenv
5import subprocess
6
7def gmtmask(lat,long,*varargin):
8#GMTMASK - figure out which lat,long points are on the ocean
9#
10# Usage:
11# mask.ocean = gmtmask(md.mesh.lat,md.mesh.long);
12#
13 lenlat=len(lat)
14 mask=np.empty(lenlat)
15
16 #are we doing a recursive call?
17 if len(varargin)==3:
18 recursive=1
19 else:
20 recursive=0
21
22 if recursive:
23 string=' recursing: num vertices #i'+str(lenlat)
24 else:
25 string='gmtmask: num vertices #i'+str(lenlat)
26
27 #Check lat and long size is not more than 50,000 If so, recursively call gmtmask:
28
29 if lenlat>50000:
30 for i in range(ceil(lenlat/50000)):
31 j=(i+1)*50000-1
32 if j>lenlat:
33 j=lenlat
34 mask[i:j]=gmtmask(lat[i:j],long[i:j],1)
35 return mask
36
37
38 #First, write our lat,long file for gmt:
39 nv=lenlat
40 np.savetxt('./all_vertices.txt',np.transpose([long, lat, np.arange(1,nv+1)]),delimiter='\t',fmt='%.10f')
41
42 #Avoid bypassing of the ld library path by Matlab (:()
43 try:
44 issmdir
45 except:
46 issmdir=getenv('ISSM_DIR')
47 try:
48 ismac
49 except:
50 ismac=False
51
52 if ismac:
53 dyld_library_path_old=getenv('DYLD_LIBRARY_PATH')
54 putenv('DYLD_LIBRARY_PATH',issmdir+'/externalpackages/curl/install/lib:'+issmdir+'/externalpackages/hdf5/install/lib:'+issmdir+'/externalpackages/netcdf/install/lib')
55
56 #figure out which vertices are on the ocean, which one on the continent:
57 subprocess.call(issmdir+'/externalpackages/gmt/install/bin/gmt gmtselect ./all_vertices.txt -h0 -Df -R0/360/-90/90 -A0 -JQ180/200 -Nk/s/s/k/s > ./oce_vertices.txt',shell=True)
58
59 #reset DYLD_LIBRARY_PATH to what it was:
60 if ismac:
61 putenv('DYLD_LIBRARY_PATH',dyld_library_path_old)
62
63 #read the con_vertices.txt file and flag our mesh vertices on the continent
64 fid=open('./oce_vertices.txt','r')
65 line=fid.readline()
66 line=fid.readline()
67 oce_vertices=[]
68 while line:
69 ind=int(float(line.split()[2]))-1;
70 oce_vertices.append(ind)
71 line=fid.readline()
72 fid.close()
73
74 mask=np.zeros([nv,1])
75 mask[oce_vertices]=1
76
77 subprocess.call('rm -rf ./all_vertices.txt ./oce_vertices.txt ./gmt.history',shell=True)
78 if not recursive:
79 string='gmtmask: done'
80 return mask
Note: See TracBrowser for help on using the repository browser.