function [Dati_ThL,ARRAYdateThL,ErrThermLink] = Therm(Dati_ThL,ARRAYdateThL,...
        NuovoZeroThL,ErrThermLink,NdatiMedia,margine,Tmax,Tmin,datainiThL,...
        IDcentralina,DTcatena,FileName)

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

if NuovoZeroThL == 1
    ini = round(NdatiMedia/2);
    if rem(NdatiMedia,2) == 0
        ini = ini+1;
    end
    ini = ini + margine;
    if ini < 6
        ini = 6;
    end
    ErrThermLink = ErrThermLink(ini:end,:)';
    Dati_ThL = Dati_ThL(ini:end,:);
    ARRAYdateThL = ARRAYdateThL(ini:end,1);
else
    ErrThermLink = ErrThermLink';
end

[r,c] = size(Dati_ThL);
FileTemperature = ['' IDcentralina '-' DTcatena '-ThL-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;
textT = '0 correction executed for Therm Link - Temperature filter';
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_ThL(a,b) > Tmax || Dati_ThL(a,b) < Tmin
            cont2 = cont2+1;
            if a == 1
                if isfile(FileTemperature) == 1
                    RawDate = find(DatiRaw(:,1)<=datenum(datainiThL));
                    if isempty(RawDate) == 1
                        cc = 2;
                        while cc <= c
                            if Dati_ThL(cc,b) > Tmax || Dati_ThL(cc,b) < Tmin
                                cc = cc+1;
                            else
                                break
                            end
                        end
                        Dati_ThL(a,b) = Dati_ThL(cc,b);
                    else
                        if isnan(DatiRaw(RawDate(end),b+1)) == 0
                            Dati_ThL(a,b) = DatiRaw(RawDate(end),b+1);
                            ErrThermLink(b,a) = 1;
                            wardat = 'Temperature data of Therm Link nodes corrected using Raw Data of reference Csv file.';
                            fprintf(fileID,fmt,wardat);
                        else
                            cc = 2;
                            while cc <= c
                                if Dati_ThL(cc,b) > Tmax || Dati_ThL(cc,b) < Tmin
                                    cc = cc+1;
                                else
                                    break
                                end
                            end
                            Dati_ThL(a,b) = Dati_ThL(cc,b);
                        end
                    end
                else
                    cc = 2;
                    while cc <= c
                        if Dati_ThL(cc,b) > Tmax || Dati_ThL(cc,b) < Tmin
                            cc = cc+1;
                        else
                            break
                        end
                    end
                    Dati_ThL(a,b) = Dati_ThL(cc,b);
                end
            else
                Dati_ThL(a,b) = Dati_ThL(a-1,b);
                ErrThermLink(b,a) = 1;
            end
            textT = ['' num2str(cont2) ' correction executed for Therm Link - Temperature filter!'];
        end
    end
end

if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
    RawDate1 = find(DatiRaw(:,1)<=ARRAYdateThL(1));
    if isempty(RawDate1) == 1
        RawDate2 = 1;
    elseif RawDate1(end) == rDR
        RawDate2 = find(ARRAYdateThL(:,1)>DatiRaw(end,1));
    else
        RawDate2 = find(ARRAYdateThL(:,1)>DatiRaw(RawDate1(end)+1,1));
    end
else
    RawDate1 = [];
    RawDate2 = 1;
end
if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
    Dati = [DatiRaw(1:RawDate1(end),:); ARRAYdateThL(RawDate2(1):end) Dati_ThL(RawDate2(1):end,:)];
elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
    Dati = [ARRAYdateThL Dati_ThL];
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);

fprintf(fileID,fmt,textT);

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

end