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

function [Time3DEL,Extensometer3D,Extensometer3D_Rif,NTCExtensometer3D,...
    Err3DExtensometerLink,ErrNTC3DExtensometerLink] = ...
    defDati3DEL(Dati3DExtensometerLink,Dati3DExtensometerLink_Rif,...
    DatiNTC3DExtensometerLink,Err3DExtensometerLink,ErrNTC3DExtensometerLink,...
    Nodo3DExtensometerLink,r3DEL,Ch3DEL,EL3D_NTC,Ndatidespike,FileName)

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

EL3D_Or = cell2mat(Nodo3DExtensometerLink(:,6));

[r,c] = size(Dati3DExtensometerLink);
Ncorr = 0;
% Elimino gli eventuali Not a Number
for a = 2:r
    contT = 1;
    num = 1;
    for b = 2:c
        check = isnan(Dati3DExtensometerLink(a,b));
        if check == 1
            Dati3DExtensometerLink(a,b) = Dati3DExtensometerLink(a-1,b);
            Err3DExtensometerLink(a,b-1) = 1;
            Ncorr = Ncorr+1;
        end
        if EL3D_NTC(contT) == 1 % La prima colonna è una data
            check = isnan(DatiNTC3DExtensometerLink(a,contT));
            if check == 1
                DatiNTC3DExtensometerLink(a,contT) = DatiNTC3DExtensometerLink(a-1,contT);
                ErrNTC3DExtensometerLink(a,contT) = 1;
                Ncorr = Ncorr+1;
            end
        end
        num = num+1;
        if num == 4
            num = 1;
            contT = contT+1;
        end
    end
end

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

Time3DEL = Dati3DExtensometerLink(:,1); % data
Extensometer3D = zeros(r,r3DEL);
Extensometer3D_Rif = zeros(1,r3DEL);
NTCExtensometer3D = zeros(r,r3DEL);

s = 1;
i = 1;
b = 2; % colonna del primo dato
while b <= c
    Extensometer3D(:,s:s+2) = Dati3DExtensometerLink(:,b:b+2); % dati 3D Extensometer Link
    if Ch3DEL(i,1) == 4
        if EL3D_NTC(i,1)==1 % si legge anche il termometro
            NTCExtensometer3D(:,i) = DatiNTC3DExtensometerLink(:,i); % dati T Extensometer Link
        end
    end
    s = s+3;
    b = b+3;
    i = i+1;
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,r3DEL);
Y = zeros(r,r3DEL);
Z = zeros(r,r3DEL);
X_Rif = zeros(1,r3DEL);
Y_Rif = zeros(1,r3DEL);
Z_Rif = zeros(1,r3DEL);
while i <= 3*r3DEL
    % despiking Barrette Estensimetriche
    X(:,cont) = filloutliers(Extensometer3D(:,i),'linear','movmedian',Num_Dati);    
    Y(:,cont) = filloutliers(Extensometer3D(:,i+1),'linear','movmedian',Num_Dati); 
    Z(:,cont) = filloutliers(Extensometer3D(:,i+2),'linear','movmedian',Num_Dati); 
    X_Rif(1,cont) = Dati3DExtensometerLink_Rif(:,i);
    Y_Rif(1,cont) = Dati3DExtensometerLink_Rif(:,i+1);
    Z_Rif(1,cont) = Dati3DExtensometerLink_Rif(:,i+2);
    i = i+3;
    cont = cont+1;
end

if EL3D_Or == 123
    X_Corr = X;
    Y_Corr = Y;
    Z_Corr = Z;
    X_Corr_Rif = X_Rif;
    Y_Corr_Rif = Y_Rif;
    Z_Corr_Rif = Z_Rif;
elseif EL3D_Or == 132
    X_Corr = X;
    Y_Corr = Z;
    Z_Corr = Y;
    X_Corr_Rif = X_Rif;
    Y_Corr_Rif = Z_Rif;
    Z_Corr_Rif = Y_Rif;
elseif EL3D_Or == 213
    X_Corr = Y;
    Y_Corr = X;
    Z_Corr = Z;
    X_Corr_Rif = Y_Rif;
    Y_Corr_Rif = X_Rif;
    Z_Corr_Rif = Z_Rif;
elseif EL3D_Or == 231
    X_Corr = Y;
    Y_Corr = Z;
    Z_Corr = X;
    X_Corr_Rif = Y_Rif;
    Y_Corr_Rif = Z_Rif;
    Z_Corr_Rif = X_Rif;
elseif EL3D_Or == 312
    X_Corr = Z;
    Y_Corr = X;
    Z_Corr = Y;
    X_Corr_Rif = Z_Rif;
    Y_Corr_Rif = X_Rif;
    Z_Corr_Rif = Y_Rif;
elseif EL3D_Or == 321
    X_Corr = Z;
    Y_Corr = Y;
    Z_Corr = X;
    X_Corr_Rif = Z_Rif;
    Y_Corr_Rif = Y_Rif;
    Z_Corr_Rif = X_Rif;
end

i = 1;
cont = 1;
while i <= 3*r3DEL    
      Extensometer3D(:,i) = X_Corr(:,cont);
      Extensometer3D(:,i+1) = Y_Corr(:,cont);
      Extensometer3D(:,i+2) = Z_Corr(:,cont);
      Extensometer3D_Rif(1,i) = X_Corr_Rif(1,cont);
      Extensometer3D_Rif(1,i+1) = Y_Corr_Rif(1,cont);
      Extensometer3D_Rif(1,i+2) = Z_Corr_Rif(1,cont);
      i = i+3;
      cont = cont+1;
end
    
text = 'Data of 3D Extensometer Link defined correctly. defDati3DEL function closed';
fprintf(fileID,fmt,text);
fclose(fileID);
end