% Funzione che definisce accelerazioni (accTL), dati magnetici (magTL), 
% temperature (tempTL) per i nodi di tipo Tilt Link

function [TimeIPL,accIPL,magIPL,tempIPL,ErrInPlaceLink] = defDatiIPL(DatiInPlaceLink,...
    ErrInPlaceLink,NodoInPlaceLink,Ndatidespike,rIPL,MEMS,Unit,FileName)

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

[r,c] = size(DatiInPlaceLink);
Ncorr = 0;
% Elimino gli eventuali Not a Number
for a = 2:r
    for b = 1:c
        check = isnan(DatiInPlaceLink(a,b));
        if check == 1
            DatiInPlaceLink(a,b) = DatiInPlaceLink(a-1,b);
            ErrInPlaceLink(a,b-1) = 1;
            Ncorr = Ncorr+1;
        end
    end
end

text = [num2str(Ncorr) ' NaN of In Place Link corrected by defDatiIPL function'];
fprintf(fileID,fmt,text);

% Definisco Data (gg:mm:aaaa hh:mm), Batteria, i 3 dati di accelerometro
% e magnetometro e la Temperatura per la calibrazione
TimeIPL = DatiInPlaceLink(:,1); % data
accIPL = zeros(r,rIPL*3); % dati accelerometro
magIPL = zeros(r,rIPL*3); % dati magnetometro
tempIPL = zeros(r,rIPL); % temperatura del nodo per la calibrazione
if strcmp(NodoInPlaceLink(1,4),'0-10 V')
    p = 3;
else
    p = 7; % passo nella definizione dei dati
end
s = 1;
m = 1;
for i = 1:rIPL
    accIPL(:,s) = DatiInPlaceLink(:,2+(i-1)*p);
    accIPL(:,s+1) = DatiInPlaceLink(:,3+(i-1)*p);
    accIPL(:,s+2) = DatiInPlaceLink(:,4+(i-1)*p);
    s = s+3;
    if strcmp(NodoInPlaceLink(1,4),'0-10 V') == 0
        magIPL(:,m) = DatiInPlaceLink(:,5+(i-1)*p);
        magIPL(:,m+1) = DatiInPlaceLink(:,6+(i-1)*p);
        magIPL(:,m+2) = DatiInPlaceLink(:,7+(i-1)*p);
        m = m+3;
        tempIPL(:,i) = DatiInPlaceLink(:,8+(i-1)*p);
        if MEMS == 2
            [rT,~] = size(tempIPL);
            for j = 1:rT
                if tempIPL(j,i) > 200
                    tempIPL(j,i) = tempIPL(j,i) - 256; % Correzione della temperatura dei nuovi MEMS
                end
            end
        end
    end
end

if strcmp(Unit,'G301') == 1
    i = 1;
    j = 1;
    while i <= 3*rIPL
        for ii = 2:r
            if accIPL(ii,i) == -8191 && accIPL(ii,i+1) == 0 && accIPL(ii,i+2) == 0
                accIPL(ii,i) = accIPL(ii-1,i);
                accIPL(ii,i+1) = accIPL(ii-1,i+1);
                accIPL(ii,i+2) = accIPL(ii-1,i+2);
                magIPL(ii,i) = magIPL(ii-1,i);
                magIPL(ii,i+1) = magIPL(ii-1,i+1);
                magIPL(ii,i+2) = magIPL(ii-1,i+2);
                tempIPL(ii,j) = tempIPL(ii-1,j);
                ErrInPlaceLink(ii,i) = 1;
            end
        end
        i = i+3;
        j = j+1;
    end
end

s = 1;
m = 1;
Num_Dati = Ndatidespike; % numero di dati per il despike
if Num_Dati > r
    Num_Dati = r;
end
for i = 1:rIPL
    % despiking accelerometri
    accIPL(:,s) = filloutliers(accIPL(:,s),'linear','movmedian',Num_Dati);    
    accIPL(:,s+1) = filloutliers(accIPL(:,s+1),'linear','movmedian',Num_Dati);    
    accIPL(:,s+2) = filloutliers(accIPL(:,s+2),'linear','movmedian',Num_Dati);    
    s = s+3;
    % despiking magnetometri
    magIPL(:,m) = filloutliers(magIPL(:,m),'linear','movmedian',Num_Dati);    
    magIPL(:,m+1) = filloutliers(magIPL(:,m+1),'linear','movmedian',Num_Dati);    
    magIPL(:,m+2) = filloutliers(magIPL(:,m+2),'linear','movmedian',Num_Dati);  
    m = m+3;
end

text = 'Data of In Place Link defined correctly. defDatiIPL function ended';
fprintf(fileID,fmt,text);
fclose(fileID);

end
