% Script to plot u, v, rho.
% 
% This script assumes that all relevant variables are already present in the workspace.

if ( computeerrs )
   if (~redoPlots)
      fid = fopen (fullfile(d0,app.saveimg,['errors',suffix_string,'.dat']),'a');
   else
      fid = fopen (fullfile(d0,app.saveimg,['errors',suffix_string,'.dat']),'w');
   end;
end;

for i = 0 : round(Nt/app.PlotIntrvl)
%for i = 10:3:20

   dataname  = sprintf('%s/%s/%03d%s', d0, app.savemat, i, suffix_string);
   imagename = sprintf('%s/%s/mean%03d%s.png', d0, app.saveimg, i, suffix_string);
    
   if ( ((exist(dataname,'file')==2) | (exist([dataname,'.mat'],'file')==2)) & ...
        ( (exist(imagename,'file')~=2) | redoPlots) )
    
      if ( computeerrs )
         load (fullfile(truthdir,[num2str(i+truth_offset,'%3.3d'),'.mat']));
         utruth = u;
         vtruth = v;
         rhotruth = rho;
      end;

      load (dataname);

      % Extract variance of u, v, rho.
      % Plot Everything.

      figure;
      clf;
      subplot(311)
      box on;
      pcolor(XU*h/1e3,YU*h/1e3,u(Nodeu(2:app.Ny+1, 2:app.Nx))*(nu_x/h));
      shading interp;
      colorbar
      title('U','FontSize',14)

      subplot(312)
      pcolor(XV*h/1e3,YV*h/1e3,v(Nodev(2:app.Ny, 2:app.Nx+1))*(nu_x/h));
      shading interp;
      box on;
      colorbar
      title('V','FontSize',14)

      subplot(313)
      pcolor(XP*h/1e3,YP*h/1e3,rho(NodeRho(2:app.Ny+1, 2:app.Nx+1))*DelS);
      shading interp;
      box on;
      colorbar
      title('S','FontSize',14)

      print('-dpng',imagename)
    
      if ( computeerrs )
         du = (utruth - u)*(nu_x/h);
         dv = (vtruth - v)*(nu_x/h);
         drho = (rhotruth - rho)*DelS;
         fprintf (fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f\n', ...
                 [(i+truth_offset)*app.PlotIntrvl.*app.dt*(h^2/nu_x) ...
                   sqrt(nanmean(du.*du)) nanmean(abs(du)) fliplr(extrem(abs(du))) ...
                   sqrt(nanmean(dv.*dv)) nanmean(abs(dv)) fliplr(extrem(abs(dv))) ...
                   sqrt(nanmean(drho.*drho)) nanmean(abs(drho)) fliplr(extrem(abs(drho)))]);
      end;
   end;
end;

if ( computeerrs )
   fclose(fid);
end;

