function [DatiStress,DatiNTCStress,ErrStressMeter] = conv_grezziSM(Stress,NTCStress,...
    DatiStressMeter_Rif,DatiNTCStressMeter_Rif,DCalSMTot,NodoStressMeter,ErrStressMeter,...
    SM_NTC,rSM,FileName)

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

[rA,cA] = size(Stress);
DatiStress = zeros(rA,cA);
DatiNTCStress = zeros(rA,cA);
for e = 1:rSM
    if strcmp(NodoStressMeter(e,3),'Hz')
        GF = cell2mat(DCalSMTot(e,1));
        % Spacchetto i parametri di calibrazione
        digit = (Stress(:,e).^2)*0.001; % Freq. ^2 *E-3
        digit_Rif = (DatiStressMeter_Rif(1,e).^2)*0.001; % Freq. ^2 *E-3
        if SM_NTC(e,1)==0 % non è presente il termometro
            DatiStress(:,e) = (digit(:,e)-digit_Rif(1,e))*GF; % conversione in Mpa
        elseif SM_NTC(e,1)==1 % è presente il termometro - correzione termica
            A = 1.4051*10^(-3);
            B = 2.369*10^(-4);
            C = 1.019*10^(-7);
            R = NTCStress(:,e);
            [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 ErrStressMeter(n,ii) == 0
                        ErrStressMeter(n,ii) = 0.5;
                    end
                end
            end
            L = reallog(R);
            DatiNTCStress(:,e) = (1./(A + B*L + C*(L.^3))) -273.2; % conversione in gradi centigradi
            L_Rif = reallog(DatiNTCStressMeter_Rif(1,e));
            DatiNTCStressMeter_Rif(1,e) = (1./(A + B*L_Rif + C*(L_Rif.^3))) -273.2; % conversione in gradi centigradi
            [rT,~] = size(DatiNTCStress);
            for T = 1:rT
                if DatiNTCStress(T,e) > 70 || DatiNTCStress(T,e) < -20
                    if T == 1
                        DatiNTCStress(T,e) = DatiNTCStress(T+1,e); 
                    else
                        DatiNTCStress(T,e) = DatiNTCStress(T-1,e);
                    end
                    if ErrStressMeter(T,e) == 0
                        ErrStressMeter(T,e) = 0.5;
                    end
                end
            end
            DatiStress(:,e) = (digit-digit_Rif)*GF+(DatiNTCStress(:,e)-DatiNTCStressMeter_Rif(1,e))*2*GF; % conversione in mm
        end
    end
end

text = 'Calibrations of Stress Meter defined correctly. conv_grezziSM function closed';
fprintf(fileID,fmt,text);
fclose(fileID);
end