%% auv_section.m to grid auv [X Y Z] data into vertical sections.
clear all; close all

%% Process section from assembled survey?
procit=1; 
loadmat=1;	% 1 = use matlab file; 0 = use netcdf file

%% Use only upcast in gridded section?
upcastonly=0;

%% Median filter the section?
filtit=1; fsz=5; fm=1; 

%% Print a jpeg image of the figure window after plotting?
printfigure=0;

%% Define length of X vector for gridding
xgn = 200;

%% Define survey
% day238=0; drifter0=0; drifter1=0; drifter2=0; M1C1a=0; M1C1b=1; 

survey_assembly_setup; 
if loadmat & procit
 disp(['Loading processed data from MATLAB file...']);
 eval(['load ' surveyfilename]);
elseif ~loadmat & procit
myncpath
 ncfile=[surveyfilename '.nc'];
 disp(['Loading processed data from netcdf file ' ncfile '...']);
 ncload(ncfile,'time','ILAT','ILON','Depth','IDIST','XPOS','YPOS','s','t','dens','obs','f','o2','ph'); 
end
xlbl=['West';'East']; 

%% 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

XLAB='Distance (km)';

%%-------------------------------------------------
%%  PROCESSING
%%-------------------------------------------------
if procit
 disp(['Processing to produce a vertical section...']);
 maxdist=max(IDIST); gdist=linspace(0,maxdist,xgn); gZ = [4:1:max(Depth)]';
 AX = [0 maxdist min(gZ) max(gZ)]; 

 % Define up/down cast to use for gridding discrimination and bottom vector (Depth is positive)
 fdepth=mvavgmean(Depth,7); dZdt=[NaN; diff(fdepth)./diff(time)];
 % dZdt = [NaN; diff(Depth)./diff(time)];
 yoyo_discriminator = 0.1;       % m/s
 upcast=repmat(0,length(f),1);
 idx=find(dZdt<-1*yoyo_discriminator); upcast(idx)=upcast(idx)+1;
 downcast=repmat(0,length(f),1);
 idx=find(dZdt>yoyo_discriminator); downcast(idx)=downcast(idx)+1;
 %% Get a bottom fill vector
 idx=find(diff(upcast)==1); 
 bdist=IDIST(idx); bdepth=fdepth(idx); 
 bdepth=medfilt1(bdepth,7); bdepth=mvavgmean(bdepth,3);
 bdepth=[bdepth; max(Depth); max(Depth)];
 bdist=[bdist; max(bdist); min(bdist)];

 pltchk=0;
 if pltchk
  figure(1); clf; giffig_wide;
  p=plot(IDIST,fdepth,'k.'); set(p,'markersize',5); hold on
  idx=find(downcast); p=plot(IDIST(idx),fdepth(idx),'b.'); set(p,'markersize',5);
  idx=find(upcast); p=plot(IDIST(idx),fdepth(idx),'r.'); set(p,'markersize',5);
  p=plot(bdist,bdepth,'k*'); set(p,'markersize',10);
  set(gca,'Ydir','reverse');
  disp('pause'); pause
 end
 
 disp('Using griddata (Delaunay)...'); meth='linear';
 if upcastonly
  disp(['Using upcast only...']); addstr=['& upcast)'];
 else
  disp(['Using upcast and downcast...']); addstr=[' )'];
 end

 fstr=['Tidx=find(finite(t) ' addstr ';']; eval(fstr);
 gT = griddata(IDIST(Tidx),Depth(Tidx),t(Tidx),gdist,gZ,meth);
 fstr=['Fidx=find(finite(f) ' addstr ';']; eval(fstr);
 gF = griddata(IDIST(Fidx),Depth(Fidx),f(Fidx),gdist,gZ,meth);
 fstr=['Bidx=find(finite(obs) ' addstr ';']; eval(fstr);
 gB = griddata(IDIST(Bidx),Depth(Bidx),obs(Bidx),gdist,gZ,meth);
 fstr=['Sidx=find(finite(s) ' addstr ';']; eval(fstr);
 gS = griddata(IDIST(Sidx),Depth(Sidx),s(Sidx),gdist,gZ,meth);
 fstr=['Didx=find(finite(dens) ' addstr ';']; eval(fstr);
 gD = griddata(IDIST(Didx),Depth(Didx),dens(Didx),gdist,gZ,meth);
 fstr=['Oidx=find(finite(o2) ' addstr ';']; eval(fstr);
 gO = griddata(IDIST(Oidx),Depth(Oidx),o2(Oidx),gdist,gZ,meth);
 fstr=['Pidx=find(finite(ph) ' addstr ';']; eval(fstr);
 gP = griddata(IDIST(Pidx),Depth(Pidx),ph(Pidx),gdist,gZ,meth);

 if filtit
  disp(['Median Filtering...']);
  gT=mf(gT,fsz,fm); gF=mf(gF,fsz,fm); gB=mf(gB,fsz,fm); gS=mf(gS,fsz,fm);
  gD=mf(gD,fsz,fm); gP=mf(gP,fsz,fm); gO=mf(gO,fsz,fm);
 end
 save
else
 load
end

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

%%-------------------------------------------------
%%  PLOTTING 
%%-------------------------------------------------
figure(1); clf; 
set(gcf,'units','pixels','color','w','position',[40 50 800 1100],'menubar','none');
load jetplus; colormap(jetplus);


pht=0.11;
 P1=[.15 .76 .7 pht]; bufr=.005;
 P2=P1; P2(2)=P2(2)-pht-bufr;
 P3=P2; P3(2)=P3(2)-pht-bufr;
 P4=P3; P4(2)=P4(2)-pht-bufr;
 P5=P4; P5(2)=P5(2)-pht-bufr;
 P6=P5; P6(2)=P6(2)-pht-bufr;
 P7=P6; P7(2)=P7(2)-pht-bufr;
 P8=P7; P8(2)=P8(2)-pht-bufr;
offset=0;
mapP=[.1 P1(2)+pht+.01 .18 .1];

idx=find(finite(bdepth)); bdist=bdist(idx); bdepth=bdepth(idx);
AX=[min(IDIST) max(IDIST) 4 max(Depth)-2];

axes('position',mapP);
mapAX=[-122.4 -121.75 36.55 37.05]; gry=[.2 .4 .2];
wclandfill(mapAX,gry); hold on; plot_iso;
set(gca,'tickdir','out','fontsize',12,'fontweight','bold');
plot(ILON,ILAT,'r'); 
p=plot(eplon,eplat,'ro'); set(p,'markerfacecolor','r','markersize',4);
set(gca,'fontsize',10,'Xaxislocation','top');

gvars = ['gT';'gS';'gD';'gO';'gF';'gB';'gP'];
for V=1:length(gvars);
   eval(['cvar = ' gvars(V,:) ';']);
   pstr=['axes(' '''' 'position' '''' ',P' int2str(V) ');']; eval(pstr);
   % pcolor(ilat-offset,gZ,cvar); shading interp; axis(AX); hold on
   pcolor(gdist,gZ,cvar); shading interp; axis(AX); hold on
   CAX(V,:)=caxis; cax=CAX(V,:);
   set(gca,'ydir','reverse','Xdir','normal','Tickdir','out','YTick',[25:25:75],'fontsize',15);
   ylabel('Depth (m)');
   ff=fill(bdist,bdepth,'w'); 
   set(ff,'markeredgecolor','w','markerfacecolor','w','edgecolor','w');

   if V==1; 
    plot(IDIST,Depth,'w:'); 
    t=text(10,.8*max(Depth),'AUV track in white'); set(t,'fontsize',15);
   end
   if V==length(gvars); xlabel(XLAB); else; set(gca,'XTicklabel',[]); end

   cb=[linspace(cax(1),cax(2),128)]';
   a=get(gca,'position'); cbp=[a(1)+a(3)+.05 a(2)+.01 .01 a(4)-.02];
   axes('position',cbp); pcolor([0 1],cb,[cb cb]); shading interp; caxis(cax);
   set(gca,'fontsize',15,'YaxisLocation','right','XTick',[],'Tickdir','out');
end

frame
t=text(.15,.02,xlbl(1,:)); set(t,'fontsize',15,'color',gry);
t=text(.8,.02,xlbl(2,:)); set(t,'fontsize',15,'color',gry);
t=text(.3,.96,str1); set(t,'fontsize',18,'color',gry);
t=text(.3,.92,str2); set(t,'fontsize',18,'color',gry);

%% Variable labels
LX=.88; tfs=15;
t=text(LX,P1(2)+.5*pht,['Temperature (' setstr(176) 'C)']);
 set(t,'fontsize',tfs,'rotation',90,'horizontalalignment','center');
t=text(LX,P2(2)+.5*pht,['Salinity (psu)']);
 set(t,'fontsize',tfs,'rotation',90,'horizontalalignment','center');
t=text(LX,P3(2)+.5*pht,['Density (kg m^{-3})']);
 set(t,'fontsize',tfs,'rotation',90,'horizontalalignment','center');
t=text(LX,P4(2)+.5*pht,['O_2 (ml/l)']);
 set(t,'fontsize',tfs,'rotation',90,'horizontalalignment','center');
t=text(LX,P5(2)+.5*pht,['Fluor (V)']);
 set(t,'fontsize',tfs,'rotation',90,'horizontalalignment','center');
t=text(LX,P6(2)+.5*pht,['OBS (V)']);
 set(t,'fontsize',tfs,'rotation',90,'horizontalalignment','center');
t=text(LX,P7(2)+.5*pht,['pH']);
 set(t,'fontsize',tfs,'rotation',90,'horizontalalignment','center');

if printfigure
 ofigname = [surveyfilename '.jpg'];
 disp(['Printing figure window to ' ofigname]);
 screen2jpeg(ofigname);
end
