clear all

missions = ['2000.056.04/'; '2000.056.05/'; '2000.056.06/'; '2000.056.07/'; '2000.056.09/'];

[N x] = size(missions);

for M = 1:N;
 eval(['load ' missions(M,:) 'auv']);
 if M==1;
  o2=O2; s=S; t=T; ph=pH; Depth=depth; time=epochsec; 
  % Roll=roll; Pitch=pitch; Yaw=yaw;
  lat=startlat; lon=startlon; 
 else
  o2=[o2;O2]; s=[s;S]; t=[t;T]; ph=[ph;pH]; Depth=[Depth;depth]; 
  time=[time;epochsec];
  % Roll=[Roll;roll]; Pitch=[Pitch;pitch]; Yaw=[Yaw;yaw];
  lat=[lat;startlat]; lon=[lon;startlon]; 
 end
idx(M) = length(T);	% idx contains endpoint of segment
end

%% Calculate interpolated lat and lon
%% Add last lat/lon based on next to last 
npt = length(lat); 
 alat = lat(npt) + (lat(npt) - lat(npt-1)); 
 alon = lon(npt) + (lon(npt) - lon(npt-1)); 
 lat=[lat;alat]; lon=[lon;alon];

%% Interpolate lat/lon
for M = 1:N;
 ilat = [linspace(lat(M),lat(M+1),idx(M))]';
 ilon = [linspace(lon(M),lon(M+1),idx(M))]';
 if M==1;
  ILAT = ilat; ILON=ilon;
 else
  ILAT = [ILAT;ilat]; ILON=[ILON;ilon];
 end
end

 %% Estimate transect distance
 addpath '/u/ryjo/matlab/CSIRO'
 [dist phas]=sw_dist(ILON([1 length(ILAT)]),ILON([1 length(ILON)]),'km');
 % dist = dist*1000;    % m
 tdist = linspace(0,dist,length(ILON));
 idist = linspace(0,dist,1000);
 iZ = [10:1:max(Depth)]';
 AX = [0 dist min(iZ) max(iZ)];


P1=[.25 .48 .5 .4];
P2=[.25 .17 .5 .25];
XT = [-122:.1:-121.8]; YT = [36.6:.1:37];
% YTL=['36.50' setstr(176) 'N';'36.75' setstr(176);'37.00' setstr 'N'];
XTL = ['122.0';'121.9';'121.8'];
figure(1); clf; 
% set(gcf,'units','pixels','position',[20 50 350 400],'color',[1 1 1],'menubar','none');
paper
axes('position',P1);
mapaxis =[-122.08 -121.78 36.57 37]; 
[fillLat,fillLon] = get_landfill(mapaxis);
fill(fillLon,fillLat,[.7 .7 .7]); axis(mapaxis);
hold on; 
plot_iso([80 200 500 800],mapaxis,'k');
set(gca,'tickdir','out','fontsize',12,'fontweight','bold');
llgrid(10);

%% Location of local isoline structure near 7 km along transect (avgUD);
p=plot(-121.9448,36.7918,'ko'); 
% set(p,'markersize',10,'markerfacecolor',[.8 .8 .8],'markeredgecolor',[.7 .7 .7]);
set(p,'markersize',10,'markerfacecolor','b','markeredgecolor','b');
% p=plot(-121.9652,36.7783,'ko'); set(p,'markersize',10,'markerfacecolor',[.8 .8 .8],'markeredgecolor',[.7 .7 .7]);
p=plot(ILON,ILAT,'k--'); set(p,'linewidth',2);
idx = [1:length(lon)-1];
p=plot(lon(idx),lat(idx),'kv'); set(p,'markerfacecolor','k','markersize',7);
p=plot(alon,alat,'k^'); set(p,'markerfacecolor','k','markersize',7);
% title('Mission starting points');

%% M1
p=plot(-122.03,36.75,'ko'); set(p,'markersize',7,'markerfacecolor','k');
tx=text(-121.97,36.9,'Monterey Bay'); 
set(tx,'fontweight','bold','fontsize',15,'color','k','fontangle','italic');
tx=text(-122.03,36.73,'M1'); 
set(tx,'fontweight','bold','fontsize',15,'color','k');
tx=text(-122,36.67,'Monterey Canyon'); 
set(tx,'fontweight','bold','fontsize',15,'color','k','fontangle','italic','fontname','times');

[x,y]=ginput(2);
plot(x,y,'k');

% title('AUV transect, 24 February 2000')

axes('position',P2);
plot(ILON,Depth,'k'); hold on;
g=lon(1:length(lon)-1); 
p=plot(g,repmat(1,size(g)),'kv'); 
set(p,'markerfacecolor','k','markersize',7);
p=plot(lon(length(lon)),1,'k^'); 
set(p,'markerfacecolor','k','markersize',7);
set(gca,'ydir','reverse');
set(gca,'Xlim',[min(ILON) max(ILON)]);
set(gca,'fontsize',12,'XTick',[],'Tickdir','out');
set(gca,'fontsize',12);
ylabel('Depth (m)');
axes('position',P2);
set(gca,'Xlim',[0 dist],'Xdir','reverse','tickdir','out');
set(gca,'fontsize',12,'color','none','YTick',[]);
xlabel('Distance (km)');

frame
t=text(P1(1)+.95*P1(3),P1(2)+.01,'a');
set(t,'fontsize',14,'fontweight','bold');
t=text(P1(1)+.95*P1(3),P2(2)+.01,'b');
set(t,'fontsize',14,'fontweight','bold');

return
figure(2); clf;
plot3(ILAT,ILON,Depth,'r.');
set(gca,'ydir','reverse','zdir','reverse');
grid;

odat = [time ILAT ILON Depth t s o2 ph Pitch Roll Yaw];
fid = fopen('auv_dive.asc','w');
fprintf(fid,'%15.3f\t%10.6f\t%10.6f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\n',odat');
fclose(fid);


return
XL = [min(t) max(t)];
YL = [33.2 34.2]; idx = find(s > YL(2) | s < YL(1)); s(idx)=NaN*idx;
phL = [7.51 8.05]; idx = find(ph < 7.51); ph(idx)=NaN*idx;
o2L = [.7 4.9]; idx = find(o2 > o2L(2) | o2 < o2L(1)); o2(idx)=NaN*idx;

figure(2); clf; 
subplot(211); plot3(t,s,ph,'.');
 set(gca,'xlim',XL,'ylim',YL,'zlim',phL);
 grid on 
subplot(212); plot3(t,s,o2,'.');
 set(gca,'xlim',XL,'ylim',YL,'zlim',o2L);
 grid on 

cmp = jet(256); phscale = linspace(phL(1), phL(2), length(cmp));

figure(3); thx_fig
for n=1:length(ph);  
 [mn a]=min(abs(ph(n)-phscale)); 
 p=plot3(t(n),s(n),o2(n),'.');  set(p,'color',cmp(a,:));
 hold on
end

set(gca,'xlim',XL,'ylim',YL,'zlim',o2L);
set(gca,'cameraposition',[-30 30 10]);
set(gca,'fontweight','bold','fontsize',14,'box','on')
xlab=(['Temperature (' setstr(176) 'C)']);
ylabel=(['Salinity (psu)']);
zlabel(['Oxygen (ml l^{-1})']);
grid

axes('position',[.92 .23 .02 .62]);
pcolor([0 1],phscale',[phscale' phscale']); shading interp; caxis(phL);
set(gca,'YaxisLocation','right','Ytick',[7.6:.1:8],'fontweight','bold','fontsize',14);
set(gca,'XTick',[]);
t=text(1,phL(2)+.02,'pH'); 
set(t,'fontweight','bold','fontsize',14,'horizontalalignment','center');

frame
t=text(.72,.05,xlab); set(t,'fontweight','bold','fontsize',14,'rotation',26);
