% funzione che definisce i dati per la Barretta Estensimetrica, in particolare
% tempo (Time3DCrL) e Dato (Crack3D)

function [Time3DCrL,Crack3D,NTCCrack3D,Err3DCrackLink,ErrNTC3DCrackLink] = ...
    defDati3DCrL(Dati3DCrackLink,DatiNTC3DCrackLink,Err3DCrackLink,ErrNTC3DCrackLink,...
    r3DCrL,Nodo3DCrackLink,Ch3DCrL,CrL3D_NTC,Ndatidespike,FileName)

fileID = fopen(FileName,'a');
fmt = '%s \r';
text = 'defDati3DCrL function started';
fprintf(fileID,fmt,text);

CrL3D_Or = cell2mat(Nodo3DCrackLink(:,6));

[r,c] = size(Dati3DCrackLink);
Ncorr = 0;

% Elimino gli eventuali Not a Number
for a = 2:r
    ntc = 1;
    control = 1;
    for b = 2:c
        check = isnan(Dati3DCrackLink(a,b));
        if check == 1
            Dati3DCrackLink(a,b) = Dati3DCrackLink(a-1,b);
            Err3DCrackLink(a,b-1) = 1;
            Ncorr = Ncorr+1;
        end
        if control == 1
            if CrL3D_NTC(ntc) == 1 % La prima colonna è una data
                check = isnan(DatiNTC3DCrackLink(a,ntc));
                if check == 1
                    DatiNTC3DCrackLink(a,b) = DatiNTC3DCrackLink(a-1,ntc);
                    ErrNTC3DCrackLink(a,b-1) = 0.5;
                    Ncorr = Ncorr+1;
                end
            end
            control = control+1;
            ntc = ntc+1;
        end
    end
end

text = ['' num2str(Ncorr) ' NaN of 3D Crack Link corrected by defDati3DCrL function'];
fprintf(fileID,fmt,text);

Time3DCrL = Dati3DCrackLink(:,1); % data
Crack3D = Dati3DCrackLink(:,2:end); 
NTCCrack3D = zeros(r,r3DCrL);
b = 2; % colonna del primo dato
for s = 1:r3DCrL
    if Ch3DCrL(s,1) == 1 || Ch3DCrL(s,1) >= 2 %Solo fessurimetro
        if CrL3D_NTC(s,1)==1 % si legge anche il termometro
            NTCCrack3D(:,s) = DatiNTC3DCrackLink(:,b); % dati NTC
        end
        b = b+1;
    end
end

Num_Dati = Ndatidespike; % numero di dati per il despike
if Num_Dati > r
    Num_Dati = r;
end

i = 1;
cont = 1;
X = zeros(r,r3DCrL);
Y = zeros(r,r3DCrL);
Z = zeros(r,r3DCrL);
while i <= 3*r3DCrL
    % despiking Fessurimetri
    X(1:end-1,cont) = filloutliers(Crack3D(1:end-1,i),'linear','movmedian',Num_Dati);    
    Y(1:end-1,cont) = filloutliers(Crack3D(1:end-1,i+1),'linear','movmedian',Num_Dati); 
    Z(1:end-1,cont) = filloutliers(Crack3D(1:end-1,i+2),'linear','movmedian',Num_Dati); 
    X(end,cont) = Crack3D(end,i);
    Y(end,cont) = Crack3D(end,i+1);
    Z(end,cont) = Crack3D(end,i+2);
    i = i+3;
    cont = cont+1;
end

if CrL3D_Or == 123
    X_Corr = X;
    Y_Corr = Y;
    Z_Corr = Z;
elseif CrL3D_Or == 132
    X_Corr = X;
    Y_Corr = Z;
    Z_Corr = Y;
elseif CrL3D_Or == 213
    X_Corr = Y;
    Y_Corr = X;
    Z_Corr = Z;
elseif CrL3D_Or == 231
    X_Corr = Y;
    Y_Corr = Z;
    Z_Corr = X;
elseif CrL3D_Or == 312
    X_Corr = Z;
    Y_Corr = X;
    Z_Corr = Y;
elseif CrL3D_Or == 321
    X_Corr = Z;
    Y_Corr = Y;
    Z_Corr = X;
end

i = 1;
cont = 1;
while i <= 3*r3DCrL    
      Crack3D(:,i) = X_Corr(:,cont);
      Crack3D(:,i+1) = Y_Corr(:,cont);
      Crack3D(:,i+2) = Z_Corr(:,cont);
      i = i+3;
      cont = cont+1;
end

text = 'Data of 3D Crack Link defined correctly. defDati3DCrL function closed';
fprintf(fileID,fmt,text);
fclose(fileID);
end