% map_argos_blair
%
% 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
% modified by Blair Greenan 01 Sep 2005
%

days_to_save = 100;

%%%%%%%%%%%%%% LOAD THE 52484 POSITION DATA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load the ARGOS beacon 52484 position data for the drouge deployed at the end of Tracer 1
[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');


%%%%%%%%%%%%%% RESAMPLE THE TIME SERIES TO REMOVE BAD/DUPLICATE POINTS %%%%%%%%%%%%%%%%%%%%%%
% convert longitude to degrees west
ln=lon-360;
% eliminate some bad data points from the time series
lx=find(ln< -67);
% resample longitude and latitude to account for removal of bad data points
Lonlx = ln(lx);
Latlx = lat(lx);

% Cull the date and time from the positions file
for mm = 1:length(s1)
    dtnum(mm) = datenum([s2{mm} ' ' s3{mm}],'yyyy-mm-dd HH:MM:SS');
end
% remove the date/time points that correspond to -67deg longitude cut off
dtnum = dtnum(lx);

% remove duplicate ARGOS fixes from the time series and use the index ind1 from this
% to resample the longitude and latitude time series
[unique_dtnum, ind1, ind2]  = unique(dtnum);
unique_Lonlx = Lonlx(ind1);
unique_Latlx = Latlx(ind1);


%%%%%%%%%%%%%% FILTER THE TIME SERIES TO REMOVE HIGH FREQUENCY NOISE %%%%%%%%%%%%%%%%%%%%%%
% filter the longitude and latitude time series to remove some of the high frequency noise
% run a 5-point median filter for the data
Lon = medfilt1(unique_Lonlx,5);
Lat = medfilt1(unique_Latlx,5);
% 3-point boxcar filter for data
for kk = 2:length(unique_Lonlx)-1
    Lonmean(kk-1) = mean(unique_Lonlx(kk-1:kk+1));
    Latmean(kk-1) = mean(unique_Latlx(kk-1:kk+1));
end

%%%%%%%%%%%%%% CONVERT DATE TO YEAR DAY FOR 2005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
frstday=datenum(2005,1,1,0,0,0);
netda=unique_dtnum-frstday;
netda=netda+1;

ikeep = find(unique_dtnum>=now+3/24-days_to_save);
netda = netda(ikeep);
Lon = Lon(ikeep);
Lat = Lat(ikeep);

%%%%%%%%%%%%% OUTPUT FILTERED TIME SERIES TO FILE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('Drogue_52484_smoothed.txt','w');
for pp = 1:length(Lat)
    fprintf(fid,'%f %f %f \n',netda(pp),Lon(pp),Lat(pp));
end
fclose(fid);

%%%%%%%%%%%%% PLOT THE POSITION RESULTS  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
h(1)=plot(unique_Lonlx,unique_Latlx,'b');
hold on
plot(ln(lx(end)),lat(lx(end)),'*','markersize',10)
plot(Lon,Lat,'r-')
hold on
%plot(Lonmean,Latmean,'g-')
lon4=abs(ln(lx(end)));
lonmin4=(lon4-floor(lon4))*60;
lat4=lat(end);
latmin4=(lat4-floor(lat4))*60;
%str4 = [int2str(lat4),setstr(176),num2str(latmin4),' N ', ...
%		int2str(lon4),setstr(176),num2str(lonmin4),' W'];
str4 = [int2str(lat4),'\circ ',num2str(latmin4),' N ', ...
		int2str(lon4),'\circ ',num2str(lonmin4),' W'];

title(['Drifter position as of ',s2{end},' ',s3{end},'UTC'])      
xlabel('Longitude')
ylabel('Latitude')

%%%%%%%%%%%%%%%%%% CLEAR VARIABLES BEFORE MOVING ON TO NEXT DROGUE %%%%%%%%%%%%%%%%%%%
% need to keep axis handle and str4 for the legend at the end of this m-file
clear w; w = who;
for i = length(w):-1:1
   if (~strcmp('str4',w{i}) & ~strcmp('h',w{i}) &...
	 ~strcmp('days_to_save',w{i})),
      eval(['clear ', w{i}]);
   end
end





%%%%%%%%%%%%%% LOAD THE 52486 POSITION DATA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% now add the drifter we deployed (52486)
[sn, val, val1, val2, s1, val3, s2, s3, lat, lon, n, val4] = ...
    textread('drft_shp.txt','%d %d %d %d %c %d %s %s %f %f %f %d');


%%%%%%%%%%%%%% RESAMPLE THE TIME SERIES TO REMOVE BAD/DUPLICATE POINTS %%%%%%%%%%%%%%%%%%%%%%
Lonlx = lon(22:end)-360; % first 22 points seem to be bad
Latlx = lat(22:end);

for mm = 22:length(s1)
    dtnum(mm) = datenum([s2{mm} ' ' s3{mm}],'yyyy-mm-dd HH:MM:SS');
end
dtnum(1:21) = []; 
[unique_dtnum, ind1, ind2]  = unique(dtnum);
unique_Lonlx = Lonlx(ind1);
unique_Latlx = Latlx(ind1);


%%%%%%%%%%%%%% FILTER THE TIME SERIES TO REMOVE HIGH FREQUENCY NOISE %%%%%%%%%%%%%%%%%%%%%%
% 5-point median filter for the data
Lon = medfilt1(unique_Lonlx,5);
Lat = medfilt1(unique_Latlx,5);

% 3-point boxcar filter for data
for kk = 2:length(unique_Lonlx)-1
    Lonmean2(kk-1) = mean(unique_Lonlx(kk-1:kk+1));
    Latmean2(kk-1) = mean(unique_Latlx(kk-1:kk+1));
end


%%%%%%%%%%%%%% CONVERT DATE TO YEAR DAY FOR 2005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
frstday=datenum(2005,1,1,0,0,0);
netda=unique_dtnum-frstday;
netda=netda+1;

ikeep = find(unique_dtnum>=now+3/24-days_to_save);
netda = netda(ikeep);
Lon = Lon(ikeep);
Lat = Lat(ikeep);


%%%%%%%%%%%%% OUTPUT FILTERED TIME SERIES TO FILE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('Drogue_52486_smoothed.txt','w');
for pp = 1:length(Lat)
    fprintf(fid,'%f %f %f \n',netda(pp),Lon(pp),Lat(pp));
end
fclose(fid);


%%%%%%%%%%%%% PLOT THE POSITION RESULTS  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
h(2)=plot(Lonlx, Latlx,'linewidth',3,'color','k');
plot(Lonlx(end),Latlx(end),'*','markersize',10);
hold on
plot(Lon,Lat,'r-')
hold on
%plot(Lonmean2,Latmean2,'m.-')
lon6=abs(lon(end)-360);
lonmin6=(lon6-floor(lon6))*60;
lat6=lat(end);
latmin6=(lat6-floor(lat6))*60;
str6 = [int2str(lat6),'\circ ',num2str(latmin6),' N ', ...
		int2str(lon6),'\circ ',num2str(lonmin6),' W'];


%%%%%%%%%%%%%%%%%% CLEAR VARIABLES BEFORE MOVING ON TO NEXT DROGUE %%%%%%%%%%%%%%%%%%%
% need to keep axis handle and str4 for the legend at the end of this m-file
clear w; w = who;
for i = length(w):-1:1
   if (~strcmp('str4',w{i}) & ~strcmp('str6',w{i}) &...
	 ~strcmp('h',w{i}) & ~strcmp('days_to_save',w{i})),
      eval(['clear ', w{i}]);
   end
end





%%%%%%%%%%%%%% LOAD THE 52485 POSITION DATA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% now add the drifter we deployed (52485)
[sn, val, val1, val2, s1, val3, s2, s3, lat, lon, n, val4] = ...
    textread('drift_52485.txt','%d %d %d %d %c %d %s %s %f %f %f %d');


%%%%%%%%%%%%%% RESAMPLE THE TIME SERIES TO REMOVE BAD/DUPLICATE POINTS %%%%%%%%%%%%%%%%%%%%%%
Lonlx = lon(22:end)-360; % first 22 points seem to be bad
Latlx = lat(22:end);

for mm = 22:length(s1)
    dtnum(mm) = datenum([s2{mm} ' ' s3{mm}],'yyyy-mm-dd HH:MM:SS');
end
dtnum(1:21) = []; 
[unique_dtnum, ind1, ind2]  = unique(dtnum);
unique_Lonlx = Lonlx(ind1);
unique_Latlx = Latlx(ind1);


%%%%%%%%%%%%%% FILTER THE TIME SERIES TO REMOVE HIGH FREQUENCY NOISE %%%%%%%%%%%%%%%%%%%%%%
% 5-point median filter for the data
Lon = medfilt1(unique_Lonlx,5);
Lat = medfilt1(unique_Latlx,5);

% 3-point boxcar filter for data
for kk = 2:length(unique_Lonlx)-1
    Lonmean2(kk-1) = mean(unique_Lonlx(kk-1:kk+1));
    Latmean2(kk-1) = mean(unique_Latlx(kk-1:kk+1));
end


%%%%%%%%%%%%%% CONVERT DATE TO YEAR DAY FOR 2005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
frstday=datenum(2005,1,1,0,0,0);
netda=unique_dtnum-frstday;
netda=netda+1;

ikeep = find(unique_dtnum>=now+3/24-days_to_save);
netda = netda(ikeep);
Lon = Lon(ikeep);
Lat = Lat(ikeep);

%%%%%%%%%%%%% OUTPUT FILTERED TIME SERIES TO FILE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('Drogue_52485_smoothed.txt','w');
for pp = 1:length(Lat)
    fprintf(fid,'%f %f %f \n',netda(pp),Lon(pp),Lat(pp));
end
fclose(fid);


%%%%%%%%%%%%% PLOT THE POSITION RESULTS  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
h(3)=plot(Lonlx, Latlx,'g.','markersize',10);
plot(Lonlx(end),Latlx(end),'*','markersize',10);
hold on
plot(Lon,Lat,'r-')
hold on
%plot(Lonmean2,Latmean2,'m.-')
lon5=abs(lon(end)-360);
lonmin5=(lon5-floor(lon5))*60;
lat5=lat(end);
latmin5=(lat5-floor(lat5))*60;
str5 = [int2str(lat5),'\circ ',num2str(latmin5),' N ', ...
		int2str(lon5),'\circ ',num2str(lonmin5),' W'];


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


%%%%%%%%%%%%%%%% ADD A LEGEND %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
legend(h,['52484 ',str4],['52486 ',str6],['52485 ',str5])
grid on

print -dpng drifter.png
