% plots position of float over time
% loads 31712.dat to show Greenland and Newfoundland coasts
% no mat files are saved
% if rerunning for auto colorbar, need to edit colorbar

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 (it doesn't know file names)
            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 dat00 and i (dat001) 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 dat0 and i (dat010) 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 dat and i (dat100) 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 (the 14 fields) and changes to numbers, then assigns to array B
    C=eval(name3);
    [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; %assign A to array pos
        date=C;
        dat=[B,zeros(mm,1)]; %assign B and zeros.... (?)
        serial=ones(size(A),1);
        profile_number=ones(size(B),1);
    else %if not the first time through
        pos=[pos;A]; %take what was in pos and append next A to it
        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)];
        dat=[dat;AA];
    end
end

% get data from dat array
I=find(dat<0);
dat(I)=NaN;

P=dat(:,1); % depth
T=dat(:,2); % temperature
S=dat(:,3); % salinity
O=dat(:,4);
BlueRef=dat(:,5);
BlueSig=dat(:,6);
LSSRef=dat(:,7);
LSSSig=dat(:,8);
theta=dat(:,9);
sigma=dat(:,10); % density
O2sat=dat(:,11);
O2=dat(:,12);
Chl=dat(:,13); % chlorophyll (not calibrated)
B_LSS=dat(:,14); % bb (not calibrated)

% calibrations:
B_LSS_b=(LSSSig-51.8)*0.00004637; % instrument calibration
B_LSS_b_corr=B_LSS_b/4.4260; % strutton calibration
cp=B_LSS*63;
cp_b=B_LSS_b*63;
Chl_b=(BlueSig-25)*0.01079772; % instrument calibration; was 29, 25 is mean of counts at >950m
Chl_b_corr=Chl_b/2.7350; % strutton calibration
POC_b=10000*B_LSS_b;

% clear unecessary variables
% clear A AA B B_LSS B_LSS_b BlueRef BlueSig C Chl Chl_b Chl_b_corr I LSSRef LSSSig;
% clear O O2 O2sat POC_b S cp cp_b cp_b file_i file_list_out filename filename1;
% clear i ii mm n n_file_out name name2 name3 nn dat0* dat1* dat2* pos0* pos1* pos2* sigma;
% clear date0* date1* date2* lat* lon* stat*;

%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 temperature at surface
for j=1:N
    I=find(profile_number==j);
    T_profile=T(I);
    BB_profile=B_LSS_b_corr;
    P_profile=P(I);
    II=find(P_profile==min(P_profile));
     temp_surf(j)=T_profile(II);
     bb_surf(j)=BB_profile(II);
end

% load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\ARGOS\float0005Surface.mat';
% chl_surface(1)=float0005SurfaceChl(1,2);
% time_surface(1)=datenum(2004,6,4);
% I=find(diff(float0005SurfaceChl(:,1))>4);
% for i=1:length(I)
%     chl_surface(1+i)=float0005SurfaceChl(1+I(i),2);
%     time_surface(1+i)=float0005SurfaceChl(1+I(i),1);
% end
% time_float=time_surface;
% chl=chl_surface;

% get time to plot (does by assuming one date every 5 days - not taken from files)
%tt=datenum(2004,6,17)+(serial-1)*5;
tt=date_plot;
t=datestr(tt, 'mm');
% A=load('19623.dat');
A=load('31712.dat');

%%%%%%%%%%%%%%%%%%%%%%% PLOTS
% figure(1) % plots trajectory in gray according to time
% plot(A(:,1),A(:,2),'k')
% hold on
% colormap(gray);
% %scatter(pos(:,1)-60,pos(:,2),5,(tt),'filled');colorbar
% 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]);
% colorbar('YTickLabel',[]);
% colorbar('YTickLabel',yLabel);
% xlabel('Longitude')
% ylabel('Latitude')
% title('Surface Positions of Float Through Time')
% hold off

% figure(2); % plots trajectory w/ color according to time
figure('Units','inches','Resize','off');
axes1 = axes('Position',[0.131 0.167 0.598 0.757]);
plot(A(:,1),A(:,2),'k','Parent',axes1);
hold on
% scatter(pos_plot(:,1)-60,pos_plot(:,2),5,(tt),'filled');
h=scatter(pos_plot(:,1)-60,pos_plot(:,2),5,(tt),'filled','Parent',axes1);
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 3.5 2.5]); % sets window location and size on figure for printing
% yTick=get(cbar_axes,'YTick')'; % turn these on to auto the colorbar by date
% yLabel=datestr(yTick,'dd mmm yy');
% colorbar;
% cbar_axes=colorbar();
% colorbar('YTickLabel',[]);
% colorbar('YTickLabel',{'10 Sep 04' '29 Mar 05' '15 Oct 05' '03 May 06' '19 Nov 06' '07 Jun 07'});
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');

% create scale
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');

% create arrows
annotation('arrow',[0.5774 0.5774],[0.6996 0.4979]); % 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.1997 0.1997],[0.7425 0.8756]); % north arrow
annotation('arrow',[0.4881 0.4881],[0.309 0.4635]); % current arrow

% create textboxes
annotation('textbox','Position',[0.1668 0.621 0.0754 0.1345],'LineStyle','none',...
  'FontName','Times New Roman','String',{'N'},'FitHeightToText','on');
 
annotation('textbox',...
  'Position',[0.1769 0.2704 0.2927 0.1052],'LineStyle','none','FitHeightToText','off',...
  'FontName','Times New Roman','String',{'Newfoundland'});
 
annotation('textbox',...
  'Position',[0.4226 0.2232 0.3095 0.09013],'LineStyle','none','FitHeightToText','off',...
  'FontName','Times New Roman','HorizontalAlignment','center','String',{'North Atlantic'});

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

annotation('textbox','Position',[0.2164 0.7754 0.224 0.1123],'LineStyle','none',...
  'FitHeightToText','off','FontName','Times New Roman','String',{'Greenland'});
 
annotation('textbox','Position',[0.6174 0.35 0.1029 0.1345],'LineStyle','none',...
  'FontName','Times New Roman','String',{'end'},'FitHeightToText','on');
 
annotation('textbox','Position',[0.31 0.57 0.12 0.08092],'LineStyle','none','FitHeightToText','off',...
  'FontName','Times New Roman','String',{'start'});

annotation('textbox','Position',[0.497 0.7425 0.1786 0.124],'LineStyle','none','FitHeightToText','off',...
  'FontName','Times New Roman','String',{'particle'});
 
annotation('textbox','Position',[0.5208 0.6724 0.131 0.1345],'LineStyle','none','FontName','Times New Roman',...
  'String',{'eddy'},'FitHeightToText','on');
 
% title('Surface Positions of Float Through Time')
hold off

% figure(3) % plots trajectory w/ color according to month
% plot(A(:,1),A(:,2),'k')
% hold on
% scatter(pos(:,1)-60,pos(:,2),5,str2num(tt),'filled');colorbar
% plot(pos(1,1)-60,pos(1,2),'k*')
% xlabel('Longitude')
% ylabel('Latitude')
% title('Surface positions of float [color=month of the year]')
% title('Surface Positions of Float 0005')
% hold off

% tt_min=datenum(2006,9,1);
% tt_max=datenum(2006,12,31);
% I=find(tt<tt_max & tt> tt_min);

% figure(4) % plots trajectory w/ color according to time
% plot(A(:,1),A(:,2),'k')
% hold on
% %scatter(pos(:,1)-60,pos(:,2),5,(tt),'filled');colorbar
% scatter(pos_plot(I,1)-60,pos_plot(I,2),5,(tt(I)),'filled');colorbar
% cbar_axes=colorbar();
% yTick=get(cbar_axes,'YTick')';
% yLabel=datestr(yTick,'mm/dd/yy');
% colorbar('YTickLabel',[]);colorbar('YTickLabel',yLabel);
% xlabel('Longitude')
% ylabel('Latitude')
% title('Surface Positions of Float Through Time')
% hold off

