function RC = ATD(IDcentralina,DTcatena)
%%% Programma per l'elaborazione dei dati di Cir Array, Rad Array, Tilt
%%% Link H, Tilt Link HR H, PreConv Array, PreConv Array HR e strumenti
%%% analogici per gallerie.
%%% Versione 3.4

tic

RC = 1; %#ok<NASGU>
text = ['Elaboration of chain ' DTcatena ' of control unit ' IDcentralina ' started correctly'];
d = datestr(datetime,'yyyy_mm_dd');
t = datestr(datetime,'HH_MM_SS');
FileName = ['LogFile-' IDcentralina '-' DTcatena '-' d '-' t '.txt'];
outdat = fopen(FileName,'wt+'); %#ok<NASGU>
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);
fclose(fileID);

%% Parte generale

% Associo ID company al relativo DB 
% Definisco i parametri del Database
IDcompany = '1'; % DA INTEGRARE IN VERSIONI FUTURE
DB = database_definition(IDcompany,FileName); 

% Apro la connessione al DB
dbname = cell2mat(DB{1,1});
username = cell2mat(DB{2,1});
password = cell2mat(DB{3,1});
driver = cell2mat(DB{4,1});
dburl = 'jdbc:mysql://212.237.30.90:3306/ase_lar?useLegacyDatetimeCode=false&serverTimezone=Europe/Rome&';
conn = database(dbname, username, password, driver, dburl);

% scarico Contol Unit ID
unitID = IDunit(IDcentralina,conn,FileName);
    
% questa funzione considera la prima data e ora da cui caricare i dati a seconda della catena
[date,time,date_AC,time_AC,Nodo_AC,unitID] = datainiziale(DTcatena,unitID,conn,FileName);

% funzione che elenca tutti i nodi ed il loro numero presenti per ogni tipo di sensore.
[idTool,NodoTunnelLink,NodoRadialLink,NodoTiltLinkH,...
    NodoTiltLinkHRH,NodoPreConvLink,NodoPreConvLinkHR,NodoDistoMTLink,NodoPressureLink,...
    NodoLoadLink,NodoExtensometerLink,Nodo3DExtensometerLink,NodoWireExtensometerLink,...
    NodoMultiPointRodExtensometer,NodoTiltLinkHR3DH,NodoPreConvLinkHR3D,...
    NodoAnalogLink,NodoCrackLink,Nodo3DCrackLink,Nodo2DCrackLink,NodoStressMeter,...
    rTuL,rRL,rTLH,rTLHRH,rPCL,rPCLHR,rPrL,rLL,rEL,r3DEL,rWEL,rMPBEL,rAL,rCrL,r3DCrL,...
    r2DCrL,rSM,~,~,rDM] = tipologiaNodi(DTcatena,unitID,conn,FileName);

% funzione che ricostruisce i nodi della catena
catena = schema(idTool,conn,FileName);
    
% funzione che attiva/disattiva l'elaborazione per una determinata tipologia di nodo
[yesTuL,yesRL,yesTLH,yesTLHRH,yesTLHR3DH,yesPCL,yesPCLHR,yesPCLHR3D,...
    yesPL,yesLL,yesEL,yes3DEL,yesWEL,yesMPBEL,yesAL,yesCrL,yes3DCrL,yes2DCrL,...
    yesDM,yesSM] = YesNo(NodoTunnelLink,NodoRadialLink,NodoTiltLinkH,NodoTiltLinkHRH,...
    NodoTiltLinkHR3DH,NodoPreConvLink,NodoPreConvLinkHR,NodoPreConvLinkHR3D,...
    NodoDistoMTLink,NodoPressureLink,NodoLoadLink,NodoExtensometerLink,...
    Nodo3DExtensometerLink,NodoWireExtensometerLink,NodoMultiPointRodExtensometer,...
    NodoAnalogLink,NodoCrackLink,Nodo3DCrackLink,Nodo2DCrackLink,NodoStressMeter,FileName);

% Definisco i parametri di elaborazione di un'installazione
[NdatiMedia,Ndatidespike,MEMS,tolleranzaAcc,Tmax,Tmin,Ndevst,Wdevst,...
    Ndevst_HR,Wdevst_HR,NumBasi,elab_option,Calcolo_Carico,Area,Mod_Elastico,...
    Carico_Ini] = Parametri_Installazione(idTool,conn,yesTuL,yesRL,...
    yesTLH,yesTLHRH,yesPCL,yesPCLHR,yesMPBEL,yesWEL,yesEL,FileName);

% funzione che ricostruisce segmenti di pertinenza etc
[SpeTuL,SpeRL,SpeTLH,PsTLH,SpeTLHRH,PsTLHRH,SpePCL,SpePCLHR]...
    = schemaSP(yesTuL,yesRL,yesTLH,yesTLHRH,yesPCL,yesPCLHR,...
    NodoTunnelLink,NodoRadialLink,NodoTiltLinkH,NodoTiltLinkHRH,...
    NodoPreConvLink,NodoPreConvLinkHR,rTuL,rRL,rTLH,rTLHRH,rPCL,rPCLHR,catena,FileName); 

% tipologia di centralina
Unit = centralina(IDcentralina,conn,FileName);

%% Scarico dei dati

% funzione che carica i dati di calibrazione
[DCalTuLTot,DCalRLTot,DCalTLHTot,DCalTLHRHTot,DCalPCLTot,DCalPCLHRTot,...
    DCalPLTot,DCalLLTot,DCalELTot,DCalEL3DTot,DCalWELTot,DCalMPBELTot,...
    DCalALTot,DCalCrLTot,DCal3DCrLTot,DCal2DCrLTot,DCalSMTot,yesTuL,yesRL,yesTLH,yesTLHRH,...
    yesPCL,yesPCLHR,yesPL,yesLL,yesEL,yes3DEL,yesWEL,yesMPBEL,yesCrL,yes3DCrL,yes2DCrL,...
    yesAL,yesSM] = letturaCal(IDcentralina,DTcatena,catena,rTuL,rRL,rTLH,rTLHRH,...
    rPCL,rPCLHR,rPrL,rLL,rEL,r3DEL,rWEL,rMPBEL,NumBasi,rAL,rCrL,r3DCrL,r2DCrL,rSM,...
    NodoTunnelLink,NodoRadialLink,NodoTiltLinkH,NodoPreConvLink,NodoPressureLink,...
    NodoLoadLink,NodoExtensometerLink,Nodo3DExtensometerLink,...
    NodoWireExtensometerLink,NodoMultiPointRodExtensometer,NodoAnalogLink,...
    NodoCrackLink,Nodo3DCrackLink,Nodo2DCrackLink,NodoStressMeter,yesTuL,yesRL,yesTLH,yesTLHRH,...
    yesPCL,yesPCLHR,yesPL,yesLL,yesEL,yes3DEL,yesWEL,yesMPBEL,yesAL,yesCrL,yes3DCrL,yes2DCrL,...
    yesSM,conn,FileName);

if strcmp(Unit,'Nesa evolution') == 0

    % funzione che scarica i dati da utilizzare come nuovo zero (ultimo dato
    % elaborato, con un certo margine
    [DatiElabTunnelLink,NuovoZeroTuL,ay_TuL,t_TuL,ay_TuL_AC,t_TuL_AC,...
        datainiTuL,tempoiniTuL,data1_AC,DatiElabRadialLink,NuovoZeroRL,datainiRL,tempoiniRL,...
        DatiElabTiltLinkH,NuovoZeroTLH,datainiTLH,tempoiniTLH,...
        DatiElabTiltLinkHRH,NuovoZeroTLHRH,datainiTLHRH,tempoiniTLHRH,...
        DatiElabPreConvLink,NuovoZeroPCL,datainiPCL,tempoiniPCL,...
        DatiElabPreConvLinkHR,NuovoZeroPCLHR,datainiPCLHR,tempoiniPCLHR,...
        DatiElabDistoMTLink,NuovoZeroDM,datainiDM,tempoiniDM,...
        datainiPL,tempoiniPL,NuovoZeroPrL,datainiLL,tempoiniLL,NuovoZeroLL,...
        datainiEL,tempoiniEL,Date_Rif_EL,NuovoZeroEL,...
        dataini3DEL,tempoini3DEL,Date_Rif_3DEL,NuovoZero3DEL,datainiWEL,tempoiniWEL,NuovoZeroWEL,...
        datainiMPBEL,tempoiniMPBEL,NuovoZeroMPBEL,DatiElabAnalogLink,NuovoZeroAL,datainiAL,tempoiniAL,...
        datainiCrL,tempoiniCrL,NuovoZeroCrL,dataini3DCrL,tempoini3DCrL,NuovoZero3DCrL,...
        dataini2DCrL,tempoini2DCrL,NuovoZero2DCrL, datainiSM,tempoiniSM,Date_Rif_SM,NuovoZeroSM,margine] = ...
        LastElab(conn,date,time,IDcentralina,DTcatena,Wdevst,NdatiMedia,Ndatidespike,...
        NodoTunnelLink,yesTuL,date_AC,time_AC,Nodo_AC,rTuL,...
        NodoRadialLink,yesRL,rRL,NodoTiltLinkH,yesTLH,rTLH,NodoTiltLinkHRH,yesTLHRH,rTLHRH,...
        NodoPreConvLink,yesPCL,rPCL,NodoPreConvLinkHR,yesPCLHR,rPCLHR,...
        NodoDistoMTLink,yesDM,rDM,NodoPressureLink,yesPL,...
        NodoLoadLink,yesLL,NodoExtensometerLink,yesEL,Nodo3DExtensometerLink,...
        yes3DEL,NodoWireExtensometerLink,yesWEL,NodoMultiPointRodExtensometer,...
        yesMPBEL,NodoAnalogLink,yesAL,rAL,NodoCrackLink,yesCrL,...
        Nodo3DCrackLink,yes3DCrL,Nodo2DCrackLink,yes2DCrL,NodoStressMeter,yesSM,FileName);
    
    % Lettura dati da DB
    [Batteria,DatiTunnelLink,ErrTunnelLink,datainiTuL,...
        DatiRadialLink,ErrRadialLink,datainiRL,DatiTiltLinkH,ErrTiltLinkH,datainiTLH,...
        DatiTiltLinkHRH,ErrTiltLinkHRH,datainiTLHRH,DatiPreConvLink,ErrPreConvLink,datainiPCL,...
        DatiPreConvLinkHR,ErrPreConvLinkHR,datainiPCLHR,DatiDistoMTLink,ErrDistoMTLink,datainiDM,...
        DatiPressureLink,ErrPressureLink,...
        DatiLoadLink,ErrLoadLink,DatiExtensometerLink,DatiExtensometerLink_Rif,...
        DatiNTCExtensometerLink,ErrExtensometerLink,Dati3DExtensometerLink,ChEL,EL_NTC,...
        Dati3DExtensometerLink_Rif,DatiNTC3DExtensometerLink,Err3DExtensometerLink,ErrNTC3DExtensometerLink,Ch3DEL,EL3D_NTC,...
        DatiWireExtensometerLink,DatiNTCWireExtensometerLink,ErrWireExtensometerLink,ChWEL,WEL_NTC,...
        DatiMultiPointExtensometer,DatiNTCMultiPointExtensometer,ErrMultiPointExtensometer,ErrNTCMultiPointExtensometer,ChMPBEL,MPBEL_NTC,...
        DatiAnalogLink,ErrAnalogLink,datainiAL,~,~,...
        DatiCrackLink,DatiNTCCrackLink,ErrCrackLink,ChCrL,CrL_NTC,...
        Dati3DCrackLink,DatiNTC3DCrackLink,Err3DCrackLink,ErrNTC3DCrackLink,Ch3DCrL,CrL3D_NTC,...
        Dati2DCrackLink,DatiNTC2DCrackLink,Err2DCrackLink,ErrNTC2DCrackLink,Ch2DCrL,CrL2D_NTC,...
        DatiStressMeter,DatiStressMeter_Rif,DatiNTCStressMeter,DatiNTCStressMeter_Rif,ErrStressMeter,ChSM,SM_NTC,...
        yesTuL,yesRL,yesTLH,yesTLHRH,yesPCL,yesPCLHR,yesPL,yesLL,yesEL,yes3DEL,...
        yesWEL,yesMPBEL,yesAL,yesCrL,yes3DCrL,yes2DCrL,yesSM,yesDM] = ...
        lettura(IDcentralina,DTcatena,datainiTuL,tempoiniTuL,NodoTunnelLink,NuovoZeroTuL,date_AC,time_AC,Nodo_AC,...
        datainiRL,tempoiniRL,NodoRadialLink,NuovoZeroRL,datainiTLH,tempoiniTLH,NodoTiltLinkH,NuovoZeroTLH,...
        datainiTLHRH,tempoiniTLHRH,NodoTiltLinkHRH,NuovoZeroTLHRH,datainiPCL,tempoiniPCL,NodoPreConvLink,NuovoZeroPCL,...
        datainiPCLHR,tempoiniPCLHR,NodoPreConvLinkHR,NuovoZeroPCLHR,datainiDM,tempoiniDM,NodoDistoMTLink,NuovoZeroDM,...
        datainiPL,tempoiniPL,NodoPressureLink,NuovoZeroPrL,datainiLL,tempoiniLL,NodoLoadLink,NuovoZeroLL,...
        datainiEL,tempoiniEL,Date_Rif_EL,NodoExtensometerLink,NuovoZeroEL,...
        dataini3DEL,tempoini3DEL,Date_Rif_3DEL,Nodo3DExtensometerLink,NuovoZero3DEL,...
        datainiWEL,tempoiniWEL,NodoWireExtensometerLink,NuovoZeroWEL,...
        datainiMPBEL,tempoiniMPBEL,NodoMultiPointRodExtensometer,NuovoZeroMPBEL,...
        datainiAL,tempoiniAL,NodoAnalogLink,NumBasi,NuovoZeroAL,...
        datainiCrL,tempoiniCrL,NodoCrackLink,NuovoZeroCrL,dataini3DCrL,tempoini3DCrL,Nodo3DCrackLink,NuovoZero3DCrL,...
        dataini2DCrL,tempoini2DCrL,Nodo2DCrackLink,NuovoZero2DCrL,...
        datainiSM,tempoiniSM,Date_Rif_SM,NodoStressMeter,NuovoZeroSM,yesTuL,yesRL,yesTLH,...
        yesTLHRH,yesPCL,yesPCLHR,yesPL,yesLL,yesEL,yes3DEL,yesWEL,yesMPBEL,yesAL,yesCrL,...
        yes3DCrL,yes2DCrL,yesSM,yesDM,rTuL,rRL,rTLH,rTLHRH,rPCL,rPCLHR,rPrL,...
        rLL,rEL,r3DEL,rWEL,rMPBEL,rAL,rCrL,r3DCrL,r2DCrL,rSM,rDM,conn,catena,date,time,FileName);
    
    % Controllo incrociato fra la data iniziale definita da Lettura e la data
    % iniziale definita da LastElab
    [DatiElabTunnelLink,DatiElabRadialLink,DatiElabTiltLinkH,DatiElabTiltLinkHRH,...
        DatiElabPreConvLink,DatiElabPreConvLinkHR,DatiElabAnalogLink,DatiElabDistoMTLink]...
        = checkdata(yesTuL,yesRL,yesTLH,yesTLHRH,yesPCL,yesPCLHR,yesAL,yesDM,...
        DatiElabTunnelLink,DatiElabRadialLink,DatiElabTiltLinkH,DatiElabTiltLinkHRH,...
        DatiElabPreConvLink,DatiElabPreConvLinkHR,DatiElabAnalogLink,DatiElabDistoMTLink,...
        datainiTuL,tempoiniTuL,datainiRL,tempoiniRL,datainiTLH,tempoiniTLH,...
        datainiTLHRH,tempoiniTLHRH,datainiPCL,tempoiniPCL,datainiPCLHR,tempoiniPCLHR,...
        datainiAL,tempoiniAL,datainiDM,tempoiniDM,rTuL,rRL,rTLH,rTLHRH,rPCL,rPCLHR,rAL,rDM,...
        NodoTunnelLink,NodoRadialLink,NodoTiltLinkH,NodoTiltLinkHRH,NodoPreConvLink,...
        NodoPreConvLinkHR,NodoAnalogLink,NodoDistoMTLink,NuovoZeroTuL,NuovoZeroRL,NuovoZeroTLH,...
        NuovoZeroTLHRH,NuovoZeroPCL,NuovoZeroPCLHR,NuovoZeroAL,NuovoZeroDM,...
        IDcentralina,DTcatena,conn,FileName);
    
    %% Parte di definizione dati, conversione in unit� fisiche e calcolo medie
    
    % --- Tunnel Link ---
    if yesTuL == 1 % attiva l'elaborazione dei Tunnel Link
        % definizione dei dati
        [TimeTuL,accTuL,tempTuL,ay_TuL,ay_TuL_AC,ErrTunnelLink] = defDatiTuL(...
            DatiTunnelLink,ay_TuL,ay_TuL_AC,Ndatidespike,rTuL,NuovoZeroTuL,...
            ErrTunnelLink,MEMS,FileName);
        % Conversione dei dati grezzi
        [accTuL,ris_acc_TuL,T_TuL,ay_TuL,ay_TuL_AC] = conv_grezziTuL(...
            rTuL,accTuL,tempTuL,DCalTuLTot,ay_TuL,t_TuL,ay_TuL_AC,t_TuL_AC,...
            NuovoZeroTuL,MEMS,FileName);
        % media mobile dei dati
        [ARRAYdate_TuL,accTuL,ACCdefRis_TuL,TempDef_TuL,ay_TuL,ay_TuL_AC] = ...
            MediaDati_TuL(accTuL,TimeTuL,ris_acc_TuL,ay_TuL,ay_TuL_AC,NdatiMedia,...
            T_TuL,NuovoZeroTuL,FileName);
    else
        ARRAYdate_TuL = [];
        text = 'There are not data to convert for Tunnel Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiTunnelLink
    clear tempTuL
    clear DCalTuLTot
    clear t_TuL
    clear t_TuL_AC
    clear ris_acc_TuL
    clear T_TuL
    clear TimeTuL
    
    % --- Radial Link ---
    if yesRL == 1 % attiva l'elaborazione dei Radial Link
        % definizione dei dati
        [TimeRL,accRL,tempRL,ErrRadialLink] = defDatiRL(DatiRadialLink,...
            ErrRadialLink,Ndatidespike,rRL,MEMS,FileName);
        % Conversione dei dati grezzi
        [accRL,ris_acc_RL,T_RL] = conv_grezziRL(rRL,accRL,tempRL,DCalRLTot,MEMS,FileName);
        % media mobile dei dati
        [ARRAYdate_RL,accRL,ACCdefRis_RL,TempDef_RL] = MediaDati_RL(accRL,...
            TimeRL,ris_acc_RL,NdatiMedia,T_RL,FileName);
    else
        text = 'There are not data to convert for Radial Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        ARRAYdate_RL = [];
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiRadialLink
    clear tempRL
    clear DCalRLTot
    clear ris_acc_RL
    clear T_RL
    clear TimeRL
    
    % --- Tilt Link H ---
    if yesTLH == 1 % attiva l'elaborazione dei Tilt Link H
        % definizione dei dati
        [TimeTLH,accTLH,tempTLH,ErrTiltLinkH] = defDatiTLH(...
            DatiTiltLinkH,ErrTiltLinkH,Ndatidespike,rTLH,MEMS,FileName);
        % conversione dei dati grezzi
        [accTLH,ris_acc_TLH,tempTLH,ErrTiltLinkH] = conv_grezziTLH(rTLH,...
            accTLH,tempTLH,DCalTLHTot,ErrTiltLinkH,MEMS,FileName);
        % media mobile dei dati
        [ARRAYdate_TLH,accTLH,ACCdefRis_TLH,TempDef_TLH] = MediaDati_TLH(...
            accTLH,TimeTLH,ris_acc_TLH,NdatiMedia,tempTLH,FileName);
    else
        text = 'There are not data to convert for Tilt Link H';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        ARRAYdate_TLH = [];
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiTiltLinkH
    clear tempTLH
    clear DCalTLHTot
    clear ris_acc_TLH
    clear T_TLH
    clear TimeTLH
    
    % --- Tilt Link HR H ---
    if yesTLHRH == 1 % attiva l'elaborazione dei Tilt Link HR H
        % definizione dei dati
        [TimeTLHRH,angTLHRH,tempTLHRH,ErrTiltLinkHRH] = defDatiTLHRH(...
            DatiTiltLinkHRH,ErrTiltLinkHRH,Ndatidespike,NodoTiltLinkHRH,rTLHRH,...
            IDcentralina,DTcatena,FileName);
        % conversione dei dati grezzi
        [angTLHRH,tempTLHRH] = conv_grezziTLHRH(angTLHRH,tempTLHRH,DCalTLHRHTot,...
            rTLHRH,FileName);
        % media mobile dei dati
        [ARRAYdate_TLHRH,AngDef_TLHRH,TempDef_TLHRH] = MediaDati_TLHRH(angTLHRH,...
            TimeTLHRH,NdatiMedia,tempTLHRH,FileName);
    else
        text = 'There are not data to convert for Tilt Link HR H';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        ARRAYdate_TLHRH = [];
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiTiltLinkHRH
    clear tempTLHRH
    clear DCalTLHRHTot
    clear ang_TLHRH
    clear TimeTLHRH
    
    % --- PreConv Link ---
    if yesPCL == 1 % attiva l'elaborazione dei PreConv Link
        % definizione dei dati
        [TimePCL,accPCL,tempPCL,ErrPreConvLink] = defDatiPCL(DatiPreConvLink,...
            ErrPreConvLink,Ndatidespike,rPCL,MEMS,FileName);
        % conversione dei dati grezzi
        [accPCL,ris_acc_PCL,T_PCL] = conv_grezziPCL(rPCL,accPCL,tempPCL,DCalPCLTot,...
            MEMS,FileName);
        % media mobile dei dati
        [ARRAYdate_PCL,accPCL,ACCdefRis_PCL,TempDef_PCL] = MediaDati_PCL(...
            accPCL,TimePCL,ris_acc_PCL,NdatiMedia,T_PCL,FileName);
    else
        text = 'There are not data to convert for PreConv Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        ARRAYdate_PCL = [];
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiPreConvLink
    clear tempPCL
    clear TimePCL
    clear DCalPCLTot
    clear ris_acc_PCL
    clear T_PCL
    
    % --- PreConv Link HR ---
    if yesPCLHR == 1 % attiva l'elaborazione dei PreConv Link HR
        % definizione dei dati
        [TimePCLHR,angPCLHR,tempPCLHR,ErrPreConvLinkHR] = defDatiPCLHR(...
            DatiPreConvLinkHR,ErrPreConvLinkHR,NodoPreConvLinkHR,Ndatidespike,rPCLHR,...
            IDcentralina,DTcatena,FileName);
        % conversione dei dati grezzi
        [angPCLHR,tempPCLHR] = conv_grezziPCLHR(angPCLHR,tempPCLHR,DCalPCLHRTot,...
            rPCLHR,FileName);
        % media mobile dei dati
        [ARRAYdate_PCLHR,AngDef_PCLHR,TempDef_PCLHR] = MediaDati_PCLHR(...
            angPCLHR,TimePCLHR,NdatiMedia,tempPCLHR,FileName);
    else
        text = 'There are not data to convert for PreConv Link HR';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        ARRAYdate_PCLHR = [];
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiPreConvLinkHR
    clear angPCLHR
    clear tempPCLHR
    clear DCalPCLHRTot
    clear T_PCLHR
    clear TimePCLHR
    
%     % --- DistoMT Link ---
%     if yesTuL == 1 % attiva l'elaborazione
%         % definizione dei dati
%         [TimeTuL,accTuL,tempTuL,ay_TuL,ay_TuL_AC,ErrTunnelLink] = defDatiTuL(...
%             DatiTunnelLink,ay_TuL,ay_TuL_AC,Ndatidespike,rTuL,NuovoZeroTuL,...
%             ErrTunnelLink,MEMS,FileName);
%         % Conversione dei dati grezzi
%         [accTuL,ris_acc_TuL,T_TuL,ay_TuL,ay_TuL_AC] = conv_grezziTuL(...
%             rTuL,accTuL,tempTuL,DCalTuLTot,ay_TuL,t_TuL,ay_TuL_AC,t_TuL_AC,...
%             NuovoZeroTuL,MEMS,FileName);
%         % media mobile dei dati
%         [ARRAYdate_DL,accTuL,ACCdefRis_TuL,TempDef_TuL,ay_TuL,ay_TuL_AC] = ...
%             MediaDati_TuL(accTuL,TimeTuL,ris_acc_TuL,ay_TuL,ay_TuL_AC,NdatiMedia,...
%             T_TuL,NuovoZeroTuL,FileName);
%     else
%         ARRAYdate_DL = [];
%         text = 'There are not data to convert for Tunnel Link';
%         fileID = fopen(FileName,'a');
%         fmt = '%s \r';
%         fprintf(fileID,fmt,text);
%         fclose(fileID);
%         clear text
%     end
%     % Pulisco le variabili non pi� utilizzate
%     clear DatiTunnelLink
%     clear tempTuL
%     clear DCalTuLTot
%     clear t_TuL
%     clear t_TuL_AC
%     clear ris_acc_TuL
%     clear T_TuL
%     clear TimeTuL
    
    % --- Pressure Link ---
    if yesPL == 1 % arriva l'elaborazione dei Pressure Link
        % definizione dei dati
        [TimePL,Pressure,ErrPressureLink] = defDatiPL(DatiPressureLink,...
            ErrPressureLink,Ndatidespike,rPrL,FileName);
        % conversione dei dati grezzi
        DatiPressure = conv_grezziPL(Pressure,DCalPLTot,NodoPressureLink,FileName);
        % media mobile dei dati
        [ARRAYdate_PL,DatiPressure] = MediaDati_PL(DatiPressure,TimePL,NdatiMedia,FileName);
    else
        ARRAYdate_PL = [];
        DatiPressure = [];
        text = 'There are not data to convert for Pressure Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiPressureLink
    clear TimePL
    clear Pressure
    clear DCalPLTot
    
    % --- Load Link ---
    if  yesLL == 1 % attiva l'elaborazione dei Load Link
        % definizione dei dati
        [TimeLL,Load,ErrLoadLink] = defDatiLL(DatiLoadLink,ErrLoadLink,...
            Ndatidespike,rLL,FileName);
        % Conversione dei dati grezzi
        DatiLoad = conv_grezziLL(Load,DCalLLTot,NodoLoadLink,FileName);
        % media mobile dei dati
        [ARRAYdate_LL,DatiLoad] = MediaDati_LL(DatiLoad,TimeLL,NdatiMedia,FileName);
    else
        ARRAYdate_LL = [];
        DatiLoad = [];
        text = 'There are not data to convert for Load Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiLoadLink
    clear TimeLL
    clear Load
    clear DCalLLTot
    
    % --- Extensometer Link ---
    if yesEL == 1
        % definizione dei dati
        [TimeEL,Extensometer,NTCExtensometer,ErrExtensometerLink]...
            = defDatiEL(DatiExtensometerLink,DatiNTCExtensometerLink,ErrExtensometerLink,...
            ChEL,EL_NTC,rEL,Ndatidespike,FileName);
        % Conversione dei dati grezzi
        [DatiExtensometer,DatiExtensometer_Rif,DatiNTCExtensometer,ErrExtensometerLink,Carico_Sensore]...
            = conv_grezziEL(Extensometer,NTCExtensometer,DatiExtensometerLink_Rif,DCalELTot,EL_NTC,...
            NodoExtensometerLink,ErrExtensometerLink,Calcolo_Carico,FileName);
        % media mobile dei dati
        [ARRAYdate_EL,DatiExtensometer] = MediaDati_EL(DatiExtensometer,TimeEL,NdatiMedia,FileName);
    else
        ARRAYdate_EL = [];
        DatiExtensometer = [];
        DatiNTCExtensometer = [];
        text = 'There are not data to convert for Extensometer Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiExtensometerLink
    clear DatiNTCExtensometerLink
    clear DatiExtensometerLink_Rif
    clear TimeEL
    clear Extensometer
    clear NTCExtensometer
    clear DCalELTot
    
    % --- 3D Extensometer Link ---
    if yes3DEL == 1
        % definizione dei dati
        [Time3DEL,Extensometer3D,Extensometer3D_Rif,NTCExtensometer3D,...
            Err3DExtensometerLink,ErrNTC3DExtensometerLink] = ...
            defDati3DEL(Dati3DExtensometerLink,Dati3DExtensometerLink_Rif,...
            DatiNTC3DExtensometerLink,Err3DExtensometerLink,ErrNTC3DExtensometerLink,...
            Nodo3DExtensometerLink,r3DEL,Ch3DEL,EL3D_NTC,Ndatidespike,FileName);
        % Conversione dei dati grezzi
        [DatiExtensometer3D,DatiExtensometer3D_Rif,DatiNTCExtensometer3D,...
            Err3DExtensometerLink] = conv_grezzi3DEL(Extensometer3D,NTCExtensometer3D,...
            Extensometer3D_Rif,DCalEL3DTot,Nodo3DExtensometerLink,Err3DExtensometerLink,...
            r3DEL,EL3D_NTC,FileName);
        % media mobile dei dati
        [ARRAYdate_3DEL,DatiExtensometer3D] = MediaDati_3DEL(DatiExtensometer3D,Time3DEL,...
            NdatiMedia,FileName);
        % Sottraggo la misura di riferimento
        DatiExtensometer3D = DatiExtensometer3D - DatiExtensometer3D_Rif(1,:);
    else
        ARRAYdate_3DEL = [];
        DatiExtensometer3D = [];
        DatiNTCExtensometer3D = [];
        text = 'There are not data to convert for 3D Extensometer Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear Dati3DExtensometerLink
    clear DatiNTC3DExtensometerLink
    clear Dati3DExtensometerLink_Rif
    clear Time3DEL
    clear Extensometer3D
    clear NTCExtensometer3D
    clear DCal3DELTot
    clear DatiExtensometer3D_Rif
    
    % --- Wire Extensometer Link ---
    if yesWEL == 1
        % definizione dei dati
        [TimeWEL,WireExtensometer,NTCWireExtensometer,ErrWireExtensometerLink]...
            = defDatiWEL(DatiWireExtensometerLink,DatiNTCWireExtensometerLink,...
            ErrWireExtensometerLink,ChWEL,WEL_NTC,rWEL,Ndatidespike,FileName);
        % Conversione dei dati grezzi
        [DatiWireExtensometer,DatiNTCWireExtensometer,ErrWireExtensometerLink] = conv_grezziWEL(...
            WireExtensometer,NTCWireExtensometer,DCalWELTot,NodoWireExtensometerLink,...
            ErrWireExtensometerLink,WEL_NTC,rWEL,Tmax,Tmin,FileName);
        % media mobile dei dati
        [ARRAYdate_WEL,DatiWireExtensometer] = MediaDati_WEL(DatiWireExtensometer,...
            TimeWEL,NdatiMedia,FileName);
    else
        ARRAYdate_WEL = [];
        DatiWireExtensometer = [];
        DatiNTCWireExtensometer = [];
        text = 'There are not data to convert for Wire Extensometer Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiWireExtensometerLink
    clear DatiNTCWireExtensometerLink
    clear TimeWEL
    clear WireExtensometer
    clear NTCWireExtensometer
    clear DCalWELTot
    
    % --- MultiPoint Rod Extensometer ---
    if yesMPBEL == 1
        % definizione dei dati
        [TimeMPBEL,DatiMPBEL,~,ErrMultiPointExtensometer] = ...
            defDatiMPBEL(DatiMultiPointExtensometer,DatiNTCMultiPointExtensometer,...
            ErrMultiPointExtensometer,FileName);
        % Conversione dei dati grezzi
        DatiMultiBase = conv_grezziMPBEL(DatiMPBEL,NumBasi,DCalMPBELTot,FileName);
        % media mobile dei dati
        [ARRAYdate_MPBEL,DatiMultiBase] = MediaDati_MPBEL(DatiMultiBase,TimeMPBEL,NdatiMedia,FileName);
    else
        ARRAYdate_MPBEL = [];
        DatiMultiBase = [];
        text = 'There are not data to convert for MultiPoint Borehole Rod Extensometer';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiMultiPointExtensometer
    clear DatiNTCMultiPointExtensometer
    clear TimeMPBEL
    clear DCalMPBELTot
    
    % --- Analog Link ---
    if yesAL == 1
        % definizione dei dati
        [TimeAL,ADCAnalog,ErrAnalogLink] = defDatiAL(DatiAnalogLink,ErrAnalogLink,FileName);
        % Conversione dei dati grezzi
        DatiAnalog = conv_grezziAL(ADCAnalog,DCalALTot,NodoAnalogLink,FileName);
        % media mobile dei dati
        [ARRAYdate_AL,DatiAnalog] = MediaDati_AL(DatiAnalog,TimeAL,NdatiMedia,FileName);
    else
        ARRAYdate_AL = [];
        DatiAnalog = [];
        text = 'There are not data to convert for Analog Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiAnalogLink
    clear ADCAnalog
    clear TimeAL
    clear DCalALTot
    
    % --- Crack Link ---
    if yesCrL == 1
        % definizione dei dati
        [TimeCrL,Crack,NTCCrack,ErrCrackLink] = defDatiCrL(DatiCrackLink,...
            DatiNTCCrackLink,ErrCrackLink,ChCrL,CrL_NTC,rCrL,Ndatidespike,FileName);
        % Conversione dei dati grezzi
        [DatiCrack,DatiNTCCrack,ErrCrackLink] = conv_grezziCrL(Crack,NTCCrack,DCalCrLTot,...
            NodoCrackLink,ErrCrackLink,CrL_NTC,rCrL,NuovoZeroCrL,IDcentralina,DTcatena,FileName);
        % media mobile dei dati
        [ARRAYdate_CrL,DatiCrack] = MediaDati_CrL(DatiCrack,TimeCrL,NdatiMedia,FileName);
    else
        ARRAYdate_CrL = [];
        DatiCrack = [];
        DatiNTCCrack = [];
        text = 'There are not data to convert for Crack Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiCrackLink
    clear TimeCrL
    clear Crack
    clear NTCCrack
    clear DCalCrLTot
    
    % --- 2D Crack Link ---
    if yes2DCrL == 1
        % definizione dei dati
        [Time2DCrL,Crack2D,NTCCrack2D,Err2DCrackLink,ErrNTC2DCrackLink] = ...
            defDati2DCrL(Dati2DCrackLink,DatiNTC2DCrackLink,Err2DCrackLink,ErrNTC2DCrackLink,...
            r2DCrL,Nodo2DCrackLink,Ch2DCrL,CrL2D_NTC,Ndatidespike,FileName);
        % Conversione dei dati grezzi
        [Dati2DCrack,DatiNTCCrack2D,Err2DCrackLink] = conv_grezzi2DCrL(...
            Crack2D,NTCCrack2D,DCal2DCrLTot,Nodo2DCrackLink,Err2DCrackLink,CrL2D_NTC,...
            r2DCrL,NuovoZero2DCrL,IDcentralina,DTcatena,FileName);
        % media mobile dei dati
        [ARRAYdate_2DCrL,Dati2DCrack] = MediaDati_2DCrL(Dati2DCrack,Time2DCrL,NdatiMedia,FileName);
    else
        ARRAYdate_2DCrL = [];
        Dati2DCrack = [];
        DatiNTCCrack2D = [];
        text = 'There are not data to convert for 2D Crack Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    clear Dati2DCrackLink
    clear Time2DCrL
    clear Crack2D
    clear DCal2DCrLTot
    clear DatiNTC2DCrackLink
    
    % --- 3D Crack Link ---
    if yes3DCrL == 1
        % definizione dei dati
        [Time3DCrL,Crack3D,NTCCrack3D,Err3DCrackLink,ErrNTC3DCrackLink] = ...
            defDati3DCrL(Dati3DCrackLink,DatiNTC3DCrackLink,Err3DCrackLink,ErrNTC3DCrackLink,...
            r3DCrL,Nodo3DCrackLink,Ch3DCrL,CrL3D_NTC,Ndatidespike,FileName);
        % Conversione dei dati grezzi
        [Dati3DCrack,DatiNTCCrack3D,Err3DCrackLink] = conv_grezzi3DCrL(...
            Crack3D,NTCCrack3D,DCal3DCrLTot,Nodo3DCrackLink,Err3DCrackLink,CrL3D_NTC,...
            r3DCrL,NuovoZero3DCrL,IDcentralina,DTcatena,FileName);
        % media mobile dei dati
        [ARRAYdate_3DCrL,Dati3DCrack] = MediaDati_3DCrL(Dati3DCrack,Time3DCrL,NdatiMedia,FileName);
    else
        ARRAYdate_3DCrL = [];
        Dati3DCrack = [];
        DatiNTCCrack3D = [];
        text = 'There are not data to convert for 3D Crack Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    clear Dati3DCrackLink
    clear Time3DCrL
    clear Crack3D
    clear DCal3DCrLTot
    clear DatiNTC3DCrackLink
    
    % --- Stress Meter ---
    if yesSM == 1
        % definizione dei dati
        [TimeSM,Stress,NTCStress,ErrStressMeter] = defDatiSM(DatiStressMeter,...
            DatiNTCStressMeter,ErrStressMeter,ChSM,SM_NTC,rSM,Ndatidespike,FileName);
        % Conversione dei dati grezzi
        [DatiStress,DatiNTCStress,ErrStressMeter] = conv_grezziSM(Stress,NTCStress,...
            DatiStressMeter_Rif,DatiNTCStressMeter_Rif,DCalSMTot,NodoStressMeter,ErrStressMeter,...
            SM_NTC,rSM,FileName);
        % media mobile dei dati
        [ARRAYdate_SM,DatiStress] = MediaDati_SM(DatiStress,TimeSM,NdatiMedia,FileName);
    else
        ARRAYdate_SM = [];
        DatiStress = [];
        DatiNTCStress = [];
        text = 'There are not data to convert for Stress Meter';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili non pi� utilizzate
    clear DatiStressMeter
    clear DatiNTCStressMeter
    clear TimeSM
    clear Stress
    clear NTCStress
    clear DCalSMTot
    
    %% Parte di Elaborazione
    
    % --- Analisi Tunnel Link ---
    if yesTuL == 1
        [X_TuL,Xstar_TuL,Y_TuL,Z_TuL,Zstar_TuL,Xlocal_TuL,Ylocal_TuL,Zlocal_TuL,...
            SegStar,HShift_local_TuL,TempDef_TuL,ARRAYdate_TuL,Area_TuL,Speed_local_TuL,...
            Acceleration_local_TuL,ErrTunnelLink] = elab2D_TuL(rTuL,accTuL,ay_TuL,ay_TuL_AC,...
            ACCdefRis_TuL,SpeTuL,IDcentralina,DTcatena,DatiElabTunnelLink,NuovoZeroTuL,...
            TempDef_TuL,tolleranzaAcc,Ndevst,Wdevst,NdatiMedia,Ndatidespike,ARRAYdate_TuL,...
            data1_AC,Nodo_AC,margine,ErrTunnelLink,Tmax,Tmin,datainiTuL,FileName);
    else
        [X_TuL,Xstar_TuL,Y_TuL,Z_TuL,Zstar_TuL,Xlocal_TuL,Ylocal_TuL,Zlocal_TuL,...
            SegStar,HShift_local_TuL,TempDef_TuL,Area_TuL,Speed_local_TuL,...
            Acceleration_local_TuL] = matrici_vuote_TuL(FileName);
    end
    clear DatiElabTunnelLink
    clear rTuL
    clear accTuL
    clear ay_TuL
    clear ay_TuL_AC
    clear data1_AC
    clear ACCdefRis_TuL
    clear SpeTuL
    clear DatiElabTunnelLink
    clear NuovoZeroTuL
    clear Nodo_AC
    clear data1_AC
    % fine elaborazione Tunnel Link
    
    % --- Analisi Radial Link ---
    if yesRL == 1
        [X_RL,Y_RL,Z_RL,Xlocal_RL,Ylocal_RL,Zlocal_RL,TempDef_RL,ARRAYdate_RL,...
            ErrRadialLink] = elab2D_RL(rRL,accRL,ACCdefRis_RL,SpeRL,IDcentralina,DTcatena,...
            DatiElabRadialLink,NuovoZeroRL,tolleranzaAcc,TempDef_RL,Ndevst,Wdevst,ARRAYdate_RL,...
            elab_option,NdatiMedia,Ndatidespike,margine,ErrRadialLink,Tmax,Tmin,datainiRL,FileName);
    else
        [X_RL,Y_RL,Z_RL,Xlocal_RL,Ylocal_RL,Zlocal_RL,TempDef_RL] = matrici_vuote_RL(FileName);
    end
    clear DatiElabRadialLink
    clear accRL
    clear SpeRL
    clear ACCdefRis_RL
    clear NuovoZeroRL
    clear rRL
    % fine elaborazione Radial Link
    
    % --- Tilt Link H ---
    if yesTLH == 1
        [Y_TLH,Z_TLH,Ylocal_TLH,Zlocal_TLH,AlfaX_TLH,AlfaY_TLH,TempDef_TLH,...
            speed_TLH,speed_local_TLH,acceleration_TLH,acceleration_local_TLH,ARRAYdate_TLH,...
            ErrTiltLinkH] = elab2D_TLH(IDcentralina,DTcatena,rTLH,accTLH,ACCdefRis_TLH,...
            TempDef_TLH,SpeTLH,PsTLH,tolleranzaAcc,DatiElabTiltLinkH,Ndevst,Wdevst,...
            ARRAYdate_TLH,NuovoZeroTLH,NdatiMedia,Ndatidespike,ErrTiltLinkH,margine,...
            elab_option,Tmax,Tmin,datainiTLH,FileName);
    else
        [Y_TLH,Z_TLH,Ylocal_TLH,Zlocal_TLH,AlfaX_TLH,AlfaY_TLH,TempDef_TLH,...
            speed_TLH,speed_local_TLH,acceleration_TLH,acceleration_local_TLH] = matrici_vuote_TLH(FileName);
    end
    clear DatiElabTiltLinkH
    clear accTLH
    clear ACCdefRis_TLH
    clear SpeTLH
    clear PsTLH
    clear NuovoZeroTLH
    % fine elaborazione Tilt Link H
    
    % --- Tilt Link HR H ---
    if  yesTLHRH == 1
        [Y_TLHRH,Z_TLHRH,Ylocal_TLHRH,Zlocal_TLHRH,AlfaX_TLHRH,AlfaY_TLHRH,...
            TempDef_TLHRH,speed_TLHRH,speed_local_TLHRH,acceleration_TLHRH,acceleration_local_TLHRH,...
            ARRAYdate_TLHRH,ErrTiltLinkHRH] = elab2D_TLHRH(rTLHRH,AngDef_TLHRH,TempDef_TLHRH,...
            SpeTLHRH,PsTLHRH,DatiElabTiltLinkHRH,Ndevst_HR,Wdevst_HR,ARRAYdate_TLHRH,...
            NuovoZeroTLHRH,NdatiMedia,Ndatidespike,ErrTiltLinkHRH,margine,elab_option,...
            Tmax,Tmin,datainiTLHRH,FileName);
    else
        [Y_TLHRH,Z_TLHRH,Ylocal_TLHRH,Zlocal_TLHRH,AlfaX_TLHRH,AlfaY_TLHRH,TempDef_TLHRH,...
            speed_TLHRH,speed_local_TLHRH,acceleration_TLHRH,acceleration_local_TLHRH] = matrici_vuote_TLHRH(FileName);
    end
    clear calotta_base
    clear DatiElabTiltLinkHRH
    clear AngDef_TLHRH
    clear SpeTLHRH
    clear PsTLHRH
    clear NuovoZeroTLHRH
    % fine elaborazione Tilt Link HR H
    
    % --- PreConv Link ---
    if yesPCL == 1
        [Y_PCL,Z_PCL,Ylocal_PCL,Zlocal_PCL,AlfaX_PCL,AlfaY_PCL,TempDef_PCL,...
            speed_PCL,speed_local_PCL,acceleration_PCL,acceleration_local_PCL,ARRAYdate_PCL,...
            ErrPreConvLink] = elab2D_PCL(IDcentralina,DTcatena,rPCL,accPCL,ACCdefRis_PCL,...
            TempDef_PCL,SpePCL,tolleranzaAcc,DatiElabPreConvLink,Ndevst,Wdevst,ARRAYdate_PCL,...
            NuovoZeroPCL,NdatiMedia,Ndatidespike,ErrPreConvLink,margine,elab_option,...
            Tmax,Tmin,datainiPCL,FileName);
    else
        [Y_PCL,Z_PCL,Ylocal_PCL,Zlocal_PCL,AlfaX_PCL,AlfaY_PCL,TempDef_PCL,...
            speed_PCL,speed_local_PCL,acceleration_PCL,acceleration_local_PCL] = matrici_vuote_PCL(FileName);
    end
    clear DatiElabPreConvLink
    clear accPCL
    clear ACCdefRis_PCL
    clear SpePCL
    clear tolleranzaAcc
    clear DatiElabPreConvLink
    clear NuovoZeroPCL
    clear rPCL
    clear Ndevst
    clear Wdevst
    % fine elaborazione PreConv Link
    
    % --- PreConv Link HR ---
    if  yesPCLHR == 1
        [Y_PCLHR,Z_PCLHR,Ylocal_PCLHR,Zlocal_PCLHR,AlfaX_PCLHR,AlfaY_PCLHR,TempDef_PCLHR,...
            speed_PCLHR,speed_local_PCLHR,acceleration_PCLHR,acceleration_local_PCLHR,ARRAYdate_PCLHR,...
            ErrPreConvLinkHR] = elab2D_PCLHR(IDcentralina,DTcatena,rPCLHR,AngDef_PCLHR,...
            TempDef_PCLHR,SpePCLHR,DatiElabPreConvLinkHR,Ndevst_HR,Wdevst_HR,ARRAYdate_PCLHR,...
            NuovoZeroPCLHR,NdatiMedia,Ndatidespike,ErrPreConvLinkHR,margine,elab_option,Tmax,Tmin,...
            datainiPCLHR,FileName);
    else
        [Y_PCLHR,Z_PCLHR,Ylocal_PCLHR,Zlocal_PCLHR,AlfaX_PCLHR,AlfaY_PCLHR,TempDef_PCLHR,...
            speed_PCLHR,speed_local_PCLHR,acceleration_PCLHR,acceleration_local_PCLHR] = matrici_vuote_PCLHR(FileName);
    end
    clear DatiElabPreConvLinkHR
    clear rPCLHR
    clear AngDef_PCLHR
    clear SpePCLHR
    clear elab_option
    clear Ndevst_HR
    clear Wdevst_HR
    clear NuovoZeroPCLHR
    clear NdatiMedia
    clear Ndatidespike
    clear tolleranzaT
    clear margine
    clear elab_option
    
    % --- Extensometer Link ---
    if yesEL == 1
       % Sottraggo la misura di riferimento
        DatiExtensometer = DatiExtensometer - DatiExtensometer_Rif(1,:);
        % Calcolo lo sforzo e il carico assoluto
        if Calcolo_Carico == 1
            Epsilon = DatiExtensometer/1000000;
            Sforzo = Epsilon.*Mod_Elastico; % MPa
            Carico_rel = Area*Sforzo/1000; % kN
            Carico_ass = Carico_rel+Carico_Ini+Carico_Sensore'; % kN
            Sforzo = Carico_ass*1000/Area; % MPa
        else
            Carico_ass = [];
            Sforzo = [];
        end
        clear DatiExtensometer_Rif
    else
        Carico_ass = [];
        Sforzo = [];
    end
    
    % --- 3D Extensometer Link ---
    % Correzione 3D Extensometer Link
    if yes3DEL == 1
        [r,~] = size(Err3DExtensometerLink);
        Matrice_err = zeros(r,r3DEL);
        for i = 1:r % date
            d = 1;
            for n = 1:r3DEL % nodi
                j = 1;
                err = Err3DExtensometerLink(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
        end
        Err3DExtensometerLink = Matrice_err;
        Join = find(ErrNTC3DExtensometerLink);
        [rJ,cJ] = size(Join);
        for ii = 1:rJ
            for jj = 1:cJ
                if Err3DExtensometerLink(Join(ii,jj)) == 0
                    Err3DExtensometerLink(Join(ii,jj)) = 0.5;
                end
            end
        end
    end
    clear ErrNTC3DExtensometerLink
    
    % --- Crack Link ---
    if yesCrL == 1
        NomeFile = ['' IDcentralina '-' DTcatena '-RifCrL.csv'];
        if NuovoZeroCrL == 0 % prima elaborazione
            csvwrite(NomeFile,DatiCrack(1,:));
            DatiCrack = DatiCrack - DatiCrack(1,:);
        else % Ci sono gi� dei dati elaborati
            RIF = csvread(NomeFile);
            DatiCrack = DatiCrack - RIF;
        end
    end
    
    % --- 3D Crack Link ---
    if yes3DCrL == 1
        [r,~] = size(Err3DCrackLink);
        Matrice_err = zeros(r,r3DCrL);
        for i = 1:r % date
            d = 1;
            for n = 1:r3DCrL % nodi
                j = 1;
                err = Err3DCrackLink(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
        end
        Err3DCrackLink = Matrice_err;
        Join = find(ErrNTC3DCrackLink);
        [rJ,cJ] = size(Join);
        for ii = 1:rJ
            for jj = 1:cJ
                if Err3DCrackLink(Join(ii,jj)) == 0
                    Err3DCrackLink(Join(ii,jj)) = 0.5;
                end
            end
        end
    end
    clear ErrNTC3DCrackLink
    
    % --- 2D Crack Link ---
    if yes2DCrL == 1
        [r,~] = size(Err2DCrackLink);
        Matrice_err = zeros(r,r2DCrL);
        for i = 1:r % date
            d = 1;
            for n = 1:r2DCrL % nodi
                j = 1;
                err = Err2DCrackLink(i,d:d+1);
                while j <= 2
                    if err(1,j) == 1
                        Matrice_err(i,n) = 1;
                    end
                    j = j+1;
                end
                d = d+2;
            end
        end
        Err2DCrackLink = Matrice_err;
        Join = find(ErrNTC2DCrackLink);
        [rJ,cJ] = size(Join);
        for ii = 1:rJ
            for jj = 1:cJ
                if Err2DCrackLink(Join(ii,jj)) == 0
                    Err2DCrackLink(Join(ii,jj)) = 0.5;
                end
            end
        end
    end
    clear ErrNTC2DCrackLink
    
    % --- MultiPoint Borehole Extensometer Link ---
    % Correzione MultiPoint Extensometer
    if yesMPBEL == 1
        [r,~] = size(ErrMultiPointExtensometer);
        Matrice_err = zeros(r,rMPBEL);
        for i = 1:r % date
            d = 1;
            for n = 1:rMPBEL % nodi
                j = 1;
                err = ErrMultiPointExtensometer(i,d:d+NumBasi-1);
                while j <= NumBasi
                    if err(1,j) == 1
                        Matrice_err(i,n) = 1;
                    end
                    j = j+1;
                end
                d = d+NumBasi;
            end
        end
        ErrMultiPointExtensometer = Matrice_err;
        Join = find(ErrNTCMultiPointExtensometer);
        [rJ,cJ] = size(Join);
        for ii = 1:rJ
            for jj = 1:cJ
                if ErrMultiPointExtensometer(Join(ii,jj)) == 0
                    ErrMultiPointExtensometer(Join(ii,jj)) = 0.5;
                end
            end
        end
    end
    clear ErrNTCMultiPointExtensometer
    
    %% Parte di Scrittura su Database
    database_write(catena,IDcentralina,DTcatena,X_TuL,Xstar_TuL,Y_TuL,Z_TuL,Zstar_TuL,...
        Xlocal_TuL,Ylocal_TuL,Zlocal_TuL,SegStar,HShift_local_TuL,Area_TuL,...
        Speed_local_TuL,Acceleration_local_TuL,TempDef_TuL,ARRAYdate_TuL,ErrTunnelLink,...
        X_RL,Y_RL,Z_RL,Xlocal_RL,Ylocal_RL,Zlocal_RL,TempDef_RL,ARRAYdate_RL,ErrRadialLink,...
        Y_TLH,Z_TLH,Ylocal_TLH,Zlocal_TLH,AlfaX_TLH,AlfaY_TLH,TempDef_TLH,speed_TLH,...
        speed_local_TLH,acceleration_TLH,acceleration_local_TLH,ARRAYdate_TLH,ErrTiltLinkH,...
        Y_TLHRH,Z_TLHRH,Ylocal_TLHRH,Zlocal_TLHRH,AlfaX_TLHRH,AlfaY_TLHRH,TempDef_TLHRH,speed_TLHRH,...
        speed_local_TLHRH,acceleration_TLHRH,acceleration_local_TLHRH,ARRAYdate_TLHRH,ErrTiltLinkHRH,...
        Y_PCL,Z_PCL,Ylocal_PCL,Zlocal_PCL,AlfaX_PCL,AlfaY_PCL,TempDef_PCL,speed_PCL,...
        speed_local_PCL,acceleration_PCL,acceleration_local_PCL,ARRAYdate_PCL,ErrPreConvLink,...
        Y_PCLHR,Z_PCLHR,Ylocal_PCLHR,Zlocal_PCLHR,AlfaX_PCLHR,AlfaY_PCLHR,TempDef_PCLHR,speed_PCLHR,...
        speed_local_PCLHR,acceleration_PCLHR,acceleration_local_PCLHR,ARRAYdate_PCLHR,ErrPreConvLinkHR,...
        DatiPressure,ARRAYdate_PL,ErrPressureLink,DatiLoad,ARRAYdate_LL,ErrLoadLink,...
        DatiExtensometer,DatiNTCExtensometer,ARRAYdate_EL,Carico_ass,Sforzo,Calcolo_Carico,ErrExtensometerLink,ChEL,EL_NTC,...
        DatiExtensometer3D,DatiNTCExtensometer3D,ARRAYdate_3DEL,Err3DExtensometerLink,Ch3DEL,EL3D_NTC,...
        DatiWireExtensometer,DatiNTCWireExtensometer,ARRAYdate_WEL,ErrWireExtensometerLink,ChWEL,WEL_NTC,...
        DatiMultiBase,ARRAYdate_MPBEL,ErrMultiPointExtensometer,NumBasi,...
        DatiAnalog,ARRAYdate_AL,ErrAnalogLink,DatiCrack,DatiNTCCrack,ChCrL,CrL_NTC,ARRAYdate_CrL,ErrCrackLink,...
        Dati3DCrack,DatiNTCCrack3D,ARRAYdate_3DCrL,Ch3DCrL,CrL3D_NTC,Err3DCrackLink,...
        Dati2DCrack,DatiNTCCrack2D,ARRAYdate_2DCrL,Ch2DCrL,CrL2D_NTC,Err2DCrackLink,...
        DatiStress,DatiNTCStress,ARRAYdate_SM,ErrStressMeter,ChSM,SM_NTC,...
        conn,FileName);
    
    %% Rilancio software
    [yesRSN,yesMusa] = SWSearch(idTool,conn,FileName);
    try
        if yesRSN == 1
            rilancio = ['/usr/local/matlab_func/run_RSN_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93 ' IDcentralina ' ' DTcatena ''];
            status = system(rilancio);
            fileID = fopen(FileName,'a');
            fmt = '%s \r';
            fprintf(fileID,fmt,status);
            fclose(fileID);
        end
        if yesMusa == 1
            rilancio = ['/usr/local/matlab_func/run_Musa_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93 ' IDcentralina ' ' DTcatena ''];
            status = system(rilancio);
            fileID = fopen(FileName,'a');
            fmt = '%s \r';
            fprintf(fileID,fmt,status);
            fclose(fileID);
        end
    catch err
        FileErr = ['ErrorFile-' IDcentralina '-' datestr(today) '-' datestr(now,'hhMMss')];
        fid = fopen(FileErr,'a+');
        fprintf(fid, '%s', err.getReport('extended','hyperlinks','off'));
        fclose(fid);
    end
end

%% Parte di allertamento
 
% Determino gli utenti da utilizzare in tutte le funzioni successive
[Mail,~,~,Users_Report,~,activeEN,...
    sms,siteID,NomeSito,ini_CoV] = Users_Def(IDcentralina,conn,FileName);

% Determino alcune informazioni del sito che mi servono nei passaggi
% successivi
[unitID,chainID,Chain_Scheme,num_nodi,alarms] = Site_Info(siteID,conn,FileName);

% % Funzione Soglie sperimentale ASE
% [~,unitID,chainID,Chain_Scheme,num_nodi] = soglie(IDcentralina,...
%     yesTuL,yesRL,yesTLH,yesTLHRH,yesPCL,yesPCLHR,conn,FileName);

% Funzione per soglie sito specifiche
alert_Levels(rLL,rCrL,DatiLoad,DatiCrack,ARRAYdate_LL,ARRAYdate_CrL,...
    ErrLoadLink,ErrCrackLink,NodoLoadLink,NodoCrackLink,IDcentralina,DTcatena,...
    sms,date,time,conn,FileName);

if strcmp(Unit,'Nesa evolution') == 0
    % Funzione che elabora i Control Tools
    alarms = Siren(siteID,yesTuL,yesRL,yesTLH,yesTLHRH,yesPCL,yesPCLHR,...
        yesCrL,yes3DCrL,yes2DCrL,yesEL,yes3DEL,yesWEL,yesMPBEL,datainiTuL,tempoiniTuL,...
        datainiRL,tempoiniRL,datainiTLH,tempoiniTLH,datainiTLHRH,tempoiniTLHRH,...
        datainiPCL,tempoiniPCL,datainiPCLHR,tempoiniPCLHR,datainiCrL,tempoiniCrL,...
        dataini3DCrL,tempoini3DCrL,dataini2DCrL,tempoini2DCrL,...
        datainiEL,tempoiniEL,dataini3DEL,tempoini3DEL,...
        datainiWEL,tempoiniWEL,datainiMPBEL,tempoiniMPBEL,ARRAYdate_TuL,ARRAYdate_RL,...
        ARRAYdate_TLH,ARRAYdate_TLHRH,ARRAYdate_PCL,ARRAYdate_PCLHR,ARRAYdate_CrL,...
        ARRAYdate_3DCrL,ARRAYdate_2DCrL,ARRAYdate_EL,ARRAYdate_3DEL,ARRAYdate_WEL,ARRAYdate_MPBEL,...
        conn,date,time,FileName);
end

% Controllo del Livello della Batteria
checkBattery(Batteria,IDcentralina,Mail,conn,FileName);

% Controllo della data di chiusura piattaforma per una determinata centralina
checkUnit(IDcentralina,conn,FileName);

% Controllo della data di scadenza della SIM
checkSIM(IDcentralina,conn,FileName);

% Controllo della data di scadenza del GIS
checkGIS(siteID,NomeSito,conn,FileName)

%% Parte di Report
Report_ASE(IDcentralina,siteID,unitID,chainID,Chain_Scheme,num_nodi,...
    alarms,Mail,Users_Report,activeEN,time,conn,FileName);

toc
text = ['ATD calculation ended in ' num2str(toc) ' seconds.'];
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);
fclose(fileID);

RC = 0;

end
                    