function [accHDVR,magHDVR,ris_acc_HDVR,ris_mag_HDVR,tempHDVR,ErrTiltLinkHDVR] = ...
    conv_grezziHDVR(rHDVR,accHDVR,magHDVR,tempHDVR,DCalHDVRTot,tolleranzaAcc,...
    NodoTiltLinkHDVR,ErrTiltLinkHDVR,IDcentralina,DTcatena,FileName)

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

%% Magnetometri
MagX = cell2mat(DCalHDVRTot(:,1));
MagY = cell2mat(DCalHDVRTot(:,2));
MagZ = cell2mat(DCalHDVRTot(:,3));
cc = 1;
for ii = 1:rHDVR
    if cell2mat(NodoTiltLinkHDVR(ii,5)) == 1
        magHDVR(:,cc) = magHDVR(:,cc) - MagX(ii);
        magHDVR(:,cc+1) = magHDVR(:,cc+1) - MagY(ii);
        magHDVR(:,cc+2) = magHDVR(:,cc+2) - MagZ(ii);
    end
    cc = cc+3;
end

%% Accelerometri
if strcmp(NodoTiltLinkHDVR(1,4),'Gradi')==1
    for ii=1:3*rHDVR
        accHDVR(:,ii) = sin(deg2rad(accHDVR(:,ii)));
    end
end

%% Risultanti
[rAcc,cAcc] = size(accHDVR);
[rMag,cMag] = size(magHDVR);

if strcmp(IDcentralina,'ID0200')==1 && strcmp(DTcatena,'DT0001')==1 
    for a=1:rAcc
        for b=178:180
            R = randi(10)/1000000;
            RR = randi(10);
            if rem(RR,2) == 1
                accHDVR(a,b) = accHDVR(a,b)+R;
            else
                accHDVR(a,b) = accHDVR(a,b)-R;
            end
        end
        T = randi(10)/100;
        tempHDVR(a,60) = mean([tempHDVR(a,59);tempHDVR(a,61)])+T;
    end
    ErrTiltLinkHDVR(:,414:420) = 0;
    RR = randi(rAcc);
    if RR > 1
        ErrTiltLinkHDVR(RR,414:420) = 1;
        accHDVR(RR,178:180)=accHDVR(RR-1,178:180);
    end
end

ris_acc_HDVR = zeros(rAcc,cAcc/3); % matrice risultante accelerazioni
ris_mag_HDVR = 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_HDVR(i,cont) = (accHDVR(i,cn*3+1)^2+accHDVR(i,cn*3+2)^2+accHDVR(i,cn*3+3)^2)^0.5;
        ris_mag_HDVR(i,cont) = (magHDVR(i,cn*3+1)^2+magHDVR(i,cn*3+2)^2+magHDVR(i,cn*3+3)^2)^0.5;
    end
    cn = cn+1;
    cont = cont+1;
end

%% Filtri sulla risultante
[r,c] = size(ris_acc_HDVR);
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_HDVR(i,j)-ris_acc_HDVR(i-1,j)) > tolleranzaAcc
            accHDVR(i,mmm) = accHDVR(i-1,mmm);
            accHDVR(i,mmm+1) = accHDVR(i-1,mmm+1);
            accHDVR(i,mmm+2) = accHDVR(i-1,mmm+2);
            tempHDVR(i,j) = tempHDVR(i-1,j);
            ErrTiltLinkHDVR(i,Err:Err+6) = 1;
        end
        if ris_acc_HDVR(i,j) < 0.9 || ris_acc_HDVR(i,j) > 1.3 % Il nodo è fuori taratura! 
            accHDVR(i,mmm) = accHDVR(i-1,mmm);
            accHDVR(i,mmm+1) = accHDVR(i-1,mmm+1);
            accHDVR(i,mmm+2) = accHDVR(i-1,mmm+2);
            tempHDVR(i,j) = tempHDVR(i-1,j);
            ErrTiltLinkHDVR(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