% Plots for double Gyre
%Save initially everything for plotting later

d0=fileparts([pwd,filesep]);
            d0=d0(1:strfind(d0,'trunk')+4);
if k==0
    if isfield(app,'savemat')
        if isempty(app.savemat)
            d0=fileparts([pwd,filesep]);
            d0=d0(1:strfind(d0,'trunk')+4);
            save(sprintf('%s/Save/mat/param.mat',d0));
            fprintf('Saving %s mat-file in %s/Save/mat/parameters.mat',...
                SetupScript,d0);
        else
            if k < 10
                if ~exist(sprintf('%s/Save/%s/',d0,app.savemat),'dir')
                    mkdir(sprintf('%s/Save/%s/',d0,app.savemat))
                end
            end
            d0=fileparts([pwd,filesep]);
            d0=d0(1:strfind(d0,'trunk')+4);
            save(sprintf('%s/Save/%s/param.mat',d0,app.savemat));
            fprintf('Saving %s mat-file in %s/Save/%s/parameters.mat',...
                SetupScript,d0,app.savemat);
        end
    end
    colormap(mk_colmap)
end

clf
subplot(221)
pcolor(XP,YP,P(NodeP(2:app.Ny+1,2:app.Nx+1))) 
xlim([min(xp) max(xp)]);ylim([min(yp) max(yp)]);
% TT(1)=xlabel('x(m)');TT(2)=ylabel('y(m)');TT(3)=zlabel('Pressure');
TT(4)=title(sprintf('Pressure app.Nx=%g, app.Ny=%g',app.Nx,app.Ny));
% formatplot([],gca,TT);view(2),colorbar,
shading interp,
axis off

subplot(222)
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)));
quiver(XP(:),YP(:),(tmp1(:)),(tmp2(:)),'g') 
hold on
streamslice(XP,YP,tmp1,tmp2)
hold off 
ylim([0 1]),xlim([0 1])
% TT(1)=xlabel('x(m)');TT(2)=ylabel('y(m)');TT(3)=zlabel('Velocity');
TT(4)=title(sprintf('Velocity, time =%03.03g',T));
% formatplot([],gca,TT);view(2)
axis off

subplot(2,2,3)
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;
pcolor(XU(2:end,:),YV(:,2:end),dvdx-dudy),shading interp
ylim([0 1]),xlim([0 1])
% TT(1)=xlabel('x(m)');TT(2)=ylabel('y(m)');TT(3)=zlabel('Vorticity');
TT(4)=title(sprintf('Vorticity'));
% formatplot([],gca,TT);view(2)
axis off

subplot(2,2,4)
pcolor(XP,YP,sqrt(tmp1.^2+tmp2.^2)), shading interp
ylim([0 1]),xlim([0 1])
% TT(1)=xlabel('x(m)');TT(2)=ylabel('y(m)');TT(3)=zlabel('Velocity');
TT(4)=title(sprintf('Velocity Magnitude'));
% formatplot([],gca,TT);view(2)
axis off

%Save image
if isfield(app,'saveimg')
    d0=fileparts([pwd,filesep]);
    d0=d0(1:strfind(d0,'trunk')+4);
    print('-djpeg85','-r200',...
        sprintf('%s/Save/img/%05d.jpg',d0,k/app.PlotIntrvl));
    fprintf('Saving %s image in %s/Save/img/%05d.jpg\n',...
             SetupScript,d0,k/app.PlotIntrvl);
end

%Save only essentials for plotting later
if isfield(app,'savemat')
    if isempty(app.savemat)
        d0=fileparts([pwd,filesep]);
        d0=d0(1:strfind(d0,'trunk')+4);
        save(sprintf('%s/Save/mat/%05d.mat',d0,floor(k/app.PlotIntrvl)),'P','u','v','T','rho');
        fprintf('Saving %s mat-file in %s/Save/mat/%05d.mat\n',...
             SetupScript,d0,floor(k/app.PlotIntrvl));
    else
        if k < 10
            if ~exist(sprintf('%s/Save/%s/',d0,app.savemat),'dir')
                mkdir(sprintf('%s/Save/%s/',d0,app.savemat))
            end
        end
        d0=fileparts([pwd,filesep]);
        d0=d0(1:strfind(d0,'trunk')+4);
        save(sprintf('%s/Save/%s/%05d.mat',d0,app.savemat,floor(k/app.PlotIntrvl)),'P','u','v','T','rho');
        fprintf('Saving %s mat-file in %s/Save/%s/%05d.mat\n',...
             SetupScript,d0,app.savemat,floor(k/app.PlotIntrvl));
    end
end
