%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                 ProcED's Subroutine                     %
%       set isolines for the velocity field plots         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    function isoline(isoline,speed,dist_i,depth_i,dist,maxdepth,k)

   	% isoline = set isoline type
    %  = 1 - equal isolines, 
    %  = 2 - dashed line for negative values and fine line for positive values
    %  = 3 - countorf plot

    % speed - velocity matrix
    % dist_i - distance array
    % depth_i - depth array
    % dist - bottom depth countor distance
    % maxdepth - max bottom depth
    
    
    % set the boundaries
    c1=round(min(smartmean(speed,1))*100)/100;
    c2=round(max(smartmean(speed,1))*100)/100;

    c1_bk=c1; c2_bk=c2; % save c1 e c2
    
    % set initial isolines interval
    int = 0.20;

    % set other intervals
    
    if abs(c2-c1) < 0.6; int=0.1;end
    if abs(c2-c1) <= 0.25; int=0.05;end  
    if abs(c2-c1) <= 0.09; int=0.03;end
    if abs(c2-c1) > 1.2; int=0.3;end

    % correct boundaries when near to zero
    
    c_c1=c1/int;
    c_c2=c2/int;

    if c_c1 > 0
        c3=floor(c_c1);
    else
        c3=ceil(c_c1);
    end
    if c3 == 0
        c1=sign(c1_bk)*int;
        if abs(c1) > abs(c1_bk)
            c1=0;
        end
    else
        c1=c3*int;
    end

    if c_c2 > 0
        c4=floor(c_c2);
    else
        c4=ceil(c_c2);
    end
    if c4 == 0
        c2=sign(c2_bk)*int;
        if abs(c2) > abs(c2_bk)
            c2=0;
        end
    else
        c2=c4*int;
    end

    % review intervals after rounding
    if abs(c2-c1) <= 0.6; int=0.1;end
    if abs(c2-c1) <= 0.3; int=0.05;end
    if abs(c2-c1) <= 0.09; int=0.03;end
    if abs(c2-c1) > 1.2; int=0.3;end

    % check if the boundaries can be expanded
    if abs(c1_bk-c1) >= int
        c1=c1+sign(c1_bk)*int;
    end
    
    if abs(c2_bk-c2) >= int
        c2=c2+sign(c2_bk)*int;
    end
    
    % set isolines
    cnt1 = [c1:int:0 0+int:int:c2];

    % Plots
    
    % TYPE # 1
    
if isoline==1 
    
    figure(k);
	
	ind = find(cnt1 ~= 0); % nonzero isolines
    if (ind > 0)
        cntnz = cnt1(ind);
        if length(ind) > 1
			[cs,h]=contour(dist_i,depth_i,speed,cntnz,'-k');          
			clabel(cs,h,cnt1(ind),'fontsize',10,'fontweight','bold','rotation',0);
		else
			[cs,b]=contour(dist_i,depth_i,speed,[cntnz cntnz],'-k');          
			clabel(cs,b,cntnz,'fontsize',10,'fontweight','bold','rotation',0);
		end
	end
	hold on
	ind = find(cnt1 == 0); % zero isolines
    if (ind > 0)
        cntzero = cnt1(ind);
        [cs,b]=contour(dist_i,depth_i,speed,[cntzero cntzero],'k');        
        hold on;
		set(b,'linewidth',1.5);
    end		

    plot(dist,maxdepth,'-k','linewidth',2);
end
    
% TYPE # 2

if isoline==2  % different lines to u>0 and u<0
    
   figure(k+100);

    ind = find(cnt1 < 0); % negative isolines
    if (ind > 0)
        cntneg = cnt1(ind);
        if length(ind) > 1
            [cs,h]=contour(dist_i,depth_i,speed,cntneg,':k');        
            h = clabel(cs,h,cnt1(ind),'fontsize',9,'rotation',0);
        else
            [cs,b]=contour(dist_i,depth_i,speed,[cntneg cntneg],':k');        
            b = clabel(cs,b,cntneg,'fontsize',9,'rotation',0);
        end
        hold on;
    end
   
    ind = find(cnt1 > 0); % positive isolines
    if (ind > 0)
        cntpos = cnt1(ind);
        if length(ind) > 1
            [cs,h]=contour(dist_i,depth_i,speed,cntpos,'-k');        
            h = clabel(cs,h,cnt1(ind),'fontsize',9,'rotation',0);
        else
            [cs,b]=contour(dist_i,depth_i,speed,[cntpos cntpos],'-k');
            b = clabel(cs,b,cntpos,'fontsize',9,'rotation',0);
        end
        hold on;
    end
    
    ind = find(cnt1 == 0); % zero isolines
    if (ind > 0)
        cntzero = cnt1(ind);
        [cs,b]=contour(dist_i,depth_i,speed,[cntzero cntzero],'k');        
        hold on;
		set(b,'linewidth',1.5);
    end
    
    plot(dist,maxdepth,'-k','linewidth',2);
    
end

% TYPE # 3

if isoline==3 %% color plot %%

    load BlWhRe_colormap
    min_speed=min(smartmean(speed,1));
    max_speed=max(smartmean(speed,1));
    figure(k);
    pcolor(dist_i,depth_i,speed),shading interp, hold on
	
	ind = find(cnt1 ~= 0); % nonzero isolines
    if (ind > 0)
        cntnz = cnt1(ind);
        if length(ind) > 1
			[cs,h]=contour(dist_i,depth_i,speed,cntnz,'-k');          
			clabel(cs,h,cnt1(ind),'fontsize',10,'fontweight','bold','rotation',0);
		else
			[cs,b]=contour(dist_i,depth_i,speed,[cntnz cntnz],'-k');          
			clabel(cs,b,cntnz,'fontsize',10,'fontweight','bold','rotation',0);
		end
	end
	hold on
	ind = find(cnt1 == 0); % zero isolines
    if (ind > 0)
        cntzero = cnt1(ind);
        [cs,b]=contour(dist_i,depth_i,speed,[cntzero cntzero],'k');        
        hold on;
		set(b,'linewidth',1.5);
    end		
	colormap(BlWhRe_colormap);
	caxis([min_speed max_speed]);
    plot(dist,maxdepth,'-k','linewidth',2); 
	
end
    xlabel('Distance [m]');
    ylabel('Depth [m]');
    hold on;
    
% plot matrix points
%    ind0=isnan(speed);
%    [ind0x,ind0y]=find(ind0==0);
%    plot(dist_i(ind0y),depth_i(ind0x),'.k')

