clear all

missions = ['2000.241.04/'; '2000.241.05/'; '2000.241.06/'; '2000.241.07/'];

plotit = 1;
xport = 1; xpfile = ['test.asc'];

[N x] = size(missions);

for M = 1:N;
 eval(['load c:/muse/auv/night/' missions(M,:) 'mission.mat']);
 if M==1;
  obs=OBS; s=S; t=T; biolum=Biolum; Depth=depth; time=epochsec; 
  Roll=roll; Pitch=pitch; Yaw=yaw;
  currentdir=pwd;
  eval(['cd c:/muse/auv/night/' missions(M,:)]); %load starting info for mission;
  missiondata;
  eval(['cd ' currentdir]); %load starting info for mission;
 % latlon=bearing(shipstart,startrng); %calculate starting point of first mission
  idx(M) = length(T);	% idx contains endpoint of segment
   lat=startlat; lon=startlon; 
 else
  obs=[obs;OBS]; s=[s;S]; t=[t;T]; biolum=[biolum;Biolum]; 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

if plotit
 figure(1); clf; 
 set(gcf,'units','pixels','position',[20 50 350 400],'color',[1 1 1],'menubar','none');
 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_iso([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');
%p=plot(latlon(1,1),latlon(2,1),'ro'); set(p,'markerfacecolor','r')
%p=plot(latlon(2,1),latlon(2,2),'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');
 pause;
 figure(2); clf;
 plot3(ILAT,ILON,Depth,'r.');
 set(gca,'ydir','reverse','zdir','reverse');
 grid;
end

if xport
 disp(['Exporting data to ascii file ' xpfile]);
 odat = [time ILAT ILON Depth t s obs biolum Pitch Roll Yaw];
 eval(['fid=fopen(','''',xpfile,'''',',','''w''',');']);
 fprintf(fid,'epochsec, lat, lon, depth, temp, sal, OBS, Biolum, pitch, roll, yaw\n')
 fprintf(fid,'%15.3f,%10.6f,%10.6f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f,%7.3f\n',odat');
 fclose(fid);
end


