% Funzione che converte i dati grezzi dei Weir Link in dati di altezza d'acqua

function [lev_WL,TdefWL,ErrWeirLink] = conv_grezziWL(lev_WL,temp_WL,rWL,...
    DCalWLTot,Tmax,Tmin,NodoWeirLink,ErrWeirLink,datainiWL,TimeWL,...
    IDcentralina,DTcatena,FileName)

text = 'conv_grezziWL function started';
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);
[rT,cT] = size(temp_WL);
TdefWL = zeros(rT,cT);

if strcmp(NodoWeirLink(1,4),'Hz')
    % Conversione Weir
    A = DCalWLTot(:,1);
    B = DCalWLTot(:,2);
    C = DCalWLTot(:,3);
    % Conversione Temperature
    AT = 1.4051*10^(-3);
    BT = 2.369*10^(-4);
    CT = 1.019*10^(-7);
    Therm = cell2mat(NodoWeirLink(:,6));
    for ii = 1:rWL
        digit = (lev_WL(:,ii).^2)/1000;
        lev_WL(:,ii) = A(ii)*digit.^2+B(ii)*digit+C(ii);    
        if Therm(ii,1)==1 % č presente il termometro
            R = temp_WL(:,ii);
            [rN,~] = size(R);
            for n = 1:rN
                if R(n) <= 0
                    if n > 1
                        R(n) = R(n-1);
                    else
                        R(n) = 5000;
                    end
                    if ErrWeirLink(n,ii) == 0
                        ErrWeirLink(n,ii) = 0.5;
                    end
                end
            end
            L = reallog(R);
            TdefWL(:,ii) = (1./(AT + BT*L + CT*(L.^3))) -273.2; % conversione in gradi centigradi
        end
    end
end
FileTemperature = ['' IDcentralina '-' DTcatena '-WL-Therm.csv'];
if isfile(FileTemperature) == 1
    DatiRaw = csvread(FileTemperature);
    [rDR,cDR] = size(DatiRaw);
else
    rDR = 1;
    cDR = 1;
end
textT = 'There are not correction for Weir Link - Temperature filter!';
cont2 = 1;
for ii = 1:rWL
    if Therm(ii,1) == 1
        for T = 1:rT                
            if TdefWL(T,ii) > Tmax || TdefWL(T,ii) < Tmin
                if T == 1
                    if isfile(FileTemperature) == 1
                        DatiRaw(:,1) = DatiRaw(:,1) + 730000;
                        RawDate = find(DatiRaw(:,1)<=datenum(datainiWL));
                        if isempty(RawDate) == 1
                            cc = 2;
                            while cc <= rT
                                if TdefWL(cc,ii) > Tmax || TdefWL(cc,ii) < Tmin
                                    cc = cc+1;
                                else
                                    break
                                end
                            end
                            TdefWL(T,ii) = TdefWL(cc,ii);
                        else
                            if isnan(DatiRaw(ii+1,RawDate(end))) == 0
                                TdefWL(T,ii) = cellstr(num2str(DatiRaw(RawDate(end),ii+1)));
                                if ErrWeirLink(ii,T) == 0
                                    ErrWeirLink(ii,T) = 0.5;
                                end
                                wardat = 'Temperature data of Weir Link nodes corrected using Raw Data of reference Csv file.';
                                fprintf(fileID,fmt,wardat);
                            else
                                cc = 2;
                                while cc <= rT
                                    if TdefWL(cc,ii) > Tmax || TdefWL(cc,ii) < Tmin
                                        cc = cc+1;
                                    else
                                        break
                                    end
                                end
                                TdefWL(T,ii) = TdefWL(cc,ii);
                            end
                        end
                    else
                        cc = 2;
                        while cc <= rT
                            if TdefWL(cc,ii) > Tmax || TdefWL(cc,ii) < Tmin
                                cc = cc+1;
                            else
                                break
                            end
                        end
                        TdefWL(T,ii) = TdefWL(cc,ii);
                    end
                else
                    TdefWL(T,ii) = TdefWL(T-1,ii);
                    ErrWeirLink(ii,T) = 0.5;
                end
                textT = ['' num2str(cont2) ' correction executed for Weir Link - Temperature filter!'];
            end
        end
    end
end
if  rDR~=1 && cDR~=1 && isempty(DatiRaw) == 0
    RawDate1 = find(DatiRaw(:,1)<=TimeWL(1));
    if isempty(RawDate1) == 1
        RawDate2 = 1;
    elseif RawDate1(end) == rDR
        RawDate2 = find(TimeWL(:,1)>DatiRaw(end,1));
    else
        RawDate2 = find(TimeWL(:,1)>DatiRaw(RawDate1(end)+1,1));
    end
else
    RawDate1 = [];
    RawDate2 = 1;
end
if isempty(RawDate1) == 0 && isempty(RawDate2) == 0
    Dati = [DatiRaw(1:RawDate1(end),:);TimeWL TdefWL(:,RawDate2(1):end)];
elseif isempty(RawDate1) == 1 && isempty(RawDate2) == 0
    Dati = [TimeWL TdefWL];
else
    Dati = DatiRaw;
end
% Elimino appoggio pių vecchio di un mese
RawDate3 = find(Dati(:,1)<now-30);
if isempty(RawDate3) == 0
    [rDati,~] = size(Dati);
    if RawDate3(end) == rDati
    else
        Dati = Dati(RawDate3(end)+1:end,:);
    end
end
if isfile(FileTemperature) == 1
    delete(FileTemperature);
end
Dati(:,1) = Dati(:,1) - 730000;
csvwrite(FileTemperature,Dati);
fprintf(fileID,fmt,textT);

text = 'Raw Data of Weir Link converted into physical units correctly. conv_grezziWL function ended';
fprintf(fileID,fmt,text);
fclose(fileID);

end