procit=1;

if procit
   
%%------------------------------------------------------   
%% requires a 'missiondata' file in the directory to be processed
%% example file is below...
%% insert missionID and load this file in each directory
%% ship head, bearing to auv, dist to auv (km)
%% longitude west should be positive --
%% will be converted to negative by bearing.m
%
%% startrng includes ship head, bearing to auv, dist to auv (km)
%
%% edit these six lines...
%
%
%missionID='2000.238.17';
%surveydate='25 August 2000'; %for graph title
%
%% degrees can be DD.DDDD or DD MM.MMM 
%% must be consistent within one set of bearings
%
%shipstart=[34 30.00 122 59.99] ;
%startrng =[0 180 400] ;          
%
% shipend=[46.5 120.0] ;% endrng=[90 0 500]   ;         
%
% end of example mission data
% --------------------------------------------------



clear all;
% load ctd_section;
% Test yoyo
load mission; t=T; s=S; Depth=depth;

tempswitch = 0 %from  vehicle =0, from biolum =1

missiondata ;%contains missionID, surveydate, lat lon, also can override tempswitch

if (tempswitch)
   disp('Using biolum temp...');
   else
      disp('Using auv temp...');
   end%if
   
titl=['AUV Bioluminescence survey line, 'surveydate]; %surveydate from missiondata
%% Estimate transect distance


 minZ=3;
%% Comment out all but current mission
%ep=[36+54.61/60 36+53.78/60 -122-6.45/60 -122-6.283/60]; missionID='2000.238.19';
%ep=[36+55.983/60 36+55.102/60 -122-5.991/60 -122-6.283/60]; missionID='2000.238.17';
%ep=[36+55.324/60 36+54.527/60 -122-6.76/60 -122-6.376/60]; missionID='2000.238.18';

ep = [NaN NaN NaN NaN];
 %values used in bearing.m are loaded from missiondata
 ep([1 3])=bearing(shipstart,startrng);
 ep([2 4])=bearing(shipend,endrng)


% assume vehicle speed of 1 m/s
dist = (max(epochsec)-min(epochsec))/1000;

%not used 
[dist2 phas]=sw_dist(ep([1 2]),ep([3 4]),'km');

disp(['Distance from Epochsec : ' num2str(dist) ' km']);
disp(['Distance from LatLong  : ' num2str(dist2) ' km']);

tdist = linspace(0,dist,length(F));
idist = linspace(0,dist,200);
iZ = [minZ:1:max(Depth)]';
AX = [0 dist min(iZ) max(iZ)]; 

%% Choose interpolation method
delaunay=1;
jgbgrid=0; 
   
if delaunay 
   disp('Using griddata (Delaunay)...'); meth='linear';
   gT = griddata(tdist,Depth,t,idist,iZ,meth);
   gS = griddata(tdist,Depth,s,idist,iZ,meth);
   gF = griddata(tdist,Depth,F,idist,iZ,meth);
   gOBS = griddata(tdist,Depth,OBS,idist,iZ,meth);
   gbiolum = griddata(tdist,Depth,Biolum,idist,iZ,meth);
   gbioflow = griddata(tdist,Depth,Bioflow,idist,iZ,meth);
   gbiotemp= griddata(tdist,Depth,Biotemp,idist,iZ,meth);

end
end
 %% Plot locations
ph=.2;
y1=1-.23-ph; y2=y1-ph-.05; y3=y2-ph-.05;
x1=.08; 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];

figure(1); clf; giffig_wide

%% little MAP
mapP = [x1+.05 y1+ph+.05 .10 .15];
axes('position', mapP);
% load transect_endpoints; 
plot_transectP(ep);
tl=get(gca,'Ticklength'); set(gca,'Ticklength',tl*3);
% axes('position',P1);axes('position',P2);axes('position',P3);
% axes('position',P4);axes('position',P5);axes('position',P6);


 axes('position',P1);
 if tempswitch
    pcolor(idist,iZ,gbiotemp);
 else
    pcolor(idist,iZ,gT); 
 end%if
 shading interp; 
 % caxis([8.9 12.7]); 
 cb=colorbar; p=get(cb,'position'); set(cb,'position',p.*[1 1 .5 1]);
 % set(cb,'YTick',[]) % removes label
 set(gca,'ydir','reverse','Tickdir','out','XtickLabel',[]); 
 % xlabel('Distance along transect (km)'); 
 ylabel('Depth (m)');
 if tempswitch
   title(['Biolum temp (' setstr(176) 'C)']); 
else
  title(['Temperature (' setstr(176) 'C)']); 
 end%if
 % title(['Temperature: observed [min max] = [' num2str([min(t) max(t)]) ']']);
 hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 
 axes('position',P2);
 pcolor(idist,iZ,gS); shading interp;
 % imagesc(idist,iZ,gS);
 % caxis([33.6 34]); 
 cb=colorbar; p=get(cb,'position'); set(cb,'position',p.*[1 1 .5 1]);
 % set(cb,'YTick',[]) % removes label
 set(gca,'ydir','reverse','Xticklabel',[],'tickdir','out'); 
 % xlabel('Distance along transect (km)'); 
 ylabel('Depth (m)'); 
 title(['Salinity (psu)']);
% title(['Salinity: observed [min max] = [' num2str([min(s) max(s)]) ']']);
 hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 
 axes('position',P3); 
 pcolor(idist,iZ,gF); shading interp; 
 % caxis([8.9 12.7]); 
 cb=colorbar; p=get(cb,'position'); set(cb,'position',p.*[1 1 .5 1]);
 set(gca,'ydir','reverse','Tickdir','out');
 xlabel('Distance along transect (km)'); 
 ylabel('Depth (m)'); 
 title('Fluorometer (volts)');
 hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 
 axes('position',P4);
 pcolor(idist,iZ,gOBS); shading interp; 
 % caxis([8.9 12.7]); 
 cb=colorbar; p=get(cb,'position'); set(cb,'position',p.*[1 1 .5 1]);
 set(gca,'ydir','reverse','Tickdir','out','XtickLabel',[]);
 % xlabel('Distance along transect (km)'); 
 ylabel('Depth (m)'); title('OBS (volts)');
  hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 
 axes('position',P5);
 pcolor(idist,iZ,gbiolum); shading interp; 
 % caxis([8.9 12.7]); 
 cb=colorbar; p=get(cb,'position'); set(cb,'position',p.*[1 1 .5 1]);
 set(gca,'ydir','reverse','Tickdir','out','XTickLabel',[]);
 % xlabel('Distance along transect (km)'); 
 ylabel('Depth (m)'); title('Biolum (counts)') 
 % title(['pH: observed [min max] = [' num2str([min(ph) max(ph)]) ']']);
 hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 
 axes('position',P6);
 pcolor(idist,iZ,gbioflow); shading interp; 
 % caxis([8.9 12.7]); 
 cb=colorbar; p=get(cb,'position'); set(cb,'position',p.*[1 1 .5 1]);
 set(gca,'ydir','reverse','Tickdir','out');
 xlabel('Distance along transect (km)'); ylabel('Depth (m)');
 title('Biolum flow');
 % title(['pH: observed [min max] = [' num2str([min(ph) max(ph)]) ']']);
 hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 
frame
txt=text(.3,.92,titl); set(txt,'fontweight','bold','fontsize',18);
txt=text(.7,.88,['Mission ID: ' missionID]); set(txt,'fontsize',12)
% txt=text(.7,.85,['(Straightened out zig-zag mission)']); set(txt,'fontsize',10)


return
t=text(sum(P1([1 3]))-.02,sum(P1([2 4])),[setstr(176) 'C']); set(t,'fontsize',12);
t=text(sum(P2([1 3]))-.02,sum(P2([2 4])),['psu']); set(t,'fontsize',12);
t=text(sum(P3([1 3]))-.02,sum(P3([2 4])),['flvolt']); set(t,'fontsize',12);
t=text(sum(P4([1 3]))-.02,sum(P4([2 4])),['obsvolt']); set(t,'fontsize',12);
t=text(sum(P5([1 3]))-.02,sum(P5([2 4])),['biolum']); set(t,'fontsize',12);
t=text(sum(P6([1 3]))-.02,sum(P6([2 4])),['bioflow']); set(t,'fontsize',12);


if jgbgrid
 disp('Using potgrid...'); 
 figure(1); clf; set(gcf,'units','pixels','position',[20 50 600 800]);
 %% Use JGB's potgrid to grid data
 %% Size of output grid
 nx = 100; ny = 50;
 %% Weighting for gridding;
 wi = 1; wj = round(dist*1000/max(Depth));
 disp(['Weighting for Distance : ' int2str(wj) ':1 x:z']);
 
 %% Temperature
 [xg,yg,zg] = potgrid(tdist',Depth,t,[nx ny],[wi wj]);
 subplot(411); pcolor(xg,yg,zg); shading interp; colorbar; set(gca,'Ydir','rev');
 hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 title(['Temperature, observed [min max] = [' num2str([min(t) max(t)]) ']']);
 ylabel('Depth (m)');
 
 %% Salinity 
 [xg,yg,zg] = potgrid(tdist',Depth,s,[nx ny],[wi wj]);
 subplot(412); pcolor(xg,yg,zg); shading interp; colorbar; set(gca,'Ydir','rev');
 hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 title(['Salinity, observed [min max] = [' num2str([min(s) max(s)]) ']']);
 ylabel('Depth (m)');
 
 %% O2 
 [xg,yg,zg] = potgrid(tdist',Depth,o2,[nx ny],[wi wj]);
 subplot(413); pcolor(xg,yg,zg); shading interp; colorbar; set(gca,'Ydir','rev');
 hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 title(['O_2, observed [min max] = [' num2str([min(o2) max(o2)]) ']']);
 ylabel('Depth (m)');
 
 %% pH 
 [xg,yg,zg] = potgrid(tdist',Depth,ph,[nx ny],[wi wj]);
 subplot(414); pcolor(xg,yg,zg); shading interp; colorbar; set(gca,'Ydir','rev');
 hold on; p=plot(tdist,Depth,'k.'); set(p,'markersize',4);
 title(['pH, observed [min max] = [' num2str([min(ph) max(ph)]) ']']);
 xlabel('Distance along transect (km)');
 ylabel('Depth (m)');

 axes('position',[0 0 1 1]); set(gca,'XTick',[],'YTick',[],'color','none');
 txt=text(.5,.97,'Gridded AUV observations, using potgrid.m');
 set(txt,'fontweight','bold','horizontalalignment','center');
end

