clear all
close('all')

load apex_msg
%dat compile dat files into one
for i=1:N %for files 1 through end - N from gen_msg_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 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]; %make array dat with B
%    else %if this is not the first time through
%       AA=[B]; %make array AA with B
%       dat=[dat;AA]; %append array AA to dat
%    end
    if i==1 %if this is the first time through
        dat=[B,zeros(mm,5)]; %make array dat with B and five extra columns of zeros (for as many rows as there are in B)
    else %if this is not the first time through
        AA=[B,zeros(mm,5)]; %make array AA with B and five extra columns of zeros
        dat=[dat;AA]; %append array AA to dat and make array AA - should have 6 data fields plus empty columns
    end
end
%stat combine stat files into one
for i=1:N
    file_i=int2str(i);
    n=length(file_i);
        if n==1
             name2=['stat00',file_i];
        elseif n==2
             name2=['stat0',file_i];
        else
             name2=['stat',file_i];
        end
    B=eval(name2);
    [mm nn]=size(B);
    if i==1
        stat=[B];
    else
       AA=[B];
       stat=[stat;AA];
    end
end
%lat combine lat files into one
for i=1:N
    file_i=int2str(i);
    n=length(file_i);
        if n==1
             name2=['lat00',file_i];
        elseif n==2
             name2=['lat0',file_i];
        else
             name2=['lat',file_i];
        end
    B=eval(name2);
    [mm nn]=size(B);
    if i==1
        lat=[B];
    else
       AA=[B];
       lat=[lat;AA];
    end
end
%lon combine lon files into one
for i=1:N
    file_i=int2str(i);
    n=length(file_i);
        if n==1
             name2=['lon00',file_i];
        elseif n==2
             name2=['lon0',file_i];
        else
             name2=['lon',file_i];
        end
    B=eval(name2);
    [mm nn]=size(B);
    if i==1
        lon=[B];
    else
       AA=[B];
       lon=[lon;AA];
    end
end
%date combine date files into one
for i=1:N
    file_i=int2str(i);
    n=length(file_i);
        if n==1
             name2=['date00',file_i];
        elseif n==2
             name2=['date0',file_i];
        else
             name2=['date',file_i];
        end
    B=eval(name2);
    [mm nn]=size(B);
    if i==1
        date=[B];
    else
       AA=[B];
       date=[date;AA];
    end
end
%date2 combine date2 files into one
for i=1:N
    file_i=int2str(i);
    n=length(file_i);
        if n==1
             name2=['date200',file_i];
        elseif n==2
             name2=['date20',file_i];
        else
             name2=['date2',file_i];
        end
    B=eval(name2);
    [mm nn]=size(B);
    if i==1
        date2=[B];
    else
       AA=[B];
       date2=[date2;AA];
    end
end
%
%add stat, lat, lon, date and date2 to dat
dat(:,7)=str2num(stat); %replaces column 7 zeros with stat
dat(:,8)=str2num(lat); %replaces column 8 zeros with lat
dat(:,9)=str2num(lon);  %replaces column 9 zeros with lon
dat(:,10)=date;  %replaces column 10 with zeros date
dat(:,11)=date2;  %replaces column 11 with zeros date2
%
%compare two dates
%d1=datestr(date + 10);
%d2=datestr(date2 + 10);
%d3=[d1 d2];
% 
%dat column 1: pressure
%dat column 2: F_Ref
%dat column 3: F_Sig (blue sig)
%dat column 4: LSS_Ref
%dat column 5: LSS_Sig (blue sig)
%dat column 6: T_sensor
%dat column 7: stat
%dat column 8: lat
%dat column 9: lon
%dat column 10: date
%dat column 11: date2
%
P=dat(:,1); %get pressure from array dat(column 1)and store in P
FRef=dat(:,2); %F_Ref
Chl=dat(:,3); %F_Sig
LSSRef=dat(:,4); %LSS_Ref
LSSSig=dat(:,5); %LSS_Sig
T=dat(:,6);
stat=dat(:,7);
lat=dat(:,8);
lon=dat(:,9);
date=dat(:,10);
date2=dat(:,11);

% apply calibrations
% B_LSS_b=(LSSSig-51.8)*0.00004637;
% cp_b=B_LSS_b*63;
% Chl_b=(Chl-51)*0.0149;
% POC_b=10000*B_LSS_b;
%run figures

chl_corr=(Chl-50)*0.0149; % wetlabs calibration
% chl_corr=Chl;

figure(1);
plot(date,chl_corr,'.'); %plot
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',10);
% set(gca,'xtick',[732464 732829 733194]);
datetick(gca,'x','mmmyy','keepticks');
% datetick('x','mmmyy');
title({'Chlorophyll','Float 0015, North Pacific'});
ylabel('Concentration [mg m^{-3}]');

% make array of all surface data
float0015SurfaceAll=[date, lat, lon, P, chl_corr, stat]; 

%make array of just date and chlorophyll
float0015SurfaceChl=[date, chl_corr];

% clear unnecessary variables from workspace
clear dat dat0* dat1* dat2* date0* date* pos*;
clear AA B C Chl chl_corr D E F FRef G H LSSRef LSSSig N P T;
clear file_i file_list_out filename filename1 i ii;
clear lat lat0* lat1* lon lon0* lon1* lat2* lon2*;
clear mm n n_file_out name2 nn;
clear stat stat0* stat1* stat2*;

% save all -v6
clear float0015SurfaceAll;

% % save  argos_msg_plots -v6
save float0015Surface -v6
% clear float0015SurfaceChl;
% load all
% clear float0015SurfaceChl;
% save float0015Argos -v6

