% Saving, Adaptation, and Plots for Adaptive DO solvers

%At first timestep do preliminaries
if k==0
    %Initialize the number of outputs for zero contour level and vehicle
    %path
    pathcount=0;
    %     auvplotcol={'bs','rs','ys','ws','ms','ks','cs','gs'};
    auvplotcol={'b-','r-','y-','w-','m-','g-','k-','c-'};
    %Will also require second smallest mode to be less
    %than twice this value
    %Make limits for plotting
    XLIM = [min(XP(:)),max(XP(:))];
    YLIM = [min(YP(:)),max(YP(:))];
    %Select the colormap
    colormap(mk_colmap());
    %How many std dev's away to plot vorticity
    STDVORT = 3;
end
if ~isfield(app,'noplot')
    %Plotting Routines
    NPLOTSx=2;
    NPLOTSy=2;
    
    PLTNUM=1;
    subplot(NPLOTSx,NPLOTSy,PLTNUM),cla
    tmp1 = 0.5*(u(Nodeu(2:app.Ny+1,1:app.Nx))+u(Nodeu(2:app.Ny+1,2:app.Nx+1)));
    tmp2 = 0.5*(v(Nodev(2:app.Ny+1,2:app.Nx+1))+v(Nodev(1:app.Ny,2:app.Nx+1)));
    hold on
    dudy = (u(Nodeu(2:end-2,2:end-1))-u(Nodeu(3:end-1,2:end-1)))/dy;
    dvdx = (v(Nodev(2:end-1,3:end-1))-v(Nodev(2:end-1,2:end-2)))/dx;
    
    vort = dvdx-dudy;
    
    pcolor(XU(2:end,:),YV(:,2:end),vort),
    shading interp
    caxis([min(-std(vort(:)),-eps),max(std(vort(:)),0)]*STDVORT+mean(vort(:)));
    h=streamslice(XP,YP,tmp1,tmp2);
    set(h,'color','k');
    plot(APath(:,1)',APath(:,2)','ok')
    plot(APath(:,1)',APath(:,2)','.k','MarkerSize',10)
    
    if isfield(app,'Targets')
        plot(app.Targets(:,1)',app.Targets(:,2)','ok')
        plot(app.Targets(:,1)',app.Targets(:,2)','xk','MarkerSize',10)
    end
    hold off
    
    xlim(XLIM);ylim(YLIM)
    TT(1)=xlabel('x');TT(2)=ylabel('y');
    TT(3)=title(sprintf('Time =%03.03g \nu=[%0.2g,%0.2g]',T,...
        min(sqrt(tmp1(:).^2+tmp2(:).^2)),max(sqrt(tmp1(:).^2+tmp2(:).^2))));
    axis equal
    
    PLTNUM=PLTNUM+1;
    subplot(NPLOTSx,NPLOTSy,PLTNUM),cla
    pcolor(XP,YP,rho(NodeP(2:app.Ny+1,2:app.Nx+1)))
    caxis([-0.2,0.2]);
    shading interp
    hold on
end
pathcount=pathcount+1;
pathout.C{pathcount}=contour(XP,YP,rho(NodeP(2:app.Ny+1,2:app.Nx+1)),[0.0 0.0],'k','linewidth',2);
for i = 1:pathout.N
    pathout.p(pathcount,:,i) = [APath(i,:) T];
    if ~isfield(app,'noplot')
        plot(pathout.p(1:pathcount,1,i)',pathout.p(1:pathcount,2,i)',auvplotcol{i},'linewidth',2)
        plot(pathout.p(pathcount,1,i)',pathout.p(pathcount,2,i)',sprintf('.%s',auvplotcol{i}))
    end
end
pathout.t(pathcount)     = T;

%Figure out how many close contours we have:
lev1=pathout.C{pathcount}(1);
num=pathout.C{pathcount}(2);
numS = 1;
numC=0;
while numS < size(pathout.C{pathcount},2)
    num = pathout.C{pathcount}(2,numS);
    numS = num+numS+1;
    numC=numC+1;
end

%Plot endpoints
%plot([0.1, .3],[0.1,0.85 ],'xk','LineWidth',1.5)
%plot([0.1, .3],[0.1,0.85 ],'.k','MarkerSize',10)
%plot([0.1, .3],[0.1,0.85 ],'oK')

if ~isfield(app,'noplot')
    title('Level Set with Zero-level highlighted')
    axis equal
    PLTNUM=PLTNUM+1;
    subplot(NPLOTSx,NPLOTSy,PLTNUM),hold on
end
numS=1;
CC=[];
for i = 1:numC
    num = pathout.C{pathcount}(2,numS);
    ids = numS+1:numS+num;
    CC = [CC,pathout.C{pathcount}(1:2,ids)];
    
    numS = 1+numS+num;
end

pathout.v{pathcount}=full(double([interp2(XU,YU,u(Nodeu(2:end-1,2:end-1)),...
    CC(1,:),CC(2,:));...
    interp2(XV,YV,v(Nodev(2:end-1,2:end-1)),...
    CC(1,:),CC(2,:))]));

c=sqrt(sum(pathout.v{pathcount}.^2));
pathout.c{pathcount}=c;
if ~isfield(app,'noplot')
    plot3k([CC;CC(1,:)*0+ T]',c,'o');
    view(3)
    hold off
    title('Zero-Level Set, colored with maximum flowfield velocity')
    
    if 0 && k>0
        PLTNUM=PLTNUM+1;
        subplot(NPLOTSx,NPLOTSy,PLTNUM),cla
        imagesc(dpdx(Nodeu(2:end-1,2:end-1)));
        PLTNUM=PLTNUM+1;
        subplot(NPLOTSx,NPLOTSy,PLTNUM),cla
        imagesc(dpdy(Nodev(2:end-1,2:end-1)));
        
        shading flat
        axis equal
    end
elseif k>0 & isfield(app,'plotNB')
    hold on
    tmp = zeros(size(XP));
    tmp(NBids) = 1;
    pcolor(XP,YP,tmp)
    hold off
elseif k>0 & isfield(app,'plotrho')
    hold on
    pcolor(XP,YP,rho(NodeP(2:end-1,2:end-1)))
    hold off  
end