%mov = avifile('example2.avi')
%close(F1);
ix=6;  % this is the x axis variable
iy=11;  % this is the y axis variable
%  1 is ncount
%  2 is j+1
%  3 is jj
%  6 is time
%  7 is r(j)
% 10 is v(n,j+1), relative volume [cc/g]
% 11 is U(n,j), particle velocity [cm/us]
% 12 is P(n,j+1) [Mbar] but it might be converted to [GPa]
% 13 is rho(j+1)
% 14 is E(n,j+1) Mbar/cc, this is in the code as e(n+2,j+1)
% 15 is q(n,j+1) artifical viscosity [Mbar]
% 16 s1
% 17 s2
% 18 k(n,j+1)
% 19 diffE
% 20 is Temperature [K]
% 21 is Stress = s1-P [Mbar]
% 22 is Stress = s1-P [Mbar]
% 23 is epsi1
% 24 is equivalent plastic strain
% 25 x-component plastic strain: x = y + z
% 26 y-component plastic strain: y
% 27 z-component plastic strain: z
% 28 total elastic strain future time
% 29 total elastic strain current time
% 30 Y
% 31 mu
tracer = 306+960; % tracer is ignored if iplot=1
%tracer = 306+960/2; % tracer is ignored if iplot=1
              
iplot = 2; %(1) for movie and (2) for xy-plot
%interface=[0 594 (594+1710)] %list j value (node value) of initial interface locations for use with movie

%%%%%%%%%%%%%%%%%%%%%%%%  User do not change these variables %%%%%%%%%%%%%%%%%%%%%%%%%
jj = ko(1,3);
if exist('interface','var')
 interface=interface/2;
end
itracer=tracer/2+1; %1700 2200 half the number of nodes jj/2+1
itotal=jj/2-1; %number of lagraian points (i.e. set this equal to parameter jj/2-1)
n=100000; %  length(ko)/(jj-4/2);  % this is the number of frames to plot nn/iskip
k=0; % initalize the dummy index variable
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if     iy==11
    convert=10000.; %this is to put velocity into m/s
    convert=10.; %this is to put velocity into km/s
    ystr='Particle Velocity [km/s]';
elseif iy==12 
%     convert=1;     % this leaves the pressure in Mbar
    convert=100.;  %this converts the pressure from Mbar to Gpa 1Mbar = 100GPa
    ystr='Pressure [GPa]'
%   convert=1000; %this converts the pressure from Mbar to kbar
elseif iy==21
    convert=-100.;  %this converts the longitudinal stress from Mbar to Gpa
    ystr ='Longitudinal Stress [GPa]'
%     convert=1;
elseif iy==22
    convert=-100.;  %this converts the laterial stress from Mbar to Gpa
    ystr='Laterial Stress [GPa]'
%     convert=1;
else
    convert=1.;
end
%if ix==10
%    v0=0.4545;
%else
%    v0=1.0;
%end 
%close(3)
for i = 1:n
    i;
    k=k+1;
    
if iplot == 1 % movie    
   %this is for all the tracers points
    figure(3),F1=plot(ko((i-1)*itotal+1:i*itotal,ix),ko((i-1)*itotal+1:i*itotal,iy)*convert,'.') %,'MarkerSize',20);
    if exist('interface','var')
        xline(ko((i-1)*itotal+interface(:)+1,7))
    end
%    if ko((i-1)*itotal+1,6) > 1.1253952450889388 
%        newinterface=[1872/2 1874/2];
%            xline(ko((i-1)*itotal+newinterface(:)+1,7))
%    end
    %plot(ko((i-1)*itotal+1:i*itotal,ix),ko((i-1)*itotal+1:i*itotal,iy),'.')
%   F = getframe;
 %  mov=addframe(mov,F)
%axis([-0.2 1.0 -18 23])  % pressure limits
%axis([-0.4 1.10 -13 23])  % pressure limits
xlabel('Position (cm)')
ylabel(ystr)
set(gca,'XMinorTick','on','YMinorTick','on')
set(gca,'FontSize',18)
%annotation('textbox', [0.35, 0.9, 0.1, 0.1], 'EdgeColor','none','FontSize',18,'String', "Time =" + ko((i-1)*itotal+1,6)+"\mus")
% txt = sprintf('Time=%2.2f %c s',i*0.01,956)';
% text(-0.3,-0.1,txt','FontSize',18)
 
%pause(0.5)
%    if i==1 
%       hold
%    end
%if i == 1 % only open movie file once
%set(gca,'nextplot','replacechildren'); 
%v = VideoWriter('peaks.avi');
%open(v);
%end  %for open movie file
%set(gca,'nextplot','replacechildren'); 
%frame = getframe(gcf);
%   writeVideo(v,frame);
saveas(gcf,sprintf('%s_%03d.jpg','filename',i)) 
clf
elseif iplot == 2 % xyplot

   %this is for single tracer point
   %plot(kool((i-1)*itotal+itracer:(i-1)*itotal+itracer,ix),kool((i-1)*itotal+itracer:(i-1)*itotal+itracer,iy),'.')
   if iy==1
    % figure(1),plot(ko((i-1)*itotal+itracer:(i-1)*itotal+itracer,ix),convert*sqrt(ko((i-1)*itotal+itracer:(i-1)*itotal+itracer,12)/ko((i-1)*itotal+itracer:(i-1)*itotal+itracer,13)),'.')
   else
    figure(2),F1=plot(ko((i-1)*itotal+itracer:(i-1)*itotal+itracer,ix),convert*ko((i-1)*itotal+itracer:(i-1)*itotal+itracer,iy),'.');
   end
    %dat k,(i-1)*itotal+itracer
     data(k,1)=ko((i-1)*itotal+itracer:(i-1)*itotal+itracer,ix);
     data(k,2)=ko((i-1)*itotal+itracer:(i-1)*itotal+itracer,iy)*convert;
   if i==1 
     hold
   end
else
    ans='iplot should be 1 or 2'
%    pause
end    
%   AXIS MANUAL    
%AXIS([-15. 125 -.1 .4])  % velocity limits
%AXIS([-1.0 1. -25. 25.])  % volume limits
%AXIS([-1.5 25])  % volume limits
%AXIS([-150 50 0 0.1])  % volume limits
%AXIS([-0 1e-4 1.15e-3 2.15e-3])  % volume limits
%pause(1.00)
xlabel('Time [\mus]')
%ylabel('Particle Velocity (km/s)')
ylabel(ystr)
set(gca,'XMinorTick','on','YMinorTick','on')
set(gca,'FontSize',18)
%saveas(gcf,sprintf('NoSpall.jpg','filename')) 
end
%mov=close(mov)
