clear all

missions %loads missions from missions file in directory
% format of missions file: 
% missions = ['2000.245.01/'; '2000.245.02/'; '2000.245.03/'; '2000.245.04/'; '2000.245.05/'; '2000.245.06/'];


plotit = 1;
asciiout = 0;
matout=1; outfile = 'biolum_section';

[N x] = size(missions);
startdir=eval(['pwd']);
for M = 1:N;
   eval(['cd ../' missions(M,:)]); load mission; 
   clear ep
   clear startlat
   clear startlon
   missiondata
   ep([1 3])=bearing(shipstart,startrng);
   startlat=ep(1);
   startlon=ep(3);
   ep([2 4])=bearing(shipend,endrng)
 % eval(['load ../' missions(M,:) 'ctd']);
 eval(['cd ' startdir]);
 
 if M==1;
   biolum=Biolum; s=S; t=T; obs=OBS; f=F; biotemp=Biotemp; bioflow=Bioflow; Depth=depth; time=epochsec; 
  % Roll=roll; Pitch=pitch; Yaw=yaw;
  lat=startlat; lon=startlon; 
  firstlat=startlat; firstlon=startlon;
 else
  biolum=[biolum;Biolum]; obs=[obs;OBS]; biotemp=[biotemp;Biotemp]; f=[f;F]; s=[s;S]; t=[t;T]; bioflow=[bioflow;Bioflow]; Depth=[Depth;depth]; 
  time=[time;epochsec];
  % Roll=[Roll;roll]; Pitch=[Pitch;pitch]; Yaw=[Yaw;yaw];
  lat=[lat;startlat]; lon=[lon;startlon]; 
end%if
idx(M) = length(T);	% idx contains endpoint of segment
end%for
ep(1)=firstlat; ep(3)=firstlon; %by this time we have exited the loop, ep2&4 have the last vals 


%% Bioflow prediction
% X = [t s biolum]; [beta stats] = mlr(X,bioflow,1); return

%% Hysteresis check
% down = find(diff(depth)>0); up = find(diff(depth)<0);
% figure(1); clf; plot(t(down),depth(down),'r.',t(up),depth(up),'b.');
% set(gca,'Ydir','reverse'); legend('down','up'); return

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

if plotit
   figure(1); clf; giffig;
 axes('position',[.2 .15 .7 .7]);
 mapaxis =[-122.08 -121.78 36.57 37]; 
 [fillLat,fillLon] = get_landfill(mapaxis);
 fill(fillLon,fillLat,[.5 .5 .5]); axis(mapaxis);
 hold on; % plot_isoA([200 500 800],mapaxis,'b');
 set(gca,'tickdir','out','fontsize',12,'fontweight','bold');
 llgrid(10);

 plot(ILON,ILAT,'r'); 
 p=plot(lon,lat,'ro'); set(p,'markerfacecolor','r');
 p=plot(alon,alat,'ro'); set(p,'markerfacecolor','r');
 % title('Mission starting points');
 %% M1
 p=plot(-122.03,36.75,'ko'); 
 set(p,'markerfacecolor','k','markersize',8);
 tx=text(-122.03,36.73,'M1'); 
 set(tx,'fontweight','bold','fontsize',15,'color','k');
 
 figure(2); clf; giffig
 plot3(ILAT,ILON,Depth,'r.');
 set(gca,'ydir','reverse','zdir','reverse');
 grid;
end

if matout
   eval(['save ' outfile ' t s f obs biotemp biolum bioflow Depth ILAT ILON time ep']);
end

if asciiout
odat = [time ILAT ILON Depth t s biolum bioflow];
fid = fopen('auvdive2.asc','w');
fprintf(fid,'%15.3f,%10.6f,%10.6f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f\n',odat');
fclose(fid);
end

