clear all
close('all')
% run gen_msg_data first (calls rd_msg) to open msg files and get data
% plot_argos_data opens dat files and:
% consolidates data from each into array dat
  % adds stat, lon, lat, date, and date2 data to array dat
  % applies calibrations (dilated)
  % generates parameter vs. incremented time plots
  % saves data to float0005Surface_dilate

load apex_msg % from gen_msg_data
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,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 array dat
dat(:,6)=str2num(stat); %replaces column 6 zeros with stat
dat(:,7)=str2num(lat); %replaces column 7 zeros with lat
dat(:,8)=str2num(lon);  %replaces column 8 zeros with lon
dat(:,9)=date;  %replaces column 9 with zeros date
dat(:,10)=date2;  %replaces column 10 with zeros date2

%compare two dates
%d1=datestr(date + 10);
%d2=datestr(date2 + 10);
%d3=[d1 d2];

P=dat(:,1); %get pressure from array dat(column 1)and store in P
FRef=dat(:,2); %F_Ref
BlueSig=dat(:,3); %F_Sig (blue sig = chlorophyll)
LSSRef=dat(:,4); %LSS_Ref
LSSSig=dat(:,5); %LSS_Sig (backscattering)
stat=dat(:,6); % stat
lat=dat(:,7); % lat
lon=dat(:,8); % lon
date=dat(:,9); % date
date2=dat(:,10); % date2

% calibrations:
B_LSS_b=(LSSSig-51.8)*0.00004637; % instrument calibration
B_LSS_b_corr=B_LSS_b/1.8903; % satellite calibration
Chl_b=(BlueSig-25)*0.01079772; % instrument calibration; 25 is mean of counts at >950m
% Chl_b_corr=Chl_b/5.0359; % satellite calibration
Chl_b_corr=Chl_b/4.9281; % satellite calibration
POC_b=10000*B_LSS_b;

% make array of surface data
float0005Surface=[date, lat, lon, P, Chl_b_corr, B_LSS_b_corr, stat]; 

%make array of just date and chlorophyll
float0005SurfaceChl=[date, Chl_b_corr];

%%%%%%%%%%%%%%%%%%%%%%%%%% PLOTS
% Need to run firstReading and plot chl from that
% figure(1); % plot chlorophyll
% plot(date,Chl_b_corr,'.')
% datetick('x','mmmyy')
% title({'Chlorophyll','Float 0005, North Atlantic'})
% ylabel('Concentration [mg/m^3]')
% xlabel('time [month]')
%%%%%%%%%%%%%%%%%%%%%%%%%%

% Need to plot "surface" bb from surfaceBb.m (profile data) to remove bubbles found in surface (Argos) data
% figure(2); % plot backscattering
% plot(date,B_LSS_b_corr,'.')
% datetick('x','mmmyy')
% title({'Backscatter','Float 0005, North Atlantic'})
% ylabel('LSS_Sig')
% xlabel('time [month]')

% clear unnecessary variables from workspace
clear dat dat0* dat1* dat2* date0* date* pos*;
clear AA B B_LSS_b B_LSS_b_corr BlueSig C Chl_b Chl_b_corr D E F FRef G H LSSRef LSSSig N P;
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* POC_b;

save float0005Surface_dilate -v6

