% moviemake
% /home5/tom/mesh/meshtools/moviemake.m
% Generalized movievec.m
disp('/home5/tom/mesh/meshtools/moviemake.m')

% Load up the UVATB if it isn't already
if exist('UVATB')==0
clear
disp(['loading UVATB.vCd up'])
 load  UVATB.vCd
% load  UVATB.dat
disp(['loading setup info (setups.m)'])
setups
Bullriver = Ariver(2);
Willriver = Ariver(1);
filename= 'mesh';
disp(['loading ',filename,' X,Y up'])
[ meshnod,meshele,bathy,bndele,bcs]=filein(filename);

X = meshnod(:,2);
Y = meshnod(:,3);
l = find(bndele(:,5)==1);
Xbnd = [X(bndele(l,2)) X(bndele(l,3))]';
Ybnd = [Y(bndele(l,2)) Y(bndele(l,3))]';
%Ybnd = [Y(bndele(l,2)); Y(bndele(1,2))];
save UVATB.mat

end

% Input parameters

windangle = 0;

% Crazy directory string stuff to determine windangle

d = pwd
l = max(findstr(d,'/'))+1;
if ~isletter(d(l)) 
%  directory name starts with a number, assume it is wind angle
m=l:length(d); i=1;
while ~isletter(d(m(i)))
i = i+1;
end
i = i-1;
m = m(1:i);
windangle = str2num(d(m))
end



%  interesting nodes which get UVH plots
lnod=[ 32  340 1987  3924  4462 4814 2439 4517  2727 2648 2799 5000];

uperx = .51444*2;    %  Two knots per kilometer
 
gridthin = 1;   %  Plots only every gridthin node point

ax = [   17   32   5 15];   % Olympic View

%ax = [   15   35   5 25];   % sound, Sav rivers and near shore
%ax = [   15   35   5 20];   % sound, Sav rivers and near shore
%ax = [   17   32   5 15];   % Olympic View
%ax = [   15   35   0 22];   % sound, rivers and near shore
%ax = [   14   26   5 15];   % Sound mainly
%ax = [   14   24   6 14];   % Close up of Sound 
%ax = [   15   35   5 25];   % sound, Sav rivers and near shore
%ax = [   28   33  18 23];   %  Sav rivers

% time steps to print out
printides = [1:2:60] ;
%printides = [] ;




%  This is the box drawn at the base of the velocity vectors.
box=0.001* 10*[0 0; NaN NaN; cos([0:2*pi/6:2*pi]') sin([0:2*pi/6:2*pi]') ];

nstep = NPRINT;
dt = DELT/3600;
firsttime = MINPR*dt;

disp(['start the program  makeup the speeds etc.'])

N=length(X);

M = fix(length(UVATB)/N)
t = [1:M]*nstep*dt+firsttime;

dep = ones(N,M);
dep(:) = UVATB(1:N*M,8);
dep = dep';

uall = ones(N,M);
uall(:) = UVATB(1:N*M,2);
uall = uall';

vall = ones(N,M);
vall(:) = UVATB(1:N*M,3);
vall = vall';
speed = sqrt(vall.^2 + uall.^2);
mdep = mean(dep);
mn = mean(dep(:,lnod(1:4))) ;
ln = length(t);

disp(['part three Movie time'])
figure(1)
clf

onetide = min( fix(12.425/(NPRINT*dt)) , M)
Mone= M-onetide+1;
if ~exist('Mov') ; 
    disp(['Make space for movie']);
    Mov = moviein(onetide,gcf); 
end

disp(['Movie loop'])
for plt = Mone:1:M
clf
lm = [1:gridthin:N] + (plt-1)*N;
lxy = 1:gridthin:length(X);

arrowuv2(X(lxy),Y(lxy),UVATB(lm,4),UVATB(lm,5),uperx,box,ax,[0 1 0]);
kids = get(gca,'children');
set(kids,'zdata',-ones(1,length(get(kids,'xdata'))));
xlabel('Kilometers, Top Velocity')

% UVATB(lm,2),UVATB(lm,3)  Average Velocity 
% UVATB(lm,4),UVATB(lm,5)  Surface Velocity 
% UVATB(lm,6),UVATB(lm,7)  Bottom Velocity 

title(['Wassaw Sound (',int2str(windangle),' deg 0.1 Pascal Wind) ', int2str(plt)...
,'; ',num2str(plt*nstep*dt+firsttime),' hr'])

putunitvec(uperx*[cos(pi/2-windangle*pi/180) sin(pi/2-windangle*pi/180)],...
      uperx,[num2str(windangle),' Wind'],'y-',18.5,15)
putunitvec([0.0 uperx],uperx,[num2str(uperx/.5144),' Knots'],'g-',18,7)
text(17.5,6.5,[num2str(plt*nstep*dt+firsttime),' hr'])
%text(15,19,[num2str(Willriver)])
%text(22,17,[num2str(Bullriver)])
%text(30,22.5,[num2str(Ariver(5))],'rotation',90)
axis('equal')
hold on
%  land boundary 
%plot(Xbnd,Ybnd,'r')
xx = [ Xbnd ; NaN*ones(1,length(Xbnd))]; xx = xx(:);
yy = [ Ybnd ; NaN*ones(1,length(Ybnd))]; yy = yy(:);
plot(xx,yy,'r')

% Plot tidal roses at first 4 nodes in lnod
for inod = 1:6
lall = lnod(inod):N:(onetide*N);
Xl = X(lnod(inod))*ones(length(lall),1);
Yl = Y(lnod(inod))*ones(length(lall),1);
%tidalrose(Xl,Yl,UVATB(lall,4),UVATB(lall,5),uperx,ax,'-m');
%text(X(lnod(inod)),Y(lnod(inod)),['  ',int2str(lnod(inod))],'color',[1 .7 .7])
end

hold off

orientslide
signature('Dr.Thomas F. Gross','Skidaway Inst. Oceanography') ;

% plot of tidal height
% h = axes('position',[.15 .75 .15 .15]);   %  in upper left corner
% h = axes('position',[.15 .15 .15 .15]);   %  in lower left corner
  h = axes('position',[.45 .75 .15 .15]);   % Upper Center
 set(h,'box','on','yticklabels',[],'xticklabels',[])
 lunk = Mone:1:M;
 plot((t(lunk)-t(Mone))/(t(M)-t(Mone)),dep(lunk,lnod(1))-mn(1),'b',...
      (t(plt)-t(Mone))/(t(M)-t(Mone)),dep(plt,lnod(1))-mn(1),'ro')
 grid
 set(h,'box','on','yticklabels',[],'xticklabels',[])

drawnow
plt
if plt>= Mone
  fr = plt-Mone+1;
  Mov(:,fr) = getframe(gcf);



%                orient landscape
%                un = get(gcf,'PaperUnits');
%                set(gcf,'PaperUnits','inches');
%                set(gcf,'Papertype', 'a5'); 
%                % size=   8.2639   5.8472   
%                set(gcf,'PaperPosition',[ .63 .44  7 5 ]);
%                set(gcf,'PaperUnits',un);
%                set(gcf,'InvertHardcopy','off')
%       disp(['print figgif',int2str(1000+plt),'.gif -dgif8'])
%       eval(['print figgif',int2str(1000+plt),'.gif -dgif8'])

end

if ~isempty(find(plt==printides))
  labriver= ['w',num2str(Willriver),'b',num2str(Bullriver)];
  labriver=strrep(labriver,'+0','');
  disp(['Printing  fig1',labriver,'plt',int2str(plt),'.ps'])
  eval(['print fig1',labriver,'plt',int2str(plt),'.ps -dps'])
end  

end

fps = onetide/5;   % frames per second  for one cycle in 5 sec
disp('For movie type:')
disp('axis off;movie(gcf,Mov,100,fps)')


% paused('  Pause, Continue for overhead conversions of fig 1, 3')
% overheadset([.3 .3 .9],[.5 .5 .9])
% overhead([.3 .3 .9],[1 1 .999])
