Home > utilities > transect_nodes_screen.m

transect_nodes_screen

PURPOSE ^

function to select and output index of nodes along a defined transect

SYNOPSIS ^

function [transect] = transect_nodes_screen(Mobj,VarType)

DESCRIPTION ^

 function to select and output index of nodes along a defined transect
 By clicking on points on the screen
   Choose between nodes or vertices (U,V or tracers)
 [transect]  = transect_nodes_screen(Mobj,VarType)

 DESCRIPTION:
    Select using ginput the set of nodes comprising a transect. It will
    interpolate between consecutive points to the resolution of the mesh

 INPUT
    Mobj = Matlab mesh object
    VarType = node or element (for elements, one needs xc and yc from
    FVCOM NC file. Not implemented yet.


 OUTPUT:
    transect = Matlab structure with indices

 EXAMPLE USAGE
    [transect]  = transect_nodes_screen(Mobj,'nodes')

 Author(s):
    Ricardo Torres and Geoff Cowles (University of Massachusetts Dartmouth)

 Note:
    Uses ginput2 which allows zoom/pan before selecting points and displays
    clicked points realtime

 Revision history

==============================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [transect]  = transect_nodes_screen(Mobj,VarType)
0002 % function to select and output index of nodes along a defined transect
0003 % By clicking on points on the screen
0004 %   Choose between nodes or vertices (U,V or tracers)
0005 % [transect]  = transect_nodes_screen(Mobj,VarType)
0006 %
0007 % DESCRIPTION:
0008 %    Select using ginput the set of nodes comprising a transect. It will
0009 %    interpolate between consecutive points to the resolution of the mesh
0010 %
0011 % INPUT
0012 %    Mobj = Matlab mesh object
0013 %    VarType = node or element (for elements, one needs xc and yc from
0014 %    FVCOM NC file. Not implemented yet.
0015 %
0016 %
0017 % OUTPUT:
0018 %    transect = Matlab structure with indices
0019 %
0020 % EXAMPLE USAGE
0021 %    [transect]  = transect_nodes_screen(Mobj,'nodes')
0022 %
0023 % Author(s):
0024 %    Ricardo Torres and Geoff Cowles (University of Massachusetts Dartmouth)
0025 %
0026 % Note:
0027 %    Uses ginput2 which allows zoom/pan before selecting points and displays
0028 %    clicked points realtime
0029 %
0030 % Revision history
0031 %
0032 %==============================================================================
0033 subname = 'transect_nodes_screen';
0034 global ftbverbose
0035 if(ftbverbose)
0036     fprintf('\n')
0037     fprintf(['begin : ' subname '\n'])
0038 end;
0039 %%-----------------------------------------------------------
0040 % Plot the mesh
0041 %------------------------------------------------------------------------------
0042 
0043 if(lower(Mobj.nativeCoords(1:3)) == 'car')
0044     x = Mobj.x;
0045     y = Mobj.y;
0046 else
0047     x = Mobj.lon;
0048     y = Mobj.lat;
0049 end;
0050 
0051 figure
0052 patch('Vertices',[x,y],'Faces',Mobj.tri,...
0053     'Cdata',Mobj.h,'edgecolor','k','facecolor','interp');
0054 hold on;
0055 plot(x(1316),y(1316),'ro')
0056 % use ginput2 (which allows zooming and plots points as they are clicked) to let
0057 % user select the boundary points
0058 [xselect] = ginput2(true,'k+')
0059 [npts,jnk] = size(xselect);
0060 
0061 if(npts == 0)
0062     fprintf('you didn''t select any points')
0063     fprintf(['end   : ' subname '\n'])
0064     return
0065 end;
0066 fprintf('you selected %d points\n',npts)
0067 
0068 % snap to the closest vertices
0069 for i=1:npts
0070     [ipt(i),dist] = find_nearest_pt(xselect(i,1),xselect(i,2),Mobj);
0071 end;
0072 
0073 % replot domain with snapped vertices
0074 plot(x(ipt),y(ipt),'ro');
0075 % find nodes closes to straight lines between selected points
0076 transect.x=[];
0077 transect.y=[];
0078 transect.idx=[];
0079 for i=1:npts-1
0080     dI=1;
0081     keepgoing=1;
0082     while keepgoing
0083         dx=diff(x([ipt(i),ipt(i+1)]));
0084         dy=diff(y([ipt(i),ipt(i+1)]));
0085         datadxy=[dx,dy];
0086         [~,direction]=max(abs([dx,dy]));
0087         data=circshift([x([ipt(i),ipt(i+1)]),y([ipt(i),ipt(i+1)])],[0,direction-1]);
0088         datadxy= circshift(  datadxy,[0,direction-1]);
0089         dataXI=data(1,1):datadxy(1)/dI:data(end,1);
0090         dataYI = interp1(data(:,1),data(:,2),dataXI);
0091         dataI=circshift([dataXI(:),dataYI(:)],[0,direction-1]);
0092         plot(dataI(:,1),dataI(:,2),'yx');
0093         idx=nan*ones(1,length(dataI));
0094         for ii=1:length(dataI)
0095             [idx(ii),~] = find_nearest_pt(dataI(ii,1),dataI(ii,2),Mobj);
0096         end;
0097         % find if there are duplicates in idx
0098         [~,igood]=unique(idx);
0099         if length(igood)~=length(idx)
0100             % there are duplicates so no need to interpolate to higher resolution
0101             keepgoing=0;
0102         else
0103             % increase interpolation to dI*2;
0104             dI=dI*2;
0105             disp('Increasing resolution of transect')
0106         end
0107         idx= idx(sort(igood));
0108         checkelems={};
0109         % check if there are more than two nodes per element in the transect
0110         % first get all possible elements in the transect
0111         for ii=1:length(idx)
0112             checkelems{ii,1}=find(Mobj.tri(:,1)==idx(ii));
0113             checkelems{ii,2}=find(Mobj.tri(:,2)==idx(ii));
0114             checkelems{ii,3}=find(Mobj.tri(:,3)==idx(ii));
0115             
0116         end
0117         
0118         unique_elems=unique(sort(cat(1,checkelems{:})));
0119         cc=1;newidx=zeros(length(unique_elems),2);
0120         for ii=1:length(unique_elems)
0121             test4nodes=zeros(1,3);
0122             for rr=1:3
0123                 test= find(idx==Mobj.tri(unique_elems(ii),rr));
0124                 if ~isempty(test);
0125                     test4nodes(rr)=test;
0126                 end
0127             end
0128             switch length(find(test4nodes))
0129                 case 2
0130                     % we have an element with two nodes. Its a keeper
0131                     newidx(cc,1:2)=test4nodes(find(test4nodes));
0132                     cc=cc+1;
0133                 case 3
0134                     disp(['Too many nodes in element ',num2str(ii)])
0135                     disp('Removing the middle node as locations are in order')
0136 %                     plot(x(idx(test4nodes)),y(idx(test4nodes)),'w-')
0137                     newidx(cc,1:2)=test4nodes([1,3]);
0138                      cc=cc+1;
0139             end
0140         end
0141         newidx=unique(newidx(:));
0142         newidx=newidx(find(newidx));
0143         if length(newidx)==length(idx)
0144             disp('All nodes are linked, continue to next section')
0145             keepgoing=0;
0146             
0147             transect.x=[transect.x;x(idx)];
0148             transect.y=[transect.y;y(idx)];
0149             transect.idx=[transect.idx;idx(:)];
0150             plot(transect.x,transect.y,'wo');
0151             
0152         else
0153             disp('Mising linked nodes in transect, continue with increased resolution')
0154             keepgoing=1;
0155         end
0156     end
0157 end
0158 % check that no repeated values are present in indices
0159             transect.x=unique(transect.x);
0160             transect.y=unique(transect.y);
0161             transect.idx=unique(transect.idx);
0162             return

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005