% timepath.m
%  Input xp,yp,timep 
%  Generate vectors of Up,Vp at those locations
% 
%  Input variables:

modellabel = input('Label for the model plot> ','s')
timeoffset = input('What is the first hour of the data (in model time)> ')

load /home5/tom/wassawadcp/Outsidejuly/pathtxyuv.mat
if exist('coast')==0
load /home5/tom/matlab/coastdata.mat
end

% convert positions to meters

% Approx conversions for wassawmar95/meshall
lonperm = 1/60 / (1.700*36*2.54/100) ;
latperm =  1/60 / (1.700*36*2.54/100)* cos(32*pi/180) ;

%meshlon = (meshnod(:,2)-21.3601)*lonperm -80.-56/60;
%meshlat = (meshnod(:,3)-7.7332)*latperm +31+54.6/60;

xpm = (xp+80+56/60)/lonperm +21.3601;
ypm = (yp-31-54.6/60)/latperm +7.7332;
coastm=coast;
coastm(:,1) = (coast(:,1)+80+56/60)/lonperm +21.3601;
coastm(:,2) = (coast(:,2)-31-54.6/60)/latperm +7.7332;


%plot(coastm(:,1),coastm(:,2),'g',meshnod(:,2),meshnod(:,3),'b.','markersize',8)
%axis('equal')

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

drawnow

%*****************************************
tp = tp -tp(1)+ timeoffset;
%*****************************************
disp(['Re-Set time base for ADCP data tp(1)=',num2str(tp(1)),...
      '  tp(',int2str(length(tp)),')=' ,num2str(tp(length(tp)))])



if exist('UVATB')==0
disp('Run movievec first or load up UVATB.mat')
stop
end

if exist('a')==0
tic
%Create interpolation functions
[a, b, c] = elefunc(meshele,meshnod);
% Returns the element functions for meshnod and meshele
disp([' time to create elefunctions',num2str(toc)])
end

tic
% Time and array sizes for UVATB
nstep = NPRINT;
dt = DELT/3600;
firsttime = MINPR*dt;
N=length(X);
M = fix(length(UVATB)/N);
t = [0:(M-1)]*nstep*dt+firsttime;

disp([modellabel,' Model time base t(1)=',num2str(t(1)),...
    '  t(',int2str(length(t)),')=' ,num2str(t(length(t)))])

timeindex = (tp-firsttime)/(nstep*dt);
% notice timeindex goes from 0:(M-1)

U = xpm;  V = xpm;
for i=1:length(xpm)
iele = findele(a,b,c,xpm(i),ypm(i));
tf = fix(timeindex(i));
frac = timeindex(i)-tf;
tl = tf+1;
tf = min(M-1,tf); tl = min(M-1,tl);
tf = max(0,tf); tl = max(0,tl);

%  UVATB   velocities:
%  Ave 2,3    Top 4,5   Bottom 6,7
U1 = UVATB(tf*N+meshele(iele,2:4),2);
V1 = UVATB(tf*N+meshele(iele,2:4),3);
U2 = UVATB(tl*N+meshele(iele,2:4),2);
V2 = UVATB(tl*N+meshele(iele,2:4),3);
Um = (1-frac)*U1+frac*U2;
Vm = (1-frac)*V1+frac*V2;
U(i) = (a(iele,:)*xpm(i) + b(iele,:)*ypm(i) + c(iele,:))*Um(:);
V(i) = (a(iele,:)*xpm(i) + b(iele,:)*ypm(i) + c(iele,:))*Vm(:);
end

disp(['length(xp)=',int2str(length(xp)),'  toc=',num2str(toc)])




% **********************Adjustable parameters**********************
figure(1)
clf
uperx = .51444*2;  
% plot(xp,yp); axis('equal'); ax = axis;
ax = [   17   32   5 15];   % Olympic View

%  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]') ];

arrowuv2(xpm,ypm,U,V,uperx,box,ax,[0 1 0]);
putunitvec([0.0 uperx],uperx,['2knot'],'g-',30,7)

%for i = 1:length(xp)
%text(xp(i),yp(i),['-',num2str(tp(i))])
%end

title([modellabel,' tp(1)=',num2str(tp(1)),...
      '  tp(',int2str(length(tp)),')=' ,num2str(tp(length(tp)))])
hold on
%  land boundary 
%plot(Xbnd,Ybnd,'r')
Xbnd = [X(bndele(:,2)) X(bndele(:,3))]';
Ybnd = [Y(bndele(:,2)) Y(bndele(:,3))]';
xx = [ Xbnd ; NaN*ones(1,length(Xbnd))]; xx = xx(:);
yy = [ Ybnd ; NaN*ones(1,length(Ybnd))]; yy = yy(:);
plot(xx,yy,'r')
plot(coastm(:,1),coastm(:,2),'g')
hold off

 h = axes('position',[.74 .12 .15 .15]);
set(h,'box','on','yticklabels',[],'xticklabels',[])
Mone = 1;
 lunk =Mone:M;
plt = fix(timeindex);
lnod= 3496;
dep = UVATB(lnod:N:length(UVATB(:,1)),8);
dep = dep-mean(dep);
 plot((t(lunk)-t(Mone))/(t(M)-t(Mone)),dep,'b',...
      (t(plt)-t(Mone))/(t(M)-t(Mone)),dep(plt),'ro')
 grid





figure(2)
clf
arrowuv2(xpm,ypm,up/100,vp/100,uperx,box,ax,[0 1 0]);
title('ADCP Velocities')
putunitvec([0.0 uperx],uperx,['2knot'],'g-',30,7)
hold on
plot(xx,yy,'r')
plot(coastm(:,1),coastm(:,2),'g')
hold off

%figure(3)
%clf
%uv = [up' vp' up'];
%Z = rotZ(40);
%uv = uv*Z;
%arrowuv2(xpm,ypm,uv(:,1)/100,uv(:,2)/100,uperx,box,ax,[0 1 0]);
%title('ADCP Velocities Rot 40')
%putunitvec([0.0 uperx],uperx,['2knot'],'g-',30,7)
%hold on
%plot(xx,yy,'r')
%plot(coastm(:,1),coastm(:,2),'g')
%hold off
