function [DatiPiez,DatiPiezT,RIF_PL] = conv_grezziPL(rPL,ADCPiez,tempPiez,DCalPLTot,...
    NodoPiezoLink,NuovoZeroPL,IDcentralina,DTcatena,FileName)

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

if isempty(NodoPiezoLink) == 1
    caPiez = DCalPLTot(:,1);
    intPiez = DCalPLTot(:,2);
    caT = DCalPLTot(:,3);
    intT = DCalPLTot(:,4);
    RIF_PL = [];
else
    if strcmp(NodoPiezoLink(1,4),'VW kg/cm2') == 1
        A = DCalPLTot(:,1);
        B = DCalPLTot(:,2);
        C = DCalPLTot(:,3);
        RIF_PL = [];
    elseif strcmp(NodoPiezoLink(1,4),'Hz') == 1 % Convertitore per DSAS o ModBus
        caPiez = DCalPLTot(:,1);
        intPiez = DCalPLTot(:,2);
        D = DCalPLTot(:,4);
        RIF_PL = [];
    elseif strcmp(NodoPiezoLink(1,4),'VW kPa') == 1
        A = DCalPLTot(:,1);
        B = DCalPLTot(:,2);
        C = DCalPLTot(:,3);
        if C == 0 % Identifica i piezometri di Geosense
           D = DCalPLTot(:,4);
           if D ~= 0
               NomeFile = ['' IDcentralina '-' DTcatena '-RifPL.csv'];
               NomeFileT = ['' IDcentralina '-' DTcatena '-RifPL_T.csv'];
               if NuovoZeroPL == 0 % prima elaborazione
                   P = (ADCPiez(1,:).^2)/1000;
                   csvwrite(NomeFile,P);
                   a = 1.4051*10^(-3);
                   b = 2.369*10^(-4);
                   c = 9.9*10^(-8);
                   T = 1./(a+b*reallog(tempPiez(1,:))...
                    +c*(reallog(tempPiez(1,:))).^3)-273.15; % conversione in gradi
                   csvwrite(NomeFileT,T);
               else
                   P = csvread(NomeFile);
                   T = csvread(NomeFileT);
               end
               C = -A.*(P'.^2)-B.*P';
               RIF_PL = 1000*C;
           else
               RIF_PL = 0;
           end
        else
            D = 0;
            RIF_PL = 0;
        end
    else
        if isnan(DCalPLTot(end)) == 1 % Conversione Lineare
            Lineare = 1;
            caPiez = DCalPLTot(:,1);
            intPiez = DCalPLTot(:,2);
        elseif isnan(DCalPLTot(end)) == 0 % Conversione Parabolica
            Lineare = 0;
            aPiez = DCalPLTot(:,5);
            bPiez = DCalPLTot(:,1);
            cPiez = DCalPLTot(:,2);
            intPiez = [];
        end
        caT = DCalPLTot(:,3);
        intT = DCalPLTot(:,4);
        RIF_PL = 0;
    end
end

[rP,~] = size(ADCPiez); % Numero di dati
% Conversione dei punti ADC in dati di pressione

DatiPiez = zeros(rP,rPL);
DatiPiezT = zeros(rP,rPL);

if strcmp(NodoPiezoLink(1,4),'VW kPa') == 1 || strcmp(NodoPiezoLink(1,4),'VW kg/cm2') == 1 % Conversione da digit a kPa e poi in Pa
    kPa = zeros(rP,rPL);
    kg_cm2 = zeros(rP,rPL);
    a = 1.4051*10^(-3);
    b = 2.369*10^(-4);
    c = 9.9*10^(-8);
    % Piezometro
    for i=1:rPL
        digit = (ADCPiez(:,i).^2)/1000;
        Therm = cell2mat(NodoPiezoLink(i,5));
        if strcmp(NodoPiezoLink(i,4),'VW kPa') == 1
            if Therm == 1 && D(1) ~= 0
                DatiPiezT(:,i) = 1./(a+b*reallog(tempPiez(:,i))...
                    +c*(reallog(tempPiez(:,i))).^3)-273.15; % conversione in gradi
                kPa(:,i) = A(i,1)*digit.^2+B(i,1)*digit+C(i,1)+D(i,1)*(DatiPiezT(:,i)-T(1,i)); % conversione in kPa
            else
                kPa(:,i) = A(i,1)*digit.^2+B(i,1)*digit+C(i,1); % conversione in kPa
            end
            DatiPiez(:,i) = kPa(:,i)*1000;
        elseif strcmp(NodoPiezoLink(i,4),'VW kg/cm2') == 1 
            kg_cm2(:,i) = A(i,1)*digit.^2+B(i,1)*digit+C(i,1); % conversione in kg/cm2
            DatiPiez(:,i) = kg_cm2(:,i)*98066.5;
        end
        if Therm == 1
            DatiPiezT(:,i) = 1./(a+b*reallog(tempPiez(:,i))...
                +c*(reallog(tempPiez(:,i))).^3)-273.15; % conversione in gradi
        end
    end
elseif strcmp(NodoPiezoLink(1,4),'Hz') == 1 % Convertitore per DSAS o ModBus
    NomeFileT = ['' IDcentralina '-' DTcatena '-RifPL_T.csv'];
    kPa = zeros(rP,rPL);
    a = 1.4051*10^(-3);
    b = 2.369*10^(-4);
    c = 9.9*10^(-8);
    if D ~= 0
        if NuovoZeroPL == 0 % prima elaborazione
            T = 1./(a+b*reallog(tempPiez(1,:))...
                +c*(reallog(tempPiez(1,:))).^3)-273.15; % conversione in gradi
            csvwrite(NomeFileT,T);
        else
            T = csvread(NomeFileT);
        end
        for i=1:rPL
            Therm = cell2mat(NodoPiezoLink(i,5));
            if Therm == 1
                DatiPiezT(:,i) = 1./(a+b*reallog(tempPiez(:,i))...
                    +c*(reallog(tempPiez(:,i))).^3)-273.15; % conversione in gradi
                 kPa(:,i) = caPiez(i,1)*ADCPiez(:,i)+intPiez(i,1)+D(i,1)*(DatiPiezT(:,i)-T(1,i)); % conversione in kPa
            else
                kPa(:,i) = caPiez(i,1)*ADCPiez(:,i)+intPiez(i,1); % conversione in kPa
            end
        end
    else
        for i=1:rPL
            kPa(:,i) = caPiez(i,1)*ADCPiez(:,i)+intPiez(i,1); % conversione in kPa
        end
    end
    DatiPiez(:,i) = kPa(:,i)*1000; % Pascal
else
    mBar = zeros(rP,rPL);
    for i=1:rPL
        if Lineare == 1 && isnan(intPiez(i,1)) == 1
            mBar(:,i) = caPiez(i,1)*ADCPiez(:,i);
        else
            if Lineare == 1
                mBar(:,i) = caPiez(i,1)*ADCPiez(:,i)+intPiez(i,1); % conversione lineare in mBar
            elseif Lineare == 0
                mBar(:,i) = aPiez(i,1)*ADCPiez(:,i).^2+bPiez(i,1)*ADCPiez(:,i)+cPiez(i,1); % conversione parabolica in mBar
            end
        end
        DatiPiez(:,i) = mBar(:,i)*100;
        DatiPiezT(:,i) = caT(i,1)*tempPiez(:,i)+intT(i,1); % conversione in gradi
    end
end

text = 'Raw Data of Piezo Link converted into physical units correctly. conv_grezziPL function ended';
fprintf(fileID,fmt,text);
fclose(fileID);

end