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

function [TimeHD,accHD,magHD,tempHD,ErrTiltLinkHD] = defDatiHD(DatiTiltLinkHD,...
    ErrTiltLinkHD,NodoTiltLinkHD,Ndatidespike,rHD,Unit,FileName)

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

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

text = [num2str(Ncorr) ' NaN of Tilt Link HD 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
TimeHD = DatiTiltLinkHD(:,1); % data
accHD = zeros(r,rHD*3); % dati accelerometro
magHD = zeros(r,rHD*3); % dati magnetometro
tempHD = zeros(r,rHD); % temperatura del nodo per la calibrazione
s = 1;
m = 1;
p = 2;
for i=1:rHD
     if cell2mat(NodoTiltLinkHD(i,5)) == 1
         accHD(:,s) = DatiTiltLinkHD(:,p);
         accHD(:,s+1) = DatiTiltLinkHD(:,p+1);
         accHD(:,s+2) = DatiTiltLinkHD(:,p+2);
         s = s+3;
         magHD(:,m) = DatiTiltLinkHD(:,p+3);
         magHD(:,m+1) = DatiTiltLinkHD(:,p+4);
         magHD(:,m+2) = DatiTiltLinkHD(:,p+5);
         m = m+3;
         tempHD(:,i) = DatiTiltLinkHD(:,p+6);
         p = p+7;
     else
         accHD(:,s) = DatiTiltLinkHD(:,p);
         accHD(:,s+1) = DatiTiltLinkHD(:,p+1);
         accHD(:,s+2) = DatiTiltLinkHD(:,p+2);
         s = s+3;
         tempHD(:,i) = DatiTiltLinkHD(:,p+3);
         p = p+4;
     end
end

if strcmp(Unit,'G301') == 1
    i = 1;
    j = 1;
    while i <= 3*rHD
        for ii = 2:r
            if accHD(ii,i) == -8191 && accHD(ii,i+1) == 0 && accHD(ii,i+2) == 0
                accHD(ii,i) = accHD(ii-1,i);
                accHD(ii,i+1) = accHD(ii-1,i+1);
                accHD(ii,i+2) = accHD(ii-1,i+2);
                magHD(ii,i) = magHD(ii-1,i);
                magHD(ii,i+1) = magHD(ii-1,i+1);
                magHD(ii,i+2) = magHD(ii-1,i+2);
                tempHD(ii,j) = tempHD(ii-1,j);
                ErrTiltLinkHD(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:rHD
    % despiking accelerometri
    accHD(:,s) = filloutliers(accHD(:,s),'linear','movmedian',Num_Dati);    
    accHD(:,s+1) = filloutliers(accHD(:,s+1),'linear','movmedian',Num_Dati);    
    accHD(:,s+2) = filloutliers(accHD(:,s+2),'linear','movmedian',Num_Dati);    
    s = s+3;
    % despiking magnetometri
    magHD(:,m) = filloutliers(magHD(:,m),'linear','movmedian',Num_Dati);    
    magHD(:,m+1) = filloutliers(magHD(:,m+1),'linear','movmedian',Num_Dati);    
    magHD(:,m+2) = filloutliers(magHD(:,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
