clear all
close('all')

load apex_data
for i=1:N %for files 1 through end - N from gen_apex_data
    file_i=int2str(i); %change i to string and store as file_i
    n=length(file_i); %gets length of string file_i
        if n==1 %if length is 1 (i is 1,2,3....9)
            name2=['dat00',file_i]; %concatenate dat00 and file_i (dat001, dat002, dat003...dat009) and store in array name2
        elseif n==2 %if length is 2 (i is 10, 11, 12...99)
             name2=['dat0',file_i]; %concatenate dat0 and file_i (dat010, dat011, dat012...dat099) and store in array name2
        else %if length is 2 (i is 100, 101, 102...999)
             name2=['dat',file_i]; %concatenate dat and file_i (dat100, dat101, dat102...dat999) and store in array name2
        end           
    B=eval(name2); %takes strings in dat001 (the 14 fields) and changes to numbers, then assigns to array B
    [mm nn]=size(B); %returns number of rows and columns in B (dat001)
    if i==1 %if this is the first time through
        dat=[B,zeros(mm,1)]; %make array dat with B and one extra column of zeros (for as many rows as there are in B)
    else %if this is not the first time through
        AA=[B,(i-1)*5*ones(mm,1)]; %make array AA with B and one extra column of ?? time from start
        dat=[dat;AA]; %append array AA to dat and make array AA - should have 14 data fields plus time increment from all dat files in one array AA
    end
end
%
tt=datenum(2005,8,25)+dat(:,17); %time, along x axis - change date to num, add time interval from column 15, and assign to variable tt
t=168+dat(:,17); %add 168 and time interval from column 15 and assign to variable t
I=find(dat<-100); %find data <-100 (ie, the original *, which were changed to -999 in rd_apex.m)
dat(I)=NaN; %set I to NaN
%get data and apply corrections, if necessary
P=dat(:,1);
T=dat(:,2);
S=dat(:,3);
O=dat(:,4);
BlueRef=dat(:,5);
FSig=dat(:,6); % on F0005 this row is BlueSig=dat(:,6);
NtuReg=dat(:,7); % on F0005 this row is LSSRef=dat(:,7);
NtuSig=dat(:,8); % on F0005 this row is LSSSig=dat(:,8);
Tsig=dat(:,9); % on F0005 this row doesn't exist
theta=dat(:,10); % mistakenly (:,9)
sigma=dat(:,11); % mistakenly (:,10) etc...
O2sat=dat(:,12);
O2=dat(:,13);
Chl=dat(:,14);
B_LSS=dat(:,15);
% B_LSS_b=(LSSSig-51.8)*0.00004637;
% cp=B_LSS*63;
% cp_b=B_LSS_b*63;
% Chl_b=(BlueSig-29)*0.01079772; %should this be Chl_b=(BlueSig-51)*0.0149
% POC_b=10000*B_LSS_b;
% chl_corr=(Chl-50)*0.0149; % wetlabs calibration
chl_corr=Chl;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% plots
figure(1); % density
%subplot(2,2,1)
scatter(tt,-P,5,sigma,'filled'),colorbar; %scatter plot - SCATTER(X,Y,S,C)
xlim([min(tt),max(tt)]);
ylim([-200,max(-P)]);
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',10);
% set(gca,'xtick',[732464 732829 733194 733560]);
datetick(gca,'x','mmmyy','keepticks');
title('Density-1000 [kg m^{-3}]'); %specifies title
ylabel('depth [db]'); %specifies y axis label

figure(2); % temperature
%subplot(2,2,2)
scatter(tt,-P,5,theta,'filled'),colorbar;
xlim([min(tt),max(tt)]);
ylim([-200,max(-P)]);
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',10);
% set(gca,'xtick',[732464 732829 733194 733560]);
datetick(gca,'x','mmmyy','keepticks');
title('Potential temperature [^\oC]');
ylabel('depth [db]');

figure(3); % salinity
%subplot(2,2,2)
scatter(tt,-P,5,S,'filled'),colorbar;
xlim([min(tt),max(tt)]);
ylim([-200,max(-P)]);
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',10);
% set(gca,'xtick',[732464 732829 733194 733560]);
datetick(gca,'x','mmmyy','keepticks');
title('Salinity [PSU]');
ylabel('depth [db]');

figure(4); % chlorophyll
%subplot(2,2,3)
scatter(tt,-P,5,chl_corr,'filled'),colorbar;
xlim([min(tt),max(tt)]);
ylim([-200,max(-P)]);
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',10);
% set(gca,'xtick',[732464 732829 733194 733560]);
datetick(gca,'x','mmmyy','keepticks');
title('Chlorophyll [mg m^{-3}]');
ylabel('depth [db]');

% figure(5); % backscattering, sensor no longer working
% %subplot(2,2,4)
% scatter(tt,-P,5,B_LSS,'filled'), colorbar;
% xlim([min(tt),max(tt)]);
% ylim([-200,max(-P)]);
% set(gca,'FontName','Times New Roman');
% set(gca,'FontSize',16);
% set(gca,'xtick',[732464 732829 733194]);% title('side scatter [m^{-1}]');
% ylabel('depth [db]');
% xlabel('time [month] starting in 2005');
 
% figure(6); % oxygen saturation, sensor no longer working
% %subplot(2,2,4)
% scatter(tt,-P,5,O2./O2sat*100,'filled'), colorbar;
% xlim([min(tt),max(tt)]);
% ylim([-200,max(-P)]);
% set(gca,'FontName','Times New Roman');
% set(gca,'FontSize',16);
% set(gca,'xtick',[732464 732829 733194]);% title('Oxygen saturation');
% ylabel('depth [db]');

% I=find(P>900);
% figure(7); % five plots on one figure
% subplot(2,3,1) %six (2x3) plots
% %first subplot in position 1 (upper left)
% plot(tt(I),sigma(I),'o');
% datetick('x','mmmyy')
% ylabel('density-1000 [kg]');
% %
% subplot(2,3,2) %second subplot in position 2 (upper middle)
% plot(tt(I),theta(I),'o');
% datetick('x','mmmyy')
% ylabel('potential temperature [C]')
% %
% subplot(2,3,3) %third subplot in position 3 (upper right)
% plot(tt(I),S(I),'o');
% datetick('x','mmmyy')
% ylabel('salinity [psu]')
% %
% %subplot(2,3,4)
% %plot(tt(I),O2(I)./O2sat(I)*100,'o');
% %datetick('x','mmmyy')
% %ylabel('O2 saturation')
% %
% subplot(2,3,4) %fourth subplot in position 4 (lower left)
% plot(tt(I),chl_corr(I)/3,'o');
% datetick('x','mmmyy')
% ylabel('chlorophyll [mg/m^3]')
% %
% subplot(2,3,5) %fifth subplot in position 5 (lower middle)
% plot(tt(I),B_LSS(I),'o');
% datetick('x','mmmyy')
% ylabel('side scatter [m^{-1}]')
% I=find(P>2 & P<30 & isfinite(B_LSS_b));
% min(chl_corr(I));
% %end