function [alert,alert_local,alarm,alarm_local,Date,RQ,RQ_Loc,DataFuK,...
    DataFuK_local,Tr_mail,Tr_Loc_mail,Tr_Inf,Tr_Inf_Loc,Tr_Sup,Tr_Sup_Loc,...
    Tr_Loc_HR,DataFuK_local_HR,Number,Number_Loc] = Fukuzono(IDcentralina,...
    DTcatena,date,ARRAYdate,yes3D,rNL,NodoTilt,NodoTiltHR,NodoTiltHR3D,conn,FileName)

fileID = fopen(FileName,'a');
fmt = '%s \r';

text = 'Fukuzono function started';
fprintf(fileID,fmt,text);

Tr_Loc_HR = [];
DataFuK_local_HR = [];

%% Scarico i dati da DB
data_rif = ARRAYdate(end)-30; %30
if data_rif > datenum(date)
    data = datestr(data_rif,'yyyy-mm-dd');
else
    data = date;
end
for n = 1:rNL
    NodeNum = num2str(cell2mat(NodoTilt(n,2)));
    comando = ['select EventDate, EventTime, HShift_local, HShift from ElabDataView where EventDate >= ''' ...
        data ''' and  ToolNameID = ''' DTcatena ''' and UnitName = ''' IDcentralina ...
        ''' and NodeNum = ''' NodeNum ''' '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Dati = curs.Data;
    % Modifico il formato di data e ora in DATini.
    [rD,~] = size(Dati);
    T = [cell2mat(Dati(:,1)) repmat(' ', [rD,1]) cell2mat(Dati(:,2))];
    Date = datenum(T); % Data
    HShift_local(:,n) = cell2mat(Dati(:,3)); % Loc 2D
    HShift(:,n) = cell2mat(Dati(:,4)); % Cum 2D
end

%% Criterio della velocità positiva - Criterio 0
[rS,cS] = size(HShift);
Criterio_0 = zeros(rS-1,cS);
Criterio_0_local = zeros(rS-1,cS);
Speed = zeros(rS-1,cS);
Speed_local = zeros(rS-1,cS);
for i = 1:cS % nodi
    for j = 4:rS-1 % date
        % Cumulati
        Speed(j,i) = (HShift(j+1,i) - HShift(j,i))/(Date(j+1)-Date(j)); % m/g
        if j == 4
            Speed(j-1,i) = (HShift(j,i) - HShift(j-1,i))/(Date(j)-Date(j-1)); % m/g
            Speed(j-2,i) = (HShift(j-1,i) - HShift(j-2,i))/(Date(j-1)-Date(j-2)); % m/g
            Speed(j-3,i) = (HShift(j-2,i) - HShift(j-3,i))/(Date(j-2)-Date(j-3)); % m/g
            Speed_local(j-1,i) = (HShift_local(j,i) - HShift_local(j-1,i))/(Date(j)-Date(j-1)); % m/g
            Speed_local(j-2,i) = (HShift_local(j-1,i) - HShift_local(j-2,i))/(Date(j-1)-Date(j-2)); % m/g
            Speed_local(j-3,i) = (HShift_local(j-2,i) - HShift_local(j-3,i))/(Date(j-2)-Date(j-3)); % m/g
        end
        if Speed(j,i) > 0 && Speed(j-1,i) > 0 && Speed(j-2,i) > 0 && Speed(j-3,i) > 0
            Criterio_0(j,i) = 1;
        end
        % Locali
        Speed_local(j,i) = (HShift_local(j+1,i) - HShift_local(j,i))/(Date(j+1)-Date(j)); % m/g
        if Speed_local(j,i) > 0 && Speed_local(j-1,i) > 0 && Speed_local(j-2,i) > 0 && Speed_local(j-3,i) > 0
            Criterio_0_local(j,i) = 1;
        end

    end
end
Date = Date(2:end);
text = 'Criterion 0 executed';
fprintf(fileID,fmt,text);

%% Faccio i calcoli
[r,c] = size(Speed_local);
Delta_Speed_local = zeros(r,c);
Delta_Speed = zeros(r,c);
diff_Cum = diff(Speed);
diff_Loc = diff(Speed_local);
Delta_Speed(2:end,:) = diff_Cum;
Delta_Speed_local(2:end,:) = diff_Loc;
act = zeros(r,c);
act_local = zeros(r,c);
for i = 1:c % nodi
    for j = 2:r % date
        if Delta_Speed(j,i) > 0
            act(j,i) = 1;
        elseif Delta_Speed(j,i) < 0
            act(j,i) = -1;
        end
        if Delta_Speed_local(j,i) > 0
            act_local(j,i) = 1;
        elseif Delta_Speed_local(j,i) < 0
            act_local(j,i) = -1;
        end
    end
end

%% Criterio della Velocità Crescente - Criterio 1
Criterio_1 = zeros(r,c);
Criterio_1_local = zeros(r,c);
alert = zeros(r,c);
alert_local = zeros(r,c);
if r > 10
    for i = 1:c % nodi
        for j = 10:r % date
            if act(j,i) == 1 && act(j-1,i) == 1 && act(j-2,i) == 1 && act(j-3,i) == 1 || ...
                    act(j,i) == 1  && act(j-1,i) == 1 && act(j-2,i) == 1 || ...
                    act(j,i) == 1  && act(j-1,i) == 1 && act(j-3,i) == 1 || ...
                    act(j,i) == 1  && act(j-2,i) == 1 && act(j-3,i) == 1 || ...
                    act(j-1,i) == 1 && act(j-2,i) == 1 && act(j-3,i) == 1
                if Criterio_0(j,i) == 1
                    Criterio_1(j,i) = 1;
                    if act(j,i) == 1 && act(j-1,i) == 1 && act(j-2,i) == 1 && act(j-3,i) == 1 && act(j-4,i) == 1
                        alert(j,i) = 1;
                    end
                end
            end
            if act_local(j,i) == 1 && act_local(j-1,i) == 1 && act_local(j-2,i) == 1 && act_local(j-3,i) == 1 || ...
                    act_local(j,i) == 1 && act_local(j-1,i) == 1 && act_local(j-2,i) == 1 || ...
                    act_local(j,i) == 1 && act_local(j-1,i) == 1 && act_local(j-3,i) == 1 || ...
                    act_local(j,i) == 1 && act_local(j-2,i) == 1 && act_local(j-3,i) == 1 || ...
                    act_local(j-1,i) == 1 && act_local(j-2,i) == 1 && act_local(j-3,i) == 1
                if Criterio_0_local(j,i) == 1
                    Criterio_1_local(j,i) = 1;
                    if act_local(j,i) == 1 && act_local(j-1,i) == 1 && act_local(j-2,i) == 1 && act_local(j-3,i) == 1 && act_local(j-4,i) == 1
                        alert_local(j,i) = 1;
                    end
                end
            end
        end
    end
    text = 'Criterion 1 executed';
    fprintf(fileID,fmt,text);

    %% Criterio delle parabole - Criterio 2
    a = zeros(r,c);
    a_local = zeros(r,c);
    Criterio_2 = zeros(r,c);
    Criterio_2_local = zeros(r,c);
    for i = 1:c % nodi
        for j = 13:r % date
            if Criterio_1(j,i) == 1
                % j-3
                x = Date(j-12:j-3);
                y = Speed(j-12:j-3,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a(j-3,i) = coeff(1,1);
                % j-2
                x = Date(j-11:j-2);
                y = Speed(j-11:j-2,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a(j-2,i) = coeff(1,1);
                % j-1
                x = Date(j-10:j-1);
                y = Speed(j-10:j-1,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a(j-1,i) = coeff(1,1);
                % j
                x = Date(j-9:j);
                y = Speed(j-9:j,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a(j,i) = coeff(1,1);
                % Criterio 2
                if a(j,i) > 0 && a(j-1,i) > 0 && a(j-2,i) > 0 && a(j-3,i) > 0 ||...
                        a(j,i) > 0 && a(j-1,i) > 0 && a(j-2,i) > 0 ||...
                        a(j,i) > 0 && a(j-1,i) > 0 && a(j-3,i) > 0 ||...
                        a(j,i) > 0 && a(j-2,i) > 0 && a(j-3,i) > 0 ||...
                        a(j-1,i) > 0 && a(j-2,i) > 0 && a(j-3,i) > 0

                    Criterio_2(j,i) = 1;
                end
            end
            if Criterio_1_local(j,i) == 1
                % j-3
                x = Date(j-12:j-3);
                y = Speed_local(j-12:j-3,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a_local(j-3,i) = coeff(1,1);
                % j-2
                x = Date(j-11:j-2);
                y = Speed_local(j-11:j-2,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a_local(j-2,i) = coeff(1,1);
                % j-1
                x = Date(j-10:j-1);
                y = Speed_local(j-10:j-1,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a_local(j-1,i) = coeff(1,1);
                % j
                x = Date(j-9:j);
                y = Speed_local(j-9:j,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a_local(j,i) = coeff(1,1);
                if a_local(j,i) > 0 && a_local(j-1,i) > 0 && a_local(j-2,i) > 0 && a_local(j-3,i) > 0 || ...
                        a_local(j,i) > 0 && a_local(j-1,i) > 0 && a_local(j-2,i) > 0 || ...
                        a_local(j,i) > 0 && a_local(j-1,i) > 0 && a_local(j-3,i) > 0 || ...
                        a_local(j,i) > 0 && a_local(j-2,i) > 0 && a_local(j-3,i) > 0 || ...
                        a_local(j-1,i) > 0 && a_local(j-2,i) > 0 && a_local(j-3,i) > 0

                    Criterio_2_local(j,i) = 1;
                end
            end
        end
    end
    text = 'Criterion 2 executed';
    fprintf(fileID,fmt,text);

    %% Criterio della concavità crescente - Criterio 3
    Delta_a = zeros(r,c);
    Delta_a_local = zeros(r,c);
    diff_a = diff(a);
    diff_a_Loc = diff(a_local);
    Delta_a(2:end,:) = diff_a;
    Delta_a_local(2:end,:) = diff_a_Loc;
    Criterio_3 = zeros(r,c);
    Criterio_3_local = zeros(r,c);
    for i = 1:c % nodi
        for j = 13:r % date
            if Criterio_2(j,i) == 1
                if Delta_a(j,i) > 0 && Delta_a(j-1,i) > 0 && Delta_a(j-2,i) > 0 && Delta_a(j-3,i) > 0 || ...
                        Delta_a(j,i) > 0 && Delta_a(j-1,i) > 0 && Delta_a(j-2,i) > 0 || ...
                        Delta_a(j,i) > 0 && Delta_a(j-1,i) > 0 && Delta_a(j-3,i) > 0 || ...
                        Delta_a(j,i) > 0 && Delta_a(j-2,i) > 0 && Delta_a(j-3,i) > 0 || ...
                        Delta_a(j-1,i) > 0 && Delta_a(j-2,i) > 0 && Delta_a(j-3,i) > 0

                    Criterio_3(j,i) = 1;
                end
            end
            if Criterio_2_local(j,i) == 1
                if Delta_a_local(j,i) > 0 && Delta_a_local(j-1,i) > 0 && Delta_a_local(j-2,i) > 0 && Delta_a_local(j-3,i) > 0 ||...
                        Delta_a_local(j,i) > 0 && Delta_a_local(j-1,i) > 0 && Delta_a_local(j-2,i) > 0 ||...
                        Delta_a_local(j,i) > 0 && Delta_a_local(j-1,i) > 0 && Delta_a_local(j-3,i) > 0 ||...
                        Delta_a_local(j,i) > 0 && Delta_a_local(j-2,i) > 0 && Delta_a_local(j-3,i) > 0 ||...
                        Delta_a_local(j-1,i) > 0 && Delta_a_local(j-2,i) > 0 && Delta_a_local(j-3,i) > 0

                    Criterio_3_local(j,i) = 1;
                end
            end
        end
    end
    text = 'Criterion 3 executed';
    fprintf(fileID,fmt,text);

    %% Criterio di Fukuzono
    alarm = zeros(r,c);
    alarm_local = zeros(r,c);
    RQ = zeros(r,c);
    Number = zeros(r,c);
    RQ_Loc = zeros(r,c);
    Number_Loc = zeros(r,c);
    Tr = zeros(r,c) - 99;
    Tr_Inf = zeros(r,c) - 99;
    Tr_Sup = zeros(r,c) - 99;
    Tr_mail = zeros(r,c) - 99;
    Tr_Loc = zeros(r,c) - 99;
    Tr_Inf_Loc = zeros(r,c) - 99;
    Tr_Sup_Loc = zeros(r,c) - 99;
    Tr_Loc_mail = zeros(r,c) - 99;
    DataFuK = zeros(r,c);
    DataFuK_local = zeros(r,c);
    for i = 1:c % nodi
        j = 13;
        % -------------- Cumulati --------------------------
        while j <=(r-1)
            hold off
            GIGI = 0;
            if Criterio_3(j,i) == 1
                Tempo = Date(j-3:j); % giorni
                Vel = 1000*Speed(j-3:j,i); % mm/g
                Vel(Vel==0) = 0.0000000001;
                Inv_Vel = 1./Vel;
                [f,rsquare] = fit(Tempo,Inv_Vel,'poly1');
                retta = coeffvalues(f);
                m = retta(1,1); % m
                q = retta(1,2); % q
                % Time of failure
                Tr(j,i) = -q/m - Date(j);
                if Tr(j,i) > 0 && Tr(j,i) < 30 % giorni
                    % RQ
                    RQ(j,i) = rsquare.rsquare;
                    % Grafico Inv V - T
                    Data_Img = datestr(Date(j-3),'dd-mm-yyyy');
                    Tempo_Img = datestr(Date(j-3),'HH-MM-SS');
                    fig = plot(Tempo,Inv_Vel,'-o','LineStyle','none');
                    hold on
                    legend(num2str(RQ(j,i)));
                    Y = m*Tempo + q;
                    plot(Tempo,Y);
                    title('Time of Failure prediction model');
                    xlabel('Date [dd/mm/yyyy HH:MM]')
                    ylabel('Inverse of Velocity [d/mm]')
                    datetick('x','dd/mm/yyyy HH:MM')
                    ylim([0 max(Inv_Vel)+max(Inv_Vel)/10])
                    % Grafico Vel - T
                    hold off
                    fig2 = plot(Tempo,Vel,'-o','LineStyle','none');
                    title('Time of Failure prediction model');
                    xlabel('Date [dd/mm/yyyy HH:MM]')
                    ylabel('Velocity [mm/d]')
                    datetick('x','dd/mm/yyyy HH:MM')
                    ylim([0 max(Vel)+max(Vel)/10])
                end
                % dato successivo
                for n = 1:(r-j)
                    if Criterio_3(n+j,i) == 1 || Criterio_3(n+j-1,i) == 1
                        Tempo = Date(j-3:j+n); % giorni
                        Vel = 1000*Speed(j-3:j+n,i); % mm/g
                        Vel(Vel==0) = 0.0000000001;
                        Inv_Vel = 1./Vel;
                        [f,rsquare] = fit(Tempo,Inv_Vel,'poly1');
                        RQ(j+n,i) = rsquare.rsquare;
                        retta = coeffvalues(f);
                        m = retta(1,1);
                        q = retta(1,2);
                        % Time of Failure
                        Tr(j+n,i) = -q/m - Date(j+n);
                        if Tr(j+n,i) > 0 && Tr(j+n,i) < 30 % giorni
                            if RQ(j+n,i) >= 0.85
                                Tr_mail(j+n,i) = -q/m - Date(j+n);% attivo
                                Tr_mail(j+n-1,i) = -99; % disattivo
                                alarm(j+n-1,i) = 0; % disattivo
                                alarm(j+n,i) = 1; % attivo
                                DataFuK(j+n,i) = Date(j-3);
                                DataFuK(j,i) = 0;
                                Data_Img = datestr(Date(j-3),'dd-mm-yyyy');
                                Tempo_Img = datestr(Date(j-3),'HH-MM-SS');
                                % Grafico asintotico
                                hold off
                                fig2 = plot(Tempo,Vel,'-o','LineStyle','none');
                                title('Time of Failure prediction model');
                                xlabel('Date [dd/mm/yyyy HH:MM]')
                                ylabel('Velocity [mm/d]')
                                datetick('x','dd/mm/yyyy HH:MM')
                                ylim([0 max(Vel)+max(Vel)/10])
                                Nodo = cell2mat(NodoTilt(i,2));
                                filename = strcat(IDcentralina,'-',DTcatena,'-Node_',num2str(Nodo),'-',Data_Img,'_',Tempo_Img,'_Vel.jpg');
                                saveas(fig2,filename)
                                % Grafico inverso velocità
                                fig = plot(Tempo,Inv_Vel,'-o','LineStyle','none');
                                hold on
                                legend(num2str(RQ(j+n,i)));
                                Y = m*Tempo + q;
                                Y = [Y; 0];
                                Tempo_End = -q/m;
                                Tempo_G = [Tempo; Tempo_End];
                                plot(Tempo_G,Y);
                                title('Time of Failure prediction model');
                                xlabel('Date [dd/mm/yyyy HH:MM]')
                                ylabel('Inverse of Velocity [d/mm]')
                                datetick('x','dd/mm/yyyy HH:MM')
                                ylim([0 max(Inv_Vel)+max(Inv_Vel)/10])
                                LIM = floor(fig.XData(1));
                                Nodo = cell2mat(NodoTilt(i,2));
                                % Variazione statistica dei dati di
                                % monitoraggio
                                % Limite Superiore
                                Inv_Vel_Sup = Inv_Vel + mean(Inv_Vel)*(1-RQ(j+n,i));
                                [f,rsquare] = fit(Tempo,Inv_Vel_Sup,'poly1');
                                retta = coeffvalues(f);
                                m_Sup = retta(1,1);
                                q_Sup = retta(1,2);
                                Tr_Sup(j+n,i) = -q_Sup/m_Sup;
                                Tr_Sup(j+n-1,i) = 99;
                                Y_Sup = m_Sup*Tempo + q_Sup;
                                Y_Sup = [Y_Sup; 0];
                                Tempo_End = -q_Sup/m_Sup;
                                Tempo_G = [Tempo; Tempo_End];
                                plot(Tempo_G,Y_Sup,'--');
                                % Limite Inferiore
                                Inv_Vel_Inf = Inv_Vel - mean(Inv_Vel)*(1-RQ(j+n,i));
                                [f,rsquare] = fit(Tempo,Inv_Vel_Inf,'poly1');
                                retta = coeffvalues(f);
                                m_Inf = retta(1,1);
                                q_Inf = retta(1,2);
                                Tr_Inf(j+n,i) = -q_Inf/m_Inf;
                                Tr_Inf(j+n-1,i) = 99;
                                Y_Inf = m_Inf*Tempo + q_Inf;
                                Y_Inf = [Y_Inf; 0];
                                Tempo_End = -q_Inf/m_Inf;
                                Tempo_G = [Tempo; Tempo_End];
                                plot(Tempo_G,Y_Inf,'--');
                                datetick('x','dd/mm/yyyy HH:MM')
                                GIGI = 1;
                                N = n;
                                Number(j+N,i) = N+4;
                            end
                        end
                    else
                        break
                    end
                end
                if GIGI == 1
                    hold on
                    fig = plot(Date(j+N)+Tr(j+N,i),0,'-d','LineStyle','none','Color','r');
                    plot(Tr_Sup(j+N,i),0,'-v','LineStyle','none','Color','k');
                    LIM_S = ceil(Tr_Sup(j+N,i));
                    plot(Tr_Inf(j+N,i),0,'-v','LineStyle','none','Color','k');
                    legend(['RQ: ' num2str(RQ(j+N,i))]);
                    h = gca; % Get axis to modify
                    h.XAxis.MinorTick = 'on'; % Must turn on minor ticks if they are off
                    h.XAxis.MinorTickValues = LIM(1):1/24:LIM_S(end); % Minor ticks which don't line up with majors
                    datetick('x','dd/mm/yyyy HH:MM')
                    grid on
                    grid minor
                    filename = strcat(IDcentralina,'-',DTcatena,'-Node_',num2str(Nodo),'-',Data_Img,'_',Tempo_Img,'.jpg');
                    saveas(fig,filename)
                end
                j = j+n+1;
            else
                j = j+1;
            end
        end
        % -------------- Locali --------------------------
        j = 13;
        while j <=(r-1)
            hold off
            DIBBA = 0;
            if Criterio_3_local(j,i) == 1
                Tempo = Date(j-3:j); % giorni
                Vel = 1000*Speed_local(j-3:j,i); % mm/g
                Vel(Vel==0) = 0.0000000001;
                Inv_Vel = 1./Vel;
                [f,rsquare] = fit(Tempo,Inv_Vel,'poly1');
                retta = coeffvalues(f);
                m = retta(1,1);
                q = retta(1,2);
                % Time of Failure
                Tr_Loc(j,i) = -q/m - Date(j);
                if Tr_Loc(j,i) > 0 && Tr_Loc(j,i) < 30
                    % RQ
                    RQ_Loc(j,i) = rsquare.rsquare;
                    % Grafico Inv V - T
                    Data_Img = datestr(Date(j-3),'dd-mm-yyyy');
                    Tempo_Img = datestr(Date(j-3),'HH-MM-SS');
                    % Grafico
                    fig = plot(Tempo,Inv_Vel,'-o','LineStyle','none');
                    hold on
                    legend(num2str(RQ_Loc(j,i)));
                    Y = m*Tempo + q;
                    plot(Tempo,Y);
                    title('Time of Failure prediction model');
                    xlabel('Date [dd/mm/yyyy HH:MM]')
                    ylabel('Inverse of Velocity [d/mm]')
                    datetick('x','dd/mm/yyyy HH:MM')
                    ylim([0 max(Inv_Vel)+max(Inv_Vel)/10])
                    % Grafico Vel - T
                    hold off
                    fig2 = plot(Tempo,Vel,'-o','LineStyle','none');
                    title('Time of Failure prediction model');
                    xlabel('Date [dd/mm/yyyy HH:MM]')
                    ylabel('Velocity [mm/d]')
                    datetick('x','dd/mm/yyyy HH:MM')
                    ylim([0 max(Vel)+max(Vel)/10])
                end
                % dato successivo
                for n = 1:(r-j)
                    if Criterio_3_local(n+j,i) == 1 || Criterio_3_local(n+j-1,i) == 1
                        Tempo = Date(j-3:j+n); % giorni
                        Vel = 1000*Speed_local(j-3:j+n,i); % mm/g
                        Vel(Vel==0) = 0.0000000001;
                        Inv_Vel = 1./Vel;
                        [f,rsquare] = fit(Tempo,Inv_Vel,'poly1');
                        RQ_Loc(j+n,i) = rsquare.rsquare;
                        retta = coeffvalues(f);
                        m = retta(1,1);
                        q = retta(1,2);
                        % Time of Failure
                        Tr_Loc(j+n,i) = -q/m - Date(j+n);
                        if Tr_Loc(j+n,i) > 0 && Tr_Loc(j+n,i) < 30 % giorni
                            if RQ_Loc(j+n,i) >= 0.85
                                Tr_Loc_mail(j+n,i) = -q/m - Date(j+n);
                                Tr_Loc_mail(j+n-1,i) = -99; % disattivo
                                alarm_local(j+n-1,i) = 0; % disattivo
                                alarm_local(j+n,i) = 1; % attivo
                                DataFuK_local(j+n,i) = Date(j-3);
                                DataFuK_local(j,i) = 0;
                                Data_Img = datestr(Date(j-3),'dd-mm-yyyy');
                                Tempo_Img = datestr(Date(j-3),'HH-MM-SS');
                                % Grafico Asintotico
                                hold off
                                fig2 = plot(Tempo,Vel,'-o','LineStyle','none');
                                title('Time of Failure prediction model');
                                xlabel('Date [dd/mm/yyyy HH:MM]')
                                ylabel('Velocity [mm/d]')
                                datetick('x','dd/mm/yyyy HH:MM')
                                ylim([0 max(Vel)+max(Vel)/10])
                                Nodo = cell2mat(NodoTilt(i,2));
                                filename = strcat(IDcentralina,'-',DTcatena,'-Node_',num2str(Nodo),'-',Data_Img,'_',Tempo_Img,'-Local_Vel.jpg');
                                saveas(fig2,filename)
                                % Grafico inverso velocità
                                fig = plot(Tempo,Inv_Vel,'-o','LineStyle','none');
                                hold on
                                legend(num2str(RQ_Loc(j+n,i)));
                                Y = m*Tempo + q;
                                Y = [Y; 0];
                                Tempo_End = -q/m;
                                Tempo_G = [Tempo; Tempo_End];
                                plot(Tempo_G,Y);
                                title('Time of Failure prediction model');
                                xlabel('Date [dd/mm/yyyy HH:MM]')
                                ylabel('Inverse of Velocity [d/mm]')
                                datetick('x','dd/mm/yyyy HH:MM')
                                ylim([0 max(Inv_Vel)+max(Inv_Vel)/10])
                                LIM = floor(fig.XData(1));
                                Nodo = cell2mat(NodoTilt(i,2));
                                % Variazione statistica dei dati di
                                % monitoraggio
                                % Limite Superiore
                                Inv_Vel_Sup = Inv_Vel + mean(Inv_Vel)*(1-RQ_Loc(j+n,i));
                                [f,rsquare] = fit(Tempo,Inv_Vel_Sup,'poly1');
                                retta = coeffvalues(f);
                                m_Sup = retta(1,1);
                                q_Sup = retta(1,2);
                                Tr_Loc_Sup(j+n,i) = -q_Sup/m_Sup;
                                Tr_Loc_Sup(j+n-1,i) = 99;
                                Y_Sup = m_Sup*Tempo + q_Sup;
                                Y_Sup = [Y_Sup; 0];
                                Tempo_End = -q_Sup/m_Sup;
                                Tempo_G = [Tempo; Tempo_End];
                                plot(Tempo_G,Y_Sup,'--');
                                % Limite Inferiore
                                Inv_Vel_Inf = Inv_Vel - mean(Inv_Vel)*(1-RQ_Loc(j+n,i));
                                [f,rsquare] = fit(Tempo,Inv_Vel_Inf,'poly1');
                                retta = coeffvalues(f);
                                m_Inf = retta(1,1);
                                q_Inf = retta(1,2);
                                Tr_Loc_Inf(j+n,i) = -q_Inf/m_Inf;
                                Tr_Loc_Inf(j+n-1,i) = 99;
                                Y_Inf = m_Inf*Tempo + q_Inf;
                                Y_Inf = [Y_Inf; 0];
                                Tempo_End = -q_Inf/m_Inf;
                                Tempo_G = [Tempo; Tempo_End];
                                plot(Tempo_G,Y_Inf,'--');
                                datetick('x','dd/mm/yyyy HH:MM')
                                DIBBA = 1;
                                N = n;
                                Number_Loc(j+N,i) = N+4;
                            end
                        end
                    else
                        break
                    end
                end
                if DIBBA == 1
                    hold on
                    fig = plot(Date(j+N)+Tr_Loc(j+N,i),0,'-d','LineStyle','none','Color','r');
                    plot(Tr_Loc_Sup(j+N,i),0,'-v','LineStyle','none','Color','k');
                    LIM_S = ceil(Tr_Sup(j+N,i));
                    plot(Tr_Loc_Inf(j+N,i),0,'-v','LineStyle','none','Color','k');
                    legend(['RQ: ' num2str(RQ_Loc(j+N,i))]);
                    h = gca; % Get axis to modify
                    h.XAxis.MinorTick = 'on'; % Must turn on minor ticks if they are off
                    h.XAxis.MinorTickValues = LIM(1):1/24:LIM_S(end); % Minor ticks which don't line up with majors
                    datetick('x','dd/mm/yyyy HH:MM')
                    grid on
                    grid minor
                    filename = strcat(IDcentralina,'-',DTcatena,'-Node_',num2str(Nodo),'-',Data_Img,'_',Tempo_Img,'-Local.jpg');
                    saveas(fig,filename)
                    if yes3D == 1
                        HRindex = find(cell2mat(NodoTiltHR3D(:,2))==Nodo);
                        if isempty(HRindex) ~= 1
                            NodeHR = NodoTiltHR3D(HRindex,3);
                            HRprog = find(cell2mat(NodoTiltHR(:,2))==cell2mat(NodeHR));
                            [rT,~] = size(Tempo);
                            DateHR = zeros(rT+1,1);
                            HShift_local_HR = zeros(rT,1);
                            nHR = 1;
                            for tt = 1:rT+1
                                if tt == 1
                                    Data_HR = datestr(Date(j-4),'yyyy-mm-dd');
                                    Tempo_HR = datestr(Date(j-4),'HH:MM:SS');
                                else
                                    Data_HR = datestr(Tempo(tt-1),'yyyy-mm-dd');
                                    Tempo_HR = datestr(Tempo(tt-1),'HH:MM:SS');
                                end
                                NodeNum = num2str(cell2mat(NodeHR));
                                comando = ['select EventDate, EventTime, HShift_local from ElabDataView where EventDate = ''' ...
                                    Data_HR ''' and EventTime = ''' Tempo_HR ''' and  ToolNameID = ''' DTcatena...
                                    ''' and UnitName = ''' IDcentralina ''' and NodeNum = ''' NodeNum ''' '];
                                curs = exec(conn,comando);
                                curs = fetch(curs);
                                Dati_HR = curs.Data;
                                % Modifico il formato di data e ora in DATini.
                                [rD,~] = size(Dati_HR);
                                T = [cell2mat(Dati_HR(:,1)) repmat(' ', [rD,1]) cell2mat(Dati_HR(:,2))];
                                DateHR(nHR,1) = datenum(T); % Data
                                HShift_local_HR(nHR,1) = cell2mat(Dati_HR(:,3)); % Loc 2D
                                nHR = nHR+1;
                            end
                            [rS,~] = size(HShift_local_HR);
                            Speed_local_HR = zeros(rS-1,1);
                            DateGiornoHR = DateHR(2:end);
                            for jH = 4:rS-1 % date
                                % Locali
                                Speed_local_HR(jH,1) = (HShift_local(jH+1,1) - HShift_local(jH,1))/(DateHR(jH+1)-DateHR(jH)); % m/g
                                if jH == 4
                                    Speed_local_HR(jH-1,1) = (HShift_local_HR(jH,1) - HShift_local_HR(jH-1,1))/(DateHR(jH)-DateHR(jH-1)); % m/g
                                    Speed_local_HR(jH-2,1) = (HShift_local_HR(jH-1,1) - HShift_local_HR(jH-2,1))/(DateHR(jH-1)-DateHR(jH-2)); % m/g
                                    Speed_local_HR(jH-3,1) = (HShift_local_HR(jH-2,1) - HShift_local_HR(jH-3,1))/(DateHR(jH-2)-DateHR(jH-3)); % m/g
                                end
                            end

                            % Velocità in metri/giorno
                            jH = find(DateHR == Date(j))-1;

                            % ---Criterio della velocità positiva Criterio 0---
                            if Speed_local_HR(jH,1) > 0 && ...
                                    Speed_local_HR(jH-1,1) > 0 ...
                                    && Speed_local_HR(jH-2,1) > 0 ...
                                    && Speed_local_HR(jH-3,1) > 0

                                % --- Calcolo Fukuzono ---
                                TempoHR = DateGiornoHR(jH-3:jH+N); % giorni
                                Vel_HR = 1000*Speed_local_HR(jH-3:jH+N,1); % mm/g
                                Vel_HR(Vel_HR==0) = 0.0000000001;
                                Inv_Vel_HR = 1./Vel_HR;
                                [f,rsquare] = fit(TempoHR,Inv_Vel_HR,'poly1');
                                RQ_Loc_HR(jH+N,HRprog) = rsquare.rsquare;
                                retta = coeffvalues(f);
                                m = retta(1,1);
                                q = retta(1,2);
                                % Time of Failure
                                Tr_Loc_HR(jH+N,HRprog) = -q/m - DateGiornoHR(jH+N);
                                if Tr_Loc_HR(jH+N,HRprog) > 0 && Tr_Loc_HR(jH+N,HRprog) < 30 % giorni
                                    if RQ_Loc_HR(jH+N,i) >= 0.85
                                        DataFuK_local_HR(jH+N,HRprog) = DateGiornoHR(jH-3);
                                        Data_Img_HR = datestr(DateGiornoHR(jH-3),'dd-mm-yyyy');
                                        Tempo_Img_HR = datestr(DateGiornoHR(jH-3),'HH-MM-SS');
                                        % Grafico Asintotico
                                        fig2 = plot(TempoHR,Vel_HR,'-o','LineStyle','none');
                                        title('Time of Failure prediction model evaluated with Electrolytic Cell');
                                        xlabel('Date [dd/mm/yyyy HH:MM]')
                                        ylabel('Velocity [mm/d]')
                                        datetick('x','dd/mm/yyyy HH:MM')
                                        ylim([0 max(Vel_HR)+max(Vel_HR)/10])
                                        Nodo = cell2mat(NodoTiltHR(HRprog,2));
                                        filename = strcat(IDcentralina,'-',DTcatena,'-Node_',num2str(Nodo),'-',Data_Img_HR,'_',Tempo_Img_HR,'-Local_Vel.jpg');
                                        saveas(fig2,filename)
                                        % Grafico inverso velocità
                                        fig = plot(TempoHR,Inv_Vel_HR,'-o','LineStyle','none');
                                        hold on
                                        legend(num2str(RQ_Loc_HR(jH+N,i)));
                                        Y_HR = m*TempoHR + q;
                                        plot(Tempo,Y_HR);
                                        title('Time of Failure prediction model evaluated with Electrolytic Cell');
                                        xlabel('Date [dd/mm/yyyy HH:MM]')
                                        ylabel('Inverse of Velocity [d/mm]')
                                        datetick('x','dd/mm/yyyy HH:MM')
                                        ylim([0 max(Inv_Vel_HR)+max(Inv_Vel_HR)/10])
                                        LIM = floor(fig.XData(1));
                                        fig = plot(DateGiornoHR(jH+N)+Tr_Loc_HR(jH+N,HRprog),0,'-d','LineStyle','none','Color','r');
                                        LIM_S = ceil(Tr_Loc_HR(jH+N,HRprog)+DateGiornoHR(jH+N));
                                        h = gca; % Get axis to modify
                                        xlim([LIM LIM_S])
                                        h.XAxis.MinorTick = 'on'; % Must turn on minor ticks if they are off
                                        h.XAxis.MinorTickValues = LIM:1/4:LIM_S; % Minor ticks which don't line up with majors
                                        h.XAxis.TickValues = LIM:1:LIM_S;
                                        datetick('x','dd/mm/yyyy')
                                        grid on
                                        grid minor
                                        filename = strcat(IDcentralina,'-',DTcatena,'-Node_',num2str(Nodo),'-',Data_Img_HR,'_',Tempo_Img_HR,'-Local.jpg');
                                        saveas(fig,filename)
                                    else
                                        Tr_Loc_HR(jH+N,HRprog) = 0;
                                    end
                                else
                                    Tr_Loc_HR(jH+N,HRprog) = 0;
                                end
                            else
                                Tr_Loc_HR(jH,HRprog) = 0;
                            end
                        end
                    end
                end
                j = j+n+1;
            else
                j = j+1;
            end
        end
    end
    text = 'Fukuzono Calculation executed';
    fprintf(fileID,fmt,text);
else
    alert = zeros(r,c);
    alert_local = zeros(r,c);
    alarm = zeros(r,c);
    alarm_local = zeros(r,c);
    RQ = zeros(r,c);
    Number = zeros(r,c);
    RQ_Loc = zeros(r,c);
    Number_Loc = zeros(r,c);
    Tr_Inf = zeros(r,c) - 99;
    Tr_Sup = zeros(r,c) - 99;
    Tr_mail = zeros(r,c) - 99;
    Tr_Inf_Loc = zeros(r,c) - 99;
    Tr_Sup_Loc = zeros(r,c) - 99;
    Tr_Loc_mail = zeros(r,c) - 99;
    DataFuK = zeros(r,c);
    DataFuK_local = zeros(r,c);
end

text = 'Fukuzono Function ended';
fprintf(fileID,fmt,text);
fclose(fileID);
        
end