function [Dati_PT100,ARRAYdatePT100,ErrPT100Link] = PT100(Dati_PT100,...
    ARRAYdatePT100,NuovoZeroPT100,ErrPT100Link,NdatiMedia,margine,Tmax,Tmin,...
    datainiPT100,IDcentralina,DTcatena,FileName)

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

if NuovoZeroPT100 == 1
    ini = round(NdatiMedia/2);
    if rem(NdatiMedia,2) == 0
        ini = ini+1;
    end
    ini = ini + margine;
    if ini < 6
        ini = 6;
    end
    ErrPT100Link = ErrPT100Link(ini:end,:)';
    Dati_PT100 = Dati_PT100(ini:end,:);
    ARRAYdatePT100 = ARRAYdatePT100(ini:end,1);
else
    ErrPT100Link = ErrPT100Link';
end

[r,c] = size(Dati_PT100);
FileTemperature = ['' IDcentralina '-' DTcatena '-PT100-Therm.csv'];
if isfile(FileTemperature) == 1
    DatiRaw = csvread(FileTemperature);
    [rDR,cDR] = size(DatiRaw);
    DatiRaw(:,1) = DatiRaw(:,1) + 730000;
else
    rDR = 1;
    cDR = 1;
end
cont2 = 1;
for a = 1:r % Data
    for b = 1:c % Nodo
        % NON considero i dati al di sopra dei 80 °C o al di sotto dei -30 °C!
        if Dati_PT100(a,b) > Tmax || Dati_PT100(a,b) < Tmin
            cont2 = cont2+1;
            if a == 1
                if isfile(FileTemperature) == 1
                    RawDate = find(DatiRaw(:,1)<=datenum(datainiPT100));
                    if isempty(RawDate) == 1
                        cc = 2;
                        while cc <= c
                            if Dati_PT100(cc,b) > Tmax || Dati_PT100(cc,b) < Tmin
                                cc = cc+1;
                            else
                                break
                            end
                        end
                        Dati_PT100(a,b) = Dati_PT100(cc,b);
                    else
                        if isnan(DatiRaw(RawDate(end),b+1)) == 0
                            Dati_PT100(a,b) = DatiRaw(RawDate(end),b+1);
                            ErrPT100Link(b,a) = 1;
                            wardat = 'Temperature data of PT100 Link nodes corrected using Raw Data of reference Csv file.';
                            fprintf(fileID,fmt,wardat);
                        else
                            cc = 2;
                            while cc <= c
                                if Dati_PT100(cc,b) > Tmax || Dati_PT100(cc,b) < Tmin
                                    cc = cc+1;
                                else
                                    break
                                end
                            end
                            Dati_PT100(a,b) = Dati_PT100(cc,b);
                        end
                    end
                else
                    cc = 2;
                    while cc <= c
                        if Dati_PT100(cc,b) > Tmax || Dati_PT100(cc,b) < Tmin
                            cc = cc+1;
                        else
                            break
                        end
                    end
                    Dati_PT100(a,b) = Dati_PT100(cc,b);
                end
            else
                Dati_PT100(a,b) = Dati_PT100(a-1,b);
                ErrPT100Link(b,a) = 1;
            end
            textT = ['' num2str(cont2) ' correction executed for PT100 Link - Temperature filter!'];
            fprintf(fileID,fmt,textT);
        end
    end
end

if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
    RawDate1 = find(DatiRaw(:,1)<=ARRAYdatePT100(1));
    if isempty(RawDate1) == 1
        RawDate2 = 1;
    elseif RawDate1(end) == rDR
        RawDate2 = find(ARRAYdatePT100(:,1)>DatiRaw(end,1));
    else
        RawDate2 = find(ARRAYdatePT100(:,1)>DatiRaw(RawDate1(end)+1,1));
    end
else
    RawDate1 = [];
    RawDate2 = 1;
end
if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
    Dati = [DatiRaw(1:RawDate1(end),:); ARRAYdatePT100(RawDate2(1):end) Dati_PT100(RawDate2(1):end,:)];
elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
    Dati = [ARRAYdatePT100 Dati_PT100];
else
    Dati = DatiRaw;
end
% Elimino appoggio più vecchio di un mese
RawDate3 = find(Dati(:,1)<now-30);
if isempty(RawDate3) == 0
    [rDati,~] = size(Dati);
    if RawDate3(end) == rDati
    else
        Dati = Dati(RawDate3(end)+1:end,:);
    end
end
if isfile(FileTemperature) == 1
    delete(FileTemperature);
end
Dati(:,1) = Dati(:,1) - 730000;
csvwrite(FileTemperature,Dati);


text = 'PT100 Link elaborated correctly. PT100 function closed';
fprintf(fileID,fmt,text);
fclose(fileID);

end