function [ax,ay,az] = check_RL(ax,ay,az,rRL,IDcentralina,DTcatena,FileName)

fileID = fopen(FileName,'a');
fmt = '%s \r';

incliX = ax;
incliY = ay;
incliZ = az;

switchXY = zeros(rRL,1);
switchYZ = zeros(rRL,1);
switch_signXY = zeros(rRL,1);

instAx = ax(:,1);
instAy = ay(:,1);
    
for i = 1:rRL
    if instAy(i,1) < 0 && abs(instAx(i,1)) < sin(pi/6) % il nodo è sottosopra, devo cambiare i segni di X e Y
       switch_signXY(i,1) = 1;
    end
    if instAx(i,1) > sin(pi/6) || instAx(i,1) < sin(-pi/6) % uso Z anzichè Y
        if instAx(i,1) > sin(pi/4) 
            switchXY(i,1) = 1;
        elseif instAx(i,1) < sin(-pi/4) % uso X anzichè Y
               switchXY(i,1) = -1;
        else
            switchYZ(i,1) = 1;
        end
    end
    if switch_signXY(i,1) == 1
       ax(i,:) = -incliX(i,:);
       ay(i,:) = -incliY(i,:);
       text = ['ax and ay signs changed during the execution of check_RL function of Radial Link node number ' num2str(i)];
       fprintf(fileID,fmt,text);
    end
    if switchXY(i,1) == 1
       ax(i,:) = incliY(i,:);
       ay(i,:) = incliX(i,:);
       text = ['ax and ay switched during the execution of check_RL function of Radial Link node number ' num2str(i)];
       fprintf(fileID,fmt,text);
    elseif switchXY(i,1) == -1
        ax(i,:) = -incliY(i,:);
        ay(i,:) = -incliX(i,:);
        text = ['ax and ay switched and signs changed during the execution of check_RL function of Radial Link node number ' num2str(i)];
       fprintf(fileID,fmt,text);
    end
    if switchYZ(i,1) == 1
       az(i,:) = -incliY(i,:);
       ay(i,:) = -incliZ(i,:);
       text = ['ay and az switched and signs changed during the execution of check_RL function of Radial Link node number ' num2str(i)];
       fprintf(fileID,fmt,text);
    end
end

nameXY = strcat([IDcentralina,'-',DTcatena,'-','switchXY']);
name_switchXY = strcat([IDcentralina,'-',DTcatena,'-','switch_signXY']);
nameYZ = strcat([IDcentralina,'-',DTcatena,'-','switchYZ']);
save(nameXY,'switchXY')
save(nameYZ,'switchYZ')
save(name_switchXY,'switch_signXY')


text = 'check_RL function executed correctly';
fprintf(fileID,fmt,text);
fclose(fileID);
end

