% Funzione che definisce accelerazioni (acc), dati magnetici (mag), 
% temperature (temp) per i nodi di tipo Tilt Link

function [TimeHDVR,accHDVR,magHDVR,tempHDVR,ErrTiltLinkHDVR] = defDatiHDVR(DatiTiltLinkHDVR,...
    ErrTiltLinkHDVR,NodoTiltLinkHDVR,Ndatidespike,rHDVR,Unit,FileName)

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

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

text = [num2str(Ncorr) ' NaN of Tilt Link HD VR corrected by defDatiHD 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
TimeHDVR = DatiTiltLinkHDVR(:,1); % data
accHDVR = zeros(r,rHDVR*3); % dati accelerometro
magHDVR = zeros(r,rHDVR*3); % dati magnetometro
tempHDVR = zeros(r,rHDVR); % temperatura del nodo per la calibrazione
s = 1;
m = 1;
p = 2;
for i=1:rHDVR
     if cell2mat(NodoTiltLinkHDVR(i,5)) == 1
         accHDVR(:,s) = DatiTiltLinkHDVR(:,p);
         accHDVR(:,s+1) = DatiTiltLinkHDVR(:,p+1);
         accHDVR(:,s+2) = DatiTiltLinkHDVR(:,p+2);
         s = s+3;
         magHDVR(:,m) = DatiTiltLinkHDVR(:,p+3);
         magHDVR(:,m+1) = DatiTiltLinkHDVR(:,p+4);
         magHDVR(:,m+2) = DatiTiltLinkHDVR(:,p+5);
         m = m+3;
         tempHDVR(:,i) = DatiTiltLinkHDVR(:,p+6);
         p = p+7;
     else
         accHDVR(:,s) = DatiTiltLinkHDVR(:,p);
         accHDVR(:,s+1) = DatiTiltLinkHDVR(:,p+1);
         accHDVR(:,s+2) = DatiTiltLinkHDVR(:,p+2);
         s = s+3;
         tempHDVR(:,i) = DatiTiltLinkHDVR(:,p+3);
         p = p+4;
     end
end

if strcmp(Unit,'G301') == 1
    i = 1;
    j = 1;
    while i <= 3*rHDVR
        for ii = 2:r
            if accHDVR(ii,i) == -8191 && accHDVR(ii,i+1) == 0 && accHDVR(ii,i+2) == 0
                accHDVR(ii,i) = accHDVR(ii-1,i);
                accHDVR(ii,i+1) = accHDVR(ii-1,i+1);
                accHDVR(ii,i+2) = accHDVR(ii-1,i+2);
                magHDVR(ii,i) = magHDVR(ii-1,i);
                magHDVR(ii,i+1) = magHDVR(ii-1,i+1);
                magHDVR(ii,i+2) = magHDVR(ii-1,i+2);
                tempHDVR(ii,j) = tempHDVR(ii-1,j);
                ErrTiltLinkHDVR(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:rHDVR
    % despiking accelerometri
    accHDVR(:,s) = filloutliers(accHDVR(:,s),'linear','movmedian',Num_Dati);    
    accHDVR(:,s+1) = filloutliers(accHDVR(:,s+1),'linear','movmedian',Num_Dati);    
    accHDVR(:,s+2) = filloutliers(accHDVR(:,s+2),'linear','movmedian',Num_Dati);    
    s = s+3;
    % despiking magnetometri
    magHDVR(:,m) = filloutliers(magHDVR(:,m),'linear','movmedian',Num_Dati);    
    magHDVR(:,m+1) = filloutliers(magHDVR(:,m+1),'linear','movmedian',Num_Dati);    
    magHDVR(:,m+2) = filloutliers(magHDVR(:,m+2),'linear','movmedian',Num_Dati);  
    m = m+3;
end

text = 'Data of Tilt Link HD VR defined correctly. defDatiTL function ended';
fprintf(fileID,fmt,text);
fclose(fileID);

end
