% plots position of float over time

% loads:
  % loads apex_data.mat (generated by gen_apex_data.m)  
  % 31712.dat (or 1226.dat) to show Greenland and Newfoundland coasts

% no mat files are saved

% written by E. Boss, UMaine, 2004 
% email: emmanuel.boss@maine.edu

clear all
close all
warning off all

load apex_data

for i=1:N %for all files (pos, dat)
    file_i=int2str(i); %change i to string and store as file_i
    n=length(file_i); %check length of file_i
        if n==1 %if 1, 2, 3...9
            name=['pos00',file_i]; %concatenates pos00 and i (pos001) and assigns to array name
            name2=['dat00',file_i]; %concatenates dat00 and i (dat001) and assigns to array name2
            name3=['date00',file_i]; %concatenates date00 and i (date001) and assigns to array name3
        elseif n==2 %if 10, 11, 12...99 
             name=['pos0',file_i]; %concatenates pos0 and i (pos010) and assigns to array name
             name2=['dat0',file_i]; %concatenates dat0 and i (dat010) and assigns to array name2
             name3=['date0',file_i]; %concatenates date0 and i (date010) and assigns to array name3
        else %if 100, 101, 102...999
             name=['pos',file_i]; %concatenates pos and i (pos100) and assigns to array name
             name2=['dat',file_i]; %concatenates dat and i (dat100) and assigns to array name2
             name3=['date',file_i]; %concatenates date and i (date100) and assigns to array name3
        end           
    A=eval(name); %eval(pos001) - takes strings in pos001 (lat, lon, minutes) and changes to numbers, then assigns to array A
    B=eval(name2); %eval(dat001) - takes strings in dat001 (14 fields) and changes to numbers, then assigns to array B
    C=eval(name3); %eval(date001 - takes strings in date001 (date) and changes to numbers, then assigns to array C
    [mm nn]=size(B); %returns number of rows and columns in B (dat001)
    if i==1 %if this is the first time through
        pos=A;
        date=C;
        dat=[B,zeros(mm,1)]; %create array dat from data in dat file with last column of zeros
        serial=ones(size(A),1);
        profile_number=ones(size(B),1);
    else %if not the first time through
        pos=[pos;A];
        date=[date;C];
        serial=[serial;ones(size(A),1)*i];
        profile_number=[profile_number;ones(size(B),1)*i];
        AA=[B,(i-1)*5*ones(mm,1)]; %create array AA from file and replace/increment last column to get profile stages
        dat=[dat;AA]; %append AA to dat
    end
end

% remove negative values (no data collected)
I=find(dat<0);
dat(I)=NaN;

% other variables (not used in plots) are:
% P=dat(:,1); %depth (dB)
% T=dat(:,2); %temperature (degrees C)
% S=dat(:,3); %salinity (psu)
% O=dat(:,4);
% BlueRef=dat(:,5);
% BlueSig=dat(:,6);
% LSSRef=dat(:,7);
% LSSSig=dat(:,8);
% theta=dat(:,9); %potential temperature (degrees C)
% sigma=dat(:,10); %density-1000 (kg/m3)
% O2sat=dat(:,11); %oxygen (% saturation) !oxygen sensor not working
% O2=dat(:,12); %oxygen (um/kg) !oxygen sensor not working
% Chl=dat(:,13); %chlorophyll (mg/m3, not calibrated)
% B_LSS=dat(:,14); %bb (m-1, not calibrated)

% get single date per profile
I=find(diff(date(:,1))>4);
date_plot=[date(1,1);date(I+1,1)];
pos_plot=[pos(1,:);pos(I+1,:)];

% get time to plot (does by assuming one date every 5 days - not taken from files)
tt=date_plot;
t=datestr(tt, 'mm');
A=load('31712.dat'); %use this data file for small map
%B=load('1226.dat'); %use this data file for large map

%%%%%%%%%%%%%%%%%%%%%%% PLOTS

% plot trajectory in grey according to time
figure(1);
plot(A(:,1),A(:,2),'k')
hold on
colormap(gray);
scatter(pos_plot(:,1)-60,pos_plot(:,2),5,(tt),'filled');
colorbar;
cbar_axes=colorbar();
yTick=get(cbar_axes,'YTick')';
yLabel=datestr(yTick,'mm/dd/yy');
ylim([40 65]);
%xlim([-61 0]); %coordinates if using 1226.dat
colorbar('YTickLabel',[]);
colorbar('YTickLabel',yLabel);
xlabel('Longitude')
ylabel('Latitude')
title('Surface Positions of Float Through Time')
hold off

% draw arrows
annotation('arrow',[0.17 0.17],[0.7988 0.8789]); % north arrow

% write text
annotation('textbox','Position',[0.15 0.7123 0.0754 0.08826],'LineStyle','none',...
  'FontName','Times New Roman','String',{'N'},'FitHeightToText','on');
annotation('textbox','Position',[0.3739 0.8274 0.1296 0.0708],'LineStyle','none',...
  'FitHeightToText','off','FontName','Times New Roman','String',{'Greenland'});
annotation('textbox',...
  'Position',[0.1445 0.255 0.1769 0.06292],'LineStyle','none','FitHeightToText','off',...
  'FontName','Times New Roman','String',{'Newfoundland'});

% plot trajectory w/ color according to time
figure('Units','inches','Resize','off'); 
% axes1 = axes('Position',[0.131 0.167 0.598 0.757]); %position of plot w/colorbar
axes1 = axes('Position',[0.131 0.18 0.7352 0.757]); %position of plot w/out colorbar
plot(A(:,1),A(:,2),'k','Parent',axes1);
hold on
% h=scatter(pos_plot(:,1)-60,pos_plot(:,2),5,(tt),'filled','Parent',axes1); %colors
 h=scatter(pos_plot(:,1)-60,pos_plot(:,2),5,(tt),'k','filled','Parent',axes1); %greyscale
hChildren = get(h, 'Children');
set(hChildren, 'Markersize',3)
box on; %sets box around graph
set(gcf,'paperposition',[1.5 3.5 3.5 2.5]); %sets location and size on print out - left, bottom, width, height
set(gcf,'position',[0 0 4.75 3.75]); %sets window location and size on figure for printing (eos figure)
ylim([40 65]);
% colorbar(axis,[0.754 0.1674 0.03274 0.7406],'YTickLabel',{'10 Sep 04' '29 Mar 05' '15 Oct 05' '03 May 06' '19 Nov 06' '07 Jun 07'});
set(gca,'FontName','Times New Roman','FontSize',10);
set(gca,'LineWidth',0.75);
xlabel('Longitude');
ylabel('Latitude');

% draw scale w/colorbar
% annotation('line',[0.2988 0.2988],[0.2388 0.2640]);
% annotation('line',[0.1989 0.1989],[0.2388 0.2640]);
% annotation('line',[0.1989 0.2988],[0.2640 0.2640]);
% annotation('textbox','Position',[0.1667 0.118 0.2768 0.1345],'LineStyle','none','FontName','Times New Roman','String',{'0    500 km'},'FitHeightToText','on');

% draw scale w/out colorbar
annotation('line',[0.1989 0.1989],[0.2388 0.2640]);
annotation('line',[0.3199 0.3199],[0.2388 0.2640]);
annotation('line',[0.1989 0.321],[0.2640 0.2640]);
annotation('textbox','Position',[0.1667 0.118 0.2768 0.1345],'LineStyle','none',...
   'FontName','Times New Roman','String',{'0        500 km'},'FitHeightToText','on');

% draw arrows
annotation('arrow',[0.6799 0.6805],[0.6256 0.5291]); %particle eddy arrow
% annotation('arrow',[0.3452 0.3452],[0.6867 0.5536]); %start arrow
% annotation('textarrow',[0.6756 0.6756],[0.6652 0.3991],'String',{}); %end arrow
annotation('arrow',[0.8006 0.8004],[0.8158 0.8959]); %north arrow
annotation('arrow',[0.5692 0.5679],[0.3118 0.4394]); %current arrow

% write text
annotation('textbox','Position',[0.7742 0.7288 0.0754 0.08876],'LineStyle','none',...
  'FontName','Times New Roman','String',{'N'},'FitHeightToText','on');
  
annotation('textbox','Position',[0.1574 0.7204 0.1786 0.111],'LineStyle','none',...
  'FontName','Times New Roman','HorizontalAlignment','center','String',{'Labrador','Sea'},'FitHeightToText','on');

% annotation('textbox','Position',[0.3407 0.5447 0.09132 0.08092],'LineStyle','none','FitHeightToText','off',...
%   'FontName','Times New Roman','HorizontalAlignment','center','String',{'start'});

annotation('textbox','Position',[0.3587 0.4834 0.09722 0.1143],'LineStyle','none',...
  'FontName','Times New Roman','FontSize',16,'String',{'X'},'FitHeightToText','on');

annotation('textbox','Position',[0.319 0.5515 0.1433 0.1341],'LineStyle','none',...
  'FontName','Times New Roman','HorizontalAlignment','center','String',{'06/17/04','start'},'FitHeightToText','on');

% annotation('textbox','Position',[0.7621 0.3761 0.1038 0.08876],'LineStyle','none',...
%   'FontName','Times New Roman','String',{'06/07'},'FitHeightToText','on');

annotation('textbox','Position',[0.7828 0.3149 0.09722 0.1143],'LineStyle','none',...
  'FontName','Times New Roman','FontSize',16,'String',{'X'},'FitHeightToText','on');

annotation('textbox','Position',[0.7224 0.3816 0.1433 0.1341],'LineStyle','none',...
  'FontName','Times New Roman','HorizontalAlignment','center','String',{'06/22/07','end'},'FitHeightToText','on');

% annotation('textbox','Position',[0.6151 0.6056 0.131 0.08876],'LineStyle','none','FontName','Times New Roman',...
%   'HorizontalAlignment','center','HorizontalAlignment','center','String',{'11/06'},'FitHeightToText','on');

annotation('textbox','Position',[0.5957 0.6271 0.1786 0.111],'LineStyle','none','FitHeightToText','off',...
  'HorizontalAlignment','center','FontName','Times New Roman','String',{'11/06','eddy'});
 
annotation('textbox','Position',[0.5186 0.1628 0.242 0.1794],'LineStyle','none','FontName','Times New Roman',...
  'HorizontalAlignment','center','String',{'09/05/06','North Atlantic','Current crossing'},'FitHeightToText','on');

% annotation('textbox',...
%   'Position',[0.4752 0.2175 0.3095 0.09013],'LineStyle','none','FitHeightToText','off',...
%   'FontName','Times New Roman','HorizontalAlignment','center','String',{'North Atlantic','Current crossing'});

% annotation('textbox',...
%   'Position',[0.4536 0.1745 0.3482 0.09013],'LineStyle','none','FitHeightToText','off',...
%   'FontName','Times New Roman','HorizontalAlignment','center','String',{'Current crossing'});

% annotation('textbox',...
%   'Position',[0.144 0.3184 0.22 0.06292],'LineStyle','none','FitHeightToText','off',...
%   'FontName','Times New Roman','String',{'Newfoundland'});

% annotation('textbox','Position',[0.5804 0.7999 0.152 0.0708],'LineStyle','none',...
%   'FitHeightToText','off','FontName','Times New Roman','String',{'Greenland'});
 
% title('Surface Positions of Float Through Time')
hold off


