function RC = Tilt(IDcentralina,DTcatena)
%%% Programma per l'elaborazione dei dati inclinometrici
%%% Versione 2.6 iniziata a maggio 21

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);

DTcatenaRif = '0';

%% Parte generale

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

% Apro 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);
% conn = database('',<username>,<password>,'com.mysql.cj.jdbc.Driver','jdbc:mysql://servername:portnumber/databasename?serverTimezone=UTC&')

% 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,unitID] = datainiziale(DTcatena,unitID,conn,FileName);

% funzione che elenca tutti i nodi ed il loro numero presenti per ogni tipo di sensore.
[idTool,NodoTiltLink,NodoTiltLinkHR,NodoTiltLinkH,NodoTiltLinkHRH,...
    NodoTiltLinkHR3D,NodoTiltLinkHR3DH,NodoPiezoLink,NodoBaroLink,...
    NodoThermLink,NodoAnalogLink,NodoLoadLink,NodoKlinoLink,NodoKlinoLinkHR,...
    NodoRainLink,NodoPT100Link,NodoInPlaceLink,NodoInPlaceLinkHR,NodoInPlaceLinkHR3D,...
    NodoWeirLink,NodoPendulum,NodoWind,NodoTiltLinkHD,NodoTiltLinkHDVR,NodoSPPLink,NodoSnowLink,...
    rTL,rTLHR,rTLH,rTLHRH,~,~,rPL,rBL,rThL,rAL,rLL,rKL,rKLHR,...
    rRL,rPT100,rIPL,rIPLHR,~,rWL,rPE,~,rHD,rHDVR,rSPP,rSL]...
    = 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
[yesTL,yesTLHR,yesTLHR3D,yesTLH,yesTLHRH,yesTLHR3DH,yesPL,yesBL,...
    yesThL,yesAL,yesLL,yesKL,yesKLHR,yesRL,yesPT100,yesIPL,yesIPLHR,yesIPLHR3D,...
    yesWL,yesPE,NodoPiezoLink,~,yesHD,yesHDVR,yesSPP,yesSL] = YesNo(NodoTiltLink,NodoTiltLinkHR,...
    NodoTiltLinkHR3D,NodoTiltLinkH,NodoTiltLinkHRH,NodoTiltLinkHR3DH,NodoPiezoLink,...
    NodoBaroLink,NodoThermLink,NodoAnalogLink,NodoLoadLink,NodoKlinoLink,NodoKlinoLinkHR,...
    NodoRainLink,NodoPT100Link,NodoInPlaceLink,NodoInPlaceLinkHR,NodoInPlaceLinkHR3D,...
    NodoWeirLink,NodoPendulum,NodoWind,NodoTiltLinkHD,NodoTiltLinkHDVR,NodoSPPLink,NodoSnowLink,FileName);

% funzione che definisce tutti i parametri importanti per elaborare un'installazione
[MEMS,biax,triax,struct,tolleranzaAcc,tolleranzaMag,segnoNS,...
    segnoEO,Ndevst,Wdevst,segnoNS_HR,segnoEO_HR,Ndevst_HR,Wdevst_HR,allineato,...
    NdatiMedia,NdatiMediaP,Ndatidespike,Anchor_real,Tmax,Tmin,Corr_Azimuth,Traversine,...
    base,altezza,larghezza] = ...
    Parametri_Installazione(unitID,idTool,DTcatena,yesTL,yesTLHR,yesTLH,yesTLHRH,yesPL,...
    yesThL,yesKL,yesKLHR,yesPT100,yesIPL,yesIPLHR,yesWL,yesHD,yesHDVR,conn,FileName);

% funzione che ricostruisce segmenti di pertinenza etc
[SpeTL,PsTL,SpeTLHR,PsTLHR,SpeTLH,PsTLH,SpeTLHRH,PsTLHRH,SpeIPL,PsIPL,Inverti,...
    SpeIPLHR,PsIPLHR,SpeHD,PsHD,SpeHDVR,PsHDVR] = schemaSP(yesTL,yesTLHR,yesTLH,yesTLHRH,yesTLHR3D,yesTLHR3DH,...
    yesIPL,yesIPLHR,yesIPLHR3D,yesHD,yesHDVR,NodoTiltLink,NodoTiltLinkHR,NodoTiltLinkHR3D,...
    NodoTiltLinkH,NodoTiltLinkHRH,NodoTiltLinkHR3DH,NodoInPlaceLink,NodoInPlaceLinkHR,...
    NodoInPlaceLinkHR3D,NodoTiltLinkHD,NodoTiltLinkHDVR,Anchor_real,...
    rTL,rTLH,rTLHR,rTLHRH,rIPL,rIPLHR,rHD,rHDVR,catena,IDcentralina,DTcatena,FileName);

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

%% Scarico dei dati

% funzione che scarica i dati di calibrazione
[DCalTLTot,DCalHDTot,DCalHDVRTot,DCalTLHRTot,DCalTLHTot,DCalTLHRHTot,DCalPLTot,DCalBLTot,...
    ~,DCalLLTot,DCalKLTot,DCalKLHRTot,DCalRLTot,DCalThLTot,DCalPT100Tot,...
    DCalIPLTot,DCalIPLHRTot,DCalWLTot,DCalPETot,Barometri,yesTL,yesHD,yesHDVR,yesTLHR,yesTLH,...
    yesTLHRH,yesPL,yesBL,~,yesLL,yesKL,yesKLHR,yesThL,yesPT100,yesIPL,yesIPLHR,yesWL] = ...
    letturaCal(IDcentralina,DTcatena,catena,yesTL,yesHD,yesHDVR,yesTLHR,yesTLH,yesTLHRH,...
    yesPL,yesBL,yesAL,yesLL,yesKL,yesKLHR,yesThL,yesPT100,yesIPL,yesIPLHR,yesWL,...
    rTL,rHD,rHDVR,rPL,rBL,rTLHR,rAL,rTLH,rTLHRH,rLL,rThL,rKL,rKLHR,rRL,rPT100,rIPL,rIPLHR,...
    rWL,rPE,NodoTiltLink,NodoTiltLinkHD,NodoTiltLinkHDVR,NodoTiltLinkH,NodoPiezoLink,NodoBaroLink,...
    NodoLoadLink,NodoKlinoLink,NodoKlinoLinkHR,NodoInPlaceLink,NodoInPlaceLinkHR,NodoWeirLink,...
    NodoPendulum,NodoRainLink,NodoThermLink,conn,FileName);

if strcmp(Unit,'Nesa evolution')
    Data = datestr(now-30,'yyyy-mm-dd HH:MM:SS.FFF'); % faccio di default 30 gg
    comando = ['select JSON_VALUE(dataJSON,"$.battery") from ELABDATANESA where EventTimestamp >= ''' Data ...
        ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeNum = 1'];
    curs = exec(conn,comando);
    curs = fetch(curs);
    Batteria = curs.Data;
    comando = ['select EventTimestamp from ELABDATANESA where EventTimestamp >= ''' Data ...
        ''' and UnitName = ''' IDcentralina ''' and ToolNameID = '''...
        DTcatena ''' and NodeNum = 1'];
    curs = exec(conn,comando);
    curs = fetch(curs);
    DataOra = curs.Data;
    Batteria = [DataOra Batteria];
    ARRAYdateRL = [];
    Batteria_LoRa = [];
elseif (strcmp(Unit,'SISGEO WR-GW-868'))
    % --- Batteria ---
    Data = datestr(now-30,'yyyy-mm-dd'); % faccio di default 30 gg
    Ora = datestr(now-30,'HH:MM:SS.FFF'); % faccio di default 30 gg
    comando = ['select Date, Time, BatLevel from RawDataView where Date = ''' ...
        Data ''' and Time >= ''' Ora ''' and  ToolNameID = ''' DTcatena...
        ''' and UnitName = ''' IDcentralina ''' and NodeNum = 1 '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    BatTempo = curs.Data;
    comando = ['select Date, Time, BatLevel from RawDataView where Date > ''' ...
        Data ''' and  ToolNameID = ''' DTcatena...
        ''' and UnitName = ''' IDcentralina ''' and NodeNum = 1 '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    BatData = curs.Data;
    if strcmp(char(BatData(1)),'No Data')
        Batteria = BatTempo;
    elseif strcmp(char(BatTempo(1)),'No Data')
        Batteria = BatData;
    else
        Batteria = [BatTempo;BatData];
    end
    
    % --- Batteria LoRa ---
    comando = ['select Date, Time, BatLevelModule from RawDataView where Date = ''' ...
        Data ''' and Time >= ''' Ora ''' and  ToolNameID = ''' DTcatena...
        ''' and UnitName = ''' IDcentralina ''' and NodeNum = 1 '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    BatTempo = curs.Data;
    comando = ['select Date, Time, BatLevelModule from RawDataView where Date > ''' ...
        Data ''' and  ToolNameID = ''' DTcatena...
        ''' and UnitName = ''' IDcentralina ''' and NodeNum = 1 '];
    curs = exec(conn,comando);
    curs = fetch(curs);
    BatData = curs.Data;
    if strcmp(char(BatData(1)),'No Data')
        Batteria_LoRa = BatTempo;
    elseif strcmp(char(BatTempo(1)),'No Data')
        Batteria_LoRa = BatData;
    else
        Batteria_LoRa = [BatTempo;BatData];
    end
else
    % Funzione che scarica la data di riferimento
    [DatiElabTiltLink,NuovoZeroTL,datainiTL,tempoiniTL,...
        DatiElabTiltLinkHD,NuovoZeroHD,datainiHD,tempoiniHD,...
        DatiElabTiltLinkHDVR,NuovoZeroHDVR,datainiHDVR,tempoiniHDVR,...
        DatiElabTiltLinkHR,NuovoZeroTLHR,datainiTLHR,tempoiniTLHR,...
        DatiElabTiltLinkH,NuovoZeroTLH,datainiTLH,tempoiniTLH,...
        DatiElabTiltLinkHRH,NuovoZeroTLHRH,datainiTLHRH,tempoiniTLHRH,...
        DatiElabInPlaceLink,NuovoZeroIPL,datainiIPL,tempoiniIPL,...
        DatiElabInPlaceLinkHR,NuovoZeroIPLHR,datainiIPLHR,tempoiniIPLHR,...
        NuovoZeroPL,datainiPL,tempoiniPL,NuovoZeroBL,datainiBL,tempoiniBL,Barometro,...
        NuovoZeroThL,datainiThL,tempoiniThL,NuovoZeroLL,datainiLL,tempoiniLL,...
        NuovoZeroKL,datainiKL,tempoiniKL,NuovoZeroKLHR,datainiKLHR,tempoiniKLHR,...
        DatiElabRainLink,NuovoZeroRL,datainiRL,tempoiniRL,datainiSPP,tempoiniSPP,NuovoZeroSPP,...
        DatiElabSnowLink,NuovoZeroSL,datainiSL,tempoiniSL,...
        NuovoZeroPT100,datainiPT100,tempoiniPT100,NuovoZeroWL,datainiWL,tempoiniWL,...
        NuovoZeroPE,datainiPE,tempoiniPE,margine,marginePiezo] = LastElab(...
        conn,date,time,IDcentralina,DTcatena,Wdevst,Wdevst_HR,NdatiMedia,Ndatidespike,NdatiMediaP,...
        NodoTiltLink,yesTL,rTL,NodoTiltLinkHD,yesHD,rHD,NodoTiltLinkHDVR,yesHDVR,rHDVR,...
        NodoTiltLinkHR,yesTLHR,rTLHR,NodoTiltLinkH,yesTLH,rTLH,NodoTiltLinkHRH,yesTLHRH,rTLHRH,...
        NodoPiezoLink,yesPL,NodoBaroLink,yesBL,Barometri,...
        NodoLoadLink,yesLL,NodoThermLink,yesThL,NodoRainLink,yesRL,rRL,...
        NodoSnowLink,yesSL,rSL,NodoSPPLink,yesSPP,NodoKlinoLink,yesKL,rKL,...
        NodoKlinoLinkHR,yesKLHR,rKLHR,NodoPT100Link,yesPT100,NodoInPlaceLink,yesIPL,rIPL,...
        NodoInPlaceLinkHR,yesIPLHR,rIPLHR,NodoWeirLink,yesWL,NodoPendulum,yesPE,FileName);
    
    % Sito Specificità
    if strcmp(IDcentralina,'ID0003') && strcmp(DTcatena,'DT0002')
        datainiBL = '2015-03-01';
        tempoiniBL = '00:00:01';
        datainiPL = '2015-03-01';
        tempoiniPL = '00:00:01';
    end
    
    if strcmp(IDcentralina,'ID0070') == 1 && strcmp(DTcatena,'DT0111 IPI') == 1 || ...
            strcmp(IDcentralina,'ID0071') == 1 && strcmp(DTcatena,'DT0112 IPI') == 1 || ...
            strcmp(IDcentralina,'ID0072') == 1 && strcmp(DTcatena,'DT0113 IPI') == 1 || ...
            strcmp(IDcentralina,'ID0073') == 1 && strcmp(DTcatena,'DT0114 IPI') == 1
        DTcatenaRif = DTcatena;
        if strcmp(DTcatena,'DT0111 IPI') == 1
            DTcatena = 'DT0111';
        elseif strcmp(DTcatena,'DT0112 IPI') == 1
            DTcatena = 'DT0112';
        elseif strcmp(DTcatena,'DT0113 IPI') == 1
            DTcatena = 'DT0113';
        elseif strcmp(DTcatena,'DT0114 IPI') == 1
            DTcatena = 'DT0114';
        end
        % Carico i dati dal database a seconda della catena
        [Batteria,Batteria_LoRa,DatiTiltLink,ErrTiltLink,datainiTL,yesTL,...
            ErrTiltLinkHD,ErrTiltLinkHDVR,...
            DatiTiltLinkH,ErrTiltLinkH,datainiTLH,yesTLH,DatiPiezoLink,ErrPiezoLink,yesPL,DatiBaroLink,ErrBaroLink,yesBL,...
            DatiTiltLinkHR,ErrTiltLinkHR,datainiTLHR,yesTLHR,DatiTiltLinkHRH,ErrTiltLinkHRH,datainiTLHRH,yesTLHRH,...
            DatiLoadLink,ErrLoadLink,yesLL,DatiThermLink,ErrThermLink,yesThL,DatiKlinoLink,ErrKlinoLink,yesKL,...
            DatiKlinoLinkHR,ErrKlinoLinkHR,yesKLHR,DatiRainLink,ErrRainLink,datainiRL,yesRL,...
            ErrSnowLink,DatiPT100Link,ErrPT100Link,yesPT100,DatiInPlaceLink,ErrInPlaceLink,datainiIPL,yesIPL,...
            DatiInPlaceLinkHR,ErrInPlaceLinkHR,datainiIPLHR,yesIPLHR,DatiWeirLink,DatiNTCWeirLink,ErrWeirLink,datainiWL,yesWL,...
            DatiPendulum,ErrPendulum,datainiPE,yesPE] = lettura_Hybrid(IDcentralina,DTcatena,DTcatenaRif,datainiTL,tempoiniTL,NodoTiltLink,NuovoZeroTL,...
            datainiTLH,tempoiniTLH,NodoTiltLinkH,NuovoZeroTLH,datainiPL,tempoiniPL,NodoPiezoLink,NuovoZeroPL,...
            datainiBL,tempoiniBL,NodoBaroLink,NuovoZeroBL,datainiTLHR,tempoiniTLHR,NodoTiltLinkHR,NuovoZeroTLHR,...
            datainiTLHRH,tempoiniTLHRH,NodoTiltLinkHRH,NuovoZeroTLHRH,datainiThL,tempoiniThL,NodoThermLink,NuovoZeroThL,...
            datainiLL,tempoiniLL,NodoLoadLink,NuovoZeroLL,datainiKL,tempoiniKL,NodoKlinoLink,NuovoZeroKL,...
            datainiKLHR,tempoiniKLHR,NodoKlinoLinkHR,NuovoZeroKLHR,datainiRL,tempoiniRL,NodoRainLink,NuovoZeroRL,...
            datainiPT100,tempoiniPT100,NodoPT100Link,NuovoZeroPT100,datainiIPL,tempoiniIPL,NodoInPlaceLink,NuovoZeroIPL,...
            datainiIPLHR,tempoiniIPLHR,NodoInPlaceLinkHR,NuovoZeroIPLHR,datainiWL,tempoiniWL,NodoWeirLink,NuovoZeroWL,...
            datainiPE,tempoiniPE,NodoPendulum,NuovoZeroPE,yesTL,yesTLH,yesTLHR,yesTLHRH,yesPL,yesBL,yesThL,yesKL,yesKLHR,yesRL,...
            yesPT100,yesLL,yesIPL,yesIPLHR,yesWL,yesPE,rTL,rTLH,rTLHR,rTLHRH,rPL,rBL,rThL,rKL,rKLHR,rRL,rPT100,rLL,rIPL,rIPLHR,...
            rWL,rPE,conn,Barometro,catena,date,time,FileName);
    else    
        % Carico i dati dal database a seconda della catena
        [Batteria,Batteria_LoRa,DatiTiltLink,ErrTiltLink,datainiTL,yesTL,...
            DatiTiltLinkHD,ErrTiltLinkHD,datainiHD,yesHD,DatiTiltLinkHDVR,ErrTiltLinkHDVR,datainiHDVR,yesHDVR,...
            DatiTiltLinkH,ErrTiltLinkH,datainiTLH,yesTLH,DatiPiezoLink,ErrPiezoLink,yesPL,DatiBaroLink,ErrBaroLink,yesBL,...
            DatiTiltLinkHR,ErrTiltLinkHR,datainiTLHR,yesTLHR,DatiTiltLinkHRH,ErrTiltLinkHRH,datainiTLHRH,yesTLHRH,...
            DatiLoadLink,ErrLoadLink,yesLL,DatiThermLink,ErrThermLink,yesThL,DatiKlinoLink,ErrKlinoLink,yesKL,...
            DatiKlinoLinkHR,ErrKlinoLinkHR,yesKLHR,DatiRainLink,ErrRainLink,datainiRL,yesRL,...
            DatiSPPLink,~,yesSPP,DatiSnowLink,ErrSnowLink,datainiSL,yesSL,...
            DatiPT100Link,ErrPT100Link,yesPT100,DatiInPlaceLink,ErrInPlaceLink,datainiIPL,yesIPL,...
            DatiInPlaceLinkHR,ErrInPlaceLinkHR,datainiIPLHR,yesIPLHR,DatiWeirLink,DatiNTCWeirLink,ErrWeirLink,datainiWL,yesWL,...
            DatiPendulum,ErrPendulum,~,yesPE] = lettura(IDcentralina,DTcatena,datainiTL,tempoiniTL,NodoTiltLink,NuovoZeroTL,...
            datainiHD,tempoiniHD,NodoTiltLinkHD,NuovoZeroHD,datainiHDVR,tempoiniHDVR,NodoTiltLinkHDVR,NuovoZeroHDVR,...
            datainiTLH,tempoiniTLH,NodoTiltLinkH,NuovoZeroTLH,...
            datainiPL,tempoiniPL,NodoPiezoLink,NuovoZeroPL,datainiBL,tempoiniBL,NodoBaroLink,NuovoZeroBL,...
            datainiTLHR,tempoiniTLHR,NodoTiltLinkHR,NuovoZeroTLHR,...
            datainiTLHRH,tempoiniTLHRH,NodoTiltLinkHRH,NuovoZeroTLHRH,datainiThL,tempoiniThL,NodoThermLink,NuovoZeroThL,...
            datainiLL,tempoiniLL,NodoLoadLink,NuovoZeroLL,datainiKL,tempoiniKL,NodoKlinoLink,NuovoZeroKL,...
            datainiKLHR,tempoiniKLHR,NodoKlinoLinkHR,NuovoZeroKLHR,datainiRL,tempoiniRL,NodoRainLink,NuovoZeroRL,...
            datainiSPP,tempoiniSPP,NuovoZeroSPP,NodoSPPLink,datainiPT100,tempoiniPT100,NodoPT100Link,NuovoZeroPT100,...
            datainiIPL,tempoiniIPL,NodoInPlaceLink,NuovoZeroIPL,datainiIPLHR,tempoiniIPLHR,NodoInPlaceLinkHR,NuovoZeroIPLHR,...
            datainiWL,tempoiniWL,NodoWeirLink,NuovoZeroWL,datainiPE,tempoiniPE,NodoPendulum,NuovoZeroPE,...
            datainiSL,tempoiniSL,NodoSnowLink,NuovoZeroSL,...
            yesTL,yesTLH,yesTLHR,yesTLHRH,yesPL,yesBL,yesThL,yesKL,yesKLHR,yesRL,yesSPP,yesSL,...
            yesPT100,yesLL,yesIPL,yesIPLHR,yesWL,yesPE,yesHD,yesHDVR,rTL,rTLH,rTLHR,rTLHRH,rPL,...
            rBL,rThL,rKL,rKLHR,rRL,rSPP,rSL,rPT100,rLL,rIPL,rIPLHR,rWL,rPE,rHD,rHDVR,conn,Barometro,catena,date,time,FileName);
    end
    
    if strcmp(IDcentralina,'ID0070') == 1 && strcmp(DTcatenaRif,'DT0111 IPI') == 1 || ...
            strcmp(IDcentralina,'ID0071') == 1 && strcmp(DTcatenaRif,'DT0112 IPI') == 1 || ...
            strcmp(IDcentralina,'ID0072') == 1 && strcmp(DTcatenaRif,'DT0113 IPI') == 1 || ...
            strcmp(IDcentralina,'ID0073') == 1 && strcmp(DTcatenaRif,'DT0114 IPI') == 1
        DTcatena = DTcatenaRif;
    end
    
    % Controllo incrociato fra la data iniziale definita da Lettura e la data
    % iniziale definita da LastElab
    [DatiElabTiltLink,DatiElabTiltLinkHR,DatiElabTiltLinkH,DatiElabTiltLinkHRH,...
        DatiElabRainLink,DatiElabInPlaceLink,DatiElabInPlaceLinkHR,DatiElabSnowLink] = checkdata(...
        yesTL,yesTLHR,yesTLH,yesTLHRH,yesRL,yesSL,yesIPL,yesIPLHR,...
        DatiElabTiltLink,DatiElabTiltLinkHR,DatiElabTiltLinkH,DatiElabTiltLinkHRH,...
        DatiElabRainLink,DatiElabInPlaceLink,DatiElabInPlaceLinkHR,DatiElabSnowLink,...
        datainiTL,tempoiniTL,datainiTLHR,tempoiniTLHR,datainiTLH,tempoiniTLH,...
        datainiTLHRH,tempoiniTLHRH,datainiRL,tempoiniRL,datainiIPL,tempoiniIPL,...
        datainiIPLHR,tempoiniIPLHR,datainiSL,tempoiniSL,rTL,rTLHR,rTLH,rTLHRH,rRL,rSL,rIPL,rIPLHR,...
        NodoTiltLink,NodoTiltLinkHR,NodoTiltLinkH,NodoTiltLinkHRH,...
        NodoRainLink,NodoInPlaceLink,NodoInPlaceLinkHR,NodoSnowLink,...
        NuovoZeroTL,NuovoZeroTLHR,NuovoZeroTLH,NuovoZeroTLHRH,NuovoZeroRL,NuovoZeroSL,...
        NuovoZeroIPL,NuovoZeroIPLHR,IDcentralina,DTcatena,conn,FileName);
    
    if strcmp(IDcentralina,'ID0070') == 1 && strcmp(DTcatenaRif,'DT0111 IPI') == 1 || ...
            strcmp(IDcentralina,'ID0071') == 1 && strcmp(DTcatenaRif,'DT0112 IPI') == 1 || ...
            strcmp(IDcentralina,'ID0072') == 1 && strcmp(DTcatenaRif,'DT0113 IPI') == 1 || ...
            strcmp(IDcentralina,'ID0073') == 1 && strcmp(DTcatenaRif,'DT0114 IPI') == 1
        if strcmp(DTcatenaRif,'DT0111 IPI') == 1
            DTcatena = 'DT0111';
        elseif strcmp(DTcatenaRif,'DT0112 IPI') == 1
            DTcatena = 'DT0112';
        elseif strcmp(DTcatenaRif,'DT0113 IPI') == 1
            DTcatena = 'DT0113';
        elseif strcmp(DTcatenaRif,'DT0114 IPI') == 1
            DTcatena = 'DT0114';
        end
    end
    
    %% Parte di definizione dati, conversione in unità fisiche e calcolo medie
    
    % ---Tilt Link V---
    if yesTL == 1
        [TimeTL,accTL,magTL,tempTL,ErrTiltLink] = defDatiTL(DatiTiltLink,...
            ErrTiltLink,Ndatidespike,rTL,MEMS,conn,catena,IDcentralina,DTcatena,Unit,FileName);
        % Conversione dei dati grezzi
        [accTL,magTL,ris_acc,ris_mag,tempTL,ErrTiltLink] = conv_grezziTL(rTL,accTL,magTL,...
            tempTL,DCalTLTot,tolleranzaAcc,MEMS,ErrTiltLink,IDcentralina,DTcatena,FileName);
        % Calcolo medie
        [ACCdef_TL,MAGdef_TL,ARRAYdateTL,ACCdefRis_TL,~,TempDef_TL] = MediaDati_TL(...
            accTL,magTL,TimeTL,ris_acc,ris_mag,tempTL,tolleranzaAcc,tolleranzaMag,...
            NdatiMedia,FileName);
    else
        ARRAYdateTL = [];
        text = 'There are not data to convert for Tilt Link V';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiTiltLink
    clear TimeTL
    clear accTL
    clear magTL
    clear tempTL
    clear DCalTLTot
    clear ris_acc
    clear ris_mag
    
    % ---In Place Link---
    if yesIPL == 1
        [TimeIPL,accIPL,magIPL,tempIPL,ErrInPlaceLink] = defDatiIPL(DatiInPlaceLink,...
            ErrInPlaceLink,NodoInPlaceLink,Ndatidespike,rIPL,MEMS,Unit,FileName);
        % Conversione dei dati grezzi
        [accIPL,magIPL,ris_acc_IPL,ris_mag_IPL,tempIPL,ErrInPlaceLink] = conv_grezziIPL(IDcentralina,rIPL,...
            accIPL,magIPL,tempIPL,DCalIPLTot,tolleranzaAcc,ErrInPlaceLink,NodoInPlaceLink,MEMS,FileName);
        % Calcolo medie
        [ACCdef_IPL,MAGdef_IPL,ARRAYdateIPL,ACCdefRis_IPL,~,TempDef_IPL]...
            = MediaDati_IPL(accIPL,magIPL,TimeIPL,ris_acc_IPL,ris_mag_IPL,tempIPL,tolleranzaAcc,...
            tolleranzaMag,NdatiMedia,NodoInPlaceLink,MEMS,FileName);
    else
        ARRAYdateIPL = [];
        text = 'There are not data to convert for In Place Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiInPlaceLink
    clear TimeIPL
    clear accIPL
    clear magIPL
    clear tempIPL
    clear DCalIPLTot
    clear ris_acc_IPL
    clear ris_mag_IPL
    
    % ---Tilt Link HD---
    if yesHD == 1
        [TimeHD,accHD,magHD,tempHD,ErrTiltLinkHD] = defDatiHD(DatiTiltLinkHD,...
            ErrTiltLinkHD,NodoTiltLinkHD,Ndatidespike,rHD,Unit,FileName);
        % Conversione dei dati grezzi
        [accHD,angHD,magHD,ris_acc_HD,ris_mag_HD,tempHD,ErrTiltLinkHD] = conv_grezziHD(rHD,accHD,magHD,...
            tempHD,DCalHDTot,tolleranzaAcc,NodoTiltLinkHD,ErrTiltLinkHD,IDcentralina,DTcatena,FileName);
        % Calcolo medie
        [ACCdef_HD,ANGdef_HD,MAGdef_HD,ARRAYdateHD,ACCdefRisHD,MAGdefRisHD] = MediaDati_HD(...
            accHD,angHD,magHD,TimeHD,ris_acc_HD,ris_mag_HD,tolleranzaAcc,...
            tolleranzaMag,NdatiMedia,FileName);
    else
        ARRAYdateHD = [];
        ACCdefRisHD = [];
        MAGdefRisHD = [];
        text = 'There are not data to convert for Tilt Link HD VR';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiTiltLinkHD
    clear TimeHD
    clear accHD
    clear angHD
    clear magHD
    clear DCalHDTot
    clear ris_acc_HD
    clear ris_mag_HD
    
    % ---Tilt Link HD VR---
    if yesHDVR == 1
        [TimeHDVR,accHDVR,magHDVR,tempHDVR,ErrTiltLinkHDVR] = defDatiHDVR(DatiTiltLinkHDVR,...
            ErrTiltLinkHDVR,NodoTiltLinkHDVR,Ndatidespike,rHDVR,Unit,FileName);
        % Conversione dei dati grezzi
        [accHDVR,magHDVR,ris_acc_HDVR,ris_mag_HDVR,tempHDVR,ErrTiltLinkHDVR] = ...
            conv_grezziHDVR(rHDVR,accHDVR,magHDVR,tempHDVR,DCalHDVRTot,tolleranzaAcc,...
            NodoTiltLinkHDVR,ErrTiltLinkHDVR,IDcentralina,DTcatena,FileName);
        % Calcolo medie
        [ACCdef_HDVR,MAGdef_HDVR,ARRAYdateHDVR,ACCdefRisHDVR,MAGdefRisHDVR] = MediaDati_HDVR(...
            accHDVR,magHDVR,TimeHDVR,ris_acc_HDVR,ris_mag_HDVR,tolleranzaAcc,...
            tolleranzaMag,NdatiMedia,FileName);
    else
        ARRAYdateHDVR = [];
        ACCdefRisHDVR = [];
        MAGdefRisHDVR = [];
        text = 'There are not data to convert for Tilt Link HD VR';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiTiltLinkHDVR
    clear TimeHDVR
    clear accHDVR
    clear magHDVR
    clear DCalHDVRTot
    clear ris_acc_HDVR
    clear ris_mag_HDVR
    
    % ---Tilt Link H---
    if yesTLH == 1
        % 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,tolleranzaAcc,MEMS,ErrTiltLinkH,FileName);
        % Calcolo medie
        [ACCdef_TLH,ARRAYdateTLH,ACCdefRis_TLH,TempDef_TLH] = MediaDati_TLH...
            (accTLH,TimeTLH,ris_acc_TLH,tempTLH,tolleranzaAcc,NdatiMedia,FileName);
    else
        ARRAYdateTLH = [];
        text = 'There are not data to convert for Tilt Link H';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiTiltLinkH
    clear TimeTLH
    clear accTLH
    clear tempTLH
    clear DCalTLHTot
    clear ris_acc_TLH
    
    % ---Tilt Link HR V---
    if yesTLHR == 1 % attiva l'elaborazione dei Tilt Link HR
        [TimeTLHR,angTLHR,tempTLHR,ErrTiltLinkHR] = defDatiTLHR(...
            DatiTiltLinkHR,ErrTiltLinkHR,Ndatidespike,MEMS,rTLHR,NodoTiltLinkHR,...
            NuovoZeroTLHR,IDcentralina,DTcatena,Unit,FileName);
        % Conversione dei dati grezzi
        [angTLHR,tempTLHR] = conv_grezziTLHR(angTLHR,tempTLHR,DCalTLHRTot,...
            rTLHR,FileName);
        % Calcolo medie
        [ANGdef_TLHR,ARRAYdateTLHR,TempDef_TLHR] = MediaDati_TLHR(angTLHR,...
            TimeTLHR,tempTLHR,NdatiMedia,FileName);
    else
        ARRAYdateTLHR = [];
        text = 'There are not data to convert for Tilt Link HR V';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiTiltLinkHR
    clear TimeTLHR
    clear angTLHR
    clear tempTLHR
    clear DCalTLHRTot
    clear tempTLHR
    
    % ---In Place Link HR---
    if yesIPLHR == 1
        [TimeIPLHR,angIPLHR,tempIPLHR,ErrInPlaceLinkHR] = defDatiIPLHR(...
            DatiInPlaceLinkHR,ErrInPlaceLinkHR,Ndatidespike,rIPLHR,NodoInPlaceLinkHR,...
            NuovoZeroIPLHR,IDcentralina,DTcatena,Unit,FileName);
        % Conversione dei dati grezzi
        [angIPLHR,tempIPLHR] = conv_grezziIPLHR(angIPLHR,tempIPLHR,DCalIPLHRTot,...
            NodoInPlaceLinkHR,rIPLHR,FileName);
        % Calcolo medie
        [ANGdef_IPLHR,ARRAYdateIPLHR,TempDef_IPLHR] = MediaDati_IPLHR(angIPLHR,...
            TimeIPLHR,tempIPLHR,NdatiMedia,FileName);
    else
        ARRAYdateIPLHR = [];
        text = 'There are not data to convert for In Place Link HR';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiInPlaceLinkHR
    clear TimeIPLHR
    clear angIPLHR
    clear tempIPLHR
    clear DCalIPLHRTot
    clear tempIPLHR
    
    % ---Tilt Link HR H---
    if yesTLHRH == 1 % attiva l'elaborazione dei Tilt Link HR H
        [TimeTLHRH,angTLHRH,tempTLHRH,ErrTiltLinkHRH] = defDatiTLHRH(...
            DatiTiltLinkHRH,ErrTiltLinkHRH,Ndatidespike,MEMS,rTLHRH,NodoTiltLinkHRH,...
            NuovoZeroTLHRH,IDcentralina,DTcatena,FileName);
        % Conversione dei dati grezzi
        [angTLHRH,tempTLHRH] = conv_grezziTLHRH(angTLHRH,tempTLHRH,DCalTLHRHTot,...
            rTLHRH,FileName);
        % Calcolo medie
        [ANGdef_TLHRH,ARRAYdateTLHRH,TempDef_TLHRH] = MediaDati_TLHRH(angTLHRH,...
            TimeTLHRH,tempTLHRH,NdatiMedia,FileName);
    else
        ARRAYdateTLHRH = [];
        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);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiTiltLinkHRH
    clear TimeTLHRH
    clear angTLHRH
    clear tempTLHRH
    clear DCalTLHRHTot
    clear tempTLHRH
    
    % ---Piezo Link---
    if  yesPL == 1 % attiva l'elaborazione dei Piezo Link
        [TimePL,ADCPiez,tempPiez,ErrPiezoLink] = defDatiPL(DatiPiezoLink,ErrPiezoLink,rPL,FileName);
        % Conversione dei dati grezzi
        [DatiPiez,DatiPiezT,~] = conv_grezziPL(rPL,ADCPiez,tempPiez,DCalPLTot,...
            NodoPiezoLink,NuovoZeroPL,IDcentralina,DTcatena,FileName);
        % Calcolo medie
        [PiezDef,PiezDefT,ARRAYdatePL] = MediaDati_PL(DatiPiez,DatiPiezT,TimePL,NdatiMediaP,FileName);
    else
        ARRAYdatePL = [];
        text = 'There are not data to convert for Piezo Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiPiezoLink
    clear TimePL
    clear ADCPiez
    clear tempPiez
    clear DatiPiez
    clear DatiPiezT
    
    % ---Baro Link---
    % Attivazione/disattivazione elaborazione e definizione dati
    if strcmp(NodoPiezoLink(1,4),'VW kPa') == 1 || strcmp(NodoPiezoLink(1,4),'VW kg/cm2') == 1
        BaroDef = [];
        ARRAYdateBL = [];
        text = 'There are not data to convert for Baro Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    elseif  yesBL == 1 || yesPL == 1  % attiva l'elaborazione dei Baro Link
        [TimeBL,ADCBaro,tempBaro,ErrBaroLink] = defDatiBL(DatiBaroLink,ErrBaroLink,rBL,FileName);
        % Conversione dei dati grezzi
        if yesBL == 0 % Recupero il barometro corretto
            [rB,~] = size(Barometri);
            for b = 1:rB
                if strcmp(Barometri(b,1),Barometro(1,1)) == 1
                    DCalBLTot = DCalBLTot(b,:);
                    rBL = 1;
                    break
                end
            end
        end
        [DatiBaro,DatiBaroT] = conv_grezziBL(rBL,ADCBaro,tempBaro,DCalBLTot,FileName);
        % Calcolo medie
        [BaroDef,BaroDefT,ARRAYdateBL] = MediaDati_BL(DatiBaro,DatiBaroT,TimeBL,NdatiMediaP,FileName);
    else
        BaroDef = [];
        ARRAYdateBL = [];
        text = 'There are not data to convert for Baro Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiBaroLink
    clear TimeBL
    clear ADCBaro
    clear tempBaro
    clear DCalBLTot
    clear DatiBaro
    clear DatiBaroT
    
    % ---Klino Link---
    if yesKL == 1 % attiva l'elaborazione dei Klino Link
        [TimeKL,ang_KL,temp_KL,ErrKlinoLink] = defDatiKL(DatiKlinoLink,...
            ErrKlinoLink,NodoKlinoLink,MEMS,Ndatidespike,rKL,FileName);
        % Conversione dei dati grezzi
        [ang_KL,temp_KL,ris_acc,ErrKlinoLink] = conv_grezziKL(ang_KL,temp_KL,...
            rKL,DCalKLTot,IDcentralina,DTcatena,NodoKlinoLink,ErrKlinoLink,FileName);
        % Medie
        [ANGdefKL,TdefKL,ARRAYdateKL] = MediaDati_KL(ang_KL,temp_KL,TimeKL,NdatiMedia,FileName);
    else
        ANGdefKL = [];
        TdefKL = [];
        ARRAYdateKL = [];
        text = 'There are not data to convert for Klino Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiKlinoLink
    clear TimeKL
    clear ang_KL
    clear temp_KL
    clear DCalKLTot
    clear ANGoutKL
    clear TEMPoutKL
    clear TimeRcKL
    
    % ---Klino Link HR---
    if yesKLHR == 1 % attiva l'elaborazione dei Klino Link HR
        [TimeKLHR,ang_KLHR,temp_KLHR,ErrKlinoLinkHR] = defDatiKLHR(...
            DatiKlinoLinkHR,ErrKlinoLinkHR,Ndatidespike,rKLHR,NodoKlinoLinkHR,...
            NuovoZeroKLHR,IDcentralina,DTcatena,FileName);
        % Conversione dei dati grezzi
        [ang_KLHR,temp_KLHR] = conv_grezziKLHR(ang_KLHR,temp_KLHR,rKLHR,...
            DCalKLHRTot,FileName);
        % Calcolo medie
        [ANGdefKLHR,TdefKLHR,ARRAYdateKLHR] = MediaDati_KLHR(ang_KLHR,...
            temp_KLHR,TimeKLHR,NdatiMedia,FileName);
    else
        ANGdefKLHR = [];
        TdefKLHR = [];
        ARRAYdateKLHR = [];
        text = 'There are not data to convert for Klino Link HR';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiKlinoLinkHR
    clear TimeKLHR
    clear ang_KLHR
    clear temp_KLHR
    clear DCalKLHRTot
    
    % ---Load Link---
    if  yesLL == 1 % attiva l'elaborazione dei Load Link
        [TimeLL,ADCLoad,ErrLoadLink] = defDatiLL(DatiLoadLink,ErrLoadLink,Ndatidespike,rLL,FileName);
        % Conversione dei dati grezzi
        DatiLoad = conv_grezziLL(ADCLoad,DCalLLTot,NodoLoadLink,FileName);
        % Calcolo medie
        [LoadDef,ARRAYdateLL] = MediaDati_LL(DatiLoad,TimeLL,NdatiMedia,FileName);
    else
        ARRAYdateLL = [];
        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 che non uso
    clear DatiLoadLink
    clear TimeLL
    clear ADCLoad
    clear DCalLLTot
    clear DatiLoad
    clear TimeLL
    
    % ---Rain Link---
    if yesRL == 1 % attiva l'elaborazione dei Klino Link
        [TimeRL,val_RL,ErrRainLink] = defDatiRL(DatiRainLink,ErrRainLink,FileName);
        % Conversione dei dati grezzi
        val_RL = conv_grezziRL(val_RL,rRL,DCalRLTot,NodoRainLink,FileName);
        % Calcolo media
        [Dati_RL,ARRAYdateRL] = MediaDati_RL(val_RL,TimeRL,FileName);
    else
        ARRAYdateRL = [];
        Rain = [];
        RainCum = [];
        text = 'There are not data to convert for Rain Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiRainLink
    clear TimeRL
    clear val_RL
    clear DCalRLTot
    
    % ---Therm Link---
    if yesThL == 1
        [TimeThL,val_ThL,ErrThermLink] = defDatiThL(DatiThermLink,ErrThermLink,rThL,FileName);
        % Conversione dei dati grezzi
        val_ThL = conv_grezziThL(val_ThL,rThL,DCalThLTot,FileName);
        % Calcolo medie
        [Dati_ThL,ARRAYdateThL] = MediaDati_ThL(val_ThL,TimeThL,NdatiMedia,FileName);
    else
        ARRAYdateThL = [];
        Dati_ThL = [];
        text = 'There are not data to convert for Therm Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiThermLink
    clear TimeThL
    clear val_ThL
    clear DCalThLTot
    
    % ---PT100 Link---
    if yesPT100 == 1
        [TimePT100,val_PT100,ErrPT100Link] = defDatiPT100(DatiPT100Link,ErrPT100Link,FileName);
        % Conversione dei dati grezzi
        val_PT100 = conv_grezziPT100(val_PT100,rPT100,DCalPT100Tot,FileName);
        % Calcolo medie
        [Dati_PT100,ARRAYdatePT100] = MediaDati_PT100(val_PT100,TimePT100,FileName);
    else
        ARRAYdatePT100 = [];
        Dati_PT100 = [];
        text = 'There are not data to convert for PT100 Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    % Pulisco le variabili che non uso
    clear DatiPT100Link
    clear TimePT100
    clear val_PT100
    clear DCalPT100Tot
    
    % ---Weir Link---
    if yesWL == 1
        [TimeWL,lev_WL,temp_WL,ErrWeirLink] = defDatiWL(DatiWeirLink,...
            DatiNTCWeirLink,ErrWeirLink,FileName);
        % Conversione dei dati grezzi
        [lev_WL,TdefWL,ErrWeirLink] = conv_grezziWL(lev_WL,temp_WL,rWL,...
            DCalWLTot,Tmax,Tmin,NodoWeirLink,ErrWeirLink,datainiWL,TimeWL,...
            IDcentralina,DTcatena,FileName);
        % Media
        [Dati_WL,ARRAYdateWL] = MediaDati_WL(lev_WL,TimeWL,FileName);
    else
        ARRAYdateWL = [];
        Dati_WL = [];
        TdefWL = [];
        text = 'There are not data to convert for Weir Link';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    
    % ---Pendulum---
    if yesPE == 1
        [TimePE,Def_PE,ErrPendulum] = defDatiPE(DatiPendulum,ErrPendulum,FileName);
        % Conversione dei dati grezzi
        Def_PE = conv_grezziPE(Def_PE,rPE,DCalPETot,NodoPendulum,FileName);
        % Media
        [Dati_PE,ARRAYdatePE] = MediaDati_PE(Def_PE,TimePE,NdatiMedia,FileName);
    else
        ARRAYdatePE = [];
        Dati_PE = [];
        text = 'There are not data to convert for Pendulum';
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        fprintf(fileID,fmt,text);
        fclose(fileID);
        clear text
    end
    
    %% Parte di elaborazione
    
    % --- Analisi Tilt Link V ---
    if yesTL == 1
        if struct == 1 % Vertical Array Structure
            % ......
        elseif triax == 1 % Analisi ai Quaternioni
            [X,Y,Z,Xlocal,Ylocal,Zlocal,HShift,HShift_local,AlfaX,AlfaY,Azimuth,...
                Speed_local,Speed,Acceleration_local,Acceleration,TempDef_TL,ARRAYdateTL,...
                ErrTiltLink,MAGdef_TL] = triax_TL(IDcentralina,DTcatena,rTL,ACCdef_TL,...
                ACCdefRis_TL,MAGdef_TL,TempDef_TL,SpeTL,PsTL,NodoTiltLink,tolleranzaAcc,...
                tolleranzaMag,DatiElabTiltLink,segnoNS,segnoEO,MEMS,Ndevst,Wdevst,ARRAYdateTL,...
                Tmax,Tmin,NuovoZeroTL,NdatiMedia,Ndatidespike,ErrTiltLink,margine,datainiTL,FileName);
        elseif biax == 1 % Analisi semplificata 2D
            [X,Y,Z,Xlocal,Ylocal,Zlocal,HShift,HShift_local,AlfaX,AlfaY,Azimuth,...
                Speed_local,Speed,Acceleration_local,Acceleration,TempDef_TL,...
                ARRAYdateTL,ErrTiltLink,MAGdef_TL] = biax_TL(IDcentralina,DTcatena,rTL,...
                ACCdef_TL,ACCdefRis_TL,MAGdef_TL,TempDef_TL,SpeTL,PsTL,...
                NodoTiltLink,tolleranzaAcc,tolleranzaMag,DatiElabTiltLink,segnoNS,segnoEO,...
                Ndevst,Wdevst,ARRAYdateTL,Tmax,Tmin,NuovoZeroTL,NdatiMedia,Ndatidespike,...
                ErrTiltLink,margine,date,datainiTL,FileName);
        end
    else
        [X,Y,Z,Xlocal,Ylocal,Zlocal,HShift,HShift_local,Azimuth,TempDef_TL,Speed_local,...
            Speed,Acceleration_local,Acceleration,AlfaX,AlfaY] = matrici_vuote_TL(FileName);
    end
    clear ACCdefRis_TL
    clear SpeTL
    clear PsTL
    clear DatiElabTiltLink
    clear NuovoZeroTL
    % fine elaborazione Tilt Link V
    
    % --- Analisi Tilt Link H ---
    if yesTLH == 1
        [Y_TLH,Z_TLH,Xlocal_TLH,Ylocal_TLH,Zlocal_TLH,AlfaX_TLH,AlfaY_TLH,TempDef_TLH,...
            Speed_TLH,Speed_local_TLH,Acceleration_TLH,Acceleration_local_TLH,ARRAYdateTLH,...
            ErrTiltLinkH] = biax_TLH(IDcentralina,DTcatena,rTLH,ACCdef_TLH,ACCdefRis_TLH,...
            TempDef_TLH,SpeTLH,PsTLH,tolleranzaAcc,DatiElabTiltLinkH,Ndevst,Wdevst,ARRAYdateTLH,...
            NuovoZeroTLH,Tmax,Tmin,NodoTiltLinkH,NdatiMedia,Ndatidespike,Traversine,ErrTiltLinkH,...
            margine,FileName);
    else
        [Y_TLH,Z_TLH,Xlocal_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 ACCdef_TLH
    clear ACCdefRis_TLH
    clear SpeTLH
    clear PsTLH
    clear Ndevst_TLH
    clear Wdevst_TLH
    clear NuovoZeroTLH
    % fine elaborazione Tilt Link H
    
    % --- Analisi In Place Link ---
    if yesIPL == 1
        if triax == 1
            [X_IPL,Y_IPL,Z_IPL,Xlocal_IPL,Ylocal_IPL,Zlocal_IPL,HShift_IPL,...
                HShift_local_IPL,AlfaX_IPL,AlfaY_IPL,Azimuth_IPL,Speed_local_IPL,...
                Speed_IPL,Acceleration_local_IPL,Acceleration_IPL,TempDef_IPL,...
                ARRAYdateIPL,ErrInPlaceLink,MAGdef_IPL] = triax_IPL(IDcentralina,...
                DTcatena,rIPL,ACCdef_IPL,ACCdefRis_IPL,MAGdef_IPL,TempDef_IPL,SpeIPL,PsIPL,...
                NodoInPlaceLink,tolleranzaAcc,tolleranzaMag,DatiElabInPlaceLink,...
                segnoNS,segnoEO,MEMS,Ndevst,Wdevst,ARRAYdateIPL,NuovoZeroIPL,Tmax,Tmin,...
                NdatiMedia,Ndatidespike,ErrInPlaceLink,Corr_Azimuth,margine,datainiIPL,FileName);
        elseif biax == 1
            [X_IPL,Y_IPL,Z_IPL,Xlocal_IPL,Ylocal_IPL,Zlocal_IPL,HShift_IPL,HShift_local_IPL,...
                AlfaX_IPL,AlfaY_IPL,Azimuth_IPL,Speed_local_IPL,Speed_IPL,Acceleration_local_IPL,...
                Acceleration_IPL,TempDef_IPL,ARRAYdateIPL,ErrInPlaceLink] = biax_IPL(IDcentralina,...
                DTcatena,rIPL,ACCdef_IPL,TempDef_IPL,SpeIPL,PsIPL,NodoInPlaceLink,tolleranzaAcc,...
                DatiElabInPlaceLink,segnoNS,segnoEO,MEMS,Ndevst,Wdevst,ARRAYdateIPL,NuovoZeroIPL,...
                Tmax,Tmin,NdatiMedia,Ndatidespike,ErrInPlaceLink,Corr_Azimuth,Inverti,margine,datainiIPL,FileName);
        end
    else
        [X_IPL,Y_IPL,Z_IPL,Xlocal_IPL,Ylocal_IPL,Zlocal_IPL,HShift_IPL,...
            HShift_local_IPL,Azimuth_IPL,TempDef_IPL,Speed_local_IPL,Speed_IPL,...
            Acceleration_local_IPL,Acceleration_IPL,AlfaX_IPL,AlfaY_IPL,ACCdef_IPL,...
            MAGdef_IPL] = matrici_vuote_IPL(FileName);
    end
    clear segnoNS
    clear segnoEO
    clear ACCdefRis_IPL
    clear SpeIPL
    clear PsIPL
    clear DatiElabInPlaceLink
    clear NuovoZeroIPL
    % fine elaborazione In Place Link
    
%   --- Analisi Tilt Link HD ---
    if yesHD == 1
        if cell2mat(NodoTiltLinkHD(1,5)) == 1 % Analisi ai Quaternioni - USO i Magnetometri
            [X_HD,Y_HD,Z_HD,Xlocal_HD,Ylocal_HD,Zlocal_HD,HShift_HD,HShift_local_HD,...
                AlfaX_HD,AlfaY_HD,Azimuth_HD,Speed_local_HD,Speed_HD,Acceleration_local_HD,...
                Acceleration_HD,tempHD,ARRAYdateHD,ErrTiltLinkHD] = triax_HD(rHD,...
                ACCdef_HD,ACCdefRisHD,MAGdef_HD,tempHD,SpeHD,PsHD,NodoTiltLinkHD,tolleranzaAcc,...
                tolleranzaMag,DatiElabTiltLinkHD,Ndevst,Wdevst,ARRAYdateHD,Tmax,Tmin,NuovoZeroHD,...
                NdatiMedia,Ndatidespike,ErrTiltLinkHD,margine,datainiHD,MEMS,IDcentralina,DTcatena,FileName);
        else % Analisi semplificata 2D
            [X_HD,Y_HD,Z_HD,Xlocal_HD,Ylocal_HD,Zlocal_HD,HShift_HD,HShift_local_HD,...
                AlfaX_HD,AlfaY_HD,Azimuth_HD,Speed_local_HD,Speed_HD,Acceleration_local_HD,...
                Acceleration_HD,tempHD,ARRAYdateHD,ErrTiltLinkHD] = biax_HD(rHD,...
                ACCdef_HD,ANGdef_HD,ACCdefRisHD,tempHD,SpeHD,PsHD,NodoTiltLinkHD,tolleranzaAcc,...
                DatiElabTiltLinkHD,Ndevst,Wdevst,ARRAYdateHD,Tmax,Tmin,NuovoZeroHD,...
                NdatiMedia,Ndatidespike,ErrTiltLinkHD,margine,Corr_Azimuth,datainiHD,IDcentralina,DTcatena,FileName);
        end
    else
        [X_HD,Y_HD,Z_HD,Xlocal_HD,Ylocal_HD,Zlocal_HD,HShift_HD,HShift_local_HD,...
            Azimuth_HD,tempHD,Speed_local_HD,Speed_HD,Acceleration_local_HD,Acceleration_HD,...
            AlfaX_HD,AlfaY_HD] = matrici_vuote_HD(FileName);
    end
    clear SpeHD
    clear PsHD
    clear DatiElabTiltLinkHD
    clear NuovoZeroHD
%     fine elaborazione Tilt Link HD

%   --- Analisi Tilt Link HD VR ---
    if yesHDVR == 1
        if cell2mat(NodoTiltLinkHDVR(1,5)) == 1 % Analisi ai Quaternioni - USO i Magnetometri
            [X_HDVR,Y_HDVR,Z_HDVR,Xlocal_HDVR,Ylocal_HDVR,Zlocal_HDVR,HShift_HDVR,HShift_local_HDVR,...
                AlfaX_HDVR,AlfaY_HDVR,Azimuth_HDVR,Speed_local_HDVR,Speed_HDVR,Acceleration_local_HDVR,...
                Acceleration_HDVR,tempHDVR,ARRAYdateHDVR,ErrTiltLinkHDVR] = triax_HDVR(rHDVR,...
                ACCdef_HDVR,ACCdefRisHDVR,MAGdef_HDVR,tempHDVR,SpeHDVR,PsHDVR,NodoTiltLinkHDVR,tolleranzaAcc,...
                tolleranzaMag,DatiElabTiltLinkHDVR,Ndevst,Wdevst,ARRAYdateHDVR,Tmax,Tmin,NuovoZeroHDVR,...
                NdatiMedia,Ndatidespike,ErrTiltLinkHDVR,margine,datainiHDVR,MEMS,IDcentralina,DTcatena,FileName);
        elseif biax == 1 % Analisi semplificata 2D
            [X_HDVR,Y_HDVR,Z_HDVR,Xlocal_HDVR,Ylocal_HDVR,Zlocal_HDVR,HShift_HDVR,HShift_local_HDVR,...
                AlfaX_HDVR,AlfaY_HDVR,Azimuth_HDVR,Speed_local_HDVR,Speed_HDVR,Acceleration_local_HDVR,...
                Acceleration_HDVR,tempHDVR,ARRAYdateHDVR,ErrTiltLinkHDVR] = biax_HDVR(rHDVR,...
                ACCdef_HDVR,ANGdef_HDVR,ACCdefRisHDVR,tempHDVR,SpeHDVR,PsHDVR,NodoTiltLinkHDVR,tolleranzaAcc,...
                DatiElabTiltLinkHDVR,Ndevst,Wdevst,ARRAYdateHDVR,Tmax,Tmin,NuovoZeroHDVR,...
                NdatiMedia,Ndatidespike,ErrTiltLinkHDVR,margine,Corr_Azimuth,datainiHDVR,IDcentralina,DTcatena,FileName);
        end
    else
        [X_HDVR,Y_HDVR,Z_HDVR,Xlocal_HDVR,Ylocal_HDVR,Zlocal_HDVR,HShift_HDVR,HShift_local_HDVR,...
            Azimuth_HDVR,tempHDVR,Speed_local_HDVR,Speed_HDVR,Acceleration_local_HDVR,Acceleration_HDVR,...
            AlfaX_HDVR,AlfaY_HDVR] = matrici_vuote_HDVR(FileName);
    end
    clear SpeHDVR
    clear PsHDVR
    clear DatiElabTiltLinkHDVR
    clear NuovoZeroHDVR
    clear Ndevst
    clear Wdevst
%     fine elaborazione Tilt Link HD VR
    
    % --- Analisi Tilt Link HR V ---
    if  yesTLHR == 1
        [X_HR,Y_HR,Z_HR,Xlocal_HR,Ylocal_HR,Zlocal_HR,HShift_HR,HShift_local_HR,...
            AlfaX_HR,AlfaY_HR,Azimuth_HR,Speed_local_HR,Speed_HR,Acceleration_local_HR,...
            Acceleration_HR,TempDef_TLHR,ARRAYdateTLHR,ErrTiltLinkHR] = biax_TLHR(IDcentralina,...
            DTcatena,rTLHR,rTL,ANGdef_TLHR,ACCdef_TL,MAGdef_TL,TempDef_TLHR,SpeTLHR,...
            PsTLHR,yesTL,yesTLHR3D,NodoTiltLinkHR,NodoTiltLink,NodoTiltLinkHR3D,...
            DatiElabTiltLinkHR,segnoNS_HR,segnoEO_HR,Ndevst_HR,Wdevst_HR,ARRAYdateTLHR,...
            NuovoZeroTLHR,NdatiMedia,Ndatidespike,allineato,ErrTiltLinkHR,Tmax,Tmin,datainiTLHR,...
            margine,date,FileName);
    else
        [X_HR,Y_HR,Z_HR,Xlocal_HR,Ylocal_HR,Zlocal_HR,AlfaX_HR,AlfaY_HR,...
            TempDef_TLHR,HShift_HR,HShift_local_HR,Azimuth_HR,Speed_HR,Speed_local_HR,...
            Acceleration_HR,Acceleration_local_HR] = matrici_vuote_TLHR(FileName);
    end
    % Faccio Pulizia di Variabili
    clear ANGdefTLHR
    clear ACCdef
    clear MAGdef
    clear SpeTLHR
    clear PsTLHR
    clear DatiElabTiltLinkHR
    clear DatiElabTiltLink
    clear NuovoZeroTLHR
    clear ACCdef_TL
    clear MAGdef_TL
    % fine elaborazione Tilt Link HR V
    
    % --- Analisi 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,ARRAYdateTLHRH,ErrTiltLinkHRH] = biax_TLHRH(...
            IDcentralina,DTcatena,NodoTiltLinkHRH,rTLHRH,ANGdef_TLHRH,TempDef_TLHRH,SpeTLHRH,PsTLHRH,...
            DatiElabTiltLinkHRH,Ndevst_HR,Wdevst_HR,ARRAYdateTLHRH,NuovoZeroTLHRH,NdatiMedia,...
            Ndatidespike,ErrTiltLinkHRH,Tmax,Tmin,datainiTLHRH,margine,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
    % Faccio Pulizia di Variabili
    % Faccio Pulizia di Variabili
    clear ANGdef_TLHRH
    clear SpeTLHRH
    clear PsTLHRH
    clear DatiElabTiltLinkHRH
    clear Ndevst_TLHRH
    clear Wdevst_TLHRH
    clear NuovoZeroTLHRH
    % fine elaborazione Tilt Link HR H
    
    % --- Analisi In Place Link HR ---
    if yesIPLHR == 1
        [X_IPLHR,Y_IPLHR,Z_IPLHR,Xlocal_IPLHR,Ylocal_IPLHR,Zlocal_IPLHR,...
            HShift_IPLHR,HShift_local_IPLHR,AlfaX_IPLHR,AlfaY_IPLHR,Azimuth_IPLHR,...
            Speed_local_IPLHR,Speed_IPLHR,Acceleration_local_IPLHR,Acceleration_IPLHR,...
            TempDef_IPLHR,ARRAYdateIPLHR,ErrInPlaceLinkHR] = biax_IPLHR(IDcentralina,...
            DTcatena,rIPLHR,rIPL,ANGdef_IPLHR,ACCdef_IPL,MAGdef_IPL,TempDef_IPLHR,...
            SpeIPLHR,PsIPLHR,yesIPL,yesIPLHR3D,NodoInPlaceLinkHR,NodoInPlaceLink,...
            NodoInPlaceLinkHR3D,DatiElabInPlaceLinkHR,segnoNS_HR,segnoEO_HR,Ndevst_HR,...
            Wdevst_HR,ARRAYdateIPLHR,NuovoZeroIPLHR,NdatiMedia,Ndatidespike,allineato,...
            Tmax,Tmin,datainiIPLHR,ErrInPlaceLinkHR,Corr_Azimuth,margine,FileName);
    else
        [X_IPLHR,Y_IPLHR,Z_IPLHR,Xlocal_IPLHR,Ylocal_IPLHR,Zlocal_IPLHR,...
            HShift_IPLHR,HShift_local_IPLHR,Azimuth_IPLHR,AlfaX_IPLHR,AlfaY_IPLHR,...
            TempDef_IPLHR,Speed_IPLHR,Speed_local_IPLHR,Acceleration_IPLHR,...
            Acceleration_local_IPLHR] = matrici_vuote_IPLHR(FileName);
    end
    % Faccio Pulizia di Variabili
    clear ANGdefIPLHR
    clear ACCdef_TLHR
    clear MAGdef_TLHR
    clear SpeIPLHR
    clear PsIPLHR
    clear tolleranzaMag
    clear DatiElabInPlaceLinkHR
    clear DatiElabInPlaceLink
    clear segnoNS_HR
    clear segnoEO_HR
    clear Ndevst_HR
    clear Wdevst_HR
    clear NuovoZeroIPLHR
    clear allineato
    clear ACCdef_IPL
    clear MAGdef_IPL
    % fine elaborazione In Place Link HR
    
    % --- Analisi Piezo Link ---
    if yesPL == 1
        %     if strcmp(NodoPiezoLink(1,4),'VW kPa') == 1
        %         C = DCalPLTot(:,3);
        %         if C == 0 % Identifica i piezometri di Geosense
        %             for p=1:rPL
        %                 PiezDef(:,p) = PiezDef(:,p)+RIF_PL(p,1)';
        %             end
        %         end
        %     end
        [Livello,PiezDef,PiezDefT,ErrPiezoLink,ARRAYdatePL] = Piezo(PiezDef,PiezDefT,...
            BaroDef,ARRAYdatePL,NuovoZeroPL,NdatiMediaP,Ndatidespike,NodoPiezoLink,ErrPiezoLink,rPL,...
            Anchor_real,marginePiezo,FileName);
    else
        [Livello,PiezDef,PiezDefT] = matrici_vuote_PL(FileName);
    end
    clear NuovoZeroPL
    clear NodoPiezoLink
    clear Anchor_real
    clear DCalPLTot
    
    % --- Analisi Baro Link ---
    if yesBL == 1
        [BaroDef,BaroDefT,ARRAYdateBL,ErrBaroLink] = Baro(BaroDef,BaroDefT,...
            ARRAYdateBL,NuovoZeroBL,NdatiMediaP,Ndatidespike,ErrBaroLink,rBL,...
            marginePiezo,FileName);
    else
        [BaroDef,BaroDefT] = matrici_vuote_BL(FileName);
    end
    clear NuovoZeroBL
    clear NdatiMediaP
    clear marginePiezo
    clear NodoBaroLink
    
    % --- Analisi Load Link ---
    if yesLL == 1
        [DatiLoad,ErrLoadLink,ARRAYdateLL] = loadcell(LoadDef,ErrLoadLink,...
            ARRAYdateLL,NuovoZeroLL,NdatiMedia,Ndatidespike,margine,FileName);
    else
        DatiLoad = [];
    end
    clear LoadDef
    clear NuovoZeroLL
    clear NodoLoadLink
    
    %--- Analisi Klino Link ---
    if yesKL == 1
        [ANGdefKL,TdefKL,ErrKlinoLink,ARRAYdateKL] = Klino(ANGdefKL,TdefKL,...
            ARRAYdateKL,ErrKlinoLink,NuovoZeroKL,NdatiMedia,Ndatidespike,ris_acc,tolleranzaAcc,...
            Tmax,Tmin,datainiKL,rKL,IDcentralina,DTcatena,margine,NodoKlinoLink,FileName);
    end
    clear NuovoZeroKL
    clear ris_acc
    clear tolleranzaAcc
    
    %--- Analisi Klino Link HR ---
    if yesKLHR == 1
        [ANGdefKLHR,TdefKLHR,ErrKlinoLinkHR,ARRAYdateKLHR] = KlinoHR(ANGdefKLHR,...
            TdefKLHR,ARRAYdateKLHR,ErrKlinoLinkHR,NuovoZeroKLHR,NodoKlinoLinkHR,NdatiMedia,...
            Ndatidespike,Tmax,Tmin,datainiKLHR,rKLHR,IDcentralina,DTcatena,margine,FileName);
    end
    clear NuovoZeroKLHR
    clear NodoKlinoLinkHR
    
    %--- Analisi Rain Link ---
    if yesRL == 1
        [Rain,RainCum,ARRAYdateRL,ErrRainLink] = CalcoloRain(Dati_RL,ARRAYdateRL,...
            DatiElabRainLink,NuovoZeroRL,NdatiMedia,ErrRainLink,rRL,margine,Unit,...
            IDcentralina,conn,FileName);
    end
    clear NuovoZeroRL
    clear Dati_RL
    clear DatiElabRainLink
    
    %--- Analisi SPP Link ---
    if yesSPP == 1
        [SPP,ARRAYdateSPP] = CalcoloSPP(DatiSPPLink,FileName);
    else
        SPP = [];
        ARRAYdateSPP = [];
    end
    clear DatiSPPLink
    
    %--- Analisi Snow Link ---
    if yesSL == 1
    [Snow,SnowCum,ARRAYdateSL,ErrSnowLink] = CalcoloSnow(DatiSnowLink,...
    DatiElabSnowLink,NuovoZeroSL,NdatiMedia,ErrSnowLink,rSL,margine,Unit,...
    IDcentralina,conn,FileName);
    else
        Snow = [];
        SnowCum = [];
        ARRAYdateSL = [];
        ErrSnowLink = [];
    end
    clear DatiSnowLink
    
    %--- Analisi Therm Link ---
    if yesThL == 1
        [Dati_ThL,ARRAYdateThL,ErrThermLink] = Therm(Dati_ThL,ARRAYdateThL,...
            NuovoZeroThL,ErrThermLink,NdatiMedia,margine,Tmax,Tmin,datainiThL,...
            IDcentralina,DTcatena,FileName);
    end
    clear NuovoZeroThL
    clear NodoThermLink
    
    %--- Analisi PT100 Link ---
    if yesPT100 == 1
        [Dati_PT100,ARRAYdatePT100,ErrPT100Link] = PT100(Dati_PT100,...
            ARRAYdatePT100,NuovoZeroPT100,ErrPT100Link,NdatiMedia,margine,Tmax,Tmin,...
            datainiPT100,IDcentralina,DTcatena,FileName);
    end
    clear NuovoZeroPT100
    clear NdatiMedia
    clear margine
    clear NodoPT100Link
    
    %--- Analisi Weir Link ---
    if yesWL == 1
        therm = cell2mat(NodoWeirLink(:,6));
        for ii = 1:rWL
            if therm(ii,1)==1 % è presente il termometro
                Dati_WL(:,ii) = Dati_WL(:,ii)./(1-0.0002*TdefWL(:,ii)); % vale solo per correzione LINEARE
            end
        end
        % Calcolo differenziali
        NomeFile = ['' IDcentralina '-' DTcatena '-RifWL.csv'];
        if NuovoZeroWL == 0 % prima elaborazione
            csvwrite(NomeFile,Dati_WL(1,:));
            Dati_WL = Dati_WL(1,:) - Dati_WL;
        else % Ci sono già dei dati elaborati
            RIF = csvread(NomeFile);
            Dati_WL = RIF - Dati_WL;
        end
        H = Dati_WL/1000;
        [rH,cH] = size(H);
        for i=1:rH
            for j=1:cH
                if H(i,j)<0
                    H(i,j)=0; % Per la radice quadrata mi servono numeri positivi o nulli
                end
            end
        end
        h = H.^1.5; % passo in metri
        b = larghezza + 2*base/altezza;
        Portata = 1.86*b*h*1000; % litri/secondo
    else
        Portata = [];
    end
    
    if strcmp(IDcentralina,'ID0070') == 1 && strcmp(DTcatenaRif,'DT0111 IPI') == 1
        DTcatena = 'DT0111 IPI';
    elseif strcmp(IDcentralina,'ID0071') == 1 && strcmp(DTcatenaRif,'DT0112 IPI') == 1
        DTcatena = 'DT0112 IPI';
    elseif strcmp(IDcentralina,'ID0072') == 1 && strcmp(DTcatenaRif,'DT0113 IPI') == 1
        DTcatena = 'DT0113 IPI';
    elseif strcmp(IDcentralina,'ID0073') == 1 && strcmp(DTcatenaRif,'DT0114 IPI') == 1
        DTcatena = 'DT0114 IPI';
    end
    
    %% Scrittura Database
    database_write(catena,IDcentralina,DTcatena,...
        X,Y,Z,Xlocal,Ylocal,Zlocal,HShift,HShift_local,AlfaX,AlfaY,Azimuth,TempDef_TL,...
        Speed_local,Speed,Acceleration,Acceleration_local,ErrTiltLink,ARRAYdateTL,...
        X_HD,Y_HD,Z_HD,Xlocal_HD,Ylocal_HD,Zlocal_HD,HShift_HD,HShift_local_HD,...
        AlfaX_HD,AlfaY_HD,Azimuth_HD,tempHD,Speed_HD,Speed_local_HD,Acceleration_HD,...
        Acceleration_local_HD,ErrTiltLinkHD,ARRAYdateHD,ACCdefRisHD,MAGdefRisHD,...
        X_HDVR,Y_HDVR,Z_HDVR,Xlocal_HDVR,Ylocal_HDVR,Zlocal_HDVR,HShift_HDVR,HShift_local_HDVR,...
        AlfaX_HDVR,AlfaY_HDVR,Azimuth_HDVR,tempHDVR,Speed_HDVR,Speed_local_HDVR,Acceleration_HDVR,...
        Acceleration_local_HDVR,ErrTiltLinkHDVR,ARRAYdateHDVR,ACCdefRisHDVR,MAGdefRisHDVR,...
        Y_TLH,Z_TLH,Xlocal_TLH,Ylocal_TLH,Zlocal_TLH,AlfaX_TLH,AlfaY_TLH,TempDef_TLH,Speed_TLH,Traversine,...
        Speed_local_TLH,Acceleration_TLH,Acceleration_local_TLH,ErrTiltLinkH,ARRAYdateTLH,...
        X_IPL,Y_IPL,Z_IPL,Xlocal_IPL,Ylocal_IPL,Zlocal_IPL,HShift_IPL,...
        HShift_local_IPL,Azimuth_IPL,TempDef_IPL,Speed_local_IPL,Speed_IPL,...
        Acceleration_local_IPL,Acceleration_IPL,AlfaX_IPL,AlfaY_IPL,ErrInPlaceLink,ARRAYdateIPL,...
        X_HR,Y_HR,Z_HR,Xlocal_HR,Ylocal_HR,Zlocal_HR,HShift_HR,HShift_local_HR,...
        AlfaX_HR,AlfaY_HR,Azimuth_HR,TempDef_TLHR,Speed_local_HR,Speed_HR,...
        Acceleration_HR,Acceleration_local_HR,ErrTiltLinkHR,ARRAYdateTLHR,...
        Y_TLHRH,Z_TLHRH,Ylocal_TLHRH,Zlocal_TLHRH,AlfaX_TLHRH,AlfaY_TLHRH,TempDef_TLHRH,Speed_TLHRH,...
        Speed_local_TLHRH,Acceleration_TLHRH,Acceleration_local_TLHRH,ErrTiltLinkHRH,ARRAYdateTLHRH,...
        X_IPLHR,Y_IPLHR,Z_IPLHR,Xlocal_IPLHR,Ylocal_IPLHR,Zlocal_IPLHR,HShift_IPLHR,...
        HShift_local_IPLHR,AlfaX_IPLHR,AlfaY_IPLHR,TempDef_IPLHR,Azimuth_IPLHR,Speed_IPLHR,...
        Speed_local_IPLHR,Acceleration_IPLHR,Acceleration_local_IPLHR,ErrInPlaceLinkHR,ARRAYdateIPLHR,...
        Livello,PiezDef,PiezDefT,ErrPiezoLink,ARRAYdatePL,BaroDef,BaroDefT,ErrBaroLink,ARRAYdateBL,...
        DatiLoad,ErrLoadLink,ARRAYdateLL,Rain,RainCum,ErrRainLink,ARRAYdateRL,SPP,ARRAYdateSPP,...
        Snow,SnowCum,ErrSnowLink,ARRAYdateSL,ANGdefKL,TdefKL,ErrKlinoLink,ARRAYdateKL,NodoKlinoLink,...
        ANGdefKLHR,TdefKLHR,ErrKlinoLinkHR,ARRAYdateKLHR,...
        Dati_ThL,ErrThermLink,ARRAYdateThL,Dati_PT100,ErrPT100Link,ARRAYdatePT100,...
        Dati_PE,ErrPendulum,ARRAYdatePE,Dati_WL,Portata,TdefWL,ErrWeirLink,NodoWeirLink,ARRAYdateWL,...
        conn,FileName);
     
    %% Parte di lancio SW complementari
      [yesATD,yesRSN,yesMusa] = SWSearch(idTool,conn,FileName);
    try
        if yesATD == 1
            rilancio = ['/usr/local/matlab_func/run_ATD_lnx.sh /usr/local/MATLAB/MATLAB_Runtime/v93 ' IDcentralina ' ' DTcatena ''];
            status = system(rilancio);
            fileID = fopen(FileName,'a');
            fmt = '%s \r';
            text = 'ATD relaunch operation executed with success';
            fprintf(fileID,fmt,text);
            fprintf(fileID,fmt,status);
            fclose(fileID);
        end
        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';
            text = 'RSN relaunch operation executed with success';
            fprintf(fileID,fmt,text);
            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';
            text = 'Musa relaunch operation executed with success';
            fprintf(fileID,fmt,text);
            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);
        fileID = fopen(FileName,'a');
        fmt = '%s \r';
        text = 'relaunch operation FAILED';
        fprintf(fileID,fmt,text);
        fclose(fileID);
    end
end

%% Parte di allertamento

if strcmp(Unit,'Nesa evolution') == 0 && strcmp(Unit,'SISGEO WR-GW-868')==0
    % ---Soglie imposte dal cliente---
    threshold(yesHD,ARRAYdateHD,Xlocal_HD,Ylocal_HD,X_HD,Y_HD,HShift_local_HD,...
        HShift_HD,NodoTiltLinkHD,unitID,IDcentralina,DTcatena,conn,FileName);
    
    % ---Soglie da codice Matlab---
    alert_Levels(ARRAYdateIPL,HShift_local_IPL,Zlocal_IPL,NodoInPlaceLink,ARRAYdateRL,...
        ARRAYdateKL,ANGdefKL,NodoKlinoLink,IDcentralina,DTcatena,conn,FileName);
end

% Determino gli utenti da utilizzare in tutte le funzioni successive
[Mail,Users_Report,activeEN,siteID,NomeSito] = 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);

if strcmp(Unit,'Nesa evolution') == 0 && strcmp(Unit,'SISGEO WR-GW-868')==0
    
    % Calcolo con Fukuzono - Inverso della Velocità
    Users_Alert = [];
    Users_SMS = [];
    ini_CoV = 1;
    activeIT = 1;
    sms = 0;
    Time_Of_Failure(IDcentralina,DTcatena,date,ARRAYdateTL,ARRAYdateIPL,...
        yesTL,yesTLHR3D,yesIPL,yesIPLHR3D,rTL,rIPL,NodoTiltLink,NodoTiltLinkHR,...
        NodoTiltLinkHR3D,NodoInPlaceLink,NodoInPlaceLinkHR,NodoInPlaceLinkHR3D,...
        Mail,Users_Alert,Users_SMS,ini_CoV,activeIT,activeEN,sms,siteID,conn,FileName);

    % Attivazione di sistemi di allertamento
    Siren(alarms,yesKL,yesKLHR,yesTL,yesIPL,datainiKL,tempoiniKL,datainiKLHR,tempoiniKLHR,...
        datainiTL,tempoiniTL,datainiIPL,tempoiniIPL,ARRAYdateKL,ARRAYdateKLHR,ARRAYdateTL,ARRAYdateIPL,...
        conn,date,time,FileName);
end

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

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

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

% Controllo della data di scadenza per una determinata SIM
checkGIS(siteID,NomeSito,conn,FileName);

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

close(conn)

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

RC = 0;
end
