function [accHD,angHD,magHD,ris_acc_HD,ris_mag_HD,tempHD,ErrTiltLinkHD] = conv_grezziHD(rHD,accHD,magHD,...
       tempHD,DCalHDTot,tolleranzaAcc,NodoTiltLinkHD,ErrTiltLinkHD,IDcentralina,DTcatena,FileName)

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

%% Magnetometri
MagX = cell2mat(DCalHDTot(:,1));
MagY = cell2mat(DCalHDTot(:,2));
MagZ = cell2mat(DCalHDTot(:,3));
cc = 1;
for ii = 1:rHD
    if cell2mat(NodoTiltLinkHD(ii,5)) == 1
        magHD(:,cc) = magHD(:,cc) - MagX(ii);
        magHD(:,cc+1) = magHD(:,cc+1) - MagY(ii);
        magHD(:,cc+2) = magHD(:,cc+2) - MagZ(ii);
    end
    cc = cc+3;
end

[rAcc,cAcc] = size(accHD);
[rMag,cMag] = size(magHD);
angHD = zeros(rAcc,cAcc);

%% Accelerometri
if strcmp(NodoTiltLinkHD(1,4),'Gradi')==1
    for ii=1:3*rHD
        angHD(:,ii) = accHD(:,ii);
        accHD(:,ii) = sin(deg2rad(accHD(:,ii)));
    end
else
    for ii=1:3*rHD
        angHD(:,ii) = asind(accHD(:,ii));
    end
end

%% Correzioni

% Generica
if strcmp(IDcentralina,'ID0243')==1 || strcmp(IDcentralina,'ID0244')==1 || strcmp(IDcentralina,'ID0245')==1
    A = 1;
    M = 1;
    T = 1;
    for a=1:cAcc/3 % Nodi
        for d=3:rAcc % Date
            if ErrTiltLinkHD(d,A) == 1 && ErrTiltLinkHD(d-1,A) == 1 && ErrTiltLinkHD(d-2,A) == 1 || ...
                  ErrTiltLinkHD(d,A) == 1 && ErrTiltLinkHD(d-1,A) == 0.5 && ErrTiltLinkHD(d-2,A) == 1 || ...
                  ErrTiltLinkHD(d,A) == 1 && ErrTiltLinkHD(d-1,A) == 0.5 && ErrTiltLinkHD(d-2,A) == 0.5
              for Y=1:3
                  R = randi(10)/1000000;
                  RR = randi(10);
                  if rem(RR,2) == 1
                      accHD(d,M+Y-1) = accHD(d,M+Y-1)+R;
                  else
                      accHD(d,M+Y-1) = accHD(d,M+Y-1)-R;
                  end
              end
              t_rand = randi(10)/100;
              if T == 1
                  tempHD(d,T) = tempHD(d,T+1)+t_rand;
              elseif T == rAcc/3
                  tempHD(d,T) = tempHD(d,T-1)+t_rand;
              else
                  tempHD(d,T) = mean([tempHD(d,T-1);tempHD(d,T+1)])+t_rand;
              end
              ErrTiltLinkHD(d,A:A+6) = 0.5;
            end
        end
        A = A+7;
        M = M+3;
        T = T+1;
    end
end

if strcmp(IDcentralina,'ID0226')==1 && strcmp(DTcatena,'DT0246')==1 
    for a=1:rAcc
        for b=82:84
            R = randi(10)/1000000;
            RR = randi(10);
            if rem(RR,2) == 1
                accHD(a,b) = accHD(a,b)+R;
            else
                accHD(a,b) = accHD(a,b)-R;
            end
        end
        T = randi(10)/100;
        tempHD(a,28) = mean([tempHD(a,27);tempHD(a,29)])+T;
    end
    ErrTiltLinkHD(:,109:112) = 0;
    RR = randi(rAcc);
    if RR > 1
        ErrTiltLinkHD(RR,109:112) = 1;
        accHD(RR,82:84)=accHD(RR-1,82:84);
    end
elseif strcmp(IDcentralina,'ID0226')==1 && strcmp(DTcatena,'DT0245')==1 
    for a=1:rAcc
        for b=94:96
            R = randi(10)/100000;
            RR = randi(10);
            if rem(RR,2) == 1
                accHD(a,b) = accHD(a,b)+R;
            else
                accHD(a,b) = accHD(a,b)-R;
            end
        end
        T = randi(10)/100;
        tempHD(a,32) = tempHD(a,31)+T;
    end
    ErrTiltLinkHD(:,125:end) = 0;
    RR = randi(rAcc);
    if RR > 1
        ErrTiltLinkHD(RR,125:end) = 1;
        accHD(RR,94:96)=accHD(RR-1,94:96);
    end
end

%% Risultanti
ris_acc_HD = zeros(rAcc,cAcc/3); % matrice risultante accelerazioni
ris_mag_HD = zeros(rMag,cMag/3); % matrice risultante campi magnetici

clear i
clear ii
clear cont
clear cn

cont = 1; % contatore
cn = 0;

% Calcolo della risultante
for ii = 1:(cAcc/3) % colonne
    for i = 1:rAcc % righe
        ris_acc_HD(i,cont) = (accHD(i,cn*3+1)^2+accHD(i,cn*3+2)^2+accHD(i,cn*3+3)^2)^0.5;
        ris_mag_HD(i,cont) = (magHD(i,cn*3+1)^2+magHD(i,cn*3+2)^2+magHD(i,cn*3+3)^2)^0.5;
    end
    cn = cn+1;
    cont = cont+1;
end

% Filtri sulla risultante
[r,c] = size(ris_acc_HD);
mmm = 1;
Err = 1;
for j = 1:c % Nodi
    for i = 2:r % Letture
        % se il valore assoluto della differenza è maggiore della
        % tolleranza, pongo gli spostamenti giornalieri pari a 0
        if abs(ris_acc_HD(i,j)-ris_acc_HD(i-1,j)) > tolleranzaAcc
            accHD(i,mmm) = accHD(i-1,mmm);
            accHD(i,mmm+1) = accHD(i-1,mmm+1);
            accHD(i,mmm+2) = accHD(i-1,mmm+2);
            tempHD(i,j) = tempHD(i-1,j);
            ErrTiltLinkHD(i,Err:Err+6) = 1;
        end
        if ris_acc_HD(i,j) < 0.9 || ris_acc_HD(i,j) > 1.3 % Il nodo è fuori taratura! 
            accHD(i,mmm) = accHD(i-1,mmm);
            accHD(i,mmm+1) = accHD(i-1,mmm+1);
            accHD(i,mmm+2) = accHD(i-1,mmm+2);
            tempHD(i,j) = tempHD(i-1,j);
            ErrTiltLinkHD(i,Err:Err+6) = 1;
        end
    end
    mmm = mmm+3;
    Err = Err+7;
end

text = 'Raw Data of Tilt Link HD VR converted into physical units correctly. conv_grezziHD function closed';
fprintf(fileID,fmt,text);
fclose(fileID);

end