% map_argos
%
% function for reading in data from argos drifters and extracting 
%  the positions.   This currently does nothing to the hex data stream
%
% emontgomery@whoi.edu 8/05
% modified by C Sellers
%

figure(1)
clf

[sn, val, val1, val2, s1, val3, s2, s3, lat, lon, n, val4]= ...
    textread('l_argos_pos.txt','%d %d %d %d %c %d %s %s %f %f %f %d');

lon=lon-360;
lx=find(lon< -67);
lon=lon(lx);
lat=lat(lx);
s2=s2(lx);
s3=s3(lx);

h(1)=plot(lon,lat,'b');
hold on
plot(lon(end),lat(end),'*','markersize',10)

% generate time vector for data
a=[]; b=[];
for ii = 1:length(s3)
  a(ii,:)=sscanf(s2{ii},'%4d%c%2d%c%2d')';
  b(ii,:)=sscanf(s3{ii},'%2d%c%2d%c%2d')';
end

days=[31 28 31 30 31 30 31 31 30 31 30 31];
mm=a(:,3);
dd=a(:,5);
hh=b(:,1);
minit=b(:,2);
ss=b(:,3);
clear yd
for ii = 1:length(mm),
  yd(ii)=sum(days(1:mm(ii)-1))+dd(ii)-1;
end
yd = yd+(hh/24+minit/24/60+ss/24/60/60)';

% get the whole days
ddd=[min(floor(yd)):max(floor(yd))];
    
% get the indices of the points nearest the whole and half days
for ii = 1:length(ddd)
  x = find(min(abs(yd-ddd(ii))) == abs(yd-ddd(ii)));
  aa(ii) =x(1);
  x = find(min(abs(yd-ddd(ii)+.5)) == abs(yd-ddd(ii)+.5));
  bb(ii) =x(1);
end

hhh=plot(lon(aa),lat(aa),'k+');
set(hhh,'markersize',10);
text(lon(aa)',lat(aa)',int2str(ddd'),'fontweight','bold')
%hhh=plot(lon(bb),lat(bb),'kx');
%set(hhh,'markersize',10);

lon4=abs(lon(end));
lonmin4=(lon4-floor(lon4))*60;
lon4=floor(lon4);
lat4=lat(end);
latmin4=(lat4-floor(lat4))*60;
lat4=floor(lat4);
% string for legend
str4 = [int2str(lat4),setstr(176),num2str(latmin4),''' N ', ...
		int2str(lon4),setstr(176),num2str(lonmin4),''' W'];

title(['Drifter position as of ',s2{end},'(Day ',int2str(yd(end)),')', ...
       '  ',s3{end},'UTC'])      
xlabel('lon')
ylabel('lat')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% now add the drifter deployed on oc415-3 (52486)

[sn, val, val1, val2, s1n, val3, s2n, s3n, lt, ln, n, val4]= ...
    textread('drft_shp.txt','%d %d %d %d %c %d %s %s %f %f %f %d');
%[sn, val, val1, val2, s1n, val3, s2n, s3n, lt, ln, n]= ...
%textread('drft_shp.txt','%d %d %d %d %c %d %s %s %f %f %f');

hold on

h(2)=plot(ln(22:end)-360,lt(22:end),'linewidth',3,'color','k');
plot(ln(end)-360,lt(end),'*','markersize',10);

% generate time vector for data
a=[]; b=[];
for ii = 1:length(s3n)
  a(ii,:)=sscanf(s2n{ii},'%4d%c%2d%c%2d')';
  b(ii,:)=sscanf(s3n{ii},'%2d%c%2d%c%2d')';
end

days=[31 28 31 30 31 30 31 31 30 31 30 31];
mm=a(:,3);
dd=a(:,5);
hh=b(:,1);
minit=b(:,2);
ss=b(:,3);
clear yd
for ii = 1:length(mm),
  yd(ii)=sum(days(1:mm(ii)-1))+dd(ii)-1;
end
yd = yd+(hh/24+minit/24/60+ss/24/60/60)';

% get the whole days
ddd=[min(floor(yd)):max(floor(yd))];
    
% get the indices of the points nearest the whole and half days
clear aa bb
for ii = 1:length(ddd)
  x = find(min(abs(yd-ddd(ii))) == abs(yd-ddd(ii)));
  aa(ii) =x(1);
  x = find(min(abs(yd-ddd(ii)+.5)) == abs(yd-ddd(ii)+.5));
  bb(ii) =x(1);
end

hhh=plot(ln(aa)-360,lt(aa),'k+');
set(hhh,'markersize',10);
text(ln(aa)'-360,lt(aa)',int2str(ddd'))
%hhh=plot(ln(bb)-360,lt(bb),'kx');
%set(hhh,'markersize',10);

lon6=abs(ln(end)-360);
lonmin6=(lon6-floor(lon6))*60;
lon6=floor(lon6);
lat6=lt(end);
latmin6=(lat6-floor(lat6))*60;
lat6=floor(lat6);
str6 = [int2str(lat6),setstr(176),num2str(latmin6),''' N ', ...
		int2str(lon6),setstr(176),num2str(lonmin6),''' W'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% now add the drifter deployed during oc415-4 (52485)
[sn5,val5,val15,val25,s1n5,val35,s2n5,s3n5,lt5,ln5,n5,val45] ...
    = textread('drift_52485.txt','%d %d %d %d %c %d %s %s %f %f %f %d');
%[sn, val, val1, val2, s1n, val3, s2n, s3n, lt, ln, n]=  ...
%textread('drift_52485.txt','%d %d %d %d %c %d %s %s %f %f %f');

h(3)=plot(ln5(1:end)-360,lt5(1:end),'linewidth',1.5, ...
	  'color','r','linestyle','--');
plot(ln5(end)-360,lt5(end),'r*','markersize',10);

% generate time vector for data
a=[]; b=[];
for ii = 1:length(s3n5)
  a(ii,:)=sscanf(s2n5{ii},'%4d%c%2d%c%2d')';
  b(ii,:)=sscanf(s3n5{ii},'%2d%c%2d%c%2d')';
end

days=[31 28 31 30 31 30 31 31 30 31 30 31];
mm=a(:,3);
dd=a(:,5);
hh=b(:,1);
minit=b(:,2);
ss=b(:,3);
clear yd
for ii = 1:length(mm),
  yd(ii)=sum(days(1:mm(ii)-1))+dd(ii)-1;
end
yd = yd+(hh/24+minit/24/60+ss/24/60/60)';

% get the whole days
ddd=[min(floor(yd)):max(floor(yd))];
    
% get the indices of the points nearest the whole and half days
clear aa bb
for ii = 1:length(ddd)
  x = find(min(abs(yd-ddd(ii))) == abs(yd-ddd(ii)));
  aa(ii) =x(1);
  x = find(min(abs(yd-ddd(ii)+.5)) == abs(yd-ddd(ii)+.5));
  bb(ii) =x(1);
end

hhh=plot(ln5(aa)-360,lt5(aa),'k+');
set(hhh,'markersize',10);
text(ln5(aa)'-360,lt5(aa)',int2str(ddd'))
%hhh=plot(ln5(bb)-360,lt5(bb),'kx');
%set(hhh,'markersize',10);

lon5=abs(ln5(end)-360);
lonmin5=(lon5-floor(lon5))*60;
lon5=floor(lon5);

lat5=lt5(end);
latmin5=(lat5-floor(lat5))*60;
lat5=floor(lat5);
str5 = [int2str(lat5),setstr(176),num2str(latmin5),''' N ', ...
		int2str(lon5),setstr(176),num2str(lonmin5),''' W'];


ax=axis;
axrat = 1/cos(mean(ax(3:4))*pi/180);
set(gca,'DataAspectRatio',[1 1/axrat 1]);

legend(h,['52484 ',str4],['52486 ',str6],['52485 ',str5])

print -dpng drifter.png
