function [alarm_STORICO,alarm_local_STORICO,DateGiorno,Tr_Loc_Sup_S,Tr_Loc_Inf_S,...
    Tr_Loc_S,Tr_Sup_S,Tr_Inf_S,Tr_S,Tr_Loc_HR_S,Tr_mail_S,Tr_Loc_mail_S,DataFuK_S,...
    DataFuK_local_S,DataFuK_local_HR_S,Number_S,Number_Loc_S,RQ_S,RQ_Loc_S] = ...
    Fukuzono_Giornaliero(IDcentralina,DTcatena,date,ARRAYdate,yes3D,rNL,NodoTilt,...
    NodoTiltHR,NodoTiltHR3D,conn,FileName)

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

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

data_rif = ARRAYdate(end)-45; % 45
Tr_Loc_HR_S = [];
DataFuK_local_HR_S = [];
                    
%% Scarico i dati da DB
% Tilt Link V
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

%% Calcolo una singola velocità media giornaliera
[rDate,~] = size(Date);
day = 0;
fine = rDate;
diffDate = 0;
n = 2;
period = 1; % calcolo giornaliero
Indici(1,1) = rDate;
while day <= rDate
    while diffDate < period
          day = day+1;
          if day >= rDate
              break
          end
          diffDate = Date(fine) - Date(rDate-day);
    end
    if day >= rDate
        break
    end
    Indici(n,1) = rDate-day;
    n = n+1;
    diffDate = 0;
    fine = rDate-day;
end

Indici = flipud(Indici);
[rI,~] = size(Indici);
DateGiorno(1,1) = Date(1,1);
if rI < 2
   Speed = [];
   Speed_local = [];
   DateGiorno = [];
else
    for s = 2:rI
        Speed(s,:) = (HShift(Indici(s),:) - HShift(Indici(s-1),:))/(Date(Indici(s),1) - Date(Indici(s-1),:));
        Speed_local(s,:) = (HShift_local(Indici(s),:) - HShift_local(Indici(s-1),:))/(Date(Indici(s),1) - Date(Indici(s-1),:));
        DateGiorno(s,1) = Date(Indici(s),1);
    end
end
% Velocità in metri/giorno
    
%% Criterio della velocità positiva - Criterio 0
[rS,cS] = size(Speed);
Criterio_0 = zeros(rS,cS);
Criterio_0_local = zeros(rS,cS);
for i = 1:cS % nodi
    for j = 4:rS % date
        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
        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
text = 'Criterion 0 executed';
fprintf(fileID,fmt,text);

%% Faccio i calcoli
Delta_Speed_local = zeros(rS,cS);
Delta_Speed = zeros(rS,cS);
diff_Cum = diff(Speed);
diff_Loc = diff(Speed_local);
Delta_Speed(2:end,:) = diff_Cum;
Delta_Speed_local(2:end,:) = diff_Loc;
act = zeros(rS,cS);
act_local = zeros(rS,cS);
for i = 1:cS % nodi
    for j = 2:rS % 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(rS,cS);
Criterio_1_local = zeros(rS,cS);
if rS > 10
    for i = 1:cS % nodi
        for j = 10:rS % 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;
                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;
                end
            end
        end
    end
    text = 'Criterion 1 executed';
    fprintf(fileID,fmt,text);

    %% Criterio delle parabole - Criterio 2
    a = zeros(rS,cS);
    a_Loc = zeros(rS,cS);
    Criterio_2 = zeros(rS,cS);
    Criterio_2_local = zeros(rS,cS);
    for i = 1:cS % nodi
        for j = 13:rS % date
            if Criterio_1(j,i) == 1
                % j-3
                x = DateGiorno(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 = DateGiorno(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 = DateGiorno(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 = DateGiorno(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 = DateGiorno(j-12:j-3);
                y = Speed_local(j-12:j-3,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a_Loc(j-3,i) = coeff(1,1);
                % j-2
                x = DateGiorno(j-11:j-2);
                y = Speed_local(j-11:j-2,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a_Loc(j-2,i) = coeff(1,1);
                % j-1
                x = DateGiorno(j-10:j-1);
                y = Speed_local(j-10:j-1,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a_Loc(j-1,i) = coeff(1,1);
                % j
                x = DateGiorno(j-9:j);
                y = Speed_local(j-9:j,i);
                f = fit(x,y,'poly2');
                coeff = coeffvalues(f);
                a_Loc(j,i) = coeff(1,1);
                if a_Loc(j,i) > 0 && a_Loc(j-1,i) > 0 && a_Loc(j-2,i) > 0 && a_Loc(j-3,i) > 0 || ...
                        a_Loc(j,i) > 0 && a_Loc(j-1,i) > 0 && a_Loc(j-2,i) > 0 || ...
                        a_Loc(j,i) > 0 && a_Loc(j-1,i) > 0 && a_Loc(j-3,i) > 0 || ...
                        a_Loc(j,i) > 0 && a_Loc(j-2,i) > 0 && a_Loc(j-3,i) > 0 || ...
                        a_Loc(j-1,i) > 0 && a_Loc(j-2,i) > 0 && a_Loc(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(rS,cS);
    Delta_a_local = zeros(rS,cS);
    diff_a = diff(a);
    diff_a_Loc = diff(a_Loc);
    Delta_a(2:end,:) = diff_a;
    Delta_a_local(2:end,:) = diff_a_Loc;
    Criterio_3 = zeros(rS,cS);
    Criterio_3_local = zeros(rS,cS);
    for i = 1:cS % nodi
        for j = 13:rS % 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_STORICO = zeros(rS,cS);
    alarm_local_STORICO = zeros(rS,cS);
    RQ_S = zeros(rS,cS);
    RQ_Loc_S = zeros(rS,cS);
    Number_S = zeros(rS,cS);
    Number_Loc_S = zeros(rS,cS);
    Tr_S = zeros(rS,cS) - 99;
    Tr_Inf_S = zeros(rS,cS) - 99;
    Tr_Sup_S = zeros(rS,cS) - 99;
    Tr_mail_S = zeros(rS,cS) - 99;
    Tr_Loc_S = zeros(rS,cS) - 99;
    Tr_Loc_Inf_S = zeros(rS,cS) - 99;
    Tr_Loc_Sup_S = zeros(rS,cS) - 99;
    Tr_Loc_mail_S = zeros(rS,cS) - 99;
    DataFuK_S = zeros(rS,cS);
    DataFuK_local_S = zeros(rS,cS);
    for i = 1:cS % nodi
        j = 13;
        % -------------- Cumulati --------------------------
        while j <=(rS-1)
            hold off
            DEPETRIS = 0;
            if Criterio_3(j,i) == 1
                Tempo = DateGiorno(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_S(j,i) = -q/m - DateGiorno(j);
                if Tr_S(j,i) > 0 && Tr_S(j,i) < 30 % giorni
                    % RQ
                    RQ_S(j,i) = rsquare.rsquare;
                    % Grafico Inv V - T
                    Data_Img = datestr(DateGiorno(j-3),'dd-mm-yyyy');
                    Tempo_Img = datestr(DateGiorno(j-3),'HH-MM-SS');
                    fig = plot(Tempo,Inv_Vel,'-o','LineStyle','none');
                    hold on
                    legend(num2str(RQ_S(j,i)));
                    Y = m*Tempo + q;
                    plot(Tempo,Y);
                    title('Time of Failure prediction model');
                    xlabel('Date [dd/mm/yyyy]')
                    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]')
                    ylabel('Velocity [mm/d]')
                    datetick('x','dd/mm/yyyy HH:MM')
                    ylim([0 max(Vel)+max(Vel)/10])
                end
                % dato successivo
                for n = 1:(rS-j)
                    if Criterio_3(n+j,i) == 1 || Criterio_3(n+j-1,i) == 1
                        Tempo = DateGiorno(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_S(j+n,i) = rsquare.rsquare;
                        retta = coeffvalues(f);
                        m = retta(1,1);
                        q = retta(1,2);
                        % Time of Failure
                        Tr_S(j+n,i) = -q/m - DateGiorno(j+n);
                        if Tr_S(j+n,i) > 0 && Tr_S(j+n,i) < 30 % giorni
                            if RQ_S(j+n,i) >= 0.85
                                Tr_mail_S(j+n,i) = -q/m - DateGiorno(j+n);% attivo
                                Tr_mail_S(j+n-1,i) = -99; % disattivo
                                alarm_STORICO(j+n-1,i) = 0; % disattivo
                                alarm_STORICO(j+n,i) = 1; % attivo
                                DataFuK_S(j+n,i) = DateGiorno(j-3);
                                DataFuK_S(j,i) = 0;
                                Data_Img = datestr(DateGiorno(j-3),'dd-mm-yyyy');
                                Tempo_Img = datestr(DateGiorno(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]')
                                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_Storico.jpg');
                                saveas(fig2,filename)
                                % Grafico inverso velocità
                                fig = plot(Tempo,Inv_Vel,'-o','LineStyle','none');
                                hold on
                                legend(num2str(RQ_S(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]')
                                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));
                                % Variazione statistica dei dati di
                                % monitoraggio
                                % Limite Superiore
                                Inv_Vel_Sup = Inv_Vel + mean(Inv_Vel)*(1-RQ_S(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_S(j+n,i) = -q_Sup/m_Sup;
                                Tr_Sup_S(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_S(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_S(j+n,i) = -q_Inf/m_Inf;
                                Tr_Inf_S(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')
                                DEPETRIS = 1;
                                N = n;
                                Number_S(j+N,i) = N+4;
                            end
                        end
                    else
                        break
                    end
                end
                if DEPETRIS == 1
                    hold on
                    fig = plot(DateGiorno(j+N)+Tr_S(j+N,i),0,'-d','LineStyle','none','Color','r');
                    plot(Tr_Sup_S(j+N,i),0,'-v','LineStyle','none','Color','k');
                    LIM_S = ceil(Tr_Sup_S(j+N,i));
                    plot(Tr_Inf_S(j+N,i),0,'-v','LineStyle','none','Color','k');
                    legend(['RQ: ' num2str(RQ_S(j+N,i))]);
                    h = gca; % Get axis to modify
                    xlim([LIM(1) LIM_S(end)])
                    h.XAxis.MinorTick = 'on'; % Must turn on minor ticks if they are off
                    h.XAxis.MinorTickValues = LIM(1):1/4:LIM_S(end); % Minor ticks which don't line up with majors
                    h.XAxis.TickValues = LIM(1):1:LIM_S(end);
                    datetick('x','dd/mm/yyyy')
                    grid on
                    grid minor
                    filename = strcat(IDcentralina,'-',DTcatena,'-Node_',num2str(Nodo),'-',Data_Img,'_',Tempo_Img,'_Storico.jpg');
                    saveas(fig,filename)
                end
                j = j+n+1;
            else
                j = j+1;
            end
        end
        % -------------- Locali --------------------------
        j = 13;
        while j <=(rS-1)
            hold off
            GIOLITTI = 0;
            if Criterio_3_local(j,i) == 1
                Tempo = DateGiorno(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_S(j,i) = -q/m - DateGiorno(j);
                if Tr_Loc_S(j,i) > 0 && Tr_Loc_S(j,i) < 30
                    % RQ
                    RQ_Loc_S(j,i) = rsquare.rsquare;
                    % Grafico Inv V - T
                    Data_Img = datestr(DateGiorno(j-3),'dd-mm-yyyy');
                    Tempo_Img = datestr(DateGiorno(j-3),'HH-MM-SS');
                    % Grafico
                    fig = plot(Tempo,Inv_Vel,'-o','LineStyle','none');
                    hold on
                    legend(num2str(RQ_Loc_S(j,i)));
                    Y = m*Tempo + q;
                    plot(Tempo,Y);
                    title('Time of Failure prediction model');
                    xlabel('Date [dd/mm/yyyy]')
                    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]')
                    ylabel('Velocity [mm/d]')
                    datetick('x','dd/mm/yyyy HH:MM')
                    ylim([0 max(Vel)+max(Vel)/10])
                end
                % dato successivo
                for n = 1:(rS-j)
                    if Criterio_3_local(n+j,i) == 1 || Criterio_3_local(n+j-1,i) == 1
                        Tempo = DateGiorno(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_S(j+n,i) = rsquare.rsquare;
                        retta = coeffvalues(f);
                        m = retta(1,1);
                        q = retta(1,2);
                        % Time of Failure
                        Tr_Loc_S(j+n,i) = -q/m - DateGiorno(j+n);
                        if Tr_Loc_S(j+n,i) > 0 && Tr_Loc_S(j+n,i) < 30 % giorni
                            if RQ_Loc_S(j+n,i) >= 0.85
                                Tr_Loc_mail_S(j+n,i) = -q/m - DateGiorno(j+n);
                                Tr_Loc_mail_S(j+n-1,i) = -99; % disattivo
                                alarm_local_STORICO(j+n-1,i) = 0; % disattivo
                                alarm_local_STORICO(j+n,i) = 1; % attivo
                                DataFuK_local_S(j+n,i) = DateGiorno(j-3);
                                DataFuK_local_S(j,i) = 0;
                                Data_Img = datestr(DateGiorno(j-3),'dd-mm-yyyy');
                                Tempo_Img = datestr(DateGiorno(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]')
                                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_Storico.jpg');
                                saveas(fig2,filename)
                                % Grafico inverso velocità
                                fig = plot(Tempo,Inv_Vel,'-o','LineStyle','none');
                                hold on
                                legend(num2str(RQ_Loc_S(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]')
                                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));
                                % Variazione statistica dei dati di
                                % monitoraggio
                                % Limite Superiore
                                Inv_Vel_Sup = Inv_Vel + mean(Inv_Vel)*(1-RQ_Loc_S(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_S(j+n,i) = -q_Sup/m_Sup;
                                Tr_Loc_Sup_S(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_S(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_S(j+n,i) = -q_Inf/m_Inf;
                                Tr_Loc_Inf_S(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')
                                GIOLITTI = 1;
                                N = n;
                                Number_Loc_S(j+N,i) = N+4;
                            end
                        end
                    else
                        break
                    end
                end
                if GIOLITTI == 1
                    hold on
                    fig = plot(DateGiorno(j+N)+Tr_Loc_S(j+N,i),0,'-d','LineStyle','none','Color','r');
                    plot(Tr_Loc_Sup_S(j+N,i),0,'-v','LineStyle','none','Color','k');
                    LIM_S = ceil(Tr_Loc_Sup_S(j+N,i));
                    plot(Tr_Loc_Inf_S(j+N,i),0,'-v','LineStyle','none','Color','k');
                    legend(['RQ: ' num2str(RQ_Loc_S(j+N,i))]);
                    h = gca; % Get axis to modify
                    xlim([LIM(1) LIM_S(end)])
                    h.XAxis.MinorTick = 'on'; % Must turn on minor ticks if they are off
                    h.XAxis.MinorTickValues = LIM(1):1/4:LIM_S(end); % Minor ticks which don't line up with majors
                    h.XAxis.TickValues = LIM(1):1:LIM_S(end);
                    datetick('x','dd/mm/yyyy')
                    grid on
                    grid minor
                    filename = strcat(IDcentralina,'-',DTcatena,'-Node_',num2str(Nodo),'-',Data_Img,'_',Tempo_Img,'-Local_Storico.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);
                            HShift_local_HR = zeros(rT,1);
                            nHR = 1;
                            for tt = 1:rT+1
                                if tt == 1
                                    Data_HR = datestr(DateGiorno(j-4),'yyyy-mm-dd');
                                    Tempo_HR = datestr(DateGiorno(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));
                                comando1 = ['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,comando1);
                                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
                            %% Calcolo una singola velocità media giornaliera
                            [rI,~] = size(DateHR);
                            DateGiornoHR = DateHR(2:end);
                            for s = 2:rI
                                Speed_local_HR(s-1,:) = (HShift_local_HR(s,:) - HShift_local_HR(s-1,:))/(DateHR(s,1) - DateHR(s-1,:));
                            end
                            % Velocità in metri/giorno
                            jHR = find(DateGiornoHR == DateGiorno(j));
 
                            % ---Criterio della velocità positiva Criterio 0---
                            if Speed_local_HR(jHR,1) > 0 && ...
                                    Speed_local_HR(jHR-1,1) > 0 ...
                                    && Speed_local_HR(jHR-2,1) > 0 ...
                                    && Speed_local_HR(jHR-3,1) > 0
                                
                                    % --- Calcolo Fukuzono ---
                                    TempoHR = DateGiornoHR(jHR-3:jHR+N); % giorni
                                    Vel_HR = 1000*Speed_local_HR(jHR-3:jHR+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_S(jHR+N,HRprog) = rsquare.rsquare;
                                    retta = coeffvalues(f);
                                    m = retta(1,1);
                                    q = retta(1,2);
                                    % Time of Failure
                                    Tr_Loc_HR_S(jHR+N,HRprog) = -q/m - DateGiornoHR(jHR+N);
                                    if Tr_Loc_HR_S(jHR+N,HRprog) > 0 && Tr_Loc_HR_S(jHR+N,HRprog) < 30 % giorni
                                        if RQ_Loc_HR_S(jHR+N,i) >= 0.85
                                           DataFuK_local_HR_S(jHR+N,HRprog) = DateGiornoHR(jHR-3);
                                           Data_Img_HR = datestr(DateGiornoHR(jHR-3),'dd-mm-yyyy');
                                           Tempo_Img_HR = datestr(DateGiornoHR(jHR-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]')
                                           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_Storico.jpg');
                                           saveas(fig2,filename)
                                           % Grafico inverso velocità
                                           fig = plot(TempoHR,Inv_Vel_HR,'-o','LineStyle','none');
                                           hold on
                                           legend(num2str(RQ_Loc_HR_S(jHR+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]')
                                           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(jHR+N)+Tr_Loc_HR_S(jHR+N,HRprog),0,'-d','LineStyle','none','Color','r');
                                           LIM_S = ceil(Tr_Loc_HR_S(jHR+N,HRprog)+DateGiornoHR(jHR+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_Storico.jpg');
                                           saveas(fig,filename)
                                        else
                                            Tr_Loc_HR_S(jHR+N,HRprog) = 0;
                                        end
                                    else
                                        Tr_Loc_HR_S(jHR+N,HRprog) = 0;
                                    end
                            else
                                Tr_Loc_HR_S(jHR,HRprog) = 0;
                            end
                        end
                    end
                end
                j = j+n+1;
            else
                j = j+1;
            end
        end
    end
    text = 'Fukuzono_Giornaliero Calculation executed';
    fprintf(fileID,fmt,text);
else
    alarm_STORICO = zeros(rS,cS);
    alarm_local_STORICO = zeros(rS,cS);
    Tr_Loc_Sup_S = [];
    Tr_Loc_Inf_S = [];
    Tr_Loc_S = [];
    Tr_Sup_S = [];
    Tr_Inf_S = [];
    Tr_S = [];
    Tr_mail_S = [];
    Tr_Loc_mail_S = [];
    DataFuK_local_S = [];
    DataFuK_S = [];
    Number_S = [];
    Number_Loc_S = [];
    RQ_S = [];
    RQ_Loc_S = [];
end
text = 'Fukuzono_Giornaliero function executed';
fprintf(fileID,fmt,text);
fclose(fileID);
end