% clear all; 
procit=0; filtit=0;
drifter0=0; drifter1=1; drifter2=0;

%% First Drifter Survey
% titl=['MBARI AUV Drifter Pass, 27 August 2000 (day 240)'];
%% The first W to E transect crossed the drifter path at its eastern end
%%  Near 7.5 km along transect (of 9 km)
%%  Path crossing at 36.93 N.
%%  Drifter crossed this latitude at day 240.8
%%  AUV crossed this latitude at epochsec 9.6740133e+008

%% Drifter survey #1 & 2, day 242
titl=['MBARI AUV Drifter Track Crossing, 29 August 2000'];

%% M1 to C1
% titl=['MBARI AUV M1 to C1 Survey, 30 August 2000'];

XLAB='Distance (km)';

if procit
 cd g:\auv\day\D1\; load drifter1; 
 
 %% Estimate transect distance
 %% Drifter 1 and 2
 [dist phas]=sw_dist(ILAT([1 length(ILAT)]),ILON([1 length(ILON)]),'km');
 tdist = [linspace(0,dist,length(f))]';
 
 %% M1 to C1, and Drifter 0 (need to update Drifter 1 and 2 to this
 % tdist=IDIST; dist=max(IDIST); 
 
 idist = linspace(0,dist,200);
 iZ = [4:1:max(Depth)]';
 AX = [0 dist min(iZ) max(iZ)]; 

 disp('Using griddata (Delaunay)...'); meth='linear';
  
 % idx=find(finite(t) & upcast);
 idx=find(finite(t));
 gT = griddata(tdist(idx),Depth(idx),t(idx),idist,iZ,meth);
 idx=find(finite(s));
 gS = griddata(tdist(idx),Depth(idx),s(idx),idist,iZ,meth);
 idx=find(finite(dens));
 gD = griddata(tdist(idx),Depth(idx),dens(idx),idist,iZ,meth);
 idx=find(finite(o2));
 gO = griddata(tdist(idx),Depth(idx),o2(idx),idist,iZ,meth);
 idx=find(finite(ph));
 gP = griddata(tdist(idx),Depth(idx),ph(idx),idist,iZ,meth);
 idx=find(finite(f));
 gF = griddata(tdist(idx),Depth(idx),f(idx),idist,iZ,meth);
 idx=find(finite(obs));
 gB = griddata(tdist(idx),Depth(idx),obs(idx),idist,iZ,meth);
 
 if filtit
 disp(['Median Filtering...']);
 gT=medfiltJR(gT,3); gS=medfiltJR(gS,3); gO=medfiltJR(gO,3);
 gP=medfiltJR(gP,3); gF=medfiltJR(gF,3); gB=medfiltJR(gP,3);
 gD=medfiltJR(gD,3);
end

 if drifter1; idist=fliplr(idist); end	% plot with correct shoreward/seaward reference

 end 
 
 % Plot Locations
 % PH=.2; y1=1-.23-PH; y2=y1-PH-.05; y3=y2-PH-.05;
 % x1=.17; PW=.4; x2=x1+PW+.08;
 % P1=[x1 y1 PW PH]; P2=[x1 y2 PW PH]; P3=[x1 y3 PW PH];
 % P4=[x2 y1 PW PH]; P5=[x2 y2 PW PH]; P6=[x2 y3 PW PH];
PH=.16; y1=1-.07-PH; y2=y1-PH-.01; y3=y2-PH-.01; y4=y3-PH-.01; y5=y4-PH-.01;
x1=.17; PW=.83;  
P1=[x1 y1 PW PH]; P2=[x1 y2 PW PH]; P3=[x1 y3 PW PH]; P4=[x1 y4 PW PH]; P5=[x1 y5 PW PH];

figure(1); clf; giffig_wide

%%----------------------
%% MAP
%%----------------------
mapP = [.01 .88 .08 .10]; 
axes('position', mapP);load wcoastline.dat

p=plot(wcoastline(:,1),wcoastline(:,2),'k'); set(p,'linewidth',2); 
hold on; plot(ILON,ILAT,'b');

if drifter0		%% Drifter Pass, Day 240
	axis([-122.4 -122.1 36.8 37.05]); llgrid(15); set(gca,'fontsize',8);
	t=text(-122.39,37.02,'Drifter'); set(t,'fontsize',8,'color','m');
   t=text(-122.39,36.85,'AUV'); set(t,'fontsize',8,'color','b');
   load drifter1_2000240.dat; plot(drifter1_2000240(:,3),drifter1_2000240(:,2),'m');
   drifterXing=[7.5,0.5];
end

%% Drifter Surveys, day 242
if drifter1		%% Drifter Pass, Day 240
	axis([-122.6 -121.75 36.5 37.05]); llgrid(60); set(gca,'fontsize',8);
	t=text(-122.39,36.9,'Drifter'); set(t,'fontsize',8,'color','m');
   t=text(-122.3,36.6,'AUV'); set(t,'fontsize',8,'color','b');
   load drifter1_2000242.dat; plot(drifter1_2000242(:,3),drifter1_2000242(:,2),'m');
   drifterXing=[max(tdist)-8.5,0.5];	% adjusted to make onshore/offshore orientation correct
end

if drifter2		%% Drifter Pass, Day 240
	axis([-122.6 -121.75 36.5 37.05]); llgrid(60); set(gca,'fontsize',8);
	t=text(-122.39,36.9,'Drifter'); set(t,'fontsize',8,'color','m');
   t=text(-122.35,36.6,'AUV'); set(t,'fontsize',8,'color','b');
   load drifter1_2000242.dat; plot(drifter1_2000242(:,3),drifter1_2000242(:,2),'m');
   drifterXing=[6.94,0.5];	% adjusted to make onshore/offshore orientation correct
end

tl=get(gca,'Ticklength'); set(gca,'Ticklength',tl*3,'YaxisLocation','right','fontsize',8,'Tickdir','out');


%%----------------------
%% Data
%%----------------------
gvars = ['gT';'gS';'gD';'gO';'gF';'gB';'gP'];
AX=[0 max(tdist) 0 max(Depth)];

for V=1:5; % length(gvars);
   eval(['cvar = ' gvars(V,:) ';']) 
   pstr=['axes(' '''' 'position' '''' ',P' int2str(V) ');']; eval(pstr);
   pcolor(idist,iZ,cvar); shading interp; axis(AX); hold on
   p=plot(drifterXing(1),drifterXing(2),'hm'); set(p,'markerfacecolor','m','markersize',8);
   cb=colorbar; p=get(cb,'position'); set(cb,'position',p.*[1 1 .2 1]);
   CAX(V,:)=caxis;
   set(gca,'ydir','reverse','Tickdir','out','YTick',[25:25:75]); ylabel('Depth (m)');
   if V==5;
      %if V==3 | V==6
      xlabel(XLAB);
   else
      set(gca,'XTicklabel',[])
   end
   
   % title(vartitl(V,:));
   % hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',3);
end

frame
txt=text(x1+.45*PW,.97,titl); set(txt,'fontweight','bold','fontsize',18,'horizontalalignment','center');
txt=text(.9,.04,'* uncalibrated'); set(txt,'fontsize',9)
LX=.9;
t=text(LX,y1+.5*PH,['Temperature (' setstr(176) 'C)']); set(t,'fontsize',10,'rotation',90,'horizontalalignment','center');
t=text(LX,y2+.5*PH,['Salinity (psu)']); set(t,'fontsize',10,'rotation',90,'horizontalalignment','center');
t=text(LX,y3+.5*PH,['Density (kg/m^3)']); set(t,'fontsize',10,'rotation',90,'horizontalalignment','center');
t=text(LX,y4+.5*PH,['Oxygen (ml/l)']); set(t,'fontsize',10,'rotation',90,'horizontalalignment','center');
t=text(LX,y5+.5*PH,['Fluor (volts)']); set(t,'fontsize',10,'rotation',90,'horizontalalignment','center');


