
%
% Plots the surface for allu 
%
% If text='no' or text='NO' no JPG files are produced
% Usage:
%             wave_plotsurf(allt,allx,allu,L,t0,t1,'no')
%
% Ex:         wave_plotsurf(allt,allx,allu,L,1,1001,'no')
%
% Ricardo Carretero (20/3/2001)
%

function [] = wave_plotsurf(allt,allx,allu,L,t0,t1,text)

global DD
format compact

fig1=1;
fig2=0;
fig3=1;
fig4=1;
jpg=1;

moreplots=0*4+4;

if(length(text)==2)
 if(text=='no'|text=='NO')
  jpg=0;
 end
end

if(L==0)
 L=allx(1);
end

[np,nl]=size(text);
for i=1:np
 for j=1:nl
  if(text(i,j)=='_')
   text(i,j)='-';
  end
 end
end
  
if(jpg)
  file1 = [text '_u.jpg']
  file2 = [text '_Du.jpg']
  file3 = [text '_uini_fin.jpg']
  file4 = [text '_shade.jpg']
else
  fprintf('NOT saving JPG\n');
end

if(t1==0)
 t0=1;
 t1=length(allt);
end

Dt=1
if(floor((t1-t0)/Dt)~=(t1-t0)/Dt)
 error('interval is not n*DT');
end

if(t1+Dt<length(allt))
 extra=1;
else
 extra=0;
end

dt = allt(2)-allt(1);
myu=allu(:,t0:t1+extra*Dt);
myt=allt(  t0:t1+extra*Dt);

Mu=max(max(myu));
mu=min(min(myu));

Lm=min(min(allx));
LM=max(max(allx));

if(Mu==mu) 
 if(Mu==0)
  Mu=0.1;
 end
 mu=mu-Mu*0.1;
 Mu=Mu+Mu*0.1;
end

bounds = [allt(t0) allt(t1) Lm LM mu Mu];

%boundsextra = [allt(t0) allt(t1-1+extra) -Lm LM min(min(allu(:,t0:t1+extra))) max(max(allu(:,t0:t1+extra)))];
mytitle=['L=[' num2str(Lm) ':' num2str(LM) ']'];

DD = diff(allu(:,t0:Dt:t1+extra*Dt)')/dt;
MabsDD = max(max(abs(DD)));
MDD = max(max(DD));
mDD = min(min(DD));
mp = (MDD-mDD)/2;
%% the zero is centered at the boundary between white and first red
DD = (DD>0).*DD*(mp/MDD)-(DD<0).*DD*mp/mDD;

%% the white is centered around zero
%DD = (DD>0).*DD*(32/31)*(mp/MDD)-(DD<0).*DD*(mp/mDD);
%% the white is centered around zero and the small fluctuations are hidden
%DD = DD-(abs(DD)<mp*(2/32)).*abs(DD).^1.1.*sign(DD);

xx=1:32;
blue_red=ones(64,3)*1; 

% this gives two white zones around zero
blue_red(xx,1)    =   xx'/31-1/31;
blue_red(xx,2)    =   xx'/31-1/31;
blue_red(xx+32,2) =1-(xx'/31-1/31);
blue_red(xx+32,3) =1-(xx'/31-1/31);

%% this gives NO white zone
%blue_red(xx,1) =xx'/32-1/32;
%blue_red(xx,2) =xx'/32-1/32;
%blue_red(xx+32,2) =1-xx'/32;
%blue_red(xx+32,3) =1-xx'/32;

%% this gives ONE white zone BELOW zero
%blue_red(xx,1) =xx'/31-1/31;
%blue_red(xx,2) =xx'/31-1/31;
%blue_red(xx+32,2) =1-xx'/32;
%blue_red(xx+32,3) =1-xx'/32;

if fig1==1
 figure(1+moreplots)
 clf;
fs=13;
% axes('position', [0.1 0.2 0.8 0.6])
 axes('position', [0.1 0.15 0.8 0.7])
 surf(myt,allx,myu);shading interp;colormap('default'); 
 view([90,90]); axis(bounds);
 set(gca,'FontSize',[fs]);
 xl=xlabel('t');
 yl=ylabel('x');
 set(xl,'Rotation',[1],'FontSize',[fs]);
 set(yl,'Rotation',[1],'FontSize',[fs]);
%% colorbar;
 title(['u(x,t)']);
 zl=zlim;zlim([zl(1)-10*(zl(2)-zl(1)) zl(2)+10*(zl(2)-zl(1))]);
 if(jpg)
  print('-djpeg',file1)
 end
end

if fig2==1
 figure(2+moreplots)
 clf;
 axes('position', [0.1 0.15 0.8 0.7])
 span=t0:Dt:t1-1+extra*Dt;
 surf(allt(span),allx,DD');
 shading interp;colormap(blue_red); 
 axis tight;
 ax = axis;
 boundsDD = [allt(t0) allt(t1) Lm LM ax(5) ax(6)];
 view([90,90]); axis(boundsDD);
 xl=xlabel('t');
 yl=ylabel('x');
 set(gca,'FontSize',[fs]);
 set(xl,'Rotation',[1],'FontSize',[fs]);
 set(yl,'FontSize',[fs]);
 set(gca,'LineWidth',1.5);
% set(gca,'Ytick',[-50:10:50],'FontSize',[8]);
 colorbar;
 title(['u_t(x,t)']);
 pause(0.01)
 
 if(jpg)
%  orient tall
  print('-djpeg',file2)
 end

end

if fig3==1
 figure(3+moreplots)
 clf;
 axes('position', [0.1 0.15 0.8 0.7])
 mm = mu-(Mu-mu)*0.15;
 MM = Mu+(Mu-mu)*0.15;
 Nplots = 6;			% Number of intermidiate equispaced solutions to show
 for np=1:Nplots+1
  subplot(Nplots+1,1,np)
  ii=1+(np-1)*(t1-t0+extra*Dt)/Nplots;
  ii=fix(ii);
  plot(allx,myu(:,ii),'k','LineWidth',2)
  yl = ['u(x,' num2str(allt(ii)) ')'];
  ylabel(yl);
  axis([Lm LM mm MM])
 end
 xlabel 'x'; 
 if(jpg)
  print('-djpeg',file3)
 end
end

if fig4==1
 figure(4+moreplots)
 clf;
 fs=13;
 axes('position', [0.1 0.15 0.8 0.7])
 set(gca,'FontSize',[fs]);
 surfl(allx,myt,myu',[25 30])
 shading interp;
 colormap(gray);
 hold on
 span=t0:Dt:t1-1+extra*Dt;
 %for the cat map:
 %surf(allx,allt(span),(mu-(Mu-mu))*ones(length(allt(span)),length(allx)),1-abs(DD)/MabsDD);
 %for the density:
 surf(allx,myt,(mu-(Mu-mu))*ones(length(myt),length(allx)),myu')
 shading interp;
 view([15 30]);
 %axis([Lm LM allt(t0) allt(t1+extra*Dt) (mu-(Mu-mu)) ceil(Mu)])
 %axis([Lm LM allt(t0) allt(t1+extra*Dt) (mu-(Mu-mu)) (Mu)])
 axis tight
 Time=max(myt);
 xl=xlabel('x');
 yl=ylabel('t');
 zl=zlabel('u(x,t)');
 set(xl,'Rotation',[1],'FontSize',[20],'position',[(Lm+LM)/2 -Time/10 (mu-(Mu-mu))]);
 set(yl,'Rotation',[1],'FontSize',[20],'position',[LM+LM/10 Time/4 (mu-(Mu-mu))]);
 set(zl,'Rotation',[0],'FontSize',[20],'position',[Lm-Lm/5 Time/2 ceil(Mu)]);
 set(zl,'Rotation',[0],'FontSize',[20],'position',[Lm-Lm/5 Time/2 (Mu)]);
% set(gca,'Xtick',[Lm Lm/2 0 LM/2 LM],'FontSize',[fs]);
 set(gca,'Ytick',[0 Time/2 Time],'FontSize',[fs]);
 set(gca,'Ztick',[mu mu+(Mu-mu)/4 mu+(Mu-mu)/2 mu+0.75*(Mu-mu) Mu],'FontSize',[fs]);
 set(gca,'FontSize',[fs]);
 if(jpg)
  print('-djpeg',file4)
%  the following is GOOD !
% print('-djpeg','-r120','mio.jpg')
 end
end

