clear all

plotit = 1;
xport = 1; xpfile = ['ody_drifter2.asc']; matfile=['drifter2'];
% cd c:\muse\auv\day\T4
cd g:\auv\day\
% load missionlist241;
missionlist=['2000.242.03';'2000.242.04'];
[N x] = size(missionlist);

for M = 1:N;
 % eval(['cd 2000.238.' missionN(M,:)]);
 eval(['cd ' missionlist(M,:)]);
 load ctd
 if M==1;
  o2=O2; s=S; t=T; ph=pH; Depth=depth; time=epochsec; 
  Roll=roll; Pitch=pitch; Yaw=yaw;
  f=F; obs=OBS;
  % lat=startlat; lon=startlon; 
  EP(1,:)= ep([1 3]);
 else
  o2=[o2;O2]; s=[s;S]; t=[t;T]; ph=[ph;pH]; Depth=[Depth;depth]; 
  time=[time;epochsec];
  Roll=[Roll;roll]; Pitch=[Pitch;pitch]; Yaw=[Yaw;yaw];
  f=[f;F]; obs=[obs;OBS];
  % lat=[lat;startlat]; lon=[lon;startlon]; 
  EP=[EP;ep([1 3;2 4])]; ep=EP; lat=ep(:,1); lon=ep(:,2);
   end
idx(M) = length(T);	% idx contains endpoint of segment
cd g:\auv\day\
end

% load ep238; lat=ep(:,1); lon=ep(:,2);
% define_ep239; lat=ep(:,1); lon=ep(:,2);
% define_ep241;
%% 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;eplat]; lon=[lon;eplon];


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

%% Cheap fix for misaligned vectors
% ILAT=ILAT(1:11513); ILON=ILON(1:11513); s=s(1:11513); t=t(1:11513); 

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);
 plot_monitoring_trans
 YL=get(gca,'Ylim'); set(gca,'Ylim',YL-[.1 0]);
 % 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');
 % 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;
 plot3(ILAT,ILON,Depth,'r.');
 set(gca,'ydir','reverse','zdir','reverse');
 grid;
end

% Define up/down cast
% Depth is positive
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;

if xport
 eval(['save ' matfile ' time ILAT ILON Depth t s o2 ph f obs Pitch Roll Yaw upcast downcast']);  
 disp(['Exporting data to ascii file ' xpfile]);
 % odat = [ILAT ILON Depth t s o2 ph f obs Pitch Roll Yaw];
 % eval(['fid=fopen(','''',xpfile,'''',',','''w''',');']);
 % fprintf(fid,'epochsec, lat, lon, depth, temp, sal, O2, pH, fluorvolt, OBSvolt, 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,%7.3f,%7.3f\n',odat');
 % fclose(fid);
end
