r_combine_results.m 7.73 KB
function r_combine_results(nbF)
% R_COMBINE_RESULTS - Combine the result files from individual rows into
% a single file.
%
% Syntax:  r_combine_results(nbF)
%
% Inputs:
%    nbF       - The number of floes (40 or 80)
%
% Example:
%    r_combine_results(80)
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: data_$expNumber_$camNumber.mat (produced by R_RECON)
%                     list1.mat
%
% See also: R_RECON, R_ISOLATE, R_LIST
%
% Author: Dany Dumont
% UQAR/ISMER
% email: dany_dumont@uqar.ca
% Website: http://www.ismer.ca/dumont-dany
% November 2017
% ______________________________________________________________________

nbFStr  = num2str(nbF);
if nbF == 80
    e1 = 201307241037; % same experiment as 201307241018
    e2 = 201307241056; % same experiment as 201307241018
elseif nbF == 40
    e1 = 201307251513; % same experiment as 201307251413
    e2 = 201307251532; % same experiment as 201307251413
end


%% Open files
rootdir  = r_root(nbF);
infofile = [rootdir,'/info.mat'];
listfile = [rootdir,'/list1.mat'];
load(infofile)
load(listfile)

fps      = info.fps;
ave      = info.average;
type     = info.waveType;
res      = info.result;
figsdata = info.figsData;
fpsStr   = num2str(fps);
aveStr   = num2str(ave);

% Initializing arrays
nRows        = 16;
nFloesPerRow = 5;
% set to 5 also when nbF = 40 in which we pad with nan's

xFloe = nan.*ones(nFloesPerRow,nRows);
xRow  = nan.*ones(1,nRows);
for r = 1:nRows
    xFloe(:,r) = 0.5 + r - 1;
    xRow (r)   = 0.5 + r - 1;
end

counter = 0;
first   = 1;
%% Different figures, experiment by experiment.
for i = 1 : length(list1.exp)
    if list1.nf(i)==nbF && list1.wt(i)==type && ...
            list1.exp(i)~=e1 && list1.exp(i)~=e2
        
        counter = counter + 1;
        disp(['counter = ', num2str(counter)])
        
        % Initializing
        yi_avg = nan.*ones(nFloesPerRow,nRows);
        yi_std = nan.*ones(nFloesPerRow,nRows);
        yf_avg = nan.*ones(nFloesPerRow,nRows);
        yf_std = nan.*ones(nFloesPerRow,nRows);
        dy_avg = nan.*ones(nFloesPerRow,nRows);
        dy_std = nan.*ones(nFloesPerRow,nRows);
        dyrow_avg = nan.*ones(1,nRows);
        dyrow_std = nan.*ones(1,nRows);
        
        % Data from list1
        expNumber = list1.exp(i);
        exp       = num2str(expNumber);
        
        for c = 1:4
            for rr = 1:4
                r = (c-1)*4 + rr;    % row number (1:16)
                
                %disp([' reading exp ',exp,' row number ',num2str(n)])
                
                cam       = num2str(c);
                row       = num2str(rr);
                expdir    = [rootdir,'/',exp,'_multicam'];
                resdir    = [expdir,'/',fpsStr,'fps_',nbFStr,'f_',aveStr,res];
                datafile  = [resdir,figsdata,fpsStr,'fps_',nbFStr,'f_',...
                    aveStr,'_',exp,'_',cam,'_',row,'.mat'];
                
                if exist(datafile,'file')
                    clear iPos iStdPos fPos fStdPos delta deltaStd ...
                        floeF meanDisp meanStd
                    load(datafile)
                    disp(['  ',num2str(i),'  ',num2str(r)])
                else
                    clear iPos iStdPos fPos fStdPos delta deltaStd ...
                        floeF meanDisp meanStd
                    disp([' * file for ',exp,' cam ',cam,' row ',row,' does not exist'])
                    continue
                end
                
                if first
                    x      = nan.*ones(nFrames,nFloesPerRow,nRows);
                    y      = nan.*ones(nFrames,nFloesPerRow,nRows);
                    rafted = nan.*ones(nFrames,nFloesPerRow,nRows);
                    floeDist = nan.*ones(nFrames,nFloesPerRow - 1,nRows);
                    yRaft  = nan.*ones(nFrames,nFloesPerRow - 1,nRows);
                    t      = 0:1/fps:nFrames*fps - 1;
                    nFramesMin = nFrames;
                    nt     = nFrames;
                    first  = 0;
                end
                
                nFramesMin = min(nFramesMin,nFrames);
                
                if nObjects == 5
                    % time evolving floe position
                    x(:,:,r)               = 0.5 + r - 1;
                    y(1:size(floeF,2),:,r) = floeF';
                    yRaft(1:nt,:,r) = 0.5.*(y(1:nt,2:5,r) + y(1:nt,1:4,r));
                    % floe mean displacements
                    yi_avg(:,r) = iPos;
                    yi_std(:,r) = iStdPos;
                    yf_avg(:,r) = fPos;
                    yf_std(:,r) = fStdPos;
                    dy_avg(:,r) = delta;
                    dy_std(:,r) = deltaStd;
                    % floe rafting
                    cutoff = 0.97;
                    floeDist(1:nt,:,r) = abs(squeeze(y(1:nt,1:end-1,r)) - ...
                        squeeze(y(1:nt,2:end,r)));
                    raftIntf = floeDist;
                    raftIntf(floeDist > cutoff)  = 0;
                    raftIntf(floeDist <= cutoff) = 1;
                    for m = 1:nFramesMin
                        tmp1 = squeeze(raftIntf(m,:,r));
                        tmp2 = inv_and(tmp1);
                        rafted(m,:,r) = tmp2;
                    end
                elseif nObjects == 3
                    % floe time evolving displacement
                    x(:,:,r)                     = 0.5 + r - 1;
                    y(1:size(floeF,2),[1 3 5],r) = floeF';
                    % floe mean displacements
                    yi_avg([1 3 5],r) = iPos;
                    yi_std([1 3 5],r) = iStdPos;
                    yf_avg([1 3 5],r) = fPos;
                    yf_std([1 3 5],r) = fStdPos;
                    dy_avg([1 3 5],r) = delta;
                    dy_std([1 3 5],r) = deltaStd;
                elseif nObjects == 2
                    % floe time evolving displacement
                    x(:,:,r)                   = 0.5 + r - 1;
                    y(1:size(floeF,2),[2 4],r) = floeF';
                    % floe mean displacements
                    yi_avg([2 4],r) = iPos;
                    yi_std([2 4],r) = iStdPos;
                    yf_avg([2 4],r) = fPos;
                    yf_std([2 4],r) = fStdPos;
                    dy_avg([2 4],r) = delta;
                    dy_std([2 4],r) = deltaStd;
                end
                
                % row displacements
                dyrow_avg(r) = meanDisp;
                dyrow_std(r) = meanStd;
                
            end
        end
        
        %% Identifying rafting events
        
        S(counter).hs        = list1.hs(i);
        S(counter).tp        = list1.tp(i);
        S(counter).exp       = list1.exp(i);
        S(counter).test      = list1.test(i);
        S(counter).nFrames   = nFramesMin;
        S(counter).x         = squeeze(x(1:nFramesMin,:,:));
        S(counter).y         = squeeze(y(1:nFramesMin,:,:));
        S(counter).t         = squeeze(t(1:nFramesMin));
        S(counter).xFloe     = xFloe;
        S(counter).xRow      = xRow;
        S(counter).yi_avg    = yi_avg;
        S(counter).yi_std    = yi_std;
        S(counter).yf_avg    = yf_avg;
        S(counter).yf_std    = yf_std;
        S(counter).dy_avg    = dy_avg;
        S(counter).dy_std    = dy_std;
        S(counter).dyrow_avg = dyrow_avg;
        S(counter).dyrow_std = dyrow_std;
        if nbF == 80
            %floeDist = abs(squeeze(y(:,1:end-1,:)) - squeeze(y(:,2:end,:)));
            %floeDist(floeDist > 0.97) = nan;
            %raftOn = floeDist./floeDist;
            S(counter).yRaft    = squeeze(yRaft(1:nFramesMin,:,:));
            S(counter).floeDist = squeeze(floeDist(1:nFramesMin,:,:));
            S(counter).raftIntf = squeeze(floeDist(1:nFramesMin,:,:));
            S(counter).rafted   = squeeze(rafted(1:nFramesMin,:,:));
        end
        
    end
end
save([rootdir,'/data_',nbFStr,'f.mat'], 'S');

end