%This program plots the rotation axis / angle pair of the rotation of a
%crystal away from a specified initial orientation with respect to the
%sample coordinate system. 


while(1)
   AAfile = input('Image data from file (*.txt): ','s');
   fidIn = fopen(AAfile, 'rt');
   if fidIn ~= -1		%if filename exists, the loop ends. Otherwise input is required once more.
      break;
   end
    disp('Warning: the file does not exist in the indicated directory! Please input again.');
end


% Input orientation data
F1 = fscanf(fidIn,'%g', [6,inf]);
F1 = F1';

stepsize = 2; %input('EBSD scan stepsize in micrometers: ');

rowcount = size(F1,1);
 

C = zeros((F1(rowcount,1)/stepsize),(F1(rowcount,2)/stepsize),3);


C2 = zeros((F1(rowcount,1)/stepsize),(F1(rowcount,2)/stepsize));

int32 x; 
int32 y;
for n = 1: rowcount
    y = (F1(n,1)/stepsize) + 1;
    x = (F1(n,2)/stepsize) + 1;
    C(x,y,1) = abs(F1(n,3));
    C(x,y,2) = abs(F1(n,4));
    C(x,y,3) = abs(F1(n,5));
    
   

end

        for s = 2:rowcount
            y = (F1(s,1)/stepsize) + 1;
            x = (F1(s,2)/stepsize) + 1;

            if (F1(s,1)-F1(s-1,1)) == stepsize
                if abs((F1(s,6)-F1(s-1,6))) > 0.25
                C(x,y,1:3) = 0.0;
                end
            end
        end

clims = input('Enter grayscale lower and upper limits of rotations in radians, e.g [0 0.47]. :');

image(C); 
axis equal;


for h = 1: rowcount
    y = (F1(h,1)/stepsize) + 1;
    x = (F1(h,2)/stepsize) + 1;
    
    
    C2(x,y) = (F1(h,6));
     
end

figure 


imagesc(C2,clims); 
%colormap(red)
colorbar
axis equal; 


%figure 


%meshsc(C2,clims); 
%grid off;


