function [ANGdefKLHR,TdefKLHR,ErrKlinoLinkHR,ARRAYdateKLHR] = KlinoHR(ANGdefKLHR,...
    TdefKLHR,ARRAYdateKLHR,ErrKlinoLinkHR,NuovoZeroKLHR,NodoKlinoLinkHR,NdatiMedia,...
    Ndatidespike,Tmax,Tmin,datainiKLHR,rKLHR,IDcentralina,DTcatena,margine,FileName)

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

if NuovoZeroKLHR == 1
    if NdatiMedia > Ndatidespike
        Ndati = NdatiMedia;
    else
        Ndati = Ndatidespike;
    end
    ini = round(Ndati/2)+1;
    if rem(Ndati,2) == 0
        ini = ini+1;
    end
    clear NDati
    ini = ini + margine;
    if ini < 6
        ini = 6;
    end
    ErrKlinoLinkHR = ErrKlinoLinkHR(ini:end,:);
    ANGdefKLHR = ANGdefKLHR(ini:end,:);
    TdefKLHR = TdefKLHR(ini:end,:);
    ARRAYdateKLHR = ARRAYdateKLHR(ini:end,1); 
end

if strcmp(NodoKlinoLinkHR(1,4),'IPTM') == 1
else
    %% Controllo della temperatura
    FileTemperature = ['' IDcentralina '-' DTcatena '-KLHR-Therm.csv'];
    if isfile(FileTemperature) == 1
        DatiRaw = csvread(FileTemperature);
        [rDR,cDR] = size(DatiRaw);
        DatiRaw(:,1) = DatiRaw(:,1) + 730000;
    else
        rDR = 1;
        cDR = 1;
    end    
    [r,c] = size(ANGdefKLHR);
    contT = 1; % contatore
    TempDef = TdefKLHR';
    Angolo = ANGdefKLHR';
    textT = 'There are not correction of Klino Link HR based on temperature filter';
    for j = 1:r % Data
        nodo = 1;
        jj = 6;
        for i = 1:c/2 % Nodo
            % NON considero i dati al di sopra di TempMAX °C o al di sotto di TempMIN °C!
            if TempDef(i,j) > Tmax || TempDef(i,j) < Tmin
                if j == 1
                    if isfile(FileTemperature) == 1
                        RawDate = find(DatiRaw(:,1)<=datenum(datainiKLHR));
                        if isempty(RawDate) == 1
                            cc = 2;
                            while cc <= Ndate
                                if TempDef(i,cc) > Tmax || TempDef(i,cc) < Tmin
                                    cc = cc+1;
                                else
                                    break
                                end
                            end
                            TempDef(i,j) = TempDef(i,cc);
                        else
                            if isnan(DatiRaw(RawDate(end),jj)) == 0
                                TempDef(i,j) = DatiRaw(RawDate(end),j+1);
                                ErrKlinoLinkHR(j,jj) = 0.5;
                                wardat = 'Temperature data of Klino Link HR nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                            else
                                cc = 2;
                                while cc <= c
                                    if TempDef(i,cc) > Tmax || TempDef(i,cc) < Tmin
                                        cc = cc+1;
                                    else
                                        break
                                    end
                                end
                                TempDef(i,j) = TempDef(i,cc);
                            end
                        end
                    else
                        cc = 2;
                        while cc <= c
                            if TempDef(i,cc) > Tmax || TempDef(i,cc) < Tmin
                                cc = cc+1;
                            else
                                break
                            end
                        end
                        TempDef(i,j) = TempDef(i,cc);
                    end
                else
                    Angolo(nodo:nodo+1,j) = Angolo(nodo:nodo+1,j-1);
                    TempDef(i,j) = TempDef(i,j-1);
                    ErrKlinoLinkHR(j,jj) = 0.5;
                end
            end
            textT = ['' num2str(contT) ' correction executed for Klino Link HR - Temperature filter!'];
            jj = jj+6;
        end
    end
    
    if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
        RawDate1 = find(DatiRaw(:,1)<=ARRAYdateKLHR(1));
        if isempty(RawDate1) == 1
            RawDate2 = 1;
        elseif RawDate1(end) == rDR
            RawDate2 = find(ARRAYdateKLHR(:,1)>DatiRaw(end,1));
        else
            RawDate2 = find(ARRAYdateKLHR(:,1)>DatiRaw(RawDate1(end)+1,1));
        end
    else
        RawDate1 = [];
        RawDate2 = 1;
    end
    if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
        Dati = [DatiRaw(1:RawDate1(end),:); ARRAYdateKLHR(RawDate2(1):end,1) TempDef(:,RawDate2(1):end)'];
    elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
        Dati = [ARRAYdateKLHR TempDef'];
    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);
    
    TdefKLHR = TempDef';
    ANGdefKLHR = Angolo';
    fprintf(fileID,fmt,textT);

    %% Calcolo differenziali
    NomeFile = ['' IDcentralina '-' DTcatena '-RifKLHR.csv'];
    if NuovoZeroKLHR == 0 % prima elaborazione
        csvwrite(NomeFile,ANGdefKLHR(1,:));
        ANGdefKLHR = ANGdefKLHR - ANGdefKLHR(1,:);
    else % Ci sono gią dei dati elaborati
        RIF = csvread(NomeFile);
        ANGdefKLHR = ANGdefKLHR - RIF;
    end
end

%% Matrice Errori
[r,~] = size(ErrKlinoLinkHR);
Matrice_err = zeros(r,rKLHR);
for i = 1:r % date
    d = 1;
    if strcmp(NodoKlinoLinkHR(1,4),'IPTM') == 1
        for n = 1:rKLHR % nodi
            j = 1;
            err = ErrKlinoLinkHR(i,d:d+2);
            while j <= 3
                if err(1,j) == 1
                    Matrice_err(i,n) = 1;
                end
                j = j+1;
            end
            d = d+3;
        end
    else
        for n = 1:rKLHR % nodi
            j = 1;
            err = ErrKlinoLinkHR(i,d:d+5);
            while j <= 6
                if err(1,j) == 1
                    Matrice_err(i,n) = 1;
                end
                j = j+1;
            end
            d = d+6;
        end
    end
end
ErrKlinoLinkHR = Matrice_err';
   
text = 'Klino Link HR elaborated correctly. KlinoHR function closed';
fprintf(fileID,fmt,text);
fclose(fileID);

end