close all;
clear all;

[mode,hr,min,sec,depth,temp,sal,u]=textread('SOLODepth.txt','%c\t %2c%2c%2c\t %f\t %f\t %f\t %f');
date=input('enter the experiment date as m/d/yr:','s');
SOLOdatetime=strcat(date,',',strcat(hr,':',min,':',sec));
timeonly=strcat(strcat(hr,':',min,':',sec));
DepthSeries=timeseries(depth,timeonly);
plot(DepthSeries);
ylabel('Depth (m)');
xlabel('Time (hr)');
title(strcat('Depth Profile',' ',date))

datetick('x','HH:MM');

break;
%process sonar data 
filename=input('enter the filename:','s');

%works for IB03-095, 96
[Datadate,time,freq,j,returntime,ms,d,l,level]=textread(filename,'%s\t%s\t%f\t%s\t%f\t%s\t%s\t%s\t%d');

[Datadate,time,freq,nj,returntime,ms,d,level]=textread(filename,'%s\t%s\t%f\t%s\t%f\t%s\t%s\t%d');

clear j d ms l v freq
matname=input('enter the event:','s');

save(matname,'Datadate','time','returntime','level');

datetime=strcat(Datadate,',',time);
%speed of sound calculated based on 0.44 deg C, 600m depth and salinity of
%34.668- yeilds 1460m/sec *(return time in msec)*(1sec/1000msec)/2 (because
%time is round trip)
dist=(1460.457/2000)*returntime;
scatter(datenum(datetime),dist,level);
hold on;
figure;
plot(datenum(SOLOdatetime)-0.125,depth*-1,'k')
hold on;
scatter(datenum(datetime),dist-600,level);

legend('Float Depth','Sonar');
ylabel('Depth (m)');
xlabel('Time (day)');
datetick('x','mm/dd')
title('LST 1 -IB03-027');
title('LST 3 -IB03-096');
title('LST 1 -IB03-095');
title('LST 4 -IB03-142');


figure; subplot(2,1,2);
figure(4); subplot(2,1,1);



close all;
clear all;

[mode,date,hr,min,sec,depth,temp,sal,u]=textread('SOLODepth.txt','%c\t %8c\t %2c%2c%2c\t %f\t %f\t %f\t %f');
SOLOdatetime=strcat(date,',',strcat(hr,':',min,':',sec));
timeonly=strcat(strcat(hr,':',min,':',sec));
%DepthSeries=timeseries(depth,timeonly);
DepthSeries=timeseries(depth,SOLOdatetime);
figure;
plot(DepthSeries);
title(strcat('Depth Profile',' ',date(end,:)))


figure; plot(datenum(SOLOdatetime),sal)


DriftSeries=timeseries(depth(194:end),SOLOdatetime(194:end,:));

%drift ctd data for IB03-096
figure; plotyy(datenum(SOLOdatetime(177:end,:)),sal(177:end,:),datenum(SOLOdatetime(177:end,:)),depth(177:end,:))

SOLOElapsedTime=datenum(SOLOdatetime)-datenum(SOLOdatetime(1,:));
%shift time to compare to sonar returns
%SOLOElapsedTime=(datenum(SOLOdatetime)-0.125)-datenum(SOLOdatetime(1,:));

figure;
plot(DriftSeries);
plot(datenum(SOLOdatetime(194:end,:)),depth(194:end))
plot(datenum(SOLOdatetime),depth,'k')
ylabel('Depth (m)');


%just look at max returns
i=find((level>=128)&(abs(dist)<650));
Dist=zeros(size(i));
Date=zeros(size(i));
Level=zeros(size(i));
for b=1:size(i,1),
     Date(b,1)=datenum(datetime(i(b,1),:));
     Dist(b,1)=dist(i(b,1));
     Level(b,1)=level(i(b,1));
end

MET=Date-Date(1);
figure;subplot(2,2,1)
scatter(MET,Dist,Level);
ylabel('Depth (m)');
xlabel('Elapsed Time (days)');

hold on;subplot(2,2,2);
plot(MET(2:end-20),Dist(2:end-20));
ylabel('Taret Distance from LST (m)');
xlabel('Elapsed Time (days)');

MET=Date-Date(1);
figure;subplot(2,2,3)
scatter(MET,Dist,Level);
ylabel('Depth (m)');
xlabel('Elapsed Time (days)');

subplot(2,2,4);
plotyy(MET(2:end-20),Dist(2:end-20),SOLOElapsedTime(2:end-20),depth(2:end-20).*-1);
plotyy(MET,Dist,SOLOElapsedTime,depth.*-1);


figure;
plotyy(MET(1:end-20)+0.125,Dist(1:end-20),SOLOElapsedTime(2:end-20),depth(2:end-20).*-1);
plotyy(MET,Dist,SOLOElapsedTime,depth.*-1);



ylabel('Salinity (PSU)');
xlabel('Date');
legend('SOLO CTD','Ship-Based CTD');

Title('Comparison of Salinity Measurements around 600m'); 
ylabel('Taret Distance from LST (m)');
xlabel('Elapsed Time (days)');

CTD=[34.58799048,	88.433746,-0.366161905;

34.63422857,	89.584257,0.119438095;

34.75291905,	91.805085,0.422971429
];

CTDdate=datevec(CTD(:,2));
CTDdate(:,1)=[2009;2009;2009];
plot(datenum(CTDdate),CTD(:,2),'x')

figure; [AX,H1,H2]=plotyy(datenum(SOLOdatetime(177:end,:)),sal(177:end,:),datenum(SOLOdatetime(177:end,:)),temp(177:end,:))
hold on (AX(1); plot(AX(1),datenum(CTDdate),CTD(:,1))

figure; subplot(2,1,1);plot(datenum(SOLOdatetime(177:end,:)),sal(177:end,:),'b',datenum(CTDdate),CTD(:,1),'bo')
datetick('x','mm/dd')
ylabel('Salinity (PSU)');
xlabel('Date');
legend('LST CTD','Ship-Based CTD');

subplot(2,1,2); plot(datenum(SOLOdatetime(177:end,:)),temp(177:end,:),'g',datenum(CTDdate),CTD(:,3),'go');
datetick('x','mm/dd')
ylabel('Temperature (Deg C)');
xlabel('Date');
legend('LST CTD','Ship-Based CTD');

figure;[AX,H1,H2]=plotyy(datenum(CTDdate),CTD(:,1),datenum(CTDdate),CTD(:,3));
hold on (AX(1);
plot(AX(1),datenum(SOLOdatetime(177:end,:)),sal(177:end,:))
% 
% a=level==256;
% maxReturns=level.*a;
% maxDist=dist.*a;
% maxDatenum=datenum(datetime).*a;
% Dist=sparse(maxDist);
% Date=sparse(maxDatenum);
