%% Import Script for EBSD Data
%
% This script was automatically created by the import wizard. You should
% run the whoole script or parts of it in order to import your data. There
% is no problem in making any changes to this script.

close all
%% Specify Crystal and Specimen Symmetries

% crystal symmetry
CS = crystalSymmetry('m-3m', [3.52 3.52 3.52], 'mineral', 'Nickel', 'color', 'light blue');

% plotting convention
setMTEXpref('xAxisDirection','north');
setMTEXpref('zAxisDirection','outOfPlane');

%% Specify File Names

% path to files
pname = 'data_OIM\';

% sample name
%sname = 'sample_2_scan1';
%sname = 'sample2s_scan3';
%sname = 'sample_9_scan2';
sname = 'sample10_scan3';

% which files to be imported
fname = [pname sname '.osc'];

%% Specify Miller Indice

h = { ...
  Miller(1,1,1,CS),...
  Miller(2,0,0,CS),...
  Miller(2,2,0,CS),...
  };

%% Import the Data

% create an EBSD variable containing the data
ebsd = loadEBSD(fname,CS,'interface','osc',...
  'convertSpatial2EulerReferenceFrame');

%% Plot the raw data
% overwrite the CS imported from the osc-format
ebsd.CS = CS

% this defines a color mapping
oM = ipdfHSVOrientationMapping(ebsd);
figure;
plot(oM);

% compute the colors
oM.inversePoleFigureDirection = zvector;
color = oM.orientation2color(ebsd.orientations);

% and plot them
figure;
plot(ebsd,color);
figure;
plotPDF(ebsd.orientations,h,'points',1000)

%% compute odf

% filter for confidence_index > 0.1
ebsd_filtered = ebsd(ebsd.confidenceindex>0.1)
plot(ebsd_filtered,oM.orientation2color(ebsd_filtered.orientations));

% compute halfwidth 
psi = deLaValeePoussinKernel(400);

% compute the ODF with the kernel psi
odf = calcODF(ebsd.orientations,'kernel',psi)

figure;
plotPDF(odf,h,'projection','stereo','antipodal');
CLim(gcm,'equal');
colorbar;
annotate([xvector,yvector,zvector],'label',{'Xs','Ys','Zs'});

%% Rotate reference frame

if (sname == 'sample_2_scan1')
    rot = rotation('Euler',0*degree,70*degree,90*degree)
elseif (sname == 'sample2s_scan3')
    rot = rotation('axis',zvector,'angle',0*degree) 
elseif (sname == 'sample_9_scan2') 
    rot = rotation('Euler',270*degree,90*degree,0*degree) 
elseif (sname == 'sample10_scan3')
    rot = rotation('axis',zvector,'angle',0*degree) 
end;    
odf = rotate(odf,rot);

figure;
plotPDF(odf,h,'projection','stereo','antipodal');
CLim(gcm,'equal');
colorbar;
annotate([xvector,-yvector,zvector],'label',{'P','T','B'});

