% run gen_apex_data first (calls rd_apex), which opens apex edf files, gets
    % data, and saves apex_data.mat

% this m file:
    % loads apex_data.mat 
    % consolidates position data from each pos file into arrays long and lat
    % consolidates data from each dat file with lat/long into array alldat
    % applies satellite calibarations
    % makes array float0005 with profile, date, lat, long, P, theta, S, sigma,
        % B_LSS_b_corr, Chl_b_corr, O2sat, O2, POC_b, and C_phy
    % clears unnecessary variables
    % saves float0005_dilate.mat

% written by E. Boss and L. Taylor, UMaine, 10/19/06

close all
clear 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];
        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];
        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];
        end           
    A=eval(name); % eval(pos001) - takes strings in pos001 (lat, lon, minutes) and changes to numbers, then assigns to array A
    latlong=A(1,1:2); 
    long=[latlong(:,1)-60]; % longitude
    lat=latlong(:,2); % latitude
    B=eval(name2); % data
    C=eval(name3); % date
    [mm nn]=size(B);

    % open each file (pos, dat and date) 000, 001, 000+ in turn
    if i==1 % if first time through (pos001, dat001 and date001)
        dat=[B,zeros(mm,1)]; % create array dat from data in dat file with last column (16) of zeros
        alldat=[dat,zeros(mm,1:2)]; % create array alldat with dat plus columns 17 and 18 of zeros
        alldat(:,17)=long(1,1); % replace column 17 with longitude
        alldat(:,18)=lat(1,1); % replace column 18 with latitude
         
    else %if not the first time around
        AA=[B,(i-1)*5*ones(mm,1)]; % create array AA from file and replace/increment last column (16) to get profile stages 
        dat=[dat;AA]; % append AA to dat
        CC=[AA,zeros(mm,1:2)]; % create array CC from AA, and add two columns of zeros
        CC(:,17)=long(1,1); % replace column 17 with longitude
        CC(:,18)=lat(1,1); % replace column 18 with latitude
        alldat=[alldat;CC]; % append CC to alldat array
    end
end

% remove negative values (no data collected)
I=find(alldat<-100);
alldat(I)=NaN;

%variables are:
P=alldat(:,1); % depth (dB)
T=alldat(:,2); % temperature (degrees C)
S=alldat(:,3); % salinity (psu)
O=alldat(:,4);
BlueRef=alldat(:,5);
BlueSig=alldat(:,6);
LSSRef=alldat(:,7);
LSSSig=alldat(:,8);
theta=alldat(:,9); % temperature (degrees C)
sigma=alldat(:,10); % density-1000 (kg/m3)
O2sat=alldat(:,11); % oxygen (% saturation)
O2=alldat(:,12); % oxygen (um/kg)
Chl=alldat(:,13); % chlorophyll (mg/m3, not calibrated)
B_LSS=alldat(:,14); % bb (m-1, not calibrated)
date=alldat(:,15); % date (datenum format)
profile=alldat(:,16); % profile (in days from start)
long=alldat(:,17); % longitude (degrees)
lat=alldat(:,18); % latitude (degrees)

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

% get profile number
profileNum=profile/5+1; 

% get time beginning June 17, 2004
tt=datenum(2004,6,17)+alldat(:,16);
t=168+alldat(:,16);

% array to use for plotting:
float0005=[profileNum, date, lat, long, P, theta, S, sigma, B_LSS_b_corr, Chl_b_corr, O2sat, O2, POC_b, C_phy, profile];

% clear unnecessary variables from workspace
clear dat dat0* dat1* dat2* date0* date1* date2* pos* alldat;
clear name* filename* file*;
clear A* i* B C CC I N mm n* t;
clear latlong lat long;
clear B_LSS B_LSS_b B_LSS_b_corr BlueRef BlueSig Chl Chl_b Chl_b_corr LSSRef LSSSig O O2 O2sat P POC_b C_phy;
clear S T cp cp_b profile sigma theta date;

save float0005_dilate -v6

