% This script was written to compute the 3D-Force vector from force
% x, y and z force compartments
% Author: Joschka Wiegleb (2021)

%Title: Flow, force, behaviour: Assessment of a prototype hydraulic barrier for
%invasive fish
%Journal: Hydrobiologia
%Article authors: Joschka Wiegleb, Philipp E. Hirsch, Frank Seidel, Georg Rauter,
%Patricia Burkhardt-Holm
%Corresponding Author: Patricia Burkhardt-Holm, Program
%Man-Society-Environment, Department of Environmental Sciences, University of Basel, Vesalgasse 1, 4051 Basel, Switzerland, patricia.holm@unibas.ch



folders=dir('Filepath')
folderss=struct2table(folders)
foldersss=folderss.name([4:92])
pat=struct2cell(folders)
path=pat{2,1}
ZcorCoeff=0.1742

%%


format long

ProcessInPercent1='start'
ProcessInPerncent1='start'
ProcessInPercent2='start'
ProcessInPercent3='start'
Process2='start'
Process3='start'

for i=67:length(foldersss)
    pathi=fullfile(path,foldersss(i));
    foldersi=dir(char(pathi));
    foldersi2=struct2table(foldersi);
    folderparts=regexp(pathi{1,1},'\','split');
    newpath=fullfile(folderparts{1,1},folderparts{1,2},'00_Analysis\10_CombineForcesOneVector\');
    mkdir(newpath,folderparts{1,3});
    newpathh=fullfile(newpath,folderparts{1,3});
    for v=1:length(foldersi2.name)
        if strlength(foldersi2.name(v)) >= 3 && strlength(foldersi2.name(v))<= 10;
            folPosition(v,:)={foldersi2.name(v)};
        end
    end
    folPositions = folPosition(~cellfun('isempty', folPosition'));
    for ii=1:length(folPositions)
        folderposii=folPositions{ii,1}{1,1};
        mkdir(newpathh,folderposii);
        newpathhh=fullfile(newpathh,folderposii,'/MainVectorAndAngles.csv');
        filesii=fullfile(pathi,folderposii);
        forcefilesii=dir(char(filesii));
        forcefilesiis=struct2table(forcefilesii);
        bytes=max(forcefilesiis.bytes);
        for iin=1:height(forcefilesiis)
            if forcefilesiis.bytes(iin) == bytes
                bytefile=forcefilesiis.name(iin);
            end
        end
        forcefileii=char(bytefile);
        forcedat=char(fullfile(pathi,folderposii,forcefileii));
        forcedatt=importdata(forcedat);
        forcedattt=forcedatt(:,1:60000);
        tt=forcedattt(1,:);
        xx=forcedattt(6,:)*10; 
        yy=forcedattt(5,:)*-10;
        zz=forcedattt(4,:);
        zc=forcedattt(4,:)+ZcorCoeff;
        for vv=1:60000 
            c=sqrt(x(vv)^2+y(vv)^2);
            force_xyI(vv,:)={vv t(vv) c};
        end
        force_xy=cell2table(force_xyI);
        vectorsz=zeros(60000,8);
        vectors=num2cell(vectorsz);
        for vvv=1:60000 
            %-----------
            x=xx(vvv);
            y=yy(vvv);
            la=LengthAngle(x,y,3,'ag');
            c_xy=la{1,1}
            angle_xy=la{1,2}
            
            %-----------
            BetrX=sqrt(^2);
            BetrY=sqrt(y(vvv)^2);
            if BetrX>BetrY
                alpha=asind(BetrY/force_xy.force_xyI3(vvv));
            elseif BetrX<BetrY
                alpha=acosd(BetrX/force_xy.force_xyI3(vvv));
            elseif BetrX==BetrY
                    alpha=0;
            end
            xx=x(:,vvv); 
            yy=y(:,vvv);
            if xx <= 0 && yy >= 0 && BetrX >= BetrY 
                angle_xy=alpha;
            elseif xx <= 0 && yy >= 0 && BetrX <= BetrY
                angle_xy=90-alpha;
            elseif xx >= 0 && yy >= 0 && BetrX <= BetrY
                angle_xy=90+alpha;
            elseif xx >= 0 && yy >= 0 && BetrX >= BetrY
                angle_xy=180-alpha;
            elseif xx >= 0 && yy <= 0 && BetrX >= BetrY
                angle_xy=180+alpha;
            elseif xx > 0 && yy < 0 && BetrX <= BetrY
                angle_xy=275-alpha;
            elseif xx < 0 && yy < 0 && BetrX <= BetrY
                angle_xy=275+alpha;
            elseif xx < 0 && yy < 0 && BetrX >= BetrY
                angle_xy=360-alpha;
            else angle_xy=0
            end
            c_xy=force_xy.force_xyI3(vvv);
            zz=z(1,vvv);
            force_xyz=sqrt(c_xy^2+zz^2);
            BetrXY=sqrt(c_xy^2);
            BetrZ=sqrt(zz^2);
            if BetrXY>=BetrZ
                alpha2=asind(BetrZ/BetrXY);
            elseif BetrXY<BetrZ
                alpha2=acosd(BetrZ/BetrXY);
            end
            if zz >= 0 && BetrXY <= BetrZ
                alphaz=90+alpha2;
            elseif zz >= 0 && BetrXY >= BetrZ
                alphaz=180-alpha2;
            elseif zz <= 0 && BetrXY >= BetrZ
                alphaz=180+alpha2;
            elseif zz < 0 && BetrXY < BetrZ
                alphaz=275-alpha2;
            elseif alpha2==0
                alphaz=0
            else alphaz==0 
            end
            
            zzc=zc(1,vvv);
            force_xyzc=sqrt(c_xy^2+zzc^2);
            BetrXY=sqrt(c_xy^2);
            BetrZc=sqrt(zzc^2);
            if BetrXY>=BetrZc
                alpha2c=asind(BetrZc/BetrXY);
            elseif BetrXY<BetrZc
                alpha2c=acosd(BetrZc/BetrXY);
            end
            if zzc > 0 && BetrXY < BetrZc
                alphazc=90+alpha2c;
            elseif zzc > 0 && BetrXY > BetrZc
                alphazc=180-alpha2c;
            elseif zzc < 0 && BetrXY > BetrZc
                alphazc=180+alpha2c;
            elseif zzc < 0 && BetrXY < BetrZc
                alphazc=275-alpha2c;
            elseif alpha2c==0
                alphazc=0;
            else Status= 'Error line 133c'
            end
            
            tt=t(1,vvv);
            f_xy=force_xyI{vvv,3};
            vectors(vvv,:)={vvv tt f_xy angle_xy force_xyz alphaz force_xyzc alphazc};
            clearvars BetrX BetrY alpha xx yy angle_xy c_xy zz force_xyz BetrXY BetrZ alpha2 alphaz tt f_xy zzc force_xyzc zzc force_xyzc BetrZc alpha2c alphazc
            Process3=(vvv/60000)*100;
            ProcessInPercent3={ProcessInPerncent1 Process2 Process3};
        end
        vectorss=cell2table(vectors);
        vectorss.Properties.VariableNames{'vectors1'}= 'ID';
        vectorss.Properties.VariableNames{'vectors2'}= 'time';
        vectorss.Properties.VariableNames{'vectors3'}= 'force_xy';
        vectorss.Properties.VariableNames{'vectors4'}= 'angle_xy';
        vectorss.Properties.VariableNames{'vectors5'}= 'Force_xyz';
        vectorss.Properties.VariableNames{'vectors6'}= 'angle_xy_z';
        vectorss.Properties.VariableNames{'vectors7'}= 'Force_xy_correctedZ';
        vectorss.Properties.VariableNames{'vectors8'}= 'angle_xy_correctedZ';
        
        mean_force_xy=mean(vectorss.force_xy);
        mean_angle_xy=mean(vectorss.angle_xy);
        mean_Force_xyz=mean(vectorss.Force_xyz);
        mean_angle_xy_z=mean(vectorss.angle_xy_z);
        mean_Force_xy_correctedZ=mean(vectorss.Force_xy_correctedZ);
        mean_angle_xy_correctedZ=mean(vectorss.angle_xy_correctedZ);
        
        sd_force_xy=std(vectorss.force_xy);
        sd_angle_xy=std(vectorss.angle_xy);
        sd_Force_xyz=std(vectorss.Force_xyz);
        sd_angle_xy_z=std(vectorss.angle_xy_z);
        sd_Force_xy_correctedZ=std(vectorss.Force_xy_correctedZ);
        sd_angle_xy_correctedZ=std(vectorss.angle_xy_correctedZ);
        
        output(ii,:)={ii folderparts{1,3} folderposii mean_force_xy mean_angle_xy mean_Force_xyz mean_angle_xy_z mean_Force_xy_correctedZ mean_angle_xy_correctedZ sd_force_xy, sd_angle_xy sd_Force_xyz sd_angle_xy_z sd_Force_xy_correctedZ sd_angle_xy_correctedZ};
        
        writetable(vectorss,newpathhh);
        clearvars folderposii  newpathhh filesii forcefilesii bytes bytefile forcefilesiis  forcefileii forcedat forcedatt forcedattt t x y z force_xy vectorss
        clearvars mean_force_xy mean_angle_xy mean_Force_xyz mean_angle_xy_z mean_Force_xy_correctedZ
        clearvars mean_angle_xy_correctedZ sd_force_xy sd_angle_xy sd_Force_xyz sd_angle_xy_z sd_Force_xy_correctedZ
        clearvars sd_angle_xy_correctedZ
        Process2=(ii/length(folPositions))*100;
        ProcessInPercent2={ProcessInPerncent1 Process2}
    end
    outputI(i,:)={i, folderparts{1,3}, output};
    clearvars pathi foldersi foldersi2 folderparts newpath newpathh folPosition folPositions output
    ProcessInPerncent1=(i/length(foldersss))*100;
    outputII=vertcat(outputI{:,3});
    outputIII=cell2table(outputII);
    outputIII.Properties.VariableNames{'outputII1'}= 'ID';
    outputIII.Properties.VariableNames{'outputII2'}= 'FishId';
    outputIII.Properties.VariableNames{'outputII3'}= 'Pos';
    outputIII.Properties.VariableNames{'outputII4'}= 'Mean_Force_xy';
    outputIII.Properties.VariableNames{'outputII5'}= 'Mean_Angle_xy';
    outputIII.Properties.VariableNames{'outputII6'}= 'Mean_Force_xyz';
    outputIII.Properties.VariableNames{'outputII7'}= 'Mean_Angle_xy_z';
    outputIII.Properties.VariableNames{'outputII8'}= 'Mean_Force_xy_correctedZ';
    outputIII.Properties.VariableNames{'outputII9'}= 'Mean_Angle_xy_correctedZ';
    outputIII.Properties.VariableNames{'outputII10'}= 'Sd_Angle_xy';
    outputIII.Properties.VariableNames{'outputII11'}= 'Sd_Force_xyz';
    outputIII.Properties.VariableNames{'outputII12'}= 'Sd_Angle_xy_z';
    outputIII.Properties.VariableNames{'outputII13'}= 'Sd_Force_xy_correctedZ';
    outputIII.Properties.VariableNames{'outputII14'}= 'Sd_Angle_xy_correctedZ';
    pathComb='Filepath\AllMeans.csv'; 
    writetable(outputIII,pathComb);
end



   

        