/[MITgcm]/MITgcm/utils/matlab/ocean_basin/set_mask.m
ViewVC logotype

Contents of /MITgcm/utils/matlab/ocean_basin/set_mask.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Tue Feb 7 17:02:10 2017 UTC (7 years, 5 months ago) by dimitri
Branch: MAIN
CVS Tags: checkpoint66g, checkpoint66f, checkpoint66e, checkpoint66d, checkpoint66o, checkpoint66n, checkpoint66m, checkpoint66l, checkpoint66k, checkpoint66j, checkpoint66i, checkpoint66h, HEAD
adding ocean_basin routines for defining basin masks

1 function [newmask,iMask,XV,YV]=set_mask(x,y,mask,val)
2
3 % [newmask,iMask,XV,YV]=set_mask(x,y,mask,val)
4 % GUI to create mask by hand. Idea is to set masking area
5 % by selecting polygon(s) or selecting single points.
6 % Area enclosed by the polygon or the selected point is
7 % set to value 'val'.
8 % Press 'v' or 'o' to begin selecting a polygon.
9 % If you want to mark a single point, place cursor over that point
10 % and press 'm'.
11 % If you pressed 'v' or 'o', click as many times as needed to
12 % pick the vertices of a polygon.
13 % When finished picking points, hit 'enter'.
14 % (It is not necessary to close the polygon. This will be
15 % done automatically.) Points with a value of '1' (ocean) are set
16 % to a value of 'val'. Thus land points (NaN) and points
17 % corresponding to other masks i.e., values ~= 1 (displayed in
18 % different colors) will not be touched. HOWEVER, if you
19 % pressed 'o', the latter points will be OVERWRITTEN.
20 % If you pressed 'm', the selected point is set to value 'val',
21 % but ONLY IF its original value was '1' (ocean).
22 % So you can call this
23 % function multiple times with a different value of 'val', and
24 % set a different mask each time (without overwriting what was
25 % set during a previous call).
26 % Once the polygon has been selected and the values set, the
27 % plot will update. You can then pick another polygon or select
28 % another point by hitting 'v'/'o' or 'm'.
29 % To end, press 'e'.
30 % To undo the last step, press 'u'.
31 % Outputs:
32 % newmask: the modified mask
33 % iMask: vector of indices of points that were modified
34 % XV,YV: cell arrays containing the vertices of polygons selected.
35 % Thus, the action of this script can be reproduced in two ways:
36 % (1) newmask=mask;
37 % newmask(iMask)=val;
38 % (2) newmask=mask; [Xp,Yp]=ndgrid(x,y);
39 % for iv=1:length(XV)
40 % in=inpolygon(Xp,Yp,XV{iv},YV{iv});
41 % ii=find(newmask==1 & in==1);
42 % newmask(ii)=val;
43 % end
44 % (1) is most robust. (2) will not reproduce the action of
45 % selecting single points, but it will allow you to set masks
46 % defined on a different grid.
47
48 if val==0 | val==1
49 error('Cannot set mask value to 0 or 1')
50 end
51
52 newmask=mask;
53 iNaNs=find(isnan(newmask));
54 newmask(iNaNs)=0;
55 [Xp,Yp]=ndgrid(x,y);
56
57 % pcolor(x,y,newmask');
58 % set(gcf,'renderer','zbuffer'); % hack to deal with disappearing crosshairs
59 refresh_plot(x,y,newmask)
60
61 iv=1;
62 iMask=[];
63 XV=[];
64 YV=[];
65 but='v';
66 while (~strcmp(but,'e'))
67 [x1,y1,but]=ginput(1);
68 but=char(but);
69 if (strcmp(but,'v') | strcmp(but,'o')) % select polygon
70 xv=[];yv=[]; but1=but;
71 while (~strcmp(but1,''))
72 [x1,y1,but1]=ginput(1);
73 but1=char(but1);
74 xv=[xv;x1];yv=[yv;y1];
75 add_point(xv,yv)
76 end
77 xv=[xv;xv(1)]; yv=[yv;yv(1)];
78 XV{iv}=xv; YV{iv}=yv; iv=iv+1;
79 in=inpolygon(Xp,Yp,xv,yv);
80 if (strcmp(but,'v'))
81 % Only select water points and points in polygon. Points belonging to
82 % other masks (different colors/values~=val) are not touched.
83 lastBut='v';
84 ii=find(newmask==1 & in==1);
85 else
86 % Only select water points and points in polygon. Points belonging to
87 % other masks (different colors/values~=val) may be overwritten.
88 % ii=find(~isnan(newmask) & in==1);
89 lastBut='o';
90 ii=find(newmask~=0 & in==1);
91 end
92 oldmask=newmask; % in case we want to undo
93 newmask(ii)=val;
94 iMask=[iMask;ii];
95 % pcolor(x,y,newmask');
96 % set(gcf,'renderer','zbuffer'); % hack to deal with disappearing crosshairs
97 refresh_plot(x,y,newmask,XV,YV)
98 elseif (strcmp(but,'m'))
99 lastBut='m';
100 [m,im]=min(abs(x1-x));
101 [m,jm]=min(abs(y1-y));
102 % only mark water points
103 if newmask(im,jm)==1
104 ii=sub2ind(size(newmask),im,jm);
105 oldmask=newmask; % in case we want to undo
106 newmask(ii)=val;
107 iMask=[iMask;ii];
108 refresh_plot(x,y,newmask)
109 end
110 elseif (strcmp(but,'u')) % undo
111 nadd=length(ii); % number of points added
112 disp('Undoing last step ...')
113 newmask=oldmask; % reset previous values
114 iMask=iMask(1:end-nadd); % delete mask indices
115 if lastBut~='m'
116 tmpXV=XV; tmpYV=YV; % delete polygon vertices. is there an easy way to do this?
117 clear XV YV
118 for k=1:length(tmpXV)-1
119 XV{k}=tmpXV{k};
120 YV{k}=tmpYV{k};
121 end
122 iv=iv-1;
123 % pcolor(x,y,newmask');
124 % set(gcf,'renderer','zbuffer'); % hack to deal with disappearing crosshairs
125 end
126 refresh_plot(x,y,newmask,XV,YV)
127 end
128 end
129
130 newmask(iNaNs)=NaN;
131
132 function refresh_plot(x,y,mask,XV,YV)
133
134 % pcolor(x,y,mask');
135 % shading interp
136 imagesc(x,y,mask')
137 axis xy
138 % set(gcf,'renderer','zbuffer'); % hack to deal with disappearing crosshairs
139
140 if nargin>3
141 hold on
142 for k=1:length(XV)
143 plot(XV{k},YV{k},'k-o')
144 end
145 hold off
146 end
147
148 function add_point(x,y)
149
150 hold on
151 plot(x,y,'k-o')
152 hold off

  ViewVC Help
Powered by ViewVC 1.1.22