% Import Merged Data -- Requires formatted merged data from Emmanuel Boss's
% merge script.  Also requires input filename string.
% ENA 7/24/2013

%% Import all data from file into matlab and get the easy stuff
datastruct=importdata(filename);
datacell=struct2cell(datastruct); % cells are easier to work with
time=datacell{1}(:,1);
pres=datacell{1}(:,2);
temp=datacell{1}(:,3);
sal=datacell{1}(:,4);
sig=datacell{1}(:,5);
vchl=datacell{1}(:,6);
%% Start on the hard stuff -- find column indexes
cidxc=strfind(datacell{2},'C');  % creates a cell array with a 1 wherever C appears
aidxc=strfind(datacell{2},'A');
betaxc=strfind(datacell{2},'beta');
ecoxc=strfind(datacell{2},'Eco_bb');
cidx=zeros(length(datacell{2}),1);
aidx=cidx;
betax=aidx;
ecox=betax;
for i=1:length(datacell{2})  % converts cell array to matrix. deletes anomalies
    if cidxc{i}==1
        cidx(i)=1;
    end
    if aidxc{i}==1
        aidx(i)=1;
    end
    if betaxc{i}==1
        betax(i)=1;
    end
    if ecoxc{i}==1
        ecox(i)=1;
    end
end
aidx=logical(aidx);  % converts to logical array for logical indexing
cidx=logical(cidx);
betax=logical(betax);
ecox=logical(ecox);
%% Import instrument data
cdata=datacell{1}(:,cidx);
adata=datacell{1}(:,aidx);
betadata=datacell{1}(:,betax);
ecobbdata=datacell{1}(:,ecox);
%% Import text data -- wavelengths, etc
wvla=zeros(size(adata,2),1);
wvlc=wvla;
wvlbeta=zeros(size(betadata,2),1);
ecobbangle=zeros(size(ecobbdata,2),1);
wc=1;
wa=1;
wbeta=1;
weco=1;
for i=1:length(datacell{2})  % searches for text in data, extracts numbers
    if cidx(i)==true
        wvlc(wc)=str2double(datacell{2}{i}(2:end));
        wc=wc+1;
    end
    if aidx(i)==true
        wvla(wa)=str2double(datacell{2}{i}(2:end));
        wa=wa+1;
    end
    if betax(i)==true
        wvlbeta(wbeta)=str2double(datacell{2}{i}(5:end));
        wbeta=wbeta+1;
    end
    if ecox(i)==true
        ecobbangle(weco)=str2double(datacell{2}{i}(8:end));
        weco=weco+1;
    end
end

clear aidx cidx aidxc cidxc betax betaxc ecox ecoxc wa wc wbeta weco datastruct