%%original scripted by Thijs Robijns. Changed by Alessio Exer

%% Import data from raw measurement data to MATLAB data

% This script imports the raw measurement data into MATLAB data files. 

clear;close,clc;

Directory='P:\1220038-kpp-rivierkunde-2015\C1_interactie\work_Alessio\Experiment\Experiment Data';                % Directory where the raw measurement data is stored
Directory2='P:\1220038-kpp-rivierkunde-2015\C1_interactie\work_Alessio\Experiment\Data analysis\Data_MFiles';         % Directory where the MATLAB data files will be stored

path(Directory,path);                                   % Adds directory to path of MATLAB
path([Directory '\Flume'],path);                        % Adds flume data to path


[data Names]=xlsread([Directory '\Tests.xlsx'],'B3:E578');           % Reads the different numbers of the data files per experiment run from 'Tests.xlsx'
Flume=data(:,2); Ship=data(:,1); Experiment=data(:,3);   

%% Making labels
% This part creates labels for the different vectors of each measurement
% device.

A=csvread([Directory '\Flume\1220038001.dat'],1,0); % comma separated value file (filename,R,C), R=Row and C=Colum
B=csvread([Directory '\Ship\1220038.008001.dat'],1,0);

Doc1=fopen([Directory '\Flume\1220038001.dat']);
Label1=textscan(Doc1,'%s','delimiter',',');
label_Flume=Label1{1,1}';
label_Flume(:,(size(A,2)+1):1:length(label_Flume))=[];

Doc2=fopen([Directory '\Ship\1220038.008001.dat']);
Label2=textscan(Doc2,'%s','delimiter',',');
label_Ship=Label2{1,1}';
label_Ship(:,(size(B,2)+1):1:length(label_Ship))=[];

clear A B Doc1 Doc2 Label1 Label2

%% Finding markers

% This parts finds the puls signals (needed for the synchronizing of the
% data in time) in the measurement data.

for j=1:1:length(label_Ship)
    for i=1:1:length(label_Flume)
        if strcmp(label_Flume(i),'Markeer')==1 && strcmp(label_Ship(j),'puls')==1 %strcmp=compare strings
            markerno_Ship=j;
            markerno_Flume=i;
        end
    end
end

clear i j

%% Import data

% This part imports the data from the ship and the flume into a MATLAB data
% file (one file per experiment run). It also synchronizes the data from 
% the flume and the ship in time by shifting the puls signals so they
% overlap.

for i=1:1:length(Flume)
    if strcmp(Names(i,4),'-')==0
        
        no_Flume=Flume(i); no_Ship=Ship(i);no_Experiment=Experiment(i); n=0;
    
        if no_Flume==0
            disp('no Flume data')
            n=1;
        elseif no_Flume<10
            filename=([Directory '\Flume\122003800' int2str(no_Flume) '.dat']);
            data_Flume=csvread(filename,1,0);
        elseif no_Flume>=10 && no_Flume<100
            filename=([Directory '\Flume\12200380' int2str(no_Flume) '.dat']);
            data_Flume=csvread(filename,1,0);
        elseif no_Flume>=100
            filename=([Directory '\Flume\1220038' int2str(no_Flume) '.dat']);
            data_Flume=csvread(filename,1,0);
        else
            display error
        end
    
        if no_Ship==0
            disp('no Ship data')
            n=2;
        elseif no_Ship<10
            filename=([Directory '\Ship\1220038.00800' int2str(no_Ship) '.dat']);
            data_Ship=csvread(filename,1,0);
        elseif no_Ship>=10 && no_Ship<100
            filename=([Directory '\Ship\1220038.0080' int2str(no_Ship) '.dat']);
            data_Ship=csvread(filename,1,0);
        elseif no_Ship>=100
            filename=([Directory '\Ship\1220038.008' int2str(no_Ship) '.dat']);
            data_Ship=csvread(filename,1,0);
        else
            display error
        end
    
        %% Set data equal in time
        
        j=1; k=1;
            
        if n==0 || n==1
            marker_Ship=data_Ship(:,markerno_Ship);
            dt=data_Ship(2,1)-data_Ship(1,1);
            if max(marker_Ship)>0.5
                while marker_Ship(j)<0.5
                    j=j+1;
                end
            end  
        end
        
        if n==0 || n==2
            marker_Flume=data_Flume(:,markerno_Flume);
            dt=data_Flume(2,1)-data_Flume(1,1);
            if max(marker_Flume)>0.5
                while marker_Flume(k)<0.5
                    k=k+1;
                end
            end
        end
        
        if n==0 && k>j
            data_Flume(1:1:k-j,:)=[];
            data_Flume(:,1)=-(j-1)*dt:dt:(length(data_Flume)*dt-(j-1)*dt-dt);
            data_Ship(:,1)=-(j-1)*dt:dt:(length(data_Ship)*dt-(j-1)*dt-dt);
            if length(data_Flume)>length(data_Ship)
                data_Flume((length(data_Ship)+1):1:length(data_Flume),:)=[];
            elseif length(data_Flume)<length(data_Ship)
                data_Ship((length(data_Flume)+1):1:length(data_Ship),:)=[];
            end
        elseif n==0 && k<j
            data_Ship(1:1:j-k,:)=[];
            data_Flume(:,1)=-(k-1)*dt:dt:(length(data_Flume)*dt-(k-1)*dt-dt);
            data_Ship(:,1)=-(k-1)*dt:dt:(length(data_Ship)*dt-(k-1)*dt-dt);
            if length(data_Flume)>length(data_Ship)
                data_Flume((length(data_Ship)+1):1:length(data_Flume),:)=[];
            elseif length(data_Flume)<length(data_Ship)
                data_Ship((length(data_Flume)+1):1:length(data_Ship),:)=[];
            end
        elseif n==0 && k==j
            data_Flume(:,1)=-(k-1)*dt:dt:(length(data_Flume)*dt-(k-1)*dt-dt);
            data_Ship(:,1)=-(k-1)*dt:dt:(length(data_Ship)*dt-(k-1)*dt-dt);
            if length(data_Flume)>length(data_Ship)
                data_Flume((length(data_Ship)+1):1:length(data_Flume),:)=[];
            elseif length(data_Flume)<length(data_Ship)
                data_Ship((length(data_Flume)+1):1:length(data_Ship),:)=[];
            end
        elseif n==1
            data_Ship(:,1)=-(j-1)*dt:dt:(length(data_Ship)*dt-(j-1)*dt-dt);
        elseif n==2
            data_Flume(:,1)=-(k-1)*dt:dt:(length(data_Flume)*dt-(k-1)*dt-dt);
        end
        
        if n==0 || n==1
            time=data_Ship(:,1)/1000;
            PDCR1=data_Ship(:,2);
            PDCR2=data_Ship(:,3);
            PDCR3=data_Ship(:,4);
            PDCR4=data_Ship(:,5);
            F1_x=data_Ship(:,6);
            F1_y=data_Ship(:,7);
            F1_z=data_Ship(:,8);
            F2_x=data_Ship(:,9);
            F2_y=data_Ship(:,10);
            F2_z=data_Ship(:,11);
            EMSship1_x=data_Ship(:,12);
            EMSship1_y=data_Ship(:,13);
            EMSship2_x=data_Ship(:,14);
            EMSship2_y=data_Ship(:,15);
            Vs=data_Ship(:,16)/100;
            marker_Ship=data_Ship(:,17);
        end
            
        if n==0 || n==2
            time=data_Flume(:,1)/1000;
            DP1=data_Flume(:,2);
            DP2=data_Flume(:,3);
            DP3=data_Flume(:,4);
            DP4=data_Flume(:,5);
            DP5=data_Flume(:,6);
            EMS1_x=data_Flume(:,7);
            EMS1_y=data_Flume(:,8);
            EMS2_x=data_Flume(:,9);
            EMS2_y=data_Flume(:,10);
            EMS3_x=data_Flume(:,11);
            EMS3_y=data_Flume(:,12);
            EMS4_x=data_Flume(:,13);
            EMS4_y=data_Flume(:,14);
            EMS5_x=data-Flume(:,15);
            EMS5_y=data-Flume(:,16);
            P_begin=data_Flume(:,17);
            P_end=data_Flume(:,18);
            P_atm=data_Flume(:,19);
            Kulite1=data_Flume(:,20);
            Kulite2=data_Flume(:,21);
            marker_Flume=data_Flume(:,22);
        end
        
        if n==0 && no_Experiment~=0
            t_start=NaN; t_end=NaN; t1=NaN; index_t_start=NaN; index_t0=NaN;
            index_t1=NaN; index_t_end=NaN;
            for l=1:1:length(time)
                if time(l)<0 && Vs(l)<0.01
                    t_start=time(l);
                    index_t_start=l;
                elseif time(l)==0
                    index_t0=l;
                elseif time(l)>0 && marker_Ship(l)>0.5
                    t1=time(l);
                    index_t1=l;
                elseif time(l)>0 && Vs(l)>0.01
                    t_end=time(l);
                    index_t_end=l;
                end
            end

            clear l

            Vs_mean=mean(Vs(index_t0:1:index_t1));
            dt=round(1/(time(2)-time(1)));

            Puls1=4.022; EMS=11; DP_1=11.5; DP_2=12.542; Ls=3;

            t_EMS=(EMS-Puls1)/(Vs_mean);
            t_DP1=(DP_1-Puls1)/(Vs_mean);
            t_DP2=(DP_2-Puls1)/(Vs_mean);
            t_ship=Ls/(Vs_mean);

            index_t_EMS=round(t_EMS*dt)+index_t0;
            index_t_DP1=round(t_DP1*dt)+index_t0;
            index_t_DP2=round(t_DP2*dt)+index_t0;
            length_t_ship=round(t_ship*dt);
        end
        
        if n==0 && no_Experiment~=0
            save([Directory2 '\Raw_Matlab_Data\Experiment' num2str(no_Experiment) ' ' char(Names(i,4)) '.mat'],'time','DP1','DP2',...
                'DP3','DP4','DP5','EMS1_x','EMS1_y','EMS2_x','EMS2_y','EMS3_x','EMS3_y','EMS4_x','EMS4_y','EMS5_x','EMS5_y','P_begin',...
                'P_end','P_atm','Kulite1', 'Kulite2','marker_Flume','PDCR1','PDCR2','PDCR3','PDCR4','F1_x','F1_y','F1_z','F2_x','F2_y',...
                'F2_z','EMSship1_x','EMSship1_y','EMSship2_x','EMSship2_y','Vs','marker_Ship','Vs_mean','t_EMS',...
                't_DP1','t_DP2','t_ship','index_t_EMS','index_t_DP1','index_t_DP2','length_t_ship','t_start','t1',...
                't_end','index_t_start','index_t0','index_t1','index_t_end');
        elseif n==0 && no_Experiment==0
            save([Directory2 '\Raw_Matlab_Data\Experiment' num2str(no_Experiment) ' ' char(Names(i,4)) '.mat'],'time','DP1','DP2',...
                'DP3','DP4','DP5','EMS1_x','EMS1_y','EMS2_x','EMS2_y','EMS3_x','EMS3_y','EMS4_x','EMS4_y','EMS5_x','EMS5_y','P_begin',...
                'P_end','P_atm','Kulite1', 'Kulite2','marker_Flume','PDCR1','PDCR2','PDCR3','PDCR4','F1_x','F1_y','F1_z','F2_x','F2_y',...
                'F2_z','EMSship1_x','EMSship1_y','EMSship2_x','EMSship2_y','Vs','marker_Ship');
        elseif n==1
            save([Directory2 '\Raw_Matlab_Data\Experiment' num2str(no_Experiment) ' ' char(Names(i,4)) '.mat'],'time','PDCR1','PDCR2',...
                'PDCR3','PDCR4','F1_x','F1_y','F1_z','F2_x','F2_y','F2_z','EMSship1_x','EMSship1_y','EMSship2_x',...
                'EMSship2_y','Vs','marker_Ship');
        elseif n==2
            save([Directory2 '\Raw_Matlab_Data\Experiment' num2str(no_Experiment) ' ' char(Names(i,4)) '.mat'],'time','DP1','DP2',...
                'DP3','DP4','DP5','EMS1_x','EMS1_y','EMS2_x','EMS2_y','EMS3_x','EMS3_y','EMS4_x','EMS4_y','P_begin',...
                'P_end','P_atm','marker_Flume');
        end
        
        disp(num2str(no_Experiment))
        
        clear j k filename no_Experiment no_Flume no_Ship dt time DP1 DP2 DP3 DP4 DP5 EMS1_x EMS1_y EMS2_x EMS2_y ...
            EMS3_x EMS3_y EMS4_x EMS4_y EMS5_x EMS5_y P_begin P_end P_atm Kulite1 Kulite2 marker_Flume PDCR1 PDCR2 PDCR3 PDCR4 F1_x F1_y F1_z ...
            F2_x F2_y F2_z EMSship1_x EMSship1_y EMSship2_x EMSship2_y Vs marker_Ship data_Flume data_Ship dt ...
            Vs_mean t_EMS t_DP1 t_DP2 t_ship index_t_EMS index_t_DP1 index_t_DP2 length_t_ship t_start t1 t_end...
            index_t_start index_t0 index_t1 index_t_end
       
    end 
end

clear, close;