%% Funzione che definisce i parametri specifici di elaborazione

function [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)

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

comando = ['select avg, despike from software where tool_id like ''' idTool ''' '];
Dati = fetch(conn,comando);

NdatiMedia = cell2mat(Dati(1));
Ndatidespike = cell2mat(Dati(2));

%% MEMS
if yesTL == 1 || yesTLH == 1 || yesKL == 1 || yesIPL == 1 || yesHD == 1 || yesHDVR == 1
   comando = ['select mems, toll_Acc, num_Ds, win_Ds from software where tool_id like ''' idTool ''' '];
   Dati = fetch(conn,comando);
   MEMS = cell2mat(Dati(1)); 
   tolleranzaAcc = cell2mat(Dati(2)); 
   Ndevst = cell2mat(Dati(3)); 
   Wdevst = cell2mat(Dati(4)); 
   if yesTL == 1 || yesIPL == 1
       comando = ['select elab, toll_Mag, ns_Sign, eo_Sign from software where tool_id like ''' idTool ''' '];
       Dati = fetch(conn,comando);
       elab = cell2mat(Dati(1));
       if elab == 1
           triax = 1;
           biax = 0;
           struct = 0;
       elseif elab == 2
           triax = 0;
           biax = 1;
           struct = 0;
       elseif elab == 3
           triax = 0;
           biax = 0;
           struct = 1;
       end
       tolleranzaMag = cell2mat(Dati(2)); 
       segnoNS = cell2mat(Dati(3)); 
       segnoEO = cell2mat(Dati(4));
   elseif yesHD == 1 || yesHDVR == 1
       comando = ['select toll_Mag from software where tool_id like ''' idTool ''' '];
       Dati = fetch(conn,comando);
       tolleranzaMag = cell2mat(Dati(1));
       segnoNS = [];
       segnoEO = [];
       triax = [];
       biax = [];
       struct = [];
   else
       segnoNS = [];
       segnoEO = [];
       triax = [];
       biax = [];
       struct = [];
       tolleranzaMag = [];
   end
else
    MEMS = [];
    tolleranzaAcc = [];
    tolleranzaMag = [];
    Ndevst = [];
    Wdevst = [];
    triax = [];
    biax = [];
    struct = [];
    segnoNS = [];
    segnoEO = [];
end
       
%% Elettrolitici   
if yesTLHR == 1 || yesTLHRH == 1 || yesIPLHR == 1
    comando = ['select num_Ds_HR, win_Ds_HR, align from software where tool_id like ''' idTool ''' '];
    Dati = fetch(conn,comando);
    Ndevst_HR = cell2mat(Dati(1));
    Wdevst_HR = cell2mat(Dati(2));
    allineato = cell2mat(Dati(3));
    if yesTLHR == 1 || yesTLHRH == 1
        comando = ['select ns_Sign_HR, eo_Sign_HR, align from software where tool_id like ''' idTool ''' '];
        Dati = fetch(conn,comando);
        segnoNS_HR = cell2mat(Dati(1));
        segnoEO_HR = cell2mat(Dati(2));
        allineato = cell2mat(Dati(3));
    else
        segnoNS_HR = [];
        segnoEO_HR = [];
    end
else
   segnoNS_HR = [];
   segnoEO_HR = [];
   Ndevst_HR = [];
   Wdevst_HR = [];
   allineato = [];
end

%% Termometri
if yesTL == 1 || yesTLH == 1 || yesKL == 1 || yesIPL == 1 || yesTLHR == 1 || ...
        yesTLHRH == 1 || yesIPLHR == 1 || yesKLHR == 1 || yesThL == 1 || yesPT100 == 1 || ...
        yesWL == 1 || yesHD == 1 || yesHDVR == 1
    comando = ['select max_temp, min_temp from software where tool_id like ''' idTool ''' '];
    Dati = fetch(conn,comando);
    Tmax = cell2mat(Dati(1));
    Tmin = cell2mat(Dati(2));
else
    Tmax = [];
    Tmin = [];
end

if yesIPL == 1 || yesIPLHR == 1 || yesHD == 1 || yesHDVR == 1
   comando = ['select azimut_guida_a from software where tool_id like ''' idTool ''' '];
    Dati = fetch(conn,comando);
    Corr_Azimuth = cell2mat(Dati(1));
else
    Corr_Azimuth = [];
end

%% Piezo Link
if yesPL == 1
    comando = ['select avg_piezo from software where tool_id like ''' idTool ''' '];
    Dati = fetch(conn,comando);
    NdatiMediaP = cell2mat(Dati(1));
else
    NdatiMediaP = [];
end

%% Weir Link
if yesWL == 1
    comando = ['select base, altezza, larghezza from software where tool_id like ''' idTool ''' '];
    Dati = fetch(conn,comando);
    base = cell2mat(Dati(1));
    altezza = cell2mat(Dati(1));
    larghezza = cell2mat(Dati(1));
else
    base = [];
    altezza = [];
    larghezza = [];
end
   
%% Calcolo sghembo binari
if yesTLH == 1
    comando = ['select rail_width from software where tool_id like ''' idTool ''' '];
    Dati = fetch(conn,comando);
    Traversine = cell2mat(Dati(1));
else
    Traversine = [];
end

%% Seleziono profondità "reale" dell'ancora
comando = ['select anchor_real from tools where unit_id like ''' ...
       unitID ''' and name like ''' DTcatena ''' '];
curs = exec(conn,comando);
curs = fetch(curs);
Anchor_real = cell2mat(curs.Data);
if ischar(Anchor_real) == 1
    Anchor_real = 0;
end

text = 'Chain parameters defined correctly. Parametri_Installazione function closed';
fprintf(fileID,fmt,text);
fclose(fileID);
end