% Funzione che converte i dati grezzi

function [angIPLHR,tempIPLHR] = conv_grezziIPLHR(angIPLHR,tempIPLHR,DCalIPLHRTot,...
    NodoInPlaceLinkHR,rIPLHR,FileName)

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

if strcmp(NodoInPlaceLinkHR(1,4),'ADC') || strcmp(NodoInPlaceLinkHR{1,4} ,'null') || isempty(NodoInPlaceLinkHR{1,4}) == 1
    if DCalIPLHRTot(:,4) == 0 % Guadagno asse X e asse Y in comune
        caIPLHR = DCalIPLHRTot(:,1);
        caT_HR = DCalIPLHRTot(:,2);
        intT_HR = DCalIPLHRTot(:,3);
        XY = 1;
    else
        caIPLHR = [DCalIPLHRTot(:,1) DCalIPLHRTot(:,2)];
        caT_HR = DCalIPLHRTot(:,3);
        intT_HR = DCalIPLHRTot(:,4);
        XY = 0;
    end
elseif strcmp(NodoInPlaceLinkHR{1,4} ,'digit')
    caIPLHR = [DCalIPLHRTot(:,1) DCalIPLHRTot(:,2)];
    intIPLHR = [DCalIPLHRTot(:,3) DCalIPLHRTot(:,4)];
    caT_HR = DCalIPLHRTot(:,5);
    intT_HR = DCalIPLHRTot(:,6);
    XY = 0;
end

i = 1;
m = 1;
c = 1;
% Contatore dei nodi, converte i punti ADC con le calibrazioni in valori
% angolari
while i <= 2*rIPLHR % nodo
    if XY == 1
        angIPLHR(:,i) = angIPLHR(:,i)*caIPLHR(m,1);
        i = i+1;
        if c == 2
            m = m+1;
            c = 1;
        else
            c = c+1;
        end
    else
        % MUMS
        if strcmp(NodoInPlaceLinkHR(1,4),'ADC') || strcmp(NodoInPlaceLinkHR{1,4} ,'null') || isempty(NodoInPlaceLinkHR{1,4}) == 1
            angIPLHR(:,i) = angIPLHR(:,i)*caIPLHR(m,1);
            angIPLHR(:,i+1) = angIPLHR(:,i+1)*caIPLHR(m,2);
            i = i+2;
            m = m+1;
        elseif strcmp(NodoInPlaceLinkHR{1,4} ,'digit')
            angIPLHR(:,i) = angIPLHR(:,i)*caIPLHR(m,1)+intIPLHR(m,1);
            angIPLHR(:,i+1) = angIPLHR(:,i+1)*caIPLHR(m,2)+intIPLHR(m,2);
            i = i+2;
            m = m+1;
        end
    end
end

%% Conversione delle temperature
for t = 1:rIPLHR
    tempIPLHR(:,t) = tempIPLHR(:,t)*caT_HR(t,1) + intT_HR(t,1);
end

text = 'Raw Data of In Place Link HR converted into physical units correctly. conv_grezziIPLHR function ended';
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);
fclose(fileID);

end