clear all

plotit = 1; xport = 1; 
% xpfile = ['ody_drifter2.asc']; matfile=['drifter2'];
wdir = 'c:\muse\auv\day\T3'; xpfile = ['T3a.asc']; matfile=['T3a']; 
eval(['cd ' wdir]);
% missionlist=['2000.243.00';'2000.243.01';'2000.243.02';'2000.243.03';'2000.243.04';'2000.243.05';'2000.243.11';'2000.243.12';];
missionlist=['2000.240.06';'2000.240.07';'2000.240.09'];
[N x] = size(missionlist);

%% Initialize Variables
s=[]; t=[]; dens=[]; f=[]; obs=[]; o2=[]; ph=[]; Depth=[]; time=[];  
Roll=[]; Pitch=[]; Yaw=[];
ILAT=[]; ILON=[]; IDIST=[0];

for M = 1:N;
 eval(['cd ' missionlist(M,:)]);
 load ctd
 	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]; dens=[dens;D];
   if M<N; EP(M,:)= ep([1 3]); else	EP=[EP;ep([1 3;2 4])]; EP; lat=EP(:,1); lon=EP(:,2); end
   ILAT=[ILAT; [linspace(ep(1),ep(2),length(S))]'];
   ILON=[ILON; [linspace(ep(3),ep(4),length(S))]'];
   IDIST=[IDIST; max(IDIST)+[linspace(0,dist,length(S))]'];
eval(['cd ' wdir]);
end
IDIST(1)=[];

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');
 %% 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; set(gcf,'units','pixels','position',[10 100 800 500]);
 % subplot(121); 
 load wcoastline.dat; idx=find(wcoastline(:,2)>=36.6 & wcoastline(:,2)<=37);
 % plot(ILON,ILAT,'r.'); hold on;
 % plot(wcoastline(idx,1),wcoastline(idx,2),'g.'); 
 % subplot(122);
 plot3(ILON,ILAT,Depth,'r.'); grid on; set(gca,'zdir','reverse'); hold on; box on; 
 plot3(wcoastline(idx,1),wcoastline(idx,2),0*idx,'g.');
 
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 IDIST Depth t s dens 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
